1 local minetest,math = minetest,math
3 local mod_storage = minetest.get_mod_storage()
7 -- loads data from mod storage
10 local load_data = function(player)
11 name = player:get_player_name()
13 temp_pool = pool[name]
14 if mod_storage:get_int(name.."h_save") > 0 then
15 temp_pool.hunger = mod_storage:get_int(name.."hunger" )
16 temp_pool.satiation = mod_storage:get_int(name.."satiation" )
17 temp_pool.exhaustion = mod_storage:get_int(name.."exhaustion" )
18 temp_pool.regeneration_interval = mod_storage:get_int(name.."regeneration_interval")
21 temp_pool.satiation = 20
22 temp_pool.regeneration_interval = 0
23 temp_pool.exhaustion = 0
27 -- saves data to be utilized on next login
30 local save_data = function(name)
31 if type(name) ~= "string" and name:is_player() then
32 name = name:get_player_name()
34 temp_pool = pool[name]
36 mod_storage:set_int(name.."hunger", temp_pool.hunger )
37 mod_storage:set_int(name.."satiation", temp_pool.satiation )
38 mod_storage:set_int(name.."exhaustion", temp_pool.exhaustion )
39 mod_storage:set_int(name.."regeneration_interval",temp_pool.regeneration_interval)
41 mod_storage:set_int(name.."h_save",1)
47 -- is used for shutdowns to save all data
48 local save_all = function()
49 for name,_ in pairs(pool) do
54 -- an easy translation pool
55 local satiation_pool = {
62 -- ticks up the exhaustion when counting down satiation
63 local tick_up_satiation = function(state,exhaustion)
64 return(exhaustion + satiation_pool[state])
67 -- an easy translation pool
75 -- ticks up the exhaustion when counting down hunger
76 local tick_up_hunger = function(state,exhaustion)
77 return(exhaustion + hunger_pool[state])
80 -- allows other mods to set hunger data
82 get_player_hunger = function(player)
83 name = player:get_player_name()
84 return(pool[name].hunger)
88 -- saves specific users data for when they relog
89 minetest.register_on_leaveplayer(function(player)
93 -- save all data to mod storage on shutdown
94 minetest.register_on_shutdown(function()
98 -- create new data for hunger per player
100 minetest.register_on_joinplayer(function(player)
101 name = player:get_player_name()
103 hud_manager.add_hud(player,"hunger_bg",{
104 hud_elem_type = "statbar",
105 position = {x = 0.5, y = 1},
106 text = "hunger_icon_bg.png",
109 size = {x = 24, y = 24},
110 offset = {x = 24*10, y= -(48 + 24 + 39)},
112 hud_manager.add_hud(player,"hunger",{
113 hud_elem_type = "statbar",
114 position = {x = 0.5, y = 1},
115 text = "hunger_icon.png",
116 number = pool[name].hunger,
118 size = {x = 24, y = 24},
119 offset = {x = 24*10, y= -(48 + 24 + 39)},
123 -- resets the players hunger settings to max
126 minetest.register_on_respawnplayer(function(player)
127 name = player:get_player_name()
128 temp_pool = pool[name]
129 temp_pool.hunger = 20
130 temp_pool.satiation = 20
131 temp_pool.regeneration_interval = 0
132 temp_pool.exhaustion = 0
133 hud_manager.change_hud({
137 data = temp_pool.hunger
142 local exhaustion_peak = 512
143 local hunger_peak = 128
149 hunger_update = function()
150 for _,player in ipairs(minetest.get_connected_players()) do
151 --do not regen player's health if dead - this will be reused for 1up apples
152 if player:get_hp() > 0 then
153 name = player:get_player_name()
154 temp_pool = pool[name]
157 state = get_player_state(player)
159 -- if player is moving in state 0 add 0.5
161 input = player:get_player_control()
162 if input.jump or input.right or input.left or input.down or input.up then
166 -- count down invisible satiation bar
167 if temp_pool.satiation > 0 and temp_pool.hunger >= 20 then
169 temp_pool.exhaustion = tick_up_satiation(state, temp_pool.exhaustion)
171 if temp_pool.exhaustion > exhaustion_peak then
173 temp_pool.satiation = temp_pool.satiation - 1
175 temp_pool.exhaustion = temp_pool.exhaustion - exhaustion_peak
177 --reset this to use for the hunger tick
178 if temp_pool.satiation == 0 then
179 temp_pool.exhaustion = 0
182 -- count down hunger bars
183 elseif temp_pool.hunger > 0 then
185 temp_pool.exhaustion = tick_up_hunger(state,temp_pool.exhaustion)
187 if temp_pool.exhaustion >= hunger_peak then
188 --don't allow hunger to go negative
189 if temp_pool.hunger > 0 then
191 temp_pool.exhaustion = temp_pool.exhaustion - hunger_peak
193 temp_pool.hunger = temp_pool.hunger - 1
197 hud_manager.change_hud({
201 data = temp_pool.hunger
205 -- hurt the player if hunger bar empty
206 elseif temp_pool.hunger <= 0 then
208 temp_pool.exhaustion = temp_pool.exhaustion + 1
212 if hp > 0 and temp_pool.exhaustion >= 2 then
213 player:set_hp( hp - 1 )
214 temp_pool.exhaustion = 0
221 drowning = is_player_drowning(player)
223 --make regeneration happen every second
224 if not is_player_on_fire(player) and drowning == 0 and temp_pool.hunger >= 20 and hp < 20 then -- meta:get_int("on_fire") == 0
226 temp_pool.regeneration_interval = temp_pool.regeneration_interval + 1
228 if temp_pool.regeneration_interval >= 2 then
230 player:set_hp( hp + 1 )
232 temp_pool.exhaustion = temp_pool.exhaustion + 32
234 temp_pool.regeneration_interval = 0
237 --reset the regen interval
239 temp_pool.regeneration_interval = 0
244 minetest.after(0.5, function()
249 minetest.register_on_mods_loaded(function()
250 minetest.after(0.5,function()
255 --take away hunger and satiation randomly while mining
257 minetest.register_on_dignode(function(pos, oldnode, digger)
258 if digger and digger:is_player() then
259 name = digger:get_player_name()
260 pool[name].exhaustion = pool[name].exhaustion + math.random(0,2)
264 -- take the eaten food
266 local take_food = function(player)
267 item = player:get_wielded_item()
269 player:set_wielded_item(item)
278 player_eat_food = function(player,item)
279 name = player:get_player_name()
280 temp_pool = pool[name]
281 if type(item) == "string" then
282 item = ItemStack(item)
283 elseif type(item) == "table" then
284 item = ItemStack(item.name)
286 item = item:get_name()
288 satiation = minetest.get_item_group( item, "satiation" )
289 hunger = minetest.get_item_group( item, "hunger" )
291 temp_pool.hunger = temp_pool.hunger + hunger
293 if temp_pool.hunger > 20 then
294 temp_pool.hunger = 20
298 -- this makes the game easier
299 temp_pool.satiation = temp_pool.satiation + satiation
303 hud_manager.change_hud({
307 data = temp_pool.hunger
311 -- easily allows mods to register food
312 minetest.register_food = function(name,def)
313 minetest.register_craftitem(":"..name, {
314 description = def.description,
315 inventory_image = def.texture,
316 groups = {satiation=def.satiation,hunger=def.hunger},
319 minetest.register_node(":"..name.."node", {
320 tiles = {def.texture},
321 drawtype = "allfaces",
326 minetest.register_chatcommand("hungry", {
328 description = "A debug command to test food",
329 privs = {server = true},
330 func = function(name)
331 local temp_pool = pool[name]
332 temp_pool.exhaustion = 0
334 temp_pool.satiation = 0
335 hud_manager.change_hud({
336 player = minetest.get_player_by_name(name) ,
339 data = temp_pool.hunger