Search in sources :

Example 6 with SpellBook

use of pcgen.core.character.SpellBook in project pcgen by PCGen.

the class SpellBookToken method getToken.

/**
	 * @see pcgen.io.exporttoken.AbstractExportToken#getToken(java.lang.String, pcgen.core.display.CharacterDisplay, pcgen.io.ExportHandler)
	 */
@Override
public String getToken(String tokenSource, CharacterDisplay display, ExportHandler eh) {
    String retString = "";
    final StringTokenizer aTok = new StringTokenizer(tokenSource, ".");
    // Ignore the token itself
    aTok.nextToken();
    final int bookNum = Integer.parseInt(aTok.nextToken());
    String aLabel = "NAME";
    if (aTok.hasMoreTokens()) {
        aLabel = aTok.nextToken();
    }
    SpellBook book = null;
    if (bookNum >= 0 && bookNum < display.getSpellBookCount()) {
        String bookName = display.getSpellBookNames().get(bookNum);
        book = display.getSpellBookByName(bookName);
    }
    if (book != null) {
        if ("NAME".equals(aLabel)) {
            retString = book.getName();
        } else if ("NUMPAGES".equals(aLabel)) {
            retString = String.valueOf(book.getNumPages());
        } else if ("NUMPAGESUSED".equals(aLabel)) {
            retString = String.valueOf(book.getNumPagesUsed());
        } else if ("NUMSPELLS".equals(aLabel)) {
            retString = String.valueOf(book.getNumSpells());
        } else if ("PAGEFORMULA".equals(aLabel)) {
            retString = book.getPageFormula().toString();
        } else if ("TYPE".equals(aLabel)) {
            retString = book.getTypeName();
        }
    }
    return retString;
}
Also used : StringTokenizer(java.util.StringTokenizer) SpellBook(pcgen.core.character.SpellBook)

Example 7 with SpellBook

use of pcgen.core.character.SpellBook in project pcgen by PCGen.

the class PlayerCharacterTest method testAvailableSpellsMemorizedDivine.

/**
	 * Tests available spell slot calculations for a divine caster who 
	 * memorizes spells.
	 */
public void testAvailableSpellsMemorizedDivine() {
    readyToRun();
    final PlayerCharacter character = new PlayerCharacter();
    character.setRace(human);
    character.setStat(wis, 15);
    character.incrementClassLevel(1, classMemDivine, true);
    PCClass pcMdClass = character.getClassKeyed(classMemDivine.getKeyName());
    Spell spellNonSpec0 = new Spell();
    spellNonSpec0.setName("Basic Spell Lvl0");
    CharacterSpell charSpellNonSpec0 = new CharacterSpell(pcMdClass, spellNonSpec0);
    Spell spellNonSpec1 = new Spell();
    spellNonSpec1.setName("Basic Spell Lvl1");
    CharacterSpell charSpellNonSpec1 = new CharacterSpell(pcMdClass, spellNonSpec1);
    Spell spellNonSpec2 = new Spell();
    spellNonSpec2.setName("Basic Spell Lvl2");
    CharacterSpell charSpellNonSpec2 = new CharacterSpell(pcMdClass, spellNonSpec2);
    final List<Ability> none = Collections.emptyList();
    boolean available = character.availableSpells(1, pcMdClass, Globals.getDefaultSpellBook(), true, false);
    assertEquals("availableSpells should not be called when there ar eno limits on known spells", false, available);
    // Test specialty/non with no spells, some spells, all spells, spells from lower level
    String spellBookName = "Town Spells";
    SpellBook townSpells = new SpellBook(spellBookName, SpellBook.TYPE_PREPARED_LIST);
    assertTrue("Adding spellbook " + townSpells, character.addSpellBook(townSpells));
    assertTrue("Adding domain " + luckDomain, character.addDomain(luckDomain));
    DomainApplication.applyDomain(character, luckDomain);
    // Test for spell availability with no spells in list
    for (int i = 0; i < 3; i++) {
        assertEquals("Empty list - Non specialty available for level " + i, true, character.availableSpells(i, pcMdClass, townSpells.getName(), false, false));
        assertEquals("Empty list - Specialty available for level " + i, i > 0, character.availableSpells(i, pcMdClass, townSpells.getName(), false, true));
    }
    // Test for spell availability with some spells in list
    assertEquals("", character.addSpell(charSpellNonSpec0, none, pcMdClass.getKeyName(), spellBookName, 0, 0));
    assertEquals("", character.addSpell(charSpellNonSpec1, none, pcMdClass.getKeyName(), spellBookName, 1, 1));
    assertEquals("", character.addSpell(charSpellNonSpec2, none, pcMdClass.getKeyName(), spellBookName, 2, 2));
    for (int i = 0; i < 3; i++) {
        assertEquals("Partial list - Non specialty available for level " + i, true, character.availableSpells(i, pcMdClass, townSpells.getName(), false, false));
        assertEquals("Partial list - Specialty available for level " + i, i > 0, character.availableSpells(i, pcMdClass, townSpells.getName(), false, true));
    }
    // Test for spell availability with only 1st level with a spare slot
    assertEquals("", character.addSpell(charSpellNonSpec0, none, pcMdClass.getKeyName(), spellBookName, 0, 0));
    assertEquals("", character.addSpell(charSpellNonSpec0, none, pcMdClass.getKeyName(), spellBookName, 0, 0));
    assertEquals("", character.addSpell(charSpellNonSpec2, none, pcMdClass.getKeyName(), spellBookName, 2, 2));
    for (int i = 0; i < 3; i++) {
        assertEquals("Full lvl0, lvl2 list - Non specialty available for level " + i, i == 1, character.availableSpells(i, pcMdClass, townSpells.getName(), false, false));
        //TODO: The current implementation only finds the domain specialty slot if a domain spell is already prepared. 
        // So the domain spell can't be the last added. Once fixed, i==1 should be i>=1
        assertEquals("Full lvl0, lvl2 list - Specialty available for level " + i, i >= 1, character.availableSpells(i, pcMdClass, townSpells.getName(), false, true));
    }
    // Test for spell availability with 1st having one domain spell full and one non domain free
    CharacterSpell charSpellSpec1 = new CharacterSpell(luckDomain, luckDomainLvl1Spell);
    assertEquals("", character.addSpell(charSpellSpec1, none, pcMdClass.getKeyName(), spellBookName, 1, 1));
    for (int i = 0; i < 3; i++) {
        assertEquals("Specialty: No, Level: " + i + ". 1st lvl non domain only free", i == 1, character.availableSpells(i, pcMdClass, townSpells.getName(), false, false));
        assertEquals("Specialty: Yes, Level: " + i + ". 1st lvl non domain only free", i == 2, character.availableSpells(i, pcMdClass, townSpells.getName(), false, true));
    }
    // Test for spell availability with 2nd having both domain and normal full
    CharacterSpell charSpellSpec2 = new CharacterSpell(luckDomain, luckDomainLvl2Spell);
    assertEquals("", character.addSpell(charSpellSpec2, none, pcMdClass.getKeyName(), spellBookName, 2, 2));
    for (int i = 0; i < 3; i++) {
        assertEquals("Specialty: No, Level: " + i + ". 1st lvl non domain only free", i == 1, character.availableSpells(i, pcMdClass, townSpells.getName(), false, false));
        assertEquals("Specialty: Yes, Level: " + i + ". 1st lvl non domain only free", false, character.availableSpells(i, pcMdClass, townSpells.getName(), false, true));
    }
}
Also used : SpellBook(pcgen.core.character.SpellBook) CharacterSpell(pcgen.core.character.CharacterSpell) Spell(pcgen.core.spell.Spell) CharacterSpell(pcgen.core.character.CharacterSpell)

Example 8 with SpellBook

use of pcgen.core.character.SpellBook in project pcgen by PCGen.

the class PlayerCharacterTest method testAddSpells.

/**
	 * Tests adding a spell.
	 */
public void testAddSpells() {
    readyToRun();
    final PlayerCharacter character = new PlayerCharacter();
    character.setRace(human);
    character.incrementClassLevel(1, pcClass, true);
    final List<Ability> none = Collections.emptyList();
    String response = character.addSpell(null, none, pcClass.getKeyName(), null, 1, 1);
    assertEquals("Add spell should be rejected due to no spell", "Invalid parameter to add spell", response);
    Spell spell = new Spell();
    spell.setName("test spell 1");
    CharacterSpell charSpell = new CharacterSpell(pcClass, spell);
    response = character.addSpell(charSpell, none, pcClass.getKeyName(), null, 1, 1);
    assertEquals("Add spell should be rejected due to no book", "Invalid spell list/book name.", response);
    response = character.addSpell(charSpell, none, pcClass.getKeyName(), "", 1, 1);
    assertEquals("Add spell should be rejected due to no book", "Invalid spell list/book name.", response);
    // Add a non existant spell to a non existent spellbook
    String spellBookName = "Test book";
    response = character.addSpell(charSpell, none, pcClass.getKeyName(), spellBookName, 1, 1);
    assertEquals("Add spell should be rejected due to book not existing", "Could not find spell list/book Test book", response);
    character.addSpellBook(spellBookName);
    response = character.addSpell(charSpell, none, pcClass.getKeyName(), spellBookName, 1, 1);
    assertEquals("Add spell should be rejected due to no levels.", "You can only prepare 0 spells for level 1 \nand there are no higher-level slots available.", response);
    response = character.addSpell(charSpell, none, "noclass", spellBookName, 1, 1);
    assertEquals("Add spell should be rejected due to no matching class", "No class keyed noclass", response);
    SpellBook book = character.getSpellBookByName(spellBookName);
    book.setType(SpellBook.TYPE_PREPARED_LIST);
    character.addSpellBook(spellBookName);
    response = character.addSpell(charSpell, none, pcClass.getKeyName(), spellBookName, 1, 1);
    assertEquals("Add spell should be rejected due to no levels.", "You can only prepare 0 spells for level 1 \nand there are no higher-level slots available.", response);
    book.setType(SpellBook.TYPE_SPELL_BOOK);
    book.setPageFormula(FormulaFactory.getFormulaFor("SPELLLEVEL"));
    book.setNumPages(3);
    character.addSpellBook(spellBookName);
    response = character.addSpell(charSpell, none, pcClass.getKeyName(), spellBookName, 1, 1);
    assertEquals("Add spell should not be rejected.", "", response);
    // Add a second time to cover multiples
    response = character.addSpell(charSpell, none, pcClass.getKeyName(), spellBookName, 1, 1);
    assertEquals("Add spell should not be rejected.", "", response);
    response = character.addSpell(charSpell, none, giantClass.getKeyName(), spellBookName, 1, 1);
    assertEquals("Add spell should not be rejected.", "", response);
    response = character.addSpell(charSpell, none, giantClass.getKeyName(), spellBookName, 1, 1);
    assertEquals("Add spell should be rejected due to the book being full.", "There are not enough pages left to add this spell to the spell book.", response);
    PCClass c = character.getClassKeyed(pcClass.getKeyName());
    List<CharacterSpell> aList = character.getCharacterSpells(c, null, spellBookName, 1);
    CharacterSpell addedSpell = aList.get(0);
    response = character.delSpell(addedSpell.getSpellInfoFor(spellBookName, 1, none), pcClass, spellBookName);
    assertEquals("Delete spell should not be rejected.", "", response);
    aList = character.getCharacterSpells(giantClass, null, spellBookName, 1);
    addedSpell = aList.get(0);
    response = character.delSpell(addedSpell.getSpellInfoFor(spellBookName, 1), giantClass, spellBookName);
    assertEquals("Delete spell should not be rejected.", "", response);
}
Also used : SpellBook(pcgen.core.character.SpellBook) CharacterSpell(pcgen.core.character.CharacterSpell) Spell(pcgen.core.spell.Spell) CharacterSpell(pcgen.core.character.CharacterSpell)

Example 9 with SpellBook

use of pcgen.core.character.SpellBook in project pcgen by PCGen.

the class PlayerCharacter method updateEquipmentQty.

/**
	 * Update the number of a particular equipment item the character possesses.
	 * Mostly concerned with ensuring that the spellbook objects remain in sync
	 * with the number of equipment spellbooks.
	 *
	 * @param eq
	 *            The Equipment being updated.
	 * @param oldQty
	 *            The original number of items.
	 * @param newQty
	 *            The new number of items.
	 */
public void updateEquipmentQty(final Equipment eq, double oldQty, double newQty) {
    if (eq.isType(Constants.TYPE_SPELLBOOK)) {
        String baseBookname = eq.getName();
        String bookName = eq.getName();
        int old = (int) oldQty;
        int newQ = (int) newQty;
        // Add any new items
        for (int i = old; i < newQ; i++) {
            if (i > 0) {
                bookName = baseBookname + " #" + (i + 1);
            }
            SpellBook book = spellBookFacet.getBookNamed(id, bookName);
            if (book == null) {
                book = new SpellBook(bookName, SpellBook.TYPE_SPELL_BOOK);
            }
            book.setEquip(eq);
            addSpellBook(book);
        }
        // Remove any old items
        for (int i = old; i > newQ; i--) {
            if (i > 0) {
                bookName = baseBookname + " #" + i;
            }
            delSpellBook(bookName);
        }
    }
    setDirty(true);
}
Also used : SpellBook(pcgen.core.character.SpellBook)

Example 10 with SpellBook

use of pcgen.core.character.SpellBook in project pcgen by PCGen.

the class PlayerCharacter method delSpell.

/**
	 * Return value indicates whether or not a spell was deleted.
	 *
	 * @param si
	 * @param aClass
	 * @param bookName
	 * @return String
	 */
public String delSpell(SpellInfo si, final PCClass aClass, final String bookName) {
    if ((bookName == null) || (bookName.isEmpty())) {
        return "Invalid spell book name.";
    }
    if (aClass == null) {
        return "Error: Class is null";
    }
    final CharacterSpell acs = si.getOwner();
    final boolean isDefault = bookName.equals(Globals.getDefaultSpellBook());
    // accident (or is saved in the .pcg file)
    if (isDefault && this.getSpellSupport(aClass).isAutoKnownSpell(acs.getSpell(), si.getActualLevel(), false, this)) {
        Logging.errorPrint("Notice: removing " + acs.getSpell().getDisplayName() + " even though it is an auto known spell");
    }
    SpellBook spellBook = getSpellBookByName(bookName);
    if (spellBook.getType() == SpellBook.TYPE_SPELL_BOOK) {
        int pagesPerSpell = si.getNumPages() / si.getTimes();
        spellBook.setNumPagesUsed(spellBook.getNumPagesUsed() - pagesPerSpell);
        spellBook.setNumSpells(spellBook.getNumSpells() - 1);
        si.setNumPages(si.getNumPages() - pagesPerSpell);
    }
    si.setTimes(si.getTimes() - 1);
    if (si.getTimes() <= 0) {
        acs.removeSpellInfo(si);
    }
    // is no longer present in any book
    if (acs.getInfoList().isEmpty()) {
        removeCharacterSpell(aClass, acs);
    }
    return "";
}
Also used : SpellBook(pcgen.core.character.SpellBook) CharacterSpell(pcgen.core.character.CharacterSpell)

Aggregations

SpellBook (pcgen.core.character.SpellBook)14 CharacterSpell (pcgen.core.character.CharacterSpell)6 Spell (pcgen.core.spell.Spell)4 SpellInfo (pcgen.core.character.SpellInfo)3 ArrayList (java.util.ArrayList)2 PCClass (pcgen.core.PCClass)2 PObject (pcgen.core.PObject)2 StringTokenizer (java.util.StringTokenizer)1 Formula (pcgen.base.formula.Formula)1 AssociatedPrereqObject (pcgen.cdom.base.AssociatedPrereqObject)1 CDOMReference (pcgen.cdom.base.CDOMReference)1 CNAbility (pcgen.cdom.content.CNAbility)1 SpellLikeAbility (pcgen.cdom.content.SpellLikeAbility)1 CharID (pcgen.cdom.enumeration.CharID)1 ClassSource (pcgen.cdom.helper.ClassSource)1 ClassSpellList (pcgen.cdom.list.ClassSpellList)1 Ability (pcgen.core.Ability)1 Domain (pcgen.core.Domain)1 Equipment (pcgen.core.Equipment)1 PlayerCharacter (pcgen.core.PlayerCharacter)1