Search in sources :

Example 1 with ChallengeRating

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;
}
Also used : Formula(pcgen.base.formula.Formula) ChallengeRating(pcgen.cdom.content.ChallengeRating) ParseResult(pcgen.rules.persistence.token.ParseResult)

Example 2 with ChallengeRating

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;
}
Also used : Formula(pcgen.base.formula.Formula) ChallengeRating(pcgen.cdom.content.ChallengeRating)

Example 3 with ChallengeRating

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());
    }
}
Also used : Formula(pcgen.base.formula.Formula) ChallengeRating(pcgen.cdom.content.ChallengeRating) Random(java.util.Random) Race(pcgen.core.Race) ArrayList(java.util.ArrayList) File(java.io.File) VectorTable(gmgen.io.VectorTable) ReadXML(gmgen.io.ReadXML)

Example 4 with ChallengeRating

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");
}
Also used : ChallengeRating(pcgen.cdom.content.ChallengeRating) Test(org.junit.Test)

Example 5 with ChallengeRating

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());
}
Also used : ChallengeRating(pcgen.cdom.content.ChallengeRating) GenericLoader(pcgen.persistence.lst.GenericLoader) URISyntaxException(java.net.URISyntaxException) UnreachableError(pcgen.base.lang.UnreachableError) URI(java.net.URI) StringManager(pcgen.base.format.StringManager) CampaignSourceEntry(pcgen.persistence.lst.CampaignSourceEntry) OrderedPairManager(pcgen.base.format.OrderedPairManager) LoadContext(pcgen.rules.context.LoadContext) PCClassLoader(pcgen.persistence.lst.PCClassLoader)

Aggregations

ChallengeRating (pcgen.cdom.content.ChallengeRating)7 Formula (pcgen.base.formula.Formula)3 Test (org.junit.Test)2 Race (pcgen.core.Race)2 ReadXML (gmgen.io.ReadXML)1 VectorTable (gmgen.io.VectorTable)1 File (java.io.File)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 OrderedPairManager (pcgen.base.format.OrderedPairManager)1 StringManager (pcgen.base.format.StringManager)1 UnreachableError (pcgen.base.lang.UnreachableError)1 CampaignSourceEntry (pcgen.persistence.lst.CampaignSourceEntry)1 GenericLoader (pcgen.persistence.lst.GenericLoader)1 PCClassLoader (pcgen.persistence.lst.PCClassLoader)1 LoadContext (pcgen.rules.context.LoadContext)1 ParseResult (pcgen.rules.persistence.token.ParseResult)1