use of pcgen.base.formula.Formula in project pcgen by PCGen.
the class Vision method getVision.
public static Vision getVision(String visionType) {
// expecting value in form of Darkvision (60') or Darkvision
int commaLoc = visionType.indexOf(',');
if (commaLoc != -1) {
throw new IllegalArgumentException("Invalid Vision: " + visionType + ". May not contain a comma");
}
int quoteLoc = visionType.indexOf('\'');
int openParenLoc = visionType.indexOf('(');
Formula distance;
String type;
if (openParenLoc == -1) {
if (visionType.indexOf(')') != -1) {
throw new IllegalArgumentException("Invalid Vision: " + visionType + ". Had close paren without open paren");
}
if (quoteLoc != -1) {
throw new IllegalArgumentException("Invalid Vision: " + visionType + ". Had quote parens");
}
type = visionType;
distance = FormulaFactory.ZERO;
} else {
int length = visionType.length();
if (visionType.indexOf(')') != length - 1) {
throw new IllegalArgumentException("Invalid Vision: " + visionType + ". Close paren not at end of string");
}
int endDistance = length - 1;
if (quoteLoc != -1) {
if (quoteLoc == length - 2) {
endDistance--;
} else {
throw new IllegalArgumentException("Invalid Vision: " + visionType + ". Foot character ' not immediately before close paren");
}
}
type = visionType.substring(0, openParenLoc).trim();
String dist = visionType.substring(openParenLoc + 1, endDistance);
if (dist.isEmpty()) {
throw new IllegalArgumentException("Invalid Vision: " + visionType + ". No Distance provided");
}
if (quoteLoc != -1) {
try {
Integer.parseInt(dist);
} catch (NumberFormatException nfe) {
throw new IllegalArgumentException("Invalid Vision: " + visionType + ". Vision Distance with Foot character ' was not an integer");
}
}
distance = FormulaFactory.getFormulaFor(dist);
if (!distance.isValid()) {
throw new IllegalArgumentException("Invalid: Vision Distance was not valid: " + distance.toString());
}
}
if (type.isEmpty()) {
throw new IllegalArgumentException("Invalid Vision: " + visionType + ". No Vision Type provided");
}
return new Vision(VisionType.getVisionType(type), distance);
}
use of pcgen.base.formula.Formula in project pcgen by PCGen.
the class LookupToken method parseToken.
@Override
public ParseResult parseToken(LoadContext context, KitGear kitGear, String value) {
ParsingSeparator sep = new ParsingSeparator(value, ',');
sep.addGroupingPair('[', ']');
sep.addGroupingPair('(', ')');
String first = sep.next();
if (!sep.hasNext()) {
return new ParseResult.Fail("Token must contain separator ','", context);
}
String second = sep.next();
if (sep.hasNext()) {
return new ParseResult.Fail("Token cannot have more than one separator ','", context);
}
Formula formula = FormulaFactory.getFormulaFor(second);
if (!formula.isValid()) {
return new ParseResult.Fail("Formula in " + getTokenName() + " was not valid: " + formula.toString(), context);
}
kitGear.loadLookup(first, formula);
return ParseResult.SUCCESS;
}
use of pcgen.base.formula.Formula in project pcgen by PCGen.
the class OptionToken method parseNonEmptyToken.
@Override
protected ParseResult parseNonEmptyToken(LoadContext context, BaseKit kit, String value) {
ParsingSeparator pipeSep = new ParsingSeparator(value, '|');
pipeSep.addGroupingPair('[', ']');
pipeSep.addGroupingPair('(', ')');
while (pipeSep.hasNext()) {
String subTok = pipeSep.next();
if (subTok.isEmpty()) {
return new ParseResult.Fail(getTokenName() + " arguments has invalid pipe separator: " + value, context);
}
ParseResult pr = checkForIllegalSeparator(',', subTok);
if (!pr.passed()) {
return pr;
}
ParsingSeparator commaSep = new ParsingSeparator(subTok, ',');
commaSep.addGroupingPair('[', ']');
commaSep.addGroupingPair('(', ')');
String minString = commaSep.next();
String maxString;
if (commaSep.hasNext()) {
maxString = commaSep.next();
} else {
maxString = subTok;
}
if (commaSep.hasNext()) {
return new ParseResult.Fail("Token cannot have more than one separator ','", context);
}
Formula min = FormulaFactory.getFormulaFor(minString);
if (!min.isValid()) {
return new ParseResult.Fail("Min Formula in " + getTokenName() + " was not valid: " + min.toString(), context);
}
Formula max = FormulaFactory.getFormulaFor(maxString);
if (!max.isValid()) {
return new ParseResult.Fail("Max Formula in " + getTokenName() + " was not valid: " + max.toString(), context);
}
kit.setOptionBounds(min, max);
}
return ParseResult.SUCCESS;
}
use of pcgen.base.formula.Formula in project pcgen by PCGen.
the class PoolToken method parseNonEmptyToken.
@Override
protected ParseResult parseNonEmptyToken(LoadContext context, AbilityCategory ac, String value) {
Formula formula = FormulaFactory.getFormulaFor(value);
if (!formula.isValid()) {
return new ParseResult.Fail("Formula in " + getTokenName() + " was not valid: " + formula.toString(), context);
}
ac.setPoolFormula(formula);
return ParseResult.SUCCESS;
}
use of pcgen.base.formula.Formula in project pcgen by PCGen.
the class SizeToken method process.
@Override
public boolean process(LoadContext context, PCTemplate template) {
String value = template.get(StringKey.SIZEFORMULA);
if (value == null) {
return true;
}
SizeAdjustment size = context.getReferenceContext().silentlyGetConstructedCDOMObject(SizeAdjustment.class, value);
Formula sizeFormula;
if (size == null) {
sizeFormula = FormulaFactory.getFormulaFor(value);
} else {
sizeFormula = new FixedSizeFormula(CDOMDirectSingleRef.getRef(size));
}
if (!sizeFormula.isValid()) {
Logging.errorPrint("Size in " + getTokenName() + " was not valid: " + sizeFormula.toString(), context);
return false;
}
context.getObjectContext().put(template, FormulaKey.SIZE, sizeFormula);
return false;
}
Aggregations