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());
}
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;
}
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();
}
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);
}
}
}
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());
}
}
}
}
Aggregations