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")
|
|
|
|
|
|
|
|
armor = {
|
|
|
|
timer = 0,
|
|
|
|
elements = {"head", "torso", "legs", "feet"},
|
2017-03-18 21:12:43 +01:00
|
|
|
physics = {"jump", "speed", "gravity"},
|
2017-03-23 01:52:32 +01:00
|
|
|
attributes = {"heal", "fire", "water"},
|
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-03-17 21:05:55 +01:00
|
|
|
def = {},
|
|
|
|
textures = {},
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
fire_nodes = {
|
|
|
|
{"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},
|
|
|
|
{"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 = {},
|
|
|
|
on_destroy = {},
|
|
|
|
},
|
2017-03-17 21:05:55 +01:00
|
|
|
version = "0.4.8",
|
|
|
|
}
|
|
|
|
|
2017-03-18 18:23:50 +01:00
|
|
|
armor.config = {
|
|
|
|
init_delay = 1,
|
|
|
|
init_times = 1,
|
|
|
|
bones_delay = 1,
|
|
|
|
update_time = 1,
|
|
|
|
drop = minetest.get_modpath("bones") ~= nil,
|
|
|
|
destroy = false,
|
|
|
|
level_multiplier = 1,
|
|
|
|
heal_multiplier = 1,
|
|
|
|
radiation_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,
|
2017-03-31 22:18:29 +02:00
|
|
|
water_protect = true,
|
2017-03-18 18:23:50 +01:00
|
|
|
fire_protect = minetest.get_modpath("ethereal") ~= nil
|
|
|
|
}
|
|
|
|
|
2017-03-23 01:52:32 +01:00
|
|
|
-- Armor Registration
|
|
|
|
|
|
|
|
armor.register_armor = function(self, name, def)
|
|
|
|
minetest.register_tool(name, def)
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2017-03-18 21:12:43 +01:00
|
|
|
-- Armor callbacks
|
|
|
|
|
|
|
|
armor.register_on_update = function(self, func)
|
|
|
|
if type(func) == "function" then
|
|
|
|
table.insert(self.registered_callbacks.on_update, func)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
armor.register_on_equip = function(self, func)
|
|
|
|
if type(func) == "function" then
|
|
|
|
table.insert(self.registered_callbacks.on_equip, func)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
armor.register_on_unequip = function(self, func)
|
|
|
|
if type(func) == "function" then
|
|
|
|
table.insert(self.registered_callbacks.on_unequip, func)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
armor.register_on_destroy = function(self, func)
|
|
|
|
if type(func) == "function" then
|
|
|
|
table.insert(self.registered_callbacks.on_destroy, func)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
armor.run_callbacks = function(self, callback, player, stack)
|
|
|
|
if stack then
|
|
|
|
local def = stack:get_definition() or {}
|
|
|
|
if type(def[callback]) == "function" then
|
|
|
|
def[callback](player, stack)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
local callbacks = self.registered_callbacks[callback]
|
|
|
|
if callbacks then
|
|
|
|
for _, func in pairs(callbacks) do
|
|
|
|
func(player, stack)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
default.player_set_textures(player, {
|
|
|
|
self.textures[name].skin,
|
|
|
|
self.textures[name].armor,
|
|
|
|
self.textures[name].wielditem,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
armor.set_player_armor = function(self, player)
|
2017-03-23 01:52:32 +01:00
|
|
|
local name, player_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
|
|
|
|
local material = {count=1}
|
2017-03-17 21:05:55 +01:00
|
|
|
local preview = armor:get_preview(name)
|
2017-03-23 01:52:32 +01:00
|
|
|
local texture = "3d_armor_trans.png"
|
|
|
|
local textures = {}
|
|
|
|
local physics = {}
|
|
|
|
local attributes = {}
|
|
|
|
local levels = {}
|
|
|
|
local groups = {}
|
|
|
|
local change = {}
|
|
|
|
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
|
2017-03-23 01:52:32 +01:00
|
|
|
local list = player_inv:get_list("armor")
|
|
|
|
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
|
|
|
|
end
|
2017-03-23 01:52:32 +01:00
|
|
|
-- DEPRECATED, use armor_groups instead
|
|
|
|
if def.groups["armor_radiation"] and levels["radiation"] then
|
|
|
|
levels["radiation"] = def.groups["armor_radiation"]
|
|
|
|
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$", "")
|
|
|
|
texture = texture.."^"..tex..".png"
|
|
|
|
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
|
|
|
|
local mat = string.match(item, "%:.+_(.+)$")
|
|
|
|
if material.name then
|
|
|
|
if material.name == mat then
|
|
|
|
material.count = material.count + 1
|
|
|
|
end
|
|
|
|
else
|
|
|
|
material.name = mat
|
2017-03-17 21:05:55 +01:00
|
|
|
end
|
|
|
|
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
|
2017-03-23 01:52:32 +01:00
|
|
|
if material.name and material.count == #self.elements then
|
|
|
|
level = level * 1.1
|
|
|
|
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
|
|
|
|
self.def[name][attr] = attributes[attr]
|
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
|
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")
|
|
|
|
else
|
2017-03-23 01:52:32 +01:00
|
|
|
player:set_physics_override(physics)
|
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)
|
2017-03-18 21:12:43 +01:00
|
|
|
self:run_callbacks("on_update", player)
|
2017-03-17 21:05:55 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
armor.get_player_skin = function(self, name)
|
|
|
|
local skin = nil
|
|
|
|
if self.skin_mod == "skins" or self.skin_mod == "simple_skins" then
|
|
|
|
skin = skins.skins[name]
|
|
|
|
elseif self.skin_mod == "u_skins" then
|
|
|
|
skin = u_skins.u_skins[name]
|
|
|
|
elseif self.skin_mod == "wardrobe" then
|
2017-03-18 23:14:08 +01:00
|
|
|
local skins = wardrobe.playerSkins or {}
|
|
|
|
if skins[name] then
|
|
|
|
skin = string.gsub(skins[name], "%.png$","")
|
|
|
|
end
|
2017-03-17 21:05:55 +01:00
|
|
|
end
|
|
|
|
return skin or armor.default_skin
|
|
|
|
end
|
|
|
|
|
|
|
|
armor.add_preview = function(self, preview)
|
|
|
|
skin_previews[preview] = true
|
|
|
|
end
|
|
|
|
|
|
|
|
armor.get_preview = function(self, name)
|
|
|
|
local preview = armor:get_player_skin(name).."_preview.png"
|
|
|
|
if skin_previews[preview] then
|
|
|
|
return preview
|
|
|
|
end
|
|
|
|
return "character_preview.png"
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
2017-03-29 20:18:49 +02:00
|
|
|
if not armor.def[name] or not armor.textures[name] then
|
|
|
|
return formspec
|
|
|
|
end
|
2017-03-17 21:05:55 +01:00
|
|
|
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
|
|
|
|
for _, group in pairs(self.attributes) do
|
|
|
|
formspec = formspec:gsub("armor_group_"..group, armor.def[name][group])
|
|
|
|
end
|
2017-03-17 21:05:55 +01:00
|
|
|
return formspec
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2017-03-22 20:00:48 +01:00
|
|
|
armor.set_inventory_stack = function(self, player, i, stack)
|
|
|
|
local msg = "[set_inventory_stack]"
|
|
|
|
local name = player:get_player_name()
|
|
|
|
if not name then
|
|
|
|
minetest.log("warning", "3d_armor: Player name is nil "..msg)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local player_inv = player:get_inventory()
|
|
|
|
local armor_inv = minetest.get_inventory({type="detached", name=name.."_armor"})
|
|
|
|
if not player_inv then
|
|
|
|
minetest.log("warning", "3d_armor: Player inventory is nil "..msg)
|
|
|
|
return
|
|
|
|
elseif not armor_inv then
|
|
|
|
minetest.log("warning", "3d_armor: Detached armor inventory is nil "..msg)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
player_inv:set_stack("armor", i, stack)
|
|
|
|
armor_inv:set_stack("armor", i, stack)
|
|
|
|
end
|
|
|
|
|
2017-03-17 21:05:55 +01:00
|
|
|
armor.get_valid_player = function(self, player, msg)
|
|
|
|
msg = msg or ""
|
|
|
|
if not player then
|
2017-03-22 20:00:48 +01:00
|
|
|
minetest.log("warning", "3d_armor: Player reference is nil "..msg)
|
2017-03-17 21:05:55 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
local name = player:get_player_name()
|
|
|
|
if not name then
|
2017-03-22 20:00:48 +01:00
|
|
|
minetest.log("warning", "3d_armor: Player name is nil "..msg)
|
2017-03-17 21:05:55 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
local pos = player:getpos()
|
2017-03-22 20:00:48 +01:00
|
|
|
local inv = player:get_inventory()
|
2017-03-17 21:05:55 +01:00
|
|
|
if not pos then
|
2017-03-22 20:00:48 +01:00
|
|
|
minetest.log("warning", "3d_armor: Player position is nil "..msg)
|
2017-03-17 21:05:55 +01:00
|
|
|
return
|
2017-03-22 20:00:48 +01:00
|
|
|
elseif not inv then
|
|
|
|
minetest.log("warning", "3d_armor: Player inventory is nil "..msg)
|
2017-03-17 21:05:55 +01:00
|
|
|
return
|
2017-03-22 20:00:48 +01:00
|
|
|
elseif not minetest.get_inventory({type="detached", name=name.."_armor"}) then
|
|
|
|
minetest.log("warning", "3d_armor: Detached armor inventory is nil "..msg)
|
2017-03-17 21:05:55 +01:00
|
|
|
return
|
|
|
|
end
|
2017-03-22 20:00:48 +01:00
|
|
|
return name, inv, pos
|
2017-03-17 21:05:55 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
armor.drop_armor = function(pos, stack)
|
|
|
|
local obj = minetest.add_item(pos, stack)
|
|
|
|
if obj then
|
|
|
|
obj:setvelocity({x=math.random(-1, 1), y=5, z=math.random(-1, 1)})
|
|
|
|
end
|
|
|
|
end
|