ExchangeClone/exchangeclone/armor.lua

400 lines
15 KiB
Lua
Raw Normal View History

2023-11-19 01:48:35 +01:00
local S = minetest.get_translator()
2023-08-07 22:56:29 +02:00
local function get_armor_texture(type, matter, preview)
2023-10-04 02:08:07 +02:00
local modifier
-- hsl unfortunately only works in 5.8
2023-08-07 22:56:29 +02:00
if matter == "dark" then
modifier = "^[multiply:#222222"
--modifier = "^[hsl:0:-100:-100^[hsl:0:-100:-100"
else
modifier = "^[multiply:#990000"
--modifier = "^[hsl:-180:100:-100"
end
local result
if type:sub(1,3) == "inv" then
result = "exchangeclone_"..type..".png"
elseif exchangeclone.mcl then
2023-08-07 22:56:29 +02:00
result = "exchangeclone_mcl_"..type.."_base.png"..modifier
else
result = "exchangeclone_mtg_"..type.."_base"
if preview then result = result.."_preview" end
result = result..".png"..modifier
end
return result
end
2024-02-04 22:29:48 +01:00
local armor_pieces = {
["exchangeclone:helmet_dark_matter"] = {material = "dark_matter", piece = "helmet", category = "weak"},
["exchangeclone:helmet_red_matter"] = {material = "red_matter", piece = "helmet", category = "weak"},
["exchangeclone:chestplate_dark_matter"] = {material = "dark_matter", piece = "chestplate", category = "strong"},
["exchangeclone:chestplate_red_matter"] = {material = "red_matter", piece = "chestplate", category = "strong"},
["exchangeclone:leggings_dark_matter"] = {material = "dark_matter", piece = "leggings", category = "strong"},
["exchangeclone:leggings_red_matter"] = {material = "red_matter", piece = "leggings", category = "strong"},
["exchangeclone:boots_dark_matter"] = {material = "dark_matter", piece = "boots", category = "weak"},
["exchangeclone:boots_red_matter"] = {material = "red_matter", piece = "boots", category = "weak"},
}
local armor_categories = {
weak = {dark_matter = exchangeclone.mcl and 0.16 or 0.13, red_matter = exchangeclone.mcl and 0.18 or 0.15},
strong = {dark_matter = exchangeclone.mcl and 0.24 or 0.18, red_matter = exchangeclone.mcl and 0.27 or 0.2}
}
local function a_damage_formula(armor_data, damage, reason)
local start
local threshold
local first_value
if armor_data.material == "dark_matter" then
if armor_data.category == "weak" then
if reason.type == "explosion" then
start = 70
else
start = 20
end
threshold = start + 6
first_value = 0.9
else
if reason.type == "explosion" then
start = 105
else
start = 45
end
threshold = start + 16
first_value = 0.7
end
else
if armor_data.category == "weak" then
if reason.type == "explosion" then
start = 100
else
start = 50
end
threshold = start + 6
first_value = 0.9
else
if reason.type == "explosion" then
start = 150
threshold = 162
first_value = 0.78
else
start = 70
threshold = 76
first_value = 0.9
end
end
end
if damage < start then
return damage
elseif damage <= threshold then
-- This formula took me unnecessarily long to figure out.
return damage-(first_value+((damage-1)*((0.03*(damage)/2)+first_value)))
else
return damage*0.05 -- Not exact (some armor pieces are 3%, some are 7%, but I don't care)
end
end
local function get_blocked_damage(itemstack, damage, reason)
local armor_data = armor_pieces[itemstack:get_name()]
if not armor_data then return 0 end
local base_block = armor_categories[armor_data.category][armor_data.material]
if reason.type == "lava" then
return damage
elseif reason.type == "fall" then
if armor_data.piece == "boots" then
if armor_data.material == "dark_matter" then
if damage < 31 then
return 5
end
elseif damage < 55 then
return 10
end
end
return base_block*damage
elseif reason.type == "explosion" or reason.type == "anvil" or reason.flags.is_projectile then
return a_damage_formula(armor_data, damage, reason)
elseif reason.type == "drown" then
if armor_data.piece == "helmet" then
if damage < 10 then
return 2
end
end
return base_block*damage
end
return base_block*damage
end
2023-10-30 23:42:11 +01:00
-- Reset health
2023-08-07 22:56:29 +02:00
function exchangeclone.check_armor_health(obj)
2023-10-30 23:42:11 +01:00
if obj:get_meta():get_int("exchangeclone_red_matter_armor") == 1 then
obj:set_hp(math.min(obj:get_hp(), 20))
obj:set_properties({hp_max = 20})
obj:get_meta():set_int("exchangeclone_red_matter_armor", 0)
2023-08-07 22:56:29 +02:00
end
end
minetest.register_on_joinplayer(function(ObjectRef, last_login)
exchangeclone.check_armor_health(ObjectRef)
end)
2023-09-29 01:23:49 +02:00
if exchangeclone.mcl then
2023-08-07 22:56:29 +02:00
mcl_armor.register_set({
name = "dark_matter",
description = "Dark Matter",
descriptions = exchangeclone.mcla and {
2023-11-19 01:48:35 +01:00
head = S("Dark Matter Helmet"),
torso = S("Dark Matter Chestplate"),
legs = S("Dark Matter Leggings"),
feet = S("Dark Matter Boots")
2023-09-29 01:23:49 +02:00
},
2023-08-07 22:56:29 +02:00
durability = -1,
enchantability = 0,
2024-02-04 22:29:48 +01:00
-- No armor points because I don't want MCL's armor code messing up the math.
2023-08-07 22:56:29 +02:00
points = {
2024-02-04 22:29:48 +01:00
head = 0,
torso = 0,
legs = 0,
feet = 0,
2023-08-07 22:56:29 +02:00
},
textures = {
head = get_armor_texture("helmet","dark"),
torso = get_armor_texture("chestplate","dark"),
legs = get_armor_texture("leggings","dark"),
feet = get_armor_texture("boots","dark"),
},
toughness = 4,
2023-10-07 17:26:21 +02:00
groups = {dark_matter_armor = 1, fire_immune = 1, exchangeclone_upgradable = 1},
2023-08-07 22:56:29 +02:00
craft_material = "exchangeclone:dark_matter",
cook_material = "mcl_core:diamondblock",
2024-02-04 22:29:48 +01:00
sound_equip = "mcl_armor_equip_diamond",
sound_unequip = "mcl_armor_unequip_diamond",
2023-08-07 22:56:29 +02:00
})
mcl_armor.register_set({
name = "red_matter",
description = "Red Matter",
descriptions = exchangeclone.mcla and {
2023-11-19 01:48:35 +01:00
head = S("Red Matter Helmet"),
torso = S("Red Matter Chestplate"),
legs = S("Red Matter Leggings"),
feet = S("Red Matter Boots")
2023-09-29 01:23:49 +02:00
},
2023-08-07 22:56:29 +02:00
durability = -1,
enchantability = 0,
2024-02-04 22:29:48 +01:00
-- No armor points because I don't want MCL's armor code messing up the math.
2023-08-07 22:56:29 +02:00
points = {
2024-02-04 22:29:48 +01:00
head = 0,
torso = 0,
legs = 0,
feet = 0,
2023-08-07 22:56:29 +02:00
},
textures = {
head = get_armor_texture("helmet","red"),
torso = get_armor_texture("chestplate","red"),
legs = get_armor_texture("leggings","red"),
feet = get_armor_texture("boots","red"),
},
toughness = 5,
2023-10-07 17:26:21 +02:00
groups = {red_matter_armor = 1, disable_repair = 1, fire_immune = 1, exchangeclone_upgradable = 1},
2023-08-07 22:56:29 +02:00
craft_material = "exchangeclone:red_matter",
cook_material = "exchangeclone:dark_matter",
2024-02-04 22:29:48 +01:00
sound_equip = "mcl_armor_equip_diamond",
sound_unequip = "mcl_armor_unequip_diamond",
2023-08-07 22:56:29 +02:00
})
for _, matter in pairs({"dark", "red"}) do
for _, type in pairs({"helmet", "chestplate", "leggings", "boots"}) do
2023-08-07 22:56:29 +02:00
minetest.override_item("exchangeclone:"..type.."_"..matter.."_matter", {
2024-02-13 01:25:25 +01:00
inventory_image = get_armor_texture("inv_"..matter.."_matter_"..type, matter),
2023-08-07 22:56:29 +02:00
wield_image = get_armor_texture("inv_"..type, matter),
})
end
end
2024-02-11 20:06:16 +01:00
-- Until Minetest fixes an issue, there's no way to make this work correctly.
2023-08-07 22:56:29 +02:00
mcl_damage.register_modifier(function(obj, damage, reason)
2024-02-04 22:29:48 +01:00
local start_time = minetest.get_us_time()
if not obj:is_player() then return end
2023-08-07 22:56:29 +02:00
local inv = mcl_util.get_inventory(obj)
2024-02-04 22:29:48 +01:00
local blocked = 0
2023-08-07 22:56:29 +02:00
if inv then
for name, element in pairs(mcl_armor.elements) do
local itemstack = inv:get_stack("armor", element.index)
2024-02-04 22:29:48 +01:00
local item_blocked = math.max(0,get_blocked_damage(itemstack, damage, reason))
blocked = blocked + item_blocked
minetest.log(dump({name, item_blocked}))
2023-08-07 22:56:29 +02:00
end
2024-02-04 22:29:48 +01:00
minetest.log(((minetest.get_us_time() - start_time)/1000).." milliseconds")
return math.max(0, damage - blocked)
2023-08-07 22:56:29 +02:00
end
2024-02-04 22:29:48 +01:00
end, -100)
2023-08-07 22:56:29 +02:00
else
armor:register_armor("exchangeclone:helmet_dark_matter", {
2023-11-19 01:48:35 +01:00
description = S("Dark Matter Helmet"),
2023-08-07 22:56:29 +02:00
texture = get_armor_texture("helmet","dark"),
inventory_image = get_armor_texture("inv_helmet","dark"),
preview = get_armor_texture("helmet","dark", true),
2024-02-04 22:29:48 +01:00
groups = {armor_head = 1, dark_matter_armor = 1, disable_repair = 1, exchangeclone_upgradable = 1}
2023-08-07 22:56:29 +02:00
})
armor:register_armor("exchangeclone:chestplate_dark_matter", {
2023-11-19 01:48:35 +01:00
description = S("Dark Matter Chestplate"),
2023-08-07 22:56:29 +02:00
texture = get_armor_texture("chestplate","dark"),
inventory_image = get_armor_texture("inv_chestplate","dark"),
preview = get_armor_texture("chestplate","dark", true),
2024-02-04 22:29:48 +01:00
groups = {armor_torso = 1, dark_matter_armor = 1, disable_repair = 1, exchangeclone_upgradable = 1}
2023-08-07 22:56:29 +02:00
})
armor:register_armor("exchangeclone:leggings_dark_matter", {
2023-11-19 01:48:35 +01:00
description = S("Dark Matter Leggings"),
2023-08-07 22:56:29 +02:00
texture = get_armor_texture("leggings","dark"),
inventory_image = get_armor_texture("inv_leggings","dark"),
preview = get_armor_texture("leggings","dark", true),
2024-02-04 22:29:48 +01:00
groups = {armor_legs = 1, dark_matter_armor = 1, disable_repair = 1, exchangeclone_upgradable = 1}
2023-08-07 22:56:29 +02:00
})
armor:register_armor("exchangeclone:boots_dark_matter", {
2023-11-19 01:48:35 +01:00
description = S("Dark Matter Boots"),
2023-08-07 22:56:29 +02:00
texture = get_armor_texture("boots","dark"),
inventory_image = get_armor_texture("inv_boots","dark"),
preview = get_armor_texture("boots","dark", true),
2024-02-04 22:29:48 +01:00
groups = {armor_feet = 1, dark_matter_armor = 1, disable_repair = 1, exchangeclone_upgradable = 1, armor_feather = 1}
2023-08-07 22:56:29 +02:00
})
armor:register_armor("exchangeclone:helmet_red_matter", {
2023-11-19 01:48:35 +01:00
description = S("Red Matter Helmet"),
2023-08-07 22:56:29 +02:00
texture = get_armor_texture("helmet","red"),
inventory_image = get_armor_texture("inv_helmet","red"),
preview = get_armor_texture("helmet","red", true),
2024-02-04 22:29:48 +01:00
groups = {armor_head = 1, red_matter_armor = 1, disable_repair = 1, exchangeclone_upgradable = 1}
2023-08-07 22:56:29 +02:00
})
armor:register_armor("exchangeclone:chestplate_red_matter", {
2023-11-19 01:48:35 +01:00
description = S("Red Matter Chestplate"),
2023-08-07 22:56:29 +02:00
texture = get_armor_texture("chestplate","red"),
inventory_image = get_armor_texture("inv_chestplate","red"),
preview = get_armor_texture("chestplate","red", true),
2024-02-04 22:29:48 +01:00
groups = {armor_torso = 1, red_matter_armor = 1, disable_repair = 1, exchangeclone_upgradable = 1}
2023-08-07 22:56:29 +02:00
})
armor:register_armor("exchangeclone:leggings_red_matter", {
2023-11-19 01:48:35 +01:00
description = S("Red Matter Leggings"),
2023-08-07 22:56:29 +02:00
texture = get_armor_texture("leggings","red"),
inventory_image = get_armor_texture("inv_leggings","red"),
2023-08-07 22:56:29 +02:00
preview = get_armor_texture("leggings","red", true),
2024-02-04 22:29:48 +01:00
groups = {armor_legs = 1, red_matter_armor = 1, disable_repair = 1, exchangeclone_upgradable = 1}
2023-08-07 22:56:29 +02:00
})
armor:register_armor("exchangeclone:boots_red_matter", {
2023-11-19 01:48:35 +01:00
description = S("Red Matter Boots"),
2023-08-07 22:56:29 +02:00
texture = get_armor_texture("boots","red"),
inventory_image = get_armor_texture("inv_boots","red"),
2023-08-07 22:56:29 +02:00
preview = get_armor_texture("boots","red", true),
2024-02-04 22:29:48 +01:00
groups = {armor_feet = 1, red_matter_armor = 1, disable_repair = 1, exchangeclone_upgradable = 1}
2023-08-07 22:56:29 +02:00
})
2024-02-04 22:29:48 +01:00
minetest.register_on_player_hpchange(function(player, hp_change, reason)
if hp_change < 0 then
local _, armor_inv = armor:get_valid_player(player, "3d_armor")
local blocked = 0
for i = 1, 6 do
local itemstack = armor_inv:get_stack("armor", i)
blocked = blocked + get_blocked_damage(itemstack, hp_change, reason)
end
return math.max(0, hp_change - blocked)
else
return hp_change
end
end, true)
2023-08-07 22:56:29 +02:00
end
2024-02-04 22:29:48 +01:00
2023-08-07 22:56:29 +02:00
local d = "exchangeclone:dark_matter"
local r = "exchangeclone:red_matter"
minetest.register_craft({
output = "exchangeclone:helmet_dark_matter",
recipe = {
{d,d,d},
{d,"",d}
}
})
minetest.register_craft({
output = "exchangeclone:chestplate_dark_matter",
recipe = {
{d,"",d},
{d,d,d},
{d,d,d},
}
})
minetest.register_craft({
output = "exchangeclone:leggings_dark_matter",
recipe = {
{d,d,d},
{d,"",d},
{d,"",d},
}
})
minetest.register_craft({
output = "exchangeclone:boots_dark_matter",
recipe = {
{d,"",d},
{d,"",d},
}
})
minetest.register_craft({
output = "exchangeclone:helmet_red_matter",
recipe = {
{r,r,r},
{r,"exchangeclone:helmet_dark_matter",r}
}
})
minetest.register_craft({
output = "exchangeclone:chestplate_red_matter",
recipe = {
{r,"exchangeclone:chestplate_dark_matter",r},
{r,r,r},
{r,r,r},
}
})
minetest.register_craft({
output = "exchangeclone:leggings_red_matter",
recipe = {
{r,r,r},
{r,"exchangeclone:leggings_dark_matter",r},
{r,"",r},
}
})
minetest.register_craft({
output = "exchangeclone:boots_red_matter",
recipe = {
{r,"exchangeclone:boots_dark_matter",r},
{r,"",r},
}
})
2024-02-13 16:45:40 +01:00
if exchangeclone.mtg then
2024-02-13 16:53:29 +01:00
minetest.register_tool("exchangeclone:shield_dark_matter", {
description = "Dark Matter Shield (deprecated)\nYou still have this so you can turn it into energy.\nAnd no, it's not supposed to have a texture.",
2024-02-13 16:45:40 +01:00
groups = {disable_repair = 1, not_in_creative_inventory = 1}
})
exchangeclone.register_craft({
2024-02-13 16:53:29 +01:00
output = "exchangeclone:shield_dark_matter",
2024-02-13 16:45:40 +01:00
type = "shapeless",
2023-08-07 22:56:29 +02:00
recipe = {
2024-02-13 16:45:40 +01:00
"exchangeclone:dark_matter",
"exchangeclone:dark_matter",
"exchangeclone:dark_matter",
"exchangeclone:dark_matter",
"exchangeclone:dark_matter",
"exchangeclone:dark_matter",
"exchangeclone:dark_matter",
2023-08-07 22:56:29 +02:00
}
})
2024-02-13 16:45:40 +01:00
2024-02-13 16:53:29 +01:00
minetest.register_tool("exchangeclone:shield_red_matter", {
description = "Red Matter Shield (deprecated)\nYou still have this so you can turn it into energy.\nAnd no, it's not supposed to have a texture.",
2024-02-13 16:45:40 +01:00
groups = {disable_repair = 1, not_in_creative_inventory = 1}
})
exchangeclone.register_craft({
2024-02-13 16:53:29 +01:00
output = "exchangeclone:shield_red_matter",
2024-02-13 16:45:40 +01:00
type = "shapeless",
2023-08-07 22:56:29 +02:00
recipe = {
2024-02-13 16:53:29 +01:00
"exchangeclone:shield_dark_matter",
2024-02-13 16:45:40 +01:00
"exchangeclone:red_matter",
"exchangeclone:red_matter",
"exchangeclone:red_matter",
"exchangeclone:red_matter",
"exchangeclone:red_matter",
"exchangeclone:red_matter",
2023-08-07 22:56:29 +02:00
}
})
end