]> git.lizzy.rs Git - Crafter.git/blob - mods/hunger/init.lua
remove server debug
[Crafter.git] / mods / hunger / init.lua
1 local minetest,math = minetest,math
2
3 local mod_storage = minetest.get_mod_storage()
4 local pool        = {}
5
6
7 -- loads data from mod storage
8 local name
9 local temp_pool
10 local load_data = function(player)
11         name = player:get_player_name()
12         pool[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")
19         else
20                 temp_pool.hunger                = 20
21                 temp_pool.satiation             = 20
22                 temp_pool.regeneration_interval = 0
23                 temp_pool.exhaustion            = 0
24         end
25 end
26
27 -- saves data to be utilized on next login
28 local name
29 local temp_pool
30 local save_data = function(name)
31         if type(name) ~= "string" and name:is_player() then
32                 name = name:get_player_name()
33         end
34         temp_pool = pool[name]
35         
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)
40
41         mod_storage:set_int(name.."h_save",1)
42
43         pool[name] = nil
44 end
45
46
47 -- is used for shutdowns to save all data
48 local save_all = function()
49         for name,_ in pairs(pool) do
50                 save_data(name)
51         end
52 end
53
54 -- an easy translation pool
55 local satiation_pool = {
56         [0]   = 1,
57         [0.5] = 3,
58         [1]   = 6,
59         [2]   = 8,
60         [3]   = 1
61 }
62 -- ticks up the exhaustion when counting down satiation
63 local tick_up_satiation = function(state,exhaustion)
64         return(exhaustion + satiation_pool[state])
65 end
66
67 -- an easy translation pool
68 local hunger_pool = {
69         [0]   = 1,
70         [0.5] = 2,
71         [1]   = 3,
72         [2]   = 4,
73         [3]   = 1
74 }
75 -- ticks up the exhaustion when counting down hunger
76 local tick_up_hunger = function(state,exhaustion)
77         return(exhaustion + hunger_pool[state])
78 end
79
80 -- allows other mods to set hunger data
81 local name
82 get_player_hunger = function(player)
83         name = player:get_player_name()
84         return(pool[name].hunger)
85 end
86
87
88 -- saves specific users data for when they relog
89 minetest.register_on_leaveplayer(function(player)
90         save_data(player)
91 end)
92
93 -- save all data to mod storage on shutdown
94 minetest.register_on_shutdown(function()
95         save_all()
96 end)
97
98 -- create new data for hunger per player
99 local name
100 minetest.register_on_joinplayer(function(player)
101         name = player:get_player_name()
102         load_data(player)
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",
107                 number        = 20,
108                 direction     = 1,
109                 size          = {x = 24, y = 24},
110                 offset        = {x = 24*10, y= -(48 + 24 + 39)},
111         })
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,
117                 direction     = 1,
118                 size          = {x = 24, y = 24},
119                 offset        = {x = 24*10, y= -(48 + 24 + 39)},
120         })
121 end)
122
123 -- resets the players hunger settings to max
124 local name
125 local temp_pool
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({
134                 player    =  player ,
135                 hud_name  = "hunger",
136                 element   = "number",
137                 data      =  temp_pool.hunger
138         })
139 end)
140
141
142 local exhaustion_peak  = 512
143 local hunger_peak      = 128
144 local temp_pool
145 local state
146 local input
147 local hp
148 local drowning
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]
155
156                         --movement state
157                         state = get_player_state(player)
158
159                         -- if player is moving in state 0 add 0.5
160                         if state == 0 then
161                                 input = player:get_player_control()
162                                 if input.jump or input.right or input.left or input.down or input.up then
163                                         state = 0.5
164                                 end
165                         end
166                         -- count down invisible satiation bar
167                         if temp_pool.satiation > 0 and temp_pool.hunger >= 20 then
168
169                                 temp_pool.exhaustion = tick_up_satiation(state, temp_pool.exhaustion)
170
171                                 if temp_pool.exhaustion > exhaustion_peak then
172
173                                         temp_pool.satiation = temp_pool.satiation - 1
174
175                                         temp_pool.exhaustion = temp_pool.exhaustion - exhaustion_peak
176                                         
177                                         --reset this to use for the hunger tick
178                                         if temp_pool.satiation == 0 then
179                                                 temp_pool.exhaustion = 0
180                                         end
181                                 end
182                         -- count down hunger bars
183                         elseif temp_pool.hunger > 0 then
184
185                                 temp_pool.exhaustion = tick_up_hunger(state,temp_pool.exhaustion)
186                                 
187                                 if temp_pool.exhaustion >= hunger_peak then
188                                         --don't allow hunger to go negative
189                                         if temp_pool.hunger > 0 then
190
191                                                 temp_pool.exhaustion = temp_pool.exhaustion - hunger_peak
192
193                                                 temp_pool.hunger = temp_pool.hunger - 1
194
195                                         end
196
197                                         hud_manager.change_hud({
198                                                 player    =  player ,
199                                                 hud_name  = "hunger",
200                                                 element   = "number",
201                                                 data      =  temp_pool.hunger
202                                         })
203                                 end
204                                 
205                         -- hurt the player if hunger bar empty
206                         elseif temp_pool.hunger <= 0 then
207
208                                 temp_pool.exhaustion = temp_pool.exhaustion + 1
209
210                                 hp = player:get_hp()
211
212                                 if hp > 0 and temp_pool.exhaustion >= 2 then
213                                         player:set_hp( hp - 1 )
214                                         temp_pool.exhaustion = 0
215                                 end                             
216                         end
217                         
218                         
219                         hp = player:get_hp()
220
221                         drowning = is_player_drowning(player)           
222
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 
225
226                                 temp_pool.regeneration_interval = temp_pool.regeneration_interval + 1
227
228                                 if temp_pool.regeneration_interval >= 2 then
229
230                                         player:set_hp( hp + 1 )
231
232                                         temp_pool.exhaustion = temp_pool.exhaustion + 32
233
234                                         temp_pool.regeneration_interval = 0
235
236                                 end
237                         --reset the regen interval
238                         else
239                                 temp_pool.regeneration_interval = 0
240                         end
241                 end
242         end
243         
244         minetest.after(0.5, function()
245                 hunger_update()
246         end)
247 end
248
249 minetest.register_on_mods_loaded(function()
250         minetest.after(0.5,function()
251                 hunger_update()
252         end)
253 end)
254
255 --take away hunger and satiation randomly while mining
256 local name
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)
261         end
262 end)
263
264 -- take the eaten food
265 local item
266 local take_food = function(player)
267         item = player:get_wielded_item()
268         item:take_item()
269         player:set_wielded_item(item)
270 end
271
272 -- players eat food
273 local name
274 local temp_pool
275 local item
276 local satiation
277 local hunger
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)
285         end
286         item = item:get_name()
287         
288         satiation = minetest.get_item_group( item, "satiation" )
289         hunger    = minetest.get_item_group( item, "hunger"    )
290         
291         temp_pool.hunger = temp_pool.hunger + hunger
292
293         if temp_pool.hunger > 20 then
294                 temp_pool.hunger = 20
295         end
296         
297         -- unlimited
298         -- this makes the game easier
299         temp_pool.satiation = temp_pool.satiation + satiation
300         
301         take_food(player)
302
303         hud_manager.change_hud({
304                 player    =  player ,
305                 hud_name  = "hunger",
306                 element   = "number",
307                 data      =  temp_pool.hunger
308         })
309 end
310
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},
317         })
318
319         minetest.register_node(":"..name.."node", {
320                 tiles = {def.texture},
321                 drawtype = "allfaces",
322         })
323 end
324
325
326 minetest.register_chatcommand("hungry", {
327         params = "<mob>",
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
333                 temp_pool.hunger     = 1
334                 temp_pool.satiation  = 0
335                 hud_manager.change_hud({
336                         player    =  minetest.get_player_by_name(name) ,
337                         hud_name  = "hunger",
338                         element   = "number",
339                         data      =  temp_pool.hunger
340                 })
341         end
342 })