diff --git a/src/kernel/magic.c b/src/kernel/magic.c index 8d80846d5..69166f220 100644 --- a/src/kernel/magic.c +++ b/src/kernel/magic.c @@ -480,7 +480,7 @@ void pick_random_spells(faction * f, int level, spellbook * book, int num_spells commonspells[spellno] = commonspells[maxspell]; commonspells[maxspell--] = sp; sp = 0; - } else if (spellbook_get(f->spellbook, sp)) { + } else if (f->spellbook && spellbook_get(f->spellbook, sp)) { commonspells[spellno] = commonspells[numspells--]; if (maxspell>numspells) { maxspell = numspells; @@ -490,6 +490,9 @@ void pick_random_spells(faction * f, int level, spellbook * book, int num_spells } if (spellnospellbook) { + f->spellbook = create_spellbook(0); + } spellbook_add(f->spellbook, sp, f->max_spelllevel); commonspells[spellno] = commonspells[numspells--]; } diff --git a/src/kernel/magic_test.c b/src/kernel/magic_test.c index 49960933d..ed6f1e684 100644 --- a/src/kernel/magic_test.c +++ b/src/kernel/magic_test.c @@ -27,7 +27,7 @@ void test_updatespells(CuTest * tc) CuAssertPtrNotNull(tc, book); 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); CuAssertPtrNotNull(tc, f->spellbook); CuAssertIntEquals(tc, 1, ql_length(f->spellbook->spells)); diff --git a/src/kernel/spellbook.c b/src/kernel/spellbook.c index 568096896..5002c14d1 100644 --- a/src/kernel/spellbook.c +++ b/src/kernel/spellbook.c @@ -57,6 +57,7 @@ spellbook_entry * spellbook_get(spellbook *sb, struct spell * sp) quicklist *ql; int qi; + assert(sb); for (qi = 0, ql = sb->spells; ql; ql_advance(&ql, &qi, 1)) { spellbook_entry *sbe = (spellbook_entry *) ql_get(ql, qi); if (sp==sbe->sp) {