]> git.lizzy.rs Git - Crafter.git/blobdiff - mods/hunger/init.lua
Balance hunger so it doesn't last forever
[Crafter.git] / mods / hunger / init.lua
index 7cbe9fcf8aa89313f583f87da79ceb1543472932..d06efccb278b66883f7c805d140e9365f640724c 100644 (file)
@@ -1,10 +1,5 @@
 minetest.register_on_joinplayer(function(player)
        local meta = player:get_meta()
-       --give players new hunger when they join
-       if meta:get_int("hunger") == 0 then
-               meta:set_int("hunger", 20)
-               meta:set_int("satiation", 5)
-       end
        player:hud_add({
                hud_elem_type = "statbar",
                position = {x = 0.5, y = 1},
@@ -18,7 +13,7 @@ minetest.register_on_joinplayer(function(player)
                hud_elem_type = "statbar",
                position = {x = 0.5, y = 1},
                text = "hunger_icon.png",
-               number = 20,
+               number = meta:get_int("hunger"),
                direction = 1,
                size = {x = 24, y = 24},
                offset = {x = 24*10, y= -(48 + 50 + 39)},
@@ -26,6 +21,23 @@ minetest.register_on_joinplayer(function(player)
        meta:set_int("hunger_bar", hunger_bar)
 end)
 
+
+minetest.register_on_newplayer(function(player)
+       local meta = player:get_meta()
+       --give players new hunger when they join
+       if meta:get_int("hunger") == 0 then
+               meta:set_int("hunger", 20)
+               meta:set_int("satiation", 5)
+       end
+       
+end)
+
+minetest.register_on_respawnplayer(function(player)
+       local meta = player:get_meta()
+       meta:set_int("hunger", 20)
+       meta:set_int("satiation", 5)
+end)
+
 local function hunger_update()
        for _,player in ipairs(minetest.get_connected_players()) do
                local meta = player:get_meta()
@@ -36,26 +48,25 @@ local function hunger_update()
                local sneaking = (meta:get_string("player.player_movement_state") == "3")               
                local got_hungry = math.random()
                if satiation > 0 then
-                       if running and got_hungry > 0.95 then
+                       if running and got_hungry > 0.959 then
                                satiation = satiation - 1
-                       elseif bunny_hopping and got_hungry > 0.90 then
+                       elseif bunny_hopping and got_hungry > 0.941 then
                                satiation = satiation - 1
-                       elseif sneaking and got_hungry > 0.997 then
+                       elseif sneaking and got_hungry > 0.9930 then
                                satiation = satiation - 1
-                       elseif got_hungry > 0.998 then
+                       elseif got_hungry > 0.97 then
                                satiation = satiation - 1
                        end
                end
-               
                if satiation == 0 then
                        if hunger > 0 then
-                               if running and got_hungry > 0.82 then
+                               if running and got_hungry > 0.922 then
                                        hunger = hunger - 1
-                               elseif bunny_hopping and got_hungry > 0.77 then
+                               elseif bunny_hopping and got_hungry > 0.917 then
                                        hunger = hunger - 1
-                               elseif sneaking and got_hungry > 0.954 then
+                               elseif sneaking and got_hungry > 0.941 then
                                        hunger = hunger - 1
-                               elseif got_hungry > 0.958 then
+                               elseif got_hungry > 0.937 then
                                        hunger = hunger - 1
                                end
                        end
@@ -76,7 +87,6 @@ local function hunger_update()
                                satiation = 0
                        end
                end
-               
                meta:set_int("satiation", satiation)
                local hunger_bar = meta:get_int("hunger_bar")
                player:hud_change(hunger_bar, "number", hunger)
@@ -89,6 +99,32 @@ end
 
 hunger_update()
 
+--take away hunger and satiation randomly while mining
+minetest.register_on_dignode(function(pos, oldnode, digger)
+       local meta = digger:get_meta()
+       local satiation = meta:get_int("satiation")
+       local hunger = meta:get_int("hunger")
+       local got_hungry = math.random()
+       if satiation > 0 then
+               if got_hungry > 0.955 then
+                       satiation = satiation - 1
+                       meta:set_int("satiation", satiation)
+               end
+       end
+       
+       if satiation == 0 then
+               if hunger > 0 then
+                       if got_hungry > 0.925 then
+                               hunger = hunger - 1
+                               meta:set_int("hunger", hunger)
+                               local hunger_bar = meta:get_int("hunger_bar")
+                               digger:hud_change(hunger_bar, "number", hunger)
+                       end
+               end
+       end
+end)
+
+
 --allow players to eat food
 function minetest.eat_food(player,item)
        local meta = player:get_meta()
@@ -123,4 +159,9 @@ function minetest.eat_food(player,item)
        
        meta:set_int("hunger", player_hunger)
        meta:set_int("satiation", player_satiation)
+       local hunger_bar = meta:get_int("hunger_bar")
+       player:hud_change(hunger_bar, "number", player_hunger)
+       local stack = player:get_wielded_item()
+       stack:take_item()
+       player:set_wielded_item(stack)
 end