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)
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}})
54 local armor_absorbtion
58 function calculate_armor_absorbtion(player)
59 if not player or (player and not player:is_player()) then return end
61 inv = player:get_inventory()
64 stack = inv:get_stack("armor_head",1):get_name()
66 level = get_item_group(stack,"armor_level")
67 defense = get_item_group(stack,"armor_defense")
68 armor_absorbtion = armor_absorbtion + (level*defense)
71 stack = inv:get_stack("armor_torso",1):get_name()
73 level = get_item_group(stack,"armor_level")
74 defense = get_item_group(stack,"armor_defense")
75 armor_absorbtion = armor_absorbtion + (level*defense)
78 stack = inv:get_stack("armor_legs",1):get_name()
80 level = get_item_group(stack,"armor_level")
81 defense = get_item_group(stack,"armor_defense")
82 armor_absorbtion = armor_absorbtion + (level*defense)
85 stack = inv:get_stack("armor_feet",1):get_name()
87 level = get_item_group(stack,"armor_level")
88 defense = get_item_group(stack,"armor_defense")
89 armor_absorbtion = armor_absorbtion + (level*defense)
91 if armor_absorbtion > 0 then
92 armor_absorbtion = ceil(armor_absorbtion/4)
94 return(armor_absorbtion)
98 function set_armor_gui(player)
99 if not player or (player and not player:is_player()) then return end
100 level = calculate_armor_absorbtion(player)
101 hud_manager.change_hud({
103 hud_name = "armor_fg",
117 function damage_armor(player,damage)
118 if not player or (player and not player:is_player()) then return end
120 inv = player:get_inventory()
124 stack = inv:get_stack("armor_head",1)
125 name = stack:get_name()
127 wear_level = ((9-get_item_group(name,"armor_level"))*8)*(5-get_item_group(name,"armor_type"))*damage
128 stack:add_wear(wear_level)
129 inv:set_stack("armor_head", 1, stack)
130 new_stack = inv:get_stack("armor_head",1):get_name()
131 if new_stack == "" then
136 stack = inv:get_stack("armor_torso",1)
137 name = stack:get_name()
139 wear_level = ((9-get_item_group(name,"armor_level"))*4)*(5-get_item_group(name,"armor_type"))*damage
140 stack:add_wear(wear_level)
141 inv:set_stack("armor_torso", 1, stack)
142 new_stack = inv:get_stack("armor_torso",1):get_name()
143 if new_stack == "" then
148 stack = inv:get_stack("armor_legs",1)
149 name = stack:get_name()
151 wear_level = ((9-get_item_group(name,"armor_level"))*6)*(5-get_item_group(name,"armor_type"))*damage
152 stack:add_wear(wear_level)
153 inv:set_stack("armor_legs", 1, stack)
154 new_stack = inv:get_stack("armor_legs",1):get_name()
155 if new_stack == "" then
160 stack = inv:get_stack("armor_feet",1)
161 name = stack:get_name()
163 wear_level = ((9-get_item_group(name,"armor_level"))*10)*(5-get_item_group(name,"armor_type"))*damage
164 stack:add_wear(wear_level)
165 inv:set_stack("armor_feet", 1, stack)
166 new_stack = inv:get_stack("armor_feet",1):get_name()
167 if new_stack == "" then
172 if recalc == true then
173 minetest.sound_play("armor_break",{to_player=player:get_player_name(),gain=1,pitch=random(80,100)/100})
174 recalculate_armor(player)
175 set_armor_gui(player)
181 minetest.register_on_joinplayer(function(player)
182 hud_manager.add_hud(player,"armor_bg",{
183 hud_elem_type = "statbar",
184 position = {x = 0.5, y = 1},
185 text = "armor_icon_bg.png",
187 size = {x = 24, y = 24},
188 offset = {x = (-10 * 24) - 25, y = -(48 + 50 + 39)},
190 hud_manager.add_hud(player,"armor_fg",{
191 hud_elem_type = "statbar",
192 position = {x = 0.5, y = 1},
193 text = "armor_icon.png",
194 number = calculate_armor_absorbtion(player),
195 size = {x = 24, y = 24},
196 offset = {x = (-10 * 24) - 25, y = -(48 + 50 + 39)},
199 inv = player:get_inventory()
200 inv:set_size("armor_head" ,1)
201 inv:set_size("armor_torso",1)
202 inv:set_size("armor_legs" ,1)
203 inv:set_size("armor_feet" ,1)
206 minetest.register_on_dieplayer(function(player)
207 set_armor_gui(player)
211 ["armor_head"] = true,
212 ["armor_torso"] = true,
213 ["armor_legs"] = true,
214 ["armor_feet"] = true,
216 minetest.register_on_player_inventory_action(function(player, action, inventory, inventory_info)
217 if acceptable[inventory_info.from_list] or acceptable[inventory_info.to_list] then
218 minetest.after(0,function()
219 recalculate_armor(player)
220 set_armor_gui(player)
225 --only allow players to put armor in the right slots to stop exploiting chestplates
228 minetest.register_allow_player_inventory_action(function(player, action, inventory, inventory_info)
229 if inventory_info.to_list == "armor_head" then
230 stack = inventory:get_stack(inventory_info.from_list,inventory_info.from_index)
231 item = stack:get_name()
232 if get_item_group(item, "helmet") == 0 then
235 elseif inventory_info.to_list == "armor_torso" 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, "chestplate") == 0 then
241 elseif inventory_info.to_list == "armor_legs" 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, "leggings") == 0 then
247 elseif inventory_info.to_list == "armor_feet" 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, "boots") == 0 then
256 local materials = {["coal"]=1,["lapis"]=2,["iron"]=3,["chain"]=4,["gold"]=2,["diamond"]=5,["emerald"]=6,["sapphire"]=7,["ruby"]=8} --max 8
257 local armor_type = {["helmet"]=2,["chestplate"]=4,["leggings"]=3,["boots"]=1} --max 4
259 local function bool_int(state)
260 if state == true then return(1) end
261 if state == false or not state then return(0) end
264 for material_id,material in pairs(materials) do
265 for armor_id,armor in pairs(armor_type) do
266 --print(material_id,material,"|",armor_id,armor)
267 minetest.register_tool("armor:"..material_id.."_"..armor_id,{
268 description = material_id:gsub("^%l", string.upper).." "..armor_id:gsub("^%l", string.upper),
272 armor_level = material,
273 armor_defense = armor,
274 helmet = bool_int(armor_id == "helmet"),
275 chestplate = bool_int(armor_id == "chestplate"),
276 leggings = bool_int(armor_id == "leggings"),
277 boots = bool_int(armor_id == "boots"),
279 inventory_image = material_id.."_"..armor_id.."_item.png",
281 wearing_texture = material_id.."_"..armor_id..".png",
282 tool_capabilities = {
283 full_punch_interval = 0,
290 punch_attack_uses = 0,
294 if armor_id == "helmet" then
295 minetest.register_craft({
296 output = "armor:"..material_id.."_"..armor_id,
298 {"main:"..material_id, "main:"..material_id, "main:"..material_id},
299 {"main:"..material_id, "" , "main:"..material_id},
303 elseif armor_id == "chestplate" then
304 minetest.register_craft({
305 output = "armor:"..material_id.."_"..armor_id,
307 {"main:"..material_id, "" , "main:"..material_id},
308 {"main:"..material_id, "main:"..material_id, "main:"..material_id},
309 {"main:"..material_id, "main:"..material_id, "main:"..material_id}
312 elseif armor_id == "leggings" then
313 minetest.register_craft({
314 output = "armor:"..material_id.."_"..armor_id,
316 {"main:"..material_id, "main:"..material_id, "main:"..material_id},
317 {"main:"..material_id, "" , "main:"..material_id},
318 {"main:"..material_id, "" , "main:"..material_id}
321 elseif armor_id == "boots" then
322 minetest.register_craft({
323 output = "armor:"..material_id.."_"..armor_id,
326 {"main:"..material_id, "", "main:"..material_id},
327 {"main:"..material_id, "", "main:"..material_id}
330 minetest.register_node("armor:"..material_id.."_"..armor_id.."particletexture", {
332 tiles = {material_id.."_"..armor_id.."_item.png"},
335 drawtype = "allfaces",
336 on_construct = function(pos)
337 minetest.remove_node(pos)