From 8ffafd2815d5ecd9b759edab2cd28396b5f33562 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 20 Jul 2009 05:47:37 +0000 Subject: [PATCH] owner update fix and test cases --- src/common/gamecode/laws.c | 20 +++++++++++--------- src/eressea/tolua/bind_region.c | 24 ++++++++++++++++++++++++ src/scripts/tests.lua | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 9 deletions(-) diff --git a/src/common/gamecode/laws.c b/src/common/gamecode/laws.c index 57b2b8775..63e8f065b 100644 --- a/src/common/gamecode/laws.c +++ b/src/common/gamecode/laws.c @@ -3068,16 +3068,18 @@ static double rc_popularity(const struct race * rc) void update_owners(region * r) { - building * blargest = NULL; - blargest = largestbuilding(r, &is_tax_building, false); + building * bowner = largestbuilding(r, &is_owner_building, false); + building * blargest = largestbuilding(r, &is_tax_building, false); if (blargest) { - /* region owners update? */ - faction * f = region_get_owner(r); - unit * u = buildingowner(r, blargest); - if (u==NULL) { - region_set_owner(r, NULL, turn); - } else if (u->faction!=f) { - region_set_owner(r, u->faction, turn); + if (!bowner || bowner->sizesize) { + /* region owners update? */ + faction * f = region_get_owner(r); + unit * u = buildingowner(r, blargest); + if (u==NULL) { + region_set_owner(r, NULL, turn); + } else if (u->faction!=f) { + region_set_owner(r, u->faction, turn); + } } } } diff --git a/src/eressea/tolua/bind_region.c b/src/eressea/tolua/bind_region.c index f6ae5b4a7..75146b928 100644 --- a/src/eressea/tolua/bind_region.c +++ b/src/eressea/tolua/bind_region.c @@ -118,6 +118,29 @@ tolua_region_get_terrainname(lua_State* L) 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 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_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 "set_key", tolua_region_setkey); diff --git a/src/scripts/tests.lua b/src/scripts/tests.lua index 0890fad2d..ac90f1cdc 100644 --- a/src/scripts/tests.lua +++ b/src/scripts/tests.lua @@ -244,6 +244,36 @@ local function test_recruit2() print(u.number) 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() free_game() local r = region.create(0, 0, "plain") @@ -587,9 +617,11 @@ tests = { ["upkeep"] = test_upkeep, ["id"] = test_id, ["work"] = test_work, + ["owners"] = test_owners, ["market"] = test_market } mytests = { + ["owners"] = test_owners, ["mallorn"] = test_mallorn, ["recruit2"] = test_recruit2 }