Implemented health regeneration mechanics as described in minecraft wiki. Saturation values and different regeneration speeds now used.

This commit is contained in:
Dieter44 2021-11-04 21:01:28 +01:00
parent d1d11f9740
commit d0d60804a3

View File

@ -134,46 +134,58 @@ minetest.register_on_player_hpchange(function(player, hp_change)
end end
end) end)
local main_timer = 0
local timer = 0 -- Half second timer
local timerMult = 1 -- Cycles from 0 to 7, each time when timer hits half a second
minetest.register_globalstep(function(dtime)
main_timer = main_timer + dtime
timer = timer + dtime
if main_timer > mcl_hunger.HUD_TICK or timer > 0.25 then
if main_timer > mcl_hunger.HUD_TICK then main_timer = 0 end
for _,player in pairs(minetest.get_connected_players()) do
local name = player:get_player_name()
local h = tonumber(mcl_hunger.get_hunger(player))
local hp = player:get_hp() local fastFoodTickTimer = 0 -- 0.5 second cycle
if timer > 0.25 then local slowFoodTickTimer = 0 -- 4 second cycle
-- Slow health regeneration, and hunger damage (every 4s). minetest.register_globalstep(function(dtime)
-- Regeneration rate based on tutorial video <https://www.youtube.com/watch?v=zs2t-xCVHBo>. fastFoodTickTimer = fastFoodTickTimer + dtime
-- Minecraft Wiki seems to be wrong in claiming that full hunger gives 0.5s regen rate. slowFoodTickTimer = slowFoodTickTimer + dtime
if timerMult == 0 then
if h >= 18 and hp > 0 and hp < 20 then local fastTimerWrapped = false -- if the fastFoodTickTimer wrapped around and everything dependent should be updated
-- +1 HP, +exhaustion local slowTimerWrapped = false
player:set_hp(hp+1)
mcl_hunger.exhaust(name, mcl_hunger.EXHAUST_REGEN) if fastFoodTickTimer > 0.5 then
mcl_hunger.update_exhaustion_hud(player, mcl_hunger.get_exhaustion(player)) fastFoodTickTimer = 0
elseif h == 0 then fastTimerWrapped = true
-- Damage hungry player down to 1 HP end
-- TODO: Allow starvation at higher difficulty levels if slowFoodTickTimer > 4.0 then
if hp-1 > 0 then slowFoodTickTimer = 0
mcl_util.deal_damage(player, 1, {type = "starve"}) slowTimerWrapped = true
end
if fastTimerWrapped or slowTimerWrapped then -- only update players if something must be updated
for _,player in ipairs(minetest.get_connected_players()) do
local playerName = player:get_player_name()
local foodLevel = mcl_hunger.get_hunger(player)
local foodSaturationLevel = mcl_hunger.get_saturation(player)
local playerHealth = player:get_hp()
if playerHealth < 20 then
if foodLevel == 20 and foodSaturationLevel >= 6 then -- fast regeneration (2 health per second)
if fastTimerWrapped then
player:set_hp(playerHealth+1)
mcl_hunger.exhaust(playerName, mcl_hunger.EXHAUST_REGEN)
mcl_hunger.update_exhaustion_hud(player, mcl_hunger.get_exhaustion(player))
end
elseif foodLevel >= 18 then -- slow regeneration (1 health every 4 seconds)
if slowTimerWrapped then
player:set_hp(playerHealth+1)
mcl_hunger.exhaust(playerName, mcl_hunger.EXHAUST_REGEN)
mcl_hunger.update_exhaustion_hud(player, mcl_hunger.get_exhaustion(player))
end end
end end
end end
end if foodLevel == 0 then --starvation
end maximumStarvation = 1 -- the amount of health at which a player will stop to get harmed by starvation (10 for Easy, 1 for Normal, 0 for Hard)
end -- TODO: implement Minecraft-like difficulty modes and the update maximumStarvation here
if timer > 0.25 then if playerHealth > maximumStarvation and slowTimerWrapped then
timer = 0 mcl_util.deal_damage(player, 1, {type = "starve"})
timerMult = timerMult + 2 end
if timerMult > 7 then end
timerMult = 0
end end
end end
end) end)