]> git.lizzy.rs Git - xdecor.git/blobdiff - enchanting.lua
Slight tweaking of iron lightbox texture
[xdecor.git] / enchanting.lua
index 820e5b95019985fca447d2bff4ca2196f3e05377..44ecd7deaa4f586dea988ba59b240dd64b77da4f 100644 (file)
 local enchanting = {}
+screwdriver = screwdriver or {}
+
+function enchanting.formspec(pos, tool)
+       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 inflicts more damage]
+                       tooltip[durable;Your tool is more resistant]
+                       tooltip[fast;Your tool is more powerful]
+                       tooltip[strong;Your armor is more resistant]
+                       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
 
-function enchanting.construct(pos)
-       local meta = minetest.get_meta(pos)
-       local xbg = default.gui_bg..default.gui_bg_img..default.gui_slots
-       local concat = table.concat
-
-       local f = {"size[8,7;]"..xbg..
-               "label[0.85,-0.15;Enchant]".."image[0.6,0.2;2,2;xdecor_enchbook.png]"..
-               "list[current_name;tool;0.5,2;1,1;]"..
-               "list[current_name;mese;1.5,2;1,1;]".."image[1.5,2;1,1;mese_layout.png]"..
-               "image_button[2.75,0;5,1.5;ench_bg.png;durable;Durable]"..
-               "image_button[2.75,1.5;5,1.5;ench_bg.png;fast;Fast]"..
-               "list[current_player;main;0,3.3;8,4;]"}
-       local formspec = concat(f)
-
-       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)
+       minetest.get_meta(pos):set_string("formspec", formspec)
 end
 
-function enchanting.is_allowed(toolname)
-       local tdef = minetest.registered_tools[toolname]
-       if tdef and toolname:find("default:") and not toolname:find("sword") and not
-                       toolname:find("stone") and not toolname:find("wood") then
-               return 1
-       else return 0 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 name:gmatch("[%w_]+") do
+                       if stack:get_name():find(n) then
+                               enchanting.formspec(pos, cat)
+                       end
+               end
+               end
+       end
 end
 
-function enchanting.fields(pos, _, fields, _)
-       local meta = minetest.get_meta(pos)
-       local inv = meta:get_inventory()
-       local toolstack = inv:get_stack("tool", 1)
-       local mesestack = inv:get_stack("mese", 1)
-       local toolname = toolstack:get_name()
-       local toolwear = toolstack:get_wear()
-       local mese = mesestack:get_count()
-       local ench = dump(fields):match("%w+")
-
-       if enchanting.is_allowed(toolname) ~= 0 and mese > 0 and
-                       fields[ench] and ench ~= "quit" then
-               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)
+function enchanting.fields(pos, _, fields)
+       if fields.quit then return end
+       local inv = minetest.get_meta(pos):get_inventory()
+       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() > 0 and minetest.registered_tools[enchanted_tool] then
+               tool:replace(enchanted_tool)
+               tool:add_wear(orig_wear)
+               mese:take_item()
+               inv:set_stack("mese", 1, mese)
+               inv:set_stack("tool", 1, tool)
        end
 end
 
-function enchanting.dig(pos, _)
-       local meta = minetest.get_meta(pos)
-       local inv = meta:get_inventory()
+function enchanting.dig(pos)
+       local inv = minetest.get_meta(pos):get_inventory()
+       return inv:is_empty("tool") and inv:is_empty("mese")
+end
 
-       if not inv:is_empty("tool") or not inv:is_empty("mese") then
-               return false
+local function allowed(tool)
+       for item in pairs(minetest.registered_tools) do
+               if item:match("enchanted_"..tool) then return true end
        end
-       return true
+       return false
 end
 
-function enchanting.put(_, listname, _, stack, _)
-       local toolname = stack:get_name()
-       local count = stack:get_count()
-
-       if listname == "mese" then
-               if toolname == "default:mese_crystal" then return count
-               else return 0 end
+function enchanting.put(_, listname, _, stack)
+       local item = stack:get_name():match("[^:]+$")
+       if listname == "mese" and item == "mese_crystal" then
+               return stack:get_count()
+       elseif listname == "tool" and allowed(item) then
+               return 1 
        end
+
+       return 0
+end
+
+function enchanting.on_take(pos, listname)
        if listname == "tool" then
-               return enchanting.is_allowed(toolname)
+               enchanting.formspec(pos, nil)
        end
-       return count
+end
+
+function enchanting.construct(pos)
+       local meta = minetest.get_meta(pos)
+       meta:set_string("infotext", "Enchantment Table")
+       enchanting.formspec(pos, nil)
+
+       local inv = meta:get_inventory()
+       inv:set_size("tool", 1)
+       inv:set_size("mese", 1)
 end
 
 xdecor.register("enchantment_table", {
@@ -81,59 +114,119 @@ xdecor.register("enchantment_table", {
                "xdecor_enchantment_side.png", "xdecor_enchantment_side.png",
                "xdecor_enchantment_side.png", "xdecor_enchantment_side.png"
        },
-       groups = {cracky=1},
+       groups = {cracky=1, oddly_breakable_by_hand=1, level=1},
        sounds = default.node_sound_stone_defaults(),
+       on_rotate = screwdriver.rotate_simple,
        can_dig = enchanting.dig,
        on_construct = enchanting.construct,
        on_receive_fields = enchanting.fields,
+       on_metadata_inventory_put = enchanting.on_put,
+       on_metadata_inventory_take = enchanting.on_take,
        allow_metadata_inventory_put = enchanting.put,
-       allow_metadata_inventory_move = function(...) return 0 end
+       allow_metadata_inventory_move = function() return 0 end
 })
 
-local function capitalize(str)
+local function cap(str)
        return str:gsub("^%l", string.upper)
 end
 
- -- Higher number = longer use.
-local use_factor = 1.2
--- Higher number = faster dig.
-local times_subtractor = 0.1
-
-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]
+ -- 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"}
+       }
+}
+
+for mod, defs in pairs(tools) do
+for material in defs[1]:gmatch("[%w_]+") do
+for _, tooldef in next, defs, 1 do
+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
+               if mod == "default" then
+                       local original_damage_groups = original_tool.tool_capabilities.damage_groups
                        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
+                       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
+
+                       if enchant == "durable" then
+                               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] - enchanting.times
                                end
+                       elseif enchant == "sharp" then
+                               fleshy = fleshy + enchanting.damages
+                       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}
-                               })
+                       minetest.register_tool(":"..mod..":enchanted_"..tool.."_"..material.."_"..enchant, {
+                               description = "Enchanted "..cap(material).." "..cap(tool).." ("..cap(enchant)..")",
+                               inventory_image = original_tool.inventory_image.."^[colorize:violet:50",
+                               wield_image = original_tool.wield_image,
+                               groups = {not_in_creative_inventory=1},
+                               tool_capabilities = {
+                                       groupcaps = groupcaps, damage_groups = {fleshy = fleshy},
+                                       full_punch_interval = full_punch_interval, max_drop_level = max_drop_level
+                               }
+                       })
+               end
+
+               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
+
+                       for armor_group, value in pairs(original_armor_groups) do
+                               if enchant == "strong" then
+                                       armorcaps[armor_group] = math.ceil(value * enchanting.strength)
+                               elseif enchant == "speed" then
+                                       armorcaps[armor_group] = value
+                                       armorcaps.physics_speed = enchanting.speed
+                                       armorcaps.physics_jump = enchanting.jump
+                               end
                        end
+
+                       minetest.register_tool(":"..mod..":enchanted_"..tool.."_"..material.."_"..enchant, {
+                               description = "Enchanted "..cap(material).." "..cap(tool).." ("..cap(enchant)..")",
+                               inventory_image = original_tool.inventory_image,
+                               texture = "3d_armor_"..tool.."_"..material,
+                               wield_image = original_tool.wield_image,
+                               groups = armorcaps,
+                               wear = 0
+                       })
                end
        end
 end
-
-enchanting.register_enchtools()
+end
+end
+end