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