2019-03-30 14:00:31 +01:00
|
|
|
local S = minetest.get_translator("hbarmor")
|
2016-08-05 17:00:45 +02:00
|
|
|
|
2016-12-04 01:49:34 +01:00
|
|
|
if (not armor) or (not armor.def) then
|
|
|
|
minetest.log("error", "[hbarmor] Outdated 3d_armor version. Please update your version of 3d_armor!")
|
|
|
|
end
|
|
|
|
|
2016-12-04 01:44:34 +01:00
|
|
|
local hbarmor = {}
|
2013-06-28 22:58:06 +02:00
|
|
|
|
2014-04-06 16:33:38 +02:00
|
|
|
-- HUD statbar values
|
2015-02-24 06:25:29 +01:00
|
|
|
hbarmor.armor = {}
|
2014-04-06 16:33:38 +02:00
|
|
|
|
2015-02-24 05:30:08 +01:00
|
|
|
-- Stores if player's HUD bar has been initialized so far.
|
2015-02-24 06:25:29 +01:00
|
|
|
hbarmor.player_active = {}
|
2015-02-24 05:30:08 +01:00
|
|
|
|
2016-12-04 01:41:47 +01:00
|
|
|
-- Time difference in seconds between updates to the HUD armor bar.
|
|
|
|
-- Increase this number for slow servers.
|
2015-02-24 06:25:29 +01:00
|
|
|
hbarmor.tick = 0.1
|
2014-10-03 08:12:55 +02:00
|
|
|
|
2015-02-24 06:37:19 +01:00
|
|
|
-- If true, the armor bar is hidden when the player does not wear any armor
|
2015-02-24 07:16:15 +01:00
|
|
|
hbarmor.autohide = true
|
2015-02-24 06:37:19 +01:00
|
|
|
|
2014-04-14 06:02:42 +02:00
|
|
|
--load custom settings
|
2017-08-09 16:25:59 +02:00
|
|
|
local set = minetest.settings:get_bool("hbarmor_autohide")
|
2016-12-04 01:41:47 +01:00
|
|
|
if set ~= nil then
|
|
|
|
hbarmor.autohide = set
|
2013-07-20 22:07:24 +02:00
|
|
|
end
|
2013-06-28 22:58:06 +02:00
|
|
|
|
2017-08-09 16:25:59 +02:00
|
|
|
set = minetest.settings:get("hbarmor_tick")
|
2016-12-04 01:41:47 +01:00
|
|
|
if tonumber(set) ~= nil then
|
|
|
|
hbarmor.tick = tonumber(set)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-02-24 04:49:32 +01:00
|
|
|
local must_hide = function(playername, arm)
|
|
|
|
return ((not armor.def[playername].count or armor.def[playername].count == 0) and arm == 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
local arm_printable = function(arm)
|
|
|
|
return math.ceil(math.floor(arm+0.5))
|
|
|
|
end
|
|
|
|
|
2014-04-14 06:02:42 +02:00
|
|
|
local function custom_hud(player)
|
2015-02-24 02:54:55 +01:00
|
|
|
local name = player:get_player_name()
|
2013-07-20 14:02:30 +02:00
|
|
|
|
2017-08-09 16:25:59 +02:00
|
|
|
if minetest.settings:get_bool("enable_damage") then
|
2015-02-24 07:02:35 +01:00
|
|
|
local ret = hbarmor.get_armor(player)
|
|
|
|
if ret == false then
|
|
|
|
minetest.log("error", "[hbarmor] Call to hbarmor.get_armor in custom_hud returned with false!")
|
|
|
|
end
|
2015-02-24 06:25:29 +01:00
|
|
|
local arm = tonumber(hbarmor.armor[name])
|
2015-02-24 04:49:32 +01:00
|
|
|
if not arm then arm = 0 end
|
2015-02-24 06:37:19 +01:00
|
|
|
local hide
|
|
|
|
if hbarmor.autohide then
|
|
|
|
hide = must_hide(name, arm)
|
|
|
|
else
|
|
|
|
hide = false
|
|
|
|
end
|
2015-02-24 06:12:53 +01:00
|
|
|
hb.init_hudbar(player, "armor", arm_printable(arm), nil, hide)
|
2015-02-24 02:54:55 +01:00
|
|
|
end
|
2013-06-28 22:58:06 +02:00
|
|
|
end
|
|
|
|
|
2015-02-24 02:02:45 +01:00
|
|
|
--register and define armor HUD bar
|
2019-03-30 14:00:31 +01:00
|
|
|
hb.register_hudbar("armor", 0xFFFFFF, S("Armor"), { icon = "hbarmor_icon.png", bgicon = "hbarmor_bgicon.png", bar = "hbarmor_bar.png" }, 0, 100, hbarmor.autohide, S("@1: @2%"))
|
2015-02-24 02:02:45 +01:00
|
|
|
|
2016-12-04 01:49:34 +01:00
|
|
|
function hbarmor.get_armor(player)
|
|
|
|
if not player or not armor.def then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
local name = player:get_player_name()
|
|
|
|
local def = armor.def[name] or nil
|
|
|
|
if def and def.state and def.count then
|
|
|
|
hbarmor.set_armor(name, def.state, def.count)
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
2013-09-13 20:18:16 +02:00
|
|
|
|
2016-12-04 01:49:34 +01:00
|
|
|
function hbarmor.set_armor(player_name, ges_state, items)
|
|
|
|
local max_items = 4
|
|
|
|
if items == 5 then
|
|
|
|
max_items = items
|
|
|
|
end
|
|
|
|
local max = max_items * 65535
|
|
|
|
local lvl = max - ges_state
|
|
|
|
lvl = lvl/max
|
|
|
|
if ges_state == 0 and items == 0 then
|
|
|
|
lvl = 0
|
|
|
|
end
|
|
|
|
|
2017-03-08 01:00:41 +01:00
|
|
|
hbarmor.armor[player_name] = math.max(0, math.min(lvl* (items * (100 / max_items)), 100))
|
2016-12-04 01:49:34 +01:00
|
|
|
end
|
2015-02-24 04:49:32 +01:00
|
|
|
|
2014-04-06 16:33:38 +02:00
|
|
|
-- update hud elemtens if value has changed
|
2013-06-28 22:58:06 +02:00
|
|
|
local function update_hud(player)
|
2014-04-06 16:33:38 +02:00
|
|
|
local name = player:get_player_name()
|
2016-12-04 01:49:34 +01:00
|
|
|
--armor
|
2015-02-24 06:25:29 +01:00
|
|
|
local arm = tonumber(hbarmor.armor[name])
|
2015-02-24 05:30:08 +01:00
|
|
|
if not arm then
|
|
|
|
arm = 0
|
2015-02-24 06:25:29 +01:00
|
|
|
hbarmor.armor[name] = 0
|
2015-02-24 05:30:08 +01:00
|
|
|
end
|
2015-02-24 06:45:16 +01:00
|
|
|
if hbarmor.autohide then
|
2015-02-24 06:37:19 +01:00
|
|
|
-- hide armor bar completely when there is none
|
|
|
|
if must_hide(name, arm) then
|
|
|
|
hb.hide_hudbar(player, "armor")
|
|
|
|
else
|
|
|
|
hb.change_hudbar(player, "armor", arm_printable(arm))
|
|
|
|
hb.unhide_hudbar(player, "armor")
|
|
|
|
end
|
2015-02-24 04:51:48 +01:00
|
|
|
else
|
2015-02-24 05:30:08 +01:00
|
|
|
hb.change_hudbar(player, "armor", arm_printable(arm))
|
2014-04-06 16:33:38 +02:00
|
|
|
end
|
2013-06-28 22:58:06 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
minetest.register_on_joinplayer(function(player)
|
2014-04-06 16:33:38 +02:00
|
|
|
local name = player:get_player_name()
|
2015-02-24 01:55:07 +01:00
|
|
|
custom_hud(player)
|
2015-02-24 06:25:29 +01:00
|
|
|
hbarmor.player_active[name] = true
|
2015-02-24 05:30:08 +01:00
|
|
|
end)
|
|
|
|
|
|
|
|
minetest.register_on_leaveplayer(function(player)
|
|
|
|
local name = player:get_player_name()
|
2015-02-24 06:25:29 +01:00
|
|
|
hbarmor.player_active[name] = false
|
2013-06-28 22:58:06 +02:00
|
|
|
end)
|
|
|
|
|
2014-04-14 06:51:28 +02:00
|
|
|
local main_timer = 0
|
2013-06-28 22:58:06 +02:00
|
|
|
local timer = 0
|
2015-02-24 04:49:32 +01:00
|
|
|
minetest.register_globalstep(function(dtime)
|
2015-02-24 02:54:55 +01:00
|
|
|
main_timer = main_timer + dtime
|
|
|
|
timer = timer + dtime
|
2015-02-24 06:25:29 +01:00
|
|
|
if main_timer > hbarmor.tick or timer > 4 then
|
2017-08-09 16:25:59 +02:00
|
|
|
if minetest.settings:get_bool("enable_damage") then
|
2015-02-24 06:25:29 +01:00
|
|
|
if main_timer > hbarmor.tick then main_timer = 0 end
|
2015-02-24 05:37:11 +01:00
|
|
|
for _,player in ipairs(minetest.get_connected_players()) do
|
|
|
|
local name = player:get_player_name()
|
2015-02-24 06:25:29 +01:00
|
|
|
if hbarmor.player_active[name] == true then
|
2015-02-24 07:02:35 +01:00
|
|
|
local ret = hbarmor.get_armor(player)
|
|
|
|
if ret == false then
|
|
|
|
minetest.log("error", "[hbarmor] Call to hbarmor.get_armor in globalstep returned with false!")
|
|
|
|
end
|
2015-02-24 05:30:08 +01:00
|
|
|
-- update all hud elements
|
|
|
|
update_hud(player)
|
|
|
|
end
|
2013-06-28 22:58:06 +02:00
|
|
|
end
|
2014-04-14 06:51:28 +02:00
|
|
|
end
|
2015-02-24 04:49:32 +01:00
|
|
|
end
|
|
|
|
if timer > 4 then timer = 0 end
|
2013-06-28 22:58:06 +02:00
|
|
|
end)
|