use of pcgen.core.prereq.Prerequisite in project pcgen by PCGen.
the class NaturalattacksLst method getRequiredSize.
/**
* Retrieve the required size (i.e. PRESIZE) for the object defining the attack. Will
* only return a value if there is a single size.
* @param obj The defining object.
* @return The size integer, or null if none (or multiple) specified.
*/
private Integer getRequiredSize(CDOMObject obj) {
Set<Prerequisite> sizePrereqs = new HashSet<>();
for (Prerequisite prereq : obj.getPrerequisiteList()) {
sizePrereqs.addAll(PrerequisiteUtilities.getPreReqsOfKind(prereq, "SIZE"));
}
Integer requiredSize = null;
for (Prerequisite prereq : sizePrereqs) {
SizeAdjustment sa = Globals.getContext().getReferenceContext().silentlyGetConstructedCDOMObject(SizeAdjustment.class, prereq.getOperand());
final int targetSize = sa.get(IntegerKey.SIZEORDER);
if (requiredSize != null && requiredSize != targetSize) {
return null;
}
requiredSize = targetSize;
}
return requiredSize;
}
use of pcgen.core.prereq.Prerequisite in project pcgen by PCGen.
the class DomainsToken method unparse.
@Override
public String[] unparse(LoadContext context, Spell spell) {
DoubleKeyMapToList<Prerequisite, Integer, CDOMReference<DomainSpellList>> dkmtl = new DoubleKeyMapToList<>();
List<String> list = new ArrayList<>();
Changes<CDOMReference<DomainSpellList>> masterChanges = context.getListContext().getMasterListChanges(getTokenName(), spell, SPELLLIST_CLASS);
if (masterChanges.includesGlobalClear()) {
list.add(Constants.LST_DOT_CLEAR_ALL);
}
if (masterChanges.hasRemovedItems()) {
for (CDOMReference<DomainSpellList> swl : masterChanges.getRemoved()) {
AssociatedChanges<Spell> changes = context.getListContext().getChangesInMasterList(getTokenName(), spell, swl);
MapToList<Spell, AssociatedPrereqObject> map = changes.getRemovedAssociations();
if (map != null && !map.isEmpty()) {
for (Spell added : map.getKeySet()) {
if (!spell.getLSTformat().equals(added.getLSTformat())) {
context.addWriteMessage("Spell " + getTokenName() + " token cannot remove another Spell " + "(must only remove itself)");
return null;
}
for (AssociatedPrereqObject assoc : map.getListFor(added)) {
List<Prerequisite> prereqs = assoc.getPrerequisiteList();
if (prereqs != null && !prereqs.isEmpty()) {
context.addWriteMessage("Incoming Remove " + "Edge to " + spell.getKeyName() + " had a " + "Prerequisite: " + prereqs.size());
return null;
}
dkmtl.addToListFor(null, -1, swl);
}
}
}
}
}
for (CDOMReference<DomainSpellList> swl : masterChanges.getAdded()) {
AssociatedChanges<Spell> changes = context.getListContext().getChangesInMasterList(getTokenName(), spell, swl);
Collection<Spell> removedItems = changes.getRemoved();
if (removedItems != null && !removedItems.isEmpty() || changes.includesGlobalClear()) {
context.addWriteMessage(getTokenName() + " does not support .CLEAR.");
return null;
}
MapToList<Spell, AssociatedPrereqObject> map = changes.getAddedAssociations();
if (map != null && !map.isEmpty()) {
for (Spell added : map.getKeySet()) {
if (!spell.getLSTformat().equals(added.getLSTformat())) {
context.addWriteMessage("Spell " + getTokenName() + " token cannot allow another Spell " + "(must only allow itself)");
return null;
}
for (AssociatedPrereqObject assoc : map.getListFor(added)) {
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("Incoming Edge to " + spell.getKeyName() + " had more than one " + "Prerequisite: " + prereqs.size());
return null;
}
Integer level = assoc.getAssociation(AssociationKey.SPELL_LEVEL);
if (level == null) {
context.addWriteMessage("Incoming Allows Edge to " + spell.getKeyName() + " had no Spell Level defined");
return null;
}
if (level.intValue() < 0) {
context.addWriteMessage("Incoming Allows Edge to " + spell.getKeyName() + " had invalid Level: " + level + ". Must be >= 0.");
return null;
}
dkmtl.addToListFor(prereq, level, swl);
}
}
}
}
if (dkmtl.isEmpty()) {
if (list.isEmpty()) {
// Legal if no DOMAINS was present in the Spell
return null;
} else {
return list.toArray(new String[list.size()]);
}
}
PrerequisiteWriter prereqWriter = new PrerequisiteWriter();
SortedSet<CDOMReference<DomainSpellList>> set = new TreeSet<>(ReferenceUtilities.REFERENCE_SORTER);
SortedSet<Integer> levelSet = new TreeSet<>();
for (Prerequisite prereq : dkmtl.getKeySet()) {
StringBuilder sb = new StringBuilder();
boolean needPipe = false;
levelSet.clear();
levelSet.addAll(dkmtl.getSecondaryKeySet(prereq));
for (Integer i : levelSet) {
set.clear();
set.addAll(dkmtl.getListFor(prereq, i));
if (needPipe) {
sb.append(Constants.PIPE);
}
sb.append(ReferenceUtilities.joinLstFormat(set, Constants.COMMA));
sb.append('=').append(i);
needPipe = true;
}
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(']');
}
list.add(sb.toString());
}
return list.toArray(new String[list.size()]);
}
use of pcgen.core.prereq.Prerequisite in project pcgen by PCGen.
the class ClassesToken method unparse.
@Override
public String[] unparse(LoadContext context, Spell spell) {
DoubleKeyMapToList<Prerequisite, Integer, CDOMReference<ClassSpellList>> dkmtl = new DoubleKeyMapToList<>();
List<String> list = new ArrayList<>();
Changes<CDOMReference<ClassSpellList>> masterChanges = context.getListContext().getMasterListChanges(getTokenName(), spell, SPELLLIST_CLASS);
if (masterChanges.includesGlobalClear()) {
list.add(Constants.LST_DOT_CLEAR_ALL);
}
if (masterChanges.hasRemovedItems()) {
for (CDOMReference<ClassSpellList> swl : masterChanges.getRemoved()) {
AssociatedChanges<Spell> changes = context.getListContext().getChangesInMasterList(getTokenName(), spell, swl);
MapToList<Spell, AssociatedPrereqObject> map = changes.getRemovedAssociations();
if (map != null && !map.isEmpty()) {
for (Spell added : map.getKeySet()) {
if (!spell.getLSTformat().equals(added.getLSTformat())) {
context.addWriteMessage("Spell " + getTokenName() + " token cannot remove another Spell " + "(must only remove itself)");
return null;
}
for (AssociatedPrereqObject assoc : map.getListFor(added)) {
List<Prerequisite> prereqs = assoc.getPrerequisiteList();
if (prereqs != null && !prereqs.isEmpty()) {
context.addWriteMessage("Incoming Remove " + "Edge to " + spell.getKeyName() + " had a " + "Prerequisite: " + prereqs.size());
return null;
}
dkmtl.addToListFor(null, -1, swl);
}
}
}
}
}
for (CDOMReference<ClassSpellList> swl : masterChanges.getAdded()) {
AssociatedChanges<Spell> changes = context.getListContext().getChangesInMasterList(getTokenName(), spell, swl);
Collection<Spell> removedItems = changes.getRemoved();
if (removedItems != null && !removedItems.isEmpty() || changes.includesGlobalClear()) {
context.addWriteMessage(getTokenName() + " does not support .CLEAR.");
return null;
}
MapToList<Spell, AssociatedPrereqObject> map = changes.getAddedAssociations();
if (map != null && !map.isEmpty()) {
for (Spell added : map.getKeySet()) {
if (!spell.getLSTformat().equals(added.getLSTformat())) {
context.addWriteMessage("Spell " + getTokenName() + " token cannot allow another Spell " + "(must only allow itself)");
return null;
}
for (AssociatedPrereqObject assoc : map.getListFor(added)) {
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("Incoming Edge to " + spell.getKeyName() + " had more than one " + "Prerequisite: " + prereqs.size());
return null;
}
Integer level = assoc.getAssociation(AssociationKey.SPELL_LEVEL);
if (level == null) {
context.addWriteMessage("Incoming Allows Edge to " + spell.getKeyName() + " had no Spell Level defined");
return null;
}
if (level.intValue() < 0) {
context.addWriteMessage("Incoming Allows Edge to " + spell.getKeyName() + " had invalid Level: " + level + ". Must be >= 0.");
return null;
}
dkmtl.addToListFor(prereq, level, swl);
}
}
}
}
if (dkmtl.isEmpty()) {
if (list.isEmpty()) {
// Legal if no CLASSES was present in the Spell
return null;
} else {
return list.toArray(new String[list.size()]);
}
}
PrerequisiteWriter prereqWriter = new PrerequisiteWriter();
SortedSet<CDOMReference<ClassSpellList>> set = new TreeSet<>(ReferenceUtilities.REFERENCE_SORTER);
SortedSet<Integer> levelSet = new TreeSet<>();
for (Prerequisite prereq : dkmtl.getKeySet()) {
StringBuilder sb = new StringBuilder();
boolean needPipe = false;
levelSet.clear();
levelSet.addAll(dkmtl.getSecondaryKeySet(prereq));
for (Integer i : levelSet) {
set.clear();
set.addAll(dkmtl.getListFor(prereq, i));
if (needPipe) {
sb.append(Constants.PIPE);
}
sb.append(ReferenceUtilities.joinLstFormat(set, Constants.COMMA));
sb.append('=').append(i);
needPipe = true;
}
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(']');
}
list.add(sb.toString());
}
return list.toArray(new String[list.size()]);
}
use of pcgen.core.prereq.Prerequisite in project pcgen by PCGen.
the class DataSetTest method testGetPrereqAbilities.
/**
* Verify the getPrereqAbilities method is functioning correctly.
* @throws Exception
*/
public void testGetPrereqAbilities() throws Exception {
Ability acrobatics = TestHelper.makeAbility("Acrobatics", AbilityCategory.FEAT, "general");
Ability dodge = TestHelper.makeAbility("Dodge", AbilityCategory.FEAT, "general");
Ability mobility = TestHelper.makeAbility("Mobility", AbilityCategory.FEAT, "general");
Ability springAttack = TestHelper.makeAbility("Spring Attack", AbilityCategory.FEAT, "general");
PreAbilityParser parser = new PreAbilityParser();
Prerequisite prereq = parser.parse("ability", "1,CATEGORY=FEAT,KEY_Dodge", false, false);
mobility.addPrerequisite(prereq);
prereq = parser.parse("ability", "2,CATEGORY=FEAT,KEY_Dodge,KEY_Mobility", false, false);
springAttack.addPrerequisite(prereq);
DataSet dataset = new DataSet(Globals.getContext(), SettingsHandler.getGame(), new DefaultListFacade<>());
List<AbilityFacade> abilities = dataset.getPrereqAbilities(acrobatics);
assertEquals("Acrobatics prereq should be empty", 0, abilities.size());
abilities = dataset.getPrereqAbilities(dodge);
assertEquals("Dodge prereq should be empty", 0, abilities.size());
abilities = dataset.getPrereqAbilities(mobility);
assertEquals("Mobility prereq should not be empty", 1, abilities.size());
assertEquals("Mobility prereq should be dodge", dodge, abilities.get(0));
abilities = dataset.getPrereqAbilities(springAttack);
assertEquals("Spring Attack prereq should not be empty", 2, abilities.size());
assertEquals("Spring Attack prereq should be dodge", dodge, abilities.get(0));
assertEquals("Spring Attack prereq should be mobility", mobility, abilities.get(1));
}
use of pcgen.core.prereq.Prerequisite in project pcgen by PCGen.
the class DescriptionTest method testPreReqs.
/**
* Test PREREQs for Desc
* @throws Exception
*/
public void testPreReqs() throws Exception {
final Ability dummy = TestHelper.makeAbility("dummy", AbilityCategory.FEAT, "Foo");
final String simpleDesc = "This is a test";
final Description desc = new Description(simpleDesc);
final PreParserFactory factory = PreParserFactory.getInstance();
final Prerequisite prereqNE = factory.parse("PRETEMPLATE:1,KEY_Natural Lycanthrope");
desc.addPrerequisite(prereqNE);
is(desc.getDescription(getCharacter(), Collections.singletonList(CNAbilityFactory.getCNAbility(AbilityCategory.FEAT, Nature.NORMAL, dummy))), strEq(""));
PCTemplate template = new PCTemplate();
template.setName("Natural Lycanthrope");
template.put(StringKey.KEY_NAME, "KEY_Natural Lycanthrope");
Globals.getContext().getReferenceContext().importObject(template);
getCharacter().addTemplate(template);
is(desc.getDescription(getCharacter(), Collections.singletonList(CNAbilityFactory.getCNAbility(AbilityCategory.FEAT, Nature.NORMAL, dummy))), strEq(simpleDesc));
}
Aggregations