]> git.lizzy.rs Git - mcl_enchanting.git/blob - init.lua
Initial commit
[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
9 enchanting.times = 0.1
10 enchanting.damages = 1
11 enchanting.strength = 1.2
12 enchanting.speed = 0.2
13 enchanting.jump = 0.2
14
15 -- Enchanted tools registration.
16 -- Available enchantments: durable, fast, sharp, strong, speed.
17 enchanting.tools = {
18         --[[ Registration format:
19                 [Mod name] = {
20                         materials,
21                         {tool name, enchantments}
22                  }
23         ]]
24         ["default"] = {
25                 "steel, bronze, mese, diamond",
26                 {"axe",    "durable, fast"}, 
27                 {"pick",   "durable, fast"}, 
28                 {"shovel", "durable, fast"},
29                 {"sword",  "sharp"}
30         },
31         ["3d_armor"] = {
32                 "steel, bronze, gold, diamond",
33                 {"boots",      "strong, speed"},
34                 {"chestplate", "strong"},
35                 {"helmet",     "strong"},
36                 {"leggings",   "strong"}
37         }
38 }
39
40 function enchanting.formspec(pos, num)
41         local formspec = [[ size[9,9;]
42                         bgcolor[#080808BB;true]
43                         background[0,0;9,9;ench_ui.png]
44                         list[context;tool;0.9,2.9;1,1;]
45                         list[context;mese;2,2.9;1,1;]
46                         list[current_player;main;0.5,4.5;8,4;]
47                         image[2,2.9;1,1;mese_layout.png]
48                         tooltip[sharp;Your sword inflicts more damage]
49                         tooltip[durable;Your tool is more resistant]
50                         tooltip[fast;Your tool is more powerful]
51                         tooltip[strong;Your armor is more resistant]
52                         tooltip[speed;Your speed is increased] ]]
53                         ..default.gui_slots..default.get_hotbar_bg(0.5,4.5)
54
55         local tool_enchs = {
56                 [[ image_button[3.9,0.85;4,0.92;bg_btn.png;fast;Efficiency]
57                 image_button[3.9,1.77;4,1.12;bg_btn.png;durable;Durability] ]],
58                 "image_button[3.9,0.85;4,0.92;bg_btn.png;strong;Strength]",
59                 "image_button[3.9,2.9;4,0.92;bg_btn.png;sharp;Sharpness]",
60                 [[ image_button[3.9,0.85;4,0.92;bg_btn.png;strong;Strength]
61                 image_button[3.9,1.77;4,1.12;bg_btn.png;speed;Speed] ]] }
62
63         formspec = formspec..(tool_enchs[num] or "")
64         minetest.get_meta(pos):set_string("formspec", formspec)
65 end
66
67 function enchanting.on_put(pos, listname, _, stack)
68         if listname == "tool" then
69                 for k, v in pairs({"axe, pick, shovel",
70                                 "chestplate, leggings, helmet",
71                                 "sword", "boots"}) do
72                         if v:find(stack:get_name():match(":(%w+)")) then
73                                 enchanting.formspec(pos, k)
74                         end
75                 end
76         end
77 end
78
79 function enchanting.fields(pos, _, fields)
80         if fields.quit then return end
81         local inv = minetest.get_meta(pos):get_inventory()
82         local tool = inv:get_stack("tool", 1)
83         local mese = inv:get_stack("mese", 1)
84         local orig_wear = tool:get_wear()
85         local mod, name = tool:get_name():match("(.*):(.*)")
86         local enchanted_tool = (mod or "")..":enchanted_"..(name or "").."_"..next(fields)
87
88         if mese:get_count() >= mese_cost and minetest.registered_tools[enchanted_tool] then
89                 tool:replace(enchanted_tool)
90                 tool:add_wear(orig_wear)
91                 mese:take_item(mese_cost)
92                 inv:set_stack("mese", 1, mese)
93                 inv:set_stack("tool", 1, tool)
94         end
95 end
96
97 function enchanting.dig(pos)
98         local inv = minetest.get_meta(pos):get_inventory()
99         return inv:is_empty("tool") and inv:is_empty("mese")
100 end
101
102 local function allowed(tool)
103         for item in pairs(minetest.registered_tools) do
104                 if item:find("enchanted_"..tool) then return true end
105         end
106         return false
107 end
108
109 function enchanting.put(_, listname, _, stack)
110         local item = stack:get_name():match("[^:]+$")
111         if listname == "mese" and item == "mese_crystal" then
112                 return stack:get_count()
113         elseif listname == "tool" and allowed(item) then
114                 return 1 
115         end
116         return 0
117 end
118
119 function enchanting.on_take(pos, listname)
120         if listname == "tool" then enchanting.formspec(pos, nil) end
121 end
122
123 function enchanting.construct(pos)
124         local meta = minetest.get_meta(pos)
125         meta:set_string("infotext", "Enchantment Table")
126         enchanting.formspec(pos, nil)
127
128         local inv = meta:get_inventory()
129         inv:set_size("tool", 1)
130         inv:set_size("mese", 1)
131 end
132
133 minetest.register_node(":xdecor:enchantment_table", {
134         description = "Enchantment Table",
135         paramtype = "light",
136         paramtype2 = "facedir",
137         tiles = {"enchtable_top.png", "enchtable_bottom.png",
138                  "enchtable_side.png", "enchtable_side.png",
139                  "enchtable_side.png", "enchtable_side.png"},
140         groups = {cracky=1, oddly_breakable_by_hand=1, level=1},
141         sounds = default.node_sound_stone_defaults(),
142         on_rotate = screwdriver.rotate_simple,
143         can_dig = enchanting.dig,
144         on_construct = enchanting.construct,
145         on_receive_fields = enchanting.fields,
146         on_metadata_inventory_put = enchanting.on_put,
147         on_metadata_inventory_take = enchanting.on_take,
148         allow_metadata_inventory_put = enchanting.put,
149         allow_metadata_inventory_move = function() return 0 end
150 })
151
152 minetest.register_craft({
153         output = "xdecor:enchantment_table",
154         recipe = {
155                 {"", "default:book", ""},
156                 {"default:diamond", "default:obsidian", "default:diamond"},
157                 {"default:obsidian", "default:obsidian", "default:obsidian"}
158         }
159 })
160
161 local function cap(S) return S:gsub("^%l", string.upper) end
162
163 for mod, defs in pairs(enchanting.tools) do
164 for material in defs[1]:gmatch("[%w_]+") do
165 for _, tooldef in next, defs, 1 do
166 for enchant in tooldef[2]:gmatch("[%w_]+") do
167         local tool, group = tooldef[1], ""
168         local original_tool = minetest.registered_tools[mod..":"..tool.."_"..material]
169
170         if original_tool then
171                 if original_tool.tool_capabilities then
172                         local original_damage_groups = original_tool.tool_capabilities.damage_groups
173                         local original_groupcaps = original_tool.tool_capabilities.groupcaps
174                         local groupcaps = table.copy(original_groupcaps)
175                         local fleshy = original_damage_groups.fleshy
176                         local full_punch_interval = original_tool.tool_capabilities.full_punch_interval
177                         local max_drop_level = original_tool.tool_capabilities.max_drop_level
178                         group = tostring(next(original_groupcaps))
179
180                         if enchant == "durable" then
181                                 groupcaps[group].uses = math.ceil(original_groupcaps[group].uses * enchanting.uses)
182                         elseif enchant == "fast" then
183                                 for i = 1, 3 do
184                                         groupcaps[group].times[i] = original_groupcaps[group].times[i] - enchanting.times
185                                 end
186                         elseif enchant == "sharp" then
187                                 fleshy = fleshy + enchanting.damages
188                         end
189
190                         minetest.register_tool(":"..mod..":enchanted_"..tool.."_"..material.."_"..enchant, {
191                                 description = "Enchanted "..cap(material).." "..cap(tool).." ("..cap(enchant)..")",
192                                 inventory_image = original_tool.inventory_image.."^[colorize:violet:50",
193                                 wield_image = original_tool.wield_image,
194                                 groups = {not_in_creative_inventory=1},
195                                 tool_capabilities = {
196                                         groupcaps = groupcaps, damage_groups = {fleshy = fleshy},
197                                         full_punch_interval = full_punch_interval, max_drop_level = max_drop_level
198                                 }
199                         })
200                 end
201
202                 if mod == "3d_armor" then
203                         local original_armor_groups = original_tool.groups
204                         local armorcaps = {}
205                         armorcaps.not_in_creative_inventory = 1
206
207                         for armor_group, value in pairs(original_armor_groups) do
208                                 if enchant == "strong" then
209                                         armorcaps[armor_group] = math.ceil(value * enchanting.strength)
210                                 elseif enchant == "speed" then
211                                         armorcaps[armor_group] = value
212                                         armorcaps.physics_speed = enchanting.speed
213                                         armorcaps.physics_jump = enchanting.jump
214                                 end
215                         end
216
217                         minetest.register_tool(":"..mod..":enchanted_"..tool.."_"..material.."_"..enchant, {
218                                 description = "Enchanted "..cap(material).." "..cap(tool).." ("..cap(enchant)..")",
219                                 inventory_image = original_tool.inventory_image,
220                                 texture = "3d_armor_"..tool.."_"..material,
221                                 wield_image = original_tool.wield_image,
222                                 groups = armorcaps,
223                                 wear = 0
224                         })
225                 end
226         end
227 end
228 end
229 end
230 end
231