Search in sources :

Example 61 with Skill

use of pcgen.core.Skill in project pcgen by PCGen.

the class ClassSkillsLevelToken method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, PCClassLevel obj, String value) {
    ParsingSeparator sep = new ParsingSeparator(value, '|');
    sep.addGroupingPair('[', ']');
    sep.addGroupingPair('(', ')');
    String activeValue = sep.next();
    Formula count;
    if (!sep.hasNext()) {
        count = FormulaFactory.ONE;
    } else {
        count = FormulaFactory.getFormulaFor(activeValue);
        if (!count.isValid()) {
            return new ParseResult.Fail("Count in " + getTokenName() + " was not valid: " + count.toString(), context);
        }
        if (count.isStatic() && count.resolveStatic().doubleValue() <= 0) {
            return new ParseResult.Fail("Count in " + getFullName() + " must be > 0", context);
        }
        activeValue = sep.next();
    }
    if (sep.hasNext()) {
        return new ParseResult.Fail(getFullName() + " had too many pipe separated items: " + value, context);
    }
    ParseResult pr = checkSeparatorsAndNonEmpty(',', activeValue);
    if (!pr.passed()) {
        return pr;
    }
    List<CDOMReference<Skill>> refs = new ArrayList<>();
    StringTokenizer tok = new StringTokenizer(activeValue, Constants.COMMA);
    CDOMGroupRef<Skill> allRef = context.getReferenceContext().getCDOMAllReference(SKILL_CLASS);
    Integer autoRank = null;
    while (tok.hasMoreTokens()) {
        String tokText = tok.nextToken();
        if (Constants.LST_ALL.equals(tokText) || Constants.LST_ANY.equals(tokText)) {
            refs.add(allRef);
        } else {
            if (Constants.LST_UNTRAINED.equals(tokText)) {
                ObjectMatchingReference<Skill, Boolean> omr = new ObjectMatchingReference<>(tokText, SKILL_CLASS, allRef, ObjectKey.USE_UNTRAINED, Boolean.TRUE);
                omr.returnIncludesNulls(true);
                refs.add(omr);
            } else if (Constants.LST_TRAINED.equals(tokText)) {
                refs.add(new ObjectMatchingReference<>(tokText, SKILL_CLASS, allRef, ObjectKey.USE_UNTRAINED, Boolean.FALSE));
            } else if (Constants.LST_EXCLUSIVE.equals(tokText)) {
                refs.add(new ObjectMatchingReference<>(tokText, SKILL_CLASS, allRef, ObjectKey.EXCLUSIVE, Boolean.TRUE));
            } else if (Constants.LST_NONEXCLUSIVE.equals(tokText) || Constants.LST_CROSS_CLASS.equals(tokText)) {
                ObjectMatchingReference<Skill, Boolean> omr = new ObjectMatchingReference<>(tokText, SKILL_CLASS, allRef, ObjectKey.EXCLUSIVE, Boolean.FALSE);
                omr.returnIncludesNulls(true);
                refs.add(omr);
            } else if (tokText.startsWith("AUTORANK=")) {
                if (autoRank != null) {
                    return new ParseResult.Fail("Cannot have two " + "AUTORANK= items in " + getFullName() + ": " + value, context);
                }
                String rankString = tokText.substring(9);
                try {
                    autoRank = Integer.decode(rankString);
                    if (autoRank <= 0) {
                        return new ParseResult.Fail("Expected AUTORANK= to be" + " greater than zero, found: " + autoRank, context);
                    }
                } catch (NumberFormatException e) {
                    return new ParseResult.Fail("Expected AUTORANK= to have" + " an integer value, found: " + rankString, context);
                }
            } else {
                CDOMReference<Skill> skref = TokenUtilities.getTypeOrPrimitive(context, SKILL_CLASS, tokText);
                if (skref == null) {
                    return new ParseResult.Fail("  Error was encountered while parsing " + getFullName() + ": " + value + " had an invalid reference: " + tokText, context);
                }
                refs.add(skref);
            }
        }
    }
    if (refs.isEmpty()) {
        return new ParseResult.Fail("Non-sensical " + getFullName() + ": Contains no skill reference: " + value, context);
    }
    ReferenceChoiceSet<Skill> rcs = new ReferenceChoiceSet<>(refs);
    if (!rcs.getGroupingState().isValid()) {
        return new ParseResult.Fail("Non-sensical " + getFullName() + ": Contains ANY and a specific reference: " + value, context);
    }
    ChoiceSet<Skill> cs = new ChoiceSet<>(getTokenName(), rcs, true);
    PersistentTransitionChoice<Skill> tc = new ConcretePersistentTransitionChoice<>(cs, count);
    // TODO This is a hack, to get this to work pre-CDOM
    PCClass parent = (PCClass) obj.get(ObjectKey.TOKEN_PARENT);
    ClassSkillChoiceActor actor = new ClassSkillChoiceActor(parent, autoRank);
    tc.setChoiceActor(actor);
    context.getObjectContext().addToList(obj, ListKey.ADD, tc);
    return ParseResult.SUCCESS;
}
Also used : ArrayList(java.util.ArrayList) ConcretePersistentTransitionChoice(pcgen.cdom.base.ConcretePersistentTransitionChoice) Formula(pcgen.base.formula.Formula) ObjectMatchingReference(pcgen.cdom.reference.ObjectMatchingReference) ParseResult(pcgen.rules.persistence.token.ParseResult) ReferenceChoiceSet(pcgen.cdom.choiceset.ReferenceChoiceSet) ChoiceSet(pcgen.cdom.base.ChoiceSet) PCClass(pcgen.core.PCClass) StringTokenizer(java.util.StringTokenizer) Skill(pcgen.core.Skill) ParsingSeparator(pcgen.base.text.ParsingSeparator) ClassSkillChoiceActor(pcgen.cdom.helper.ClassSkillChoiceActor) CDOMReference(pcgen.cdom.base.CDOMReference) ReferenceChoiceSet(pcgen.cdom.choiceset.ReferenceChoiceSet)

Example 62 with Skill

use of pcgen.core.Skill in project pcgen by PCGen.

the class SkillTypeToken method getToken.

/**
	 * @see pcgen.io.exporttoken.Token#getToken(java.lang.String, pcgen.core.PlayerCharacter, pcgen.io.ExportHandler)
	 */
@Override
public String getToken(String tokenSource, PlayerCharacter pc, ExportHandler eh) {
    SkillDetails details = buildSkillDetails(tokenSource);
    if (details.getPropertyCount() < 2) {
        return "";
    }
    Skill aSkill = getSkill(tokenSource, pc, details, eh);
    return getSkillProperty(aSkill, details.getProperty(1), pc);
}
Also used : Skill(pcgen.core.Skill)

Example 63 with Skill

use of pcgen.core.Skill in project pcgen by PCGen.

the class ClassSkillsToken method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, PCClass obj, String value) {
    ParsingSeparator sep = new ParsingSeparator(value, '|');
    sep.addGroupingPair('[', ']');
    sep.addGroupingPair('(', ')');
    String activeValue = sep.next();
    Formula count;
    if (!sep.hasNext()) {
        count = FormulaFactory.ONE;
    } else {
        count = FormulaFactory.getFormulaFor(activeValue);
        if (!count.isValid()) {
            return new ParseResult.Fail("Count in " + getTokenName() + " was not valid: " + count.toString(), context);
        }
        if (count.isStatic() && count.resolveStatic().doubleValue() <= 0) {
            return new ParseResult.Fail("Count in " + getFullName() + " must be > 0", context);
        }
        activeValue = sep.next();
    }
    if (sep.hasNext()) {
        return new ParseResult.Fail(getFullName() + " had too many pipe separated items: " + value, context);
    }
    ParseResult pr = checkSeparatorsAndNonEmpty(',', activeValue);
    if (!pr.passed()) {
        return pr;
    }
    List<CDOMReference<Skill>> refs = new ArrayList<>();
    StringTokenizer tok = new StringTokenizer(activeValue, Constants.COMMA);
    CDOMGroupRef<Skill> allRef = context.getReferenceContext().getCDOMAllReference(SKILL_CLASS);
    Integer autoRank = null;
    while (tok.hasMoreTokens()) {
        String tokText = tok.nextToken();
        if (Constants.LST_ALL.equals(tokText) || Constants.LST_ANY.equals(tokText)) {
            refs.add(allRef);
        } else {
            if (Constants.LST_UNTRAINED.equals(tokText)) {
                ObjectMatchingReference<Skill, Boolean> omr = new ObjectMatchingReference<>(tokText, SKILL_CLASS, allRef, ObjectKey.USE_UNTRAINED, Boolean.TRUE);
                omr.returnIncludesNulls(true);
                refs.add(omr);
            } else if (Constants.LST_TRAINED.equals(tokText)) {
                refs.add(new ObjectMatchingReference<>(tokText, SKILL_CLASS, allRef, ObjectKey.USE_UNTRAINED, Boolean.FALSE));
            } else if (Constants.LST_EXCLUSIVE.equals(tokText)) {
                refs.add(new ObjectMatchingReference<>(tokText, SKILL_CLASS, allRef, ObjectKey.EXCLUSIVE, Boolean.TRUE));
            } else if (Constants.LST_NONEXCLUSIVE.equals(tokText) || Constants.LST_CROSS_CLASS.equals(tokText)) {
                ObjectMatchingReference<Skill, Boolean> omr = new ObjectMatchingReference<>(tokText, SKILL_CLASS, allRef, ObjectKey.EXCLUSIVE, Boolean.FALSE);
                omr.returnIncludesNulls(true);
                refs.add(omr);
            } else if (tokText.startsWith("AUTORANK=")) {
                if (autoRank != null) {
                    return new ParseResult.Fail("Cannot have two " + "AUTORANK= items in " + getFullName() + ": " + value, context);
                }
                String rankString = tokText.substring(9);
                try {
                    autoRank = Integer.decode(rankString);
                    if (autoRank <= 0) {
                        return new ParseResult.Fail("Expected AUTORANK= to be" + " greater than zero, found: " + autoRank, context);
                    }
                } catch (NumberFormatException e) {
                    return new ParseResult.Fail("Expected AUTORANK= to have" + " an integer value, found: " + rankString, context);
                }
            } else {
                CDOMReference<Skill> skref = TokenUtilities.getTypeOrPrimitive(context, SKILL_CLASS, tokText);
                if (skref == null) {
                    return new ParseResult.Fail("  Error was encountered while parsing " + getFullName() + ": " + value + " had an invalid reference: " + tokText, context);
                }
                refs.add(skref);
            }
        }
    }
    if (refs.isEmpty()) {
        return new ParseResult.Fail("Non-sensical " + getFullName() + ": Contains no skill reference: " + value, context);
    }
    ReferenceChoiceSet<Skill> rcs = new ReferenceChoiceSet<>(refs);
    if (!rcs.getGroupingState().isValid()) {
        return new ParseResult.Fail("Non-sensical " + getFullName() + ": Contains ANY and a specific reference: " + value, context);
    }
    ChoiceSet<Skill> cs = new ChoiceSet<>(getTokenName(), rcs, true);
    PersistentTransitionChoice<Skill> tc = new ConcretePersistentTransitionChoice<>(cs, count);
    ClassSkillChoiceActor actor = new ClassSkillChoiceActor(obj, autoRank);
    tc.setChoiceActor(actor);
    context.getObjectContext().addToList(obj, ListKey.ADD, tc);
    return ParseResult.SUCCESS;
}
Also used : ArrayList(java.util.ArrayList) ConcretePersistentTransitionChoice(pcgen.cdom.base.ConcretePersistentTransitionChoice) Formula(pcgen.base.formula.Formula) ObjectMatchingReference(pcgen.cdom.reference.ObjectMatchingReference) ParseResult(pcgen.rules.persistence.token.ParseResult) ReferenceChoiceSet(pcgen.cdom.choiceset.ReferenceChoiceSet) ChoiceSet(pcgen.cdom.base.ChoiceSet) StringTokenizer(java.util.StringTokenizer) Skill(pcgen.core.Skill) ParsingSeparator(pcgen.base.text.ParsingSeparator) ClassSkillChoiceActor(pcgen.cdom.helper.ClassSkillChoiceActor) CDOMReference(pcgen.cdom.base.CDOMReference) ReferenceChoiceSet(pcgen.cdom.choiceset.ReferenceChoiceSet)

Example 64 with Skill

use of pcgen.core.Skill in project pcgen by PCGen.

the class PreCSkillTester method passes.

/**
	 * @see pcgen.core.prereq.PrerequisiteTest#passes(pcgen.core.PlayerCharacter)
	 */
@Override
public int passes(final Prerequisite prereq, final PlayerCharacter character, CDOMObject source) {
    final int reqnumber = Integer.parseInt(prereq.getOperand());
    int runningTotal = 0;
    HashMap<Skill, HashSet<Skill>> serveAsSkills = new HashMap<>();
    Set<Skill> imitators = new HashSet<>();
    PreCSkillTester.getImitators(serveAsSkills, imitators);
    // Compute the skill name from the Prerequisite
    String requiredSkillKey = prereq.getKey().toUpperCase();
    if (prereq.getSubKey() != null) {
        //$NON-NLS-1$ //$NON-NLS-2$
        requiredSkillKey += " (" + prereq.getSubKey().toUpperCase() + ')';
    }
    final boolean isType = //$NON-NLS-1$ //$NON-NLS-2$
    (requiredSkillKey.startsWith("TYPE.") || requiredSkillKey.startsWith("TYPE="));
    if (isType) {
        requiredSkillKey = requiredSkillKey.substring(5);
    }
    final String skillKey = requiredSkillKey;
    Set<Skill> skillMatches = new HashSet<>();
    if (isType) {
        //loop through skill list checking for type and class skill
        for (Skill skill : Globals.getContext().getReferenceContext().getConstructedCDOMObjects(Skill.class)) {
            if (skill.isType(skillKey) && character.isClassSkill(skill)) {
                skillMatches.add(skill);
                runningTotal++;
            }
        }
        if (runningTotal < reqnumber) {
            BREAKOUT: for (Skill fake : serveAsSkills.keySet()) {
                if (character.isClassSkill(fake)) {
                    for (Skill mock : serveAsSkills.get(fake)) {
                        if (skillMatches.contains(mock)) {
                            // a second time
                            break BREAKOUT;
                        }
                        if (mock.isType(skillKey)) {
                            runningTotal++;
                            break BREAKOUT;
                        }
                    }
                }
            }
        }
    } else {
        Skill skill = Globals.getContext().getReferenceContext().silentlyGetConstructedCDOMObject(Skill.class, skillKey);
        if (skill != null && character.isClassSkill(skill)) {
            runningTotal++;
        } else {
            for (Skill mock : imitators) {
                if (character.isClassSkill(mock) && serveAsSkills.get(mock).contains(skill)) {
                    runningTotal++;
                    break;
                }
            }
        }
    }
    runningTotal = prereq.getOperator().compare(runningTotal, reqnumber);
    return countedTotal(prereq, runningTotal);
}
Also used : Skill(pcgen.core.Skill) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Example 65 with Skill

use of pcgen.core.Skill in project pcgen by PCGen.

the class PreCSkillTester method getImitators.

/**
	 * @param serveAsSkills
	 * @param imitators
	 * @param character
	 */
private static void getImitators(HashMap<Skill, HashSet<Skill>> serveAsSkills, Set<Skill> imitators) {
    for (Skill aSkill : Globals.getContext().getReferenceContext().getConstructedCDOMObjects(Skill.class)) {
        Set<Skill> servesAs = new HashSet<>();
        for (CDOMReference<Skill> ref : aSkill.getSafeListFor(ListKey.SERVES_AS_SKILL)) {
            servesAs.addAll(ref.getContainedObjects());
        }
        if (!servesAs.isEmpty()) {
            imitators.add(aSkill);
            serveAsSkills.put(aSkill, (HashSet<Skill>) servesAs);
        }
    }
}
Also used : Skill(pcgen.core.Skill) HashSet(java.util.HashSet)

Aggregations

Skill (pcgen.core.Skill)165 Test (org.junit.Test)60 PCClass (pcgen.core.PCClass)55 ArrayList (java.util.ArrayList)29 CDOMReference (pcgen.cdom.base.CDOMReference)24 CharID (pcgen.cdom.enumeration.CharID)23 SkillCost (pcgen.cdom.enumeration.SkillCost)16 CDOMObject (pcgen.cdom.base.CDOMObject)15 PlayerCharacter (pcgen.core.PlayerCharacter)14 ChoiceSet (pcgen.cdom.base.ChoiceSet)13 ConcretePersistentTransitionChoice (pcgen.cdom.base.ConcretePersistentTransitionChoice)13 ReferenceChoiceSet (pcgen.cdom.choiceset.ReferenceChoiceSet)13 ClassSkillChoiceActor (pcgen.cdom.helper.ClassSkillChoiceActor)13 StringTokenizer (java.util.StringTokenizer)10 ObjectMatchingReference (pcgen.cdom.reference.ObjectMatchingReference)10 Ability (pcgen.core.Ability)10 LoadContext (pcgen.rules.context.LoadContext)9 ClassSkillList (pcgen.cdom.list.ClassSkillList)8 HashSet (java.util.HashSet)7 CNAbility (pcgen.cdom.content.CNAbility)7