]> git.lizzy.rs Git - xdecor.git/blobdiff - enchanting.lua
Craft guide : extend search function to descriptions
[xdecor.git] / enchanting.lua
index 75ae0ff6a20c201bcf3125db2fff90612c4e4674..16e0eae67b6b2e5cc8a3a310af2c6c6a2b56f220 100644 (file)
@@ -1,8 +1,43 @@
 local enchanting = {}
 screwdriver = screwdriver or {}
 
-function enchanting.formspec(pos, tooltype)
-       local meta = minetest.get_meta(pos)
+-- 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.
+-- Available enchantments: durable, fast, sharp, strong, speed.
+enchanting.tools = {
+       --[[ Registration format:
+               [Mod name] = {
+                       materials,
+                       {tool name, enchantments}
+                }
+       ]]
+       ["default"] = {
+               "steel, bronze, mese, diamond",
+               {"axe",    "durable, fast"}, 
+               {"pick",   "durable, fast"}, 
+               {"shovel", "durable, fast"},
+               {"sword",  "sharp"}
+       },
+       ["3d_armor"] = {
+               "steel, bronze, gold, diamond",
+               {"boots",      "strong, speed"},
+               {"chestplate", "strong"},
+               {"helmet",     "strong"},
+               {"leggings",   "strong"}
+       }
+}
+
+function enchanting.formspec(pos, num)
        local formspec = [[ size[9,9;]
                        bgcolor[#080808BB;true]
                        background[0,0;9,9;ench_ui.png]
@@ -17,32 +52,26 @@ function enchanting.formspec(pos, tooltype)
                        tooltip[speed;Your speed is increased] ]]
                        ..default.gui_slots..default.get_hotbar_bg(0.5,4.5)
 
-       if tooltype == "sword" then
-               formspec = formspec.."image_button[3.9,2.9;4,0.92;bg_btn.png;sharp;Sharpness]"
-       elseif tooltype == "tool" then
-               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
-               formspec = formspec.."image_button[3.9,0.85;4,0.92;bg_btn.png;strong;Strength]"
-       elseif tooltype == "boots" then
-               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
+       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] ]] }
 
-       meta:set_string("formspec", formspec)
+       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 stn = stack:get_name()
-               if stn:find("pick") or stn:find("axe") or stn:find("shovel") then
-                       enchanting.formspec(pos, "tool")
-               elseif stn:find("sword") then
-                       enchanting.formspec(pos, "sword")
-               elseif stn:find("chestplate") or stn:find("leggings") or stn:find("helmet") then
-                       enchanting.formspec(pos, "armor")
-               elseif stn:find("boots") then
-                       enchanting.formspec(pos, "boots")
+               for k, v in pairs({"axe, pick, shovel",
+                               "chestplate, leggings, helmet",
+                               "sword", "boots"}) do
+                       if v:find(stack:get_name():match(":(%w+)")) then
+                               enchanting.formspec(pos, k)
+                       end
                end
        end
 end
@@ -50,21 +79,18 @@ end
 function enchanting.fields(pos, _, fields)
        if fields.quit then return end
        local inv = minetest.get_meta(pos):get_inventory()
-       local toolstack = inv:get_stack("tool", 1)
-       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()
-       local mese = mesestack:get_count()
-       local ench = next(fields)
-       local enchanted_tool = mod..":enchanted_"..tool.."_"..ench
-
-       if mese > 0 and minetest.registered_tools[enchanted_tool] then
-               toolstack:replace(enchanted_tool)
-               toolstack:add_wear(toolwear)
-               mesestack:take_item()
-               inv:set_stack("mese", 1, mesestack)
-               inv:set_stack("tool", 1, toolstack)
+       local tool = inv:get_stack("tool", 1)
+       local mese = inv:get_stack("mese", 1)
+       local orig_wear = tool:get_wear()
+       local mod, name = tool:get_name():match("(.*):(.*)")
+       local enchanted_tool = mod..":enchanted_"..name.."_"..next(fields)
+
+       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_cost)
+               inv:set_stack("mese", 1, mese)
+               inv:set_stack("tool", 1, tool)
        end
 end
 
@@ -75,30 +101,23 @@ end
 
 local function allowed(tool)
        for item in pairs(minetest.registered_tools) do
-               if item:match("enchanted_"..tool) then
-                       return true
-               end
+               if item:find("enchanted_"..tool) then return true end
        end
        return false
 end
 
 function enchanting.put(_, listname, _, stack)
-       local toolstack = stack:get_name()
-       local toolname = toolstack:match("[%w_]+:([%w_]+)")
-
-       if listname == "mese" and toolstack == "default:mese_crystal" then
+       local item = stack:get_name():match("[^:]+$")
+       if listname == "mese" and item == "mese_crystal" then
                return stack:get_count()
-       elseif listname == "tool" and allowed(toolname) then
+       elseif listname == "tool" and allowed(item) then
                return 1 
        end
-
        return 0
 end
 
 function enchanting.on_take(pos, listname)
-       if listname == "tool" then
-               enchanting.formspec(pos, nil)
-       end
+       if listname == "tool" then enchanting.formspec(pos, nil) end
 end
 
 function enchanting.construct(pos)
@@ -130,65 +149,33 @@ 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.
-local use_factor = 1.2
-local times_subtractor = 0.1
-local damage_adder = 1
-local strength_factor = 1.2
-local speed_factor = 0.2
-local jump_factor = 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[2]:gmatch("[%w_]+") do
+       local tool, group = tooldef[1], ""
        local original_tool = minetest.registered_tools[mod..":"..tool.."_"..material]
 
        if original_tool then
-               if mod == "default" then
+               if original_tool.tool_capabilities 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
                        local full_punch_interval = original_tool.tool_capabilities.full_punch_interval
                        local max_drop_level = original_tool.tool_capabilities.max_drop_level
+                       group = tostring(next(original_groupcaps))
 
                        if enchant == "durable" then
-                               groupcaps[group].uses = math.ceil(original_groupcaps[group].uses * use_factor)
+                               groupcaps[group].uses = math.ceil(original_groupcaps[group].uses * enchanting.uses)
                        elseif enchant == "fast" then
                                for i = 1, 3 do
-                                       groupcaps[group].times[i] = original_groupcaps[group].times[i] - times_subtractor
+                                       groupcaps[group].times[i] = original_groupcaps[group].times[i] - enchanting.times
                                end
                        elseif enchant == "sharp" then
-                               fleshy = fleshy + damage_adder
+                               fleshy = fleshy + enchanting.damages
                        end
 
                        minetest.register_tool(":"..mod..":enchanted_"..tool.."_"..material.."_"..enchant, {
@@ -205,17 +192,16 @@ for _, ench in pairs(tooldef[3]) do
 
                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
+                       armorcaps.not_in_creative_inventory = 1
 
                        for armor_group, value in pairs(original_armor_groups) do
                                if enchant == "strong" then
-                                       armorcaps[armor_group] = math.ceil(value * strength_factor)
+                                       armorcaps[armor_group] = math.ceil(value * enchanting.strength)
                                elseif enchant == "speed" then
                                        armorcaps[armor_group] = value
-                                       armorcaps.physics_speed = speed_factor
-                                       armorcaps.physics_jump = jump_factor
+                                       armorcaps.physics_speed = enchanting.speed
+                                       armorcaps.physics_jump = enchanting.jump
                                end
                        end