Search in sources :

Example 41 with AssociatedPrereqObject

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

the class PlayerCharacterSpellTest method additionalSetUp.

@Override
protected void additionalSetUp() throws Exception {
    LoadContext context = Globals.getContext();
    CampaignSourceEntry source = TestHelper.createSource(getClass());
    // Spells
    classSpell = TestHelper.makeSpell("classSpell");
    domainSpell = TestHelper.makeSpell("domainSpell");
    final String classLine = "CLASS:MyClass	TYPE:Base.PC	SPELLSTAT:CHA	MEMORIZE:YES	SPELLBOOK:NO";
    PCClassLoader classLoader = new PCClassLoader();
    divineClass = classLoader.parseLine(context, null, classLine, source);
    BuildUtilities.setFact(divineClass, "SpellType", "Divine");
    classLoader.parseLine(context, divineClass, "CLASS:MyClass	KNOWNSPELLS:LEVEL=0|LEVEL=1|LEVEL=2|LEVEL=3|LEVEL=4|LEVEL=5|LEVEL=6|LEVEL=7|LEVEL=8|LEVEL=9	BONUS:CASTERLEVEL|Cleric|CL", source);
    classLoader.parseClassLevelLine(context, divineClass, 1, source, "CAST:5,4	BONUS:DOMAIN|NUMBER|2	BONUS:VAR|DomainLVL|CL");
    context.getReferenceContext().importObject(divineClass);
    final String domainLine = "Sun	SPELLLEVEL:DOMAIN|Sun=1|KEY_domainSpell";
    GenericLoader<Domain> domainLoader = new GenericLoader<>(Domain.class);
    domainLoader.parseLine(context, null, domainLine, source);
    sunDomain = context.getReferenceContext().silentlyGetConstructedCDOMObject(Domain.class, "Sun");
    CDOMReference<ClassSpellList> ref = TokenUtilities.getTypeOrPrimitive(context, ClassSpellList.class, divineClass.getKeyName());
    AssociatedPrereqObject edge = context.getListContext().addToMasterList("CLASSES", classSpell, ref, classSpell);
    edge.setAssociation(AssociationKey.SPELL_LEVEL, 1);
    context.commit();
}
Also used : CampaignSourceEntry(pcgen.persistence.lst.CampaignSourceEntry) ClassSpellList(pcgen.cdom.list.ClassSpellList) LoadContext(pcgen.rules.context.LoadContext) GenericLoader(pcgen.persistence.lst.GenericLoader) PCClassLoader(pcgen.persistence.lst.PCClassLoader) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject)

Example 42 with AssociatedPrereqObject

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

the class ClassesToken method parseTokenWithSeparator.

@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, Spell spell, String value) {
    // Note: May contain PRExxx
    String classKey;
    Prerequisite prereq = null;
    int openBracketLoc = value.indexOf('[');
    if (openBracketLoc == -1) {
        classKey = value;
    } else {
        if (value.lastIndexOf(']') != value.length() - 1) {
            return new ParseResult.Fail("Invalid " + getTokenName() + " must end with ']' if it contains a PREREQ tag", context);
        }
        if (value.lastIndexOf('|') > openBracketLoc) {
            return new ParseResult.Fail("Invalid " + getTokenName() + ": PRExxx must be at the END of the Token. Token was " + value, context);
        }
        classKey = value.substring(0, openBracketLoc);
        String prereqString = value.substring(openBracketLoc + 1, value.length() - 1);
        if (prereqString.isEmpty()) {
            return new ParseResult.Fail(getTokenName() + " cannot have empty prerequisite : " + value, context);
        }
        prereq = getPrerequisite(prereqString);
        if (prereq == null) {
            return new ParseResult.Fail(getTokenName() + " had invalid prerequisite : " + prereqString, context);
        }
    }
    boolean foundAny = false;
    boolean foundOther = false;
    StringTokenizer pipeTok = new StringTokenizer(classKey, Constants.PIPE);
    while (pipeTok.hasMoreTokens()) {
        // could be name=x or name,name=x
        String tokString = pipeTok.nextToken();
        int startPos = tokString.startsWith("TYPE=") ? "TYPE=".length() : 0;
        int equalLoc = tokString.indexOf(Constants.EQUALS, startPos);
        if (equalLoc == -1) {
            return new ParseResult.Fail("Malformed " + getTokenName() + " Token (expecting an =): " + tokString, context);
        }
        if (equalLoc != tokString.lastIndexOf(Constants.EQUALS)) {
            return new ParseResult.Fail("Malformed " + getTokenName() + " Token (more than one =): " + tokString, context);
        }
        String nameList = tokString.substring(0, equalLoc);
        String levelString = tokString.substring(equalLoc + 1);
        Integer level;
        try {
            level = Integer.valueOf(levelString);
            if (level.intValue() < -1) {
                return new ParseResult.Fail(getTokenName() + " may not use a negative level: " + value, context);
            } else if (level.intValue() == -1) {
                if (prereq != null) {
                    return new ParseResult.Fail(getTokenName() + " may not use -1 with a PREREQ: " + value, context);
                }
            // Logging.deprecationPrint(getTokenName()
            // + " should not use a negative level: " + value);
            }
        } catch (NumberFormatException nfe) {
            return new ParseResult.Fail("Malformed Level in " + getTokenName() + " (expected an Integer): " + levelString, context);
        }
        ParseResult pr = checkForIllegalSeparator(',', nameList);
        if (!pr.passed()) {
            return pr;
        }
        StringTokenizer commaTok = new StringTokenizer(nameList, Constants.COMMA);
        while (commaTok.hasMoreTokens()) {
            CDOMReference<ClassSpellList> ref;
            String token = commaTok.nextToken();
            if (Constants.LST_ALL.equals(token)) {
                foundAny = true;
                ref = context.getReferenceContext().getCDOMAllReference(SPELLLIST_CLASS);
            } else {
                foundOther = true;
                ref = TokenUtilities.getTypeOrPrimitive(context, SPELLLIST_CLASS, token);
                if (ref == null) {
                    return new ParseResult.Fail("  error was in " + getTokenName(), context);
                }
            }
            if (level == -1) {
                //No need to check for prereq here - done above
                context.getListContext().removeFromMasterList(getTokenName(), spell, ref, spell);
            } else {
                AssociatedPrereqObject edge = context.getListContext().addToMasterList(getTokenName(), spell, ref, spell);
                edge.setAssociation(AssociationKey.SPELL_LEVEL, level);
                if (prereq != null) {
                    edge.addPrerequisite(prereq);
                }
                context.getObjectContext().addToList(spell, ListKey.SPELL_CLASSLEVEL, token + ' ' + level);
            }
        }
    }
    if (foundAny && foundOther) {
        return new ParseResult.Fail("Non-sensical " + getTokenName() + ": Contains ANY and a specific reference: " + value, context);
    }
    return ParseResult.SUCCESS;
}
Also used : ParseResult(pcgen.rules.persistence.token.ParseResult) ClassSpellList(pcgen.cdom.list.ClassSpellList) StringTokenizer(java.util.StringTokenizer) Prerequisite(pcgen.core.prereq.Prerequisite) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject)

Example 43 with AssociatedPrereqObject

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

the class DomainsToken method parseTokenWithSeparator.

@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, Spell spell, String value) {
    // Note: May contain PRExxx
    String domainKey;
    Prerequisite prereq = null;
    int openBracketLoc = value.indexOf('[');
    if (openBracketLoc == -1) {
        domainKey = value;
    } else {
        if (value.lastIndexOf(']') != value.length() - 1) {
            return new ParseResult.Fail("Invalid " + getTokenName() + " must end with ']' if it contains a PREREQ tag", context);
        }
        domainKey = value.substring(0, openBracketLoc);
        String prereqString = value.substring(openBracketLoc + 1, value.length() - 1);
        if (prereqString.isEmpty()) {
            return new ParseResult.Fail(getTokenName() + " cannot have empty prerequisite : " + value, context);
        }
        prereq = getPrerequisite(prereqString);
        if (prereq == null) {
            return new ParseResult.Fail(getTokenName() + " had invalid prerequisite : " + prereqString, context);
        }
    }
    boolean foundAny = false;
    boolean foundOther = false;
    StringTokenizer pipeTok = new StringTokenizer(domainKey, Constants.PIPE);
    while (pipeTok.hasMoreTokens()) {
        // could be name=x or name,name=x
        String tokString = pipeTok.nextToken();
        int equalLoc = tokString.indexOf(Constants.EQUALS);
        if (equalLoc == -1) {
            return new ParseResult.Fail("Malformed " + getTokenName() + " Token (expecting an =): " + tokString, context);
        }
        if (equalLoc != tokString.lastIndexOf(Constants.EQUALS)) {
            return new ParseResult.Fail("Malformed " + getTokenName() + " Token (more than one =): " + tokString, context);
        }
        String nameList = tokString.substring(0, equalLoc);
        String levelString = tokString.substring(equalLoc + 1);
        Integer level;
        try {
            level = Integer.valueOf(levelString);
            if (level.intValue() < -1) {
                return new ParseResult.Fail(getTokenName() + " may not use a negative level: " + value, context);
            } else if (level.intValue() == -1) {
                if (prereq != null) {
                    return new ParseResult.Fail(getTokenName() + " may not use -1 with a PREREQ: " + value, context);
                }
            // Logging.deprecationPrint(getTokenName()
            // + " should not use a negative level: " + value);
            }
        } catch (NumberFormatException nfe) {
            return new ParseResult.Fail("Malformed Level in " + getTokenName() + " (expected an Integer): " + levelString, context);
        }
        ParseResult pr = checkForIllegalSeparator(',', nameList);
        if (!pr.passed()) {
            return pr;
        }
        StringTokenizer commaTok = new StringTokenizer(nameList, Constants.COMMA);
        while (commaTok.hasMoreTokens()) {
            CDOMReference<DomainSpellList> ref;
            String token = commaTok.nextToken();
            if (Constants.LST_ALL.equals(token)) {
                foundAny = true;
                ref = context.getReferenceContext().getCDOMAllReference(SPELLLIST_CLASS);
            } else {
                foundOther = true;
                ref = TokenUtilities.getTypeOrPrimitive(context, SPELLLIST_CLASS, token);
            }
            if (ref == null) {
                return new ParseResult.Fail("  Error was in " + getTokenName(), context);
            }
            if (level == -1) {
                //No need to check for prereq here - done above
                context.getListContext().removeFromMasterList(getTokenName(), spell, ref, spell);
            } else {
                AssociatedPrereqObject edge = context.getListContext().addToMasterList(getTokenName(), spell, ref, spell);
                edge.setAssociation(AssociationKey.SPELL_LEVEL, level);
                if (prereq != null) {
                    edge.addPrerequisite(prereq);
                }
                context.getObjectContext().addToList(spell, ListKey.SPELL_DOMAINLEVEL, token + ' ' + level);
            }
        }
    }
    if (foundAny && foundOther) {
        return new ParseResult.Fail("Non-sensical " + getTokenName() + ": Contains ANY and a specific reference: " + value, context);
    }
    return ParseResult.SUCCESS;
}
Also used : ParseResult(pcgen.rules.persistence.token.ParseResult) DomainSpellList(pcgen.cdom.list.DomainSpellList) StringTokenizer(java.util.StringTokenizer) Prerequisite(pcgen.core.prereq.Prerequisite) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject)

Example 44 with AssociatedPrereqObject

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

the class AvailableSpellInputFacet method processList.

private void processList(CharID id, CDOMList<Spell> spelllist, CDOMReference<? extends CDOMList<?>> listref, CDOMObject cdo) {
    for (CDOMReference<Spell> objref : cdo.getListMods((CDOMReference<? extends CDOMList<Spell>>) listref)) {
        for (AssociatedPrereqObject apo : cdo.getListAssociations(listref, objref)) {
            Collection<Spell> spells = objref.getContainedObjects();
            Integer lvl = apo.getAssociation(AssociationKey.SPELL_LEVEL);
            if (apo.hasPrerequisites()) {
                List<Prerequisite> prereqs = apo.getPrerequisiteList();
                for (Spell spell : spells) {
                    AvailableSpell as = new AvailableSpell(spelllist, spell, lvl);
                    as.addAllPrerequisites(prereqs);
                    conditionallyAvailableSpellFacet.add(id, as, cdo);
                }
            } else {
                for (Spell spell : spells) {
                    availableSpellFacet.add(id, spelllist, lvl, spell, cdo);
                }
            }
        }
    }
}
Also used : AvailableSpell(pcgen.cdom.helper.AvailableSpell) AvailableSpell(pcgen.cdom.helper.AvailableSpell) Spell(pcgen.core.spell.Spell) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 45 with AssociatedPrereqObject

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

the class AbstractSpellListToken method getMap.

/**
	 * Gets the map.
	 *
	 * @param context the context
	 * @param obj the obj
	 * @param changedLists the changed lists
	 * @param knownSpells Should this scan be for known spells
	 * @return the map
	 */
protected TripleKeyMapToList<String, Integer, CDOMReference<? extends CDOMList<?>>, CDOMReference<Spell>> getMap(LoadContext context, CDOMObject obj, Collection<CDOMReference<? extends CDOMList<?>>> changedLists, boolean knownSpells) {
    TripleKeyMapToList<String, Integer, CDOMReference<? extends CDOMList<?>>, CDOMReference<Spell>> map = new TripleKeyMapToList<>();
    for (CDOMReference listRef : changedLists) {
        AssociatedChanges<CDOMReference<Spell>> changes = context.getListContext().getChangesInList(getTokenName(), obj, listRef);
        Collection<?> removedItems = changes.getRemoved();
        if (removedItems != null && !removedItems.isEmpty() || changes.includesGlobalClear()) {
            context.addWriteMessage(getTokenName() + " does not support .CLEAR");
            return null;
        }
        MapToList<CDOMReference<Spell>, AssociatedPrereqObject> mtl = changes.getAddedAssociations();
        if (mtl == null || mtl.isEmpty()) {
            // TODO Error message - unexpected?
            continue;
        }
        for (CDOMReference<Spell> added : mtl.getKeySet()) {
            for (AssociatedPrereqObject assoc : mtl.getListFor(added)) {
                Integer lvl = assoc.getAssociation(AssociationKey.SPELL_LEVEL);
                String prereqString = getPrerequisiteString(context, assoc.getPrerequisiteList());
                Boolean known = assoc.getAssociation(AssociationKey.KNOWN);
                boolean isKnown = known != null && known.booleanValue();
                if (knownSpells == isKnown) {
                    map.addToListFor(prereqString, lvl, listRef, added);
                }
            }
        }
    }
    return map;
}
Also used : Spell(pcgen.core.spell.Spell) TripleKeyMapToList(pcgen.base.util.TripleKeyMapToList) CDOMList(pcgen.cdom.base.CDOMList) CDOMReference(pcgen.cdom.base.CDOMReference) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject)

Aggregations

AssociatedPrereqObject (pcgen.cdom.base.AssociatedPrereqObject)50 CDOMReference (pcgen.cdom.base.CDOMReference)34 ArrayList (java.util.ArrayList)18 Prerequisite (pcgen.core.prereq.Prerequisite)18 Spell (pcgen.core.spell.Spell)14 StringTokenizer (java.util.StringTokenizer)11 TreeSet (java.util.TreeSet)10 Domain (pcgen.core.Domain)8 CDOMObject (pcgen.cdom.base.CDOMObject)7 ClassSpellList (pcgen.cdom.list.ClassSpellList)7 CDOMList (pcgen.cdom.base.CDOMList)6 PersistenceLayerException (pcgen.persistence.PersistenceLayerException)6 ParseResult (pcgen.rules.persistence.token.ParseResult)6 HashSet (java.util.HashSet)5 MasterListInterface (pcgen.cdom.base.MasterListInterface)5 AssociationKey (pcgen.cdom.enumeration.AssociationKey)5 ClassSkillList (pcgen.cdom.list.ClassSkillList)5 DomainSpellList (pcgen.cdom.list.DomainSpellList)5 StringWriter (java.io.StringWriter)4 PCClass (pcgen.core.PCClass)4