]> git.lizzy.rs Git - xdecor.git/blob - enchanting.lua
63641cdea58fd168e2a01ee39bd1916d580f0862
[xdecor.git] / enchanting.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 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 = "xdecor_glyph"..math.random(1,18)..".png"
154                 })
155         end
156         return true
157 end
158
159 xdecor.register("enchantment_table", {
160         description = "Enchantment Table",
161         tiles = {"xdecor_enchantment_top.png",  "xdecor_enchantment_bottom.png",
162                  "xdecor_enchantment_side.png", "xdecor_enchantment_side.png",
163                  "xdecor_enchantment_side.png", "xdecor_enchantment_side.png"},
164         groups = {cracky=1, level=1},
165         sounds = default.node_sound_stone_defaults(),
166         on_rotate = screwdriver.rotate_simple,
167         can_dig = enchanting.dig,
168         on_timer = enchanting.timer,
169         on_construct = enchanting.construct,
170         on_destruct = enchanting.destruct,
171         on_receive_fields = enchanting.fields,
172         on_metadata_inventory_put = enchanting.on_put,
173         on_metadata_inventory_take = enchanting.on_take,
174         allow_metadata_inventory_put = enchanting.put,
175         allow_metadata_inventory_move = function() return 0 end
176 })
177
178 minetest.register_entity("xdecor:book_open", {
179         visual = "sprite",
180         visual_size = {x=0.75, y=0.75},
181         collisionbox = {0},
182         physical = false,
183         textures = {"xdecor_book_open.png"},
184         on_activate = function(self)
185                 local pos = self.object:getpos()
186                 local pos_under = {x=pos.x, y=pos.y-1, z=pos.z}
187
188                 if minetest.get_node(pos_under).name ~= "xdecor:enchantment_table" then
189                         self.object:remove()
190                 end
191         end
192 })
193
194 local function cap(S) return S:gsub("^%l", string.upper) end
195
196 function enchanting:register_tools(mod, def)
197         for tool in pairs(def.tools) do
198         for material in def.materials:gmatch("[%w_]+") do
199         for enchant in def.tools[tool].enchants:gmatch("[%w_]+") do
200                 local original_tool = minetest.registered_tools[mod..":"..tool.."_"..material]
201                 if not original_tool then break end
202
203                 if original_tool.tool_capabilities then
204                         local original_damage_groups = original_tool.tool_capabilities.damage_groups
205                         local original_groupcaps = original_tool.tool_capabilities.groupcaps
206                         local groupcaps = table.copy(original_groupcaps)
207                         local fleshy = original_damage_groups.fleshy
208                         local full_punch_interval = original_tool.tool_capabilities.full_punch_interval
209                         local max_drop_level = original_tool.tool_capabilities.max_drop_level
210                         local group = next(original_groupcaps)
211
212                         if enchant == "durable" then
213                                 groupcaps[group].uses = math.ceil(original_groupcaps[group].uses * enchanting.uses)
214                         elseif enchant == "fast" then
215                                 for i, time in pairs(original_groupcaps[group].times) do
216                                         groupcaps[group].times[i] = time - enchanting.times
217                                 end
218                         elseif enchant == "sharp" then
219                                 fleshy = fleshy + enchanting.damages
220                         end
221
222                         minetest.register_tool(":"..mod..":enchanted_"..tool.."_"..material.."_"..enchant, {
223                                 description = "Enchanted "..cap(material).." "..cap(tool).." ("..cap(enchant)..")",
224                                 inventory_image = original_tool.inventory_image.."^[colorize:violet:50",
225                                 wield_image = original_tool.wield_image,
226                                 groups = {not_in_creative_inventory=1},
227                                 tool_capabilities = {
228                                         groupcaps = groupcaps, damage_groups = {fleshy = fleshy},
229                                         full_punch_interval = full_punch_interval, max_drop_level = max_drop_level
230                                 }
231                         })
232                 end
233
234                 if mod == "3d_armor" then
235                         local original_armor_groups = original_tool.groups
236                         local armorcaps = {}
237                         armorcaps.not_in_creative_inventory = 1
238
239                         for armor_group, value in pairs(original_armor_groups) do
240                                 if enchant == "strong" then
241                                         armorcaps[armor_group] = math.ceil(value * enchanting.strength)
242                                 elseif enchant == "speed" then
243                                         armorcaps[armor_group] = value
244                                         armorcaps.physics_speed = enchanting.speed
245                                         armorcaps.physics_jump = enchanting.jump
246                                 end
247                         end
248
249                         minetest.register_tool(":"..mod..":enchanted_"..tool.."_"..material.."_"..enchant, {
250                                 description = "Enchanted "..cap(material).." "..cap(tool).." ("..cap(enchant)..")",
251                                 inventory_image = original_tool.inventory_image,
252                                 texture = "3d_armor_"..tool.."_"..material,
253                                 wield_image = original_tool.wield_image,
254                                 groups = armorcaps,
255                                 wear = 0
256                         })
257                 end
258         end
259         end
260         end
261 end
262
263 enchanting:register_tools("default", {
264         materials = "steel, bronze, mese, diamond",
265         tools = {
266                 axe    = {enchants = "durable, fast"},
267                 pick   = {enchants = "durable, fast"}, 
268                 shovel = {enchants = "durable, fast"},
269                 sword  = {enchants = "sharp"}
270         }
271 })
272
273 enchanting:register_tools("3d_armor", {
274         materials = "steel, bronze, gold, diamond",
275         tools = {
276                 boots      = {enchants = "strong, speed"},
277                 chestplate = {enchants = "strong"},
278                 helmet     = {enchants = "strong"},
279                 leggings   = {enchants = "strong"}
280         }
281 })
282