From d056f6049e95a1ce459f748e1e22aea7a59edf08 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 2 Aug 2012 08:08:39 +0200 Subject: [PATCH 1/8] fix an intermittently segfaulting test --- src/kernel/region.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/kernel/region.c b/src/kernel/region.c index c2d5e6587..491efa290 100644 --- a/src/kernel/region.c +++ b/src/kernel/region.c @@ -1604,7 +1604,7 @@ int owner_change(const region * r) if (r->land && r->land->ownership) { return r->land->ownership->since_turn; } - return -1; + return INT_MIN; } bool is_mourning(const region * r, int in_turn) From 65e5a8250466f1792468006d87aedfd474b19560 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 1 Aug 2012 23:12:23 -0700 Subject: [PATCH 2/8] we can now compare regions, do not need to compare ids --- scripts/tests/common.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/tests/common.lua b/scripts/tests/common.lua index e6b3b8f4a..84d614b1b 100755 --- a/scripts/tests/common.lua +++ b/scripts/tests/common.lua @@ -329,7 +329,7 @@ function test_guard() u2:add_item("money", 100) u3:add_item("money", 100) process_orders() - assert_equal(r.id, u1.region.id, "unit may not move after combat") + assert_equal(r, u1.region, "unit may not move after combat") end function test_recruit() From 166d4ccc2cc187b15bcd40f7449fbfc98162f7af Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 1 Aug 2012 23:20:59 -0700 Subject: [PATCH 3/8] make the fighter in this test not die --- scripts/tests/common.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/tests/common.lua b/scripts/tests/common.lua index 84d614b1b..287736c66 100755 --- a/scripts/tests/common.lua +++ b/scripts/tests/common.lua @@ -309,12 +309,13 @@ function test_guard() local r = region.create(0, 0, "plain") local f1 = faction.create("noreply@eressea.de", "human", "de") f1.age = 20 - local u1 = unit.create(f1, r, 1) + local u1 = unit.create(f1, r, 10) u1:add_item("sword", 10) u1:add_item("money", 10) u1:set_skill("melee", 10) u1:clear_orders() u1:add_order("NACH O") + u1.name="Kalle Pimp" local f2 = faction.create("noreply@eressea.de", "human", "de") f2.age = 20 From 1fe075e82d4f84c4e198389b4e85304e10557570 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 2 Aug 2012 00:06:03 -0700 Subject: [PATCH 4/8] laen test does not work. trying to make setresource add the raw material to fix it --- scripts/tests/common.lua | 21 ++++++--------------- src/kernel/region.c | 10 ++++++++++ 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/scripts/tests/common.lua b/scripts/tests/common.lua index 287736c66..f8f546e05 100755 --- a/scripts/tests/common.lua +++ b/scripts/tests/common.lua @@ -570,20 +570,11 @@ function test_config() end local function _test_create_laen() - local r = region.create(0,0, "mountain") - local f1 = faction.create("noreply@eressea.de", "human", "de") - local u1 = unit.create(f1, r, 1) - - -- TODO this is a stupid way to create a laen region - for i = 1, 10000 do - r = region.create(i,0, "mountain") - if r:get_resource("laen") > 2 then - break - end - end - assert(r:get_resource("laen")>2, "could not run test properly, please try again") - - return r, u1 + local r = region.create(0,0, "mountain") + local f1 = faction.create("noreply@eressea.de", "human", "de") + local u1 = unit.create(f1, r, 1) + r:set_resource("laen", 3) + return r, u1 end function test_laen1() @@ -611,7 +602,7 @@ function test_laen2() u1.building = b local laen = r:get_resource("laen") - process_orders() + process_orders() assert_equal(2, u1:get_item("laen")) assert_equal(laen - 2, r:get_resource("laen")) end diff --git a/src/kernel/region.c b/src/kernel/region.c index 491efa290..e22e5ef62 100644 --- a/src/kernel/region.c +++ b/src/kernel/region.c @@ -1016,6 +1016,16 @@ void region_setresource(region * r, const resource_type * rtype, int value) rsetpeasants(r, value); else if (rtype == rt_find("horse")) rsethorses(r, value); + else { + int i; + for (i = 0; r->terrain->production[i].type; ++i) { + const terrain_production *production = r->terrain->production + i; + if (production->type==rtype) { + add_resource(r, 1, value, dice_rand(production->divisor), rtype); + break; + } + } + } } } From cde0ce07424e19db30c2d61ce6356cf58fa12ba8 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 3 Aug 2012 00:41:17 -0700 Subject: [PATCH 5/8] fix broken laen tests by hacking in another config setting --- scripts/tests/common.lua | 12 ++++++++---- src/kernel/resources.c | 11 ++++++++--- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/scripts/tests/common.lua b/scripts/tests/common.lua index f8f546e05..0521e66e4 100755 --- a/scripts/tests/common.lua +++ b/scripts/tests/common.lua @@ -570,10 +570,11 @@ function test_config() end local function _test_create_laen() + eressea.settings.set("rules.terraform.all", "1") local r = region.create(0,0, "mountain") local f1 = faction.create("noreply@eressea.de", "human", "de") local u1 = unit.create(f1, r, 1) - r:set_resource("laen", 3) + r:set_resource("laen", 50) return r, u1 end @@ -596,15 +597,18 @@ function test_laen2() u1:set_skill("mining", 15) u1:clear_orders() u1:add_order("MACHEN Laen") - + u1.name = "Laenmeister" + local b = building.create(r, "mine") b.size = 10 u1.building = b local laen = r:get_resource("laen") - process_orders() - assert_equal(2, u1:get_item("laen")) + process_orders() + init_reports() + write_report(u1.faction) assert_equal(laen - 2, r:get_resource("laen")) + assert_equal(2, u1:get_item("laen")) end function test_mine() diff --git a/src/kernel/resources.c b/src/kernel/resources.c index 4a3607d7b..5f3c14757 100644 --- a/src/kernel/resources.c +++ b/src/kernel/resources.c @@ -82,6 +82,10 @@ void terraform_resources(region * r) { int i; 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); + } if (terrain->production == NULL) return; @@ -94,10 +98,11 @@ void terraform_resources(region * r) if (rm->type->rtype == rtype) break; } - if (rm) + if (rm) { continue; - - if (chance(production->chance)) { + } + + if (terraform_all || chance(production->chance)) { add_resource(r, dice_rand(production->startlevel), dice_rand(production->base), dice_rand(production->divisor), production->type); From 5e58f3a881737f625c45e2cf0979ae4db431d752 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 21 Oct 2012 18:37:51 -0700 Subject: [PATCH 6/8] start adding test coverage for res_changeitem --- src/kernel/item.c | 4 ++++ src/kernel/item_test.c | 25 +++++++++++++++++++++++++ src/tests.c | 8 ++++---- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/kernel/item.c b/src/kernel/item.c index fd35f0776..2fd76ded3 100644 --- a/src/kernel/item.c +++ b/src/kernel/item.c @@ -1226,6 +1226,10 @@ void test_clear_resources(void) void register_resources(void) { + static bool registered = false; + if (registered) return; + registered = true; + register_function((pf_generic) mod_elves_only, "mod_elves_only"); register_function((pf_generic) mod_dwarves_only, "mod_dwarves_only"); register_function((pf_generic) res_changeitem, "changeitem"); diff --git a/src/kernel/item_test.c b/src/kernel/item_test.c index c59e47d33..6279bc0af 100644 --- a/src/kernel/item_test.c +++ b/src/kernel/item_test.c @@ -2,11 +2,35 @@ #include #include +#include #include +#include #include #include +void test_change_item(CuTest * tc) +{ + rtype_uchange res_changeitem; + const resource_type * rtype; + unit * u; + + register_resources(); + res_changeitem = (rtype_uchange)get_function("changeitem"); + CuAssertPtrNotNull(tc, res_changeitem); + + test_cleanup(); + test_create_world(); + rtype = olditemtype[I_IRON]->rtype; + + u = test_create_unit(0, 0); + CuAssertIntEquals(tc, 0, res_changeitem(u, rtype, 0)); + i_change(&u->items, rtype->itype, 4); + CuAssertIntEquals(tc, 4, res_changeitem(u, rtype, 0)); + CuAssertIntEquals(tc, 1, res_changeitem(u, rtype, -3)); + CuAssertIntEquals(tc, 1, i_get(u->items, rtype->itype)); +} + void test_resource_type(CuTest * tc) { struct item_type *itype; @@ -68,6 +92,7 @@ void test_findresourcetype(CuTest * tc) CuSuite *get_item_suite(void) { CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_change_item); SUITE_ADD_TEST(suite, test_resource_type); SUITE_ADD_TEST(suite, test_finditemtype); SUITE_ADD_TEST(suite, test_findresourcetype); diff --git a/src/tests.c b/src/tests.c index dd05c7d93..568e65ac4 100644 --- a/src/tests.c +++ b/src/tests.c @@ -180,15 +180,15 @@ void test_create_world(void) terrain_type *t_plain, *t_ocean; region *island[2]; int i; - item_type * itype; - const char * names[] = { "horse", "horse_p", "boat", "boat_p" }; + const char * names[] = { "horse", "horse_p", "boat", "boat_p", "iron", "iron_p", "stone", "stone_p" }; make_locale("de"); init_resources(); assert(!olditemtype[I_HORSE]); - itype = test_create_itemtype(names); - olditemtype[I_HORSE] = itype; + olditemtype[I_HORSE] = test_create_itemtype(names+0); + olditemtype[I_IRON] = test_create_itemtype(names+4); + olditemtype[I_STONE] = test_create_itemtype(names+6); t_plain = test_create_terrain("plain", LAND_REGION | FOREST_REGION | WALK_INTO | CAVALRY_REGION); t_plain->size = 1000; From 79feb5723c075a45a1a94ed286c87993314ce8da Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 21 Oct 2012 19:42:59 -0700 Subject: [PATCH 7/8] also test changing persons --- src/kernel/item.c | 6 +++++- src/kernel/item_test.c | 42 ++++++++++++++++++++++++++++++------------ 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/src/kernel/item.c b/src/kernel/item.c index 2fd76ded3..db1da94ad 100644 --- a/src/kernel/item.c +++ b/src/kernel/item.c @@ -73,7 +73,11 @@ static int res_changeaura(unit * u, const resource_type * rtype, int delta) static int res_changeperson(unit * u, const resource_type * rtype, int delta) { assert(rtype != NULL || !"not implemented"); - scale_number(u, u->number + delta); + if (u->number + delta >=0) { + scale_number(u, u->number + delta); + } else { + scale_number(u, 0); + } return u->number; } diff --git a/src/kernel/item_test.c b/src/kernel/item_test.c index 6279bc0af..c9a144ede 100644 --- a/src/kernel/item_test.c +++ b/src/kernel/item_test.c @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -9,26 +10,42 @@ #include #include +static void test_uchange(CuTest * tc, unit * u, const resource_type * rtype) { + int n; + change_resource(u, rtype, 4); + n = get_resource(u, rtype); + CuAssertPtrNotNull(tc, rtype->uchange); + CuAssertIntEquals(tc, n, rtype->uchange(u, rtype, 0)); + CuAssertIntEquals(tc, n-3, rtype->uchange(u, rtype, -3)); + CuAssertIntEquals(tc, n-3, get_resource(u, rtype)); + CuAssertIntEquals(tc, 0, rtype->uchange(u, rtype, -n)); +} + void test_change_item(CuTest * tc) { - rtype_uchange res_changeitem; - const resource_type * rtype; unit * u; - register_resources(); - res_changeitem = (rtype_uchange)get_function("changeitem"); - CuAssertPtrNotNull(tc, res_changeitem); - test_cleanup(); + register_resources(); + init_resources(); test_create_world(); - rtype = olditemtype[I_IRON]->rtype; u = test_create_unit(0, 0); - CuAssertIntEquals(tc, 0, res_changeitem(u, rtype, 0)); - i_change(&u->items, rtype->itype, 4); - CuAssertIntEquals(tc, 4, res_changeitem(u, rtype, 0)); - CuAssertIntEquals(tc, 1, res_changeitem(u, rtype, -3)); - CuAssertIntEquals(tc, 1, i_get(u->items, rtype->itype)); + test_uchange(tc, u, olditemtype[I_IRON]->rtype); +} + +void test_change_person(CuTest * tc) +{ + unit * u; + + test_cleanup(); + + register_resources(); + init_resources(); + test_create_world(); + + u = test_create_unit(0, 0); + test_uchange(tc, u, r_unit); } void test_resource_type(CuTest * tc) @@ -93,6 +110,7 @@ CuSuite *get_item_suite(void) { CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_change_item); + SUITE_ADD_TEST(suite, test_change_person); SUITE_ADD_TEST(suite, test_resource_type); SUITE_ADD_TEST(suite, test_finditemtype); SUITE_ADD_TEST(suite, test_findresourcetype); From 841876e3359e49d059704ee7b5aa7a138447f871 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 23 Feb 2013 17:23:47 -0800 Subject: [PATCH 8/8] bind faction.flags to Lua --- scripts/tests/common.lua | 7 +++++++ src/bindings/bind_faction.c | 10 +++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/scripts/tests/common.lua b/scripts/tests/common.lua index 0521e66e4..88dae3080 100755 --- a/scripts/tests/common.lua +++ b/scripts/tests/common.lua @@ -1330,3 +1330,10 @@ function test_bug_1795_demons() assert_equal(limit+1, u1.number, u1.number .. "!=" .. (limit+1)) assert_equal(peasants+growth, r:get_resource("peasant")) end + +function test_faction_flags() + f = faction.create("noreply@eressea.de", "human", "de") + assert_equal(0, f.flags) + f.flags = 42 + assert_equal(42, f.flags) +end diff --git a/src/bindings/bind_faction.c b/src/bindings/bind_faction.c index d5efd226c..775cb5ab8 100644 --- a/src/bindings/bind_faction.c +++ b/src/bindings/bind_faction.c @@ -160,6 +160,14 @@ static int tolua_faction_get_flags(lua_State * L) return 1; } +static int tolua_faction_set_flags(lua_State * L) +{ + faction *self = (faction *) tolua_tousertype(L, 1, 0); + int flags = (int)tolua_tonumber(L, 2, self->flags); + self->flags = flags; + return 1; +} + static int tolua_faction_get_options(lua_State * L) { faction *self = (faction *) tolua_tousertype(L, 1, 0); @@ -525,7 +533,7 @@ void tolua_faction_open(lua_State * L) tolua_faction_set_age); tolua_variable(L, TOLUA_CAST "options", tolua_faction_get_options, tolua_faction_set_options); - tolua_variable(L, TOLUA_CAST "flags", tolua_faction_get_flags, NULL); + tolua_variable(L, TOLUA_CAST "flags", tolua_faction_get_flags, tolua_faction_set_flags); tolua_variable(L, TOLUA_CAST "lastturn", tolua_faction_get_lastturn, tolua_faction_set_lastturn);