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 */ /* 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) int u_hasspell(const unit *u, const struct spell *sp)
{ {
spellbook * book = unit_get_spellbook(u); 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); sc_mage * mage = get_mage(u);
if (mage) { if (mage) {
variant token; variant token;
struct spell_names * names = mage->spellnames; void * tokens = 0;
for (;names;names=names->next) { spellbook *sb = unit_get_spellbook(u);
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;
for (qi = 0, ql = sb->spells; ql; ql_advance(&ql, &qi, 1)) { if (sb) {
spellbook_entry *sbe = (spellbook_entry *)ql_get(ql, qi); quicklist * ql;
spell *sp = sbe->sp; int qi;
const char *n = spell_name(sp, lang);
if (!n) { for (qi = 0, ql = sb->spells; ql; ql_advance(&ql, &qi, 1)) {
log_error("no translation in locale %s for spell %s\n", locale_name(lang), sp->sname); spellbook_entry *sbe = (spellbook_entry *)ql_get(ql, qi);
} else { spell *sp = sbe->sp;
token.v = sp; const char *n = spell_name(sp, lang);
addtoken(&names->tokens, n, token); 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) { if (tokens) {
return (spell *) token.v; if (findtoken(tokens, name, &token) != E_TOK_NOMATCH) {
freetokens(tokens);
return (spell *) token.v;
}
freetokens(tokens);
} }
} }
return 0; return 0;

View File

@ -118,7 +118,6 @@ typedef struct sc_mage {
int spellcount; int spellcount;
combatspell combatspells[MAXCOMBATSPELLS]; combatspell combatspells[MAXCOMBATSPELLS];
struct spellbook *spellbook; struct spellbook *spellbook;
struct spell_names * spellnames;
} sc_mage; } sc_mage;
/* ------------------------------------------------------------- */ /* ------------------------------------------------------------- */
@ -250,7 +249,6 @@ typedef struct sc_mage {
/* setzt Kampfzauber */ /* setzt Kampfzauber */
void unset_combatspell(struct unit *u, struct spell * sp); void unset_combatspell(struct unit *u, struct spell * sp);
/* löscht Kampfzauber */ /* 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. */ /* fügt den Spruch mit der Id spellid der Spruchliste der Einheit hinzu. */
int u_hasspell(const struct unit *u, const struct spell *sp); int u_hasspell(const struct unit *u, const struct spell *sp);
/* prüft, ob der Spruch in der Spruchliste der Einheit steht. */ /* 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); r = findregion(0, 0);
f = test_create_faction(0); f = test_create_faction(0);
u = test_create_unit(f, r); u = test_create_unit(f, r);
create_mage(u, M_GRAY);
skill_enabled[SK_MAGIC] = 1; skill_enabled[SK_MAGIC] = 1;
set_level(u, SK_MAGIC, 1); set_level(u, SK_MAGIC, 1);
@ -206,7 +207,9 @@ void test_getspell_faction(CuTest * tc)
test_create_world(); test_create_world();
r = findregion(0, 0); r = findregion(0, 0);
f = test_create_faction(0); f = test_create_faction(0);
f->magiegebiet = M_TYBIED;
u = test_create_unit(f, r); u = test_create_unit(f, r);
create_mage(u, f->magiegebiet);
skill_enabled[SK_MAGIC] = 1; skill_enabled[SK_MAGIC] = 1;
set_level(u, 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 = test_create_faction(0);
f->magiegebiet = M_TYBIED; f->magiegebiet = M_TYBIED;
u = test_create_unit(f, r); u = test_create_unit(f, r);
create_mage(u, f->magiegebiet);
skill_enabled[SK_MAGIC] = 1; skill_enabled[SK_MAGIC] = 1;
set_level(u, 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); mage->spellbook = create_spellbook(0);
} }
spellbook_add(mage->spellbook, sp, level); spellbook_add(mage->spellbook, sp, level);
add_spellname(mage, sp);
} }
struct spellbook * unit_get_spellbook(const struct unit * u) struct spellbook * unit_get_spellbook(const struct unit * u)

View File

@ -212,7 +212,9 @@ void freetokens(void * root)
tnode * node = (tnode *)root; tnode * node = (tnode *)root;
int i; int i;
for (i=0;node && i!=NODEHASHSIZE;++i) { for (i=0;node && i!=NODEHASHSIZE;++i) {
freetokens(node->next[i]->node); if (node->next[i]) {
freetokens(node->next[i]->node);
}
} }
free(node); free(node);
} }

View File

@ -33,7 +33,6 @@ static void test_umlaut(CuTest * tc)
variant id; variant id;
int result; int result;
memset(&tokens, 0, sizeof(tokens));
/* don't crash on an empty set */ /* don't crash on an empty set */
result = findtoken(tokens, "herpderp", &id); result = findtoken(tokens, "herpderp", &id);
CuAssertIntEquals(tc, E_TOK_NOMATCH, result); CuAssertIntEquals(tc, E_TOK_NOMATCH, result);
@ -65,6 +64,8 @@ static void test_umlaut(CuTest * tc)
result = findtoken(tokens, "herp-a-derp", &id); result = findtoken(tokens, "herp-a-derp", &id);
CuAssertIntEquals(tc, E_TOK_NOMATCH, result); CuAssertIntEquals(tc, E_TOK_NOMATCH, result);
freetokens(tokens);
} }
CuSuite *get_umlaut_suite(void) CuSuite *get_umlaut_suite(void)