From 44c3838d790c290bff577e385be65d3f2f8182b9 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 27 Feb 2017 04:22:28 +0100 Subject: [PATCH] WIP: itype->use elimination. --- res/e3a/items.xml | 4 +--- res/eressea/items.xml | 24 ++++++------------------ src/helpers.c | 10 +++++++--- src/kernel/item.h | 1 + src/kernel/resources.c | 2 ++ src/kernel/resources.h | 3 ++- src/kernel/xmlreader.c | 3 +++ src/laws.c | 4 ++-- 8 files changed, 24 insertions(+), 27 deletions(-) diff --git a/res/e3a/items.xml b/res/e3a/items.xml index 7d7e6b286..fa3af4b41 100644 --- a/res/e3a/items.xml +++ b/res/e3a/items.xml @@ -81,9 +81,7 @@ - - - + diff --git a/res/eressea/items.xml b/res/eressea/items.xml index 69f84c890..c0b8b5126 100644 --- a/res/eressea/items.xml +++ b/res/eressea/items.xml @@ -17,21 +17,15 @@ - - - + - - - + - - - + @@ -44,23 +38,17 @@ - - - + - - - + - - - + diff --git a/src/helpers.c b/src/helpers.c index ad222a0c6..e5bdf6ff9 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -490,14 +490,18 @@ static int lua_equipmentcallback(const struct equipment *eq, unit * u) } /** callback for an item-use function written in lua. */ -int -lua_useitem(struct unit *u, const struct item_type *itype, int amount, +static int +use_item_lua(struct unit *u, const struct item_type *itype, int amount, struct order *ord) { lua_State *L = (lua_State *)global.vm_state; int result = 0; char fname[64]; + if (itype->use) { + return itype->use(u, itype, amount, ord); + } + strlcpy(fname, "use_", sizeof(fname)); strlcat(fname, itype->rtype->_name, sizeof(fname)); @@ -551,7 +555,6 @@ void register_tolua_helpers(void) register_function((pf_generic)lua_callspell, TOLUA_CAST "lua_castspell"); register_function((pf_generic)lua_initfamiliar, TOLUA_CAST "lua_initfamiliar"); - register_item_use(&lua_useitem, TOLUA_CAST "lua_useitem"); register_function((pf_generic)lua_getresource, TOLUA_CAST "lua_getresource"); register_function((pf_generic)lua_canuse_item, @@ -565,6 +568,7 @@ void register_tolua_helpers(void) register_function((pf_generic)lua_maintenance, TOLUA_CAST "lua_maintenance"); + item_use_fun = use_item_lua; res_produce_fun = produce_resource_lua; res_limit_fun = limit_resource_lua; register_item_give(lua_giveitem, TOLUA_CAST "lua_giveitem"); diff --git a/src/kernel/item.h b/src/kernel/item.h index a2e588318..b0bc10cd8 100644 --- a/src/kernel/item.h +++ b/src/kernel/item.h @@ -107,6 +107,7 @@ extern "C" { #define ITF_BIG 0x0008 /* big item, e.g. does not fit in a bag of holding */ #define ITF_ANIMAL 0x0010 /* an animal */ #define ITF_VEHICLE 0x0020 /* a vehicle, drawn by two animals */ +#define ITF_CANUSE 0x0040 /* can be used with use_item_fun callout */ /* error codes for item_type::use */ #define ECUSTOM -1 diff --git a/src/kernel/resources.c b/src/kernel/resources.c index f99783763..10cb2bab6 100644 --- a/src/kernel/resources.c +++ b/src/kernel/resources.c @@ -213,6 +213,8 @@ struct rawmaterial_type *rmt_create(struct resource_type *rtype) return rmtype; } +int(*item_use_fun)(struct unit *u, const struct item_type *itype, int amount, + struct order *ord); int(*res_limit_fun)(const struct region *, const struct resource_type *); void(*res_produce_fun)(struct region *, const struct resource_type *, int); diff --git a/src/kernel/resources.h b/src/kernel/resources.h index b448425f4..7f48fa0dd 100644 --- a/src/kernel/resources.h +++ b/src/kernel/resources.h @@ -74,7 +74,8 @@ extern "C" { extern int(*res_limit_fun)(const struct region *, const struct resource_type *); extern void(*res_produce_fun)(struct region *, const struct resource_type *, int); - + extern int (*item_use_fun)(struct unit *, const struct item_type *, int amount, + struct order *ord); int limit_resource(const struct region *r, const struct resource_type *rtype); void produce_resource(struct region *r, const struct resource_type *rtype, int amount); diff --git a/src/kernel/xmlreader.c b/src/kernel/xmlreader.c index f5e45dd5c..6f62738e3 100644 --- a/src/kernel/xmlreader.c +++ b/src/kernel/xmlreader.c @@ -767,6 +767,8 @@ static item_type *xml_readitem(xmlXPathContextPtr xpath, resource_type * rtype) if (xml_bvalue(node, "cursed", false)) flags |= ITF_CURSED; + if (xml_bvalue(node, "use", false)) + flags |= ITF_CANUSE; if (xml_bvalue(node, "notlost", false)) flags |= ITF_NOTLOST; if (xml_bvalue(node, "herb", false)) @@ -849,6 +851,7 @@ static item_type *xml_readitem(xmlXPathContextPtr xpath, resource_type * rtype) struct order *))fun; } else if (strcmp((const char *)propValue, "use") == 0) { + itype->flags |= ITF_CANUSE; itype->use = (int(*)(struct unit *, const struct item_type *, int, struct order *))fun; diff --git a/src/laws.c b/src/laws.c index 09b1439c0..0d0e53110 100644 --- a/src/laws.c +++ b/src/laws.c @@ -3252,8 +3252,8 @@ static int use_item(unit * u, const item_type * itype, int amount, struct order return ENOITEM; } - if (itype->use) { - int result = itype->use(u, itype, amount, ord); + if (itype->flags & ITF_CANUSE) { + int result = item_use_fun(u, itype, amount, ord); if (result > 0) { use_pooled(u, itype->rtype, GET_DEFAULT, result); }