added a test for pay_spell so I don't break it.

This commit is contained in:
Enno Rehling 2012-05-25 12:46:00 -07:00
parent f5b35a9a2b
commit ae9f12c904
5 changed files with 76 additions and 26 deletions

View File

@ -483,27 +483,20 @@ static int tolua_unit_castspell(lua_State * L)
return 0; 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) 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); 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); lua_pushinteger(L, err);
return 1; return 1;
} }

View File

@ -130,29 +130,25 @@ void equip_unit_mask(struct unit *u, const struct equipment *eq, int mask)
if (eq) { if (eq) {
if (mask & EQUIP_SKILLS) { if (mask & EQUIP_SKILLS) {
skill_t sk; int sk;
for (sk = 0; sk != MAXSKILLS; ++sk) { for (sk = 0; sk != MAXSKILLS; ++sk) {
if (eq->skills[sk] != NULL) { if (eq->skills[sk] != NULL) {
int i = dice_rand(eq->skills[sk]); int i = dice_rand(eq->skills[sk]);
if (i > 0) if (i > 0)
set_level(u, sk, i); set_level(u, (skill_t)sk, i);
} }
} }
} }
if (mask & EQUIP_SPELLS) { if (mask & EQUIP_SPELLS) {
if (eq->spellbook) { if (eq->spellbook) {
sc_mage *m = get_mage(u);
quicklist * ql = eq->spellbook->spells; quicklist * ql = eq->spellbook->spells;
int qi; 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)) { for (qi = 0; ql; ql_advance(&ql, &qi, 1)) {
spellbook_entry *sbe = (spellbook_entry *) ql_get(ql, qi); spellbook_entry *sbe = (spellbook_entry *) ql_get(ql, qi);
add_spell(&m->spells, sbe->sp); unit_add_spell(u, mage, sbe->sp);
add_spellname(m, sbe->sp);
} }
} }
} }

View File

@ -2,9 +2,13 @@
#include <kernel/types.h> #include <kernel/types.h>
#include <kernel/faction.h> #include <kernel/faction.h>
#include <kernel/item.h>
#include <kernel/magic.h> #include <kernel/magic.h>
#include <kernel/region.h>
#include <kernel/spell.h> #include <kernel/spell.h>
#include <kernel/spellbook.h> #include <kernel/spellbook.h>
#include <kernel/unit.h>
#include <kernel/pool.h>
#include <util/quicklist.h> #include <util/quicklist.h>
#include <util/language.h> #include <util/language.h>
@ -64,10 +68,52 @@ void test_spellbooks(CuTest * tc)
/* CuAssertPtrEquals(tc, 0, spellbook_get(herp, sname)); */ /* 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 *get_magic_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_updatespells); SUITE_ADD_TEST(suite, test_updatespells);
SUITE_ADD_TEST(suite, test_spellbooks); SUITE_ADD_TEST(suite, test_spellbooks);
SUITE_ADD_TEST(suite, test_pay_spell);
return suite; return suite;
} }

View File

@ -30,6 +30,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "plane.h" #include "plane.h"
#include "race.h" #include "race.h"
#include "region.h" #include "region.h"
#include "spell.h"
#include "save.h" #include "save.h"
#include "ship.h" #include "ship.h"
#include "skill.h" #include "skill.h"
@ -1724,3 +1725,15 @@ const struct race *u_irace(const struct unit *u)
} }
return u->race; 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);
}

View File

@ -231,6 +231,8 @@ extern "C" {
int unit_max_hp(const struct unit *u); int unit_max_hp(const struct unit *u);
void scale_number(struct unit *u, int n); 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; extern struct attrib_type at_creator;
#ifdef __cplusplus #ifdef __cplusplus
} }