]> git.lizzy.rs Git - xdecor.git/blobdiff - enchanting.lua
Move and regroup enchanting params and registrations on top of page
[xdecor.git] / enchanting.lua
index df7961db9bd3b4a703cfb25031b4739cc04c8241..15aaad69ae8c4e7d24c44bd2a61bb8922440db51 100644 (file)
@@ -1,7 +1,42 @@
 local enchanting = {}
 screwdriver = screwdriver or {}
 
-function enchanting.formspec(pos, tool)
+-- Cost in Mese crystal(s) for enchanting.
+local mese_cost = 1
+
+-- Force of the enchantments.
+enchanting.uses = 1.2
+enchanting.times = 0.1
+enchanting.damages = 1
+enchanting.strength = 1.2
+enchanting.speed = 0.2
+enchanting.jump = 0.2
+
+-- Enchanted tools registration.
+enchanting.tools = {
+       --[[ Registration format:
+               [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"}
+       }
+}
+
+function enchanting.formspec(pos, num)
        local formspec = [[ size[9,9;]
                        bgcolor[#080808BB;true]
                        background[0,0;9,9;ench_ui.png]
@@ -16,37 +51,27 @@ function enchanting.formspec(pos, tool)
                        tooltip[speed;Your speed is increased] ]]
                        ..default.gui_slots..default.get_hotbar_bg(0.5,4.5)
 
-       local tool_fs = {
-               ["tool"] = [[ 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] ]],
-               ["armor"] = "image_button[3.9,0.85;4,0.92;bg_btn.png;strong;Strength]",
-               ["sword"] = "image_button[3.9,2.9;4,0.92;bg_btn.png;sharp;Sharpness]",
-               ["boots"] = [[ 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] ]] }
-
-       for cat in pairs(tool_fs) do
-               if tool == cat then
-                       formspec = formspec..tool_fs[cat]
-               end
-       end
+       local tool_enchs = {
+               [[ 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] ]],
+               "image_button[3.9,0.85;4,0.92;bg_btn.png;strong;Strength]",
+               "image_button[3.9,2.9;4,0.92;bg_btn.png;sharp;Sharpness]",
+               [[ 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] ]] }
 
+       formspec = formspec..(tool_enchs[num] or "")
        minetest.get_meta(pos):set_string("formspec", formspec)
 end
 
 function enchanting.on_put(pos, listname, _, stack)
        if listname == "tool" then
-               local tools_cat = {
-                       ["tool"] = {"pick", "axe", "shovel"},
-                       ["armor"] = {"chestplate", "leggings", "helmet"},
-                       ["sword"] = {"sword"}, ["boots"] = {"boots"} }
-
-               for cat, name in pairs(tools_cat) do
-               for _, n in pairs(name) do
-                       if stack:get_name():find(n) then
-                               enchanting.formspec(pos, cat)
+               for k, v in pairs({"axe, pick, shovel",
+                               "chestplate, leggings, helmet",
+                               "sword", "boots"}) do
+                       if v:match(stack:get_name():match(":(.-)%_")) then
+                               enchanting.formspec(pos, k)
                        end
                end
-               end
        end
 end
 
@@ -59,10 +84,10 @@ function enchanting.fields(pos, _, fields)
        local mod, name = tool:get_name():match("(.*):(.*)")
        local enchanted_tool = mod..":enchanted_"..name.."_"..next(fields)
 
-       if mese:get_count() > 0 and minetest.registered_tools[enchanted_tool] then
+       if mese:get_count() >= mese_cost and minetest.registered_tools[enchanted_tool] then
                tool:replace(enchanted_tool)
                tool:add_wear(orig_wear)
-               mese:take_item()
+               mese:take_item(mese_cost)
                inv:set_stack("mese", 1, mese)
                inv:set_stack("tool", 1, tool)
        end
@@ -75,9 +100,7 @@ end
 
 local function allowed(tool)
        for item in pairs(minetest.registered_tools) do
-               if item:match("enchanted_"..tool) then
-                       return true
-               end
+               if item:match("enchanted_"..tool) then return true end
        end
        return false
 end
@@ -89,7 +112,6 @@ function enchanting.put(_, listname, _, stack)
        elseif listname == "tool" and allowed(item) then
                return 1 
        end
-
        return 0
 end
 
@@ -128,46 +150,13 @@ xdecor.register("enchantment_table", {
        allow_metadata_inventory_move = function() return 0 end
 })
 
-local function cap(str)
-       return str:gsub("^%l", string.upper)
-end
-
- -- Higher number = stronger enchant.
-enchanting.uses = 1.2
-enchanting.times = 0.1
-enchanting.damages = 1
-enchanting.strength = 1.2
-enchanting.speed = 0.2
-enchanting.jump = 0.2
-
-local tools = {
-       --[[ Registration format :
-               [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"}}
-       }
-}
+local function cap(S) return S:gsub("^%l", string.upper) end
 
-for mod, defs in pairs(tools) do
-for _, mat in pairs(defs[1]) do
+for mod, defs in pairs(enchanting.tools) do
+for material in defs[1]:gmatch("[%w_]+") 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
+for enchant in tooldef[3]:gmatch("[%w_]+") do
+       local tool, group = tooldef[1], tooldef[2]
        local original_tool = minetest.registered_tools[mod..":"..tool.."_"..material]
 
        if original_tool then