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