Search in sources :

Example 61 with CNAbilitySelection

use of pcgen.cdom.helper.CNAbilitySelection in project pcgen by PCGen.

the class AbstractCNASEnforcingFacetTest method testTypeAddSingleGet.

@Test
public void testTypeAddSingleGet() {
    Object source1 = new Object();
    CNAbilitySelection t1 = getObject();
    getFacet().add(id, t1, source1);
    assertEquals(1, getFacet().getCount(id));
    assertFalse(getFacet().isEmpty(id));
    assertNotNull(getFacet().getSet(id));
    assertEquals(1, getFacet().getSet(id).size());
    assertEquals(t1, getFacet().getSet(id).iterator().next());
    assertEventCount(1, 0);
    // No cross-pollution
    assertEquals(0, getFacet().getCount(altid));
    assertTrue(getFacet().isEmpty(altid));
    assertNotNull(getFacet().getSet(altid));
    assertTrue(getFacet().getSet(altid).isEmpty());
}
Also used : CNAbilitySelection(pcgen.cdom.helper.CNAbilitySelection) Test(org.junit.Test)

Example 62 with CNAbilitySelection

use of pcgen.cdom.helper.CNAbilitySelection 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 63 with CNAbilitySelection

use of pcgen.cdom.helper.CNAbilitySelection in project pcgen by PCGen.

the class VAbilityTokenTest method setUp.

/*
	 * @see TestCase#setUp()
	 */
@Override
protected void setUp() throws Exception {
    super.setUp();
    PlayerCharacter character = getCharacter();
    // Make some ability categories and add them to the game mode
    Ability ab1 = TestHelper.makeAbility("Perform (Dance)", AbilityCategory.FEAT, "General.Fighter");
    ab1.put(ObjectKey.MULTIPLE_ALLOWED, Boolean.FALSE);
    ab1.put(ObjectKey.VISIBILITY, Visibility.DEFAULT);
    List<Aspect> colourList = new ArrayList<>();
    colourList.add(new Aspect("Colour", "Green"));
    ab1.addToMapFor(MapKey.ASPECT, AspectName.getConstant("Colour"), colourList);
    List<Aspect> sizeList = new ArrayList<>();
    sizeList.add(new Aspect("Size", "L"));
    ab1.addToMapFor(MapKey.ASPECT, AspectName.getConstant("Size"), sizeList);
    List<Aspect> shapeList = new ArrayList<>();
    Aspect cube = new Aspect("Shape", "Cube");
    Prerequisite prereq = new Prerequisite();
    prereq.setKind("ALIGN");
    prereq.setKey("LG");
    prereq.setOperator(PrerequisiteOperator.EQ);
    cube.addPrerequisite(prereq);
    shapeList.add(cube);
    shapeList.add(new Aspect("Shape", "Icosahedron"));
    ab1.addToMapFor(MapKey.ASPECT, AspectName.getConstant("Shape"), shapeList);
    List<Aspect> sidesList = new ArrayList<>();
    sidesList.add(new Aspect("Sides", "20"));
    ab1.addToMapFor(MapKey.ASPECT, AspectName.getConstant("Sides"), sidesList);
    List<Aspect> ageList = new ArrayList<>();
    ageList.add(new Aspect("Age In Years", "2000"));
    ab1.addToMapFor(MapKey.ASPECT, AspectName.getConstant("Age In Years"), ageList);
    CNAbility cna = CNAbilityFactory.getCNAbility(AbilityCategory.FEAT, Nature.VIRTUAL, ab1);
    character.addAbility(new CNAbilitySelection(cna), UserSelection.getInstance(), UserSelection.getInstance());
    TestHelper.makeSkill("Bluff", "Charisma", cha, true, SkillArmorCheck.NONE);
    TestHelper.makeSkill("Listen", "Wisdom", wis, true, SkillArmorCheck.NONE);
    skillFocus = TestHelper.makeAbility("Skill Focus", AbilityCategory.FEAT, "General");
    BonusObj aBonus = Bonus.newBonus(Globals.getContext(), "SKILL|LIST|3");
    if (aBonus != null) {
        skillFocus.addToListFor(ListKey.BONUS, aBonus);
    }
    skillFocus.put(ObjectKey.MULTIPLE_ALLOWED, true);
    Globals.getContext().unconditionallyProcess(skillFocus, "CHOOSE", "SKILL|ALL");
    cna = CNAbilityFactory.getCNAbility(AbilityCategory.FEAT, Nature.VIRTUAL, skillFocus);
    character.addAbility(new CNAbilitySelection(cna, "KEY_Bluff"), UserSelection.getInstance(), UserSelection.getInstance());
    character.addAbility(new CNAbilitySelection(cna, "KEY_Listen"), UserSelection.getInstance(), UserSelection.getInstance());
    character.calcActiveBonuses();
}
Also used : Ability(pcgen.core.Ability) CNAbility(pcgen.cdom.content.CNAbility) CNAbility(pcgen.cdom.content.CNAbility) PlayerCharacter(pcgen.core.PlayerCharacter) BonusObj(pcgen.core.bonus.BonusObj) ArrayList(java.util.ArrayList) CNAbilitySelection(pcgen.cdom.helper.CNAbilitySelection) Aspect(pcgen.cdom.helper.Aspect) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 64 with CNAbilitySelection

use of pcgen.cdom.helper.CNAbilitySelection in project pcgen by PCGen.

the class PCGVer2Parser method parseFeatsHandleAppliedToAndSaveTags.

private void parseFeatsHandleAppliedToAndSaveTags(final Iterator<PCGElement> it, final CNAbility cna) {
    Ability aFeat = cna.getAbility();
    while (it.hasNext()) {
        final PCGElement element = it.next();
        final String tag = element.getName();
        if (IOConstants.TAG_APPLIEDTO.equals(tag)) {
            final String appliedToKey = EntityEncoder.decode(element.getText());
            if (aFeat.getSafe(ObjectKey.MULTIPLE_ALLOWED)) {
                String[] assoc = appliedToKey.split(Constants.COMMA, -1);
                for (String string : assoc) {
                    CNAbilitySelection cnas = new CNAbilitySelection(cna, string);
                    if (cna.getNature() == Nature.VIRTUAL) {
                        thePC.addSavedAbility(cnas, UserSelection.getInstance(), UserSelection.getInstance());
                    } else {
                        thePC.addAbility(cnas, UserSelection.getInstance(), UserSelection.getInstance());
                    }
                }
            }
        } else if (IOConstants.TAG_SAVE.equals(tag)) {
            final String saveKey = EntityEncoder.decode(element.getText());
            if (saveKey.startsWith(IOConstants.TAG_BONUS) && (saveKey.length() > 6)) {
                final BonusObj aBonus = Bonus.newBonus(Globals.getContext(), saveKey.substring(6));
                if (aBonus != null) {
                    thePC.addSaveableBonus(aBonus, aFeat);
                }
            } else {
                if (Logging.isDebugMode()) {
                    Logging.debugPrint("Ignoring SAVE:" + saveKey);
                }
            }
        } else if (tag.equals(IOConstants.TAG_LEVELABILITY)) {
            parseLevelAbilityInfo(element, aFeat);
        } else if (tag.equals(IOConstants.TAG_ADDTOKEN)) {
            parseAddTokenInfo(element, aFeat);
        }
    }
}
Also used : CNAbility(pcgen.cdom.content.CNAbility) Ability(pcgen.core.Ability) SpecialAbility(pcgen.core.SpecialAbility) BonusObj(pcgen.core.bonus.BonusObj) CNAbilitySelection(pcgen.cdom.helper.CNAbilitySelection)

Example 65 with CNAbilitySelection

use of pcgen.cdom.helper.CNAbilitySelection in project pcgen by PCGen.

the class PCGVer2Parser method parseAbilityLine.

/*
	 * ###############################################################
	 * Character Ability methods
	 * ###############################################################
	 */
private void parseAbilityLine(final String line) {
    final PCGTokenizer tokens;
    try {
        tokens = new PCGTokenizer(line);
    } catch (PCGParseException pcgpex) {
        final String msg = LanguageBundle.getFormattedString(//$NON-NLS-1$
        "Warnings.PCGenParser.IllegalAbility", line, pcgpex.getMessage());
        warnings.add(msg);
        return;
    }
    AbilityCategory category = null;
    Nature nature = Nature.NORMAL;
    String abilityCat = null;
    Ability ability = null;
    String missingCat = null;
    final Iterator<PCGElement> it = tokens.getElements().iterator();
    // the first element defines the AbilityCategory key name
    if (it.hasNext()) {
        final PCGElement element = it.next();
        final String categoryKey = EntityEncoder.decode(element.getText());
        category = SettingsHandler.getGame().getAbilityCategory(categoryKey);
        if (category == null) {
            missingCat = categoryKey;
        }
    }
    // The next element will be the nature
    if (it.hasNext()) {
        final PCGElement element = it.next();
        final String natureKey = EntityEncoder.decode(element.getText());
        nature = Nature.valueOf(natureKey);
    }
    // The next element will be the ability's innate category
    if (it.hasNext()) {
        final PCGElement element = it.next();
        abilityCat = EntityEncoder.decode(element.getText());
    }
    // The next element will be the ability key
    if (it.hasNext()) {
        final PCGElement element = it.next();
        String abilityKey = EntityEncoder.decode(element.getText());
        // Check for an ability that has been updated.
        CategorisedKey categorisedKey = AbilityMigration.getNewAbilityKey(abilityCat, abilityKey, pcgenVersion, SettingsHandler.getGame().getName());
        abilityCat = categorisedKey.getCategory();
        abilityKey = categorisedKey.getKey();
        AbilityCategory innateCategory = SettingsHandler.getGame().getAbilityCategory(abilityCat);
        if (innateCategory == null) {
            missingCat = abilityCat;
        }
        if (innateCategory == null || category == null) {
            final String msg = LanguageBundle.getFormattedString(//$NON-NLS-1$
            "Warnings.PCGenParser.AbilityCategoryNotFound", abilityKey, missingCat);
            warnings.add(msg);
            return;
        }
        ability = Globals.getContext().getReferenceContext().silentlyGetConstructedCDOMObject(Ability.class, innateCategory, abilityKey);
        if (ability == null) {
            warnings.add("Unable to Find Ability: " + abilityKey);
            return;
        }
    }
    List<String> associations = new ArrayList<>();
    List<BonusObj> bonuses = new ArrayList<>();
    while (it.hasNext()) {
        final PCGElement element = it.next();
        final String tag = element.getName();
        if (tag.equals(IOConstants.TAG_APPLIEDTO)) {
            associations.add(EntityEncoder.decode(element.getText()));
        } else if (IOConstants.TAG_SAVE.equals(tag)) {
            final String saveKey = EntityEncoder.decode(element.getText());
            // TODO - This never gets written to the file
            if (saveKey.startsWith(IOConstants.TAG_BONUS) && (saveKey.length() > 6)) {
                final BonusObj aBonus = Bonus.newBonus(Globals.getContext(), saveKey.substring(6));
                if (aBonus != null) {
                    bonuses.add(aBonus);
                }
            } else {
                if (Logging.isDebugMode()) {
                    Logging.debugPrint("Ignoring SAVE:" + saveKey);
                }
            }
        }
    }
    if (ability != null && category != null && nature != null) {
        CNAbility cna = null;
        boolean needError = true;
        if (nature == Nature.NORMAL) {
            // lines, save the feat now.
            if (!featsPresent || category != AbilityCategory.FEAT) {
                try {
                    cna = CNAbilityFactory.getCNAbility(category, nature, ability);
                } catch (IllegalArgumentException e) {
                    Logging.log(Logging.INFO, "Unabe to parse ability line: " + e.getMessage());
                }
            } else {
                needError = false;
            }
        } else if (nature == Nature.VIRTUAL) {
            cna = CNAbilityFactory.getCNAbility(category, nature, ability);
        }
        if (cna == null) {
            if (needError) {
                warnings.add("Unable to build Ability: " + ability);
            }
        } else {
            if (ability.getSafe(ObjectKey.MULTIPLE_ALLOWED)) {
                for (String appliedToKey : associations) {
                    String[] assoc = appliedToKey.split(Constants.COMMA, -1);
                    for (String string : assoc) {
                        CNAbilitySelection cnas = new CNAbilitySelection(cna, string);
                        try {
                            if (nature == Nature.VIRTUAL) {
                                thePC.addSavedAbility(cnas, UserSelection.getInstance(), UserSelection.getInstance());
                            } else {
                                thePC.addAbility(cnas, UserSelection.getInstance(), UserSelection.getInstance());
                            }
                        } catch (IllegalArgumentException e) {
                            Logging.errorPrint("PCGVer2Parser.parseAbilityLine failed", e);
                            warnings.add(cna + " with selection: " + string + " is no longer valid.");
                        }
                    }
                }
            } else {
                if (associations != null && !associations.isEmpty()) {
                    warnings.add(cna + " found with selections: " + associations + " but is MULT:NO in the data");
                }
                CNAbilitySelection cnas = new CNAbilitySelection(cna);
                if (nature == Nature.VIRTUAL) {
                    thePC.addSavedAbility(cnas, UserSelection.getInstance(), UserSelection.getInstance());
                } else {
                    thePC.addAbility(cnas, UserSelection.getInstance(), UserSelection.getInstance());
                }
            }
            for (BonusObj b : bonuses) {
                thePC.addSaveableBonus(b, cna.getAbility());
            }
        }
    }
}
Also used : Nature(pcgen.cdom.enumeration.Nature) CNAbility(pcgen.cdom.content.CNAbility) Ability(pcgen.core.Ability) SpecialAbility(pcgen.core.SpecialAbility) BonusObj(pcgen.core.bonus.BonusObj) ArrayList(java.util.ArrayList) CategorisedKey(pcgen.io.migration.AbilityMigration.CategorisedKey) CNAbility(pcgen.cdom.content.CNAbility) CNAbilitySelection(pcgen.cdom.helper.CNAbilitySelection) AbilityCategory(pcgen.core.AbilityCategory)

Aggregations

CNAbilitySelection (pcgen.cdom.helper.CNAbilitySelection)70 CNAbility (pcgen.cdom.content.CNAbility)35 Test (org.junit.Test)28 Ability (pcgen.core.Ability)26 ArrayList (java.util.ArrayList)17 AbilityCategory (pcgen.core.AbilityCategory)7 CDOMReference (pcgen.cdom.base.CDOMReference)6 PlayerCharacter (pcgen.core.PlayerCharacter)6 ParseResult (pcgen.rules.persistence.token.ParseResult)6 ConcretePersistentTransitionChoice (pcgen.cdom.base.ConcretePersistentTransitionChoice)5 AbilityRefChoiceSet (pcgen.cdom.choiceset.AbilityRefChoiceSet)5 Nature (pcgen.cdom.enumeration.Nature)5 PCTemplate (pcgen.core.PCTemplate)5 CharID (pcgen.cdom.enumeration.CharID)4 List (java.util.List)3 AbilityChoiceSet (pcgen.cdom.base.ChoiceSet.AbilityChoiceSet)3 SpecialAbility (pcgen.core.SpecialAbility)3 BonusObj (pcgen.core.bonus.BonusObj)3 AbstractTokenModelTest (tokenmodel.testsupport.AbstractTokenModelTest)3 HashSet (java.util.HashSet)2