From 7a289ceb860df735b953856cd4ee6db70a53fe9b Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 29 Apr 2017 19:21:48 +0200 Subject: [PATCH] simplify building_taxes, eliminate dead lua callbacks. --- res/buildings/castle-2.xml | 3 +- res/e3a/buildings.xml | 3 +- scripts/eressea/e3/rules.lua | 10 ------- src/economy.c | 8 +++--- src/helpers.c | 56 ------------------------------------ src/kernel/building.c | 21 +++++--------- src/kernel/building.h | 4 +-- src/kernel/building.test.c | 23 ++++++++------- src/kernel/xmlreader.c | 4 +-- src/laws.test.c | 6 +--- src/morale.c | 3 +- 11 files changed, 31 insertions(+), 110 deletions(-) diff --git a/res/buildings/castle-2.xml b/res/buildings/castle-2.xml index 989832940..7e64f88af 100644 --- a/res/buildings/castle-2.xml +++ b/res/buildings/castle-2.xml @@ -1,6 +1,5 @@ - - + diff --git a/res/e3a/buildings.xml b/res/e3a/buildings.xml index 27b94dbbe..8641fd89e 100644 --- a/res/e3a/buildings.xml +++ b/res/e3a/buildings.xml @@ -3,8 +3,7 @@ - - + diff --git a/scripts/eressea/e3/rules.lua b/scripts/eressea/e3/rules.lua index e7a54ff41..e49e03438 100644 --- a/scripts/eressea/e3/rules.lua +++ b/scripts/eressea/e3/rules.lua @@ -1,13 +1,3 @@ -function building_taxes(b, blevel) - btype = b.type - if btype=="castle" then - return blevel * 0.01 - elseif btype=="watch" then - return blevel * 0.005 - end - return 0.0 -end - -- the "raindance" spell function raindance(r, mage, level, force) if (create_curse(mage, r, "blessedharvest", force, 1+force*2, 100 * force)) then diff --git a/src/economy.c b/src/economy.c index 9f9a6815e..1a4cc5b18 100644 --- a/src/economy.c +++ b/src/economy.c @@ -2929,7 +2929,7 @@ static void peasant_taxes(region * r) unit *u; building *b; int money; - int maxsize; + int level; f = region_get_owner(r); if (f == NULL || is_mourning(r, turn)) { @@ -2947,9 +2947,9 @@ static void peasant_taxes(region * r) if (u == NULL || u->faction != f) return; - maxsize = buildingeffsize(b, false); - if (maxsize > 0) { - double taxfactor = money / building_taxes(b, maxsize); + level = buildingeffsize(b, false); + if (level > 0) { + double taxfactor = money * level / building_taxes(b); double morale = money * region_get_morale(r) / MORALE_TAX_FACTOR; if (taxfactor > morale) { taxfactor = morale; diff --git a/src/helpers.c b/src/helpers.c index 402ecdd48..59695e9b4 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -352,58 +352,6 @@ lua_wage(const region * r, const faction * f, const race * rc, int in_turn) return result; } -static void lua_agebuilding(building * b) -{ - lua_State *L = (lua_State *)global.vm_state; - char fname[64]; - - strlcpy(fname, "age_", sizeof(fname)); - strlcat(fname, b->type->_name, sizeof(fname)); - - lua_getglobal(L, fname); - if (lua_isfunction(L, -1)) { - tolua_pushusertype(L, (void *)b, TOLUA_CAST "building"); - - if (lua_pcall(L, 1, 0, 0) != 0) { - const char *error = lua_tostring(L, -1); - log_error("agebuilding(%s) calling '%s': %s.\n", buildingname(b), fname, error); - lua_pop(L, 1); - } - } - else { - log_error("agebuilding(%s) calling '%s': not a function.\n", buildingname(b), fname); - lua_pop(L, 1); - } -} - -static double lua_building_taxes(building * b, int level) -{ - lua_State *L = (lua_State *)global.vm_state; - const char *fname = "building_taxes"; - double result = 0.0F; - - lua_getglobal(L, fname); - if (lua_isfunction(L, -1)) { - tolua_pushusertype(L, (void *)b, TOLUA_CAST "building"); - lua_pushinteger(L, level); - - if (lua_pcall(L, 2, 1, 0) != 0) { - const char *error = lua_tostring(L, -1); - log_error("building_taxes(%s) calling '%s': %s.\n", buildingname(b), fname, error); - lua_pop(L, 1); - } - else { - result = (double)lua_tonumber(L, -1); - lua_pop(L, 1); - } - } - else { - log_error("building_taxes(%s) calling '%s': not a function.\n", buildingname(b), fname); - lua_pop(L, 1); - } - return result; -} - static int lua_maintenance(const unit * u) { lua_State *L = (lua_State *)global.vm_state; @@ -526,10 +474,6 @@ void register_tolua_helpers(void) at_register(&at_direction); at_register(&at_building_action); - register_function((pf_generic)lua_building_taxes, - TOLUA_CAST "lua_building_taxes"); - register_function((pf_generic)lua_agebuilding, - TOLUA_CAST "lua_agebuilding"); register_function((pf_generic)lua_callspell, TOLUA_CAST "lua_castspell"); register_function((pf_generic)lua_initfamiliar, TOLUA_CAST "lua_initfamiliar"); diff --git a/src/kernel/building.c b/src/kernel/building.c index 2cbd8e74d..2107551de 100644 --- a/src/kernel/building.c +++ b/src/kernel/building.c @@ -469,7 +469,7 @@ int bt_effsize(const building_type * btype, const building * b, int bsize) bsize = adjust_size(b, bsize); } - if (!cons || !cons->improvement) { + if (!cons) { return 0; } @@ -784,16 +784,9 @@ bool is_owner_building(const struct building * b) return false; } -int building_taxes(const building *b, int bsize) { +int building_taxes(const building *b) { assert(b); - if (b->type->taxes) { - int level = buildingeffsize(b, false); - double tax = b->type->taxes(b, level); - if (tax > 0) { - return (int)(0.5 + 1 / tax); - } - } - return 0; + return b->type->taxes; } @@ -806,8 +799,8 @@ int cmp_taxes(const building * b, const building * a) return -1; } else if (a) { - int newtaxes = building_taxes(b, b->size); - int oldtaxes = building_taxes(a, a->size); + int newtaxes = building_taxes(b); + int oldtaxes = building_taxes(a); if (newtaxes > oldtaxes) return -1; @@ -844,8 +837,8 @@ int cmp_current_owner(const building * b, const building * a) if (!u || u->faction != f) return -1; if (a) { - int newtaxes = building_taxes(b, b->size); - int oldtaxes = building_taxes(a, a->size); + int newtaxes = building_taxes(b); + int oldtaxes = building_taxes(a); if (newtaxes > oldtaxes) { return 1; diff --git a/src/kernel/building.h b/src/kernel/building.h index fd4c38ee7..edb46edfd 100644 --- a/src/kernel/building.h +++ b/src/kernel/building.h @@ -63,6 +63,7 @@ extern "C" { variant magres; /* how well it resists against spells */ int magresbonus; /* bonus it gives the target against spells */ int fumblebonus; /* bonus that reduces fumbling */ + int taxes; /* receive $1 tax per `taxes` in region */ double auraregen; /* modifier for aura regeneration inside building */ struct maintenance *maintenance; /* array of requirements */ struct construction *construction; /* construction of 1 building-level */ @@ -70,7 +71,6 @@ extern "C" { const char *(*name) (const struct building_type *, const struct building * b, int size); - double(*taxes) (const struct building *, int level); struct attrib *attribs; } building_type; @@ -138,7 +138,7 @@ extern "C" { int cmp_taxes(const struct building *b, const struct building *bother); int cmp_current_owner(const struct building *b, const struct building *bother); - int building_taxes(const building *b, int bsize); + int building_taxes(const building *b); /* old functions, still in build.c: */ int buildingeffsize(const building * b, int imaginary); diff --git a/src/kernel/building.test.c b/src/kernel/building.test.c index 5cee76813..2b546cd79 100644 --- a/src/kernel/building.test.c +++ b/src/kernel/building.test.c @@ -432,11 +432,6 @@ static void test_cmp_castle_size(CuTest *tc) { test_cleanup(); } -static double tax_cb(const building *b, int level) { - UNUSED_ARG(level); - return b->size * 0.01; -} - static void test_cmp_wage(CuTest *tc) { region *r; building *b1, *b2; @@ -444,7 +439,7 @@ static void test_cmp_wage(CuTest *tc) { test_setup(); btype = test_create_buildingtype("castle"); - btype->taxes = tax_cb; + btype->taxes = 100; r = test_create_region(0, 0, 0); b1 = test_create_building(r, btype); b2 = test_create_building(r, btype); @@ -465,7 +460,7 @@ static void test_cmp_taxes(CuTest *tc) { test_setup(); btype = test_create_buildingtype("castle"); - btype->taxes = tax_cb; + btype->taxes = 100; r = test_create_region(0, 0, 0); b1 = test_create_building(r, btype); b2 = test_create_building(r, btype); @@ -490,13 +485,19 @@ static void test_cmp_current_owner(CuTest *tc) { test_setup(); config_set("rules.region_owners", "1"); - btype = test_create_buildingtype("castle"); - btype->taxes = tax_cb; r = test_create_region(0, 0, 0); + btype = test_create_buildingtype("watch"); + btype->construction->maxsize = 1; + btype->taxes = 200; b1 = test_create_building(r, btype); + btype = test_create_buildingtype("castle"); + btype->construction->maxsize = 1; + btype->taxes = 100; b2 = test_create_building(r, btype); - b1->size = 5; - b2->size = 10; + b1->size = 1; + CuAssertIntEquals(tc, 1, buildingeffsize(b1, false)); + b2->size = 1; + CuAssertIntEquals(tc, 1, buildingeffsize(b2, false)); u1 = test_create_unit(test_create_faction(0), r); u_set_building(u1, b1); u2 = test_create_unit(test_create_faction(0), r); diff --git a/src/kernel/xmlreader.c b/src/kernel/xmlreader.c index 8d4c51176..e29f2811c 100644 --- a/src/kernel/xmlreader.c +++ b/src/kernel/xmlreader.c @@ -320,6 +320,7 @@ static int parse_buildings(xmlDocPtr doc) btype->magresbonus = xml_ivalue(node, "magresbonus", btype->magresbonus); btype->fumblebonus = xml_ivalue(node, "fumblebonus", btype->fumblebonus); btype->auraregen = xml_fvalue(node, "auraregen", btype->auraregen); + btype->taxes = xml_ivalue(node, "taxes", btype->taxes); if (xml_bvalue(node, "nodestroy", false)) btype->flags |= BTF_INDESTRUCTIBLE; @@ -369,9 +370,6 @@ static int parse_buildings(xmlDocPtr doc) (const char *(*)(const struct building_type *, const struct building *, int))fun; } - else if (strcmp((const char *)propValue, "taxes") == 0) { - btype->taxes = (double(*)(const struct building *, int))fun; - } else { log_error("unknown function type '%s' for building %s\n", (const char *)propValue, btype->_name); } diff --git a/src/laws.test.c b/src/laws.test.c index ce8ca2c19..36aafb4cb 100644 --- a/src/laws.test.c +++ b/src/laws.test.c @@ -525,10 +525,6 @@ struct pay_fixture { unit *u2; }; -static double level_taxes(const building * b, int level) { - return b->size * level * 2.0; -} - static void setup_pay_cmd(struct pay_fixture *fix) { faction *f; region *r; @@ -540,7 +536,7 @@ static void setup_pay_cmd(struct pay_fixture *fix) { r = findregion(0, 0); assert(r && f); btcastle = test_create_buildingtype("castle"); - btcastle->taxes = level_taxes; + btcastle->taxes = 100; b = test_create_building(r, btcastle); assert(b); fix->u1 = test_create_unit(f, r); diff --git a/src/morale.c b/src/morale.c index 47476fdf8..8b0a8c6ee 100644 --- a/src/morale.c +++ b/src/morale.c @@ -46,7 +46,8 @@ void morale_update(region *r) { building *b = largestbuilding(r, cmp_taxes, false); if (b) { int bsize = buildingeffsize(b, false); - maxmorale = (int)(0.5 + b->type->taxes(b, bsize + 1) * MORALE_TAX_FACTOR); + assert(b->type->taxes>0); + maxmorale = (bsize + 1) * MORALE_TAX_FACTOR / b->type->taxes; } if (morale < maxmorale) { if (stability > MORALE_COOLDOWN && r->land->ownership->owner