clarify use_potion responsibilities.

move USE potion of life to items.c.
fix some use_ function return values.
This commit is contained in:
Enno Rehling 2018-02-17 10:39:23 +01:00
parent e697a23f9d
commit 8a8bf489ae
4 changed files with 88 additions and 64 deletions

View File

@ -63,9 +63,10 @@ end
function use_snowman(u, amount) function use_snowman(u, amount)
if amount > 0 and u.region.terrain == "glacier" then if amount > 0 and u.region.terrain == "glacier" then
local man = unit.create(u.faction, u.region, amount, "snowman") unit.create(u.faction, u.region, amount, "snowman")
return amount return amount
end end
-- print error76:
return -4 return -4
end end
@ -79,7 +80,8 @@ function use_xmastree(u, amount)
msg:send_region(u.region) msg:send_region(u.region)
return amount return amount
end end
return 0 -- print error76:
return -4
end end
local self = {} local self = {}

View File

@ -160,37 +160,6 @@ static void end_potion(unit * u, const item_type * itype, int amount)
"unit potion", u, itype->rtype)); "unit potion", u, itype->rtype));
} }
static int potion_water_of_life(unit * u, region *r, int amount) {
static int config;
static int tree_type, tree_count;
int wood = 0;
if (config_changed(&config)) {
tree_type = config_get_int("rules.magic.wol_type", 1);
tree_count = config_get_int("rules.magic.wol_effect", 10);
}
/* mallorn is required to make mallorn forests, wood for regular ones */
if (fval(r, RF_MALLORN)) {
wood = use_pooled(u, rt_find("mallorn"), GET_DEFAULT, tree_count * amount);
}
else {
wood = use_pooled(u, rt_find("log"), GET_DEFAULT, tree_count * amount);
}
if (r->land == 0)
wood = 0;
if (wood < tree_count * amount) {
int x = wood / tree_count;
if (wood % tree_count)
++x;
if (x < amount)
amount = x;
}
rsettrees(r, tree_type, rtrees(r, tree_type) + wood);
ADDMSG(&u->faction->msgs, msg_message("growtree_effect",
"mage amount", u, wood));
return amount;
}
void show_potions(faction *f, int sklevel) void show_potions(faction *f, int sklevel)
{ {
const potion_type *ptype; const potion_type *ptype;
@ -262,11 +231,8 @@ static int potion_healing(struct unit *user, int amount)
static int do_potion(unit * u, region *r, const item_type * itype, int amount) static int do_potion(unit * u, region *r, const item_type * itype, int amount)
{ {
/* TODO: why do some of these take a region argument? */ /* TODO: this function should only be used for effect-changing potions */
if (itype == oldpotiontype[P_LIFE]) { if (itype == oldpotiontype[P_PEOPLE]) {
return potion_water_of_life(u, r, amount);
}
else if (itype == oldpotiontype[P_PEOPLE]) {
return potion_luck(u, r, &at_peasantluck, amount); return potion_luck(u, r, &at_peasantluck, amount);
} }
else if (itype == oldpotiontype[P_HORSE]) { else if (itype == oldpotiontype[P_HORSE]) {

View File

@ -249,19 +249,9 @@ lua_changeresource(unit * u, const struct resource_type *rtype, int delta)
/** callback for an item-use function written in lua. */ /** callback for an item-use function written in lua. */
static int static int
use_item_lua(unit *u, const item_type *itype, int amount, struct order *ord) lua_use_item(unit *u, const item_type *itype, const char * fname, int amount, struct order *ord)
{ {
lua_State *L = (lua_State *)global.vm_state; lua_State *L = (lua_State *)global.vm_state;
int len, result = 0;
char fname[64];
int (*callout)(unit *, const item_type *, int, struct order *);
len = snprintf(fname, sizeof(fname), "use_%s", itype->rtype->_name);
if (len > 0 && (size_t)len < 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); lua_getglobal(L, fname);
if (lua_isfunction(L, -1)) { if (lua_isfunction(L, -1)) {
@ -272,23 +262,52 @@ use_item_lua(unit *u, const item_type *itype, int amount, struct order *ord)
if (lua_pcall(L, 4, 1, 0) != 0) { if (lua_pcall(L, 4, 1, 0) != 0) {
const char *error = lua_tostring(L, -1); const char *error = lua_tostring(L, -1);
log_error("use(%s) calling '%s': %s.\n", unitname(u), fname, error); log_error("use(%s) calling '%s': %s.\n", unitname(u), fname, error);
lua_pop(L, 1);
} }
else { else {
result = (int)lua_tonumber(L, -1); int result = (int)lua_tonumber(L, -1);
lua_pop(L, 1); lua_pop(L, 1);
}
return result; return result;
} }
}
lua_pop(L, 1); lua_pop(L, 1);
return 0;
}
static int
use_item_callback(unit *u, const item_type *itype, int amount, struct order *ord)
{
int len;
char fname[64];
int(*callout)(unit *, const item_type *, int, struct order *);
len = snprintf(fname, sizeof(fname), "use_%s", itype->rtype->_name);
if (len > 0 && (size_t)len < sizeof(fname)) {
int result;
/* check if we have a register_item_use function */
callout = (int(*)(unit *, const item_type *, int, struct order *))get_function(fname);
if (callout) {
return callout(u, itype, amount, ord);
}
/* check if we have a matching lua function */
result = lua_use_item(u, itype, fname, amount, ord);
if (result != 0) {
return result;
}
/* if the item is a potion, try use_potion, the generic function for
* potions that add an effect: */
if (itype->flags & ITF_POTION) { if (itype->flags & ITF_POTION) {
return use_potion(u, itype, amount, ord); return use_potion(u, itype, amount, ord);
} else { }
else {
log_error("no such callout: %s", fname); log_error("no such callout: %s", fname);
} }
log_error("use(%s) calling '%s': not a function.\n", unitname(u), fname); log_error("use(%s) calling '%s': not a function.\n", unitname(u), fname);
} }
return result;
return 0;
} }
/* compat code for old data files */ /* compat code for old data files */
@ -328,7 +347,7 @@ void register_tolua_helpers(void)
at_deprecate("lcbuilding", building_action_read); at_deprecate("lcbuilding", building_action_read);
callbacks.cast_spell = lua_callspell; callbacks.cast_spell = lua_callspell;
callbacks.use_item = use_item_lua; callbacks.use_item = use_item_callback;
callbacks.produce_resource = produce_resource_lua; callbacks.produce_resource = produce_resource_lua;
callbacks.limit_resource = limit_resource_lua; callbacks.limit_resource = limit_resource_lua;

View File

@ -376,10 +376,47 @@ static int use_warmthpotion(unit *u, const item_type *itype,
return 0; return 0;
} }
static int potion_water_of_life(unit * u, region *r, int amount) {
static int config;
static int tree_type, tree_count;
int wood = 0;
if (config_changed(&config)) {
tree_type = config_get_int("rules.magic.wol_type", 1);
tree_count = config_get_int("rules.magic.wol_effect", 10);
}
/* mallorn is required to make mallorn forests, wood for regular ones */
if (fval(r, RF_MALLORN)) {
wood = use_pooled(u, rt_find("mallorn"), GET_DEFAULT, tree_count * amount);
}
else {
wood = use_pooled(u, rt_find("log"), GET_DEFAULT, tree_count * amount);
}
if (r->land == 0)
wood = 0;
if (wood < tree_count * amount) {
int x = wood / tree_count;
if (wood % tree_count)
++x;
if (x < amount)
amount = x;
}
rsettrees(r, tree_type, rtrees(r, tree_type) + wood);
ADDMSG(&u->faction->msgs, msg_message("growtree_effect",
"mage amount", u, wood));
return amount;
}
static int use_water_of_life(unit *u, const item_type *itype,
int amount, struct order *ord)
{
return potion_water_of_life(u, u->region, amount);
}
void register_itemfunctions(void) void register_itemfunctions(void)
{ {
/* have tests: */ /* have tests: */
/* TODO: potions should really use use_potion */ register_item_use(use_water_of_life, "use_p2");
register_item_use(use_mistletoe, "use_mistletoe"); register_item_use(use_mistletoe, "use_mistletoe");
register_item_use(use_tacticcrystal, "use_dreameye"); register_item_use(use_tacticcrystal, "use_dreameye");
register_item_use(use_studypotion, "use_studypotion"); register_item_use(use_studypotion, "use_studypotion");