Search in sources :

Example 1 with Ungranted

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

the class SpellsLst method parseNonEmptyToken.

/**
	 * {@literal 
	 * SPELLS:<spellbook name>|[<optional parameters, pipe deliminated>] |<spell
	 * name>[,<formula for DC>] |<spell name2>[,<formula2 for DC>] |PRExxx
	 * |PRExxx
	 *
	 * CASTERLEVEL=<formula> Casterlevel of spells TIMES=<formula> Cast Times
	 * per day, -1=At Will
	 *}
	 * @param sourceLine
	 *            Line from the LST file without the SPELLS:
	 * @return spells list
	 */
@Override
protected ParseResult parseNonEmptyToken(LoadContext context, CDOMObject obj, String sourceLine) {
    if (obj instanceof Ungranted) {
        return new ParseResult.Fail("Cannot use " + getTokenName() + " on an Ungranted object type: " + obj.getClass().getSimpleName(), context);
    }
    if ((sourceLine == null) || sourceLine.isEmpty()) {
        return new ParseResult.Fail("Argument in " + getTokenName() + " cannot be empty", context);
    }
    if (sourceLine.equals(Constants.LST_DOT_CLEAR_ALL)) {
        context.getListContext().removeAllFromList(getTokenName(), obj, Spell.SPELLS);
        return ParseResult.SUCCESS;
    }
    ParsingSeparator sep = new ParsingSeparator(sourceLine, '|');
    sep.addGroupingPair('[', ']');
    sep.addGroupingPair('(', ')');
    String spellBook = sep.next();
    if (spellBook.isEmpty()) {
        return new ParseResult.Fail("SpellBook in " + getTokenName() + " cannot be empty", context);
    }
    // Formula casterLevel = null;
    String casterLevel = null;
    String times = null;
    String timeunit = null;
    if (!sep.hasNext()) {
        return new ParseResult.Fail(getTokenName() + ": minimally requires a Spell Name", context);
    }
    String token = sep.next();
    while (true) {
        if (token.startsWith("TIMES=")) {
            if (times != null) {
                return new ParseResult.Fail("Found two TIMES entries in " + getTokenName() + ": invalid: " + sourceLine, context);
            }
            times = token.substring(6);
            if (times.isEmpty()) {
                return new ParseResult.Fail("Error in Times in " + getTokenName() + ": argument was empty", context);
            }
            if (!sep.hasNext()) {
                return new ParseResult.Fail(getTokenName() + ": minimally requires " + "a Spell Name (after TIMES=)", context);
            }
            token = sep.next();
        } else if (token.startsWith("TIMEUNIT=")) {
            if (timeunit != null) {
                return new ParseResult.Fail("Found two TIMEUNIT entries in " + getTokenName() + ": invalid: " + sourceLine, context);
            }
            timeunit = token.substring(9);
            if (timeunit.isEmpty()) {
                return new ParseResult.Fail("Error in TimeUnit in " + getTokenName() + ": argument was empty", context);
            }
            if (!sep.hasNext()) {
                return new ParseResult.Fail(getTokenName() + ": minimally requires " + "a Spell Name (after TIMEUNIT=)", context);
            }
            token = sep.next();
        } else if (token.startsWith("CASTERLEVEL=")) {
            if (casterLevel != null) {
                return new ParseResult.Fail("Found two CASTERLEVEL entries in " + getTokenName() + ": invalid: " + sourceLine, context);
            }
            casterLevel = token.substring(12);
            if (casterLevel.isEmpty()) {
                return new ParseResult.Fail("Error in Caster Level in " + getTokenName() + ": argument was empty", context);
            }
            if (!sep.hasNext()) {
                return new ParseResult.Fail(getTokenName() + ": minimally requires a " + "Spell Name (after CASTERLEVEL=)", context);
            }
            token = sep.next();
        } else {
            break;
        }
    }
    if (times == null) {
        times = "1";
    }
    if (token.isEmpty()) {
        return new ParseResult.Fail("Spell arguments may not be empty in " + getTokenName() + ": " + sourceLine, context);
    }
    if (token.charAt(0) == ',') {
        return new ParseResult.Fail(getTokenName() + " Spell arguments may not start with , : " + token, context);
    }
    if (token.charAt(token.length() - 1) == ',') {
        return new ParseResult.Fail(getTokenName() + " Spell arguments may not end with , : " + token, context);
    }
    if (token.indexOf(",,") != -1) {
        return new ParseResult.Fail(getTokenName() + " Spell arguments uses double separator ,, : " + token, context);
    }
    /*
		 * CONSIDER This is currently order enforcing the reference fetching to
		 * match the integration tests that we perform, and their current
		 * behavior. Not sure if this is really tbe best solution?
		 *
		 * See CDOMObject.
		 */
    DoubleKeyMap<CDOMReference<Spell>, AssociationKey<?>, Object> dkm = new DoubleKeyMap<>(LinkedHashMap.class, HashMap.class);
    while (true) {
        if (token.isEmpty()) {
            return new ParseResult.Fail("Spell arguments may not end with comma or pipe in " + getTokenName() + ": " + sourceLine, context);
        }
        int commaLoc = token.indexOf(',');
        String name = commaLoc == -1 ? token : token.substring(0, commaLoc);
        CDOMReference<Spell> spell = context.getReferenceContext().getCDOMReference(Spell.class, name);
        dkm.put(spell, AssociationKey.CASTER_LEVEL, casterLevel);
        Formula timesFormula = FormulaFactory.getFormulaFor(times);
        if (!timesFormula.isValid()) {
            return new ParseResult.Fail("Times in " + getTokenName() + " was not valid: " + timesFormula.toString(), context);
        }
        dkm.put(spell, AssociationKey.TIMES_PER_UNIT, timesFormula);
        if (timeunit != null) {
            dkm.put(spell, AssociationKey.TIME_UNIT, timeunit);
        }
        dkm.put(spell, AssociationKey.SPELLBOOK, spellBook);
        if (commaLoc != -1) {
            dkm.put(spell, AssociationKey.DC_FORMULA, token.substring(commaLoc + 1));
        }
        if (!sep.hasNext()) {
            // No prereqs, so we're done
            finish(context, obj, dkm, null);
            return ParseResult.SUCCESS;
        }
        token = sep.next();
        if (looksLikeAPrerequisite(token)) {
            break;
        }
    }
    List<Prerequisite> prereqs = new ArrayList<>();
    while (true) {
        Prerequisite prereq = getPrerequisite(token);
        if (prereq == null) {
            return new ParseResult.Fail("   (Did you put spells after the " + "PRExxx tags in SPELLS:?)", context);
        }
        prereqs.add(prereq);
        if (!sep.hasNext()) {
            break;
        }
        token = sep.next();
    }
    finish(context, obj, dkm, prereqs);
    return ParseResult.SUCCESS;
}
Also used : AssociationKey(pcgen.cdom.enumeration.AssociationKey) ParseResult(pcgen.rules.persistence.token.ParseResult) ArrayList(java.util.ArrayList) Ungranted(pcgen.cdom.base.Ungranted) Spell(pcgen.core.spell.Spell) Formula(pcgen.base.formula.Formula) ParsingSeparator(pcgen.base.text.ParsingSeparator) CDOMObject(pcgen.cdom.base.CDOMObject) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject) DoubleKeyMap(pcgen.base.util.DoubleKeyMap) CDOMReference(pcgen.cdom.base.CDOMReference) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 2 with Ungranted

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

the class RegionLst 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);
    String item = tok.nextToken();
    Formula count = FormulaFactory.getFormulaFor(item);
    if (!count.isValid()) {
        return new ParseResult.Fail("Count in " + getTokenName() + " was not valid: " + count.toString(), context);
    }
    if (count.isStatic()) {
        if (!tok.hasMoreTokens()) {
            return new ParseResult.Fail(getTokenName() + " cannot have only a count: " + value, context);
        }
        item = tok.nextToken();
        if (count.resolveStatic().intValue() <= 0) {
            return new ParseResult.Fail("Count in " + getTokenName() + " must be > 0: " + value, context);
        }
    } else {
        count = FormulaFactory.ONE;
    }
    List<Region> regions = new ArrayList<>();
    while (true) {
        regions.add(Region.getConstant(item));
        if (!tok.hasMoreTokens()) {
            break;
        }
        item = tok.nextToken();
    }
    SimpleChoiceSet<Region> rcs = new SimpleChoiceSet<>(regions);
    ChoiceSet<Region> cs = new ChoiceSet<>(getTokenName(), rcs);
    cs.setTitle("Region Selection");
    TransitionChoice<Region> tc = new ConcreteTransitionChoice<>(cs, count);
    context.getObjectContext().put(obj, ObjectKey.REGION_CHOICE, tc);
    tc.setRequired(false);
    tc.setChoiceActor(this);
    return ParseResult.SUCCESS;
}
Also used : ConcreteTransitionChoice(pcgen.cdom.base.ConcreteTransitionChoice) SimpleChoiceSet(pcgen.cdom.choiceset.SimpleChoiceSet) ChoiceSet(pcgen.cdom.base.ChoiceSet) ArrayList(java.util.ArrayList) Ungranted(pcgen.cdom.base.Ungranted) Formula(pcgen.base.formula.Formula) StringTokenizer(java.util.StringTokenizer) SimpleChoiceSet(pcgen.cdom.choiceset.SimpleChoiceSet) NonInteractive(pcgen.cdom.base.NonInteractive) Region(pcgen.cdom.enumeration.Region)

Example 3 with Ungranted

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

the class SpellknownLst 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);
    }
    String workingValue = value;
    List<Prerequisite> prereqs = new ArrayList<>();
    while (true) {
        int lastPipeLoc = workingValue.lastIndexOf('|');
        if (lastPipeLoc == -1) {
            return new ParseResult.Fail("Invalid " + getTokenName() + " not enough tokens: " + value, context);
        }
        String lastToken = workingValue.substring(lastPipeLoc + 1);
        if (looksLikeAPrerequisite(lastToken)) {
            workingValue = workingValue.substring(0, lastPipeLoc);
            Prerequisite prerequisite = getPrerequisite(lastToken);
            if (prerequisite == null) {
                return new ParseResult.Fail("Invalid prerequisite " + lastToken + " in " + getTokenName() + " tag: " + value, context);
            }
            prereqs.add(prerequisite);
        } else {
            break;
        }
    }
    StringTokenizer tok = new StringTokenizer(workingValue, Constants.PIPE);
    if (tok.countTokens() < 3) {
        return new ParseResult.Fail("Insufficient values in SPELLKNOWN tag: " + value, context);
    }
    // CLASS only
    String tagType = tok.nextToken();
    while (tok.hasMoreTokens()) {
        String tokString = tok.nextToken();
        String spellString = tok.nextToken();
        if (tagType.equalsIgnoreCase("CLASS")) {
            if (!subParse(context, obj, ClassSpellList.class, tokString, spellString, prereqs)) {
                return ParseResult.INTERNAL_ERROR;
            //return new ParseResult.Fail("  " + getTokenName()
            //		+ " error - entire token was " + value, context);
            }
        } else {
            return new ParseResult.Fail("First token of " + getTokenName() + " must be CLASS: " + value, context);
        }
    }
    return ParseResult.SUCCESS;
}
Also used : StringTokenizer(java.util.StringTokenizer) ArrayList(java.util.ArrayList) ClassSpellList(pcgen.cdom.list.ClassSpellList) Ungranted(pcgen.cdom.base.Ungranted) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 4 with Ungranted

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

the class VisionLst 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);
    }
    StringTokenizer aTok = new StringTokenizer(value, Constants.PIPE);
    String visionString = aTok.nextToken();
    if (looksLikeAPrerequisite(visionString)) {
        return new ParseResult.Fail("Cannot have only PRExxx subtoken in " + getTokenName() + ": " + value, context);
    }
    ArrayList<AssociatedPrereqObject> edgeList = new ArrayList<>();
    boolean foundClear = false;
    while (true) {
        if (Constants.LST_DOT_CLEAR.equals(visionString)) {
            context.getListContext().removeAllFromList(getTokenName(), obj, Vision.VISIONLIST);
            foundClear = true;
        } else if (visionString.startsWith(Constants.LST_DOT_CLEAR_DOT)) {
            try {
                Vision vis = Vision.getVision(visionString.substring(7));
                context.getListContext().removeFromList(getTokenName(), obj, Vision.VISIONLIST, new CDOMDirectSingleRef<>(vis));
            } catch (IllegalArgumentException e) {
                ComplexParseResult cpr = new ComplexParseResult();
                cpr.addErrorMessage("Bad Syntax for Cleared Vision in " + getTokenName());
                cpr.addErrorMessage(e.getMessage());
                return cpr;
            }
            foundClear = true;
        } else if (looksLikeAPrerequisite(visionString)) {
            break;
        } else {
            try {
                Vision vision = Vision.getVision(visionString);
                AssociatedPrereqObject edge = context.getListContext().addToList(getTokenName(), obj, Vision.VISIONLIST, new CDOMDirectSingleRef<>(vision));
                edgeList.add(edge);
            } catch (IllegalArgumentException e) {
                ComplexParseResult cpr = new ComplexParseResult();
                cpr.addErrorMessage("Bad Syntax for Vision in " + getTokenName());
                cpr.addErrorMessage(e.getMessage());
                return cpr;
            }
        }
        if (!aTok.hasMoreTokens()) {
            return ParseResult.SUCCESS;
        }
        visionString = aTok.nextToken();
    }
    if (foundClear) {
        return new ParseResult.Fail("Cannot use PREREQs when using .CLEAR or .CLEAR. in " + getTokenName(), context);
    }
    while (true) {
        Prerequisite prereq = getPrerequisite(visionString);
        if (prereq == null) {
            return new ParseResult.Fail("   (Did you put vision after the " + "PRExxx tags in " + getTokenName() + ":?)", context);
        }
        for (AssociatedPrereqObject edge : edgeList) {
            edge.addPrerequisite(prereq);
        }
        if (!aTok.hasMoreTokens()) {
            break;
        }
        visionString = aTok.nextToken();
    }
    return ParseResult.SUCCESS;
}
Also used : CDOMDirectSingleRef(pcgen.cdom.reference.CDOMDirectSingleRef) ArrayList(java.util.ArrayList) ComplexParseResult(pcgen.rules.persistence.token.ComplexParseResult) Ungranted(pcgen.cdom.base.Ungranted) StringTokenizer(java.util.StringTokenizer) Vision(pcgen.core.Vision) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 5 with Ungranted

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

the class MoveLst 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);
    }
    StringTokenizer moves = new StringTokenizer(value, Constants.COMMA);
    Movement cm;
    if (moves.countTokens() == 1) {
        cm = new Movement(1);
        String mod = moves.nextToken();
        ParseResult pr = validateMove(value, mod);
        if (!pr.passed()) {
            return pr;
        }
        cm.assignMovement(0, "Walk", mod);
    } else {
        cm = new Movement(moves.countTokens() / 2);
        int x = 0;
        while (moves.countTokens() > 1) {
            String type = moves.nextToken();
            String mod = moves.nextToken();
            ParseResult pr = validateMove(value, mod);
            if (!pr.passed()) {
                return pr;
            }
            cm.assignMovement(x++, type, mod);
        }
        if (moves.countTokens() != 0) {
            return new ParseResult.Fail("Badly formed MOVE token " + "(extra value at end of list): " + value, context);
        }
    }
    cm.setMoveRatesFlag(0);
    context.getObjectContext().addToList(obj, ListKey.MOVEMENT, cm);
    return ParseResult.SUCCESS;
}
Also used : StringTokenizer(java.util.StringTokenizer) Movement(pcgen.core.Movement) ParseResult(pcgen.rules.persistence.token.ParseResult) Ungranted(pcgen.cdom.base.Ungranted)

Aggregations

Ungranted (pcgen.cdom.base.Ungranted)28 StringTokenizer (java.util.StringTokenizer)19 ParseResult (pcgen.rules.persistence.token.ParseResult)15 ArrayList (java.util.ArrayList)10 Formula (pcgen.base.formula.Formula)10 Prerequisite (pcgen.core.prereq.Prerequisite)8 CDOMReference (pcgen.cdom.base.CDOMReference)7 ComplexParseResult (pcgen.rules.persistence.token.ComplexParseResult)6 ParsingSeparator (pcgen.base.text.ParsingSeparator)4 NonInteractive (pcgen.cdom.base.NonInteractive)4 AssociatedPrereqObject (pcgen.cdom.base.AssociatedPrereqObject)3 Skill (pcgen.core.Skill)3 ChoiceSet (pcgen.cdom.base.ChoiceSet)2 ConcreteTransitionChoice (pcgen.cdom.base.ConcreteTransitionChoice)2 PCClassLevel (pcgen.cdom.inst.PCClassLevel)2 ClassSpellList (pcgen.cdom.list.ClassSpellList)2 Ability (pcgen.core.Ability)2 Movement (pcgen.core.Movement)2 PCTemplate (pcgen.core.PCTemplate)2 Race (pcgen.core.Race)2