From 423e2937456c0956fac09808c6d28a0a88edab13 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 23 Sep 2016 20:36:57 +0200 Subject: [PATCH] some more config lookup caching --- src/kernel/ally.c | 26 ++++++++++++++------------ src/kernel/config.c | 7 +++++-- src/kernel/faction.c | 22 +++++++++++++++++----- src/kernel/skills.c | 8 ++++++-- src/kernel/unit.c | 17 +++++++++++------ src/magic.c | 6 +++++- src/monsters.c | 7 ++++++- src/study.c | 12 ++++++++++-- src/teleport.c | 8 ++++++-- 9 files changed, 80 insertions(+), 33 deletions(-) diff --git a/src/kernel/ally.c b/src/kernel/ally.c index 3152ab4b7..2bb980dd0 100644 --- a/src/kernel/ally.c +++ b/src/kernel/ally.c @@ -133,19 +133,21 @@ attrib_type at_npcfaction = { */ int HelpMask(void) { - const char *str = config_get("rules.help.mask"); - int rule = 0; - if (str != NULL) { - char *sstr = _strdup(str); - char *tok = strtok(sstr, " "); - while (tok) { - rule |= ally_flag(tok, -1); - tok = strtok(NULL, " "); + static int config, rule = 0; + if (config_changed(&config)) { + const char *str = config_get("rules.help.mask"); + if (str != NULL) { + char *sstr = _strdup(str); + char *tok = strtok(sstr, " "); + while (tok) { + rule |= ally_flag(tok, -1); + tok = strtok(NULL, " "); + } + free(sstr); + } + else { + rule = HELP_ALL; } - free(sstr); - } - else { - rule = HELP_ALL; } return rule; } diff --git a/src/kernel/config.c b/src/kernel/config.c index aaecd2c91..5921535c7 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -731,7 +731,7 @@ int create_directories(void) { double get_param_flt(const struct param *p, const char *key, double def) { - const char *str = get_param(p, key); + const char *str = p ? get_param(p, key) : NULL; return str ? atof(str) : def; } @@ -862,7 +862,10 @@ int cmp_current_owner(const building * b, const building * a) bool rule_stealth_other(void) { - int rule = config_get_int("stealth.faction.other", 1); + static int rule, config; + if (config_changed(&config)) { + rule = config_get_int("stealth.faction.other", 1); + } return rule != 0; } diff --git a/src/kernel/faction.c b/src/kernel/faction.c index 823932f88..0ccf62f52 100755 --- a/src/kernel/faction.c +++ b/src/kernel/faction.c @@ -796,14 +796,26 @@ attrib_type at_maxmagicians = { int max_magicians(const faction * f) { - int m = config_get_int("rules.maxskills.magic", MAXMAGICIANS); - attrib *a; + static int rule, config, rc_cache; + static const race *rc_elf; + int m; - if ((a = a_find(f->attribs, &at_maxmagicians)) != NULL) { - m = a->data.i; + if (config_changed(&config)) { + rule = config_get_int("rules.maxskills.magic", MAXMAGICIANS); } - if (f->race == get_race(RC_ELF)) + m = rule; + if (f->attribs) { + attrib *a = a_find(f->attribs, &at_maxmagicians); + if (a) { + m = a->data.i; + } + } + if (rc_changed(&rc_cache)) { + rc_elf = get_race(RC_ELF); + } + if (f->race == rc_elf) { ++m; + } return m; } diff --git a/src/kernel/skills.c b/src/kernel/skills.c index 9a6c0f1d7..17e1194c1 100644 --- a/src/kernel/skills.c +++ b/src/kernel/skills.c @@ -184,9 +184,13 @@ void sk_set(skill * sv, int level) sv->level = level; } -static int rule_random_progress(void) +static bool rule_random_progress(void) { - return config_get_int("study.random_progress", 1); + static int rule, config; + if (config_changed(&config)) { + rule = config_get_int("study.random_progress", 1); + } + return rule != 0; } int skill_weeks(int level) diff --git a/src/kernel/unit.c b/src/kernel/unit.c index 4623e895e..c5172cafc 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -883,15 +883,16 @@ void leave_building(unit * u) bool can_leave(unit * u) { - int rule_leave; + static int config; + static bool rule_leave; if (!u->building) { return true; } - - rule_leave = config_get_int("rules.move.owner_leave", 0); - - if (rule_leave != 0 && u->building && u == building_owner(u->building)) { + if (config_changed(&config)) { + rule_leave = config_get_int("rules.move.owner_leave", 0) != 0; + } + if (rule_leave && u->building && u == building_owner(u->building)) { return false; } return true; @@ -1721,9 +1722,13 @@ int unit_max_hp(const unit * u) { int h; double p; - int rule_stamina = config_get_int("rules.stamina", STAMINA_AFFECTS_HP); + static int config; + static int rule_stamina; h = u_race(u)->hitpoints; + if (config_changed(&config)) { + rule_stamina = config_get_int("rules.stamina", STAMINA_AFFECTS_HP); + } if (rule_stamina & 1) { p = pow(effskill(u, SK_STAMINA, u->region) / 2.0, 1.5) * 0.2; h += (int)(h * p + 0.5); diff --git a/src/magic.c b/src/magic.c index 83899f583..bcf3403aa 100644 --- a/src/magic.c +++ b/src/magic.c @@ -225,7 +225,11 @@ static void free_mage(attrib * a) bool FactionSpells(void) { - return config_get_int("rules.magic.factionlist", 0) != 0; + static int config, rule; + if (config_changed(&config)) { + rule = config_get_int("rules.magic.factionlist", 0); + } + return rule != 0; } void read_spells(struct quicklist **slistp, magic_t mtype, diff --git a/src/monsters.c b/src/monsters.c index 78999e015..ecf768cab 100644 --- a/src/monsters.c +++ b/src/monsters.c @@ -84,7 +84,12 @@ static void give_peasants(unit *u, const item_type *itype, int reduce) { } static double random_move_chance(void) { - return config_get_flt("rules.monsters.random_move_chance", MOVECHANCE); + static double rule; + static int config; + if (config_changed(&config)) { + rule = config_get_flt("rules.monsters.random_move_chance", MOVECHANCE); + } + return rule; } static void reduce_weight(unit * u) diff --git a/src/study.c b/src/study.c index eb5cec12d..027f67dde 100644 --- a/src/study.c +++ b/src/study.c @@ -800,7 +800,11 @@ int study_cmd(unit * u, order * ord) } static int produceexp_days(void) { - return config_get_int("study.produceexp", 10); + static int config, rule; + if (config_changed(&config)) { + rule = config_get_int("study.produceexp", 10); + } + return rule; } void produceexp_ex(struct unit *u, skill_t sk, int n, learn_fun learn) @@ -863,7 +867,11 @@ void demon_skillchange(unit *u) if (fval(u, UFL_HUNGER)) { /* hungry demons only go down, never up in skill */ - int rule_hunger = config_get_int("hunger.demon.skill", 0) != 0; + static int config; + static bool rule_hunger; + if (config_changed(&config)) { + rule_hunger = config_get_int("hunger.demon.skill", 0) != 0; + } if (rule_hunger) { upchance = 0; downchance = 15; diff --git a/src/teleport.c b/src/teleport.c index 5b7f71831..c7e775345 100644 --- a/src/teleport.c +++ b/src/teleport.c @@ -176,8 +176,12 @@ bool is_astral(const region * r) plane *get_astralplane(void) { plane *astralspace = 0; - int rule_astralplane = config_get_int("modules.astralspace", 1); - + static int config; + static bool rule_astralplane; + + if (config_changed(&config)) { + rule_astralplane = config_get_int("modules.astralspace", 1) != 0; + } if (!rule_astralplane) { return NULL; }