1 screwdriver = screwdriver or {}
2 local ceil, abs, random = math.ceil, math.abs, math.random
3 local reg_tools = minetest.registered_tools
5 -- Cost in Mese crystal(s) for enchanting.
8 -- Force of the enchantments.
10 uses = 1.2, -- Durability
11 times = 0.1, -- Efficiency
12 damages = 1, -- Sharpness
15 local function cap(S) return
16 S:gsub("^%l", string.upper)
19 local function to_percent(orig_value, final_value)
20 return abs(ceil(((final_value - orig_value) / orig_value) * 100))
23 function enchanting:get_tooltip(enchant, orig_caps, fleshy)
24 local bonus = {durable = 0, efficiency = 0, damages = 0}
27 bonus.durable = to_percent(orig_caps.uses, orig_caps.uses * enchanting.uses)
28 local sum_caps_times = 0
29 for i=1, #orig_caps.times do
30 sum_caps_times = sum_caps_times + orig_caps.times[i]
32 local average_caps_time = sum_caps_times / #orig_caps.times
33 bonus.efficiency = to_percent(average_caps_time, average_caps_time -
38 bonus.damages = to_percent(fleshy, fleshy + enchanting.damages)
41 local specs = { -- not finished, to complete
42 durable = {"#00baff", " (+" .. bonus.durable .. "%)"},
43 fast = {"#74ff49", " (+" .. bonus.efficiency .. "%)"},
44 sharp = {"#ffff00", " (+" .. bonus.damages .. "%)"},
47 return minetest.colorize and minetest.colorize(specs[enchant][1],
48 "\n" .. cap(enchant) .. specs[enchant][2]) or
49 "\n" .. cap(enchant) .. specs[enchant][2]
52 local enchant_buttons = {
53 "image_button[3.9,0.85;4,0.92;bg_btn.png;fast;Efficiency]" ..
54 "image_button[3.9,1.77;4,1.12;bg_btn.png;durable;Durability]",
55 "image_button[3.9,2.9;4,0.92;bg_btn.png;sharp;Sharpness]",
58 function enchanting.formspec(pos, num)
59 local meta = minetest.get_meta(pos)
60 local formspec = [[ size[9,9;]
61 bgcolor[#080808BB;true]
62 background[0,0;9,9;ench_ui.png]
63 list[context;tool;0.9,2.9;1,1;]
64 list[context;mese;2,2.9;1,1;]
65 list[current_player;main;0.5,4.5;8,4;]
66 listring[current_player;main]
67 listring[context;tool]
68 listring[current_player;main]
69 listring[context;mese]
70 image[2,2.9;1,1;mese_layout.png]
71 tooltip[sharp;Your weapon inflicts more damages]
72 tooltip[durable;Your tool last longer]
73 tooltip[fast;Your tool digs faster] ]] ..
74 default.gui_slots .. default.get_hotbar_bg(0.5,4.5)
76 formspec = formspec .. (enchant_buttons[num] or "")
77 meta:set_string("formspec", formspec)
80 function enchanting.on_put(pos, listname, _, stack)
81 if listname == "tool" then
82 local stackname = stack:get_name()
88 for idx, tools in ipairs(tool_groups) do
89 if tools:find(stackname:match(":(%w+)")) then
90 enchanting.formspec(pos, idx)
96 function enchanting.fields(pos, _, fields, sender)
97 if not next(fields) or fields.quit then return end
98 local inv = minetest.get_meta(pos):get_inventory()
99 local tool = inv:get_stack("tool", 1)
100 local mese = inv:get_stack("mese", 1)
101 local orig_wear = tool:get_wear()
102 local mod, name = tool:get_name():match("(.*):(.*)")
103 local enchanted_tool = (mod or "") .. ":enchanted_" .. (name or "") .. "_" .. next(fields)
105 if mese:get_count() >= mese_cost and reg_tools[enchanted_tool] then
106 minetest.sound_play("xdecor_enchanting", {
107 to_player = sender:get_player_name(),
111 tool:replace(enchanted_tool)
112 tool:add_wear(orig_wear)
113 mese:take_item(mese_cost)
114 inv:set_stack("mese", 1, mese)
115 inv:set_stack("tool", 1, tool)
119 function enchanting.dig(pos)
120 local inv = minetest.get_meta(pos):get_inventory()
121 return inv:is_empty("tool") and inv:is_empty("mese")
124 local function allowed(tool)
125 if not tool then return end
127 for item in pairs(reg_tools) do
128 if item:find("enchanted_" .. tool) then
134 function enchanting.put(_, listname, _, stack)
135 local stackname = stack:get_name()
136 if listname == "mese" and stackname == "default:mese_crystal" then
137 return stack:get_count()
138 elseif listname == "tool" and allowed(stackname:match("[^:]+$")) then
145 function enchanting.on_take(pos, listname)
146 if listname == "tool" then
147 enchanting.formspec(pos)
151 function enchanting.construct(pos)
152 local meta = minetest.get_meta(pos)
153 meta:set_string("infotext", "Enchantment Table")
154 enchanting.formspec(pos)
156 local inv = meta:get_inventory()
157 inv:set_size("tool", 1)
158 inv:set_size("mese", 1)
160 minetest.add_entity({x = pos.x, y = pos.y + 0.85, z = pos.z}, "xdecor:book_open")
161 local timer = minetest.get_node_timer(pos)
165 function enchanting.destruct(pos)
166 for _, obj in pairs(minetest.get_objects_inside_radius(pos, 0.9)) do
167 if obj and obj:get_luaentity() and
168 obj:get_luaentity().name == "xdecor:book_open" then
175 function enchanting.timer(pos)
176 local num = #minetest.get_objects_inside_radius(pos, 0.9)
178 minetest.add_entity({x = pos.x, y = pos.y + 0.85, z = pos.z}, "xdecor:book_open")
181 local minp = {x = pos.x - 2, y = pos.y, z = pos.z - 2}
182 local maxp = {x = pos.x + 2, y = pos.y + 1, z = pos.z + 2}
184 local bookshelves = minetest.find_nodes_in_area(minp, maxp, "default:bookshelf")
185 if #bookshelves == 0 then
189 local bookshelf_pos = bookshelves[random(1, #bookshelves)]
190 local x = pos.x - bookshelf_pos.x
191 local y = bookshelf_pos.y - pos.y
192 local z = pos.z - bookshelf_pos.z
194 if tostring(x .. z):find(2) then
195 minetest.add_particle({
197 velocity = {x = x, y = 2 - y, z = z},
198 acceleration = {x = 0, y = -2.2, z = 0},
202 texture = "xdecor_glyph" .. random(1,18) .. ".png"
209 xdecor.register("enchantment_table", {
210 description = "Enchantment Table",
212 "xdecor_enchantment_top.png", "xdecor_enchantment_bottom.png",
213 "xdecor_enchantment_side.png", "xdecor_enchantment_side.png",
214 "xdecor_enchantment_side.png", "xdecor_enchantment_side.png"
216 groups = {cracky = 1, level = 1},
218 sounds = default.node_sound_stone_defaults(),
219 on_rotate = screwdriver.rotate_simple,
220 can_dig = enchanting.dig,
221 on_timer = enchanting.timer,
222 on_construct = enchanting.construct,
223 on_destruct = enchanting.destruct,
224 on_receive_fields = enchanting.fields,
225 on_metadata_inventory_put = enchanting.on_put,
226 on_metadata_inventory_take = enchanting.on_take,
227 allow_metadata_inventory_put = enchanting.put,
228 allow_metadata_inventory_move = function()
233 minetest.register_entity("xdecor:book_open", {
235 visual_size = {x=0.75, y=0.75},
238 textures = {"xdecor_book_open.png"},
239 on_activate = function(self)
240 local pos = self.object:getpos()
241 local pos_under = {x = pos.x, y = pos.y - 1, z = pos.z}
243 if minetest.get_node(pos_under).name ~= "xdecor:enchantment_table" then
249 function enchanting:register_tools(mod, def)
250 for tool in pairs(def.tools) do
251 for material in def.materials:gmatch("[%w_]+") do
252 for enchant in def.tools[tool].enchants:gmatch("[%w_]+") do
253 local original_tool = reg_tools[mod .. ":" .. tool .. "_" .. material]
254 if not original_tool then break end
255 local original_toolcaps = original_tool.tool_capabilities
257 if original_toolcaps then
258 local original_damage_groups = original_toolcaps.damage_groups
259 local original_groupcaps = original_toolcaps.groupcaps
260 local groupcaps = table.copy(original_groupcaps)
261 local fleshy = original_damage_groups.fleshy
262 local full_punch_interval = original_toolcaps.full_punch_interval
263 local max_drop_level = original_toolcaps.max_drop_level
264 local group = next(original_groupcaps)
266 if enchant == "durable" then
267 groupcaps[group].uses = ceil(original_groupcaps[group].uses *
269 elseif enchant == "fast" then
270 for i, time in pairs(original_groupcaps[group].times) do
271 groupcaps[group].times[i] = time - enchanting.times
273 elseif enchant == "sharp" then
274 fleshy = fleshy + enchanting.damages
277 minetest.register_tool(":" .. mod .. ":enchanted_" .. tool .. "_" .. material .. "_" .. enchant, {
278 description = "Enchanted " .. cap(material) .. " " .. cap(tool) ..
279 self:get_tooltip(enchant, original_groupcaps[group], fleshy),
280 inventory_image = original_tool.inventory_image .. "^[colorize:violet:50",
281 wield_image = original_tool.wield_image,
282 groups = {not_in_creative_inventory = 1},
283 tool_capabilities = {
284 groupcaps = groupcaps, damage_groups = {fleshy = fleshy},
285 full_punch_interval = full_punch_interval,
286 max_drop_level = max_drop_level
295 enchanting:register_tools("default", {
296 materials = "steel, bronze, mese, diamond",
298 axe = {enchants = "durable, fast"},
299 pick = {enchants = "durable, fast"},
300 shovel = {enchants = "durable, fast"},
301 sword = {enchants = "sharp"}
307 minetest.register_craft({
308 output = "xdecor:enchantment_table",
310 {"", "default:book", ""},
311 {"default:diamond", "default:obsidian", "default:diamond"},
312 {"default:obsidian", "default:obsidian", "default:obsidian"}