1
0
Fork 0
MineClone2/mods/PLAYER/mcl_hunger/hunger.lua

276 lines
9.6 KiB
Lua
Raw Permalink Normal View History

--local S = minetest.get_translator(minetest.get_current_modname())
2019-03-08 19:22:01 +00:00
2017-02-16 13:19:34 +00:00
-- wrapper for minetest.item_eat (this way we make sure other mods can't break this one)
function minetest.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
if not user or user:is_player() == false then
return itemstack
end
-- Call on_rightclick if the pointed node defines it
if pointed_thing.type == "node" then
local node = minetest.get_node(pointed_thing.under)
if user and not user:get_player_control().sneak then
if minetest.registered_nodes[node.name] and minetest.registered_nodes[node.name].on_rightclick then
return minetest.registered_nodes[node.name].on_rightclick(pointed_thing.under, node, user, itemstack) or itemstack
end
end
end
-- Also don't eat when pointing object (it could be an animal)
if pointed_thing.type == "object" then
return itemstack
end
2017-02-16 13:19:34 +00:00
local old_itemstack = itemstack
2017-05-22 23:39:19 +00:00
local name = user:get_player_name()
2020-07-10 14:08:40 +00:00
local creative = minetest.is_creative_enabled(name)
2017-05-22 23:51:37 +00:00
-- Special foodstuffs like the cake may disable the eating delay
local no_eat_delay = creative or (minetest.get_item_group(itemstack:get_name(), "no_eat_delay") == 1)
2017-05-22 23:51:37 +00:00
-- Allow eating only after a delay of 2 seconds. This prevents eating as an excessive speed.
-- FIXME: time() is not a precise timer, so the actual delay may be +- 1 second, depending on which fraction
-- of the second the player made the first eat.
2017-05-22 23:39:19 +00:00
-- FIXME: In singleplayer, there's a cheat to circumvent this, simply by pausing the game between eats.
-- This is because os.time() obviously does not care about the pause. A fix needs a different timer mechanism.
if no_eat_delay or (mcl_hunger.last_eat[name] < 0) or (os.difftime(os.time(), mcl_hunger.last_eat[name]) >= 2) then
2021-04-15 21:41:34 +00:00
local can_eat_when_full = creative or (mcl_hunger.active == false)
or minetest.get_item_group(itemstack:get_name(), "can_eat_when_full") == 1
-- Don't allow eating when player has full hunger bar (some exceptional items apply)
if not no_eat_delay and not mcl_hunger.eat_internal[name].is_eating and not mcl_hunger.eat_internal[name].do_item_eat and (can_eat_when_full or (mcl_hunger.get_hunger(user) < 20)) then
local itemname = itemstack:get_name()
table.update(mcl_hunger.eat_internal[name], {
is_eating = true,
is_eating_no_padding = true,
itemname = itemname,
item_definition = minetest.registered_items[itemname],
hp_change = hp_change,
replace_with_item = replace_with_item,
itemstack = itemstack,
user = user,
pointed_thing = pointed_thing
})
elseif (mcl_hunger.eat_internal[name].do_item_eat or no_eat_delay) and (can_eat_when_full or (mcl_hunger.get_hunger(user) < 20)) then
if mcl_hunger.eat_internal[name]._custom_itemstack and
mcl_hunger.eat_internal[name]._custom_wrapper and
mcl_hunger.eat_internal[name]._custom_itemstack == itemstack then
mcl_hunger.eat_internal[name]._custom_wrapper(name)
end
itemstack = mcl_hunger.eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
2019-02-05 20:14:28 +00: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
2017-05-22 23:39:19 +00:00
end
mcl_hunger.last_eat[name] = os.time()
user:get_inventory():set_stack("main", user:get_wield_index(), itemstack)
2017-02-16 13:19:34 +00:00
end
end
2017-05-22 23:39:19 +00:00
2017-02-16 13:19:34 +00:00
return itemstack
end
function mcl_hunger.eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
local item = itemstack:get_name()
2019-02-06 20:23:05 +00:00
local def = mcl_hunger.registered_foods[item]
2017-02-16 13:19:34 +00:00
if not def then
def = {}
if type(hp_change) ~= "number" then
hp_change = 1
2019-02-05 20:14:28 +00:00
minetest.log("error", "Wrong on_use() definition for item '" .. item .. "'")
2017-02-16 13:19:34 +00:00
end
def.saturation = hp_change
def.replace = replace_with_item
end
2021-04-15 21:41:34 +00:00
local func = mcl_hunger.item_eat(def.saturation, def.replace, def.poisontime,
def.poison, def.exhaust, def.poisonchance, def.sound)
2017-02-16 13:19:34 +00:00
return func(itemstack, user, pointed_thing)
end
-- Reset HUD bars after food poisoning
2019-02-06 20:23:05 +00:00
function mcl_hunger.reset_bars_poison_hunger(player)
hb.change_hudbar(player, "hunger", nil, nil, "hbhunger_icon.png", nil, "hbhunger_bar.png")
if mcl_hunger.debug then
hb.change_hudbar(player, "exhaustion", nil, nil, nil, nil, "mcl_hunger_bar_exhaustion.png")
end
end
2017-02-16 13:19:34 +00:00
-- Poison player
local function poisonp(tick, time, time_left, damage, exhaustion, name)
2019-02-06 20:23:05 +00:00
if not mcl_hunger.active then
return
end
local player = minetest.get_player_by_name(name)
2017-02-16 13:19:34 +00:00
-- First check if player is still there
if not player then
2017-02-16 13:19:34 +00:00
return
end
-- Abort if food poisonings have been stopped
if mcl_hunger.poison_hunger[name] == 0 then
2017-05-20 22:19:12 +00:00
return
end
2017-02-16 13:19:34 +00:00
time_left = time_left + tick
if time_left < time then
minetest.after(tick, poisonp, tick, time, time_left, damage, exhaustion, name)
2017-02-16 13:19:34 +00:00
else
if exhaustion > 0 then
mcl_hunger.poison_hunger [name] = mcl_hunger.poison_hunger[name] - 1
end
if mcl_hunger.poison_hunger[name] <= 0 then
2019-02-06 20:23:05 +00:00
mcl_hunger.reset_bars_poison_hunger(player)
2017-02-16 13:19:34 +00:00
end
end
2017-05-20 22:01:39 +00:00
-- Deal damage and exhaust player
2019-03-08 19:22:01 +00:00
-- TODO: Introduce fatal poison at higher difficulties
2017-05-20 22:01:39 +00:00
if player:get_hp()-damage > 0 then
mcl_util.deal_damage(player, damage, {type = "hunger"})
2017-02-16 13:19:34 +00:00
end
mcl_hunger.exhaust(name, exhaustion)
2017-05-22 23:39:19 +00:00
2017-02-16 13:19:34 +00:00
end
local poisonrandomizer = PseudoRandom(os.time())
function mcl_hunger.item_eat(hunger_change, replace_with_item, poisontime, poison, exhaust, poisonchance, sound)
2017-02-16 13:19:34 +00:00
return function(itemstack, user, pointed_thing)
2017-02-16 13:47:50 +00:00
local itemname = itemstack:get_name()
2020-07-10 14:08:40 +00:00
local creative = minetest.is_creative_enabled(user:get_player_name())
if itemstack:peek_item() and user then
if not creative then
itemstack:take_item()
end
2017-02-16 13:19:34 +00:00
local name = user:get_player_name()
2021-04-15 21:41:34 +00:00
--local hp = user:get_hp()
2017-02-16 13:47:50 +00:00
2019-02-01 05:33:07 +00:00
local pos = user:get_pos()
local def = minetest.registered_items[itemname]
mcl_hunger.eat_effects(user, itemname, pos, hunger_change, def)
2017-02-16 13:19:34 +00:00
2019-02-06 20:23:05 +00:00
if mcl_hunger.active and hunger_change then
-- Add saturation (must be defined in item table)
2017-05-20 22:04:39 +00:00
local _mcl_saturation = minetest.registered_items[itemname]._mcl_saturation
local saturation
if not _mcl_saturation then
2017-05-20 20:33:10 +00:00
saturation = 0
2017-05-20 22:04:39 +00:00
else
saturation = minetest.registered_items[itemname]._mcl_saturation
2017-05-20 15:45:04 +00:00
end
mcl_hunger.saturate(name, saturation, false)
-- Add food points
2017-05-20 19:23:02 +00:00
local h = mcl_hunger.get_hunger(user)
if h < 20 and hunger_change then
h = h + hunger_change
if h > 20 then h = 20 end
2017-05-20 19:23:02 +00:00
mcl_hunger.set_hunger(user, h, false)
end
2017-05-20 21:26:54 +00:00
hb.change_hudbar(user, "hunger", h)
2017-05-20 20:51:40 +00:00
mcl_hunger.update_saturation_hud(user, mcl_hunger.get_saturation(user), h)
end
2017-02-16 13:19:34 +00:00
-- Poison
2019-02-06 20:23:05 +00:00
if mcl_hunger.active and poisontime then
local do_poison = false
if poisonchance then
if poisonrandomizer:next(0,100) < poisonchance then
do_poison = true
end
else
do_poison = true
end
if do_poison then
-- Set food poison bars
if exhaust and exhaust > 0 then
hb.change_hudbar(user, "hunger", nil, nil, "mcl_hunger_icon_foodpoison.png", nil, "mcl_hunger_bar_foodpoison.png")
if mcl_hunger.debug then
hb.change_hudbar(user, "exhaustion", nil, nil, nil, nil, "mcl_hunger_bar_foodpoison.png")
end
mcl_hunger.poison_hunger[name] = mcl_hunger.poison_hunger[name] + 1
end
poisonp(1, poisontime, 0, poison, exhaust, user:get_player_name())
end
2017-02-16 13:19:34 +00:00
end
if not creative then
itemstack:add_item(replace_with_item)
end
2017-02-16 13:19:34 +00:00
end
return itemstack
end
end
function mcl_hunger.eat_effects(user, itemname, pos, hunger_change, item_def, pitch)
if user and itemname and pos and hunger_change and item_def then
local name = user:get_player_name()
if mcl_hunger.eat_internal[name] and mcl_hunger.eat_internal[name].do_item_eat then
pitch = 0.95
end
local def = item_def
-- player height
pos.y = pos.y + 1.5
local foodtype = minetest.get_item_group(itemname, "food")
if foodtype == 3 then
-- Item is a drink, only play drinking sound (no particle)
minetest.sound_play("survival_thirst_drink", {
max_hear_distance = 12,
gain = 1.0,
pitch = pitch or 1 + math.random(-10, 10)*0.005,
object = user,
}, true)
else
-- Assume the item is a food
-- Add eat particle effect and sound
--local def = minetest.registered_items[itemname]
local texture = def.inventory_image
if not texture or texture == "" then
texture = def.wield_image
end
-- Special item definition field: _food_particles
-- If false, force item to not spawn any food partiles when eaten
if def._food_particles ~= false and texture and texture ~= "" then
local v = user:get_velocity() or user:get_player_velocity()
for i = 0, math.min(math.max(8, hunger_change*2), 25) do
minetest.add_particle({
pos = { x = pos.x, y = pos.y, z = pos.z },
velocity = vector.add(v, { x = math.random(-1, 1), y = math.random(1, 2), z = math.random(-1, 1) }),
acceleration = { x = 0, y = math.random(-9, -5), z = 0 },
expirationtime = 1,
size = math.random(1, 2),
collisiondetection = true,
vertical = false,
texture = "[combine:3x3:" .. -i .. "," .. -i .. "=" .. texture,
})
end
end
minetest.sound_play("mcl_hunger_bite", {
max_hear_distance = 12,
gain = 1.0,
pitch = pitch or 1 + math.random(-10, 10)*0.005,
object = user,
}, true)
end
else
return false
end
end
2019-02-06 20:23:05 +00:00
if mcl_hunger.active then
-- player-action based hunger changes
minetest.register_on_dignode(function(pos, oldnode, player)
-- is_fake_player comes from the pipeworks, we are not interested in those
if not player or not player:is_player() or player.is_fake_player == true then
return
end
local name = player:get_player_name()
-- dig event
mcl_hunger.exhaust(name, mcl_hunger.EXHAUST_DIG)
end)
end