From 131840054cb3c3b997e778aea716d2db75a2c8eb Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 27 May 2012 08:38:17 -0700 Subject: [PATCH] giving ownerships over buildings was broken by a recent refactoring --- src/gamecode.vcxproj | 1 + src/gamecode.vcxproj.filters | 3 ++ src/gamecode/economy.c | 20 ++++++------ src/gamecode/economy.h | 1 + src/gamecode/economy_test.c | 60 ++++++++++++++++++++++++++++++++++++ src/tests.c | 10 +++--- 6 files changed, 82 insertions(+), 13 deletions(-) create mode 100644 src/gamecode/economy_test.c diff --git a/src/gamecode.vcxproj b/src/gamecode.vcxproj index 17572c776..e9cd8c4be 100644 --- a/src/gamecode.vcxproj +++ b/src/gamecode.vcxproj @@ -92,6 +92,7 @@ + diff --git a/src/gamecode.vcxproj.filters b/src/gamecode.vcxproj.filters index 9105225f2..176c488c8 100644 --- a/src/gamecode.vcxproj.filters +++ b/src/gamecode.vcxproj.filters @@ -68,6 +68,9 @@ Source Files + + Source Files + diff --git a/src/gamecode/economy.c b/src/gamecode/economy.c index 2d0af734c..dcf4bdcc7 100644 --- a/src/gamecode/economy.c +++ b/src/gamecode/economy.c @@ -626,17 +626,19 @@ static void friendly_takeover(region * r, faction * f) } } -static void give_control(unit * u, unit * u2) +void give_control(unit * u, unit * u2) { - if (u->building && u->faction != u2->faction && rule_region_owners()) { - region *r = u->region; - faction *f = region_get_owner(r); + if (u->building) { + if (u->faction != u2->faction && rule_region_owners()) { + region *r = u->region; + faction *f = region_get_owner(r); - assert(u->building==u2->building); - if (f == u->faction) { - building *b = largestbuilding(r, &cmp_current_owner, false); - if (b == u->building) { - friendly_takeover(r, u2->faction); + assert(u->building==u2->building); + if (f == u->faction) { + building *b = largestbuilding(r, &cmp_current_owner, false); + if (b == u->building) { + friendly_takeover(r, u2->faction); + } } } building_set_owner(u2); diff --git a/src/gamecode/economy.h b/src/gamecode/economy.h index 9d931e433..b5ccac86d 100644 --- a/src/gamecode/economy.h +++ b/src/gamecode/economy.h @@ -55,6 +55,7 @@ extern "C" { extern void split_allocations(struct region *r); extern int recruit_archetypes(void); extern int give_control_cmd(struct unit *u, struct order *ord); + extern void give_control(struct unit * u, struct unit * u2); #ifdef __cplusplus } diff --git a/src/gamecode/economy_test.c b/src/gamecode/economy_test.c new file mode 100644 index 000000000..0d44c2dc1 --- /dev/null +++ b/src/gamecode/economy_test.c @@ -0,0 +1,60 @@ +#include "platform.h" +#include "economy.h" + +#include +#include +#include +#include + +#include +#include + +static void test_give_control_building(CuTest * tc) +{ + unit *u1, *u2; + building *b; + struct faction *f; + region *r; + + test_cleanup(); + test_create_world(); + f = test_create_faction(0); + r = findregion(0, 0); + b = test_create_building(r, 0); + u1 = test_create_unit(f, r); + u_set_building(u1, b); + u2 = test_create_unit(f, r); + u_set_building(u2, b); + CuAssertPtrEquals(tc, u1, building_owner(b)); + give_control(u1, u2); + CuAssertPtrEquals(tc, u2, building_owner(b)); +} + +static void test_give_control_ship(CuTest * tc) +{ + unit *u1, *u2; + ship *sh; + struct faction *f; + region *r; + + test_cleanup(); + test_create_world(); + f = test_create_faction(0); + r = findregion(0, 0); + sh = test_create_ship(r, 0); + u1 = test_create_unit(f, r); + u_set_ship(u1, sh); + u2 = test_create_unit(f, r); + u_set_ship(u2, sh); + CuAssertPtrEquals(tc, u1, ship_owner(sh)); + give_control(u1, u2); + CuAssertPtrEquals(tc, u2, ship_owner(sh)); +} + +CuSuite *get_economy_suite(void) +{ + CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_give_control_building); + SUITE_ADD_TEST(suite, test_give_control_ship); + return suite; +} diff --git a/src/tests.c b/src/tests.c index 4dcdaa1b3..1f6d4df38 100644 --- a/src/tests.c +++ b/src/tests.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -67,6 +68,7 @@ int RunAllTests(void) /* gamecode */ CuSuiteAddSuite(suite, get_market_suite()); CuSuiteAddSuite(suite, get_laws_suite()); + CuSuiteAddSuite(suite, get_economy_suite()); CuSuiteRun(suite); CuSuiteSummary(suite, output); @@ -137,15 +139,15 @@ test_create_terrain(const char * name, unsigned int flags) building * test_create_building(region * r, const building_type * btype) { - building * b = new_building(btype, r, default_locale); - b->size = btype->maxsize>0?btype->maxsize:1; + building * b = new_building(btype?btype:bt_find("castle"), r, default_locale); + b->size = b->type->maxsize>0?b->type->maxsize:1; return b; } ship * test_create_ship(region * r, const ship_type * stype) { - ship * s = new_ship(stype, r, default_locale); - s->size = stype->construction?stype->construction->maxsize:1; + ship * s = new_ship(stype?stype:st_find("boat"), r, default_locale); + s->size = s->type->construction?s->type->construction->maxsize:1; return s; }