]> git.lizzy.rs Git - Crafter.git/blob - mods/hunger/init.lua
Use actual state modifiers with ticks to control satiation and hunger instead of...
[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 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                                         print("should be wroking")
112                                         satiation = satiation - 1
113                                         exhaustion_tick = exhaustion_tick - exhaustion_peak
114                                         
115                                         --reset this to use for the hunger tick
116                                         if satiation == 0 then
117                                                 exhaustion_tick = 0
118                                         end
119                                         
120                                         meta:set_int("satiation", satiation)
121                                 end
122                                 
123                                 meta:set_int("exhaustion_tick", exhaustion_tick)
124                         elseif hunger > 0 then
125                                 --this is copied again because this is for future tuning
126                                 if running then
127                                         exhaustion_tick = exhaustion_tick + 3
128                                 elseif bunny_hopping then
129                                         exhaustion_tick = exhaustion_tick + 4
130                                 elseif sneaking then
131                                         exhaustion_tick = exhaustion_tick + 1
132                                 elseif walking then
133                                         exhaustion_tick = exhaustion_tick + 2
134                                 elseif standing then
135                                         exhaustion_tick = exhaustion_tick + 1
136                                 end
137                                 if exhaustion_tick >= hunger_peak then
138                                         --don't allow hunger to go negative
139                                         if hunger > 0 then
140                                                 exhaustion_tick = 0
141                                                 hunger = hunger - 1
142                                                 meta:set_int("hunger", hunger)
143                                                 local hunger_bar = meta:get_int("hunger_bar")
144                                                 player:hud_change(hunger_bar, "number", hunger)
145                                         end
146                                 end
147                                 meta:set_int("exhaustion_tick", exhaustion_tick)
148                         elseif hunger <= 0 then
149                                 exhaustion_tick = exhaustion_tick + 1
150                                 
151                                 local hp =  player:get_hp()
152                                 if hp > 0 and exhaustion_tick >= 2 then
153                                         player:set_hp(hp-1)
154                                         exhaustion_tick = 0
155                                 end
156                                 meta:set_int("exhaustion_tick", exhaustion_tick)
157                         end
158                         
159                         print("satiation:",satiation,"exhaustion_tick:",exhaustion_tick)
160                         local hp = player:get_hp()
161                         if hunger >= 20 and hp < 20 then
162                                 player:set_hp(hp+1)
163                                 exhaustion_tick = 0
164                                 satiation = satiation - 1
165                                 if satiation < 0 then
166                                         satiation = 0
167                                 end
168                                 meta:set_int("exhaustion_tick", exhaustion_tick)
169                         end
170                         
171                         meta:set_int("satiation", satiation)
172                 end
173         end
174         
175         minetest.after(0.5, function()
176                 hunger_update()
177         end)
178 end
179
180 hunger_update()
181
182 --take away hunger and satiation randomly while mining
183 minetest.register_on_dignode(function(pos, oldnode, digger)
184         local meta = digger:get_meta()
185         local exhaustion_tick = meta:get_int("exhaustion_tick")
186         exhaustion_tick = exhaustion_tick + math.random(0,2)
187         meta:set_int("exhaustion_tick", exhaustion_tick)
188 end)
189
190 --allow players to eat food
191 function minetest.eat_food(player,item)
192         local meta = player:get_meta()
193         
194         local player_hunger = meta:get_int("hunger")
195         local player_satiation = meta:get_int("satiation")
196         
197         
198         if type(item) == "string" then
199                 item = ItemStack(item)
200         elseif type(item) == "table" then
201                 item = ItemStack(item.name)
202         end
203         
204         item = item:get_name()
205         
206         local satiation = minetest.get_item_group(item, "satiation")
207         local hunger = minetest.get_item_group(item, "hunger")
208         
209         if player_hunger < 20 then
210                 player_hunger = player_hunger + hunger
211                 if player_hunger > 20 then
212                         player_hunger = 20
213                 end
214         end
215         if player_satiation < 20 then
216                 player_satiation = player_satiation + satiation
217                 if player_satiation > 20 then
218                         player_satiation = 20
219                 end
220         end
221         
222         meta:set_int("exhaustion_tick", 0)
223         meta:set_int("hunger", player_hunger)
224         meta:set_int("satiation", player_satiation)
225         local hunger_bar = meta:get_int("hunger_bar")
226         player:hud_change(hunger_bar, "number", player_hunger)
227         local stack = player:get_wielded_item()
228         stack:take_item()
229         player:set_wielded_item(stack)
230 end