Search in sources :

Example 91 with PreParserFactory

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));
}
Also used : LevelCommandFactory(pcgen.cdom.content.LevelCommandFactory) PlayerCharacter(pcgen.core.PlayerCharacter) PreParserFactory(pcgen.persistence.lst.prereq.PreParserFactory) PCClass(pcgen.core.PCClass) SizeAdjustment(pcgen.core.SizeAdjustment) FixedSizeFormula(pcgen.cdom.formula.FixedSizeFormula)

Example 92 with PreParserFactory

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));
}
Also used : PersistenceLayerException(pcgen.persistence.PersistenceLayerException) StringTokenizer(java.util.StringTokenizer) PreParserFactory(pcgen.persistence.lst.prereq.PreParserFactory) Equipment(pcgen.core.Equipment) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 93 with PreParserFactory

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));
}
Also used : PlayerCharacter(pcgen.core.PlayerCharacter) PreParserFactory(pcgen.persistence.lst.prereq.PreParserFactory) LoadContext(pcgen.rules.context.LoadContext) PCTemplate(pcgen.core.PCTemplate)

Example 94 with PreParserFactory

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));
}
Also used : PlayerCharacter(pcgen.core.PlayerCharacter) PreParserFactory(pcgen.persistence.lst.prereq.PreParserFactory) LoadContext(pcgen.rules.context.LoadContext) PCTemplate(pcgen.core.PCTemplate)

Example 95 with PreParserFactory

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);
}
Also used : PlayerCharacter(pcgen.core.PlayerCharacter) PreParserFactory(pcgen.persistence.lst.prereq.PreParserFactory)

Aggregations

PreParserFactory (pcgen.persistence.lst.prereq.PreParserFactory)107 PlayerCharacter (pcgen.core.PlayerCharacter)89 LoadContext (pcgen.rules.context.LoadContext)16 BonusObj (pcgen.core.bonus.BonusObj)13 Prerequisite (pcgen.core.prereq.Prerequisite)12 Ability (pcgen.core.Ability)11 PCClass (pcgen.core.PCClass)11 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 StringTokenizer (java.util.StringTokenizer)5 Domain (pcgen.core.Domain)5 PCTemplate (pcgen.core.PCTemplate)5 PersistenceLayerException (pcgen.persistence.PersistenceLayerException)5 URI (java.net.URI)4 Equipment (pcgen.core.Equipment)4 PersistenceManager (pcgen.persistence.PersistenceManager)4 DamageReduction (pcgen.cdom.content.DamageReduction)3 ClassSource (pcgen.cdom.helper.ClassSource)3 CDOMSingleRef (pcgen.cdom.reference.CDOMSingleRef)3 QualifiedObject (pcgen.core.QualifiedObject)3