]> git.lizzy.rs Git - xdecor.git/blob - enchanting.lua
Work Table : use pixel nodeboxes
[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 name:gmatch("[%w_]+") 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("(.*):(.*)")
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 return true end
79         end
80         return false
81 end
82
83 function enchanting.put(_, listname, _, stack)
84         local item = stack:get_name():match("[^:]+$")
85         if listname == "mese" and item == "mese_crystal" then
86                 return stack:get_count()
87         elseif listname == "tool" and allowed(item) then
88                 return 1 
89         end
90
91         return 0
92 end
93
94 function enchanting.on_take(pos, listname)
95         if listname == "tool" then
96                 enchanting.formspec(pos, nil)
97         end
98 end
99
100 function enchanting.construct(pos)
101         local meta = minetest.get_meta(pos)
102         meta:set_string("infotext", "Enchantment Table")
103         enchanting.formspec(pos, nil)
104
105         local inv = meta:get_inventory()
106         inv:set_size("tool", 1)
107         inv:set_size("mese", 1)
108 end
109
110 xdecor.register("enchantment_table", {
111         description = "Enchantment Table",
112         tiles = {
113                 "xdecor_enchantment_top.png", "xdecor_enchantment_bottom.png",
114                 "xdecor_enchantment_side.png", "xdecor_enchantment_side.png",
115                 "xdecor_enchantment_side.png", "xdecor_enchantment_side.png"
116         },
117         groups = {cracky=1, oddly_breakable_by_hand=1, level=1},
118         sounds = default.node_sound_stone_defaults(),
119         on_rotate = screwdriver.rotate_simple,
120         can_dig = enchanting.dig,
121         on_construct = enchanting.construct,
122         on_receive_fields = enchanting.fields,
123         on_metadata_inventory_put = enchanting.on_put,
124         on_metadata_inventory_take = enchanting.on_take,
125         allow_metadata_inventory_put = enchanting.put,
126         allow_metadata_inventory_move = function() return 0 end
127 })
128
129 local function cap(str)
130         return str:gsub("^%l", string.upper)
131 end
132
133  -- Higher number = stronger enchant.
134 enchanting.uses = 1.2
135 enchanting.times = 0.1
136 enchanting.damages = 1
137 enchanting.strength = 1.2
138 enchanting.speed = 0.2
139 enchanting.jump = 0.2
140
141 local tools = {
142         --[[ Registration format:
143                 [Mod name] = {
144                         materials,
145                         {tool name, tool group, enchantments}
146                  }
147         --]]
148         ["default"] = {
149                 "steel, bronze, mese, diamond",
150                 {"axe", "choppy", "durable, fast"}, 
151                 {"pick", "cracky", "durable, fast"}, 
152                 {"shovel", "crumbly", "durable, fast"},
153                 {"sword", "fleshy", "sharp"}
154         },
155         ["3d_armor"] = {
156                 "steel, bronze, gold, diamond",
157                 {"boots", nil, "strong, speed"},
158                 {"chestplate", nil, "strong"},
159                 {"helmet", nil, "strong"},
160                 {"leggings", nil, "strong"}
161         }
162 }
163
164 for mod, defs in pairs(tools) do
165 for material in defs[1]:gmatch("[%w_]+") do
166 for _, tooldef in next, defs, 1 do
167 for enchant in tooldef[3]:gmatch("[%w_]+") do
168         local tool, group = tooldef[1], tooldef[2]
169         local original_tool = minetest.registered_tools[mod..":"..tool.."_"..material]
170
171         if original_tool then
172                 if mod == "default" then
173                         local original_damage_groups = original_tool.tool_capabilities.damage_groups
174                         local original_groupcaps = original_tool.tool_capabilities.groupcaps
175                         local groupcaps = table.copy(original_groupcaps)
176                         local fleshy = original_damage_groups.fleshy
177                         local full_punch_interval = original_tool.tool_capabilities.full_punch_interval
178                         local max_drop_level = original_tool.tool_capabilities.max_drop_level
179
180                         if enchant == "durable" then
181                                 groupcaps[group].uses = math.ceil(original_groupcaps[group].uses * enchanting.uses)
182                         elseif enchant == "fast" then
183                                 for i = 1, 3 do
184                                         groupcaps[group].times[i] = original_groupcaps[group].times[i] - enchanting.times
185                                 end
186                         elseif enchant == "sharp" then
187                                 fleshy = fleshy + enchanting.damages
188                         end
189
190                         minetest.register_tool(":"..mod..":enchanted_"..tool.."_"..material.."_"..enchant, {
191                                 description = "Enchanted "..cap(material).." "..cap(tool).." ("..cap(enchant)..")",
192                                 inventory_image = original_tool.inventory_image.."^[colorize:violet:50",
193                                 wield_image = original_tool.wield_image,
194                                 groups = {not_in_creative_inventory=1},
195                                 tool_capabilities = {
196                                         groupcaps = groupcaps, damage_groups = {fleshy = fleshy},
197                                         full_punch_interval = full_punch_interval, max_drop_level = max_drop_level
198                                 }
199                         })
200                 end
201
202                 if mod == "3d_armor" then
203                         local original_armor_groups = original_tool.groups
204                         local armorcaps = table.copy(original_armor_groups)
205                         local armorcaps = {}
206                         armorcaps.not_in_creative_inventory = 1
207
208                         for armor_group, value in pairs(original_armor_groups) do
209                                 if enchant == "strong" then
210                                         armorcaps[armor_group] = math.ceil(value * enchanting.strength)
211                                 elseif enchant == "speed" then
212                                         armorcaps[armor_group] = value
213                                         armorcaps.physics_speed = enchanting.speed
214                                         armorcaps.physics_jump = enchanting.jump
215                                 end
216                         end
217
218                         minetest.register_tool(":"..mod..":enchanted_"..tool.."_"..material.."_"..enchant, {
219                                 description = "Enchanted "..cap(material).." "..cap(tool).." ("..cap(enchant)..")",
220                                 inventory_image = original_tool.inventory_image,
221                                 texture = "3d_armor_"..tool.."_"..material,
222                                 wield_image = original_tool.wield_image,
223                                 groups = armorcaps,
224                                 wear = 0
225                         })
226                 end
227         end
228 end
229 end
230 end
231 end
232