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