diff --git a/scripts/tests/common.lua b/scripts/tests/common.lua index e6b3b8f4a..88dae3080 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 @@ -329,7 +330,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() @@ -569,20 +570,12 @@ 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 + 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", 50) + return r, u1 end function test_laen1() @@ -604,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")) + 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() @@ -1334,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); diff --git a/src/kernel/item.c b/src/kernel/item.c index fd35f0776..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; } @@ -1226,6 +1230,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..c9a144ede 100644 --- a/src/kernel/item_test.c +++ b/src/kernel/item_test.c @@ -2,11 +2,52 @@ #include #include +#include +#include #include +#include #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) +{ + unit * u; + + test_cleanup(); + register_resources(); + init_resources(); + test_create_world(); + + u = test_create_unit(0, 0); + 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) { struct item_type *itype; @@ -68,6 +109,8 @@ 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_change_person); SUITE_ADD_TEST(suite, test_resource_type); SUITE_ADD_TEST(suite, test_finditemtype); SUITE_ADD_TEST(suite, test_findresourcetype); diff --git a/src/kernel/region.c b/src/kernel/region.c index c2d5e6587..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; + } + } + } } } @@ -1604,7 +1614,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) 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); 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;