]> git.lizzy.rs Git - Crafter.git/blob - mods/hunger/init.lua
c719cbd78b38457e85d90fefab4679ffa6604f29
[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                 
72                 if hunger >= 20 then
73                         player:set_hp(player:get_hp()+1)
74                 end
75                 
76                 meta:set_int("satiation", satiation)
77                 local hunger_bar = meta:get_int("hunger_bar")
78                 player:hud_change(hunger_bar, "number", hunger)
79         end
80         
81         minetest.after(1, function()
82                 hunger_update()
83         end)
84 end
85
86 hunger_update()
87
88 --allow players to eat food
89 function minetest.eat_food(player,item)
90         local meta = player:get_meta()
91         
92         local player_hunger = meta:get_int("hunger")
93         local player_satiation = meta:get_int("satiation")
94         
95         
96         if type(item) == "string" then
97                 item = ItemStack(item)
98         elseif type(item) == "table" then
99                 item = ItemStack(item.name)
100         end
101         
102         item = item:get_name()
103         
104         local satiation = minetest.get_item_group(item, "satiation")
105         local hunger = minetest.get_item_group(item, "hunger")
106         
107         if player_hunger < 20 then
108                 player_hunger = player_hunger + hunger
109                 if player_hunger > 20 then
110                         player_hunger = 20
111                 end
112         end
113         if player_satiation < 20 then
114                 player_satiation = player_satiation + satiation
115                 if player_satiation > 20 then
116                         player_satiation = 20
117                 end
118         end
119         
120         meta:set_int("hunger", player_hunger)
121         meta:set_int("satiation", player_satiation)
122 end