]> git.lizzy.rs Git - xdecor.git/blob - src/workbench.lua
Fix #86 - crash in workbench: `attempt to concatenate a nil value`
[xdecor.git] / src / workbench.lua
1 local workbench = {}
2 WB = {}
3 screwdriver = screwdriver or {}
4 local min, ceil = math.min, math.ceil
5 local registered_nodes = minetest.registered_nodes
6
7 -- Nodes allowed to be cut
8 -- Only the regular, solid blocks without metas or explosivity can be cut
9 local nodes = {}
10 for node, def in pairs(registered_nodes) do
11         if (def.drawtype == "normal" or def.drawtype:sub(1,5) == "glass") and
12            (def.groups.cracky or def.groups.choppy) and
13            not def.on_construct and
14            not def.after_place_node and
15            not def.on_rightclick and
16            not def.on_blast and
17            not def.allow_metadata_inventory_take and
18            not (def.groups.not_in_creative_inventory == 1) and
19            not (def.groups.not_cuttable == 1) and
20            not def.groups.wool and
21            (def.tiles and type(def.tiles[1]) == "string" and not
22                 def.tiles[1]:find("default_mineral")) and
23            not def.mesecons and
24            def.description and
25            def.description ~= "" and
26            def.light_source == 0
27         then
28                 nodes[#nodes+1] = node
29         end
30 end
31
32 -- Optionally, you can register custom cuttable nodes in the workbench
33 WB.custom_nodes_register = {
34         -- "default:leaves",
35 }
36
37 setmetatable(nodes, {
38         __concat = function(t1, t2)
39                 for i=1, #t2 do
40                         t1[#t1+1] = t2[i]
41                 end
42                 return t1
43         end
44 })
45
46 nodes = nodes..WB.custom_nodes_register
47
48 -- Nodeboxes definitions
49 workbench.defs = {
50         -- Name       Yield   X  Y   Z  W   H  L
51         {"nanoslab",    16, { 0, 0,  0, 8,  1, 8  }},
52         {"micropanel",  16, { 0, 0,  0, 16, 1, 8  }},
53         {"microslab",   8,  { 0, 0,  0, 16, 1, 16 }},
54         {"thinstair",   8,  { 0, 7,  0, 16, 1, 8  },
55                             { 0, 15, 8, 16, 1, 8  }},
56         {"cube",        4,  { 0, 0,  0, 8,  8, 8  }},
57         {"panel",       4,  { 0, 0,  0, 16, 8, 8  }},
58         {"slab",        2,  nil                   },
59         {"doublepanel", 2,  { 0, 0,  0, 16, 8, 8  },
60                             { 0, 8,  8, 16, 8, 8  }},
61         {"halfstair",   2,  { 0, 0,  0, 8,  8, 16 },
62                             { 0, 8,  8, 8,  8, 8  }},
63         {"outerstair",  1,  { 0, 0,  0, 16, 8, 16 },
64                             { 0, 8,  8, 8,  8, 8  }},
65         {"stair",       1,  nil                   },
66         {"innerstair",  1,  { 0, 0,  0, 16, 8, 16 },
67                             { 0, 8,  8, 16, 8, 8  },
68                             { 0, 8,  0, 8,  8, 8  }}
69 }
70
71 -- Tools allowed to be repaired
72 function workbench:repairable(stack)
73         local tools = {"pick", "axe", "shovel", "sword", "hoe", "armor", "shield"}
74         for _, t in pairs(tools) do
75                 if stack:find(t) then return true end
76         end
77         return false
78 end
79
80 function workbench:get_output(inv, input, name)
81         local output = {}
82         for i=1, #self.defs do
83                 local nbox = self.defs[i]
84                 local count = min(nbox[2] * input:get_count(), input:get_stack_max())
85                 local item = name.."_"..nbox[1]
86                 item = nbox[3] and item or "stairs:"..nbox[1].."_"..name:match(":(.*)")
87                 output[#output+1] = item.." "..count
88         end
89         inv:set_list("forms", output)
90 end
91
92 local formspecs = {
93         -- Main formspec
94         [[ label[0.9,1.23;Cut]
95            label[0.9,2.23;Repair]
96            box[-0.05,1;2.05,0.9;#555555]
97            box[-0.05,2;2.05,0.9;#555555]
98            button[0,0;2,1;craft;Crafting]
99            button[2,0;2,1;storage;Storage]
100            image[3,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
101            image[0,1;1,1;worktable_saw.png]
102            image[0,2;1,1;worktable_anvil.png]
103            image[3,2;1,1;hammer_layout.png]
104            list[context;input;2,1;1,1;]
105            list[context;tool;2,2;1,1;]
106            list[context;hammer;3,2;1,1;]
107            list[context;forms;4,0;4,3;]
108            listring[current_player;main]
109            listring[context;tool]
110            listring[current_player;main]
111            listring[context;hammer]
112            listring[current_player;main]
113            listring[context;forms]
114            listring[current_player;main]
115            listring[context;input] ]],
116         -- Crafting formspec
117         [[ image[5,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
118            button[0,0;1.5,1;back;< Back]
119            list[current_player;craft;2,0;3,3;]
120            list[current_player;craftpreview;6,1;1,1;]
121            listring[current_player;main]
122            listring[current_player;craft] ]],
123         -- Storage formspec
124         [[ list[context;storage;0,1;8,2;]
125            button[0,0;1.5,1;back;< Back]
126            listring[context;storage]
127            listring[current_player;main] ]]
128 }
129
130 function workbench:set_formspec(meta, id)
131         meta:set_string("formspec",
132                         "size[8,7;]list[current_player;main;0,3.25;8,4;]"..
133                         formspecs[id]..xbg..default.get_hotbar_bg(0,3.25))
134 end
135
136 function workbench.construct(pos)
137         local meta = minetest.get_meta(pos)
138         local inv = meta:get_inventory()
139
140         inv:set_size("tool", 1)
141         inv:set_size("input", 1)
142         inv:set_size("hammer", 1)
143         inv:set_size("forms", 4*3)
144         inv:set_size("storage", 8*2)
145
146         meta:set_string("infotext", "Work Bench")
147         workbench:set_formspec(meta, 1)
148 end
149
150 function workbench.fields(pos, _, fields)
151         if fields.quit then return end
152         local meta = minetest.get_meta(pos)
153         local id = fields.back and 1 or
154                    fields.craft and 2 or
155                    fields.storage and 3
156         if not id then return end
157         workbench:set_formspec(meta, id)
158 end
159
160 function workbench.dig(pos)
161         local inv = minetest.get_meta(pos):get_inventory()
162         return inv:is_empty("input") and inv:is_empty("hammer") and
163                inv:is_empty("tool") and inv:is_empty("storage")
164 end
165
166 function workbench.timer(pos)
167         local timer = minetest.get_node_timer(pos)
168         local inv = minetest.get_meta(pos):get_inventory()
169         local tool = inv:get_stack("tool", 1)
170         local hammer = inv:get_stack("hammer", 1)
171
172         if tool:is_empty() or hammer:is_empty() or tool:get_wear() == 0 then
173                 timer:stop()
174                 return
175         end
176
177         -- Tool's wearing range: 0-65535 | 0 = new condition
178         tool:add_wear(-500)
179         hammer:add_wear(700)
180
181         inv:set_stack("tool", 1, tool)
182         inv:set_stack("hammer", 1, hammer)
183         return true
184 end
185
186 function workbench.put(_, listname, _, stack)
187         local stackname = stack:get_name()
188         if (listname == "tool" and stack:get_wear() > 0 and
189             workbench:repairable(stackname)) or
190            (listname == "input" and registered_nodes[stackname.."_cube"]) or
191            (listname == "hammer" and stackname == "xdecor:hammer") or
192             listname == "storage" then
193                 return stack:get_count()
194         end
195         return 0
196 end
197
198 function workbench.move(_, from_list, _, to_list, _, count)
199         return (to_list == "storage" and from_list ~= "forms") and count or 0
200 end
201
202 function workbench.on_put(pos, listname, _, stack)
203         local inv = minetest.get_meta(pos):get_inventory()
204         if listname == "input" then
205                 local input = inv:get_stack("input", 1)
206                 workbench:get_output(inv, input, stack:get_name())
207         elseif listname == "tool" or listname == "hammer" then
208                 local timer = minetest.get_node_timer(pos)
209                 timer:start(3.0)
210         end
211 end
212
213 function workbench.on_take(pos, listname, index, stack, player)
214         local inv = minetest.get_meta(pos):get_inventory()
215         local input = inv:get_stack("input", 1)
216         local inputname = input:get_name()
217         local stackname = stack:get_name()
218
219         if listname == "input" then
220                 if stackname == inputname then
221                         workbench:get_output(inv, input, stackname)
222                 else
223                         inv:set_list("forms", {})
224                 end
225         elseif listname == "forms" then
226                 local fromstack = inv:get_stack(listname, index)
227                 if not fromstack:is_empty() and fromstack:get_name() ~= stackname then
228                         local player_inv = player:get_inventory()
229                         if player_inv:room_for_item("main", fromstack) then
230                                 player_inv:add_item("main", fromstack)
231                         end
232                 end
233
234                 input:take_item(ceil(stack:get_count() / workbench.defs[index][2]))
235                 inv:set_stack("input", 1, input)
236                 workbench:get_output(inv, input, inputname)
237         end
238 end
239
240 xdecor.register("workbench", {
241         description = "Work Bench",
242         groups = {cracky=2, choppy=2, oddly_breakable_by_hand=1},
243         sounds = default.node_sound_wood_defaults(),
244         tiles = {"xdecor_workbench_top.png",   "xdecor_workbench_top.png",
245                  "xdecor_workbench_sides.png", "xdecor_workbench_sides.png",
246                  "xdecor_workbench_front.png", "xdecor_workbench_front.png"},
247         on_rotate = screwdriver.rotate_simple,
248         can_dig = workbench.dig,
249         on_timer = workbench.timer,
250         on_construct = workbench.construct,
251         on_receive_fields = workbench.fields,
252         on_metadata_inventory_put = workbench.on_put,
253         on_metadata_inventory_take = workbench.on_take,
254         allow_metadata_inventory_put = workbench.put,
255         allow_metadata_inventory_move = workbench.move
256 })
257
258 for _, d in pairs(workbench.defs) do
259 for i=1, #nodes do
260         local node = nodes[i]
261         local def = registered_nodes[node]
262
263         if d[3] then
264                 local groups = {}
265                 local tiles
266                 groups.not_in_creative_inventory = 1
267
268                 for k, v in pairs(def.groups) do
269                         if k ~= "wood" and k ~= "stone" and k ~= "level" then
270                                 groups[k] = v
271                         end
272                 end
273
274                 if def.tiles then
275                         if #def.tiles > 1 and (def.drawtype:sub(1,5) ~= "glass") then
276                                 tiles = def.tiles
277                         else
278                                 tiles = {def.tiles[1]}
279                         end
280                 else
281                         tiles = {def.tile_images[1]}
282                 end
283
284                 if not registered_nodes["stairs:slab_"..node:match(":(.*)")] then
285                         stairs.register_stair_and_slab(node:match(":(.*)"), node,
286                                 groups, tiles, def.description.." Stair",
287                                 def.description.." Slab", def.sounds)
288                 end
289
290                 minetest.register_node(":"..node.."_"..d[1], {
291                         description = def.description.." "..d[1]:gsub("^%l", string.upper),
292                         paramtype = "light",
293                         paramtype2 = "facedir",
294                         drawtype = "nodebox",
295                         sounds = def.sounds,
296                         tiles = tiles,
297                         groups = groups,
298                         -- `unpack` has been changed to `table.unpack` in newest Lua versions
299                         node_box = xdecor.pixelbox(16, {unpack(d, 3)}),
300                         sunlight_propagates = true,
301                         on_place = minetest.rotate_node
302                 })
303         end
304 end
305 end