From a015d72adda122a023e3e243606177e0e980932a Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 25 May 2012 21:07:26 -0700 Subject: [PATCH] unit_add_spell gets a level added a test that fails for get_spellfromtoken if spell is not stored with the unit --- src/bindings/bind_unit.c | 3 +- src/gamecode/laws.c | 12 ++----- src/kernel/equipment.c | 2 +- src/kernel/magic.c | 11 ++----- src/kernel/magic_test.c | 68 +++++++++++++++++++++++++++++++++++++--- src/kernel/spell.c | 51 +++++++++++++++++------------- src/kernel/spell.h | 2 +- src/kernel/unit.c | 2 +- src/kernel/unit.h | 2 +- 9 files changed, 103 insertions(+), 50 deletions(-) diff --git a/src/bindings/bind_unit.c b/src/bindings/bind_unit.c index e5fc11bf9..79f2660e5 100644 --- a/src/bindings/bind_unit.c +++ b/src/bindings/bind_unit.c @@ -487,6 +487,7 @@ static int tolua_unit_addspell(lua_State * L) { unit *u = (unit *) tolua_tousertype(L, 1, 0); const char *str = tolua_tostring(L, 2, 0); + int level = (int)tolua_tonumber(L, 3, 1); int err = 0; spell *sp = find_spell(str); @@ -494,7 +495,7 @@ static int tolua_unit_addspell(lua_State * L) log_error("spell %s could not be found\n", str); return EINVAL; } else { - unit_add_spell(u, 0, sp); + unit_add_spell(u, 0, sp, level); } lua_pushinteger(L, err); diff --git a/src/gamecode/laws.c b/src/gamecode/laws.c index 9fdfd964a..8b0875afc 100644 --- a/src/gamecode/laws.c +++ b/src/gamecode/laws.c @@ -2427,7 +2427,6 @@ static void reshow(unit * u, struct order *ord, const char *s, param_t p) const item_type *itype; const spell *sp = 0; const race *rc; - sc_mage * mage; switch (p) { case P_ZAUBER: @@ -2458,10 +2457,7 @@ static void reshow(unit * u, struct order *ord, const char *s, param_t p) } } /* try for a spell */ - mage = get_mage(u); - if (mage) { - sp = get_spellfromtoken(mage, s, u->faction->locale); - } + sp = get_spellfromtoken(u, s, u->faction->locale); if (sp) { attrib *a = a_find(u->faction->attribs, &at_seenspell); while (a != NULL && a->type == &at_seenspell && a->data.v != sp) { @@ -2626,7 +2622,6 @@ static int combatspell_cmd(unit * u, struct order *ord) const char *s; int level = 0; spell *sp = 0; - sc_mage * mage; init_tokens(ord); skip_token(); @@ -2646,10 +2641,7 @@ static int combatspell_cmd(unit * u, struct order *ord) s = getstrtoken(); } - mage = get_mage(u); - if (mage) { - sp = get_spellfromtoken(mage, s, u->faction->locale); - } + sp = get_spellfromtoken(u, s, u->faction->locale); if (!sp) { cmistake(u, ord, 173, MSG_MAGIC); return 0; diff --git a/src/kernel/equipment.c b/src/kernel/equipment.c index 4e91cac81..2ef21717c 100644 --- a/src/kernel/equipment.c +++ b/src/kernel/equipment.c @@ -148,7 +148,7 @@ void equip_unit_mask(struct unit *u, const struct equipment *eq, int mask) for (qi = 0; ql; ql_advance(&ql, &qi, 1)) { spellbook_entry *sbe = (spellbook_entry *) ql_get(ql, qi); - unit_add_spell(u, mage, sbe->sp); + unit_add_spell(u, mage, sbe->sp, sbe->level); } } } diff --git a/src/kernel/magic.c b/src/kernel/magic.c index f1a857053..17dbd1218 100644 --- a/src/kernel/magic.c +++ b/src/kernel/magic.c @@ -2526,7 +2526,6 @@ static castorder *cast_cmd(unit * u, order * ord) plane *pl; spellparameter *args = NULL; unit * caster = u; - sc_mage * mage; if (LongHunger(u)) { cmistake(u, ord, 224, MSG_MAGIC); @@ -2587,10 +2586,7 @@ static castorder *cast_cmd(unit * u, order * ord) return 0; } - mage = get_mage(u); - if (mage) { - sp = get_spellfromtoken(mage, s, u->faction->locale); - } + sp = get_spellfromtoken(u, s, u->faction->locale); /* Vertraute können auch Zauber sprechen, die sie selbst nicht * können. get_spellfromtoken findet aber nur jene Sprüche, die @@ -2598,9 +2594,8 @@ static castorder *cast_cmd(unit * u, order * ord) if (!sp && is_familiar(u)) { caster = get_familiar_mage(u); if (caster) { - mage = get_mage(caster); familiar = u; - sp = get_spellfromtoken(mage, s, caster->faction->locale); + sp = get_spellfromtoken(caster, s, caster->faction->locale); } else { /* somehow, this familiar has no mage! */ log_error("cast_cmd: familiar %s is without a mage?\n", unitname(u)); @@ -2693,7 +2688,7 @@ static castorder *cast_cmd(unit * u, order * ord) if (caster != familiar) { /* Magier zaubert durch Vertrauten */ if (range > 1) { /* Fehler! Versucht zu Farcasten */ ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "familiar_farcast", - "mage", mage)); + "mage", caster)); return 0; } if (distance(caster->region, r) > eff_skill(caster, SK_MAGIC, caster->region)) { diff --git a/src/kernel/magic_test.c b/src/kernel/magic_test.c index b05c174b7..df343ebd1 100644 --- a/src/kernel/magic_test.c +++ b/src/kernel/magic_test.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -105,7 +106,7 @@ void test_pay_spell(CuTest * tc) CuAssertPtrNotNull(tc, sp); set_level(u, SK_MAGIC, 5); - unit_add_spell(u, 0, sp); + unit_add_spell(u, 0, sp, 1); change_resource(u, rt_find("money"), 1); change_resource(u, rt_find("aura"), 3); @@ -122,9 +123,9 @@ void test_pay_spell(CuTest * tc) void test_pay_spell_failure(CuTest * tc) { spell *sp; - unit * u; - faction * f; - region * r; + struct unit * u; + struct faction * f; + struct region * r; int level; test_cleanup(); @@ -138,7 +139,7 @@ void test_pay_spell_failure(CuTest * tc) CuAssertPtrNotNull(tc, sp); set_level(u, SK_MAGIC, 5); - unit_add_spell(u, 0, sp); + unit_add_spell(u, 0, sp, 1); CuAssertIntEquals(tc, 1, change_resource(u, rt_find("money"), 1)); CuAssertIntEquals(tc, 2, change_resource(u, rt_find("aura"), 2)); @@ -166,6 +167,61 @@ void test_pay_spell_failure(CuTest * tc) CuAssertIntEquals(tc, 2, get_resource(u, rt_find("horse"))); } +void test_get_spellfromtoken_unit(CuTest * tc) +{ + spell *sp; + struct unit * u; + struct faction * f; + struct region * r; + struct locale * lang; + + test_cleanup(); + test_create_world(); + r = findregion(0, 0); + f = test_create_faction(0); + u = test_create_unit(f, r); + skill_enabled[SK_MAGIC] = 1; + + set_level(u, SK_MAGIC, 1); + + lang = find_locale("de"); + sp = create_spell("testspell", 0); + locale_setstring(lang, mkname("spell", sp->sname), "Herp-a-derp"); + + CuAssertPtrEquals(tc, 0, get_spellfromtoken(u, "Herp-a-derp", lang)); + + unit_add_spell(u, 0, sp, 1); + CuAssertPtrNotNull(tc, get_spellfromtoken(u, "Herp-a-derp", lang)); +} + +void test_get_spellfromtoken_faction(CuTest * tc) +{ + spell *sp; + struct unit * u; + struct faction * f; + struct region * r; + struct locale * lang; + + test_cleanup(); + test_create_world(); + r = findregion(0, 0); + f = test_create_faction(0); + u = test_create_unit(f, r); + skill_enabled[SK_MAGIC] = 1; + + set_level(u, SK_MAGIC, 1); + + lang = find_locale("de"); + sp = create_spell("testspell", 0); + locale_setstring(lang, mkname("spell", sp->sname), "Herp-a-derp"); + + CuAssertPtrEquals(tc, 0, get_spellfromtoken(u, "Herp-a-derp", lang)); + + f->spellbook = create_spellbook(0); + spellbook_add(f->spellbook, sp, 1); + CuAssertPtrNotNull(tc, get_spellfromtoken(u, "Herp-a-derp", lang)); +} + CuSuite *get_magic_suite(void) { CuSuite *suite = CuSuiteNew(); @@ -173,5 +229,7 @@ CuSuite *get_magic_suite(void) SUITE_ADD_TEST(suite, test_spellbooks); SUITE_ADD_TEST(suite, test_pay_spell); SUITE_ADD_TEST(suite, test_pay_spell_failure); + SUITE_ADD_TEST(suite, test_get_spellfromtoken_unit); + SUITE_ADD_TEST(suite, test_get_spellfromtoken_faction); return suite; } diff --git a/src/kernel/spell.c b/src/kernel/spell.c index e674796a5..5af6793f4 100644 --- a/src/kernel/spell.c +++ b/src/kernel/spell.c @@ -104,32 +104,39 @@ spell *find_spell(const char *name) /* ------------------------------------------------------------- */ /* Spruch identifizieren */ -spell *get_spellfromtoken(sc_mage *mage, const char *name, +spell *get_spellfromtoken(struct unit *u, const char *name, const struct locale * lang) { - variant token; - struct spell_names * names = mage->spellnames; - for (;names;names=names->next) { - if (names->lang==lang) break; - } - if (!names) { - quicklist *ql = mage->spells; - int qi; - names = (spell_names *)calloc(1, sizeof(spell_names)); - names->next = mage->spellnames; - names->lang = lang; - names->tokens = 0; - for (qi = 0, ql = mage->spells; ql; ql_advance(&ql, &qi, 1)) { - spell *sp = (spell *) ql_get(ql, qi); - const char *n = spell_name(sp, lang); - token.v = sp; - addtoken(&names->tokens, n, token); + 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) { + quicklist *ql = mage->spells; + int qi; + names = (spell_names *)calloc(1, sizeof(spell_names)); + names->next = mage->spellnames; + names->lang = lang; + names->tokens = 0; + for (qi = 0, ql = mage->spells; ql; ql_advance(&ql, &qi, 1)) { + spell *sp = (spell *) ql_get(ql, qi); + const char *n = spell_name(sp, lang); + if (!n) { + log_error("no translation in locae %s for spell $s\n", locale_name(lang), sp->sname); + } else { + token.v = sp; + addtoken(&names->tokens, n, token); + } + } + mage->spellnames = names; } - mage->spellnames = names; - } - if (findtoken(names->tokens, name, &token) != E_TOK_NOMATCH) { - return (spell *) token.v; + if (findtoken(names->tokens, name, &token) != E_TOK_NOMATCH) { + return (spell *) token.v; + } } return 0; } diff --git a/src/kernel/spell.h b/src/kernel/spell.h index 226de96e4..627a855d6 100644 --- a/src/kernel/spell.h +++ b/src/kernel/spell.h @@ -37,7 +37,7 @@ extern "C" { extern struct spell * create_spell(const char * name, unsigned int id); extern struct spell * find_spell(const char *name); extern struct spell * find_spellbyid(unsigned int i); - extern struct spell * get_spellfromtoken(struct sc_mage *mage, const char *s, + extern struct spell * get_spellfromtoken(struct unit *u, const char *s, const struct locale *lang); extern void free_spells(void); diff --git a/src/kernel/unit.c b/src/kernel/unit.c index f4b58fd59..6e19710f1 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -1726,7 +1726,7 @@ const struct race *u_irace(const struct unit *u) return u->race; } -void unit_add_spell(unit * u, sc_mage * m, struct spell * sp) +void unit_add_spell(unit * u, sc_mage * m, struct spell * sp, int level) { sc_mage *mage = m ? m : get_mage(u); diff --git a/src/kernel/unit.h b/src/kernel/unit.h index d75a85aa9..93dc55ce7 100644 --- a/src/kernel/unit.h +++ b/src/kernel/unit.h @@ -234,7 +234,7 @@ extern "C" { void scale_number(struct unit *u, int n); struct spellbook * unit_get_spellbook(struct unit * u); - void unit_add_spell(struct unit * u, struct sc_mage * m, struct spell * sp); + void unit_add_spell(struct unit * u, struct sc_mage * m, struct spell * sp, int level); extern struct attrib_type at_creator; #ifdef __cplusplus