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).
This commit is contained in:
Enno Rehling 2015-05-24 07:17:23 +02:00
parent 72e4ee8e9f
commit e99db198eb
12 changed files with 82 additions and 31 deletions

View File

@ -1,18 +1,11 @@
function use_snowman(u, amount) function use_snowman(u, amount)
local have = u:get_item("snowman") if amount>0 and u.region.terrain == "glacier" then
if have<amount then local man = unit.create(u.faction, u.region)
amount = have man.race = "snowman"
end man.number = amount
if amount>0 and u.region.terrain == "glacier" then return amount
local man = unit.create(u.faction, u.region) end
man.race = "snowman" return -4
man.number = amount
if u:add_item("snowman", -amount)~= nil then
return -1
end
return 0
end
return -4
end end
local self = {} local self = {}

View File

@ -11,8 +11,7 @@ function use_stardust(u, amount)
u.region:set_resource("peasant", p) u.region:set_resource("peasant", p)
local msg = usepotion_message(u, "stardust") local msg = usepotion_message(u, "stardust")
msg:send_region(u.region) msg:send_region(u.region)
u:use_pooled("stardust", amount) return amount
return 0
end end
local self = {} local self = {}

View File

@ -1,11 +1,10 @@
function use_xmastree(u, amount) function use_xmastree(u, amount)
u.region:set_key("xm06", true) u.region:set_key("xm06", true)
u:use_pooled("xmastree", amount)
local msg = message.create("usepotion") local msg = message.create("usepotion")
msg:set_unit("unit", u) msg:set_unit("unit", u)
msg:set_resource("potion", "xmastree") msg:set_resource("potion", "xmastree")
msg:send_region(u.region) msg:send_region(u.region)
return 0 return amount
end end
local self = {} local self = {}

View File

@ -1,14 +1,15 @@
function use_xmastree(u, amount) function use_xmastree(u, amount)
if u.region.herb~=nil then if u.region.herb~=nil then
-- TODO: else?
local trees = u.region:get_resource("tree") local trees = u.region:get_resource("tree")
u.region:set_resource("tree", 10+trees) u.region:set_resource("tree", 10+trees)
u:use_pooled("xmastree", amount)
local msg = message.create("usepotion") local msg = message.create("usepotion")
msg:set_unit("unit", u) msg:set_unit("unit", u)
msg:set_resource("potion", "xmastree") msg:set_resource("potion", "xmastree")
msg:send_region(u.region) msg:send_region(u.region)
return 0 return amount
end end
return 0
end end
local xmas = {} local xmas = {}

View File

@ -183,6 +183,7 @@ set(TESTS_SRC
${ATTRIBUTES_TESTS} ${ATTRIBUTES_TESTS}
${UTIL_TESTS} ${UTIL_TESTS}
${KERNEL_TESTS} ${KERNEL_TESTS}
${ITEMS_TESTS}
) )
add_executable(test_eressea ${TESTS_SRC}) add_executable(test_eressea ${TESTS_SRC})

View File

@ -1,3 +1,3 @@
#define VERSION_MAJOR 3 #define VERSION_MAJOR 3
#define VERSION_MINOR 5 #define VERSION_MINOR 5
#define VERSION_BUILD 0 #define VERSION_BUILD 1

View File

@ -1,4 +1,9 @@
PROJECT(items C) PROJECT(items C)
SET(_TEST_FILES
xerewards.test.c
)
SET(_FILES SET(_FILES
artrewards.c artrewards.c
demonseye.c demonseye.c
@ -14,4 +19,8 @@ FOREACH(_FILE ${_FILES})
ENDFOREACH(_FILE) ENDFOREACH(_FILE)
SET(ITEMS_SRC ${_SOURCES} PARENT_SCOPE) SET(ITEMS_SRC ${_SOURCES} PARENT_SCOPE)
FOREACH(_FILE ${_TEST_FILES})
LIST(APPEND _TESTS ${PROJECT_NAME}/${_FILE})
ENDFOREACH(_FILE)
SET(ITEMS_TESTS ${_TESTS} PARENT_SCOPE)

View File

@ -39,7 +39,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
static int int
use_skillpotion(struct unit *u, const struct item_type *itype, int amount, use_skillpotion(struct unit *u, const struct item_type *itype, int amount,
struct order *ord) struct order *ord)
{ {
@ -59,12 +59,10 @@ struct order *ord)
} }
} }
ADDMSG(&u->faction->msgs, msg_message("skillpotion_use", "unit", u)); ADDMSG(&u->faction->msgs, msg_message("skillpotion_use", "unit", u));
return amount;
change_resource(u, itype->rtype, -amount);
return 0;
} }
static int int
use_manacrystal(struct unit *u, const struct item_type *itype, int amount, use_manacrystal(struct unit *u, const struct item_type *itype, int amount,
struct order *ord) struct order *ord)
{ {
@ -82,8 +80,7 @@ struct order *ord)
ADDMSG(&u->faction->msgs, msg_message("manacrystal_use", "unit aura", u, sp)); ADDMSG(&u->faction->msgs, msg_message("manacrystal_use", "unit aura", u, sp));
change_resource(u, itype->rtype, -amount); return amount;
return 0;
} }
void register_xerewards(void) void register_xerewards(void)

View File

@ -22,7 +22,12 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
extern "C" { extern "C" {
#endif #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 #ifdef __cplusplus
} }

View File

@ -0,0 +1,39 @@
#include <platform.h>
#include "xerewards.h"
#include <kernel/unit.h>
#include <kernel/item.h>
#include <kernel/pool.h>
#include <kernel/region.h>
#include <tests.h>
#include <CuTest.h>
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;
}

View File

@ -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); i = get_pooled(u, itype->rtype, GET_DEFAULT, amount);
if (amount > i) { if (amount > i) {
/* TODO: message? eg. "not enough %, using only %" */
amount = i; amount = i;
} }
if (amount == 0) { if (amount == 0) {
@ -3517,10 +3518,15 @@ use_item(unit * u, const item_type * itype, int amount, struct order *ord)
} }
if (target == -1) { if (target == -1) {
int result;
if (itype->use == NULL) { if (itype->use == NULL) {
return EUNUSABLE; 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 { else {
if (itype->useonother == NULL) { if (itype->useonother == NULL) {

View File

@ -54,6 +54,8 @@ int RunAllTests(void)
RUN_TESTS(suite, unicode); RUN_TESTS(suite, unicode);
RUN_TESTS(suite, strings); RUN_TESTS(suite, strings);
RUN_TESTS(suite, rng); RUN_TESTS(suite, rng);
/* items */
RUN_TESTS(suite, xerewards);
/* kernel */ /* kernel */
RUN_TESTS(suite, alliance); RUN_TESTS(suite, alliance);
RUN_TESTS(suite, unit); RUN_TESTS(suite, unit);