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