Merge branch 'stm2-market_tests' into develop

This commit is contained in:
Enno Rehling 2015-11-26 18:42:34 +01:00
commit cf4bb060b7
13 changed files with 324 additions and 110 deletions

View File

@ -3,7 +3,7 @@ local function get_markets(r, result)
result = result or {}
for b in r.buildings do
if b.type=="market" then
if b.type == "market" and b.working and b.size == b.maxsize then
u = b.owner
if u~=nil then
table.insert(result, u)

View File

@ -59,28 +59,6 @@ function disable_test_bug_1738_build_castle_e3()
assert_equal(c.size, 250)
end
function disable_test_market_action()
local f = faction.create("noreply@eressea.de", "human", "de")
local x, y, r
for x=0,2 do
for y=0,2 do
r = region.create(x, y, "plain")
r.luxury = "balm"
r.herb = "h2"
r:set_resource("peasant", 5000)
end
end
r = get_region(1, 1)
local u = unit.create(f, r, 1)
b = building.create(r, "market")
b.size = 10
u.building = b
update_owners()
process.markets()
assert_equal(35, u:get_item("balm"))
assert_equal(70, u:get_item("h2"))
end
function disable_test_alliance()
local r = region.create(0, 0, "plain")
local f1 = faction.create("noreply@eressea.de", "human", "de")
@ -345,43 +323,204 @@ function test_region_owner_cannot_leave_castle()
assert_equal(b1, u.building, "region owner has left the building") -- region owners may not leave
end
function test_market()
-- if i am the only trader around, i should be getting all the herbs from all 7 regions
function reset_items(u)
for i in u.items do
u:add_item(i, u:get_item(i))
end
u:add_item("money", u.number * 10000)
end
function market_fixture()
local herb_multi = 500 -- from rc_herb_trade()
local r, idx
local herbnames = { 'h0', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'h8' }
idx = 1
local r
local herbnames = { 'h0', 'h1', 'h2', 'h3', 'h4', 'h5' }
local herbtable = {}
for _, name in pairs(herbnames) do
herbtable[name] = name
end
local luxurynames = { 'balm', 'jewel', 'myrrh', 'oil', 'silk', 'incense' } -- no spice in E3
local luxurytable = {}
for _, name in pairs(luxurynames) do
luxurytable[name] = name
end
for x = -1, 1 do for y = -1, 1 do
r = region.create(x, y, "plain")
r:set_resource("peasant", herb_multi * 9 + 50) -- 10 herbs per region
r.herb = herbnames[idx]
idx = idx+1
r:set_resource("peasant", 1)
end end
r = get_region(0, 0)
local b = building.create(r, "market")
b.size = 10
local f = faction.create("noreply@eressea.de", "human", "de")
b.working = true
local f = faction.create("market1@eressea.de", "human", "de")
f.id = 42
local u = unit.create(f, r, 1)
u.building = b
u:add_item("money", u.number * 10000)
for i = 0, 5 do
r.herb = herbnames[6]
r.luxury = luxurynames[6]
r:set_resource("peasant", herb_multi * 9 + 50) -- 10 herbs per region
for i = 0, 4 do
local rn = r:next(i)
rn.name = luxurynames[i+1]
rn.herb = herbnames[i+1]
rn.luxury = luxurynames[i+1]
rn:set_resource("peasant", herb_multi * 9 + 50) -- 10 herbs per region
unit.create(f, rn, 1)
end
b.working = true
eressea.process.markets()
u:add_item("money", -u:get_item("money")) -- now we only have herbs
local len = 0
for i in u.items do
reset_items(u)
return r, u, b, herbnames, luxurynames, herbtable, luxurytable
end
local function test_items(u, names, amount)
local len = 0
for i in u.items do
if names[i] ~= nil then
len = len + 1
end
end
if amount > 0 then
assert_not_equal(0, len, "trader did not get any items")
else
assert_equal(0, len, "trader should not have items")
end
for _, name in pairs(names) do
local n = u:get_item(name)
if n>0 then
if amount == 0 then
assert_equal(0, n, 'trader should have no ' .. name)
else
assert_equal(amount, n, 'trader has ' .. n .. ' instead of ' .. amount .. ' ' .. name)
end
end
end
end
function test_market_regions()
-- if i am the only trader around, i should be getting all the herbs from all 7 regions
local r, u, b, herbnames, luxurynames, herbtable, luxurytable = market_fixture()
eressea.process.markets()
test_items(u, herbtable, 10)
test_items(u, luxurytable, 5)
end
function test_multiple_markets()
local r, u1, b, herbnames, luxurynames, herbtable, luxurytable = market_fixture()
local r2 = get_region(1,0)
local f = faction.create("multim@eressea.de", "human", "de")
local u2 = unit.create(f, r2, 1)
local b2 = building.create(r2, "market")
b2.size = 10
b2.working = true
reset_items(u2)
u2.building = b2
eressea.process.markets()
for _, i in pairs(luxurytable) do
assert_equal(5, u1:get_item(i)+u2:get_item(i), "not enough " .. i )
end
for _, i in pairs(herbtable) do
assert_equal(10, u1:get_item(i)+u2:get_item(i), "not enough " .. i )
end
assert_equal(5, u1:get_item('silk')) -- uncontested
end
function test_market()
local r = region.create(0, 0, "plain")
local f1 = faction.create("market2@eressea.de", "human", "de")
local u1 = unit.create(f1, r, 1)
local b = building.create(r, "market")
eressea.settings.set("rules.peasants.growth", "0")
b.size = 10
u1.building = b
u1:add_item("money", 10000)
r.herb = "h0"
r.luxury = "balm"
r:set_resource("peasant", 1050)
process_orders()
assert_equal(3, u1:get_item("h0"))
assert_equal(2, u1:get_item("balm"))
local function reset_items()
for i in u1.items do
u1:add_item(i, -1 * u1:get_item(i))
end
assert_not_equal(0, len, "trader did not get any herbs")
for idx, name in pairs(herbnames) do
local n = u:get_item(name)
if n>0 then
assert_equal(10, n, 'trader did not get exaxtly 10 herbs')
end
u1:add_item("money", u1.number * 10000)
-- eressea.game.reset()
f1.lastturn=get_turn()
assert_not_equal(nil, factions())
local idx = 0
for f in factions() do
assert_equal(1,1,"fac".. f.email)
idx = idx + 1
end
assert_not_equal(0, idx)
end
reset_items()
b.size = 1
eressea.process.markets()
assert_equal(0, u1:get_item("h0"))
b.size = 10
reset_items()
r:set_resource("peasant", 2100)
eressea.process.markets()
assert_equal(5, u1:get_item("h0"))
assert_equal(3, u1:get_item("balm"))
reset_items()
r:set_resource("peasant", 1049)
eressea.process.markets()
assert_equal(2, u1:get_item("h0"))
assert_equal(1, u1:get_item("balm"))
reset_items()
r:set_resource("peasant", 550)
eressea.process.markets()
assert_equal(2, u1:get_item("h0"))
assert_equal(1, u1:get_item("balm"))
reset_items()
r:set_resource("peasant", 549)
eressea.process.markets()
assert_equal(1, u1:get_item("h0"))
assert_equal(1, u1:get_item("balm"))
reset_items()
r:set_resource("peasant", 50)
eressea.process.markets()
assert_equal(1, u1:get_item("h0"))
assert_equal(1, u1:get_item("balm"))
reset_items()
r:set_resource("peasant", 49)
eressea.process.markets()
assert_equal(0, u1:get_item("h0"))
r:set_resource("peasant", 1050)
reset_items()
u1:add_item("money", -1 * u1:get_item("money"))
assert_equal(0, u1:get_item("money"))
process_orders() -- process_orders to update maintenance
assert_equal(0, u1:get_item("h0"))
process_orders()
eressea.settings.set("rules.peasants.growth", "1")
end
function test_market_gives_items()
@ -393,7 +532,7 @@ function test_market_gives_items()
r = get_region(0, 0)
local b = building.create(r, "market")
b.size = 10
local f = faction.create("noreply@eressea.de", "human", "de")
local f = faction.create("market0@eressea.de", "human", "de")
f.id = 42
local u = unit.create(f, r, 1)
u.building = b
@ -789,3 +928,36 @@ function test_volcanooutbreak_message()
assert_not_equal("", msg:render("de"))
assert_not_equal("", msg:render("en"))
end
function test_bug2083()
local herb_multi = 500 -- from rc_herb_trade()
local r = region.create(0,0,"plain")
r:set_resource("peasant", 2000)
r.luxury = "balm"
local f = faction.create("2083@eressea.de", "human", "de")
local u = unit.create(f, r, 1)
u:set_skill("building", 8)
u:add_item("stone", 100)
u:add_item("log", 100)
u:add_item("iron", 100)
u:add_item("money", 10000)
u:clear_orders()
u:add_order("MACHE Markt")
process_orders()
-- this is a bit weird, but the bug was caused by market code
-- being called in two places. We want to make sure this doesn't happen
for k, v in pairs(rules) do
set_key("xm09", true)
if 'table' == type(v) then
cb = v['update']
if 'function' == type(cb) then
cb()
end
end
end
assert_equal(0, u:get_item("balm"))
end

View File

@ -112,6 +112,13 @@ static int tolua_building_set_name(lua_State * L)
return 0;
}
static int tolua_building_get_maxsize(lua_State * L)
{
building *self = (building *)tolua_tousertype(L, 1, 0);
lua_pushinteger(L, self->type->maxsize);
return 1;
}
static int tolua_building_get_size(lua_State * L)
{
building *self = (building *)tolua_tousertype(L, 1, 0);
@ -247,6 +254,7 @@ void tolua_building_open(lua_State * L)
tolua_variable(L, TOLUA_CAST "units", tolua_building_get_units, NULL);
tolua_variable(L, TOLUA_CAST "region", tolua_building_get_region,
tolua_building_set_region);
tolua_variable(L, TOLUA_CAST "maxsize", tolua_building_get_maxsize, NULL);
tolua_variable(L, TOLUA_CAST "size", tolua_building_get_size,
tolua_building_set_size);
tolua_function(L, TOLUA_CAST "get_typename", tolua_building_get_typename);

View File

@ -2418,13 +2418,11 @@ static void breedtrees(unit * u, int raw)
static void breedhorses(unit * u)
{
int n, c, breed = 0;
struct building *b = inside_building(u);
const struct building_type *btype = building_is_active(b) ? b->type : NULL;
const struct resource_type *rhorse = get_resourcetype(R_HORSE);
int horses, effsk;
assert(rhorse && rhorse->itype);
if (btype != bt_find("stables")) {
if (!active_building(u, bt_find("stables"))) {
cmistake(u, u->thisorder, 122, MSG_PRODUCE);
return;
}

View File

@ -642,10 +642,31 @@ region *building_getregion(const building * b)
return b->region;
}
bool building_is_active(const struct building *b) {
return b && fval(b, BLD_WORKING);
bool
buildingtype_exists(const region * r, const building_type * bt, bool working)
{
building *b;
for (b = rbuildings(r); b; b = b->next) {
if (b->type == bt && b->size >= bt->maxsize && (!working || fval(b, BLD_WORKING)))
return true;
}
return false;
}
bool building_is_active(const struct building *b) {
return b && fval(b, BLD_WORKING) && b->size >= b->type->maxsize;
}
building *active_building(const unit *u, const struct building_type *btype) {
if (u->building && u->building->type == btype && building_is_active(u->building)) {
return inside_building(u);
}
return 0;
}
void building_setregion(building * b, region * r)
{
building **blist = &b->region->buildings;

View File

@ -166,7 +166,10 @@ extern "C" {
extern void building_set_owner(struct unit * u);
extern void building_update_owner(struct building * bld);
bool buildingtype_exists(const struct region *r,
const struct building_type *bt, bool working);
bool building_is_active(const struct building *b);
struct building *active_building(const struct unit *u, const struct building_type *btype);
#ifdef WDW_PYRAMID
extern int wdw_pyramid_level(const struct building *b);

View File

@ -6,10 +6,13 @@
#include <kernel/building.h>
#include <kernel/unit.h>
#include <util/language.h>
#include <CuTest.h>
#include <tests.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
static void test_register_building(CuTest * tc)
{
@ -382,16 +385,76 @@ static void test_btype_defaults(CuTest *tc) {
test_cleanup();
}
static void test_active_building(CuTest *tc) {
static void test_buildingtype_exists(CuTest * tc)
{
region *r;
building *b;
building_type *btype, *btype2;
test_cleanup();
b = test_create_building(test_create_region(0, 0, 0), 0);
test_create_world();
btype2 = bt_get_or_create("castle");
assert(btype2);
btype = test_create_buildingtype("Hodor");
btype->maxsize = 10;
r = findregion(-1, 0);
b = new_building(btype, r, default_locale);
b->size = 10;
CuAssertPtrNotNull(tc, b);
CuAssertTrue(tc, !buildingtype_exists(r, NULL, false));
CuAssertTrue(tc, !buildingtype_exists(r, btype2, false));
CuAssertTrue(tc, buildingtype_exists(r, btype, false));
b->size = 9;
fset(b, BLD_WORKING);
CuAssertTrue(tc, !buildingtype_exists(r, btype, false));
btype->maxsize = 0;
freset(b, BLD_WORKING);
CuAssertTrue(tc, buildingtype_exists(r, btype, false));
btype->maxsize = 10;
b->size = 10;
fset(b, BLD_WORKING);
CuAssertTrue(tc, buildingtype_exists(r, btype, true));
freset(b, BLD_WORKING);
CuAssertTrue(tc, !buildingtype_exists(r, btype, true));
}
static void test_active_building(CuTest *tc) {
building *b;
region *r;
unit *u;
building_type *btype;
test_cleanup();
btype = test_create_buildingtype("castle");
assert(btype && btype->maxsize == -1);
b = test_create_building(r = test_create_region(0, 0, 0), btype);
u = test_create_unit(test_create_faction(0), r);
CuAssertIntEquals(tc, false, building_is_active(b));
CuAssertPtrEquals(tc, NULL, active_building(u, btype));
b->flags |= BLD_WORKING;
CuAssertIntEquals(tc, true, building_is_active(b));
CuAssertPtrEquals(tc, NULL, active_building(u, btype));
u_set_building(u, b);
CuAssertIntEquals(tc, true, building_is_active(b));
CuAssertPtrNotNull(tc, active_building(u, btype) );
btype->maxsize = 10;
b->size = btype->maxsize;
CuAssertIntEquals(tc, true, building_is_active(b));
CuAssertPtrNotNull(tc, active_building(u, btype) );
b->size = 9;
CuAssertIntEquals(tc, false, building_is_active(b));
CuAssertPtrEquals(tc, NULL, active_building(u, btype));
btype->maxsize = -1;
b->flags &= ~BLD_WORKING;
CuAssertIntEquals(tc, false, building_is_active(b));
CuAssertPtrEquals(tc, NULL, active_building(u, btype));
test_cleanup();
}
@ -432,6 +495,7 @@ CuSuite *get_building_suite(void)
SUITE_ADD_TEST(suite, test_buildingowner_goes_to_same_faction_after_leave);
SUITE_ADD_TEST(suite, test_buildingowner_goes_to_empty_unit_after_leave);
SUITE_ADD_TEST(suite, test_active_building);
SUITE_ADD_TEST(suite, test_buildingtype_exists);
SUITE_ADD_TEST(suite, test_safe_building);
return suite;
}

View File

@ -3531,9 +3531,7 @@ void monthly_healing(void)
if (u->hp < umhp) {
double maxheal = _max(u->number, umhp / 20.0);
int addhp;
struct building *b = inside_building(u);
const struct building_type *btype = building_is_active(b) ? b->type : NULL;
if (btype == bt_find("inn")) {
if (active_building(u, bt_find("inn"))) {
p *= 1.5;
}
/* pro punkt 5% höher */

View File

@ -36,9 +36,9 @@ static unsigned int get_markets(region * r, unit ** results, size_t size)
if (!btype)
return 0;
for (b = r->buildings; n < size && b; b = b->next) {
if (b->type == btype && (b->flags & BLD_WORKING)
&& b->size >= b->type->maxsize) {
if (b->type == btype && building_is_active(b)) {
unit *u = building_owner(b);
/* I decided to omit check for inside_building(u) */
unsigned int i;
for (i = 0; u && i != n; ++i) {
/* only one market per faction */

View File

@ -1685,21 +1685,7 @@ unit *owner_buildingtyp(const region * r, const building_type * bt)
return NULL;
}
bool
buildingtype_exists(const region * r, const building_type * bt, bool working)
{
building *b;
for (b = rbuildings(r); b; b = b->next) {
if (b->type == bt && b->size >= bt->maxsize && (!working || fval(b, BLD_WORKING)))
return true;
}
return false;
}
/* Prüft, ob Ablegen von einer Küste in eine der erlaubten Richtungen erfolgt. */
bool can_takeoff(const ship * sh, const region * from, const region * to)
{
if (!fval(from->terrain, SEA_REGION) && sh->coast != NODIRECTION) {

View File

@ -69,8 +69,6 @@ extern "C" {
struct region *to, struct region_list *route);
int walkingcapacity(const struct unit *u);
void follow_unit(struct unit *u);
bool buildingtype_exists(const struct region *r,
const struct building_type *bt, bool working);
struct unit *owner_buildingtyp(const struct region *r,
const struct building_type *bt);
bool move_blocked(const struct unit *u, const struct region *src,

View File

@ -158,27 +158,6 @@ static void test_ship_has_harbormaster_ally(CuTest * tc) {
test_cleanup();
}
static void test_building_type_exists(CuTest * tc)
{
region *r;
building *b;
building_type *btype, *btype2;
test_cleanup();
test_create_world();
btype2 = bt_get_or_create("lighthouse");
btype = bt_get_or_create("castle");
r = findregion(-1, 0);
b = new_building(btype, r, default_locale);
CuAssertPtrNotNull(tc, b);
CuAssertTrue(tc, !buildingtype_exists(r, NULL, false));
CuAssertTrue(tc, buildingtype_exists(r, btype, false));
CuAssertTrue(tc, !buildingtype_exists(r, btype2, false));
}
static void test_walkingcapacity(CuTest *tc) {
region *r;
unit *u;
@ -367,7 +346,6 @@ CuSuite *get_move_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_walkingcapacity);
SUITE_ADD_TEST(suite, test_building_type_exists);
SUITE_ADD_TEST(suite, test_ship_not_allowed_in_coast);
SUITE_ADD_TEST(suite, test_ship_allowed_without_harbormaster);
SUITE_ADD_TEST(suite, test_ship_blocked_by_harbormaster);

View File

@ -176,13 +176,6 @@ static int study_days(unit * student, skill_t sk)
return student->number * speed;
}
static building *active_building(const unit *u, const struct building_type *btype) {
if (u->building && u->building->type == btype && building_is_active(u->building)) {
return inside_building(u);
}
return 0;
}
static int
teach_unit(unit * teacher, unit * student, int nteaching, skill_t sk,
bool report, int *academy)
@ -547,8 +540,6 @@ 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);
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 (!unit_can_study(u)) {
@ -601,10 +592,7 @@ int study_cmd(unit * u, order * ord)
return 0;
}
/* Akademie: */
b = inside_building(u);
btype = building_is_active(b) ? b->type : NULL;
if (btype && btype == bt_find("academy")) {
if (active_building(u, bt_find("academy"))) {
studycost = _max(50, studycost * 2);
}