]> git.lizzy.rs Git - xdecor.git/blobdiff - src/enchanting.lua
Global code style cleaning
[xdecor.git] / src / enchanting.lua
index 5e30d59738b8072b3864e9e67f26b9f55e192437..744dab41471d3f393068fa305f5e83ac20b8b479 100644 (file)
@@ -1,5 +1,6 @@
 screwdriver = screwdriver or {}
 local ceil, abs, random = math.ceil, math.abs, math.random
+local reg_tools = minetest.registered_tools
 
 -- Cost in Mese crystal(s) for enchanting.
 local mese_cost = 1
@@ -11,13 +12,17 @@ local enchanting = {
        damages  = 1,    -- Sharpness
 }
 
-local function cap(S) return S:gsub("^%l", string.upper) end
+local function cap(S) return
+       S:gsub("^%l", string.upper)
+end
+
 local function to_percent(orig_value, final_value)
        return abs(ceil(((final_value - orig_value) / orig_value) * 100))
 end
 
 function enchanting:get_tooltip(enchant, orig_caps, fleshy)
-       local bonus = {durable=0, efficiency=0, damages=0}
+       local bonus = {durable = 0, efficiency = 0, damages = 0}
+
        if orig_caps then
                bonus.durable = to_percent(orig_caps.uses, orig_caps.uses * enchanting.uses)
                local sum_caps_times = 0
@@ -28,23 +33,25 @@ function enchanting:get_tooltip(enchant, orig_caps, fleshy)
                bonus.efficiency = to_percent(average_caps_time, average_caps_time -
                                              enchanting.times)
        end
+
        if fleshy then
                bonus.damages = to_percent(fleshy, fleshy + enchanting.damages)
        end
 
        local specs = { -- not finished, to complete
-               durable = {"#00baff", " (+"..bonus.durable.."%)"},
-               fast    = {"#74ff49", " (+"..bonus.efficiency.."%)"},
-               sharp   = {"#ffff00", " (+"..bonus.damages.."%)"},
+               durable = {"#00baff", " (+" .. bonus.durable .. "%)"},
+               fast    = {"#74ff49", " (+" .. bonus.efficiency .. "%)"},
+               sharp   = {"#ffff00", " (+" .. bonus.damages .. "%)"},
        }
+
        return minetest.colorize and minetest.colorize(specs[enchant][1],
-                       "\n"..cap(enchant)..specs[enchant][2]) or
-                       "\n"..cap(enchant)..specs[enchant][2]
+                       "\n" .. cap(enchant) .. specs[enchant][2]) or
+                       "\n" .. cap(enchant) .. specs[enchant][2]
 end
 
 local enchant_buttons = {
-       [[ 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;fast;Efficiency]" ..
+       "image_button[3.9,1.77;4,1.12;bg_btn.png;durable;Durability]",
        "image_button[3.9,2.9;4,0.92;bg_btn.png;sharp;Sharpness]",
 }
 
@@ -63,10 +70,10 @@ function enchanting.formspec(pos, num)
                        image[2,2.9;1,1;mese_layout.png]
                        tooltip[sharp;Your weapon inflicts more damages]
                        tooltip[durable;Your tool last longer]
-                       tooltip[fast;Your tool digs faster] ]]
-                       ..default.gui_slots..default.get_hotbar_bg(0.5,4.5)
+                       tooltip[fast;Your tool digs faster] ]] ..
+                       default.gui_slots .. default.get_hotbar_bg(0.5,4.5)
 
-       formspec = formspec..(enchant_buttons[num] or "")
+       formspec = formspec .. (enchant_buttons[num] or "")
        meta:set_string("formspec", formspec)
 end
 
@@ -78,7 +85,7 @@ function enchanting.on_put(pos, listname, _, stack)
                        "sword",
                }
 
-               for idx, tools in pairs(tool_groups) do
+               for idx, tools in ipairs(tool_groups) do
                        if tools:find(stackname:match(":(%w+)")) then
                                enchanting.formspec(pos, idx)
                        end
@@ -93,11 +100,14 @@ function enchanting.fields(pos, _, fields, sender)
        local mese = inv:get_stack("mese", 1)
        local orig_wear = tool:get_wear()
        local mod, name = tool:get_name():match("(.*):(.*)")
-       local enchanted_tool = (mod or "")..":enchanted_"..(name or "").."_"..next(fields)
+       local enchanted_tool = (mod or "") .. ":enchanted_" .. (name or "") .. "_" .. next(fields)
 
-       if mese:get_count() >= mese_cost and minetest.registered_tools[enchanted_tool] then
+       if mese:get_count() >= mese_cost and reg_tools[enchanted_tool] then
                minetest.sound_play("xdecor_enchanting", {
-                       to_player=sender:get_player_name(), gain=0.8})
+                       to_player = sender:get_player_name(),
+                       gain = 0.8
+               })
+
                tool:replace(enchanted_tool)
                tool:add_wear(orig_wear)
                mese:take_item(mese_cost)
@@ -112,11 +122,13 @@ function enchanting.dig(pos)
 end
 
 local function allowed(tool)
-       if not tool then return false end
-       for item in pairs(minetest.registered_tools) do
-               if item:find("enchanted_"..tool) then return true end
+       if not tool then return end
+
+       for item in pairs(reg_tools) do
+               if item:find("enchanted_" .. tool) then
+                       return true
+               end
        end
-       return false
 end
 
 function enchanting.put(_, listname, _, stack)
@@ -126,23 +138,26 @@ function enchanting.put(_, listname, _, stack)
        elseif listname == "tool" and allowed(stackname:match("[^:]+$")) 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)
+       end
 end
 
 function enchanting.construct(pos)
        local meta = minetest.get_meta(pos)
        meta:set_string("infotext", "Enchantment Table")
-       enchanting.formspec(pos, nil)
+       enchanting.formspec(pos)
 
        local inv = meta:get_inventory()
        inv:set_size("tool", 1)
        inv:set_size("mese", 1)
 
-       minetest.add_entity({x=pos.x, y=pos.y+0.85, z=pos.z}, "xdecor:book_open")
+       minetest.add_entity({x = pos.x, y = pos.y + 0.85, z = pos.z}, "xdecor:book_open")
        local timer = minetest.get_node_timer(pos)
        timer:start(0.5)
 end
@@ -160,39 +175,45 @@ end
 function enchanting.timer(pos)
        local num = #minetest.get_objects_inside_radius(pos, 0.9)
        if num == 0 then
-               minetest.add_entity({x=pos.x, y=pos.y+0.85, z=pos.z}, "xdecor:book_open")
+               minetest.add_entity({x = pos.x, y = pos.y + 0.85, z = pos.z}, "xdecor:book_open")
        end
 
-       local minp = {x=pos.x-2, y=pos.y, z=pos.z-2}
-       local maxp = {x=pos.x+2, y=pos.y+1, z=pos.z+2}
+       local minp = {x = pos.x - 2, y = pos.y,     z = pos.z - 2}
+       local maxp = {x = pos.x + 2, y = pos.y + 1, z = pos.z + 2}
+
        local bookshelves = minetest.find_nodes_in_area(minp, maxp, "default:bookshelf")
-       if #bookshelves == 0 then return true end
+       if #bookshelves == 0 then
+               return true
+       end
 
        local bookshelf_pos = bookshelves[random(1, #bookshelves)]
        local x = pos.x - bookshelf_pos.x
        local y = bookshelf_pos.y - pos.y
        local z = pos.z - bookshelf_pos.z
 
-       if tostring(x..z):find(2) then
+       if tostring(x .. z):find(2) then
                minetest.add_particle({
                        pos = bookshelf_pos,
-                       velocity = {x=x, y=2-y, z=z},
-                       acceleration = {x=0, y=-2.2, z=0},
+                       velocity = {x = x, y = 2 - y, z = z},
+                       acceleration = {x = 0, y = -2.2, z = 0},
                        expirationtime = 1,
                        size = 1.5,
                        glow = 5,
-                       texture = "xdecor_glyph"..random(1,18)..".png"
+                       texture = "xdecor_glyph" .. random(1,18) .. ".png"
                })
        end
+
        return true
 end
 
 xdecor.register("enchantment_table", {
        description = "Enchantment Table",
-       tiles = {"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, level=1},
+       tiles = {
+               "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, level = 1},
        light_source = 6,
        sounds = default.node_sound_stone_defaults(),
        on_rotate = screwdriver.rotate_simple,
@@ -204,7 +225,9 @@ xdecor.register("enchantment_table", {
        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,
 })
 
 minetest.register_entity("xdecor:book_open", {
@@ -215,7 +238,7 @@ minetest.register_entity("xdecor:book_open", {
        textures = {"xdecor_book_open.png"},
        on_activate = function(self)
                local pos = self.object:getpos()
-               local pos_under = {x=pos.x, y=pos.y-1, z=pos.z}
+               local pos_under = {x = pos.x, y = pos.y - 1, z = pos.z}
 
                if minetest.get_node(pos_under).name ~= "xdecor:enchantment_table" then
                        self.object:remove()
@@ -227,7 +250,7 @@ function enchanting:register_tools(mod, def)
        for tool in pairs(def.tools) do
        for material in def.materials:gmatch("[%w_]+") do
        for enchant in def.tools[tool].enchants:gmatch("[%w_]+") do
-               local original_tool = minetest.registered_tools[mod..":"..tool.."_"..material]
+               local original_tool = reg_tools[mod .. ":" .. tool .. "_" .. material]
                if not original_tool then break end
                local original_toolcaps = original_tool.tool_capabilities
 
@@ -251,12 +274,12 @@ function enchanting:register_tools(mod, def)
                                fleshy = fleshy + enchanting.damages
                        end
 
-                       minetest.register_tool(":"..mod..":enchanted_"..tool.."_"..material.."_"..enchant, {
-                               description = "Enchanted "..cap(material).." "..cap(tool)..
+                       minetest.register_tool(":" .. mod .. ":enchanted_" .. tool .. "_" .. material .. "_" .. enchant, {
+                               description = "Enchanted " .. cap(material) .. " " .. cap(tool) ..
                                        self:get_tooltip(enchant, original_groupcaps[group], fleshy),
-                               inventory_image = original_tool.inventory_image.."^[colorize:violet:50",
+                               inventory_image = original_tool.inventory_image .. "^[colorize:violet:50",
                                wield_image = original_tool.wield_image,
-                               groups = {not_in_creative_inventory=1},
+                               groups = {not_in_creative_inventory = 1},
                                tool_capabilities = {
                                        groupcaps = groupcaps, damage_groups = {fleshy = fleshy},
                                        full_punch_interval = full_punch_interval,
@@ -288,4 +311,4 @@ minetest.register_craft({
                {"default:diamond", "default:obsidian", "default:diamond"},
                {"default:obsidian", "default:obsidian", "default:obsidian"}
        }
-})
\ No newline at end of file
+})