owner update fix and test cases

This commit is contained in:
Enno Rehling 2009-07-20 05:47:37 +00:00
parent de4604fdcc
commit 8ffafd2815
3 changed files with 67 additions and 9 deletions

View File

@ -3068,16 +3068,18 @@ static double rc_popularity(const struct race * rc)
void update_owners(region * r) void update_owners(region * r)
{ {
building * blargest = NULL; building * bowner = largestbuilding(r, &is_owner_building, false);
blargest = largestbuilding(r, &is_tax_building, false); building * blargest = largestbuilding(r, &is_tax_building, false);
if (blargest) { if (blargest) {
/* region owners update? */ if (!bowner || bowner->size<blargest->size) {
faction * f = region_get_owner(r); /* region owners update? */
unit * u = buildingowner(r, blargest); faction * f = region_get_owner(r);
if (u==NULL) { unit * u = buildingowner(r, blargest);
region_set_owner(r, NULL, turn); if (u==NULL) {
} else if (u->faction!=f) { region_set_owner(r, NULL, turn);
region_set_owner(r, u->faction, turn); } else if (u->faction!=f) {
region_set_owner(r, u->faction, turn);
}
} }
} }
} }

View File

@ -118,6 +118,29 @@ tolua_region_get_terrainname(lua_State* L)
return 0; return 0;
} }
static int
tolua_region_set_owner(lua_State* L)
{
region* r = (region*) tolua_tousertype(L, 1, 0);
struct faction* f = (struct faction*) tolua_tousertype(L, 2, 0);
if (r) {
region_set_owner(r, f, turn);
}
return 0;
}
static int
tolua_region_get_owner(lua_State* L)
{
region* r = (region*) tolua_tousertype(L, 1, 0);
if (r) {
struct faction * f = region_get_owner(r);
tolua_pushusertype(L, f, "faction");
return 1;
}
return 0;
}
static int static int
tolua_region_set_terrainname(lua_State* L) tolua_region_set_terrainname(lua_State* L)
{ {
@ -553,6 +576,7 @@ tolua_region_open(lua_State* L)
tolua_function(L, TOLUA_CAST "next", tolua_region_get_adj); tolua_function(L, TOLUA_CAST "next", tolua_region_get_adj);
tolua_variable(L, TOLUA_CAST "terrain_name", &tolua_region_get_terrainname, &tolua_region_set_terrainname); tolua_variable(L, TOLUA_CAST "terrain_name", &tolua_region_get_terrainname, &tolua_region_set_terrainname);
tolua_variable(L, TOLUA_CAST "owner", &tolua_region_get_owner, &tolua_region_set_owner);
tolua_function(L, TOLUA_CAST "get_key", tolua_region_getkey); tolua_function(L, TOLUA_CAST "get_key", tolua_region_getkey);
tolua_function(L, TOLUA_CAST "set_key", tolua_region_setkey); tolua_function(L, TOLUA_CAST "set_key", tolua_region_setkey);

View File

@ -244,6 +244,36 @@ local function test_recruit2()
print(u.number) print(u.number)
end end
local function test_owners()
free_game()
local r = region.create(0, 0, "plain")
local f1 = faction.create("enno@eressea.de", "human", "de")
local u1 = unit.create(f1, r, 1)
local f2 = faction.create("enno@eressea.de", "human", "de")
local u2 = unit.create(f2, r, 1)
local u3 = unit.create(f2, r, 1)
local b3 = building.create(r, "castle")
b3.size = 2
u3.building = b3
local b1 = building.create(r, "castle")
b1.size = 1
u1.building = b1
local b2 = building.create(r, "castle")
b2.size = 2
u2.building = b2
update_owners()
assert(r.owner==u3.faction)
b1.size=3
b2.size=3
update_owners()
assert(r.owner==u2.faction)
b1.size=4
update_owners()
assert(r.owner==u1.faction)
end
local function test_recruit() local function test_recruit()
free_game() free_game()
local r = region.create(0, 0, "plain") local r = region.create(0, 0, "plain")
@ -587,9 +617,11 @@ tests = {
["upkeep"] = test_upkeep, ["upkeep"] = test_upkeep,
["id"] = test_id, ["id"] = test_id,
["work"] = test_work, ["work"] = test_work,
["owners"] = test_owners,
["market"] = test_market ["market"] = test_market
} }
mytests = { mytests = {
["owners"] = test_owners,
["mallorn"] = test_mallorn, ["mallorn"] = test_mallorn,
["recruit2"] = test_recruit2 ["recruit2"] = test_recruit2
} }