]> git.lizzy.rs Git - Crafter.git/blob - mods/hunger/init.lua
ed6e02ed1942cdd41d331af4846077bcddd38e35
[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                         print(satiation)
75
76                         --movement states
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
82                         local walking = false
83                         
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
89                                         walking = true
90                                 else
91                                         standing = true
92                                 end
93                         end
94                         
95                         --we count up the exhaustion of the player moving around
96                         --based on their states
97                         if satiation > 0 and hunger >= 20 then
98                                 if running then
99                                         exhaustion_tick = exhaustion_tick + 6
100                                 elseif bunny_hopping then
101                                         exhaustion_tick = exhaustion_tick + 8
102                                 elseif sneaking then
103                                         exhaustion_tick = exhaustion_tick + 1
104                                         
105                                 elseif walking then
106                                         exhaustion_tick = exhaustion_tick + 3
107                                 elseif standing then
108                                         exhaustion_tick = exhaustion_tick + 1
109                                 end
110                                 
111                                 
112                                 if exhaustion_tick >= exhaustion_peak then
113                                         satiation = satiation - 1
114                                         exhaustion_tick = exhaustion_tick - exhaustion_peak
115                                         
116                                         --reset this to use for the hunger tick
117                                         if satiation == 0 then
118                                                 exhaustion_tick = 0
119                                         end
120                                         
121                                         meta:set_int("satiation", satiation)
122                                 end
123                                 
124                                 meta:set_int("exhaustion_tick", exhaustion_tick)
125                         elseif hunger > 0 then
126                                 --this is copied again because this is for future tuning
127                                 if running then
128                                         exhaustion_tick = exhaustion_tick + 3
129                                 elseif bunny_hopping then
130                                         exhaustion_tick = exhaustion_tick + 4
131                                 elseif sneaking then
132                                         exhaustion_tick = exhaustion_tick + 1
133                                 elseif walking then
134                                         exhaustion_tick = exhaustion_tick + 2
135                                 elseif standing then
136                                         exhaustion_tick = exhaustion_tick + 1
137                                 end
138                                 if exhaustion_tick >= hunger_peak then
139                                         --don't allow hunger to go negative
140                                         if hunger > 0 then
141                                                 exhaustion_tick = 0
142                                                 hunger = hunger - 1
143                                                 meta:set_int("hunger", hunger)
144                                                 local hunger_bar = meta:get_int("hunger_bar")
145                                                 player:hud_change(hunger_bar, "number", hunger)
146                                         end
147                                 end
148                                 meta:set_int("exhaustion_tick", exhaustion_tick)
149                         elseif hunger <= 0 then
150                                 exhaustion_tick = exhaustion_tick + 1
151                                 
152                                 local hp =  player:get_hp()
153                                 if hp > 0 and exhaustion_tick >= 2 then
154                                         player:set_hp(hp-1)
155                                         exhaustion_tick = 0
156                                 end
157                                 meta:set_int("exhaustion_tick", exhaustion_tick)
158                         end
159                         
160                         
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
168                                         player:set_hp(hp+1)
169                                         exhaustion_tick = exhaustion_tick + 32
170                                         meta:set_int("exhaustion_tick", exhaustion_tick)
171                                         meta:set_int("satiation", satiation)
172                                         regeneration_interval = 0
173                                 end
174                                 meta:set_int("regeneration_interval",regeneration_interval)
175                         --reset the regen interval
176                         else
177                                 meta:set_int("regeneration_interval",0)
178                         end
179                         
180                         --print("satiation:",satiation,"exhaustion_tick:",exhaustion_tick)
181                 end
182         end
183         
184         minetest.after(0.5, function()
185                 hunger_update()
186         end)
187 end
188
189 minetest.register_on_mods_loaded(function()
190         minetest.after(0,function()
191                 hunger_update()
192         end)
193 end)
194
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)
202         end
203 end)
204
205 --allow players to eat food
206 function minetest.eat_food(player,item)
207         local meta = player:get_meta()
208         
209         local player_hunger = meta:get_int("hunger")
210         local player_satiation = meta:get_int("satiation")
211         
212         
213         if type(item) == "string" then
214                 item = ItemStack(item)
215         elseif type(item) == "table" then
216                 item = ItemStack(item.name)
217         end
218         
219         item = item:get_name()
220         
221         local satiation = minetest.get_item_group(item, "satiation")
222         local hunger = minetest.get_item_group(item, "hunger")
223         
224         if player_hunger < 20 then
225                 player_hunger = player_hunger + hunger
226                 if player_hunger > 20 then
227                         player_hunger = 20
228                 end
229         end
230         if player_satiation < satiation then
231                 player_satiation =  satiation
232         end
233         
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()
240         stack:take_item()
241         player:set_wielded_item(stack)
242 end
243
244
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},
250         })
251
252         minetest.register_node(":"..name.."node", {
253                 description = "NIL",
254                 tiles = {def.texture},
255                 groups = {},
256                 drop = "",
257                 drawtype = "allfaces",
258                 on_construct = function(pos)
259                         minetest.remove_node(pos)
260                 end,
261         })
262 end
263
264 minetest.register_chatcommand("hungry", {
265         params = "<mob>",
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)
276         end
277 })