]> git.lizzy.rs Git - Crafter.git/blob - mods/hunger/init.lua
Fix regeneration not continuing with full hunger bar
[Crafter.git] / mods / hunger / init.lua
1 minetest.register_on_joinplayer(function(player)
2         local meta = player:get_meta()
3         player:hud_add({
4                 hud_elem_type = "statbar",
5                 position = {x = 0.5, y = 1},
6                 text = "hunger_icon_bg.png",
7                 number = 20,
8                 direction = 1,
9                 size = {x = 24, y = 24},
10                 offset = {x = 24*10, y= -(48 + 50 + 39)},
11         })
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"),
17                 direction = 1,
18                 size = {x = 24, y = 24},
19                 offset = {x = 24*10, y= -(48 + 50 + 39)},
20         })
21         meta:set_int("hunger_bar", hunger_bar)
22 end)
23
24
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)
32         end
33         
34 end)
35
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)
44 end)
45
46 minetest.register_on_dieplayer(function(player)
47         local meta = player:get_meta()
48         meta:set_int("dead", 1)
49 end)
50
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
58
59
60 local function hunger_update()
61         for _,player in ipairs(minetest.get_connected_players()) do
62         
63                 --get the metas
64                 local meta = player:get_meta()
65                 
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")
68                 
69                         --internal variables
70                         local satiation = meta:get_int("satiation")
71                         local hunger = meta:get_int("hunger")
72                         local exhaustion_tick = meta:get_int("exhaustion_tick")
73
74                         --movement states
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
80                         local walking = false
81                         
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
87                                         walking = true
88                                 else
89                                         standing = true
90                                 end
91                         end
92                         
93                         --we count up the exhaustion of the player moving around
94                         --based on their states
95                         if satiation > 0 and hunger >= 20 then
96                                 if running then
97                                         exhaustion_tick = exhaustion_tick + 6
98                                 elseif bunny_hopping then
99                                         exhaustion_tick = exhaustion_tick + 8
100                                 elseif sneaking then
101                                         exhaustion_tick = exhaustion_tick + 1
102                                         
103                                 elseif walking then
104                                         exhaustion_tick = exhaustion_tick + 3
105                                 elseif standing then
106                                         exhaustion_tick = exhaustion_tick + 1
107                                 end
108                                 
109                                 
110                                 if exhaustion_tick >= exhaustion_peak then
111                                         satiation = satiation - 1
112                                         exhaustion_tick = exhaustion_tick - exhaustion_peak
113                                         
114                                         --reset this to use for the hunger tick
115                                         if satiation == 0 then
116                                                 exhaustion_tick = 0
117                                         end
118                                         
119                                         meta:set_int("satiation", satiation)
120                                 end
121                                 
122                                 meta:set_int("exhaustion_tick", exhaustion_tick)
123                         elseif hunger > 0 then
124                                 --this is copied again because this is for future tuning
125                                 if running then
126                                         exhaustion_tick = exhaustion_tick + 3
127                                 elseif bunny_hopping then
128                                         exhaustion_tick = exhaustion_tick + 4
129                                 elseif sneaking then
130                                         exhaustion_tick = exhaustion_tick + 1
131                                 elseif walking then
132                                         exhaustion_tick = exhaustion_tick + 2
133                                 elseif standing then
134                                         exhaustion_tick = exhaustion_tick + 1
135                                 end
136                                 if exhaustion_tick >= hunger_peak then
137                                         --don't allow hunger to go negative
138                                         if hunger > 0 then
139                                                 exhaustion_tick = 0
140                                                 hunger = hunger - 1
141                                                 meta:set_int("hunger", hunger)
142                                                 local hunger_bar = meta:get_int("hunger_bar")
143                                                 player:hud_change(hunger_bar, "number", hunger)
144                                         end
145                                 end
146                                 meta:set_int("exhaustion_tick", exhaustion_tick)
147                         elseif hunger <= 0 then
148                                 exhaustion_tick = exhaustion_tick + 1
149                                 
150                                 local hp =  player:get_hp()
151                                 if hp > 0 and exhaustion_tick >= 2 then
152                                         player:set_hp(hp-1)
153                                         exhaustion_tick = 0
154                                 end
155                                 meta:set_int("exhaustion_tick", exhaustion_tick)
156                         end
157                         
158                         
159                         local hp = player:get_hp()
160                         --make regeneration happen every second
161                         if meta:get_int("drowning") == 0 and hunger >= 20 and hp < 20 then
162                                 local regeneration_interval = meta:get_int("regeneration_interval")
163                                 --print(regeneration_interval,"--------------------------")
164                                 regeneration_interval = regeneration_interval + 1
165                                 if regeneration_interval >= 2 then
166                                         player:set_hp(hp+1)
167                                         exhaustion_tick = exhaustion_tick + 32
168                                         meta:set_int("exhaustion_tick", exhaustion_tick)
169                                         meta:set_int("satiation", satiation)
170                                         regeneration_interval = 0
171                                 end
172                                 meta:set_int("regeneration_interval",regeneration_interval)
173                         --reset the regen interval
174                         else
175                                 meta:set_int("regeneration_interval",0)
176                         end
177                         
178                         --print("satiation:",satiation,"exhaustion_tick:",exhaustion_tick)
179                 end
180         end
181         
182         minetest.after(0.5, function()
183                 hunger_update()
184         end)
185 end
186
187 minetest.register_on_mods_loaded(function()
188         minetest.after(0,function()
189                 hunger_update()
190         end)
191 end)
192
193 --take away hunger and satiation randomly while mining
194 minetest.register_on_dignode(function(pos, oldnode, digger)
195         if digger and digger:is_player() then
196                 local meta = digger:get_meta()
197                 local exhaustion_tick = meta:get_int("exhaustion_tick")
198                 exhaustion_tick = exhaustion_tick + math.random(0,2)
199                 meta:set_int("exhaustion_tick", exhaustion_tick)
200         end
201 end)
202
203 --allow players to eat food
204 function minetest.eat_food(player,item)
205         local meta = player:get_meta()
206         
207         local player_hunger = meta:get_int("hunger")
208         local player_satiation = meta:get_int("satiation")
209         
210         
211         if type(item) == "string" then
212                 item = ItemStack(item)
213         elseif type(item) == "table" then
214                 item = ItemStack(item.name)
215         end
216         
217         item = item:get_name()
218         
219         local satiation = minetest.get_item_group(item, "satiation")
220         local hunger = minetest.get_item_group(item, "hunger")
221         
222         if player_hunger < 20 then
223                 player_hunger = player_hunger + hunger
224                 if player_hunger > 20 then
225                         player_hunger = 20
226                 end
227         end
228         if player_satiation < satiation then
229                 player_satiation =  satiation
230         end
231         
232         meta:set_int("exhaustion_tick", 0)
233         meta:set_int("hunger", player_hunger)
234         meta:set_int("satiation", player_satiation)
235         local hunger_bar = meta:get_int("hunger_bar")
236         player:hud_change(hunger_bar, "number", player_hunger)
237         local stack = player:get_wielded_item()
238         stack:take_item()
239         player:set_wielded_item(stack)
240 end
241
242
243 function minetest.register_food(name,def)
244         minetest.register_craftitem(":"..name, {
245                 description = def.description,
246                 inventory_image = def.texture,
247                 groups = {satiation=def.satiation,hunger=def.hunger},
248         })
249
250         minetest.register_node(":"..name.."node", {
251                 description = "NIL",
252                 tiles = {def.texture},
253                 groups = {},
254                 drop = "",
255                 drawtype = "allfaces",
256                 on_construct = function(pos)
257                         minetest.remove_node(pos)
258                 end,
259         })
260 end
261
262 minetest.register_chatcommand("hungry", {
263         params = "<mob>",
264         description = "A debug command to test food",
265         privs = {server = true},
266         func = function(name)
267                 local player = minetest.get_player_by_name(name)
268                 local meta = player:get_meta()
269                 meta:set_int("exhaustion_tick", 0)
270                 meta:set_int("hunger", 1)
271                 meta:set_int("satiation", 0)
272                 local hunger_bar = meta:get_int("hunger_bar")
273                 player:hud_change(hunger_bar, "number", 1)
274         end
275 })