Search in sources :

Example 21 with Prerequisite

use of pcgen.core.prereq.Prerequisite in project pcgen by PCGen.

the class TotalCostToken method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, Kit kit, String value) {
    ParsingSeparator sep = new ParsingSeparator(value, '|');
    sep.addGroupingPair('[', ']');
    sep.addGroupingPair('(', ')');
    String activeValue = sep.next();
    if (looksLikeAPrerequisite(activeValue)) {
        return new ParseResult.Fail("Cannot have only PRExxx subtoken in " + getTokenName(), context);
    }
    Formula f = FormulaFactory.getFormulaFor(activeValue);
    if (!f.isValid()) {
        return new ParseResult.Fail("Formula in " + getTokenName() + " was not valid: " + f.toString(), context);
    }
    List<Prerequisite> prereqs = new ArrayList<>();
    while (sep.hasNext()) {
        activeValue = sep.next();
        Prerequisite prereq = getPrerequisite(activeValue);
        if (prereq == null) {
            return new ParseResult.Fail("   (Did you put total costs after the " + "PRExxx tags in " + getTokenName() + ":?)", context);
        }
        prereqs.add(prereq);
    }
    kit.put(ObjectKey.KIT_TOTAL_COST, new QualifiedObject<>(f, prereqs));
    return ParseResult.SUCCESS;
}
Also used : Formula(pcgen.base.formula.Formula) ParsingSeparator(pcgen.base.text.ParsingSeparator) ArrayList(java.util.ArrayList) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 22 with Prerequisite

use of pcgen.core.prereq.Prerequisite in project pcgen by PCGen.

the class TotalCostToken method unparse.

@Override
public String[] unparse(LoadContext context, Kit kit) {
    QualifiedObject<Formula> qo = kit.get(ObjectKey.KIT_TOTAL_COST);
    if (qo == null) {
        return null;
    }
    Formula f = qo.getRawObject();
    List<Prerequisite> prereqs = qo.getPrerequisiteList();
    String ab = f.toString();
    if (prereqs != null && !prereqs.isEmpty()) {
        ab = ab + Constants.PIPE + getPrerequisiteString(context, prereqs);
    }
    return new String[] { ab };
}
Also used : Formula(pcgen.base.formula.Formula) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 23 with Prerequisite

use of pcgen.core.prereq.Prerequisite in project pcgen by PCGen.

the class PreCSkillWriter method specialCase.

@Override
public boolean specialCase(Writer writer, Prerequisite prereq) throws IOException {
    PrerequisiteOperator po = getConsolidateMethod(kindHandled(), prereq, false);
    if (po == null) {
        return false;
    }
    if (po != prereq.getOperator()) {
        writer.write('!');
    }
    writer.write("PRE" + kindHandled().toUpperCase() + ':' + (prereq.isOverrideQualify() ? "Q:" : ""));
    writer.write(po == PrerequisiteOperator.GTEQ ? prereq.getOperand() : "1");
    for (Prerequisite p : prereq.getPrerequisites()) {
        writer.write(',');
        writer.write(p.getKey());
    }
    return true;
}
Also used : PrerequisiteOperator(pcgen.core.prereq.PrerequisiteOperator) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 24 with Prerequisite

use of pcgen.core.prereq.Prerequisite in project pcgen by PCGen.

the class PreCampaignWriter method specialCase.

/**
	 * @see pcgen.persistence.lst.output.prereq.AbstractPrerequisiteWriter#specialCase(java.io.Writer writer, pcgen.core.prereq.Prerequisite prereq)
	 */
@Override
public boolean specialCase(Writer writer, Prerequisite prereq) throws IOException {
    PrerequisiteOperator po = getConsolidateMethod(kindHandled(), prereq, false);
    if (po == null) {
        return false;
    }
    if (!po.equals(prereq.getOperator())) {
        writer.write('!');
    }
    writer.write("PRE" + kindHandled().toUpperCase() + ':' + (prereq.isOverrideQualify() ? "Q:" : ""));
    writer.write(po.equals(PrerequisiteOperator.GTEQ) ? prereq.getOperand() : "1");
    for (Prerequisite p : prereq.getPrerequisites()) {
        writer.write(',');
        writer.write(p.getKey());
    }
    return true;
}
Also used : PrerequisiteOperator(pcgen.core.prereq.PrerequisiteOperator) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 25 with Prerequisite

use of pcgen.core.prereq.Prerequisite in project pcgen by PCGen.

the class PreRuleTester method passes.

/**
	 * @see pcgen.core.prereq.PrerequisiteTest#passes(pcgen.core.prereq.Prerequisite, pcgen.core.Equipment)
	 */
@Override
public int passes(final Prerequisite prereq, final Equipment equipment, PlayerCharacter aPC) throws PrerequisiteException {
    int runningTotal = 0;
    int targetNumber;
    try {
        targetNumber = Integer.parseInt(prereq.getOperand());
    } catch (NumberFormatException ne) {
        // Not an error, just a string
        targetNumber = 1;
    }
    if (Globals.checkRule(prereq.getKey())) {
        runningTotal = 1;
    }
    for (Prerequisite element : prereq.getPrerequisites()) {
        final PrerequisiteTestFactory factory = PrerequisiteTestFactory.getInstance();
        final PrerequisiteTest test = factory.getTest(element.getKind());
        if (test != null) {
            runningTotal += test.passes(element, equipment, aPC);
        }
    }
    return countedTotal(prereq, prereq.getOperator().compare(runningTotal, targetNumber));
}
Also used : PrerequisiteTestFactory(pcgen.core.prereq.PrerequisiteTestFactory) AbstractPrerequisiteTest(pcgen.core.prereq.AbstractPrerequisiteTest) PrerequisiteTest(pcgen.core.prereq.PrerequisiteTest) Prerequisite(pcgen.core.prereq.Prerequisite)

Aggregations

Prerequisite (pcgen.core.prereq.Prerequisite)267 PersistenceLayerException (pcgen.persistence.PersistenceLayerException)62 Test (org.junit.Test)61 PrerequisiteOperator (pcgen.core.prereq.PrerequisiteOperator)50 StringTokenizer (java.util.StringTokenizer)36 ArrayList (java.util.ArrayList)35 StringWriter (java.io.StringWriter)19 AssociatedPrereqObject (pcgen.cdom.base.AssociatedPrereqObject)19 CDOMReference (pcgen.cdom.base.CDOMReference)18 ParseResult (pcgen.rules.persistence.token.ParseResult)18 BonusObj (pcgen.core.bonus.BonusObj)13 TreeSet (java.util.TreeSet)12 PreParserFactory (pcgen.persistence.lst.prereq.PreParserFactory)12 CDOMSingleRef (pcgen.cdom.reference.CDOMSingleRef)10 Domain (pcgen.core.Domain)10 QualifiedObject (pcgen.core.QualifiedObject)10 PrerequisiteWriterInterface (pcgen.persistence.lst.output.prereq.PrerequisiteWriterInterface)10 PrerequisiteWriter (pcgen.persistence.lst.output.prereq.PrerequisiteWriter)9 Ungranted (pcgen.cdom.base.Ungranted)8 PrerequisiteException (pcgen.core.prereq.PrerequisiteException)8