Search in sources :

Example 96 with Formula

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

the class SpellsLst method processAdds.

private Collection<? extends String> processAdds(LoadContext context, MapToList<CDOMReference<Spell>, AssociatedPrereqObject> mtl) {
    TripleKeyMap<Set<Prerequisite>, Map<AssociationKey<?>, Object>, CDOMReference<Spell>, String> m = new TripleKeyMap<>();
    for (CDOMReference<Spell> lw : mtl.getKeySet()) {
        for (AssociatedPrereqObject assoc : mtl.getListFor(lw)) {
            Map<AssociationKey<?>, Object> am = new HashMap<>();
            String dc = null;
            for (AssociationKey<?> ak : assoc.getAssociationKeys()) {
                // else
                if (AssociationKey.DC_FORMULA.equals(ak)) {
                    dc = assoc.getAssociation(AssociationKey.DC_FORMULA);
                } else {
                    am.put(ak, assoc.getAssociation(ak));
                }
            }
            m.put(new HashSet<>(assoc.getPrerequisiteList()), am, lw, dc);
        }
    }
    Set<String> set = new TreeSet<>();
    for (Set<Prerequisite> prereqs : m.getKeySet()) {
        for (Map<AssociationKey<?>, Object> am : m.getSecondaryKeySet(prereqs)) {
            StringBuilder sb = new StringBuilder();
            sb.append(am.get(AssociationKey.SPELLBOOK));
            Formula times = AssociationKey.TIMES_PER_UNIT.cast(am.get(AssociationKey.TIMES_PER_UNIT));
            sb.append(Constants.PIPE).append("TIMES=").append(times);
            String timeunit = AssociationKey.TIME_UNIT.cast(am.get(AssociationKey.TIME_UNIT));
            if (timeunit != null) {
                sb.append(Constants.PIPE).append("TIMEUNIT=").append(timeunit);
            }
            String casterLvl = AssociationKey.CASTER_LEVEL.cast(am.get(AssociationKey.CASTER_LEVEL));
            if (casterLvl != null) {
                sb.append(Constants.PIPE).append("CASTERLEVEL=").append(casterLvl);
            }
            Set<String> spellSet = new TreeSet<>();
            for (CDOMReference<Spell> spell : m.getTertiaryKeySet(prereqs, am)) {
                String spellString = spell.getLSTformat(false);
                String dc = m.get(prereqs, am, spell);
                if (dc != null) {
                    spellString += Constants.COMMA + dc;
                }
                spellSet.add(spellString);
            }
            sb.append(Constants.PIPE);
            sb.append(StringUtil.join(spellSet, Constants.PIPE));
            if (prereqs != null && !prereqs.isEmpty()) {
                sb.append(Constants.PIPE);
                sb.append(getPrerequisiteString(context, prereqs));
            }
            set.add(sb.toString());
        }
    }
    return set;
}
Also used : AssociationKey(pcgen.cdom.enumeration.AssociationKey) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Spell(pcgen.core.spell.Spell) Formula(pcgen.base.formula.Formula) TripleKeyMap(pcgen.base.util.TripleKeyMap) TreeSet(java.util.TreeSet) CDOMObject(pcgen.cdom.base.CDOMObject) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) DoubleKeyMap(pcgen.base.util.DoubleKeyMap) TripleKeyMap(pcgen.base.util.TripleKeyMap) CDOMReference(pcgen.cdom.base.CDOMReference) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 97 with Formula

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

the class SrLst method parseToken.

@Override
public ParseResult parseToken(LoadContext context, CDOMObject obj, String value) {
    if (obj instanceof Ungranted) {
        return new ParseResult.Fail("Cannot use " + getTokenName() + " on an Ungranted object type: " + obj.getClass().getSimpleName(), context);
    }
    if (Constants.LST_DOT_CLEAR.equals(value)) {
        context.getObjectContext().remove(obj, ObjectKey.SR);
    } else {
        Formula formula = FormulaFactory.getFormulaFor(value);
        if (!formula.isValid()) {
            return new ParseResult.Fail("Formula in " + getTokenName() + " was not valid: " + formula.toString(), context);
        }
        context.getObjectContext().put(obj, ObjectKey.SR, new SpellResistance(formula));
    }
    return ParseResult.SUCCESS;
}
Also used : Formula(pcgen.base.formula.Formula) SpellResistance(pcgen.cdom.content.SpellResistance) Ungranted(pcgen.cdom.base.Ungranted)

Example 98 with Formula

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

the class KitLst method parseTokenWithSeparator.

@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, CDOMObject obj, String value) {
    if (obj instanceof Ungranted) {
        return new ParseResult.Fail("Cannot use " + getTokenName() + " on an Ungranted object type: " + obj.getClass().getSimpleName(), context);
    }
    if (obj instanceof NonInteractive) {
        return new ParseResult.Fail("Cannot use " + getTokenName() + " on an Non-Interactive object type: " + obj.getClass().getSimpleName(), context);
    }
    StringTokenizer tok = new StringTokenizer(value, Constants.PIPE);
    Formula count = FormulaFactory.getFormulaFor(tok.nextToken());
    if (!count.isValid()) {
        return new ParseResult.Fail("Count in " + getTokenName() + " was not valid: " + count.toString(), context);
    }
    if (!count.isStatic()) {
        return new ParseResult.Fail("Count in " + getTokenName() + " must be a number", context);
    }
    if (count.resolveStatic().intValue() <= 0) {
        return new ParseResult.Fail("Count in " + getTokenName() + " must be > 0", context);
    }
    if (!tok.hasMoreTokens()) {
        return new ParseResult.Fail(getTokenName() + " must have a | separating " + "count from the list of possible values: " + value, context);
    }
    List<CDOMReference<Kit>> refs = new ArrayList<>();
    while (tok.hasMoreTokens()) {
        String token = tok.nextToken();
        CDOMReference<Kit> ref;
        if (Constants.LST_ALL.equals(token)) {
            ref = context.getReferenceContext().getCDOMAllReference(KIT_CLASS);
        } else {
            ref = context.getReferenceContext().getCDOMReference(KIT_CLASS, token);
        }
        refs.add(ref);
    }
    ReferenceChoiceSet<Kit> rcs = new ReferenceChoiceSet<>(refs);
    if (!rcs.getGroupingState().isValid()) {
        return new ParseResult.Fail("Non-sensical " + getTokenName() + ": Contains ANY and a specific reference: " + value, context);
    }
    ChoiceSet<Kit> cs = new ChoiceSet<>(getTokenName(), new QualifiedDecorator<>(rcs));
    cs.setTitle("Kit Selection");
    TransitionChoice<Kit> tc = new ConcreteTransitionChoice<>(cs, count);
    context.getObjectContext().addToList(obj, ListKey.KIT_CHOICE, tc);
    tc.setRequired(false);
    tc.setChoiceActor(this);
    return ParseResult.SUCCESS;
}
Also used : ConcreteTransitionChoice(pcgen.cdom.base.ConcreteTransitionChoice) ReferenceChoiceSet(pcgen.cdom.choiceset.ReferenceChoiceSet) ChoiceSet(pcgen.cdom.base.ChoiceSet) ArrayList(java.util.ArrayList) Ungranted(pcgen.cdom.base.Ungranted) Formula(pcgen.base.formula.Formula) StringTokenizer(java.util.StringTokenizer) Kit(pcgen.core.Kit) NonInteractive(pcgen.cdom.base.NonInteractive) CDOMReference(pcgen.cdom.base.CDOMReference) ReferenceChoiceSet(pcgen.cdom.choiceset.ReferenceChoiceSet)

Example 99 with Formula

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

the class RegionLst method unparse.

@Override
public String[] unparse(LoadContext context, CDOMObject pcc) {
    TransitionChoice<Region> tc = context.getObjectContext().getObject(pcc, ObjectKey.REGION_CHOICE);
    if (tc == null) {
        // indicates no Token
        return null;
    }
    StringBuilder sb = new StringBuilder();
    Formula count = tc.getCount();
    if (!FormulaFactory.ONE.equals(count)) {
        sb.append(count);
        sb.append(Constants.PIPE);
    }
    sb.append(tc.getChoices().getLSTformat().replaceAll(Constants.COMMA, Constants.PIPE));
    return new String[] { sb.toString() };
}
Also used : Formula(pcgen.base.formula.Formula) Region(pcgen.cdom.enumeration.Region)

Example 100 with Formula

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

the class CharacterDisplay method getTemplateSR.

/**
	 * Get the Spell Resistance granted by the given template to a character at a
	 * given level (Class and Hit Dice). This will include the absolute
	 * adjustment made with {@literal SR:, LEVEL:<num>:SR and HD:<num>:SR tags}
	 * 
	 * Note: unlike DR and CR, the value returned here includes the PCs own
	 * Spell Resistance.
	 * 
	 * @param pct
	 * 			The PCTemplate for which the Spell Resistance will be returned.
	 * @param level
	 *            The level to calculate the SR for
	 * @param hitdice
	 *            The Hit dice to calculate the SR for
	 * 
	 * @return the Spell Resistance granted by the given Template at the given level
	 *         and HD
	 */
public int getTemplateSR(PCTemplate pct, int level, int hitdice) {
    String qualifiedKey = pct.getQualifiedKey();
    Formula reduction = pct.getSafe(ObjectKey.SR).getReduction();
    int aSR = formulaResolvingFacet.resolve(id, reduction, qualifiedKey).intValue();
    for (PCTemplate rlt : pct.getSafeListFor(ListKey.REPEATLEVEL_TEMPLATES)) {
        for (PCTemplate lt : rlt.getSafeListFor(ListKey.LEVEL_TEMPLATES)) {
            if (lt.get(IntegerKey.LEVEL) <= level) {
                Formula ltReduction = lt.getSafe(ObjectKey.SR).getReduction();
                int ltSR = formulaResolvingFacet.resolve(id, ltReduction, qualifiedKey).intValue();
                aSR = Math.max(aSR, ltSR);
            }
        }
    }
    for (PCTemplate lt : pct.getSafeListFor(ListKey.LEVEL_TEMPLATES)) {
        if (lt.get(IntegerKey.LEVEL) <= level) {
            Formula ltReduction = lt.getSafe(ObjectKey.SR).getReduction();
            int ltSR = formulaResolvingFacet.resolve(id, ltReduction, qualifiedKey).intValue();
            aSR = Math.max(aSR, ltSR);
        }
    }
    for (PCTemplate lt : pct.getSafeListFor(ListKey.HD_TEMPLATES)) {
        if (lt.get(IntegerKey.HD_MAX) <= hitdice && lt.get(IntegerKey.HD_MIN) >= hitdice) {
            Formula ltReduction = lt.getSafe(ObjectKey.SR).getReduction();
            int ltSR = formulaResolvingFacet.resolve(id, ltReduction, qualifiedKey).intValue();
            aSR = Math.max(aSR, ltSR);
        }
    }
    return aSR;
}
Also used : Formula(pcgen.base.formula.Formula) PCTemplate(pcgen.core.PCTemplate)

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