forked from github/server
BUG 2389: insects starve in glaciers.
add feature tests for hunger.
This commit is contained in:
parent
efb78ea8db
commit
3685bcabfb
7 changed files with 158 additions and 20 deletions
|
@ -48,8 +48,7 @@
|
|||
"healing.forest": 2.0,
|
||||
"hunger.long": false,
|
||||
"hunger.damage": "1d9+9",
|
||||
"hunger.demons.skill": true,
|
||||
"hunger.demons.peasant_tolerance": true,
|
||||
"hunger.demon.peasant_tolerance": true,
|
||||
"init_spells": 0,
|
||||
"recruit.allow_merge": true,
|
||||
"study.expensivemigrants": true,
|
||||
|
|
|
@ -71,13 +71,3 @@ function test_recruit_in_desert()
|
|||
assert_equal('winter', get_season(get_turn()))
|
||||
assert_equal(2, u.number)
|
||||
end
|
||||
|
||||
function bug_1841_test_hunger_in_glacier()
|
||||
local r = region.create(0, 0, "glacier")
|
||||
local f = faction.create("insect", "insect@eressea.de", "de")
|
||||
local u = unit.create(f, r, 1)
|
||||
|
||||
local flags = u.flags
|
||||
process_orders()
|
||||
assert_equal(flags+2048, u.flags)
|
||||
end
|
||||
|
|
108
scripts/tests/hunger.lua
Normal file
108
scripts/tests/hunger.lua
Normal file
|
@ -0,0 +1,108 @@
|
|||
require "lunit"
|
||||
|
||||
module("tests.hunger", package.seeall, lunit.testcase)
|
||||
|
||||
function setup()
|
||||
eressea.free_game()
|
||||
conf = [[{
|
||||
"races": {
|
||||
"demon": {
|
||||
"maintenance": 10,
|
||||
"hp" : 50
|
||||
},
|
||||
"insect": {
|
||||
"maintenance": 10,
|
||||
"hp" : 50
|
||||
}
|
||||
},
|
||||
"terrains" : {
|
||||
"plain": { "flags" : [ "land", "walk" ] },
|
||||
"glacier": { "flags" : [ "arctic", "land", "walk" ] }
|
||||
}
|
||||
}]]
|
||||
|
||||
eressea.config.reset()
|
||||
eressea.config.parse(conf)
|
||||
|
||||
eressea.settings.set("rules.peasants.growth.factor", "0")
|
||||
eressea.settings.set("rules.peasantluck.growth.factor", "0")
|
||||
eressea.settings.set("hunger.damage", "10")
|
||||
end
|
||||
|
||||
function test_maintenance()
|
||||
local r = region.create(0, 0, "plain")
|
||||
local f = faction.create("insect")
|
||||
local u = unit.create(f, r, 2)
|
||||
local flags = u.flags
|
||||
u:add_item('money', 100)
|
||||
|
||||
process_orders()
|
||||
assert_equal(flags, u.flags, "should not be hungry")
|
||||
assert_equal(80, u:get_item('money'), "should pay maintenance")
|
||||
assert_equal(100, u.hp, "should not take damage")
|
||||
end
|
||||
|
||||
function test_demons_eat_peasants()
|
||||
local r = region.create(0, 0, "plain")
|
||||
local f = faction.create("demon")
|
||||
local u = unit.create(f, r, 10)
|
||||
local flags = u.flags
|
||||
u:add_item('money', 120)
|
||||
|
||||
r.peasants = 3
|
||||
process_orders()
|
||||
assert_equal(20, u:get_item('money'))
|
||||
assert_equal(2, r.peasants)
|
||||
assert_equal(flags, u.flags)
|
||||
assert_equal(500, u.hp)
|
||||
end
|
||||
|
||||
function test_demons_need_peasants()
|
||||
local r = region.create(0, 0, "plain")
|
||||
local f = faction.create("demon")
|
||||
local u = unit.create(f, r, 1)
|
||||
local flags = u.flags
|
||||
u:add_item('money', 100)
|
||||
|
||||
eressea.settings.set("hunger.demon.peasant_tolerance", "0")
|
||||
r.peasants = 0
|
||||
process_orders()
|
||||
assert_equal(90, u:get_item('money')) -- use money even if no peasants
|
||||
assert_equal(flags+2048, u.flags)
|
||||
assert_equal(40, u.hp)
|
||||
|
||||
eressea.settings.set("hunger.demon.peasant_tolerance", "1")
|
||||
u.flags = flags
|
||||
u.hp = 50
|
||||
process_orders()
|
||||
assert_equal(80, u:get_item('money')) -- use money even if no peasants
|
||||
assert_equal(flags+2048, u.flags)
|
||||
assert_equal(50, u.hp)
|
||||
assert_equal(0, r.peasants)
|
||||
end
|
||||
|
||||
function test_insects_hunger_in_glacier()
|
||||
-- bug 2389
|
||||
local r = region.create(0, 0, "plain")
|
||||
local f = faction.create("insect")
|
||||
local u = unit.create(f, r, 1)
|
||||
local flags = u.flags
|
||||
u:add_item('money', 1000)
|
||||
|
||||
-- eressea.settings.set("hunger.insect.cold", "1") -- default
|
||||
process_orders()
|
||||
assert_equal(flags, u.flags)
|
||||
assert_equal(50, u.hp)
|
||||
|
||||
r.terrain = 'glacier'
|
||||
process_orders()
|
||||
assert_equal(flags+2048, u.flags)
|
||||
assert_equal(40, u.hp)
|
||||
|
||||
u.flags = u.flags-2048
|
||||
u.hp = 50
|
||||
eressea.settings.set("hunger.insect.cold", "0")
|
||||
process_orders()
|
||||
assert_equal(flags, u.flags)
|
||||
assert_equal(50, u.hp)
|
||||
end
|
|
@ -9,3 +9,4 @@ require 'tests.settings'
|
|||
require 'tests.study'
|
||||
require 'tests.laws'
|
||||
require 'tests.bindings'
|
||||
require 'tests.hunger'
|
||||
|
|
|
@ -536,6 +536,27 @@ static int tolua_region_get_age(lua_State * L)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int tolua_region_get_peasants(lua_State * L)
|
||||
{
|
||||
region *self = (region *)tolua_tousertype(L, 1, 0);
|
||||
|
||||
if (self) {
|
||||
lua_pushinteger(L, self->land ? self->land->peasants : 0);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int tolua_region_set_peasants(lua_State * L)
|
||||
{
|
||||
region *self = (region *)tolua_tousertype(L, 1, 0);
|
||||
|
||||
if (self && self->land) {
|
||||
self->land->peasants = lua_tointeger(L, 2);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int tolua_region_getkey(lua_State * L)
|
||||
{
|
||||
region *self = (region *)tolua_tousertype(L, 1, 0);
|
||||
|
@ -734,6 +755,8 @@ void tolua_region_open(lua_State * L)
|
|||
tolua_variable(L, TOLUA_CAST "age", tolua_region_get_age, NULL);
|
||||
tolua_variable(L, TOLUA_CAST "buildings", tolua_region_get_buildings,
|
||||
NULL);
|
||||
tolua_variable(L, TOLUA_CAST "peasants", tolua_region_get_peasants,
|
||||
tolua_region_set_peasants);
|
||||
tolua_variable(L, TOLUA_CAST "terrain", tolua_region_get_terrain,
|
||||
tolua_region_set_terrain);
|
||||
tolua_function(L, TOLUA_CAST "get_resourcelevel",
|
||||
|
|
|
@ -1607,7 +1607,7 @@ static void test_demon_hunger(CuTest * tc)
|
|||
u = test_create_unit(f, r);
|
||||
u->hp = 999;
|
||||
|
||||
config_set("hunger.demons.peasant_tolerance", "1");
|
||||
config_set("hunger.demon.peasant_tolerance", "1");
|
||||
|
||||
rtype = get_resourcetype(R_SILVER);
|
||||
i_change(&u->items, rtype->itype, 30);
|
||||
|
@ -1619,7 +1619,7 @@ static void test_demon_hunger(CuTest * tc)
|
|||
CuAssertIntEquals(tc, 20, i_get(u->items, rtype->itype));
|
||||
CuAssertPtrEquals(tc, 0, test_find_messagetype(f->msgs, "malnourish"));
|
||||
|
||||
config_set("hunger.demons.peasant_tolerance", "0");
|
||||
config_set("hunger.demon.peasant_tolerance", "0");
|
||||
|
||||
get_food(r);
|
||||
|
||||
|
|
29
src/upkeep.c
29
src/upkeep.c
|
@ -109,16 +109,27 @@ void get_food(region * r)
|
|||
plane *pl = rplane(r);
|
||||
unit *u;
|
||||
int peasantfood = rpeasants(r) * 10;
|
||||
int food_rules = config_get_int("rules.food.flags", 0);
|
||||
static const race *rc_demon;
|
||||
static int rc_cache;
|
||||
|
||||
static const race *rc_demon, *rc_insect;
|
||||
static int rc_cache, config_cache;
|
||||
static int food_rules;
|
||||
static bool insect_hunger;
|
||||
static bool demon_hunger;
|
||||
bool is_cold;
|
||||
|
||||
if (rc_changed(&rc_cache)) {
|
||||
rc_demon = get_race(RC_DAEMON);
|
||||
rc_insect = get_race(RC_INSECT);
|
||||
}
|
||||
if (config_changed(&config_cache)) {
|
||||
food_rules = config_get_int("rules.food.flags", 0);
|
||||
insect_hunger = config_get_int("hunger.insect.cold", 1) != 0;
|
||||
demon_hunger = config_get_int("hunger.demon.peasant_tolerance", 0) == 0;
|
||||
}
|
||||
if (food_rules & FOOD_IS_FREE) {
|
||||
return;
|
||||
}
|
||||
is_cold = insect_hunger && r_insectstalled(r);
|
||||
|
||||
/* 1. Versorgung von eigenen Einheiten. Das vorhandene Silber
|
||||
* wird zun<EFBFBD>chst so auf die Einheiten aufgeteilt, dass idealerweise
|
||||
* jede Einheit genug Silber f<EFBFBD>r ihren Unterhalt hat. */
|
||||
|
@ -227,7 +238,8 @@ void get_food(region * r)
|
|||
* bei fehlenden Bauern den D<EFBFBD>mon hungern lassen
|
||||
*/
|
||||
for (u = r->units; u; u = u->next) {
|
||||
if (u_race(u) == rc_demon) {
|
||||
const race * rc = u_race(u);
|
||||
if (rc == rc_demon) {
|
||||
int hungry = u->number;
|
||||
|
||||
/* use peasantblood before eating the peasants themselves */
|
||||
|
@ -271,7 +283,6 @@ void get_food(region * r)
|
|||
peasantfood = 0;
|
||||
}
|
||||
if (hungry > 0) {
|
||||
bool demon_hunger = config_get_int("hunger.demons.peasant_tolerance", 0) == 0;
|
||||
if (demon_hunger) {
|
||||
/* demons who don't feed are hungry */
|
||||
if (hunger(hungry, u))
|
||||
|
@ -284,6 +295,12 @@ void get_food(region * r)
|
|||
}
|
||||
}
|
||||
}
|
||||
else if (is_cold && rc == rc_insect) {
|
||||
/* insects in glaciers get hunger damage */
|
||||
if (hunger(u->number, u)) {
|
||||
fset(u, UFL_HUNGER);
|
||||
}
|
||||
}
|
||||
}
|
||||
rsetpeasants(r, peasantfood / 10);
|
||||
|
||||
|
|
Loading…
Reference in a new issue