]> git.lizzy.rs Git - Crafter.git/blob - mods/hunger/init.lua
Fix more balancing with hunger
[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 minetest.register_on_respawnplayer(function(player)
30         local meta = player:get_meta()
31         meta:set_int("hunger", 20)
32         meta:set_int("satiation", 5)
33 end)
34
35 local function hunger_update()
36         for _,player in ipairs(minetest.get_connected_players()) do
37                 local meta = player:get_meta()
38                 local satiation = meta:get_int("satiation")
39                 local hunger = meta:get_int("hunger")
40                 local running = (meta:get_string("player.player_movement_state") == "1")
41                 local bunny_hopping = (meta:get_string("player.player_movement_state") == "2")
42                 local sneaking = (meta:get_string("player.player_movement_state") == "3")               
43                 local got_hungry = math.random()
44                 if satiation > 0 then
45                         if running and got_hungry > 0.95 then
46                                 satiation = satiation - 1
47                         elseif bunny_hopping and got_hungry > 0.90 then
48                                 satiation = satiation - 1
49                         elseif sneaking and got_hungry > 0.998 then
50                                 satiation = satiation - 1
51                         elseif got_hungry > 0.996 then
52                                 satiation = satiation - 1
53                         end
54                 end
55                 
56                 if satiation == 0 then
57                         if hunger > 0 then
58                                 if running and got_hungry > 0.82 then
59                                         hunger = hunger - 1
60                                 elseif bunny_hopping and got_hungry > 0.77 then
61                                         hunger = hunger - 1
62                                 elseif sneaking and got_hungry > 0.968 then
63                                         hunger = hunger - 1
64                                 elseif got_hungry > 0.954 then
65                                         hunger = hunger - 1
66                                 end
67                         end
68                         meta:set_int("hunger", hunger)
69                         if hunger <= 0 then
70                                 local hp =  player:get_hp()
71                                 if hp > 0 then
72                                         player:set_hp(hp-1)
73                                 end
74                         end
75                 end
76                 
77                 local hp = player:get_hp()
78                 if hunger >= 20 and hp < 20 then
79                         player:set_hp(hp+1)
80                         satiation = satiation - 1
81                         if satiation < 0 then
82                                 satiation = 0
83                         end
84                 end
85                 
86                 meta:set_int("satiation", satiation)
87                 local hunger_bar = meta:get_int("hunger_bar")
88                 player:hud_change(hunger_bar, "number", hunger)
89         end
90         
91         minetest.after(1, function()
92                 hunger_update()
93         end)
94 end
95
96 hunger_update()
97
98 --allow players to eat food
99 function minetest.eat_food(player,item)
100         local meta = player:get_meta()
101         
102         local player_hunger = meta:get_int("hunger")
103         local player_satiation = meta:get_int("satiation")
104         
105         
106         if type(item) == "string" then
107                 item = ItemStack(item)
108         elseif type(item) == "table" then
109                 item = ItemStack(item.name)
110         end
111         
112         item = item:get_name()
113         
114         local satiation = minetest.get_item_group(item, "satiation")
115         local hunger = minetest.get_item_group(item, "hunger")
116         
117         if player_hunger < 20 then
118                 player_hunger = player_hunger + hunger
119                 if player_hunger > 20 then
120                         player_hunger = 20
121                 end
122         end
123         if player_satiation < 20 then
124                 player_satiation = player_satiation + satiation
125                 if player_satiation > 20 then
126                         player_satiation = 20
127                 end
128         end
129         
130         meta:set_int("hunger", player_hunger)
131         meta:set_int("satiation", player_satiation)
132         local hunger_bar = meta:get_int("hunger_bar")
133         player:hud_change(hunger_bar, "number", player_hunger)
134         local stack = player:get_wielded_item()
135         stack:take_item()
136         player:set_wielded_item(stack)
137 end