Search in sources :

Example 6 with StatLock

use of pcgen.cdom.helper.StatLock in project pcgen by PCGen.

the class StatLockFacetTest method testGetLock.

@Test
public void testGetLock() {
    Object source1 = new Object();
    Object source2 = new Object();
    PCStat stat = new PCStat();
    stat.setName("Stat" + n++);
    StatLock t1 = new StatLock(CDOMDirectSingleRef.getRef(stat), FormulaFactory.getFormulaFor(1));
    PCStat stat1 = new PCStat();
    stat1.setName("Stat" + n++);
    StatLock t2 = new StatLock(CDOMDirectSingleRef.getRef(stat1), FormulaFactory.getFormulaFor(4));
    PCStat stat3 = new PCStat();
    stat3.setName("Stat" + n++);
    StatLock t3 = new StatLock(CDOMDirectSingleRef.getRef(stat3), FormulaFactory.getFormulaFor(2));
    assertNull(facet.getLockedStat(id, stat));
    getFacet().add(id, t1, source1);
    assertEquals(1, facet.getLockedStat(id, stat));
    assertNull(facet.getLockedStat(id, stat1));
    getFacet().add(id, t2, source1);
    assertEquals(4, facet.getLockedStat(id, stat1));
    getFacet().add(id, t3, source1);
    assertEquals(2, facet.getLockedStat(id, stat3));
}
Also used : CDOMObject(pcgen.cdom.base.CDOMObject) PCStat(pcgen.core.PCStat) StatLock(pcgen.cdom.helper.StatLock) AbstractExtractingFacetTest(pcgen.cdom.testsupport.AbstractExtractingFacetTest) Test(org.junit.Test)

Example 7 with StatLock

use of pcgen.cdom.helper.StatLock in project pcgen by PCGen.

the class DefineStatLst method parseToken.

@Override
public ParseResult parseToken(LoadContext context, CDOMObject obj, String value) {
    if (obj instanceof Ungranted) {
        return new ParseResult.Fail("Cannot use " + getTokenName() + " on an Ungranted object type: " + obj.getClass().getSimpleName(), context);
    }
    ParsingSeparator sep = new ParsingSeparator(value, '|');
    sep.addGroupingPair('[', ']');
    sep.addGroupingPair('(', ')');
    if (!sep.hasNext()) {
        return new ParseResult.Fail(getTokenName() + " may not be empty", context);
    }
    String firstItem = sep.next();
    DefineStatSubToken subToken;
    try {
        subToken = DefineStatSubToken.valueOf(firstItem);
    } catch (IllegalArgumentException e1) {
        return new ParseResult.Fail("Found unexpected sub tag " + firstItem + " in " + getTokenName() + Constants.COLON + value + ". Must be one of " + StringUtils.join(DefineStatSubToken.values(), ", ") + Constants.DOT, context);
    }
    if (!sep.hasNext()) {
        return new ParseResult.Fail(getTokenName() + Constants.COLON + subToken + "| must be followed by a stat.", context);
    }
    String statKey = sep.next();
    CDOMSingleRef<PCStat> stat = context.getReferenceContext().getCDOMReference(PCSTAT_CLASS, statKey);
    Formula f = null;
    if (subToken == DefineStatSubToken.LOCK || subToken == DefineStatSubToken.MINVALUE || subToken == DefineStatSubToken.MAXVALUE) {
        if (!sep.hasNext()) {
            return new ParseResult.Fail(getTokenName() + Constants.COLON + subToken + "| must be followed by both a stat and a value.", context);
        }
        String formula = sep.next();
        f = FormulaFactory.getFormulaFor(formula);
        if (!f.isValid()) {
            return new ParseResult.Fail("Formula in " + getTokenName() + " was not valid: " + f.toString(), context);
        }
    }
    if (sep.hasNext()) {
        return new ParseResult.Fail(getTokenName() + Constants.COLON + value + " has too many pipe separated item.", context);
    }
    switch(subToken) {
        case LOCK:
            context.getObjectContext().addToList(obj, ListKey.STAT_LOCKS, new StatLock(stat, f));
            break;
        case UNLOCK:
            context.getObjectContext().addToList(obj, ListKey.UNLOCKED_STATS, stat);
            break;
        case NONSTAT:
            context.getObjectContext().addToList(obj, ListKey.NONSTAT_STATS, stat);
            break;
        case STAT:
            context.getObjectContext().addToList(obj, ListKey.NONSTAT_TO_STAT_STATS, stat);
            break;
        case MINVALUE:
            context.getObjectContext().addToList(obj, ListKey.STAT_MINVALUE, new StatLock(stat, f));
            break;
        case MAXVALUE:
            context.getObjectContext().addToList(obj, ListKey.STAT_MAXVALUE, new StatLock(stat, f));
            break;
    }
    return ParseResult.SUCCESS;
}
Also used : Formula(pcgen.base.formula.Formula) ParsingSeparator(pcgen.base.text.ParsingSeparator) ParseResult(pcgen.rules.persistence.token.ParseResult) PCStat(pcgen.core.PCStat) Ungranted(pcgen.cdom.base.Ungranted) StatLock(pcgen.cdom.helper.StatLock)

Example 8 with StatLock

use of pcgen.cdom.helper.StatLock in project pcgen by PCGen.

the class StatListTest method setUp.

/**
	 * @see pcgen.AbstractCharacterTestCase#setUp()
	 */
@Override
protected void setUp() throws Exception {
    super.setUp();
    PlayerCharacter pc = getCharacter();
    LoadContext context = Globals.getContext();
    locker = new PCTemplate();
    locker.setName("locker");
    CDOMDirectSingleRef<PCStat> strRef = CDOMDirectSingleRef.getRef(str);
    locker.addToListFor(ListKey.STAT_LOCKS, new StatLock(strRef, FormulaFactory.getFormulaFor(12)));
    unlocker = new PCTemplate();
    unlocker.setName("unlocker");
    unlocker.addToListFor(ListKey.UNLOCKED_STATS, strRef);
    bonus = TestHelper.makeAbility("Bonus", AbilityCategory.FEAT, "General.Fighter");
    BonusObj aBonus = Bonus.newBonus(context, "STAT|STR|7|TYPE=Enhancement");
    if (aBonus != null) {
        bonus.addToListFor(ListKey.BONUS, aBonus);
    }
    lockedBonus = TestHelper.makeAbility("LockedBonus", AbilityCategory.FEAT, "General.Fighter");
    aBonus = Bonus.newBonus(context, "LOCKEDSTAT|STR|3|TYPE=Morale");
    if (aBonus != null) {
        lockedBonus.addToListFor(ListKey.BONUS, aBonus);
    }
    setPCStat(pc, str, 6);
}
Also used : BonusObj(pcgen.core.bonus.BonusObj) LoadContext(pcgen.rules.context.LoadContext) StatLock(pcgen.cdom.helper.StatLock)

Example 9 with StatLock

use of pcgen.cdom.helper.StatLock in project pcgen by PCGen.

the class StatIntegrationTest method causeLock.

private static void causeLock(CDOMObject r, PCStat stat, int i) {
    StatLock sl = new StatLock(CDOMDirectSingleRef.getRef(stat), FormulaFactory.getFormulaFor(i));
    r.addToListFor(ListKey.STAT_LOCKS, sl);
}
Also used : StatLock(pcgen.cdom.helper.StatLock)

Example 10 with StatLock

use of pcgen.cdom.helper.StatLock in project pcgen by PCGen.

the class StatLockFacet method getLockedStat.

/**
	 * Returns the numerical value for the given PCStat which has been locked
	 * for the Player Character identified by the given CharID. Returns null if
	 * no StatLock exists on the Player Character for the given PCStat.
	 * 
	 * @param id
	 *            The CharID identifying the Player Character for which the
	 *            locked stat value is to be returned
	 * @param stat
	 *            The PCStat for which the numerical lock value is to be
	 *            returned
	 * @return The numerical value for the given PCStat which has been locked
	 *         for the Player Character identified by the given CharID; null if
	 *         no StatLock exists on the Player Character for the given PCStat
	 */
public Number getLockedStat(CharID id, PCStat stat) {
    Number max = Double.NEGATIVE_INFINITY;
    boolean hit = false;
    Map<StatLock, Set<Object>> componentMap = getCachedMap(id);
    if (componentMap != null) {
        for (Iterator<Map.Entry<StatLock, Set<Object>>> it = componentMap.entrySet().iterator(); it.hasNext(); ) {
            Entry<StatLock, Set<Object>> me = it.next();
            Set<Object> set = me.getValue();
            StatLock lock = me.getKey();
            if (lock.getLockedStat().equals(stat)) {
                for (Object source : set) {
                    String sourceString = (source instanceof CDOMObject) ? ((CDOMObject) source).getQualifiedKey() : "";
                    Number val = formulaResolvingFacet.resolve(id, lock.getLockValue(), sourceString);
                    if (val.doubleValue() > max.doubleValue()) {
                        hit = true;
                        max = val;
                    }
                }
            }
        }
    }
    return hit ? max : null;
}
Also used : Entry(java.util.Map.Entry) Set(java.util.Set) CDOMObject(pcgen.cdom.base.CDOMObject) CDOMObject(pcgen.cdom.base.CDOMObject) StatLock(pcgen.cdom.helper.StatLock)

Aggregations

StatLock (pcgen.cdom.helper.StatLock)15 CDOMObject (pcgen.cdom.base.CDOMObject)8 PCStat (pcgen.core.PCStat)5 Set (java.util.Set)3 Entry (java.util.Map.Entry)2 PCTemplate (pcgen.core.PCTemplate)2 BonusObj (pcgen.core.bonus.BonusObj)2 LoadContext (pcgen.rules.context.LoadContext)2 TreeSet (java.util.TreeSet)1 Test (org.junit.Test)1 Formula (pcgen.base.formula.Formula)1 ParsingSeparator (pcgen.base.text.ParsingSeparator)1 Ungranted (pcgen.cdom.base.Ungranted)1 FormulaResolvingFacet (pcgen.cdom.facet.FormulaResolvingFacet)1 CDOMSingleRef (pcgen.cdom.reference.CDOMSingleRef)1 AbstractExtractingFacetTest (pcgen.cdom.testsupport.AbstractExtractingFacetTest)1 Equipment (pcgen.core.Equipment)1 Race (pcgen.core.Race)1 Spell (pcgen.core.spell.Spell)1 ParseResult (pcgen.rules.persistence.token.ParseResult)1