hacked the item-use function,

XML needs cleaning up, funpointer needs to die
This commit is contained in:
Enno Rehling 2017-02-27 09:48:24 +01:00
parent 44c3838d79
commit 715c8569ba
3 changed files with 24 additions and 11 deletions

View File

@ -13,6 +13,7 @@ without prior permission by the authors of Eressea.
#include <platform.h>
#include "helpers.h"
#include "vortex.h"
#include "alchemy.h"
#include <util/attrib.h>
#include <util/base36.h>
@ -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;
}
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);
}
else {
log_error("use(%s) calling '%s': not a function.\n", unitname(u), fname);
lua_pop(L, 1);
}
return result;
}

View File

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

View File

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