From ae9f12c904f3e4154b82d5219fa60e58d71520e8 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 25 May 2012 12:46:00 -0700 Subject: [PATCH] added a test for pay_spell so I don't break it. --- src/bindings/bind_unit.c | 29 ++++++++++--------------- src/kernel/equipment.c | 12 ++++------- src/kernel/magic_test.c | 46 ++++++++++++++++++++++++++++++++++++++++ src/kernel/unit.c | 13 ++++++++++++ src/kernel/unit.h | 2 ++ 5 files changed, 76 insertions(+), 26 deletions(-) diff --git a/src/bindings/bind_unit.c b/src/bindings/bind_unit.c index 1184617d8..e5fc11bf9 100644 --- a/src/bindings/bind_unit.c +++ b/src/bindings/bind_unit.c @@ -483,27 +483,20 @@ static int tolua_unit_castspell(lua_State * L) return 0; } -static int unit_addspell(unit * u, const char *name) -{ - sc_mage *m = get_mage(u); - spell *spadd = find_spell(name); - - if (!spadd) { - log_error("spell %s could not be found\n", name); - return EINVAL; - } else { - quicklist **starget = get_spelllist(m, u->faction); - add_spell(starget, spadd); - add_spellname(m, spadd); - } - return 0; -} - static int tolua_unit_addspell(lua_State * L) { - unit *self = (unit *) tolua_tousertype(L, 1, 0); + unit *u = (unit *) tolua_tousertype(L, 1, 0); const char *str = tolua_tostring(L, 2, 0); - int err = unit_addspell(self, str); + int err = 0; + spell *sp = find_spell(str); + + if (!sp) { + log_error("spell %s could not be found\n", str); + return EINVAL; + } else { + unit_add_spell(u, 0, sp); + } + lua_pushinteger(L, err); return 1; } diff --git a/src/kernel/equipment.c b/src/kernel/equipment.c index 44e275ce6..4e91cac81 100644 --- a/src/kernel/equipment.c +++ b/src/kernel/equipment.c @@ -130,29 +130,25 @@ void equip_unit_mask(struct unit *u, const struct equipment *eq, int mask) if (eq) { if (mask & EQUIP_SKILLS) { - skill_t sk; + int sk; for (sk = 0; sk != MAXSKILLS; ++sk) { if (eq->skills[sk] != NULL) { int i = dice_rand(eq->skills[sk]); if (i > 0) - set_level(u, sk, i); + set_level(u, (skill_t)sk, i); } } } if (mask & EQUIP_SPELLS) { if (eq->spellbook) { - sc_mage *m = get_mage(u); quicklist * ql = eq->spellbook->spells; int qi; + sc_mage * mage = get_mage(u); - if (!m) { - m = create_mage(u, u->faction?u->faction->magiegebiet:M_GRAY); - } for (qi = 0; ql; ql_advance(&ql, &qi, 1)) { spellbook_entry *sbe = (spellbook_entry *) ql_get(ql, qi); - add_spell(&m->spells, sbe->sp); - add_spellname(m, sbe->sp); + unit_add_spell(u, mage, sbe->sp); } } } diff --git a/src/kernel/magic_test.c b/src/kernel/magic_test.c index ed6f1e684..4c41ef3e6 100644 --- a/src/kernel/magic_test.c +++ b/src/kernel/magic_test.c @@ -2,9 +2,13 @@ #include #include +#include #include +#include #include #include +#include +#include #include #include @@ -64,10 +68,52 @@ void test_spellbooks(CuTest * tc) /* CuAssertPtrEquals(tc, 0, spellbook_get(herp, sname)); */ } +void test_pay_spell(CuTest * tc) +{ + spell *sp; + unit * u; + faction * f; + region * r; + + test_cleanup(); + test_create_world(); + r = findregion(0, 0); + f = test_create_faction(0); + u = test_create_unit(f, r); + CuAssertPtrNotNull(tc, u); + + sp = create_spell("testspell", 0); + CuAssertPtrNotNull(tc, sp); + + sp->components = (spell_component *) calloc(4, sizeof(spell_component)); + sp->components[0].amount = 1; + sp->components[0].type = rt_find("money"); + sp->components[0].cost = SPC_FIX; + sp->components[1].amount = 1; + sp->components[1].type = rt_find("aura"); + sp->components[1].cost = SPC_LEVEL; + sp->components[2].amount = 1; + sp->components[2].type = rt_find("horse"); + sp->components[2].cost = SPC_LINEAR; + + set_level(u, SK_MAGIC, 5); + unit_add_spell(u, 0, sp); + + change_resource(u, rt_find("money"), 1); /* fix costs of 1 money */ + change_resource(u, rt_find("aura"), 3); /* leveled costs of 3 money */ + change_resource(u, rt_find("horse"), 3); /* leveled costs of 3 money */ + + pay_spell(u, sp, 3, 1); + CuAssertIntEquals(tc, 0, get_resource(u, rt_find("money"))); + CuAssertIntEquals(tc, 0, get_resource(u, rt_find("aura"))); + CuAssertIntEquals(tc, 0, get_resource(u, rt_find("horse"))); +} + CuSuite *get_magic_suite(void) { CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_updatespells); SUITE_ADD_TEST(suite, test_spellbooks); + SUITE_ADD_TEST(suite, test_pay_spell); return suite; } diff --git a/src/kernel/unit.c b/src/kernel/unit.c index d868627ea..d7315df8d 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -30,6 +30,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include "plane.h" #include "race.h" #include "region.h" +#include "spell.h" #include "save.h" #include "ship.h" #include "skill.h" @@ -1724,3 +1725,15 @@ const struct race *u_irace(const struct unit *u) } return u->race; } + +void unit_add_spell(unit * u, sc_mage * m, struct spell * sp) +{ + sc_mage *mage = m ? m : get_mage(u); + + if (!mage) { + log_debug("adding new spell %s to a previously non-mage unit %s\n", sp->sname, unitname(u)); + mage = create_mage(u, u->faction?u->faction->magiegebiet:M_GRAY); + } + add_spell(&mage->spells, sp); + add_spellname(mage, sp); +} diff --git a/src/kernel/unit.h b/src/kernel/unit.h index 1aab73569..cb0ab5f68 100644 --- a/src/kernel/unit.h +++ b/src/kernel/unit.h @@ -231,6 +231,8 @@ extern "C" { int unit_max_hp(const struct unit *u); void scale_number(struct unit *u, int n); + void unit_add_spell(struct unit * u, struct sc_mage * m, struct spell * sp); + extern struct attrib_type at_creator; #ifdef __cplusplus }