Search in sources :

Example 41 with Formula

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

the class DefineStatLst 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);
    }
    ParsingSeparator sep = new ParsingSeparator(value, '|');
    sep.addGroupingPair('[', ']');
    sep.addGroupingPair('(', ')');
    if (!sep.hasNext()) {
        return new ParseResult.Fail(getTokenName() + " may not be empty", context);
    }
    String firstItem = sep.next();
    DefineStatSubToken subToken;
    try {
        subToken = DefineStatSubToken.valueOf(firstItem);
    } catch (IllegalArgumentException e1) {
        return new ParseResult.Fail("Found unexpected sub tag " + firstItem + " in " + getTokenName() + Constants.COLON + value + ". Must be one of " + StringUtils.join(DefineStatSubToken.values(), ", ") + Constants.DOT, context);
    }
    if (!sep.hasNext()) {
        return new ParseResult.Fail(getTokenName() + Constants.COLON + subToken + "| must be followed by a stat.", context);
    }
    String statKey = sep.next();
    CDOMSingleRef<PCStat> stat = context.getReferenceContext().getCDOMReference(PCSTAT_CLASS, statKey);
    Formula f = null;
    if (subToken == DefineStatSubToken.LOCK || subToken == DefineStatSubToken.MINVALUE || subToken == DefineStatSubToken.MAXVALUE) {
        if (!sep.hasNext()) {
            return new ParseResult.Fail(getTokenName() + Constants.COLON + subToken + "| must be followed by both a stat and a value.", context);
        }
        String formula = sep.next();
        f = FormulaFactory.getFormulaFor(formula);
        if (!f.isValid()) {
            return new ParseResult.Fail("Formula in " + getTokenName() + " was not valid: " + f.toString(), context);
        }
    }
    if (sep.hasNext()) {
        return new ParseResult.Fail(getTokenName() + Constants.COLON + value + " has too many pipe separated item.", context);
    }
    switch(subToken) {
        case LOCK:
            context.getObjectContext().addToList(obj, ListKey.STAT_LOCKS, new StatLock(stat, f));
            break;
        case UNLOCK:
            context.getObjectContext().addToList(obj, ListKey.UNLOCKED_STATS, stat);
            break;
        case NONSTAT:
            context.getObjectContext().addToList(obj, ListKey.NONSTAT_STATS, stat);
            break;
        case STAT:
            context.getObjectContext().addToList(obj, ListKey.NONSTAT_TO_STAT_STATS, stat);
            break;
        case MINVALUE:
            context.getObjectContext().addToList(obj, ListKey.STAT_MINVALUE, new StatLock(stat, f));
            break;
        case MAXVALUE:
            context.getObjectContext().addToList(obj, ListKey.STAT_MAXVALUE, new StatLock(stat, f));
            break;
    }
    return ParseResult.SUCCESS;
}
Also used : Formula(pcgen.base.formula.Formula) ParsingSeparator(pcgen.base.text.ParsingSeparator) ParseResult(pcgen.rules.persistence.token.ParseResult) PCStat(pcgen.core.PCStat) Ungranted(pcgen.cdom.base.Ungranted) StatLock(pcgen.cdom.helper.StatLock)

Example 42 with Formula

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

the class FollowersLst 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 ((value == null) || value.isEmpty()) {
        return new ParseResult.Fail("Argument in " + getTokenName() + " cannot be empty", context);
    }
    ParsingSeparator sep = new ParsingSeparator(value, '|');
    sep.addGroupingPair('[', ']');
    sep.addGroupingPair('(', ')');
    String followerType = sep.next();
    if (followerType.isEmpty()) {
        return new ParseResult.Fail("Follower Type in " + getTokenName() + " cannot be empty", context);
    }
    if (!sep.hasNext()) {
        return new ParseResult.Fail(getTokenName() + " has no PIPE character: " + "Must be of the form <follower type>|<formula>", context);
    }
    String followerNumber = sep.next();
    if (sep.hasNext()) {
        return new ParseResult.Fail(getTokenName() + " has too many PIPE characters: " + "Must be of the form <follower type>|<formula", context);
    }
    if (followerNumber.isEmpty()) {
        return new ParseResult.Fail("Follower Count in " + getTokenName() + " cannot be empty", context);
    }
    CDOMSingleRef<CompanionList> cl = context.getReferenceContext().getCDOMReference(CompanionList.class, followerType);
    Formula num = FormulaFactory.getFormulaFor(followerNumber);
    if (!num.isValid()) {
        return new ParseResult.Fail("Number of Followers in " + getTokenName() + " was not valid: " + num.toString(), context);
    }
    context.getObjectContext().addToList(obj, ListKey.FOLLOWERS, new FollowerLimit(cl, num));
    return ParseResult.SUCCESS;
}
Also used : Formula(pcgen.base.formula.Formula) ParsingSeparator(pcgen.base.text.ParsingSeparator) CompanionList(pcgen.cdom.list.CompanionList) FollowerLimit(pcgen.cdom.helper.FollowerLimit) Ungranted(pcgen.cdom.base.Ungranted)

Example 43 with Formula

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

the class FollowersLst method unparse.

@Override
public String[] unparse(LoadContext context, CDOMObject obj) {
    Changes<FollowerLimit> changes = context.getObjectContext().getListChanges(obj, ListKey.FOLLOWERS);
    if (changes == null || changes.isEmpty()) {
        return null;
    }
    TreeSet<String> returnSet = new TreeSet<>();
    for (FollowerLimit fl : changes.getAdded()) {
        String followerType = fl.getCompanionList().getLSTformat(false);
        Formula followerNumber = fl.getValue();
        returnSet.add(followerType + Constants.PIPE + followerNumber.toString());
    }
    return returnSet.toArray(new String[returnSet.size()]);
}
Also used : Formula(pcgen.base.formula.Formula) TreeSet(java.util.TreeSet) FollowerLimit(pcgen.cdom.helper.FollowerLimit)

Example 44 with Formula

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

the class ChooseLst method unparse.

@Override
public String[] unparse(LoadContext context, CDOMObject obj) {
    String[] str = context.unparseSubtoken(obj, getTokenName());
    if (str == null) {
        return null;
    }
    Formula choices = context.getObjectContext().getFormula(obj, FormulaKey.NUMCHOICES);
    String choicesString = choices == null ? null : "NUMCHOICES=" + choices.toString() + Constants.PIPE;
    for (int i = 0; i < str.length; i++) {
        if (str[i].endsWith(Constants.PIPE)) {
            str[i] = str[i].substring(0, str[i].length() - 1);
        }
        if (choicesString != null) {
            str[i] = choicesString + str[i];
        }
        if (str[i].startsWith("FEATEQ|")) {
            str[i] = "FEAT=" + str[i].substring(7);
        }
    }
    return str;
}
Also used : Formula(pcgen.base.formula.Formula)

Example 45 with Formula

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

the class StatToken method parseTokenWithSeparator.

@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, KitStat kitStat, String value) {
    StringTokenizer st = new StringTokenizer(value, Constants.PIPE);
    while (st.hasMoreTokens()) {
        String token = st.nextToken();
        int equalLoc = token.indexOf('=');
        if (equalLoc == -1) {
            return new ParseResult.Fail("Illegal " + getTokenName() + " did not have Stat=X format: " + value, context);
        }
        if (equalLoc != token.lastIndexOf('=')) {
            return new ParseResult.Fail("Illegal " + getTokenName() + " had two equal signs, is not Stat=X format: " + value, context);
        }
        String statName = token.substring(0, equalLoc);
        if (statName.isEmpty()) {
            return new ParseResult.Fail("Illegal " + getTokenName() + " had no stat, is not Stat=X format: " + value, context);
        }
        CDOMSingleRef<PCStat> stat = context.getReferenceContext().getCDOMReference(PCStat.class, statName);
        String formula = token.substring(equalLoc + 1);
        if (formula.isEmpty()) {
            return new ParseResult.Fail("Unable to find STAT value: " + value, context);
        }
        Formula statValue = FormulaFactory.getFormulaFor(formula);
        if (!statValue.isValid()) {
            return new ParseResult.Fail("StatValue in " + getTokenName() + " was not valid: " + statValue.toString(), context);
        }
        kitStat.addStat(stat, statValue);
    }
    return ParseResult.SUCCESS;
}
Also used : Formula(pcgen.base.formula.Formula) StringTokenizer(java.util.StringTokenizer) PCStat(pcgen.core.PCStat)

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