From f75be76ee145f7fd838a9a866e9aecdf88e36ed3 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 11 Sep 2016 12:15:53 +0200 Subject: [PATCH 1/2] created a new configuration caching API (config_changed). applied this to some rules. --- src/kernel/config.c | 24 ++++++++++++++++++++++-- src/kernel/config.h | 1 + src/kernel/config.test.c | 23 +++++++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/kernel/config.c b/src/kernel/config.c index 89d816f0f..481a68220 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -887,14 +887,23 @@ int rule_blessed_harvest(void) int rule_alliance_limit(void) { - int rule = config_get_int("rules.limit.alliance", 0); + static int cache_token; + static int rule = 0; + + if (config_changed(&cache_token)) { + rule = config_get_int("rules.limit.alliance", 0); + } assert(rule >= 0); return rule; } int rule_faction_limit(void) { - int rule = config_get_int("rules.limit.faction", 0); + static int cache_token; + static int rule = 0; + if (config_changed(&cache_token)) { + rule = config_get_int("rules.limit.faction", 0); + } assert(rule >= 0); return rule; } @@ -1053,8 +1062,19 @@ bool markets_module(void) } static struct param *configuration; +static int config_cache_key = 1; + +bool config_changed(int *cache_key) { + assert(cache_key); + if (config_cache_key != *cache_key) { + *cache_key = config_cache_key; + return true; + } + return false; +} void config_set(const char *key, const char *value) { + ++config_cache_key; set_param(&configuration, key, value); } diff --git a/src/kernel/config.h b/src/kernel/config.h index 081a96e61..8e0bd72e5 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -184,6 +184,7 @@ struct param; int config_get_int(const char *key, int def); double config_get_flt(const char *key, double def); bool config_token(const char *key, const char *tok); + bool config_changed(int *cache_key); char * join_path(const char *p1, const char *p2, char *dst, size_t len); bool ExpensiveMigrants(void); diff --git a/src/kernel/config.test.c b/src/kernel/config.test.c index 6bc193bbf..a5bad30c7 100644 --- a/src/kernel/config.test.c +++ b/src/kernel/config.test.c @@ -190,9 +190,31 @@ static void test_default_order(CuTest *tc) { test_cleanup(); } +static void test_config_cache(CuTest *tc) { + int key = 0; + + test_setup(); + CuAssertTrue(tc, config_changed(&key)); + config_set("hodor", "0"); + CuAssertTrue(tc, config_changed(&key)); + CuAssertTrue(tc, !config_changed(&key)); + test_cleanup(); +} + +static void test_rules(CuTest *tc) { + CuAssertIntEquals(tc, 0, rule_alliance_limit()); + config_set("rules.limit.alliance", "1"); + CuAssertIntEquals(tc, 1, rule_alliance_limit()); + + CuAssertIntEquals(tc, 0, rule_faction_limit()); + config_set("rules.limit.faction", "1000"); + CuAssertIntEquals(tc, 1000, rule_faction_limit()); +} + CuSuite *get_config_suite(void) { CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_config_cache); SUITE_ADD_TEST(suite, test_get_set_param); SUITE_ADD_TEST(suite, test_param_int); SUITE_ADD_TEST(suite, test_param_flt); @@ -200,5 +222,6 @@ CuSuite *get_config_suite(void) SUITE_ADD_TEST(suite, test_getunit); SUITE_ADD_TEST(suite, test_read_unitid); SUITE_ADD_TEST(suite, test_default_order); + SUITE_ADD_TEST(suite, test_rules); return suite; } From 86ef225ccbb29232a87ad7db5e28b0f77d08c759 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 11 Sep 2016 12:48:00 +0200 Subject: [PATCH 2/2] add some more local caching. --- src/alchemy.c | 9 +++++++-- src/battle.c | 11 ++++++----- src/kernel/build.c | 5 +++-- src/kernel/build.test.c | 9 +++++++++ src/kernel/config.c | 1 + src/kernel/config.h | 1 - src/kernel/config.test.c | 2 ++ src/laws.c | 5 +++-- 8 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/alchemy.c b/src/alchemy.c index 5e369a3e1..cf4dc6f73 100644 --- a/src/alchemy.c +++ b/src/alchemy.c @@ -123,9 +123,14 @@ static void end_potion(unit * u, const potion_type * ptype, int amount) } static int potion_water_of_life(unit * u, region *r, int amount) { + static int config; + static int tree_type, tree_count; int wood = 0; - int tree_type = config_get_int("rules.magic.wol_type", 1); - int tree_count = config_get_int("rules.magic.wol_effect", 10); + + if (config_changed(&config)) { + tree_type = config_get_int("rules.magic.wol_type", 1); + tree_count = config_get_int("rules.magic.wol_effect", 10); + } /* mallorn is required to make mallorn forests, wood for regular ones */ if (fval(r, RF_MALLORN)) { wood = use_pooled(u, rt_find("mallorn"), diff --git a/src/battle.c b/src/battle.c index 3ab117c2a..4686a942a 100644 --- a/src/battle.c +++ b/src/battle.c @@ -141,6 +141,7 @@ static int rule_goblin_bonus; static int rule_tactics_formula; static int rule_nat_armor; static int rule_cavalry_mode; +static int rule_vampire; static const curse_type *peace_ct, *slave_ct, *calm_ct; @@ -159,6 +160,7 @@ static void init_rules(void) rule_anon_battle = config_get_int("rules.stealth.anon_battle", 1) != 0; rule_cavalry_mode = config_get_int("rules.cavalry.mode", 1); rule_cavalry_skill = config_get_int("rules.cavalry.skill", 2); + rule_vampire = config_get_int("rules.combat.demon_vampire", 0); rule_loot = config_get_int("rules.combat.loot", LOOT_MONSTERS | LOOT_OTHERS | LOOT_KEEPLOOT); /* new formula to calculate to-hit-chance */ @@ -1009,11 +1011,10 @@ const char *rel_dam(int dam, int hp) static void vampirism(troop at, int damage) { - int vampire = config_get_int("rules.combat.demon_vampire", 0); - if (vampire > 0) { - int gain = damage / vampire; - int chance = damage - vampire * gain; - if (chance > 0 && (rng_int() % vampire < chance)) + if (rule_vampire > 0) { + int gain = damage / rule_vampire; + int chance = damage - rule_vampire * gain; + if (chance > 0 && (rng_int() % rule_vampire < chance)) ++gain; if (gain > 0) { int maxhp = unit_max_hp(at.fighter->unit); diff --git a/src/kernel/build.c b/src/kernel/build.c index 6bea3e6b4..6bc953241 100644 --- a/src/kernel/build.c +++ b/src/kernel/build.c @@ -419,8 +419,9 @@ int value) int roqf_factor(void) { - int value = -1; - if (value < 0) { + static int config; + static int value; + if (config_changed(&config)) { value = config_get_int("rules.economy.roqf", 10); } return value; diff --git a/src/kernel/build.test.c b/src/kernel/build.test.c index c0775051e..9ea473725 100644 --- a/src/kernel/build.test.c +++ b/src/kernel/build.test.c @@ -394,10 +394,19 @@ static void test_build_destroy_cmd(CuTest *tc) { test_cleanup(); } +static void test_build_roqf_factor(CuTest *tc) { + test_setup(); + CuAssertIntEquals(tc, 10, roqf_factor()); + config_set("rules.economy.roqf", "50"); + CuAssertIntEquals(tc, 50, roqf_factor()); + test_cleanup(); +} + CuSuite *get_build_suite(void) { CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_build_limits); + SUITE_ADD_TEST(suite, test_build_roqf_factor); SUITE_ADD_TEST(suite, test_build_failure_low_skill); SUITE_ADD_TEST(suite, test_build_failure_missing_skill); SUITE_ADD_TEST(suite, test_build_requires_materials); diff --git a/src/kernel/config.c b/src/kernel/config.c index 481a68220..135df78c5 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -1097,6 +1097,7 @@ bool config_token(const char *key, const char *tok) { void free_config(void) { global.functions.wage = NULL; free_params(&configuration); + ++config_cache_key; } /** releases all memory associated with the game state. diff --git a/src/kernel/config.h b/src/kernel/config.h index 8e0bd72e5..74565e25e 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -64,7 +64,6 @@ struct param; int lovar(double xpct_x2); /* returns a value between [0..xpct_2], generated with two dice */ - int distribute(int old, int new_value, int n); void init_locale(struct locale *lang); int forbiddenid(int id); diff --git a/src/kernel/config.test.c b/src/kernel/config.test.c index a5bad30c7..c8fc84a1a 100644 --- a/src/kernel/config.test.c +++ b/src/kernel/config.test.c @@ -198,6 +198,8 @@ static void test_config_cache(CuTest *tc) { config_set("hodor", "0"); CuAssertTrue(tc, config_changed(&key)); CuAssertTrue(tc, !config_changed(&key)); + free_config(); + CuAssertTrue(tc, config_changed(&key)); test_cleanup(); } diff --git a/src/laws.c b/src/laws.c index 197433bfa..19078353e 100755 --- a/src/laws.c +++ b/src/laws.c @@ -776,6 +776,8 @@ void demographics(void) region *r; static int last_weeks_season = -1; static int current_season = -1; + int plant_rules = config_get_int("rules.grow.formula", 2); + const struct building_type *bt_harbour = bt_find("harbour"); if (current_season < 0) { gamedate date; @@ -793,11 +795,10 @@ void demographics(void) /* die Nachfrage nach Produkten steigt. */ struct demand *dmd; if (r->land) { - int plant_rules = config_get_int("rules.grow.formula", 2); for (dmd = r->land->demands; dmd; dmd = dmd->next) { if (dmd->value > 0 && dmd->value < MAXDEMAND) { float rise = DMRISE; - if (buildingtype_exists(r, bt_find("harbour"), true)) + if (buildingtype_exists(r, bt_harbour, true)) rise = DMRISEHAFEN; if (rng_double() < rise) ++dmd->value;