mirror of
https://github.com/minetest-mods/3d_armor.git
synced 2024-09-12 07:38:30 +02:00
562 lines
15 KiB
Lua
562 lines
15 KiB
Lua
local skin_previews = {}
|
|
local use_player_monoids = minetest.global_exists("player_monoids")
|
|
local use_armor_monoid = minetest.global_exists("armor_monoid")
|
|
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
|
|
})
|
|
|
|
armor = {
|
|
timer = 0,
|
|
elements = {"head", "torso", "legs", "feet"},
|
|
physics = {"jump", "speed", "gravity"},
|
|
attributes = {"heal", "fire", "water"},
|
|
formspec = "image[2.5,0;2,4;armor_preview]"..
|
|
default.gui_bg..
|
|
default.gui_bg_img..
|
|
default.gui_slots..
|
|
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]",
|
|
def = armor_def,
|
|
textures = armor_textures,
|
|
default_skin = "character",
|
|
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},
|
|
},
|
|
registered_groups = {["fleshy"]=100},
|
|
registered_callbacks = {
|
|
on_update = {},
|
|
on_equip = {},
|
|
on_unequip = {},
|
|
on_damage = {},
|
|
on_destroy = {},
|
|
},
|
|
version = "0.4.8",
|
|
}
|
|
|
|
armor.config = {
|
|
init_delay = 2,
|
|
init_times = 10,
|
|
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,
|
|
water_protect = true,
|
|
fire_protect = minetest.get_modpath("ethereal") ~= nil,
|
|
punch_damage = true,
|
|
}
|
|
|
|
-- 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
|
|
|
|
-- 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_damage = function(self, func)
|
|
if type(func) == "function" then
|
|
table.insert(self.registered_callbacks.on_damage, 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
|
|
|
|
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.init_player_armor = function(self, player)
|
|
local name = player:get_player_name()
|
|
local player_inv = player:get_inventory()
|
|
local pos = player:getpos()
|
|
if not name or not player_inv or not pos then
|
|
return false
|
|
end
|
|
local armor_inv = minetest.create_detached_inventory(name.."_armor", {
|
|
on_put = function(inv, listname, index, stack, player)
|
|
player:get_inventory():set_stack(listname, index, stack)
|
|
armor:set_player_armor(player)
|
|
armor:run_callbacks("on_equip", player, stack)
|
|
end,
|
|
on_take = function(inv, listname, index, stack, player)
|
|
player:get_inventory():set_stack(listname, index, nil)
|
|
armor:set_player_armor(player)
|
|
armor:run_callbacks("on_unequip", player, stack)
|
|
end,
|
|
on_move = function(inv, from_list, from_index, to_list, to_index, count, player)
|
|
local plaver_inv = player:get_inventory()
|
|
local stack = inv:get_stack(to_list, to_index)
|
|
player_inv:set_stack(to_list, to_index, stack)
|
|
player_inv:set_stack(from_list, from_index, nil)
|
|
armor:set_player_armor(player)
|
|
end,
|
|
allow_put = function(inv, listname, index, stack, player)
|
|
local def = stack:get_definition() or {}
|
|
for _, element in pairs(armor.elements) do
|
|
if def.groups["armor_"..element] then
|
|
for i = 1, 6 do
|
|
local item = inv:get_stack("armor", i):get_name()
|
|
if minetest.get_item_group(item, "armor_"..element) > 0 then
|
|
return 0
|
|
end
|
|
end
|
|
end
|
|
end
|
|
return 1
|
|
end,
|
|
allow_take = function(inv, listname, index, stack, player)
|
|
return stack:get_count()
|
|
end,
|
|
allow_move = function(inv, from_list, from_index, to_list, to_index, count, player)
|
|
return count
|
|
end,
|
|
}, name)
|
|
armor_inv:set_size("armor", 6)
|
|
player_inv:set_size("armor", 6)
|
|
for i=1, 6 do
|
|
local stack = player_inv:get_stack("armor", i)
|
|
armor_inv:set_stack("armor", i, stack)
|
|
self:run_callbacks("on_equip", player, stack)
|
|
end
|
|
self.def[name] = {
|
|
init_time = minetest.get_gametime(),
|
|
level = 0,
|
|
state = 0,
|
|
count = 0,
|
|
groups = {},
|
|
}
|
|
for _, phys in pairs(armor.physics) do
|
|
self.def[name][phys] = 1
|
|
end
|
|
for _, attr in pairs(armor.attributes) do
|
|
self.def[name][attr] = 0
|
|
end
|
|
for group, _ in pairs(armor.registered_groups) do
|
|
self.def[name].groups[group] = 0
|
|
end
|
|
local skin = self:get_player_skin(name)
|
|
self.textures[name] = {
|
|
skin = skin..".png",
|
|
armor = "3d_armor_trans.png",
|
|
wielditem = "3d_armor_trans.png",
|
|
preview = armor.default_skin.."_preview.png",
|
|
}
|
|
local texture_path = minetest.get_modpath("player_textures")
|
|
if texture_path then
|
|
local dir_list = minetest.get_dir_list(texture_path.."/textures")
|
|
for _, fn in pairs(dir_list) do
|
|
if fn == "player_"..name..".png" then
|
|
self.textures[name].skin = fn
|
|
break
|
|
end
|
|
end
|
|
end
|
|
self:set_player_armor(player)
|
|
return true
|
|
end
|
|
|
|
armor.set_player_armor = function(self, player)
|
|
local name, player_inv = self:get_valid_player(player, "[set_player_armor]")
|
|
if not name then
|
|
return
|
|
end
|
|
local state = 0
|
|
local count = 0
|
|
local material = {count=1}
|
|
local preview = armor:get_preview(name)
|
|
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
|
|
physics[phys] = 1
|
|
end
|
|
for _, attr in pairs(self.attributes) do
|
|
attributes[attr] = 0
|
|
end
|
|
for group, _ in pairs(self.registered_groups) do
|
|
change[group] = 1
|
|
levels[group] = 0
|
|
end
|
|
local list = player_inv:get_list("armor")
|
|
for i, stack in pairs(list) do
|
|
if stack:get_count() == 1 then
|
|
local def = stack:get_definition()
|
|
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
|
|
end
|
|
end
|
|
else
|
|
local level = def.groups["armor_"..element]
|
|
levels["fleshy"] = levels["fleshy"] + level
|
|
end
|
|
end
|
|
-- 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$", "")
|
|
local prev = def.preview or tex.."_preview"
|
|
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
|
|
end
|
|
end
|
|
end
|
|
for group, level in pairs(levels) do
|
|
if level > 0 then
|
|
level = level * armor.config.level_multiplier
|
|
if material.name and material.count == #self.elements then
|
|
level = level * 1.1
|
|
end
|
|
end
|
|
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
|
|
end
|
|
for _, attr in pairs(self.attributes) do
|
|
self.def[name][attr] = attributes[attr]
|
|
end
|
|
for _, phys in pairs(self.physics) do
|
|
self.def[name][phys] = physics[phys]
|
|
end
|
|
if use_armor_monoid then
|
|
armor_monoid.monoid:add_change(player, change, "3d_armor:armor")
|
|
else
|
|
player:set_armor_groups(groups)
|
|
end
|
|
if use_player_monoids then
|
|
player_monoids.speed:add_change(player, physics.speed,
|
|
"3d_armor:physics")
|
|
player_monoids.jump:add_change(player, physics.jump,
|
|
"3d_armor:physics")
|
|
player_monoids.gravity:add_change(player, physics.gravity,
|
|
"3d_armor:physics")
|
|
else
|
|
player:set_physics_override(physics)
|
|
end
|
|
self.textures[name].armor = texture
|
|
self.textures[name].preview = preview
|
|
self.def[name].level = self.def[name].groups.fleshy or 0
|
|
self.def[name].state = state
|
|
self.def[name].count = count
|
|
self:update_player_visuals(player)
|
|
self:run_callbacks("on_update", player)
|
|
end
|
|
|
|
armor.punch = function(self, player, hitter, time_from_last_punch, tool_capabilities)
|
|
local name, player_inv = self:get_valid_player(player, "[punch]")
|
|
if not name then
|
|
return
|
|
end
|
|
local state = 0
|
|
local count = 0
|
|
local recip = true
|
|
local default_groups = {cracky=3, snappy=3, choppy=3, crumbly=3, level=1}
|
|
local list = player_inv:get_list("armor")
|
|
for i, stack in pairs(list) do
|
|
if stack:get_count() == 1 then
|
|
local name = stack:get_name()
|
|
local use = minetest.get_item_group(name, "armor_use") or 0
|
|
local damage = use > 0
|
|
local def = stack:get_definition() or {}
|
|
if type(def.on_punch) == "function" then
|
|
damage = def.on_punch(player, hitter, time_from_last_punch,
|
|
tool_capabilities) ~= false and damage == true
|
|
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
|
|
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()
|
|
if item and item:get_name() ~= "" then
|
|
item:add_wear(65535 / uses)
|
|
hitter:set_wielded_item(item)
|
|
end
|
|
-- reciprocate tool damage only once
|
|
recip = false
|
|
end
|
|
end
|
|
if damage == true and hitter == "fire" then
|
|
damage = minetest.get_item_group(name, "flammable") > 0
|
|
end
|
|
if damage == true then
|
|
local old_stack = ItemStack(stack)
|
|
stack:add_wear(use)
|
|
self:set_inventory_stack(player, i, stack)
|
|
self:run_callbacks("on_damage", player, stack)
|
|
if stack:get_count() == 0 then
|
|
self:run_callbacks("on_unequip", player, old_stack)
|
|
self:run_callbacks("on_destroy", player, old_stack)
|
|
self:set_player_armor(player)
|
|
end
|
|
end
|
|
state = state + stack:get_wear()
|
|
count = count + 1
|
|
end
|
|
end
|
|
self.def[name].state = state
|
|
self.def[name].count = count
|
|
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
|
|
local skins = wardrobe.playerSkins or {}
|
|
if skins[name] then
|
|
skin = string.gsub(skins[name], "%.png$","")
|
|
end
|
|
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)
|
|
if armor.def[name].init_time == 0 then
|
|
return "label[0,0;Armor not initialized!]"
|
|
end
|
|
local formspec = armor.formspec..
|
|
"list[detached:"..name.."_armor;armor;0,0.5;2,3;]"
|
|
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)
|
|
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
|
|
return formspec
|
|
end
|
|
|
|
armor.update_inventory = function(self, player)
|
|
-- DEPRECATED: Legacy inventory support
|
|
end
|
|
|
|
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
|
|
|
|
armor.get_valid_player = function(self, player, msg)
|
|
msg = msg or ""
|
|
if not player then
|
|
minetest.log("warning", "3d_armor: Player reference is nil "..msg)
|
|
return
|
|
end
|
|
local name = player:get_player_name()
|
|
if not name then
|
|
minetest.log("warning", "3d_armor: Player name is nil "..msg)
|
|
return
|
|
end
|
|
local inv = player:get_inventory()
|
|
if not inv then
|
|
minetest.log("warning", "3d_armor: Player inventory is nil "..msg)
|
|
return
|
|
end
|
|
return name, inv
|
|
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
|
|
|