giving ownerships over buildings was broken by a recent refactoring

This commit is contained in:
Enno Rehling 2012-05-27 08:38:17 -07:00
parent 36ad727394
commit 131840054c
6 changed files with 82 additions and 13 deletions

View File

@ -92,6 +92,7 @@
<ClCompile Include="gamecode\creport.c" /> <ClCompile Include="gamecode\creport.c" />
<ClCompile Include="gamecode\economy.c" /> <ClCompile Include="gamecode\economy.c" />
<ClCompile Include="eressea.c" /> <ClCompile Include="eressea.c" />
<ClCompile Include="gamecode\economy_test.c" />
<ClCompile Include="gamecode\give.c" /> <ClCompile Include="gamecode\give.c" />
<ClCompile Include="gamecode\xmlreport.c" /> <ClCompile Include="gamecode\xmlreport.c" />
<ClCompile Include="gmtool.c" /> <ClCompile Include="gmtool.c" />

View File

@ -68,6 +68,9 @@
<ClCompile Include="gamecode\xmlreport.c"> <ClCompile Include="gamecode\xmlreport.c">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="gamecode\economy_test.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="gamecode\archetype.h"> <ClInclude Include="gamecode\archetype.h">

View File

@ -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()) { if (u->building) {
region *r = u->region; if (u->faction != u2->faction && rule_region_owners()) {
faction *f = region_get_owner(r); region *r = u->region;
faction *f = region_get_owner(r);
assert(u->building==u2->building); assert(u->building==u2->building);
if (f == u->faction) { if (f == u->faction) {
building *b = largestbuilding(r, &cmp_current_owner, false); building *b = largestbuilding(r, &cmp_current_owner, false);
if (b == u->building) { if (b == u->building) {
friendly_takeover(r, u2->faction); friendly_takeover(r, u2->faction);
}
} }
} }
building_set_owner(u2); building_set_owner(u2);

View File

@ -55,6 +55,7 @@ extern "C" {
extern void split_allocations(struct region *r); extern void split_allocations(struct region *r);
extern int recruit_archetypes(void); extern int recruit_archetypes(void);
extern int give_control_cmd(struct unit *u, struct order *ord); extern int give_control_cmd(struct unit *u, struct order *ord);
extern void give_control(struct unit * u, struct unit * u2);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -0,0 +1,60 @@
#include "platform.h"
#include "economy.h"
#include <kernel/unit.h>
#include <kernel/region.h>
#include <kernel/building.h>
#include <kernel/ship.h>
#include <cutest/CuTest.h>
#include <tests.h>
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;
}

View File

@ -21,6 +21,7 @@
#include <kernel/equipment_test.c> #include <kernel/equipment_test.c>
#include <kernel/reports_test.c> #include <kernel/reports_test.c>
#include <kernel/spellbook_test.c> #include <kernel/spellbook_test.c>
#include <gamecode/economy_test.c>
#include <gamecode/laws_test.c> #include <gamecode/laws_test.c>
#include <gamecode/market_test.c> #include <gamecode/market_test.c>
@ -67,6 +68,7 @@ int RunAllTests(void)
/* gamecode */ /* gamecode */
CuSuiteAddSuite(suite, get_market_suite()); CuSuiteAddSuite(suite, get_market_suite());
CuSuiteAddSuite(suite, get_laws_suite()); CuSuiteAddSuite(suite, get_laws_suite());
CuSuiteAddSuite(suite, get_economy_suite());
CuSuiteRun(suite); CuSuiteRun(suite);
CuSuiteSummary(suite, output); 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 * test_create_building(region * r, const building_type * btype)
{ {
building * b = new_building(btype, r, default_locale); building * b = new_building(btype?btype:bt_find("castle"), r, default_locale);
b->size = btype->maxsize>0?btype->maxsize:1; b->size = b->type->maxsize>0?b->type->maxsize:1;
return b; return b;
} }
ship * test_create_ship(region * r, const ship_type * stype) ship * test_create_ship(region * r, const ship_type * stype)
{ {
ship * s = new_ship(stype, r, default_locale); ship * s = new_ship(stype?stype:st_find("boat"), r, default_locale);
s->size = stype->construction?stype->construction->maxsize:1; s->size = s->type->construction?s->type->construction->maxsize:1;
return s; return s;
} }