1 screwdriver = screwdriver or {}
2 local ceil, abs, random = math.ceil, math.abs, math.random
4 -- Cost in Mese crystal(s) for enchanting.
7 -- Force of the enchantments.
9 uses = 1.2, -- Durability
10 times = 0.1, -- Efficiency
11 damages = 1, -- Sharpness
14 local function cap(S) return S:gsub("^%l", string.upper) end
15 local function to_percent(orig_value, final_value)
16 return abs(ceil(((final_value - orig_value) / orig_value) * 100))
19 function enchanting:get_tooltip(enchant, orig_caps, fleshy)
20 local bonus = {durable=0, efficiency=0, damages=0}
22 bonus.durable = to_percent(orig_caps.uses, orig_caps.uses * enchanting.uses)
23 local sum_caps_times = 0
24 for i=1, #orig_caps.times do
25 sum_caps_times = sum_caps_times + orig_caps.times[i]
27 local average_caps_time = sum_caps_times / #orig_caps.times
28 bonus.efficiency = to_percent(average_caps_time, average_caps_time -
32 bonus.damages = to_percent(fleshy, fleshy + enchanting.damages)
35 local specs = { -- not finished, to complete
36 durable = {"#00baff", " (+"..bonus.durable.."%)"},
37 fast = {"#74ff49", " (+"..bonus.efficiency.."%)"},
38 sharp = {"#ffff00", " (+"..bonus.damages.."%)"},
40 return minetest.colorize and minetest.colorize(specs[enchant][1],
41 "\n"..cap(enchant)..specs[enchant][2]) or
42 "\n"..cap(enchant)..specs[enchant][2]
45 local enchant_buttons = {
46 [[ image_button[3.9,0.85;4,0.92;bg_btn.png;fast;Efficiency]
47 image_button[3.9,1.77;4,1.12;bg_btn.png;durable;Durability] ]],
48 "image_button[3.9,2.9;4,0.92;bg_btn.png;sharp;Sharpness]",
51 function enchanting.formspec(pos, num)
52 local meta = minetest.get_meta(pos)
53 local formspec = [[ size[9,9;]
54 bgcolor[#080808BB;true]
55 background[0,0;9,9;ench_ui.png]
56 list[context;tool;0.9,2.9;1,1;]
57 list[context;mese;2,2.9;1,1;]
58 list[current_player;main;0.5,4.5;8,4;]
59 listring[current_player;main]
60 listring[context;tool]
61 listring[current_player;main]
62 listring[context;mese]
63 image[2,2.9;1,1;mese_layout.png]
64 tooltip[sharp;Your weapon inflicts more damages]
65 tooltip[durable;Your tool last longer]
66 tooltip[fast;Your tool digs faster] ]]
67 ..default.gui_slots..default.get_hotbar_bg(0.5,4.5)
69 formspec = formspec..(enchant_buttons[num] or "")
70 meta:set_string("formspec", formspec)
73 function enchanting.on_put(pos, listname, _, stack)
74 if listname == "tool" then
75 local stackname = stack:get_name()
81 for idx, tools in pairs(tool_groups) do
82 if tools:find(stackname:match(":(%w+)")) then
83 enchanting.formspec(pos, idx)
89 function enchanting.fields(pos, _, fields, sender)
90 if not next(fields) or fields.quit then return end
91 local inv = minetest.get_meta(pos):get_inventory()
92 local tool = inv:get_stack("tool", 1)
93 local mese = inv:get_stack("mese", 1)
94 local orig_wear = tool:get_wear()
95 local mod, name = tool:get_name():match("(.*):(.*)")
96 local enchanted_tool = (mod or "")..":enchanted_"..(name or "").."_"..next(fields)
98 if mese:get_count() >= mese_cost and minetest.registered_tools[enchanted_tool] then
99 minetest.sound_play("xdecor_enchanting", {
100 to_player=sender:get_player_name(), gain=0.8})
101 tool:replace(enchanted_tool)
102 tool:add_wear(orig_wear)
103 mese:take_item(mese_cost)
104 inv:set_stack("mese", 1, mese)
105 inv:set_stack("tool", 1, tool)
109 function enchanting.dig(pos)
110 local inv = minetest.get_meta(pos):get_inventory()
111 return inv:is_empty("tool") and inv:is_empty("mese")
114 local function allowed(tool)
115 if not tool then return false end
116 for item in pairs(minetest.registered_tools) do
117 if item:find("enchanted_"..tool) then return true end
122 function enchanting.put(_, listname, _, stack)
123 local stackname = stack:get_name()
124 if listname == "mese" and stackname == "default:mese_crystal" then
125 return stack:get_count()
126 elseif listname == "tool" and allowed(stackname:match("[^:]+$")) then
132 function enchanting.on_take(pos, listname)
133 if listname == "tool" then enchanting.formspec(pos, nil) end
136 function enchanting.construct(pos)
137 local meta = minetest.get_meta(pos)
138 meta:set_string("infotext", "Enchantment Table")
139 enchanting.formspec(pos, nil)
141 local inv = meta:get_inventory()
142 inv:set_size("tool", 1)
143 inv:set_size("mese", 1)
145 minetest.add_entity({x=pos.x, y=pos.y+0.85, z=pos.z}, "xdecor:book_open")
146 local timer = minetest.get_node_timer(pos)
150 function enchanting.destruct(pos)
151 for _, obj in pairs(minetest.get_objects_inside_radius(pos, 0.9)) do
152 if obj and obj:get_luaentity() and
153 obj:get_luaentity().name == "xdecor:book_open" then
160 function enchanting.timer(pos)
161 local num = #minetest.get_objects_inside_radius(pos, 0.9)
163 minetest.add_entity({x=pos.x, y=pos.y+0.85, z=pos.z}, "xdecor:book_open")
166 local minp = {x=pos.x-2, y=pos.y, z=pos.z-2}
167 local maxp = {x=pos.x+2, y=pos.y+1, z=pos.z+2}
168 local bookshelves = minetest.find_nodes_in_area(minp, maxp, "default:bookshelf")
169 if #bookshelves == 0 then return true end
171 local bookshelf_pos = bookshelves[random(1, #bookshelves)]
172 local x = pos.x - bookshelf_pos.x
173 local y = bookshelf_pos.y - pos.y
174 local z = pos.z - bookshelf_pos.z
176 if tostring(x..z):find(2) then
177 minetest.add_particle({
179 velocity = {x=x, y=2-y, z=z},
180 acceleration = {x=0, y=-2.2, z=0},
184 texture = "xdecor_glyph"..random(1,18)..".png"
190 xdecor.register("enchantment_table", {
191 description = "Enchantment Table",
192 tiles = {"xdecor_enchantment_top.png", "xdecor_enchantment_bottom.png",
193 "xdecor_enchantment_side.png", "xdecor_enchantment_side.png",
194 "xdecor_enchantment_side.png", "xdecor_enchantment_side.png"},
195 groups = {cracky=1, level=1},
197 sounds = default.node_sound_stone_defaults(),
198 on_rotate = screwdriver.rotate_simple,
199 can_dig = enchanting.dig,
200 on_timer = enchanting.timer,
201 on_construct = enchanting.construct,
202 on_destruct = enchanting.destruct,
203 on_receive_fields = enchanting.fields,
204 on_metadata_inventory_put = enchanting.on_put,
205 on_metadata_inventory_take = enchanting.on_take,
206 allow_metadata_inventory_put = enchanting.put,
207 allow_metadata_inventory_move = function() return 0 end
210 minetest.register_entity("xdecor:book_open", {
212 visual_size = {x=0.75, y=0.75},
215 textures = {"xdecor_book_open.png"},
216 on_activate = function(self)
217 local pos = self.object:getpos()
218 local pos_under = {x=pos.x, y=pos.y-1, z=pos.z}
220 if minetest.get_node(pos_under).name ~= "xdecor:enchantment_table" then
226 function enchanting:register_tools(mod, def)
227 for tool in pairs(def.tools) do
228 for material in def.materials:gmatch("[%w_]+") do
229 for enchant in def.tools[tool].enchants:gmatch("[%w_]+") do
230 local original_tool = minetest.registered_tools[mod..":"..tool.."_"..material]
231 if not original_tool then break end
232 local original_toolcaps = original_tool.tool_capabilities
234 if original_toolcaps then
235 local original_damage_groups = original_toolcaps.damage_groups
236 local original_groupcaps = original_toolcaps.groupcaps
237 local groupcaps = table.copy(original_groupcaps)
238 local fleshy = original_damage_groups.fleshy
239 local full_punch_interval = original_toolcaps.full_punch_interval
240 local max_drop_level = original_toolcaps.max_drop_level
241 local group = next(original_groupcaps)
243 if enchant == "durable" then
244 groupcaps[group].uses = ceil(original_groupcaps[group].uses *
246 elseif enchant == "fast" then
247 for i, time in pairs(original_groupcaps[group].times) do
248 groupcaps[group].times[i] = time - enchanting.times
250 elseif enchant == "sharp" then
251 fleshy = fleshy + enchanting.damages
254 minetest.register_tool(":"..mod..":enchanted_"..tool.."_"..material.."_"..enchant, {
255 description = "Enchanted "..cap(material).." "..cap(tool)..
256 self:get_tooltip(enchant, original_groupcaps[group], fleshy),
257 inventory_image = original_tool.inventory_image.."^[colorize:violet:50",
258 wield_image = original_tool.wield_image,
259 groups = {not_in_creative_inventory=1},
260 tool_capabilities = {
261 groupcaps = groupcaps, damage_groups = {fleshy = fleshy},
262 full_punch_interval = full_punch_interval,
263 max_drop_level = max_drop_level
272 enchanting:register_tools("default", {
273 materials = "steel, bronze, mese, diamond",
275 axe = {enchants = "durable, fast"},
276 pick = {enchants = "durable, fast"},
277 shovel = {enchants = "durable, fast"},
278 sword = {enchants = "sharp"}
284 minetest.register_craft({
285 output = "xdecor:enchantment_table",
287 {"", "default:book", ""},
288 {"default:diamond", "default:obsidian", "default:diamond"},
289 {"default:obsidian", "default:obsidian", "default:obsidian"}