Use new Minetest settings API syntax
[minetest_hbarmor.git] / init.lua
blob21193f91286c36c47aac07942592adbe15482ffb
1 local S
2 if (minetest.get_modpath("intllib")) then
3 S = intllib.Getter()
4 else
5 S = function ( s ) return s end
6 end
8 if (not armor) or (not armor.def) then
9 minetest.log("error", "[hbarmor] Outdated 3d_armor version. Please update your version of 3d_armor!")
10 end
12 local hbarmor = {}
14 -- HUD statbar values
15 hbarmor.armor = {}
17 -- Stores if player's HUD bar has been initialized so far.
18 hbarmor.player_active = {}
20 -- Time difference in seconds between updates to the HUD armor bar.
21 -- Increase this number for slow servers.
22 hbarmor.tick = 0.1
24 -- If true, the armor bar is hidden when the player does not wear any armor
25 hbarmor.autohide = true
27 --load custom settings
28 local set = minetest.settings:get_bool("hbarmor_autohide")
29 if set ~= nil then
30 hbarmor.autohide = set
31 end
33 set = minetest.settings:get("hbarmor_tick")
34 if tonumber(set) ~= nil then
35 hbarmor.tick = tonumber(set)
36 end
39 local must_hide = function(playername, arm)
40 return ((not armor.def[playername].count or armor.def[playername].count == 0) and arm == 0)
41 end
43 local arm_printable = function(arm)
44 return math.ceil(math.floor(arm+0.5))
45 end
47 local function custom_hud(player)
48 local name = player:get_player_name()
50 if minetest.settings:get_bool("enable_damage") then
51 local ret = hbarmor.get_armor(player)
52 if ret == false then
53 minetest.log("error", "[hbarmor] Call to hbarmor.get_armor in custom_hud returned with false!")
54 end
55 local arm = tonumber(hbarmor.armor[name])
56 if not arm then arm = 0 end
57 local hide
58 if hbarmor.autohide then
59 hide = must_hide(name, arm)
60 else
61 hide = false
62 end
63 hb.init_hudbar(player, "armor", arm_printable(arm), nil, hide)
64 end
65 end
67 --register and define armor HUD bar
68 hb.register_hudbar("armor", 0xFFFFFF, S("Armor"), { icon = "hbarmor_icon.png", bgicon = "hbarmor_bgicon.png", bar = "hbarmor_bar.png" }, 0, 100, hbarmor.autohide, S("%s: %d%%"))
70 function hbarmor.get_armor(player)
71 if not player or not armor.def then
72 return false
73 end
74 local name = player:get_player_name()
75 local def = armor.def[name] or nil
76 if def and def.state and def.count then
77 hbarmor.set_armor(name, def.state, def.count)
78 else
79 return false
80 end
81 return true
82 end
84 function hbarmor.set_armor(player_name, ges_state, items)
85 local max_items = 4
86 if items == 5 then
87 max_items = items
88 end
89 local max = max_items * 65535
90 local lvl = max - ges_state
91 lvl = lvl/max
92 if ges_state == 0 and items == 0 then
93 lvl = 0
94 end
96 hbarmor.armor[player_name] = math.max(0, math.min(lvl* (items * (100 / max_items)), 100))
97 end
99 -- update hud elemtens if value has changed
100 local function update_hud(player)
101 local name = player:get_player_name()
102 --armor
103 local arm = tonumber(hbarmor.armor[name])
104 if not arm then
105 arm = 0
106 hbarmor.armor[name] = 0
108 if hbarmor.autohide then
109 -- hide armor bar completely when there is none
110 if must_hide(name, arm) then
111 hb.hide_hudbar(player, "armor")
112 else
113 hb.change_hudbar(player, "armor", arm_printable(arm))
114 hb.unhide_hudbar(player, "armor")
116 else
117 hb.change_hudbar(player, "armor", arm_printable(arm))
121 minetest.register_on_joinplayer(function(player)
122 local name = player:get_player_name()
123 custom_hud(player)
124 hbarmor.player_active[name] = true
125 end)
127 minetest.register_on_leaveplayer(function(player)
128 local name = player:get_player_name()
129 hbarmor.player_active[name] = false
130 end)
132 local main_timer = 0
133 local timer = 0
134 minetest.register_globalstep(function(dtime)
135 main_timer = main_timer + dtime
136 timer = timer + dtime
137 if main_timer > hbarmor.tick or timer > 4 then
138 if minetest.settings:get_bool("enable_damage") then
139 if main_timer > hbarmor.tick then main_timer = 0 end
140 for _,player in ipairs(minetest.get_connected_players()) do
141 local name = player:get_player_name()
142 if hbarmor.player_active[name] == true then
143 local ret = hbarmor.get_armor(player)
144 if ret == false then
145 minetest.log("error", "[hbarmor] Call to hbarmor.get_armor in globalstep returned with false!")
147 -- update all hud elements
148 update_hud(player)
153 if timer > 4 then timer = 0 end
154 end)