Search in sources :

Example 1 with PrereqObject

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

the class DomainToken method parseTokenWithSeparator.

@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, PCClass pcc, String value) {
    StringTokenizer pipeTok = new StringTokenizer(value, Constants.PIPE);
    boolean first = true;
    String tok = pipeTok.nextToken();
    if (looksLikeAPrerequisite(tok)) {
        return new ParseResult.Fail("Cannot have only PRExxx subtoken in " + getTokenName() + ": " + value, context);
    }
    List<QualifiedObject<CDOMSingleRef<Domain>>> toAdd = new ArrayList<>();
    boolean foundClear = false;
    while (true) {
        if (Constants.LST_DOT_CLEAR.equals(tok)) {
            if (!first) {
                return new ParseResult.Fail("  Non-sensical " + getTokenName() + ": .CLEAR was not the first list item", context);
            }
            context.getObjectContext().removeList(pcc, ListKey.DOMAIN);
            foundClear = true;
        } else {
            CDOMSingleRef<Domain> domain = context.getReferenceContext().getCDOMReference(DOMAIN_CLASS, tok);
            QualifiedObject<CDOMSingleRef<Domain>> qo = new QualifiedObject<>(domain);
            toAdd.add(qo);
            context.getObjectContext().addToList(pcc, ListKey.DOMAIN, qo);
        }
        first = false;
        if (!pipeTok.hasMoreTokens()) {
            // No prereqs, so we're done
            return ParseResult.SUCCESS;
        }
        tok = pipeTok.nextToken();
        if (looksLikeAPrerequisite(tok)) {
            break;
        }
    }
    if (foundClear) {
        return new ParseResult.Fail("Cannot use PREREQs when using .CLEAR in " + getTokenName(), context);
    }
    while (true) {
        Prerequisite prereq = getPrerequisite(tok);
        if (prereq == null) {
            return new ParseResult.Fail("   (Did you put feats after the " + "PRExxx tags in " + getTokenName() + ":?)", context);
        }
        for (PrereqObject pro : toAdd) {
            pro.addPrerequisite(prereq);
        }
        if (!pipeTok.hasMoreTokens()) {
            break;
        }
        tok = pipeTok.nextToken();
    }
    return ParseResult.SUCCESS;
}
Also used : ArrayList(java.util.ArrayList) CDOMSingleRef(pcgen.cdom.reference.CDOMSingleRef) PrereqObject(pcgen.cdom.base.PrereqObject) StringTokenizer(java.util.StringTokenizer) QualifiedObject(pcgen.core.QualifiedObject) Domain(pcgen.core.Domain) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 2 with PrereqObject

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

the class DomainToken method parseTokenWithSeparator.

@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, PCClassLevel pcl, String value) {
    StringTokenizer pipeTok = new StringTokenizer(value, Constants.PIPE);
    boolean first = true;
    String tok = pipeTok.nextToken();
    if (looksLikeAPrerequisite(tok)) {
        return new ParseResult.Fail("Cannot have only PRExxx subtoken in " + getTokenName() + ": " + value, context);
    }
    List<QualifiedObject<CDOMSingleRef<Domain>>> toAdd = new ArrayList<>();
    boolean foundClear = false;
    while (true) {
        if (Constants.LST_DOT_CLEAR.equals(tok)) {
            if (!first) {
                return new ParseResult.Fail("  Non-sensical " + getTokenName() + ": .CLEAR was not the first list item", context);
            }
            context.getObjectContext().removeList(pcl, ListKey.DOMAIN);
            foundClear = true;
        } else {
            CDOMSingleRef<Domain> domain = context.getReferenceContext().getCDOMReference(DOMAIN_CLASS, tok);
            QualifiedObject<CDOMSingleRef<Domain>> qo = new QualifiedObject<>(domain);
            toAdd.add(qo);
            context.getObjectContext().addToList(pcl, ListKey.DOMAIN, qo);
        }
        first = false;
        if (!pipeTok.hasMoreTokens()) {
            // No prereqs, so we're done
            return ParseResult.SUCCESS;
        }
        tok = pipeTok.nextToken();
        if (looksLikeAPrerequisite(tok)) {
            break;
        }
    }
    if (foundClear) {
        return new ParseResult.Fail("Cannot use PREREQs when using .CLEAR in " + getTokenName(), context);
    }
    while (true) {
        Prerequisite prereq = getPrerequisite(tok);
        if (prereq == null) {
            return new ParseResult.Fail("   (Did you put feats after the " + "PRExxx tags in " + getTokenName() + ":?)", context);
        }
        for (PrereqObject pro : toAdd) {
            pro.addPrerequisite(prereq);
        }
        if (!pipeTok.hasMoreTokens()) {
            break;
        }
        tok = pipeTok.nextToken();
    }
    return ParseResult.SUCCESS;
}
Also used : ArrayList(java.util.ArrayList) CDOMSingleRef(pcgen.cdom.reference.CDOMSingleRef) PrereqObject(pcgen.cdom.base.PrereqObject) StringTokenizer(java.util.StringTokenizer) QualifiedObject(pcgen.core.QualifiedObject) Domain(pcgen.core.Domain) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 3 with PrereqObject

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

the class AbilityLst 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 tok = new StringTokenizer(value, Constants.PIPE);
    String cat = tok.nextToken();
    CDOMSingleRef<AbilityCategory> acRef = context.getReferenceContext().getCDOMReference(ABILITY_CATEGORY_CLASS, cat);
    if (!tok.hasMoreTokens()) {
        return new ParseResult.Fail(getTokenName() + " must have a Nature, " + "Format is: CATEGORY|NATURE|AbilityName: " + value, context);
    }
    final String natureKey = tok.nextToken();
    Nature nature;
    try {
        nature = Nature.valueOf(natureKey);
    } catch (IllegalArgumentException iae) {
        return new ParseResult.Fail(getTokenName() + " refers to invalid Ability Nature: " + natureKey, context);
    }
    if (Nature.ANY.equals(nature)) {
        return new ParseResult.Fail(getTokenName() + " refers to ANY Ability Nature, cannot be used in " + getTokenName() + ": " + value, context);
    }
    if (!tok.hasMoreTokens()) {
        return new ParseResult.Fail(getTokenName() + " must have abilities, Format is: " + "CATEGORY|NATURE|AbilityName: " + value, context);
    }
    String token = tok.nextToken();
    if (looksLikeAPrerequisite(token)) {
        return new ParseResult.Fail("Cannot have only PRExxx subtoken in " + getTokenName() + ": " + value, context);
    }
    String lkString = "GA_CA_" + cat + '_' + natureKey;
    ListKey glk = ListKey.getKeyFor(ChooseSelectionActor.class, lkString);
    ListKey<ChooseSelectionActor<?>> lk = glk;
    ArrayList<PrereqObject> edgeList = new ArrayList<>();
    CDOMReference<AbilityList> abilList = AbilityList.getAbilityListReference(acRef, nature);
    boolean first = true;
    boolean removed = false;
    ReferenceManufacturer<Ability> rm = context.getReferenceContext().getManufacturer(ABILITY_CLASS, ABILITY_CATEGORY_CLASS, cat);
    if (rm == null) {
        return new ParseResult.Fail("Could not get Reference Manufacturer for Category: " + cat, context);
    }
    boolean prereqsAllowed = true;
    while (true) {
        if (Constants.LST_DOT_CLEAR.equals(token)) {
            if (!first) {
                return new ParseResult.Fail("  Non-sensical " + getTokenName() + ": .CLEAR was not the first list item: " + value, context);
            }
            context.getListContext().removeAllFromList(getTokenName(), obj, abilList);
            context.getObjectContext().removeFromList(obj, ListKey.GA_CAKEYS, lk);
            context.getObjectContext().removeList(obj, lk);
            removed = true;
        } else if (token.startsWith(Constants.LST_DOT_CLEAR_DOT)) {
            String clearText = token.substring(7);
            CDOMReference<Ability> ref = TokenUtilities.getTypeOrPrimitive(rm, clearText);
            if (ref == null) {
                return ParseResult.INTERNAL_ERROR;
            }
            AssociatedPrereqObject assoc = context.getListContext().removeFromList(getTokenName(), obj, abilList, ref);
            assoc.setAssociation(AssociationKey.NATURE, nature);
            assoc.setAssociation(AssociationKey.CATEGORY, acRef);
            removed = true;
        } else if (Constants.LST_PERCENT_LIST.equals(token)) {
            prereqsAllowed = false;
            AbilitySelector as = new AbilitySelector(getTokenName(), acRef, nature);
            context.getObjectContext().addToList(obj, ListKey.NEW_CHOOSE_ACTOR, as);
        } else {
            CDOMReference<Ability> ability = TokenUtilities.getTypeOrPrimitive(rm, token);
            if (ability == null) {
                return ParseResult.INTERNAL_ERROR;
            }
            ability.setRequiresTarget(true);
            boolean loadList = true;
            List<String> choices = null;
            if (token.indexOf('(') != -1) {
                choices = new ArrayList<>();
                AbilityUtilities.getUndecoratedName(token, choices);
                if (choices.size() == 1) {
                    if (Constants.LST_PERCENT_LIST.equals(choices.get(0)) && (ability instanceof CDOMSingleRef)) {
                        CDOMSingleRef<Ability> ref = (CDOMSingleRef<Ability>) ability;
                        AbilityTargetSelector ats = new AbilityTargetSelector(getTokenName(), acRef, ref, nature);
                        context.getObjectContext().addToList(obj, ListKey.GA_CAKEYS, lk);
                        context.getObjectContext().addToList(obj, lk, ats);
                        edgeList.add(ats);
                        loadList = false;
                    }
                }
            }
            if (loadList) {
                AssociatedPrereqObject assoc = context.getListContext().addToList(getTokenName(), obj, abilList, ability);
                assoc.setAssociation(AssociationKey.NATURE, nature);
                assoc.setAssociation(AssociationKey.CATEGORY, acRef);
                if (choices != null) {
                    assoc.setAssociation(AssociationKey.ASSOC_CHOICES, choices);
                }
                edgeList.add(assoc);
            }
        }
        if (!tok.hasMoreTokens()) {
            // No prereqs, so we're done
            return ParseResult.SUCCESS;
        }
        first = false;
        token = tok.nextToken();
        if (looksLikeAPrerequisite(token)) {
            break;
        }
    }
    if (removed || !prereqsAllowed) {
        return new ParseResult.Fail("Cannot use PREREQs when using .CLEAR, .CLEAR., or %LIST in " + getTokenName(), context);
    }
    while (true) {
        Prerequisite prereq = getPrerequisite(token);
        if (prereq == null) {
            return new ParseResult.Fail("   (Did you put feats after the " + "PRExxx tags in " + getTokenName() + ":?)", context);
        }
        for (PrereqObject edge : edgeList) {
            edge.addPrerequisite(prereq);
        }
        if (!tok.hasMoreTokens()) {
            break;
        }
        token = tok.nextToken();
    }
    return ParseResult.SUCCESS;
}
Also used : ChooseSelectionActor(pcgen.cdom.base.ChooseSelectionActor) ArrayList(java.util.ArrayList) Ungranted(pcgen.cdom.base.Ungranted) AbilitySelector(pcgen.cdom.helper.AbilitySelector) ListKey(pcgen.cdom.enumeration.ListKey) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject) Nature(pcgen.cdom.enumeration.Nature) Ability(pcgen.core.Ability) AbilityTargetSelector(pcgen.cdom.helper.AbilityTargetSelector) ParseResult(pcgen.rules.persistence.token.ParseResult) AbilityList(pcgen.cdom.list.AbilityList) CDOMSingleRef(pcgen.cdom.reference.CDOMSingleRef) PrereqObject(pcgen.cdom.base.PrereqObject) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject) StringTokenizer(java.util.StringTokenizer) AbilityCategory(pcgen.core.AbilityCategory) CDOMReference(pcgen.cdom.base.CDOMReference) Prerequisite(pcgen.core.prereq.Prerequisite)

Aggregations

ArrayList (java.util.ArrayList)3 StringTokenizer (java.util.StringTokenizer)3 PrereqObject (pcgen.cdom.base.PrereqObject)3 CDOMSingleRef (pcgen.cdom.reference.CDOMSingleRef)3 Prerequisite (pcgen.core.prereq.Prerequisite)3 Domain (pcgen.core.Domain)2 QualifiedObject (pcgen.core.QualifiedObject)2 AssociatedPrereqObject (pcgen.cdom.base.AssociatedPrereqObject)1 CDOMReference (pcgen.cdom.base.CDOMReference)1 ChooseSelectionActor (pcgen.cdom.base.ChooseSelectionActor)1 Ungranted (pcgen.cdom.base.Ungranted)1 ListKey (pcgen.cdom.enumeration.ListKey)1 Nature (pcgen.cdom.enumeration.Nature)1 AbilitySelector (pcgen.cdom.helper.AbilitySelector)1 AbilityTargetSelector (pcgen.cdom.helper.AbilityTargetSelector)1 AbilityList (pcgen.cdom.list.AbilityList)1 Ability (pcgen.core.Ability)1 AbilityCategory (pcgen.core.AbilityCategory)1 ParseResult (pcgen.rules.persistence.token.ParseResult)1