summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--functions.lua30
-rw-r--r--init.lua10
2 files changed, 24 insertions, 16 deletions
diff --git a/functions.lua b/functions.lua
index d3c8af3..80f356f 100644
--- a/functions.lua
+++ b/functions.lua
@@ -65,6 +65,10 @@ function hunger.handle_node_actions(pos, oldnode, player, ext)
return
end
local name = player:get_player_name()
+ if not name or not hunger[name] then
+ return
+ end
+
local exhaus = hunger[name].exhaus
if not exhaus then
hunger[name].exhaus = 0
@@ -96,12 +100,13 @@ function hunger.handle_node_actions(pos, oldnode, player, ext)
hunger[name].exhaus = exhaus
end
+
-- Time based hunger functions
-if minetest.setting_getbool("enable_damage") then
- local hunger_timer = 0
- local health_timer = 0
- local action_timer = 0
- minetest.register_globalstep(function(dtime)
+local hunger_timer = 0
+local health_timer = 0
+local action_timer = 0
+
+local function hunger_globaltimer(dtime)
hunger_timer = hunger_timer + dtime
health_timer = health_timer + dtime
action_timer = action_timer + dtime
@@ -141,8 +146,8 @@ if minetest.setting_getbool("enable_damage") then
local air = player:get_breath() or 0
local hp = player:get_hp()
- -- heal player by 1 hp if not dead and saturation is > 15 (of 30)
- if tonumber(tab.lvl) > HUNGER_HEAL_LVL and air > 0 then
+ -- heal player by 1 hp if not dead and saturation is > 15 (of 30) player is not drowning
+ if tonumber(tab.lvl) > HUNGER_HEAL_LVL and hp > 0 and air > 0 then
player:set_hp(hp + HUNGER_HEAL)
end
@@ -155,7 +160,10 @@ if minetest.setting_getbool("enable_damage") then
health_timer = 0
end
- end)
+end
+
+if minetest.setting_getbool("enable_damage") then
+ minetest.register_globalstep(hunger_globaltimer)
end
@@ -166,9 +174,9 @@ function hunger.register_food(name, hunger_change, replace_with_item, poisen, he
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
+ 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
-- Poison player
diff --git a/init.lua b/init.lua
index ed855ec..efbe5e5 100644
--- a/init.lua
+++ b/init.lua
@@ -1,7 +1,7 @@
hunger = {}
hunger.food = {}
-HUNGER_TICK = 800 -- time in seconds after that 1 hunger point is taken
+HUNGER_TICK = 800 -- time in seconds after that 1 hunger point is taken
HUNGER_HEALTH_TICK = 4 -- time in seconds after player gets healed/damaged
HUNGER_MOVE_TICK = 0.5 -- time in seconds after the movement is checked
@@ -10,12 +10,12 @@ HUNGER_EXHAUST_PLACE = 1 -- exhaustion increased this value after placed
HUNGER_EXHAUST_MOVE = 1.5 -- exhaustion increased this value if player movement detected
HUNGER_EXHAUST_LVL = 160 -- at what exhaustion player saturation gets lowered
-HUNGER_HEAL = 1 -- number of HP player gets healed after HUNGER_HEALTH_TICK
+HUNGER_HEAL = 1 -- number of HP player gets healed after HUNGER_HEALTH_TICK
HUNGER_HEAL_LVL = 15 -- lower level of saturation needed to get healed
-HUNGER_STARVE = 1 -- number of HP player gets damaged by hunger after HUNGER_HEALTH_TICK
+HUNGER_STARVE = 1 -- number of HP player gets damaged by hunger after HUNGER_HEALTH_TICK
HUNGER_STARVE_LVL = 3 -- level of staturation that causes starving
-HUNGER_MAX = 30 -- maximum level of saturation
+HUNGER_MAX = 30 -- maximum level of saturation
local modpath = minetest.get_modpath("hunger")
@@ -39,7 +39,7 @@ if minetest.setting_getbool("enable_damage") then
lvl = 20
end
minetest.after(0.8, function()
- hud.change_item(player, "hunger", {offset = "item", item_name = "air"})
+ hud.swap_statbar(player, "hunger", "air")
hud.change_item(player, "hunger", {number = lvl, max = 20})
end)
end)