]> git.lizzy.rs Git - Crafter.git/blob - mods/hunger/init.lua
Fix hunger crashing with nil digger
[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 hunger >= 20 and hp < 20 and satiation > 0 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 hunger_update()
188
189 --take away hunger and satiation randomly while mining
190 minetest.register_on_dignode(function(pos, oldnode, digger)
191         if digger and digger:is_player() then
192                 local meta = digger:get_meta()
193                 local exhaustion_tick = meta:get_int("exhaustion_tick")
194                 exhaustion_tick = exhaustion_tick + math.random(0,2)
195                 meta:set_int("exhaustion_tick", exhaustion_tick)
196         end
197 end)
198
199 --allow players to eat food
200 function minetest.eat_food(player,item)
201         local meta = player:get_meta()
202         
203         local player_hunger = meta:get_int("hunger")
204         local player_satiation = meta:get_int("satiation")
205         
206         
207         if type(item) == "string" then
208                 item = ItemStack(item)
209         elseif type(item) == "table" then
210                 item = ItemStack(item.name)
211         end
212         
213         item = item:get_name()
214         
215         local satiation = minetest.get_item_group(item, "satiation")
216         local hunger = minetest.get_item_group(item, "hunger")
217         
218         if player_hunger < 20 then
219                 player_hunger = player_hunger + hunger
220                 if player_hunger > 20 then
221                         player_hunger = 20
222                 end
223         end
224         if player_satiation < 20 then
225                 player_satiation = player_satiation + satiation
226                 if player_satiation > 20 then
227                         player_satiation = 20
228                 end
229         end
230         
231         meta:set_int("exhaustion_tick", 0)
232         meta:set_int("hunger", player_hunger)
233         meta:set_int("satiation", player_satiation)
234         local hunger_bar = meta:get_int("hunger_bar")
235         player:hud_change(hunger_bar, "number", player_hunger)
236         local stack = player:get_wielded_item()
237         stack:take_item()
238         player:set_wielded_item(stack)
239 end