Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/guard.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,30 @@ static void test_guard_checks_allowed_weapon(CuTest *tc)
test_teardown();
}

static void test_fleeing_temp_cant_guard(CuTest *tc)
{
unit *ug, *u;
region *r;
item_type *itype;

test_setup();
itype = it_get_or_create(rt_get_or_create("sword"));
new_weapontype(itype, 0, frac_zero, NULL, 0, 0, 0, SK_MELEE);
r = test_create_plain(0, 0);
u = test_create_unit(test_create_faction(), r);
fset(u, UFL_FLEEING);
ug = test_create_unit(u->faction, r);
set_number(ug, 0);
i_change(&ug->items, itype, 1);
set_level(u, SK_MELEE, 1);
transfermen(u, ug, 1);
CuAssertIntEquals(tc, UFL_FLEEING, ug->flags & UFL_FLEEING);
CuAssertIntEquals(tc, E_GUARD_FLEEING, can_start_guarding(ug));
CuAssertIntEquals(tc, E_GUARD_FLEEING, can_start_guarding(u));

test_teardown();
}

CuSuite *get_guard_suite(void)
{
CuSuite *suite = CuSuiteNew();
Expand All @@ -289,5 +313,6 @@ CuSuite *get_guard_suite(void)
SUITE_ADD_TEST(suite, test_guard_unarmed);
SUITE_ADD_TEST(suite, test_guard_monsters);
SUITE_ADD_TEST(suite, test_update_guard);
SUITE_ADD_TEST(suite, test_fleeing_temp_cant_guard);
return suite;
}
10 changes: 10 additions & 0 deletions src/kernel/unit.c
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,16 @@ void transfermen(unit* u, unit* dst, int n)
dst->flags |=
u->flags & (UFL_LONGACTION | UFL_NOTMOVING | UFL_HUNGER | UFL_MOVED |
UFL_ENTER);
if (u->flags & UFL_FLEEING) {
if (dst->number == 0) {
/* after transfer, dst consists entirely of men fleeing from a fight (cannot guard) */
dst->flags |= UFL_FLEEING;
}
}
else {
/* after transfer, dst contains men not fleeing from a fight (can guard) */
dst->flags &= ~UFL_FLEEING;
}
if (u->attribs) {
transfer_curse(u, dst, n);
transfer_effects(u, dst, n);
Expand Down
38 changes: 37 additions & 1 deletion src/kernel/unit.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,8 @@ static void test_transfermen_bug_2386(CuTest *tc) {
test_teardown();
}

static void test_transfermen_bug_2886(CuTest *tc) {
static void test_transfermen_bug_2886(CuTest *tc)
{
unit *u1, *u2;
region *r;
faction *f;
Expand All @@ -689,6 +690,40 @@ static void test_transfermen_bug_2886(CuTest *tc) {
test_teardown();
}

static void test_transfermen_bug_3120(CuTest *tc)
{
unit *u1, *u2, *u3;
region *r;
faction *f;

test_setup();
r = test_create_plain(0, 0);
f = test_create_faction();
/* u1 is a unit that survived combat, but can't guard */
u1 = test_create_unit(f, r);
set_number(u1, 2);
fset(u1, UFL_FLEEING);

/* u2 is an empty unit that receives men from u1, so it can't guard either */
u2 = test_create_unit(f, r);
set_number(u2, 0);
transfermen(u1, u2, 1);
CuAssertIntEquals(tc, UFL_FLEEING, u2->flags & UFL_FLEEING);

/* u2 is a non-empty unit that receives men from u1, but it can still guard */
u3 = test_create_unit(f, r);
transfermen(u1, u3, 1);
CuAssertIntEquals(tc, 0, u3->flags & UFL_FLEEING);

/* u2 is a tainted unit that receives men from a non-tainted one, so it can guard */
CuAssertIntEquals(tc, UFL_FLEEING, u2->flags & UFL_FLEEING);
CuAssertIntEquals(tc, 0, u3->flags & UFL_FLEEING);
transfermen(u3, u2, 1);
CuAssertIntEquals(tc, 0, u2->flags & UFL_FLEEING);

test_teardown();
}

static void test_transfermen(CuTest *tc) {
unit *u1, *u2;
region *r;
Expand Down Expand Up @@ -1121,6 +1156,7 @@ CuSuite *get_unit_suite(void)
SUITE_ADD_TEST(suite, test_change_faction_clears_private);
SUITE_ADD_TEST(suite, test_transfermen_bug_2386);
SUITE_ADD_TEST(suite, test_transfermen_bug_2886);
SUITE_ADD_TEST(suite, test_transfermen_bug_3120);
SUITE_ADD_TEST(suite, test_remove_unit);
SUITE_ADD_TEST(suite, test_remove_empty_units);
SUITE_ADD_TEST(suite, test_remove_units_without_faction);
Expand Down
Loading