Search in sources :

Example 26 with Formula

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

the class NaturalattacksLst method process.

@Override
public boolean process(LoadContext context, CDOMObject obj) {
    List<Equipment> natWeapons = obj.getListFor(ListKey.NATURAL_WEAPON);
    if (natWeapons != null) {
        Formula sizeFormula = obj.getSafe(FormulaKey.SIZE);
        // If the size was just a default, check for a size prereq and use that instead.
        if (obj.get(FormulaKey.SIZE) == null && obj.hasPreReqTypeOf("SIZE")) {
            Integer requiredSize = getRequiredSize(obj);
            if (requiredSize != null) {
                sizeFormula = FormulaFactory.getFormulaFor(requiredSize);
            }
        }
        if (sizeFormula.isStatic()) {
            int isize = sizeFormula.resolveStatic().intValue();
            SizeAdjustment size = context.getReferenceContext().getSortedList(SizeAdjustment.class, IntegerKey.SIZEORDER).get(isize);
            for (Equipment e : natWeapons) {
                CDOMDirectSingleRef<SizeAdjustment> sizeRef = CDOMDirectSingleRef.getRef(size);
                e.put(ObjectKey.BASESIZE, sizeRef);
                e.put(ObjectKey.SIZE, sizeRef);
            }
        } else {
            Logging.errorPrint("SIZE in " + obj.getClass().getSimpleName() + ' ' + obj.getKeyName() + " must not be a variable " + "if it contains a NATURALATTACKS token");
        }
    }
    return true;
}
Also used : Formula(pcgen.base.formula.Formula) Equipment(pcgen.core.Equipment) SizeAdjustment(pcgen.core.SizeAdjustment)

Example 27 with Formula

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

the class SelectLst method parseToken.

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

Example 28 with Formula

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

the class AddLevelToken method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, PCTemplate template, String value) {
    ParsingSeparator sep = new ParsingSeparator(value, '|');
    sep.addGroupingPair('[', ']');
    sep.addGroupingPair('(', ')');
    String classString = sep.next();
    if (classString.isEmpty()) {
        ComplexParseResult cpr = new ComplexParseResult();
        cpr.addErrorMessage("Empty Class found in " + getTokenName());
        cpr.addErrorMessage("  " + getTokenName() + " requires at format: Class|LevelCount");
        return cpr;
    }
    if (!sep.hasNext()) {
        ComplexParseResult cpr = new ComplexParseResult();
        cpr.addErrorMessage("No | found in " + getTokenName());
        cpr.addErrorMessage("  " + getTokenName() + " requires at format: Class|LevelCount");
        return cpr;
    }
    String numLevels = sep.next();
    if (numLevels.isEmpty()) {
        ComplexParseResult cpr = new ComplexParseResult();
        cpr.addErrorMessage("Empty Level Count found in " + getTokenName());
        cpr.addErrorMessage("  " + getTokenName() + " requires at format: Class|LevelCount");
        return cpr;
    }
    if (sep.hasNext()) {
        ComplexParseResult cpr = new ComplexParseResult();
        cpr.addErrorMessage("Two | found in " + getTokenName());
        cpr.addErrorMessage("  " + getTokenName() + " requires at format: Class|LevelCount");
        return cpr;
    }
    CDOMSingleRef<PCClass> cl = context.getReferenceContext().getCDOMReference(PCClass.class, classString);
    Formula f;
    try {
        int lvls = Integer.parseInt(numLevels);
        if (lvls <= 0) {
            return new ParseResult.Fail("Number of Levels granted in " + getTokenName() + " must be greater than zero", context);
        }
        f = FormulaFactory.getFormulaFor(lvls);
    } catch (NumberFormatException nfe) {
        f = FormulaFactory.getFormulaFor(numLevels);
    }
    if (!f.isValid()) {
        return new ParseResult.Fail("Formula in " + getTokenName() + " was not valid: " + f.toString(), context);
    }
    LevelCommandFactory cf = new LevelCommandFactory(cl, f);
    context.getObjectContext().addToList(template, ListKey.ADD_LEVEL, cf);
    return ParseResult.SUCCESS;
}
Also used : Formula(pcgen.base.formula.Formula) LevelCommandFactory(pcgen.cdom.content.LevelCommandFactory) ParsingSeparator(pcgen.base.text.ParsingSeparator) ComplexParseResult(pcgen.rules.persistence.token.ComplexParseResult) PCClass(pcgen.core.PCClass)

Example 29 with Formula

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

the class StatmodToken method parseToken.

@Override
public ParseResult parseToken(LoadContext context, PCStat stat, String value) {
    if (value == null || value.isEmpty()) {
        return new ParseResult.Fail(getTokenName() + " arguments may not be empty", context);
    }
    Formula formula = FormulaFactory.getFormulaFor(value);
    if (!formula.isValid()) {
        return new ParseResult.Fail("Formula in " + getTokenName() + " was not valid: " + formula.toString(), context);
    }
    context.getObjectContext().put(stat, FormulaKey.STAT_MOD, formula);
    return ParseResult.SUCCESS;
}
Also used : Formula(pcgen.base.formula.Formula)

Example 30 with Formula

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

the class TemplateToken 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 (PCTEMPLATE_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;
            }
            StringBuilder sb = new StringBuilder();
            if (!FormulaFactory.ONE.equals(f)) {
                sb.append(f).append(Constants.PIPE);
            }
            sb.append(cs.getLSTformat());
            addStrings.add(sb.toString());
        // assoc.getAssociation(AssociationKey.CHOICE_MAXCOUNT);
        }
    }
    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)

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