Lua unit:cast_spell should only cast spells the unit knows.

do not allow payment for spells cast at level <= 0
This commit is contained in:
Enno Rehling 2012-05-29 20:50:20 -07:00
parent 44e5c60c96
commit 3e67e7fb4e
3 changed files with 29 additions and 19 deletions

View file

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

View file

@ -32,6 +32,7 @@ without prior permission by the authors of Eressea.
#include <kernel/pool.h>
#include <kernel/race.h>
#include <kernel/region.h>
#include <kernel/spellbook.h>
#include <kernel/ship.h>
#include <kernel/skill.h>
#include <kernel/spell.h>
@ -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 {

View file

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