Version 1.0.1
[minetest_hbarmor.git] / init.lua
blobc9d4299b3556e321200a7b4f960e37c539e2e50b
1 local S = minetest.get_translator("hbarmor")
2 local N = function(s) return s end
4 if (not armor) or (not armor.def) then
5 minetest.log("error", "[hbarmor] Outdated 3d_armor version. Please update your version of 3d_armor!")
6 end
8 local hbarmor = {}
10 -- HUD statbar values
11 hbarmor.armor = {}
13 -- Stores if player's HUD bar has been initialized so far.
14 hbarmor.player_active = {}
16 -- Time difference in seconds between updates to the HUD armor bar.
17 -- Increase this number for slow servers.
18 hbarmor.tick = 0.1
20 -- If true, the armor bar is hidden when the player does not wear any armor
21 hbarmor.autohide = true
23 --load custom settings
24 local set = minetest.settings:get_bool("hbarmor_autohide")
25 if set ~= nil then
26 hbarmor.autohide = set
27 end
29 set = minetest.settings:get("hbarmor_tick")
30 if tonumber(set) ~= nil then
31 hbarmor.tick = tonumber(set)
32 end
35 local must_hide = function(playername, arm)
36 return ((not armor.def[playername].count or armor.def[playername].count == 0) and arm == 0)
37 end
39 local arm_printable = function(arm)
40 return math.ceil(math.floor(arm+0.5))
41 end
43 local function custom_hud(player)
44 local name = player:get_player_name()
46 if minetest.settings:get_bool("enable_damage") then
47 local ret = hbarmor.get_armor(player)
48 if ret == false then
49 minetest.log("error", "[hbarmor] Call to hbarmor.get_armor in custom_hud returned with false!")
50 end
51 local arm = tonumber(hbarmor.armor[name])
52 if not arm then arm = 0 end
53 local hide
54 if hbarmor.autohide then
55 hide = must_hide(name, arm)
56 else
57 hide = false
58 end
59 hb.init_hudbar(player, "armor", arm_printable(arm), nil, hide)
60 end
61 end
63 --register and define armor HUD bar
64 hb.register_hudbar("armor", 0xFFFFFF, S("Armor"), { icon = "hbarmor_icon.png", bgicon = "hbarmor_bgicon.png", bar = "hbarmor_bar.png" }, 0, 100, hbarmor.autohide, N("@1: @2%"), { order = { "label", "value" }, textdomain = "hbarmor" } )
66 function hbarmor.get_armor(player)
67 if not player or not armor.def then
68 return false
69 end
70 local name = player:get_player_name()
71 local def = armor.def[name] or nil
72 if def and def.state and def.count then
73 hbarmor.set_armor(name, def.state, def.count)
74 else
75 return false
76 end
77 return true
78 end
80 function hbarmor.set_armor(player_name, ges_state, items)
81 local max_items = 4
82 if items == 5 then
83 max_items = items
84 end
85 local max = max_items * 65535
86 local lvl = max - ges_state
87 lvl = lvl/max
88 if ges_state == 0 and items == 0 then
89 lvl = 0
90 end
92 hbarmor.armor[player_name] = math.max(0, math.min(lvl* (items * (100 / max_items)), 100))
93 end
95 -- update hud elemtens if value has changed
96 local function update_hud(player)
97 local name = player:get_player_name()
98 --armor
99 local arm = tonumber(hbarmor.armor[name])
100 if not arm then
101 arm = 0
102 hbarmor.armor[name] = 0
104 if hbarmor.autohide then
105 -- hide armor bar completely when there is none
106 if must_hide(name, arm) then
107 hb.hide_hudbar(player, "armor")
108 else
109 hb.change_hudbar(player, "armor", arm_printable(arm))
110 hb.unhide_hudbar(player, "armor")
112 else
113 hb.change_hudbar(player, "armor", arm_printable(arm))
117 minetest.register_on_joinplayer(function(player)
118 local name = player:get_player_name()
119 custom_hud(player)
120 hbarmor.player_active[name] = true
121 end)
123 minetest.register_on_leaveplayer(function(player)
124 local name = player:get_player_name()
125 hbarmor.player_active[name] = false
126 end)
128 local main_timer = 0
129 local timer = 0
130 minetest.register_globalstep(function(dtime)
131 main_timer = main_timer + dtime
132 timer = timer + dtime
133 if main_timer > hbarmor.tick or timer > 4 then
134 if minetest.settings:get_bool("enable_damage") then
135 if main_timer > hbarmor.tick then main_timer = 0 end
136 for _,player in ipairs(minetest.get_connected_players()) do
137 local name = player:get_player_name()
138 if hbarmor.player_active[name] == true then
139 local ret = hbarmor.get_armor(player)
140 if ret == false then
141 minetest.log("error", "[hbarmor] Call to hbarmor.get_armor in globalstep returned with false!")
143 -- update all hud elements
144 update_hud(player)
149 if timer > 4 then timer = 0 end
150 end)