From 3dc1d61611c9cb844b469610d0808b568c991eba Mon Sep 17 00:00:00 2001 From: Tim Date: Mon, 24 Aug 2015 20:01:57 +0200 Subject: [PATCH] rewrite enchanting registrations to avoid having registration run multiple times for the same tool-material-chant combination --- enchanting.lua | 84 ++++++++++++++++++++++++-------------------------- 1 file changed, 41 insertions(+), 43 deletions(-) diff --git a/enchanting.lua b/enchanting.lua index eed01f3..820e5b9 100644 --- a/enchanting.lua +++ b/enchanting.lua @@ -90,52 +90,50 @@ xdecor.register("enchantment_table", { allow_metadata_inventory_move = function(...) return 0 end }) -function enchanting.register_enchtools(init, m, def) - local longer = init.uses * 1.2 -- Higher number = longer use. - local faster = {} - for i = 1, 3 do - faster[i] = init.times[i] - 0.1 -- Higher number = faster dig. - end +local function capitalize(str) + return str:gsub("^%l", string.upper) +end - local fast = {times=faster, uses=def.uses, maxlevel=def.maxlvl} - local long = {times=def.times, uses=longer, maxlevel=def.maxlvl} + -- Higher number = longer use. +local use_factor = 1.2 +-- Higher number = faster dig. +local times_subtractor = 0.1 - local enchtools = { - {"axe", "durable", {choppy = long}}, {"axe", "fast", {choppy = fast}}, - {"pick", "durable", {cracky = long}}, {"pick", "fast", {cracky = fast}}, - {"shovel", "durable", {crumbly = long}}, {"shovel", "fast", {crumbly = fast}} - } - for i = 1, #enchtools do - local x = enchtools[i] - local t, e, g = x[1], x[2], x[3] - minetest.register_tool("xdecor:enchanted_"..t.."_"..m.."_"..e, { - description = "Enchanted "..m:gsub("%l", string.upper, 1).." ".. - t:gsub("%l", string.upper, 1).." ("..e:gsub("%l", string.upper, 1)..")", - inventory_image = minetest.registered_tools["default:"..t.."_"..m].inventory_image, - wield_image = minetest.registered_tools["default:"..t.."_"..m].wield_image, - groups = {not_in_creative_inventory=1}, - tool_capabilities = {groupcaps = g, damage_groups = def.dmg} - }) +function enchanting.register_enchtools() + local materials = {"steel", "bronze", "mese", "diamond"} + local tools = { {"axe", "choppy"}, {"pick", "cracky"}, {"shovel", "crumbly"} } + local chants = { "durable", "fast" } + for j = 1, #materials do + local material = materials[j] + for t = 1, #tools do + local tool_name = tools[t][1] + local original_tool = minetest.registered_tools["default:"..tool_name.."_"..material] + local original_groupcaps = original_tool.tool_capabilities.groupcaps + local main_groupcap = tools[t][2] + + for i = 1, #chants do + local chant = chants[i] + + local groupcaps = table.copy(original_groupcaps) + if chant == "durable" then + groupcaps[main_groupcap].uses = original_groupcaps[main_groupcap].uses * use_factor + elseif chant == "fast" then + for i = 1, 3 do + groupcaps[main_groupcap].times[i] = original_groupcaps[main_groupcap].times[i] - times_subtractor + end + end + + minetest.register_tool(string.format("xdecor:enchanted_%s_%s_%s", tool_name, material, chant), { + description = string.format("Enchanted %s %s (%s)", capitalize(material), capitalize(tool_name), capitalize(chant)), + inventory_image = original_tool.inventory_image, + wield_image = original_tool.wield_image, + groups = {not_in_creative_inventory=1}, + tool_capabilities = {groupcaps = groupcaps, damage_groups = original_tool.damage_groups} + }) + end + end end end -local tools = { - {"axe", "choppy"}, {"pick", "cracky"}, {"shovel", "crumbly"} -} -local materials = {"steel", "bronze", "mese", "diamond"} +enchanting.register_enchtools() -for i = 1, #tools do -for j = 1, #materials do - local t, m = tools[i], materials[j] - local toolname = t[1].."_"..m - local init_def = minetest.registered_tools["default:"..toolname].tool_capabilities.groupcaps[t[2]] - - local tooldef = { - times = init_def.times, - uses = init_def.uses, - dmg = init_def.damage_groups, - maxlvl = init_def.maxlevel - } - enchanting.register_enchtools(init_def, m, tooldef) -end -end