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