]> git.lizzy.rs Git - mcl_enchanting.git/blob - init.lua
Import changes from xdecor
[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, sender)
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                 minetest.sound_play("xdecor_enchanting", {to_player=sender:get_player_name(), gain=0.8})
72                 tool:replace(enchanted_tool)
73                 tool:add_wear(orig_wear)
74                 mese:take_item(mese_cost)
75                 inv:set_stack("mese", 1, mese)
76                 inv:set_stack("tool", 1, tool)
77         end
78 end
79
80 function enchanting.dig(pos)
81         local inv = minetest.get_meta(pos):get_inventory()
82         return inv:is_empty("tool") and inv:is_empty("mese")
83 end
84
85 local function allowed(tool)
86         for item in pairs(minetest.registered_tools) do
87                 if item:find("enchanted_"..tool) then return true end
88         end
89         return false
90 end
91
92 function enchanting.put(_, listname, _, stack)
93         local item = stack:get_name():match("[^:]+$")
94         if listname == "mese" and item == "mese_crystal" then
95                 return stack:get_count()
96         elseif listname == "tool" and allowed(item) then
97                 return 1 
98         end
99         return 0
100 end
101
102 function enchanting.on_take(pos, listname)
103         if listname == "tool" then enchanting.formspec(pos, nil) end
104 end
105
106 function enchanting.construct(pos)
107         local meta = minetest.get_meta(pos)
108         meta:set_string("infotext", "Enchantment Table")
109         enchanting.formspec(pos, nil)
110
111         local inv = meta:get_inventory()
112         inv:set_size("tool", 1)
113         inv:set_size("mese", 1)
114
115         minetest.add_entity({x=pos.x, y=pos.y+0.85, z=pos.z}, "xdecor:book_open")
116         local timer = minetest.get_node_timer(pos)
117         timer:start(5.0)
118 end
119
120 function enchanting.destruct(pos)
121         for _, obj in pairs(minetest.get_objects_inside_radius(pos, 0.9)) do
122                 if obj and obj:get_luaentity() and
123                                 obj:get_luaentity().name == "xdecor:book_open" then
124                         obj:remove() break
125                 end
126         end
127 end
128
129 function enchanting.timer(pos)
130         local node = minetest.get_node(pos)
131         local num = #minetest.get_objects_inside_radius(pos, 0.9)
132
133         if num == 0 then
134                 minetest.add_entity({x=pos.x, y=pos.y+0.85, z=pos.z}, "xdecor:book_open")
135         end
136
137         local minp = {x=pos.x-2, y=pos.y, z=pos.z-2}
138         local maxp = {x=pos.x+2, y=pos.y+1, z=pos.z+2}
139         local bookshelves = minetest.find_nodes_in_area(minp, maxp, "default:bookshelf")
140         if #bookshelves == 0 then return true end
141
142         local bookshelf_pos = bookshelves[math.random(1, #bookshelves)]
143         local x = pos.x - bookshelf_pos.x
144         local y = bookshelf_pos.y - pos.y
145         local z = pos.z - bookshelf_pos.z
146
147         if tostring(x..z):find(2) then
148                 minetest.add_particle({
149                         pos = bookshelf_pos,
150                         velocity = {x=x, y=2-y, z=z},
151                         acceleration = {x=0, y=-2.2, z=0},
152                         expirationtime = 1,
153                         size = 2,
154                         texture = "xdecor_glyph"..math.random(1,18)..".png"
155                 })
156         end
157         return true
158 end
159
160 minetest.register_node(":xdecor:enchantment_table", {
161         description = "Enchantment Table",
162         paramtype = "light",
163         paramtype2 = "facedir",
164         tiles = {"enchtable_top.png",  "enchtable_bottom.png",
165                  "enchtable_side.png", "enchtable_side.png",
166                  "enchtable_side.png", "enchtable_side.png"},
167         groups = {cracky=1, level=1},
168         sounds = default.node_sound_stone_defaults(),
169         on_rotate = screwdriver.rotate_simple,
170         can_dig = enchanting.dig,
171         on_timer = enchanting.timer,
172         on_construct = enchanting.construct,
173         on_destruct = enchanting.destruct,
174         on_receive_fields = enchanting.fields,
175         on_metadata_inventory_put = enchanting.on_put,
176         on_metadata_inventory_take = enchanting.on_take,
177         allow_metadata_inventory_put = enchanting.put,
178         allow_metadata_inventory_move = function() return 0 end
179 })
180
181 minetest.register_entity(":xdecor:book_open", {
182         visual = "sprite",
183         visual_size = {x=0.75, y=0.75},
184         collisionbox = {0},
185         physical = false,
186         textures = {"book_open.png"},
187         on_activate = function(self)
188                 local pos = self.object:getpos()
189                 local pos_under = {x=pos.x, y=pos.y-1, z=pos.z}
190
191                 if minetest.get_node(pos_under).name ~= "xdecor:enchantment_table" then
192                         self.object:remove()
193                 end
194         end
195 })
196
197 minetest.register_craft({
198         output = "xdecor:enchantment_table",
199         recipe = {
200                 {"", "default:book", ""},
201                 {"default:diamond", "default:obsidian", "default:diamond"},
202                 {"default:obsidian", "default:obsidian", "default:obsidian"}
203         }
204 })
205
206 local function cap(S) return S:gsub("^%l", string.upper) end
207
208 function enchanting:register_tools(mod, def)
209         for tool in pairs(def.tools) do
210         for material in def.materials:gmatch("[%w_]+") do
211         for enchant in def.tools[tool].enchants:gmatch("[%w_]+") do
212                 local original_tool = minetest.registered_tools[mod..":"..tool.."_"..material]
213                 if not original_tool then break end
214
215                 if original_tool.tool_capabilities then
216                         local original_damage_groups = original_tool.tool_capabilities.damage_groups
217                         local original_groupcaps = original_tool.tool_capabilities.groupcaps
218                         local groupcaps = table.copy(original_groupcaps)
219                         local fleshy = original_damage_groups.fleshy
220                         local full_punch_interval = original_tool.tool_capabilities.full_punch_interval
221                         local max_drop_level = original_tool.tool_capabilities.max_drop_level
222                         local group = next(original_groupcaps)
223
224                         if enchant == "durable" then
225                                 groupcaps[group].uses = math.ceil(original_groupcaps[group].uses * enchanting.uses)
226                         elseif enchant == "fast" then
227                                 for i, time in pairs(original_groupcaps[group].times) do
228                                         groupcaps[group].times[i] = time - enchanting.times
229                                 end
230                         elseif enchant == "sharp" then
231                                 fleshy = fleshy + enchanting.damages
232                         end
233
234                         minetest.register_tool(":"..mod..":enchanted_"..tool.."_"..material.."_"..enchant, {
235                                 description = "Enchanted "..cap(material).." "..cap(tool).." ("..cap(enchant)..")",
236                                 inventory_image = original_tool.inventory_image.."^[colorize:violet:50",
237                                 wield_image = original_tool.wield_image,
238                                 groups = {not_in_creative_inventory=1},
239                                 tool_capabilities = {
240                                         groupcaps = groupcaps, damage_groups = {fleshy = fleshy},
241                                         full_punch_interval = full_punch_interval, max_drop_level = max_drop_level
242                                 }
243                         })
244                 end
245
246                 if mod == "3d_armor" then
247                         local original_armor_groups = original_tool.groups
248                         local armorcaps = {}
249                         armorcaps.not_in_creative_inventory = 1
250
251                         for armor_group, value in pairs(original_armor_groups) do
252                                 if enchant == "strong" then
253                                         armorcaps[armor_group] = math.ceil(value * enchanting.strength)
254                                 elseif enchant == "speed" then
255                                         armorcaps[armor_group] = value
256                                         armorcaps.physics_speed = enchanting.speed
257                                         armorcaps.physics_jump = enchanting.jump
258                                 end
259                         end
260
261                         minetest.register_tool(":"..mod..":enchanted_"..tool.."_"..material.."_"..enchant, {
262                                 description = "Enchanted "..cap(material).." "..cap(tool).." ("..cap(enchant)..")",
263                                 inventory_image = original_tool.inventory_image,
264                                 texture = "3d_armor_"..tool.."_"..material,
265                                 wield_image = original_tool.wield_image,
266                                 groups = armorcaps,
267                                 wear = 0
268                         })
269                 end
270         end
271         end
272         end
273 end
274
275 enchanting:register_tools("default", {
276         materials = "steel, bronze, mese, diamond",
277         tools = {
278                 axe    = {enchants = "durable, fast"},
279                 pick   = {enchants = "durable, fast"}, 
280                 shovel = {enchants = "durable, fast"},
281                 sword  = {enchants = "sharp"}
282         }
283 })
284
285 enchanting:register_tools("3d_armor", {
286         materials = "steel, bronze, gold, diamond",
287         tools = {
288                 boots      = {enchants = "strong, speed"},
289                 chestplate = {enchants = "strong"},
290                 helmet     = {enchants = "strong"},
291                 leggings   = {enchants = "strong"}
292         }
293 })
294