use of pcgen.rules.context.LoadContext in project pcgen by PCGen.
the class WieldCategoryLoader method parseLine.
/**
* Parse the WIELDCATEGORY line
*
* @param gameMode
* @param lstLine
* @throws PersistenceLayerException
*/
public void parseLine(GameMode gameMode, String lstLine, URI source) throws PersistenceLayerException {
LoadContext context = gameMode.getModeContext();
StringTokenizer colToken = new StringTokenizer(lstLine, SystemLoader.TAB_DELIM);
WieldCategory cat = null;
String preKey = null;
CDOMSingleRef<WieldCategory> preVal = null;
while (colToken.hasMoreTokens()) {
final String colString = colToken.nextToken().trim();
final int idxColon = colString.indexOf(':');
String key = "";
try {
key = colString.substring(0, idxColon);
} catch (StringIndexOutOfBoundsException e) {
// TODO Deal with Exception
}
if (key.equals("WIELDCATEGORY")) {
final String value = colString.substring(idxColon + 1).trim();
cat = context.getReferenceContext().silentlyGetConstructedCDOMObject(WieldCategory.class, value);
if (cat == null) {
cat = new WieldCategory();
cat.setName(value.intern());
gameMode.addWieldCategory(cat);
}
} else if (colString.startsWith("PREVAR")) {
//TODO ensure preKey is null
// a PREVARxx formula used to switch
// weapon categories based on size
preKey = colString;
} else if (key.equals("SWITCH")) {
//TODO ensure preVal is null
// If matches PRE, switch category to this
preVal = context.getReferenceContext().getCDOMReference(WieldCategory.class, colString.substring(7));
} else {
final String value = colString.substring(idxColon + 1).trim();
if (context.processToken(cat, key, value)) {
context.commit();
} else {
context.rollback();
Logging.replayParsedMessages();
}
Logging.clearParseMessages();
}
}
//TODO Error checking if preVal w/o preKey, vice versa, etc.
if ((cat != null) && (preVal != null) && (preKey != null)) {
try {
QualifiedObject<CDOMSingleRef<WieldCategory>> qo = new QualifiedObject<>(preVal);
qo.addPrerequisite(prereqParser.parse(preKey));
cat.addCategorySwitch(qo);
} catch (PersistenceLayerException ple) {
Logging.errorPrint("Error parsing Prerequisite in " + source + ": " + preKey + "\n " + ple.getMessage());
}
}
}
use of pcgen.rules.context.LoadContext in project pcgen by PCGen.
the class BonusLst method process.
@Override
public boolean process(LoadContext context, CDOMObject obj) {
List<BonusObj> bonusList = obj.getListFor(ListKey.BONUS);
boolean returnValue = true;
if (bonusList != null) {
for (BonusObj bonus : bonusList) {
String bonusName = bonus.getBonusName();
if ("ABILITYPOOL".equalsIgnoreCase(bonusName)) {
for (Object o : bonus.getBonusInfoList()) {
if (context.getReferenceContext().silentlyGetConstructedCDOMObject(ABILITY_CATEGORY_CLASS, o.toString()) == null) {
LoadContext dummyCtx = new RuntimeLoadContext(context.getReferenceContext(), new ConsolidatedListCommitStrategy());
dummyCtx.setSourceURI(obj.getSourceURI());
Logging.errorPrint("BONUS: " + bonus + " in " + obj.getClass().getSimpleName() + ' ' + obj.getKeyName() + " contained an invalid AbilityCategory " + o.toString(), dummyCtx);
returnValue = false;
}
}
} else if ("UDAM".equals(bonusName)) {
for (Object o : bonus.getBonusInfoList()) {
String classKey = o.toString();
final PCClass aClass = context.getReferenceContext().silentlyGetConstructedCDOMObject(PCCLASS_CLASS, classKey);
if (aClass == null) {
Logging.errorPrint("Could not find class '" + classKey + "' for UDAM token", context);
}
}
}
}
}
try {
obj.ownBonuses(obj);
} catch (CloneNotSupportedException e) {
Logging.errorPrint(e.getLocalizedMessage());
return false;
}
return returnValue;
}
use of pcgen.rules.context.LoadContext in project pcgen by PCGen.
the class PCClassTest method testBypassClassPrereqsDeprecated.
/**
* Test the interaction of prerequisites on PCClasses and bonuses and the
* Bypass Class Prereqs flag.
* @throws Exception
*/
public void testBypassClassPrereqsDeprecated() throws Exception {
LoadContext context = Globals.getContext();
// Setup class with prereqs and var based abilities with prereqs.
final PreVariableParser parser = new PreVariableParser();
final Prerequisite aPrereq = parser.parse("VARGTEQ", "Foo,1", false, false);
final GameMode gameMode = SettingsHandler.getGame();
RuleCheck aClassPreRule = gameMode.getModeContext().getReferenceContext().silentlyGetConstructedCDOMObject(RuleCheck.class, "CLASSPRE");
if (aClassPreRule == null) {
aClassPreRule = new RuleCheck();
aClassPreRule.setName("CLASSPRE");
gameMode.getModeContext().getReferenceContext().importObject(aClassPreRule);
}
aClassPreRule.setDefault(false);
final PCClass aPrClass = new PCClass();
aPrClass.setName("PreReqClass");
aPrClass.put(StringKey.KEY_NAME, "KEY_PreReqClass");
final BonusObj aBonus = Bonus.newBonus(context, "MISC|SR|10|PREVARGTEQ:Foo,2");
if (aBonus != null) {
aPrClass.addToListFor(ListKey.BONUS, aBonus);
}
aPrClass.addPrerequisite(aPrereq);
final PCClass aQClass = new PCClass();
aQClass.setName("QualClass");
aQClass.put(StringKey.KEY_NAME, "KEY_QualClass");
CDOMDirectSingleRef<PCClass> ref = CDOMDirectSingleRef.getRef(aPrClass);
aQClass.addToListFor(ListKey.QUALIFY, new Qualifier(PCClass.class, ref));
final PCClass aNqClass = new PCClass();
aNqClass.setName("NonQualClass");
aNqClass.put(StringKey.KEY_NAME, "KEY_NonQualClass");
aNqClass.put(VariableKey.getConstant("Foo"), FormulaFactory.ONE);
aNqClass.getOriginalClassLevel(2).put(VariableKey.getConstant("Foo"), FormulaFactory.getFormulaFor(2));
// Setup character without prereqs
final PlayerCharacter character = getCharacter();
// Test no prereqs and no bypass fails class and var
assertFalse("PC with no prereqs should fail class qual test.", aPrClass.qualifies(character, aPrClass));
assertEquals("PC with no prereqs should fail var qual test.", 0.0, aPrClass.getBonusTo("MISC", "SR", 1, character), 0.1);
// Test no prereqs and bypass passes class and fails var
aClassPreRule.setDefault(true);
assertTrue("PC with no prereqs should pass class qual test when bypassing prereqs is on.", aPrClass.qualifies(character, aPrClass));
assertEquals("PC with no prereqs should fail var qual test when bypass prereqs is on.", 0.0, aPrClass.getBonusTo("MISC", "SR", 1, character), 0.1);
// Test prereqs and bypass pass class and var
character.incrementClassLevel(1, aNqClass);
assertTrue("PC with prereqs and bypass should pass class qual test.", aPrClass.qualifies(character, aPrClass));
character.incrementClassLevel(1, aNqClass);
assertEquals("PC with prereqs and bypass should pass var qual test.", 10.0, aPrClass.getBonusTo("MISC", "SR", 1, character), 0.1);
// Test prereqs and no bypass passes class and var
aClassPreRule.setDefault(false);
assertTrue("PC with prereqs and no bypass should pass class qual test.", aPrClass.qualifies(character, aPrClass));
assertEquals("PC with prereqs and no bypass should pass var qual test.", 10.0, aPrClass.getBonusTo("MISC", "SR", 1, character), 0.1);
}
use of pcgen.rules.context.LoadContext in project pcgen by PCGen.
the class PCClassTest method testGetKnownForLevel.
public void testGetKnownForLevel() {
LoadContext context = Globals.getContext();
PCClass megaCasterClass = new PCClass();
megaCasterClass.setName("MegaCaster");
BuildUtilities.setFact(megaCasterClass, "SpellType", "Arcane");
context.unconditionallyProcess(megaCasterClass, "SPELLSTAT", "CHA");
megaCasterClass.put(ObjectKey.SPELLBOOK, false);
megaCasterClass.put(ObjectKey.MEMORIZE_SPELLS, false);
context.unconditionallyProcess(megaCasterClass.getOriginalClassLevel(1), "KNOWN", "4,2,2,3,4,5,0");
context.unconditionallyProcess(megaCasterClass.getOriginalClassLevel(1), "CAST", "3,1,2,3,4,5,0,0");
context.unconditionallyProcess(megaCasterClass.getOriginalClassLevel(2), "KNOWN", "4,2,2,3,4,5,6,7,8,9,10");
context.unconditionallyProcess(megaCasterClass.getOriginalClassLevel(2), "CAST", "3,1,2,3,4,5,6,7,8,9,10");
Globals.getContext().getReferenceContext().importObject(megaCasterClass);
context.getReferenceContext().buildDerivedObjects();
context.loadCampaignFacets();
final PlayerCharacter character = getCharacter();
// Test retrieval for a non-spell casting class.
character.incrementClassLevel(1, nqClass);
PCClass charClass = character.getClassKeyed(nqClass.getKeyName());
assertEquals("Known 0th level for non spell casting class", 0, character.getSpellSupport(charClass).getKnownForLevel(0, character));
// Test retrieval for a spell casting class.
character.incrementClassLevel(1, megaCasterClass);
charClass = character.getClassKeyed(megaCasterClass.getKeyName());
setPCStat(character, cha, 10);
assertEquals("Known 0th level for character's class", 4, character.getSpellSupport(charClass).getKnownForLevel(0, character));
assertEquals("Known 1st level where stat is too low", 0, character.getSpellSupport(charClass).getKnownForLevel(1, character));
setPCStat(character, cha, 11);
character.calcActiveBonuses();
assertEquals("Known 1st level where stat is high enough, but no bonus", 2, character.getSpellSupport(charClass).getKnownForLevel(1, character));
setPCStat(character, cha, 18);
character.calcActiveBonuses();
assertEquals("Known 1st level where stat gives bonus but not active", 2, character.getSpellSupport(charClass).getKnownForLevel(1, character));
RuleCheck bonusKnownRule = new RuleCheck();
bonusKnownRule.setName(RuleConstants.BONUSSPELLKNOWN);
bonusKnownRule.setDefault(true);
GameMode gameMode = SettingsHandler.getGame();
gameMode.getModeContext().getReferenceContext().importObject(bonusKnownRule);
BonusSpellInfo bsi = new BonusSpellInfo();
bsi.setName("1");
bsi.setStatScore(12);
bsi.setStatRange(8);
context.getReferenceContext().importObject(bsi);
bsi = new BonusSpellInfo();
bsi.setName("5");
bsi.setStatScore(20);
bsi.setStatRange(8);
assertEquals("Known 1st level where stat gives bonus and active", 3, character.getSpellSupport(charClass).getKnownForLevel(1, character));
assertEquals("Known 2nd level for character's class", 2, character.getSpellSupport(charClass).getKnownForLevel(2, character));
assertEquals("Known 3rd level for character's class", 3, character.getSpellSupport(charClass).getKnownForLevel(3, character));
assertEquals("Known 4th level for character's class", 4, character.getSpellSupport(charClass).getKnownForLevel(4, character));
charClass.put(IntegerKey.KNOWN_SPELLS_FROM_SPECIALTY, 1);
assertEquals("Known 5th level for character's class", 6, character.getSpellSupport(charClass).getKnownForLevel(5, character));
assertEquals("Known 6th level for character's class", 0, character.getSpellSupport(charClass).getKnownForLevel(6, character));
assertEquals("Known 7th level for character's class", 0, character.getSpellSupport(charClass).getKnownForLevel(7, character));
// Add spell bonus for level above known max
bsi = new BonusSpellInfo();
bsi.setName("7");
bsi.setStatScore(12);
bsi.setStatRange(8);
assertEquals("Known 7th level for character's class", 0, character.getSpellSupport(charClass).getKnownForLevel(7, character));
assertEquals("Known 8th level for character's class", 0, character.getSpellSupport(charClass).getKnownForLevel(8, character));
}
use of pcgen.rules.context.LoadContext in project pcgen by PCGen.
the class PCClassTest method testGetHighestLevelSpell.
/**
* Test the function of the getHighestLevelSpell method.
* @throws PersistenceLayerException
*/
public void testGetHighestLevelSpell() throws PersistenceLayerException {
LoadContext context = Globals.getContext();
PCClass megaCasterClass = new PCClass();
megaCasterClass.setName("MegaCaster");
BuildUtilities.setFact(megaCasterClass, "SpellType", "Arcane");
context.unconditionallyProcess(megaCasterClass, "SPELLSTAT", "CHA");
megaCasterClass.put(ObjectKey.SPELLBOOK, false);
megaCasterClass.put(ObjectKey.MEMORIZE_SPELLS, false);
context.unconditionallyProcess(megaCasterClass.getOriginalClassLevel(1), "KNOWN", "4,2,2,3,4,5");
context.unconditionallyProcess(megaCasterClass.getOriginalClassLevel(1), "CAST", "3,1,2,3,4,5");
context.unconditionallyProcess(megaCasterClass.getOriginalClassLevel(2), "KNOWN", "4,2,2,3,4,5,6,7,8,9,10");
context.unconditionallyProcess(megaCasterClass.getOriginalClassLevel(2), "CAST", "3,1,2,3,4,5,6,7,8,9,10");
Globals.getContext().getReferenceContext().importObject(megaCasterClass);
final PlayerCharacter character = getCharacter();
assertEquals("Highest spell level for class", 10, character.getSpellSupport(megaCasterClass).getHighestLevelSpell());
character.incrementClassLevel(1, megaCasterClass);
PCClass charClass = character.getClassKeyed(megaCasterClass.getKeyName());
assertEquals("Highest spell level for character's class", 10, character.getSpellSupport(charClass).getHighestLevelSpell());
String sbook = Globals.getDefaultSpellBook();
String cast = character.getSpellSupport(charClass).getCastForLevel(10, sbook, true, false, character) + character.getSpellSupport(charClass).getBonusCastForLevelString(10, sbook, character);
assertEquals("Should not be able to cast 10th level spells at 1st level", "0", cast);
cast = character.getSpellSupport(charClass).getCastForLevel(5, sbook, true, false, character) + character.getSpellSupport(charClass).getBonusCastForLevelString(5, sbook, character);
assertEquals("Should be able to cast 5th level spells at 1st level", "5", cast);
Ability casterFeat = new Ability();
FeatLoader featLoader = new FeatLoader();
CampaignSourceEntry source;
try {
source = new CampaignSourceEntry(new Campaign(), new URI("file:/" + getClass().getName() + ".java"));
} catch (URISyntaxException e) {
throw new UnreachableError(e);
}
featLoader.parseLine(Globals.getContext(), casterFeat, "CasterBoost TYPE:General BONUS:SPELLCAST|CLASS=MegaCaster;LEVEL=11|1", source);
casterFeat.setCDOMCategory(AbilityCategory.FEAT);
context.getReferenceContext().importObject(casterFeat);
AbstractCharacterTestCase.applyAbility(character, AbilityCategory.FEAT, casterFeat, null);
cast = character.getSpellSupport(charClass).getCastForLevel(11, sbook, true, false, character) + character.getSpellSupport(charClass).getBonusCastForLevelString(11, sbook, character);
assertEquals("Should be able to cast 11th level spells with feat", "1", cast);
assertEquals("Should be able to cast 11th level spells with feat", 11, character.getSpellSupport(charClass).getHighestLevelSpell(character));
}
Aggregations