2020-06-13 23:16:47 -04:00
|
|
|
local minetest,math,skins_pointer = minetest,math,skins_pointer
|
2020-06-10 13:56:00 -04:00
|
|
|
armor_class = {} --the armor class
|
|
|
|
|
|
|
|
function armor_class.recalculate_armor(player)
|
2020-05-28 18:20:02 -04:00
|
|
|
if not player or (player and not player:is_player()) then return end
|
|
|
|
local inv = player:get_inventory()
|
2020-06-10 12:41:19 -04:00
|
|
|
|
2020-06-13 23:16:47 -04:00
|
|
|
local player_skin = skins_pointer.get_skin(player)
|
2020-05-30 00:45:38 -04:00
|
|
|
local armor_skin = "blank_skin.png"
|
2020-05-28 18:20:02 -04:00
|
|
|
|
|
|
|
local stack = inv:get_stack("armor_head",1):get_name()
|
2020-06-09 07:51:26 -04:00
|
|
|
if stack ~= "" and minetest.get_item_group(stack,"helmet") > 0 then
|
2020-06-07 06:03:17 -04:00
|
|
|
local skin_element = minetest.get_itemdef(stack, "wearing_texture")
|
|
|
|
player_skin = player_skin.."^"..skin_element
|
2020-05-28 18:20:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
stack = inv:get_stack("armor_torso",1):get_name()
|
2020-06-09 07:51:26 -04:00
|
|
|
if stack ~= "" and minetest.get_item_group(stack,"chestplate") > 0 then
|
2020-06-07 06:03:17 -04:00
|
|
|
local skin_element = minetest.get_itemdef(stack, "wearing_texture")
|
|
|
|
armor_skin = armor_skin.."^"..skin_element
|
2020-05-28 18:20:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
stack = inv:get_stack("armor_legs",1):get_name()
|
2020-06-09 07:51:26 -04:00
|
|
|
if stack ~= "" and minetest.get_item_group(stack,"leggings") > 0 then
|
2020-06-07 06:03:17 -04:00
|
|
|
local skin_element = minetest.get_itemdef(stack, "wearing_texture")
|
|
|
|
armor_skin = armor_skin.."^"..skin_element
|
2020-05-28 18:20:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
stack = inv:get_stack("armor_feet",1):get_name()
|
2020-06-09 07:51:26 -04:00
|
|
|
if stack ~= "" and minetest.get_item_group(stack,"boots") > 0 then
|
2020-06-07 06:03:17 -04:00
|
|
|
local skin_element = minetest.get_itemdef(stack, "wearing_texture")
|
|
|
|
armor_skin = armor_skin.."^"..skin_element
|
2020-05-28 18:20:02 -04:00
|
|
|
end
|
2020-05-30 00:45:38 -04:00
|
|
|
player:set_properties({textures = {player_skin,armor_skin}})
|
2020-05-28 18:20:02 -04:00
|
|
|
end
|
|
|
|
|
2020-06-10 13:56:00 -04:00
|
|
|
function armor_class.calculate_armor_absorbtion(player)
|
2020-05-30 23:20:50 -04:00
|
|
|
if not player or (player and not player:is_player()) then return end
|
|
|
|
|
|
|
|
local inv = player:get_inventory()
|
|
|
|
local armor_absorbtion = 0
|
|
|
|
|
|
|
|
local stack = inv:get_stack("armor_head",1):get_name()
|
|
|
|
if stack ~= "" then
|
|
|
|
local level = minetest.get_item_group(stack,"armor_level")
|
|
|
|
local defense = minetest.get_item_group(stack,"armor_defense")
|
|
|
|
armor_absorbtion = armor_absorbtion + (level*defense)
|
|
|
|
end
|
|
|
|
|
|
|
|
stack = inv:get_stack("armor_torso",1):get_name()
|
|
|
|
if stack ~= "" then
|
|
|
|
local level = minetest.get_item_group(stack,"armor_level")
|
|
|
|
local defense = minetest.get_item_group(stack,"armor_defense")
|
|
|
|
armor_absorbtion = armor_absorbtion + (level*defense)
|
|
|
|
end
|
|
|
|
|
|
|
|
stack = inv:get_stack("armor_legs",1):get_name()
|
|
|
|
if stack ~= "" then
|
|
|
|
local level = minetest.get_item_group(stack,"armor_level")
|
|
|
|
local defense = minetest.get_item_group(stack,"armor_defense")
|
|
|
|
armor_absorbtion = armor_absorbtion + (level*defense)
|
|
|
|
end
|
|
|
|
|
|
|
|
stack = inv:get_stack("armor_feet",1):get_name()
|
|
|
|
if stack ~= "" then
|
|
|
|
local level = minetest.get_item_group(stack,"armor_level")
|
|
|
|
local defense = minetest.get_item_group(stack,"armor_defense")
|
|
|
|
armor_absorbtion = armor_absorbtion + (level*defense)
|
|
|
|
end
|
|
|
|
if armor_absorbtion > 0 then
|
|
|
|
armor_absorbtion = math.ceil(armor_absorbtion/4)
|
|
|
|
end
|
|
|
|
return(armor_absorbtion)
|
|
|
|
end
|
|
|
|
|
2020-06-10 13:56:00 -04:00
|
|
|
function armor_class.set_armor_gui(player)
|
2020-05-31 00:55:13 -04:00
|
|
|
if not player or (player and not player:is_player()) then return end
|
|
|
|
local meta = player:get_meta()
|
2020-06-10 13:56:00 -04:00
|
|
|
local level = armor_class.calculate_armor_absorbtion(player)
|
2020-05-31 00:55:13 -04:00
|
|
|
local hud = meta:get_int("armor_bar")
|
|
|
|
player:hud_change(hud, "number", level)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-06-10 13:56:00 -04:00
|
|
|
function armor_class.damage_armor(player,damage)
|
2020-05-30 23:20:50 -04:00
|
|
|
if not player or (player and not player:is_player()) then return end
|
|
|
|
|
|
|
|
local inv = player:get_inventory()
|
|
|
|
|
|
|
|
local recalc = false
|
|
|
|
|
|
|
|
local stack = inv:get_stack("armor_head",1)
|
|
|
|
local name = stack:get_name()
|
|
|
|
if name ~= "" then
|
|
|
|
local wear_level = ((9-minetest.get_item_group(name,"armor_level"))*8)*(5-minetest.get_item_group(name,"armor_type"))*damage
|
|
|
|
stack:add_wear(wear_level)
|
|
|
|
inv:set_stack("armor_head", 1, stack)
|
|
|
|
local new_stack = inv:get_stack("armor_head",1):get_name()
|
|
|
|
if new_stack == "" then
|
|
|
|
recalc = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
stack = inv:get_stack("armor_torso",1)
|
|
|
|
name = stack:get_name()
|
|
|
|
if name ~= "" then
|
|
|
|
local wear_level = ((9-minetest.get_item_group(name,"armor_level"))*4)*(5-minetest.get_item_group(name,"armor_type"))*damage
|
|
|
|
stack:add_wear(wear_level)
|
|
|
|
inv:set_stack("armor_torso", 1, stack)
|
|
|
|
local new_stack = inv:get_stack("armor_torso",1):get_name()
|
|
|
|
if new_stack == "" then
|
|
|
|
recalc = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
stack = inv:get_stack("armor_legs",1)
|
|
|
|
name = stack:get_name()
|
|
|
|
if name ~= "" then
|
|
|
|
local wear_level = ((9-minetest.get_item_group(name,"armor_level"))*6)*(5-minetest.get_item_group(name,"armor_type"))*damage
|
|
|
|
stack:add_wear(wear_level)
|
|
|
|
inv:set_stack("armor_legs", 1, stack)
|
|
|
|
local new_stack = inv:get_stack("armor_legs",1):get_name()
|
|
|
|
if new_stack == "" then
|
|
|
|
recalc = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
stack = inv:get_stack("armor_feet",1)
|
|
|
|
name = stack:get_name()
|
|
|
|
if name ~= "" then
|
|
|
|
local wear_level = ((9-minetest.get_item_group(name,"armor_level"))*10)*(5-minetest.get_item_group(name,"armor_type"))*damage
|
|
|
|
stack:add_wear(wear_level)
|
|
|
|
inv:set_stack("armor_feet", 1, stack)
|
|
|
|
local new_stack = inv:get_stack("armor_feet",1):get_name()
|
|
|
|
if new_stack == "" then
|
|
|
|
recalc = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if recalc == true then
|
|
|
|
minetest.sound_play("armor_break",{to_player=player:get_player_name(),gain=1,pitch=math.random(80,100)/100})
|
2020-06-10 13:56:00 -04:00
|
|
|
armor_class.recalculate_armor(player)
|
|
|
|
armor_class.set_armor_gui(player)
|
2020-05-30 23:20:50 -04:00
|
|
|
--do particles too
|
|
|
|
end
|
|
|
|
end
|
2020-05-28 18:20:02 -04:00
|
|
|
|
2020-05-31 00:55:13 -04:00
|
|
|
|
2020-05-28 18:20:02 -04:00
|
|
|
minetest.register_on_joinplayer(function(player)
|
2020-05-31 00:55:13 -04:00
|
|
|
local meta = player:get_meta()
|
|
|
|
player:hud_add({
|
|
|
|
hud_elem_type = "statbar",
|
|
|
|
position = {x = 0.5, y = 1},
|
|
|
|
text = "armor_icon_bg.png",
|
|
|
|
number = 20,
|
|
|
|
--direction = 1,
|
|
|
|
size = {x = 24, y = 24},
|
|
|
|
offset = {x = (-10 * 24) - 25, y = -(48 + 50 + 39)},
|
|
|
|
})
|
|
|
|
local armor_bar = player:hud_add({
|
|
|
|
hud_elem_type = "statbar",
|
|
|
|
position = {x = 0.5, y = 1},
|
|
|
|
text = "armor_icon.png",
|
2020-06-10 13:56:00 -04:00
|
|
|
number = armor_class.calculate_armor_absorbtion(player),--meta:get_int("hunger"),
|
2020-05-31 00:55:13 -04:00
|
|
|
--direction = 1,
|
|
|
|
size = {x = 24, y = 24},
|
|
|
|
offset = {x = (-10 * 24) - 25, y = -(48 + 50 + 39)},
|
|
|
|
})
|
|
|
|
meta:set_int("armor_bar", armor_bar)
|
|
|
|
|
2020-05-28 18:20:02 -04:00
|
|
|
local inv = player:get_inventory()
|
|
|
|
inv:set_size("armor_head" ,1)
|
|
|
|
inv:set_size("armor_torso",1)
|
|
|
|
inv:set_size("armor_legs" ,1)
|
|
|
|
inv:set_size("armor_feet" ,1)
|
|
|
|
end)
|
|
|
|
|
2020-05-31 00:55:13 -04:00
|
|
|
minetest.register_on_dieplayer(function(player)
|
2020-06-10 13:56:00 -04:00
|
|
|
armor_class.set_armor_gui(player)
|
2020-05-31 00:55:13 -04:00
|
|
|
end)
|
|
|
|
|
2020-05-28 18:20:02 -04:00
|
|
|
minetest.register_on_player_inventory_action(function(player, action, inventory, inventory_info)
|
|
|
|
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
|
|
|
|
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
|
|
|
|
minetest.after(0,function()
|
2020-06-10 13:56:00 -04:00
|
|
|
armor_class.recalculate_armor(player)
|
|
|
|
armor_class.set_armor_gui(player)
|
2020-05-28 18:20:02 -04:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
--only allow players to put armor in the right slots to stop exploiting chestplates
|
|
|
|
minetest.register_allow_player_inventory_action(function(player, action, inventory, inventory_info)
|
|
|
|
if inventory_info.to_list == "armor_head" then
|
|
|
|
local stack = inventory:get_stack(inventory_info.from_list,inventory_info.from_index)
|
|
|
|
local item = stack:get_name()
|
|
|
|
if minetest.get_item_group(item, "helmet") == 0 then
|
|
|
|
return(0)
|
|
|
|
end
|
|
|
|
elseif inventory_info.to_list == "armor_torso" then
|
|
|
|
local stack = inventory:get_stack(inventory_info.from_list,inventory_info.from_index)
|
|
|
|
local item = stack:get_name()
|
|
|
|
if minetest.get_item_group(item, "chestplate") == 0 then
|
|
|
|
return(0)
|
|
|
|
end
|
|
|
|
elseif inventory_info.to_list == "armor_legs" then
|
|
|
|
local stack = inventory:get_stack(inventory_info.from_list,inventory_info.from_index)
|
|
|
|
local item = stack:get_name()
|
|
|
|
if minetest.get_item_group(item, "leggings") == 0 then
|
|
|
|
return(0)
|
|
|
|
end
|
|
|
|
elseif inventory_info.to_list == "armor_feet" then
|
|
|
|
local stack = inventory:get_stack(inventory_info.from_list,inventory_info.from_index)
|
|
|
|
local item = stack:get_name()
|
|
|
|
if minetest.get_item_group(item, "boots") == 0 then
|
|
|
|
return(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2020-05-30 23:20:50 -04:00
|
|
|
local materials = {["iron"]=4,["chain"]=6,["gold"]=2,["diamond"]=8} --max 8
|
|
|
|
local armor_type = {["helmet"]=2,["chestplate"]=4,["leggings"]=3,["boots"]=1} --max 4
|
2020-05-28 18:20:02 -04:00
|
|
|
|
|
|
|
local function bool_int(state)
|
|
|
|
if state == true then return(1) end
|
|
|
|
if state == false or not state then return(0) end
|
|
|
|
end
|
|
|
|
|
|
|
|
for material_id,material in pairs(materials) do
|
|
|
|
for armor_id,armor in pairs(armor_type) do
|
2020-05-30 02:46:28 -04:00
|
|
|
--print(material_id,material,"|",armor_id,armor)
|
2020-05-28 18:20:02 -04:00
|
|
|
minetest.register_tool("armor:"..material_id.."_"..armor_id,{
|
|
|
|
description = material_id:gsub("^%l", string.upper).." "..armor_id:gsub("^%l", string.upper),
|
|
|
|
|
|
|
|
groups = {
|
|
|
|
armor = 1,
|
|
|
|
armor_level = material,
|
|
|
|
armor_defense = armor,
|
|
|
|
helmet = bool_int(armor_id == "helmet"),
|
|
|
|
chestplate = bool_int(armor_id == "chestplate"),
|
|
|
|
leggings = bool_int(armor_id == "leggings"),
|
|
|
|
boots = bool_int(armor_id == "boots"),
|
|
|
|
},
|
|
|
|
inventory_image = material_id.."_"..armor_id.."_item.png",
|
|
|
|
stack_max = 1,
|
2020-06-07 06:03:17 -04:00
|
|
|
wearing_texture = material_id.."_"..armor_id..".png",
|
2020-05-28 18:20:02 -04:00
|
|
|
tool_capabilities = {
|
|
|
|
full_punch_interval = 0,
|
|
|
|
max_drop_level = 0,
|
|
|
|
groupcaps = {
|
|
|
|
},
|
|
|
|
damage_groups = {
|
|
|
|
|
|
|
|
},
|
|
|
|
punch_attack_uses = 0,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if armor_id == "helmet" then
|
|
|
|
minetest.register_craft({
|
|
|
|
output = "armor:"..material_id.."_"..armor_id,
|
|
|
|
recipe = {
|
|
|
|
{"main:"..material_id, "main:"..material_id, "main:"..material_id},
|
|
|
|
{"main:"..material_id, "" , "main:"..material_id},
|
|
|
|
{"" , "" , "" }
|
|
|
|
}
|
|
|
|
})
|
|
|
|
elseif armor_id == "chestplate" then
|
|
|
|
minetest.register_craft({
|
|
|
|
output = "armor:"..material_id.."_"..armor_id,
|
|
|
|
recipe = {
|
|
|
|
{"main:"..material_id, "" , "main:"..material_id},
|
|
|
|
{"main:"..material_id, "main:"..material_id, "main:"..material_id},
|
|
|
|
{"main:"..material_id, "main:"..material_id, "main:"..material_id}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
elseif armor_id == "leggings" then
|
|
|
|
minetest.register_craft({
|
|
|
|
output = "armor:"..material_id.."_"..armor_id,
|
|
|
|
recipe = {
|
|
|
|
{"main:"..material_id, "main:"..material_id, "main:"..material_id},
|
|
|
|
{"main:"..material_id, "" , "main:"..material_id},
|
|
|
|
{"main:"..material_id, "" , "main:"..material_id}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
elseif armor_id == "boots" then
|
|
|
|
minetest.register_craft({
|
|
|
|
output = "armor:"..material_id.."_"..armor_id,
|
|
|
|
recipe = {
|
|
|
|
{"" , "", "" },
|
|
|
|
{"main:"..material_id, "", "main:"..material_id},
|
|
|
|
{"main:"..material_id, "", "main:"..material_id}
|
|
|
|
}
|
|
|
|
})
|
2020-05-31 17:33:56 -04:00
|
|
|
minetest.register_node("armor:"..material_id.."_"..armor_id.."particletexture", {
|
|
|
|
description = "NIL",
|
|
|
|
tiles = {material_id.."_"..armor_id.."_item.png"},
|
|
|
|
groups = {},
|
|
|
|
drop = "",
|
|
|
|
drawtype = "allfaces",
|
|
|
|
on_construct = function(pos)
|
|
|
|
minetest.remove_node(pos)
|
|
|
|
end,
|
|
|
|
})
|
2020-05-28 18:20:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|