6 local get_item_group = minetest.get_item_group
7 local get_itemdef = minetest.get_itemdef
10 local ceil = math.ceil
11 local random = math.random
18 function recalculate_armor(player, reload_inv)
20 if not player or (player and not player:is_player()) then return end
22 inv = player:get_inventory()
24 player_skin = get_skin(player)
25 armor_skin = "blank_skin.png"
27 stack = inv:get_stack("armor_head",1):get_name()
28 if stack ~= "" and get_item_group(stack,"helmet") > 0 then
29 skin_element = get_itemdef(stack, "wearing_texture")
30 player_skin = player_skin.."^"..skin_element
33 stack = inv:get_stack("armor_torso",1):get_name()
34 if stack ~= "" and get_item_group(stack,"chestplate") > 0 then
35 skin_element = get_itemdef(stack, "wearing_texture")
36 armor_skin = armor_skin.."^"..skin_element
39 stack = inv:get_stack("armor_legs",1):get_name()
40 if stack ~= "" and get_item_group(stack,"leggings") > 0 then
41 skin_element = get_itemdef(stack, "wearing_texture")
42 armor_skin = armor_skin.."^"..skin_element
45 stack = inv:get_stack("armor_feet",1):get_name()
46 if stack ~= "" and get_item_group(stack,"boots") > 0 then
47 skin_element = get_itemdef(stack, "wearing_texture")
48 armor_skin = armor_skin.."^"..skin_element
50 player:set_properties({textures = {player_skin,armor_skin}})
53 inventory.reload(player)
60 local armor_absorbtion
64 function calculate_armor_absorbtion(player)
65 if not player or (player and not player:is_player()) then return end
67 inv = player:get_inventory()
70 stack = inv:get_stack("armor_head",1):get_name()
72 level = get_item_group(stack,"armor_level")
73 defense = get_item_group(stack,"armor_defense")
74 armor_absorbtion = armor_absorbtion + (level*defense)
77 stack = inv:get_stack("armor_torso",1):get_name()
79 level = get_item_group(stack,"armor_level")
80 defense = get_item_group(stack,"armor_defense")
81 armor_absorbtion = armor_absorbtion + (level*defense)
84 stack = inv:get_stack("armor_legs",1):get_name()
86 level = get_item_group(stack,"armor_level")
87 defense = get_item_group(stack,"armor_defense")
88 armor_absorbtion = armor_absorbtion + (level*defense)
91 stack = inv:get_stack("armor_feet",1):get_name()
93 level = get_item_group(stack,"armor_level")
94 defense = get_item_group(stack,"armor_defense")
95 armor_absorbtion = armor_absorbtion + (level*defense)
97 if armor_absorbtion > 0 then
98 armor_absorbtion = ceil(armor_absorbtion/4)
100 return(armor_absorbtion)
104 function set_armor_gui(player)
105 if not player or (player and not player:is_player()) then return end
106 level = calculate_armor_absorbtion(player)
107 hud_manager.change_hud({
109 hud_name = "armor_fg",
123 function damage_armor(player,damage)
124 if not player or (player and not player:is_player()) then return end
126 inv = player:get_inventory()
130 stack = inv:get_stack("armor_head",1)
131 name = stack:get_name()
133 wear_level = ((9-get_item_group(name,"armor_level"))*8)*(5-get_item_group(name,"armor_type"))*damage
134 stack:add_wear(wear_level)
135 inv:set_stack("armor_head", 1, stack)
136 new_stack = inv:get_stack("armor_head",1):get_name()
137 if new_stack == "" then
142 stack = inv:get_stack("armor_torso",1)
143 name = stack:get_name()
145 wear_level = ((9-get_item_group(name,"armor_level"))*4)*(5-get_item_group(name,"armor_type"))*damage
146 stack:add_wear(wear_level)
147 inv:set_stack("armor_torso", 1, stack)
148 new_stack = inv:get_stack("armor_torso",1):get_name()
149 if new_stack == "" then
154 stack = inv:get_stack("armor_legs",1)
155 name = stack:get_name()
157 wear_level = ((9-get_item_group(name,"armor_level"))*6)*(5-get_item_group(name,"armor_type"))*damage
158 stack:add_wear(wear_level)
159 inv:set_stack("armor_legs", 1, stack)
160 new_stack = inv:get_stack("armor_legs",1):get_name()
161 if new_stack == "" then
166 stack = inv:get_stack("armor_feet",1)
167 name = stack:get_name()
169 wear_level = ((9-get_item_group(name,"armor_level"))*10)*(5-get_item_group(name,"armor_type"))*damage
170 stack:add_wear(wear_level)
171 inv:set_stack("armor_feet", 1, stack)
172 new_stack = inv:get_stack("armor_feet",1):get_name()
173 if new_stack == "" then
178 if recalc == true then
179 minetest.sound_play("armor_break",{to_player=player:get_player_name(),gain=1,pitch=random(80,100)/100})
180 recalculate_armor(player)
181 set_armor_gui(player)
187 minetest.register_on_joinplayer(function(player)
188 hud_manager.add_hud(player,"armor_bg",{
189 hud_elem_type = "statbar",
190 position = {x = 0.5, y = 1},
191 text = "armor_icon_bg.png",
193 size = {x = 24, y = 24},
194 offset = {x = (-10 * 24) - 25, y = -(48 + 50 + 39)},
196 hud_manager.add_hud(player,"armor_fg",{
197 hud_elem_type = "statbar",
198 position = {x = 0.5, y = 1},
199 text = "armor_icon.png",
200 number = calculate_armor_absorbtion(player),
201 size = {x = 24, y = 24},
202 offset = {x = (-10 * 24) - 25, y = -(48 + 50 + 39)},
205 inv = player:get_inventory()
206 inv:set_size("armor_head" ,1)
207 inv:set_size("armor_torso",1)
208 inv:set_size("armor_legs" ,1)
209 inv:set_size("armor_feet" ,1)
212 minetest.register_on_dieplayer(function(player)
213 set_armor_gui(player)
217 ["armor_head"] = true,
218 ["armor_torso"] = true,
219 ["armor_legs"] = true,
220 ["armor_feet"] = true,
222 minetest.register_on_player_inventory_action(function(player, action, inventory, inventory_info)
223 if acceptable[inventory_info.from_list] or acceptable[inventory_info.to_list] then
224 minetest.after(0,function()
225 recalculate_armor(player, true)
226 set_armor_gui(player)
231 --only allow players to put armor in the right slots to stop exploiting chestplates
234 minetest.register_allow_player_inventory_action(function(player, action, inventory, inventory_info)
235 if inventory_info.to_list == "armor_head" then
236 stack = inventory:get_stack(inventory_info.from_list,inventory_info.from_index)
237 item = stack:get_name()
238 if get_item_group(item, "helmet") == 0 then
241 elseif inventory_info.to_list == "armor_torso" then
242 stack = inventory:get_stack(inventory_info.from_list,inventory_info.from_index)
243 item = stack:get_name()
244 if get_item_group(item, "chestplate") == 0 then
247 elseif inventory_info.to_list == "armor_legs" then
248 stack = inventory:get_stack(inventory_info.from_list,inventory_info.from_index)
249 item = stack:get_name()
250 if get_item_group(item, "leggings") == 0 then
253 elseif inventory_info.to_list == "armor_feet" then
254 stack = inventory:get_stack(inventory_info.from_list,inventory_info.from_index)
255 item = stack:get_name()
256 if get_item_group(item, "boots") == 0 then
262 local materials = {["coal"]=1,["lapis"]=2,["iron"]=3,["chain"]=4,["gold"]=2,["diamond"]=5,["emerald"]=6,["sapphire"]=7,["ruby"]=8} --max 8
263 local armor_type = {["helmet"]=2,["chestplate"]=4,["leggings"]=3,["boots"]=1} --max 4
265 local function bool_int(state)
266 if state == true then return(1) end
267 if state == false or not state then return(0) end
270 for material_id,material in pairs(materials) do
271 for armor_id,armor in pairs(armor_type) do
272 --print(material_id,material,"|",armor_id,armor)
273 minetest.register_tool("armor:"..material_id.."_"..armor_id,{
274 description = material_id:gsub("^%l", string.upper).." "..armor_id:gsub("^%l", string.upper),
278 armor_level = material,
279 armor_defense = armor,
280 helmet = bool_int(armor_id == "helmet"),
281 chestplate = bool_int(armor_id == "chestplate"),
282 leggings = bool_int(armor_id == "leggings"),
283 boots = bool_int(armor_id == "boots"),
285 inventory_image = material_id.."_"..armor_id.."_item.png",
287 wearing_texture = material_id.."_"..armor_id..".png",
288 tool_capabilities = {
289 full_punch_interval = 0,
296 punch_attack_uses = 0,
300 if armor_id == "helmet" then
301 minetest.register_craft({
302 output = "armor:"..material_id.."_"..armor_id,
304 {"main:"..material_id, "main:"..material_id, "main:"..material_id},
305 {"main:"..material_id, "" , "main:"..material_id},
309 elseif armor_id == "chestplate" then
310 minetest.register_craft({
311 output = "armor:"..material_id.."_"..armor_id,
313 {"main:"..material_id, "" , "main:"..material_id},
314 {"main:"..material_id, "main:"..material_id, "main:"..material_id},
315 {"main:"..material_id, "main:"..material_id, "main:"..material_id}
318 elseif armor_id == "leggings" then
319 minetest.register_craft({
320 output = "armor:"..material_id.."_"..armor_id,
322 {"main:"..material_id, "main:"..material_id, "main:"..material_id},
323 {"main:"..material_id, "" , "main:"..material_id},
324 {"main:"..material_id, "" , "main:"..material_id}
327 elseif armor_id == "boots" then
328 minetest.register_craft({
329 output = "armor:"..material_id.."_"..armor_id,
332 {"main:"..material_id, "", "main:"..material_id},
333 {"main:"..material_id, "", "main:"..material_id}
336 minetest.register_node("armor:"..material_id.."_"..armor_id.."particletexture", {
338 tiles = {material_id.."_"..armor_id.."_item.png"},
341 drawtype = "allfaces",
342 on_construct = function(pos)
343 minetest.remove_node(pos)