1 minetest.register_on_joinplayer(function(player)
2 local meta = player:get_meta()
3 --give players new hunger when they join
4 if meta:get_int("hunger") == 0 then
5 meta:set_int("hunger", 20)
6 meta:set_int("satiation", 5)
9 hud_elem_type = "statbar",
10 position = {x = 0.5, y = 1},
11 text = "hunger_icon_bg.png",
14 size = {x = 24, y = 24},
15 offset = {x = 24*10, y= -(48 + 50 + 39)},
17 local hunger_bar = player:hud_add({
18 hud_elem_type = "statbar",
19 position = {x = 0.5, y = 1},
20 text = "hunger_icon.png",
23 size = {x = 24, y = 24},
24 offset = {x = 24*10, y= -(48 + 50 + 39)},
26 meta:set_int("hunger_bar", hunger_bar)
29 minetest.register_on_respawnplayer(function(player)
30 local meta = player:get_meta()
31 meta:set_int("hunger", 20)
32 meta:set_int("satiation", 5)
35 local function hunger_update()
36 for _,player in ipairs(minetest.get_connected_players()) do
37 local meta = player:get_meta()
38 local satiation = meta:get_int("satiation")
39 local hunger = meta:get_int("hunger")
40 local running = (meta:get_string("player.player_movement_state") == "1")
41 local bunny_hopping = (meta:get_string("player.player_movement_state") == "2")
42 local sneaking = (meta:get_string("player.player_movement_state") == "3")
43 local got_hungry = math.random()
45 if running and got_hungry > 0.95 then
46 satiation = satiation - 1
47 elseif bunny_hopping and got_hungry > 0.90 then
48 satiation = satiation - 1
49 elseif sneaking and got_hungry > 0.998 then
50 satiation = satiation - 1
51 elseif got_hungry > 0.996 then
52 satiation = satiation - 1
56 if satiation == 0 then
58 if running and got_hungry > 0.82 then
60 elseif bunny_hopping and got_hungry > 0.77 then
62 elseif sneaking and got_hungry > 0.968 then
64 elseif got_hungry > 0.954 then
68 meta:set_int("hunger", hunger)
70 local hp = player:get_hp()
77 local hp = player:get_hp()
78 if hunger >= 20 and hp < 20 then
80 satiation = satiation - 1
86 meta:set_int("satiation", satiation)
87 local hunger_bar = meta:get_int("hunger_bar")
88 player:hud_change(hunger_bar, "number", hunger)
91 minetest.after(1, function()
98 --allow players to eat food
99 function minetest.eat_food(player,item)
100 local meta = player:get_meta()
102 local player_hunger = meta:get_int("hunger")
103 local player_satiation = meta:get_int("satiation")
106 if type(item) == "string" then
107 item = ItemStack(item)
108 elseif type(item) == "table" then
109 item = ItemStack(item.name)
112 item = item:get_name()
114 local satiation = minetest.get_item_group(item, "satiation")
115 local hunger = minetest.get_item_group(item, "hunger")
117 if player_hunger < 20 then
118 player_hunger = player_hunger + hunger
119 if player_hunger > 20 then
123 if player_satiation < 20 then
124 player_satiation = player_satiation + satiation
125 if player_satiation > 20 then
126 player_satiation = 20
130 meta:set_int("hunger", player_hunger)
131 meta:set_int("satiation", player_satiation)
132 local hunger_bar = meta:get_int("hunger_bar")
133 player:hud_change(hunger_bar, "number", player_hunger)
134 local stack = player:get_wielded_item()
136 player:set_wielded_item(stack)