fix region owner when a building transfers

This commit is contained in:
Enno Rehling 2012-05-26 10:43:05 -07:00
parent 5547893be7
commit 707262c61d
11 changed files with 25 additions and 18 deletions

View File

@ -170,7 +170,10 @@ static int tolua_building_set_owner(lua_State * L)
{ {
building *b = (building *) tolua_tousertype(L, 1, 0); building *b = (building *) tolua_tousertype(L, 1, 0);
unit *u = (unit *) tolua_tousertype(L, 2, 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; return 0;
} }

View File

@ -631,15 +631,19 @@ static void give_control(unit * u, unit * u2)
if (u->building && u->faction != u2->faction && rule_region_owners()) { if (u->building && u->faction != u2->faction && rule_region_owners()) {
region *r = u->region; region *r = u->region;
faction *f = region_get_owner(r); faction *f = region_get_owner(r);
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);
} }
if (u->ship) { if (u->ship) {
ship_set_owner(u->ship, u2); assert(u->ship==u2->ship);
ship_set_owner(u2);
} }
} }

View File

@ -627,10 +627,10 @@ const char *buildingname(const building * b)
return write_buildingname(b, ibuf, sizeof(name)); 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); assert(owner && owner->building);
b->_owner = owner; owner->building->_owner = owner;
} }
static unit *building_owner_ex(const building * bld, const struct faction * last_owner) static unit *building_owner_ex(const building * bld, const struct faction * last_owner)

View File

@ -152,7 +152,7 @@ extern "C" {
extern struct building *findbuilding(int n); extern struct building *findbuilding(int n);
extern struct unit *building_owner(const struct building *b); 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 void building_update_owner(struct building * bld);
extern struct attrib_type at_building_action; extern struct attrib_type at_building_action;

View File

@ -50,7 +50,7 @@ static void test_building_set_owner(CuTest * tc)
u2 = test_create_unit(f, r); u2 = test_create_unit(f, r);
u_set_building(u2, bld); u_set_building(u2, bld);
CuAssertPtrEquals(tc, u1, building_owner(bld)); CuAssertPtrEquals(tc, u1, building_owner(bld));
building_set_owner(bld, u2); building_set_owner(u2);
CuAssertPtrEquals(tc, u2, building_owner(bld)); CuAssertPtrEquals(tc, u2, building_owner(bld));
} }

View File

@ -34,13 +34,13 @@ static void test_reorder_units(CuTest * tc)
u_set_ship(u0, s); u_set_ship(u0, s);
u1 = test_create_unit(f, r); u1 = test_create_unit(f, r);
u_set_ship(u1, s); u_set_ship(u1, s);
ship_set_owner(s, u1); ship_set_owner(u1);
u2 = test_create_unit(f, r); u2 = test_create_unit(f, r);
u3 = test_create_unit(f, r); u3 = test_create_unit(f, r);
u_set_building(u3, b); u_set_building(u3, b);
u4 = test_create_unit(f, r); u4 = test_create_unit(f, r);
u_set_building(u4, b); u_set_building(u4, b);
building_set_owner(b, u4); building_set_owner(u4);
reorder_units(r); reorder_units(r);

View File

@ -802,7 +802,7 @@ unit *read_unit(struct storage *store)
if (b) { if (b) {
u_set_building(u, b); u_set_building(u, b);
if (fval(u, UFL_OWNER)) { if (fval(u, UFL_OWNER)) {
building_set_owner(b, u); building_set_owner(u);
} }
} else { } else {
log_error("read_unit: unit in unkown building '%s'\n", itoa36(n)); log_error("read_unit: unit in unkown building '%s'\n", itoa36(n));
@ -815,7 +815,7 @@ unit *read_unit(struct storage *store)
if (sh) { if (sh) {
u_set_ship(u, sh); u_set_ship(u, sh);
if (fval(u, UFL_OWNER)) { if (fval(u, UFL_OWNER)) {
ship_set_owner(sh, u); ship_set_owner(u);
} }
} else { } else {
log_error("read_unit: unit in unkown ship '%s'\n", itoa36(n)); log_error("read_unit: unit in unkown ship '%s'\n", itoa36(n));

View File

@ -286,9 +286,9 @@ void getshipweight(const ship * sh, int *sweight, int *scabins)
} }
} }
void ship_set_owner(ship * sh, unit * u) { void ship_set_owner(unit * u) {
assert(u->ship==sh); assert(u && u->ship);
sh->_owner = u; u->ship->_owner = u;
} }
static unit * ship_owner_ex(const ship * sh, const struct faction * last_owner) static unit * ship_owner_ex(const ship * sh, const struct faction * last_owner)

View File

@ -95,7 +95,7 @@ extern "C" {
} ship; } ship;
extern void damage_ship(struct ship * sh, double percent); 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 struct unit *ship_owner(const struct ship *sh);
extern void ship_update_owner(struct ship * sh); extern void ship_update_owner(struct ship * sh);

View File

@ -50,7 +50,7 @@ static void test_ship_set_owner(CuTest * tc)
u2 = test_create_unit(f, r); u2 = test_create_unit(f, r);
u_set_ship(u2, sh); u_set_ship(u2, sh);
CuAssertPtrEquals(tc, u1, ship_owner(sh)); CuAssertPtrEquals(tc, u1, ship_owner(sh));
ship_set_owner(sh, u2); ship_set_owner(u2);
CuAssertPtrEquals(tc, u2, ship_owner(sh)); CuAssertPtrEquals(tc, u2, ship_owner(sh));
} }

View File

@ -781,7 +781,7 @@ void u_set_building(unit * u, building * b)
assert(!u->building); /* you must leave first */ assert(!u->building); /* you must leave first */
u->building = b; u->building = b;
if (b && !b->_owner) { 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 */ assert(!u->ship); /* you must leave_ship */
u->ship = sh; u->ship = sh;
if (sh && !sh->_owner) { if (sh && !sh->_owner) {
ship_set_owner(sh, u); ship_set_owner(u);
} }
} }