]> git.lizzy.rs Git - Crafter.git/blob - mods/hunger/init.lua
292c59a9490bd386f0ba75d3c8ff8363e8f0ad43
[Crafter.git] / mods / hunger / init.lua
1 minetest.register_on_joinplayer(function(player)
2         local meta = player:get_meta()
3         --give players new hunger when they join
4         if meta:get_int("hunger") == 0 then
5                 meta:set_int("hunger", 20)
6                 meta:set_int("satiation", 5)
7         end
8         player:hud_add({
9                 hud_elem_type = "statbar",
10                 position = {x = 0.5, y = 1},
11                 text = "hunger_icon_bg.png",
12                 number = 20,
13                 direction = 1,
14                 size = {x = 24, y = 24},
15                 offset = {x = 24*10, y= -(48 + 50 + 39)},
16         })
17         local hunger_bar = player:hud_add({
18                 hud_elem_type = "statbar",
19                 position = {x = 0.5, y = 1},
20                 text = "hunger_icon.png",
21                 number = 20,
22                 direction = 1,
23                 size = {x = 24, y = 24},
24                 offset = {x = 24*10, y= -(48 + 50 + 39)},
25         })
26         meta:set_int("hunger_bar", hunger_bar)
27 end)
28
29 local function hunger_update()
30         for _,player in ipairs(minetest.get_connected_players()) do
31                 local meta = player:get_meta()
32                 local satiation = meta:get_int("satiation")
33                 local hunger = meta:get_int("hunger")
34                 local running = (meta:get_string("player.player_movement_state") == "1")
35                 local bunny_hopping = (meta:get_string("player.player_movement_state") == "2")
36                 local sneaking = (meta:get_string("player.player_movement_state") == "3")               
37                 local got_hungry = math.random()
38                 if satiation > 0 then
39                         if running and got_hungry > 0.95 then
40                                 satiation = satiation - 1
41                         elseif bunny_hopping and got_hungry > 0.90 then
42                                 satiation = satiation - 1
43                         elseif sneaking and got_hungry > 0.997 then
44                                 satiation = satiation - 1
45                         elseif got_hungry > 0.998 then
46                                 satiation = satiation - 1
47                         end
48                 end
49                 
50                 if satiation == 0 then
51                         if hunger > 0 then
52                                 if running and got_hungry > 0.82 then
53                                         hunger = hunger - 1
54                                 elseif bunny_hopping and got_hungry > 0.77 then
55                                         hunger = hunger - 1
56                                 elseif sneaking and got_hungry > 0.954 then
57                                         hunger = hunger - 1
58                                 elseif got_hungry > 0.958 then
59                                         hunger = hunger - 1
60                                 end
61                         end
62                         meta:set_int("hunger", hunger)
63                         if hunger <= 0 then
64                                 local hp =  player:get_hp()
65                                 if hp > 0 then
66                                         player:set_hp(hp-1)
67                                 end
68                         end
69                 end
70                 
71                 local hp = player:get_hp()
72                 if hunger >= 20 and hp < 20 then
73                         player:set_hp(hp+1)
74                         satiation = satiation - 1
75                         if satiation < 0 then
76                                 satiation = 0
77                         end
78                 end
79                 
80                 meta:set_int("satiation", satiation)
81                 local hunger_bar = meta:get_int("hunger_bar")
82                 player:hud_change(hunger_bar, "number", hunger)
83         end
84         
85         minetest.after(1, function()
86                 hunger_update()
87         end)
88 end
89
90 hunger_update()
91
92 --allow players to eat food
93 function minetest.eat_food(player,item)
94         local meta = player:get_meta()
95         
96         local player_hunger = meta:get_int("hunger")
97         local player_satiation = meta:get_int("satiation")
98         
99         
100         if type(item) == "string" then
101                 item = ItemStack(item)
102         elseif type(item) == "table" then
103                 item = ItemStack(item.name)
104         end
105         
106         item = item:get_name()
107         
108         local satiation = minetest.get_item_group(item, "satiation")
109         local hunger = minetest.get_item_group(item, "hunger")
110         
111         if player_hunger < 20 then
112                 player_hunger = player_hunger + hunger
113                 if player_hunger > 20 then
114                         player_hunger = 20
115                 end
116         end
117         if player_satiation < 20 then
118                 player_satiation = player_satiation + satiation
119                 if player_satiation > 20 then
120                         player_satiation = 20
121                 end
122         end
123         
124         meta:set_int("hunger", player_hunger)
125         meta:set_int("satiation", player_satiation)
126         local hunger_bar = meta:get_int("hunger_bar")
127         player:hud_change(hunger_bar, "number", player_hunger)
128         local stack = player:get_wielded_item()
129         stack:take_item()
130         player:set_wielded_item(stack)
131 end