WIP: itype->use elimination.

This commit is contained in:
Enno Rehling 2017-02-27 04:22:28 +01:00
parent 8b69b6d003
commit 44c3838d79
8 changed files with 24 additions and 27 deletions

View File

@ -81,9 +81,7 @@
</resource>
<resource name="xmastree">
<item weight="0">
<function name="use" value="lua_useitem"/>
</item>
<item weight="0" use="yes" />
</resource>
</resources>

View File

@ -17,21 +17,15 @@
</resource>
<resource name="snowman">
<item notlost="yes" weight="1">
<function name="use" value="lua_useitem"/>
</item>
<item notlost="yes" weight="1" use="yes" />
</resource>
<resource name="snowglobe">
<item notlost="yes" weight="1">
<function name="use" value="lua_useitem"/>
</item>
<item notlost="yes" weight="1" use="yes" />
</resource>
<resource name="ring_of_levitation" appearance="ring">
<item notlost="yes" weight="0" cursed="true">
<function name="use" value="lua_useitem"/>
</item>
<item notlost="yes" weight="0" cursed="true" use="yes" />
</resource>
<resource name="birthdaycake">
@ -44,23 +38,17 @@
<!-- ambassador rewards -->
<resource name="seashell">
<item cursed="true" weight="0">
<function name="use" value="lua_useitem"/>
</item>
<item cursed="true" weight="0" use="yes" />
</resource>
<!-- xmas 2005 -->
<resource name="stardust" appearance="vial">
<item weight="0">
<function name="use" value="lua_useitem"/>
</item>
<item weight="0" use="yes" />
</resource>
<!-- xmas 2006 -->
<resource name="xmastree">
<item weight="0">
<function name="use" value="lua_useitem"/>
</item>
<item weight="0" use="yes" />
</resource>
<!-- museum items -->

View File

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

View File

@ -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

View File

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

View File

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

View File

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

View File

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