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>
<resource name="xmastree"> <resource name="xmastree">
<item weight="0"> <item weight="0" use="yes" />
<function name="use" value="lua_useitem"/>
</item>
</resource> </resource>
</resources> </resources>

View File

@ -17,21 +17,15 @@
</resource> </resource>
<resource name="snowman"> <resource name="snowman">
<item notlost="yes" weight="1"> <item notlost="yes" weight="1" use="yes" />
<function name="use" value="lua_useitem"/>
</item>
</resource> </resource>
<resource name="snowglobe"> <resource name="snowglobe">
<item notlost="yes" weight="1"> <item notlost="yes" weight="1" use="yes" />
<function name="use" value="lua_useitem"/>
</item>
</resource> </resource>
<resource name="ring_of_levitation" appearance="ring"> <resource name="ring_of_levitation" appearance="ring">
<item notlost="yes" weight="0" cursed="true"> <item notlost="yes" weight="0" cursed="true" use="yes" />
<function name="use" value="lua_useitem"/>
</item>
</resource> </resource>
<resource name="birthdaycake"> <resource name="birthdaycake">
@ -44,23 +38,17 @@
<!-- ambassador rewards --> <!-- ambassador rewards -->
<resource name="seashell"> <resource name="seashell">
<item cursed="true" weight="0"> <item cursed="true" weight="0" use="yes" />
<function name="use" value="lua_useitem"/>
</item>
</resource> </resource>
<!-- xmas 2005 --> <!-- xmas 2005 -->
<resource name="stardust" appearance="vial"> <resource name="stardust" appearance="vial">
<item weight="0"> <item weight="0" use="yes" />
<function name="use" value="lua_useitem"/>
</item>
</resource> </resource>
<!-- xmas 2006 --> <!-- xmas 2006 -->
<resource name="xmastree"> <resource name="xmastree">
<item weight="0"> <item weight="0" use="yes" />
<function name="use" value="lua_useitem"/>
</item>
</resource> </resource>
<!-- museum items --> <!-- 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. */ /** callback for an item-use function written in lua. */
int static int
lua_useitem(struct unit *u, const struct item_type *itype, int amount, use_item_lua(struct unit *u, const struct item_type *itype, int amount,
struct order *ord) struct order *ord)
{ {
lua_State *L = (lua_State *)global.vm_state; lua_State *L = (lua_State *)global.vm_state;
int result = 0; int result = 0;
char fname[64]; char fname[64];
if (itype->use) {
return itype->use(u, itype, amount, ord);
}
strlcpy(fname, "use_", sizeof(fname)); strlcpy(fname, "use_", sizeof(fname));
strlcat(fname, itype->rtype->_name, 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_callspell, TOLUA_CAST "lua_castspell");
register_function((pf_generic)lua_initfamiliar, register_function((pf_generic)lua_initfamiliar,
TOLUA_CAST "lua_initfamiliar"); TOLUA_CAST "lua_initfamiliar");
register_item_use(&lua_useitem, TOLUA_CAST "lua_useitem");
register_function((pf_generic)lua_getresource, register_function((pf_generic)lua_getresource,
TOLUA_CAST "lua_getresource"); TOLUA_CAST "lua_getresource");
register_function((pf_generic)lua_canuse_item, register_function((pf_generic)lua_canuse_item,
@ -565,6 +568,7 @@ void register_tolua_helpers(void)
register_function((pf_generic)lua_maintenance, register_function((pf_generic)lua_maintenance,
TOLUA_CAST "lua_maintenance"); TOLUA_CAST "lua_maintenance");
item_use_fun = use_item_lua;
res_produce_fun = produce_resource_lua; res_produce_fun = produce_resource_lua;
res_limit_fun = limit_resource_lua; res_limit_fun = limit_resource_lua;
register_item_give(lua_giveitem, TOLUA_CAST "lua_giveitem"); 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_BIG 0x0008 /* big item, e.g. does not fit in a bag of holding */
#define ITF_ANIMAL 0x0010 /* an animal */ #define ITF_ANIMAL 0x0010 /* an animal */
#define ITF_VEHICLE 0x0020 /* a vehicle, drawn by two animals */ #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 */ /* error codes for item_type::use */
#define ECUSTOM -1 #define ECUSTOM -1

View File

@ -213,6 +213,8 @@ struct rawmaterial_type *rmt_create(struct resource_type *rtype)
return rmtype; 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 *); int(*res_limit_fun)(const struct region *, const struct resource_type *);
void(*res_produce_fun)(struct region *, const struct resource_type *, int); 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 int(*res_limit_fun)(const struct region *, const struct resource_type *);
extern void(*res_produce_fun)(struct region *, const struct resource_type *, int); 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); 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); 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)) if (xml_bvalue(node, "cursed", false))
flags |= ITF_CURSED; flags |= ITF_CURSED;
if (xml_bvalue(node, "use", false))
flags |= ITF_CANUSE;
if (xml_bvalue(node, "notlost", false)) if (xml_bvalue(node, "notlost", false))
flags |= ITF_NOTLOST; flags |= ITF_NOTLOST;
if (xml_bvalue(node, "herb", false)) if (xml_bvalue(node, "herb", false))
@ -849,6 +851,7 @@ static item_type *xml_readitem(xmlXPathContextPtr xpath, resource_type * rtype)
struct order *))fun; struct order *))fun;
} }
else if (strcmp((const char *)propValue, "use") == 0) { else if (strcmp((const char *)propValue, "use") == 0) {
itype->flags |= ITF_CANUSE;
itype->use = itype->use =
(int(*)(struct unit *, const struct item_type *, int, (int(*)(struct unit *, const struct item_type *, int,
struct order *))fun; 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; return ENOITEM;
} }
if (itype->use) { if (itype->flags & ITF_CANUSE) {
int result = itype->use(u, itype, amount, ord); int result = item_use_fun(u, itype, amount, ord);
if (result > 0) { if (result > 0) {
use_pooled(u, itype->rtype, GET_DEFAULT, result); use_pooled(u, itype->rtype, GET_DEFAULT, result);
} }