use of pcgen.base.formula.Formula in project pcgen by PCGen.
the class NaturalattacksLst method process.
@Override
public boolean process(LoadContext context, CDOMObject obj) {
List<Equipment> natWeapons = obj.getListFor(ListKey.NATURAL_WEAPON);
if (natWeapons != null) {
Formula sizeFormula = obj.getSafe(FormulaKey.SIZE);
// If the size was just a default, check for a size prereq and use that instead.
if (obj.get(FormulaKey.SIZE) == null && obj.hasPreReqTypeOf("SIZE")) {
Integer requiredSize = getRequiredSize(obj);
if (requiredSize != null) {
sizeFormula = FormulaFactory.getFormulaFor(requiredSize);
}
}
if (sizeFormula.isStatic()) {
int isize = sizeFormula.resolveStatic().intValue();
SizeAdjustment size = context.getReferenceContext().getSortedList(SizeAdjustment.class, IntegerKey.SIZEORDER).get(isize);
for (Equipment e : natWeapons) {
CDOMDirectSingleRef<SizeAdjustment> sizeRef = CDOMDirectSingleRef.getRef(size);
e.put(ObjectKey.BASESIZE, sizeRef);
e.put(ObjectKey.SIZE, sizeRef);
}
} else {
Logging.errorPrint("SIZE in " + obj.getClass().getSimpleName() + ' ' + obj.getKeyName() + " must not be a variable " + "if it contains a NATURALATTACKS token");
}
}
return true;
}
use of pcgen.base.formula.Formula in project pcgen by PCGen.
the class SelectLst method parseToken.
@Override
public ParseResult parseToken(LoadContext context, CDOMObject cdo, String value) {
if (cdo instanceof Ungranted) {
return new ParseResult.Fail("Cannot use " + getTokenName() + " on an Ungranted object type: " + cdo.getClass().getSimpleName(), context);
}
Formula formula = FormulaFactory.getFormulaFor(value);
if (!formula.isValid()) {
return new ParseResult.Fail("Formula in " + getTokenName() + " was not valid: " + formula.toString(), context);
}
context.getObjectContext().put(cdo, FormulaKey.SELECT, formula);
return ParseResult.SUCCESS;
}
use of pcgen.base.formula.Formula in project pcgen by PCGen.
the class AddLevelToken method parseNonEmptyToken.
@Override
protected ParseResult parseNonEmptyToken(LoadContext context, PCTemplate template, String value) {
ParsingSeparator sep = new ParsingSeparator(value, '|');
sep.addGroupingPair('[', ']');
sep.addGroupingPair('(', ')');
String classString = sep.next();
if (classString.isEmpty()) {
ComplexParseResult cpr = new ComplexParseResult();
cpr.addErrorMessage("Empty Class found in " + getTokenName());
cpr.addErrorMessage(" " + getTokenName() + " requires at format: Class|LevelCount");
return cpr;
}
if (!sep.hasNext()) {
ComplexParseResult cpr = new ComplexParseResult();
cpr.addErrorMessage("No | found in " + getTokenName());
cpr.addErrorMessage(" " + getTokenName() + " requires at format: Class|LevelCount");
return cpr;
}
String numLevels = sep.next();
if (numLevels.isEmpty()) {
ComplexParseResult cpr = new ComplexParseResult();
cpr.addErrorMessage("Empty Level Count found in " + getTokenName());
cpr.addErrorMessage(" " + getTokenName() + " requires at format: Class|LevelCount");
return cpr;
}
if (sep.hasNext()) {
ComplexParseResult cpr = new ComplexParseResult();
cpr.addErrorMessage("Two | found in " + getTokenName());
cpr.addErrorMessage(" " + getTokenName() + " requires at format: Class|LevelCount");
return cpr;
}
CDOMSingleRef<PCClass> cl = context.getReferenceContext().getCDOMReference(PCClass.class, classString);
Formula f;
try {
int lvls = Integer.parseInt(numLevels);
if (lvls <= 0) {
return new ParseResult.Fail("Number of Levels granted in " + getTokenName() + " must be greater than zero", context);
}
f = FormulaFactory.getFormulaFor(lvls);
} catch (NumberFormatException nfe) {
f = FormulaFactory.getFormulaFor(numLevels);
}
if (!f.isValid()) {
return new ParseResult.Fail("Formula in " + getTokenName() + " was not valid: " + f.toString(), context);
}
LevelCommandFactory cf = new LevelCommandFactory(cl, f);
context.getObjectContext().addToList(template, ListKey.ADD_LEVEL, cf);
return ParseResult.SUCCESS;
}
use of pcgen.base.formula.Formula in project pcgen by PCGen.
the class StatmodToken method parseToken.
@Override
public ParseResult parseToken(LoadContext context, PCStat stat, String value) {
if (value == null || value.isEmpty()) {
return new ParseResult.Fail(getTokenName() + " arguments may not be empty", context);
}
Formula formula = FormulaFactory.getFormulaFor(value);
if (!formula.isValid()) {
return new ParseResult.Fail("Formula in " + getTokenName() + " was not valid: " + formula.toString(), context);
}
context.getObjectContext().put(stat, FormulaKey.STAT_MOD, formula);
return ParseResult.SUCCESS;
}
use of pcgen.base.formula.Formula in project pcgen by PCGen.
the class TemplateToken method unparse.
@Override
public String[] unparse(LoadContext context, CDOMObject obj) {
Changes<PersistentTransitionChoice<?>> grantChanges = context.getObjectContext().getListChanges(obj, ListKey.ADD);
Collection<PersistentTransitionChoice<?>> addedItems = grantChanges.getAdded();
if (addedItems == null || addedItems.isEmpty()) {
// Zero indicates no Token
return null;
}
List<String> addStrings = new ArrayList<>();
for (TransitionChoice<?> container : addedItems) {
SelectableSet<?> cs = container.getChoices();
if (PCTEMPLATE_CLASS.equals(cs.getChoiceClass())) {
Formula f = container.getCount();
if (f == null) {
context.addWriteMessage("Unable to find " + getFullName() + " Count");
return null;
}
if (f.isStatic() && f.resolveStatic().doubleValue() <= 0) {
context.addWriteMessage("Count in " + getFullName() + " must be > 0");
return null;
}
StringBuilder sb = new StringBuilder();
if (!FormulaFactory.ONE.equals(f)) {
sb.append(f).append(Constants.PIPE);
}
sb.append(cs.getLSTformat());
addStrings.add(sb.toString());
// assoc.getAssociation(AssociationKey.CHOICE_MAXCOUNT);
}
}
return addStrings.toArray(new String[addStrings.size()]);
}
Aggregations