use of pcgen.core.prereq.Prerequisite in project pcgen by PCGen.
the class EquipmentModifierTest method testChoice.
/**
* Test the expansion of the %CHOICE in a prereq for a bonus. Note as the
* options for the choice are processed in reverse order, we have to check the
* values in reverse order.
*/
public void testChoice() {
LoadContext context = Globals.getContext();
final EquipmentModifier eqMod = new EquipmentModifier();
final BonusObj aBonus = Bonus.newBonus(context, "WEAPON|TOHIT|-2|PREVARGT:%CHOICE,STR");
final Equipment e = new Equipment();
e.addAssociation(eqMod, "+1");
e.addAssociation(eqMod, "+2");
eqMod.addToListFor(ListKey.BONUS, aBonus);
final List<BonusObj> list = eqMod.getBonusList(e);
for (int j = list.size() - 1; j > 0; j--) {
final BonusObj bonusObj = list.get(j);
assertEquals("-2", bonusObj.getValue());
final Prerequisite prereq = bonusObj.getPrerequisiteList().get(0);
assertEquals("+" + (j + 1), prereq.getKey());
assertEquals("STR", prereq.getOperand());
}
assertEquals("-2", aBonus.getValue());
final Prerequisite prereq = aBonus.getPrerequisiteList().get(0);
assertEquals("%CHOICE", prereq.getKey());
}
use of pcgen.core.prereq.Prerequisite in project pcgen by PCGen.
the class PreWeaponProfWriter method specialCase.
@Override
public boolean specialCase(Writer writer, Prerequisite prereq) throws IOException {
PrerequisiteOperator po = getConsolidateMethod(kindHandled(), prereq, false);
if (po == null) {
return false;
}
if (!po.equals(prereq.getOperator())) {
writer.write('!');
}
writer.write("PRE" + kindHandled().toUpperCase() + ':' + (prereq.isOverrideQualify() ? "Q:" : ""));
writer.write(po.equals(PrerequisiteOperator.GTEQ) ? prereq.getOperand() : "1");
for (Prerequisite p : prereq.getPrerequisites()) {
writer.write(',');
writer.write(p.getKey());
}
return true;
}
use of pcgen.core.prereq.Prerequisite in project pcgen by PCGen.
the class ArmorProfToken method parseNonEmptyToken.
@Override
protected ParseResult parseNonEmptyToken(LoadContext context, CDOMObject obj, String value) {
String armorProf;
// Do not initialize, null is significant!
Prerequisite prereq = null;
boolean isPre = false;
if (value.indexOf("[") == -1) {
// Supported version of PRExxx using |. Needs to be at the front of the
// Parsing code because many objects expect the pre to have been determined
// Ahead of time. Until deprecated code is removed, it will have to stay
// like this.
armorProf = value;
StringTokenizer tok = new StringTokenizer(armorProf, Constants.PIPE);
while (tok.hasMoreTokens()) {
String token = tok.nextToken();
if (PreParserFactory.isPreReqString(token)) {
if (isPre) {
String errorText = "Invalid " + getTokenName() + ": " + value + " PRExxx must be at the END of the Token";
Logging.errorPrint(errorText);
return new ParseResult.Fail(errorText, context);
}
prereq = getPrerequisite(token);
if (prereq == null) {
return new ParseResult.Fail("Error generating Prerequisite " + prereq + " in " + getFullName(), context);
}
int preStart = value.indexOf(token) - 1;
armorProf = value.substring(0, preStart);
isPre = true;
}
}
} else {
return new ParseResult.Fail("Use of [] for Prerequisites has been removed. " + "Please use | based standard", context);
}
ParseResult pr = checkSeparatorsAndNonEmpty('|', armorProf);
if (!pr.passed()) {
return pr;
}
boolean foundAny = false;
boolean foundOther = false;
StringTokenizer tok = new StringTokenizer(armorProf, Constants.PIPE);
List<CDOMReference<ArmorProf>> armorProfs = new ArrayList<>();
List<CDOMReference<Equipment>> equipTypes = new ArrayList<>();
while (tok.hasMoreTokens()) {
String aProf = tok.nextToken();
if (Constants.LST_PERCENT_LIST.equals(aProf)) {
foundOther = true;
ChooseSelectionActor<ArmorProf> cra;
if (prereq == null) {
cra = this;
} else {
ConditionalSelectionActor<ArmorProf> cca = new ConditionalSelectionActor<>(this);
cca.addPrerequisite(prereq);
cra = cca;
}
context.getObjectContext().addToList(obj, ListKey.NEW_CHOOSE_ACTOR, cra);
} else if (Constants.LST_ALL.equalsIgnoreCase(aProf)) {
foundAny = true;
armorProfs.add(context.getReferenceContext().getCDOMAllReference(ARMORPROF_CLASS));
} else if (aProf.startsWith("ARMORTYPE.") || aProf.startsWith("ARMORTYPE=")) {
foundOther = true;
CDOMReference<Equipment> ref = TokenUtilities.getTypeReference(context, EQUIPMENT_CLASS, "ARMOR." + aProf.substring(10));
if (ref == null) {
return ParseResult.INTERNAL_ERROR;
}
equipTypes.add(ref);
} else {
foundOther = true;
armorProfs.add(context.getReferenceContext().getCDOMReference(ARMORPROF_CLASS, aProf));
}
}
if (foundAny && foundOther) {
return new ParseResult.Fail("Non-sensical " + getFullName() + ": Contains ANY and a specific reference: " + value, context);
}
if (!armorProfs.isEmpty() || !equipTypes.isEmpty()) {
ArmorProfProvider pp = new ArmorProfProvider(armorProfs, equipTypes);
if (prereq != null) {
pp.addPrerequisite(prereq);
}
context.getObjectContext().addToList(obj, ListKey.AUTO_ARMORPROF, pp);
}
return ParseResult.SUCCESS;
}
use of pcgen.core.prereq.Prerequisite in project pcgen by PCGen.
the class LangToken method unparse.
@Override
public String[] unparse(LoadContext context, CDOMObject obj) {
PrerequisiteWriter prereqWriter = new PrerequisiteWriter();
Changes<QualifiedObject<CDOMReference<Language>>> changes = context.getObjectContext().getListChanges(obj, ListKey.AUTO_LANGUAGE);
Changes<ChooseSelectionActor<?>> listChanges = context.getObjectContext().getListChanges(obj, ListKey.NEW_CHOOSE_ACTOR);
Collection<QualifiedObject<CDOMReference<Language>>> added = changes.getAdded();
StringBuilder sb = new StringBuilder();
Collection<ChooseSelectionActor<?>> listAdded = listChanges.getAdded();
boolean foundAny = false;
boolean foundOther = false;
if (changes.includesGlobalClear()) {
sb.append(Constants.LST_DOT_CLEAR);
}
if (listAdded != null && !listAdded.isEmpty()) {
for (ChooseSelectionActor<?> cra : listAdded) {
if (cra.getSource().equals(getTokenName())) {
try {
if (sb.length() > 0) {
sb.append('|');
}
sb.append(cra.getLstFormat());
foundOther = true;
} catch (PersistenceLayerException e) {
context.addWriteMessage("Error writing Prerequisite: " + e);
return null;
}
}
}
}
if (added != null) {
boolean needPipe = sb.length() > 0;
Prerequisite prereq = null;
for (QualifiedObject<CDOMReference<Language>> spp : added) {
CDOMReference<Language> lang = spp.getRawObject();
List<Prerequisite> prereqs = spp.getPrerequisiteList();
String ab = lang.getLSTformat(false);
boolean isUnconditionalAll = Constants.LST_ALL.equals(ab);
foundAny |= isUnconditionalAll;
foundOther |= !isUnconditionalAll;
if (needPipe) {
sb.append('|');
}
needPipe = true;
if (prereqs != null && !prereqs.isEmpty()) {
if (prereqs.size() > 1) {
context.addWriteMessage("Error: " + obj.getClass().getSimpleName() + " had more than one Prerequisite for " + getFullName());
return null;
}
Prerequisite p = prereqs.get(0);
if (prereq != null && !p.equals(prereq)) {
context.addWriteMessage("Error: " + obj.getClass().getSimpleName() + " had differing Prerequisites for " + getFullName());
return null;
}
prereq = p;
}
sb.append(ab);
}
if (prereq != null) {
StringWriter swriter = new StringWriter();
try {
prereqWriter.write(swriter, prereq);
} catch (PersistenceLayerException e) {
context.addWriteMessage("Error writing Prerequisite: " + e);
return null;
}
sb.append('|').append(swriter.toString());
}
}
if (foundAny && foundOther) {
context.addWriteMessage("Non-sensical " + getFullName() + ": Contains ANY and a specific reference: " + sb);
return null;
}
if (sb.length() == 0) {
// okay
return null;
}
return new String[] { sb.toString() };
}
use of pcgen.core.prereq.Prerequisite in project pcgen by PCGen.
the class ShieldProfToken method parseNonEmptyToken.
@Override
protected ParseResult parseNonEmptyToken(LoadContext context, CDOMObject obj, String value) {
String shieldProf;
// Do not initialize, null is significant!
Prerequisite prereq = null;
boolean isPre = false;
if (value.indexOf("[") == -1) {
// Supported version of PRExxx using |. Needs to be at the front of the
// Parsing code because many objects expect the pre to have been determined
// Ahead of time. Until deprecated code is removed, it will have to stay
// like this.
shieldProf = value;
StringTokenizer tok = new StringTokenizer(shieldProf, Constants.PIPE);
while (tok.hasMoreTokens()) {
String token = tok.nextToken();
if (PreParserFactory.isPreReqString(token)) {
if (isPre) {
String errorText = "Invalid " + getTokenName() + ": " + value + " PRExxx must be at the END of the Token";
Logging.errorPrint(errorText);
return new ParseResult.Fail(errorText, context);
}
prereq = getPrerequisite(token);
if (prereq == null) {
return new ParseResult.Fail("Error generating Prerequisite " + prereq + " in " + getFullName(), context);
}
int preStart = value.indexOf(token) - 1;
shieldProf = value.substring(0, preStart);
isPre = true;
}
}
} else {
return new ParseResult.Fail("Use of [] for Prerequisites has been removed. " + "Please use | based standard", context);
}
ParseResult pr = checkForIllegalSeparator('|', shieldProf);
if (!pr.passed()) {
return pr;
}
boolean foundAny = false;
boolean foundOther = false;
StringTokenizer tok = new StringTokenizer(shieldProf, Constants.PIPE);
List<CDOMReference<ShieldProf>> shieldProfs = new ArrayList<>();
List<CDOMReference<Equipment>> equipTypes = new ArrayList<>();
while (tok.hasMoreTokens()) {
String aProf = tok.nextToken();
if (Constants.LST_PERCENT_LIST.equals(aProf)) {
foundOther = true;
ChooseSelectionActor<ShieldProf> cra;
if (prereq == null) {
cra = this;
} else {
ConditionalSelectionActor<ShieldProf> cca = new ConditionalSelectionActor<>(this);
cca.addPrerequisite(prereq);
cra = cca;
}
context.getObjectContext().addToList(obj, ListKey.NEW_CHOOSE_ACTOR, cra);
} else if (Constants.LST_ALL.equalsIgnoreCase(aProf)) {
foundAny = true;
shieldProfs.add(context.getReferenceContext().getCDOMAllReference(SHIELDPROF_CLASS));
} else if (aProf.startsWith("SHIELDTYPE.") || aProf.startsWith("SHIELDTYPE=")) {
foundOther = true;
CDOMReference<Equipment> ref = TokenUtilities.getTypeReference(context, EQUIPMENT_CLASS, "SHIELD." + aProf.substring(11));
if (ref == null) {
return ParseResult.INTERNAL_ERROR;
}
equipTypes.add(ref);
} else {
foundOther = true;
shieldProfs.add(context.getReferenceContext().getCDOMReference(SHIELDPROF_CLASS, aProf));
}
}
if (foundAny && foundOther) {
return new ParseResult.Fail("Non-sensical " + getFullName() + ": Contains ANY and a specific reference: " + value, context);
}
if (!shieldProfs.isEmpty() || !equipTypes.isEmpty()) {
ShieldProfProvider pp = new ShieldProfProvider(shieldProfs, equipTypes);
if (prereq != null) {
pp.addPrerequisite(prereq);
}
context.getObjectContext().addToList(obj, ListKey.AUTO_SHIELDPROF, pp);
}
return ParseResult.SUCCESS;
}
Aggregations