refactor some get_param lookups

kill unused global.functions.maintenance
This commit is contained in:
Enno Rehling 2015-09-12 23:58:37 +02:00
parent 1122bb70a4
commit b0c79b9978
4 changed files with 22 additions and 33 deletions

View File

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

View File

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

View File

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

View File

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