2021-07-30 16:12:29 +02:00
|
|
|
|
|
|
|
--- 3D Armor API
|
|
|
|
--
|
|
|
|
-- @topic api
|
|
|
|
|
|
|
|
|
|
|
|
local transparent_armor = minetest.settings:get_bool("armor_transparent", false)
|
|
|
|
|
|
|
|
|
|
|
|
--- Tables
|
|
|
|
--
|
|
|
|
-- @section tables
|
|
|
|
|
|
|
|
--- Armor definition table used for registering armor.
|
|
|
|
--
|
|
|
|
-- @table ArmorDef
|
|
|
|
-- @tfield string description Human-readable name/description.
|
|
|
|
-- @tfield string inventory_image Image filename used for icon.
|
|
|
|
-- @tfield table groups See: `ArmorDef.groups`
|
|
|
|
-- @tfield table armor_groups See: `ArmorDef.armor_groups`
|
|
|
|
-- @tfield table damage_groups See: `ArmorDef.damage_groups`
|
|
|
|
-- @see ItemDef
|
|
|
|
-- @usage local def = {
|
|
|
|
-- description = "Wood Helmet",
|
|
|
|
-- inventory_image = "3d_armor_inv_helmet_wood.png",
|
|
|
|
-- groups = {armor_head=1, armor_heal=0, armor_use=2000, flammable=1},
|
|
|
|
-- armor_groups = {fleshy=5},
|
|
|
|
-- damage_groups = {cracky=3, snappy=2, choppy=3, crumbly=2, level=1},
|
|
|
|
-- }
|
|
|
|
|
|
|
|
--- Groups table.
|
|
|
|
--
|
|
|
|
-- General groups defining item behavior.
|
|
|
|
--
|
|
|
|
-- Some commonly used groups: ***armor\_<type>***, ***armor\_heal***, ***armor\_use***
|
|
|
|
--
|
|
|
|
-- @table ArmorDef.groups
|
|
|
|
-- @tfield int armor_type The armor type. "head", "torso", "hands", "shield", etc.
|
|
|
|
-- (**Note:** replace "type" with actual type).
|
|
|
|
-- @tfield int armor_heal Healing value of armor when equipped.
|
|
|
|
-- @tfield int armor_use Amount of uses/damage before armor "breaks".
|
|
|
|
-- @see groups
|
|
|
|
-- @usage groups = {
|
|
|
|
-- armor_head = 1,
|
|
|
|
-- armor_heal = 5,
|
|
|
|
-- armor_use = 2000,
|
|
|
|
-- flammable = 1,
|
|
|
|
-- }
|
|
|
|
|
|
|
|
--- Armor groups table.
|
|
|
|
--
|
|
|
|
-- Groups that this item is effective against when taking damage.
|
|
|
|
--
|
|
|
|
-- Some commonly used groups: ***fleshy***
|
|
|
|
--
|
|
|
|
-- @table ArmorDef.armor_groups
|
|
|
|
-- @usage armor_groups = {
|
|
|
|
-- fleshy = 5,
|
|
|
|
-- }
|
|
|
|
|
|
|
|
--- Damage groups table.
|
|
|
|
--
|
|
|
|
-- Groups that this item is effective on when used as a weapon/tool.
|
|
|
|
--
|
|
|
|
-- Some commonly used groups: ***cracky***, ***snappy***, ***choppy***, ***crumbly***, ***level***
|
|
|
|
--
|
|
|
|
-- @table ArmorDef.damage_groups
|
|
|
|
-- @see entity_damage_mechanism
|
|
|
|
-- @usage damage_groups = {
|
|
|
|
-- cracky = 3,
|
|
|
|
-- snappy = 2,
|
|
|
|
-- choppy = 3,
|
|
|
|
-- crumbly = 2,
|
|
|
|
-- level = 1,
|
|
|
|
-- }
|
|
|
|
|
|
|
|
--- @section end
|
|
|
|
|
|
|
|
|
2017-08-06 18:43:41 +02:00
|
|
|
-- support for i18n
|
2020-08-31 06:00:18 +02:00
|
|
|
local S = minetest.get_translator(minetest.get_current_modname())
|
2017-08-06 18:43:41 +02:00
|
|
|
|
2017-03-17 21:05:55 +01:00
|
|
|
local skin_previews = {}
|
|
|
|
local use_player_monoids = minetest.global_exists("player_monoids")
|
|
|
|
local use_armor_monoid = minetest.global_exists("armor_monoid")
|
2018-05-07 15:41:40 +02:00
|
|
|
local use_pova_mod = minetest.get_modpath("pova")
|
2017-04-13 21:11:15 +02:00
|
|
|
local armor_def = setmetatable({}, {
|
|
|
|
__index = function()
|
|
|
|
return setmetatable({
|
|
|
|
groups = setmetatable({}, {
|
|
|
|
__index = function()
|
|
|
|
return 0
|
|
|
|
end})
|
|
|
|
}, {
|
|
|
|
__index = function()
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
})
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
local armor_textures = setmetatable({}, {
|
|
|
|
__index = function()
|
|
|
|
return setmetatable({}, {
|
|
|
|
__index = function()
|
|
|
|
return "blank.png"
|
|
|
|
end
|
|
|
|
})
|
|
|
|
end
|
|
|
|
})
|
2017-03-17 21:05:55 +01:00
|
|
|
|
|
|
|
armor = {
|
|
|
|
timer = 0,
|
|
|
|
elements = {"head", "torso", "legs", "feet"},
|
2017-03-18 21:12:43 +01:00
|
|
|
physics = {"jump", "speed", "gravity"},
|
2022-04-02 17:16:23 +02:00
|
|
|
attributes = {"heal", "fire", "water", "feather"},
|
2017-03-19 19:39:58 +01:00
|
|
|
formspec = "image[2.5,0;2,4;armor_preview]"..
|
2017-03-17 21:05:55 +01:00
|
|
|
default.gui_bg..
|
|
|
|
default.gui_bg_img..
|
|
|
|
default.gui_slots..
|
2017-03-19 19:39:58 +01:00
|
|
|
default.get_hotbar_bg(0, 4.7)..
|
|
|
|
"list[current_player;main;0,4.7;8,1;]"..
|
|
|
|
"list[current_player;main;0,5.85;8,3;8]",
|
2017-04-13 21:11:15 +02:00
|
|
|
def = armor_def,
|
|
|
|
textures = armor_textures,
|
2017-03-17 21:05:55 +01:00
|
|
|
default_skin = "character",
|
2017-03-18 18:23:50 +01:00
|
|
|
materials = {
|
|
|
|
wood = "group:wood",
|
|
|
|
cactus = "default:cactus",
|
|
|
|
steel = "default:steel_ingot",
|
|
|
|
bronze = "default:bronze_ingot",
|
|
|
|
diamond = "default:diamond",
|
|
|
|
gold = "default:gold_ingot",
|
|
|
|
mithril = "moreores:mithril_ingot",
|
|
|
|
crystal = "ethereal:crystal_ingot",
|
2021-11-10 10:15:55 +01:00
|
|
|
nether = "nether:nether_ingot",
|
2017-03-18 18:23:50 +01:00
|
|
|
},
|
|
|
|
fire_nodes = {
|
2021-06-14 11:56:05 +02:00
|
|
|
{"nether:lava_source", 5, 8},
|
2017-03-18 18:23:50 +01:00
|
|
|
{"default:lava_source", 5, 8},
|
|
|
|
{"default:lava_flowing", 5, 8},
|
|
|
|
{"fire:basic_flame", 3, 4},
|
|
|
|
{"fire:permanent_flame", 3, 4},
|
|
|
|
{"ethereal:crystal_spike", 2, 1},
|
|
|
|
{"ethereal:fire_flower", 2, 1},
|
2021-06-14 11:56:05 +02:00
|
|
|
{"nether:lava_crust", 2, 1},
|
2017-03-18 18:23:50 +01:00
|
|
|
{"default:torch", 1, 1},
|
|
|
|
{"default:torch_ceiling", 1, 1},
|
|
|
|
{"default:torch_wall", 1, 1},
|
|
|
|
},
|
2017-03-23 01:52:32 +01:00
|
|
|
registered_groups = {["fleshy"]=100},
|
2017-03-18 21:12:43 +01:00
|
|
|
registered_callbacks = {
|
|
|
|
on_update = {},
|
|
|
|
on_equip = {},
|
|
|
|
on_unequip = {},
|
2017-04-05 18:46:09 +02:00
|
|
|
on_damage = {},
|
2017-03-18 21:12:43 +01:00
|
|
|
on_destroy = {},
|
|
|
|
},
|
2018-02-11 13:33:46 +01:00
|
|
|
migrate_old_inventory = true,
|
2020-08-31 06:00:18 +02:00
|
|
|
version = "0.4.13",
|
|
|
|
get_translator = S
|
2017-03-17 21:05:55 +01:00
|
|
|
}
|
|
|
|
|
2017-03-18 18:23:50 +01:00
|
|
|
armor.config = {
|
2017-04-11 20:31:25 +02:00
|
|
|
init_delay = 2,
|
2017-03-18 18:23:50 +01:00
|
|
|
bones_delay = 1,
|
|
|
|
update_time = 1,
|
|
|
|
drop = minetest.get_modpath("bones") ~= nil,
|
|
|
|
destroy = false,
|
|
|
|
level_multiplier = 1,
|
|
|
|
heal_multiplier = 1,
|
|
|
|
material_wood = true,
|
|
|
|
material_cactus = true,
|
|
|
|
material_steel = true,
|
|
|
|
material_bronze = true,
|
|
|
|
material_diamond = true,
|
|
|
|
material_gold = true,
|
|
|
|
material_mithril = true,
|
|
|
|
material_crystal = true,
|
2021-11-10 10:15:55 +01:00
|
|
|
material_nether = true,
|
2021-01-11 15:34:28 +01:00
|
|
|
set_elements = "head torso legs feet shield",
|
|
|
|
set_multiplier = 1.1,
|
2017-03-31 22:18:29 +02:00
|
|
|
water_protect = true,
|
2017-04-05 18:46:09 +02:00
|
|
|
fire_protect = minetest.get_modpath("ethereal") ~= nil,
|
2020-12-13 13:07:39 +01:00
|
|
|
fire_protect_torch = minetest.get_modpath("ethereal") ~= nil,
|
2022-04-02 17:16:23 +02:00
|
|
|
feather_fall = true,
|
2017-04-05 18:46:09 +02:00
|
|
|
punch_damage = true,
|
2017-03-18 18:23:50 +01:00
|
|
|
}
|
|
|
|
|
2017-03-23 01:52:32 +01:00
|
|
|
|
2021-07-30 16:12:29 +02:00
|
|
|
--- Methods
|
|
|
|
--
|
|
|
|
-- @section methods
|
|
|
|
|
|
|
|
--- Registers a new armor item.
|
|
|
|
--
|
|
|
|
-- @function armor:register_armor
|
|
|
|
-- @tparam string name Armor item technical name (ex: "3d\_armor:helmet\_gold").
|
|
|
|
-- @tparam ArmorDef def Armor definition table.
|
|
|
|
-- @usage armor:register_armor("3d_armor:helmet_wood", {
|
|
|
|
-- description = "Wood Helmet",
|
|
|
|
-- inventory_image = "3d_armor_inv_helmet_wood.png",
|
|
|
|
-- groups = {armor_head=1, armor_heal=0, armor_use=2000, flammable=1},
|
|
|
|
-- armor_groups = {fleshy=5},
|
|
|
|
-- damage_groups = {cracky=3, snappy=2, choppy=3, crumbly=2, level=1},
|
|
|
|
-- })
|
2017-03-23 01:52:32 +01:00
|
|
|
armor.register_armor = function(self, name, def)
|
2020-09-24 14:08:17 +02:00
|
|
|
def.on_secondary_use = function(itemstack, player)
|
|
|
|
return armor:equip(player, itemstack)
|
|
|
|
end
|
2020-10-01 20:09:31 +02:00
|
|
|
def.on_place = function(itemstack, player, pointed_thing)
|
|
|
|
if pointed_thing.type == "node" and player and not player:get_player_control().sneak then
|
|
|
|
local node = minetest.get_node(pointed_thing.under)
|
|
|
|
local ndef = minetest.registered_nodes[node.name]
|
|
|
|
if ndef and ndef.on_rightclick then
|
|
|
|
return ndef.on_rightclick(pointed_thing.under, node, player, itemstack, pointed_thing)
|
|
|
|
end
|
|
|
|
end
|
2020-09-24 14:08:17 +02:00
|
|
|
return armor:equip(player, itemstack)
|
2020-09-22 07:45:39 +02:00
|
|
|
end
|
2021-01-11 15:34:28 +01:00
|
|
|
-- The below is a very basic check to try and see if a material name exists as part
|
|
|
|
-- of the item name. However this check is very simple and just checks theres "_something"
|
|
|
|
-- at the end of the item name and logging an error to debug if not.
|
|
|
|
local check_mat_exists = string.match(name, "%:.+_(.+)$")
|
|
|
|
if check_mat_exists == nil then
|
|
|
|
minetest.log("warning:[3d_armor] Registered armor "..name..
|
|
|
|
" does not have \"_material\" specified at the end of the item registration name")
|
|
|
|
end
|
2020-09-24 14:08:17 +02:00
|
|
|
minetest.register_tool(name, def)
|
2017-03-23 01:52:32 +01:00
|
|
|
end
|
|
|
|
|
2021-07-30 16:12:29 +02:00
|
|
|
--- Registers a new armor group.
|
|
|
|
--
|
|
|
|
-- @function armor:register_armor_group
|
|
|
|
-- @tparam string group Group ID.
|
|
|
|
-- @tparam int base Base armor value.
|
2017-03-23 01:52:32 +01:00
|
|
|
armor.register_armor_group = function(self, group, base)
|
|
|
|
base = base or 100
|
|
|
|
self.registered_groups[group] = base
|
|
|
|
if use_armor_monoid then
|
|
|
|
armor_monoid.register_armor_group(group, base)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-30 16:12:29 +02:00
|
|
|
--- Armor Callbacks Registration
|
|
|
|
--
|
|
|
|
-- @section callbacks
|
2017-03-18 21:12:43 +01:00
|
|
|
|
2021-07-30 16:12:29 +02:00
|
|
|
--- Registers a callback for when player visuals are update.
|
|
|
|
--
|
|
|
|
-- @function armor:register_on_update
|
|
|
|
-- @tparam function func Function to be executed.
|
|
|
|
-- @see armor:update_player_visuals
|
|
|
|
-- @usage armor:register_on_update(function(player, index, stack)
|
|
|
|
-- -- code to execute
|
|
|
|
-- end)
|
2017-03-18 21:12:43 +01:00
|
|
|
armor.register_on_update = function(self, func)
|
|
|
|
if type(func) == "function" then
|
|
|
|
table.insert(self.registered_callbacks.on_update, func)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-30 16:12:29 +02:00
|
|
|
--- Registers a callback for when armor is equipped.
|
|
|
|
--
|
|
|
|
-- @function armor:register_on_equip
|
|
|
|
-- @tparam function func Function to be executed.
|
|
|
|
-- @usage armor:register_on_equip(function(player, index, stack)
|
|
|
|
-- -- code to execute
|
|
|
|
-- end)
|
2017-03-18 21:12:43 +01:00
|
|
|
armor.register_on_equip = function(self, func)
|
|
|
|
if type(func) == "function" then
|
|
|
|
table.insert(self.registered_callbacks.on_equip, func)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-30 16:12:29 +02:00
|
|
|
--- Registers a callback for when armor is unequipped.
|
|
|
|
--
|
|
|
|
-- @function armor:register_on_unequip
|
|
|
|
-- @tparam function func Function to be executed.
|
|
|
|
-- @usage armor:register_on_unequip(function(player, index, stack)
|
|
|
|
-- -- code to execute
|
|
|
|
-- end)
|
2017-03-18 21:12:43 +01:00
|
|
|
armor.register_on_unequip = function(self, func)
|
|
|
|
if type(func) == "function" then
|
|
|
|
table.insert(self.registered_callbacks.on_unequip, func)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-30 16:12:29 +02:00
|
|
|
--- Registers a callback for when armor is damaged.
|
|
|
|
--
|
|
|
|
-- @function armor:register_on_damage
|
|
|
|
-- @tparam function func Function to be executed.
|
|
|
|
-- @see armor:damage
|
|
|
|
-- @usage armor:register_on_damage(function(player, index, stack)
|
|
|
|
-- -- code to execute
|
|
|
|
-- end)
|
2017-04-05 18:46:09 +02:00
|
|
|
armor.register_on_damage = function(self, func)
|
|
|
|
if type(func) == "function" then
|
|
|
|
table.insert(self.registered_callbacks.on_damage, func)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-30 16:12:29 +02:00
|
|
|
--- Registers a callback for when armor is destroyed.
|
|
|
|
--
|
|
|
|
-- @function armor:register_on_destroy
|
|
|
|
-- @tparam function func Function to be executed.
|
|
|
|
-- @see armor:damage
|
|
|
|
-- @usage armor:register_on_destroy(function(player, index, stack)
|
|
|
|
-- -- code to execute
|
|
|
|
-- end)
|
2017-03-18 21:12:43 +01:00
|
|
|
armor.register_on_destroy = function(self, func)
|
|
|
|
if type(func) == "function" then
|
|
|
|
table.insert(self.registered_callbacks.on_destroy, func)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-30 16:12:29 +02:00
|
|
|
--- @section end
|
|
|
|
|
|
|
|
|
|
|
|
--- Methods
|
|
|
|
--
|
|
|
|
-- @section methods
|
|
|
|
|
|
|
|
--- Runs callbacks.
|
|
|
|
--
|
|
|
|
-- @function armor:run_callbacks
|
|
|
|
-- @tparam function callback Function to execute.
|
|
|
|
-- @tparam ObjectRef player First parameter passed to callback.
|
|
|
|
-- @tparam int index Second parameter passed to callback.
|
|
|
|
-- @tparam ItemStack stack Callback owner.
|
2017-04-15 21:37:32 +02:00
|
|
|
armor.run_callbacks = function(self, callback, player, index, stack)
|
2017-03-18 21:12:43 +01:00
|
|
|
if stack then
|
|
|
|
local def = stack:get_definition() or {}
|
|
|
|
if type(def[callback]) == "function" then
|
2017-04-15 21:37:32 +02:00
|
|
|
def[callback](player, index, stack)
|
2017-03-18 21:12:43 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
local callbacks = self.registered_callbacks[callback]
|
|
|
|
if callbacks then
|
|
|
|
for _, func in pairs(callbacks) do
|
2017-04-15 21:37:32 +02:00
|
|
|
func(player, index, stack)
|
2017-03-18 21:12:43 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-30 16:12:29 +02:00
|
|
|
--- Updates visuals.
|
|
|
|
--
|
|
|
|
-- @function armor:update_player_visuals
|
|
|
|
-- @tparam ObjectRef player
|
2017-03-17 21:05:55 +01:00
|
|
|
armor.update_player_visuals = function(self, player)
|
|
|
|
if not player then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local name = player:get_player_name()
|
|
|
|
if self.textures[name] then
|
2023-12-26 18:34:43 +01:00
|
|
|
player_api.set_textures(player, {
|
2017-03-17 21:05:55 +01:00
|
|
|
self.textures[name].skin,
|
|
|
|
self.textures[name].armor,
|
|
|
|
self.textures[name].wielditem,
|
|
|
|
})
|
|
|
|
end
|
2017-08-01 19:48:06 +02:00
|
|
|
self:run_callbacks("on_update", player)
|
2017-03-17 21:05:55 +01:00
|
|
|
end
|
|
|
|
|
2021-07-30 16:12:29 +02:00
|
|
|
--- Sets player's armor attributes.
|
|
|
|
--
|
|
|
|
-- @function armor:set_player_armor
|
|
|
|
-- @tparam ObjectRef player
|
2017-03-17 21:05:55 +01:00
|
|
|
armor.set_player_armor = function(self, player)
|
2018-02-11 13:33:46 +01:00
|
|
|
local name, armor_inv = self:get_valid_player(player, "[set_player_armor]")
|
2017-03-17 21:05:55 +01:00
|
|
|
if not name then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local state = 0
|
2017-03-23 01:52:32 +01:00
|
|
|
local count = 0
|
2017-03-17 21:05:55 +01:00
|
|
|
local preview = armor:get_preview(name)
|
2024-04-04 07:25:05 +02:00
|
|
|
local texture = "blank.png"
|
2017-03-23 01:52:32 +01:00
|
|
|
local physics = {}
|
|
|
|
local attributes = {}
|
|
|
|
local levels = {}
|
|
|
|
local groups = {}
|
|
|
|
local change = {}
|
2021-01-11 15:34:28 +01:00
|
|
|
local set_worn = {}
|
|
|
|
local armor_multi = 0
|
|
|
|
local worn_armor = armor:get_weared_armor_elements(player)
|
2017-03-23 01:52:32 +01:00
|
|
|
for _, phys in pairs(self.physics) do
|
2017-03-29 18:44:40 +02:00
|
|
|
physics[phys] = 1
|
2017-03-23 01:52:32 +01:00
|
|
|
end
|
|
|
|
for _, attr in pairs(self.attributes) do
|
|
|
|
attributes[attr] = 0
|
|
|
|
end
|
2017-03-29 18:44:40 +02:00
|
|
|
for group, _ in pairs(self.registered_groups) do
|
2017-03-23 01:52:32 +01:00
|
|
|
change[group] = 1
|
|
|
|
levels[group] = 0
|
2017-03-17 21:05:55 +01:00
|
|
|
end
|
2018-02-11 13:33:46 +01:00
|
|
|
local list = armor_inv:get_list("armor")
|
2017-10-20 21:17:37 +02:00
|
|
|
if type(list) ~= "table" then
|
|
|
|
return
|
|
|
|
end
|
2017-03-23 01:52:32 +01:00
|
|
|
for i, stack in pairs(list) do
|
2017-03-17 21:05:55 +01:00
|
|
|
if stack:get_count() == 1 then
|
|
|
|
local def = stack:get_definition()
|
2017-03-23 01:52:32 +01:00
|
|
|
for _, element in pairs(self.elements) do
|
|
|
|
if def.groups["armor_"..element] then
|
|
|
|
if def.armor_groups then
|
|
|
|
for group, level in pairs(def.armor_groups) do
|
|
|
|
if levels[group] then
|
|
|
|
levels[group] = levels[group] + level
|
2017-03-17 21:05:55 +01:00
|
|
|
end
|
|
|
|
end
|
2017-03-23 01:52:32 +01:00
|
|
|
else
|
|
|
|
local level = def.groups["armor_"..element]
|
|
|
|
levels["fleshy"] = levels["fleshy"] + level
|
2017-03-17 21:05:55 +01:00
|
|
|
end
|
2018-01-10 20:16:23 +01:00
|
|
|
break
|
2017-03-17 21:05:55 +01:00
|
|
|
end
|
2017-03-23 01:52:32 +01:00
|
|
|
-- DEPRECATED, use armor_groups instead
|
|
|
|
if def.groups["armor_radiation"] and levels["radiation"] then
|
2024-03-22 22:35:43 +01:00
|
|
|
levels["radiation"] = levels["radiation"] + def.groups["armor_radiation"]
|
2017-03-23 01:52:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
local item = stack:get_name()
|
|
|
|
local tex = def.texture or item:gsub("%:", "_")
|
|
|
|
tex = tex:gsub(".png$", "")
|
2017-03-26 23:46:51 +02:00
|
|
|
local prev = def.preview or tex.."_preview"
|
2017-03-23 01:52:32 +01:00
|
|
|
prev = prev:gsub(".png$", "")
|
2021-05-27 09:11:04 +02:00
|
|
|
if not transparent_armor then
|
|
|
|
texture = texture.."^"..tex..".png"
|
|
|
|
end
|
2017-03-23 01:52:32 +01:00
|
|
|
preview = preview.."^"..prev..".png"
|
|
|
|
state = state + stack:get_wear()
|
|
|
|
count = count + 1
|
|
|
|
for _, phys in pairs(self.physics) do
|
|
|
|
local value = def.groups["physics_"..phys] or 0
|
|
|
|
physics[phys] = physics[phys] + value
|
|
|
|
end
|
|
|
|
for _, attr in pairs(self.attributes) do
|
|
|
|
local value = def.groups["armor_"..attr] or 0
|
|
|
|
attributes[attr] = attributes[attr] + value
|
|
|
|
end
|
2021-01-11 15:34:28 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
-- The following code compares player worn armor items against requirements
|
|
|
|
-- of which armor pieces are needed to be worn to meet set bonus requirements
|
|
|
|
for loc,item in pairs(worn_armor) do
|
|
|
|
local item_mat = string.match(item, "%:.+_(.+)$")
|
|
|
|
local worn_key = item_mat or "unknown"
|
|
|
|
|
|
|
|
-- Perform location checks to ensure the armor is worn correctly
|
|
|
|
for k,set_loc in pairs(armor.config.set_elements)do
|
|
|
|
if set_loc == loc then
|
|
|
|
if set_worn[worn_key] == nil then
|
|
|
|
set_worn[worn_key] = 0
|
|
|
|
set_worn[worn_key] = set_worn[worn_key] + 1
|
|
|
|
else
|
|
|
|
set_worn[worn_key] = set_worn[worn_key] + 1
|
2017-03-23 01:52:32 +01:00
|
|
|
end
|
2017-03-17 21:05:55 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-01-11 15:34:28 +01:00
|
|
|
|
|
|
|
-- Apply the armor multiplier only if the player is wearing a full set of armor
|
|
|
|
for mat_name,arm_piece_num in pairs(set_worn) do
|
|
|
|
if arm_piece_num == #armor.config.set_elements then
|
|
|
|
armor_multi = armor.config.set_multiplier
|
|
|
|
end
|
|
|
|
end
|
2017-03-23 01:52:32 +01:00
|
|
|
for group, level in pairs(levels) do
|
|
|
|
if level > 0 then
|
2017-03-29 20:13:13 +02:00
|
|
|
level = level * armor.config.level_multiplier
|
2021-01-11 15:34:28 +01:00
|
|
|
if armor_multi ~= 0 then
|
|
|
|
level = level * armor.config.set_multiplier
|
2017-03-23 01:52:32 +01:00
|
|
|
end
|
|
|
|
end
|
2017-03-29 18:44:40 +02:00
|
|
|
local base = self.registered_groups[group]
|
|
|
|
self.def[name].groups[group] = level
|
|
|
|
if level > base then
|
|
|
|
level = base
|
|
|
|
end
|
|
|
|
groups[group] = base - level
|
|
|
|
change[group] = groups[group] / base
|
2017-03-17 21:05:55 +01:00
|
|
|
end
|
2017-03-23 01:52:32 +01:00
|
|
|
for _, attr in pairs(self.attributes) do
|
2018-05-13 17:52:29 +02:00
|
|
|
local mult = attr == "heal" and self.config.heal_multiplier or 1
|
|
|
|
self.def[name][attr] = attributes[attr] * mult
|
2017-03-17 21:05:55 +01:00
|
|
|
end
|
2017-03-23 01:52:32 +01:00
|
|
|
for _, phys in pairs(self.physics) do
|
|
|
|
self.def[name][phys] = physics[phys]
|
2017-03-17 21:05:55 +01:00
|
|
|
end
|
|
|
|
if use_armor_monoid then
|
2017-03-23 01:52:32 +01:00
|
|
|
armor_monoid.monoid:add_change(player, change, "3d_armor:armor")
|
2017-03-17 21:05:55 +01:00
|
|
|
else
|
2019-10-07 20:12:58 +02:00
|
|
|
-- Preserve immortal group (damage disabled for player)
|
2022-09-07 22:01:18 +02:00
|
|
|
local player_groups = player:get_armor_groups()
|
|
|
|
local immortal = player_groups.immortal
|
2019-10-07 20:12:58 +02:00
|
|
|
if immortal and immortal ~= 0 then
|
|
|
|
groups.immortal = 1
|
|
|
|
end
|
2022-09-07 22:01:18 +02:00
|
|
|
-- Preserve fall_damage_add_percent group (fall damage modifier)
|
|
|
|
groups.fall_damage_add_percent = player_groups.fall_damage_add_percent
|
2017-03-23 01:52:32 +01:00
|
|
|
player:set_armor_groups(groups)
|
2017-03-17 21:05:55 +01:00
|
|
|
end
|
|
|
|
if use_player_monoids then
|
2017-03-23 01:52:32 +01:00
|
|
|
player_monoids.speed:add_change(player, physics.speed,
|
2017-03-17 21:05:55 +01:00
|
|
|
"3d_armor:physics")
|
2017-03-23 01:52:32 +01:00
|
|
|
player_monoids.jump:add_change(player, physics.jump,
|
2017-03-17 21:05:55 +01:00
|
|
|
"3d_armor:physics")
|
2017-03-23 01:52:32 +01:00
|
|
|
player_monoids.gravity:add_change(player, physics.gravity,
|
2017-03-17 21:05:55 +01:00
|
|
|
"3d_armor:physics")
|
2018-05-07 15:41:40 +02:00
|
|
|
elseif use_pova_mod then
|
|
|
|
-- only add the changes, not the default 1.0 for each physics setting
|
|
|
|
pova.add_override(name, "3d_armor", {
|
|
|
|
speed = physics.speed - 1,
|
|
|
|
jump = physics.jump - 1,
|
|
|
|
gravity = physics.gravity - 1,
|
|
|
|
})
|
|
|
|
pova.do_override(player)
|
2017-03-17 21:05:55 +01:00
|
|
|
else
|
2020-10-12 07:47:57 +02:00
|
|
|
local player_physics_locked = player:get_meta():get_int("player_physics_locked")
|
|
|
|
if player_physics_locked == nil or player_physics_locked == 0 then
|
|
|
|
player:set_physics_override(physics)
|
|
|
|
end
|
2017-03-17 21:05:55 +01:00
|
|
|
end
|
2017-03-23 01:52:32 +01:00
|
|
|
self.textures[name].armor = texture
|
2017-03-17 21:05:55 +01:00
|
|
|
self.textures[name].preview = preview
|
2017-03-23 01:52:32 +01:00
|
|
|
self.def[name].level = self.def[name].groups.fleshy or 0
|
2017-03-17 21:05:55 +01:00
|
|
|
self.def[name].state = state
|
2017-03-23 01:52:32 +01:00
|
|
|
self.def[name].count = count
|
2017-03-17 21:05:55 +01:00
|
|
|
self:update_player_visuals(player)
|
|
|
|
end
|
|
|
|
|
2021-07-30 16:12:29 +02:00
|
|
|
--- Action when armor is punched.
|
|
|
|
--
|
|
|
|
-- @function armor:punch
|
|
|
|
-- @tparam ObjectRef player Player wearing the armor.
|
|
|
|
-- @tparam ObjectRef hitter Entity attacking player.
|
|
|
|
-- @tparam[opt] int time_from_last_punch Time in seconds since last punch action.
|
|
|
|
-- @tparam[opt] table tool_capabilities See `entity_damage_mechanism`.
|
2017-04-05 18:46:09 +02:00
|
|
|
armor.punch = function(self, player, hitter, time_from_last_punch, tool_capabilities)
|
2018-02-11 13:33:46 +01:00
|
|
|
local name, armor_inv = self:get_valid_player(player, "[punch]")
|
2017-04-05 18:46:09 +02:00
|
|
|
if not name then
|
|
|
|
return
|
|
|
|
end
|
2020-04-29 09:24:46 +02:00
|
|
|
local set_state
|
|
|
|
local set_count
|
2017-04-05 18:46:09 +02:00
|
|
|
local state = 0
|
|
|
|
local count = 0
|
|
|
|
local recip = true
|
|
|
|
local default_groups = {cracky=3, snappy=3, choppy=3, crumbly=3, level=1}
|
2018-02-11 13:33:46 +01:00
|
|
|
local list = armor_inv:get_list("armor")
|
2017-04-05 18:46:09 +02:00
|
|
|
for i, stack in pairs(list) do
|
|
|
|
if stack:get_count() == 1 then
|
2020-02-13 12:26:32 +01:00
|
|
|
local itemname = stack:get_name()
|
|
|
|
local use = minetest.get_item_group(itemname, "armor_use") or 0
|
2017-04-05 18:46:09 +02:00
|
|
|
local damage = use > 0
|
|
|
|
local def = stack:get_definition() or {}
|
2017-04-21 19:33:30 +02:00
|
|
|
if type(def.on_punched) == "function" then
|
|
|
|
damage = def.on_punched(player, hitter, time_from_last_punch,
|
2017-04-09 18:34:22 +02:00
|
|
|
tool_capabilities) ~= false and damage == true
|
2017-04-05 18:46:09 +02:00
|
|
|
end
|
|
|
|
if damage == true and tool_capabilities then
|
|
|
|
local damage_groups = def.damage_groups or default_groups
|
|
|
|
local level = damage_groups.level or 0
|
|
|
|
local groupcaps = tool_capabilities.groupcaps or {}
|
|
|
|
local uses = 0
|
|
|
|
damage = false
|
2020-12-11 10:23:05 +01:00
|
|
|
if next(groupcaps) == nil then
|
|
|
|
damage = true
|
|
|
|
end
|
2017-04-05 18:46:09 +02:00
|
|
|
for group, caps in pairs(groupcaps) do
|
|
|
|
local maxlevel = caps.maxlevel or 0
|
|
|
|
local diff = maxlevel - level
|
|
|
|
if diff == 0 then
|
|
|
|
diff = 1
|
|
|
|
end
|
|
|
|
if diff > 0 and caps.times then
|
|
|
|
local group_level = damage_groups[group]
|
|
|
|
if group_level then
|
|
|
|
local time = caps.times[group_level]
|
|
|
|
if time then
|
|
|
|
local dt = time_from_last_punch or 0
|
|
|
|
if dt > time / diff then
|
|
|
|
if caps.uses then
|
|
|
|
uses = caps.uses * math.pow(3, diff)
|
|
|
|
end
|
|
|
|
damage = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if damage == true and recip == true and hitter and
|
|
|
|
def.reciprocate_damage == true and uses > 0 then
|
|
|
|
local item = hitter:get_wielded_item()
|
2017-04-09 18:58:19 +02:00
|
|
|
if item and item:get_name() ~= "" then
|
|
|
|
item:add_wear(65535 / uses)
|
|
|
|
hitter:set_wielded_item(item)
|
|
|
|
end
|
2017-04-05 18:46:09 +02:00
|
|
|
-- reciprocate tool damage only once
|
|
|
|
recip = false
|
|
|
|
end
|
|
|
|
end
|
2017-04-09 18:34:22 +02:00
|
|
|
if damage == true and hitter == "fire" then
|
2020-02-13 12:26:32 +01:00
|
|
|
damage = minetest.get_item_group(itemname, "flammable") > 0
|
2017-04-09 18:34:22 +02:00
|
|
|
end
|
2017-04-05 18:46:09 +02:00
|
|
|
if damage == true then
|
2017-04-19 19:31:47 +02:00
|
|
|
self:damage(player, i, stack, use)
|
2020-04-29 09:24:46 +02:00
|
|
|
set_state = self.def[name].state
|
|
|
|
set_count = self.def[name].count
|
2017-04-05 18:46:09 +02:00
|
|
|
end
|
|
|
|
state = state + stack:get_wear()
|
|
|
|
count = count + 1
|
|
|
|
end
|
|
|
|
end
|
2020-04-29 09:24:46 +02:00
|
|
|
if set_count and set_count ~= count then
|
|
|
|
state = set_state or state
|
|
|
|
count = set_count or count
|
|
|
|
end
|
2017-04-05 18:46:09 +02:00
|
|
|
self.def[name].state = state
|
2017-04-10 00:20:04 +02:00
|
|
|
self.def[name].count = count
|
2017-04-05 18:46:09 +02:00
|
|
|
end
|
|
|
|
|
2021-07-30 16:12:29 +02:00
|
|
|
--- Action when armor is damaged.
|
|
|
|
--
|
|
|
|
-- @function armor:damage
|
|
|
|
-- @tparam ObjectRef player
|
|
|
|
-- @tparam int index Inventory index where armor is equipped.
|
|
|
|
-- @tparam ItemStack stack Armor item receiving damaged.
|
|
|
|
-- @tparam int use Amount of wear to add to armor item.
|
2017-04-19 19:31:47 +02:00
|
|
|
armor.damage = function(self, player, index, stack, use)
|
|
|
|
local old_stack = ItemStack(stack)
|
2020-12-24 03:11:06 +01:00
|
|
|
local worn_armor = armor:get_weared_armor_elements(player)
|
2023-08-04 16:35:02 +02:00
|
|
|
if not worn_armor then
|
|
|
|
return
|
|
|
|
end
|
2020-12-24 03:11:06 +01:00
|
|
|
local armor_worn_cnt = 0
|
|
|
|
for k,v in pairs(worn_armor) do
|
|
|
|
armor_worn_cnt = armor_worn_cnt + 1
|
|
|
|
end
|
|
|
|
use = math.ceil(use/armor_worn_cnt)
|
2017-04-19 19:31:47 +02:00
|
|
|
stack:add_wear(use)
|
2017-05-03 18:56:32 +02:00
|
|
|
self:run_callbacks("on_damage", player, index, stack)
|
2017-04-19 19:31:47 +02:00
|
|
|
self:set_inventory_stack(player, index, stack)
|
|
|
|
if stack:get_count() == 0 then
|
2018-06-02 19:59:32 +02:00
|
|
|
self:run_callbacks("on_unequip", player, index, old_stack)
|
2017-04-19 19:31:47 +02:00
|
|
|
self:run_callbacks("on_destroy", player, index, old_stack)
|
|
|
|
self:set_player_armor(player)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-30 16:12:29 +02:00
|
|
|
--- Get elements of equipped armor.
|
|
|
|
--
|
|
|
|
-- @function armor:get_weared_armor_elements
|
|
|
|
-- @tparam ObjectRef player
|
|
|
|
-- @treturn table List of equipped armors.
|
2020-09-22 07:45:39 +02:00
|
|
|
armor.get_weared_armor_elements = function(self, player)
|
|
|
|
local name, inv = self:get_valid_player(player, "[get_weared_armor]")
|
|
|
|
local weared_armor = {}
|
|
|
|
if not name then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
for i=1, inv:get_size("armor") do
|
|
|
|
local item_name = inv:get_stack("armor", i):get_name()
|
|
|
|
local element = self:get_element(item_name)
|
|
|
|
if element ~= nil then
|
|
|
|
weared_armor[element] = item_name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return weared_armor
|
|
|
|
end
|
|
|
|
|
2021-07-30 16:12:29 +02:00
|
|
|
--- Equips a piece of armor to a player.
|
|
|
|
--
|
|
|
|
-- @function armor:equip
|
|
|
|
-- @tparam ObjectRef player Player to whom item is equipped.
|
|
|
|
-- @tparam ItemStack itemstack Armor item to be equipped.
|
|
|
|
-- @treturn ItemStack Leftover item stack.
|
2020-09-24 14:08:17 +02:00
|
|
|
armor.equip = function(self, player, itemstack)
|
|
|
|
local name, armor_inv = self:get_valid_player(player, "[equip]")
|
|
|
|
local armor_element = self:get_element(itemstack:get_name())
|
|
|
|
if name and armor_element then
|
2021-07-30 16:02:59 +02:00
|
|
|
local index
|
|
|
|
for i=1, armor_inv:get_size("armor") do
|
|
|
|
local stack = armor_inv:get_stack("armor", i)
|
|
|
|
if self:get_element(stack:get_name()) == armor_element then
|
2023-05-31 14:59:20 +02:00
|
|
|
--prevents equiping an armor that would unequip a cursed armor.
|
|
|
|
if minetest.get_item_group(stack:get_name(), "cursed") ~= 0 then
|
|
|
|
return itemstack
|
|
|
|
end
|
2021-07-30 16:02:59 +02:00
|
|
|
index = i
|
|
|
|
self:unequip(player, armor_element)
|
|
|
|
break
|
|
|
|
elseif not index and stack:is_empty() then
|
|
|
|
index = i
|
|
|
|
end
|
2020-09-24 14:08:17 +02:00
|
|
|
end
|
2021-07-30 16:02:59 +02:00
|
|
|
local stack = itemstack:take_item()
|
|
|
|
armor_inv:set_stack("armor", index, stack)
|
|
|
|
self:run_callbacks("on_equip", player, index, stack)
|
2020-09-24 14:08:17 +02:00
|
|
|
self:set_player_armor(player)
|
|
|
|
self:save_armor_inventory(player)
|
|
|
|
end
|
|
|
|
return itemstack
|
2020-09-22 07:45:39 +02:00
|
|
|
end
|
|
|
|
|
2021-07-30 16:12:29 +02:00
|
|
|
--- Unequips a piece of armor from a player.
|
|
|
|
--
|
|
|
|
-- @function armor:unequip
|
|
|
|
-- @tparam ObjectRef player Player from whom item is removed.
|
|
|
|
-- @tparam string armor_element Armor type identifier associated with the item
|
|
|
|
-- to be removed ("head", "torso", "hands", "shield", "legs", "feet", etc.).
|
2020-09-24 14:08:17 +02:00
|
|
|
armor.unequip = function(self, player, armor_element)
|
|
|
|
local name, armor_inv = self:get_valid_player(player, "[unequip]")
|
2021-07-30 16:02:59 +02:00
|
|
|
if not name then
|
2020-09-22 07:45:39 +02:00
|
|
|
return
|
2020-09-24 14:08:17 +02:00
|
|
|
end
|
2021-07-30 16:02:59 +02:00
|
|
|
for i=1, armor_inv:get_size("armor") do
|
|
|
|
local stack = armor_inv:get_stack("armor", i)
|
|
|
|
if self:get_element(stack:get_name()) == armor_element then
|
|
|
|
armor_inv:set_stack("armor", i, "")
|
|
|
|
minetest.after(0, function()
|
2024-05-01 07:24:49 +02:00
|
|
|
local pplayer = minetest.get_player_by_name(name)
|
|
|
|
if pplayer then -- player is still online
|
|
|
|
local inv = pplayer:get_inventory()
|
|
|
|
if inv:room_for_item("main", stack) then
|
|
|
|
inv:add_item("main", stack)
|
|
|
|
else
|
|
|
|
minetest.add_item(pplayer:get_pos(), stack)
|
|
|
|
end
|
2021-07-30 16:02:59 +02:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
self:run_callbacks("on_unequip", player, i, stack)
|
|
|
|
self:set_player_armor(player)
|
|
|
|
self:save_armor_inventory(player)
|
|
|
|
return
|
2020-09-24 14:08:17 +02:00
|
|
|
end
|
2021-07-30 16:02:59 +02:00
|
|
|
end
|
2020-09-22 07:45:39 +02:00
|
|
|
end
|
|
|
|
|
2021-07-30 16:12:29 +02:00
|
|
|
--- Removes all armor worn by player.
|
|
|
|
--
|
|
|
|
-- @function armor:remove_all
|
|
|
|
-- @tparam ObjectRef player
|
2020-09-19 13:30:39 +02:00
|
|
|
armor.remove_all = function(self, player)
|
2020-09-22 07:45:39 +02:00
|
|
|
local name, inv = self:get_valid_player(player, "[remove_all]")
|
2020-09-19 13:30:39 +02:00
|
|
|
if not name then
|
|
|
|
return
|
|
|
|
end
|
2020-09-22 07:45:39 +02:00
|
|
|
inv:set_list("armor", {})
|
2020-09-19 13:30:39 +02:00
|
|
|
self:set_player_armor(player)
|
|
|
|
self:save_armor_inventory(player)
|
|
|
|
end
|
|
|
|
|
2021-07-13 20:50:22 +02:00
|
|
|
local skin_mod
|
|
|
|
|
2021-07-30 16:12:29 +02:00
|
|
|
--- Retrieves player's current skin.
|
|
|
|
--
|
|
|
|
-- @function armor:get_player_skin
|
|
|
|
-- @tparam string name Player name.
|
|
|
|
-- @treturn string Skin filename.
|
2017-03-17 21:05:55 +01:00
|
|
|
armor.get_player_skin = function(self, name)
|
2021-07-13 20:50:22 +02:00
|
|
|
if (skin_mod == "skins" or skin_mod == "simple_skins") and skins.skins[name] then
|
2017-06-18 22:13:48 +02:00
|
|
|
return skins.skins[name]..".png"
|
2021-07-13 20:50:22 +02:00
|
|
|
elseif skin_mod == "u_skins" and u_skins.u_skins[name] then
|
2017-06-18 22:13:48 +02:00
|
|
|
return u_skins.u_skins[name]..".png"
|
2021-07-13 20:50:22 +02:00
|
|
|
elseif skin_mod == "wardrobe" and wardrobe.playerSkins and wardrobe.playerSkins[name] then
|
2017-06-20 07:34:09 +02:00
|
|
|
return wardrobe.playerSkins[name]
|
2017-03-17 21:05:55 +01:00
|
|
|
end
|
2017-06-18 22:13:48 +02:00
|
|
|
return armor.default_skin..".png"
|
2017-03-17 21:05:55 +01:00
|
|
|
end
|
|
|
|
|
2021-07-30 16:12:29 +02:00
|
|
|
--- Updates skin.
|
|
|
|
--
|
|
|
|
-- @function armor:update_skin
|
|
|
|
-- @tparam string name Player name.
|
2020-10-12 07:47:57 +02:00
|
|
|
armor.update_skin = function(self, name)
|
|
|
|
minetest.after(0, function()
|
|
|
|
local pplayer = minetest.get_player_by_name(name)
|
|
|
|
if pplayer then
|
|
|
|
self.textures[name].skin = self:get_player_skin(name)
|
|
|
|
self:set_player_armor(pplayer)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2021-07-30 16:12:29 +02:00
|
|
|
--- Adds preview for armor inventory.
|
|
|
|
--
|
|
|
|
-- @function armor:add_preview
|
|
|
|
-- @tparam string preview Preview image filename.
|
2017-03-17 21:05:55 +01:00
|
|
|
armor.add_preview = function(self, preview)
|
|
|
|
skin_previews[preview] = true
|
|
|
|
end
|
|
|
|
|
2021-07-30 16:12:29 +02:00
|
|
|
--- Retrieves preview for armor inventory.
|
|
|
|
--
|
|
|
|
-- @function armor:get_preview
|
|
|
|
-- @tparam string name Player name.
|
|
|
|
-- @treturn string Preview image filename.
|
2017-03-17 21:05:55 +01:00
|
|
|
armor.get_preview = function(self, name)
|
2017-06-18 22:13:48 +02:00
|
|
|
local preview = string.gsub(armor:get_player_skin(name), ".png", "_preview.png")
|
2017-03-17 21:05:55 +01:00
|
|
|
if skin_previews[preview] then
|
|
|
|
return preview
|
|
|
|
end
|
|
|
|
return "character_preview.png"
|
|
|
|
end
|
|
|
|
|
2021-07-30 16:12:29 +02:00
|
|
|
--- Retrieves armor formspec.
|
|
|
|
--
|
|
|
|
-- @function armor:get_armor_formspec
|
|
|
|
-- @tparam string name Player name.
|
|
|
|
-- @tparam[opt] bool listring Use `listring` formspec element (default: `false`).
|
|
|
|
-- @treturn string Formspec formatted string.
|
2017-03-17 21:05:55 +01:00
|
|
|
armor.get_armor_formspec = function(self, name, listring)
|
2017-03-23 01:52:32 +01:00
|
|
|
local formspec = armor.formspec..
|
|
|
|
"list[detached:"..name.."_armor;armor;0,0.5;2,3;]"
|
2017-03-17 21:05:55 +01:00
|
|
|
if listring == true then
|
|
|
|
formspec = formspec.."listring[current_player;main]"..
|
|
|
|
"listring[detached:"..name.."_armor;armor]"
|
|
|
|
end
|
|
|
|
formspec = formspec:gsub("armor_preview", armor.textures[name].preview)
|
|
|
|
formspec = formspec:gsub("armor_level", armor.def[name].level)
|
2017-03-23 01:52:32 +01:00
|
|
|
for _, attr in pairs(self.attributes) do
|
|
|
|
formspec = formspec:gsub("armor_attr_"..attr, armor.def[name][attr])
|
|
|
|
end
|
2018-01-10 20:16:23 +01:00
|
|
|
for group, _ in pairs(self.registered_groups) do
|
|
|
|
formspec = formspec:gsub("armor_group_"..group,
|
|
|
|
armor.def[name].groups[group])
|
2017-03-23 01:52:32 +01:00
|
|
|
end
|
2017-03-17 21:05:55 +01:00
|
|
|
return formspec
|
|
|
|
end
|
|
|
|
|
2021-07-30 16:12:29 +02:00
|
|
|
--- Retrieves element.
|
|
|
|
--
|
|
|
|
-- @function armor:get_element
|
|
|
|
-- @tparam string item_name
|
|
|
|
-- @return Armor element.
|
2018-05-19 21:25:27 +02:00
|
|
|
armor.get_element = function(self, item_name)
|
|
|
|
for _, element in pairs(armor.elements) do
|
|
|
|
if minetest.get_item_group(item_name, "armor_"..element) > 0 then
|
|
|
|
return element
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-30 16:12:29 +02:00
|
|
|
--- Serializes armor inventory.
|
|
|
|
--
|
|
|
|
-- @function armor:serialize_inventory_list
|
|
|
|
-- @tparam table list Inventory contents.
|
|
|
|
-- @treturn string
|
2018-02-11 13:33:46 +01:00
|
|
|
armor.serialize_inventory_list = function(self, list)
|
|
|
|
local list_table = {}
|
|
|
|
for _, stack in ipairs(list) do
|
|
|
|
table.insert(list_table, stack:to_string())
|
|
|
|
end
|
|
|
|
return minetest.serialize(list_table)
|
|
|
|
end
|
|
|
|
|
2021-07-30 16:12:29 +02:00
|
|
|
--- Deserializes armor inventory.
|
|
|
|
--
|
|
|
|
-- @function armor:deserialize_inventory_list
|
|
|
|
-- @tparam string list_string Serialized inventory contents.
|
|
|
|
-- @treturn table
|
2018-02-11 13:33:46 +01:00
|
|
|
armor.deserialize_inventory_list = function(self, list_string)
|
|
|
|
local list_table = minetest.deserialize(list_string)
|
|
|
|
local list = {}
|
|
|
|
for _, stack in ipairs(list_table or {}) do
|
|
|
|
table.insert(list, ItemStack(stack))
|
|
|
|
end
|
|
|
|
return list
|
|
|
|
end
|
|
|
|
|
2021-07-30 16:12:29 +02:00
|
|
|
--- Loads armor inventory.
|
|
|
|
--
|
|
|
|
-- @function armor:load_armor_inventory
|
|
|
|
-- @tparam ObjectRef player
|
|
|
|
-- @treturn bool
|
2018-02-11 13:33:46 +01:00
|
|
|
armor.load_armor_inventory = function(self, player)
|
2018-06-02 23:13:41 +02:00
|
|
|
local _, inv = self:get_valid_player(player, "[load_armor_inventory]")
|
|
|
|
if inv then
|
2019-06-17 20:03:27 +02:00
|
|
|
local meta = player:get_meta()
|
2019-06-16 22:17:21 +02:00
|
|
|
local armor_list_string = meta:get_string("3d_armor_inventory")
|
2018-06-02 23:13:41 +02:00
|
|
|
if armor_list_string then
|
|
|
|
inv:set_list("armor",
|
|
|
|
self:deserialize_inventory_list(armor_list_string))
|
|
|
|
return true
|
|
|
|
end
|
2018-02-11 13:33:46 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-30 16:12:29 +02:00
|
|
|
--- Saves armor inventory.
|
|
|
|
--
|
|
|
|
-- Inventory is stored in `PlayerMetaRef` string "3d\_armor\_inventory".
|
|
|
|
--
|
|
|
|
-- @function armor:save_armor_inventory
|
|
|
|
-- @tparam ObjectRef player
|
2018-02-11 13:33:46 +01:00
|
|
|
armor.save_armor_inventory = function(self, player)
|
2018-06-02 23:13:41 +02:00
|
|
|
local _, inv = self:get_valid_player(player, "[save_armor_inventory]")
|
|
|
|
if inv then
|
2019-06-17 20:03:27 +02:00
|
|
|
local meta = player:get_meta()
|
2019-06-16 22:17:21 +02:00
|
|
|
meta:set_string("3d_armor_inventory",
|
2018-06-02 23:13:41 +02:00
|
|
|
self:serialize_inventory_list(inv:get_list("armor")))
|
2018-02-11 13:33:46 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-30 16:12:29 +02:00
|
|
|
--- Updates inventory.
|
|
|
|
--
|
|
|
|
-- DEPRECATED: Legacy inventory support.
|
|
|
|
--
|
|
|
|
-- @function armor:update_inventory
|
|
|
|
-- @param player
|
2017-03-17 21:05:55 +01:00
|
|
|
armor.update_inventory = function(self, player)
|
2017-03-23 01:52:32 +01:00
|
|
|
-- DEPRECATED: Legacy inventory support
|
2017-03-17 21:05:55 +01:00
|
|
|
end
|
|
|
|
|
2021-07-30 16:12:29 +02:00
|
|
|
--- Sets inventory stack.
|
|
|
|
--
|
|
|
|
-- @function armor:set_inventory_stack
|
|
|
|
-- @tparam ObjectRef player
|
|
|
|
-- @tparam int i Armor inventory index.
|
|
|
|
-- @tparam ItemStack stack Armor item.
|
2017-03-22 20:00:48 +01:00
|
|
|
armor.set_inventory_stack = function(self, player, i, stack)
|
2018-06-02 23:13:41 +02:00
|
|
|
local _, inv = self:get_valid_player(player, "[set_inventory_stack]")
|
2018-05-24 19:56:20 +02:00
|
|
|
if inv then
|
|
|
|
inv:set_stack("armor", i, stack)
|
|
|
|
self:save_armor_inventory(player)
|
2017-03-22 20:00:48 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-30 16:12:29 +02:00
|
|
|
--- Checks for a player that can use armor.
|
|
|
|
--
|
|
|
|
-- @function armor:get_valid_player
|
|
|
|
-- @tparam ObjectRef player
|
|
|
|
-- @tparam string msg Additional info for log messages.
|
|
|
|
-- @treturn list Player name & armor inventory.
|
|
|
|
-- @usage local name, inv = armor:get_valid_player(player, "[equip]")
|
2017-03-17 21:05:55 +01:00
|
|
|
armor.get_valid_player = function(self, player, msg)
|
|
|
|
msg = msg or ""
|
|
|
|
if not player then
|
2021-03-24 19:21:39 +01:00
|
|
|
minetest.log("warning", ("3d_armor%s: Player reference is nil"):format(msg))
|
2017-03-17 21:05:55 +01:00
|
|
|
return
|
|
|
|
end
|
2022-11-20 16:44:50 +01:00
|
|
|
if type(player) ~= "userdata" then
|
|
|
|
-- Fake player, fail silently
|
|
|
|
return
|
|
|
|
end
|
2017-03-17 21:05:55 +01:00
|
|
|
local name = player:get_player_name()
|
|
|
|
if not name then
|
2021-03-24 19:21:39 +01:00
|
|
|
minetest.log("warning", ("3d_armor%s: Player name is nil"):format(msg))
|
2017-03-17 21:05:55 +01:00
|
|
|
return
|
|
|
|
end
|
2018-02-11 13:33:46 +01:00
|
|
|
local inv = minetest.get_inventory({type="detached", name=name.."_armor"})
|
2017-04-11 20:31:25 +02:00
|
|
|
if not inv then
|
2021-03-24 19:21:39 +01:00
|
|
|
-- This check may fail when called inside `on_joinplayer`
|
|
|
|
-- in that case, the armor will be initialized/updated later on
|
|
|
|
minetest.log("warning", ("3d_armor%s: Detached armor inventory is nil"):format(msg))
|
2017-03-17 21:05:55 +01:00
|
|
|
return
|
|
|
|
end
|
2017-04-11 21:54:26 +02:00
|
|
|
return name, inv
|
2017-03-17 21:05:55 +01:00
|
|
|
end
|
|
|
|
|
2021-07-30 16:12:29 +02:00
|
|
|
--- Drops armor item at given position.
|
|
|
|
--
|
|
|
|
-- @tparam vector pos
|
|
|
|
-- @tparam ItemStack stack Armor item to be dropped.
|
2017-03-17 21:05:55 +01:00
|
|
|
armor.drop_armor = function(pos, stack)
|
2017-08-01 19:42:04 +02:00
|
|
|
local node = minetest.get_node_or_nil(pos)
|
|
|
|
if node then
|
|
|
|
local obj = minetest.add_item(pos, stack)
|
|
|
|
if obj then
|
2019-06-16 22:17:21 +02:00
|
|
|
obj:set_velocity({x=math.random(-1, 1), y=5, z=math.random(-1, 1)})
|
2017-08-01 19:42:04 +02:00
|
|
|
end
|
2017-03-17 21:05:55 +01:00
|
|
|
end
|
|
|
|
end
|
2021-06-23 17:28:29 +02:00
|
|
|
|
|
|
|
--- Allows skin mod to be set manually.
|
|
|
|
--
|
|
|
|
-- Useful for skin mod forks that do not use the same name.
|
2021-07-30 16:12:29 +02:00
|
|
|
--
|
|
|
|
-- @tparam string mod Name of skin mod. Recognized names are "simple\_skins", "u\_skins", & "wardrobe".
|
2021-06-23 17:28:29 +02:00
|
|
|
armor.set_skin_mod = function(mod)
|
2021-07-13 20:50:22 +02:00
|
|
|
skin_mod = mod
|
2021-06-23 17:28:29 +02:00
|
|
|
end
|