Add Wuzzy to mailmap
[minetest_hbhunger.git] / init.lua
blob8d19e164078e4ef39d678a8561e88f07757d55a6
1 local S = minetest.get_translator("hbhunger")
3 if minetest.settings:get_bool("enable_damage") then
5 hbhunger = {}
6 hbhunger.food = {}
8 -- HUD statbar values
9 hbhunger.hunger = {}
10 hbhunger.hunger_out = {}
12 -- Count number of poisonings a player has at once
13 hbhunger.poisonings = {}
15 -- HUD item ids
16 local hunger_hud = {}
18 hbhunger.HUD_TICK = 0.1
20 --Some hunger settings
21 hbhunger.exhaustion = {} -- Exhaustion is experimental!
23 hbhunger.HUNGER_TICK = 800 -- time in seconds after that 1 hunger point is taken
24 hbhunger.EXHAUST_DIG = 3 -- exhaustion increased this value after digged node
25 hbhunger.EXHAUST_PLACE = 1 -- exhaustion increased this value after placed
26 hbhunger.EXHAUST_MOVE = 0.3 -- exhaustion increased this value if player movement detected
27 hbhunger.EXHAUST_LVL = 160 -- at what exhaustion player satiation gets lowerd
28 hbhunger.SAT_MAX = 30 -- maximum satiation points
29 hbhunger.SAT_INIT = 20 -- initial satiation points
30 hbhunger.SAT_HEAL = 15 -- required satiation points to start healing
33 --load custom settings
34 local set = io.open(minetest.get_modpath("hbhunger").."/hbhunger.conf", "r")
35 if set then
36 dofile(minetest.get_modpath("hbhunger").."/hbhunger.conf")
37 set:close()
38 end
40 local function custom_hud(player)
41 hb.init_hudbar(player, "satiation", hbhunger.get_hunger_raw(player))
42 end
44 dofile(minetest.get_modpath("hbhunger").."/hunger.lua")
45 dofile(minetest.get_modpath("hbhunger").."/register_foods.lua")
47 -- register satiation hudbar
48 hb.register_hudbar("satiation", 0xFFFFFF, S("Satiation"), { icon = "hbhunger_icon.png", bgicon = "hbhunger_bgicon.png", bar = "hbhunger_bar.png" }, hbhunger.SAT_INIT, hbhunger.SAT_MAX, false, nil, { format_value = "%.1f", format_max_value = "%d" })
50 -- update hud elemtens if value has changed
51 local function update_hud(player)
52 local name = player:get_player_name()
53 --hunger
54 local h_out = tonumber(hbhunger.hunger_out[name])
55 local h = tonumber(hbhunger.hunger[name])
56 if h_out ~= h then
57 hbhunger.hunger_out[name] = h
58 hb.change_hudbar(player, "satiation", h)
59 end
60 end
62 hbhunger.get_hunger_raw = function(player)
63 local inv = player:get_inventory()
64 if not inv then return nil end
65 local hgp = inv:get_stack("hunger", 1):get_count()
66 if hgp == 0 then
67 hgp = 21
68 inv:set_stack("hunger", 1, ItemStack({name=":", count=hgp}))
69 else
70 hgp = hgp
71 end
72 return hgp-1
73 end
75 hbhunger.set_hunger_raw = function(player)
76 local inv = player:get_inventory()
77 local name = player:get_player_name()
78 local value = hbhunger.hunger[name]
79 if not inv or not value then return nil end
80 if value > hbhunger.SAT_MAX then value = hbhunger.SAT_MAX end
81 if value < 0 then value = 0 end
83 inv:set_stack("hunger", 1, ItemStack({name=":", count=value+1}))
85 return true
86 end
88 minetest.register_on_joinplayer(function(player)
89 local name = player:get_player_name()
90 local inv = player:get_inventory()
91 inv:set_size("hunger",1)
92 hbhunger.hunger[name] = hbhunger.get_hunger_raw(player)
93 hbhunger.hunger_out[name] = hbhunger.hunger[name]
94 hbhunger.exhaustion[name] = 0
95 hbhunger.poisonings[name] = 0
96 custom_hud(player)
97 hbhunger.set_hunger_raw(player)
98 end)
100 minetest.register_on_respawnplayer(function(player)
101 -- reset hunger (and save)
102 local name = player:get_player_name()
103 hbhunger.hunger[name] = hbhunger.SAT_INIT
104 hbhunger.set_hunger_raw(player)
105 hbhunger.exhaustion[name] = 0
106 end)
108 local main_timer = 0
109 local timer = 0
110 local timer2 = 0
111 minetest.register_globalstep(function(dtime)
112 main_timer = main_timer + dtime
113 timer = timer + dtime
114 timer2 = timer2 + dtime
115 if main_timer > hbhunger.HUD_TICK or timer > 4 or timer2 > hbhunger.HUNGER_TICK then
116 if main_timer > hbhunger.HUD_TICK then main_timer = 0 end
117 for _,player in ipairs(minetest.get_connected_players()) do
118 local name = player:get_player_name()
120 local h = tonumber(hbhunger.hunger[name])
121 local hp = player:get_hp()
122 if timer > 4 then
123 -- heal player by 1 hp if not dead and satiation is > hbhunger.SAT_HEAL
124 if h > hbhunger.SAT_HEAL and hp > 0 and player:get_breath() > 0 then
125 player:set_hp(hp+1)
126 -- or damage player by 1 hp if satiation is < 2
127 elseif h <= 1 then
128 if hp-1 >= 0 then player:set_hp(hp-1) end
131 -- lower satiation by 1 point after xx seconds
132 if timer2 > hbhunger.HUNGER_TICK then
133 if h > 0 then
134 h = h-1
135 hbhunger.hunger[name] = h
136 hbhunger.set_hunger_raw(player)
140 -- update all hud elements
141 update_hud(player)
143 local controls = player:get_player_control()
144 -- Determine if the player is walking
145 if controls.up or controls.down or controls.left or controls.right then
146 hbhunger.handle_node_actions(nil, nil, player)
150 if timer > 4 then timer = 0 end
151 if timer2 > hbhunger.HUNGER_TICK then timer2 = 0 end
152 end)
154 minetest.register_chatcommand("satiation", {
155 privs = {["server"]=true},
156 params = S("[<player>] <satiation>"),
157 description = S("Set satiation of player or yourself"),
158 func = function(name, param)
159 if minetest.settings:get_bool("enable_damage") == false then
160 return false, S("Not possible, damage is disabled.")
162 local targetname, satiation = string.match(param, "(%S+) (%S+)")
163 if not targetname then
164 satiation = param
166 satiation = tonumber(satiation)
167 if not satiation then
168 return false, S("Invalid satiation!")
170 if not targetname then
171 targetname = name
173 local target = minetest.get_player_by_name(targetname)
174 if target == nil then
175 return false, S("Player @1 does not exist.", targetname)
177 if satiation > hbhunger.SAT_MAX then
178 satiation = hbhunger.SAT_MAX
179 elseif satiation < 0 then
180 satiation = 0
182 hbhunger.hunger[targetname] = satiation
183 hbhunger.set_hunger_raw(target)
184 return true
185 end,