From f1f46782f774ffb3dcb3b21ec85ad382069ae720 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 8 Oct 2010 23:39:16 -0700 Subject: [PATCH] * fix some rules to not use static variables across multiple runs * add tests for upkeep * add a config setting to disable upkeep in tests * add a config setting to disable random factor in studying --- scripts/tests/common.lua | 27 +++++++++++++- src/gamecode/laws.c | 17 +++++++-- src/kernel/config.c | 76 +++++++++++++++++++++++++--------------- src/kernel/skill.c | 20 +++++++---- 4 files changed, 101 insertions(+), 39 deletions(-) diff --git a/scripts/tests/common.lua b/scripts/tests/common.lua index 76bc5a763..d83a5314e 100644 --- a/scripts/tests/common.lua +++ b/scripts/tests/common.lua @@ -2,6 +2,7 @@ require "lunit" function setup() free_game() + settings.set("rules.economy.food", "4") end function one_unit(r, f) @@ -650,6 +651,30 @@ function test_expensive_skills_cost_money() u:clear_orders() u:add_order("LERNEN MAGIE Gwyrrd") process_orders() - assert_equal(9890, u:get_item("money")) + assert_equal(9900, u:get_item("money")) assert_equal(1, u:get_skill("magic")) end + +function test_food_is_consumed() + local r = region.create(0, 0, "plain") + local f = faction.create("noreply@eressea.de", "human", "de") + local u = unit.create(f, r, 1) + u:add_item("money", 100) + u:clear_orders() + u:add_order("LERNEN Reiten") -- don't work + settings.set("rules.economy.food", "4") + process_orders() + assert_equal(100, u:get_item("money")) +end + +function test_food_can_override() + local r = region.create(0, 0, "plain") + local f = faction.create("noreply@eressea.de", "human", "de") + local u = unit.create(f, r, 1) + u:add_item("money", 100) + u:clear_orders() + u:add_order("LERNEN Reiten") -- don't work + settings.set("rules.economy.food", "0") + process_orders() + assert_equal(90, u:get_item("money")) +end diff --git a/src/gamecode/laws.c b/src/gamecode/laws.c index 232637508..5986ff315 100644 --- a/src/gamecode/laws.c +++ b/src/gamecode/laws.c @@ -196,6 +196,12 @@ help_feed(unit * donor, unit * u, int * need_p) *need_p = need; } +enum { + FOOD_FROM_PEASANTS = 1, + FOOD_FROM_OWNER = 2, + FOOD_IS_FREE = 4 +}; + static void get_food(region *r) { @@ -203,11 +209,16 @@ get_food(region *r) unit *u; int peasantfood = rpeasants(r)*10; static int food_rules = -1; + static int gamecookie = -1; - if (food_rules<0) { + if (food_rules<0 || gamecookie!=global.cookie) { + gamecookie = global.cookie; food_rules = get_param_int(global.parameters, "rules.economy.food", 0); } + if (food_rules&FOOD_IS_FREE) { + return; + } /* 1. Versorgung von eigenen Einheiten. Das vorhandene Silber * wird zunächst so auf die Einheiten aufgeteilt, dass idealerweise * jede Einheit genug Silber für ihren Unterhalt hat. */ @@ -238,7 +249,7 @@ get_food(region *r) u->ship->flags -= SF_FISHING; } - if (food_rules&1) { + if (food_rules&FOOD_FROM_PEASANTS) { faction * owner = region_get_owner(r); /* if the region is owned, and the owner is nice, then we'll get * food from the peasants - should not be used with WORK */ @@ -279,7 +290,7 @@ get_food(region *r) if (need > 0) { unit *v; - if (food_rules&2) { + if (food_rules&FOOD_FROM_OWNER) { /* the owner of the region is the first faction to help out when you're hungry */ faction * owner = region_get_owner(r); if (owner && owner!=u->faction) { diff --git a/src/kernel/config.c b/src/kernel/config.c index 2591ce7ec..294827f51 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -115,7 +115,9 @@ attrib_type at_xontormiaexpress = { int NewbieImmunity(void) { static int value = -1; - if (value<0) { + static int gamecookie = -1; + if (value<0 || gamecookie!=global.cookie) { + gamecookie = global.cookie; value = get_param_int(global.parameters, "NewbieImmunity", 0); } return value; @@ -130,7 +132,9 @@ IsImmune(const faction * f) static int MaxAge(void) { static int value = -1; - if (value<0) { + static int gamecookie = -1; + if (value<0 || gamecookie!=global.cookie) { + gamecookie = global.cookie; value = get_param_int(global.parameters, "MaxAge", 0); } return value; @@ -151,8 +155,10 @@ ally_flag(const char * s, int help_mask) boolean ExpensiveMigrants(void) { - int value = -1; - if (value<0) { + 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; @@ -165,8 +171,10 @@ int AllianceAuto(void) { static int value = -1; - if (value<0) { + 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); @@ -190,78 +198,88 @@ AllianceAuto(void) int HelpMask(void) { - static int value = -1; - if (value<0) { + static int rule = -1; + static int gamecookie = -1; + if (rule<0 || gamecookie!=global.cookie) { const char * str = get_param(global.parameters, "rules.help.mask"); - value = 0; + gamecookie = global.cookie; + rule = 0; if (str!=NULL) { char * sstr = strdup(str); char * tok = strtok(sstr, " "); while (tok) { - value |= ally_flag(tok, -1); + rule |= ally_flag(tok, -1); tok = strtok(NULL, " "); } free(sstr); } else { - value = HELP_ALL; + rule = HELP_ALL; } } - return value; + return rule; } int AllianceRestricted(void) { - static int value = -1; - if (value<0) { + static int rule = -1; + static int gamecookie = -1; + if (rule<0 || gamecookie!=global.cookie) { const char * str = get_param(global.parameters, "alliance.restricted"); - value = 0; + gamecookie = global.cookie; + rule = 0; if (str!=NULL) { char * sstr = strdup(str); char * tok = strtok(sstr, " "); while (tok) { - value |= ally_flag(tok, -1); + rule |= ally_flag(tok, -1); tok = strtok(NULL, " "); } free(sstr); } - value &= HelpMask(); + rule &= HelpMask(); } - return value; + return rule; } int LongHunger(const struct unit * u) { - static int value = -1; + static int gamecookie = -1; + static int rule = -1; if (u!=NULL) { if (!fval(u, UFL_HUNGER)) return false; #ifdef NEW_DAEMONHUNGER_RULE if (u->race==new_race[RC_DAEMON]) return false; #endif } - if (value<0) { - value = get_param_int(global.parameters, "hunger.long", 0); + if (rule<0 || gamecookie!=global.cookie) { + gamecookie = global.cookie; + rule = get_param_int(global.parameters, "hunger.long", 0); } - return value; + return rule; } int SkillCap(skill_t sk) { - static int value = -1; + static int gamecookie = -1; + static int rule = -1; if (sk==SK_MAGIC) return 0; /* no caps on magic */ - if (value<0) { - value = get_param_int(global.parameters, "skill.maxlevel", 0); + if (rule<0 || gamecookie!=global.cookie) { + gamecookie = global.cookie; + rule = get_param_int(global.parameters, "skill.maxlevel", 0); } - return value; + return rule; } int NMRTimeout(void) { - static int value = -1; - if (value<0) { - value = get_param_int(global.parameters, "nmr.timeout", 0); + 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 value; + return rule; } race_t diff --git a/src/kernel/skill.c b/src/kernel/skill.c index 5f8d053d4..ebc225d27 100644 --- a/src/kernel/skill.c +++ b/src/kernel/skill.c @@ -277,16 +277,24 @@ sk_set(skill * sv, int level) sv->level = (unsigned char)level; } +static int +rule_random_progress(void) { + return get_param_int(global.parameters, "study.random_progress", 1); +} + int skill_weeks(int level) /* how many weeks must i study to get from level to level+1 */ { - int coins = 2*level; - int heads = 1; - while (coins--) { - heads += rng_int() % 2; - } - return heads; + if (rule_random_progress()) { + int coins = 2*level; + int heads = 1; + while (coins--) { + heads += rng_int() % 2; + } + return heads; + } + return level+1; } void