]> git.lizzy.rs Git - xdecor.git/blob - src/enchanting.lua
Minor cleanup
[xdecor.git] / src / enchanting.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 -
32                                               enchanting.times)
33         end
34         if fleshy then
35                 bonus.damages = to_percent(fleshy, fleshy + enchanting.damages)
36         end
37
38         local specs = { -- not finished, to complete
39                 durable = {"#00baff", " (+"..bonus.durable.."%)"},
40                 fast    = {"#74ff49", " (+"..bonus.efficiency.."%)"},
41                 sharp   = {"#ffff00", " (+"..bonus.damages.."%)"},
42                 strong  = {"#ff3d3d", ""},
43                 speed   = {"#fd5eff", ""}
44         }
45         return minetest.colorize and minetest.colorize(specs[enchant][1],
46                         "\n"..cap(enchant)..specs[enchant][2]) or
47                         "\n"..cap(enchant)..specs[enchant][2]
48 end
49
50 local enchant_buttons = {
51         [[ image_button[3.9,0.85;4,0.92;bg_btn.png;fast;Efficiency]
52         image_button[3.9,1.77;4,1.12;bg_btn.png;durable;Durability] ]],
53         "image_button[3.9,0.85;4,0.92;bg_btn.png;strong;Strength]",
54         "image_button[3.9,2.9;4,0.92;bg_btn.png;sharp;Sharpness]",
55         [[ image_button[3.9,0.85;4,0.92;bg_btn.png;strong;Strength]
56         image_button[3.9,1.77;4,1.12;bg_btn.png;speed;Speed] ]]
57 }
58
59 function enchanting.formspec(pos, num)
60         local meta = minetest.get_meta(pos)
61         local formspec = [[ size[9,9;]
62                         bgcolor[#080808BB;true]
63                         background[0,0;9,9;ench_ui.png]
64                         list[context;tool;0.9,2.9;1,1;]
65                         list[context;mese;2,2.9;1,1;]
66                         list[current_player;main;0.5,4.5;8,4;]
67                         image[2,2.9;1,1;mese_layout.png]
68                         tooltip[sharp;Your weapon inflicts more damages]
69                         tooltip[durable;Your tool last longer]
70                         tooltip[fast;Your tool digs faster]
71                         tooltip[strong;Your armor is more resistant]
72                         tooltip[speed;Your speed is increased] ]]
73                         ..default.gui_slots..default.get_hotbar_bg(0.5,4.5)
74
75         formspec = formspec..(enchant_buttons[num] or "")
76         meta:set_string("formspec", formspec)
77 end
78
79 function enchanting.on_put(pos, listname, _, stack)
80         if listname == "tool" then
81                 local stackname = stack:get_name()
82                 local tool_groups = {
83                         "axe, pick, shovel",
84                         "chestplate, leggings, helmet",
85                         "sword", "boots"
86                 }
87
88                 for idx, tools in pairs(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 minetest.registered_tools[enchanted_tool] then
106                 minetest.sound_play("xdecor_enchanting", {
107                         to_player=sender:get_player_name(), gain=0.8})
108                 tool:replace(enchanted_tool)
109                 tool:add_wear(orig_wear)
110                 mese:take_item(mese_cost)
111                 inv:set_stack("mese", 1, mese)
112                 inv:set_stack("tool", 1, tool)
113         end
114 end
115
116 function enchanting.dig(pos)
117         local inv = minetest.get_meta(pos):get_inventory()
118         return inv:is_empty("tool") and inv:is_empty("mese")
119 end
120
121 local function allowed(tool)
122         if not tool then return false end
123         for item in pairs(minetest.registered_tools) do
124                 if item:find("enchanted_"..tool) then return true end
125         end
126         return false
127 end
128
129 function enchanting.put(_, listname, _, stack)
130         local stackname = stack:get_name()
131         if listname == "mese" and stackname == "default:mese_crystal" then
132                 return stack:get_count()
133         elseif listname == "tool" and allowed(stackname:match("[^:]+$")) then
134                 return 1
135         end
136         return 0
137 end
138
139 function enchanting.on_take(pos, listname)
140         if listname == "tool" then enchanting.formspec(pos, nil) end
141 end
142
143 function enchanting.construct(pos)
144         local meta = minetest.get_meta(pos)
145         meta:set_string("infotext", "Enchantment Table")
146         enchanting.formspec(pos, nil)
147
148         local inv = meta:get_inventory()
149         inv:set_size("tool", 1)
150         inv:set_size("mese", 1)
151
152         minetest.add_entity({x=pos.x, y=pos.y+0.85, z=pos.z}, "xdecor:book_open")
153         local timer = minetest.get_node_timer(pos)
154         timer:start(5.0)
155 end
156
157 function enchanting.destruct(pos)
158         for _, obj in pairs(minetest.get_objects_inside_radius(pos, 0.9)) do
159                 if obj and obj:get_luaentity() and
160                                 obj:get_luaentity().name == "xdecor:book_open" then
161                         obj:remove()
162                         break
163                 end
164         end
165 end
166
167 function enchanting.timer(pos)
168         local num = #minetest.get_objects_inside_radius(pos, 0.9)
169         if num == 0 then
170                 minetest.add_entity({x=pos.x, y=pos.y+0.85, z=pos.z}, "xdecor:book_open")
171         end
172
173         local minp = {x=pos.x-2, y=pos.y, z=pos.z-2}
174         local maxp = {x=pos.x+2, y=pos.y+1, z=pos.z+2}
175         local bookshelves = minetest.find_nodes_in_area(minp, maxp, "default:bookshelf")
176         if #bookshelves == 0 then return true end
177
178         local bookshelf_pos = bookshelves[random(1, #bookshelves)]
179         local x = pos.x - bookshelf_pos.x
180         local y = bookshelf_pos.y - pos.y
181         local z = pos.z - bookshelf_pos.z
182
183         if tostring(x..z):find(2) then
184                 minetest.add_particle({
185                         pos = bookshelf_pos,
186                         velocity = {x=x, y=2-y, z=z},
187                         acceleration = {x=0, y=-2.2, z=0},
188                         expirationtime = 1,
189                         size = 2,
190                         texture = "xdecor_glyph"..random(1,18)..".png"
191                 })
192         end
193         return true
194 end
195
196 xdecor.register("enchantment_table", {
197         description = "Enchantment Table",
198         tiles = {"xdecor_enchantment_top.png",  "xdecor_enchantment_bottom.png",
199                  "xdecor_enchantment_side.png", "xdecor_enchantment_side.png",
200                  "xdecor_enchantment_side.png", "xdecor_enchantment_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 = {"xdecor_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 function enchanting:register_tools(mod, def)
232         for tool in pairs(def.tools) do
233         for material in def.materials:gmatch("[%w_]+") do
234         for enchant in def.tools[tool].enchants:gmatch("[%w_]+") do
235                 local original_tool = minetest.registered_tools[mod..":"..tool.."_"..material]
236                 if not original_tool then break end
237                 local original_toolcaps = original_tool.tool_capabilities
238
239                 if original_toolcaps then
240                         local original_damage_groups = original_toolcaps.damage_groups
241                         local original_groupcaps = original_toolcaps.groupcaps
242                         local groupcaps = table.copy(original_groupcaps)
243                         local fleshy = original_damage_groups.fleshy
244                         local full_punch_interval = original_toolcaps.full_punch_interval
245                         local max_drop_level = original_toolcaps.max_drop_level
246                         local group = next(original_groupcaps)
247
248                         if enchant == "durable" then
249                                 groupcaps[group].uses = ceil(original_groupcaps[group].uses *
250                                                              enchanting.uses)
251                         elseif enchant == "fast" then
252                                 for i, time in pairs(original_groupcaps[group].times) do
253                                         groupcaps[group].times[i] = time - enchanting.times
254                                 end
255                         elseif enchant == "sharp" then
256                                 fleshy = fleshy + enchanting.damages
257                         end
258
259                         minetest.register_tool(":"..mod..":enchanted_"..tool.."_"..material.."_"..enchant, {
260                                 description = "Enchanted "..cap(material).." "..cap(tool)..
261                                         self:get_tooltip(enchant, original_groupcaps[group], fleshy),
262                                 inventory_image = original_tool.inventory_image.."^[colorize:violet:50",
263                                 wield_image = original_tool.wield_image,
264                                 groups = {not_in_creative_inventory=1},
265                                 tool_capabilities = {
266                                         groupcaps = groupcaps, damage_groups = {fleshy = fleshy},
267                                         full_punch_interval = full_punch_interval,
268                                         max_drop_level = max_drop_level
269                                 }
270                         })
271                 end
272
273                 if mod == "3d_armor" then
274                         local original_armor_groups = original_tool.groups
275                         local armorcaps = {}
276                         armorcaps.not_in_creative_inventory = 1
277
278                         for armor_group, value in pairs(original_armor_groups) do
279                                 if enchant == "strong" then
280                                         armorcaps[armor_group] = ceil(value * enchanting.strength)
281                                 elseif enchant == "speed" then
282                                         armorcaps[armor_group] = value
283                                         armorcaps.physics_speed = enchanting.speed
284                                         armorcaps.physics_jump = enchanting.jump
285                                 end
286                         end
287
288                         minetest.register_tool(":"..mod..":enchanted_"..tool.."_"..material.."_"..enchant, {
289                                 description = "Enchanted "..cap(material).." "..cap(tool)..
290                                         self:get_tooltip(enchant),
291                                 inventory_image = original_tool.inventory_image,
292                                 texture = "3d_armor_"..tool.."_"..material,
293                                 wield_image = original_tool.wield_image,
294                                 groups = armorcaps,
295                                 wear = 0
296                         })
297                 end
298         end
299         end
300         end
301 end
302
303 enchanting:register_tools("default", {
304         materials = "steel, bronze, mese, diamond",
305         tools = {
306                 axe    = {enchants = "durable, fast"},
307                 pick   = {enchants = "durable, fast"},
308                 shovel = {enchants = "durable, fast"},
309                 sword  = {enchants = "sharp"}
310         }
311 })
312
313 enchanting:register_tools("3d_armor", {
314         materials = "steel, bronze, gold, diamond",
315         tools = {
316                 boots      = {enchants = "strong, speed"},
317                 chestplate = {enchants = "strong"},
318                 helmet     = {enchants = "strong"},
319                 leggings   = {enchants = "strong"}
320         }
321 })
322