diff --git a/scripts/tests/e2/guard.lua b/scripts/tests/e2/guard.lua new file mode 100644 index 000000000..1afd011e9 --- /dev/null +++ b/scripts/tests/e2/guard.lua @@ -0,0 +1,78 @@ +require "lunit" + +module("tests.e2.guard", package.seeall, lunit.testcase) + +function setup() + eressea.free_game() + eressea.settings.set("nmr.removenewbie", "0") + eressea.settings.set("nmr.timeout", "0") + eressea.settings.set("NewbieImmunity", "0") + eressea.settings.set("rules.economy.food", "4") +end + +function test_guard_unarmed() + local r1 = region.create(0, 0, "plain") + local f1 = faction.create("hodor@eressea.de", "human", "de") + local u1 = unit.create(f1, r1, 1) + assert_equal(nil, u1.guard) + u1:clear_orders() + u1:add_order("BEWACHE") + process_orders() + assert_equal(nil, u1.guard) +end + +function test_guard_armed() + local r1 = region.create(0, 0, "plain") + local f1 = faction.create("hodor@eressea.de", "human", "de") + local u1 = unit.create(f1, r1, 1) + assert_equal(nil, u1.guard) + u1:add_item("sword", 1) + u1:set_skill("melee", 2) + u1:clear_orders() + u1:add_order("BEWACHE") + process_orders() + assert_equal(249, u1.guard) +end + +function test_guard_allows_move_after_combat() -- bug 1493 + local r1 = region.create(0, 0, "plain") + local r2 = region.create(1, 0, "plain") + local f1 = faction.create("bernd@eressea.de", "human", "de") + local u1 = unit.create(f1, r1, 10) + local uid1 = u1.id + local f2 = faction.create("horst@eressea.de", "human", "de") + local u2 = unit.create(f2, r1, 1) + u1:add_order("BEWACHE") + u1:add_item("sword", 10) + u1:set_skill("melee", 2) + u1:clear_orders() + u1:add_order("BEWACHE") + process_orders() + assert_equal(249, u1.guard) + u1:clear_orders() + u1:add_order("NACH O") + u1:add_order("ATTACKIERE " .. itoa36(u2.id)) + process_orders() + u1 = get_unit(uid1) + assert_equal(r2, u1.region) +end + +function test_no_guard_no_move_after_combat() -- bug 1493 + local r1 = region.create(0, 0, "plain") + local r2 = region.create(1, 0, "plain") + local f1 = faction.create("bernd@eressea.de", "human", "de") + local u1 = unit.create(f1, r1, 10) + local uid1 = u1.id + local f2 = faction.create("horst@eressea.de", "human", "de") + local u2 = unit.create(f2, r1, 1) + u1:add_order("BEWACHE") + u1:add_item("sword", 10) + u1:set_skill("melee", 2) + assert_equal(nil, u1.guard) + u1:clear_orders() + u1:add_order("NACH O") + u1:add_order("ATTACKIERE " .. itoa36(u2.id)) + process_orders() + u1 = get_unit(uid1) + assert_equal(r1, u1.region) +end diff --git a/scripts/tests/e2/init.lua b/scripts/tests/e2/init.lua index 33cc843ba..427a7884d 100644 --- a/scripts/tests/e2/init.lua +++ b/scripts/tests/e2/init.lua @@ -1,4 +1,4 @@ --- new tests 2015-02-13 -require 'tests.e2.shiplanding' -require 'tests.e2.e2features' -require 'tests.e2.movement' +-- require 'tests.e2.shiplanding' +-- require 'tests.e2.e2features' +-- require 'tests.e2.movement' +require 'tests.e2.guard' diff --git a/src/bind_unit.c b/src/bind_unit.c index 6c61b2beb..5e06cab37 100755 --- a/src/bind_unit.c +++ b/src/bind_unit.c @@ -262,6 +262,24 @@ static int tolua_unit_set_flags(lua_State * L) return 0; } +static int tolua_unit_get_guard(lua_State * L) +{ + unit *self = (unit *)tolua_tousertype(L, 1, 0); + if (is_guard(self, GUARD_ALL)) { + lua_pushinteger(L, getguard(self)); + return 1; + } + return 0; +} + +static int tolua_unit_set_guard(lua_State * L) +{ + unit *self = (unit *)tolua_tousertype(L, 1, 0); + unsigned int flags = (unsigned int)tolua_tonumber(L, 2, 0); + setguard(self, flags); + return 0; +} + static const char *unit_getmagic(const unit * u) { sc_mage *mage = get_mage(u); @@ -984,6 +1002,8 @@ void tolua_unit_open(lua_State * L) /* key-attributes for named flags: */ tolua_function(L, TOLUA_CAST "set_flag", &tolua_unit_set_flag); tolua_function(L, TOLUA_CAST "get_flag", &tolua_unit_get_flag); + tolua_variable(L, TOLUA_CAST "guard", &tolua_unit_get_guard, + &tolua_unit_set_guard); tolua_variable(L, TOLUA_CAST "flags", &tolua_unit_get_flags, &tolua_unit_set_flags); tolua_variable(L, TOLUA_CAST "age", &tolua_unit_get_age,