From efc87a16e39da415fd40a1034faf25ba87257e7c Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 16 Nov 2015 16:57:51 +0100 Subject: [PATCH 01/28] clarify dragon planning code (bug 2159) --- src/monsters.c | 5 ++--- src/reports.c | 9 +++------ 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/monsters.c b/src/monsters.c index 0ab88fa61..541a71c3f 100644 --- a/src/monsters.c +++ b/src/monsters.c @@ -685,14 +685,13 @@ static order *plan_dragon(unit * u) /* dragon gets bored and looks for a different place to go */ ta = set_new_dragon_target(u, u->region, DRAGON_RANGE); } - else - ta = a_find(u->attribs, &at_targetregion); if (ta != NULL) { tr = (region *)ta->data.v; if (tr == NULL || !path_exists(u->region, tr, DRAGON_RANGE, allowed_dragon)) { ta = set_new_dragon_target(u, u->region, DRAGON_RANGE); - if (ta) + if (ta) { tr = findregion(ta->data.sa[0], ta->data.sa[1]); + } } } if (tr != NULL) { diff --git a/src/reports.c b/src/reports.c index ca1cfb0f3..d88769457 100644 --- a/src/reports.c +++ b/src/reports.c @@ -1547,6 +1547,7 @@ static void mkreportdir(const char *rpath) { abort(); } } + errno = 0; } int write_reports(faction * f, time_t ltime) @@ -1563,11 +1564,7 @@ int write_reports(faction * f, time_t ltime) } prepare_report(&ctx, f); get_addresses(&ctx); - mkreportdir(path); - if (errno) { - log_warning("errno was %d before writing reports", errno); - errno = 0; - } + mkreportdir(path); // FIXME: too many mkdir calls! init_reports is enough log_debug("Reports for %s:", factionname(f)); for (rtype = report_types; rtype != NULL; rtype = rtype->next) { if (f->options & rtype->flag) { @@ -1662,7 +1659,7 @@ int reports(void) report_donations(); remove_empty_units(); - mkreportdir(rpath); + mkreportdir(rpath); // FIXME: init_reports already does this? sprintf(path, "%s/reports.txt", rpath); mailit = fopen(path, "w"); if (mailit == NULL) { From 06f8ba9ee44670eb714cf637bf042c7c63cd4081 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 20 Nov 2015 15:48:09 +0100 Subject: [PATCH 02/28] faster lookup for get_param with tries eliminate unnecessary constatn from rand.c --- critbit | 2 +- src/kernel/config.c | 97 +++++++++++++++++++--------------------- src/kernel/config.test.c | 8 ++-- src/util/rand.c | 2 - 4 files changed, 52 insertions(+), 57 deletions(-) diff --git a/critbit b/critbit index dfe57a077..934c2dd94 160000 --- a/critbit +++ b/critbit @@ -1 +1 @@ -Subproject commit dfe57a077222c6b572da61a79dc0687f81c10055 +Subproject commit 934c2dd94d41da19637a76a1a8b3dfeb7aa8524d diff --git a/src/kernel/config.c b/src/kernel/config.c index 14ca56242..b231ad7c6 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -1024,32 +1024,61 @@ void init_locale(struct locale *lang) } typedef struct param { - struct param *next; - char *name; - char *data; + critbit_tree cb; } param; +size_t pack_keyval(const char *key, const char *value, char *data, size_t len) { + size_t klen = strlen(key); + size_t vlen = strlen(value); + assert(klen + vlen + 2 + sizeof(vlen) <= len); + memcpy(data, key, klen + 1); + memcpy(data + klen + 1, value, vlen + 1); + return klen + vlen + 2; +} + +void set_param(struct param **p, const char *key, const char *value) +{ + struct param *par; + assert(p); + + par = *p; + if (!par && value) { + *p = par = calloc(1, sizeof(param)); + } + if (par) { + void *match; + size_t klen = strlen(key) + 1; + if (cb_find_prefix(&par->cb, key, klen, &match, 1, 0) > 0) { + const char * kv = (const char *)match; + size_t vlen = strlen(kv + klen) + 1; + cb_erase(&par->cb, kv, klen + vlen); + ++global.cookie; + } + } + if (value) { + char data[512]; + size_t sz = pack_keyval(key, value, data, sizeof(data)); + if (cb_insert(&par->cb, data, sz) == CB_SUCCESS) { + ++global.cookie; + } + } +} + void free_params(struct param **pp) { - while (*pp) { - param *p = *pp; - free(p->name); - free(p->data); - *pp = p->next; + param *p = *pp; + if (p) { + cb_clear(&p->cb); free(p); } + *pp = 0; } const char *get_param(const struct param *p, const char *key) { - while (p != NULL) { - int cmp = strcmp(p->name, key); - if (cmp == 0) { - return p->data; - } - else if (cmp > 0) { - break; - } - p = p->next; + void *match; + if (p && cb_find_prefix(&p->cb, key, strlen(key)+1, &match, 1, 0) > 0) { + cb_get_kv_ex(match, &match); + return (const char *)match; } return NULL; } @@ -1135,40 +1164,6 @@ double get_param_flt(const struct param *p, const char *key, double def) return str ? atof(str) : def; } -void set_param(struct param **p, const char *key, const char *data) -{ - struct param *par; - - ++global.cookie; - while (*p != NULL) { - int cmp = strcmp((*p)->name, key); - if (cmp == 0) { - par = *p; - free(par->data); - if (data) { - par->data = _strdup(data); - } - else { - *p = par->next; - free(par->name); - free(par); - } - return; - } - else if (cmp > 0) { - break; - } - p = &(*p)->next; - } - if (data) { - par = malloc(sizeof(param)); - par->name = _strdup(key); - par->data = _strdup(data); - par->next = *p; - *p = par; - } -} - void kernel_done(void) { /* calling this function releases memory assigned to static variables, etc. diff --git a/src/kernel/config.test.c b/src/kernel/config.test.c index fab30fc6e..7078d17ac 100644 --- a/src/kernel/config.test.c +++ b/src/kernel/config.test.c @@ -130,6 +130,8 @@ static void test_get_set_param(CuTest * tc) set_param(&par, "bar", "foo"); CuAssertStrEquals(tc, "bar", get_param(par, "foo")); CuAssertStrEquals(tc, "foo", get_param(par, "bar")); + set_param(&par, "bar", "bar"); + CuAssertStrEquals(tc, "bar", get_param(par, "bar")); set_param(&par, "bar", NULL); CuAssertPtrEquals(tc, NULL, (void *)get_param(par, "bar")); free_params(&par); @@ -175,11 +177,11 @@ static void test_forbiddenid(CuTest *tc) { CuSuite *get_config_suite(void) { CuSuite *suite = CuSuiteNew(); - SUITE_ADD_TEST(suite, test_forbiddenid); - SUITE_ADD_TEST(suite, test_getunit); - SUITE_ADD_TEST(suite, test_read_unitid); SUITE_ADD_TEST(suite, test_get_set_param); SUITE_ADD_TEST(suite, test_param_int); SUITE_ADD_TEST(suite, test_param_flt); + SUITE_ADD_TEST(suite, test_forbiddenid); + SUITE_ADD_TEST(suite, test_getunit); + SUITE_ADD_TEST(suite, test_read_unitid); return suite; } diff --git a/src/util/rand.c b/src/util/rand.c index 11f71839b..4e600b1a5 100644 --- a/src/util/rand.c +++ b/src/util/rand.c @@ -26,8 +26,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include -#define M_PIl 3.1415926535897932384626433832795029L /* pi */ - /* NormalRand aus python, random.py geklaut, dort ist Referenz auf * den Algorithmus. mu = Mittelwert, sigma = Standardabweichung. * http://de.wikipedia.org/wiki/Standardabweichung#Diskrete_Gleichverteilung.2C_W.C3.BCrfel From 0d91a7dfde9c99c1a28d8ea8e877f096ca3f14e9 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 21 Nov 2015 12:18:38 +0100 Subject: [PATCH 03/28] eliminate static variables and cache logic --- src/move.c | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/src/move.c b/src/move.c index 00752c3cf..b6a9a3c75 100644 --- a/src/move.c +++ b/src/move.c @@ -837,24 +837,14 @@ static unit *bewegung_blockiert_von(unit * reisender, region * r) unit *guard = NULL; int guard_count = 0; int stealth = eff_stealth(reisender, r); - static int gamecookie = -1; - static double base_prob = -999; - static double skill_prob = -999; - static double amulet_prob = -999; - static double guard_number_prob = -999; - static double castle_prob = -999; - static double region_type_prob = -999; const struct resource_type *ramulet = get_resourcetype(R_AMULET_OF_TRUE_SEEING); - if (gamecookie < 0 || gamecookie != global.cookie) { - base_prob = get_param_flt(global.parameters, "rules.guard.base_stop_prob", .3f); - skill_prob = get_param_flt(global.parameters, "rules.guard.skill_stop_prob", .1f); - amulet_prob = get_param_flt(global.parameters, "rules.guard.amulet_stop_prob", .1f); - guard_number_prob = get_param_flt(global.parameters, "rules.guard.guard_number_stop_prob", .001f); - castle_prob = get_param_flt(global.parameters, "rules.guard.castle_stop_prob", .1f); - region_type_prob = get_param_flt(global.parameters, "rules.guard.region_type_stop_prob", .1f); - gamecookie = global.cookie; - } + double base_prob = get_param_flt(global.parameters, "rules.guard.base_stop_prob", .3f); + double skill_prob = get_param_flt(global.parameters, "rules.guard.skill_stop_prob", .1f); + double amulet_prob = get_param_flt(global.parameters, "rules.guard.amulet_stop_prob", .1f); + double guard_number_prob = get_param_flt(global.parameters, "rules.guard.guard_number_stop_prob", .001f); + double castle_prob = get_param_flt(global.parameters, "rules.guard.castle_stop_prob", .1f); + double region_type_prob = get_param_flt(global.parameters, "rules.guard.region_type_stop_prob", .1f); if (fval(u_race(reisender), RCF_ILLUSIONARY)) return NULL; From 86faae6eeabfeaaf2efaf6e1d211eb5b8b7cd260 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 21 Nov 2015 12:21:07 +0100 Subject: [PATCH 04/28] eliminate static variables and cache logic from upkeep --- src/upkeep.c | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/src/upkeep.c b/src/upkeep.c index 162f41b75..3845c5e68 100644 --- a/src/upkeep.c +++ b/src/upkeep.c @@ -24,10 +24,6 @@ int lifestyle(const unit * u) { int need; plane *pl; - static int gamecookie = -1; - if (gamecookie != global.cookie) { - gamecookie = global.cookie; - } if (is_monsters(u->faction)) return 0; @@ -116,13 +112,7 @@ void get_food(region * r) plane *pl = rplane(r); unit *u; int peasantfood = rpeasants(r) * 10; - static int food_rules = -1; - static int gamecookie = -1; - - if (food_rules < 0 || gamecookie != global.cookie) { - gamecookie = global.cookie; - food_rules = get_param_int(global.parameters, "rules.food.flags", 0); - } + int food_rules = get_param_int(global.parameters, "rules.food.flags", 0); if (food_rules & FOOD_IS_FREE) { return; From 7e27928d1712e1042c4f50f9687485a57288f69e Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 21 Nov 2015 12:28:20 +0100 Subject: [PATCH 05/28] eliminate static variables and cache logic from more modules --- src/economy.c | 14 +++++--------- src/kernel/teleport.c | 17 ++++++----------- src/kernel/unit.c | 8 ++------ src/laws.c | 8 +------- src/report.c | 21 ++++++++------------- 5 files changed, 22 insertions(+), 46 deletions(-) diff --git a/src/economy.c b/src/economy.c index 19223daa1..fc27a1c5d 100644 --- a/src/economy.c +++ b/src/economy.c @@ -2360,16 +2360,12 @@ static void breedtrees(unit * u, int raw) { int n, i, skill, planted = 0; const resource_type *rtype; - static int gamecookie = -1; - static int current_season; + int current_season; region *r = u->region; - - if (gamecookie != global.cookie) { - gamedate date; - get_gamedate(turn, &date); - current_season = date.season; - gamecookie = global.cookie; - } + gamedate date; + + get_gamedate(turn, &date); + current_season = date.season; /* Bäume züchten geht nur im Frühling */ if (current_season != SEASON_SPRING) { diff --git a/src/kernel/teleport.c b/src/kernel/teleport.c index 00d568a22..8d9b696a5 100644 --- a/src/kernel/teleport.c +++ b/src/kernel/teleport.c @@ -181,22 +181,17 @@ bool is_astral(const region * r) plane *get_astralplane(void) { - static plane *astralspace; - static int rule_astralplane = -1; - static int gamecookie = -1; - if (rule_astralplane < 0) { - rule_astralplane = - get_param_int(global.parameters, "modules.astralspace", 1); - } + plane *astralspace = 0; + int rule_astralplane = + get_param_int(global.parameters, "modules.astralspace", 1); + if (!rule_astralplane) { return NULL; } - if (gamecookie != global.cookie) { + if (!astralspace) { astralspace = getplanebyname("Astralraum"); - gamecookie = global.cookie; } - - if (astralspace == NULL) { + if (!astralspace) { astralspace = create_new_plane(1, "Astralraum", TE_CENTER_X - 500, TE_CENTER_X + 500, TE_CENTER_Y - 500, TE_CENTER_Y + 500, 0); diff --git a/src/kernel/unit.c b/src/kernel/unit.c index 493a3a66f..a72745eea 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -850,17 +850,13 @@ void leave_building(unit * u) bool can_leave(unit * u) { - static int gamecookie = -1; - static int rule_leave = -1; + int rule_leave; if (!u->building) { return true; } - if (rule_leave < 0 || gamecookie != global.cookie) { - gamecookie = global.cookie; - rule_leave = get_param_int(global.parameters, "rules.move.owner_leave", 0); - } + rule_leave = get_param_int(global.parameters, "rules.move.owner_leave", 0); if (rule_leave!=0 && u->building && u == building_owner(u->building)) { return false; diff --git a/src/laws.c b/src/laws.c index 15c5404b4..2da0a1a51 100755 --- a/src/laws.c +++ b/src/laws.c @@ -117,13 +117,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. static bool RemoveNMRNewbie(void) { - static int value = -1; - static int gamecookie = -1; - - if (value < 0 || gamecookie != global.cookie) { - value = get_param_int(global.parameters, "nmr.removenewbie", 0); - gamecookie = global.cookie; - } + int value = get_param_int(global.parameters, "nmr.removenewbie", 0); return value!=0; } diff --git a/src/report.c b/src/report.c index f157a75f4..5431f93df 100644 --- a/src/report.c +++ b/src/report.c @@ -2054,19 +2054,14 @@ const char *charset) char *bufp; bool utf8 = _strcmpl(charset, "utf8") == 0 || _strcmpl(charset, "utf-8") == 0; size_t size; - - /* static variables can cope with writing for different turns */ - static int thisseason = -1; - static int nextseason = -1; - static int gamecookie = -1; - if (gamecookie != global.cookie) { - gamedate date; - get_gamedate(turn + 1, &date); - thisseason = date.season; - get_gamedate(turn + 2, &date); - nextseason = date.season; - gamecookie = global.cookie; - } + int thisseason; + int nextseason; + gamedate date; + + get_gamedate(turn + 1, &date); + thisseason = date.season; + get_gamedate(turn + 2, &date); + nextseason = date.season; if (F == NULL) { perror(filename); From bc936bf0192cd92c71e5c7d8a81660fe550e0632 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 21 Nov 2015 13:52:47 +0100 Subject: [PATCH 06/28] eliminate more static variable configuration caching --- src/kernel/config.c | 118 ++++++++++++++++---------------------------- 1 file changed, 43 insertions(+), 75 deletions(-) diff --git a/src/kernel/config.c b/src/kernel/config.c index b231ad7c6..226735228 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -110,13 +110,7 @@ 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); - } - return value; + return get_param_int(global.parameters, "NewbieImmunity", 0); } bool IsImmune(const faction * f) @@ -143,13 +137,7 @@ static int ally_flag(const char *s, int help_mask) bool ExpensiveMigrants(void) { - static int value = -1; - static int gamecookie = -1; - if (value < 0 || gamecookie != global.cookie) { - gamecookie = global.cookie; - value = get_param_int(global.parameters, "study.expensivemigrants", 0); - } - return value != 0; + return get_param_int(global.parameters, "study.expensivemigrants", 0) != 0; } /** Specifies automatic alliance modes. @@ -158,21 +146,17 @@ bool ExpensiveMigrants(void) */ int AllianceAuto(void) { - static int value = -1; - static int gamecookie = -1; - if (value < 0 || gamecookie != global.cookie) { - const char *str = get_param(global.parameters, "alliance.auto"); - gamecookie = global.cookie; - value = 0; - if (str != NULL) { - char *sstr = _strdup(str); - char *tok = strtok(sstr, " "); - while (tok) { - value |= ally_flag(tok, -1); - tok = strtok(NULL, " "); - } - free(sstr); + int value; + const char *str = get_param(global.parameters, "alliance.auto"); + value = 0; + if (str != NULL) { + char *sstr = _strdup(str); + char *tok = strtok(sstr, " "); + while (tok) { + value |= ally_flag(tok, -1); + tok = strtok(NULL, " "); } + free(sstr); } return value & HelpMask(); } @@ -185,65 +169,49 @@ int AllianceAuto(void) */ int HelpMask(void) { - static int rule = -1; - static int gamecookie = -1; - if (rule < 0 || gamecookie != global.cookie) { - const char *str = get_param(global.parameters, "rules.help.mask"); - gamecookie = global.cookie; - rule = 0; - 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; + const char *str = get_param(global.parameters, "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, " "); } + free(sstr); + } + else { + rule = HELP_ALL; } return rule; } int AllianceRestricted(void) { - static int rule = -1; - static int gamecookie = -1; - if (rule < 0 || gamecookie != global.cookie) { - const char *str = get_param(global.parameters, "alliance.restricted"); - gamecookie = global.cookie; - rule = 0; - if (str != NULL) { - char *sstr = _strdup(str); - char *tok = strtok(sstr, " "); - while (tok) { - rule |= ally_flag(tok, -1); - tok = strtok(NULL, " "); - } - free(sstr); + const char *str = get_param(global.parameters, "alliance.restricted"); + int rule = 0; + if (str != NULL) { + char *sstr = _strdup(str); + char *tok = strtok(sstr, " "); + while (tok) { + rule |= ally_flag(tok, -1); + tok = strtok(NULL, " "); } - rule &= HelpMask(); + free(sstr); } + rule &= HelpMask(); return rule; } int LongHunger(const struct unit *u) { - static int gamecookie = -1; - static int rule = -1; if (u != NULL) { if (!fval(u, UFL_HUNGER)) return false; if (u_race(u) == get_race(RC_DAEMON)) return false; } - if (rule < 0 || gamecookie != global.cookie) { - gamecookie = global.cookie; - rule = get_param_int(global.parameters, "hunger.long", 0); - } - return rule; + return get_param_int(global.parameters, "hunger.long", 0); } int SkillCap(skill_t sk) @@ -498,7 +466,7 @@ static int ally_mode(const ally * sf, int mode) int alliedgroup(const struct plane *pl, const struct faction *f, -const struct faction *f2, const struct ally *sf, int mode) + const struct faction *f2, const struct ally *sf, int mode) { while (sf && sf->faction != f2) sf = sf->next; @@ -522,7 +490,7 @@ const struct faction *f2, const struct ally *sf, int mode) int alliedfaction(const struct plane *pl, const struct faction *f, -const struct faction *f2, int mode) + const struct faction *f2, int mode) { return alliedgroup(pl, f, f2, f->allies, mode); } @@ -1076,7 +1044,7 @@ void free_params(struct param **pp) { const char *get_param(const struct param *p, const char *key) { void *match; - if (p && cb_find_prefix(&p->cb, key, strlen(key)+1, &match, 1, 0) > 0) { + if (p && cb_find_prefix(&p->cb, key, strlen(key) + 1, &match, 1, 0) > 0) { cb_get_kv_ex(match, &match); return (const char *)match; } @@ -1345,7 +1313,7 @@ bool rule_stealth_anon(void) gamecookie = global.cookie; assert(rule >= 0); } - return rule!=0; + return rule != 0; } bool rule_region_owners(void) @@ -1357,7 +1325,7 @@ bool rule_region_owners(void) gamecookie = global.cookie; assert(rule >= 0); } - return rule!=0; + return rule != 0; } bool rule_auto_taxation(void) @@ -1380,7 +1348,7 @@ int rule_blessed_harvest(void) if (rule < 0 || gamecookie != global.cookie) { rule = get_param_int(global.parameters, "rules.blessed_harvest.flags", - HARVEST_WORK); + HARVEST_WORK); gamecookie = global.cookie; assert(rule >= 0); } @@ -1420,7 +1388,7 @@ bool rule_transfermen(void) gamecookie = global.cookie; assert(rule >= 0); } - return rule!=0; + return rule != 0; } static int @@ -1632,7 +1600,7 @@ order *default_order(const struct locale *lang) order *result = 0; assert(i < MAXLOCALES); - if (default_keyword!=NOKEYWORD) { + if (default_keyword != NOKEYWORD) { return create_order(default_keyword, lang, 0); } From 990fda6234e9f1b2cf4247573867b71c829d8415 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 21 Nov 2015 18:13:16 +0100 Subject: [PATCH 07/28] clean up config.c, remove static configuration caching --- src/economy.c | 6 +++ src/give.c | 6 +++ src/kernel/config.c | 98 ++++++--------------------------------------- src/kernel/config.h | 2 - src/kernel/unit.c | 2 +- src/study.c | 12 +++--- 6 files changed, 33 insertions(+), 93 deletions(-) diff --git a/src/economy.c b/src/economy.c index fc27a1c5d..ce5bc3f36 100644 --- a/src/economy.c +++ b/src/economy.c @@ -3134,6 +3134,12 @@ static void peasant_taxes(region * r) } } +static bool rule_auto_taxation(void) +{ + int rule = get_param_int(global.parameters, "rules.economy.taxation", 0); + return rule != 0; +} + void produce(struct region *r) { request workers[MAX_WORKERS]; diff --git a/src/give.c b/src/give.c index e41ee19f5..5dead1277 100644 --- a/src/give.c +++ b/src/give.c @@ -233,6 +233,12 @@ static bool can_give_men(const unit *u, order *ord, message **msg) { return false; } +static bool rule_transfermen(void) +{ + int rule = get_param_int(global.parameters, "rules.transfermen", 1); + return rule != 0; +} + message * give_men(int n, unit * u, unit * u2, struct order *ord) { ship *sh; diff --git a/src/kernel/config.c b/src/kernel/config.c index 226735228..3e3651c88 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -216,26 +216,13 @@ int LongHunger(const struct unit *u) int SkillCap(skill_t sk) { - static int gamecookie = -1; - static int rule = -1; - if (sk == SK_MAGIC) - return 0; /* no caps on magic */ - if (rule < 0 || gamecookie != global.cookie) { - gamecookie = global.cookie; - rule = get_param_int(global.parameters, "skill.maxlevel", 0); - } - return rule; + if (sk == SK_MAGIC) return 0; /* no caps on magic */ + return get_param_int(global.parameters, "skill.maxlevel", 0); } int NMRTimeout(void) { - static int gamecookie = -1; - static int rule = -1; - if (rule < 0 || gamecookie != global.cookie) { - gamecookie = global.cookie; - rule = get_param_int(global.parameters, "nmr.timeout", 0); - } - return rule; + return get_param_int(global.parameters, "nmr.timeout", 0); } race_t old_race(const struct race * rc) @@ -1294,103 +1281,44 @@ int cmp_current_owner(const building * b, const building * a) bool rule_stealth_other(void) { - static int gamecookie = -1; - static int rule = -1; - if (rule < 0 || gamecookie != global.cookie) { - rule = get_param_int(global.parameters, "stealth.faction.other", 1); - gamecookie = global.cookie; - assert(rule >= 0); - } + int rule = get_param_int(global.parameters, "stealth.faction.other", 1); return rule != 0; } bool rule_stealth_anon(void) { - static int gamecookie = -1; - static int rule = -1; - if (rule < 0 || gamecookie != global.cookie) { - rule = get_param_int(global.parameters, "stealth.faction.anon", 1); - gamecookie = global.cookie; - assert(rule >= 0); - } + int rule = get_param_int(global.parameters, "stealth.faction.anon", 1); return rule != 0; } bool rule_region_owners(void) { - static int gamecookie = -1; - static int rule = -1; - if (rule < 0 || gamecookie != global.cookie) { - rule = get_param_int(global.parameters, "rules.region_owners", 0); - gamecookie = global.cookie; - assert(rule >= 0); - } + int rule = get_param_int(global.parameters, "rules.region_owners", 0); return rule != 0; } -bool rule_auto_taxation(void) -{ - static int gamecookie = -1; - static int rule = -1; - if (rule < 0 || gamecookie != global.cookie) { - rule = - get_param_int(global.parameters, "rules.economy.taxation", 0); - gamecookie = global.cookie; - assert(rule >= 0); - } - return rule; -} - int rule_blessed_harvest(void) { - static int gamecookie = -1; - static int rule = -1; - if (rule < 0 || gamecookie != global.cookie) { - rule = - get_param_int(global.parameters, "rules.blessed_harvest.flags", - HARVEST_WORK); - gamecookie = global.cookie; - assert(rule >= 0); - } + int rule = get_param_int(global.parameters, "rules.blessed_harvest.flags", + HARVEST_WORK); + assert(rule >= 0); return rule; } int rule_alliance_limit(void) { - static int gamecookie = -1; - static int rule = -1; - if (rule < 0 || gamecookie != global.cookie) { - rule = get_param_int(global.parameters, "rules.limit.alliance", 0); - gamecookie = global.cookie; - assert(rule >= 0); - } + int rule = get_param_int(global.parameters, "rules.limit.alliance", 0); + assert(rule >= 0); return rule; } int rule_faction_limit(void) { - static int gamecookie = -1; - static int rule = -1; - if (rule < 0 || gamecookie != global.cookie) { - rule = get_param_int(global.parameters, "rules.limit.faction", 0); - gamecookie = global.cookie; - assert(rule >= 0); - } + int rule = get_param_int(global.parameters, "rules.limit.faction", 0); + assert(rule >= 0); return rule; } -bool rule_transfermen(void) -{ - static int gamecookie = -1; - static int rule = -1; - if (rule < 0 || gamecookie != global.cookie) { - rule = get_param_int(global.parameters, "rules.transfermen", 1); - gamecookie = global.cookie; - assert(rule >= 0); - } - return rule != 0; -} - static int default_wage(const region * r, const faction * f, const race * rc, int in_turn) { diff --git a/src/kernel/config.h b/src/kernel/config.h index 5eddd1ff3..80559ad89 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -151,7 +151,6 @@ extern "C" { int cmp_current_owner(const struct building *b, const struct building *bother); - bool rule_transfermen(void); bool rule_region_owners(void); bool rule_stealth_other(void); // units can pretend to be another faction, TARNE PARTEI bool rule_stealth_anon(void); // units can anonymize their faction, TARNE PARTEI [NICHT] @@ -160,7 +159,6 @@ extern "C" { #define HARVEST_WORK 0x00 #define HARVEST_TAXES 0x01 int rule_blessed_harvest(void); - bool rule_auto_taxation(void); #define GIVE_SELF 1 #define GIVE_PEASANTS 2 #define GIVE_LUXURIES 4 diff --git a/src/kernel/unit.c b/src/kernel/unit.c index a72745eea..6565c8f48 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -1366,7 +1366,7 @@ int eff_skill(const unit * u, const skill *sv, const region *r) if (mlevel > 0) { int skillcap = SkillCap(sv->id); - if (skillcap && mlevel > skillcap) { + if (skillcap>0 && mlevel > skillcap) { return skillcap; } return mlevel; diff --git a/src/study.c b/src/study.c index b798277ca..32b9b5029 100644 --- a/src/study.c +++ b/src/study.c @@ -184,7 +184,7 @@ static building *active_building(const unit *u, const struct building_type *btyp static int teach_unit(unit * teacher, unit * student, int nteaching, skill_t sk, -bool report, int *academy) + bool report, int *academy) { teaching_info *teach = NULL; attrib *a; @@ -343,7 +343,8 @@ int teach_cmd(unit * u, struct order *ord) for (student = r->units; teaching && student; student = student->next) { if (LongHunger(student)) { continue; - } else if (student->faction == u->faction) { + } + else if (student->faction == u->faction) { if (getkeyword(student->thisorder) == K_STUDY) { /* Input ist nun von student->thisorder !! */ init_order(student->thisorder); @@ -509,7 +510,7 @@ static double study_speedup(unit * u, skill_t s, study_rule_t rule) if (rule == STUDY_FASTER) { for (i = 0; i != u->skill_size; ++i) { skill *sv = u->skills + i; - if (sv->id == s){ + if (sv->id == s) { learnweeks = sv->level * (sv->level + 1) / 2.0; if (learnweeks < turn / 3.0) { return 2.0; @@ -534,7 +535,7 @@ static double study_speedup(unit * u, skill_t s, study_rule_t rule) int study_cmd(unit * u, order * ord) { region *r = u->region; - int p; + int p, cap; magic_t mtyp; int l; int studycost, days; @@ -569,7 +570,8 @@ int study_cmd(unit * u, order * ord) cmistake(u, ord, 77, MSG_EVENT); return 0; } - if (SkillCap(sk) && SkillCap(sk) <= effskill(u, sk, 0)) { + cap = SkillCap(sk); + if (cap > 0 && cap <= effskill(u, sk, 0)) { cmistake(u, ord, 771, MSG_EVENT); return 0; } From 7dae5aa035194cb0f6ea482ed1f8d881a094c6f6 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 21 Nov 2015 19:02:14 +0100 Subject: [PATCH 08/28] eliminate silly caching logic from natural armor calculation --- src/battle.c | 28 +++++++++------------------- src/battle.h | 1 + src/battle.test.c | 21 ++++++++++++++++++++- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/battle.c b/src/battle.c index f57e7b377..a897a300e 100644 --- a/src/battle.c +++ b/src/battle.c @@ -1024,30 +1024,20 @@ static void vampirism(troop at, int damage) #define MAXRACES 128 -static int natural_armor(unit * du) +static int armor_bonus(const race *rc) { + return get_param_int(rc->parameters, "armor.stamina", -1); +} + +int natural_armor(unit * du) { - static int cookie = -1; - static int bonus[MAXRACES]; const race *rc = u_race(du); - int index, an = rc->armor; + int bonus, an = rc->armor; assert(rc); - if (cookie!=global.cookie) { - cookie = global.cookie; - memset(bonus, 0, sizeof(bonus)); - } - assert(num_races < MAXRACES); - index = rc->index; - assert(index >= 0 && index < num_races); - if (bonus[index] == 0) { - bonus[index] = - get_param_int(rc->parameters, "armor.stamina", -1); - if (bonus[index] == 0) - bonus[index] = -1; - } - if (bonus[index] > 0) { + bonus = armor_bonus(rc); + if (bonus > 0) { int sk = effskill(du, SK_STAMINA, 0); - sk /= bonus[index]; + sk /= bonus; an += sk; } return an; diff --git a/src/battle.h b/src/battle.h index 9b7b8657d..0e4fceffe 100644 --- a/src/battle.h +++ b/src/battle.h @@ -243,6 +243,7 @@ extern "C" { int count_enemies(struct battle *b, const struct fighter *af, int minrow, int maxrow, int select); + int natural_armor(struct unit * u); int calculate_armor(troop dt, const struct weapon_type *dwtype, const struct weapon_type *awtype, double *magres); bool terminate(troop dt, troop at, int type, const char *damage, bool missile); diff --git a/src/battle.test.c b/src/battle.test.c index 02213d35b..4af0575c7 100644 --- a/src/battle.test.c +++ b/src/battle.test.c @@ -3,6 +3,7 @@ #include "battle.h" #include "skill.h" +#include #include #include #include @@ -201,13 +202,30 @@ static void test_building_defence_bonus(CuTest * tc) test_cleanup(); } -fighter *setup_fighter(battle **bp, unit *u) { +static fighter *setup_fighter(battle **bp, unit *u) { battle *b; *bp = b = make_battle(u->region); return make_fighter(b, u, make_side(b, u->faction, 0, 0, 0), false); } +static void test_natural_armor(CuTest * tc) +{ + race *rc; + unit *u; + + test_cleanup(); + rc = test_create_race("human"); + u = test_create_unit(test_create_faction(rc), test_create_region(0, 0, 0)); + set_level(u, SK_STAMINA, 2); + CuAssertIntEquals(tc, 0, natural_armor(u)); + set_param(&rc->parameters, "armor.stamina", "1"); + CuAssertIntEquals(tc, 2, natural_armor(u)); + set_param(&rc->parameters, "armor.stamina", "2"); + CuAssertIntEquals(tc, 1, natural_armor(u)); + test_cleanup(); +} + static void test_calculate_armor(CuTest * tc) { troop dt; @@ -321,6 +339,7 @@ CuSuite *get_battle_suite(void) SUITE_ADD_TEST(suite, test_building_bonus_respects_size); SUITE_ADD_TEST(suite, test_building_defence_bonus); SUITE_ADD_TEST(suite, test_calculate_armor); + SUITE_ADD_TEST(suite, test_natural_armor); SUITE_ADD_TEST(suite, test_projectile_armor); return suite; } From 6b83120c7a32504825e65fad3f89a0aaf5534036 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 21 Nov 2015 21:33:17 +0100 Subject: [PATCH 09/28] remove caching from is_guarded, which should slow it down significantly :-( --- src/move.c | 55 +++--------------------------------------------------- 1 file changed, 3 insertions(+), 52 deletions(-) diff --git a/src/move.c b/src/move.c index b6a9a3c75..2eded780d 100644 --- a/src/move.c +++ b/src/move.c @@ -938,77 +938,28 @@ bool is_guard(const struct unit * u, unsigned int mask) return is_guardian_r(u) && (getguard(u) & mask) != 0; } -#define MAXGUARDCACHE 16 -/** returns the guard which prevents 'u' from doing 'mask' actions in 'r'. -*/ unit *is_guarded(region * r, unit * u, unsigned int mask) { - unit *u2 = NULL; - int i, noguards = 1; - static unit *guardcache[MAXGUARDCACHE], *lastguard; /* STATIC_XCALL: used across calls */ - static int gamecookie = -1; + unit *u2; + int noguards = 1; if (!fval(r, RF_GUARDED)) { return NULL; } - if (gamecookie != global.cookie) { - if (gamecookie >= 0) { - /* clear the previous turn's cache */ - memset(guardcache, 0, sizeof(guardcache)); - lastguard = NULL; - } - gamecookie = global.cookie; - } - - if (lastguard && lastguard->region == r) { - if (is_guardian_u(lastguard, u, mask)) { - return lastguard; - } - } - - for (i = 0; i != MAXGUARDCACHE; ++i) { - unit *guard = guardcache[i]; - if (guard && guard != lastguard && guard->region == r) { - noguards = 0; - if (is_guardian_u(guard, u, mask)) { - lastguard = guard; - return guard; - } - if (u2 == guard) { - /* same guard twice signals we've tested everyone */ - return NULL; - } - u2 = guard; - } - else { - /* exhausted all the guards in the cache, but maybe we'll find one later? */ - break; - } - } - /* at this point, u2 is the last unit we tested to * be a guard (and failed), or NULL * i is the position of the first free slot in the cache */ - for (u2 = (u2 ? u2->next : r->units); u2; u2 = u2->next) { + for (u2 = r->units; u2; u2 = u2->next) { if (is_guardian_r(u2)) { noguards = 0; - /* u2 is a guard, so worth remembering */ - if (i < MAXGUARDCACHE) - guardcache[i++] = u2; if (is_guardian_u(u2, u, mask)) { /* u2 is our guard. stop processing (we might have to go further next time) */ - lastguard = u2; return u2; } } } - /* there are no more guards. we signal this by duplicating the last one. - * i is still the position of the first free slot in the cache */ - if (i > 0 && i < MAXGUARDCACHE) { - guardcache[i] = guardcache[i - 1]; - } if (noguards) { /* you are mistaken, sir. there are no guards in these lands */ From 66dd1b81725e9f39162d3e493aa81ba5752dfd23 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 22 Nov 2015 10:21:37 +0100 Subject: [PATCH 10/28] remove produce_exp caching in static variables (more slowdown) --- src/kernel/config.h | 1 - src/kernel/unit.c | 7 +------ 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/src/kernel/config.h b/src/kernel/config.h index 80559ad89..1ada1cd5c 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -259,7 +259,6 @@ extern "C" { /* the following are some cached values, because get_param can be slow. * you should almost never need to touch them */ int cookie; - double producexpchance_; } settings; typedef struct helpmode { diff --git a/src/kernel/unit.c b/src/kernel/unit.c index 6565c8f48..c37bbb5a0 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -1931,12 +1931,7 @@ bool unit_can_study(const unit *u) { } static double produceexp_chance(void) { - static int update = 0; - if (update != global.cookie) { - global.producexpchance_ = get_param_flt(global.parameters, "study.from_use", 1.0 / 3); - update = global.cookie; - } - return global.producexpchance_; + return get_param_flt(global.parameters, "study.from_use", 1.0 / 3); } void produceexp_ex(struct unit *u, skill_t sk, int n, bool (*learn)(unit *, skill_t, double)) From b05fe9316ab86473dc29937f3d3ed3c249590326 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 22 Nov 2015 10:33:31 +0100 Subject: [PATCH 11/28] introducing config_set and config_get so we don't have to refer to globals.parameters everywhere --- src/CMakeLists.txt | 3 +-- src/alchemy.c | 7 +++--- src/attributes/otherfaction.test.c | 14 ++++++------ src/bind_process.c | 2 +- src/bind_settings.c | 14 ------------ src/bind_settings.h | 13 ------------ src/give.test.c | 10 ++++----- src/gmtool.c | 4 ++-- src/kernel/config.c | 16 ++++++++++++++ src/kernel/config.h | 7 +++++- src/kernel/jsonconf.c | 6 +++--- src/kernel/ship.test.c | 4 ++-- src/kernel/unit.test.c | 8 +++---- src/laws.test.c | 34 +++++++++++++++--------------- src/main.c | 2 +- src/market.test.c | 2 +- src/move.test.c | 2 +- src/piracy.test.c | 2 +- src/settings.pkg | 6 +++--- src/settings.pkg.c | 6 +++--- src/study.test.c | 2 +- src/upkeep.test.c | 10 ++++----- 22 files changed, 83 insertions(+), 91 deletions(-) delete mode 100755 src/bind_settings.c delete mode 100755 src/bind_settings.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ada49aad9..e30cd56d9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -73,7 +73,7 @@ TOLUA_BINDING(config.pkg bind_config.h) TOLUA_BINDING(process.pkg bind_process.h) TOLUA_BINDING(game.pkg bind_eressea.h config.h) TOLUA_BINDING(eressea.pkg bind_eressea.h) -TOLUA_BINDING(settings.pkg bind_settings.h) +TOLUA_BINDING(settings.pkg kenel/config.h) ENDIF() set (ERESSEA_SRC @@ -144,7 +144,6 @@ set(SERVER_SRC bind_monsters.c bind_process.c bind_region.c - bind_settings.c bind_ship.c bind_storage.c bind_unit.c diff --git a/src/alchemy.c b/src/alchemy.c index 9042fee9b..166cde00b 100644 --- a/src/alchemy.c +++ b/src/alchemy.c @@ -98,8 +98,7 @@ static int begin_potion(unit * u, const potion_type * ptype, struct order *ord) if (rule_multipotion < 0) { /* should we allow multiple different potions to be used the same turn? */ - rule_multipotion = - get_param_int(global.parameters, "rules.magic.multipotion", 0); + rule_multipotion = config_get_int("rules.magic.multipotion", 0); } if (!rule_multipotion) { const potion_type *use = ugetpotionuse(u); @@ -130,9 +129,9 @@ static int do_potion(unit * u, region *r, const potion_type * ptype, int amount) static int tree_type = -1; static int tree_count = -1; if (tree_type < 0) { - tree_type = get_param_int(global.parameters, "rules.magic.wol_type", 1); + tree_type = config_get_int("rules.magic.wol_type", 1); tree_count = - get_param_int(global.parameters, "rules.magic.wol_effect", 10); + config_get_int("rules.magic.wol_effect", 10); } /* mallorn is required to make mallorn forests, wood for regular ones */ if (fval(r, RF_MALLORN)) { diff --git a/src/attributes/otherfaction.test.c b/src/attributes/otherfaction.test.c index 0b38901eb..8e3d09b0a 100644 --- a/src/attributes/otherfaction.test.c +++ b/src/attributes/otherfaction.test.c @@ -17,18 +17,18 @@ static void test_rules(CuTest *tc) { test_cleanup(); - set_param(&global.parameters, "stealth.faction.other", NULL); + config_set("stealth.faction.other", NULL); CuAssertIntEquals(tc, true, rule_stealth_other()); - set_param(&global.parameters, "stealth.faction.other", "0"); + config_set("stealth.faction.other", "0"); CuAssertIntEquals(tc, false, rule_stealth_other()); - set_param(&global.parameters, "stealth.faction.other", "1"); + config_set("stealth.faction.other", "1"); CuAssertIntEquals(tc, true, rule_stealth_other()); - set_param(&global.parameters, "stealth.faction.anon", NULL); + config_set("stealth.faction.anon", NULL); CuAssertIntEquals(tc, true, rule_stealth_anon()); - set_param(&global.parameters, "stealth.faction.anon", "0"); + config_set("stealth.faction.anon", "0"); CuAssertIntEquals(tc, false, rule_stealth_anon()); - set_param(&global.parameters, "stealth.faction.anon", "1"); + config_set("stealth.faction.anon", "1"); CuAssertIntEquals(tc, true, rule_stealth_anon()); test_cleanup(); } @@ -40,7 +40,7 @@ static void test_otherfaction(CuTest *tc) { test_cleanup(); u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); f = test_create_faction(0); - set_param(&global.parameters, "stealth.faction.other", "1"); + config_set("stealth.faction.other", "1"); CuAssertIntEquals(tc, true, rule_stealth_other()); CuAssertPtrEquals(tc, u->faction, visible_faction(f, u)); a_add(&u->attribs, make_otherfaction(f)); diff --git a/src/bind_process.c b/src/bind_process.c index 2e883d38a..fcf2bd55c 100755 --- a/src/bind_process.c +++ b/src/bind_process.c @@ -223,7 +223,7 @@ void process_explain(void) { } void process_reserve(void) { - int rule = get_param_int(global.parameters, "rules.reserve.twophase", 0); + int rule = config_get_int("rules.reserve.twophase", 0); if (rule) { process_cmd(K_RESERVE, reserve_self, 0); } diff --git a/src/bind_settings.c b/src/bind_settings.c deleted file mode 100755 index b0a82baaa..000000000 --- a/src/bind_settings.c +++ /dev/null @@ -1,14 +0,0 @@ -#include "bind_settings.h" - -#include -#include - -const char * settings_get(const char *key) -{ - return get_param(global.parameters, key); -} - -void settings_set(const char *key, const char *value) -{ - set_param(&global.parameters, key, value); -} diff --git a/src/bind_settings.h b/src/bind_settings.h deleted file mode 100755 index 7f01c3856..000000000 --- a/src/bind_settings.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef BIND_ERESSEA_SETTINGS_H -#define BIND_ERESSEA_SETTINGS_H -#ifdef __cplusplus -extern "C" { -#endif - - const char * settings_get(const char *key); - void settings_set(const char *key, const char *value); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/src/give.test.c b/src/give.test.c index 76726c044..c6c3aafc8 100644 --- a/src/give.test.c +++ b/src/give.test.c @@ -75,11 +75,11 @@ static void test_give_unit(CuTest * tc) { env.f2 = test_create_faction(0); setup_give(&env); env.r->terrain = test_create_terrain("ocean", SEA_REGION); - set_param(&global.parameters, "rules.give.max_men", "0"); + config_set("rules.give.max_men", "0"); give_unit(env.src, env.dst, NULL); CuAssertPtrEquals(tc, env.f1, env.src->faction); CuAssertIntEquals(tc, 0, env.f2->newbies); - set_param(&global.parameters, "rules.give.max_men", "-1"); + config_set("rules.give.max_men", "-1"); give_unit(env.src, env.dst, NULL); CuAssertPtrEquals(tc, env.f2, env.src->faction); CuAssertIntEquals(tc, 1, env.f2->newbies); @@ -117,7 +117,7 @@ static void test_give_men_limit(CuTest * tc) { env.f2 = test_create_faction(0); env.f1 = test_create_faction(0); setup_give(&env); - set_param(&global.parameters, "rules.give.max_men", "1"); + config_set("rules.give.max_men", "1"); /* below the limit, give men, increase newbies counter */ usetcontact(env.dst, env.src); @@ -308,7 +308,7 @@ static void test_give_okay(CuTest * tc) { env.f2 = env.f1 = test_create_faction(0); setup_give(&env); - set_param(&global.parameters, "rules.give.flags", "0"); + config_set("rules.give.flags", "0"); CuAssertPtrEquals(tc, 0, check_give(env.src, env.dst, 0)); test_cleanup(); } @@ -322,7 +322,7 @@ static void test_give_denied_by_rules(CuTest * tc) { env.f2 = test_create_faction(0); setup_give(&env); - set_param(&global.parameters, "rules.give.flags", "0"); + config_set("rules.give.flags", "0"); CuAssertPtrNotNull(tc, msg = check_give(env.src, env.dst, 0)); msg_release(msg); test_cleanup(); diff --git a/src/gmtool.c b/src/gmtool.c index 2c15f6b29..92080b63b 100644 --- a/src/gmtool.c +++ b/src/gmtool.c @@ -853,8 +853,8 @@ static void handlekey(state * st, int c) new_players = read_newfactions(sbuffer); } cnormalize(&st->cursor, &nx, &ny); - minpop = get_param_int(global.parameters, "seed.population.min", 8); - maxpop = get_param_int(global.parameters, "seed.population.max", minpop); + minpop = config_get_int("seed.population.min", 8); + maxpop = config_get_int("seed.population.max", minpop); if (maxpop > minpop) { n = rng_int() % (maxpop - minpop) + minpop; } diff --git a/src/kernel/config.c b/src/kernel/config.c index 3e3651c88..1f44530f9 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -1572,6 +1572,22 @@ bool markets_module(void) return get_param_int(global.parameters, "modules.markets", 0); } +void config_set(const char *key, const char *value) { + set_param(&global.parameters, key, value); +} + +const char *config_get(const char *key) { + return get_param(global.parameters, key); +} + +int config_get_int(const char *key, int def) { + return get_param_int(global.parameters, key, def); +} + +double config_get_flt(const char *key, double def) { + return get_param_flt(global.parameters, key, def); +} + /** releases all memory associated with the game state. * call this function before calling read_game() to load a new game * if you have a previously loaded state in memory. diff --git a/src/kernel/config.h b/src/kernel/config.h index 1ada1cd5c..817850166 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -268,13 +268,18 @@ extern "C" { const char *dbrace(const struct race *rc); - void set_param(struct param **p, const char *key, const char *data); + void set_param(struct param **p, const char *key, const char *value); const char *get_param(const struct param *p, const char *key); int get_param_int(const struct param *p, const char *key, int def); int check_param(const struct param *p, const char *key, const char *searchvalue); double get_param_flt(const struct param *p, const char *key, double def); void free_params(struct param **pp); + void config_set(const char *key, const char *value); + const char *config_get(const char *key); + int config_get_int(const char *key, int def); + double config_get_flt(const char *key, double def); + bool ExpensiveMigrants(void); int NMRTimeout(void); int LongHunger(const struct unit *u); diff --git a/src/kernel/jsonconf.c b/src/kernel/jsonconf.c index 9efbc0250..eed4d4480 100644 --- a/src/kernel/jsonconf.c +++ b/src/kernel/jsonconf.c @@ -533,7 +533,7 @@ static void disable_feature(const char *str) { } _snprintf(name, sizeof(name), "%s.enabled", str); log_info("disable feature %s\n", name); - set_param(&global.parameters, name, "0"); + config_set(name, "0"); } static void json_disable_features(cJSON *json) { @@ -788,7 +788,7 @@ static void json_settings(cJSON *json) { } for (child = json->child; child; child = child->next) { if (child->valuestring) { - set_param(&global.parameters, child->string, child->valuestring); + config_set(child->string, child->valuestring); } else { char value[32]; @@ -798,7 +798,7 @@ static void json_settings(cJSON *json) { else { _snprintf(value, sizeof(value), "%d", child->valueint); } - set_param(&global.parameters, child->string, value); + config_set(child->string, value); } } } diff --git a/src/kernel/ship.test.c b/src/kernel/ship.test.c index dc125cd9b..b003f74b9 100644 --- a/src/kernel/ship.test.c +++ b/src/kernel/ship.test.c @@ -399,7 +399,7 @@ static ship *setup_ship(void) { region *r; ship_type *stype; - set_param(&global.parameters, "movement.shipspeed.skillbonus", "0"); + config_set("movement.shipspeed.skillbonus", "0"); r = test_create_region(0, 0, test_create_terrain("ocean", 0)); stype = test_create_shiptype("longboat"); stype->cptskill = 1; @@ -558,7 +558,7 @@ static void test_shipspeed_max_range(CuTest *tc) { test_cleanup(); sh = setup_ship(); setup_crew(sh, 0, &cap, &crew); - set_param(&global.parameters, "movement.shipspeed.skillbonus", "5"); + config_set("movement.shipspeed.skillbonus", "5"); r = sh->region; f = test_create_faction(0); assert(r && f); diff --git a/src/kernel/unit.test.c b/src/kernel/unit.test.c index db2751026..4486d344a 100644 --- a/src/kernel/unit.test.c +++ b/src/kernel/unit.test.c @@ -284,15 +284,15 @@ static void test_skill_hunger(CuTest *tc) { set_level(u, SK_SAILING, 6); fset(u, UFL_HUNGER); - set_param(&global.parameters, "rules.hunger.reduces_skill", "0"); + config_set("rules.hunger.reduces_skill", "0"); CuAssertIntEquals(tc, 6, effskill(u, SK_ARMORER, 0)); CuAssertIntEquals(tc, 6, effskill(u, SK_SAILING, 0)); - set_param(&global.parameters, "rules.hunger.reduces_skill", "1"); + config_set("rules.hunger.reduces_skill", "1"); CuAssertIntEquals(tc, 3, effskill(u, SK_ARMORER, 0)); CuAssertIntEquals(tc, 3, effskill(u, SK_SAILING, 0)); - set_param(&global.parameters, "rules.hunger.reduces_skill", "2"); + config_set("rules.hunger.reduces_skill", "2"); CuAssertIntEquals(tc, 3, effskill(u, SK_ARMORER, 0)); CuAssertIntEquals(tc, 5, effskill(u, SK_SAILING, 0)); set_level(u, SK_SAILING, 2); @@ -371,7 +371,7 @@ static void test_produceexp(CuTest *tc) { g_tc = tc; test_cleanup(); u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); - set_param(&global.parameters, "study.from_use", "0.5"); + config_set("study.from_use", "0.5"); produceexp_ex(u, SK_ALCHEMY, 1, cb_learn_one); produceexp_ex(u, SK_ALCHEMY, 2, cb_learn_two); test_cleanup(); diff --git a/src/laws.test.c b/src/laws.test.c index e829eff27..43936abfc 100644 --- a/src/laws.test.c +++ b/src/laws.test.c @@ -233,16 +233,16 @@ static void test_display_cmd(CuTest *tc) { } static void test_rule_force_leave(CuTest *tc) { - set_param(&global.parameters, "rules.owners.force_leave", "0"); + config_set("rules.owners.force_leave", "0"); CuAssertIntEquals(tc, false, rule_force_leave(FORCE_LEAVE_ALL)); CuAssertIntEquals(tc, false, rule_force_leave(FORCE_LEAVE_POSTCOMBAT)); - set_param(&global.parameters, "rules.owners.force_leave", "1"); + config_set("rules.owners.force_leave", "1"); CuAssertIntEquals(tc, false, rule_force_leave(FORCE_LEAVE_ALL)); CuAssertIntEquals(tc, true, rule_force_leave(FORCE_LEAVE_POSTCOMBAT)); - set_param(&global.parameters, "rules.owners.force_leave", "2"); + config_set("rules.owners.force_leave", "2"); CuAssertIntEquals(tc, true, rule_force_leave(FORCE_LEAVE_ALL)); CuAssertIntEquals(tc, false, rule_force_leave(FORCE_LEAVE_POSTCOMBAT)); - set_param(&global.parameters, "rules.owners.force_leave", "3"); + config_set("rules.owners.force_leave", "3"); CuAssertIntEquals(tc, true, rule_force_leave(FORCE_LEAVE_ALL)); CuAssertIntEquals(tc, true, rule_force_leave(FORCE_LEAVE_POSTCOMBAT)); } @@ -413,13 +413,13 @@ static void test_fishing_gets_reset(CuTest * tc) static void test_unit_limit(CuTest * tc) { - set_param(&global.parameters, "rules.limit.faction", "250"); + config_set("rules.limit.faction", "250"); CuAssertIntEquals(tc, 250, rule_faction_limit()); - set_param(&global.parameters, "rules.limit.faction", "200"); + config_set("rules.limit.faction", "200"); CuAssertIntEquals(tc, 200, rule_faction_limit()); - set_param(&global.parameters, "rules.limit.alliance", "250"); + config_set("rules.limit.alliance", "250"); CuAssertIntEquals(tc, 250, rule_alliance_limit()); } @@ -432,12 +432,12 @@ static void test_cannot_create_unit_above_limit(CuTest * tc) test_cleanup(); test_create_world(); f = test_create_faction(NULL); - set_param(&global.parameters, "rules.limit.faction", "4"); + config_set("rules.limit.faction", "4"); CuAssertIntEquals(tc, 0, checkunitnumber(f, 4)); CuAssertIntEquals(tc, 2, checkunitnumber(f, 5)); - set_param(&global.parameters, "rules.limit.alliance", "3"); + config_set("rules.limit.alliance", "3"); CuAssertIntEquals(tc, 0, checkunitnumber(f, 3)); CuAssertIntEquals(tc, 1, checkunitnumber(f, 4)); } @@ -533,8 +533,8 @@ static void test_pay_cmd_other_building(CuTest *tc) { setup_pay_cmd(&fix); f = fix.u1->faction; b = test_create_building(fix.u1->region, bt_get_or_create("lighthouse")); - set_param(&global.parameters, "rules.region_owners", "1"); - set_param(&global.parameters, "rules.region_owner_pay_building", "lighthouse"); + config_set("rules.region_owners", "1"); + config_set("rules.region_owner_pay_building", "lighthouse"); update_owners(b->region); _snprintf(cmd, sizeof(cmd), "NOT %s", itoa36(b->no)); @@ -633,7 +633,7 @@ static void test_newbie_cannot_guard(CuTest *tc) { guard_fixture fix; setup_guard(&fix, true); - set_param(&global.parameters, "NewbieImmunity", "4"); + config_set("NewbieImmunity", "4"); CuAssertTrue(tc, IsImmune(fix.u->faction)); update_guards(); CuAssertTrue(tc, !fval(fix.u, UFL_GUARD)); @@ -726,15 +726,15 @@ static void statistic_test(CuTest *tc, int peasants, int luck, int maxp, static void test_peasant_luck_effect(CuTest *tc) { test_cleanup(); - set_param(&global.parameters, "rules.peasants.peasantluck.factor", "10"); - set_param(&global.parameters, "rules.peasants.growth.factor", "0.001"); + config_set("rules.peasants.peasantluck.factor", "10"); + config_set("rules.peasants.growth.factor", "0.001"); statistic_test(tc, 100, 0, 1000, 0, 0, 0); statistic_test(tc, 100, 2, 1000, 0, 1, 1); statistic_test(tc, 1000, 400, 1000, 0, 3, 3); statistic_test(tc, 1000, 1000, 2000, .5, 1, 501); - set_param(&global.parameters, "rules.peasants.growth.factor", "1"); + config_set("rules.peasants.growth.factor", "1"); statistic_test(tc, 1000, 1000, 1000, 0, 501, 501); test_cleanup(); } @@ -1003,7 +1003,7 @@ static void test_long_order_hungry(CuTest *tc) { // see also default_order unit *u; test_cleanup(); - set_param(&global.parameters, "hunger.long", "1"); + config_set("hunger.long", "1"); u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); fset(u, UFL_HUNGER); u->faction->locale = get_or_create_locale("de"); @@ -1081,7 +1081,7 @@ static void test_ally_cmd(CuTest *tc) { static void test_nmr_warnings(CuTest *tc) { faction *f1, *f2; test_cleanup(); - set_param(&global.parameters, "nmr.timeout", "3"); + config_set("nmr.timeout", "3"); f1 = test_create_faction(0); f2 = test_create_faction(0); f2->age = 2; diff --git a/src/main.c b/src/main.c index fee0eaab0..6eac28470 100644 --- a/src/main.c +++ b/src/main.c @@ -162,7 +162,7 @@ static int parse_args(int argc, char **argv, int *exitcode) switch (argi[1]) { case 'r': i = get_arg(argc, argv, 2, i, &arg, 0); - set_param(&global.parameters, "config.rules", arg); + config_set("config.rules", arg); break; case 'f': i = get_arg(argc, argv, 2, i, &luafile, 0); diff --git a/src/market.test.c b/src/market.test.c index 12c9cab3f..6132db39b 100644 --- a/src/market.test.c +++ b/src/market.test.c @@ -43,7 +43,7 @@ static void test_market_curse(CuTest * tc) ltype->rtype->flags |= (RTF_ITEM | RTF_POOLED); lux = new_luxurytype(ltype, 0); - set_param(&global.parameters, "rules.region_owners", "1"); + config_set("rules.region_owners", "1"); btype = (building_type *)calloc(1, sizeof(building_type)); btype->_name = _strdup("market"); diff --git a/src/move.test.c b/src/move.test.c index 5c24ca72e..91dcbd2b4 100644 --- a/src/move.test.c +++ b/src/move.test.c @@ -212,7 +212,7 @@ static void test_walkingcapacity(CuTest *tc) { assert(itype); i_change(&u->items, itype, 1); CuAssertIntEquals(tc, cap + (STRENGTHMULTIPLIER-1) * u->_race->capacity, walkingcapacity(u)); - set_param(&global.parameters, "rules.trollbelt.multiplier", "5"); + config_set("rules.trollbelt.multiplier", "5"); CuAssertIntEquals(tc, cap + 4 * u->_race->capacity, walkingcapacity(u)); test_cleanup(); diff --git a/src/piracy.test.c b/src/piracy.test.c index 53907f4df..4ed735c45 100644 --- a/src/piracy.test.c +++ b/src/piracy.test.c @@ -21,7 +21,7 @@ static void setup_piracy(void) { ship_type *st_boat; test_cleanup(); - set_param(&global.parameters, "rules.ship.storms", "0"); + config_set("rules.ship.storms", "0"); lang = get_or_create_locale("de"); locale_setstring(lang, directions[D_EAST], "OSTEN"); init_directions(lang); diff --git a/src/settings.pkg b/src/settings.pkg index e216bf8b6..002ece6a9 100755 --- a/src/settings.pkg +++ b/src/settings.pkg @@ -1,10 +1,10 @@ $#undef tolua_reg_types $#define tolua_reg_types tolua_reg_types_config -$#include "bind_settings.h" +$#include module eressea { module settings { - void settings_set @ set(const char *key, const char *value); - const char * settings_get @ get(const char *key); + void config_set @ set(const char *key, const char *value); + const char * config_get @ get(const char *key); } } diff --git a/src/settings.pkg.c b/src/settings.pkg.c index 0dcd3df61..79ea280cb 100644 --- a/src/settings.pkg.c +++ b/src/settings.pkg.c @@ -20,7 +20,7 @@ LUALIB_API int luaopen_settings (lua_State* tolua_S); #undef tolua_reg_types #define tolua_reg_types tolua_reg_types_settings -#include "bind_settings.h" +#include /* function to register type */ static void tolua_reg_types (lua_State* tolua_S) @@ -44,7 +44,7 @@ static int tolua_settings_eressea_settings_set00(lua_State* tolua_S) const char* key = ((const char*) tolua_tostring(tolua_S,1,0)); const char* value = ((const char*) tolua_tostring(tolua_S,2,0)); { - settings_set(key,value); + config_set(key,value); } } return 0; @@ -70,7 +70,7 @@ static int tolua_settings_eressea_settings_get00(lua_State* tolua_S) { const char* key = ((const char*) tolua_tostring(tolua_S,1,0)); { - const char* tolua_ret = (const char*) settings_get(key); + const char* tolua_ret = (const char*) config_get(key); tolua_pushstring(tolua_S,(const char*)tolua_ret); } } diff --git a/src/study.test.c b/src/study.test.c index ac9d3947a..4b30ae842 100644 --- a/src/study.test.c +++ b/src/study.test.c @@ -27,7 +27,7 @@ static void setup_study(study_fixture *fix, skill_t sk) { assert(fix); test_cleanup(); - set_param(&global.parameters, "study.random_progress", "0"); + config_set("study.random_progress", "0"); test_create_world(); r = test_create_region(0, 0, 0); f = test_create_faction(0); diff --git a/src/upkeep.test.c b/src/upkeep.test.c index 424d5034c..bba43a95f 100644 --- a/src/upkeep.test.c +++ b/src/upkeep.test.c @@ -32,7 +32,7 @@ void test_upkeep_default(CuTest * tc) u2 = test_create_unit(f2, r); assert(r && u1 && u2); - set_param(&global.parameters, "rules.food.flags", "0"); + config_set("rules.food.flags", "0"); i_change(&u1->items, i_silver, 20); get_food(r); // since u1 and u2 are not allied, u1 should not help u2 with upkeep @@ -60,7 +60,7 @@ void test_upkeep_hunger_damage(CuTest * tc) u1 = test_create_unit(f1, r); assert(r && u1); - set_param(&global.parameters, "rules.food.flags", "0"); + config_set("rules.food.flags", "0"); u1->hp = 100; get_food(r); // since u1 and u2 are not allied, u1 should not help u2 with upkeep @@ -86,7 +86,7 @@ void test_upkeep_from_pool(CuTest * tc) u2 = test_create_unit(u1->faction, r); assert(r && u1 && u2); - set_param(&global.parameters, "rules.food.flags", "0"); + config_set("rules.food.flags", "0"); i_change(&u1->items, i_silver, 30); get_food(r); CuAssertIntEquals(tc, 10, i_get(u1->items, i_silver)); @@ -122,7 +122,7 @@ void test_upkeep_from_friend(CuTest * tc) u2 = test_create_unit(f2, r); assert(r && u1 && u2); - set_param(&global.parameters, "rules.food.flags", "0"); + config_set("rules.food.flags", "0"); i_change(&u1->items, i_silver, 30); get_food(r); CuAssertIntEquals(tc, 10, i_get(u1->items, i_silver)); @@ -151,7 +151,7 @@ void test_upkeep_free(CuTest * tc) u = test_create_unit(test_create_faction(test_create_race("human")), r); assert(r && u); - set_param(&global.parameters, "rules.food.flags", "4"); // FOOD_IS_FREE + config_set("rules.food.flags", "4"); // FOOD_IS_FREE get_food(r); CuAssertIntEquals(tc, 0, i_get(u->items, i_silver)); CuAssertIntEquals(tc, 0, fval(u, UFL_HUNGER)); From a4cb5e29066d3b0c31254e1c84b89b6b5955844e Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 22 Nov 2015 10:44:46 +0100 Subject: [PATCH 12/28] replace all get_param_* for global.parameters with config_get_* --- src/battle.c | 36 +++++++++++++++---------------- src/bindings.c | 2 +- src/chaos.c | 2 +- src/creport.c | 2 +- src/economy.c | 12 +++++------ src/give.c | 8 +++---- src/items/weapons.c | 6 ++---- src/jsreport.c | 2 +- src/kernel/build.c | 4 ++-- src/kernel/building.c | 2 +- src/kernel/config.c | 43 +++++++++++++++++++------------------- src/kernel/faction.c | 5 ++--- src/kernel/jsonconf.test.c | 14 ++++++------- src/kernel/resources.c | 4 ++-- src/kernel/ship.c | 2 +- src/kernel/skills.c | 2 +- src/kernel/teleport.c | 3 +-- src/kernel/unit.c | 11 +++++----- src/laws.c | 38 ++++++++++++++++----------------- src/magic.c | 13 ++++++------ src/monster.c | 2 +- src/monsters.c | 2 +- src/move.c | 26 +++++++++++------------ src/randenc.c | 18 +++++----------- src/reports.c | 2 +- src/study.c | 6 +++--- src/upkeep.c | 6 +++--- 27 files changed, 127 insertions(+), 146 deletions(-) diff --git a/src/battle.c b/src/battle.c index a897a300e..80c3c0583 100644 --- a/src/battle.c +++ b/src/battle.c @@ -136,26 +136,26 @@ static int skill_formula = 0; static void static_rules(void) { loot_rules = - get_param_int(global.parameters, "rules.combat.loot", + config_get_int("rules.combat.loot", LOOT_MONSTERS | LOOT_OTHERS | LOOT_KEEPLOOT); /* new formula to calculate to-hit-chance */ skill_formula = - get_param_int(global.parameters, "rules.combat.skill_formula", + config_get_int("rules.combat.skill_formula", FORMULA_ORIG); /* maximum number of combat turns */ max_turns = - get_param_int(global.parameters, "rules.combat.turns", COMBAT_TURNS); + config_get_int("rules.combat.turns", COMBAT_TURNS); /* damage calculation */ - if (get_param_int(global.parameters, "rules.combat.critical", 1)) { + if (config_get_int("rules.combat.critical", 1)) { damage_rules |= DAMAGE_CRITICAL; } - if (get_param_int(global.parameters, "rules.combat.melee_bonus", 1)) { + if (config_get_int("rules.combat.melee_bonus", 1)) { damage_rules |= DAMAGE_MELEE_BONUS; } - if (get_param_int(global.parameters, "rules.combat.missile_bonus", 1)) { + if (config_get_int("rules.combat.missile_bonus", 1)) { damage_rules |= DAMAGE_MISSILE_BONUS; } - if (get_param_int(global.parameters, "rules.combat.skill_bonus", 1)) { + if (config_get_int("rules.combat.skill_bonus", 1)) { damage_rules |= DAMAGE_SKILL_BONUS; } } @@ -667,7 +667,7 @@ static int CavalrySkill(void) static int skill = -1; if (skill < 0) { - skill = get_param_int(global.parameters, "rules.cavalry.skill", 2); + skill = config_get_int("rules.cavalry.skill", 2); } return skill; } @@ -679,7 +679,7 @@ static int CavalryBonus(const unit * u, troop enemy, int type) static int mode = -1; if (mode < 0) { - mode = get_param_int(global.parameters, "rules.cavalry.mode", 1); + mode = config_get_int("rules.cavalry.mode", 1); } if (mode == 0) { /* old rule, Eressea 1.0 compat */ @@ -1008,7 +1008,7 @@ static void vampirism(troop at, int damage) { static int vampire = -1; if (vampire < 0) - vampire = get_param_int(global.parameters, "rules.combat.demon_vampire", 0); + vampire = config_get_int("rules.combat.demon_vampire", 0); if (vampire > 0) { int gain = damage / vampire; int chance = damage - vampire * gain; @@ -1108,7 +1108,7 @@ int calculate_armor(troop dt, const weapon_type *dwtype, const weapon_type *awty am = select_magicarmor(dt); if (rule_armor < 0) { - rule_armor = get_param_int(global.parameters, "rules.combat.nat_armor", 0); + rule_armor = config_get_int("rules.combat.nat_armor", 0); } if (rule_armor == 0) { /* natürliche Rüstung ist halbkumulativ */ @@ -1616,7 +1616,7 @@ static troop select_opponent(battle * b, troop at, int mindist, int maxdist) if (tactics_formula < 0) { tactics_formula = - get_param_int(global.parameters, "rules.tactics.formula", 0); + config_get_int("rules.tactics.formula", 0); } if (tactics_formula == 1) { int tactics = get_tactics(at.fighter->side, dt.fighter->side); @@ -1942,7 +1942,7 @@ int skilldiff(troop at, troop dt, int dist) static int goblin_bonus = -1; if (goblin_bonus < 0) goblin_bonus = - get_param_int(global.parameters, "rules.combat.goblinbonus", 10); + config_get_int("rules.combat.goblinbonus", 10); if (af->side->size[SUM_ROW] >= df->side->size[SUM_ROW] * goblin_bonus) { skdiff += 1; } @@ -2142,7 +2142,7 @@ static void make_heroes(battle * b) side *s; static int hero_speed = 0; if (hero_speed == 0) { - hero_speed = get_param_int(global.parameters, "rules.combat.herospeed", 10); + hero_speed = config_get_int("rules.combat.herospeed", 10); } for (s = b->sides; s != b->sides + b->nsides; ++s) { fighter *fig; @@ -2546,7 +2546,7 @@ static int loot_quota(const unit * src, const unit * dst, static double divisor = -1; if (dst && src && src->faction != dst->faction) { if (divisor < 0) { - divisor = get_param_flt(global.parameters, "rules.items.loot_divisor", 1); + divisor = config_get_flt("rules.items.loot_divisor", 1); assert(divisor == 0 || divisor >= 1); } if (divisor >= 1) { @@ -2654,7 +2654,7 @@ static double PopulationDamage(void) static double value = -1.0; if (value < 0) { int damage = - get_param_int(global.parameters, "rules.combat.populationdamage", + config_get_int("rules.combat.populationdamage", BATTLE_KILLS_PEASANTS); value = damage / 100.0; } @@ -2908,7 +2908,7 @@ static void aftermath(battle * b) int n = b->turn - 2; if (n > 0) { double dmg = - get_param_flt(global.parameters, "rules.ship.damage.battleround", + config_get_flt("rules.ship.damage.battleround", 0.05F); damage_ship(sh, dmg * n); freset(sh, SF_DAMAGED); @@ -3216,7 +3216,7 @@ side * find_side(battle * b, const faction * f, const group * g, unsigned int fl static int rule_anon_battle = -1; if (rule_anon_battle < 0) { - rule_anon_battle = get_param_int(global.parameters, "rules.stealth.anon_battle", 1); + rule_anon_battle = config_get_int("rules.stealth.anon_battle", 1); } for (s = b->sides; s != b->sides + b->nsides; ++s) { if (s->faction == f && s->group == g) { diff --git a/src/bindings.c b/src/bindings.c index 20a8192e1..156062869 100755 --- a/src/bindings.c +++ b/src/bindings.c @@ -1006,7 +1006,7 @@ static void parse_inifile(lua_State * L, dictionary * d, const char *section) lua_pushstring(L, "reportpath"); lua_pushstring(L, reportpath()); lua_rawset(L, -3); - arg = get_param(global.parameters, "config.rules"); + arg = config_get("config.rules"); if (arg) { lua_pushstring(L, "rules"); lua_pushstring(L, arg); diff --git a/src/chaos.c b/src/chaos.c index 728048bf9..a84b26dfc 100644 --- a/src/chaos.c +++ b/src/chaos.c @@ -193,7 +193,7 @@ static void chaos(region * r) while (sh) { ship *nsh = sh->next; double dmg = - get_param_flt(global.parameters, "rules.ship.damage.atlantis", + config_get_flt("rules.ship.damage.atlantis", 0.50); damage_ship(sh, dmg); if (sh->damage >= sh->size * DAMAGE_SCALE) { diff --git a/src/creport.c b/src/creport.c index 8078c545c..80ce1ba91 100644 --- a/src/creport.c +++ b/src/creport.c @@ -1505,7 +1505,7 @@ report_computer(const char *filename, report_context * ctx, const char *charset) FILE *F = fopen(filename, "wt"); if (era < 0) { - era = get_param_int(global.parameters, "world.era", 1); + era = config_get_int("world.era", 1); } if (F == NULL) { perror(filename); diff --git a/src/economy.c b/src/economy.c index ce5bc3f36..64a2a6771 100644 --- a/src/economy.c +++ b/src/economy.c @@ -104,7 +104,7 @@ static void recruit_init(void) { if (rules_recruit < 0) { rules_recruit = 0; - if (get_param_int(global.parameters, "recruit.allow_merge", 1)) { + if (config_get_int("recruit.allow_merge", 1)) { rules_recruit |= RECRUIT_MERGE; } } @@ -2745,11 +2745,11 @@ void entertain_cmd(unit * u, struct order *ord) kwd = init_order(ord); assert(kwd == K_ENTERTAIN); if (!entertainbase) { - const char *str = get_param(global.parameters, "entertain.base"); + const char *str = config_get("entertain.base"); entertainbase = str ? atoi(str) : 0; } if (!entertainperlevel) { - const char *str = get_param(global.parameters, "entertain.perlevel"); + const char *str = config_get("entertain.perlevel"); entertainperlevel = str ? atoi(str) : 0; } if (fval(u, UFL_WERE)) { @@ -3020,7 +3020,7 @@ void loot_cmd(unit * u, struct order *ord, request ** lootorders) kwd = init_order(ord); assert(kwd == K_LOOT); - if (get_param_int(global.parameters, "rules.enable_loot", 0) == 0 && !is_monsters(u->faction)) { + if (config_get_int("rules.enable_loot", 0) == 0 && !is_monsters(u->faction)) { return; } @@ -3136,7 +3136,7 @@ static void peasant_taxes(region * r) static bool rule_auto_taxation(void) { - int rule = get_param_int(global.parameters, "rules.economy.taxation", 0); + int rule = config_get_int("rules.economy.taxation", 0); return rule != 0; } @@ -3161,7 +3161,7 @@ void produce(struct region *r) * lehren vor lernen. */ if (rule_autowork < 0) { - rule_autowork = get_param_int(global.parameters, "work.auto", 0); + rule_autowork = config_get_int("work.auto", 0); } assert(rmoney(r) >= 0); diff --git a/src/give.c b/src/give.c index 5dead1277..634a37be6 100644 --- a/src/give.c +++ b/src/give.c @@ -52,12 +52,12 @@ #define RESERVE_GIVE /* reserve anything that's given from one unit to another? */ static int max_transfers(void) { - return get_param_int(global.parameters, "rules.give.max_men", 5); + return config_get_int("rules.give.max_men", 5); } static int GiveRestriction(void) { - return get_param_int(global.parameters, "GiveRestriction", 0); + return config_get_int("GiveRestriction", 0); } static void feedback_give_not_allowed(unit * u, order * ord) @@ -132,7 +132,7 @@ int give_quota(const unit * src, const unit * dst, const item_type * type, return n; } if (dst && src && src->faction != dst->faction) { - divisor = get_param_flt(global.parameters, "rules.items.give_divisor", 1); + divisor = config_get_flt("rules.items.give_divisor", 1); assert(divisor == 0 || divisor >= 1); if (divisor >= 1) { /* predictable > correct: */ @@ -235,7 +235,7 @@ static bool can_give_men(const unit *u, order *ord, message **msg) { static bool rule_transfermen(void) { - int rule = get_param_int(global.parameters, "rules.transfermen", 1); + int rule = config_get_int("rules.transfermen", 1); return rule != 0; } diff --git a/src/items/weapons.c b/src/items/weapons.c index 9576ebd49..f5015e39a 100644 --- a/src/items/weapons.c +++ b/src/items/weapons.c @@ -141,13 +141,11 @@ int *casualties) d += terminate(dt, *at, AT_STANDARD, wp->type->damage[0], true); #ifdef CATAPULT_STRUCTURAL_DAMAGE if (dt.fighter->unit->building && rng_int() % 100 < 5) { - float dmg = - get_param_flt(global.parameters, "rules.building.damage.catapult", 1); + double dmg = config_get_flt("rules.building.damage.catapult", 1); damage_building(b, dt.fighter->unit->building, dmg); } else if (dt.fighter->unit->ship && rng_int() % 100 < 5) { - float dmg = - get_param_flt(global.parameters, "rules.ship.damage.catapult", 0.01); + double dmg = config_get_flt("rules.ship.damage.catapult", 0.01); damage_ship(dt.fighter->unit->ship, dmg) } #endif diff --git a/src/jsreport.c b/src/jsreport.c index a6be8b46b..4450d0165 100644 --- a/src/jsreport.c +++ b/src/jsreport.c @@ -25,7 +25,7 @@ static void coor_from_tiled(int *x, int *y) { static int report_json(const char *filename, report_context * ctx, const char *charset) { - if (get_param_int(global.parameters, "jsreport.enabled", 0) != 0) { + if (config_get_int("jsreport.enabled", 0) != 0) { FILE * F = fopen(filename, "w"); if (F) { int x, y, minx = INT_MAX, maxx = INT_MIN, miny = INT_MAX, maxy = INT_MIN; diff --git a/src/kernel/build.c b/src/kernel/build.c index 76821a190..20f70800f 100644 --- a/src/kernel/build.c +++ b/src/kernel/build.c @@ -419,7 +419,7 @@ int roqf_factor(void) { int value = -1; if (value < 0) { - value = get_param_int(global.parameters, "rules.economy.roqf", 10); + value = config_get_int("rules.economy.roqf", 10); } return value; } @@ -751,7 +751,7 @@ build_building(unit * u, const building_type * btype, int id, int want, order * if (b) { if (rule_other < 0) { rule_other = - get_param_int(global.parameters, "rules.build.other_buildings", 1); + config_get_int("rules.build.other_buildings", 1); } if (!rule_other) { unit *owner = building_owner(b); diff --git a/src/kernel/building.c b/src/kernel/building.c index 55005722e..4867b3687 100644 --- a/src/kernel/building.c +++ b/src/kernel/building.c @@ -531,7 +531,7 @@ int bt_effsize(const building_type * btype, const building * b, int bsize) const construction *cons = btype->construction; /* TECH DEBT: simplest thing that works for E3 dwarf/halfling faction rules */ - if (b && get_param_int(global.parameters, "rules.dwarf_castles", 0) + if (b && config_get_int("rules.dwarf_castles", 0) && strcmp(btype->_name, "castle") == 0) { unit *u = building_owner(b); if (u && u->faction->race == get_race(RC_HALFLING)) { diff --git a/src/kernel/config.c b/src/kernel/config.c index 1f44530f9..1ba1f6c7f 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -110,7 +110,7 @@ int turn = -1; int NewbieImmunity(void) { - return get_param_int(global.parameters, "NewbieImmunity", 0); + return config_get_int("NewbieImmunity", 0); } bool IsImmune(const faction * f) @@ -137,7 +137,7 @@ static int ally_flag(const char *s, int help_mask) bool ExpensiveMigrants(void) { - return get_param_int(global.parameters, "study.expensivemigrants", 0) != 0; + return config_get_int("study.expensivemigrants", 0) != 0; } /** Specifies automatic alliance modes. @@ -147,7 +147,7 @@ bool ExpensiveMigrants(void) int AllianceAuto(void) { int value; - const char *str = get_param(global.parameters, "alliance.auto"); + const char *str = config_get("alliance.auto"); value = 0; if (str != NULL) { char *sstr = _strdup(str); @@ -169,7 +169,7 @@ int AllianceAuto(void) */ int HelpMask(void) { - const char *str = get_param(global.parameters, "rules.help.mask"); + const char *str = config_get("rules.help.mask"); int rule = 0; if (str != NULL) { char *sstr = _strdup(str); @@ -188,7 +188,7 @@ int HelpMask(void) int AllianceRestricted(void) { - const char *str = get_param(global.parameters, "alliance.restricted"); + const char *str = config_get("alliance.restricted"); int rule = 0; if (str != NULL) { char *sstr = _strdup(str); @@ -211,18 +211,18 @@ int LongHunger(const struct unit *u) if (u_race(u) == get_race(RC_DAEMON)) return false; } - return get_param_int(global.parameters, "hunger.long", 0); + return config_get_int("hunger.long", 0); } int SkillCap(skill_t sk) { if (sk == SK_MAGIC) return 0; /* no caps on magic */ - return get_param_int(global.parameters, "skill.maxlevel", 0); + return config_get_int("skill.maxlevel", 0); } int NMRTimeout(void) { - return get_param_int(global.parameters, "nmr.timeout", 0); + return config_get_int("nmr.timeout", 0); } race_t old_race(const struct race * rc) @@ -371,8 +371,7 @@ static attrib_type at_maxmagicians = { int max_magicians(const faction * f) { - int m = - get_param_int(global.parameters, "rules.maxskills.magic", MAXMAGICIANS); + int m = config_get_int("rules.maxskills.magic", MAXMAGICIANS); attrib *a; if ((a = a_find(f->attribs, &at_maxmagicians)) != NULL) { @@ -567,7 +566,7 @@ int count_maxmigrants(const faction * f) static int migrants = -1; if (migrants < 0) { - migrants = get_param_int(global.parameters, "rules.migrants.max", INT_MAX); + migrants = config_get_int("rules.migrants.max", INT_MAX); } if (migrants == INT_MAX) { int x = 0; @@ -931,7 +930,7 @@ void init_locale(struct locale *lang) tokens = get_translations(lang, UT_MAGIC); if (tokens) { - const char *str = get_param(global.parameters, "rules.magic.playerschools"); + const char *str = config_get("rules.magic.playerschools"); char *sstr, *tok; if (str == NULL) { str = "gwyrrd illaun draig cerddor tybied"; @@ -1281,25 +1280,25 @@ int cmp_current_owner(const building * b, const building * a) bool rule_stealth_other(void) { - int rule = get_param_int(global.parameters, "stealth.faction.other", 1); + int rule = config_get_int("stealth.faction.other", 1); return rule != 0; } bool rule_stealth_anon(void) { - int rule = get_param_int(global.parameters, "stealth.faction.anon", 1); + int rule = config_get_int("stealth.faction.anon", 1); return rule != 0; } bool rule_region_owners(void) { - int rule = get_param_int(global.parameters, "rules.region_owners", 0); + int rule = config_get_int("rules.region_owners", 0); return rule != 0; } int rule_blessed_harvest(void) { - int rule = get_param_int(global.parameters, "rules.blessed_harvest.flags", + int rule = config_get_int("rules.blessed_harvest.flags", HARVEST_WORK); assert(rule >= 0); return rule; @@ -1307,14 +1306,14 @@ int rule_blessed_harvest(void) int rule_alliance_limit(void) { - int rule = get_param_int(global.parameters, "rules.limit.alliance", 0); + int rule = config_get_int("rules.limit.alliance", 0); assert(rule >= 0); return rule; } int rule_faction_limit(void) { - int rule = get_param_int(global.parameters, "rules.limit.faction", 0); + int rule = config_get_int("rules.limit.faction", 0); assert(rule >= 0); return rule; } @@ -1564,12 +1563,12 @@ int entertainmoney(const region * r) int rule_give(void) { - return get_param_int(global.parameters, "rules.give.flags", GIVE_DEFAULT); + return config_get_int("rules.give.flags", GIVE_DEFAULT); } bool markets_module(void) { - return get_param_int(global.parameters, "modules.markets", 0); + return config_get_int("modules.markets", 0); } void config_set(const char *key, const char *value) { @@ -1629,11 +1628,11 @@ void free_gamedata(void) } const char * game_name(void) { - const char * param = get_param(global.parameters, "game.name"); + const char * param = config_get("game.name"); return param ? param : global.gamename; } int game_id(void) { - return get_param_int(global.parameters, "game.id", 0); + return config_get_int("game.id", 0); } diff --git a/src/kernel/faction.c b/src/kernel/faction.c index c5ecbcb43..71286cffe 100755 --- a/src/kernel/faction.c +++ b/src/kernel/faction.c @@ -568,7 +568,7 @@ static int allied_skilllimit(const faction * f, skill_t sk) { static int value = -1; if (value < 0) { - value = get_param_int(global.parameters, "alliance.skilllimit", 0); + value = config_get_int("alliance.skilllimit", 0); } return value; } @@ -611,8 +611,7 @@ int skill_limit(faction * f, skill_t sk) m = max_magicians(f); } else if (sk == SK_ALCHEMY) { - m = get_param_int(global.parameters, "rules.maxskills.alchemy", - MAXALCHEMISTS); + m = config_get_int("rules.maxskills.alchemy", MAXALCHEMISTS); } return m; } diff --git a/src/kernel/jsonconf.test.c b/src/kernel/jsonconf.test.c index 6441ad424..5e7d3047e 100644 --- a/src/kernel/jsonconf.test.c +++ b/src/kernel/jsonconf.test.c @@ -74,11 +74,11 @@ static void test_settings(CuTest * tc) test_cleanup(); json_config(json); - CuAssertStrEquals(tc, "1", get_param(global.parameters, "true")); - CuAssertStrEquals(tc, "0", get_param(global.parameters, "false")); - CuAssertStrEquals(tc, "1d4", get_param(global.parameters, "string")); - CuAssertIntEquals(tc, 14, get_param_int(global.parameters, "integer", 0)); - CuAssertDblEquals(tc, 1.5f, get_param_flt(global.parameters, "float", 0), 0.01); + CuAssertStrEquals(tc, "1", config_get("true")); + CuAssertStrEquals(tc, "0", config_get("false")); + CuAssertStrEquals(tc, "1d4", config_get("string")); + CuAssertIntEquals(tc, 14, config_get_int("integer", 0)); + CuAssertDblEquals(tc, 1.5f, config_get_flt("float", 0), 0.01); cJSON_Delete(json); test_cleanup(); } @@ -117,13 +117,13 @@ static void test_disable(CuTest * tc) CuAssertTrue(tc, !keyword_disabled(K_BANNER)); CuAssertTrue(tc, !keyword_disabled(K_PAY)); CuAssertTrue(tc, !keyword_disabled(K_BESIEGE)); - CuAssertIntEquals(tc, 1, get_param_int(global.parameters, "module.enabled", 1)); + CuAssertIntEquals(tc, 1, config_get_int("module.enabled", 1)); json_config(json); CuAssertTrue(tc, !skill_enabled(SK_ALCHEMY)); CuAssertTrue(tc, !keyword_disabled(K_BANNER)); CuAssertTrue(tc, keyword_disabled(K_PAY)); CuAssertTrue(tc, keyword_disabled(K_BESIEGE)); - CuAssertIntEquals(tc, 0, get_param_int(global.parameters, "module.enabled", 1)); + CuAssertIntEquals(tc, 0, config_get_int("module.enabled", 1)); cJSON_Delete(json); test_cleanup(); } diff --git a/src/kernel/resources.c b/src/kernel/resources.c index d9eff6597..9db74a99f 100644 --- a/src/kernel/resources.c +++ b/src/kernel/resources.c @@ -31,7 +31,7 @@ static double ResourceFactor(void) { static double value = -1.0; if (value < 0) { - const char *str = get_param(global.parameters, "resource.factor"); + const char *str = config_get("resource.factor"); value = str ? atof(str) : 1.0; } return value; @@ -84,7 +84,7 @@ void terraform_resources(region * r) const terrain_type *terrain = r->terrain; static int terraform_all = -1; if (terraform_all < 0) { - terraform_all = get_param_int(global.parameters, "rules.terraform.all", 0); + terraform_all = config_get_int("rules.terraform.all", 0); } if (terrain->production == NULL) diff --git a/src/kernel/ship.c b/src/kernel/ship.c index c77a10bd4..f20c8ef59 100644 --- a/src/kernel/ship.c +++ b/src/kernel/ship.c @@ -271,7 +271,7 @@ const char *write_shipname(const ship * sh, char *ibuf, size_t size) static int ShipSpeedBonus(const unit * u) { - int level = get_param_int(global.parameters, "movement.shipspeed.skillbonus", 0); + int level = config_get_int("movement.shipspeed.skillbonus", 0); if (level > 0) { ship *sh = u->ship; int skl = effskill(u, SK_SAILING, 0); diff --git a/src/kernel/skills.c b/src/kernel/skills.c index d3e3f0ba8..7ba325dcc 100644 --- a/src/kernel/skills.c +++ b/src/kernel/skills.c @@ -212,7 +212,7 @@ void sk_set(skill * sv, int level) static int rule_random_progress(void) { - return get_param_int(global.parameters, "study.random_progress", 1); + return config_get_int("study.random_progress", 1); } int skill_weeks(int level) diff --git a/src/kernel/teleport.c b/src/kernel/teleport.c index 8d9b696a5..59b18ade8 100644 --- a/src/kernel/teleport.c +++ b/src/kernel/teleport.c @@ -182,8 +182,7 @@ bool is_astral(const region * r) plane *get_astralplane(void) { plane *astralspace = 0; - int rule_astralplane = - get_param_int(global.parameters, "modules.astralspace", 1); + int rule_astralplane = config_get_int("modules.astralspace", 1); if (!rule_astralplane) { return NULL; diff --git a/src/kernel/unit.c b/src/kernel/unit.c index c37bbb5a0..b328b748b 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -232,7 +232,7 @@ static buddy *get_friends(const unit * u, int *numfriends) for (u2 = r->units; u2; u2 = u2->next) { if (u2->faction != f && u2->number > 0) { int allied = 0; - if (get_param_int(global.parameters, "rules.alliances", 0) != 0) { + if (config_get_int("rules.alliances", 0) != 0) { allied = (f->alliance && f->alliance == u2->faction->alliance); } else if (alliedunit(u, u2->faction, HELP_MONEY) @@ -856,7 +856,7 @@ bool can_leave(unit * u) return true; } - rule_leave = get_param_int(global.parameters, "rules.move.owner_leave", 0); + rule_leave = config_get_int("rules.move.owner_leave", 0); if (rule_leave!=0 && u->building && u == building_owner(u->building)) { return false; @@ -1343,7 +1343,7 @@ int get_modifier(const unit * u, skill_t sk, int level, const region * r, bool n skill = skillmod(u->attribs, u, r, sk, skill, SMF_ALWAYS); if (hunger_red_skill == -1) { - hunger_red_skill = get_param_int(global.parameters, "rules.hunger.reduces_skill", 2); + hunger_red_skill = config_get_int("rules.hunger.reduces_skill", 2); } if (fval(u, UFL_HUNGER) && hunger_red_skill) { @@ -1727,8 +1727,7 @@ int unit_max_hp(const unit * u) static const curse_type *heal_ct = NULL; if (rules_stamina < 0) { - rules_stamina = - get_param_int(global.parameters, "rules.stamina", STAMINA_AFFECTS_HP); + rules_stamina = config_get_int("rules.stamina", STAMINA_AFFECTS_HP); } h = u_race(u)->hitpoints; @@ -1931,7 +1930,7 @@ bool unit_can_study(const unit *u) { } static double produceexp_chance(void) { - return get_param_flt(global.parameters, "study.from_use", 1.0 / 3); + return config_get_flt("study.from_use", 1.0 / 3); } void produceexp_ex(struct unit *u, skill_t sk, int n, bool (*learn)(unit *, skill_t, double)) diff --git a/src/laws.c b/src/laws.c index 2da0a1a51..175c12f6a 100755 --- a/src/laws.c +++ b/src/laws.c @@ -117,7 +117,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. static bool RemoveNMRNewbie(void) { - int value = get_param_int(global.parameters, "nmr.removenewbie", 0); + int value = config_get_int("nmr.removenewbie", 0); return value!=0; } @@ -245,7 +245,7 @@ static void calculate_emigration(region * r) static double peasant_growth_factor(void) { - return get_param_flt(global.parameters, "rules.peasants.growth.factor", 0.0001F * PEASANTGROWTH); + return config_get_flt("rules.peasants.growth.factor", 0.0001F * PEASANTGROWTH); } #ifdef SLOWLUCK @@ -275,7 +275,7 @@ int peasant_luck_effect(int peasants, int luck, int maxp, double variance) { #else static double peasant_luck_factor(void) { - return get_param_flt(global.parameters, "rules.peasants.peasantluck.factor", PEASANTLUCK); + return config_get_flt("rules.peasants.peasantluck.factor", PEASANTLUCK); } int peasant_luck_effect(int peasants, int luck, int maxp, double variance) @@ -305,7 +305,7 @@ static void peasants(region * r) int n, satiated; int dead = 0; - if (peasants > 0 && get_param_int(global.parameters, "rules.peasants.growth", 1)) { + if (peasants > 0 && config_get_int("rules.peasants.growth", 1)) { int luck = 0; double fraction = peasants * peasant_growth_factor(); int births = RAND_ROUND(fraction); @@ -692,7 +692,7 @@ void immigration(void) { region *r; log_info(" - Einwanderung..."); - int repopulate = get_param_int(global.parameters, "rules.economy.repopulate_maximum", 90); + int repopulate = config_get_int("rules.economy.repopulate_maximum", 90); for (r = regions; r; r = r->next) { if (r->land && r->land->newpeasants) { int rp = rpeasants(r) + r->land->newpeasants; @@ -738,7 +738,7 @@ void nmr_warnings(void) message *msg = NULL; for (fa = factions; fa; fa = fa->next) { int warn = 0; - if (get_param_int(global.parameters, "rules.alliances", 0) != 0) { + if (config_get_int("rules.alliances", 0) != 0) { if (f->alliance && f->alliance == fa->alliance) { warn = 1; } @@ -789,7 +789,7 @@ void demographics(void) if (plant_rules < 0) { plant_rules = - get_param_int(global.parameters, "rules.grow.formula", 0); + config_get_int("rules.grow.formula", 0); } for (dmd = r->land->demands; dmd; dmd = dmd->next) { if (dmd->value > 0 && dmd->value < MAXDEMAND) { @@ -990,7 +990,7 @@ static bool CheckOverload(void) { static int value = -1; if (value < 0) { - value = get_param_int(global.parameters, "rules.check_overload", 0); + value = config_get_int("rules.check_overload", 0); } return value != 0; } @@ -1211,7 +1211,7 @@ static void nmr_death(faction * f) { static int rule = -1; if (rule < 0) - rule = get_param_int(global.parameters, "rules.nmr.destroy", 0); + rule = config_get_int("rules.nmr.destroy", 0); if (rule) { unit *u; for (u = f->units; u; u = u->nextF) { @@ -2742,14 +2742,12 @@ void sinkships(struct region * r) if (fval(r->terrain, SEA_REGION)) { if (!enoughsailors(sh, crew_skill(sh))) { // ship is at sea, but not enough people to control it - double dmg = get_param_flt(global.parameters, - "rules.ship.damage.nocrewocean", - 0.30F); + double dmg = config_get_flt("rules.ship.damage.nocrewocean", 0.3); damage_ship(sh, dmg); } } else if (!ship_owner(sh)) { // any ship lying around without an owner slowly rots - double dmg = get_param_flt(global.parameters, "rules.ship.damage.nocrew", 0.05F); + double dmg = config_get_flt("rules.ship.damage.nocrew", 0.05); damage_ship(sh, dmg); } } @@ -4260,7 +4258,7 @@ static void do_force_leave(region *r) { } bool rule_force_leave(int flags) { - int rules = get_param_int(global.parameters, "rules.owners.force_leave", 0); + int rules = config_get_int("rules.owners.force_leave", 0); return (rules&flags) == flags; } @@ -4331,7 +4329,7 @@ void init_processor(void) add_proc_order(p, K_GUARD, guard_off_cmd, 0, NULL); add_proc_order(p, K_RESHOW, reshow_cmd, 0, NULL); - if (get_param_int(global.parameters, "rules.alliances", 0) == 1) { + if (config_get_int("rules.alliances", 0) == 1) { p += 10; add_proc_global(p, alliance_cmd, NULL); } @@ -4363,7 +4361,7 @@ void init_processor(void) p += 10; /* can't allow reserve before siege (weapons) */ add_proc_region(p, enter_1, "Betreten (3. Versuch)"); /* to claim a castle after a victory and to be able to DESTROY it in the same turn */ - if (get_param_int(global.parameters, "rules.reserve.twophase", 0)) { + if (config_get_int("rules.reserve.twophase", 0)) { add_proc_order(p, K_RESERVE, reserve_self, 0, "RESERVE (self)"); p += 10; } @@ -4415,7 +4413,7 @@ void init_processor(void) p += 10; add_proc_global(p, movement, "Bewegungen"); - if (get_param_int(global.parameters, "work.auto", 0)) { + if (config_get_int("work.auto", 0)) { p += 10; add_proc_region(p, auto_work, "Arbeiten (auto)"); } @@ -4423,7 +4421,7 @@ void init_processor(void) p += 10; add_proc_order(p, K_GUARD, guard_on_cmd, 0, "Bewache (an)"); - if (get_param_int(global.parameters, "rules.encounters", 0)) { + if (config_get_int("rules.encounters", 0)) { p += 10; add_proc_global(p, encounters, "Zufallsbegegnungen"); } @@ -4461,7 +4459,7 @@ void processorders(void) process(); /*************************************************/ - if (get_param_int(global.parameters, "modules.markets", 0)) { + if (config_get_int("modules.markets", 0)) { do_markets(); } @@ -4470,7 +4468,7 @@ void processorders(void) remove_empty_units(); /* must happen AFTER age, because that would destroy them right away */ - if (get_param_int(global.parameters, "modules.wormholes", 0)) { + if (config_get_int("modules.wormholes", 0)) { wormholes_update(); } diff --git a/src/magic.c b/src/magic.c index 1873bb041..1c6acda94 100644 --- a/src/magic.c +++ b/src/magic.c @@ -112,7 +112,7 @@ static float MagicRegeneration(void) { static float value = -1.0; if (value < 0) { - const char *str = get_param(global.parameters, "magic.regeneration"); + const char *str = config_get("magic.regeneration"); value = str ? (float)atof(str) : 1.0F; } return value; @@ -121,7 +121,7 @@ static float MagicRegeneration(void) static double MagicPower(double force) { if (force > 0) { - const char *str = get_param(global.parameters, "magic.power"); + const char *str = config_get("magic.power"); double value = str ? atof(str) : 1.0; return _max(value * force, 1.0f); } @@ -217,8 +217,7 @@ bool FactionSpells(void) { static int rules_factionspells = -1; if (rules_factionspells < 0) { - rules_factionspells = - get_param_int(global.parameters, "rules.magic.factionlist", 0); + rules_factionspells = config_get_int("rules.magic.factionlist", 0); } return rules_factionspells!=0; } @@ -1034,7 +1033,7 @@ spellpower(region * r, unit * u, const spell * sp, int cast_level, struct order if (btype && btype->flags & BTF_MAGIC) ++force; } - elf_power = get_param_int(global.parameters, "rules.magic.elfpower", 0); + elf_power = config_get_int("rules.magic.elfpower", 0); if (elf_power && u_race(u) == get_race(RC_ELF) && r_isforest(r)) { ++force; @@ -1291,7 +1290,7 @@ bool fumble(region * r, unit * u, const spell * sp, int cast_grade) int effsk = effskill(u, SK_MAGIC, r); struct building *b = inside_building(u); const struct building_type *btype = building_is_active(b) ? b->type : NULL; - int fumble_enabled = get_param_int(global.parameters, "magic.fumble.enable", 1); + int fumble_enabled = config_get_int("magic.fumble.enable", 1); sc_mage * mage; if (effsk<=0 || !fumble_enabled) { @@ -1463,7 +1462,7 @@ void regenerate_aura(void) double reg_aura; int regen; double mod; - int regen_enabled = get_param_int(global.parameters, "magic.regeneration.enable", 1); + int regen_enabled = config_get_int("magic.regeneration.enable", 1); if (!regen_enabled) return; diff --git a/src/monster.c b/src/monster.c index 47858fc5d..101377ef0 100644 --- a/src/monster.c +++ b/src/monster.c @@ -222,7 +222,7 @@ faction *get_or_create_monsters(void) faction *f = findfaction(MONSTER_ID); if (!f) { const race *rc = rc_get_or_create("dragon"); - const char *email = get_param(global.parameters, "monster.email"); + const char *email = config_get("monster.email"); f = addfaction(email ? email : "noreply@eressea.de", NULL, rc, default_locale, 0); renumber_faction(f, MONSTER_ID); faction_setname(f, "Monster"); diff --git a/src/monsters.c b/src/monsters.c index 541a71c3f..5ca357099 100644 --- a/src/monsters.c +++ b/src/monsters.c @@ -82,7 +82,7 @@ static void give_peasants(unit *u, const item_type *itype, int reduce) { } static double monster_attack_chance(void) { - return get_param_flt(global.parameters, "rules.monsters.attack_chance", 0.4f); + return config_get_flt("rules.monsters.attack_chance", 0.4); } static void reduce_weight(unit * u) diff --git a/src/move.c b/src/move.c index 2eded780d..517301828 100644 --- a/src/move.c +++ b/src/move.c @@ -319,7 +319,7 @@ int walkingcapacity(const struct unit *u) if (rbelt) { int belts = i_get(u->items, rbelt->itype); if (belts) { - int multi = get_param_int(global.parameters, "rules.trollbelt.multiplier", STRENGTHMULTIPLIER); + int multi = config_get_int("rules.trollbelt.multiplier", STRENGTHMULTIPLIER); n += _min(people, belts) * (multi - 1) * u_race(u)->capacity; } } @@ -694,7 +694,7 @@ static float damage_drift(void) { static float value = -1.0F; if (value < 0) { - value = (float)get_param_flt(global.parameters, "rules.ship.damage_drift", 0.02F); + value = (float)config_get_flt("rules.ship.damage_drift", 0.02F); } return value; } @@ -702,7 +702,7 @@ static float damage_drift(void) static void drifting_ships(region * r) { direction_t d; - bool drift = get_param_int(global.parameters, "rules.ship.drifting", 1) != 0; + bool drift = config_get_int("rules.ship.drifting", 1) != 0; if (fval(r->terrain, SEA_REGION)) { ship **shp = &r->ships; @@ -839,12 +839,12 @@ static unit *bewegung_blockiert_von(unit * reisender, region * r) int stealth = eff_stealth(reisender, r); const struct resource_type *ramulet = get_resourcetype(R_AMULET_OF_TRUE_SEEING); - double base_prob = get_param_flt(global.parameters, "rules.guard.base_stop_prob", .3f); - double skill_prob = get_param_flt(global.parameters, "rules.guard.skill_stop_prob", .1f); - double amulet_prob = get_param_flt(global.parameters, "rules.guard.amulet_stop_prob", .1f); - double guard_number_prob = get_param_flt(global.parameters, "rules.guard.guard_number_stop_prob", .001f); - double castle_prob = get_param_flt(global.parameters, "rules.guard.castle_stop_prob", .1f); - double region_type_prob = get_param_flt(global.parameters, "rules.guard.region_type_stop_prob", .1f); + double base_prob = config_get_flt("rules.guard.base_stop_prob", .3); + double skill_prob = config_get_flt("rules.guard.skill_stop_prob", .1); + double amulet_prob = config_get_flt("rules.guard.amulet_stop_prob", .1); + double guard_number_prob = config_get_flt("rules.guard.guard_number_stop_prob", .001); + double castle_prob = config_get_flt("rules.guard.castle_stop_prob", .1); + double region_type_prob = config_get_flt("rules.guard.region_type_stop_prob", .1); if (fval(u_race(reisender), RCF_ILLUSIONARY)) return NULL; @@ -1785,7 +1785,7 @@ sail(unit * u, order * ord, bool move_on_land, region_list ** routep) if (!flying_ship(sh)) { int stormchance = 0; int reason; - bool storms_enabled = get_param_int(global.parameters, "rules.ship.storms", 1) != 0; + bool storms_enabled = config_get_int("rules.ship.storms", 1) != 0; if (storms_enabled) { int stormyness; gamedate date; @@ -1795,7 +1795,7 @@ sail(unit * u, order * ord, bool move_on_land, region_list ** routep) /* storms should be the first thing we do. */ stormchance = stormyness / shipspeed(sh, u); if (check_leuchtturm(next_point, NULL)) { - int param = get_param_int(global.parameters, "rules.lighthous.stormchancedevisor", 0); + int param = config_get_int("rules.lighthous.stormchancedevisor", 0); if (param > 0) { stormchance /= param; } @@ -1885,9 +1885,7 @@ sail(unit * u, order * ord, bool move_on_land, region_list ** routep) ADDMSG(&f->msgs, msg_message("sailnolandingstorm", "ship region", sh, next_point)); } else { - double dmg = - get_param_flt(global.parameters, "rules.ship.damage.nolanding", - 0.10F); + double dmg = config_get_flt("rules.ship.damage.nolanding", 0.1); ADDMSG(&f->msgs, msg_message("sailnolanding", "ship region", sh, next_point)); damage_ship(sh, dmg); diff --git a/src/randenc.c b/src/randenc.c index 55802ff2f..80ff498aa 100644 --- a/src/randenc.c +++ b/src/randenc.c @@ -738,9 +738,7 @@ static void move_iceberg(region * r) for (sh = r->ships; sh; sh = sh->next) { /* Meldung an Kapitän */ - double dmg = - get_param_flt(global.parameters, "rules.ship.damage.intoiceberg", - 0.10F); + double dmg = config_get_flt("rules.ship.damage.intoiceberg", 0.1); damage_ship(sh, dmg); fset(sh, SF_SELECT); } @@ -751,9 +749,7 @@ static void move_iceberg(region * r) translist(&rc->buildings, &r->buildings, rc->buildings); } while (rc->ships) { - double dmg = - get_param_flt(global.parameters, "rules.ship.damage.withiceberg", - 0.10F); + double dmg = config_get_flt("rules.ship.damage.withiceberg", 0.1); fset(rc->ships, SF_SELECT); damage_ship(rc->ships, dmg); move_ship(rc->ships, rc, r, NULL); @@ -885,9 +881,7 @@ static void godcurse(void) ship *sh; for (sh = r->ships; sh;) { ship *shn = sh->next; - double dmg = - get_param_flt(global.parameters, "rules.ship.damage.godcurse", - 0.10F); + double dmg = config_get_flt("rules.ship.damage.godcurse", 0.1); damage_ship(sh, dmg); if (sh->damage >= sh->size * DAMAGE_SCALE) { unit *u = ship_owner(sh); @@ -970,8 +964,7 @@ static void demon_skillchanges(void) /* hungry demons only go down, never up in skill */ static int rule_hunger = -1; if (rule_hunger < 0) { - rule_hunger = - get_param_int(global.parameters, "hunger.demon.skill", 0); + rule_hunger = config_get_int("hunger.demon.skill", 0); } if (rule_hunger) { upchance = 0; @@ -1023,8 +1016,7 @@ static void rotting_herbs(void) region *r; if (rule_rot < 0) { - rule_rot = - get_param_int(global.parameters, "rules.economy.herbrot", HERBROTCHANCE); + rule_rot = config_get_int("rules.economy.herbrot", HERBROTCHANCE); } if (rule_rot == 0) return; diff --git a/src/reports.c b/src/reports.c index d88769457..44e79324e 100644 --- a/src/reports.c +++ b/src/reports.c @@ -1678,7 +1678,7 @@ int reports(void) free_seen(); #ifdef GLOBAL_REPORT { - const char *str = get_param(global.parameters, "globalreport"); + const char *str = config_get("globalreport"); if (str != NULL) { sprintf(path, "%s/%s.%u.cr", reportpath(), str, turn); global_report(path); diff --git a/src/study.c b/src/study.c index 32b9b5029..79adf2847 100644 --- a/src/study.c +++ b/src/study.c @@ -121,7 +121,7 @@ int study_cost(unit * u, skill_t sk) if (cost[sk] == 0) { char buffer[256]; sprintf(buffer, "skills.cost.%s", skillnames[sk]); - cost[sk] = get_param_int(global.parameters, buffer, -1); + cost[sk] = config_get_int(buffer, -1); } if (cost[sk] >= 0) { return cost[sk]; @@ -545,13 +545,13 @@ int study_cmd(unit * u, order * ord) int money = 0; skill_t sk; int maxalchemy = 0; - int speed_rule = (study_rule_t)get_param_int(global.parameters, "study.speedup", 0); + int speed_rule = (study_rule_t)config_get_int("study.speedup", 0); static int learn_newskills = -1; struct building *b = inside_building(u); const struct building_type *btype = building_is_active(b) ? b->type : NULL; if (learn_newskills < 0) { - const char *str = get_param(global.parameters, "study.newskills"); + const char *str = config_get("study.newskills"); if (str && strcmp(str, "false") == 0) learn_newskills = 0; else diff --git a/src/upkeep.c b/src/upkeep.c index 3845c5e68..d83abbe86 100644 --- a/src/upkeep.c +++ b/src/upkeep.c @@ -67,7 +67,7 @@ static bool hunger(int number, unit * u) static const race *rc = 0; if (!damage) { - damage = get_param(global.parameters, "hunger.damage"); + damage = config_get("hunger.damage"); if (damage == NULL) damage = "1d12+12"; } @@ -112,7 +112,7 @@ void get_food(region * r) plane *pl = rplane(r); unit *u; int peasantfood = rpeasants(r) * 10; - int food_rules = get_param_int(global.parameters, "rules.food.flags", 0); + int food_rules = config_get_int("rules.food.flags", 0); if (food_rules & FOOD_IS_FREE) { return; @@ -272,7 +272,7 @@ void get_food(region * r) if (hungry > 0) { static int demon_hunger = -1; if (demon_hunger < 0) { - demon_hunger = get_param_int(global.parameters, "hunger.demons", 0); + demon_hunger = config_get_int("hunger.demons", 0); } if (demon_hunger == 0) { /* demons who don't feed are hungry */ From edcd79d044f41b1a370b2b7754b0d889ca7fc88e Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 22 Nov 2015 12:20:33 +0100 Subject: [PATCH 13/28] add a config_token function to look for a token inside a list --- src/economy.c | 2 +- src/kernel/building.c | 2 +- src/kernel/config.c | 4 ++++ src/kernel/config.h | 1 + src/reports.c | 2 +- 5 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/economy.c b/src/economy.c index 64a2a6771..26eb5251b 100644 --- a/src/economy.c +++ b/src/economy.c @@ -731,7 +731,7 @@ static bool maintain(building * b, bool first) return false; } /* If the owner is the region owner, check if dontpay flag is set for the building where he is in */ - if (check_param(global.parameters, "rules.region_owner_pay_building", b->type->_name)) { + if (config_token("rules.region_owner_pay_building", b->type->_name)) { if (fval(u->building, BLD_DONTPAY)) { return false; } diff --git a/src/kernel/building.c b/src/kernel/building.c index 4867b3687..715b4de0f 100644 --- a/src/kernel/building.c +++ b/src/kernel/building.c @@ -591,7 +591,7 @@ static unit *building_owner_ex(const building * bld, const struct faction * last } } } - if (!heir && check_param(global.parameters, "rules.region_owner_pay_building", bld->type->_name)) { + if (!heir && config_token("rules.region_owner_pay_building", bld->type->_name)) { if (rule_region_owners()) { u = building_owner(largestbuilding(bld->region, &cmp_taxes, false)); } diff --git a/src/kernel/config.c b/src/kernel/config.c index 1ba1f6c7f..956c622fe 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -1587,6 +1587,10 @@ double config_get_flt(const char *key, double def) { return get_param_flt(global.parameters, key, def); } +bool config_token(const char *key, const char *tok) { + return !!check_param(global.parameters, key, tok); +} + /** releases all memory associated with the game state. * call this function before calling read_game() to load a new game * if you have a previously loaded state in memory. diff --git a/src/kernel/config.h b/src/kernel/config.h index 817850166..fc6ce99f3 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -279,6 +279,7 @@ extern "C" { const char *config_get(const char *key); 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 ExpensiveMigrants(void); int NMRTimeout(void); diff --git a/src/reports.c b/src/reports.c index 44e79324e..29b459f20 100644 --- a/src/reports.c +++ b/src/reports.c @@ -1397,7 +1397,7 @@ static void prepare_reports(void) } /* Region owner get always the Lighthouse report */ - if (bt_lighthouse && check_param(global.parameters, "rules.region_owner_pay_building", bt_lighthouse->_name)) { + if (bt_lighthouse && config_token("rules.region_owner_pay_building", bt_lighthouse->_name)) { for (b = rbuildings(r); b; b = b->next) { if (b && b->type == bt_lighthouse) { u = building_owner(b); From fe173e9551cb6995c00d786d171ce3c8b2d5e935 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 22 Nov 2015 12:24:27 +0100 Subject: [PATCH 14/28] refactor: make sure global.paramteres is used only inside config.c --- src/kernel/config.c | 16 +++++++++++----- src/kernel/config.h | 3 ++- src/tests.c | 4 +--- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/kernel/config.c b/src/kernel/config.c index 956c622fe..e039ac4c9 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -1572,23 +1572,29 @@ bool markets_module(void) } void config_set(const char *key, const char *value) { - set_param(&global.parameters, key, value); + set_param(&global.parameters_, key, value); } const char *config_get(const char *key) { - return get_param(global.parameters, key); + return get_param(global.parameters_, key); } int config_get_int(const char *key, int def) { - return get_param_int(global.parameters, key, def); + return get_param_int(global.parameters_, key, def); } double config_get_flt(const char *key, double def) { - return get_param_flt(global.parameters, key, def); + return get_param_flt(global.parameters_, key, def); } bool config_token(const char *key, const char *tok) { - return !!check_param(global.parameters, key, tok); + return !!check_param(global.parameters_, key, tok); +} + +void free_config(void) { + global.functions.maintenance = NULL; + global.functions.wage = NULL; + free_params(&global.parameters_); } /** releases all memory associated with the game state. diff --git a/src/kernel/config.h b/src/kernel/config.h index fc6ce99f3..f907e691c 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -246,7 +246,7 @@ extern "C" { const char *gamename; struct attrib *attribs; unsigned int data_turn; - struct param *parameters; + struct param *parameters_; void *vm_state; int data_version; /* TODO: eliminate in favor of gamedata.version */ struct _dictionary_ *inifile; @@ -297,6 +297,7 @@ extern "C" { void init_parameters(struct locale *lang); void free_gamedata(void); + void free_config(void); extern struct helpmode helpmodes[]; extern const char *parameters[]; diff --git a/src/tests.c b/src/tests.c index e7518211e..d553255de 100644 --- a/src/tests.c +++ b/src/tests.c @@ -78,9 +78,7 @@ void test_cleanup(void) free_terrains(); free_resources(); - global.functions.maintenance = NULL; - global.functions.wage = NULL; - free_params(&global.parameters); + free_config(); default_locale = 0; close_orders(); free_locales(); From 74b1f9872e0978083c87a7ce51b9c6e12b13616a Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 22 Nov 2015 12:27:08 +0100 Subject: [PATCH 15/28] change visibility of configuration data structures, local to config.c only --- src/kernel/config.c | 14 ++++++++------ src/kernel/config.h | 1 - 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/kernel/config.c b/src/kernel/config.c index e039ac4c9..0137bcf60 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -1571,30 +1571,32 @@ bool markets_module(void) return config_get_int("modules.markets", 0); } +static struct param *configuration; + void config_set(const char *key, const char *value) { - set_param(&global.parameters_, key, value); + set_param(&configuration, key, value); } const char *config_get(const char *key) { - return get_param(global.parameters_, key); + return get_param(configuration, key); } int config_get_int(const char *key, int def) { - return get_param_int(global.parameters_, key, def); + return get_param_int(configuration, key, def); } double config_get_flt(const char *key, double def) { - return get_param_flt(global.parameters_, key, def); + return get_param_flt(configuration, key, def); } bool config_token(const char *key, const char *tok) { - return !!check_param(global.parameters_, key, tok); + return !!check_param(configuration, key, tok); } void free_config(void) { global.functions.maintenance = NULL; global.functions.wage = NULL; - free_params(&global.parameters_); + free_params(&configuration); } /** releases all memory associated with the game state. diff --git a/src/kernel/config.h b/src/kernel/config.h index f907e691c..b405ed585 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -246,7 +246,6 @@ extern "C" { const char *gamename; struct attrib *attribs; unsigned int data_turn; - struct param *parameters_; void *vm_state; int data_version; /* TODO: eliminate in favor of gamedata.version */ struct _dictionary_ *inifile; From 0e6d122b92ff9e2725394b5501fb9d5a0935a35f Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 22 Nov 2015 12:30:34 +0100 Subject: [PATCH 16/28] fix compile --- src/kernel/config.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/kernel/config.h b/src/kernel/config.h index b405ed585..656b2e012 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -25,7 +25,7 @@ extern "C" { /* this should always be the first thing included after platform.h */ #include "types.h" - +struct param; /* experimental gameplay features (that don't affect the savefile) */ /* TODO: move these settings to settings.h or into configuration files */ #define GOBLINKILL /* Goblin-Spezialklau kann tödlich enden */ From cf6084a5ca39904d769c6747423ac8ceefdcbde6 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 22 Nov 2015 12:37:25 +0100 Subject: [PATCH 17/28] refactoring: move game configuration defines out of config.h temporarily pushing them into settings.h where others already are, though they should be in their own modules or in config files. --- src/kernel/config.h | 27 +-------------------------- src/settings.h | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/src/kernel/config.h b/src/kernel/config.h index b405ed585..a4604e318 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -25,13 +25,7 @@ extern "C" { /* this should always be the first thing included after platform.h */ #include "types.h" - - /* experimental gameplay features (that don't affect the savefile) */ - /* TODO: move these settings to settings.h or into configuration files */ -#define GOBLINKILL /* Goblin-Spezialklau kann tödlich enden */ -#define HERBS_ROT /* herbs owned by units have a chance to rot. */ -#define INSECT_POTION /* Spezialtrank für Insekten */ -#define ORCIFICATION /* giving snotlings to the peasants gets counted */ +struct param; /* for some good prime numbers, check http://www.math.niu.edu/~rusin/known-math/98/pi_x */ #ifndef MAXREGIONS @@ -42,18 +36,6 @@ extern "C" { #endif #define MAXLUXURIES 16 /* there must be no more than MAXLUXURIES kinds of luxury goods in any game */ -#define TREESIZE (8) /* space used by trees (in #peasants) */ - -#define PEASANTFORCE 0.75 /* Chance einer Vermehrung trotz 90% Auslastung */ -#define HERBROTCHANCE 5 /* Verrottchance für Kräuter (ifdef HERBS_ROT) */ - - /* Gebäudegröße = Minimalbelagerer */ -#define SIEGEFACTOR 2 - - /** Magic */ -#define MAXMAGICIANS 3 -#define MAXALCHEMISTS 3 - /* getunit results: */ #define GET_UNIT 0 #define GET_NOTFOUND 1 @@ -67,16 +49,11 @@ extern "C" { #define BP_NORMAL 3 #define BP_ROAD 2 -#define PERSON_WEIGHT 1000 /* weight of a "normal" human unit */ -#define STAMINA_AFFECTS_HP 1<<0 - /** * Hier endet der Teil von config.h, der die defines für die * Spielwelt Eressea enthält, und beginnen die allgemeinen Routinen */ -#define ENCCHANCE 10 /* %-Chance für einmalige Zufallsbegegnung */ - #define DISPLAYSIZE 8192 /* max. Länge einer Beschreibung, incl trailing 0 */ #define ORDERSIZE (DISPLAYSIZE*2) /* max. length of an order */ #define NAMESIZE 128 /* max. Länge eines Namens, incl trailing 0 */ @@ -84,8 +61,6 @@ extern "C" { #define OBJECTIDSIZE (NAMESIZE+5+IDSIZE) /* max. Länge der Strings, die * von struct unitname, etc. zurückgegeben werden. ohne die 0 */ -#define BAGCAPACITY 20000 /* soviel paßt in einen Bag of Holding */ - /* ----------------- Befehle ----------------------------------- */ #define want(option) (1< Date: Sun, 22 Nov 2015 12:41:22 +0100 Subject: [PATCH 18/28] cleanup: remove movement constatns from config.h --- src/kernel/config.h | 8 -------- src/move.c | 8 ++++++++ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/kernel/config.h b/src/kernel/config.h index a4604e318..4550ae687 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -40,14 +40,6 @@ struct param; #define GET_UNIT 0 #define GET_NOTFOUND 1 #define GET_PEASANTS 2 - /* Bewegungsweiten: */ -#define BP_WALKING 4 -#define BP_RIDING 6 -#define BP_UNICORN 9 -#define BP_DRAGON 4 - -#define BP_NORMAL 3 -#define BP_ROAD 2 /** * Hier endet der Teil von config.h, der die defines für die diff --git a/src/move.c b/src/move.c index 517301828..3a3d91d83 100644 --- a/src/move.c +++ b/src/move.c @@ -81,6 +81,14 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include +/* Bewegungsweiten: */ +#define BP_WALKING 4 +#define BP_RIDING 6 +#define BP_UNICORN 9 +#define BP_DRAGON 4 +#define BP_NORMAL 3 +#define BP_ROAD 2 + int *storms; typedef struct traveldir { From 73fc5a51b30a4d6f1edc4b88dca0ac2b80369ac9 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 22 Nov 2015 12:47:57 +0100 Subject: [PATCH 19/28] remove more junk defines from config.h, move them closer to where they belong --- src/kernel/config.c | 5 ----- src/kernel/config.h | 17 ----------------- src/kernel/faction.c | 5 +++++ src/kernel/faction.h | 1 + src/kernel/region.h | 3 +++ src/kernel/unit.h | 1 + 6 files changed, 10 insertions(+), 22 deletions(-) diff --git a/src/kernel/config.c b/src/kernel/config.c index 0137bcf60..9aa30958e 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -1152,11 +1152,6 @@ char *_strdup(const char *s) } #endif -bool faction_id_is_unused(int id) -{ - return findfaction(id) == NULL; -} - int besieged(const unit * u) { /* belagert kann man in schiffen und burgen werden */ diff --git a/src/kernel/config.h b/src/kernel/config.h index 4550ae687..2308409d6 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -27,25 +27,11 @@ extern "C" { #include "types.h" struct param; - /* for some good prime numbers, check http://www.math.niu.edu/~rusin/known-math/98/pi_x */ -#ifndef MAXREGIONS -# define MAXREGIONS 524287 /* must be prime for hashing. 262139 was a little small */ -#endif -#ifndef MAXUNITS -# define MAXUNITS 1048573 /* must be prime for hashing. 524287 was >90% full */ -#endif -#define MAXLUXURIES 16 /* there must be no more than MAXLUXURIES kinds of luxury goods in any game */ - /* getunit results: */ #define GET_UNIT 0 #define GET_NOTFOUND 1 #define GET_PEASANTS 2 - /** - * Hier endet der Teil von config.h, der die defines für die - * Spielwelt Eressea enthält, und beginnen die allgemeinen Routinen - */ - #define DISPLAYSIZE 8192 /* max. Länge einer Beschreibung, incl trailing 0 */ #define ORDERSIZE (DISPLAYSIZE*2) /* max. length of an order */ #define NAMESIZE 128 /* max. Länge eines Namens, incl trailing 0 */ @@ -64,9 +50,6 @@ struct param; #define fset(u, i) ((u)->flags |= (i)) #define freset(u, i) ((u)->flags &= ~(i)) - /* parteinummern */ - bool faction_id_is_unused(int); - int max_magicians(const struct faction * f); int findoption(const char *s, const struct locale *lang); diff --git a/src/kernel/faction.c b/src/kernel/faction.c index 71286cffe..28d50ff76 100755 --- a/src/kernel/faction.c +++ b/src/kernel/faction.c @@ -208,6 +208,11 @@ int resolve_faction(variant id, void *address) return result; } +bool faction_id_is_unused(int id) +{ + return findfaction(id) == NULL; +} + #define MAX_FACTION_ID (36*36*36*36) static int unused_faction_id(void) diff --git a/src/kernel/faction.h b/src/kernel/faction.h index 2dbce4c86..16b26c5f9 100644 --- a/src/kernel/faction.h +++ b/src/kernel/faction.h @@ -160,6 +160,7 @@ extern "C" { /* skills */ int skill_limit(struct faction *f, skill_t sk); int count_skill(struct faction *f, skill_t sk); + bool faction_id_is_unused(int); #ifdef __cplusplus } diff --git a/src/kernel/region.h b/src/kernel/region.h index b0672e758..e955a7439 100644 --- a/src/kernel/region.h +++ b/src/kernel/region.h @@ -26,6 +26,9 @@ extern "C" { #include "types.h" #include "direction.h" +#define MAXLUXURIES 16 /* there must be no more than MAXLUXURIES kinds of luxury goods in any game */ +#define MAXREGIONS 524287 /* must be prime for hashing. 262139 was a little small */ + /* FAST_CONNECT: regions are directly connected to neighbours, saves doing a hash-access each time a neighbour is needed, 6 extra pointers per hex */ #define FAST_CONNECT diff --git a/src/kernel/unit.h b/src/kernel/unit.h index 89fb90d9b..7f8cca095 100644 --- a/src/kernel/unit.h +++ b/src/kernel/unit.h @@ -26,6 +26,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. extern "C" { #endif +#define MAXUNITS 1048573 /* must be prime for hashing. 524287 was >90% full */ struct skill; struct item; struct sc_mage; From 35e7a0bc7918d2a26724d9fecdb1cbbd4ea6ce37 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 22 Nov 2015 12:50:10 +0100 Subject: [PATCH 20/28] eliminate global.cookie and the cache-invalidation which it enabled --- src/kernel/config.c | 6 +----- src/kernel/config.h | 3 --- src/kernel/save.c | 1 - 3 files changed, 1 insertion(+), 9 deletions(-) diff --git a/src/kernel/config.c b/src/kernel/config.c index 9aa30958e..e343b9628 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -1006,15 +1006,12 @@ void set_param(struct param **p, const char *key, const char *value) const char * kv = (const char *)match; size_t vlen = strlen(kv + klen) + 1; cb_erase(&par->cb, kv, klen + vlen); - ++global.cookie; } } if (value) { char data[512]; size_t sz = pack_keyval(key, value, data, sizeof(data)); - if (cb_insert(&par->cb, data, sz) == CB_SUCCESS) { - ++global.cookie; - } + cb_insert(&par->cb, data, sz); } } @@ -1631,7 +1628,6 @@ void free_gamedata(void) while (global.attribs) { a_remove(&global.attribs, global.attribs); } - ++global.cookie; /* readgame() already does this, but sjust in case */ } const char * game_name(void) { diff --git a/src/kernel/config.h b/src/kernel/config.h index 2308409d6..b39807a33 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -205,9 +205,6 @@ struct param; 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; } settings; typedef struct helpmode { diff --git a/src/kernel/save.c b/src/kernel/save.c index fd32a567d..f065743f4 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -1449,7 +1449,6 @@ int readgame(const char *filename, bool backup) global.data_turn = turn; log_debug(" - reading turn %d\n", turn); rng_init(turn); - ++global.cookie; READ_INT(&store, &nread); /* max_unique_id = ignore */ READ_INT(&store, &nextborder); From c6bac1e49e018779f86bfeb145288a4770343d19 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 22 Nov 2015 14:28:15 +0100 Subject: [PATCH 21/28] refactoring: sprout methods for all special-action potions remove static caches for WOL configuration --- src/alchemy.c | 109 +++++++++++++++++++++++++++++++------------------- 1 file changed, 67 insertions(+), 42 deletions(-) diff --git a/src/alchemy.c b/src/alchemy.c index 166cde00b..8b8220136 100644 --- a/src/alchemy.c +++ b/src/alchemy.c @@ -122,61 +122,86 @@ static void end_potion(unit * u, const potion_type * ptype, int amount) "unit potion", u, ptype->itype->rtype)); } +static int potion_water_of_life(unit * u, int amount) { + region *r = u->region; + 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); + /* mallorn is required to make mallorn forests, wood for regular ones */ + if (fval(r, RF_MALLORN)) { + wood = use_pooled(u, rt_find("mallorn"), + GET_SLACK | GET_RESERVE | GET_POOLED_SLACK, tree_count * amount); + } + else { + wood = use_pooled(u, rt_find("log"), + GET_SLACK | GET_RESERVE | GET_POOLED_SLACK, tree_count * amount); + } + if (r->land == 0) + wood = 0; + if (wood < tree_count * amount) { + int x = wood / tree_count; + if (wood % tree_count) + ++x; + if (x < amount) + amount = x; + } + rsettrees(r, tree_type, rtrees(r, tree_type) + wood); + ADDMSG(&u->faction->msgs, msg_message("growtree_effect", + "mage amount", u, wood)); + return amount; +} + +static int potion_healing(unit * u, int amount) { + u->hp = _min(unit_max_hp(u) * u->number, u->hp + 400 * amount); + return amount; +} + +static int potion_luck(unit *u, attrib_type *atype, int amount) { + region *r = u->region; + attrib *a = (attrib *)a_find(r->attribs, atype); + if (!a) { + a = a_add(&r->attribs, a_new(atype)); + } + a->data.i += amount; + return amount; +} + +static int potion_truth(unit *u) { + fset(u, UFL_DISBELIEVES); + return 1; +} + +static int potion_power(unit *u, int amount) { + int use = u->number / 10; + if (use < amount) { + if (u->number % 10 > 0) ++use; + amount = use; + } + /* Verfünffacht die HP von max. 10 Personen in der Einheit */ + u->hp += _min(u->number, 10 * amount) * unit_max_hp(u) * 4; + return amount; +} + static int do_potion(unit * u, region *r, const potion_type * ptype, int amount) { + assert(r == u->region); // TODO: is r an unnecessary argument? if (ptype == oldpotiontype[P_LIFE]) { - int holz = 0; - static int tree_type = -1; - static int tree_count = -1; - if (tree_type < 0) { - 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)) { - holz = use_pooled(u, rt_find("mallorn"), - GET_SLACK | GET_RESERVE | GET_POOLED_SLACK, tree_count * amount); - } - else { - holz = use_pooled(u, rt_find("log"), - GET_SLACK | GET_RESERVE | GET_POOLED_SLACK, tree_count * amount); - } - if (r->land == 0) - holz = 0; - if (holz < tree_count * amount) { - int x = holz / tree_count; - if (holz % tree_count) - ++x; - if (x < amount) - amount = x; - } - rsettrees(r, tree_type, rtrees(r, tree_type) + holz); - ADDMSG(&u->faction->msgs, msg_message("growtree_effect", - "mage amount", u, holz)); + return potion_water_of_life(u, amount); } else if (ptype == oldpotiontype[P_HEILWASSER]) { - u->hp = _min(unit_max_hp(u) * u->number, u->hp + 400 * amount); + return potion_healing(u, amount); } else if (ptype == oldpotiontype[P_PEOPLE]) { - attrib *a = (attrib *)a_find(r->attribs, &at_peasantluck); - if (!a) - a = a_add(&r->attribs, a_new(&at_peasantluck)); - a->data.i += amount; + return potion_luck(u, &at_peasantluck, amount); } else if (ptype == oldpotiontype[P_HORSE]) { - attrib *a = (attrib *)a_find(r->attribs, &at_horseluck); - if (!a) - a = a_add(&r->attribs, a_new(&at_horseluck)); - a->data.i += amount; + return potion_luck(u, &at_horseluck, amount); } else if (ptype == oldpotiontype[P_WAHRHEIT]) { - fset(u, UFL_DISBELIEVES); - amount = 1; + return potion_truth(u); } else if (ptype == oldpotiontype[P_MACHT]) { - /* Verfünffacht die HP von max. 10 Personen in der Einheit */ - u->hp += _min(u->number, 10 * amount) * unit_max_hp(u) * 4; + return potion_power(u, amount); } else { change_effect(u, ptype, 10 * amount); From 17068af3324372b1b043f760693bd4ae2e4a3290 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 22 Nov 2015 14:36:05 +0100 Subject: [PATCH 22/28] unusual potion delay attribute can use a potion in another region? unclear what this code does, but fixing my bad assumption. --- src/alchemy.c | 13 +++++-------- src/kernel/save.test.c | 1 + 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/alchemy.c b/src/alchemy.c index 8b8220136..406d39fd6 100644 --- a/src/alchemy.c +++ b/src/alchemy.c @@ -122,8 +122,7 @@ static void end_potion(unit * u, const potion_type * ptype, int amount) "unit potion", u, ptype->itype->rtype)); } -static int potion_water_of_life(unit * u, int amount) { - region *r = u->region; +static int potion_water_of_life(unit * u, region *r, int amount) { 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); @@ -156,8 +155,7 @@ static int potion_healing(unit * u, int amount) { return amount; } -static int potion_luck(unit *u, attrib_type *atype, int amount) { - region *r = u->region; +static int potion_luck(unit *u, region *r, attrib_type *atype, int amount) { attrib *a = (attrib *)a_find(r->attribs, atype); if (!a) { a = a_add(&r->attribs, a_new(atype)); @@ -184,18 +182,17 @@ static int potion_power(unit *u, int amount) { static int do_potion(unit * u, region *r, const potion_type * ptype, int amount) { - assert(r == u->region); // TODO: is r an unnecessary argument? if (ptype == oldpotiontype[P_LIFE]) { - return potion_water_of_life(u, amount); + return potion_water_of_life(u, r, amount); } else if (ptype == oldpotiontype[P_HEILWASSER]) { return potion_healing(u, amount); } else if (ptype == oldpotiontype[P_PEOPLE]) { - return potion_luck(u, &at_peasantluck, amount); + return potion_luck(u, r, &at_peasantluck, amount); } else if (ptype == oldpotiontype[P_HORSE]) { - return potion_luck(u, &at_horseluck, amount); + return potion_luck(u, r, &at_horseluck, amount); } else if (ptype == oldpotiontype[P_WAHRHEIT]) { return potion_truth(u); diff --git a/src/kernel/save.test.c b/src/kernel/save.test.c index 403eaa059..35e07325b 100644 --- a/src/kernel/save.test.c +++ b/src/kernel/save.test.c @@ -42,6 +42,7 @@ static void test_readwrite_unit(CuTest * tc) sprintf(path, "%s/%s", datapath(), filename); data = gamedata_open(path, "wb"); + CuAssertPtrNotNull(tc, data); // TODO: intermittent test write_unit(data, u); gamedata_close(data); From 7bbf11c6b6e86eb15dbc0365324a4d4ed8fce98d Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 22 Nov 2015 15:14:50 +0100 Subject: [PATCH 23/28] eliminate more static cached configuration values --- src/alchemy.c | 8 +++----- src/battle.c | 47 ++++++++--------------------------------------- src/helpers.c | 39 +++++++++++++++++---------------------- 3 files changed, 28 insertions(+), 66 deletions(-) diff --git a/src/alchemy.c b/src/alchemy.c index 406d39fd6..ace6a62dd 100644 --- a/src/alchemy.c +++ b/src/alchemy.c @@ -93,13 +93,11 @@ void herbsearch(unit * u, int max) static int begin_potion(unit * u, const potion_type * ptype, struct order *ord) { - static int rule_multipotion = -1; + bool rule_multipotion; assert(ptype != NULL); - if (rule_multipotion < 0) { - /* should we allow multiple different potions to be used the same turn? */ - rule_multipotion = config_get_int("rules.magic.multipotion", 0); - } + /* should we allow multiple different potions to be used the same turn? */ + rule_multipotion = config_get_int("rules.magic.multipotion", 0) != 0; if (!rule_multipotion) { const potion_type *use = ugetpotionuse(u); if (use != NULL && use != ptype) { diff --git a/src/battle.c b/src/battle.c index 80c3c0583..a1e2572dc 100644 --- a/src/battle.c +++ b/src/battle.c @@ -664,23 +664,14 @@ weapon_skill(const weapon_type * wtype, const unit * u, bool attacking) static int CavalrySkill(void) { - static int skill = -1; - - if (skill < 0) { - skill = config_get_int("rules.cavalry.skill", 2); - } - return skill; + return config_get_int("rules.cavalry.skill", 2); } #define BONUS_SKILL 1 #define BONUS_DAMAGE 2 static int CavalryBonus(const unit * u, troop enemy, int type) { - static int mode = -1; - - if (mode < 0) { - mode = config_get_int("rules.cavalry.mode", 1); - } + int mode = config_get_int("rules.cavalry.mode", 1); if (mode == 0) { /* old rule, Eressea 1.0 compat */ return (type == BONUS_SKILL) ? 2 : 0; @@ -1006,9 +997,7 @@ const char *rel_dam(int dam, int hp) static void vampirism(troop at, int damage) { - static int vampire = -1; - if (vampire < 0) - vampire = config_get_int("rules.combat.demon_vampire", 0); + int vampire = config_get_int("rules.combat.demon_vampire", 0); if (vampire > 0) { int gain = damage / vampire; int chance = damage - vampire * gain; @@ -1079,7 +1068,6 @@ static int rc_specialdamage(const unit *au, const unit *du, const struct weapon_ } int calculate_armor(troop dt, const weapon_type *dwtype, const weapon_type *awtype, double *magres) { - static int rule_armor = -1; fighter *df = dt.fighter; unit *du = df->unit; int ar = 0, an, am; @@ -1107,10 +1095,7 @@ int calculate_armor(troop dt, const weapon_type *dwtype, const weapon_type *awty /* Momentan nur Trollgürtel und Werwolf-Eigenschaft */ am = select_magicarmor(dt); - if (rule_armor < 0) { - rule_armor = config_get_int("rules.combat.nat_armor", 0); - } - if (rule_armor == 0) { + if (config_get_int("rules.combat.nat_armor", 0) == 0) { /* natürliche Rüstung ist halbkumulativ */ if (ar > 0) { ar += an / 2; @@ -1939,10 +1924,7 @@ int skilldiff(troop at, troop dt, int dist) } if (u_race(au) == get_race(RC_GOBLIN)) { - static int goblin_bonus = -1; - if (goblin_bonus < 0) - goblin_bonus = - config_get_int("rules.combat.goblinbonus", 10); + int goblin_bonus = config_get_int("rules.combat.goblinbonus", 10); if (af->side->size[SUM_ROW] >= df->side->size[SUM_ROW] * goblin_bonus) { skdiff += 1; } @@ -2140,10 +2122,7 @@ static int attacks_per_round(troop t) static void make_heroes(battle * b) { side *s; - static int hero_speed = 0; - if (hero_speed == 0) { - hero_speed = config_get_int("rules.combat.herospeed", 10); - } + int hero_speed = config_get_int("rules.combat.herospeed", 10); for (s = b->sides; s != b->sides + b->nsides; ++s) { fighter *fig; for (fig = s->fighters; fig; fig = fig->next) { @@ -3213,16 +3192,12 @@ side * get_side(battle * b, const struct unit * u) side * find_side(battle * b, const faction * f, const group * g, unsigned int flags, const faction * stealthfaction) { side * s; - static int rule_anon_battle = -1; - - if (rule_anon_battle < 0) { - rule_anon_battle = config_get_int("rules.stealth.anon_battle", 1); - } + bool rule_anon_battle = config_get_int("rules.stealth.anon_battle", 1) != 0; for (s = b->sides; s != b->sides + b->nsides; ++s) { if (s->faction == f && s->group == g) { unsigned int s1flags = flags | SIDE_HASGUARDS; unsigned int s2flags = s->flags | SIDE_HASGUARDS; - if (rule_anon_battle!=0 && s->stealthfaction != stealthfaction) { + if (rule_anon_battle && s->stealthfaction != stealthfaction) { continue; } if (s1flags == s2flags) { @@ -3611,7 +3586,6 @@ battle *make_battle(region * r) unit *u; bfaction *bf; building * bld; - static int max_fac_no = 0; /* need this only once */ /* Alle Mann raus aus der Burg! */ for (bld = r->buildings; bld != NULL; bld = bld->next) @@ -3663,7 +3637,6 @@ battle *make_battle(region * r) for (bf = b->factions; bf; bf = bf->next) { faction *f = bf->faction; - max_fac_no = _max(max_fac_no, f->no); freset(f, FFL_MARK); } return b; @@ -3709,17 +3682,13 @@ static void battle_free(battle * b) { void free_battle(battle * b) { - int max_fac_no = 0; - if (bdebug) { fclose(bdebug); } while (b->factions) { bfaction *bf = b->factions; - faction *f = bf->faction; b->factions = bf->next; - max_fac_no = _max(max_fac_no, f->no); free(bf); } diff --git a/src/helpers.c b/src/helpers.c index a81a91679..21483fa50 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -320,33 +320,28 @@ static int lua_getresource(unit * u, const struct resource_type *rtype) static bool lua_canuse_item(const unit * u, const struct item_type *itype) { - static int function_exists = 1; bool result = true; + lua_State *L = (lua_State *)global.vm_state; + const char *fname = "item_canuse"; - if (function_exists) { - lua_State *L = (lua_State *)global.vm_state; - const char *fname = "item_canuse"; + lua_getglobal(L, fname); + if (lua_isfunction(L, -1)) { + tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit"); + tolua_pushstring(L, itype->rtype->_name); - lua_getglobal(L, fname); - if (lua_isfunction(L, -1)) { - tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit"); - tolua_pushstring(L, itype->rtype->_name); - - if (lua_pcall(L, 2, 1, 0) != 0) { - const char *error = lua_tostring(L, -1); - log_error("get(%s) calling '%s': %s.\n", unitname(u), fname, error); - lua_pop(L, 1); - } - else { - result = lua_toboolean(L, -1); - lua_pop(L, 1); - } - } - else { - function_exists = 0; - log_error("get(%s) calling '%s': not a function.\n", unitname(u), fname); + if (lua_pcall(L, 2, 1, 0) != 0) { + const char *error = lua_tostring(L, -1); + log_error("use(%s) calling '%s': %s.\n", unitname(u), fname, error); lua_pop(L, 1); } + else { + result = lua_toboolean(L, -1); + lua_pop(L, 1); + } + } + else { + log_error("use(%s) calling '%s': not a function.\n", unitname(u), fname); + lua_pop(L, 1); } return result; } From 3bd458b5e8d4e52bec9e576f2b80b988d9d30c47 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 22 Nov 2015 15:29:43 +0100 Subject: [PATCH 24/28] optimization: read configuration once before all battles, not before or during each individual one. --- conf/e4/config.json | 2 +- src/battle.c | 96 +++++++++++++++++++++------------------------ src/battle.h | 2 +- src/bind_process.c | 5 +-- src/laws.c | 2 +- src/settings.h | 1 - 6 files changed, 49 insertions(+), 59 deletions(-) diff --git a/conf/e4/config.json b/conf/e4/config.json index 90c60d72b..e407ae21e 100644 --- a/conf/e4/config.json +++ b/conf/e4/config.json @@ -64,7 +64,7 @@ "rules.combat.herospeed": 3, "rules.combat.demon_vampire": 5, "rules.combat.skill_bonus": 0, - "rules.combat.nat_armor": true, + "rules.combat.nat_armor": 1, "rules.items.loot_divisor": 2, "rules.items.give_divisor": 2, "rules.move.owner_leave": true, diff --git a/src/battle.c b/src/battle.c index a1e2572dc..1ad811c9f 100644 --- a/src/battle.c +++ b/src/battle.c @@ -114,11 +114,6 @@ static message *msg_separator; const troop no_troop = { 0, 0 }; -static int max_turns = 0; -static int damage_rules = 0; -static int loot_rules = 0; -static int skill_formula = 0; - #define FORMULA_ORIG 0 #define FORMULA_NEW 1 @@ -131,20 +126,44 @@ static int skill_formula = 0; #define DAMAGE_MELEE_BONUS (1<<1) #define DAMAGE_MISSILE_BONUS (1<<2) #define DAMAGE_SKILL_BONUS (1<<4) + +static int max_turns; +static int damage_rules; +static int loot_rules; +static int skill_formula; +static int rule_cavalry_skill; +static int rule_population_damage; +static int rule_hero_speed; +static bool rule_anon_battle; +static int rule_goblin_bonus; +static int rule_tactics_formula; +static int rule_nat_armor; +static int rule_cavalry_mode; + +static const curse_type *peace_ct, *slave_ct, *calm_ct; + /** initialize rules from configuration. */ -static void static_rules(void) +static void init_rules(void) { - loot_rules = - config_get_int("rules.combat.loot", + peace_ct = ct_find("peacezone"); + slave_ct = ct_find("slavery"); + calm_ct = ct_find("calmmonster"); + rule_nat_armor = config_get_int("rules.combat.nat_armor", 0); + rule_tactics_formula = config_get_int("rules.tactics.formula", 0); + rule_goblin_bonus = config_get_int("rules.combat.goblinbonus", 10); + rule_hero_speed = config_get_int("rules.combat.herospeed", 10); + rule_population_damage = config_get_int("rules.combat.populationdamage", 20); + 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); + loot_rules = config_get_int("rules.combat.loot", LOOT_MONSTERS | LOOT_OTHERS | LOOT_KEEPLOOT); /* new formula to calculate to-hit-chance */ - skill_formula = - config_get_int("rules.combat.skill_formula", + skill_formula = config_get_int("rules.combat.skill_formula", FORMULA_ORIG); /* maximum number of combat turns */ - max_turns = - config_get_int("rules.combat.turns", COMBAT_TURNS); + max_turns = config_get_int("rules.combat.turns", COMBAT_TURNS); /* damage calculation */ if (config_get_int("rules.combat.critical", 1)) { damage_rules |= DAMAGE_CRITICAL; @@ -664,15 +683,14 @@ weapon_skill(const weapon_type * wtype, const unit * u, bool attacking) static int CavalrySkill(void) { - return config_get_int("rules.cavalry.skill", 2); + return rule_cavalry_skill; } #define BONUS_SKILL 1 #define BONUS_DAMAGE 2 static int CavalryBonus(const unit * u, troop enemy, int type) { - int mode = config_get_int("rules.cavalry.mode", 1); - if (mode == 0) { + if (rule_cavalry_mode == 0) { /* old rule, Eressea 1.0 compat */ return (type == BONUS_SKILL) ? 2 : 0; } @@ -1095,7 +1113,7 @@ int calculate_armor(troop dt, const weapon_type *dwtype, const weapon_type *awty /* Momentan nur Trollgürtel und Werwolf-Eigenschaft */ am = select_magicarmor(dt); - if (config_get_int("rules.combat.nat_armor", 0) == 0) { + if (rule_nat_armor == 0) { /* natürliche Rüstung ist halbkumulativ */ if (ar > 0) { ar += an / 2; @@ -1597,13 +1615,7 @@ static troop select_opponent(battle * b, troop at, int mindist, int maxdist) } if (b->turn == 0 && dt.fighter) { - int tactics_formula = -1; - - if (tactics_formula < 0) { - tactics_formula = - config_get_int("rules.tactics.formula", 0); - } - if (tactics_formula == 1) { + if (rule_tactics_formula == 1) { int tactics = get_tactics(at.fighter->side, dt.fighter->side); /* percentage chance to get this attack */ @@ -1924,8 +1936,7 @@ int skilldiff(troop at, troop dt, int dist) } if (u_race(au) == get_race(RC_GOBLIN)) { - int goblin_bonus = config_get_int("rules.combat.goblinbonus", 10); - if (af->side->size[SUM_ROW] >= df->side->size[SUM_ROW] * goblin_bonus) { + if (af->side->size[SUM_ROW] >= df->side->size[SUM_ROW] * rule_goblin_bonus) { skdiff += 1; } } @@ -2122,7 +2133,6 @@ static int attacks_per_round(troop t) static void make_heroes(battle * b) { side *s; - int hero_speed = config_get_int("rules.combat.herospeed", 10); for (s = b->sides; s != b->sides + b->nsides; ++s) { fighter *fig; for (fig = s->fighters; fig; fig = fig->next) { @@ -2133,7 +2143,7 @@ static void make_heroes(battle * b) log_error("Hero %s is a %s.\n", unitname(u), u_race(u)->_name); } for (i = 0; i != u->number; ++i) { - fig->person[i].speed += (hero_speed - 1); + fig->person[i].speed += (rule_hero_speed - 1); } } } @@ -2630,14 +2640,7 @@ static bool seematrix(const faction * f, const side * s) static double PopulationDamage(void) { - static double value = -1.0; - if (value < 0) { - int damage = - config_get_int("rules.combat.populationdamage", - BATTLE_KILLS_PEASANTS); - value = damage / 100.0; - } - return value; + return rule_population_damage / 100.0; } static void battle_effects(battle * b, int dead_players) @@ -3192,7 +3195,6 @@ side * get_side(battle * b, const struct unit * u) side * find_side(battle * b, const faction * f, const group * g, unsigned int flags, const faction * stealthfaction) { side * s; - bool rule_anon_battle = config_get_int("rules.stealth.anon_battle", 1) != 0; for (s = b->sides; s != b->sides + b->nsides; ++s) { if (s->faction == f && s->group == g) { unsigned int s1flags = flags | SIDE_HASGUARDS; @@ -3935,15 +3937,6 @@ static bool start_battle(region * r, battle ** bp) order *ord; for (ord = u->orders; ord; ord = ord->next) { - static bool init = false; - static const curse_type *peace_ct, *slave_ct, *calm_ct; - - if (!init) { - init = true; - peace_ct = ct_find("peacezone"); - slave_ct = ct_find("slavery"); - calm_ct = ct_find("calmmonster"); - } if (getkeyword(ord) == K_ATTACK) { unit *u2; fighter *c1, *c2; @@ -4267,12 +4260,6 @@ void do_battle(region * r) battle *b = NULL; bool fighting = false; ship *sh; - static int init_rules = 0; - - if (!init_rules) { - static_rules(); - init_rules = 1; - } if (msg_separator == NULL) { msg_separator = msg_message("battle::section", ""); } @@ -4340,3 +4327,10 @@ void do_battle(region * r) } } +void do_battles(void) { + region *r; + init_rules(); + for (r = regions; r; r = r->next) { + do_battle(r); + } +} diff --git a/src/battle.h b/src/battle.h index 0e4fceffe..4ea3834d9 100644 --- a/src/battle.h +++ b/src/battle.h @@ -230,7 +230,7 @@ extern "C" { fighter * get_fighter(battle * b, const struct unit * u); /* END battle interface */ - extern void do_battle(struct region *r); + extern void do_battles(void); /* for combat spells and special attacks */ enum { SELECT_ADVANCE = 0x1, SELECT_DISTANCE = 0x2, SELECT_FIND = 0x4 }; diff --git a/src/bind_process.c b/src/bind_process.c index fcf2bd55c..7d0124400 100755 --- a/src/bind_process.c +++ b/src/bind_process.c @@ -70,10 +70,7 @@ void process_produce(void) { } void process_battle(void) { - struct region *r; - for (r = regions; r; r = r->next) { - do_battle(r); - } + do_battles(); } void process_siege(void) { diff --git a/src/laws.c b/src/laws.c index 175c12f6a..a904aa2a1 100755 --- a/src/laws.c +++ b/src/laws.c @@ -4352,7 +4352,7 @@ void init_processor(void) add_proc_region(p, enter_1, "Betreten (2. Versuch)"); /* to allow a buildingowner to enter the castle pre combat */ p += 10; - add_proc_region(p, do_battle, "Attackieren"); + add_proc_global(p, do_battles, "Attackieren"); if (!keyword_disabled(K_BESIEGE)) { p += 10; diff --git a/src/settings.h b/src/settings.h index b439c1477..5cd5e1a80 100644 --- a/src/settings.h +++ b/src/settings.h @@ -26,7 +26,6 @@ /* Vermehrungsrate Bauern in 1/10000. * TODO: Evt. Berechnungsfehler, reale Vermehrungsraten scheinen höher. */ #define PEASANTGROWTH 10 -#define BATTLE_KILLS_PEASANTS 20 #define PEASANTLUCK 10 #define ROW_FACTOR 3 /* factor for combat row advancement rule */ From 5f457f77b4ccdb668c2a74445425dd34f601e2c5 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 22 Nov 2015 15:45:31 +0100 Subject: [PATCH 25/28] refactoring: move those count_* functions out of config.c --- src/kernel/config.c | 63 --------------------------------------- src/kernel/config.h | 12 -------- src/kernel/faction.c | 71 ++++++++++++++++++++++++++++++++++++++++++-- src/kernel/faction.h | 13 ++++++++ 4 files changed, 81 insertions(+), 78 deletions(-) diff --git a/src/kernel/config.c b/src/kernel/config.c index e343b9628..8d90098f5 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -94,7 +94,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include #include -#include #include #include #include @@ -521,68 +520,6 @@ int alliedunit(const unit * u, const faction * f2, int mode) } return 0; } - -int count_faction(const faction * f, int flags) -{ - unit *u; - int n = 0; - for (u = f->units; u; u = u->nextF) { - const race *rc = u_race(u); - int x = (flags&COUNT_UNITS) ? 1 : u->number; - if (f->race != rc) { - if (!playerrace(rc)) { - if (flags&COUNT_MONSTERS) { - n += x; - } - } - else if (flags&COUNT_MIGRANTS) { - if (!is_cursed(u->attribs, C_SLAVE, 0)) { - n += x; - } - } - } - else if (flags&COUNT_DEFAULT) { - n += x; - } - } - return n; -} - -int count_units(const faction * f) -{ - return count_faction(f, COUNT_ALL | COUNT_UNITS); -} -int count_all(const faction * f) -{ - return count_faction(f, COUNT_ALL); -} -int count_migrants(const faction * f) -{ - return count_faction(f, COUNT_MIGRANTS); -} - -int count_maxmigrants(const faction * f) -{ - static int migrants = -1; - - if (migrants < 0) { - migrants = config_get_int("rules.migrants.max", INT_MAX); - } - if (migrants == INT_MAX) { - int x = 0; - if (f->race == get_race(RC_HUMAN)) { - int nsize = count_all(f); - if (nsize > 0) { - x = (int)(log10(nsize / 50.0) * 20); - if (x < 0) - x = 0; - } - } - return x; - } - return migrants; -} - void parse(keyword_t kword, int(*dofun) (unit *, struct order *), bool thisorder) { diff --git a/src/kernel/config.h b/src/kernel/config.h index b39807a33..14cfa6d2c 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -119,18 +119,6 @@ struct param; #define GIVE_DEFAULT (GIVE_SELF|GIVE_PEASANTS|GIVE_LUXURIES|GIVE_HERBS|GIVE_GOODS) int rule_give(void); -#define COUNT_MONSTERS 0x01 -#define COUNT_MIGRANTS 0x02 -#define COUNT_DEFAULT 0x04 -#define COUNT_ALL 0x07 -#define COUNT_UNITS 0x10 - - int count_faction(const struct faction * f, int flags); - int count_migrants(const struct faction * f); - int count_maxmigrants(const struct faction * f); - int count_all(const struct faction * f); - int count_units(const struct faction * f); - bool has_limited_skills(const struct unit *u); const struct race *findrace(const char *, const struct locale *); diff --git a/src/kernel/faction.c b/src/kernel/faction.c index 28d50ff76..597e45b30 100755 --- a/src/kernel/faction.c +++ b/src/kernel/faction.c @@ -22,6 +22,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include "alliance.h" #include "ally.h" +#include "curse.h" #include "equipment.h" #include "group.h" #include "item.h" @@ -54,10 +55,11 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. /* libc includes */ #include -#include -#include -#include #include +#include +#include +#include +#include faction *factions; @@ -712,3 +714,66 @@ void faction_setorigin(faction * f, int id, int x, int y) addlist(&f->ursprung, ur); } + +int count_faction(const faction * f, int flags) +{ + unit *u; + int n = 0; + for (u = f->units; u; u = u->nextF) { + const race *rc = u_race(u); + int x = (flags&COUNT_UNITS) ? 1 : u->number; + if (f->race != rc) { + if (!playerrace(rc)) { + if (flags&COUNT_MONSTERS) { + n += x; + } + } + else if (flags&COUNT_MIGRANTS) { + if (!is_cursed(u->attribs, C_SLAVE, 0)) { + n += x; + } + } + } + else if (flags&COUNT_DEFAULT) { + n += x; + } + } + return n; +} + +int count_units(const faction * f) +{ + return count_faction(f, COUNT_ALL | COUNT_UNITS); +} + +int count_all(const faction * f) +{ + return count_faction(f, COUNT_ALL); +} + +int count_migrants(const faction * f) +{ + return count_faction(f, COUNT_MIGRANTS); +} + +int count_maxmigrants(const faction * f) +{ + static int migrants = -1; + + if (migrants < 0) { + migrants = config_get_int("rules.migrants.max", INT_MAX); + } + if (migrants == INT_MAX) { + int x = 0; + if (f->race == get_race(RC_HUMAN)) { + int nsize = count_all(f); + if (nsize > 0) { + x = (int)(log10(nsize / 50.0) * 20); + if (x < 0) + x = 0; + } + } + return x; + } + return migrants; +} diff --git a/src/kernel/faction.h b/src/kernel/faction.h index 16b26c5f9..afeef37d3 100644 --- a/src/kernel/faction.h +++ b/src/kernel/faction.h @@ -162,6 +162,19 @@ extern "C" { int count_skill(struct faction *f, skill_t sk); bool faction_id_is_unused(int); +#define COUNT_MONSTERS 0x01 +#define COUNT_MIGRANTS 0x02 +#define COUNT_DEFAULT 0x04 +#define COUNT_ALL 0x07 +#define COUNT_UNITS 0x10 + + int count_faction(const struct faction * f, int flags); + int count_migrants(const struct faction * f); + int count_maxmigrants(const struct faction * f); + int count_all(const struct faction * f); + int count_units(const struct faction * f); + + #ifdef __cplusplus } #endif From f7698d92a972bace5810a94b3a0d61a0a61f9703 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 22 Nov 2015 15:53:50 +0100 Subject: [PATCH 26/28] change how migrant quota is configured for a race, eliminate the related rule caching --- conf/e3/config.json | 1 - conf/e4/config.json | 1 - res/eressea/races.xml | 1 + src/kernel/faction.c | 26 +++++++++++--------------- 4 files changed, 12 insertions(+), 17 deletions(-) diff --git a/conf/e3/config.json b/conf/e3/config.json index a6685a06f..3e443c389 100644 --- a/conf/e3/config.json +++ b/conf/e3/config.json @@ -52,7 +52,6 @@ "world.era": 3, "seed.population.min": 8, "seed.population.max": 8, - "rules.migrants.max": 0, "rules.reserve.twophase": true, "rules.owners.force_leave": false, "rules.monsters.attack_chance": 0.1, diff --git a/conf/e4/config.json b/conf/e4/config.json index e407ae21e..654213ede 100644 --- a/conf/e4/config.json +++ b/conf/e4/config.json @@ -51,7 +51,6 @@ "study.speedup": 2, "study.from_use": 0.4, "world.era": 3, - "rules.migrants.max": 0, "rules.reserve.twophase": true, "rules.owners.force_leave": false, "rules.transfermen": false, diff --git a/res/eressea/races.xml b/res/eressea/races.xml index 7f6c6d873..98f00f726 100644 --- a/res/eressea/races.xml +++ b/res/eressea/races.xml @@ -13,6 +13,7 @@ + diff --git a/src/kernel/faction.c b/src/kernel/faction.c index 597e45b30..288c54569 100755 --- a/src/kernel/faction.c +++ b/src/kernel/faction.c @@ -756,24 +756,20 @@ int count_migrants(const faction * f) return count_faction(f, COUNT_MIGRANTS); } +#define MIGRANTS_NONE 0 +#define MIGRANTS_LOG10 1 + int count_maxmigrants(const faction * f) { - static int migrants = -1; + int formula = get_param_int(f->race->parameters, "migrants.formula", 0); - if (migrants < 0) { - migrants = config_get_int("rules.migrants.max", INT_MAX); - } - if (migrants == INT_MAX) { - int x = 0; - if (f->race == get_race(RC_HUMAN)) { - int nsize = count_all(f); - if (nsize > 0) { - x = (int)(log10(nsize / 50.0) * 20); - if (x < 0) - x = 0; - } + if (formula == MIGRANTS_LOG10) { + int nsize = count_all(f); + if (nsize > 0) { + int x = (int)(log10(nsize / 50.0) * 20); + if (x < 0) x = 0; + return x; } - return x; } - return migrants; + return 0; } From 2e392b4d7cd5b111b61648f03332ee27d7512720 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 22 Nov 2015 16:14:27 +0100 Subject: [PATCH 27/28] eliminate even more static variable caches --- conf/e4/config.json | 2 +- src/economy.c | 16 ++++----- src/kernel/build.c | 6 +--- src/kernel/resources.c | 5 +-- src/kernel/unit.c | 8 ++--- src/laws.c | 79 +++++++++++++++++++----------------------- src/magic.c | 10 ++---- src/randenc.c | 13 +++---- src/settings.h | 2 -- src/study.c | 11 ++---- src/upkeep.c | 7 ++-- 11 files changed, 58 insertions(+), 101 deletions(-) diff --git a/conf/e4/config.json b/conf/e4/config.json index 654213ede..027e4c655 100644 --- a/conf/e4/config.json +++ b/conf/e4/config.json @@ -77,7 +77,7 @@ "rules.blessed_harvest.flags": 1, "rules.magic.elfpower": true, "rules.magic.playerschools": "gwyrrd illaun draig cerddor", - "rules.build.other_buildings": 1, + "rules.build.other_buildings": true, "rules.economy.taxation": 1, "rules.food.flags": 2, "rules.economy.roqf": 5, diff --git a/src/economy.c b/src/economy.c index 26eb5251b..e4e52e0ce 100644 --- a/src/economy.c +++ b/src/economy.c @@ -3136,8 +3136,11 @@ static void peasant_taxes(region * r) static bool rule_auto_taxation(void) { - int rule = config_get_int("rules.economy.taxation", 0); - return rule != 0; + return config_get_int("rules.economy.taxation", 0) != 0; +} + +static bool rule_autowork(void) { + return config_get_int("work.auto", 0) != 0; } void produce(struct region *r) @@ -3145,7 +3148,6 @@ void produce(struct region *r) request workers[MAX_WORKERS]; request *taxorders, *lootorders, *sellorders, *stealorders, *buyorders; unit *u; - static int rule_autowork = -1; bool limited = true; request *nextworker = workers; assert(r); @@ -3160,10 +3162,6 @@ void produce(struct region *r) * * lehren vor lernen. */ - if (rule_autowork < 0) { - rule_autowork = config_get_int("work.auto", 0); - } - assert(rmoney(r) >= 0); assert(rpeasants(r) >= 0); @@ -3237,7 +3235,7 @@ void produce(struct region *r) break; case K_WORK: - if (!rule_autowork && do_work(u, u->thisorder, nextworker) == 0) { + if (!rule_autowork() && do_work(u, u->thisorder, nextworker) == 0) { assert(nextworker - workers < MAX_WORKERS); ++nextworker; } @@ -3282,7 +3280,7 @@ void produce(struct region *r) * auszugeben bereit sind. */ if (entertaining) expandentertainment(r); - if (!rule_autowork) { + if (!rule_autowork()) { expandwork(r, workers, nextworker, maxworkingpeasants(r)); } if (taxorders) diff --git a/src/kernel/build.c b/src/kernel/build.c index 20f70800f..7c387cb10 100644 --- a/src/kernel/build.c +++ b/src/kernel/build.c @@ -685,7 +685,6 @@ build_building(unit * u, const building_type * btype, int id, int want, order * const char *btname; order *new_order = NULL; const struct locale *lang = u->faction->locale; - static int rule_other = -1; assert(u->number); assert(btype->construction); @@ -749,10 +748,7 @@ build_building(unit * u, const building_type * btype, int id, int want, order * n = 1; } if (b) { - if (rule_other < 0) { - rule_other = - config_get_int("rules.build.other_buildings", 1); - } + bool rule_other = config_get_int("rules.build.other_buildings", 1) != 0; if (!rule_other) { unit *owner = building_owner(b); if (!owner || owner->faction != u->faction) { diff --git a/src/kernel/resources.c b/src/kernel/resources.c index 9db74a99f..3ffad7491 100644 --- a/src/kernel/resources.c +++ b/src/kernel/resources.c @@ -82,10 +82,7 @@ void terraform_resources(region * r) { int i; const terrain_type *terrain = r->terrain; - static int terraform_all = -1; - if (terraform_all < 0) { - terraform_all = config_get_int("rules.terraform.all", 0); - } + bool terraform_all = config_get_int("rules.terraform.all", 0) != 0; if (terrain->production == NULL) return; diff --git a/src/kernel/unit.c b/src/kernel/unit.c index b328b748b..14e064b05 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -1721,17 +1721,13 @@ void unit_addorder(unit * u, order * ord) int unit_max_hp(const unit * u) { - static int rules_stamina = -1; int h; double p; static const curse_type *heal_ct = NULL; - - if (rules_stamina < 0) { - rules_stamina = config_get_int("rules.stamina", STAMINA_AFFECTS_HP); - } + int rule_stamina = config_get_int("rules.stamina", STAMINA_AFFECTS_HP); h = u_race(u)->hitpoints; - if (rules_stamina & 1) { + 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/laws.c b/src/laws.c index a904aa2a1..a53dfe31b 100755 --- a/src/laws.c +++ b/src/laws.c @@ -118,7 +118,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. static bool RemoveNMRNewbie(void) { int value = config_get_int("nmr.removenewbie", 0); - return value!=0; + return value != 0; } static void age_unit(region * r, unit * u) @@ -250,7 +250,7 @@ static double peasant_growth_factor(void) #ifdef SLOWLUCK int peasant_luck_effect(int peasants, int luck, int maxp, double variance) { - int n, births=0; + int n, births = 0; double factor = peasant_growth_factor(); for (n = peasants; n && luck; --n) { int chances = 0; @@ -330,7 +330,7 @@ static void peasants(region * r) * Großteil. dead kann nie größer als rpeasants(r) - satiated werden, * so dass rpeasants(r) >= 0 bleiben muß. */ - /* Es verhungert maximal die unterernährten Bevölkerung. */ + /* Es verhungert maximal die unterernährten Bevölkerung. */ n = _min(peasants - satiated, rpeasants(r)); dead += (int)(0.5 + n * PEASANT_STARVATION_CHANCE); @@ -423,7 +423,7 @@ static void horses(region * r) int i; double growth = (RESOURCE_QUANTITY * HORSEGROWTH * 200 * (maxhorses - - horses)) / maxhorses; + horses)) / maxhorses; if (growth > 0) { if (a_find(r->attribs, &at_horseluck)) @@ -482,7 +482,7 @@ extern struct attrib_type at_germs; static void growing_trees_e3(region * r, const int current_season, -const int last_weeks_season) + const int last_weeks_season) { static const int transform[4][3] = { { -1, -1, 0 }, @@ -751,7 +751,7 @@ void nmr_warnings(void) if (msg == NULL) { msg = msg_message("warn_dropout", "faction turns", f, - turn - f->lastorders); + turn - f->lastorders); } add_message(&fa->msgs, msg); } @@ -785,12 +785,7 @@ void demographics(void) /* die Nachfrage nach Produkten steigt. */ struct demand *dmd; if (r->land) { - static int plant_rules = -1; - - if (plant_rules < 0) { - plant_rules = - config_get_int("rules.grow.formula", 0); - } + int plant_rules = config_get_int("rules.grow.formula", 0); for (dmd = r->land->demands; dmd; dmd = dmd->next) { if (dmd->value > 0 && dmd->value < MAXDEMAND) { float rise = DMRISE; @@ -988,11 +983,7 @@ static int mayboard(const unit * u, ship * sh) static bool CheckOverload(void) { - static int value = -1; - if (value < 0) { - value = config_get_int("rules.check_overload", 0); - } - return value != 0; + return config_get_int("rules.check_overload", 0) != 0; } int enter_ship(unit * u, struct order *ord, int id, bool report) @@ -1209,9 +1200,7 @@ int *age = NULL; static void nmr_death(faction * f) { - static int rule = -1; - if (rule < 0) - rule = config_get_int("rules.nmr.destroy", 0); + int rule = config_get_int("rules.nmr.destroy", 0) != 0; if (rule) { unit *u; for (u = f->units; u; u = u->nextF) { @@ -1311,7 +1300,7 @@ int ally_cmd(unit * u, struct order *ord) if (!s || !s[0]) { keyword = P_ANY; - } + } else { keyword = findparam(s, u->faction->locale); } @@ -1492,9 +1481,9 @@ int prefix_cmd(unit * u, struct order *ord) if (fval(u, UFL_GROUP)) { attrib *a = a_find(u->attribs, &at_group); if (a) { - group *g = (group *)a->data.v; + group *g = (group *)a->data.v; ap = &g->attribs; - } + } } set_prefix(ap, race_prefixes[var.i]); } @@ -1920,7 +1909,7 @@ deliverMail(faction * f, region * r, unit * u, const char *s, unit * receiver) else { /* BOTSCHAFT an EINHEIT */ ADDMSG(&f->msgs, msg_message("unitmessage", "region unit sender string", r, - receiver, u, s)); + receiver, u, s)); } } @@ -2304,7 +2293,7 @@ static bool display_race(faction * f, unit * u, const race * rc) /* hp_p : Trefferpunkte */ bytes = slprintf(bufp, size, " %d %s", rc->hitpoints, LOC(f->locale, - "stat_hitpoints")); + "stat_hitpoints")); assert(bytes <= INT_MAX); if (wrptr(&bufp, &size, (int)bytes) != 0) WARN_STATIC_BUFFER(); @@ -2312,7 +2301,7 @@ static bool display_race(faction * f, unit * u, const race * rc) /* b_attacke : Angriff */ bytes = slprintf(bufp, size, ", %s: %d", LOC(f->locale, "stat_attack"), - (rc->at_default + rc->at_bonus)); + (rc->at_default + rc->at_bonus)); assert(bytes <= INT_MAX); if (wrptr(&bufp, &size, (int)bytes) != 0) WARN_STATIC_BUFFER(); @@ -2320,7 +2309,7 @@ static bool display_race(faction * f, unit * u, const race * rc) /* b_defense : Verteidigung */ bytes = slprintf(bufp, size, ", %s: %d", LOC(f->locale, "stat_defense"), - (rc->df_default + rc->df_bonus)); + (rc->df_default + rc->df_bonus)); assert(bytes <= INT_MAX); if (wrptr(&bufp, &size, (int)bytes) != 0) WARN_STATIC_BUFFER(); @@ -2379,12 +2368,12 @@ static bool display_race(faction * f, unit * u, const race * rc) case AT_STANDARD: bytes = (size_t)_snprintf(bufp, size, "%s (%s)", - LOC(f->locale, "attack_standard"), rc->def_damage); + LOC(f->locale, "attack_standard"), rc->def_damage); break; case AT_NATURAL: bytes = (size_t)_snprintf(bufp, size, "%s (%s)", - LOC(f->locale, "attack_natural"), rc->attack[a].data.dice); + LOC(f->locale, "attack_natural"), rc->attack[a].data.dice); break; case AT_SPELL: case AT_COMBATSPELL: @@ -2396,7 +2385,7 @@ static bool display_race(faction * f, unit * u, const race * rc) case AT_STRUCTURAL: bytes = (size_t)_snprintf(bufp, size, "%s (%s)", - LOC(f->locale, "attack_structural"), rc->attack[a].data.dice); + LOC(f->locale, "attack_structural"), rc->attack[a].data.dice); break; default: bytes = 0; @@ -2500,7 +2489,7 @@ int promotion_cmd(unit * u, struct order *ord) if (maxheroes(u->faction) < countheroes(u->faction) + u->number) { ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "heroes_maxed", "max count", - maxheroes(u->faction), countheroes(u->faction))); + maxheroes(u->faction), countheroes(u->faction))); return 0; } if (!valid_race(u->faction, u_race(u))) { @@ -2745,7 +2734,8 @@ void sinkships(struct region * r) double dmg = config_get_flt("rules.ship.damage.nocrewocean", 0.3); damage_ship(sh, dmg); } - } else if (!ship_owner(sh)) { + } + else if (!ship_owner(sh)) { // any ship lying around without an owner slowly rots double dmg = config_get_flt("rules.ship.damage.nocrew", 0.05); damage_ship(sh, dmg); @@ -3280,14 +3270,14 @@ void new_units(void) if (err == 1) { ADDMSG(&u->faction->msgs, msg_feedback(u, makeord, - "too_many_units_in_alliance", - "allowed", maxunits(u->faction))); + "too_many_units_in_alliance", + "allowed", maxunits(u->faction))); } else { ADDMSG(&u->faction->msgs, msg_feedback(u, makeord, - "too_many_units_in_faction", - "allowed", maxunits(u->faction))); + "too_many_units_in_faction", + "allowed", maxunits(u->faction))); } ordp = &makeord->next; @@ -3360,7 +3350,7 @@ void update_long_order(unit * u) } // hungry units do not get long orders: - if (hunger) { + if (hunger) { if (u->old_orders) { // keep looking for repeated orders that might clear the old_orders continue; @@ -3432,7 +3422,8 @@ void update_long_order(unit * u) if (hunger) { // Hungernde Einheiten führen NUR den default-Befehl aus set_order(&u->thisorder, default_order(u->faction->locale)); - } else if (!exclusive) { + } + else if (!exclusive) { // Wenn die Einheit handelt oder zaubert, muss der Default-Befehl gelöscht werden. set_order(&u->thisorder, NULL); } @@ -3459,7 +3450,7 @@ static int use_item(unit * u, const item_type * itype, int amount, struct order return EUNUSABLE; } result = itype->use ? itype->use(u, itype, amount, ord) : EUNUSABLE; - if (result>0) { + if (result > 0) { use_pooled(u, itype->rtype, GET_DEFAULT, result); } return result; @@ -4543,7 +4534,7 @@ void update_subscriptions(void) bool cansee(const faction * f, const region * r, const unit * u, int modifier) -/* r kann != u->region sein, wenn es um Durchreisen geht, +/* r kann != u->region sein, wenn es um Durchreisen geht, * oder Zauber (sp_generous, sp_fetchastral). * Es muss auch niemand aus f in der region sein, wenn sie vom Turm * erblickt wird */ @@ -4645,10 +4636,10 @@ bool cansee_unit(const unit * u, const unit * target, int modifier) bool cansee_durchgezogen(const faction * f, const region * r, const unit * u, -int modifier) -/* r kann != u->region sein, wenn es um durchreisen geht */ -/* und es muss niemand aus f in der region sein, wenn sie vom Turm -* erblickt wird */ + int modifier) + /* r kann != u->region sein, wenn es um durchreisen geht */ + /* und es muss niemand aus f in der region sein, wenn sie vom Turm + * erblickt wird */ { int n; unit *u2; diff --git a/src/magic.c b/src/magic.c index 1c6acda94..e1eccbe05 100644 --- a/src/magic.c +++ b/src/magic.c @@ -215,15 +215,11 @@ static void free_mage(attrib * a) bool FactionSpells(void) { - static int rules_factionspells = -1; - if (rules_factionspells < 0) { - rules_factionspells = config_get_int("rules.magic.factionlist", 0); - } - return rules_factionspells!=0; + return config_get_int("rules.magic.factionlist", 0) != 0; } -void read_spells(struct quicklist **slistp, magic_t mtype, -struct storage *store) +void read_spells(struct quicklist **slistp, magic_t mtype, + struct storage *store) { for (;;) { spell *sp; diff --git a/src/randenc.c b/src/randenc.c index 80ff498aa..7f27fed4d 100644 --- a/src/randenc.c +++ b/src/randenc.c @@ -962,10 +962,7 @@ static void demon_skillchanges(void) if (fval(u, UFL_HUNGER)) { /* hungry demons only go down, never up in skill */ - static int rule_hunger = -1; - if (rule_hunger < 0) { - rule_hunger = config_get_int("hunger.demon.skill", 0); - } + int rule_hunger = config_get_int("hunger.demon.skill", 0) != 0; if (rule_hunger) { upchance = 0; downchance = 15; @@ -1009,15 +1006,13 @@ static void icebergs(void) } } +#define HERBS_ROT /* herbs owned by units have a chance to rot. */ +#define HERBROTCHANCE 5 /* Verrottchance für Kräuter (ifdef HERBS_ROT) */ #ifdef HERBS_ROT static void rotting_herbs(void) { - static int rule_rot = -1; region *r; - - if (rule_rot < 0) { - rule_rot = config_get_int("rules.economy.herbrot", HERBROTCHANCE); - } + int rule_rot = config_get_int("rules.economy.herbrot", HERBROTCHANCE); if (rule_rot == 0) return; for (r = regions; r; r = r->next) { diff --git a/src/settings.h b/src/settings.h index 5cd5e1a80..c44bc0f2a 100644 --- a/src/settings.h +++ b/src/settings.h @@ -42,14 +42,12 @@ /* experimental gameplay features (that don't affect the savefile) */ /* TODO: move these settings to settings.h or into configuration files */ #define GOBLINKILL /* Goblin-Spezialklau kann tödlich enden */ -#define HERBS_ROT /* herbs owned by units have a chance to rot. */ #define INSECT_POTION /* Spezialtrank für Insekten */ #define ORCIFICATION /* giving snotlings to the peasants gets counted */ #define TREESIZE (8) /* space used by trees (in #peasants) */ #define PEASANTFORCE 0.75 /* Chance einer Vermehrung trotz 90% Auslastung */ -#define HERBROTCHANCE 5 /* Verrottchance für Kräuter (ifdef HERBS_ROT) */ /* Gebäudegröße = Minimalbelagerer */ #define SIEGEFACTOR 2 diff --git a/src/study.c b/src/study.c index 79adf2847..db722fff8 100644 --- a/src/study.c +++ b/src/study.c @@ -546,17 +546,10 @@ int study_cmd(unit * u, order * ord) skill_t sk; int maxalchemy = 0; int speed_rule = (study_rule_t)config_get_int("study.speedup", 0); - static int learn_newskills = -1; struct building *b = inside_building(u); const struct building_type *btype = building_is_active(b) ? b->type : NULL; + bool learn_newskills = config_get_int("study.newskills", 1) != 0; - if (learn_newskills < 0) { - const char *str = config_get("study.newskills"); - if (str && strcmp(str, "false") == 0) - learn_newskills = 0; - else - learn_newskills = 1; - } if (!unit_can_study(u)) { ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "error_race_nolearn", "race", u_race(u))); @@ -580,7 +573,7 @@ int study_cmd(unit * u, order * ord) cmistake(u, ord, 771, MSG_EVENT); return 0; } - if (learn_newskills == 0) { + if (!learn_newskills) { skill *sv = unit_skill(u, sk); if (sv == NULL) { /* we can only learn skills we already have */ diff --git a/src/upkeep.c b/src/upkeep.c index d83abbe86..da9cbff74 100644 --- a/src/upkeep.c +++ b/src/upkeep.c @@ -270,11 +270,8 @@ void get_food(region * r) peasantfood = 0; } if (hungry > 0) { - static int demon_hunger = -1; - if (demon_hunger < 0) { - demon_hunger = config_get_int("hunger.demons", 0); - } - if (demon_hunger == 0) { + bool demon_hunger = config_get_int("hunger.demons", 0) != 0; + if (demon_hunger) { /* demons who don't feed are hungry */ if (hunger(hungry, u)) fset(u, UFL_HUNGER); From 48c75466b682b550b808298a0ab5296443c959e3 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 22 Nov 2015 17:32:35 +0100 Subject: [PATCH 28/28] kill static caches for floating-point variables --- src/battle.c | 7 ++----- src/kernel/resources.c | 7 +------ src/laws.c | 6 +++--- src/magic.c | 9 ++------- src/move.c | 6 +----- 5 files changed, 9 insertions(+), 26 deletions(-) diff --git a/src/battle.c b/src/battle.c index 1ad811c9f..d35110b4e 100644 --- a/src/battle.c +++ b/src/battle.c @@ -2532,12 +2532,9 @@ troop select_ally(fighter * af, int minrow, int maxrow, int allytype) static int loot_quota(const unit * src, const unit * dst, const item_type * type, int n) { - static double divisor = -1; if (dst && src && src->faction != dst->faction) { - if (divisor < 0) { - divisor = config_get_flt("rules.items.loot_divisor", 1); - assert(divisor == 0 || divisor >= 1); - } + double divisor = config_get_flt("rules.items.loot_divisor", 1); + assert(divisor == 0 || divisor >= 1); if (divisor >= 1) { double r = n / divisor; int x = (int)r; diff --git a/src/kernel/resources.c b/src/kernel/resources.c index 3ffad7491..658445c69 100644 --- a/src/kernel/resources.c +++ b/src/kernel/resources.c @@ -29,12 +29,7 @@ static double ResourceFactor(void) { - static double value = -1.0; - if (value < 0) { - const char *str = config_get("resource.factor"); - value = str ? atof(str) : 1.0; - } - return value; + return config_get_flt("resource.factor", 1.0); } void update_resources(region * r) diff --git a/src/laws.c b/src/laws.c index a53dfe31b..6b0ce3d35 100755 --- a/src/laws.c +++ b/src/laws.c @@ -3465,7 +3465,8 @@ static int use_item(unit * u, const item_type * itype, int amount, struct order static double heal_factor(const unit * u) { - static double elf_regen = -1; + double elf_regen; + switch (old_race(u_race(u))) { case RC_TROLL: case RC_DAEMON: @@ -3473,8 +3474,7 @@ static double heal_factor(const unit * u) case RC_GOBLIN: return 2.0; case RC_ELF: - if (elf_regen < 0) - elf_regen = get_param_flt(u_race(u)->parameters, "regen.forest", 1.0F); + elf_regen = get_param_flt(u_race(u)->parameters, "regen.forest", 1.0F); if (elf_regen != 1.0 && r_isforest(u->region)) { return elf_regen; } diff --git a/src/magic.c b/src/magic.c index e1eccbe05..510240a74 100644 --- a/src/magic.c +++ b/src/magic.c @@ -108,14 +108,9 @@ attrib_type at_reportspell = { ** TODO: separate castle-appearance from illusion-effects **/ -static float MagicRegeneration(void) +static double MagicRegeneration(void) { - static float value = -1.0; - if (value < 0) { - const char *str = config_get("magic.regeneration"); - value = str ? (float)atof(str) : 1.0F; - } - return value; + return config_get_flt("magic.regeneration", 1.0); } static double MagicPower(double force) diff --git a/src/move.c b/src/move.c index 3a3d91d83..5117b7f6a 100644 --- a/src/move.c +++ b/src/move.c @@ -700,11 +700,7 @@ static void set_coast(ship * sh, region * r, region * rnext) static float damage_drift(void) { - static float value = -1.0F; - if (value < 0) { - value = (float)config_get_flt("rules.ship.damage_drift", 0.02F); - } - return value; + return (float)config_get_flt("rules.ship.damage_drift", 0.02); } static void drifting_ships(region * r)