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
12 strength = 1.2, -- Armor strength (3d_armor only)
13 speed = 0.2, -- Player speed (3d_armor only)
14 jump = 0.2 -- Player jumping (3d_armor only)
17 local function cap(S) return S:gsub("^%l", string.upper) end
18 local function to_percent(orig_value, final_value)
19 return abs(ceil(((final_value - orig_value) / orig_value) * 100))
22 function enchanting:get_tooltip(enchant, orig_caps, fleshy)
23 local bonus = {durable=0, efficiency=0, damages=0}
25 bonus.durable = to_percent(orig_caps.uses, orig_caps.uses * enchanting.uses)
26 local sum_caps_times = 0
27 for i=1, #orig_caps.times do
28 sum_caps_times = sum_caps_times + orig_caps.times[i]
30 local average_caps_time = sum_caps_times / #orig_caps.times
31 bonus.efficiency = to_percent(average_caps_time, average_caps_time - enchanting.times)
34 bonus.damages = to_percent(fleshy, fleshy + enchanting.damages)
37 local specs = { -- not finished, to complete
38 durable = {"#00baff", " (+"..bonus.durable.."%)"},
39 fast = {"#74ff49", " (+"..bonus.efficiency.."%)"},
40 sharp = {"#ffff00", " (+"..bonus.damages.."%)"},
41 strong = {"#ff3d3d", ""},
42 speed = {"#fd5eff", ""}
44 return minetest.colorize and
45 minetest.colorize(specs[enchant][1],
46 "\n"..cap(enchant)..specs[enchant][2]) or
47 "\n"..cap(enchant)..specs[enchant][2]
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 image[2,2.9;1,1;mese_layout.png]
60 tooltip[sharp;Your weapon inflicts more damages]
61 tooltip[durable;Your tool last longer]
62 tooltip[fast;Your tool digs faster]
63 tooltip[strong;Your armor is more resistant]
64 tooltip[speed;Your speed is increased] ]]
65 ..default.gui_slots..default.get_hotbar_bg(0.5,4.5)
67 local enchant_buttons = {
68 [[ image_button[3.9,0.85;4,0.92;bg_btn.png;fast;Efficiency]
69 image_button[3.9,1.77;4,1.12;bg_btn.png;durable;Durability] ]],
70 "image_button[3.9,0.85;4,0.92;bg_btn.png;strong;Strength]",
71 "image_button[3.9,2.9;4,0.92;bg_btn.png;sharp;Sharpness]",
72 [[ image_button[3.9,0.85;4,0.92;bg_btn.png;strong;Strength]
73 image_button[3.9,1.77;4,1.12;bg_btn.png;speed;Speed] ]]
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()
85 "chestplate, leggings, helmet",
89 for idx, tools in pairs(tool_groups) do
90 if tools:find(stackname:match(":(%w+)")) then
91 enchanting.formspec(pos, idx)
97 function enchanting.fields(pos, _, fields, sender)
98 if not next(fields) or fields.quit then
101 local inv = minetest.get_meta(pos):get_inventory()
102 local tool = inv:get_stack("tool", 1)
103 local mese = inv:get_stack("mese", 1)
104 local orig_wear = tool:get_wear()
105 local mod, name = tool:get_name():match("(.*):(.*)")
106 local enchanted_tool = (mod or "")..":enchanted_"..(name or "").."_"..next(fields)
108 if mese:get_count() >= mese_cost and minetest.registered_tools[enchanted_tool] then
109 minetest.sound_play("xdecor_enchanting", {to_player=sender:get_player_name(), gain=0.8})
110 tool:replace(enchanted_tool)
111 tool:add_wear(orig_wear)
112 mese:take_item(mese_cost)
113 inv:set_stack("mese", 1, mese)
114 inv:set_stack("tool", 1, tool)
118 function enchanting.dig(pos)
119 local inv = minetest.get_meta(pos):get_inventory()
120 return inv:is_empty("tool") and inv:is_empty("mese")
123 local function allowed(tool)
124 if not tool then return false end
125 for item in pairs(minetest.registered_tools) do
126 if item:find("enchanted_"..tool) then return true end
131 function enchanting.put(_, listname, _, stack)
132 local item = stack:get_name():match("[^:]+$")
133 if listname == "mese" and item == "mese_crystal" then
134 return stack:get_count()
135 elseif listname == "tool" and allowed(item) then
141 function enchanting.on_take(pos, listname)
142 if listname == "tool" then enchanting.formspec(pos, nil) end
145 function enchanting.construct(pos)
146 local meta = minetest.get_meta(pos)
147 meta:set_string("infotext", "Enchantment Table")
148 enchanting.formspec(pos, nil)
150 local inv = meta:get_inventory()
151 inv:set_size("tool", 1)
152 inv:set_size("mese", 1)
154 minetest.add_entity({x=pos.x, y=pos.y+0.85, z=pos.z}, "xdecor:book_open")
155 local timer = minetest.get_node_timer(pos)
159 function enchanting.destruct(pos)
160 for _, obj in pairs(minetest.get_objects_inside_radius(pos, 0.9)) do
161 if obj and obj:get_luaentity() and
162 obj:get_luaentity().name == "xdecor:book_open" then
168 function enchanting.timer(pos)
169 local num = #minetest.get_objects_inside_radius(pos, 0.9)
171 minetest.add_entity({x=pos.x, y=pos.y+0.85, z=pos.z}, "xdecor:book_open")
174 local minp = {x=pos.x-2, y=pos.y, z=pos.z-2}
175 local maxp = {x=pos.x+2, y=pos.y+1, z=pos.z+2}
176 local bookshelves = minetest.find_nodes_in_area(minp, maxp, "default:bookshelf")
177 if #bookshelves == 0 then return true end
179 local bookshelf_pos = bookshelves[random(1, #bookshelves)]
180 local x = pos.x - bookshelf_pos.x
181 local y = bookshelf_pos.y - pos.y
182 local z = pos.z - bookshelf_pos.z
184 if tostring(x..z):find(2) then
185 minetest.add_particle({
187 velocity = {x=x, y=2-y, z=z},
188 acceleration = {x=0, y=-2.2, z=0},
191 texture = "xdecor_glyph"..random(1,18)..".png"
197 minetest.register_node(":xdecor:enchantment_table", {
198 description = "Enchantment Table",
200 paramtype2 = "facedir",
201 tiles = {"enchtable_top.png", "enchtable_bottom.png",
202 "enchtable_side.png", "enchtable_side.png",
203 "enchtable_side.png", "enchtable_side.png"},
204 groups = {cracky=1, level=1},
205 sounds = default.node_sound_stone_defaults(),
206 on_rotate = screwdriver.rotate_simple,
207 can_dig = enchanting.dig,
208 on_timer = enchanting.timer,
209 on_construct = enchanting.construct,
210 on_destruct = enchanting.destruct,
211 on_receive_fields = enchanting.fields,
212 on_metadata_inventory_put = enchanting.on_put,
213 on_metadata_inventory_take = enchanting.on_take,
214 allow_metadata_inventory_put = enchanting.put,
215 allow_metadata_inventory_move = function() return 0 end
218 minetest.register_entity(":xdecor:book_open", {
220 visual_size = {x=0.75, y=0.75},
223 textures = {"book_open.png"},
224 on_activate = function(self)
225 local pos = self.object:getpos()
226 local pos_under = {x=pos.x, y=pos.y-1, z=pos.z}
228 if minetest.get_node(pos_under).name ~= "xdecor:enchantment_table" then
234 minetest.register_craft({
235 output = "xdecor:enchantment_table",
237 {"", "default:book", ""},
238 {"default:diamond", "default:obsidian", "default:diamond"},
239 {"default:obsidian", "default:obsidian", "default:obsidian"}
243 function enchanting:register_tools(mod, def)
244 for tool in pairs(def.tools) do
245 for material in def.materials:gmatch("[%w_]+") do
246 for enchant in def.tools[tool].enchants:gmatch("[%w_]+") do
247 local original_tool = minetest.registered_tools[mod..":"..tool.."_"..material]
248 if not original_tool then break end
250 if original_tool.tool_capabilities then
251 local original_damage_groups = original_tool.tool_capabilities.damage_groups
252 local original_groupcaps = original_tool.tool_capabilities.groupcaps
253 local groupcaps = table.copy(original_groupcaps)
254 local fleshy = original_damage_groups.fleshy
255 local full_punch_interval = original_tool.tool_capabilities.full_punch_interval
256 local max_drop_level = original_tool.tool_capabilities.max_drop_level
257 local group = next(original_groupcaps)
259 if enchant == "durable" then
260 groupcaps[group].uses = ceil(original_groupcaps[group].uses * enchanting.uses)
261 elseif enchant == "fast" then
262 for i, time in pairs(original_groupcaps[group].times) do
263 groupcaps[group].times[i] = time - enchanting.times
265 elseif enchant == "sharp" then
266 fleshy = fleshy + enchanting.damages
269 minetest.register_tool(":"..mod..":enchanted_"..tool.."_"..material.."_"..enchant, {
270 description = "Enchanted "..cap(material).." "..cap(tool)..
271 self:get_tooltip(enchant, original_groupcaps[group], fleshy),
272 inventory_image = original_tool.inventory_image.."^[colorize:violet:50",
273 wield_image = original_tool.wield_image,
274 groups = {not_in_creative_inventory=1},
275 tool_capabilities = {
276 groupcaps = groupcaps, damage_groups = {fleshy = fleshy},
277 full_punch_interval = full_punch_interval, max_drop_level = max_drop_level
282 if mod == "3d_armor" then
283 local original_armor_groups = original_tool.groups
285 armorcaps.not_in_creative_inventory = 1
287 for armor_group, value in pairs(original_armor_groups) do
288 if enchant == "strong" then
289 armorcaps[armor_group] = ceil(value * enchanting.strength)
290 elseif enchant == "speed" then
291 armorcaps[armor_group] = value
292 armorcaps.physics_speed = enchanting.speed
293 armorcaps.physics_jump = enchanting.jump
297 minetest.register_tool(":"..mod..":enchanted_"..tool.."_"..material.."_"..enchant, {
298 description = "Enchanted "..cap(material).." "..cap(tool)..
299 self:get_tooltip(enchant),
300 inventory_image = original_tool.inventory_image,
301 texture = "3d_armor_"..tool.."_"..material,
302 wield_image = original_tool.wield_image,
312 enchanting:register_tools("default", {
313 materials = "steel, bronze, mese, diamond",
315 axe = {enchants = "durable, fast"},
316 pick = {enchants = "durable, fast"},
317 shovel = {enchants = "durable, fast"},
318 sword = {enchants = "sharp"}
322 enchanting:register_tools("3d_armor", {
323 materials = "steel, bronze, gold, diamond",
325 boots = {enchants = "strong, speed"},
326 chestplate = {enchants = "strong"},
327 helmet = {enchants = "strong"},
328 leggings = {enchants = "strong"}