xdecor-libre/enchanting.lua

238 lines
7.6 KiB
Lua
Raw Normal View History

2015-08-05 15:16:39 +03:00
local enchanting = {}
screwdriver = screwdriver or {}
2015-08-05 15:16:39 +03:00
function enchanting.formspec(pos, tooltype)
local meta = minetest.get_meta(pos)
local formspec = [[ size[9,9;]
bgcolor[#080808BB;true]
background[0,0;9,9;ench_ui.png]
list[context;tool;0.9,2.9;1,1;]
list[context;mese;2,2.9;1,1;]
list[current_player;main;0.5,4.5;8,4;]
image[2,2.9;1,1;mese_layout.png]
tooltip[sharp;Your sword kills faster]
tooltip[durable;Your tool lasts longer]
tooltip[fast;Your tool digs faster]
tooltip[strong;Your armor is more resistant]
tooltip[speed;Your speed is increased] ]]
2015-12-30 22:08:39 +03:00
..default.gui_slots..default.get_hotbar_bg(0.5,4.5)
if tooltype == "sword" then
2015-12-01 15:09:40 +03:00
formspec = formspec.."image_button[3.9,2.9;4,0.92;bg_btn.png;sharp;Sharpness]"
elseif tooltype == "tool" then
2016-01-06 02:08:58 +03:00
formspec = formspec..[[ image_button[3.9,0.85;4,0.92;bg_btn.png;fast;Efficiency]
image_button[3.9,1.77;4,1.12;bg_btn.png;durable;Durability] ]]
elseif tooltype == "armor" then
2015-12-01 15:09:40 +03:00
formspec = formspec.."image_button[3.9,0.85;4,0.92;bg_btn.png;strong;Strength]"
elseif tooltype == "boots" then
2016-01-06 02:08:58 +03:00
formspec = formspec..[[ image_button[3.9,0.85;4,0.92;bg_btn.png;strong;Strength]
image_button[3.9,1.77;4,1.12;bg_btn.png;speed;Speed] ]]
end
2015-08-22 23:06:36 +03:00
2015-12-30 22:08:39 +03:00
meta:set_string("formspec", formspec)
return formspec
end
function enchanting.on_put(pos, listname, _, stack, _)
local stn = stack:get_name()
local meta = minetest.get_meta(pos)
if listname == "tool" then
if stn:find("pick") or stn:find("axe") or stn:find("shovel") then
meta:set_string("formspec", enchanting.formspec(pos, "tool"))
elseif stn:find("sword") then
meta:set_string("formspec", enchanting.formspec(pos, "sword"))
elseif stn:find("chestplate") or stn:find("leggings") or stn:find("helmet") then
meta:set_string("formspec", enchanting.formspec(pos, "armor"))
elseif stn:find("boots") then
meta:set_string("formspec", enchanting.formspec(pos, "boots"))
end
end
end
2015-08-12 21:49:07 +03:00
function enchanting.fields(pos, _, fields, _)
2016-01-06 02:08:58 +03:00
if fields.quit then return end
2015-08-24 23:50:29 +03:00
local inv = minetest.get_meta(pos):get_inventory()
local toolstack = inv:get_stack("tool", 1)
2015-11-23 17:53:08 +03:00
local toolstack_name = toolstack:get_name()
local mesestack = inv:get_stack("mese", 1)
local mod, tool = toolstack_name:match("([%w_]+):([%w_]+)")
local toolwear = toolstack:get_wear()
2015-08-03 00:02:56 +03:00
local mese = mesestack:get_count()
2015-08-19 23:52:16 +03:00
local ench = dump(fields):match("%w+")
local enchanted_tool = mod..":enchanted_"..tool.."_"..ench
2015-11-23 17:53:08 +03:00
if mese > 0 and fields[ench] and
minetest.registered_tools[enchanted_tool] then
2015-11-26 14:07:35 +03:00
toolstack:replace(enchanted_tool)
toolstack:add_wear(toolwear)
mesestack:take_item()
inv:set_stack("mese", 1, mesestack)
inv:set_stack("tool", 1, toolstack)
end
end
2015-08-12 21:49:07 +03:00
function enchanting.dig(pos, _)
2015-08-24 23:50:29 +03:00
local inv = minetest.get_meta(pos):get_inventory()
return inv:is_empty("tool") and inv:is_empty("mese")
end
local function allowed(tool)
for item in pairs(minetest.registered_tools) do
if item:match("enchanted_"..tool) then
return true
end
end
return false
end
2015-08-12 21:49:07 +03:00
function enchanting.put(_, listname, _, stack, _)
2015-11-23 17:53:08 +03:00
local toolstack = stack:get_name()
local toolname = toolstack:match("[%w_]+:([%w_]+)")
if listname == "mese" and toolstack == "default:mese_crystal" then
return stack:get_count()
elseif listname == "tool" and allowed(toolname) then
return 1
end
return 0
end
xdecor.register("enchantment_table", {
description = "Enchantment Table",
tiles = {
2015-08-05 11:39:57 +03:00
"xdecor_enchantment_top.png", "xdecor_enchantment_bottom.png",
"xdecor_enchantment_side.png", "xdecor_enchantment_side.png",
"xdecor_enchantment_side.png", "xdecor_enchantment_side.png"
},
2015-11-25 00:23:43 +03:00
groups = {cracky=1, oddly_breakable_by_hand=1, level=1},
2015-08-16 22:57:17 +03:00
sounds = default.node_sound_stone_defaults(),
on_rotate = screwdriver.rotate_simple,
2015-08-05 15:16:39 +03:00
can_dig = enchanting.dig,
on_construct = function(pos)
local meta = minetest.get_meta(pos)
enchanting.formspec(pos, nil)
meta:set_string("infotext", "Enchantment Table")
local inv = meta:get_inventory()
inv:set_size("tool", 1)
inv:set_size("mese", 1)
end,
enchanting.formspec,
on_receive_fields = enchanting.fields,
on_metadata_inventory_put = enchanting.on_put,
allow_metadata_inventory_put = enchanting.put,
2015-12-30 22:08:39 +03:00
allow_metadata_inventory_move = function() return 0 end,
on_metadata_inventory_take = function(pos, listname, _, _, _)
if listname == "tool" then
enchanting.formspec(pos, nil)
end
end
})
2015-12-06 13:36:47 +03:00
local function cap(str)
return str:gsub("^%l", string.upper)
end
2015-08-05 15:16:39 +03:00
2015-08-24 22:01:50 +03:00
-- Higher number = stronger enchant.
local use_factor = 1.2
local times_subtractor = 0.1
2015-09-25 02:00:07 +03:00
local damage_adder = 1
2015-12-06 13:36:47 +03:00
local strength_factor = 1.2
local speed_factor = 0.2
local jump_factor = 0.2
2015-11-23 17:53:08 +03:00
local tools = {
--[[ Registration format :
2015-11-23 17:53:08 +03:00
[Mod name] = {
{materials},
{tool name, tool group, {enchantments}}
}
--]]
["default"] = {
{"steel", "bronze", "mese", "diamond"},
{"axe", "choppy", {"durable", "fast"}},
{"pick", "cracky", {"durable", "fast"}},
{"shovel", "crumbly", {"durable", "fast"}},
{"sword", "fleshy", {"sharp"}}
},
["3d_armor"] = {
{"steel", "bronze", "gold", "diamond"},
{"boots", nil, {"strong", "speed"}},
{"chestplate", nil, {"strong"}},
{"helmet", nil, {"strong"}},
{"leggings", nil, {"strong"}}
}
}
for mod, defs in pairs(tools) do
for _, mat in pairs(defs[1]) do
for _, tooldef in next, defs, 1 do
for _, ench in pairs(tooldef[3]) do
local tool, group, material, enchant = tooldef[1], tooldef[2], mat, ench
local original_tool = minetest.registered_tools[mod..":"..tool.."_"..material]
if original_tool then
if mod == "default" then
local original_damage_groups = original_tool.tool_capabilities.damage_groups
local original_groupcaps = original_tool.tool_capabilities.groupcaps
local groupcaps = table.copy(original_groupcaps)
local fleshy = original_damage_groups.fleshy
2015-11-23 22:27:29 +03:00
local full_punch_interval = original_tool.tool_capabilities.full_punch_interval
local max_drop_level = original_tool.tool_capabilities.max_drop_level
2015-11-23 17:53:08 +03:00
if enchant == "durable" then
2015-12-23 13:50:38 +03:00
groupcaps[group].uses = math.ceil(original_groupcaps[group].uses * use_factor)
2015-11-23 17:53:08 +03:00
elseif enchant == "fast" then
for i = 1, 3 do
groupcaps[group].times[i] = original_groupcaps[group].times[i] - times_subtractor
end
elseif enchant == "sharp" then
fleshy = fleshy + damage_adder
end
2015-11-23 17:53:08 +03:00
minetest.register_tool(":"..mod..":enchanted_"..tool.."_"..material.."_"..enchant, {
2015-12-06 13:36:47 +03:00
description = "Enchanted "..cap(material).." "..cap(tool).." ("..cap(enchant)..")",
2015-11-23 17:53:08 +03:00
inventory_image = original_tool.inventory_image.."^[colorize:violet:50",
wield_image = original_tool.wield_image,
groups = {not_in_creative_inventory=1},
2015-11-23 22:27:29 +03:00
tool_capabilities = {
groupcaps = groupcaps, damage_groups = {fleshy = fleshy},
full_punch_interval = full_punch_interval, max_drop_level = max_drop_level
}
2015-11-23 17:53:08 +03:00
})
end
2015-09-24 21:41:24 +03:00
2015-11-23 17:53:08 +03:00
if mod == "3d_armor" then
local original_armor_groups = original_tool.groups
local armorcaps = table.copy(original_armor_groups)
local armorcaps = {}
armorcaps.not_in_creative_inventory=1
2015-11-23 17:53:08 +03:00
2015-11-23 22:27:29 +03:00
for armor_group, value in pairs(original_armor_groups) do
if enchant == "strong" then
2015-12-23 13:50:38 +03:00
armorcaps[armor_group] = math.ceil(value * strength_factor)
2015-11-23 22:27:29 +03:00
elseif enchant == "speed" then
armorcaps[armor_group] = value
2015-12-06 13:36:47 +03:00
armorcaps.physics_speed = speed_factor
armorcaps.physics_jump = jump_factor
2015-11-23 17:53:08 +03:00
end
end
minetest.register_tool(":"..mod..":enchanted_"..tool.."_"..material.."_"..enchant, {
2015-12-06 13:36:47 +03:00
description = "Enchanted "..cap(material).." "..cap(tool).." ("..cap(enchant)..")",
inventory_image = original_tool.inventory_image,
texture = "3d_armor_"..tool.."_"..material,
2015-11-23 17:53:08 +03:00
wield_image = original_tool.wield_image,
groups = armorcaps,
2015-11-23 17:53:08 +03:00
wear = 0
})
end
2015-08-05 15:16:39 +03:00
end
2015-11-23 17:53:08 +03:00
end
end
end
2015-08-05 15:16:39 +03:00
end