use of pcgen.cdom.content.ChallengeRating in project pcgen by PCGen.
the class CrToken method parseNonEmptyToken.
@Override
public ParseResult parseNonEmptyToken(LoadContext context, Race race, String value) {
try {
int intRating = Integer.parseInt(value.startsWith("1/") ? value.substring(2) : value);
if (intRating < 0) {
return new ParseResult.Fail(getTokenName() + " Challenge Rating cannot be negative", context);
}
} catch (NumberFormatException e) {
return new ParseResult.Fail(getTokenName() + "Challenge Rating must be a positive integer i or 1/i", context);
}
Formula formula = FormulaFactory.getFormulaFor(value);
if (!formula.isValid()) {
return new ParseResult.Fail("Formula in " + getTokenName() + " was not valid: " + formula.toString(), context);
}
ChallengeRating cr = new ChallengeRating(formula);
context.getObjectContext().put(race, ObjectKey.CHALLENGE_RATING, cr);
return ParseResult.SUCCESS;
}
use of pcgen.cdom.content.ChallengeRating in project pcgen by PCGen.
the class CrToken method process.
@Override
public boolean process(LoadContext context, Race race) {
Formula levelAdjFormula = race.getSafe(FormulaKey.LEVEL_ADJUSTMENT);
if (levelAdjFormula.isStatic()) {
Number la = levelAdjFormula.resolveStatic();
ChallengeRating cr = race.get(ObjectKey.CHALLENGE_RATING);
if ((la.floatValue() != 0) && cr == null) {
race.put(ObjectKey.CHALLENGE_RATING, new ChallengeRating(FormulaFactory.getFormulaFor(la.toString())));
}
}
//TODO Should there at LEAST be a message in an else??
return true;
}
use of pcgen.cdom.content.ChallengeRating in project pcgen by PCGen.
the class EncounterPlugin method generateXofYEL.
/**
* Generates creatures for an encounter based on a specified Encounter
* Level and number of creatures.
* @param size the number of creatures needed for encounter.
* @param totalEL total experience level.
*/
private void generateXofYEL(String size, String totalEL) {
File f = new File(getDataDirectory() + File.separator + DIR_ENCOUNTER + File.separator + "4_1.xml");
ReadXML xml;
VectorTable table41;
Random roll = new Random(System.currentTimeMillis());
List<Race> critters = new ArrayList<>();
if (!f.exists()) {
//$NON-NLS-1$
Logging.errorPrintLocalised("in_plugin_encounter_error_missing", f);
return;
}
xml = new ReadXML(f);
if ((table41 = xml.getTable()) == null) {
Logging.errorPrint("ACK! error getting table41! " + f.toString());
return;
}
xml = null;
f = null;
// verrify values on the table.
String crs = (String) table41.crossReference(totalEL, size);
table41 = null;
if (crs == null) {
Logging.errorPrint("Tables do not match the given parameters (" + totalEL + ", " + size + ')');
return;
}
Formula crFormula = FormulaFactory.getFormulaFor(crs);
if (!crFormula.isValid()) {
Logging.errorPrint("CR Formula " + crs + " was not valid: " + crFormula.toString());
}
ChallengeRating cr = new ChallengeRating(crFormula);
// populate critters with a list of matching monsters with the right CR.
for (final Race race : Globals.getContext().getReferenceContext().getConstructedCDOMObjects(Race.class)) {
if (cr.equals(race.get(ObjectKey.CHALLENGE_RATING))) {
critters.add(race);
}
}
int i = roll.nextInt(critters.size());
for (int x = 0; x < Integer.parseInt(size); x++) {
theModel.addElement(critters.get(i).toString());
}
}
use of pcgen.cdom.content.ChallengeRating in project pcgen by PCGen.
the class CRTokenTest method testUnparseOne.
@Test
public void testUnparseOne() throws PersistenceLayerException {
ChallengeRating cr = new ChallengeRating(FormulaFactory.ONE);
primaryProf.put(ObjectKey.CHALLENGE_RATING, cr);
expectSingle(getToken().unparse(primaryContext, primaryProf), "1");
}
use of pcgen.cdom.content.ChallengeRating in project pcgen by PCGen.
the class PObjectTest method testGetPCCText.
/**
* Test the processing of getPCCText to ensure that it correctly produces
* an LST representation of an object and that the LST can then be reloaded
* to recrete the object.
*
* @throws PersistenceLayerException
*/
public void testGetPCCText() throws PersistenceLayerException {
OrderedPairManager opManager = new OrderedPairManager();
LoadContext context = Globals.getContext();
context.getVariableContext().assertLegalVariableID(context.getActiveScope().getLegalScope(), opManager, "Face");
Race race = new Race();
race.setName("TestRace");
race.put(ObjectKey.CHALLENGE_RATING, new ChallengeRating(FormulaFactory.getFormulaFor(5)));
String racePCCText = race.getPCCText();
assertNotNull("PCC Text for race should not be null", racePCCText);
GenericLoader<Race> raceLoader = new GenericLoader<>(Race.class);
CampaignSourceEntry source;
try {
source = new CampaignSourceEntry(new Campaign(), new URI("file:/" + getClass().getName() + ".java"));
} catch (URISyntaxException e) {
throw new UnreachableError(e);
}
raceLoader.parseLine(context, null, racePCCText, source);
Race reconstRace = context.getReferenceContext().silentlyGetConstructedCDOMObject(Race.class, "TestRace");
assertEquals("getPCCText should be the same after being encoded and reloaded", racePCCText, reconstRace.getPCCText());
assertEquals("Racial CR was not restored after saving and reloading.", race.get(ObjectKey.CHALLENGE_RATING), reconstRace.get(ObjectKey.CHALLENGE_RATING));
FactKey.getConstant("Abb", new StringManager());
PCClass aClass = new PCClass();
aClass.setName("TestClass");
String classPCCText = aClass.getPCCText();
assertNotNull("PCC Text for race should not be null", racePCCText);
PCClassLoader classLoader = new PCClassLoader();
PCClass reconstClass = classLoader.parseLine(context, null, classPCCText, source);
assertEquals("getPCCText should be the same after being encoded and reloaded", classPCCText, reconstClass.getPCCText());
assertEquals("Class abbrev was not restored after saving and reloading.", aClass.getAbbrev(), reconstClass.getAbbrev());
}
Aggregations