1
0
mirror of git://repo.or.cz/minetest_hbhunger.git synced 2024-11-20 00:50:31 +01:00
minetest_hbhunger/hunger.lua

159 lines
4.8 KiB
Lua
Raw Normal View History

-- Keep these for backwards compatibility
2015-02-27 11:17:47 +01:00
function hbhunger.save_hunger(player)
hbhunger.set_hunger_raw(player)
end
2015-02-27 11:17:47 +01:00
function hbhunger.load_hunger(player)
hbhunger.get_hunger_raw(player)
end
-- wrapper for minetest.item_eat (this way we make sure other mods can't break this one)
2020-11-04 12:58:33 +01:00
local org_eat = minetest.do_item_eat
minetest.do_item_eat = function(hp_change, replace_with_item, itemstack, user, pointed_thing)
local old_itemstack = itemstack
itemstack = hbhunger.eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
2020-11-04 12:58:33 +01:00
for _, callback in pairs(minetest.registered_on_item_eats) do
local result = callback(hp_change, replace_with_item, itemstack, user, pointed_thing, old_itemstack)
if result then
return result
end
end
return itemstack
end
-- food functions
local food = hbhunger.food
function hbhunger.register_food(name, hunger_change, replace_with_item, poisen, heal, sound)
food[name] = {}
food[name].saturation = hunger_change -- hunger points added
food[name].replace = replace_with_item -- what item is given back after eating
food[name].poisen = poisen -- time its poisening
food[name].healing = heal -- amount of HP
food[name].sound = sound -- special sound that is played when eating
end
function hbhunger.eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
local item = itemstack:get_name()
local def = food[item]
if not def then
def = {}
if type(hp_change) ~= "number" then
hp_change = 1
2020-11-04 12:58:33 +01:00
minetest.log("error", "Wrong on_use() definition for item '" .. item .. "'")
end
def.saturation = hp_change * 1.3
def.replace = replace_with_item
end
local func = hbhunger.item_eat(def.saturation, def.replace, def.poisen, def.healing, def.sound)
return func(itemstack, user, pointed_thing)
end
-- Poison player
local function poisenp(tick, time, time_left, player)
-- First check if player is still there
if not player:is_player() then
return
end
time_left = time_left + tick
if time_left < time then
minetest.after(tick, poisenp, tick, time, time_left, player)
else
hbhunger.poisonings[player:get_player_name()] = hbhunger.poisonings[player:get_player_name()] - 1
if hbhunger.poisonings[player:get_player_name()] <= 0 then
-- Reset HUD bar color
hb.change_hudbar(player, "health", nil, nil, "hudbars_icon_health.png", nil, "hudbars_bar_health.png")
end
end
if player:get_hp()-1 > 0 then
player:set_hp(player:get_hp()-1)
end
end
2016-10-20 19:25:19 +02:00
function hbhunger.item_eat(hunger_change, replace_with_item, poisen, heal, sound)
2013-06-28 22:58:06 +02:00
return function(itemstack, user, pointed_thing)
if itemstack:take_item() ~= nil and user ~= nil then
local name = user:get_player_name()
2015-02-27 11:17:47 +01:00
local h = tonumber(hbhunger.hunger[name])
local hp = user:get_hp()
if h == nil or hp == nil then
return
end
2019-09-24 18:20:12 +02:00
minetest.sound_play(
{name = sound or "hbhunger_eat_generic",
gain = 1},
{object=user,
max_hear_distance = 16,
2020-04-05 23:59:52 +02:00
pitch = 1 + math.random(-10, 10)*0.005,},
true
2019-09-24 18:20:12 +02:00
)
-- Saturation
2020-11-04 13:17:05 +01:00
if h < hbhunger.SAT_MAX and hunger_change then
h = h + hunger_change
2020-11-04 13:17:05 +01:00
if h > hbhunger.SAT_MAX then h = hbhunger.SAT_MAX end
2015-02-27 11:17:47 +01:00
hbhunger.hunger[name] = h
hbhunger.set_hunger_raw(user)
end
-- Healing
if hp < 20 and heal then
hp = hp + heal
if hp > 20 then hp = 20 end
user:set_hp(hp)
end
-- Poison
if poisen then
-- Set poison bar
hb.change_hudbar(user, "health", nil, nil, "hbhunger_icon_health_poison.png", nil, "hbhunger_bar_health_poison.png")
hbhunger.poisonings[name] = hbhunger.poisonings[name] + 1
2016-08-05 18:09:00 +02:00
poisenp(1, poisen, 0, user)
end
if itemstack:get_count() == 0 then
itemstack:add_item(replace_with_item)
else
local inv = user:get_inventory()
if inv:room_for_item("main", replace_with_item) then
inv:add_item("main", replace_with_item)
else
minetest.add_item(user:getpos(), replace_with_item)
end
end
2013-06-28 22:58:06 +02:00
end
return itemstack
end
end
-- player-action based hunger changes
2015-02-27 11:17:47 +01:00
function hbhunger.handle_node_actions(pos, oldnode, player, ext)
2016-10-29 20:56:05 +02:00
-- is_fake_player comes from the pipeworks, we are not interested in those
2016-10-29 20:42:09 +02:00
if not player or not player:is_player() or player.is_fake_player == true then
return
end
local name = player:get_player_name()
2015-02-27 11:17:47 +01:00
local exhaus = hbhunger.exhaustion[name]
if exhaus == nil then return end
local new = hbhunger.EXHAUST_PLACE
-- placenode event
if not ext then
new = hbhunger.EXHAUST_DIG
end
-- assume its send by main timer when movement detected
if not pos and not oldnode then
new = hbhunger.EXHAUST_MOVE
end
exhaus = exhaus + new
if exhaus > hbhunger.EXHAUST_LVL then
exhaus = 0
2015-02-27 11:17:47 +01:00
local h = tonumber(hbhunger.hunger[name])
h = h - 1
if h < 0 then h = 0 end
2015-02-27 11:17:47 +01:00
hbhunger.hunger[name] = h
hbhunger.set_hunger_raw(player)
end
2015-02-27 11:17:47 +01:00
hbhunger.exhaustion[name] = exhaus
end
2015-02-27 11:17:47 +01:00
minetest.register_on_placenode(hbhunger.handle_node_actions)
minetest.register_on_dignode(hbhunger.handle_node_actions)