* 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
This commit is contained in:
Enno Rehling 2010-10-08 23:39:16 -07:00
parent 6ce601828b
commit f1f46782f7
4 changed files with 101 additions and 39 deletions

View file

@ -2,6 +2,7 @@ require "lunit"
function setup() function setup()
free_game() free_game()
settings.set("rules.economy.food", "4")
end end
function one_unit(r, f) function one_unit(r, f)
@ -650,6 +651,30 @@ function test_expensive_skills_cost_money()
u:clear_orders() u:clear_orders()
u:add_order("LERNEN MAGIE Gwyrrd") u:add_order("LERNEN MAGIE Gwyrrd")
process_orders() process_orders()
assert_equal(9890, u:get_item("money")) assert_equal(9900, u:get_item("money"))
assert_equal(1, u:get_skill("magic")) assert_equal(1, u:get_skill("magic"))
end 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

View file

@ -196,6 +196,12 @@ help_feed(unit * donor, unit * u, int * need_p)
*need_p = need; *need_p = need;
} }
enum {
FOOD_FROM_PEASANTS = 1,
FOOD_FROM_OWNER = 2,
FOOD_IS_FREE = 4
};
static void static void
get_food(region *r) get_food(region *r)
{ {
@ -203,11 +209,16 @@ get_food(region *r)
unit *u; unit *u;
int peasantfood = rpeasants(r)*10; int peasantfood = rpeasants(r)*10;
static int food_rules = -1; 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); 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 /* 1. Versorgung von eigenen Einheiten. Das vorhandene Silber
* wird zunächst so auf die Einheiten aufgeteilt, dass idealerweise * wird zunächst so auf die Einheiten aufgeteilt, dass idealerweise
* jede Einheit genug Silber für ihren Unterhalt hat. */ * jede Einheit genug Silber für ihren Unterhalt hat. */
@ -238,7 +249,7 @@ get_food(region *r)
u->ship->flags -= SF_FISHING; u->ship->flags -= SF_FISHING;
} }
if (food_rules&1) { if (food_rules&FOOD_FROM_PEASANTS) {
faction * owner = region_get_owner(r); faction * owner = region_get_owner(r);
/* if the region is owned, and the owner is nice, then we'll get /* if the region is owned, and the owner is nice, then we'll get
* food from the peasants - should not be used with WORK */ * food from the peasants - should not be used with WORK */
@ -279,7 +290,7 @@ get_food(region *r)
if (need > 0) { if (need > 0) {
unit *v; 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 */ /* the owner of the region is the first faction to help out when you're hungry */
faction * owner = region_get_owner(r); faction * owner = region_get_owner(r);
if (owner && owner!=u->faction) { if (owner && owner!=u->faction) {

View file

@ -115,7 +115,9 @@ attrib_type at_xontormiaexpress = {
int int
NewbieImmunity(void) { NewbieImmunity(void) {
static int value = -1; 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); value = get_param_int(global.parameters, "NewbieImmunity", 0);
} }
return value; return value;
@ -130,7 +132,9 @@ IsImmune(const faction * f)
static int static int
MaxAge(void) { MaxAge(void) {
static int value = -1; 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); value = get_param_int(global.parameters, "MaxAge", 0);
} }
return value; return value;
@ -151,8 +155,10 @@ ally_flag(const char * s, int help_mask)
boolean boolean
ExpensiveMigrants(void) ExpensiveMigrants(void)
{ {
int value = -1; 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, "study.expensivemigrants", 0); value = get_param_int(global.parameters, "study.expensivemigrants", 0);
} }
return value; return value;
@ -165,8 +171,10 @@ int
AllianceAuto(void) AllianceAuto(void)
{ {
static int value = -1; 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"); const char * str = get_param(global.parameters, "alliance.auto");
gamecookie = global.cookie;
value = 0; value = 0;
if (str!=NULL) { if (str!=NULL) {
char * sstr = strdup(str); char * sstr = strdup(str);
@ -190,78 +198,88 @@ AllianceAuto(void)
int int
HelpMask(void) HelpMask(void)
{ {
static int value = -1; static int rule = -1;
if (value<0) { static int gamecookie = -1;
if (rule<0 || gamecookie!=global.cookie) {
const char * str = get_param(global.parameters, "rules.help.mask"); const char * str = get_param(global.parameters, "rules.help.mask");
value = 0; gamecookie = global.cookie;
rule = 0;
if (str!=NULL) { if (str!=NULL) {
char * sstr = strdup(str); char * sstr = strdup(str);
char * tok = strtok(sstr, " "); char * tok = strtok(sstr, " ");
while (tok) { while (tok) {
value |= ally_flag(tok, -1); rule |= ally_flag(tok, -1);
tok = strtok(NULL, " "); tok = strtok(NULL, " ");
} }
free(sstr); free(sstr);
} else { } else {
value = HELP_ALL; rule = HELP_ALL;
} }
} }
return value; return rule;
} }
int int
AllianceRestricted(void) AllianceRestricted(void)
{ {
static int value = -1; static int rule = -1;
if (value<0) { static int gamecookie = -1;
if (rule<0 || gamecookie!=global.cookie) {
const char * str = get_param(global.parameters, "alliance.restricted"); const char * str = get_param(global.parameters, "alliance.restricted");
value = 0; gamecookie = global.cookie;
rule = 0;
if (str!=NULL) { if (str!=NULL) {
char * sstr = strdup(str); char * sstr = strdup(str);
char * tok = strtok(sstr, " "); char * tok = strtok(sstr, " ");
while (tok) { while (tok) {
value |= ally_flag(tok, -1); rule |= ally_flag(tok, -1);
tok = strtok(NULL, " "); tok = strtok(NULL, " ");
} }
free(sstr); free(sstr);
} }
value &= HelpMask(); rule &= HelpMask();
} }
return value; return rule;
} }
int int
LongHunger(const struct unit * u) { LongHunger(const struct unit * u) {
static int value = -1; static int gamecookie = -1;
static int rule = -1;
if (u!=NULL) { if (u!=NULL) {
if (!fval(u, UFL_HUNGER)) return false; if (!fval(u, UFL_HUNGER)) return false;
#ifdef NEW_DAEMONHUNGER_RULE #ifdef NEW_DAEMONHUNGER_RULE
if (u->race==new_race[RC_DAEMON]) return false; if (u->race==new_race[RC_DAEMON]) return false;
#endif #endif
} }
if (value<0) { if (rule<0 || gamecookie!=global.cookie) {
value = get_param_int(global.parameters, "hunger.long", 0); gamecookie = global.cookie;
rule = get_param_int(global.parameters, "hunger.long", 0);
} }
return value; return rule;
} }
int int
SkillCap(skill_t sk) { 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 (sk==SK_MAGIC) return 0; /* no caps on magic */
if (value<0) { if (rule<0 || gamecookie!=global.cookie) {
value = get_param_int(global.parameters, "skill.maxlevel", 0); gamecookie = global.cookie;
rule = get_param_int(global.parameters, "skill.maxlevel", 0);
} }
return value; return rule;
} }
int int
NMRTimeout(void) { NMRTimeout(void) {
static int value = -1; static int gamecookie = -1;
if (value<0) { static int rule = -1;
value = get_param_int(global.parameters, "nmr.timeout", 0); if (rule<0 || gamecookie!=global.cookie) {
gamecookie = global.cookie;
rule = get_param_int(global.parameters, "nmr.timeout", 0);
} }
return value; return rule;
} }
race_t race_t

View file

@ -277,16 +277,24 @@ sk_set(skill * sv, int level)
sv->level = (unsigned char)level; sv->level = (unsigned char)level;
} }
static int
rule_random_progress(void) {
return get_param_int(global.parameters, "study.random_progress", 1);
}
int int
skill_weeks(int level) skill_weeks(int level)
/* how many weeks must i study to get from level to level+1 */ /* how many weeks must i study to get from level to level+1 */
{ {
int coins = 2*level; if (rule_random_progress()) {
int heads = 1; int coins = 2*level;
while (coins--) { int heads = 1;
heads += rng_int() % 2; while (coins--) {
} heads += rng_int() % 2;
return heads; }
return heads;
}
return level+1;
} }
void void