-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
+