From b0baba4405890cbb5678fa099b74aaabd18d5a35 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 2 Mar 2017 19:21:11 +0100 Subject: [PATCH] bug 2290: sawmill bonus was broken. not pleased with the test, which is in lua only. https://bugs.eressea.de/view.php?id=2290 --- scripts/tests/e2/init.lua | 3 +-- scripts/tests/e3/init.lua | 1 + scripts/tests/economy.lua | 14 ++++++++++++++ src/economy.c | 4 ++-- src/economy.test.c | 6 ++++++ 5 files changed, 24 insertions(+), 4 deletions(-) diff --git a/scripts/tests/e2/init.lua b/scripts/tests/e2/init.lua index afaac7209..25e21f3e5 100644 --- a/scripts/tests/e2/init.lua +++ b/scripts/tests/e2/init.lua @@ -1,5 +1,3 @@ -require 'tests.economy' - require 'tests.e2.undead' require 'tests.e2.shiplanding' require 'tests.e2.e2features' @@ -8,6 +6,7 @@ require 'tests.e2.destroy' require 'tests.e2.guard' require 'tests.e2.spells' require 'tests.e2.stealth' +require 'tests.economy' require 'tests.orders' require 'tests.common' require 'tests.report' diff --git a/scripts/tests/e3/init.lua b/scripts/tests/e3/init.lua index 4655fdcef..4501e35eb 100644 --- a/scripts/tests/e3/init.lua +++ b/scripts/tests/e3/init.lua @@ -4,6 +4,7 @@ require 'tests.e3.spells' require 'tests.e3.rules' require 'tests.e3.parser' require 'tests.e3.morale' +require 'tests.economy' require 'tests.orders' require 'tests.common' -- require 'tests.report' diff --git a/scripts/tests/economy.lua b/scripts/tests/economy.lua index b628dd47b..fe1574887 100644 --- a/scripts/tests/economy.lua +++ b/scripts/tests/economy.lua @@ -127,6 +127,20 @@ function test_ironkeeper_guards_iron() assert_equal(level, u:get_item("iron")) end +function test_sawmill() + local r = region.create(0, 0, "plain") + r:set_resource("tree", 100) + local u = unit.create(faction.create("human"), r) + u:add_item("money", 250) -- sawmill maintenance + u:set_skill("forestry", 6) + u.building = building.create(r, "sawmill") + u.building.size = 1 + u:add_order("MACHE 6 HOLZ") + process_orders() + assert_equal(6, u:get_item("log")) + assert_equal(97, r:get_resource("tree")) +end + function test_ent_guards_trees() local r = region.create(0, 0, "plain") r:set_resource("tree", 100) diff --git a/src/economy.c b/src/economy.c index f9ded29de..a3b300788 100644 --- a/src/economy.c +++ b/src/economy.c @@ -1123,7 +1123,7 @@ attrib_allocation(const resource_type * rtype, region * r, allocation * alist) allocation *al; int nreq = 0; resource_limit *rdata = get_resourcelimit(rtype); - int avail = 0; + int avail = INT_MAX; for (al = alist; al; al = al->next) { nreq += required(al->want, al->save); @@ -1145,7 +1145,7 @@ attrib_allocation(const resource_type * rtype, region * r, allocation * alist) if (rx>0 && rng_int() % nreq < rx) ++x; avail -= x; nreq -= want; - al->get = x * al->save.sa[0] / al->save.sa[1]; + al->get = x * al->save.sa[1] / al->save.sa[0]; al->get = MIN(al->want, al->get); if (rdata->produce) { int use = required(al->get, al->save); diff --git a/src/economy.test.c b/src/economy.test.c index 6d9c84542..e14b473b4 100644 --- a/src/economy.test.c +++ b/src/economy.test.c @@ -407,6 +407,12 @@ static void test_make_item(CuTest *tc) { CuAssertIntEquals(tc, 22, get_item(u, itype)); CuAssertIntEquals(tc, 283, u->region->resources->amount); /* no free lunches */ + rdata->modifiers[0].value = frac_make(1, 2); + make_item(u, itype, 6); + split_allocations(u->region); + CuAssertIntEquals(tc, 28, get_item(u, itype)); + CuAssertIntEquals(tc, 280, u->region->resources->amount); /* 50% saving = 3 stones make 6 stones */ + rdata->modifiers[0].flags = RMF_REQUIREDBUILDING; rdata->modifiers[0].race = NULL; rdata->modifiers[0].btype = bt_get_or_create("mine");