Search in sources :

Example 76 with Formula

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

the class CharacterSpellResistanceFacet method getSR.

/**
	 * Returns the Spell Resistance for the Player Character identified by the
	 * given CharID.
	 * 
	 * @param id
	 *            The CharID identifying the Player Character for which the
	 *            Spell Resistance will be returned
	 * @return The Spell Resistance for the Player Character identified by the
	 *         given CharID
	 */
public int getSR(CharID id) {
    Map<Formula, Set<Object>> componentMap = getCachedMap(id);
    int sr = 0;
    if (componentMap != null) {
        for (Map.Entry<Formula, Set<Object>> me : componentMap.entrySet()) {
            Formula f = me.getKey();
            Set<Object> sourceSet = me.getValue();
            for (Object source : sourceSet) {
                String sourceString = (source instanceof CDOMObject) ? ((CDOMObject) source).getQualifiedKey() : "";
                sr = Math.max(sr, formulaResolvingFacet.resolve(id, f, sourceString).intValue());
            }
        }
    }
    return sr;
}
Also used : Formula(pcgen.base.formula.Formula) Set(java.util.Set) CDOMObject(pcgen.cdom.base.CDOMObject) CDOMObject(pcgen.cdom.base.CDOMObject) Map(java.util.Map)

Example 77 with Formula

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

the class AbilityToken method unparse.

@Override
public String[] unparse(LoadContext context, CDOMObject obj) {
    Changes<PersistentTransitionChoice<?>> grantChanges = context.getObjectContext().getListChanges(obj, ListKey.ADD);
    Collection<PersistentTransitionChoice<?>> addedItems = grantChanges.getAdded();
    if (addedItems == null || addedItems.isEmpty()) {
        // Zero indicates no Token
        return null;
    }
    List<String> addStrings = new ArrayList<>();
    for (TransitionChoice<?> container : addedItems) {
        SelectableSet<?> cs = container.getChoices();
        if (getTokenName().equals(cs.getName()) && CAT_ABILITY_SELECTION_CLASS.equals(cs.getChoiceClass())) {
            AbilityChoiceSet ascs = (AbilityChoiceSet) cs;
            Formula f = container.getCount();
            if (f == null) {
                context.addWriteMessage("Unable to find " + getFullName() + " Count");
                return null;
            }
            if (f.isStatic() && f.resolveStatic().doubleValue() <= 0) {
                context.addWriteMessage("Count in " + getFullName() + " must be > 0");
                return null;
            }
            if (!cs.getGroupingState().isValid()) {
                context.addWriteMessage("Non-sensical " + getFullName() + ": Contains ANY and a specific reference: " + cs.getLSTformat());
                return null;
            }
            StringBuilder sb = new StringBuilder();
            if (!FormulaFactory.ONE.equals(f)) {
                sb.append(f).append(Constants.PIPE);
            }
            sb.append(ascs.getCategory().getLSTformat(false));
            sb.append(Constants.PIPE);
            sb.append(ascs.getNature());
            sb.append(Constants.PIPE);
            if (container.allowsStacking()) {
                sb.append("STACKS");
                Integer stackLimit = container.getStackLimit();
                if (stackLimit != null) {
                    if (stackLimit.intValue() <= 0) {
                        context.addWriteMessage("Stack Limit in " + getFullName() + " must be > 0");
                        return null;
                    }
                    sb.append(Constants.EQUALS);
                    sb.append(stackLimit.intValue());
                }
                sb.append(Constants.COMMA);
            }
            sb.append(cs.getLSTformat());
            addStrings.add(sb.toString());
        }
    }
    return addStrings.toArray(new String[addStrings.size()]);
}
Also used : Formula(pcgen.base.formula.Formula) AbilityChoiceSet(pcgen.cdom.base.ChoiceSet.AbilityChoiceSet) ConcretePersistentTransitionChoice(pcgen.cdom.base.ConcretePersistentTransitionChoice) PersistentTransitionChoice(pcgen.cdom.base.PersistentTransitionChoice) ArrayList(java.util.ArrayList)

Example 78 with Formula

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

the class AbilityToken method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, CDOMObject obj, String value) {
    if (isEmpty(value)) {
        return new ParseResult.Fail("Value in " + getFullName() + " may not be empty", context);
    }
    ParsingSeparator sep = new ParsingSeparator(value, '|');
    sep.addGroupingPair('[', ']');
    sep.addGroupingPair('(', ')');
    String first = sep.next();
    if (!sep.hasNext()) {
        return new ParseResult.Fail("Syntax of ADD:" + getTokenName() + " requires 3 to 4 |: " + value, context);
    }
    String second = sep.next();
    if (!sep.hasNext()) {
        return new ParseResult.Fail("Syntax of ADD:" + getTokenName() + " requires a minimum of three | : " + value, context);
    }
    String third = sep.next();
    Formula count;
    if (sep.hasNext()) {
        count = FormulaFactory.getFormulaFor(first);
        if (!count.isValid()) {
            return new ParseResult.Fail("Count in " + getTokenName() + " was not valid: " + count.toString(), context);
        }
        if (count.isStatic() && count.resolveStatic().doubleValue() <= 0) {
            return new ParseResult.Fail("Count in " + getFullName() + " must be > 0", context);
        }
        first = second;
        second = third;
        third = sep.next();
    } else {
        count = FormulaFactory.ONE;
    }
    if (sep.hasNext()) {
        return new ParseResult.Fail("Syntax of ADD:" + getTokenName() + " has max of four | when a count is not present: " + value, context);
    }
    CDOMSingleRef<AbilityCategory> acRef = context.getReferenceContext().getCDOMReference(ABILITY_CATEGORY_CLASS, first);
    Nature nature = Nature.valueOf(second);
    if (nature == null) {
        return new ParseResult.Fail(getFullName() + ": Invalid ability nature: " + second, context);
    }
    if (Nature.ANY.equals(nature)) {
        return new ParseResult.Fail(getTokenName() + " refers to ANY Ability Nature, cannot be used in " + getTokenName() + ": " + value);
    }
    if (Nature.AUTOMATIC.equals(nature)) {
        return new ParseResult.Fail(getTokenName() + " refers to AUTOMATIC Ability Nature, cannot be used in " + getTokenName() + ": " + value, context);
    }
    ParseResult pr = checkSeparatorsAndNonEmpty(',', third);
    if (!pr.passed()) {
        return pr;
    }
    List<CDOMReference<Ability>> refs = new ArrayList<>();
    ParsingSeparator tok = new ParsingSeparator(third, ',');
    tok.addGroupingPair('[', ']');
    tok.addGroupingPair('(', ')');
    boolean allowStack = false;
    int dupChoices = 0;
    ReferenceManufacturer<Ability> rm = context.getReferenceContext().getManufacturer(ABILITY_CLASS, ABILITY_CATEGORY_CLASS, first);
    if (rm == null) {
        return new ParseResult.Fail("Could not get Reference Manufacturer for Category: " + first, context);
    }
    while (tok.hasNext()) {
        CDOMReference<Ability> ab;
        String token = tok.next();
        if ("STACKS".equals(token)) {
            if (allowStack) {
                return new ParseResult.Fail(getFullName() + " found second stacking specification in value: " + value, context);
            }
            allowStack = true;
            continue;
        } else if (token.startsWith("STACKS=")) {
            if (allowStack) {
                return new ParseResult.Fail(getFullName() + " found second stacking specification in value: " + value, context);
            }
            allowStack = true;
            try {
                dupChoices = Integer.parseInt(token.substring(7));
            } catch (NumberFormatException nfe) {
                return new ParseResult.Fail("Invalid Stack number in " + getFullName() + ": " + value, context);
            }
            if (dupChoices <= 0) {
                return new ParseResult.Fail("Invalid (less than 1) Stack number in " + getFullName() + ": " + value, context);
            }
            continue;
        } else {
            if (Constants.LST_ALL.equals(token)) {
                ab = rm.getAllReference();
            } else {
                ab = TokenUtilities.getTypeOrPrimitive(rm, token);
            }
        }
        if (ab == null) {
            return new ParseResult.Fail("  Error was encountered while parsing " + getTokenName() + ": " + value + " had an invalid reference: " + token, context);
        }
        refs.add(ab);
    }
    if (refs.isEmpty()) {
        return new ParseResult.Fail("Non-sensical " + getFullName() + ": Contains no ability reference: " + value, context);
    }
    AbilityRefChoiceSet rcs = new AbilityRefChoiceSet(acRef, refs, nature);
    if (!rcs.getGroupingState().isValid()) {
        return new ParseResult.Fail("Non-sensical " + getFullName() + ": Contains ANY and a specific reference: " + value, context);
    }
    AbilityChoiceSet cs = new AbilityChoiceSet(getTokenName(), rcs);
    StringBuilder title = new StringBuilder(50);
    if (!Nature.NORMAL.equals(nature)) {
        title.append(nature.toString());
        title.append(' ');
    }
    title.append(first);
    title.append(" Choice");
    cs.setTitle(title.toString());
    PersistentTransitionChoice<CNAbilitySelection> tc = new ConcretePersistentTransitionChoice<>(cs, count);
    context.getObjectContext().addToList(obj, ListKey.ADD, tc);
    tc.allowStack(allowStack);
    if (dupChoices != 0) {
        tc.setStackLimit(dupChoices);
    }
    tc.setChoiceActor(this);
    return ParseResult.SUCCESS;
}
Also used : Nature(pcgen.cdom.enumeration.Nature) Ability(pcgen.core.Ability) CNAbility(pcgen.cdom.content.CNAbility) ParseResult(pcgen.rules.persistence.token.ParseResult) ArrayList(java.util.ArrayList) ConcretePersistentTransitionChoice(pcgen.cdom.base.ConcretePersistentTransitionChoice) Formula(pcgen.base.formula.Formula) ParsingSeparator(pcgen.base.text.ParsingSeparator) AbilityChoiceSet(pcgen.cdom.base.ChoiceSet.AbilityChoiceSet) CNAbilitySelection(pcgen.cdom.helper.CNAbilitySelection) AbilityCategory(pcgen.core.AbilityCategory) CDOMReference(pcgen.cdom.base.CDOMReference) AbilityRefChoiceSet(pcgen.cdom.choiceset.AbilityRefChoiceSet)

Example 79 with Formula

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

the class ClassSkillsLevelToken method unparse.

@Override
public String[] unparse(LoadContext context, PCClassLevel obj) {
    Changes<PersistentTransitionChoice<?>> grantChanges = context.getObjectContext().getListChanges(obj, ListKey.ADD);
    Collection<PersistentTransitionChoice<?>> addedItems = grantChanges.getAdded();
    if (addedItems == null || addedItems.isEmpty()) {
        // Zero indicates no Token
        return null;
    }
    List<String> addStrings = new ArrayList<>();
    for (TransitionChoice<?> container : addedItems) {
        SelectableSet<?> cs = container.getChoices();
        if (getTokenName().equals(cs.getName()) && SKILL_CLASS.equals(cs.getChoiceClass())) {
            Formula f = container.getCount();
            if (f == null) {
                context.addWriteMessage("Unable to find " + getFullName() + " Count");
                return null;
            }
            if (f.isStatic() && f.resolveStatic().doubleValue() <= 0) {
                context.addWriteMessage("Count in " + getFullName() + " must be > 0");
                return null;
            }
            if (!cs.getGroupingState().isValid()) {
                context.addWriteMessage("Non-sensical " + getFullName() + ": Contains ANY and a specific reference: " + cs.getLSTformat());
                return null;
            }
            StringBuilder sb = new StringBuilder();
            if (!FormulaFactory.ONE.equals(f)) {
                sb.append(f).append(Constants.PIPE);
            }
            sb.append(cs.getLSTformat());
            ClassSkillChoiceActor actor = (ClassSkillChoiceActor) container.getChoiceActor();
            Integer rank = actor.getApplyRank();
            if (rank != null) {
                sb.append(Constants.COMMA).append("AUTORANK=" + rank);
            }
            addStrings.add(sb.toString());
        }
    }
    return addStrings.toArray(new String[addStrings.size()]);
}
Also used : Formula(pcgen.base.formula.Formula) ConcretePersistentTransitionChoice(pcgen.cdom.base.ConcretePersistentTransitionChoice) PersistentTransitionChoice(pcgen.cdom.base.PersistentTransitionChoice) ArrayList(java.util.ArrayList) ClassSkillChoiceActor(pcgen.cdom.helper.ClassSkillChoiceActor)

Example 80 with Formula

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

the class ClassSkillsToken method unparse.

@Override
public String[] unparse(LoadContext context, PCClass obj) {
    Changes<PersistentTransitionChoice<?>> grantChanges = context.getObjectContext().getListChanges(obj, ListKey.ADD);
    Collection<PersistentTransitionChoice<?>> addedItems = grantChanges.getAdded();
    if (addedItems == null || addedItems.isEmpty()) {
        // Zero indicates no Token
        return null;
    }
    List<String> addStrings = new ArrayList<>();
    for (TransitionChoice<?> container : addedItems) {
        SelectableSet<?> cs = container.getChoices();
        if (getTokenName().equals(cs.getName()) && SKILL_CLASS.equals(cs.getChoiceClass())) {
            Formula f = container.getCount();
            if (f == null) {
                context.addWriteMessage("Unable to find " + getFullName() + " Count");
                return null;
            }
            if (f.isStatic() && f.resolveStatic().doubleValue() <= 0) {
                context.addWriteMessage("Count in " + getFullName() + " must be > 0");
                return null;
            }
            if (!cs.getGroupingState().isValid()) {
                context.addWriteMessage("Non-sensical " + getFullName() + ": Contains ANY and a specific reference: " + cs.getLSTformat());
                return null;
            }
            StringBuilder sb = new StringBuilder();
            if (!FormulaFactory.ONE.equals(f)) {
                sb.append(f).append(Constants.PIPE);
            }
            sb.append(cs.getLSTformat());
            ClassSkillChoiceActor actor = (ClassSkillChoiceActor) container.getChoiceActor();
            Integer rank = actor.getApplyRank();
            if (rank != null) {
                sb.append(Constants.COMMA).append("AUTORANK=" + rank);
            }
            addStrings.add(sb.toString());
        }
    }
    return addStrings.toArray(new String[addStrings.size()]);
}
Also used : Formula(pcgen.base.formula.Formula) ConcretePersistentTransitionChoice(pcgen.cdom.base.ConcretePersistentTransitionChoice) PersistentTransitionChoice(pcgen.cdom.base.PersistentTransitionChoice) ArrayList(java.util.ArrayList) ClassSkillChoiceActor(pcgen.cdom.helper.ClassSkillChoiceActor)

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