Search in sources :

Example 1 with MultiplyingFormula

use of pcgen.base.formula.MultiplyingFormula in project pcgen by PCGen.

the class HitdieToken method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, Race race, String value) {
    try {
        String lock = value;
        int pipeLoc = lock.indexOf(Constants.PIPE);
        if (pipeLoc != lock.lastIndexOf(Constants.PIPE)) {
            return new ParseResult.Fail(getTokenName() + " has more than one pipe, " + "is not of format: <int>[|<prereq>]", context);
        }
        // Do not initialize, null is significant
        CDOMReference<PCClass> owner = null;
        if (pipeLoc != -1) {
            // Has a limitation
            String lockPre = lock.substring(pipeLoc + 1);
            if (lockPre.startsWith("CLASS.TYPE=")) {
                String substring = lock.substring(pipeLoc + 12);
                if (substring.isEmpty()) {
                    return new ParseResult.Fail("Cannot have Empty Type Limitation in " + getTokenName() + ": " + value, context);
                }
                ParseResult pr = checkForIllegalSeparator('.', substring);
                if (!pr.passed()) {
                    return pr;
                }
                owner = context.getReferenceContext().getCDOMTypeReference(PCCLASS_CLASS, substring.split("\\."));
            } else if (lockPre.startsWith(Constants.LST_CLASS_EQUAL)) {
                String substring = lock.substring(pipeLoc + 7);
                if (substring.isEmpty()) {
                    return new ParseResult.Fail("Cannot have Empty Class Limitation in " + getTokenName() + ": " + value, context);
                }
                owner = context.getReferenceContext().getCDOMReference(PCCLASS_CLASS, substring);
            } else {
                return new ParseResult.Fail("Invalid Limitation in HITDIE: " + lockPre, context);
            }
            lock = lock.substring(0, pipeLoc);
        }
        Processor<HitDie> hdm;
        if (lock.startsWith("%/")) {
            // HITDIE:%/num --- divides the classes hit die by num.
            int denom = Integer.parseInt(lock.substring(2));
            if (denom <= 0) {
                return new ParseResult.Fail(getTokenName() + " was expecting a Positive Integer " + "for dividing Lock, was : " + lock.substring(2), context);
            }
            hdm = new HitDieFormula(new DividingFormula(denom));
        } else if (lock.startsWith("%*")) {
            // HITDIE:%*num --- multiplies the classes hit die by num.
            int mult = Integer.parseInt(lock.substring(2));
            if (mult <= 0) {
                return new ParseResult.Fail(getTokenName() + " was expecting a Positive " + "Integer for multiplying Lock, was : " + lock.substring(2), context);
            }
            hdm = new HitDieFormula(new MultiplyingFormula(mult));
        } else if (lock.startsWith("%+")) {
            // possibly redundant with BONUS:HD MAX|num
            // HITDIE:%+num --- adds num to the classes hit die.
            int add = Integer.parseInt(lock.substring(2));
            if (add <= 0) {
                return new ParseResult.Fail(getTokenName() + " was expecting a Positive " + "Integer for adding Lock, was : " + lock.substring(2), context);
            }
            hdm = new HitDieFormula(new AddingFormula(add));
        } else if (lock.startsWith("%-")) {
            // HITDIE:%-num --- subtracts num from the classes hit die.
            // possibly redundant with BONUS:HD MAX|num if that will
            // take negative numbers.
            int sub = Integer.parseInt(lock.substring(2));
            if (sub <= 0) {
                return new ParseResult.Fail(getTokenName() + " was expecting a Positive " + "Integer for subtracting Lock, was : " + lock.substring(2), context);
            }
            hdm = new HitDieFormula(new SubtractingFormula(sub));
        } else if (lock.startsWith("%up")) {
            // HITDIE:%upnum --- moves the hit die num steps up the die size
            // list d4,d6,d8,d10,d12. Stops at d12.
            int steps = Integer.parseInt(lock.substring(3));
            if (steps <= 0) {
                return new ParseResult.Fail("Invalid Step Count: " + steps + " in " + getTokenName() + " up (must be positive)", context);
            }
            if (steps >= 5) {
                return new ParseResult.Fail("Invalid Step Count: " + steps + " in " + getTokenName() + " up (too large)", context);
            }
            hdm = new HitDieStep(steps, new HitDie(12));
        } else if (lock.startsWith("%Hup")) {
            // HITDIE:%upnum --- moves the hit die num steps up the die size
            // list d4,d6,d8,d10,d12. Stops at d12.
            int steps = Integer.parseInt(lock.substring(4));
            if (steps <= 0) {
                return new ParseResult.Fail("Invalid Step Count: " + steps + " in " + getTokenName(), context);
            }
            hdm = new HitDieStep(steps, null);
        } else if (lock.startsWith("%down")) {
            // HITDIE:%downnum --- moves the hit die num steps down the die
            // size
            // list d4,d6,d8,d10,d12. Stops at d4.
            int steps = Integer.parseInt(lock.substring(5));
            if (steps <= 0) {
                return new ParseResult.Fail("Invalid Step Count: " + steps + " in " + getTokenName() + " down (must be positive)", context);
            }
            if (steps >= 5) {
                return new ParseResult.Fail("Invalid Step Count: " + steps + " in " + getTokenName() + " down (too large)", context);
            }
            hdm = new HitDieStep(-steps, new HitDie(4));
        } else if (lock.startsWith("%Hdown")) {
            // HITDIE:%downnum --- moves the hit die num steps down the die
            // size
            // list. No limit.
            int steps = Integer.parseInt(lock.substring(6));
            if (steps <= 0) {
                return new ParseResult.Fail("Invalid Step Count: " + steps + " in " + getTokenName(), context);
            }
            hdm = new HitDieStep(-steps, null);
        } else {
            int i = Integer.parseInt(lock);
            if (i <= 0) {
                return new ParseResult.Fail("Invalid HitDie: " + i + " in " + getTokenName(), context);
            }
            // HITDIE:num --- sets the hit die to num regardless of class.
            hdm = new HitDieLock(new HitDie(i));
        }
        Processor<HitDie> mod = owner == null ? hdm : new ContextProcessor<>(hdm, owner);
        context.getObjectContext().put(race, ObjectKey.HITDIE, mod);
        return ParseResult.SUCCESS;
    } catch (NumberFormatException nfe) {
        return new ParseResult.Fail("Invalid Number (must be an Integer) in " + getTokenName() + ": " + nfe.getLocalizedMessage(), context);
    }
}
Also used : HitDieFormula(pcgen.cdom.processor.HitDieFormula) ParseResult(pcgen.rules.persistence.token.ParseResult) PCClass(pcgen.core.PCClass) MultiplyingFormula(pcgen.base.formula.MultiplyingFormula) HitDieLock(pcgen.cdom.processor.HitDieLock) AddingFormula(pcgen.base.formula.AddingFormula) HitDieStep(pcgen.cdom.processor.HitDieStep) HitDie(pcgen.cdom.content.HitDie) DividingFormula(pcgen.base.formula.DividingFormula) SubtractingFormula(pcgen.base.formula.SubtractingFormula)

Example 2 with MultiplyingFormula

use of pcgen.base.formula.MultiplyingFormula in project pcgen by PCGen.

the class HitdieLst method parseToken.

@Override
public ParseResult parseToken(LoadContext context, PCClassLevel level, String value) {
    try {
        String lock = value;
        int pipeLoc = lock.indexOf(Constants.PIPE);
        if (pipeLoc != -1) {
            return new ParseResult.Fail(getTokenName() + " is invalid has a pipe: " + value, context);
        }
        Processor<HitDie> hdm;
        if (lock.startsWith("%/")) {
            // HITDIE:%/num --- divides the classes hit die by num.
            int denom = Integer.parseInt(lock.substring(2));
            if (denom <= 0) {
                return new ParseResult.Fail(getTokenName() + " was expecting a Positive Integer " + "for dividing Lock, was : " + lock.substring(2), context);
            }
            hdm = new HitDieFormula(new DividingFormula(denom));
        } else if (lock.startsWith("%*")) {
            // HITDIE:%*num --- multiplies the classes hit die by num.
            int mult = Integer.parseInt(lock.substring(2));
            if (mult <= 0) {
                return new ParseResult.Fail(getTokenName() + " was expecting a Positive " + "Integer for multiplying Lock, was : " + lock.substring(2), context);
            }
            hdm = new HitDieFormula(new MultiplyingFormula(mult));
        } else if (lock.startsWith("%+")) {
            // possibly redundant with BONUS:HD MAX|num
            // HITDIE:%+num --- adds num to the classes hit die.
            int add = Integer.parseInt(lock.substring(2));
            if (add <= 0) {
                return new ParseResult.Fail(getTokenName() + " was expecting a Positive " + "Integer for adding Lock, was : " + lock.substring(2), context);
            }
            hdm = new HitDieFormula(new AddingFormula(add));
        } else if (lock.startsWith("%-")) {
            // HITDIE:%-num --- subtracts num from the classes hit die.
            // possibly redundant with BONUS:HD MAX|num if that will
            // take negative numbers.
            int sub = Integer.parseInt(lock.substring(2));
            if (sub <= 0) {
                return new ParseResult.Fail(getTokenName() + " was expecting a Positive " + "Integer for subtracting Lock, was : " + lock.substring(2), context);
            }
            hdm = new HitDieFormula(new SubtractingFormula(sub));
        } else if (lock.startsWith("%up")) {
            // HITDIE:%upnum --- moves the hit die num steps up the die size
            // list d4,d6,d8,d10,d12. Stops at d12.
            int steps = Integer.parseInt(lock.substring(3));
            if (steps <= 0) {
                return new ParseResult.Fail("Invalid Step Count: " + steps + " in " + getTokenName() + " up (must be positive)", context);
            }
            if (steps >= 5) {
                return new ParseResult.Fail("Invalid Step Count: " + steps + " in " + getTokenName() + " up (too large)", context);
            }
            hdm = new HitDieStep(steps, new HitDie(12));
        } else if (lock.startsWith("%Hup")) {
            // HITDIE:%upnum --- moves the hit die num steps up the die size
            // list d4,d6,d8,d10,d12. Stops at d12.
            int steps = Integer.parseInt(lock.substring(4));
            if (steps <= 0) {
                return new ParseResult.Fail("Invalid Step Count: " + steps + " in " + getTokenName(), context);
            }
            hdm = new HitDieStep(steps, null);
        } else if (lock.startsWith("%down")) {
            // HITDIE:%downnum --- moves the hit die num steps down the die
            // size
            // list d4,d6,d8,d10,d12. Stops at d4.
            int steps = Integer.parseInt(lock.substring(5));
            if (steps <= 0) {
                return new ParseResult.Fail("Invalid Step Count: " + steps + " in " + getTokenName() + " down (must be positive)", context);
            }
            if (steps >= 5) {
                return new ParseResult.Fail("Invalid Step Count: " + steps + " in " + getTokenName() + " down (too large)", context);
            }
            hdm = new HitDieStep(-steps, new HitDie(4));
        } else if (lock.startsWith("%Hdown")) {
            // HITDIE:%downnum --- moves the hit die num steps down the die
            // size
            // list. No limit.
            int steps = Integer.parseInt(lock.substring(6));
            if (steps <= 0) {
                return new ParseResult.Fail("Invalid Step Count: " + steps + " in " + getTokenName(), context);
            }
            hdm = new HitDieStep(-steps, null);
        } else {
            int i = Integer.parseInt(lock);
            if (i <= 0) {
                return new ParseResult.Fail("Invalid HitDie: " + i + " in " + getTokenName(), context);
            }
            // HITDIE:num --- sets the hit die to num regardless of class.
            hdm = new HitDieLock(new HitDie(i));
        }
        context.getObjectContext().put(level, ObjectKey.HITDIE, hdm);
        return ParseResult.SUCCESS;
    } catch (NumberFormatException nfe) {
        ComplexParseResult pr = new ComplexParseResult();
        pr.addErrorMessage("Invalid Number in " + getTokenName() + ": " + nfe.getLocalizedMessage());
        pr.addErrorMessage("  Must be an Integer");
        return pr;
    }
}
Also used : HitDieFormula(pcgen.cdom.processor.HitDieFormula) ComplexParseResult(pcgen.rules.persistence.token.ComplexParseResult) ParseResult(pcgen.rules.persistence.token.ParseResult) ComplexParseResult(pcgen.rules.persistence.token.ComplexParseResult) MultiplyingFormula(pcgen.base.formula.MultiplyingFormula) HitDieLock(pcgen.cdom.processor.HitDieLock) AddingFormula(pcgen.base.formula.AddingFormula) HitDieStep(pcgen.cdom.processor.HitDieStep) HitDie(pcgen.cdom.content.HitDie) DividingFormula(pcgen.base.formula.DividingFormula) SubtractingFormula(pcgen.base.formula.SubtractingFormula)

Example 3 with MultiplyingFormula

use of pcgen.base.formula.MultiplyingFormula in project pcgen by PCGen.

the class HitdieToken method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, PCTemplate template, String value) {
    try {
        String lock = value;
        int pipeLoc = lock.indexOf(Constants.PIPE);
        if (pipeLoc != lock.lastIndexOf(Constants.PIPE)) {
            return new ParseResult.Fail(getTokenName() + " has more than one pipe, " + "is not of format: <int>[|<prereq>]", context);
        }
        // Do not initialize, null is significant
        CDOMReference<PCClass> owner = null;
        if (pipeLoc != -1) {
            // Has a limitation
            String lockPre = lock.substring(pipeLoc + 1);
            if (lockPre.startsWith("CLASS.TYPE=")) {
                String substring = lock.substring(pipeLoc + 12);
                if (substring.isEmpty()) {
                    return new ParseResult.Fail("Cannot have Empty Type Limitation in " + getTokenName() + ": " + value, context);
                }
                ParseResult pr = checkForIllegalSeparator('.', substring);
                if (!pr.passed()) {
                    return pr;
                }
                owner = context.getReferenceContext().getCDOMTypeReference(PCCLASS_CLASS, substring.split("\\."));
            } else if (lockPre.startsWith(Constants.LST_CLASS_EQUAL)) {
                String substring = lock.substring(pipeLoc + 7);
                if (substring.isEmpty()) {
                    return new ParseResult.Fail("Cannot have Empty Class Limitation in " + getTokenName() + ": " + value, context);
                }
                owner = context.getReferenceContext().getCDOMReference(PCCLASS_CLASS, substring);
            } else {
                return new ParseResult.Fail("Invalid Limitation in HITDIE: " + lockPre, context);
            }
            lock = lock.substring(0, pipeLoc);
        }
        Processor<HitDie> hdm;
        if (lock.startsWith("%/")) {
            // HITDIE:%/num --- divides the classes hit die by num.
            int denom = Integer.parseInt(lock.substring(2));
            if (denom <= 0) {
                return new ParseResult.Fail(getTokenName() + " was expecting a Positive Integer " + "for dividing Lock, was : " + lock.substring(2), context);
            }
            hdm = new HitDieFormula(new DividingFormula(denom));
        } else if (lock.startsWith("%*")) {
            // HITDIE:%*num --- multiplies the classes hit die by num.
            int mult = Integer.parseInt(lock.substring(2));
            if (mult <= 0) {
                return new ParseResult.Fail(getTokenName() + " was expecting a Positive " + "Integer for multiplying Lock, was : " + lock.substring(2), context);
            }
            hdm = new HitDieFormula(new MultiplyingFormula(mult));
        } else if (lock.startsWith("%+")) {
            // possibly redundant with BONUS:HD MAX|num
            // HITDIE:%+num --- adds num to the classes hit die.
            int add = Integer.parseInt(lock.substring(2));
            if (add <= 0) {
                return new ParseResult.Fail(getTokenName() + " was expecting a Positive " + "Integer for adding Lock, was : " + lock.substring(2), context);
            }
            hdm = new HitDieFormula(new AddingFormula(add));
        } else if (lock.startsWith("%-")) {
            // HITDIE:%-num --- subtracts num from the classes hit die.
            // possibly redundant with BONUS:HD MAX|num if that will
            // take negative numbers.
            int sub = Integer.parseInt(lock.substring(2));
            if (sub <= 0) {
                return new ParseResult.Fail(getTokenName() + " was expecting a Positive " + "Integer for subtracting Lock, was : " + lock.substring(2), context);
            }
            hdm = new HitDieFormula(new SubtractingFormula(sub));
        } else if (lock.startsWith("%up")) {
            // HITDIE:%upnum --- moves the hit die num steps up the die size
            // list d4,d6,d8,d10,d12. Stops at d12.
            int steps = Integer.parseInt(lock.substring(3));
            if (steps <= 0) {
                return new ParseResult.Fail("Invalid Step Count: " + steps + " in " + getTokenName() + " up (must be positive)", context);
            }
            if (steps >= 5) {
                return new ParseResult.Fail("Invalid Step Count: " + steps + " in " + getTokenName() + " up (too large)", context);
            }
            hdm = new HitDieStep(steps, new HitDie(12));
        } else if (lock.startsWith("%Hup")) {
            // HITDIE:%upnum --- moves the hit die num steps up the die size
            // No limit.
            int steps = Integer.parseInt(lock.substring(4));
            if (steps <= 0) {
                return new ParseResult.Fail("Invalid Step Count: " + steps + " in " + getTokenName(), context);
            }
            hdm = new HitDieStep(steps, null);
        } else if (lock.startsWith("%down")) {
            // HITDIE:%downnum --- moves the hit die num steps down the die
            // size
            // list d4,d6,d8,d10,d12. Stops at d4.
            int steps = Integer.parseInt(lock.substring(5));
            if (steps <= 0) {
                return new ParseResult.Fail("Invalid Step Count: " + steps + " in " + getTokenName() + " down (must be positive)", context);
            }
            if (steps >= 5) {
                return new ParseResult.Fail("Invalid Step Count: " + steps + " in " + getTokenName() + " down (too large)", context);
            }
            hdm = new HitDieStep(-steps, new HitDie(4));
        } else if (lock.startsWith("%Hdown")) {
            // HITDIE:%downnum --- moves the hit die num steps down the die
            // size
            // list. No limit.
            int steps = Integer.parseInt(lock.substring(6));
            if (steps <= 0) {
                return new ParseResult.Fail("Invalid Step Count: " + steps + " in " + getTokenName(), context);
            }
            hdm = new HitDieStep(-steps, null);
        } else {
            int i = Integer.parseInt(lock);
            if (i <= 0) {
                return new ParseResult.Fail("Invalid HitDie: " + i + " in " + getTokenName(), context);
            }
            // HITDIE:num --- sets the hit die to num regardless of class.
            hdm = new HitDieLock(new HitDie(i));
        }
        Processor<HitDie> mod = owner == null ? hdm : new ContextProcessor<>(hdm, owner);
        context.getObjectContext().put(template, ObjectKey.HITDIE, mod);
        return ParseResult.SUCCESS;
    } catch (NumberFormatException nfe) {
        ComplexParseResult cpr = new ComplexParseResult();
        cpr.addErrorMessage("Invalid Number in " + getTokenName() + ": " + nfe.getLocalizedMessage());
        cpr.addErrorMessage("  Must be an Integer");
        return cpr;
    }
}
Also used : HitDieFormula(pcgen.cdom.processor.HitDieFormula) ComplexParseResult(pcgen.rules.persistence.token.ComplexParseResult) ParseResult(pcgen.rules.persistence.token.ParseResult) ComplexParseResult(pcgen.rules.persistence.token.ComplexParseResult) PCClass(pcgen.core.PCClass) MultiplyingFormula(pcgen.base.formula.MultiplyingFormula) HitDieLock(pcgen.cdom.processor.HitDieLock) AddingFormula(pcgen.base.formula.AddingFormula) HitDieStep(pcgen.cdom.processor.HitDieStep) HitDie(pcgen.cdom.content.HitDie) DividingFormula(pcgen.base.formula.DividingFormula) SubtractingFormula(pcgen.base.formula.SubtractingFormula)

Aggregations

AddingFormula (pcgen.base.formula.AddingFormula)3 DividingFormula (pcgen.base.formula.DividingFormula)3 MultiplyingFormula (pcgen.base.formula.MultiplyingFormula)3 SubtractingFormula (pcgen.base.formula.SubtractingFormula)3 HitDie (pcgen.cdom.content.HitDie)3 HitDieFormula (pcgen.cdom.processor.HitDieFormula)3 HitDieLock (pcgen.cdom.processor.HitDieLock)3 HitDieStep (pcgen.cdom.processor.HitDieStep)3 ParseResult (pcgen.rules.persistence.token.ParseResult)3 PCClass (pcgen.core.PCClass)2 ComplexParseResult (pcgen.rules.persistence.token.ComplexParseResult)2