From 715c8569ba50b3f53d4824122244de34bf130a37 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 27 Feb 2017 09:48:24 +0100 Subject: [PATCH] hacked the item-use function, XML needs cleaning up, funpointer needs to die --- src/helpers.c | 27 ++++++++++++++++++--------- src/kernel/item.c | 4 ++-- src/kernel/xmlreader.c | 4 ++++ 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/helpers.c b/src/helpers.c index e5bdf6ff9..5124ca0ae 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -13,6 +13,7 @@ without prior permission by the authors of Eressea. #include #include "helpers.h" #include "vortex.h" +#include "alchemy.h" #include #include @@ -491,20 +492,21 @@ static int lua_equipmentcallback(const struct equipment *eq, unit * u) /** callback for an item-use function written in lua. */ static int -use_item_lua(struct unit *u, const struct item_type *itype, int amount, -struct order *ord) +use_item_lua(unit *u, const 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); - } + int (*callout)(unit *, const item_type *, int, struct order *); strlcpy(fname, "use_", sizeof(fname)); strlcat(fname, itype->rtype->_name, sizeof(fname)); + callout = (int(*)(unit *, const item_type *, int, struct order *))get_function(fname); + if (callout) { + return callout(u, itype, amount, ord); + } + lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit"); @@ -520,11 +522,18 @@ struct order *ord) result = (int)lua_tonumber(L, -1); lua_pop(L, 1); } + return result; } - else { - log_error("use(%s) calling '%s': not a function.\n", unitname(u), fname); - lua_pop(L, 1); + if (itype->rtype->ptype) { + return use_potion(u, itype, amount, ord); + } else { + log_error("no such callout: %s", fname); } + if (itype->use) { + return itype->use(u, itype, amount, ord); + } + log_error("use(%s) calling '%s': not a function.\n", unitname(u), fname); + lua_pop(L, 1); return result; } diff --git a/src/kernel/item.c b/src/kernel/item.c index 9240a7994..5991eca96 100644 --- a/src/kernel/item.c +++ b/src/kernel/item.c @@ -1292,9 +1292,9 @@ void register_resources(void) register_item_use(use_tacticcrystal, "use_tacticcrystal"); register_item_use(use_birthdayamulet, "use_birthdayamulet"); register_item_use(use_warmthpotion, "usewarmthpotion"); - register_item_use(use_bloodpotion, "usebloodpotion"); + register_item_use(use_bloodpotion, "use_peasantblood"); register_item_use(use_healingpotion, "usehealingpotion"); - register_item_use(use_foolpotion, "usefoolpotion"); + register_item_use(use_foolpotion, "use_p7"); register_item_use(use_mistletoe, "usemistletoe"); register_item_use(use_magicboost, "usemagicboost"); register_item_use(use_snowball, "usesnowball"); diff --git a/src/kernel/xmlreader.c b/src/kernel/xmlreader.c index 6f62738e3..c4ce845a5 100644 --- a/src/kernel/xmlreader.c +++ b/src/kernel/xmlreader.c @@ -805,6 +805,10 @@ static item_type *xml_readitem(xmlXPathContextPtr xpath, resource_type * rtype) result = xmlXPathEvalExpression(BAD_CAST "potion", xpath); assert(result->nodesetval->nodeNr <= 1); if (result->nodesetval->nodeNr != 0) { + if ((itype->flags & ITF_CANUSE) == 0) { + log_error("potion %s has no use attribute", rtype->_name); + itype->flags |= ITF_CANUSE; + } xpath->node = result->nodesetval->nodeTab[0]; rtype->ptype = xml_readpotion(xpath, itype); }