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);