From e99db198ebff3d8add9b0dc9dcb56eaf65c7e383 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 24 May 2015 07:17:23 +0200 Subject: [PATCH 1/4] bugfix use_item (use_skillpotion did not use pool). from here on, all use_* functions return the number of items used, and use_item handles removing them from the unit (or the pool). --- scripts/eressea/xmas2004.lua | 21 +++++++------------ scripts/eressea/xmas2005.lua | 3 +-- scripts/eressea/xmas2006.lua | 3 +-- scripts/eressea/xmas2009.lua | 5 +++-- src/CMakeLists.txt | 1 + src/buildno.h | 2 +- src/items/CMakeLists.txt | 11 +++++++++- src/items/xerewards.c | 11 ++++------ src/items/xerewards.h | 7 ++++++- src/items/xerewards.test.c | 39 ++++++++++++++++++++++++++++++++++++ src/laws.c | 8 +++++++- src/test_eressea.c | 2 ++ 12 files changed, 82 insertions(+), 31 deletions(-) create mode 100644 src/items/xerewards.test.c diff --git a/scripts/eressea/xmas2004.lua b/scripts/eressea/xmas2004.lua index 9b32d7a33..79d2b0665 100644 --- a/scripts/eressea/xmas2004.lua +++ b/scripts/eressea/xmas2004.lua @@ -1,18 +1,11 @@ function use_snowman(u, amount) - local have = u:get_item("snowman") - if have0 and u.region.terrain == "glacier" then - local man = unit.create(u.faction, u.region) - man.race = "snowman" - man.number = amount - if u:add_item("snowman", -amount)~= nil then - return -1 - end - return 0 - end - return -4 + if amount>0 and u.region.terrain == "glacier" then + local man = unit.create(u.faction, u.region) + man.race = "snowman" + man.number = amount + return amount + end + return -4 end local self = {} diff --git a/scripts/eressea/xmas2005.lua b/scripts/eressea/xmas2005.lua index df774cfef..9bc0ac723 100644 --- a/scripts/eressea/xmas2005.lua +++ b/scripts/eressea/xmas2005.lua @@ -11,8 +11,7 @@ function use_stardust(u, amount) u.region:set_resource("peasant", p) local msg = usepotion_message(u, "stardust") msg:send_region(u.region) - u:use_pooled("stardust", amount) - return 0 + return amount end local self = {} diff --git a/scripts/eressea/xmas2006.lua b/scripts/eressea/xmas2006.lua index fb1eaf476..85a8b3d98 100644 --- a/scripts/eressea/xmas2006.lua +++ b/scripts/eressea/xmas2006.lua @@ -1,11 +1,10 @@ function use_xmastree(u, amount) u.region:set_key("xm06", true) - u:use_pooled("xmastree", amount) local msg = message.create("usepotion") msg:set_unit("unit", u) msg:set_resource("potion", "xmastree") msg:send_region(u.region) - return 0 + return amount end local self = {} diff --git a/scripts/eressea/xmas2009.lua b/scripts/eressea/xmas2009.lua index 90fdc6e64..64bcd7762 100644 --- a/scripts/eressea/xmas2009.lua +++ b/scripts/eressea/xmas2009.lua @@ -1,14 +1,15 @@ function use_xmastree(u, amount) if u.region.herb~=nil then + -- TODO: else? local trees = u.region:get_resource("tree") u.region:set_resource("tree", 10+trees) - u:use_pooled("xmastree", amount) local msg = message.create("usepotion") msg:set_unit("unit", u) msg:set_resource("potion", "xmastree") msg:send_region(u.region) - return 0 + return amount end + return 0 end local xmas = {} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9e1a7bfca..34cb0987b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -183,6 +183,7 @@ set(TESTS_SRC ${ATTRIBUTES_TESTS} ${UTIL_TESTS} ${KERNEL_TESTS} + ${ITEMS_TESTS} ) add_executable(test_eressea ${TESTS_SRC}) diff --git a/src/buildno.h b/src/buildno.h index 2d9565015..0ffd496d9 100644 --- a/src/buildno.h +++ b/src/buildno.h @@ -1,3 +1,3 @@ #define VERSION_MAJOR 3 #define VERSION_MINOR 5 -#define VERSION_BUILD 0 +#define VERSION_BUILD 1 diff --git a/src/items/CMakeLists.txt b/src/items/CMakeLists.txt index 5046403d1..9ab324e77 100644 --- a/src/items/CMakeLists.txt +++ b/src/items/CMakeLists.txt @@ -1,4 +1,9 @@ PROJECT(items C) + +SET(_TEST_FILES +xerewards.test.c +) + SET(_FILES artrewards.c demonseye.c @@ -14,4 +19,8 @@ FOREACH(_FILE ${_FILES}) ENDFOREACH(_FILE) SET(ITEMS_SRC ${_SOURCES} PARENT_SCOPE) - \ No newline at end of file +FOREACH(_FILE ${_TEST_FILES}) + LIST(APPEND _TESTS ${PROJECT_NAME}/${_FILE}) +ENDFOREACH(_FILE) +SET(ITEMS_TESTS ${_TESTS} PARENT_SCOPE) + diff --git a/src/items/xerewards.c b/src/items/xerewards.c index b20fb19bb..1868d3a67 100644 --- a/src/items/xerewards.c +++ b/src/items/xerewards.c @@ -39,7 +39,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include -static int +int use_skillpotion(struct unit *u, const struct item_type *itype, int amount, struct order *ord) { @@ -59,12 +59,10 @@ struct order *ord) } } ADDMSG(&u->faction->msgs, msg_message("skillpotion_use", "unit", u)); - - change_resource(u, itype->rtype, -amount); - return 0; + return amount; } -static int +int use_manacrystal(struct unit *u, const struct item_type *itype, int amount, struct order *ord) { @@ -82,8 +80,7 @@ struct order *ord) ADDMSG(&u->faction->msgs, msg_message("manacrystal_use", "unit aura", u, sp)); - change_resource(u, itype->rtype, -amount); - return 0; + return amount; } void register_xerewards(void) diff --git a/src/items/xerewards.h b/src/items/xerewards.h index 36b6ca18d..189032891 100644 --- a/src/items/xerewards.h +++ b/src/items/xerewards.h @@ -22,7 +22,12 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. extern "C" { #endif - extern void register_xerewards(void); + struct unit; + struct item_type; + struct order; + void register_xerewards(void); + int use_skillpotion(struct unit *u, const struct item_type *itype, int amount, struct order *ord); + int use_manacrystal(struct unit *u, const struct item_type *itype, int amount, struct order *ord); #ifdef __cplusplus } diff --git a/src/items/xerewards.test.c b/src/items/xerewards.test.c new file mode 100644 index 000000000..5ce7716f3 --- /dev/null +++ b/src/items/xerewards.test.c @@ -0,0 +1,39 @@ +#include + +#include "xerewards.h" + +#include +#include +#include +#include + +#include +#include + +static void test_manacrystal(CuTest *tc) { + test_cleanup(); + test_create_world(); + test_cleanup(); +} + +static void test_skillpotion(CuTest *tc) { + unit *u; + const struct item_type *itype; + + test_cleanup(); + test_create_world(); + u = test_create_unit(test_create_faction(NULL), findregion(0, 0)); + itype = test_create_itemtype("skillpotion"); + change_resource(u, itype->rtype, 2); + CuAssertIntEquals(tc, 1, use_skillpotion(u, itype, 1, NULL)); + test_cleanup(); +} + +CuSuite *get_xerewards_suite(void) +{ + CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_manacrystal); + SUITE_ADD_TEST(suite, test_skillpotion); + return suite; +} + diff --git a/src/laws.c b/src/laws.c index 00553cde5..1380eb5f5 100755 --- a/src/laws.c +++ b/src/laws.c @@ -3510,6 +3510,7 @@ use_item(unit * u, const item_type * itype, int amount, struct order *ord) i = get_pooled(u, itype->rtype, GET_DEFAULT, amount); if (amount > i) { + /* TODO: message? eg. "not enough %, using only %" */ amount = i; } if (amount == 0) { @@ -3517,10 +3518,15 @@ use_item(unit * u, const item_type * itype, int amount, struct order *ord) } if (target == -1) { + int result; if (itype->use == NULL) { return EUNUSABLE; } - return itype->use(u, itype, amount, ord); + result = itype->use ? itype->use(u, itype, amount, ord) : EUNUSABLE; + if (result>0) { + use_pooled(u, itype->rtype, GET_DEFAULT, result); + } + return result; } else { if (itype->useonother == NULL) { diff --git a/src/test_eressea.c b/src/test_eressea.c index 9c8cfa8bc..baa70ae2c 100644 --- a/src/test_eressea.c +++ b/src/test_eressea.c @@ -54,6 +54,8 @@ int RunAllTests(void) RUN_TESTS(suite, unicode); RUN_TESTS(suite, strings); RUN_TESTS(suite, rng); + /* items */ + RUN_TESTS(suite, xerewards); /* kernel */ RUN_TESTS(suite, alliance); RUN_TESTS(suite, unit); From 6f50027088f5687a94b446579bb350726c65d063 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 24 May 2015 07:43:11 +0200 Subject: [PATCH 2/4] use_item can return positive values now --- src/laws.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/laws.c b/src/laws.c index 1380eb5f5..13e486c2c 100755 --- a/src/laws.c +++ b/src/laws.c @@ -3501,8 +3501,7 @@ void update_long_order(unit * u) } } -static int -use_item(unit * u, const item_type * itype, int amount, struct order *ord) +static int use_item(unit * u, const item_type * itype, int amount, struct order *ord) { int i; int target = read_unitid(u->faction, u->region); @@ -3795,10 +3794,6 @@ int use_cmd(unit * u, struct order *ord) if (itype != NULL) { err = use_item(u, itype, n, ord); - assert(err <= 0 || !"use_item should not return positive values."); - if (err > 0) { - log_error("use_item returned a value>0 for %s\n", resourcename(itype->rtype, 0)); - } } switch (err) { case ENOITEM: @@ -3810,6 +3805,9 @@ int use_cmd(unit * u, struct order *ord) case ENOSKILL: cmistake(u, ord, 50, MSG_PRODUCE); break; + default: + // no error + break; } return err; } From 34b1ead0acf5479274f0774c1ffb37704fda3e05 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 22 May 2015 16:23:18 +0200 Subject: [PATCH 3/4] Make GiveRestriction default to 0, do not cache the value in a static variable. --- src/give.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/give.c b/src/give.c index d22a09c73..3f8f62927 100644 --- a/src/give.c +++ b/src/give.c @@ -57,12 +57,7 @@ static int max_transfers(void) { static int GiveRestriction(void) { - static int value = -1; - if (value < 0) { - const char *str = get_param(global.parameters, "GiveRestriction"); - value = str ? atoi(str) : 0; - } - return value; + return get_param_int(global.parameters, "GiveRestriction", 0); } static void feedback_give_not_allowed(unit * u, order * ord) From 9780ec845f6f27ba6828322096cd2a9a2cb8478c Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 24 May 2015 07:49:01 +0200 Subject: [PATCH 4/4] still having problems with GiveRestriction --- s/runtests | 2 ++ scripts/tests/pool.lua | 1 + 2 files changed, 3 insertions(+) diff --git a/s/runtests b/s/runtests index ef7fc71df..3f3b61548 100755 --- a/s/runtests +++ b/s/runtests @@ -1,4 +1,6 @@ #!/bin/bash +set -e + ROOT=$(pwd) while [ ! -d $ROOT/.git ]; do ROOT=$(dirname $ROOT) diff --git a/scripts/tests/pool.lua b/scripts/tests/pool.lua index 2ac9c87f2..3ee3d54db 100644 --- a/scripts/tests/pool.lua +++ b/scripts/tests/pool.lua @@ -61,6 +61,7 @@ end function test_give_divisor() eressea.settings.set("rules.items.give_divisor", 2) + eressea.settings.set("GiveRestriction", 0) 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")