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")
77 local movement_state = meta:get_string("player.player_movement_state")
78 local running = (movement_state == "1")
79 local bunny_hopping = (movement_state == "2")
80 local sneaking = (movement_state == "3")
81 local standing = false
84 --we must seperate these two values because I forgot to
85 --write in a seperate clientside state for walking/standing
86 if movement_state == "0" then
87 local input = player:get_player_control()
88 if input.jump or input.right or input.left or input.down or input.up then
95 --we count up the exhaustion of the player moving around
96 --based on their states
97 if satiation > 0 and hunger >= 20 then
99 exhaustion_tick = exhaustion_tick + 6
100 elseif bunny_hopping then
101 exhaustion_tick = exhaustion_tick + 8
103 exhaustion_tick = exhaustion_tick + 1
106 exhaustion_tick = exhaustion_tick + 3
108 exhaustion_tick = exhaustion_tick + 1
112 if exhaustion_tick >= exhaustion_peak then
113 satiation = satiation - 1
114 exhaustion_tick = exhaustion_tick - exhaustion_peak
116 --reset this to use for the hunger tick
117 if satiation == 0 then
121 meta:set_int("satiation", satiation)
124 meta:set_int("exhaustion_tick", exhaustion_tick)
125 elseif hunger > 0 then
126 --this is copied again because this is for future tuning
128 exhaustion_tick = exhaustion_tick + 3
129 elseif bunny_hopping then
130 exhaustion_tick = exhaustion_tick + 4
132 exhaustion_tick = exhaustion_tick + 1
134 exhaustion_tick = exhaustion_tick + 2
136 exhaustion_tick = exhaustion_tick + 1
138 if exhaustion_tick >= hunger_peak then
139 --don't allow hunger to go negative
143 meta:set_int("hunger", hunger)
144 local hunger_bar = meta:get_int("hunger_bar")
145 player:hud_change(hunger_bar, "number", hunger)
148 meta:set_int("exhaustion_tick", exhaustion_tick)
149 elseif hunger <= 0 then
150 exhaustion_tick = exhaustion_tick + 1
152 local hp = player:get_hp()
153 if hp > 0 and exhaustion_tick >= 2 then
157 meta:set_int("exhaustion_tick", exhaustion_tick)
161 local hp = player:get_hp()
162 --make regeneration happen every second
163 if meta:get_int("drowning") == 0 and hunger >= 20 and hp < 20 and satiation > 0 then
164 local regeneration_interval = meta:get_int("regeneration_interval")
165 --print(regeneration_interval,"--------------------------")
166 regeneration_interval = regeneration_interval + 1
167 if regeneration_interval >= 2 then
169 exhaustion_tick = exhaustion_tick + 32
170 meta:set_int("exhaustion_tick", exhaustion_tick)
171 meta:set_int("satiation", satiation)
172 regeneration_interval = 0
174 meta:set_int("regeneration_interval",regeneration_interval)
175 --reset the regen interval
177 meta:set_int("regeneration_interval",0)
180 --print("satiation:",satiation,"exhaustion_tick:",exhaustion_tick)
184 minetest.after(0.5, function()
189 minetest.register_on_mods_loaded(function()
190 minetest.after(0,function()
195 --take away hunger and satiation randomly while mining
196 minetest.register_on_dignode(function(pos, oldnode, digger)
197 if digger and digger:is_player() then
198 local meta = digger:get_meta()
199 local exhaustion_tick = meta:get_int("exhaustion_tick")
200 exhaustion_tick = exhaustion_tick + math.random(0,2)
201 meta:set_int("exhaustion_tick", exhaustion_tick)
205 --allow players to eat food
206 function minetest.eat_food(player,item)
207 local meta = player:get_meta()
209 local player_hunger = meta:get_int("hunger")
210 local player_satiation = meta:get_int("satiation")
213 if type(item) == "string" then
214 item = ItemStack(item)
215 elseif type(item) == "table" then
216 item = ItemStack(item.name)
219 item = item:get_name()
221 local satiation = minetest.get_item_group(item, "satiation")
222 local hunger = minetest.get_item_group(item, "hunger")
224 if player_hunger < 20 then
225 player_hunger = player_hunger + hunger
226 if player_hunger > 20 then
230 if player_satiation < satiation then
231 player_satiation = satiation
234 meta:set_int("exhaustion_tick", 0)
235 meta:set_int("hunger", player_hunger)
236 meta:set_int("satiation", player_satiation)
237 local hunger_bar = meta:get_int("hunger_bar")
238 player:hud_change(hunger_bar, "number", player_hunger)
239 local stack = player:get_wielded_item()
241 player:set_wielded_item(stack)
245 function minetest.register_food(name,def)
246 minetest.register_craftitem(":"..name, {
247 description = def.description,
248 inventory_image = def.texture,
249 groups = {satiation=def.satiation,hunger=def.hunger},
252 minetest.register_node(":"..name.."node", {
254 tiles = {def.texture},
257 drawtype = "allfaces",
258 on_construct = function(pos)
259 minetest.remove_node(pos)
264 minetest.register_chatcommand("hungry", {
266 description = "A debug command to test food",
267 privs = {server = true},
268 func = function(name)
269 local player = minetest.get_player_by_name(name)
270 local meta = player:get_meta()
271 meta:set_int("exhaustion_tick", 0)
272 meta:set_int("hunger", 1)
273 meta:set_int("satiation", 0)
274 local hunger_bar = meta:get_int("hunger_bar")
275 player:hud_change(hunger_bar, "number", 1)