use of pcgen.base.formula.Formula in project pcgen by PCGen.
the class Equipment method getSelectCorrectedAssociationCount.
public int getSelectCorrectedAssociationCount(CDOMObject obj) {
Formula f = obj.getSafe(FormulaKey.SELECT);
//TODO Null here is probably a problem for the PC :/
int select = f.resolve(this, true, null, "").intValue();
return assocSupt.getAssocCount(obj, AssociationListKey.CHOICES) / select;
}
use of pcgen.base.formula.Formula in project pcgen by PCGen.
the class BonusManager method getStringListFromBonus.
public List<BonusPair> getStringListFromBonus(BonusObj bo) {
Object creatorObj = getSourceObject(bo);
List<String> associatedList;
CDOMObject anObj = null;
if (creatorObj instanceof CDOMObject) {
anObj = (CDOMObject) creatorObj;
associatedList = pc.getConsolidatedAssociationList(anObj);
if (associatedList == null || associatedList.isEmpty()) {
associatedList = NO_ASSOC_LIST;
}
} else {
associatedList = NO_ASSOC_LIST;
}
List<BonusPair> bonusList = new ArrayList<>();
// Must use getBonusName because it contains the unaltered bonusType
String bonusName = bo.getBonusName();
String[] bonusInfoArray = bo.getBonusInfo().split(",");
String bonusType = bo.getTypeString();
for (String assoc : associatedList) {
String replacedName;
if (bonusName.contains(VALUE_TOKEN_REPLACEMENT)) {
replacedName = bonusName.replaceAll(VALUE_TOKEN_PATTERN, assoc);
} else {
replacedName = bonusName;
}
List<String> replacedInfoList = new ArrayList<>(4);
for (String bonusInfo : bonusInfoArray) {
if (bonusInfo.contains(VALUE_TOKEN_REPLACEMENT)) {
replacedInfoList.add(bonusInfo.replaceAll(VALUE_TOKEN_PATTERN, assoc));
} else if (bonusInfo.contains(VAR_TOKEN_REPLACEMENT)) {
replacedInfoList.add(bonusName.replaceAll(VAR_TOKEN_PATTERN, assoc));
} else if (bonusInfo.equals(LIST_TOKEN_REPLACEMENT)) {
replacedInfoList.add(assoc);
} else {
replacedInfoList.add(bonusInfo);
}
}
Formula newFormula;
if (bo.isValueStatic()) {
newFormula = bo.getFormula();
} else {
String value = bo.getValue();
// A %LIST substitution also needs to be done in the val
// section
int listIndex = value.indexOf(VALUE_TOKEN_REPLACEMENT);
String thisValue = value;
if (listIndex >= 0) {
thisValue = value.replaceAll(VALUE_TOKEN_PATTERN, assoc);
}
//Need to protect against a selection not being made with a %LIST
if (thisValue.isEmpty()) {
thisValue = "0";
}
newFormula = FormulaFactory.getFormulaFor(thisValue);
}
for (String replacedInfo : replacedInfoList) {
StringBuilder sb = new StringBuilder(100);
sb.append(replacedName).append('.').append(replacedInfo);
if (bo.hasTypeString()) {
sb.append(':').append(bonusType);
}
bonusList.add(new BonusPair(sb.toString(), newFormula, creatorObj));
}
}
return bonusList;
}
use of pcgen.base.formula.Formula in project pcgen by PCGen.
the class AbstractRestrictedSpellPrimitive method allow.
public boolean allow(PlayerCharacter pc, int level, String source, Spell spell, CDOMList<Spell> optionalList) {
if (restriction != null) {
Formula maxLevel = restriction.maxLevel;
if (maxLevel != null && (level > maxLevel.resolve(pc, source).intValue())) {
return false;
}
Formula minLevel = restriction.minLevel;
if (minLevel != null && (level < minLevel.resolve(pc, source).intValue())) {
return false;
}
if (restriction.knownRequired != null) {
String defaultbook = Globals.getDefaultSpellBook();
boolean known = restriction.knownRequired.booleanValue();
boolean found = false;
for (PCClass cl : pc.getClassSet()) {
if (optionalList != null) {
/*
* This may not be a precise test of intent, but given
* the weirdness we have on lists and the use of
* SPELLLIST tag in data to share lists between classes,
* this is probably the closest we can get
*/
if (!pc.hasSpellList(cl, optionalList)) {
continue;
}
}
List<CharacterSpell> csl = pc.getCharacterSpells(cl, spell, defaultbook, -1);
if (csl != null && !csl.isEmpty()) {
/*
* Going to assume here that the level doesn't need to
* be rechecked... ?? - thpr Feb 26, 08
*/
found = true;
}
}
if (found != known) {
return false;
}
}
}
return true;
}
use of pcgen.base.formula.Formula in project pcgen by PCGen.
the class AbstractRestrictedSpellPrimitive method getRestriction.
private Restriction getRestriction(String restrString) {
StringTokenizer restr = new StringTokenizer(restrString, ";");
Formula levelMax = null;
Formula levelMin = null;
Boolean known = null;
while (restr.hasMoreTokens()) {
String tok = restr.nextToken();
if (tok.startsWith("LEVELMAX=")) {
levelMax = FormulaFactory.getFormulaFor(tok.substring(9));
if (!levelMax.isValid()) {
Logging.errorPrint("LEVELMAX Formula in " + getTokenName() + " was not valid: " + levelMax.toString());
return null;
}
} else if (tok.startsWith("LEVELMIN=")) {
levelMin = FormulaFactory.getFormulaFor(tok.substring(9));
if (!levelMin.isValid()) {
Logging.errorPrint("LEVELMIN Formula in " + getTokenName() + " was not valid: " + levelMin.toString());
return null;
}
} else if ("KNOWN=YES".equals(tok)) {
known = Boolean.TRUE;
} else if ("KNOWN=NO".equals(tok)) {
known = Boolean.FALSE;
} else {
Logging.errorPrint("Unknown restriction: " + tok + " in CHOOSE:SPELLS");
return null;
}
}
return new Restriction(levelMin, levelMax, known);
}
use of pcgen.base.formula.Formula in project pcgen by PCGen.
the class CrformulaToken method parseNonEmptyToken.
@Override
protected ParseResult parseNonEmptyToken(LoadContext context, PCClass pcc, String value) {
Formula formula = FormulaFactory.getFormulaFor(value);
if (!formula.isValid()) {
return new ParseResult.Fail("Formula in " + getTokenName() + " was not valid: " + formula.toString(), context);
}
context.getObjectContext().put(pcc, FormulaKey.CR, formula);
return ParseResult.SUCCESS;
}
Aggregations