]> git.lizzy.rs Git - xdecor.git/blob - enchanting.lua
Some refactoring of enchanting code
[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 -- 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:match(stack:get_name():match(":(.-)%_")) 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..":enchanted_"..name.."_"..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:match("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
121                 enchanting.formspec(pos, nil)
122         end
123 end
124
125 function enchanting.construct(pos)
126         local meta = minetest.get_meta(pos)
127         meta:set_string("infotext", "Enchantment Table")
128         enchanting.formspec(pos, nil)
129
130         local inv = meta:get_inventory()
131         inv:set_size("tool", 1)
132         inv:set_size("mese", 1)
133 end
134
135 xdecor.register("enchantment_table", {
136         description = "Enchantment Table",
137         tiles = {
138                 "xdecor_enchantment_top.png", "xdecor_enchantment_bottom.png",
139                 "xdecor_enchantment_side.png", "xdecor_enchantment_side.png",
140                 "xdecor_enchantment_side.png", "xdecor_enchantment_side.png"
141         },
142         groups = {cracky=1, oddly_breakable_by_hand=1, level=1},
143         sounds = default.node_sound_stone_defaults(),
144         on_rotate = screwdriver.rotate_simple,
145         can_dig = enchanting.dig,
146         on_construct = enchanting.construct,
147         on_receive_fields = enchanting.fields,
148         on_metadata_inventory_put = enchanting.on_put,
149         on_metadata_inventory_take = enchanting.on_take,
150         allow_metadata_inventory_put = enchanting.put,
151         allow_metadata_inventory_move = function() return 0 end
152 })
153
154 local function cap(S) return S:gsub("^%l", string.upper) end
155
156 for mod, defs in pairs(enchanting.tools) do
157 for material in defs[1]:gmatch("[%w_]+") do
158 for _, tooldef in next, defs, 1 do
159 for enchant in tooldef[2]:gmatch("[%w_]+") do
160         local tool, group = tooldef[1], ""
161         local original_tool = minetest.registered_tools[mod..":"..tool.."_"..material]
162
163         if original_tool then
164                 if original_tool.tool_capabilities then
165                         local original_damage_groups = original_tool.tool_capabilities.damage_groups
166                         local original_groupcaps = original_tool.tool_capabilities.groupcaps
167                         local groupcaps = table.copy(original_groupcaps)
168                         local fleshy = original_damage_groups.fleshy
169                         local full_punch_interval = original_tool.tool_capabilities.full_punch_interval
170                         local max_drop_level = original_tool.tool_capabilities.max_drop_level
171                         group = tostring(next(original_groupcaps))
172
173                         if enchant == "durable" then
174                                 groupcaps[group].uses = math.ceil(original_groupcaps[group].uses * enchanting.uses)
175                         elseif enchant == "fast" then
176                                 for i = 1, 3 do
177                                         groupcaps[group].times[i] = original_groupcaps[group].times[i] - enchanting.times
178                                 end
179                         elseif enchant == "sharp" then
180                                 fleshy = fleshy + enchanting.damages
181                         end
182
183                         minetest.register_tool(":"..mod..":enchanted_"..tool.."_"..material.."_"..enchant, {
184                                 description = "Enchanted "..cap(material).." "..cap(tool).." ("..cap(enchant)..")",
185                                 inventory_image = original_tool.inventory_image.."^[colorize:violet:50",
186                                 wield_image = original_tool.wield_image,
187                                 groups = {not_in_creative_inventory=1},
188                                 tool_capabilities = {
189                                         groupcaps = groupcaps, damage_groups = {fleshy = fleshy},
190                                         full_punch_interval = full_punch_interval, max_drop_level = max_drop_level
191                                 }
192                         })
193                 end
194
195                 if mod == "3d_armor" then
196                         local original_armor_groups = original_tool.groups
197                         local armorcaps = {}
198                         armorcaps.not_in_creative_inventory = 1
199
200                         for armor_group, value in pairs(original_armor_groups) do
201                                 if enchant == "strong" then
202                                         armorcaps[armor_group] = math.ceil(value * enchanting.strength)
203                                 elseif enchant == "speed" then
204                                         armorcaps[armor_group] = value
205                                         armorcaps.physics_speed = enchanting.speed
206                                         armorcaps.physics_jump = enchanting.jump
207                                 end
208                         end
209
210                         minetest.register_tool(":"..mod..":enchanted_"..tool.."_"..material.."_"..enchant, {
211                                 description = "Enchanted "..cap(material).." "..cap(tool).." ("..cap(enchant)..")",
212                                 inventory_image = original_tool.inventory_image,
213                                 texture = "3d_armor_"..tool.."_"..material,
214                                 wield_image = original_tool.wield_image,
215                                 groups = armorcaps,
216                                 wear = 0
217                         })
218                 end
219         end
220 end
221 end
222 end
223 end
224