hudbars/hbarmor/init.lua

186 lines
3.6 KiB
Lua
Raw Normal View History

2015-10-04 05:41:29 -07:00
hbarmor = {}
hbarmor.armor = {} -- HUD statbar values
hbarmor.player_active = {} -- stores if player hug has been init. so far
hbarmor.tick = 1 -- was 0.1
hbarmor.autohide = true -- hide when player not wearing armor
2015-10-04 05:41:29 -07:00
local armor_hud = {} -- HUD item id's
2017-12-10 19:25:51 -08:00
local enable_damage = minetest.settings:get_bool("enable_damage")
2015-10-04 05:41:29 -07:00
--[[load custom settings
local set = io.open(minetest.get_modpath("hbarmor").."/hbarmor.conf", "r")
if set then
dofile(minetest.get_modpath("hbarmor").."/hbarmor.conf")
set:close()
end--]]
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
local function custom_hud(player)
2015-10-04 05:41:29 -07:00
local name = player:get_player_name()
if not enable_damage then
return
end
if hbarmor.get_armor(player) == false then
minetest.log("error",
"[hbarmor] Call to hbarmor.get_armor in custom_hud returned with false!")
end
local arm = tonumber(hbarmor.armor[name]) or 0
local hide = false
if hbarmor.autohide then
hide = must_hide(name, arm)
2015-10-04 05:41:29 -07:00
end
hb.init_hudbar(player, "armor", arm_printable(arm), nil, hide)
2015-10-04 05:41:29 -07:00
end
--register and define armor HUD bar
hb.register_hudbar("armor", 0xFFFFFF, "Armor",
{ icon = "hbarmor_icon.png", bar = "hbarmor_bar.png" }, 0, 100, hbarmor.autohide, "%s: %d%%")
minetest.after(0, function()
2015-10-04 05:41:29 -07:00
if not armor.def then
2015-10-04 05:41:29 -07:00
minetest.after(2,minetest.chat_send_all,
"#Better HUD: Please update your version of 3darmor")
2015-10-04 05:41:29 -07:00
HUD_SHOW_ARMOR = false
end
end)
function hbarmor.get_armor(player)
2015-10-04 05:41:29 -07:00
if not player or not armor.def then
return false
end
2015-10-04 05:41:29 -07:00
local name = player:get_player_name()
local def = armor.def[name] or nil
2015-10-04 05:41:29 -07:00
if def and def.state and def.count then
2015-10-04 05:41:29 -07:00
hbarmor.set_armor(name, def.state, def.count)
else
return false
end
2015-10-04 05:41:29 -07:00
return true
end
function hbarmor.set_armor(player_name, ges_state, items)
2015-10-04 05:41:29 -07:00
local max_items = 4
if items == 5 then
2015-10-04 05:41:29 -07:00
max_items = items
end
2015-10-04 05:41:29 -07:00
local max = max_items * 65535
local lvl = max - ges_state
2015-10-04 05:41:29 -07:00
lvl = lvl / max
2015-10-04 05:41:29 -07:00
if ges_state == 0 and items == 0 then
lvl = 0
end
hbarmor.armor[player_name] = lvl * (items * (100 / max_items))
end
-- update hud elemtens if value has changed
local function update_hud(player)
if not player then
return
end
2015-10-04 05:41:29 -07:00
local name = player:get_player_name()
local arm = tonumber(hbarmor.armor[name])
2015-10-04 05:41:29 -07:00
if not arm then
arm = 0
hbarmor.armor[name] = 0
end
2015-10-04 05:41:29 -07:00
if hbarmor.autohide then
2015-10-04 05:41:29 -07:00
-- hide armor bar completely when there is none
if must_hide(name, arm) then
2015-10-04 05:41:29 -07:00
hb.hide_hudbar(player, "armor")
else
hb.change_hudbar(player, "armor", arm_printable(arm))
hb.unhide_hudbar(player, "armor")
end
else
hb.change_hudbar(player, "armor", arm_printable(arm))
end
end
minetest.register_on_joinplayer(function(player)
2015-10-04 05:41:29 -07:00
local name = player:get_player_name()
2015-10-04 05:41:29 -07:00
custom_hud(player)
2015-10-04 05:41:29 -07:00
hbarmor.player_active[name] = true
end)
minetest.register_on_leaveplayer(function(player)
2015-10-04 05:41:29 -07:00
local name = player:get_player_name()
2015-10-04 05:41:29 -07:00
hbarmor.player_active[name] = false
end)
local main_timer = 0
2015-10-04 05:41:29 -07:00
minetest.register_globalstep(function(dtime)
2015-10-04 05:41:29 -07:00
main_timer = main_timer + dtime
if main_timer > hbarmor.tick then
2015-10-04 05:41:29 -07:00
if enable_damage then
if main_timer > hbarmor.tick then
main_timer = 0
end
2016-02-01 06:06:04 -08:00
for _,player in pairs(minetest.get_connected_players()) do
2015-10-04 05:41:29 -07:00
local name = player:get_player_name()
2015-10-04 05:41:29 -07:00
if hbarmor.player_active[name] == true then
if hbarmor.get_armor(player) == false then
minetest.log("error",
"[hbarmor] Call to hbarmor.get_armor in globalstep returned with false!")
2015-10-04 05:41:29 -07:00
end
2015-10-04 05:41:29 -07:00
update_hud(player)
end
end
end
end
end)