use of pcgen.core.prereq.PrerequisiteException in project pcgen by PCGen.
the class PreMoveTester method passes.
/*
* (non-Javadoc)
* @see pcgen.core.prereq.PrerequisiteTest#passes(pcgen.core.prereq.Prerequisite, pcgen.core.PlayerCharacter)
*/
@Override
public int passes(final Prerequisite prereq, final CharacterDisplay display, CDOMObject source) throws PrerequisiteException {
int runningTotal = 0;
int moveAmount = 0;
if ((display != null) && display.hasMovement()) {
final String moveType = prereq.getKey();
try {
moveAmount = Integer.parseInt(prereq.getOperand());
} catch (NumberFormatException e) {
throw new PrerequisiteException(LanguageBundle.getFormattedString("PreMove.error.bad_operand", //$NON-NLS-1$
prereq.toString()));
}
int speed = (int) display.getMovementOfType(moveType);
if (speed >= moveAmount) {
runningTotal += speed;
}
}
runningTotal = prereq.getOperator().compare(runningTotal, moveAmount);
return countedTotal(prereq, runningTotal);
}
use of pcgen.core.prereq.PrerequisiteException in project pcgen by PCGen.
the class PreStatParser method parse.
/**
* Parse the pre req list
*
* @param kind The kind of the prerequisite (less the "PRE" prefix)
* @param formula The body of the prerequisite.
* @param invertResult Whether the prerequisite should invert the result.
* @param overrideQualify
* if set true, this prerequisite will be enforced in spite
* of any "QUALIFY" tag that may be present.
* @return PreReq
* @throws PersistenceLayerException
*/
@Override
public Prerequisite parse(String kind, String formula, boolean invertResult, boolean overrideQualify) throws PersistenceLayerException {
Prerequisite prereq = super.parse(kind, formula, invertResult, overrideQualify);
try {
// PREMULT
prereq.setKind(null);
// Get the comparator type STATGTEQ, STAT, STATNEQ etc.
String compType = kind.substring(4);
if (compType.isEmpty()) {
compType = "gteq";
}
String[] tokens = formula.split(",|\\|");
int currToken = 0;
// Get the minimum match count
String aString = tokens[currToken++];
try {
prereq.setOperand(Integer.toString(Integer.parseInt(aString)));
} catch (NumberFormatException e) {
Logging.errorPrint("Badly formed PRESTAT attribute: " + aString);
prereq.setOperand("1");
}
while (currToken < tokens.length) {
aString = tokens[currToken++];
final int idxEquals = aString.lastIndexOf('=');
if (idxEquals < 3) {
throw new PersistenceLayerException("PRE" + kindsHandled()[0] + " formula '" + formula + "' is not valid.");
}
final String stat = aString.substring(0, Math.min(3, idxEquals));
Prerequisite statPrereq = new Prerequisite();
statPrereq.setKind("stat");
statPrereq.setKey(stat);
statPrereq.setOperator(compType);
statPrereq.setOperand(aString.substring(idxEquals + 1));
prereq.addPrerequisite(statPrereq);
}
if ((prereq.getPrerequisiteCount() == 1) && (prereq.getOperator() == PrerequisiteOperator.GTEQ) && prereq.getOperand().equals("1")) {
prereq = prereq.getPrerequisites().get(0);
}
if (invertResult) {
prereq.setOperator(prereq.getOperator().invert());
}
} catch (PrerequisiteException pe) {
throw new PersistenceLayerException("Unable to parse the prerequisite :'" + kind + ':' + formula + "'. " + pe.getLocalizedMessage());
}
return prereq;
}
use of pcgen.core.prereq.PrerequisiteException in project pcgen by PCGen.
the class PreCampaignTester method passes.
/**
* @see pcgen.core.prereq.PrerequisiteTest#passes(pcgen.core.PlayerCharacter)
*/
@Override
public int passes(final Prerequisite prereq, final CharacterDisplay display, CDOMObject source) throws PrerequisiteException {
final int number;
try {
number = Integer.parseInt(prereq.getOperand());
} catch (NumberFormatException exceptn) {
throw new PrerequisiteException(LanguageBundle.getFormattedString("PreFeat.error", //$NON-NLS-1$
prereq.toString()));
}
int runningTotal = 0;
if (prereq.getKey().startsWith("BOOKTYPE=")) {
runningTotal += countCampaignByBookType(prereq.getKey().substring(9), false);
} else if (prereq.getKey().startsWith("INCLUDESBOOKTYPE=")) {
runningTotal += countCampaignByBookType(prereq.getKey().substring(17), true);
} else if (prereq.getKey().startsWith("INCLUDES=")) {
runningTotal += countCampaignByName(prereq.getKey().substring(9), source, true);
} else {
runningTotal += countCampaignByName(prereq.getKey(), source, false);
}
runningTotal = prereq.getOperator().compare(runningTotal, number);
return countedTotal(prereq, runningTotal);
}
use of pcgen.core.prereq.PrerequisiteException in project pcgen by PCGen.
the class PreVariableParser method parse.
/**
* Parse the pre req list
*
* @param kind The kind of the prerequisite (less the "PRE" prefix)
* @param formula The body of the prerequisite.
* @param invertResult Whether the prerequisite should invert the result.
* @param overrideQualify
* if set true, this prerequisite will be enforced in spite
* of any "QUALIFY" tag that may be present.
* @return PreReq
* @throws PersistenceLayerException
*/
@Override
public Prerequisite parse(String kind, String formula, boolean invertResult, boolean overrideQualify) throws PersistenceLayerException {
Prerequisite prereq = super.parse(kind, formula, invertResult, overrideQualify);
prereq.setKind("var");
// Get the comparator type SIZEGTEQ, BSIZE, SIZENEQ etc.
String compType = kind.substring(3);
if (compType.isEmpty()) {
compType = "gteq";
}
ParsingSeparator ps = new ParsingSeparator(formula, ',');
ps.addGroupingPair('[', ']');
ps.addGroupingPair('(', ')');
try {
int count = 0;
while (ps.hasNext()) {
String first = ps.next();
if (!ps.hasNext()) {
throw new PersistenceLayerException("Unable to parse prerequisite 'PRE" + kind + ':' + formula + "'. Incorrect parameter count (must be even)");
}
String second = ps.next();
Prerequisite subreq;
if (!ps.hasNext() && count == 0) {
subreq = prereq;
} else {
// PREMULT
prereq.setKind(null);
subreq = new Prerequisite();
subreq.setKind("var");
prereq.addPrerequisite(subreq);
count++;
}
subreq.setOperator(compType.intern());
subreq.setKey(first.intern());
subreq.setOperand(second.intern());
}
if (count > 0) {
prereq.setOperand(Integer.toString(count));
}
} catch (PrerequisiteException pe) {
throw new PersistenceLayerException("Unable to parse prerequisite 'PRE" + kind + ':' + formula + "'. " + pe.getLocalizedMessage());
}
if (invertResult) {
prereq.setOperator(prereq.getOperator().invert());
}
prereq.setOverrideQualify(overrideQualify);
return prereq;
}
use of pcgen.core.prereq.PrerequisiteException in project pcgen by PCGen.
the class PreAbilityTester method passes.
@Override
public int passes(final Prerequisite prereq, final PlayerCharacter character, CDOMObject source) throws PrerequisiteException {
final int number;
try {
number = Integer.parseInt(prereq.getOperand());
} catch (NumberFormatException exception) {
throw new PrerequisiteException(LanguageBundle.getFormattedString("PreAbility.error", //$NON-NLS-1$
prereq.toString()));
}
String categoryName = prereq.getCategoryName();
int runningTotal = PrerequisiteUtilities.passesAbilityTest(prereq, character, number, categoryName);
return countedTotal(prereq, runningTotal);
}
Aggregations