diff --git a/src/kernel/magic.c b/src/kernel/magic.c index 7155d37de..5cc8bba0d 100644 --- a/src/kernel/magic.c +++ b/src/kernel/magic.c @@ -507,18 +507,6 @@ sc_mage *create_mage(unit * u, magic_t mtyp) /* ------------------------------------------------------------- */ /* Funktionen für die Bearbeitung der List-of-known-spells */ -void add_spellname(sc_mage * mage, const spell * sp) -{ - spell_names * names = mage->spellnames; - while (names) { - variant token; - const char *n = spell_name(sp, names->lang); - token.v = (void *)sp; - addtoken(&names->tokens, n, token); - names = names->next; - } -} - int u_hasspell(const unit *u, const struct spell *sp) { spellbook * book = unit_get_spellbook(u); @@ -2877,37 +2865,32 @@ spell *unit_getspell(struct unit *u, const char *name, const struct locale * lan sc_mage * mage = get_mage(u); if (mage) { variant token; - struct spell_names * names = mage->spellnames; - for (;names;names=names->next) { - if (names->lang==lang) break; - } - if (!names) { - spellbook *sb = unit_get_spellbook(u); - if (sb) { - quicklist * ql; - int qi; - names = (spell_names *)calloc(1, sizeof(spell_names)); - names->next = mage->spellnames; - names->lang = lang; - names->tokens = 0; + void * tokens = 0; + spellbook *sb = unit_get_spellbook(u); - for (qi = 0, ql = sb->spells; ql; ql_advance(&ql, &qi, 1)) { - spellbook_entry *sbe = (spellbook_entry *)ql_get(ql, qi); - spell *sp = sbe->sp; - const char *n = spell_name(sp, lang); - if (!n) { - log_error("no translation in locale %s for spell %s\n", locale_name(lang), sp->sname); - } else { - token.v = sp; - addtoken(&names->tokens, n, token); - } + if (sb) { + quicklist * ql; + int qi; + + for (qi = 0, ql = sb->spells; ql; ql_advance(&ql, &qi, 1)) { + spellbook_entry *sbe = (spellbook_entry *)ql_get(ql, qi); + spell *sp = sbe->sp; + const char *n = spell_name(sp, lang); + if (!n) { + log_error("no translation in locale %s for spell %s\n", locale_name(lang), sp->sname); + } else { + token.v = sp; + addtoken(&tokens, n, token); } - mage->spellnames = names; } } - if (names && findtoken(names->tokens, name, &token) != E_TOK_NOMATCH) { - return (spell *) token.v; + if (tokens) { + if (findtoken(tokens, name, &token) != E_TOK_NOMATCH) { + freetokens(tokens); + return (spell *) token.v; + } + freetokens(tokens); } } return 0; diff --git a/src/kernel/magic.h b/src/kernel/magic.h index 0ac519711..f896be52d 100644 --- a/src/kernel/magic.h +++ b/src/kernel/magic.h @@ -118,7 +118,6 @@ typedef struct sc_mage { int spellcount; combatspell combatspells[MAXCOMBATSPELLS]; struct spellbook *spellbook; - struct spell_names * spellnames; } sc_mage; /* ------------------------------------------------------------- */ @@ -250,7 +249,6 @@ typedef struct sc_mage { /* setzt Kampfzauber */ void unset_combatspell(struct unit *u, struct spell * sp); /* löscht Kampfzauber */ - void add_spellname(sc_mage * mage, const struct spell * sp); /* fügt den Spruch mit der Id spellid der Spruchliste der Einheit hinzu. */ int u_hasspell(const struct unit *u, const struct spell *sp); /* prüft, ob der Spruch in der Spruchliste der Einheit steht. */ diff --git a/src/kernel/magic_test.c b/src/kernel/magic_test.c index 42e05a94c..9bcc1d117 100644 --- a/src/kernel/magic_test.c +++ b/src/kernel/magic_test.c @@ -180,6 +180,7 @@ void test_getspell_unit(CuTest * tc) r = findregion(0, 0); f = test_create_faction(0); u = test_create_unit(f, r); + create_mage(u, M_GRAY); skill_enabled[SK_MAGIC] = 1; set_level(u, SK_MAGIC, 1); @@ -206,7 +207,9 @@ void test_getspell_faction(CuTest * tc) test_create_world(); r = findregion(0, 0); f = test_create_faction(0); + f->magiegebiet = M_TYBIED; u = test_create_unit(f, r); + create_mage(u, f->magiegebiet); skill_enabled[SK_MAGIC] = 1; set_level(u, SK_MAGIC, 1); @@ -237,6 +240,7 @@ void test_getspell_school(CuTest * tc) f = test_create_faction(0); f->magiegebiet = M_TYBIED; u = test_create_unit(f, r); + create_mage(u, f->magiegebiet); skill_enabled[SK_MAGIC] = 1; set_level(u, SK_MAGIC, 1); diff --git a/src/kernel/unit.c b/src/kernel/unit.c index 1697daf2b..4131e317b 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -1740,7 +1740,6 @@ void unit_add_spell(unit * u, sc_mage * m, struct spell * sp, int level) mage->spellbook = create_spellbook(0); } spellbook_add(mage->spellbook, sp, level); - add_spellname(mage, sp); } struct spellbook * unit_get_spellbook(const struct unit * u) diff --git a/src/util/umlaut.c b/src/util/umlaut.c index 443decfbd..256ec2230 100644 --- a/src/util/umlaut.c +++ b/src/util/umlaut.c @@ -212,7 +212,9 @@ void freetokens(void * root) tnode * node = (tnode *)root; int i; for (i=0;node && i!=NODEHASHSIZE;++i) { - freetokens(node->next[i]->node); + if (node->next[i]) { + freetokens(node->next[i]->node); + } } free(node); } diff --git a/src/util/umlaut_test.c b/src/util/umlaut_test.c index 518c63930..7a70ccd5d 100644 --- a/src/util/umlaut_test.c +++ b/src/util/umlaut_test.c @@ -33,7 +33,6 @@ static void test_umlaut(CuTest * tc) variant id; int result; - memset(&tokens, 0, sizeof(tokens)); /* don't crash on an empty set */ result = findtoken(tokens, "herpderp", &id); CuAssertIntEquals(tc, E_TOK_NOMATCH, result); @@ -65,6 +64,8 @@ static void test_umlaut(CuTest * tc) result = findtoken(tokens, "herp-a-derp", &id); CuAssertIntEquals(tc, E_TOK_NOMATCH, result); + + freetokens(tokens); } CuSuite *get_umlaut_suite(void)