use of pcgen.persistence.lst.prereq.PreParserFactory in project pcgen by PCGen.
the class PreHDTest method testHD.
/**
* Test the PREHD code
* @throws Exception
*/
public void testHD() throws Exception {
race.setName("Human");
CDOMDirectSingleRef<SizeAdjustment> mediumRef = CDOMDirectSingleRef.getRef(medium);
race.put(FormulaKey.SIZE, new FixedSizeFormula(mediumRef));
Globals.getContext().getReferenceContext().importObject(race);
PCClass raceClass = new PCClass();
raceClass.setName("Race Class");
raceClass.put(StringKey.KEY_NAME, "RaceClass");
raceClass.put(ObjectKey.IS_MONSTER, true);
Globals.getContext().getReferenceContext().importObject(raceClass);
race.put(ObjectKey.MONSTER_CLASS, new LevelCommandFactory(CDOMDirectSingleRef.getRef(raceClass), FormulaFactory.getFormulaFor(3)));
final PlayerCharacter character = getCharacter();
character.setRace(race);
Prerequisite prereq;
final PreParserFactory factory = PreParserFactory.getInstance();
prereq = factory.parse("PREHD:MIN=4");
assertFalse("Character doesn't have 4 HD", PrereqHandler.passes(prereq, character, null));
prereq = factory.parse("PREHD:MIN=3");
assertTrue("Character has 3 HD", PrereqHandler.passes(prereq, character, null));
prereq = factory.parse("PREHD:MIN=1,MAX=3");
assertTrue("Character has 3 HD", PrereqHandler.passes(prereq, character, null));
prereq = factory.parse("PREHD:MIN=3,MAX=6");
assertTrue("Character has 3 HD", PrereqHandler.passes(prereq, character, null));
prereq = factory.parse("PREHD:MIN=4,MAX=7");
assertFalse("Character doesn't have 4 HD", PrereqHandler.passes(prereq, character, null));
prereq = factory.parse("PREHD:MIN=1,MAX=2");
assertFalse("Character doesn't have 2 or less HD", PrereqHandler.passes(prereq, character, null));
}
use of pcgen.persistence.lst.prereq.PreParserFactory in project pcgen by PCGen.
the class BonusToken method getBonusToken.
/**
* Get the bonus token
* @param tokenSource
* @param pc
* @return bonus token
*/
public static String getBonusToken(String tokenSource, PlayerCharacter pc) {
StringTokenizer bonusTok = new StringTokenizer(tokenSource, ".", false);
// tokenSource should follow this format:
// BONUS.COMBAT.AC.TOTAL
// or
// BONUS.COMBAT.AC.Armor
// or
// BONUS.COMBAT.AC.TOTAL.!BASE.!Armor.!Ability.!Size
// First token should be: BONUS
bonusTok.nextToken();
// next should be category of bonus: COMBAT
final String aType = bonusTok.nextToken();
// next should be name of bonus: AC
final String aName = bonusTok.nextToken();
double total = 0;
int decimals = 0;
double lastValue = 0;
int signIt = 1;
while (bonusTok.hasMoreTokens()) {
String bucket = bonusTok.nextToken();
if (CoreUtility.doublesEqual(total, 0.0) && "LISTING".equals(bucket)) {
return pc.listBonusesFor(aType, aName);
}
if (PreParserFactory.isPreReqString(bucket)) {
if (Logging.isDebugMode()) {
Logging.debugPrint("Why is this not parsed in loading: " + bucket + " rather than in BonusToken.getBonusToken()");
}
Prerequisite prereq = null;
try {
PreParserFactory factory = PreParserFactory.getInstance();
prereq = factory.parse(bucket);
} catch (PersistenceLayerException ple) {
Logging.errorPrint(ple.getMessage(), ple);
}
if (!PrereqHandler.passes(prereq, pc, null)) {
total -= lastValue * signIt;
lastValue = 0;
}
continue;
}
if (bucket.startsWith("MIN=")) {
double x = Float.parseFloat(bucket.substring(4));
if (lastValue < x) {
total -= (lastValue - x);
}
continue;
} else if (bucket.startsWith("MAX=")) {
double x = Float.parseFloat(bucket.substring(4));
x = Math.min(x, lastValue);
total -= (lastValue - x);
lastValue = 0;
continue;
}
signIt = 1;
if ((!bucket.isEmpty()) && (bucket.charAt(0) == '!')) {
signIt = -1;
bucket = bucket.substring(1);
}
if (bucket.equals("EQTYPE") && bonusTok.hasMoreTokens()) {
bucket += ("." + bonusTok.nextToken());
}
if ("TOTAL".equals(bucket)) {
lastValue = pc.getTotalBonusTo(aType, aName);
} else if (bucket.startsWith("DEC=")) {
decimals = Integer.parseInt(bucket.substring(4));
} else if (bucket.startsWith("TYPE=") || bucket.startsWith("EQTYPE.")) {
lastValue = 0;
String restOfBucket;
if (bucket.startsWith("TYPE=")) {
restOfBucket = bucket.substring(5);
} else {
restOfBucket = bucket.substring(7);
}
for (Equipment eq : pc.getEquipmentOfType(restOfBucket, "", 1)) {
lastValue += eq.bonusTo(pc, aType, aName, true);
}
} else {
lastValue = pc.getBonusDueToType(aType, aName, bucket);
}
total += (lastValue * signIt);
}
return String.valueOf((int) (total * Math.pow(10, decimals)) / (int) Math.pow(10, decimals));
}
use of pcgen.persistence.lst.prereq.PreParserFactory in project pcgen by PCGen.
the class PreWeaponProfTest method testMultiple.
/**
* Tests to see if a character has a certain number of weaponprofs from a list
* @throws Exception
*/
public void testMultiple() throws Exception {
final PlayerCharacter character = getCharacter();
PCTemplate pct = new PCTemplate();
LoadContext context = Globals.getContext();
context.unconditionallyProcess(pct, "AUTO", "WEAPONPROF|Longsword|Dagger");
assertTrue(context.getReferenceContext().resolveReferences(null));
Prerequisite prereq;
final PreParserFactory factory = PreParserFactory.getInstance();
prereq = factory.parse("PREWEAPONPROF:1,Longsword,Dagger");
assertFalse("Character has no proficiencies", PrereqHandler.passes(prereq, character, null));
character.addTemplate(pct);
assertTrue("Character has one of Longsword or Dagger proficiency", PrereqHandler.passes(prereq, character, null));
prereq = factory.parse("PREWEAPONPROF:2,Longsword,Dagger");
assertTrue("Character has both Longsword and Dagger proficiency", PrereqHandler.passes(prereq, character, null));
prereq = factory.parse("PREWEAPONPROF:3,Longsword,Dagger,Longbow");
assertFalse("Character has both Longsword and Dagger proficiency but not Longbow", PrereqHandler.passes(prereq, character, null));
}
use of pcgen.persistence.lst.prereq.PreParserFactory in project pcgen by PCGen.
the class PreWeaponProfTest method testInverse.
/**
* Test with negation
* @throws Exception
*/
public void testInverse() throws Exception {
final PlayerCharacter character = getCharacter();
PCTemplate pct = new PCTemplate();
LoadContext context = Globals.getContext();
context.unconditionallyProcess(pct, "AUTO", "WEAPONPROF|Longsword|Dagger");
assertTrue(context.getReferenceContext().resolveReferences(null));
Prerequisite prereq;
final PreParserFactory factory = PreParserFactory.getInstance();
prereq = factory.parse("!PREWEAPONPROF:1,Longsword");
assertTrue("Character has no proficiencies", PrereqHandler.passes(prereq, character, null));
character.addTemplate(pct);
assertFalse("Character has the Longsword proficiency.", PrereqHandler.passes(prereq, character, null));
prereq = factory.parse("!PREWEAPONPROF:1,Longbow");
assertTrue("Character does not have the Longbow proficiency", PrereqHandler.passes(prereq, character, null));
prereq = factory.parse("!PREWEAPONPROF:1,Dagger");
assertFalse("Character has the Dagger proficiency.", PrereqHandler.passes(prereq, character, null));
}
use of pcgen.persistence.lst.prereq.PreParserFactory in project pcgen by PCGen.
the class PreSpellDescriptorTest method testTwoClassDescriptor.
public void testTwoClassDescriptor() throws Exception {
final PlayerCharacter character = getCharacter();
final PreParserFactory factory = PreParserFactory.getInstance();
Prerequisite prereq = factory.parse("PRESPELLDESCRIPTOR:3,Fire=2,Useful=2");
assertFalse(PrereqHandler.passes(prereq, character, null));
character.incrementClassLevel(1, wiz);
boolean passes = PrereqHandler.passes(prereq, character, null);
assertFalse(passes);
character.incrementClassLevel(1, wiz);
passes = PrereqHandler.passes(prereq, character, null);
assertFalse(passes);
character.incrementClassLevel(1, cle);
passes = PrereqHandler.passes(prereq, character, null);
assertFalse(passes);
character.incrementClassLevel(1, cle);
passes = PrereqHandler.passes(prereq, character, null);
assertTrue(passes);
}
Aggregations