From 7fb5adc7af21ee8558168e800ff861e673b35b8c Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 12 Nov 2015 17:55:39 +0100 Subject: [PATCH] in_safe_building needs a test and some changes to logic github issue #393 fixed --- src/kernel/building.c | 15 +++++++++++++++ src/kernel/building.h | 1 + src/kernel/building.test.c | 23 ++++++++++++++++++++++- src/monsters.c | 13 ------------- 4 files changed, 38 insertions(+), 14 deletions(-) diff --git a/src/kernel/building.c b/src/kernel/building.c index 6f97efad0..d4a7fbbbd 100644 --- a/src/kernel/building.c +++ b/src/kernel/building.c @@ -662,3 +662,18 @@ void building_setregion(building * b, region * r) b->region = r; } + +bool in_safe_building(unit *u1, unit *u2) { + if (u1->building) { + building * b = inside_building(u1); + if (b->type->flags & BTF_FORTIFICATION) { + if (!u2->building) { + return true; + } + if (u2->building != b || b != inside_building(u2)) { + return true; + } + } + } + return false; +} diff --git a/src/kernel/building.h b/src/kernel/building.h index 936c8ade4..78b234f3d 100644 --- a/src/kernel/building.h +++ b/src/kernel/building.h @@ -89,6 +89,7 @@ extern "C" { int bt_effsize(const struct building_type *btype, const struct building *b, int bsize); + bool in_safe_building(struct unit *u1, struct unit *u2); /* buildingt => building_type * Name => locale_string(name) * MaxGroesse => levels diff --git a/src/kernel/building.test.c b/src/kernel/building.test.c index 1f0ca303c..4241e893f 100644 --- a/src/kernel/building.test.c +++ b/src/kernel/building.test.c @@ -386,7 +386,7 @@ static void test_active_building(CuTest *tc) { building *b; test_cleanup(); - b = test_create_building(test_create_region(0,0,0), 0); + b = test_create_building(test_create_region(0, 0, 0), 0); CuAssertIntEquals(tc, false, building_is_active(b)); b->flags |= BLD_WORKING; CuAssertIntEquals(tc, true, building_is_active(b)); @@ -395,6 +395,26 @@ static void test_active_building(CuTest *tc) { test_cleanup(); } +static void test_safe_building(CuTest *tc) { + building_type *btype; + unit *u1, *u2; + + test_cleanup(); + btype = test_create_buildingtype("castle"); + u1 = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); + u2 = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); + CuAssertIntEquals(tc, false, in_safe_building(u1, u2)); + u1->building = test_create_building(u1->region, btype); + CuAssertIntEquals(tc, false, in_safe_building(u1, u2)); + btype->flags |= BTF_FORTIFICATION; + CuAssertIntEquals(tc, true, in_safe_building(u1, u2)); + u2->building = u1->building; + CuAssertIntEquals(tc, true, in_safe_building(u1, u2)); + u1->building->size = 2; + CuAssertIntEquals(tc, false, in_safe_building(u1, u2)); + test_cleanup(); +} + CuSuite *get_building_suite(void) { CuSuite *suite = CuSuiteNew(); @@ -410,5 +430,6 @@ CuSuite *get_building_suite(void) SUITE_ADD_TEST(suite, test_buildingowner_goes_to_same_faction_after_leave); SUITE_ADD_TEST(suite, test_buildingowner_goes_to_empty_unit_after_leave); SUITE_ADD_TEST(suite, test_active_building); + SUITE_ADD_TEST(suite, test_safe_building); return suite; } diff --git a/src/monsters.c b/src/monsters.c index 4da2d0076..0ab88fa61 100644 --- a/src/monsters.c +++ b/src/monsters.c @@ -157,19 +157,6 @@ static order *monster_attack(unit * u, const unit * target) return create_order(K_ATTACK, u->faction->locale, "%i", target->no); } -static bool in_safe_building(unit *u1, unit *u2) { - if (u1->building && u2->building == u1->building) { - building * b = inside_building(u1); - if (u2->building) { - if (b != inside_building(u2)) return true; - } - if (b->type->flags & BTF_FORTIFICATION) { - return true; - } - } - return false; -} - static order *get_money_for_dragon(region * r, unit * u, int wanted) { int n;