From b2b35fd9d0beae11c1f1e2a3537461582d3cf2ca Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 7 Jul 2014 03:40:58 +0200 Subject: [PATCH] add a test for the E3 reduced give quota. foreign units receive only 50% of silver given to them. Conflicts: tests/pool.lua --- scripts/tests/e3a.lua | 1 - src/give.c | 16 +++++++--------- src/kernel/unit.c | 4 ++-- tests/pool.lua | 18 +++++++++++++++++- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/scripts/tests/e3a.lua b/scripts/tests/e3a.lua index 08cdf4035..4ddeddc1c 100644 --- a/scripts/tests/e3a.lua +++ b/scripts/tests/e3a.lua @@ -430,7 +430,6 @@ end function test_give_50_percent_of_money() local r = region.create(0, 0, "plain") - r.name = "50percent" local u1 = unit.create(faction.create("noreply@eressea.de", "human", "de"), r, 1) local u2 = unit.create(faction.create("noreply@eressea.de", "orc", "de"), r, 1) u1.faction.age = 10 diff --git a/src/give.c b/src/give.c index bacae343f..3a47957dc 100644 --- a/src/give.c +++ b/src/give.c @@ -95,21 +95,19 @@ static bool limited_give(const item_type * type) int give_quota(const unit * src, const unit * dst, const item_type * type, int n) { - static float divisor = -1; + float divisor; - if (divisor == 0 || !limited_give(type)) { + if (!limited_give(type)) { return n; } if (dst && src && src->faction != dst->faction) { - if (divisor < 0) { divisor = get_param_flt(global.parameters, "rules.items.give_divisor", 1); assert(divisor == 0 || divisor >= 1); - } - if (divisor >= 1) { - /* predictable > correct: */ - int x = (int)(n / divisor); - return x; - } + if (divisor >= 1) { + /* predictable > correct: */ + int x = (int)(n / divisor); + return x; + } } return n; } diff --git a/src/kernel/unit.c b/src/kernel/unit.c index ae2d9018c..55e5e4a91 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -1214,8 +1214,8 @@ bool has_skill(const unit * u, skill_t sk) static int item_invis(const unit *u) { const struct resource_type *rring = get_resourcetype(R_RING_OF_INVISIBILITY); const struct resource_type *rsphere = get_resourcetype(R_SPHERE_OF_INVISIBILITY); - return i_get(u->items, rring->itype) - + i_get(u->items, rsphere->itype) * 100; + return (rring ? i_get(u->items, rring->itype) : 0) + + (rsphere ? i_get(u->items, rsphere->itype) * 100 : 0); } static int item_modification(const unit * u, skill_t sk, int val) diff --git a/tests/pool.lua b/tests/pool.lua index 20d2ea0bb..eb687363a 100644 --- a/tests/pool.lua +++ b/tests/pool.lua @@ -17,7 +17,8 @@ function setup() }, "keywords" : { "de" : { - "give" : "GIB" + "give" : "GIB", + "contact" : "KONTAKTIERE" } }, "strings" : { @@ -56,3 +57,18 @@ function test_give_from_faction() assert_equal(0, u2:get_item("money")) assert_equal(100, u3:get_item("money")) end + +function test_give_divisor() + eressea.settings.set("rules.items.give_divisor", 2) + local r = region.create(1, 1, "plain") + local f1 = faction.create("test@example.com", "human", "de") + local f2 = faction.create("test@example.com", "human", "de") + local u1 = unit.create(f1, r, 1) + local u2 = unit.create(f2, r, 1) + u2:add_order("KONTAKTIERE " .. itoa36(u1.id)) + u1:add_item("money", 100) + u1:add_order("GIB " .. itoa36(u2.id) .. " 100 SILBER") + process_orders() + assert_equal(0, u1:get_item("money")) + assert_equal(50, u2:get_item("money")) +end