Merge pull request #566 from ennorehling/develop

created a new configuration caching API
This commit is contained in:
Enno Rehling 2016-09-11 12:51:41 +02:00 committed by GitHub
commit 8f147503e5
8 changed files with 77 additions and 14 deletions

View File

@ -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 potion_water_of_life(unit * u, region *r, int amount) {
static int config;
static int tree_type, tree_count;
int wood = 0; 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 */ /* mallorn is required to make mallorn forests, wood for regular ones */
if (fval(r, RF_MALLORN)) { if (fval(r, RF_MALLORN)) {
wood = use_pooled(u, rt_find("mallorn"), wood = use_pooled(u, rt_find("mallorn"),

View File

@ -141,6 +141,7 @@ static int rule_goblin_bonus;
static int rule_tactics_formula; static int rule_tactics_formula;
static int rule_nat_armor; static int rule_nat_armor;
static int rule_cavalry_mode; static int rule_cavalry_mode;
static int rule_vampire;
static const curse_type *peace_ct, *slave_ct, *calm_ct; 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_anon_battle = config_get_int("rules.stealth.anon_battle", 1) != 0;
rule_cavalry_mode = config_get_int("rules.cavalry.mode", 1); rule_cavalry_mode = config_get_int("rules.cavalry.mode", 1);
rule_cavalry_skill = config_get_int("rules.cavalry.skill", 2); 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", rule_loot = config_get_int("rules.combat.loot",
LOOT_MONSTERS | LOOT_OTHERS | LOOT_KEEPLOOT); LOOT_MONSTERS | LOOT_OTHERS | LOOT_KEEPLOOT);
/* new formula to calculate to-hit-chance */ /* 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) static void vampirism(troop at, int damage)
{ {
int vampire = config_get_int("rules.combat.demon_vampire", 0); if (rule_vampire > 0) {
if (vampire > 0) { int gain = damage / rule_vampire;
int gain = damage / vampire; int chance = damage - rule_vampire * gain;
int chance = damage - vampire * gain; if (chance > 0 && (rng_int() % rule_vampire < chance))
if (chance > 0 && (rng_int() % vampire < chance))
++gain; ++gain;
if (gain > 0) { if (gain > 0) {
int maxhp = unit_max_hp(at.fighter->unit); int maxhp = unit_max_hp(at.fighter->unit);

View File

@ -419,8 +419,9 @@ int value)
int roqf_factor(void) int roqf_factor(void)
{ {
int value = -1; static int config;
if (value < 0) { static int value;
if (config_changed(&config)) {
value = config_get_int("rules.economy.roqf", 10); value = config_get_int("rules.economy.roqf", 10);
} }
return value; return value;

View File

@ -394,10 +394,19 @@ static void test_build_destroy_cmd(CuTest *tc) {
test_cleanup(); 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 *get_build_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_build_limits); 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_low_skill);
SUITE_ADD_TEST(suite, test_build_failure_missing_skill); SUITE_ADD_TEST(suite, test_build_failure_missing_skill);
SUITE_ADD_TEST(suite, test_build_requires_materials); SUITE_ADD_TEST(suite, test_build_requires_materials);

View File

@ -887,14 +887,23 @@ int rule_blessed_harvest(void)
int rule_alliance_limit(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); assert(rule >= 0);
return rule; return rule;
} }
int rule_faction_limit(void) 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); assert(rule >= 0);
return rule; return rule;
} }
@ -1053,8 +1062,19 @@ bool markets_module(void)
} }
static struct param *configuration; 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) { void config_set(const char *key, const char *value) {
++config_cache_key;
set_param(&configuration, key, value); set_param(&configuration, key, value);
} }
@ -1077,6 +1097,7 @@ bool config_token(const char *key, const char *tok) {
void free_config(void) { void free_config(void) {
global.functions.wage = NULL; global.functions.wage = NULL;
free_params(&configuration); free_params(&configuration);
++config_cache_key;
} }
/** releases all memory associated with the game state. /** releases all memory associated with the game state.

View File

@ -64,7 +64,6 @@ struct param;
int lovar(double xpct_x2); int lovar(double xpct_x2);
/* returns a value between [0..xpct_2], generated with two dice */ /* 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); void init_locale(struct locale *lang);
int forbiddenid(int id); int forbiddenid(int id);
@ -184,6 +183,7 @@ struct param;
int config_get_int(const char *key, int def); int config_get_int(const char *key, int def);
double config_get_flt(const char *key, double def); double config_get_flt(const char *key, double def);
bool config_token(const char *key, const char *tok); 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); char * join_path(const char *p1, const char *p2, char *dst, size_t len);
bool ExpensiveMigrants(void); bool ExpensiveMigrants(void);

View File

@ -190,9 +190,33 @@ static void test_default_order(CuTest *tc) {
test_cleanup(); 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));
free_config();
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 *get_config_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_config_cache);
SUITE_ADD_TEST(suite, test_get_set_param); SUITE_ADD_TEST(suite, test_get_set_param);
SUITE_ADD_TEST(suite, test_param_int); SUITE_ADD_TEST(suite, test_param_int);
SUITE_ADD_TEST(suite, test_param_flt); SUITE_ADD_TEST(suite, test_param_flt);
@ -200,5 +224,6 @@ CuSuite *get_config_suite(void)
SUITE_ADD_TEST(suite, test_getunit); SUITE_ADD_TEST(suite, test_getunit);
SUITE_ADD_TEST(suite, test_read_unitid); SUITE_ADD_TEST(suite, test_read_unitid);
SUITE_ADD_TEST(suite, test_default_order); SUITE_ADD_TEST(suite, test_default_order);
SUITE_ADD_TEST(suite, test_rules);
return suite; return suite;
} }

View File

@ -776,6 +776,8 @@ void demographics(void)
region *r; region *r;
static int last_weeks_season = -1; static int last_weeks_season = -1;
static int current_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) { if (current_season < 0) {
gamedate date; gamedate date;
@ -793,11 +795,10 @@ void demographics(void)
/* die Nachfrage nach Produkten steigt. */ /* die Nachfrage nach Produkten steigt. */
struct demand *dmd; struct demand *dmd;
if (r->land) { if (r->land) {
int plant_rules = config_get_int("rules.grow.formula", 2);
for (dmd = r->land->demands; dmd; dmd = dmd->next) { for (dmd = r->land->demands; dmd; dmd = dmd->next) {
if (dmd->value > 0 && dmd->value < MAXDEMAND) { if (dmd->value > 0 && dmd->value < MAXDEMAND) {
float rise = DMRISE; float rise = DMRISE;
if (buildingtype_exists(r, bt_find("harbour"), true)) if (buildingtype_exists(r, bt_harbour, true))
rise = DMRISEHAFEN; rise = DMRISEHAFEN;
if (rng_double() < rise) if (rng_double() < rise)
++dmd->value; ++dmd->value;