From 3e67e7fb4e1d0e6c1036cf4a6b2644e0c8c49790 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 29 May 2012 20:50:20 -0700 Subject: [PATCH] Lua unit:cast_spell should only cast spells the unit knows. do not allow payment for spells cast at level <= 0 --- scripts/tests/spells.lua | 22 +++++++++++++++++++++- src/bindings/bind_unit.c | 10 +++++----- src/kernel/magic_test.c | 16 +++------------- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/scripts/tests/spells.lua b/scripts/tests/spells.lua index 2d23d2efe..d8b446ace 100644 --- a/scripts/tests/spells.lua +++ b/scripts/tests/spells.lua @@ -34,8 +34,8 @@ function test_spell_not_found_no_payment() local r = region.create(0,0, "plain") local f = faction.create("spell_fails_no_payment@eressea.de", "human", "de") local u = unit.create(f, r, 1) - u:set_skill("magic", 12) u.magic = "gwyrrd" -- calls create_mage + u:set_skill("magic", 12) local permaura = u:get_pooled("permaura") u:add_item("money", 3000) @@ -55,6 +55,7 @@ function test_create_firesword() local r = region.create(0,0, "plain") local f = faction.create("create_firesword@eressea.de", "human", "de") local u = unit.create(f, r, 1) + u.magic = "gwyrrd" -- calls create_mage u:set_skill("magic", 12) u:add_spell("create_firesword") @@ -66,8 +67,10 @@ function test_create_roi() local r = region.create(0,0, "plain") local f = faction.create("create_roi@eressea.de", "human", "de") local u = unit.create(f, r, 1) + u.magic = "gwyrrd" -- calls create_mage u:set_skill("magic", 10) + u:add_spell('create_roi') u:cast_spell('create_roi') assert_equal(1, u:get_item("roi")) end @@ -76,8 +79,10 @@ function test_create_roqf() local r = region.create(0,0, "plain") local f = faction.create("create_roqf@eressea.de", "human", "de") local u = unit.create(f, r, 1) + u.magic = "gwyrrd" -- calls create_mage u:set_skill("magic", 10) + u:add_spell('create_roqf') u:cast_spell('create_roqf') assert_equal(1, u:get_item("roqf")) end @@ -86,8 +91,10 @@ function test_create_aots() local r = region.create(0,0, "plain") local f = faction.create("create_aots@eressea.de", "human", "de") local u = unit.create(f, r, 1) + u.magic = "gwyrrd" -- calls create_mage u:set_skill("magic", 10) + u:add_spell('create_aots') u:cast_spell('create_aots') assert_equal(1, u:get_item("aots")) end @@ -96,8 +103,10 @@ function test_create_ror() local r = region.create(0,0, "plain") local f = faction.create("create_ror@eressea.de", "human", "de") local u = unit.create(f, r, 1) + u.magic = "gwyrrd" -- calls create_mage u:set_skill("magic", 10) + u:add_spell('create_ror') u:cast_spell('create_ror') assert_equal(1, u:get_item("ror")) end @@ -106,8 +115,10 @@ function test_create_magicherbbag() local r = region.create(0,0, "plain") local f = faction.create("create_magicherbbag@eressea.de", "human", "de") local u = unit.create(f, r, 1) + u.magic = "gwyrrd" -- calls create_mage u:set_skill("magic", 10) + u:add_spell('create_magicherbbag') u:cast_spell('create_magicherbbag') assert_equal(1, u:get_item("magicherbbag")) end @@ -116,8 +127,10 @@ function test_create_trollbelt() local r = region.create(0,0, "plain") local f = faction.create("create_trollbelt@eressea.de", "human", "de") local u = unit.create(f, r, 1) + u.magic = "gwyrrd" -- calls create_mage u:set_skill("magic", 10) + u:add_spell('create_trollbelt') u:cast_spell('create_trollbelt') assert_equal(1, u:get_item("trollbelt")) end @@ -126,8 +139,10 @@ function test_create_dreameye() local r = region.create(0,0, "plain") local f = faction.create("create_dreameye@eressea.de", "human", "de") local u = unit.create(f, r, 1) + u.magic = "gwyrrd" -- calls create_mage u:set_skill("magic", 10) + u:add_spell('create_dreameye') u:cast_spell('create_dreameye') assert_equal(1, u:get_item("dreameye")) end @@ -136,8 +151,10 @@ function test_create_antimagic() local r = region.create(0,0, "plain") local f = faction.create("create_antimagic@eressea.de", "human", "de") local u = unit.create(f, r, 1) + u.magic = "gwyrrd" -- calls create_mage u:set_skill("magic", 10) + u:add_spell('create_antimagic') u:cast_spell('create_antimagic') assert_equal(1, u:get_item("antimagic")) end @@ -146,8 +163,10 @@ function test_create_runesword() local r = region.create(0,0, "plain") local f = faction.create("create_runesword@eressea.de", "human", "de") local u = unit.create(f, r, 1) + u.magic = "gwyrrd" -- calls create_mage u:set_skill("magic", 10) + u:add_spell('create_runesword') u:cast_spell('create_runesword') assert_equal(1, u:get_item("runesword")) end @@ -158,6 +177,7 @@ function test_create_rop() local u = unit.create(f, r, 1) u:set_skill("magic", 10) + u:add_spell('create_rop') u:cast_spell('create_rop') assert_equal(1, u:get_item("rop")) end diff --git a/src/bindings/bind_unit.c b/src/bindings/bind_unit.c index 1b4547932..50c588e78 100644 --- a/src/bindings/bind_unit.c +++ b/src/bindings/bind_unit.c @@ -32,6 +32,7 @@ without prior permission by the authors of Eressea. #include #include #include +#include #include #include #include @@ -455,12 +456,11 @@ static int tolua_unit_addnotice(lua_State * L) static void unit_castspell(unit * u, const char *name, int level) { - quicklist *ql = spells; - int qi; + spell *sp = find_spell(name); - for (ql = spells, qi = 0; ql; ql_advance(&ql, &qi, 1)) { - spell *sp = (spell *) ql_get(ql, qi); - if (strcmp(name, sp->sname) == 0) { + if (sp) { + spellbook *book = unit_get_spellbook(u); + if (spellbook_get(book, sp)) { if (!sp->cast) { log_error("spell '%s' has no function.\n", sp->sname); } else { diff --git a/src/kernel/magic_test.c b/src/kernel/magic_test.c index 9bcc1d117..20899e32d 100644 --- a/src/kernel/magic_test.c +++ b/src/kernel/magic_test.c @@ -152,19 +152,9 @@ void test_pay_spell_failure(CuTest * tc) CuAssertIntEquals(tc, 3, change_resource(u, rt_find("aura"), 3)); CuAssertIntEquals(tc, 2, change_resource(u, rt_find("horse"), 1)); - level = eff_spelllevel(u, sp, 3, 1); - CuAssertIntEquals(tc, 0, level); - pay_spell(u, sp, level, 1); - CuAssertIntEquals(tc, 0, get_resource(u, rt_find("money"))); /* seems we even pay this at level 0 */ - CuAssertIntEquals(tc, 3, get_resource(u, rt_find("aura"))); - CuAssertIntEquals(tc, 2, get_resource(u, rt_find("horse"))); - - level = eff_spelllevel(u, sp, 2, 1); - CuAssertIntEquals(tc, 0, level); - pay_spell(u, sp, level, 1); - CuAssertIntEquals(tc, 0, get_resource(u, rt_find("money"))); - CuAssertIntEquals(tc, 3, get_resource(u, rt_find("aura"))); - CuAssertIntEquals(tc, 2, get_resource(u, rt_find("horse"))); + CuAssertIntEquals(tc, 0, eff_spelllevel(u, sp, 3, 1)); + CuAssertIntEquals(tc, 0, change_resource(u, rt_find("money"), -1)); + CuAssertIntEquals(tc, 0, eff_spelllevel(u, sp, 2, 1)); } void test_getspell_unit(CuTest * tc)