Search in sources :

Example 91 with Formula

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

the class Vision method getVision.

public static Vision getVision(String visionType) {
    // expecting value in form of Darkvision (60') or Darkvision
    int commaLoc = visionType.indexOf(',');
    if (commaLoc != -1) {
        throw new IllegalArgumentException("Invalid Vision: " + visionType + ". May not contain a comma");
    }
    int quoteLoc = visionType.indexOf('\'');
    int openParenLoc = visionType.indexOf('(');
    Formula distance;
    String type;
    if (openParenLoc == -1) {
        if (visionType.indexOf(')') != -1) {
            throw new IllegalArgumentException("Invalid Vision: " + visionType + ". Had close paren without open paren");
        }
        if (quoteLoc != -1) {
            throw new IllegalArgumentException("Invalid Vision: " + visionType + ". Had quote parens");
        }
        type = visionType;
        distance = FormulaFactory.ZERO;
    } else {
        int length = visionType.length();
        if (visionType.indexOf(')') != length - 1) {
            throw new IllegalArgumentException("Invalid Vision: " + visionType + ". Close paren not at end of string");
        }
        int endDistance = length - 1;
        if (quoteLoc != -1) {
            if (quoteLoc == length - 2) {
                endDistance--;
            } else {
                throw new IllegalArgumentException("Invalid Vision: " + visionType + ". Foot character ' not immediately before close paren");
            }
        }
        type = visionType.substring(0, openParenLoc).trim();
        String dist = visionType.substring(openParenLoc + 1, endDistance);
        if (dist.isEmpty()) {
            throw new IllegalArgumentException("Invalid Vision: " + visionType + ". No Distance provided");
        }
        if (quoteLoc != -1) {
            try {
                Integer.parseInt(dist);
            } catch (NumberFormatException nfe) {
                throw new IllegalArgumentException("Invalid Vision: " + visionType + ". Vision Distance with Foot character ' was not an integer");
            }
        }
        distance = FormulaFactory.getFormulaFor(dist);
        if (!distance.isValid()) {
            throw new IllegalArgumentException("Invalid: Vision Distance was not valid: " + distance.toString());
        }
    }
    if (type.isEmpty()) {
        throw new IllegalArgumentException("Invalid Vision: " + visionType + ". No Vision Type provided");
    }
    return new Vision(VisionType.getVisionType(type), distance);
}
Also used : Formula(pcgen.base.formula.Formula)

Example 92 with Formula

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

the class LookupToken method parseToken.

@Override
public ParseResult parseToken(LoadContext context, KitGear kitGear, String value) {
    ParsingSeparator sep = new ParsingSeparator(value, ',');
    sep.addGroupingPair('[', ']');
    sep.addGroupingPair('(', ')');
    String first = sep.next();
    if (!sep.hasNext()) {
        return new ParseResult.Fail("Token must contain separator ','", context);
    }
    String second = sep.next();
    if (sep.hasNext()) {
        return new ParseResult.Fail("Token cannot have more than one separator ','", context);
    }
    Formula formula = FormulaFactory.getFormulaFor(second);
    if (!formula.isValid()) {
        return new ParseResult.Fail("Formula in " + getTokenName() + " was not valid: " + formula.toString(), context);
    }
    kitGear.loadLookup(first, formula);
    return ParseResult.SUCCESS;
}
Also used : NamedFormula(pcgen.base.util.NamedFormula) Formula(pcgen.base.formula.Formula) ParsingSeparator(pcgen.base.text.ParsingSeparator)

Example 93 with Formula

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

the class OptionToken method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, BaseKit kit, String value) {
    ParsingSeparator pipeSep = new ParsingSeparator(value, '|');
    pipeSep.addGroupingPair('[', ']');
    pipeSep.addGroupingPair('(', ')');
    while (pipeSep.hasNext()) {
        String subTok = pipeSep.next();
        if (subTok.isEmpty()) {
            return new ParseResult.Fail(getTokenName() + " arguments has invalid pipe separator: " + value, context);
        }
        ParseResult pr = checkForIllegalSeparator(',', subTok);
        if (!pr.passed()) {
            return pr;
        }
        ParsingSeparator commaSep = new ParsingSeparator(subTok, ',');
        commaSep.addGroupingPair('[', ']');
        commaSep.addGroupingPair('(', ')');
        String minString = commaSep.next();
        String maxString;
        if (commaSep.hasNext()) {
            maxString = commaSep.next();
        } else {
            maxString = subTok;
        }
        if (commaSep.hasNext()) {
            return new ParseResult.Fail("Token cannot have more than one separator ','", context);
        }
        Formula min = FormulaFactory.getFormulaFor(minString);
        if (!min.isValid()) {
            return new ParseResult.Fail("Min Formula in " + getTokenName() + " was not valid: " + min.toString(), context);
        }
        Formula max = FormulaFactory.getFormulaFor(maxString);
        if (!max.isValid()) {
            return new ParseResult.Fail("Max Formula in " + getTokenName() + " was not valid: " + max.toString(), context);
        }
        kit.setOptionBounds(min, max);
    }
    return ParseResult.SUCCESS;
}
Also used : Formula(pcgen.base.formula.Formula) ParsingSeparator(pcgen.base.text.ParsingSeparator) ParseResult(pcgen.rules.persistence.token.ParseResult)

Example 94 with Formula

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

the class PoolToken method parseNonEmptyToken.

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

Example 95 with Formula

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

the class SizeToken method process.

@Override
public boolean process(LoadContext context, PCTemplate template) {
    String value = template.get(StringKey.SIZEFORMULA);
    if (value == null) {
        return true;
    }
    SizeAdjustment size = context.getReferenceContext().silentlyGetConstructedCDOMObject(SizeAdjustment.class, value);
    Formula sizeFormula;
    if (size == null) {
        sizeFormula = FormulaFactory.getFormulaFor(value);
    } else {
        sizeFormula = new FixedSizeFormula(CDOMDirectSingleRef.getRef(size));
    }
    if (!sizeFormula.isValid()) {
        Logging.errorPrint("Size in " + getTokenName() + " was not valid: " + sizeFormula.toString(), context);
        return false;
    }
    context.getObjectContext().put(template, FormulaKey.SIZE, sizeFormula);
    return false;
}
Also used : FixedSizeFormula(pcgen.cdom.formula.FixedSizeFormula) Formula(pcgen.base.formula.Formula) SizeAdjustment(pcgen.core.SizeAdjustment) FixedSizeFormula(pcgen.cdom.formula.FixedSizeFormula)

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