unit_add_spell gets a level

added a test that fails for get_spellfromtoken if spell is not stored with the unit
This commit is contained in:
Enno Rehling 2012-05-25 21:07:26 -07:00
parent d58e853074
commit a015d72add
9 changed files with 103 additions and 50 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -5,6 +5,7 @@
#include <kernel/item.h>
#include <kernel/magic.h>
#include <kernel/region.h>
#include <kernel/skill.h>
#include <kernel/spell.h>
#include <kernel/spellbook.h>
#include <kernel/unit.h>
@ -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;
}

View File

@ -104,9 +104,11 @@ 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)
{
sc_mage * mage = get_mage(u);
if (mage) {
variant token;
struct spell_names * names = mage->spellnames;
for (;names;names=names->next) {
@ -122,15 +124,20 @@ spell *get_spellfromtoken(sc_mage *mage, const char *name,
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;
}
if (findtoken(names->tokens, name, &token) != E_TOK_NOMATCH) {
return (spell *) token.v;
}
}
return 0;
}

View File

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

View File

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

View File

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