Merge pull request #164 from badgerman/develop

Bugfix: CLAIM crashing the server.
This commit is contained in:
Enno Rehling 2015-04-09 14:41:47 +02:00
commit 11243741ab
4 changed files with 117 additions and 16 deletions

View File

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

View File

@ -1,4 +1,4 @@
-- new tests 2015-02-13 -- require 'tests.e2.shiplanding'
require 'tests.e2.shiplanding' -- require 'tests.e2.e2features'
require 'tests.e2.e2features' -- require 'tests.e2.movement'
require 'tests.e2.movement' require 'tests.e2.guard'

View File

@ -262,6 +262,24 @@ static int tolua_unit_set_flags(lua_State * L)
return 0; 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) static const char *unit_getmagic(const unit * u)
{ {
sc_mage *mage = get_mage(u); sc_mage *mage = get_mage(u);
@ -984,6 +1002,8 @@ void tolua_unit_open(lua_State * L)
/* key-attributes for named flags: */ /* key-attributes for named flags: */
tolua_function(L, TOLUA_CAST "set_flag", &tolua_unit_set_flag); tolua_function(L, TOLUA_CAST "set_flag", &tolua_unit_set_flag);
tolua_function(L, TOLUA_CAST "get_flag", &tolua_unit_get_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_variable(L, TOLUA_CAST "flags", &tolua_unit_get_flags,
&tolua_unit_set_flags); &tolua_unit_set_flags);
tolua_variable(L, TOLUA_CAST "age", &tolua_unit_get_age, tolua_variable(L, TOLUA_CAST "age", &tolua_unit_get_age,

View File

@ -3911,12 +3911,13 @@ int claim_cmd(unit * u, struct order *ord)
{ {
char token[128]; char token[128];
const char *t; const char *t;
int n; int n = 1;
const item_type *itype; const item_type *itype = 0;
init_order(ord); init_order(ord);
t = gettoken(token, sizeof(token)); t = gettoken(token, sizeof(token));
if (t) {
n = atoi((const char *)t); n = atoi((const char *)t);
if (n == 0) { if (n == 0) {
n = 1; n = 1;
@ -3924,11 +3925,13 @@ int claim_cmd(unit * u, struct order *ord)
else { else {
t = gettoken(token, sizeof(token)); t = gettoken(token, sizeof(token));
} }
if (t) {
itype = finditemtype(t, u->faction->locale); itype = finditemtype(t, u->faction->locale);
}
if (itype != NULL) { }
if (itype) {
item **iclaim = i_find(&u->faction->items, itype); item **iclaim = i_find(&u->faction->items, itype);
if (iclaim != NULL && *iclaim != NULL) { if (iclaim && *iclaim) {
n = _min(n, (*iclaim)->number); n = _min(n, (*iclaim)->number);
i_change(iclaim, itype, -n); i_change(iclaim, itype, -n);
i_change(&u->items, itype, n); i_change(&u->items, itype, n);