Search in sources :

Example 1 with Spell

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

the class CustomData method writeCustomSpells.

private static void writeCustomSpells() {
    final BufferedWriter bw = getWriter(customSpellFilePath(true));
    if (bw == null) {
        return;
    }
    try {
        writeCustomHeader(bw);
        for (Spell spell : Globals.getContext().getReferenceContext().getConstructedCDOMObjects(Spell.class)) {
            if (spell.isType(Constants.TYPE_CUSTOM)) {
                bw.write(spell.getPCCText());
                bw.newLine();
            }
        }
    } catch (IOException e) {
        Logging.errorPrint("Error in writeCustomSpells", e);
    } finally {
        try {
            bw.close();
        } catch (IOException ex) {
            Logging.errorPrint("Error in writeCustomSpells while closing", ex);
        }
    }
}
Also used : IOException(java.io.IOException) Spell(pcgen.core.spell.Spell) BufferedWriter(java.io.BufferedWriter)

Example 2 with Spell

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

the class Gui2InfoFactory method getHTMLInfo.

@Override
public String getHTMLInfo(TempBonusFacade tempBonusFacade) {
    if (tempBonusFacade == null) {
        return EMPTY_STRING;
    }
    if (!(tempBonusFacade instanceof TempBonusFacadeImpl)) {
        final HtmlInfoBuilder infoText = new HtmlInfoBuilder();
        infoText.appendTitleElement(tempBonusFacade.toString());
        return infoText.toString();
    }
    TempBonusFacadeImpl tempBonus = (TempBonusFacadeImpl) tempBonusFacade;
    CDOMObject originObj = tempBonus.getOriginObj();
    final HtmlInfoBuilder infoText;
    if (originObj instanceof Equipment) {
        infoText = getEquipmentHtmlInfo((Equipment) originObj);
    } else {
        infoText = new HtmlInfoBuilder();
        infoText.appendTitleElement(OutputNameFormatting.piString(originObj, false));
        //$NON-NLS-1$ //$NON-NLS-2$
        infoText.append(" (").append(tempBonus.getOriginType()).append(")");
    }
    if (tempBonus.getTarget() != null) {
        String targetName = charDisplay.getName();
        if (tempBonus.getTarget() instanceof CDOMObject) {
            targetName = ((CDOMObject) tempBonus.getTarget()).getKeyName();
        }
        infoText.appendLineBreak();
        //$NON-NLS-1$
        infoText.appendI18nElement("in_itmInfoLabelTextTarget", targetName);
        StringBuilder bonusValues = new StringBuilder(100);
        Map<BonusObj, TempBonusInfo> bonusMap = pc.getTempBonusMap(originObj.getKeyName(), targetName);
        boolean first = true;
        List<BonusObj> bonusList = new ArrayList<>(bonusMap.keySet());
        bonusList.sort(new BonusComparator());
        for (BonusObj bonusObj : bonusList) {
            if (!first) {
                //$NON-NLS-1$
                bonusValues.append(", ");
            }
            first = false;
            //$NON-NLS-1$
            String adj = ADJ_FMT.format(bonusObj.resolve(pc, ""));
            //$NON-NLS-1$
            bonusValues.append(adj + " " + bonusObj.getDescription());
        }
        if (bonusValues.length() > 0) {
            infoText.appendLineBreak();
            infoText.appendI18nElement(//$NON-NLS-1$
            "in_itmInfoLabelTextEffect", bonusValues.toString());
        }
    }
    if (originObj instanceof Spell) {
        Spell aSpell = (Spell) originObj;
        infoText.appendLineBreak();
        //$NON-NLS-1$
        infoText.appendI18nElement(//$NON-NLS-1$
        "in_spellDuration", aSpell.getListAsString(ListKey.DURATION));
        infoText.appendSpacer();
        //$NON-NLS-1$
        infoText.appendI18nElement(//$NON-NLS-1$
        "in_spellRange", aSpell.getListAsString(ListKey.RANGE));
        infoText.appendSpacer();
        //$NON-NLS-1$
        infoText.appendI18nElement(//$NON-NLS-1$
        "in_spellTarget", aSpell.getSafe(StringKey.TARGET_AREA));
    }
    String aString = originObj.getSafe(StringKey.TEMP_DESCRIPTION);
    if (StringUtils.isEmpty(aString) && originObj instanceof Spell) {
        Spell sp = (Spell) originObj;
        aString = DescriptionFormatting.piWrapDesc(sp, pc.getDescription(sp), false);
    } else if (StringUtils.isEmpty(aString) && originObj instanceof Ability) {
        Ability ab = (Ability) originObj;
        List<CNAbility> wrappedAbility = Collections.singletonList(CNAbilityFactory.getCNAbility(ab.getCDOMCategory(), Nature.NORMAL, ab));
        aString = DescriptionFormatting.piWrapDesc(ab, pc.getDescription(wrappedAbility), false);
    }
    if (!aString.isEmpty()) {
        infoText.appendLineBreak();
        //$NON-NLS-1$
        infoText.appendI18nElement("in_itmInfoLabelTextDesc", aString);
    }
    aString = PrerequisiteUtilities.preReqHTMLStringsForList(pc, null, originObj.getPrerequisiteList(), false);
    if (!aString.isEmpty()) {
        infoText.appendLineBreak();
        //$NON-NLS-1$
        infoText.appendI18nElement("in_requirements", aString);
    }
    infoText.appendLineBreak();
    infoText.appendI18nElement(//$NON-NLS-1$
    "in_itmInfoLabelTextSource", SourceFormat.getFormattedString(originObj, Globals.getSourceDisplay(), true));
    return infoText.toString();
}
Also used : CNAbility(pcgen.cdom.content.CNAbility) Ability(pcgen.core.Ability) BonusObj(pcgen.core.bonus.BonusObj) ArrayList(java.util.ArrayList) Spell(pcgen.core.spell.Spell) CharacterSpell(pcgen.core.character.CharacterSpell) TempBonusInfo(pcgen.core.BonusManager.TempBonusInfo) Equipment(pcgen.core.Equipment) CDOMObject(pcgen.cdom.base.CDOMObject) ArrayList(java.util.ArrayList) List(java.util.List) HtmlInfoBuilder(pcgen.gui2.util.HtmlInfoBuilder)

Example 3 with Spell

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

the class SpellsFacet method dataAdded.

/**
	 * Adds a SpellLikeAbility to this facet if the CDOMObject added to a Player
	 * Character contains a SPELLS entry.
	 * 
	 * Triggered when one of the Facets to which SpellsFacet listens fires a
	 * DataFacetChangeEvent to indicate a CDOMObject was added to a Player
	 * Character.
	 * 
	 * @param dfce
	 *            The DataFacetChangeEvent containing the information about the
	 *            change
	 * 
	 * @see pcgen.cdom.facet.event.DataFacetChangeListener#dataAdded(pcgen.cdom.facet.event.DataFacetChangeEvent)
	 */
@Override
public void dataAdded(DataFacetChangeEvent<CharID, CDOMObject> dfce) {
    CharID id = dfce.getCharID();
    CDOMObject cdo = dfce.getCDOMObject();
    Collection<CDOMReference<Spell>> mods = cdo.getListMods(Spell.SPELLS);
    if (mods == null) {
        return;
    }
    for (CDOMReference<Spell> ref : mods) {
        Collection<AssociatedPrereqObject> assocs = cdo.getListAssociations(Spell.SPELLS, ref);
        Collection<Spell> spells = ref.getContainedObjects();
        for (AssociatedPrereqObject apo : assocs) {
            Formula times = apo.getAssociation(AssociationKey.TIMES_PER_UNIT);
            String timeunit = apo.getAssociation(AssociationKey.TIME_UNIT);
            // The timeunit needs to default to day as per the docs
            if (timeunit == null) {
                timeunit = "Day";
            }
            String casterlevel = apo.getAssociation(AssociationKey.CASTER_LEVEL);
            String dcformula = apo.getAssociation(AssociationKey.DC_FORMULA);
            String book = apo.getAssociation(AssociationKey.SPELLBOOK);
            String ident = cdo.getQualifiedKey();
            for (Spell sp : spells) {
                SpellLikeAbility sla = new SpellLikeAbility(sp, times, timeunit, book, casterlevel, dcformula, ident);
                sla.addAllPrerequisites(apo.getPrerequisiteList());
                add(id, sla, cdo);
            }
        }
    }
}
Also used : Formula(pcgen.base.formula.Formula) CDOMObject(pcgen.cdom.base.CDOMObject) SpellLikeAbility(pcgen.cdom.content.SpellLikeAbility) CharID(pcgen.cdom.enumeration.CharID) CDOMReference(pcgen.cdom.base.CDOMReference) Spell(pcgen.core.spell.Spell) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject)

Example 4 with Spell

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

the class KnownSpellInputFacet method processList.

private void processList(CharID id, CDOMList<Spell> spelllist, CDOMReference<? extends CDOMList<?>> listref, CDOMObject cdo) {
    for (CDOMReference<Spell> objref : cdo.getListMods((CDOMReference<? extends CDOMList<Spell>>) listref)) {
        for (AssociatedPrereqObject apo : cdo.getListAssociations(listref, objref)) {
            Boolean known = apo.getAssociation(AssociationKey.KNOWN);
            if ((known == null) || !known.booleanValue()) {
                continue;
            }
            Collection<Spell> spells = objref.getContainedObjects();
            Integer lvl = apo.getAssociation(AssociationKey.SPELL_LEVEL);
            if (apo.hasPrerequisites()) {
                List<Prerequisite> prereqs = apo.getPrerequisiteList();
                for (Spell spell : spells) {
                    AvailableSpell as = new AvailableSpell(spelllist, spell, lvl);
                    as.addAllPrerequisites(prereqs);
                    conditionallyKnownSpellFacet.add(id, as, cdo);
                }
            } else {
                for (Spell spell : spells) {
                    knownSpellFacet.add(id, spelllist, lvl, spell, cdo);
                }
            }
        }
    }
}
Also used : AvailableSpell(pcgen.cdom.helper.AvailableSpell) AvailableSpell(pcgen.cdom.helper.AvailableSpell) Spell(pcgen.core.spell.Spell) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 5 with Spell

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

the class PCGVer2Parser method parseSpellLine.

/*
	 * ###############################################################
	 * Character Spells Information methods
	 * ###############################################################
	 */
private void parseSpellLine(final String line) {
    final PCGTokenizer tokens;
    try {
        tokens = new PCGTokenizer(line);
    } catch (PCGParseException pcgpex) {
        final String message = "Illegal Spell line ignored: " + line + Constants.LINE_SEPARATOR + "Error: " + pcgpex.getMessage();
        warnings.add(message);
        return;
    }
    Spell aSpell = null;
    PCClass aPCClass = null;
    PObject source = null;
    String spellBook = null;
    int times = 1;
    int spellLevel = 0;
    int numPages = 0;
    final List<Ability> metaFeats = new ArrayList<>();
    int ppCost = -1;
    for (final PCGElement element : tokens.getElements()) {
        final String tag = element.getName();
        if (IOConstants.TAG_SPELLNAME.equals(tag)) {
            String spellName = EntityEncoder.decode(element.getText());
            spellName = SpellMigration.getNewSpellKey(spellName, pcgenVersion, SettingsHandler.getGame().getName());
            // either NULL (no spell) a Spell instance,
            aSpell = Globals.getContext().getReferenceContext().silentlyGetConstructedCDOMObject(Spell.class, spellName);
            if (aSpell == null) {
                final String message = "Could not find spell named: " + spellName;
                warnings.add(message);
                return;
            }
        } else if (IOConstants.TAG_TIMES.equals(tag)) {
            try {
                times = Integer.parseInt(element.getText());
            } catch (NumberFormatException nfe) {
            // nothing we can do about it
            }
        } else if (IOConstants.TAG_CLASS.equals(tag)) {
            final String classKey = EntityEncoder.decode(element.getText());
            aPCClass = thePC.getClassKeyed(classKey);
            if (aPCClass == null) {
                final String message = "Invalid class specification: " + classKey;
                warnings.add(message);
                return;
            }
        } else if (IOConstants.TAG_SPELL_BOOK.equals(tag)) {
            spellBook = EntityEncoder.decode(element.getText());
        } else if (IOConstants.TAG_SPELLLEVEL.equals(tag)) {
            try {
                spellLevel = Integer.parseInt(element.getText());
            } catch (NumberFormatException nfe) {
            // nothing we can do about it
            }
        } else if (IOConstants.TAG_SPELLPPCOST.equals(tag)) {
            try {
                ppCost = Integer.parseInt(element.getText());
            } catch (NumberFormatException nfe) {
            // nothing we can do about it
            }
        } else if (IOConstants.TAG_SPELLNUMPAGES.equals(tag)) {
            try {
                numPages = Integer.parseInt(element.getText());
            } catch (NumberFormatException nfe) {
            // nothing we can do about it
            }
        } else if (IOConstants.TAG_SOURCE.equals(tag)) {
            String typeName = Constants.EMPTY_STRING;
            String objectKey = Constants.EMPTY_STRING;
            for (final PCGElement child : element.getChildren()) {
                final String childTag = child.getName();
                if (IOConstants.TAG_TYPE.equals(childTag)) {
                    typeName = child.getText().toUpperCase();
                } else if (IOConstants.TAG_NAME.equals(childTag)) {
                    objectKey = child.getText();
                }
            }
            if (IOConstants.TAG_DOMAIN.equals(typeName)) {
                Domain domain = Globals.getContext().getReferenceContext().silentlyGetConstructedCDOMObject(DOMAIN_CLASS, objectKey);
                ClassSource cs = thePC.getDomainSource(domain);
                if (cs == null) {
                    final String message = "Could not find domain: " + objectKey;
                    warnings.add(message);
                    return;
                }
                source = domain;
            } else {
                // it's either the class, sub-class or a cast-as class
                // first see if it's the class
                ClassSpellList csl = Globals.getContext().getReferenceContext().silentlyGetConstructedCDOMObject(ClassSpellList.class, objectKey);
                if (((aPCClass != null) && objectKey.equals(aPCClass.getKeyName())) || (aPCClass != null && thePC.getSpellLists(aPCClass).contains(csl))) {
                    source = aPCClass;
                } else {
                    // see if PC has the class
                    source = thePC.getClassKeyed(objectKey);
                }
            }
        } else if (IOConstants.TAG_FEATLIST.equals(tag)) {
            for (PCGElement child : element.getChildren()) {
                final String featKey = EntityEncoder.decode(child.getText());
                final Ability anAbility = Globals.getContext().getReferenceContext().silentlyGetConstructedCDOMObject(Ability.class, AbilityCategory.FEAT, featKey);
                if (anAbility != null) {
                    metaFeats.add(anAbility);
                }
            }
        }
    }
    if ((aPCClass == null) || (spellBook == null)) {
        final String message = "Illegal Spell line ignored: " + line;
        warnings.add(message);
        return;
    }
    /*
		 * this can only happen if the source type was NOT DOMAIN!
		 */
    if (source == null) {
        source = aPCClass;
    }
    //		if (obj instanceof List)
    //		{
    //			// find the instance of Spell in this class
    //			// best suited to this spell
    //			for (final Spell spell : (ArrayList<Spell>) obj)
    //			{
    //				// valid spell has a non-negative spell level
    //				if ((spell != null)
    //					&& (SpellLevel.getFirstLevelForKey(spell,
    //						thePC.getSpellLists(source), thePC) >= 0))
    //				{
    //					aSpell = spell;
    //					break;
    //				}
    //			}
    //			if (aSpell == null)
    //			{
    //				Logging.errorPrint("Could not resolve spell " + obj.toString());
    //			}
    //		}
    //		if (aSpell == null)
    //		{
    //			final String message =
    //					"Could not find spell named: " + String.valueOf(obj);
    //			warnings.add(message);
    //
    //			return;
    //		}
    // just to make sure the spellbook is present
    thePC.addSpellBook(spellBook);
    final SpellBook book = thePC.getSpellBookByName(spellBook);
    thePC.calculateKnownSpellsForClassLevel(aPCClass);
    final Integer[] spellLevels = SpellLevel.levelForKey(aSpell, thePC.getSpellLists(source), thePC);
    boolean found = false;
    for (int sindex = 0; sindex < spellLevels.length; ++sindex) {
        final int level = spellLevels[sindex];
        final int metmagicLevels = totalAddedLevelsFromMetamagic(metaFeats);
        if (spellLevel > 0 && spellLevel != (level + metmagicLevels)) {
            // Skip spell in class lists that does not match level the character knows it.
            continue;
        }
        if (level < 0) {
            Collection<CDOMReference<Spell>> mods = source.getListMods(Spell.SPELLS);
            if (mods == null) {
                continue;
            }
            for (CDOMReference<Spell> ref : mods) {
                Collection<Spell> refSpells = ref.getContainedObjects();
                Collection<AssociatedPrereqObject> assocs = source.getListAssociations(Spell.SPELLS, ref);
                for (Spell sp : refSpells) {
                    if (aSpell.getKeyName().equals(sp.getKeyName())) {
                        for (AssociatedPrereqObject apo : assocs) {
                            String sb = apo.getAssociation(AssociationKey.SPELLBOOK);
                            if (spellBook.equals(sb)) {
                                found = true;
                                break;
                            }
                        }
                    }
                }
            }
            continue;
        }
        found = true;
        // do not load auto knownspells into default spellbook
        if (spellBook.equals(Globals.getDefaultSpellBook()) && thePC.getSpellSupport(aPCClass).isAutoKnownSpell(aSpell, level, false, thePC) && thePC.getAutoSpells()) {
            continue;
        }
        CharacterSpell aCharacterSpell = thePC.getCharacterSpellForSpell(aPCClass, aSpell, source);
        // so we'll need to add it to the list
        if (aCharacterSpell == null) {
            aCharacterSpell = new CharacterSpell(source, aSpell);
            aCharacterSpell.addInfo(level, times, spellBook);
            thePC.addCharacterSpell(aPCClass, aCharacterSpell);
        }
        SpellInfo aSpellInfo = null;
        if (source.getKeyName().equals(aPCClass.getKeyName()) || !spellBook.equals(Globals.getDefaultSpellBook())) {
            aSpellInfo = aCharacterSpell.getSpellInfoFor(spellBook, spellLevel);
            // metaFeats list have to do with this?
            if ((aSpellInfo == null) || !metaFeats.isEmpty()) {
                aSpellInfo = aCharacterSpell.addInfo(spellLevel, times, spellBook);
            }
        }
        if (aSpellInfo != null) {
            if (!metaFeats.isEmpty()) {
                aSpellInfo.addFeatsToList(metaFeats);
            }
            aSpellInfo.setActualPPCost(ppCost);
            aSpellInfo.setNumPages(numPages);
            book.setNumPagesUsed(book.getNumPagesUsed() + numPages);
            book.setNumSpells(book.getNumSpells() + 1);
        }
    }
    if (!found) {
        final String message = "Could not find spell " + aSpell.getDisplayName() + " in " + shortClassName(source) + " " + source.getDisplayName();
        warnings.add(message);
    }
}
Also used : ArrayList(java.util.ArrayList) Spell(pcgen.core.spell.Spell) CharacterSpell(pcgen.core.character.CharacterSpell) ClassSource(pcgen.cdom.helper.ClassSource) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject) CNAbility(pcgen.cdom.content.CNAbility) Ability(pcgen.core.Ability) SpecialAbility(pcgen.core.SpecialAbility) ClassSpellList(pcgen.cdom.list.ClassSpellList) PCClass(pcgen.core.PCClass) SpellBook(pcgen.core.character.SpellBook) PObject(pcgen.core.PObject) CharacterSpell(pcgen.core.character.CharacterSpell) Domain(pcgen.core.Domain) CDOMReference(pcgen.cdom.base.CDOMReference) SpellInfo(pcgen.core.character.SpellInfo)

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