From 707262c61daeeb8dd92da97abbebebb9ad21302b Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 26 May 2012 10:43:05 -0700 Subject: [PATCH] fix region owner when a building transfers --- src/bindings/bind_building.c | 5 ++++- src/gamecode/economy.c | 6 +++++- src/kernel/building.c | 6 +++--- src/kernel/building.h | 2 +- src/kernel/building_test.c | 2 +- src/kernel/reports_test.c | 4 ++-- src/kernel/save.c | 4 ++-- src/kernel/ship.c | 6 +++--- src/kernel/ship.h | 2 +- src/kernel/ship_test.c | 2 +- src/kernel/unit.c | 4 ++-- 11 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/bindings/bind_building.c b/src/bindings/bind_building.c index 97432bfca..4e51a0a65 100644 --- a/src/bindings/bind_building.c +++ b/src/bindings/bind_building.c @@ -170,7 +170,10 @@ static int tolua_building_set_owner(lua_State * L) { building *b = (building *) tolua_tousertype(L, 1, 0); unit *u = (unit *) tolua_tousertype(L, 2, 0); - building_set_owner(b, u); + if (b!=u->building) { + u_set_building(u, b); + } + building_set_owner(u); return 0; } diff --git a/src/gamecode/economy.c b/src/gamecode/economy.c index 11c0a9245..2d0af734c 100644 --- a/src/gamecode/economy.c +++ b/src/gamecode/economy.c @@ -631,15 +631,19 @@ static 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); + + 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); } if (u->ship) { - ship_set_owner(u->ship, u2); + assert(u->ship==u2->ship); + ship_set_owner(u2); } } diff --git a/src/kernel/building.c b/src/kernel/building.c index 77af096cb..a5178a17d 100644 --- a/src/kernel/building.c +++ b/src/kernel/building.c @@ -627,10 +627,10 @@ const char *buildingname(const building * b) return write_buildingname(b, ibuf, sizeof(name)); } -void building_set_owner(struct building *b, struct unit * owner) +void building_set_owner(struct unit * owner) { - assert(b && owner && owner->building==b); - b->_owner = owner; + assert(owner && owner->building); + owner->building->_owner = owner; } static unit *building_owner_ex(const building * bld, const struct faction * last_owner) diff --git a/src/kernel/building.h b/src/kernel/building.h index 2c7cec522..f78ba1db8 100644 --- a/src/kernel/building.h +++ b/src/kernel/building.h @@ -152,7 +152,7 @@ extern "C" { extern struct building *findbuilding(int n); extern struct unit *building_owner(const struct building *b); - extern void building_set_owner(struct building *b, struct unit * u); + extern void building_set_owner(struct unit * u); extern void building_update_owner(struct building * bld); extern struct attrib_type at_building_action; diff --git a/src/kernel/building_test.c b/src/kernel/building_test.c index 42f68690d..956266715 100644 --- a/src/kernel/building_test.c +++ b/src/kernel/building_test.c @@ -50,7 +50,7 @@ static void test_building_set_owner(CuTest * tc) u2 = test_create_unit(f, r); u_set_building(u2, bld); CuAssertPtrEquals(tc, u1, building_owner(bld)); - building_set_owner(bld, u2); + building_set_owner(u2); CuAssertPtrEquals(tc, u2, building_owner(bld)); } diff --git a/src/kernel/reports_test.c b/src/kernel/reports_test.c index c35d704d2..29bc9f5d2 100644 --- a/src/kernel/reports_test.c +++ b/src/kernel/reports_test.c @@ -34,13 +34,13 @@ static void test_reorder_units(CuTest * tc) u_set_ship(u0, s); u1 = test_create_unit(f, r); u_set_ship(u1, s); - ship_set_owner(s, u1); + ship_set_owner(u1); u2 = test_create_unit(f, r); u3 = test_create_unit(f, r); u_set_building(u3, b); u4 = test_create_unit(f, r); u_set_building(u4, b); - building_set_owner(b, u4); + building_set_owner(u4); reorder_units(r); diff --git a/src/kernel/save.c b/src/kernel/save.c index 3f1502478..7fe3572e8 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -802,7 +802,7 @@ unit *read_unit(struct storage *store) if (b) { u_set_building(u, b); if (fval(u, UFL_OWNER)) { - building_set_owner(b, u); + building_set_owner(u); } } else { log_error("read_unit: unit in unkown building '%s'\n", itoa36(n)); @@ -815,7 +815,7 @@ unit *read_unit(struct storage *store) if (sh) { u_set_ship(u, sh); if (fval(u, UFL_OWNER)) { - ship_set_owner(sh, u); + ship_set_owner(u); } } else { log_error("read_unit: unit in unkown ship '%s'\n", itoa36(n)); diff --git a/src/kernel/ship.c b/src/kernel/ship.c index 46f75689d..63787a3ab 100644 --- a/src/kernel/ship.c +++ b/src/kernel/ship.c @@ -286,9 +286,9 @@ void getshipweight(const ship * sh, int *sweight, int *scabins) } } -void ship_set_owner(ship * sh, unit * u) { - assert(u->ship==sh); - sh->_owner = u; +void ship_set_owner(unit * u) { + assert(u && u->ship); + u->ship->_owner = u; } static unit * ship_owner_ex(const ship * sh, const struct faction * last_owner) diff --git a/src/kernel/ship.h b/src/kernel/ship.h index fe1322f47..8249a1beb 100644 --- a/src/kernel/ship.h +++ b/src/kernel/ship.h @@ -95,7 +95,7 @@ extern "C" { } ship; extern void damage_ship(struct ship * sh, double percent); - extern void ship_set_owner(struct ship * sh, struct unit * u); + extern void ship_set_owner(struct unit * u); extern struct unit *ship_owner(const struct ship *sh); extern void ship_update_owner(struct ship * sh); diff --git a/src/kernel/ship_test.c b/src/kernel/ship_test.c index 6b1d2e136..488e61eef 100644 --- a/src/kernel/ship_test.c +++ b/src/kernel/ship_test.c @@ -50,7 +50,7 @@ static void test_ship_set_owner(CuTest * tc) u2 = test_create_unit(f, r); u_set_ship(u2, sh); CuAssertPtrEquals(tc, u1, ship_owner(sh)); - ship_set_owner(sh, u2); + ship_set_owner(u2); CuAssertPtrEquals(tc, u2, ship_owner(sh)); } diff --git a/src/kernel/unit.c b/src/kernel/unit.c index 4131e317b..d67f42a43 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -781,7 +781,7 @@ void u_set_building(unit * u, building * b) assert(!u->building); /* you must leave first */ u->building = b; if (b && !b->_owner) { - building_set_owner(b, u); + building_set_owner(u); } } @@ -790,7 +790,7 @@ void u_set_ship(unit * u, ship * sh) assert(!u->ship); /* you must leave_ship */ u->ship = sh; if (sh && !sh->_owner) { - ship_set_owner(sh, u); + ship_set_owner(u); } }