1 function recalculate_armor(player)
2 if not player or (player and not player:is_player()) then return end
3 local inv = player:get_inventory()
5 local player_skin = minetest.get_skin(player)
6 local armor_skin = "blank_skin.png"
8 local stack = inv:get_stack("armor_head",1):get_name()
9 if stack ~= "" and minetest.get_item_group(stack,"helmet") > 0 then
10 local skin_element = minetest.get_itemdef(stack, "wearing_texture")
11 player_skin = player_skin.."^"..skin_element
14 stack = inv:get_stack("armor_torso",1):get_name()
15 if stack ~= "" and minetest.get_item_group(stack,"chestplate") > 0 then
16 local skin_element = minetest.get_itemdef(stack, "wearing_texture")
17 armor_skin = armor_skin.."^"..skin_element
20 stack = inv:get_stack("armor_legs",1):get_name()
21 if stack ~= "" and minetest.get_item_group(stack,"leggings") > 0 then
22 local skin_element = minetest.get_itemdef(stack, "wearing_texture")
23 armor_skin = armor_skin.."^"..skin_element
26 stack = inv:get_stack("armor_feet",1):get_name()
27 if stack ~= "" and minetest.get_item_group(stack,"boots") > 0 then
28 local skin_element = minetest.get_itemdef(stack, "wearing_texture")
29 armor_skin = armor_skin.."^"..skin_element
31 player:set_properties({textures = {player_skin,armor_skin}})
34 function calculate_armor_absorbtion(player)
35 if not player or (player and not player:is_player()) then return end
37 local inv = player:get_inventory()
38 local armor_absorbtion = 0
40 local stack = inv:get_stack("armor_head",1):get_name()
42 local level = minetest.get_item_group(stack,"armor_level")
43 local defense = minetest.get_item_group(stack,"armor_defense")
44 armor_absorbtion = armor_absorbtion + (level*defense)
47 stack = inv:get_stack("armor_torso",1):get_name()
49 local level = minetest.get_item_group(stack,"armor_level")
50 local defense = minetest.get_item_group(stack,"armor_defense")
51 armor_absorbtion = armor_absorbtion + (level*defense)
54 stack = inv:get_stack("armor_legs",1):get_name()
56 local level = minetest.get_item_group(stack,"armor_level")
57 local defense = minetest.get_item_group(stack,"armor_defense")
58 armor_absorbtion = armor_absorbtion + (level*defense)
61 stack = inv:get_stack("armor_feet",1):get_name()
63 local level = minetest.get_item_group(stack,"armor_level")
64 local defense = minetest.get_item_group(stack,"armor_defense")
65 armor_absorbtion = armor_absorbtion + (level*defense)
67 if armor_absorbtion > 0 then
68 armor_absorbtion = math.ceil(armor_absorbtion/4)
70 return(armor_absorbtion)
73 function set_armor_gui(player)
74 if not player or (player and not player:is_player()) then return end
75 local meta = player:get_meta()
76 local level = calculate_armor_absorbtion(player)
77 local hud = meta:get_int("armor_bar")
78 player:hud_change(hud, "number", level)
83 function damage_armor(player,damage)
84 if not player or (player and not player:is_player()) then return end
86 local inv = player:get_inventory()
90 local stack = inv:get_stack("armor_head",1)
91 local name = stack:get_name()
93 local wear_level = ((9-minetest.get_item_group(name,"armor_level"))*8)*(5-minetest.get_item_group(name,"armor_type"))*damage
94 stack:add_wear(wear_level)
95 inv:set_stack("armor_head", 1, stack)
96 local new_stack = inv:get_stack("armor_head",1):get_name()
97 if new_stack == "" then
102 stack = inv:get_stack("armor_torso",1)
103 name = stack:get_name()
105 local wear_level = ((9-minetest.get_item_group(name,"armor_level"))*4)*(5-minetest.get_item_group(name,"armor_type"))*damage
106 stack:add_wear(wear_level)
107 inv:set_stack("armor_torso", 1, stack)
108 local new_stack = inv:get_stack("armor_torso",1):get_name()
109 if new_stack == "" then
114 stack = inv:get_stack("armor_legs",1)
115 name = stack:get_name()
117 local wear_level = ((9-minetest.get_item_group(name,"armor_level"))*6)*(5-minetest.get_item_group(name,"armor_type"))*damage
118 stack:add_wear(wear_level)
119 inv:set_stack("armor_legs", 1, stack)
120 local new_stack = inv:get_stack("armor_legs",1):get_name()
121 if new_stack == "" then
126 stack = inv:get_stack("armor_feet",1)
127 name = stack:get_name()
129 local wear_level = ((9-minetest.get_item_group(name,"armor_level"))*10)*(5-minetest.get_item_group(name,"armor_type"))*damage
130 stack:add_wear(wear_level)
131 inv:set_stack("armor_feet", 1, stack)
132 local new_stack = inv:get_stack("armor_feet",1):get_name()
133 if new_stack == "" then
138 if recalc == true then
139 minetest.sound_play("armor_break",{to_player=player:get_player_name(),gain=1,pitch=math.random(80,100)/100})
140 recalculate_armor(player)
141 set_armor_gui(player)
147 minetest.register_on_joinplayer(function(player)
148 local meta = player:get_meta()
150 hud_elem_type = "statbar",
151 position = {x = 0.5, y = 1},
152 text = "armor_icon_bg.png",
155 size = {x = 24, y = 24},
156 offset = {x = (-10 * 24) - 25, y = -(48 + 50 + 39)},
158 local armor_bar = player:hud_add({
159 hud_elem_type = "statbar",
160 position = {x = 0.5, y = 1},
161 text = "armor_icon.png",
162 number = calculate_armor_absorbtion(player),--meta:get_int("hunger"),
164 size = {x = 24, y = 24},
165 offset = {x = (-10 * 24) - 25, y = -(48 + 50 + 39)},
167 meta:set_int("armor_bar", armor_bar)
169 local inv = player:get_inventory()
170 inv:set_size("armor_head" ,1)
171 inv:set_size("armor_torso",1)
172 inv:set_size("armor_legs" ,1)
173 inv:set_size("armor_feet" ,1)
175 minetest.after(0.1,function()
176 recalculate_armor(player)
180 minetest.register_on_dieplayer(function(player)
181 set_armor_gui(player)
184 minetest.register_on_player_inventory_action(function(player, action, inventory, inventory_info)
185 if inventory_info.from_list == "armor_head" or inventory_info.from_list == "armor_torso" or inventory_info.from_list == "armor_legs" or inventory_info.from_list == "armor_feet" or
186 inventory_info.to_list == "armor_head" or inventory_info.to_list == "armor_torso" or inventory_info.to_list == "armor_legs" or inventory_info.to_list == "armor_feet" then
187 minetest.after(0,function()
188 recalculate_armor(player)
189 set_armor_gui(player)
194 --only allow players to put armor in the right slots to stop exploiting chestplates
195 minetest.register_allow_player_inventory_action(function(player, action, inventory, inventory_info)
196 if inventory_info.to_list == "armor_head" then
197 local stack = inventory:get_stack(inventory_info.from_list,inventory_info.from_index)
198 local item = stack:get_name()
199 if minetest.get_item_group(item, "helmet") == 0 then
202 elseif inventory_info.to_list == "armor_torso" then
203 local stack = inventory:get_stack(inventory_info.from_list,inventory_info.from_index)
204 local item = stack:get_name()
205 if minetest.get_item_group(item, "chestplate") == 0 then
208 elseif inventory_info.to_list == "armor_legs" then
209 local stack = inventory:get_stack(inventory_info.from_list,inventory_info.from_index)
210 local item = stack:get_name()
211 if minetest.get_item_group(item, "leggings") == 0 then
214 elseif inventory_info.to_list == "armor_feet" then
215 local stack = inventory:get_stack(inventory_info.from_list,inventory_info.from_index)
216 local item = stack:get_name()
217 if minetest.get_item_group(item, "boots") == 0 then
223 local materials = {["iron"]=4,["chain"]=6,["gold"]=2,["diamond"]=8} --max 8
224 local armor_type = {["helmet"]=2,["chestplate"]=4,["leggings"]=3,["boots"]=1} --max 4
226 local function bool_int(state)
227 if state == true then return(1) end
228 if state == false or not state then return(0) end
231 for material_id,material in pairs(materials) do
232 for armor_id,armor in pairs(armor_type) do
233 --print(material_id,material,"|",armor_id,armor)
234 minetest.register_tool("armor:"..material_id.."_"..armor_id,{
235 description = material_id:gsub("^%l", string.upper).." "..armor_id:gsub("^%l", string.upper),
239 armor_level = material,
240 armor_defense = armor,
241 helmet = bool_int(armor_id == "helmet"),
242 chestplate = bool_int(armor_id == "chestplate"),
243 leggings = bool_int(armor_id == "leggings"),
244 boots = bool_int(armor_id == "boots"),
246 inventory_image = material_id.."_"..armor_id.."_item.png",
248 wearing_texture = material_id.."_"..armor_id..".png",
249 tool_capabilities = {
250 full_punch_interval = 0,
257 punch_attack_uses = 0,
261 if armor_id == "helmet" then
262 minetest.register_craft({
263 output = "armor:"..material_id.."_"..armor_id,
265 {"main:"..material_id, "main:"..material_id, "main:"..material_id},
266 {"main:"..material_id, "" , "main:"..material_id},
270 elseif armor_id == "chestplate" then
271 minetest.register_craft({
272 output = "armor:"..material_id.."_"..armor_id,
274 {"main:"..material_id, "" , "main:"..material_id},
275 {"main:"..material_id, "main:"..material_id, "main:"..material_id},
276 {"main:"..material_id, "main:"..material_id, "main:"..material_id}
279 elseif armor_id == "leggings" then
280 minetest.register_craft({
281 output = "armor:"..material_id.."_"..armor_id,
283 {"main:"..material_id, "main:"..material_id, "main:"..material_id},
284 {"main:"..material_id, "" , "main:"..material_id},
285 {"main:"..material_id, "" , "main:"..material_id}
288 elseif armor_id == "boots" then
289 minetest.register_craft({
290 output = "armor:"..material_id.."_"..armor_id,
293 {"main:"..material_id, "", "main:"..material_id},
294 {"main:"..material_id, "", "main:"..material_id}
297 minetest.register_node("armor:"..material_id.."_"..armor_id.."particletexture", {
299 tiles = {material_id.."_"..armor_id.."_item.png"},
302 drawtype = "allfaces",
303 on_construct = function(pos)
304 minetest.remove_node(pos)