fix trying to add to null-spellbook.

all unit tests pass.
This commit is contained in:
Enno Rehling 2012-05-24 20:35:13 -07:00
parent 6417f55ec6
commit b8d8bac9ae
3 changed files with 6 additions and 2 deletions

View File

@ -480,7 +480,7 @@ void pick_random_spells(faction * f, int level, spellbook * book, int num_spells
commonspells[spellno] = commonspells[maxspell]; commonspells[spellno] = commonspells[maxspell];
commonspells[maxspell--] = sp; commonspells[maxspell--] = sp;
sp = 0; sp = 0;
} else if (spellbook_get(f->spellbook, sp)) { } else if (f->spellbook && spellbook_get(f->spellbook, sp)) {
commonspells[spellno] = commonspells[numspells--]; commonspells[spellno] = commonspells[numspells--];
if (maxspell>numspells) { if (maxspell>numspells) {
maxspell = numspells; maxspell = numspells;
@ -490,6 +490,9 @@ void pick_random_spells(faction * f, int level, spellbook * book, int num_spells
} }
if (spellno<maxspell) { if (spellno<maxspell) {
if (!f->spellbook) {
f->spellbook = create_spellbook(0);
}
spellbook_add(f->spellbook, sp, f->max_spelllevel); spellbook_add(f->spellbook, sp, f->max_spelllevel);
commonspells[spellno] = commonspells[numspells--]; commonspells[spellno] = commonspells[numspells--];
} }

View File

@ -27,7 +27,7 @@ void test_updatespells(CuTest * tc)
CuAssertPtrNotNull(tc, book); CuAssertPtrNotNull(tc, book);
spellbook_add(book, sp, 1); spellbook_add(book, sp, 1);
CuAssertIntEquals(tc, 0, ql_length(f->spellbook->spells)); CuAssertPtrEquals(tc, 0, f->spellbook);
pick_random_spells(f, 1, book, 1); pick_random_spells(f, 1, book, 1);
CuAssertPtrNotNull(tc, f->spellbook); CuAssertPtrNotNull(tc, f->spellbook);
CuAssertIntEquals(tc, 1, ql_length(f->spellbook->spells)); CuAssertIntEquals(tc, 1, ql_length(f->spellbook->spells));

View File

@ -57,6 +57,7 @@ spellbook_entry * spellbook_get(spellbook *sb, struct spell * sp)
quicklist *ql; quicklist *ql;
int qi; int qi;
assert(sb);
for (qi = 0, ql = sb->spells; ql; ql_advance(&ql, &qi, 1)) { for (qi = 0, ql = sb->spells; ql; ql_advance(&ql, &qi, 1)) {
spellbook_entry *sbe = (spellbook_entry *) ql_get(ql, qi); spellbook_entry *sbe = (spellbook_entry *) ql_get(ql, qi);
if (sp==sbe->sp) { if (sp==sbe->sp) {