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