From c73297ba44d3a31baa18f5aa15887de7ba111116 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 25 May 2012 23:47:24 -0700 Subject: [PATCH] fix an indexing error in pick_random_spell --- src/kernel/magic.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/kernel/magic.c b/src/kernel/magic.c index 092c0abe8..0518ea856 100644 --- a/src/kernel/magic.c +++ b/src/kernel/magic.c @@ -389,6 +389,14 @@ attrib_type at_seenspell = { "seenspell", NULL, NULL, NULL, write_seenspell, read_seenspell }; +#define MAXSPELLS 256 + +static boolean has_spell(quicklist * ql, const spell * sp) +{ + int qi; + return ql_set_find(&ql, &qi, sp) != 0; +} + static boolean already_seen(const faction * f, const spell * sp) { attrib *a; @@ -401,14 +409,6 @@ static boolean already_seen(const faction * f, const spell * sp) return false; } -#define MAXSPELLS 256 - -static boolean has_spell(quicklist * ql, const spell * sp) -{ - int qi; - return ql_set_find(&ql, &qi, sp) != 0; -} - static void update_spells(faction * f, sc_mage * mage, int level, const spellbook *book) { boolean ismonster = is_monsters(f); @@ -448,6 +448,7 @@ void updatespelllist(unit * u) if (mage->magietyp != M_GRAY) { spellbook * book; book = get_spellbook(magic_school[mage->magietyp]); + update_spells(u->faction, mage, sk, book); if (FactionSpells()) { @@ -489,11 +490,11 @@ void pick_random_spells(faction * f, int level, spellbook * book, int num_spells spellno = rng_int() % maxspell; sp = commonspells[spellno]; if (sp->level>f->max_spelllevel) { - commonspells[spellno] = commonspells[maxspell]; - commonspells[maxspell--] = sp; + commonspells[spellno] = commonspells[--maxspell]; + commonspells[maxspell] = sp; sp = 0; } else if (f->spellbook && spellbook_get(f->spellbook, sp)) { - commonspells[spellno] = commonspells[numspells--]; + commonspells[spellno] = commonspells[--numspells]; if (maxspell>numspells) { maxspell = numspells; } @@ -506,7 +507,7 @@ void pick_random_spells(faction * f, int level, spellbook * book, int num_spells f->spellbook = create_spellbook(0); } spellbook_add(f->spellbook, sp, f->max_spelllevel); - commonspells[spellno] = commonspells[numspells--]; + commonspells[spellno] = commonspells[--numspells]; } } }