]> git.lizzy.rs Git - mcl_enchanting.git/blob - init.lua
Backport changes from xdecor
[mcl_enchanting.git] / init.lua
1 screwdriver = screwdriver or {}
2 local ceil, abs, random = math.ceil, math.abs, math.random
3
4 -- Cost in Mese crystal(s) for enchanting.
5 local mese_cost = 1
6
7 -- Force of the enchantments.
8 local enchanting = {
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)
15 }
16
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))
20 end
21
22 function enchanting:get_tooltip(enchant, orig_caps, fleshy)
23         local bonus = {durable=0, efficiency=0, damages=0}
24         if orig_caps then
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]
29                 end
30                 local average_caps_time = sum_caps_times / #orig_caps.times
31                 bonus.efficiency = to_percent(average_caps_time, average_caps_time - enchanting.times)
32         end
33         if fleshy then
34                 bonus.damages = to_percent(fleshy, fleshy + enchanting.damages)
35         end
36
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", ""}
43         }
44         return minetest.colorize(specs[enchant][1], "\n"..cap(enchant)..specs[enchant][2])
45 end
46
47
48 function enchanting.formspec(pos, num)
49         local meta = minetest.get_meta(pos)
50         local formspec = [[ size[9,9;]
51                         bgcolor[#080808BB;true]
52                         background[0,0;9,9;ench_ui.png]
53                         list[context;tool;0.9,2.9;1,1;]
54                         list[context;mese;2,2.9;1,1;]
55                         list[current_player;main;0.5,4.5;8,4;]
56                         image[2,2.9;1,1;mese_layout.png]
57                         tooltip[sharp;Your weapon inflicts more damages]
58                         tooltip[durable;Your tool last longer]
59                         tooltip[fast;Your tool digs faster]
60                         tooltip[strong;Your armor is more resistant]
61                         tooltip[speed;Your speed is increased] ]]
62                         ..default.gui_slots..default.get_hotbar_bg(0.5,4.5)
63
64         local enchant_buttons = {
65                 [[ image_button[3.9,0.85;4,0.92;bg_btn.png;fast;Efficiency]
66                 image_button[3.9,1.77;4,1.12;bg_btn.png;durable;Durability] ]],
67                 "image_button[3.9,0.85;4,0.92;bg_btn.png;strong;Strength]",
68                 "image_button[3.9,2.9;4,0.92;bg_btn.png;sharp;Sharpness]",
69                 [[ image_button[3.9,0.85;4,0.92;bg_btn.png;strong;Strength]
70                 image_button[3.9,1.77;4,1.12;bg_btn.png;speed;Speed] ]]
71         }
72
73         formspec = formspec..(enchant_buttons[num] or "")
74         meta:set_string("formspec", formspec)
75 end
76
77 function enchanting.on_put(pos, listname, _, stack)
78         if listname == "tool" then
79                 local stackname = stack:get_name()
80                 local tool_groups = {
81                         "axe, pick, shovel",
82                         "chestplate, leggings, helmet",
83                         "sword", "boots"
84                 }
85
86                 for idx, tools in pairs(tool_groups) do
87                         if tools:find(stackname:match(":(%w+)")) then
88                                 enchanting.formspec(pos, idx)
89                         end
90                 end
91         end
92 end
93
94 function enchanting.fields(pos, _, fields, sender)
95         if not next(fields) or fields.quit then
96                 return
97         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)
104
105         if mese:get_count() >= mese_cost and minetest.registered_tools[enchanted_tool] then
106                 minetest.sound_play("xdecor_enchanting", {to_player=sender:get_player_name(), gain=0.8})
107                 tool:replace(enchanted_tool)
108                 tool:add_wear(orig_wear)
109                 mese:take_item(mese_cost)
110                 inv:set_stack("mese", 1, mese)
111                 inv:set_stack("tool", 1, tool)
112         end
113 end
114
115 function enchanting.dig(pos)
116         local inv = minetest.get_meta(pos):get_inventory()
117         return inv:is_empty("tool") and inv:is_empty("mese")
118 end
119
120 local function allowed(tool)
121         if not tool then return false end
122         for item in pairs(minetest.registered_tools) do
123                 if item:find("enchanted_"..tool) then return true end
124         end
125         return false
126 end
127
128 function enchanting.put(_, listname, _, stack)
129         local item = stack:get_name():match("[^:]+$")
130         if listname == "mese" and item == "mese_crystal" then
131                 return stack:get_count()
132         elseif listname == "tool" and allowed(item) then
133                 return 1 
134         end
135         return 0
136 end
137
138 function enchanting.on_take(pos, listname)
139         if listname == "tool" then enchanting.formspec(pos, nil) end
140 end
141
142 function enchanting.construct(pos)
143         local meta = minetest.get_meta(pos)
144         meta:set_string("infotext", "Enchantment Table")
145         enchanting.formspec(pos, nil)
146
147         local inv = meta:get_inventory()
148         inv:set_size("tool", 1)
149         inv:set_size("mese", 1)
150
151         minetest.add_entity({x=pos.x, y=pos.y+0.85, z=pos.z}, "xdecor:book_open")
152         local timer = minetest.get_node_timer(pos)
153         timer:start(5.0)
154 end
155
156 function enchanting.destruct(pos)
157         for _, obj in pairs(minetest.get_objects_inside_radius(pos, 0.9)) do
158                 if obj and obj:get_luaentity() and
159                                 obj:get_luaentity().name == "xdecor:book_open" then
160                         obj:remove() break
161                 end
162         end
163 end
164
165 function enchanting.timer(pos)
166         local num = #minetest.get_objects_inside_radius(pos, 0.9)
167         if num == 0 then
168                 minetest.add_entity({x=pos.x, y=pos.y+0.85, z=pos.z}, "xdecor:book_open")
169         end
170
171         local minp = {x=pos.x-2, y=pos.y, z=pos.z-2}
172         local maxp = {x=pos.x+2, y=pos.y+1, z=pos.z+2}
173         local bookshelves = minetest.find_nodes_in_area(minp, maxp, "default:bookshelf")
174         if #bookshelves == 0 then return true end
175
176         local bookshelf_pos = bookshelves[random(1, #bookshelves)]
177         local x = pos.x - bookshelf_pos.x
178         local y = bookshelf_pos.y - pos.y
179         local z = pos.z - bookshelf_pos.z
180
181         if tostring(x..z):find(2) then
182                 minetest.add_particle({
183                         pos = bookshelf_pos,
184                         velocity = {x=x, y=2-y, z=z},
185                         acceleration = {x=0, y=-2.2, z=0},
186                         expirationtime = 1,
187                         size = 2,
188                         texture = "xdecor_glyph"..random(1,18)..".png"
189                 })
190         end
191         return true
192 end
193
194 minetest.register_node(":xdecor:enchantment_table", {
195         description = "Enchantment Table",
196         paramtype = "light",
197         paramtype2 = "facedir",
198         tiles = {"enchtable_top.png",  "enchtable_bottom.png",
199                  "enchtable_side.png", "enchtable_side.png",
200                  "enchtable_side.png", "enchtable_side.png"},
201         groups = {cracky=1, level=1},
202         sounds = default.node_sound_stone_defaults(),
203         on_rotate = screwdriver.rotate_simple,
204         can_dig = enchanting.dig,
205         on_timer = enchanting.timer,
206         on_construct = enchanting.construct,
207         on_destruct = enchanting.destruct,
208         on_receive_fields = enchanting.fields,
209         on_metadata_inventory_put = enchanting.on_put,
210         on_metadata_inventory_take = enchanting.on_take,
211         allow_metadata_inventory_put = enchanting.put,
212         allow_metadata_inventory_move = function() return 0 end
213 })
214
215 minetest.register_entity(":xdecor:book_open", {
216         visual = "sprite",
217         visual_size = {x=0.75, y=0.75},
218         collisionbox = {0},
219         physical = false,
220         textures = {"book_open.png"},
221         on_activate = function(self)
222                 local pos = self.object:getpos()
223                 local pos_under = {x=pos.x, y=pos.y-1, z=pos.z}
224
225                 if minetest.get_node(pos_under).name ~= "xdecor:enchantment_table" then
226                         self.object:remove()
227                 end
228         end
229 })
230
231 minetest.register_craft({
232         output = "xdecor:enchantment_table",
233         recipe = {
234                 {"", "default:book", ""},
235                 {"default:diamond", "default:obsidian", "default:diamond"},
236                 {"default:obsidian", "default:obsidian", "default:obsidian"}
237         }
238 })
239
240 function enchanting:register_tools(mod, def)
241         for tool in pairs(def.tools) do
242         for material in def.materials:gmatch("[%w_]+") do
243         for enchant in def.tools[tool].enchants:gmatch("[%w_]+") do
244                 local original_tool = minetest.registered_tools[mod..":"..tool.."_"..material]
245                 if not original_tool then break end
246
247                 if original_tool.tool_capabilities then
248                         local original_damage_groups = original_tool.tool_capabilities.damage_groups
249                         local original_groupcaps = original_tool.tool_capabilities.groupcaps
250                         local groupcaps = table.copy(original_groupcaps)
251                         local fleshy = original_damage_groups.fleshy
252                         local full_punch_interval = original_tool.tool_capabilities.full_punch_interval
253                         local max_drop_level = original_tool.tool_capabilities.max_drop_level
254                         local group = next(original_groupcaps)
255
256                         if enchant == "durable" then
257                                 groupcaps[group].uses = ceil(original_groupcaps[group].uses * enchanting.uses)
258                         elseif enchant == "fast" then
259                                 for i, time in pairs(original_groupcaps[group].times) do
260                                         groupcaps[group].times[i] = time - enchanting.times
261                                 end
262                         elseif enchant == "sharp" then
263                                 fleshy = fleshy + enchanting.damages
264                         end
265
266                         minetest.register_tool(":"..mod..":enchanted_"..tool.."_"..material.."_"..enchant, {
267                                 description = "Enchanted "..cap(material).." "..cap(tool)..
268                                         self:get_tooltip(enchant, original_groupcaps[group], fleshy),
269                                 inventory_image = original_tool.inventory_image.."^[colorize:violet:50",
270                                 wield_image = original_tool.wield_image,
271                                 groups = {not_in_creative_inventory=1},
272                                 tool_capabilities = {
273                                         groupcaps = groupcaps, damage_groups = {fleshy = fleshy},
274                                         full_punch_interval = full_punch_interval, max_drop_level = max_drop_level
275                                 }
276                         })
277                 end
278
279                 if mod == "3d_armor" then
280                         local original_armor_groups = original_tool.groups
281                         local armorcaps = {}
282                         armorcaps.not_in_creative_inventory = 1
283
284                         for armor_group, value in pairs(original_armor_groups) do
285                                 if enchant == "strong" then
286                                         armorcaps[armor_group] = ceil(value * enchanting.strength)
287                                 elseif enchant == "speed" then
288                                         armorcaps[armor_group] = value
289                                         armorcaps.physics_speed = enchanting.speed
290                                         armorcaps.physics_jump = enchanting.jump
291                                 end
292                         end
293
294                         minetest.register_tool(":"..mod..":enchanted_"..tool.."_"..material.."_"..enchant, {
295                                 description = "Enchanted "..cap(material).." "..cap(tool)..
296                                         self:get_tooltip(enchant),
297                                 inventory_image = original_tool.inventory_image,
298                                 texture = "3d_armor_"..tool.."_"..material,
299                                 wield_image = original_tool.wield_image,
300                                 groups = armorcaps,
301                                 wear = 0
302                         })
303                 end
304         end
305         end
306         end
307 end
308
309 enchanting:register_tools("default", {
310         materials = "steel, bronze, mese, diamond",
311         tools = {
312                 axe    = {enchants = "durable, fast"},
313                 pick   = {enchants = "durable, fast"}, 
314                 shovel = {enchants = "durable, fast"},
315                 sword  = {enchants = "sharp"}
316         }
317 })
318
319 enchanting:register_tools("3d_armor", {
320         materials = "steel, bronze, gold, diamond",
321         tools = {
322                 boots      = {enchants = "strong, speed"},
323                 chestplate = {enchants = "strong"},
324                 helmet     = {enchants = "strong"},
325                 leggings   = {enchants = "strong"}
326         }
327 })
328