forked from github/server
clarify use_potion responsibilities.
move USE potion of life to items.c. fix some use_ function return values.
This commit is contained in:
parent
e697a23f9d
commit
8a8bf489ae
|
@ -63,9 +63,10 @@ end
|
|||
|
||||
function use_snowman(u, amount)
|
||||
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
|
||||
end
|
||||
-- print error76:
|
||||
return -4
|
||||
end
|
||||
|
||||
|
@ -79,7 +80,8 @@ function use_xmastree(u, amount)
|
|||
msg:send_region(u.region)
|
||||
return amount
|
||||
end
|
||||
return 0
|
||||
-- print error76:
|
||||
return -4
|
||||
end
|
||||
|
||||
local self = {}
|
||||
|
|
|
@ -160,37 +160,6 @@ static void end_potion(unit * u, const item_type * itype, int amount)
|
|||
"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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
/* TODO: why do some of these take a region argument? */
|
||||
if (itype == oldpotiontype[P_LIFE]) {
|
||||
return potion_water_of_life(u, r, amount);
|
||||
}
|
||||
else if (itype == oldpotiontype[P_PEOPLE]) {
|
||||
/* TODO: this function should only be used for effect-changing potions */
|
||||
if (itype == oldpotiontype[P_PEOPLE]) {
|
||||
return potion_luck(u, r, &at_peasantluck, amount);
|
||||
}
|
||||
else if (itype == oldpotiontype[P_HORSE]) {
|
||||
|
|
|
@ -249,19 +249,9 @@ lua_changeresource(unit * u, const struct resource_type *rtype, int delta)
|
|||
|
||||
/** callback for an item-use function written in lua. */
|
||||
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;
|
||||
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);
|
||||
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) {
|
||||
const char *error = lua_tostring(L, -1);
|
||||
log_error("use(%s) calling '%s': %s.\n", unitname(u), fname, error);
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
else {
|
||||
result = (int)lua_tonumber(L, -1);
|
||||
int result = (int)lua_tonumber(L, -1);
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
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) {
|
||||
return use_potion(u, itype, amount, ord);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
log_error("no such callout: %s", fname);
|
||||
}
|
||||
log_error("use(%s) calling '%s': not a function.\n", unitname(u), fname);
|
||||
}
|
||||
return result;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* compat code for old data files */
|
||||
|
@ -328,7 +347,7 @@ void register_tolua_helpers(void)
|
|||
at_deprecate("lcbuilding", building_action_read);
|
||||
|
||||
callbacks.cast_spell = lua_callspell;
|
||||
callbacks.use_item = use_item_lua;
|
||||
callbacks.use_item = use_item_callback;
|
||||
callbacks.produce_resource = produce_resource_lua;
|
||||
callbacks.limit_resource = limit_resource_lua;
|
||||
|
||||
|
|
39
src/items.c
39
src/items.c
|
@ -376,10 +376,47 @@ static int use_warmthpotion(unit *u, const item_type *itype,
|
|||
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)
|
||||
{
|
||||
/* 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_tacticcrystal, "use_dreameye");
|
||||
register_item_use(use_studypotion, "use_studypotion");
|
||||
|
|
Loading…
Reference in New Issue