2020-06-11 19:00:42 -04:00
|
|
|
local minetest,vector,hud_manager = minetest,vector,hud_manager
|
2020-06-11 18:25:35 -04:00
|
|
|
|
2020-06-14 20:15:35 -04:00
|
|
|
local mod_storage = minetest.get_mod_storage()
|
|
|
|
local pool = {}
|
|
|
|
|
|
|
|
-- updates bubble bar
|
|
|
|
local update_breath_bar = function(player,breath)
|
|
|
|
if breath > 20 then
|
|
|
|
if hud_manager.hud_exists(player,"breath_bg") then
|
|
|
|
hud_manager.remove_hud(player,"breath_bg")
|
2020-06-11 19:00:42 -04:00
|
|
|
end
|
2020-06-14 20:15:35 -04:00
|
|
|
if hud_manager.hud_exists(player,"breath") then
|
|
|
|
hud_manager.remove_hud(player,"breath")
|
2020-06-11 18:25:35 -04:00
|
|
|
end
|
2020-06-14 20:15:35 -04:00
|
|
|
else
|
|
|
|
if not hud_manager.hud_exists(player,"breath_bg") then
|
|
|
|
hud_manager.add_hud(player,"breath_bg",{
|
|
|
|
hud_elem_type = "statbar",
|
|
|
|
position = {x = 0.5, y = 1},
|
|
|
|
text = "bubble_bg.png",
|
|
|
|
number = 20,
|
|
|
|
direction = 1,
|
|
|
|
size = {x = 24, y = 24},
|
|
|
|
offset = {x = 24*10, y= -(48 + 52 + 39)},
|
|
|
|
})
|
|
|
|
end
|
|
|
|
if not hud_manager.hud_exists(player,"breath") then
|
|
|
|
hud_manager.add_hud(player,"breath",{
|
|
|
|
hud_elem_type = "statbar",
|
|
|
|
position = {x = 0.5, y = 1},
|
|
|
|
text = "bubble.png",
|
|
|
|
number = breath,
|
|
|
|
direction = 1,
|
|
|
|
size = {x = 24, y = 24},
|
|
|
|
offset = {x = 24*10, y= -(48 + 52 + 39)},
|
|
|
|
})
|
2020-06-11 18:25:35 -04:00
|
|
|
end
|
|
|
|
|
2020-06-14 20:15:35 -04:00
|
|
|
hud_manager.change_hud({
|
|
|
|
player = player ,
|
|
|
|
hud_name = "breath",
|
|
|
|
element = "number",
|
|
|
|
data = breath
|
|
|
|
})
|
2020-06-11 18:25:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- loads data from mod storage
|
2020-06-14 20:15:35 -04:00
|
|
|
local name
|
|
|
|
local temp_pool
|
|
|
|
local load_data = function(player)
|
|
|
|
name = player:get_player_name()
|
|
|
|
pool[name] = {}
|
|
|
|
temp_pool = pool[name]
|
2020-06-11 18:25:35 -04:00
|
|
|
if mod_storage:get_int(name.."d_save") > 0 then
|
2020-06-14 20:15:35 -04:00
|
|
|
temp_pool.breath = mod_storage:get_float(name.."breath" )
|
|
|
|
temp_pool.ticker = mod_storage:get_float(name.."breath_ticker")
|
|
|
|
temp_pool.drowning = mod_storage:get_float(name.."drowning" )
|
2020-06-11 18:25:35 -04:00
|
|
|
else
|
2020-06-14 20:15:35 -04:00
|
|
|
temp_pool.breath = 21
|
|
|
|
temp_pool.ticker = 0
|
|
|
|
temp_pool.drowning = 0
|
2020-06-11 18:25:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- saves data to be utilized on next login
|
2020-06-14 20:15:35 -04:00
|
|
|
local temp_pool
|
|
|
|
local save_data = function(name)
|
2020-06-14 20:23:50 -04:00
|
|
|
if type(name) ~= "string" and name:is_player() then
|
2020-06-14 20:15:35 -04:00
|
|
|
name = name:get_player_name()
|
2020-06-11 18:25:35 -04:00
|
|
|
end
|
2020-06-14 20:15:35 -04:00
|
|
|
temp_pool = pool[name]
|
2020-06-11 18:25:35 -04:00
|
|
|
|
2020-06-14 20:15:35 -04:00
|
|
|
mod_storage:set_float(name.."breath", temp_pool.breath)
|
|
|
|
mod_storage:set_float(name.."breath_ticker", temp_pool.ticker)
|
2020-06-15 12:04:00 -04:00
|
|
|
mod_storage:set_float(name.."drowning", temp_pool.drowning)
|
2020-06-11 18:25:35 -04:00
|
|
|
mod_storage:set_int(name.."d_save", 1)
|
|
|
|
|
2020-06-14 20:15:35 -04:00
|
|
|
pool[name] = nil
|
2020-06-11 18:25:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
-- is used for shutdowns to save all data
|
2020-06-14 20:15:35 -04:00
|
|
|
local save_all = function()
|
|
|
|
for name,_ in pairs(pool) do
|
|
|
|
save_data(name)
|
2020-06-11 18:25:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- remove stock health bar
|
|
|
|
minetest.hud_replace_builtin("breath",{
|
|
|
|
hud_elem_type = "statbar",
|
|
|
|
position = {x = 0, y = 0},
|
|
|
|
text = "nothing.png",
|
|
|
|
number = 0,
|
|
|
|
direction = 0,
|
|
|
|
size = {x = 0, y = 0},
|
|
|
|
offset = {x = 0, y= 0},
|
|
|
|
})
|
|
|
|
|
2020-06-14 20:15:35 -04:00
|
|
|
minetest.register_on_joinplayer(function(player)
|
|
|
|
load_data(player)
|
2020-06-11 18:25:35 -04:00
|
|
|
player:hud_set_flags({breathbar=false})
|
|
|
|
end)
|
|
|
|
|
|
|
|
-- saves specific users data for when they relog
|
|
|
|
minetest.register_on_leaveplayer(function(player)
|
2020-06-14 20:15:35 -04:00
|
|
|
save_data(player)
|
2020-06-11 18:25:35 -04:00
|
|
|
end)
|
|
|
|
|
|
|
|
-- save all data to mod storage on shutdown
|
|
|
|
minetest.register_on_shutdown(function()
|
2020-06-14 20:15:35 -04:00
|
|
|
save_all()
|
2020-06-11 18:25:35 -04:00
|
|
|
end)
|
|
|
|
|
2020-06-14 20:15:35 -04:00
|
|
|
local name
|
|
|
|
is_player_drowning = function(player)
|
|
|
|
name = player:get_player_name()
|
|
|
|
return(pool[name].drowning)
|
|
|
|
end
|
|
|
|
|
2020-06-11 18:25:35 -04:00
|
|
|
-- reset the player's data
|
2020-06-14 20:15:35 -04:00
|
|
|
local name
|
|
|
|
local temp_pool
|
2020-06-11 18:25:35 -04:00
|
|
|
minetest.register_on_respawnplayer(function(player)
|
2020-06-14 20:15:35 -04:00
|
|
|
name = player:get_player_name()
|
|
|
|
temp_pool = pool[name]
|
|
|
|
temp_pool.breath = 21
|
|
|
|
temp_pool.ticker = 0
|
|
|
|
temp_pool.drowning = 0
|
|
|
|
update_breath_bar(player,temp_pool.breath)
|
2020-06-11 18:25:35 -04:00
|
|
|
end)
|
|
|
|
|
|
|
|
--handle the breath bar
|
2020-06-14 20:15:35 -04:00
|
|
|
local name
|
|
|
|
local temp_pool
|
|
|
|
local head
|
|
|
|
local hp
|
|
|
|
local handle_breath = function(player,dtime)
|
|
|
|
name = player:get_player_name()
|
|
|
|
head = get_player_head_env(player)
|
|
|
|
temp_pool = pool[name]
|
|
|
|
hp = player:get_hp()
|
|
|
|
if hp <= 0 then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if minetest.get_item_group(head, "drowning") > 0 then
|
2020-06-11 18:25:35 -04:00
|
|
|
|
2020-06-14 20:15:35 -04:00
|
|
|
temp_pool.ticker = temp_pool.ticker + dtime
|
2020-06-11 18:25:35 -04:00
|
|
|
|
2020-06-14 20:15:35 -04:00
|
|
|
if temp_pool.breath > 0 and temp_pool.ticker >= 1.3 then
|
2020-06-11 18:25:35 -04:00
|
|
|
|
2020-06-14 20:15:35 -04:00
|
|
|
if temp_pool.breath == 21 then
|
|
|
|
temp_pool.breath = 20
|
|
|
|
end
|
|
|
|
temp_pool.breath = temp_pool.breath - 2
|
2020-06-11 18:25:35 -04:00
|
|
|
|
2020-06-14 20:15:35 -04:00
|
|
|
temp_pool.drowning = 0
|
2020-06-11 18:25:35 -04:00
|
|
|
|
2020-06-14 20:15:35 -04:00
|
|
|
update_breath_bar(player,temp_pool.breath)
|
|
|
|
elseif temp_pool.breath <= 0 and temp_pool.ticker >= 1.3 then
|
2020-06-11 18:25:35 -04:00
|
|
|
|
2020-06-14 20:15:35 -04:00
|
|
|
temp_pool.drowning = 1
|
2020-06-11 18:25:35 -04:00
|
|
|
|
2020-06-14 20:15:35 -04:00
|
|
|
if hp > 0 then
|
|
|
|
player:set_hp( hp - 2 )
|
|
|
|
end
|
|
|
|
end
|
2020-06-11 18:25:35 -04:00
|
|
|
|
2020-06-14 20:15:35 -04:00
|
|
|
if temp_pool.ticker >= 1.3 then
|
|
|
|
temp_pool.ticker = 0
|
|
|
|
end
|
2020-06-11 18:25:35 -04:00
|
|
|
|
2020-06-14 20:15:35 -04:00
|
|
|
else
|
2020-06-11 18:25:35 -04:00
|
|
|
|
2020-06-14 20:15:35 -04:00
|
|
|
temp_pool.ticker = temp_pool.ticker + dtime
|
2020-06-11 18:25:35 -04:00
|
|
|
|
2020-06-14 20:15:35 -04:00
|
|
|
if temp_pool.breath < 21 and temp_pool.ticker >= 0.25 then
|
2020-06-11 18:25:35 -04:00
|
|
|
|
2020-06-14 20:15:35 -04:00
|
|
|
temp_pool.breath = temp_pool.breath + 2
|
2020-06-11 18:25:35 -04:00
|
|
|
|
2020-06-14 20:15:35 -04:00
|
|
|
temp_pool.drowning = 0
|
|
|
|
|
|
|
|
temp_pool.ticker = 0
|
2020-06-11 18:25:35 -04:00
|
|
|
|
2020-06-14 20:15:35 -04:00
|
|
|
update_breath_bar(player,temp_pool.breath)
|
2020-06-11 18:25:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- inject into main loop
|
|
|
|
minetest.register_globalstep(function(dtime)
|
2020-06-14 20:15:35 -04:00
|
|
|
for _,player in ipairs(minetest.get_connected_players()) do
|
|
|
|
handle_breath(player,dtime)
|
|
|
|
end
|
2020-06-11 18:25:35 -04:00
|
|
|
end)
|