]> git.lizzy.rs Git - xdecor.git/blob - enchanting.lua
532efb9fa58d387e93f48d232cb4a54b711c5160
[xdecor.git] / enchanting.lua
1 local function enchconstruct(pos)
2         local meta = minetest.get_meta(pos)
3         meta:set_string("formspec", "size[8,7;]"..xdecor.fancy_gui..
4                 "label[0.85,-0.15;Enchant]"..
5                 "image[0.6,0.2;2,2;xdecor_enchbook.png]"..
6                 "image[1.5,2;1,1;ench_mese_layout.png]"..
7                 "list[current_name;tool;0.5,2;1,1;]"..
8                 "list[current_name;mese;1.5,2;1,1;]"..
9                 "image_button[2.75,0;5,1.5;ench_bg.png;durable;Durable]"..
10                 "image_button[2.75,1.5;5,1.5;ench_bg.png;fast;Fast]"..
11                 "list[current_player;main;0,3.3;8,4;]")
12         meta:set_string("infotext", "Enchantment Table")
13
14         local inv = meta:get_inventory()
15         inv:set_size("tool", 1)
16         inv:set_size("mese", 1)
17 end
18
19 local function is_allowed_tool(toolname)
20         local tdef = minetest.registered_tools[toolname]
21         if tdef and string.find(toolname, "default:") and not
22                         string.find(toolname, "sword") and not
23                         string.find(toolname, "stone") and not
24                         string.find(toolname, "wood") then
25                 return 1
26         else return 0 end
27 end
28
29 local function enchfields(pos, formname, fields, sender)
30         local meta = minetest.get_meta(pos)
31         local inv = meta:get_inventory()
32         local toolstack = inv:get_stack("tool", 1)
33         local mesestack = inv:get_stack("mese", 1)
34         local toolname = toolstack:get_name()
35         local mese = mesestack:get_count()
36         local enchs = {"durable", "fast"}
37
38         for _, e in pairs(enchs) do
39                 if is_allowed_tool(toolname) ~= 0 and mese > 0 and fields[e] then
40                         toolstack:replace("xdecor:enchanted_"..string.sub(toolname, 9).."_"..e)
41                         mesestack:take_item()
42                         inv:set_stack("mese", 1, mesestack)
43                         inv:set_stack("tool", 1, toolstack)
44                 end
45         end
46 end
47
48 local function enchdig(pos, player)
49         local meta = minetest.get_meta(pos)
50         local inv = meta:get_inventory()
51
52         if not inv:is_empty("tool") or not inv:is_empty("mese") then
53                 return false
54         end
55         return true
56 end
57
58 local function enchput(pos, listname, index, stack, player)
59         local toolname = stack:get_name()
60         local count = stack:get_count()
61
62         if listname == "mese" then
63                 if toolname == "default:mese_crystal" then return count
64                         else return 0 end
65         end
66         if listname == "tool" then
67                 return is_allowed_tool(toolname)
68         end
69         return count
70 end
71
72 xdecor.register("enchantment_table", {
73         description = "Enchantment Table",
74         tiles = {
75                 "xdecor_enchantment_top.png",
76                 "xdecor_enchantment_bottom.png",
77                 "xdecor_enchantment_side.png",
78                 "xdecor_enchantment_side.png",
79                 "xdecor_enchantment_side.png",
80                 "xdecor_enchantment_side.png"
81         },
82         groups = {cracky=1},
83         sounds = xdecor.stone,
84         on_construct = enchconstruct,
85         can_dig = enchdig,
86         allow_metadata_inventory_put = enchput,
87         on_receive_fields = enchfields
88 })
89
90 local tools = {
91         {"axe", "choppy"}, {"pick", "cracky"}, {"shovel", "crumbly"}
92 }
93 local materials = {"steel", "bronze", "mese", "diamond"}
94
95 for _, t in pairs(tools) do
96 for _, m in pairs(materials) do
97         local tool, group = t[1], t[2]
98         local toolname = tool.."_"..m
99
100         local registered_tool = {}
101         registered_tool = minetest.registered_tools["default:"..toolname]["tool_capabilities"]["groupcaps"][group]
102
103         local times = registered_tool["times"]
104         local uses = registered_tool["uses"]
105         local dmg = registered_tool["damage_groups"]
106         local maxlvl = registered_tool["maxlevel"]
107
108         local dig_faster, use_longer = {}, {}
109         use_longer = registered_tool["uses"] * 1.1 -- Wearing factor for enchanted tools (higher number = longer use).
110         for i = 1, 3 do
111                 dig_faster[i] = registered_tool["times"][i] - 0.1 -- Digging factor for enchanted tools (lower number = faster dig).
112         end
113
114         --- Pickaxes ---
115
116         minetest.register_tool("xdecor:enchanted_pick_"..m.."_durable", {
117                 description = "Enchanted "..string.sub(string.upper(m), 0, 1)..string.sub(m, 2).." Pickaxe (Durable)",
118                 inventory_image = minetest.registered_tools["default:pick_"..m]["inventory_image"],
119                 groups = {not_in_creative_inventory=1},
120                 tool_capabilities = {
121                         groupcaps = {
122                                 cracky = {times=times, uses=use_longer, maxlevel=maxlvl}
123                         },
124                         damage_groups = dmg
125                 }
126         })
127
128         minetest.register_tool("xdecor:enchanted_pick_"..m.."_fast", {
129                 description = "Enchanted "..string.sub(string.upper(m), 0, 1)..string.sub(m, 2).." Pickaxe (Fast)",
130                 inventory_image = minetest.registered_tools["default:pick_"..m]["inventory_image"],
131                 groups = {not_in_creative_inventory=1},
132                 tool_capabilities = {
133                         groupcaps = {
134                                 cracky = {times=dig_faster, uses=uses, maxlevel=maxlvl}
135                         },
136                         damage_groups = dmg
137                 }
138         })
139
140         --- Axes ---
141
142         minetest.register_tool("xdecor:enchanted_axe_"..m.."_durable", {
143                 description = "Enchanted "..string.sub(string.upper(m), 0, 1)..string.sub(m, 2).." Axe (Durable)",
144                 inventory_image = minetest.registered_tools["default:axe_"..m]["inventory_image"],
145                 groups = {not_in_creative_inventory=1},
146                 tool_capabilities = {
147                         groupcaps = {
148                                 choppy = {times=times, uses=use_longer, maxlevel=maxlvl}
149                         },
150                         damage_groups = dmg
151                 }
152         })
153
154         minetest.register_tool("xdecor:enchanted_axe_"..m.."_fast", {
155                 description = "Enchanted "..string.sub(string.upper(m), 0, 1)..string.sub(m, 2).." Axe (Fast)",
156                 inventory_image = minetest.registered_tools["default:axe_"..m]["inventory_image"],
157                 groups = {not_in_creative_inventory=1},
158                 tool_capabilities = {
159                         groupcaps = {
160                                 choppy = {times=dig_faster, uses=uses, maxlevel=maxlvl}
161                         },
162                         damage_groups = dmg
163                 }
164         })
165
166         --- Shovels ---
167
168         minetest.register_tool("xdecor:enchanted_shovel_"..m.."_durable", {
169                 description = "Enchanted "..string.sub(string.upper(m), 0, 1)..string.sub(m, 2).." Shovel (Durable)",
170                 inventory_image = minetest.registered_tools["default:shovel_"..m]["inventory_image"],
171                 groups = {not_in_creative_inventory=1},
172                 tool_capabilities = {
173                         groupcaps = {
174                                 crumbly = {times=times, uses=use_longer, maxlevel=maxlvl}
175                         },
176                         damage_groups = dmg
177                 }
178         })
179
180         minetest.register_tool("xdecor:enchanted_shovel_"..m.."_fast", {
181                 description = "Enchanted "..string.sub(string.upper(m), 0, 1)..string.sub(m, 2).." Shovel (Fast)",
182                 inventory_image = minetest.registered_tools["default:shovel_"..m]["inventory_image"],
183                 groups = {not_in_creative_inventory=1},
184                 tool_capabilities = {
185                         groupcaps = {
186                                 crumbly = {times=dig_faster, uses=uses, maxlevel=maxlvl}
187                         },
188                         damage_groups = dmg
189                 }
190         })
191 end
192 end