use of pcgen.core.character.CharacterSpell 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);
}
use of pcgen.core.character.CharacterSpell in project pcgen by PCGen.
the class AbstractRestrictedSpellPrimitive method allow.
public boolean allow(PlayerCharacter pc, int level, String source, Spell spell, CDOMList<Spell> optionalList) {
if (restriction != null) {
Formula maxLevel = restriction.maxLevel;
if (maxLevel != null && (level > maxLevel.resolve(pc, source).intValue())) {
return false;
}
Formula minLevel = restriction.minLevel;
if (minLevel != null && (level < minLevel.resolve(pc, source).intValue())) {
return false;
}
if (restriction.knownRequired != null) {
String defaultbook = Globals.getDefaultSpellBook();
boolean known = restriction.knownRequired.booleanValue();
boolean found = false;
for (PCClass cl : pc.getClassSet()) {
if (optionalList != null) {
/*
* This may not be a precise test of intent, but given
* the weirdness we have on lists and the use of
* SPELLLIST tag in data to share lists between classes,
* this is probably the closest we can get
*/
if (!pc.hasSpellList(cl, optionalList)) {
continue;
}
}
List<CharacterSpell> csl = pc.getCharacterSpells(cl, spell, defaultbook, -1);
if (csl != null && !csl.isEmpty()) {
/*
* Going to assume here that the level doesn't need to
* be rechecked... ?? - thpr Feb 26, 08
*/
found = true;
}
}
if (found != known) {
return false;
}
}
}
return true;
}
use of pcgen.core.character.CharacterSpell in project pcgen by PCGen.
the class PCCountSpellTimesTermEvaluator method resolve.
@Override
public Float resolve(PlayerCharacter pc) {
String bookName = (classNum == -1) ? Globals.getDefaultSpellBook() : (bookNum > 0) ? pc.getDisplay().getSpellBookNames().get(bookNum) : Globals.getDefaultSpellBook();
if (!"".equals(bookName)) {
List<CharacterSpell> csList = new ArrayList<>();
if (classNum == -1) {
csList = new ArrayList<>();
for (PObject cl : pc.getDisplay().getClassSet()) {
for (CharacterSpell cs : pc.getCharacterSpells(cl, bookName)) {
if (!csList.contains(cs)) {
csList.add(cs);
}
}
}
Collections.sort(csList);
} else {
final PObject pcClass = pc.getSpellClassAtIndex(classNum);
if (pcClass != null) {
csList = pc.getCharacterSpells(pcClass, null, bookName, spellLevel);
}
}
boolean found = false;
SpellInfo si = null;
if (spellNumber < csList.size()) {
final CharacterSpell cs = csList.get(spellNumber);
si = cs.getSpellInfoFor(bookName, spellLevel);
found = true;
}
if (found && (si != null)) {
return (float) si.getTimes();
}
}
return 0.0f;
}
use of pcgen.core.character.CharacterSpell in project pcgen by PCGen.
the class PCCountSpellsLevelsInBookTermEvaluator method resolve.
@Override
public Float resolve(PlayerCharacter pc) {
String bookName = Globals.getDefaultSpellBook();
if (sbookNum > 0) {
bookName = pc.getDisplay().getSpellBookNames().get(sbookNum);
}
final PObject pObj = pc.getSpellClassAtIndex(classNum);
int levelNum = 0;
if (pObj != null) {
for (; levelNum >= 0; ++levelNum) {
final List<CharacterSpell> aList = pc.getCharacterSpells(pObj, null, bookName, levelNum);
if (aList.size() < 1) {
break;
}
}
}
return (float) levelNum;
}
use of pcgen.core.character.CharacterSpell in project pcgen by PCGen.
the class ExportHandler method replaceTokenSpellListBook.
/**
* Helper method to deal with the SpellListBook token
*
* @param aString
* @param aPC
* @return 0
*/
private int replaceTokenSpellListBook(String aString, PlayerCharacter aPC) {
int sbookNum = 0;
final StringTokenizer aTok = new StringTokenizer(aString, ".");
final int classNum = Integer.parseInt(aTok.nextToken());
final int levelNum = Integer.parseInt(aTok.nextToken());
// Get the spell book number
if (aTok.hasMoreTokens()) {
sbookNum = Integer.parseInt(aTok.nextToken());
}
// Set the spell book name
String bookName = Globals.getDefaultSpellBook();
if (sbookNum > 0) {
bookName = aPC.getDisplay().getSpellBookNames().get(sbookNum);
}
canWrite = false;
final PObject aObject = aPC.getSpellClassAtIndex(classNum);
if (aObject != null) {
final List<CharacterSpell> aList = aPC.getCharacterSpells(aObject, null, bookName, levelNum);
canWrite = !aList.isEmpty();
}
return 0;
}
Aggregations