From 3278085904694df3406448d8899a1aa6d4b690e9 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Sat, 21 Jan 2017 15:10:08 +0100 Subject: [PATCH] More Minecraft-like hunger system (partially) --- mods/hbhunger/hunger.lua | 6 +++--- mods/hbhunger/init.lua | 36 +++++++++++++++++++++++++----------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/mods/hbhunger/hunger.lua b/mods/hbhunger/hunger.lua index dce52c06e..aecc3ba39 100644 --- a/mods/hbhunger/hunger.lua +++ b/mods/hbhunger/hunger.lua @@ -41,7 +41,7 @@ function hbhunger.eat(hp_change, replace_with_item, itemstack, user, pointed_thi hp_change = 1 core.log("error", "Wrong on_use() definition for item '" .. item .. "'") end - def.saturation = hp_change * 1.3 + def.saturation = hp_change def.replace = replace_with_item end local func = hbhunger.item_eat(def.saturation, def.replace, def.poisen, def.healing, def.sound) @@ -79,9 +79,9 @@ function hbhunger.item_eat(hunger_change, replace_with_item, poisen, heal, sound minetest.sound_play({name = sound or "hbhunger_eat_generic", gain = 1}, {pos=user:getpos(), max_hear_distance = 16}) -- Saturation - if h < 30 and hunger_change then + if h < 20 and hunger_change then h = h + hunger_change - if h > 30 then h = 30 end + if h > 20 then h = 20 end hbhunger.hunger[name] = h hbhunger.set_hunger_raw(user) end diff --git a/mods/hbhunger/init.lua b/mods/hbhunger/init.lua index 0a9a72222..867ded0b0 100644 --- a/mods/hbhunger/init.lua +++ b/mods/hbhunger/init.lua @@ -46,7 +46,7 @@ end dofile(minetest.get_modpath("hbhunger").."/hunger.lua") -- register satiation hudbar -hb.register_hudbar("satiation", 0xFFFFFF, S("Satiation"), { icon = "hbhunger_icon.png", bgicon = "hbhunger_bgicon.png", bar = "hbhunger_bar.png" }, 20, 30, false) +hb.register_hudbar("satiation", 0xFFFFFF, S("Satiation"), { icon = "hbhunger_icon.png", bgicon = "hbhunger_bgicon.png", bar = "hbhunger_bar.png" }, 20, 20, false) -- update hud elemtens if value has changed local function update_hud(player) @@ -78,7 +78,7 @@ hbhunger.set_hunger_raw = function(player) local name = player:get_player_name() local value = hbhunger.hunger[name] if not inv or not value then return nil end - if value > 30 then value = 30 end + if value > 20 then value = 20 end if value < 0 then value = 0 end inv:set_stack("hunger", 1, ItemStack({name=":", count=value+1})) @@ -107,26 +107,33 @@ minetest.register_on_respawnplayer(function(player) end) local main_timer = 0 -local timer = 0 +local timer = 0 -- Half second timer local timer2 = 0 +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 timer2 = timer2 + dtime - if main_timer > hbhunger.HUD_TICK or timer > 4 or timer2 > hbhunger.HUNGER_TICK then + if main_timer > hbhunger.HUD_TICK or timer > 0.5 or timer2 > hbhunger.HUNGER_TICK then if main_timer > hbhunger.HUD_TICK then main_timer = 0 end for _,player in ipairs(minetest.get_connected_players()) do local name = player:get_player_name() local h = tonumber(hbhunger.hunger[name]) local hp = player:get_hp() - if timer > 4 then - -- heal player by 1 hp if not dead and satiation is > 15 (of 30) - if h > 15 and hp > 0 and player:get_breath() > 0 then + if timer > 0.5 then + -- Quick heal (every 0.5s) + if h >= 20 and hp > 0 then player:set_hp(hp+1) - -- or damage player by 1 hp if satiation is < 2 (of 30) - elseif h <= 1 then - if hp-1 >= 0 then player:set_hp(hp-1) end + -- Slow heal, and hunger damage (every 4s) + elseif timerMult == 0 then + if h >= 18 and hp > 0 then + player:set_hp(hp+1) + elseif h == 0 then + -- Damage hungry player down to 1 HP + if hp-1 >= 0 then + player:set_hp(hp-1) + end end end -- lower satiation by 1 point after xx seconds @@ -147,8 +154,15 @@ minetest.register_globalstep(function(dtime) hbhunger.handle_node_actions(nil, nil, player) end end + end + end + if timer > 0.5 then + timer = 0 + timerMult = timerMult + 1 + if timerMult > 7 then + timerMult = 0 + end end - if timer > 4 then timer = 0 end if timer2 > hbhunger.HUNGER_TICK then timer2 = 0 end end)