1 minetest.register_on_joinplayer(function(player)
2 local meta = player:get_meta()
4 hud_elem_type = "statbar",
5 position = {x = 0.5, y = 1},
6 text = "hunger_icon_bg.png",
9 size = {x = 24, y = 24},
10 offset = {x = 24*10, y= -(48 + 50 + 39)},
12 local hunger_bar = player:hud_add({
13 hud_elem_type = "statbar",
14 position = {x = 0.5, y = 1},
15 text = "hunger_icon.png",
16 number = meta:get_int("hunger"),
18 size = {x = 24, y = 24},
19 offset = {x = 24*10, y= -(48 + 50 + 39)},
21 meta:set_int("hunger_bar", hunger_bar)
25 minetest.register_on_newplayer(function(player)
26 local meta = player:get_meta()
27 --give players new hunger when they join
28 if meta:get_int("hunger") == 0 then
29 meta:set_int("hunger", 20)
30 meta:set_int("satiation", 5)
31 meta:set_int("exhaustion_tick", 0)
36 minetest.register_on_respawnplayer(function(player)
37 local meta = player:get_meta()
38 meta:set_int("hunger", 20)
39 meta:set_int("satiation", 5)
40 meta:set_int("exhaustion_tick", 0)
41 meta:set_int("dead", 0)
42 local hunger_bar = meta:get_int("hunger_bar")
43 player:hud_change(hunger_bar, "number", 20)
46 minetest.register_on_dieplayer(function(player)
47 local meta = player:get_meta()
48 meta:set_int("dead", 1)
51 --this is the max exhaustion a player will get before their
52 --satiation goes down and rolls over
53 local exhaustion_peak = 384
54 --when satiation runs out this is when the hunger peak variable
55 --is used, everytime the player rolls over this their hunger ticks down
56 --based on what they're doing
57 local hunger_peak = 64
60 local function hunger_update()
61 for _,player in ipairs(minetest.get_connected_players()) do
64 local meta = player:get_meta()
66 --do not regen player's health if dead - this will be reused for 1up apples
67 if meta:get_int("dead") == 0 then --and meta:get_int("regeneration")
70 local satiation = meta:get_int("satiation")
71 local hunger = meta:get_int("hunger")
72 local exhaustion_tick = meta:get_int("exhaustion_tick")
75 local movement_state = meta:get_string("player.player_movement_state")
76 local running = (movement_state == "1")
77 local bunny_hopping = (movement_state == "2")
78 local sneaking = (movement_state == "3")
79 local standing = false
82 --we must seperate these two values because I forgot to
83 --write in a seperate clientside state for walking/standing
84 if movement_state == "0" then
85 local input = player:get_player_control()
86 if input.jump or input.right or input.left or input.down or input.up then
93 --we count up the exhaustion of the player moving around
94 --based on their states
97 exhaustion_tick = exhaustion_tick + 6
98 elseif bunny_hopping then
99 exhaustion_tick = exhaustion_tick + 8
101 exhaustion_tick = exhaustion_tick + 1
104 exhaustion_tick = exhaustion_tick + 3
106 exhaustion_tick = exhaustion_tick + 1
110 if exhaustion_tick >= exhaustion_peak then
111 print("should be wroking")
112 satiation = satiation - 1
113 exhaustion_tick = exhaustion_tick - exhaustion_peak
115 --reset this to use for the hunger tick
116 if satiation == 0 then
120 meta:set_int("satiation", satiation)
123 meta:set_int("exhaustion_tick", exhaustion_tick)
124 elseif hunger > 0 then
125 --this is copied again because this is for future tuning
127 exhaustion_tick = exhaustion_tick + 3
128 elseif bunny_hopping then
129 exhaustion_tick = exhaustion_tick + 4
131 exhaustion_tick = exhaustion_tick + 1
133 exhaustion_tick = exhaustion_tick + 2
135 exhaustion_tick = exhaustion_tick + 1
137 if exhaustion_tick >= hunger_peak then
138 --don't allow hunger to go negative
142 meta:set_int("hunger", hunger)
143 local hunger_bar = meta:get_int("hunger_bar")
144 player:hud_change(hunger_bar, "number", hunger)
147 meta:set_int("exhaustion_tick", exhaustion_tick)
148 elseif hunger <= 0 then
149 exhaustion_tick = exhaustion_tick + 1
151 local hp = player:get_hp()
152 if hp > 0 and exhaustion_tick >= 2 then
156 meta:set_int("exhaustion_tick", exhaustion_tick)
159 print("satiation:",satiation,"exhaustion_tick:",exhaustion_tick)
160 local hp = player:get_hp()
161 if hunger >= 20 and hp < 20 then
164 satiation = satiation - 1
165 if satiation < 0 then
168 meta:set_int("exhaustion_tick", exhaustion_tick)
171 meta:set_int("satiation", satiation)
175 minetest.after(0.5, function()
182 --take away hunger and satiation randomly while mining
183 minetest.register_on_dignode(function(pos, oldnode, digger)
184 local meta = digger:get_meta()
185 local exhaustion_tick = meta:get_int("exhaustion_tick")
186 exhaustion_tick = exhaustion_tick + math.random(0,2)
187 meta:set_int("exhaustion_tick", exhaustion_tick)
190 --allow players to eat food
191 function minetest.eat_food(player,item)
192 local meta = player:get_meta()
194 local player_hunger = meta:get_int("hunger")
195 local player_satiation = meta:get_int("satiation")
198 if type(item) == "string" then
199 item = ItemStack(item)
200 elseif type(item) == "table" then
201 item = ItemStack(item.name)
204 item = item:get_name()
206 local satiation = minetest.get_item_group(item, "satiation")
207 local hunger = minetest.get_item_group(item, "hunger")
209 if player_hunger < 20 then
210 player_hunger = player_hunger + hunger
211 if player_hunger > 20 then
215 if player_satiation < 20 then
216 player_satiation = player_satiation + satiation
217 if player_satiation > 20 then
218 player_satiation = 20
222 meta:set_int("exhaustion_tick", 0)
223 meta:set_int("hunger", player_hunger)
224 meta:set_int("satiation", player_satiation)
225 local hunger_bar = meta:get_int("hunger_bar")
226 player:hud_change(hunger_bar, "number", player_hunger)
227 local stack = player:get_wielded_item()
229 player:set_wielded_item(stack)