simplify building_taxes, eliminate dead lua callbacks.

This commit is contained in:
Enno Rehling 2017-04-29 19:21:48 +02:00
parent 3b9a6e45a1
commit 7a289ceb86
11 changed files with 31 additions and 110 deletions

View File

@ -1,6 +1,5 @@
<?xml version="1.0"?>
<building name="castle" capacity="1" fort="yes">
<function name="taxes" value="lua_building_taxes"/>
<building name="castle" capacity="1" fort="yes" taxes="100">
<construction skill="building" minskill="1" maxsize="10" name="site">
<requirement type="stone" quantity="1"/>
</construction>

View File

@ -3,8 +3,7 @@
<xi:include href="config://default/buildings/castle-2.xml" />
<building name="watch" maxsize="10" capacity="1" fort="yes">
<function name="taxes" value="lua_building_taxes"/>
<building name="watch" maxsize="10" capacity="1" fort="yes" taxes="200">
<construction skill="building" minskill="1" maxsize="5" name="scaffolding">
<requirement type="log" quantity="1"/>
</construction>

View File

@ -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

View File

@ -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;

View File

@ -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");

View File

@ -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;

View File

@ -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);

View File

@ -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);

View File

@ -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);
}

View File

@ -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);

View File

@ -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