xdecor-libre/enchanting.lua

152 lines
5.2 KiB
Lua
Raw Normal View History

2015-08-05 15:16:39 +03:00
local enchanting = {}
function enchanting.tools_fs()
2015-10-29 16:52:14 +03:00
return "size[9,8;]"..default.gui_slots..
"bgcolor[#080808BB;true]background[0,0;9,8;ench_ui.png]list[current_name;tool;0.9,2.9;1,1;]list[current_name;mese;2,2.9;1,1;]image[2,2.9;1,1;mese_layout.png]list[current_player;main;0.5,4.5;8,3;]"..
"image_button[3.9,0.9;4,0.9;bg_btn.png;fast;Efficiency]image_button[3.9,1.82;4,1.1;bg_btn.png;durable;Durability]"
end
function enchanting.swords_fs()
2015-10-29 16:52:14 +03:00
return "size[9,8;]"..default.gui_slots..
"bgcolor[#080808BB;true]background[0,0;9,8;ench_ui.png]list[current_name;tool;0.9,2.9;1,1;]list[current_name;mese;2,2.9;1,1;]image[2,2.9;1,1;mese_layout.png]list[current_player;main;0.5,4.5;8,3;]"..
"image_button[3.9,2.95;4,0.9;bg_btn.png;sharp;Sharpness]"
end
2015-08-22 23:06:36 +03:00
function enchanting.default_fs(pos)
local meta = minetest.get_meta(pos)
2015-10-29 16:52:14 +03:00
local formspec = "size[9,8;]"..default.gui_slots..
"bgcolor[#080808BB;true]background[0,0;9,8;ench_ui.png]list[current_name;tool;0.9,2.9;1,1;]list[current_name;mese;2,2.9;1,1;]image[2,2.9;1,1;mese_layout.png]list[current_player;main;0.5,4.5;8,3;]"
2015-08-22 23:06:36 +03:00
meta:set_string("formspec", formspec)
meta:set_string("infotext", "Enchantment Table")
local inv = meta:get_inventory()
inv:set_size("tool", 1)
inv:set_size("mese", 1)
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("sword") then
meta:set_string("formspec", enchanting.swords_fs())
else
meta:set_string("formspec", enchanting.tools_fs())
end
end
end
2015-08-19 23:52:16 +03:00
function enchanting.is_allowed(toolname)
local tdef = minetest.registered_tools[toolname]
if tdef and toolname:find("default:") and not toolname:find("stone") and not
toolname:find("wood") then
return 1
else return 0 end
end
2015-08-12 21:49:07 +03:00
function enchanting.fields(pos, _, fields, _)
2015-08-24 23:50:29 +03:00
local inv = minetest.get_meta(pos):get_inventory()
local toolstack = inv:get_stack("tool", 1)
local mesestack = inv:get_stack("mese", 1)
2015-08-03 00:02:56 +03:00
local toolname = toolstack:get_name()
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+")
if enchanting.is_allowed(toolname) ~= 0 and mese > 0 and
fields[ench] and ench ~= "quit" then
2015-08-19 23:52:16 +03:00
toolstack:replace("xdecor:enchanted_"..toolname:sub(9).."_"..ench)
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()
if not inv:is_empty("tool") or not inv:is_empty("mese") then
return false
end
return true
end
2015-08-12 21:49:07 +03:00
function enchanting.put(_, listname, _, stack, _)
2015-08-03 00:02:56 +03:00
local toolname = stack:get_name()
local count = stack:get_count()
if listname == "mese" then
2015-08-03 00:02:56 +03:00
if toolname == "default:mese_crystal" then return count
2015-08-05 17:47:52 +03:00
else return 0 end
end
2015-08-24 23:50:29 +03:00
if listname == "tool" then return enchanting.is_allowed(toolname) end
2015-08-03 01:36:32 +03:00
return count
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"
},
groups = {cracky=1},
2015-08-16 22:57:17 +03:00
sounds = default.node_sound_stone_defaults(),
2015-08-05 15:16:39 +03:00
can_dig = enchanting.dig,
on_construct = enchanting.default_fs,
on_receive_fields = enchanting.fields,
on_metadata_inventory_put = enchanting.on_put,
allow_metadata_inventory_put = enchanting.put,
allow_metadata_inventory_move = function(...) return 0 end,
on_metadata_inventory_take = function(pos, listname, _, _, _)
if listname == "tool" then enchanting.default_fs(pos) end
end
})
2015-08-24 23:50:29 +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
function enchanting.register_enchtools()
local materials = {"steel", "bronze", "mese", "diamond"}
local tools = { {"axe", "choppy"}, {"pick", "cracky"}, {"shovel", "crumbly"}, {"sword", "fleshy"} }
local chants = {"durable", "fast", "sharp"}
2015-08-24 22:01:50 +03:00
2015-08-24 23:50:29 +03:00
for _, m in pairs(materials) do
for k, t in pairs(tools) do
2015-08-24 23:50:29 +03:00
for _, c in pairs(chants) do
local original_tool = minetest.registered_tools["default:"..t[1].."_"..m]
local original_damage_groups = original_tool.tool_capabilities.damage_groups
2015-08-24 22:01:50 +03:00
local original_groupcaps = original_tool.tool_capabilities.groupcaps
local groupcaps = table.copy(original_groupcaps)
2015-09-24 23:40:32 +03:00
local fleshy = original_damage_groups.fleshy
if c == "durable" and k <= 3 then
2015-08-24 23:50:29 +03:00
groupcaps[t[2]].uses = original_groupcaps[t[2]].uses * use_factor
elseif c == "fast" and k <= 3 then
2015-08-24 22:01:50 +03:00
for i = 1, 3 do
2015-08-24 23:50:29 +03:00
groupcaps[t[2]].times[i] = original_groupcaps[t[2]].times[i] - times_subtractor
end
elseif c == "sharp" and k == 4 then
2015-09-25 02:00:07 +03:00
fleshy = fleshy + damage_adder
end
2015-09-24 21:41:24 +03:00
2015-09-24 23:40:32 +03:00
minetest.register_tool("xdecor:enchanted_"..t[1].."_"..m.."_"..c, {
2015-08-24 23:50:29 +03:00
description = string.format("Enchanted %s %s (%s)", cap(m), cap(t[1]), cap(c)),
2015-09-24 21:41:24 +03:00
inventory_image = original_tool.inventory_image.."^[colorize:violet:50",
2015-08-24 22:01:50 +03:00
wield_image = original_tool.wield_image,
groups = {not_in_creative_inventory=1},
2015-09-24 23:40:32 +03:00
tool_capabilities = {groupcaps = groupcaps, damage_groups = {fleshy = fleshy}}
2015-08-24 22:01:50 +03:00
})
end
end
2015-08-05 15:16:39 +03:00
end
end
enchanting.register_enchtools()