Search in sources :

Example 11 with ConcretePersistentTransitionChoice

use of pcgen.cdom.base.ConcretePersistentTransitionChoice 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 12 with ConcretePersistentTransitionChoice

use of pcgen.cdom.base.ConcretePersistentTransitionChoice in project pcgen by PCGen.

the class LanguageToken method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, CDOMObject obj, String value) {
    ParsingSeparator sep = new ParsingSeparator(value, '|');
    sep.addGroupingPair('[', ']');
    sep.addGroupingPair('(', ')');
    String activeValue = sep.next();
    Formula count;
    if (!sep.hasNext()) {
        count = FormulaFactory.ONE;
    } else {
        count = FormulaFactory.getFormulaFor(activeValue);
        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);
        }
        activeValue = sep.next();
    }
    if (sep.hasNext()) {
        return new ParseResult.Fail(getFullName() + " had too many pipe separated items: " + value, context);
    }
    ParseResult pr = checkSeparatorsAndNonEmpty(',', activeValue);
    if (!pr.passed()) {
        return pr;
    }
    List<CDOMReference<Language>> refs = new ArrayList<>();
    StringTokenizer tok = new StringTokenizer(activeValue, Constants.COMMA);
    while (tok.hasMoreTokens()) {
        String tokText = tok.nextToken();
        CDOMReference<Language> lang = TokenUtilities.getReference(context, LANGUAGE_CLASS, tokText);
        if (lang == null) {
            return new ParseResult.Fail("  Error was encountered while parsing " + getFullName() + ": " + value + " had an invalid reference: " + tokText, context);
        }
        refs.add(lang);
    }
    ReferenceChoiceSet<Language> rcs = new ReferenceChoiceSet<>(refs);
    if (!rcs.getGroupingState().isValid()) {
        return new ParseResult.Fail("Non-sensical " + getFullName() + ": Contains ANY and a specific reference: " + value, context);
    }
    ChoiceSet<Language> cs = new ChoiceSet<>(getTokenName(), rcs);
    cs.setTitle("Language Choice");
    PersistentTransitionChoice<Language> tc = new ConcretePersistentTransitionChoice<>(cs, count);
    context.getObjectContext().addToList(obj, ListKey.ADD, tc);
    tc.setChoiceActor(this);
    return ParseResult.SUCCESS;
}
Also used : ParseResult(pcgen.rules.persistence.token.ParseResult) ReferenceChoiceSet(pcgen.cdom.choiceset.ReferenceChoiceSet) ChoiceSet(pcgen.cdom.base.ChoiceSet) ArrayList(java.util.ArrayList) ConcretePersistentTransitionChoice(pcgen.cdom.base.ConcretePersistentTransitionChoice) Formula(pcgen.base.formula.Formula) StringTokenizer(java.util.StringTokenizer) ParsingSeparator(pcgen.base.text.ParsingSeparator) Language(pcgen.core.Language) CDOMReference(pcgen.cdom.base.CDOMReference) ReferenceChoiceSet(pcgen.cdom.choiceset.ReferenceChoiceSet)

Example 13 with ConcretePersistentTransitionChoice

use of pcgen.cdom.base.ConcretePersistentTransitionChoice in project pcgen by PCGen.

the class SkillToken method parseToken.

@Override
public ParseResult parseToken(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 activeValue = sep.next();
    Formula count;
    if (!sep.hasNext()) {
        count = FormulaFactory.ONE;
    } else {
        count = FormulaFactory.getFormulaFor(activeValue);
        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);
        }
        activeValue = sep.next();
    }
    if (sep.hasNext()) {
        return new ParseResult.Fail(getFullName() + " had too many pipe separated items: " + value, context);
    }
    ParseResult pr = checkSeparatorsAndNonEmpty(',', activeValue);
    if (!pr.passed()) {
        return pr;
    }
    StringTokenizer tok = new StringTokenizer(activeValue, Constants.COMMA);
    List<CDOMReference<Skill>> refs = new ArrayList<>();
    while (tok.hasMoreTokens()) {
        String token = tok.nextToken();
        CDOMReference<Skill> ref;
        if (Constants.LST_ANY.equalsIgnoreCase(token)) {
            ref = context.getReferenceContext().getCDOMAllReference(SKILL_CLASS);
        } else {
            ref = TokenUtilities.getTypeOrPrimitive(context, SKILL_CLASS, token);
            if (ref == null) {
                return new ParseResult.Fail("  Error was encountered while parsing " + getFullName() + ": " + token + " is not a valid reference: " + value, context);
            }
        }
        refs.add(ref);
    }
    ReferenceChoiceSet<Skill> rcs = new ReferenceChoiceSet<>(refs);
    if (!rcs.getGroupingState().isValid()) {
        return new ParseResult.Fail("Non-sensical " + getFullName() + ": Contains ANY and a specific reference: " + value, context);
    }
    ChoiceSet<Skill> cs = new ChoiceSet<>("SKILL", rcs, true);
    PersistentTransitionChoice<Skill> tc = new ConcretePersistentTransitionChoice<>(cs, count);
    context.getObjectContext().addToList(obj, ListKey.ADD, tc);
    tc.setChoiceActor(this);
    tc.allowStack(true);
    return ParseResult.SUCCESS;
}
Also used : ParseResult(pcgen.rules.persistence.token.ParseResult) ReferenceChoiceSet(pcgen.cdom.choiceset.ReferenceChoiceSet) ChoiceSet(pcgen.cdom.base.ChoiceSet) ArrayList(java.util.ArrayList) ConcretePersistentTransitionChoice(pcgen.cdom.base.ConcretePersistentTransitionChoice) Formula(pcgen.base.formula.Formula) StringTokenizer(java.util.StringTokenizer) Skill(pcgen.core.Skill) ParsingSeparator(pcgen.base.text.ParsingSeparator) CDOMReference(pcgen.cdom.base.CDOMReference) ReferenceChoiceSet(pcgen.cdom.choiceset.ReferenceChoiceSet)

Example 14 with ConcretePersistentTransitionChoice

use of pcgen.cdom.base.ConcretePersistentTransitionChoice in project pcgen by PCGen.

the class SpellCasterToken method parseToken.

@Override
public ParseResult parseToken(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 activeValue = sep.next();
    Formula count;
    if (!sep.hasNext()) {
        count = FormulaFactory.ONE;
    } else {
        count = FormulaFactory.getFormulaFor(activeValue);
        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);
        }
        activeValue = sep.next();
    }
    if (sep.hasNext()) {
        return new ParseResult.Fail(getFullName() + " had too many pipe separated items: " + value, context);
    }
    ParseResult pr = checkSeparatorsAndNonEmpty(',', activeValue);
    if (!pr.passed()) {
        return pr;
    }
    StringTokenizer tok = new StringTokenizer(activeValue, Constants.COMMA);
    boolean foundAny = false;
    boolean foundOther = false;
    List<CDOMReference<PCClass>> groups = new ArrayList<>();
    List<CDOMReference<PCClass>> prims = new ArrayList<>();
    List<String> spelltypes = new ArrayList<>();
    CDOMGroupRef<PCClass> allRef = context.getReferenceContext().getCDOMAllReference(PCCLASS_CLASS);
    while (tok.hasMoreTokens()) {
        String token = tok.nextToken();
        if (Constants.LST_ANY.equalsIgnoreCase(token)) {
            foundAny = true;
            groups.add(allRef);
        } else {
            foundOther = true;
            if (token.equals("Arcane") || token.equals("Divine") || token.equals("Psionic")) {
                spelltypes.add(token);
            } else if (token.startsWith(Constants.LST_TYPE_DOT) || token.startsWith(Constants.LST_TYPE_EQUAL)) {
                CDOMReference<PCClass> ref = TokenUtilities.getTypeReference(context, PCCLASS_CLASS, token.substring(5));
                if (ref == null) {
                    return new ParseResult.Fail("  Error was encountered while parsing " + getFullName() + ": " + token + " is not a valid reference: " + value, context);
                }
                groups.add(ref);
            } else {
                prims.add(context.getReferenceContext().getCDOMReference(PCCLASS_CLASS, token));
            }
        }
    }
    if (foundAny && foundOther) {
        return new ParseResult.Fail("Non-sensical " + getFullName() + ": Contains ANY and a specific reference: " + value, context);
    }
    ReferenceChoiceSet<PCClass> grcs = groups.isEmpty() ? null : new ReferenceChoiceSet<>(groups);
    ReferenceChoiceSet<PCClass> prcs = prims.isEmpty() ? null : new ReferenceChoiceSet<>(prims);
    SelectableSet<PCClass> cs = new SpellCasterChoiceSet(allRef, spelltypes, grcs, prcs);
    cs.setTitle("Spell Caster Class Choice");
    PersistentTransitionChoice<PCClass> tc = new ConcretePersistentTransitionChoice<>(cs, count);
    context.getObjectContext().addToList(obj, ListKey.ADD, tc);
    tc.setChoiceActor(this);
    return ParseResult.SUCCESS;
}
Also used : ParseResult(pcgen.rules.persistence.token.ParseResult) SpellCasterChoiceSet(pcgen.cdom.choiceset.SpellCasterChoiceSet) ArrayList(java.util.ArrayList) PCClass(pcgen.core.PCClass) ConcretePersistentTransitionChoice(pcgen.cdom.base.ConcretePersistentTransitionChoice) Formula(pcgen.base.formula.Formula) StringTokenizer(java.util.StringTokenizer) ParsingSeparator(pcgen.base.text.ParsingSeparator) CDOMReference(pcgen.cdom.base.CDOMReference)

Example 15 with ConcretePersistentTransitionChoice

use of pcgen.cdom.base.ConcretePersistentTransitionChoice in project pcgen by PCGen.

the class AbilityTokenTest method createTC.

private void createTC(List<CDOMReference<Ability>> refs, Formula count) {
    AbilityRefChoiceSet rcs = new AbilityRefChoiceSet(CDOMDirectSingleRef.getRef(AbilityCategory.FEAT), refs, Nature.NORMAL);
    // TODO: Should this be present for the unit tests?
    //assertTrue("Invalid grouping state " + rcs.getGroupingState(), rcs.getGroupingState().isValid());
    AbilityChoiceSet cs = new AbilityChoiceSet(getSubToken().getTokenName(), rcs);
    cs.setTitle("Virtual Feat Selection");
    PersistentTransitionChoice<CNAbilitySelection> tc = new ConcretePersistentTransitionChoice<>(cs, count);
    tc.allowStack(false);
    // if (dupChoices != 0)
    // {
    // tc.setStackLimit(dupChoices);
    // }
    tc.setChoiceActor(subtoken);
    primaryProf.addToListFor(ListKey.ADD, tc);
}
Also used : AbilityChoiceSet(pcgen.cdom.base.ChoiceSet.AbilityChoiceSet) CNAbilitySelection(pcgen.cdom.helper.CNAbilitySelection) AbilityRefChoiceSet(pcgen.cdom.choiceset.AbilityRefChoiceSet) ConcretePersistentTransitionChoice(pcgen.cdom.base.ConcretePersistentTransitionChoice)

Aggregations

ConcretePersistentTransitionChoice (pcgen.cdom.base.ConcretePersistentTransitionChoice)24 CDOMReference (pcgen.cdom.base.CDOMReference)21 ChoiceSet (pcgen.cdom.base.ChoiceSet)20 ArrayList (java.util.ArrayList)19 ReferenceChoiceSet (pcgen.cdom.choiceset.ReferenceChoiceSet)18 Test (org.junit.Test)13 Skill (pcgen.core.Skill)13 ClassSkillChoiceActor (pcgen.cdom.helper.ClassSkillChoiceActor)12 ObjectMatchingReference (pcgen.cdom.reference.ObjectMatchingReference)10 Formula (pcgen.base.formula.Formula)9 ParsingSeparator (pcgen.base.text.ParsingSeparator)9 ParseResult (pcgen.rules.persistence.token.ParseResult)9 StringTokenizer (java.util.StringTokenizer)7 AbilityRefChoiceSet (pcgen.cdom.choiceset.AbilityRefChoiceSet)5 CNAbilitySelection (pcgen.cdom.helper.CNAbilitySelection)5 AbilityChoiceSet (pcgen.cdom.base.ChoiceSet.AbilityChoiceSet)3 CNAbility (pcgen.cdom.content.CNAbility)2 Nature (pcgen.cdom.enumeration.Nature)2 Ability (pcgen.core.Ability)2 AbilityCategory (pcgen.core.AbilityCategory)2