Search in sources :

Example 56 with Ability

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

the class AbilitySelection method getAbilitySelectionFromPersistentFormat.

/**
	 * Decodes the given String into an AbilitySelection. The String format to
	 * be passed into this method is defined solely by the return result of the
	 * getPersistentFormat method. There is no guarantee that the encoding is
	 * human readable, simply that the encoding is uniquely identifying such
	 * that this method is capable of decoding the String into an
	 * AbilitySelection.
	 * 
	 * @param persistentFormat
	 *            The String which should be decoded to provide an
	 *            AbilitySelection.
	 * 
	 * @return An AbilitySelection that was encoded in the given String.
	 */
public static AbilitySelection getAbilitySelectionFromPersistentFormat(LoadContext context, String persistentFormat) {
    if (!persistentFormat.contains(Constants.PIPE)) {
        return decodeFeatSelectionChoice(context, persistentFormat);
    }
    StringTokenizer st = new StringTokenizer(persistentFormat, Constants.PIPE);
    String catString = st.nextToken();
    if (!catString.startsWith("CATEGORY=")) {
        throw new IllegalArgumentException("String in getAbilitySelectionFromPersistentFormat " + "must start with CATEGORY=, found: " + persistentFormat);
    }
    String cat = catString.substring(9);
    AbilityCategory ac = SettingsHandler.getGame().getAbilityCategory(cat);
    if (ac == null) {
        throw new IllegalArgumentException("Category in getAbilitySelectionFromPersistentFormat " + "must exist found: " + cat);
    }
    String ab = st.nextToken();
    Ability a = context.getReferenceContext().silentlyGetConstructedCDOMObject(Ability.class, ac, ab);
    if (a == null) {
        throw new IllegalArgumentException("Second argument in String in getAbilitySelectionFromPersistentFormat " + "must be an Ability, but it was not found: " + persistentFormat);
    }
    String sel = null;
    if (st.hasMoreTokens()) {
        /*
			 * No need to check for MULT:YES/NO here, as that is checked
			 * implicitly in the construction of AbilitySelection below
			 */
        sel = st.nextToken();
    } else if (persistentFormat.endsWith(Constants.PIPE)) {
        // Handle the StringTokenizer ignoring blank tokens at the end
        sel = "";
    }
    if (st.hasMoreTokens()) {
        throw new IllegalArgumentException("String in getAbilitySelectionFromPersistentFormat " + "must have 2 or 3 arguments, but found more: " + persistentFormat);
    }
    return new AbilitySelection(a, sel);
}
Also used : Ability(pcgen.core.Ability) StringTokenizer(java.util.StringTokenizer) AbilityCategory(pcgen.core.AbilityCategory)

Example 57 with Ability

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

the class KitSpells method testApply.

@Override
public boolean testApply(Kit aKit, PlayerCharacter aPC, List<String> warnings) {
    theSpells = null;
    PCClass aClass = findDefaultSpellClass(castingClass, aPC);
    if (aClass == null) {
        warnings.add("SPELLS: Character does not have " + castingClass + " spellcasting class.");
        return false;
    }
    String workingBook = spellBook == null ? Globals.getDefaultSpellBook() : spellBook;
    List<KitSpellBookEntry> aSpellList = new ArrayList<>();
    if (!aClass.getSafe(ObjectKey.MEMORIZE_SPELLS) && !workingBook.equals(Globals.getDefaultSpellBook())) {
        warnings.add("SPELLS: " + aClass.getDisplayName() + " can only add to " + Globals.getDefaultSpellBook());
        return false;
    }
    for (KnownSpellIdentifier ksi : spells.getKeySet()) {
        Collection<Spell> allSpells = ksi.getContainedSpells(aPC, Collections.singletonList(aClass.get(ObjectKey.CLASS_SPELLLIST)));
        Set<List<CDOMSingleRef<Ability>>> feats = spells.getSecondaryKeySet(ksi);
        for (Spell sp : allSpells) {
            for (List<CDOMSingleRef<Ability>> list : feats) {
                Integer count = spells.get(ksi, list);
                aSpellList.add(new KitSpellBookEntry(spellBook, sp, list, count));
            }
        }
    }
    final Formula choiceFormula = getCount();
    int numberOfChoices;
    if (choiceFormula == null) {
        numberOfChoices = aSpellList.size();
    } else {
        numberOfChoices = choiceFormula.resolve(aPC, "").intValue();
    }
    //
    if (numberOfChoices > aSpellList.size()) {
        numberOfChoices = aSpellList.size();
    }
    if (numberOfChoices == 0) {
        return false;
    }
    List<KitSpellBookEntry> xs;
    if (numberOfChoices == aSpellList.size()) {
        xs = aSpellList;
    } else {
        //
        while (true) {
            xs = Globals.getChoiceFromList("Choose " + aClass.getKeyName() + " spell(s) for " + workingBook, aSpellList, new ArrayList<>(), numberOfChoices, aPC);
            if (!xs.isEmpty()) {
                break;
            }
        }
    }
    //
    for (KitSpellBookEntry obj : xs) {
        if (obj != null) {
            obj.setPCClass(aClass);
            if (theSpells == null) {
                theSpells = new ArrayList<>();
            }
            theSpells.add(obj);
        } else {
            warnings.add("SPELLS: Non-existant spell chosen");
        }
    }
    if (theSpells != null && !theSpells.isEmpty()) {
        return true;
    }
    return false;
}
Also used : Ability(pcgen.core.Ability) KnownSpellIdentifier(pcgen.cdom.content.KnownSpellIdentifier) ArrayList(java.util.ArrayList) PCClass(pcgen.core.PCClass) CDOMSingleRef(pcgen.cdom.reference.CDOMSingleRef) Spell(pcgen.core.spell.Spell) CharacterSpell(pcgen.core.character.CharacterSpell) Formula(pcgen.base.formula.Formula) CDOMList(pcgen.cdom.base.CDOMList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 58 with Ability

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

the class KitSpells method updatePCSpells.

/**
	 * Add spells from this Kit to the PC
	 *
	 * @param  pc       The PC to add the spells to
	 * @param  aSpell   A Spell to add to the PC
	 * @param  pcClass  The class instance the spells are to be added to.
	 */
private void updatePCSpells(final PlayerCharacter pc, final KitSpellBookEntry aSpell, final PCClass pcClass) {
    Spell spell = aSpell.getSpell();
    int spLevel = 99;
    // Check to see if we have any domains that have this spell.
    PObject owner = null;
    if (pc.hasDomains()) {
        for (Domain domain : pc.getDomainSet()) {
            List<? extends CDOMList<Spell>> lists = pc.getSpellLists(domain);
            int newLevel = SpellLevel.getFirstLevelForKey(spell, lists, pc);
            if (newLevel > 0 && newLevel < spLevel) {
                spLevel = newLevel;
                owner = domain;
            }
        }
    }
    if (spLevel == 99) {
        spLevel = SpellLevel.getFirstLevelForKey(spell, pc.getSpellLists(pcClass), pc);
        owner = pcClass;
    }
    if (spLevel < 0) {
        Logging.errorPrint("SPELLS: " + pcClass.getDisplayName() + " cannot cast spell \"" + spell.getKeyName() + "\"");
        return;
    }
    final CharacterSpell cs = new CharacterSpell(owner, spell);
    final List<CDOMSingleRef<Ability>> modifierList = aSpell.getModifiers();
    int adjustedLevel = spLevel;
    List<Ability> metamagicFeatList = new ArrayList<>();
    for (CDOMSingleRef<Ability> feat : modifierList) {
        Ability anAbility = feat.get();
        adjustedLevel += anAbility.getSafe(IntegerKey.ADD_SPELL_LEVEL);
        metamagicFeatList.add(anAbility);
    }
    if (metamagicFeatList.size() <= 0) {
        metamagicFeatList = null;
    }
    if (!pc.hasSpellBook(aSpell.getBookName())) {
        pc.addSpellBook(aSpell.getBookName());
    }
    for (int numTimes = 0; numTimes < aSpell.getCopies(); numTimes++) {
        final String aString = pc.addSpell(cs, metamagicFeatList, pcClass.getKeyName(), aSpell.getBookName(), adjustedLevel, spLevel);
        if (!aString.isEmpty()) {
            Logging.errorPrint("Add spell failed:" + aString);
            return;
        }
    }
}
Also used : Ability(pcgen.core.Ability) ArrayList(java.util.ArrayList) CDOMSingleRef(pcgen.cdom.reference.CDOMSingleRef) Spell(pcgen.core.spell.Spell) CharacterSpell(pcgen.core.character.CharacterSpell) PObject(pcgen.core.PObject) CharacterSpell(pcgen.core.character.CharacterSpell) Domain(pcgen.core.Domain)

Example 59 with Ability

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

the class KitSpells method toString.

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    if (castingClass != null) {
        sb.append(castingClass.getLSTformat(false));
    }
    sb.append(' ').append(spellBook).append(": ");
    boolean needComma = false;
    for (KnownSpellIdentifier ksi : spells.getKeySet()) {
        if (needComma) {
            sb.append(',');
        }
        needComma = true;
        sb.append(ksi.getLSTformat());
        Set<List<CDOMSingleRef<Ability>>> abilities = spells.getSecondaryKeySet(ksi);
        for (List<CDOMSingleRef<Ability>> list : abilities) {
            if (list != null) {
                sb.append(" [");
                sb.append(ReferenceUtilities.joinLstFormat(list, ","));
                sb.append(']');
            }
            Integer count = spells.get(ksi, list);
            if (count > 1) {
                sb.append(" (").append(count).append(")");
            }
        }
    }
    return sb.toString();
}
Also used : Ability(pcgen.core.Ability) KnownSpellIdentifier(pcgen.cdom.content.KnownSpellIdentifier) CDOMList(pcgen.cdom.base.CDOMList) ArrayList(java.util.ArrayList) List(java.util.List) CDOMSingleRef(pcgen.cdom.reference.CDOMSingleRef)

Example 60 with Ability

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

the class ClassDataHandler method startElement.

/**
	 * @throws SAXException
	 * @throws IllegalArgumentException if the file being processed is not the
	 * same GameMode as requested.
	 *  
	 * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
	 */
@Override
public void startElement(final String uri, final String localName, final String aName, final Attributes anAttrs) throws SAXException {
    if (//$NON-NLS-1$
    theState == ParserState.INIT && "class_data".equals(aName)) {
        if (anAttrs != null) {
            //$NON-NLS-1$
            final String gm = anAttrs.getValue("game_mode");
            if (!SystemCollections.getGameModeNamed(gm).equals(theGameMode)) {
                //$NON-NLS-1$
                throw new IllegalArgumentException("Incorrect game mode");
            }
            theValidFlag = true;
        }
        return;
    }
    if (!theValidFlag) {
        //$NON-NLS-1$
        throw new SAXException("NPCGen.Options.InvalidFileFormat");
    }
    if (theState == ParserState.INIT) {
        if (//$NON-NLS-1$
        "class".equals(aName)) {
            if (anAttrs != null) {
                //$NON-NLS-1$
                final String classKey = anAttrs.getValue("key");
                final PCClass pcClass = Globals.getContext().getReferenceContext().silentlyGetConstructedCDOMObject(PCClass.class, classKey);
                if (pcClass == null) {
                    //$NON-NLS-1$
                    Logging.errorPrintLocalised("Exceptions.PCGenParser.ClassNotFound", classKey);
                } else {
                    theCurrentData = new ClassData(pcClass);
                    theState = ParserState.CLASSDATA;
                }
            }
        }
    } else if (theState == ParserState.CLASSDATA) {
        if (//$NON-NLS-1$
        "stats".equals(aName)) {
            theState = ParserState.STATDATA;
        } else if (//$NON-NLS-1$
        "skills".equals(aName)) {
            theState = ParserState.SKILLDATA;
        } else if (//$NON-NLS-1$
        "abilities".equals(aName)) {
            theState = ParserState.ABILITYDATA;
            theCurrentCategory = AbilityCategory.FEAT;
            if (anAttrs != null) {
                //$NON-NLS-1$
                final String catName = anAttrs.getValue("category");
                if (catName != null) {
                    theCurrentCategory = SettingsHandler.getGame().getAbilityCategory(catName);
                }
            }
        } else if (//$NON-NLS-1$
        "spells".equals(aName)) {
            theState = ParserState.SPELLDATA;
            theCurrentSpellType = SpellType.KNOWN;
            if (anAttrs != null) {
                //$NON-NLS-1$
                final String bookName = anAttrs.getValue("type");
                if (bookName != null) {
                    if (//$NON-NLS-1$
                    "Prepared Spells".equals(bookName)) {
                        theCurrentSpellType = SpellType.PREPARED;
                    }
                }
            }
        } else if (//$NON-NLS-1$
        "subclasses".equals(aName)) {
            theState = ParserState.SUBCLASSDATA;
        }
    } else if (theState == ParserState.STATDATA) {
        if (//$NON-NLS-1$
        "stat".equals(aName)) {
            if (anAttrs != null) {
                final int weight = getWeight(anAttrs);
                //$NON-NLS-1$
                final String statAbbr = anAttrs.getValue("value");
                if (statAbbr != null) {
                    PCStat stat = Globals.getContext().getReferenceContext().silentlyGetConstructedCDOMObject(PCStat.class, statAbbr);
                    theCurrentData.addStat(stat, weight);
                }
            }
        }
    } else if (theState == ParserState.SKILLDATA) {
        if (//$NON-NLS-1$
        "skill".equals(aName)) {
            if (anAttrs != null) {
                final int weight = getWeight(anAttrs);
                //$NON-NLS-1$
                final String key = anAttrs.getValue("value");
                if (key != null) {
                    if (//$NON-NLS-1$
                    "*".equals(key)) {
                        remainingWeight = weight;
                    } else if (//$NON-NLS-1$
                    key.startsWith("TYPE")) {
                        final List<Skill> skillsOfType = Globals.getPObjectsOfType(Globals.getContext().getReferenceContext().getConstructedCDOMObjects(Skill.class), key.substring(5));
                        if (skillsOfType.isEmpty()) {
                            //$NON-NLS-1$ //$NON-NLS-2$
                            Logging.debugPrint("NPCGenerator: No skills of type found (" + key + ")");
                        }
                    } else {
                        final Skill skill = Globals.getContext().getReferenceContext().silentlyGetConstructedCDOMObject(Skill.class, key);
                        if (skill == null) {
                            //$NON-NLS-1$ //$NON-NLS-2$
                            Logging.debugPrint("NPCGenerator: Skill not found (" + key + ")");
                        }
                    }
                    if (//$NON-NLS-1$
                    weight > 0 && !key.equals("*")) {
                        theCurrentData.addSkill(key, weight);
                    } else {
                        removeList.add(key);
                    }
                }
            }
        }
    } else if (theState == ParserState.ABILITYDATA) {
        if (//$NON-NLS-1$
        "ability".equals(aName)) {
            if (anAttrs != null) {
                final int weight = getWeight(anAttrs);
                //$NON-NLS-1$
                final String key = anAttrs.getValue("value");
                if (key != null) {
                    if (//$NON-NLS-1$
                    "*".equals(key)) {
                        remainingWeight = weight;
                    } else if (//$NON-NLS-1$
                    key.startsWith("TYPE")) {
                        Type type = Type.getConstant(key.substring(5));
                        for (final Ability ability : Globals.getContext().getReferenceContext().getManufacturer(Ability.class, theCurrentCategory).getAllObjects()) {
                            if (!ability.containsInList(ListKey.TYPE, type)) {
                                continue;
                            }
                            if (ability.getSafe(ObjectKey.VISIBILITY) == Visibility.DEFAULT) {
                                if (weight > 0) {
                                    theCurrentData.addAbility(theCurrentCategory, ability, weight);
                                } else {
                                    // We have to remove any feats of this
                                    // type.
                                    // TODO - This is a little goofy.  We
                                    // already have the feat but we will 
                                    // store the key and reretrieve it.
                                    removeList.add(ability.getKeyName());
                                }
                            }
                        }
                    } else {
                        final Ability ability = Globals.getContext().getReferenceContext().silentlyGetConstructedCDOMObject(Ability.class, theCurrentCategory, key);
                        if (ability == null) {
                            //$NON-NLS-1$ //$NON-NLS-2$
                            Logging.debugPrint("Ability (" + key + ") not found");
                        } else if (weight > 0) {
                            theCurrentData.addAbility(theCurrentCategory, ability, weight);
                        } else {
                            // We have to remove any feats of this
                            // type.
                            // TODO - This is a little goofy.  We
                            // already have the feat but we will 
                            // store the key and reretrieve it.
                            removeList.add(ability.getKeyName());
                        }
                    }
                }
            }
        }
    } else if (theState == ParserState.SPELLDATA) {
        if (//$NON-NLS-1$
        "level".equals(aName) && anAttrs != null) {
            //$NON-NLS-1$
            final String lvlStr = anAttrs.getValue("id");
            if (lvlStr != null) {
                theCurrentLevel = Integer.parseInt(lvlStr);
                theState = ParserState.SPELLLEVELDATA;
            }
        }
    } else if (theState == ParserState.SPELLLEVELDATA) {
        if (//$NON-NLS-1$
        "spell".equals(aName) && anAttrs != null) {
            final int weight = getWeight(anAttrs);
            //$NON-NLS-1$
            final String key = anAttrs.getValue("name");
            if (key != null) {
                if (//$NON-NLS-1$
                "*".equals(key)) {
                    remainingWeight = weight;
                } else if (//$NON-NLS-1$
                key.startsWith("SCHOOL")) {
                // Not sure how to do this yet
                } else {
                    final Spell spell = Globals.getContext().getReferenceContext().silentlyGetConstructedCDOMObject(Spell.class, key);
                    if (spell != null) {
                        if (theCurrentSpellType == SpellType.KNOWN) {
                            theCurrentData.addKnownSpell(theCurrentLevel, spell, weight);
                        } else if (theCurrentSpellType == SpellType.PREPARED) {
                            theCurrentData.addPreparedSpell(theCurrentLevel, spell, weight);
                        }
                    } else {
                        Logging.errorPrint("Spell \"" + key + "\" not found.");
                    }
                }
            }
        }
    } else if (theState == ParserState.SUBCLASSDATA) {
        if (//$NON-NLS-1$
        "subclass".equals(aName) && anAttrs != null) {
            final int weight = getWeight(anAttrs);
            //$NON-NLS-1$
            final String key = anAttrs.getValue("value");
            theCurrentData.addSubClass(key, weight);
        }
    }
}
Also used : Ability(pcgen.core.Ability) Skill(pcgen.core.Skill) Type(pcgen.cdom.enumeration.Type) PCClass(pcgen.core.PCClass) PCStat(pcgen.core.PCStat) Spell(pcgen.core.spell.Spell) SAXException(org.xml.sax.SAXException)

Aggregations

Ability (pcgen.core.Ability)279 CNAbility (pcgen.cdom.content.CNAbility)128 AbilityCategory (pcgen.core.AbilityCategory)60 PlayerCharacter (pcgen.core.PlayerCharacter)54 Test (org.junit.Test)46 ArrayList (java.util.ArrayList)43 CNAbilitySelection (pcgen.cdom.helper.CNAbilitySelection)25 ParseResult (pcgen.rules.persistence.token.ParseResult)21 HashMapToList (pcgen.base.util.HashMapToList)15 PCClass (pcgen.core.PCClass)15 Spell (pcgen.core.spell.Spell)15 StringTokenizer (java.util.StringTokenizer)14 TestContext (plugin.lsttokens.editcontext.testsupport.TestContext)13 LoadContext (pcgen.rules.context.LoadContext)12 SpecialAbility (pcgen.core.SpecialAbility)11 CharacterSpell (pcgen.core.character.CharacterSpell)11 PreParserFactory (pcgen.persistence.lst.prereq.PreParserFactory)11 List (java.util.List)10 CDOMObject (pcgen.cdom.base.CDOMObject)10 CDOMSingleRef (pcgen.cdom.reference.CDOMSingleRef)10