From fe463a9f44ad4b3e930e0cad767851359b14a654 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 17 Nov 2020 21:16:54 +0100 Subject: [PATCH] bug 2710: add tests, fix land movement --- scripts/tests/e2/guard.lua | 63 +++++++++++++++++++++++++++++++++++ scripts/tests/e2/movement.lua | 17 ++++++++++ scripts/tests/e2/ships.lua | 28 ++++++++++++++++ src/bind_unit.c | 2 +- src/kernel/unit.c | 34 +++++++++++-------- src/kernel/unit.h | 1 + tests/runtests.bat | 1 + 7 files changed, 131 insertions(+), 15 deletions(-) diff --git a/scripts/tests/e2/guard.lua b/scripts/tests/e2/guard.lua index 0cc970f8c..5ae40cf71 100644 --- a/scripts/tests/e2/guard.lua +++ b/scripts/tests/e2/guard.lua @@ -79,3 +79,66 @@ function test_no_guard_no_move_after_combat() -- bug 1493 u1 = get_unit(uid1) assert_equal(r1, u1.region) end + +function test_move_stops_guarding() + local r1 = region.create(0, 0, "plain") + local r2 = region.create(1, 0, "plain") + local f = faction.create("human", "test@example.com", "de") + local u = unit.create(f, r1, 1) + u:add_item("sword", 1) + u:set_skill("melee", 2) + u:add_order("NACH O") + u:add_order("BEWACHE") + u.guard = true + process_orders() + assert_equal(r2, u.region) + assert_false(u.guard) +end + +function test_move_to_same_region_stops_guarding() + local r1 = region.create(0, 0, "plain") + local r2 = region.create(1, 0, "plain") + local f = faction.create("human", "test@example.com", "de") + local u = unit.create(f, r1, 1) + u:add_item("horse", 1) + u:add_item("sword", 1) + u:set_skill("melee", 2) + u:set_skill("riding", 2) + u:add_order("NACH O W") + u.guard = true + process_orders() + assert_equal(r1, u.region) + assert_false(u.guard) +end + +function test_mover_cannot_start_guarding() + local r1 = region.create(0, 0, "plain") + local r2 = region.create(1, 0, "plain") + local f = faction.create("human", "test@example.com", "de") + local u = unit.create(f, r1, 1) + u:add_item("horse", 1) + u:add_item("sword", 1) + u:set_skill("melee", 2) + u:set_skill("riding", 2) + u:add_order("BEWACHE") + u:add_order("NACH O") + process_orders() + assert_equal(r2, u.region) + assert_false(u.guard) +end + +function test_move_to_same_region_stops_guarding() + local r1 = region.create(0, 0, "plain") + local r2 = region.create(1, 0, "plain") + local f = faction.create("human", "test@example.com", "de") + local u = unit.create(f, r1, 1) + u:add_item("horse", 1) + u:add_item("sword", 1) + u:set_skill("melee", 2) + u:set_skill("riding", 2) + u:add_order("BEWACHE") + u:add_order("NACH O W") + process_orders() + assert_equal(r1, u.region) + assert_false(u.guard) +end diff --git a/scripts/tests/e2/movement.lua b/scripts/tests/e2/movement.lua index d5f2f886f..77fec46ee 100644 --- a/scripts/tests/e2/movement.lua +++ b/scripts/tests/e2/movement.lua @@ -165,3 +165,20 @@ function assert_capacity(text, u, silver, r1, r2, rx) process_orders() assert_equal(rx, u.region, text .. "unit should not move") end + +function test_move_to_same_region_leaves_building() + local r1 = region.create(0, 0, "plain") + local r2 = region.create(1, 0, "plain") + local f = faction.create("human", "test@example.com", "de") + local u = unit.create(f, r1, 1) + local b = building.create(u.region, "castle") + b.size = 2 + u.building = b + assert_not_nil(u.building) + u:add_item("horse", 1) + u:set_skill("riding", 2) + u:add_order("NACH O W") + process_orders() + assert_equal(r1, u.region) + assert_nil(u.building) +end diff --git a/scripts/tests/e2/ships.lua b/scripts/tests/e2/ships.lua index db44871b3..202c74b6a 100644 --- a/scripts/tests/e2/ships.lua +++ b/scripts/tests/e2/ships.lua @@ -519,3 +519,31 @@ function test_build_convoy_max() assert_equal(100, sh.size) assert_equal(25, u:get_item('log')) end + +function test_ship_crew_stops_guarding() + local r1 = region.create(0, 0, "plain") + local r2 = region.create(1, 0, "ocean") + local f = faction.create("human") + local u1 = unit.create(f, r1, 1) + local u2 = unit.create(f, r1, 1) + local sh = ship.create(r1, "longboat") + u1.ship = sh + u2.ship = sh + u1.name = 'Bolgrim' + u1.name = 'Bolle' + u1:clear_orders() + u1:add_order("NACH O W") + u1:set_skill("sailing", 1) -- cptskill = 1 + u2:set_skill("sailing", 9) -- sumskill = 10 + u2:add_item("sword", 1) + u2:set_skill("melee", 2) + u2:add_order("BEWACHE") + u2.guard = true + process_orders() + assert_false(u2.guard) + assert_equal(sh, u1.ship) + assert_equal(sh, u2.ship) + assert_equal(r1, sh.region) + assert_equal(r1, u1.region) + assert_equal(r1, u2.region) +end diff --git a/src/bind_unit.c b/src/bind_unit.c index d494e5379..8f8aeefac 100644 --- a/src/bind_unit.c +++ b/src/bind_unit.c @@ -271,7 +271,7 @@ static int tolua_unit_get_guard(lua_State * L) static int tolua_unit_set_guard(lua_State * L) { unit *u = (unit *)tolua_tousertype(L, 1, 0); - unsigned int flags = (unsigned int)tolua_tonumber(L, 2, 0); + int flags = tolua_toboolean(L, 2, 0); setguard(u, flags!=0); return 0; } diff --git a/src/kernel/unit.c b/src/kernel/unit.c index 91a6f4803..33ac7711e 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -705,27 +705,33 @@ bool can_survive(const unit * u, const region * r) return false; } +void leave_region(unit* u) +{ + assert(u->region); + setguard(u, false); + fset(u, UFL_MOVED); + if (u->ship || u->building) { + /* can_leave must be checked in travel_i */ +#ifndef NDEBUG + bool result = leave(u, false); + assert(result); +#else + leave(u, false); +#endif + } +} + + void move_unit(unit * u, region * r, unit ** ulist) { assert(u && r); assert(u->faction || !"this unit is dead"); - if (u->region == r) - return; - if (!ulist) + if (!ulist) { ulist = (&r->units); + } if (u->region) { - setguard(u, false); - fset(u, UFL_MOVED); - if (u->ship || u->building) { - /* can_leave must be checked in travel_i */ -#ifndef NDEBUG - bool result = leave(u, false); - assert(result); -#else - leave(u, false); -#endif - } + leave_region(u); translist(&u->region->units, ulist, u); } else { diff --git a/src/kernel/unit.h b/src/kernel/unit.h index 396d97ead..4a4039602 100644 --- a/src/kernel/unit.h +++ b/src/kernel/unit.h @@ -143,6 +143,7 @@ extern "C" { int get_modifier(const struct unit *u, skill_t sk, int level, const struct region *r, bool noitem); int remove_unit(struct unit **ulist, struct unit *u); + void leave_region(struct unit* u); /* looking up dead units' factions: */ struct faction *dfindhash(int no); diff --git a/tests/runtests.bat b/tests/runtests.bat index e73580851..80a52308f 100644 --- a/tests/runtests.bat +++ b/tests/runtests.bat @@ -4,6 +4,7 @@ IF EXIST ..\build-vs11 SET BUILD=..\build-vs11\eressea\Debug IF EXIST ..\build-vs12 SET BUILD=..\build-vs12\eressea\Debug IF EXIST ..\build-vs14 SET BUILD=..\build-vs14\eressea\Debug IF EXIST ..\build-vs15 SET BUILD=..\build-vs15\eressea\Debug +IF EXIST ..\build-vs16 SET BUILD=..\build-vs16\eressea\Debug SET SERVER=%BUILD%\eressea.exe %BUILD%\test_eressea.exe