slow but working solution for getspell. rebuilding that radix tree for every call is insanity.

This commit is contained in:
Enno Rehling 2012-05-26 09:05:03 -07:00
parent 998549e687
commit 8619bd74a8
6 changed files with 30 additions and 43 deletions

View File

@ -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;

View File

@ -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. */

View File

@ -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);

View File

@ -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)

View File

@ -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);
}

View File

@ -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)