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