Search in sources :

Example 26 with AssociatedPrereqObject

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

the class MonccskillToken method unparse.

@Override
public String[] unparse(LoadContext context, Race race) {
    CDOMGroupRef<ClassSkillList> monsterList = context.getReferenceContext().getCDOMTypeReference(ClassSkillList.class, "Monster");
    AssociatedChanges<CDOMReference<Skill>> changes = context.getListContext().getChangesInList(getTokenName(), race, monsterList);
    List<String> list = new ArrayList<>();
    Collection<CDOMReference<Skill>> removedItems = changes.getRemoved();
    if (removedItems != null && !removedItems.isEmpty()) {
        if (changes.includesGlobalClear()) {
            context.addWriteMessage("Non-sensical relationship in " + getTokenName() + ": global .CLEAR and local .CLEAR. performed");
            return null;
        }
        list.add(Constants.LST_DOT_CLEAR_DOT + ReferenceUtilities.joinLstFormat(removedItems, "|.CLEAR."));
    }
    if (changes.includesGlobalClear()) {
        list.add(Constants.LST_DOT_CLEAR);
    }
    MapToList<CDOMReference<Skill>, AssociatedPrereqObject> map = changes.getAddedAssociations();
    if (map != null && !map.isEmpty()) {
        Set<CDOMReference<Skill>> added = map.getKeySet();
        for (CDOMReference<Skill> ab : added) {
            for (AssociatedPrereqObject assoc : map.getListFor(ab)) {
                if (!SkillCost.CROSS_CLASS.equals(assoc.getAssociation(AssociationKey.SKILL_COST))) {
                    context.addWriteMessage("Skill Cost must be " + "CROSS_CLASS for Token " + getTokenName());
                    return null;
                }
            }
        }
        list.add(ReferenceUtilities.joinLstFormat(added, Constants.PIPE));
    }
    if (list.isEmpty()) {
        // Zero indicates no add or clear
        return null;
    }
    return list.toArray(new String[list.size()]);
}
Also used : ArrayList(java.util.ArrayList) ClassSkillList(pcgen.cdom.list.ClassSkillList) Skill(pcgen.core.Skill) CDOMReference(pcgen.cdom.base.CDOMReference) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject)

Example 27 with AssociatedPrereqObject

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

the class MoncskillToken method parseTokenWithSeparator.

@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, Race race, String value) {
    boolean firstToken = true;
    boolean foundAny = false;
    boolean foundOther = false;
    StringTokenizer tok = new StringTokenizer(value, Constants.PIPE);
    CDOMGroupRef<ClassSkillList> monsterList = context.getReferenceContext().getCDOMTypeReference(ClassSkillList.class, "Monster");
    while (tok.hasMoreTokens()) {
        String tokText = tok.nextToken();
        if (Constants.LST_DOT_CLEAR.equals(tokText)) {
            if (!firstToken) {
                return new ParseResult.Fail("Non-sensical situation was " + "encountered while parsing " + getTokenName() + ": When used, .CLEAR must be the first argument", context);
            }
            context.getListContext().removeAllFromList(getTokenName(), race, monsterList);
        } else if (tokText.startsWith(Constants.LST_DOT_CLEAR_DOT)) {
            CDOMReference<Skill> skill = null;
            String clearText = tokText.substring(7);
            if (Constants.LST_ALL.equals(clearText)) {
                skill = context.getReferenceContext().getCDOMAllReference(SKILL_CLASS);
            } else {
                if (Constants.LST_LIST.equals(clearText)) {
                    context.getObjectContext().removeFromList(race, ListKey.NEW_CHOOSE_ACTOR, this);
                } else {
                    skill = getSkillReference(context, clearText);
                    if (skill == null) {
                        return new ParseResult.Fail("  Error was encountered while parsing " + getTokenName(), context);
                    }
                }
            }
            if (skill != null) {
                context.getListContext().removeFromList(getTokenName(), race, monsterList, skill);
            }
        } else {
            /*
				 * Note this is done one-by-one, because .CLEAR. token type
				 * needs to be able to perform the unlink. That could be
				 * changed, but the increase in complexity isn't worth it.
				 * (Changing it to a grouping object that didn't place links in
				 * the graph would also make it harder to trace the source of
				 * class skills, etc.)
				 */
            CDOMReference<Skill> skill = null;
            if (Constants.LST_ALL.equals(tokText)) {
                foundAny = true;
                skill = context.getReferenceContext().getCDOMAllReference(SKILL_CLASS);
            } else {
                foundOther = true;
                if (Constants.LST_LIST.equals(tokText)) {
                    context.getObjectContext().addToList(race, ListKey.NEW_CHOOSE_ACTOR, this);
                } else {
                    skill = getSkillReference(context, tokText);
                    if (skill == null) {
                        return new ParseResult.Fail("  Error was encountered while parsing " + getTokenName(), context);
                    }
                }
            }
            if (skill != null) {
                AssociatedPrereqObject apo = context.getListContext().addToList(getTokenName(), race, monsterList, skill);
                apo.setAssociation(AssociationKey.SKILL_COST, SkillCost.CLASS);
            }
        }
        firstToken = false;
    }
    if (foundAny && foundOther) {
        return new ParseResult.Fail("Non-sensical " + getTokenName() + ": Contains ANY and a specific reference: " + value, context);
    }
    return ParseResult.SUCCESS;
}
Also used : StringTokenizer(java.util.StringTokenizer) ParseResult(pcgen.rules.persistence.token.ParseResult) ClassSkillList(pcgen.cdom.list.ClassSkillList) CDOMReference(pcgen.cdom.base.CDOMReference) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject)

Example 28 with AssociatedPrereqObject

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

the class AdddomainsToken method parseTokenWithSeparator.

@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, PCClassLevel level, String value) {
    StringTokenizer tok = new StringTokenizer(value, Constants.DOT);
    while (tok.hasMoreTokens()) {
        String tokString = tok.nextToken();
        // Do not initialize, null is
        Prerequisite prereq = null;
        // significant!
        String domainKey;
        // Note: May contain PRExxx
        int openBracketLoc = tokString.indexOf('[');
        if (openBracketLoc == -1) {
            if (tokString.indexOf(']') != -1) {
                return new ParseResult.Fail("Invalid " + getTokenName() + " must have '[' if it contains a PREREQ tag", context);
            }
            domainKey = tokString;
        } else {
            if (tokString.indexOf(']') != tokString.length() - 1) {
                return new ParseResult.Fail("Invalid " + getTokenName() + " must end with ']' if it contains a PREREQ tag", context);
            }
            domainKey = tokString.substring(0, openBracketLoc);
            String prereqString = tokString.substring(openBracketLoc + 1, tokString.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);
            }
        }
        AssociatedPrereqObject apo = context.getListContext().addToList(getTokenName(), level, PCClass.ALLOWED_DOMAINS, context.getReferenceContext().getCDOMReference(DOMAIN_CLASS, domainKey));
        if (prereq != null) {
            apo.addPrerequisite(prereq);
        }
    }
    return ParseResult.SUCCESS;
}
Also used : StringTokenizer(java.util.StringTokenizer) Prerequisite(pcgen.core.prereq.Prerequisite) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject)

Example 29 with AssociatedPrereqObject

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

the class AdddomainsToken method unparse.

@Override
public String[] unparse(LoadContext context, PCClassLevel level) {
    AssociatedChanges<CDOMReference<Domain>> changes = context.getListContext().getChangesInList(getTokenName(), level, PCClass.ALLOWED_DOMAINS);
    Collection<CDOMReference<Domain>> removedItems = changes.getRemoved();
    if (removedItems != null && !removedItems.isEmpty() || changes.includesGlobalClear()) {
        context.addWriteMessage(getTokenName() + " does not support .CLEAR");
        return null;
    }
    MapToList<CDOMReference<Domain>, AssociatedPrereqObject> mtl = changes.getAddedAssociations();
    if (mtl == null || mtl.isEmpty()) {
        return null;
    }
    PrerequisiteWriter prereqWriter = new PrerequisiteWriter();
    Set<String> set = new TreeSet<>();
    for (CDOMReference<Domain> domain : mtl.getKeySet()) {
        for (AssociatedPrereqObject assoc : mtl.getListFor(domain)) {
            StringBuilder sb = new StringBuilder(domain.getLSTformat(false));
            List<Prerequisite> prereqs = assoc.getPrerequisiteList();
            Prerequisite prereq;
            if (prereqs == null || prereqs.isEmpty()) {
                prereq = null;
            } else if (prereqs.size() == 1) {
                prereq = prereqs.get(0);
            } else {
                context.addWriteMessage("Added Domain from " + getTokenName() + " had more than one " + "Prerequisite: " + prereqs.size());
                return null;
            }
            if (prereq != null) {
                sb.append('[');
                StringWriter swriter = new StringWriter();
                try {
                    prereqWriter.write(swriter, prereq);
                } catch (PersistenceLayerException e) {
                    context.addWriteMessage("Error writing Prerequisite: " + e);
                    return null;
                }
                sb.append(swriter.toString());
                sb.append(']');
            }
            set.add(sb.toString());
        }
    }
    return new String[] { StringUtil.join(set, Constants.DOT) };
}
Also used : PrerequisiteWriter(pcgen.persistence.lst.output.prereq.PrerequisiteWriter) PersistenceLayerException(pcgen.persistence.PersistenceLayerException) StringWriter(java.io.StringWriter) TreeSet(java.util.TreeSet) Domain(pcgen.core.Domain) CDOMReference(pcgen.cdom.base.CDOMReference) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 30 with AssociatedPrereqObject

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

the class PCClassTest method testAddAbility.

/**
	 * Test the definition and application of abilities. 
	 * @throws PersistenceLayerException 
	 */
public void testAddAbility() throws PersistenceLayerException {
    LoadContext context = Globals.getContext();
    // Create some abilities to be added
    AbilityCategory cat = context.getReferenceContext().constructCDOMObject(AbilityCategory.class, "TestCat");
    Ability ab1 = new Ability();
    ab1.setName("Ability1");
    ab1.setCDOMCategory(cat);
    context.getReferenceContext().importObject(ab1);
    Ability ab2 = new Ability();
    ab2.setName("Ability2");
    ab2.setCDOMCategory(cat);
    context.getReferenceContext().importObject(ab2);
    // Link them to a template
    CampaignSourceEntry source;
    try {
        source = new CampaignSourceEntry(new Campaign(), new URI("file:/" + getClass().getName() + ".java"));
    } catch (URISyntaxException e) {
        throw new UnreachableError(e);
    }
    String classPCCText = "CLASS:Cleric	HD:8		CLASSTYPE:PC	TYPE:Base.PC	ABB:Clr	ABILITY:TestCat|AUTOMATIC|Ability1\n" + "CLASS:Cleric	STARTSKILLPTS:2\n" + "2	ABILITY:TestCat|AUTOMATIC|Ability2";
    PCClass pcclass = parsePCClassText(classPCCText, source);
    ab1.setCDOMCategory(cat);
    ab2.setCDOMCategory(cat);
    context.getReferenceContext().importObject(ab1);
    context.getReferenceContext().importObject(ab2);
    CDOMSingleRef<AbilityCategory> acRef = context.getReferenceContext().getCDOMReference(AbilityCategory.class, "TestCat");
    assertTrue(context.getReferenceContext().resolveReferences(null));
    CDOMReference<AbilityList> autoList = AbilityList.getAbilityListReference(acRef, Nature.AUTOMATIC);
    Collection<CDOMReference<Ability>> mods = pcclass.getListMods(autoList);
    assertEquals(1, mods.size());
    CDOMReference<Ability> ref = mods.iterator().next();
    Collection<Ability> abilities = ref.getContainedObjects();
    assertEquals(1, abilities.size());
    assertEquals(ab1, abilities.iterator().next());
    Collection<AssociatedPrereqObject> assocs = pcclass.getListAssociations(autoList, ref);
    assertEquals(1, assocs.size());
    PCClassLevel level = pcclass.getOriginalClassLevel(2);
    mods = level.getListMods(autoList);
    assertEquals(1, mods.size());
    ref = mods.iterator().next();
    abilities = ref.getContainedObjects();
    assertEquals(1, abilities.size());
    assertEquals(ab2, abilities.iterator().next());
    assocs = level.getListAssociations(autoList, ref);
    assertEquals(1, assocs.size());
    // Add the class to the character
    PlayerCharacter pc = getCharacter();
    pc.incrementClassLevel(1, pcclass, true);
    assertTrue("Character should have ability1.", hasAbility(pc, cat, Nature.AUTOMATIC, ab1));
    assertFalse("Character should not have ability2.", hasAbility(pc, cat, Nature.AUTOMATIC, ab2));
    pc.incrementClassLevel(1, pcclass, true);
    assertTrue("Character should have ability1.", hasAbility(pc, cat, Nature.AUTOMATIC, ab1));
    assertTrue("Character should have ability2.", hasAbility(pc, cat, Nature.AUTOMATIC, ab2));
}
Also used : AbilityList(pcgen.cdom.list.AbilityList) URISyntaxException(java.net.URISyntaxException) UnreachableError(pcgen.base.lang.UnreachableError) URI(java.net.URI) PCClassLevel(pcgen.cdom.inst.PCClassLevel) CampaignSourceEntry(pcgen.persistence.lst.CampaignSourceEntry) LoadContext(pcgen.rules.context.LoadContext) 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