Search in sources :

Example 66 with Formula

use of pcgen.base.formula.Formula in project pcgen by PCGen.

the class Equipment method getSelectCorrectedAssociationCount.

public int getSelectCorrectedAssociationCount(CDOMObject obj) {
    Formula f = obj.getSafe(FormulaKey.SELECT);
    //TODO Null here is probably a problem for the PC :/
    int select = f.resolve(this, true, null, "").intValue();
    return assocSupt.getAssocCount(obj, AssociationListKey.CHOICES) / select;
}
Also used : Formula(pcgen.base.formula.Formula)

Example 67 with Formula

use of pcgen.base.formula.Formula in project pcgen by PCGen.

the class BonusManager method getStringListFromBonus.

public List<BonusPair> getStringListFromBonus(BonusObj bo) {
    Object creatorObj = getSourceObject(bo);
    List<String> associatedList;
    CDOMObject anObj = null;
    if (creatorObj instanceof CDOMObject) {
        anObj = (CDOMObject) creatorObj;
        associatedList = pc.getConsolidatedAssociationList(anObj);
        if (associatedList == null || associatedList.isEmpty()) {
            associatedList = NO_ASSOC_LIST;
        }
    } else {
        associatedList = NO_ASSOC_LIST;
    }
    List<BonusPair> bonusList = new ArrayList<>();
    // Must use getBonusName because it contains the unaltered bonusType
    String bonusName = bo.getBonusName();
    String[] bonusInfoArray = bo.getBonusInfo().split(",");
    String bonusType = bo.getTypeString();
    for (String assoc : associatedList) {
        String replacedName;
        if (bonusName.contains(VALUE_TOKEN_REPLACEMENT)) {
            replacedName = bonusName.replaceAll(VALUE_TOKEN_PATTERN, assoc);
        } else {
            replacedName = bonusName;
        }
        List<String> replacedInfoList = new ArrayList<>(4);
        for (String bonusInfo : bonusInfoArray) {
            if (bonusInfo.contains(VALUE_TOKEN_REPLACEMENT)) {
                replacedInfoList.add(bonusInfo.replaceAll(VALUE_TOKEN_PATTERN, assoc));
            } else if (bonusInfo.contains(VAR_TOKEN_REPLACEMENT)) {
                replacedInfoList.add(bonusName.replaceAll(VAR_TOKEN_PATTERN, assoc));
            } else if (bonusInfo.equals(LIST_TOKEN_REPLACEMENT)) {
                replacedInfoList.add(assoc);
            } else {
                replacedInfoList.add(bonusInfo);
            }
        }
        Formula newFormula;
        if (bo.isValueStatic()) {
            newFormula = bo.getFormula();
        } else {
            String value = bo.getValue();
            // A %LIST substitution also needs to be done in the val
            // section
            int listIndex = value.indexOf(VALUE_TOKEN_REPLACEMENT);
            String thisValue = value;
            if (listIndex >= 0) {
                thisValue = value.replaceAll(VALUE_TOKEN_PATTERN, assoc);
            }
            //Need to protect against a selection not being made with a %LIST
            if (thisValue.isEmpty()) {
                thisValue = "0";
            }
            newFormula = FormulaFactory.getFormulaFor(thisValue);
        }
        for (String replacedInfo : replacedInfoList) {
            StringBuilder sb = new StringBuilder(100);
            sb.append(replacedName).append('.').append(replacedInfo);
            if (bo.hasTypeString()) {
                sb.append(':').append(bonusType);
            }
            bonusList.add(new BonusPair(sb.toString(), newFormula, creatorObj));
        }
    }
    return bonusList;
}
Also used : Formula(pcgen.base.formula.Formula) CDOMObject(pcgen.cdom.base.CDOMObject) ArrayList(java.util.ArrayList) CDOMObject(pcgen.cdom.base.CDOMObject) MissingObject(pcgen.core.bonus.util.MissingObject) BonusPair(pcgen.core.bonus.BonusPair)

Example 68 with Formula

use of pcgen.base.formula.Formula in project pcgen by PCGen.

the class AbstractRestrictedSpellPrimitive method allow.

public boolean allow(PlayerCharacter pc, int level, String source, Spell spell, CDOMList<Spell> optionalList) {
    if (restriction != null) {
        Formula maxLevel = restriction.maxLevel;
        if (maxLevel != null && (level > maxLevel.resolve(pc, source).intValue())) {
            return false;
        }
        Formula minLevel = restriction.minLevel;
        if (minLevel != null && (level < minLevel.resolve(pc, source).intValue())) {
            return false;
        }
        if (restriction.knownRequired != null) {
            String defaultbook = Globals.getDefaultSpellBook();
            boolean known = restriction.knownRequired.booleanValue();
            boolean found = false;
            for (PCClass cl : pc.getClassSet()) {
                if (optionalList != null) {
                    /*
						 * This may not be a precise test of intent, but given
						 * the weirdness we have on lists and the use of
						 * SPELLLIST tag in data to share lists between classes,
						 * this is probably the closest we can get
						 */
                    if (!pc.hasSpellList(cl, optionalList)) {
                        continue;
                    }
                }
                List<CharacterSpell> csl = pc.getCharacterSpells(cl, spell, defaultbook, -1);
                if (csl != null && !csl.isEmpty()) {
                    /*
						 * Going to assume here that the level doesn't need to
						 * be rechecked... ?? - thpr Feb 26, 08
						 */
                    found = true;
                }
            }
            if (found != known) {
                return false;
            }
        }
    }
    return true;
}
Also used : Formula(pcgen.base.formula.Formula) CharacterSpell(pcgen.core.character.CharacterSpell) PCClass(pcgen.core.PCClass)

Example 69 with Formula

use of pcgen.base.formula.Formula in project pcgen by PCGen.

the class AbstractRestrictedSpellPrimitive method getRestriction.

private Restriction getRestriction(String restrString) {
    StringTokenizer restr = new StringTokenizer(restrString, ";");
    Formula levelMax = null;
    Formula levelMin = null;
    Boolean known = null;
    while (restr.hasMoreTokens()) {
        String tok = restr.nextToken();
        if (tok.startsWith("LEVELMAX=")) {
            levelMax = FormulaFactory.getFormulaFor(tok.substring(9));
            if (!levelMax.isValid()) {
                Logging.errorPrint("LEVELMAX Formula in " + getTokenName() + " was not valid: " + levelMax.toString());
                return null;
            }
        } else if (tok.startsWith("LEVELMIN=")) {
            levelMin = FormulaFactory.getFormulaFor(tok.substring(9));
            if (!levelMin.isValid()) {
                Logging.errorPrint("LEVELMIN Formula in " + getTokenName() + " was not valid: " + levelMin.toString());
                return null;
            }
        } else if ("KNOWN=YES".equals(tok)) {
            known = Boolean.TRUE;
        } else if ("KNOWN=NO".equals(tok)) {
            known = Boolean.FALSE;
        } else {
            Logging.errorPrint("Unknown restriction: " + tok + " in CHOOSE:SPELLS");
            return null;
        }
    }
    return new Restriction(levelMin, levelMax, known);
}
Also used : Formula(pcgen.base.formula.Formula) StringTokenizer(java.util.StringTokenizer)

Example 70 with Formula

use of pcgen.base.formula.Formula in project pcgen by PCGen.

the class CrformulaToken method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, PCClass pcc, String value) {
    Formula formula = FormulaFactory.getFormulaFor(value);
    if (!formula.isValid()) {
        return new ParseResult.Fail("Formula in " + getTokenName() + " was not valid: " + formula.toString(), context);
    }
    context.getObjectContext().put(pcc, FormulaKey.CR, formula);
    return ParseResult.SUCCESS;
}
Also used : Formula(pcgen.base.formula.Formula)

Aggregations

Formula (pcgen.base.formula.Formula)106 ArrayList (java.util.ArrayList)30 ParsingSeparator (pcgen.base.text.ParsingSeparator)26 ConcretePersistentTransitionChoice (pcgen.cdom.base.ConcretePersistentTransitionChoice)18 ParseResult (pcgen.rules.persistence.token.ParseResult)18 CDOMReference (pcgen.cdom.base.CDOMReference)16 StringTokenizer (java.util.StringTokenizer)15 ChoiceSet (pcgen.cdom.base.ChoiceSet)11 Ungranted (pcgen.cdom.base.Ungranted)10 CDOMObject (pcgen.cdom.base.CDOMObject)9 PersistentTransitionChoice (pcgen.cdom.base.PersistentTransitionChoice)9 ReferenceChoiceSet (pcgen.cdom.choiceset.ReferenceChoiceSet)8 PCClass (pcgen.core.PCClass)7 Prerequisite (pcgen.core.prereq.Prerequisite)7 Map (java.util.Map)6 AssociatedPrereqObject (pcgen.cdom.base.AssociatedPrereqObject)6 PCTemplate (pcgen.core.PCTemplate)6 Race (pcgen.core.Race)6 Spell (pcgen.core.spell.Spell)6 HashMap (java.util.HashMap)5