Search in sources :

Example 21 with Spell

use of pcgen.core.spell.Spell in project pcgen by PCGen.

the class ClassDataHandler method getSpellsIn.

/**
	 * Returns a List of Spell with following criteria:
	 *
	 * @param level      (optional, ignored if < 0),
	 * @param spellLists the lists of spells
	 * @param pc TODO
	 * @return a List of Spell
	 */
public static List<Spell> getSpellsIn(final int level, List<? extends CDOMList<Spell>> spellLists) {
    MasterListInterface masterLists = SettingsHandler.getGame().getMasterLists();
    ArrayList<CDOMReference<CDOMList<Spell>>> useLists = new ArrayList<>();
    for (CDOMReference ref : masterLists.getActiveLists()) {
        for (CDOMList<Spell> list : spellLists) {
            if (ref.contains(list)) {
                useLists.add(ref);
                break;
            }
        }
    }
    boolean allLevels = level == -1;
    Set<Spell> spellList = new HashSet<>();
    for (CDOMReference<CDOMList<Spell>> ref : useLists) {
        for (Spell spell : masterLists.getObjects(ref)) {
            Collection<AssociatedPrereqObject> assoc = masterLists.getAssociations(ref, spell);
            for (AssociatedPrereqObject apo : assoc) {
                // TODO Not sure if effect of null for PC
                if (PrereqHandler.passesAll(apo.getPrerequisiteList(), (PlayerCharacter) null, null)) {
                    int lvl = apo.getAssociation(AssociationKey.SPELL_LEVEL);
                    if (allLevels || level == lvl) {
                        spellList.add(spell);
                        break;
                    }
                }
            }
        }
    }
    return new ArrayList<>(spellList);
}
Also used : ArrayList(java.util.ArrayList) Spell(pcgen.core.spell.Spell) MasterListInterface(pcgen.cdom.base.MasterListInterface) CDOMList(pcgen.cdom.base.CDOMList) CDOMReference(pcgen.cdom.base.CDOMReference) HashSet(java.util.HashSet) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject)

Example 22 with Spell

use of pcgen.core.spell.Spell 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)

Example 23 with Spell

use of pcgen.core.spell.Spell in project pcgen by PCGen.

the class ClassDataHandler method endElement.

/**
	 * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
	 */
@Override
public void endElement(final String uri, final String localName, final String qName) {
    // start tag was obviously ignored.
    if (theState == ParserState.INIT) {
        return;
    }
    if (//$NON-NLS-1$
    "skills".equals(qName) && theState == ParserState.SKILLDATA) {
        if (remainingWeight > 0) {
            // Add all remaining skills at this weight.
            for (final Skill skill : Globals.getContext().getReferenceContext().getConstructedCDOMObjects(Skill.class)) {
                if (skill.getSafe(ObjectKey.VISIBILITY) == Visibility.DEFAULT) {
                    theCurrentData.addSkill(skill.getKeyName(), remainingWeight);
                }
            }
            remainingWeight = -1;
        }
        for (final String remove : removeList) {
            theCurrentData.removeSkill(remove);
        }
        removeList = new ArrayList<>();
        theState = ParserState.CLASSDATA;
    } else if (//$NON-NLS-1$
    "abilities".equals(qName) && theState == ParserState.ABILITYDATA) {
        if (remainingWeight > 0) {
            // Add all abilities at this weight.
            for (Ability ability : Globals.getContext().getReferenceContext().getManufacturer(Ability.class, theCurrentCategory).getAllObjects()) {
                if (ability.getSafe(ObjectKey.VISIBILITY) == Visibility.DEFAULT) {
                    theCurrentData.addAbility(theCurrentCategory, ability, remainingWeight);
                }
            }
            remainingWeight = -1;
        }
        for (final String remove : removeList) {
            Ability ability = Globals.getContext().getReferenceContext().silentlyGetConstructedCDOMObject(Ability.class, theCurrentCategory, remove);
            theCurrentData.removeAbility(theCurrentCategory, ability);
        }
        removeList = new ArrayList<>();
        theCurrentCategory = null;
        theState = ParserState.CLASSDATA;
    } else if (//$NON-NLS-1$
    "class".equals(qName) && theState != ParserState.INIT) {
        theList.add(theCurrentData);
        theState = ParserState.INIT;
    } else if (//$NON-NLS-1$
    "stats".equals(qName)) {
        theState = ParserState.CLASSDATA;
    } else if (//$NON-NLS-1$
    "level".equals(qName)) {
        if (remainingWeight > 0) {
            // Add all spells at this weight.
            final List<Spell> allSpells = getSpellsIn(theCurrentLevel, Collections.singletonList(theCurrentData.getPCClass().get(ObjectKey.CLASS_SPELLLIST)));
            for (final Spell spell : allSpells) {
                if (theCurrentSpellType == SpellType.KNOWN) {
                    theCurrentData.addKnownSpell(theCurrentLevel, spell, remainingWeight);
                } else if (theCurrentSpellType == SpellType.PREPARED) {
                    theCurrentData.addPreparedSpell(theCurrentLevel, spell, remainingWeight);
                }
            }
            remainingWeight = -1;
        }
        for (final String remove : removeList) {
            final Spell spell = Globals.getContext().getReferenceContext().silentlyGetConstructedCDOMObject(Spell.class, remove);
            if (theCurrentSpellType == SpellType.KNOWN) {
                theCurrentData.removeKnownSpell(theCurrentLevel, spell);
            } else if (theCurrentSpellType == SpellType.PREPARED) {
                theCurrentData.removeKnownSpell(theCurrentLevel, spell);
            }
        }
        removeList = new ArrayList<>();
        theCurrentLevel = -1;
        theState = ParserState.SPELLDATA;
    } else if (//$NON-NLS-1$
    "spells".equals(qName)) {
        theState = ParserState.CLASSDATA;
        theCurrentSpellType = SpellType.KNOWN;
    } else if (//$NON-NLS-1$
    "subclasses".equals(qName)) {
        theState = ParserState.CLASSDATA;
    }
}
Also used : Ability(pcgen.core.Ability) Skill(pcgen.core.Skill) ArrayList(java.util.ArrayList) Spell(pcgen.core.spell.Spell)

Example 24 with Spell

use of pcgen.core.spell.Spell in project pcgen by PCGen.

the class NPCGenerator method selectSpell.

private void selectSpell(final PlayerCharacter aPC, final PCClass aClass, final Domain aDomain, final String aBookName, final WeightedCollection<Spell> aSpellList, final int aLevel) {
    boolean added = false;
    while (!added) {
        final Spell spell = aSpellList.getRandomValue();
        // TODO - How do I check if this spell is prohibiited?
        final CharacterSpell cs;
        if (aDomain != null) {
            cs = new CharacterSpell(aDomain, spell);
        } else {
            cs = new CharacterSpell(aClass, spell);
        }
        final String aString = aPC.addSpell(cs, new ArrayList<>(), aClass.getKeyName(), aBookName, aLevel, aLevel);
        if (!aString.isEmpty()) {
            //$NON-NLS-1$
            Logging.debugPrint("Add spell failed: " + aString);
        } else {
            added = true;
        }
    }
}
Also used : CharacterSpell(pcgen.core.character.CharacterSpell) Spell(pcgen.core.spell.Spell) CharacterSpell(pcgen.core.character.CharacterSpell)

Example 25 with Spell

use of pcgen.core.spell.Spell in project pcgen by PCGen.

the class KnownspellsToken method unparse.

@Override
public String[] unparse(LoadContext context, PCClass pcc) {
    Changes<KnownSpellIdentifier> changes = context.getObjectContext().getListChanges(pcc, ListKey.KNOWN_SPELLS);
    List<String> list = new ArrayList<>();
    if (changes.includesGlobalClear()) {
        list.add(Constants.LST_DOT_CLEAR_ALL);
    }
    Collection<KnownSpellIdentifier> removedItems = changes.getRemoved();
    if (removedItems != null && !removedItems.isEmpty()) {
        context.addWriteMessage(getTokenName() + " does not support .CLEAR.");
        return null;
    }
    Collection<KnownSpellIdentifier> added = changes.getAdded();
    if (added != null && !added.isEmpty()) {
        TreeMapToList<CDOMReference<?>, Integer> map = new TreeMapToList<>(ReferenceUtilities.REFERENCE_SORTER);
        for (KnownSpellIdentifier ksi : added) {
            CDOMReference<Spell> ref = ksi.getSpellReference();
            Integer i = ksi.getSpellLevel();
            map.addToListFor(ref, i);
        }
        for (CDOMReference<?> ref : map.getKeySet()) {
            for (Integer lvl : map.getListFor(ref)) {
                StringBuilder sb = new StringBuilder();
                boolean needComma = false;
                String refString = ref.getLSTformat(false);
                if (!Constants.LST_ALL.equals(refString)) {
                    sb.append(refString);
                    needComma = true;
                }
                if (lvl != null) {
                    if (needComma) {
                        sb.append(',');
                    }
                    sb.append("LEVEL=").append(lvl);
                }
                list.add(sb.toString());
            }
        }
    }
    if (list.isEmpty()) {
        return null;
    }
    return new String[] { StringUtil.join(list, Constants.PIPE) };
}
Also used : KnownSpellIdentifier(pcgen.cdom.content.KnownSpellIdentifier) ArrayList(java.util.ArrayList) Spell(pcgen.core.spell.Spell) TreeMapToList(pcgen.base.util.TreeMapToList) CDOMReference(pcgen.cdom.base.CDOMReference)

Aggregations

Spell (pcgen.core.spell.Spell)87 CharacterSpell (pcgen.core.character.CharacterSpell)35 ArrayList (java.util.ArrayList)25 PCClass (pcgen.core.PCClass)24 AssociatedPrereqObject (pcgen.cdom.base.AssociatedPrereqObject)16 CDOMReference (pcgen.cdom.base.CDOMReference)15 Ability (pcgen.core.Ability)15 LoadContext (pcgen.rules.context.LoadContext)13 CDOMList (pcgen.cdom.base.CDOMList)12 CDOMObject (pcgen.cdom.base.CDOMObject)11 Test (org.junit.Test)10 KnownSpellIdentifier (pcgen.cdom.content.KnownSpellIdentifier)9 AvailableSpell (pcgen.cdom.helper.AvailableSpell)8 StringTokenizer (java.util.StringTokenizer)7 CNAbility (pcgen.cdom.content.CNAbility)7 ClassSpellList (pcgen.cdom.list.ClassSpellList)7 DomainSpellList (pcgen.cdom.list.DomainSpellList)7 Formula (pcgen.base.formula.Formula)6 Domain (pcgen.core.Domain)6 PObject (pcgen.core.PObject)6