From b0c79b997850549c1b8b90846d8db2c7252fd673 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 12 Sep 2015 23:58:37 +0200 Subject: [PATCH] refactor some get_param lookups kill unused global.functions.maintenance --- src/kernel/config.c | 38 +++++++++++++++----------------------- src/kernel/config.h | 13 +++++++------ src/kernel/xmlreader.c | 3 --- src/tests.c | 1 - 4 files changed, 22 insertions(+), 33 deletions(-) diff --git a/src/kernel/config.c b/src/kernel/config.c index 1db17a495..4a3f41305 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -110,13 +110,12 @@ int turn = -1; int NewbieImmunity(void) { - static int value = -1; - static int gamecookie = -1; - if (value < 0 || gamecookie != global.cookie) { - gamecookie = global.cookie; - value = get_param_int(global.parameters, "NewbieImmunity", 0); + static int update = -1; + if (update != global.cookie) { + update = global.cookie; + global.newbie_immunity_ = get_param_int(global.parameters, "NewbieImmunity", 0); } - return value; + return global.newbie_immunity_; } bool IsImmune(const faction * f) @@ -185,48 +184,46 @@ int AllianceAuto(void) */ int HelpMask(void) { - static int rule = -1; static int gamecookie = -1; - if (rule < 0 || gamecookie != global.cookie) { + if (gamecookie != global.cookie) { const char *str = get_param(global.parameters, "rules.help.mask"); gamecookie = global.cookie; - rule = 0; + global.help_mask_ = 0; if (str != NULL) { char *sstr = _strdup(str); char *tok = strtok(sstr, " "); while (tok) { - rule |= ally_flag(tok, -1); + global.help_mask_ |= ally_flag(tok, -1); tok = strtok(NULL, " "); } free(sstr); } else { - rule = HELP_ALL; + global.help_mask_ = HELP_ALL; } } - return rule; + return global.help_mask_; } int AllianceRestricted(void) { - static int rule = -1; static int gamecookie = -1; - if (rule < 0 || gamecookie != global.cookie) { + if (gamecookie != global.cookie) { const char *str = get_param(global.parameters, "alliance.restricted"); gamecookie = global.cookie; - rule = 0; + global.alliance_restricted_ = 0; if (str != NULL) { char *sstr = _strdup(str); char *tok = strtok(sstr, " "); while (tok) { - rule |= ally_flag(tok, -1); + global.alliance_restricted_ |= ally_flag(tok, -1); tok = strtok(NULL, " "); } free(sstr); } - rule &= HelpMask(); + global.alliance_restricted_ &= HelpMask(); } - return rule; + return global.alliance_restricted_; } int LongHunger(const struct unit *u) @@ -1506,11 +1503,6 @@ int maintenance_cost(const struct unit *u) { if (u == NULL) return MAINTENANCE; - if (global.functions.maintenance) { - int retval = global.functions.maintenance(u); - if (retval >= 0) - return retval; - } return u_race(u)->maintenance * u->number; } diff --git a/src/kernel/config.h b/src/kernel/config.h index 9449744ef..94f1a0354 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -253,16 +253,17 @@ extern "C" { void *vm_state; int data_version; /* TODO: eliminate in favor of gamedata.version */ struct _dictionary_ *inifile; - - struct global_functions { - int(*wage) (const struct region * r, const struct faction * f, - const struct race * rc, int in_turn); - int(*maintenance) (const struct unit * u); - } functions; /* the following are some cached values, because get_param can be slow. * you should almost never need to touch them */ int cookie; double producexpchance_; + int newbie_immunity_; + int alliance_restricted_; + int help_mask_; + struct global_functions { + int(*wage) (const struct region * r, const struct faction * f, + const struct race * rc, int in_turn); + } functions; } settings; typedef struct helpmode { diff --git a/src/kernel/xmlreader.c b/src/kernel/xmlreader.c index 4f6dbf0c0..f9f718a01 100644 --- a/src/kernel/xmlreader.c +++ b/src/kernel/xmlreader.c @@ -891,9 +891,6 @@ static int parse_rules(xmlDocPtr doc) (int(*)(const struct region *, const struct faction *, const struct race *, int))fun; } - else if (strcmp((const char *)propValue, "maintenance") == 0) { - global.functions.maintenance = (int(*)(const struct unit *))fun; - } else { log_error("unknown function for rule '%s'\n", (const char *)propValue); } diff --git a/src/tests.c b/src/tests.c index 596d7b02f..ee400ad2a 100644 --- a/src/tests.c +++ b/src/tests.c @@ -75,7 +75,6 @@ void test_cleanup(void) free_terrains(); free_resources(); - global.functions.maintenance = NULL; global.functions.wage = NULL; free_params(&global.parameters); default_locale = 0;