]> git.lizzy.rs Git - mcl_enchanting.git/blobdiff - init.lua
Backport changes from xdecor
[mcl_enchanting.git] / init.lua
index 201fd72ba7d496ac601ff40c6acdb7aed46ae317..ca80faaf336329c64d44002f37e577248005faae 100644 (file)
--- a/init.lua
+++ b/init.lua
@@ -1,16 +1,49 @@
-local enchanting = {}
 screwdriver = screwdriver or {}
+local ceil, abs, random = math.ceil, math.abs, math.random
 
 -- Cost in Mese crystal(s) for enchanting.
 local mese_cost = 1
 
 -- Force of the enchantments.
-enchanting.uses     = 1.2  -- Durability
-enchanting.times    = 0.1  -- Efficiency
-enchanting.damages  = 1    -- Sharpness
-enchanting.strength = 1.2  -- Armor strength (3d_armor only)
-enchanting.speed    = 0.2  -- Player speed (3d_armor only)
-enchanting.jump     = 0.2  -- Player jumping (3d_armor only)
+local enchanting = {
+       uses     = 1.2,  -- Durability
+       times    = 0.1,  -- Efficiency
+       damages  = 1,    -- Sharpness
+       strength = 1.2,  -- Armor strength (3d_armor only)
+       speed    = 0.2,  -- Player speed (3d_armor only)
+       jump     = 0.2   -- Player jumping (3d_armor only)
+}
+
+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}
+       if orig_caps then
+               bonus.durable = to_percent(orig_caps.uses, orig_caps.uses * enchanting.uses)
+               local sum_caps_times = 0
+               for i=1, #orig_caps.times do
+                       sum_caps_times = sum_caps_times + orig_caps.times[i]
+               end
+               local average_caps_time = sum_caps_times / #orig_caps.times
+               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.."%)"},
+               strong  = {"#ff3d3d", ""},
+               speed   = {"#fd5eff", ""}
+       }
+       return minetest.colorize(specs[enchant][1], "\n"..cap(enchant)..specs[enchant][2])
+end
+
 
 function enchanting.formspec(pos, num)
        local meta = minetest.get_meta(pos)
@@ -58,8 +91,10 @@ function enchanting.on_put(pos, listname, _, stack)
        end
 end
 
-function enchanting.fields(pos, _, fields)
-       if fields.quit then return end
+function enchanting.fields(pos, _, fields, sender)
+       if not next(fields) or 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)
@@ -68,6 +103,7 @@ function enchanting.fields(pos, _, fields)
        local enchanted_tool = (mod or "")..":enchanted_"..(name or "").."_"..next(fields)
 
        if mese:get_count() >= mese_cost and minetest.registered_tools[enchanted_tool] then
+               minetest.sound_play("xdecor_enchanting", {to_player=sender:get_player_name(), gain=0.8})
                tool:replace(enchanted_tool)
                tool:add_wear(orig_wear)
                mese:take_item(mese_cost)
@@ -82,6 +118,7 @@ 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
        end
@@ -113,7 +150,7 @@ function enchanting.construct(pos)
 
        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(15.0)
+       timer:start(5.0)
 end
 
 function enchanting.destruct(pos)
@@ -126,12 +163,31 @@ function enchanting.destruct(pos)
 end
 
 function enchanting.timer(pos)
-       local node = minetest.get_node(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")
        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 bookshelves = minetest.find_nodes_in_area(minp, maxp, "default:bookshelf")
+       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
+               minetest.add_particle({
+                       pos = bookshelf_pos,
+                       velocity = {x=x, y=2-y, z=z},
+                       acceleration = {x=0, y=-2.2, z=0},
+                       expirationtime = 1,
+                       size = 2,
+                       texture = "xdecor_glyph"..random(1,18)..".png"
+               })
+       end
        return true
 end
 
@@ -161,7 +217,15 @@ minetest.register_entity(":xdecor:book_open", {
        visual_size = {x=0.75, y=0.75},
        collisionbox = {0},
        physical = false,
-       textures = {"book_open.png"}
+       textures = {"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}
+
+               if minetest.get_node(pos_under).name ~= "xdecor:enchantment_table" then
+                       self.object:remove()
+               end
+       end
 })
 
 minetest.register_craft({
@@ -173,14 +237,12 @@ minetest.register_craft({
        }
 })
 
-local function cap(S) return S:gsub("^%l", string.upper) end
-
 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]
-               if not original_tool then return end
+               if not original_tool then break end
 
                if original_tool.tool_capabilities then
                        local original_damage_groups = original_tool.tool_capabilities.damage_groups
@@ -192,7 +254,7 @@ function enchanting:register_tools(mod, def)
                        local group = next(original_groupcaps)
 
                        if enchant == "durable" then
-                               groupcaps[group].uses = math.ceil(original_groupcaps[group].uses * enchanting.uses)
+                               groupcaps[group].uses = ceil(original_groupcaps[group].uses * enchanting.uses)
                        elseif enchant == "fast" then
                                for i, time in pairs(original_groupcaps[group].times) do
                                        groupcaps[group].times[i] = time - enchanting.times
@@ -202,7 +264,8 @@ function enchanting:register_tools(mod, def)
                        end
 
                        minetest.register_tool(":"..mod..":enchanted_"..tool.."_"..material.."_"..enchant, {
-                               description = "Enchanted "..cap(material).." "..cap(tool).." ("..cap(enchant)..")",
+                               description = "Enchanted "..cap(material).." "..cap(tool)..
+                                       self:get_tooltip(enchant, original_groupcaps[group], fleshy),
                                inventory_image = original_tool.inventory_image.."^[colorize:violet:50",
                                wield_image = original_tool.wield_image,
                                groups = {not_in_creative_inventory=1},
@@ -220,7 +283,7 @@ function enchanting:register_tools(mod, def)
 
                        for armor_group, value in pairs(original_armor_groups) do
                                if enchant == "strong" then
-                                       armorcaps[armor_group] = math.ceil(value * enchanting.strength)
+                                       armorcaps[armor_group] = ceil(value * enchanting.strength)
                                elseif enchant == "speed" then
                                        armorcaps[armor_group] = value
                                        armorcaps.physics_speed = enchanting.speed
@@ -229,7 +292,8 @@ function enchanting:register_tools(mod, def)
                        end
 
                        minetest.register_tool(":"..mod..":enchanted_"..tool.."_"..material.."_"..enchant, {
-                               description = "Enchanted "..cap(material).." "..cap(tool).." ("..cap(enchant)..")",
+                               description = "Enchanted "..cap(material).." "..cap(tool)..
+                                       self:get_tooltip(enchant),
                                inventory_image = original_tool.inventory_image,
                                texture = "3d_armor_"..tool.."_"..material,
                                wield_image = original_tool.wield_image,