]> git.lizzy.rs Git - xdecor.git/blob - worktable.lua
Some preparative cleanup in Work Table's code
[xdecor.git] / worktable.lua
1 local worktable = {}
2 screwdriver = screwdriver or {}
3
4 -- Nodes allowed to be cut.
5 -- Only the regular, solid blocks without formspec or explosivity can be cut.
6 function worktable:nodes(def)
7         return (def.drawtype == "normal" or def.drawtype:find("glass")) and
8                 (def.groups.cracky or def.groups.choppy) and not
9                 def.on_construct and not def.after_place_node and not
10                 def.after_place_node and not def.on_rightclick and not
11                 def.on_blast and not def.allow_metadata_inventory_take and not
12                 (def.groups.not_in_creative_inventory == 1) and not
13                 def.groups.wool and not def.description:find("Ore") and
14                 def.description and def.description ~= "" and def.light_source == 0
15 end
16
17 -- Nodeboxes definitions.
18 worktable.defs = {
19         -- Name       Yield   X  Y   Z  W   H  L
20         {"nanoslab",    16, { 0, 0,  0, 8,  1, 8  }},
21         {"micropanel",  16, { 0, 0,  0, 16, 1, 8  }},
22         {"microslab",   8,  { 0, 0,  0, 16, 1, 16 }},
23         {"thinstair",   8,  { 0, 7,  0, 16, 1, 8  },
24                             { 0, 15, 8, 16, 1, 8  }},
25         {"cube",        4,  { 0, 0,  0, 8,  8, 8  }},
26         {"panel",       4,  { 0, 0,  0, 16, 8, 8  }},
27         {"slab",        2,  nil                   },
28         {"doublepanel", 2,  { 0, 0,  0, 16, 8, 8  },
29                             { 0, 8,  8, 16, 8, 8  }},
30         {"halfstair",   2,  { 0, 0,  0, 8,  8, 16 },
31                             { 0, 8,  8, 8,  8, 8  }},
32         {"outerstair",  1,  { 0, 0,  0, 16, 8, 16 },
33                             { 0, 8,  8, 8,  8, 8  }},
34         {"stair",       1,  nil                   },
35         {"innerstair",  1,  { 0, 0,  0, 16, 8, 16 },
36                             { 0, 8,  8, 16, 8, 8  },
37                             { 0, 8,  0, 8,  8, 8  }}
38 }
39
40 -- Tools allowed to be repaired.
41 worktable.repairable_tools = [[
42         pick, axe, shovel, sword, hoe, armor, shield
43 ]]
44
45 -- Nodeboxes's combination table.
46 worktable.nodebox_blender = {
47         {"nanoslab",   nil,          2  },
48         {"micropanel", nil,          3  },
49         {"cube",       nil,          6  },
50         {"cube",       "panel",      9  },
51         {"cube",       "outerstair", 11 },
52         {"cube",       "halfstair",  7  },
53         {"cube",       "innerstair", nil},
54         {"panel",      nil,          7  },
55         {"panel",      "cube",       9  },
56         {"panel",      "outerstair", 12 },
57         {"halfstair",  nil,          11 },
58         {"halfstair",  "outerstair", nil}
59 }
60
61 function worktable:get_recipe(item)
62         if item:sub(1,6) == "group:" then
63                 if item:sub(-4) == "wool" or item:sub(-3) == "dye" then
64                         item = item:sub(7)..":white"
65                 elseif minetest.registered_items["default:"..item:sub(7)] then
66                         item = item:gsub("group:", "default:")
67                 else for node, def in pairs(minetest.registered_items) do
68                          if def.groups[item:match("[^,:]+$")] then item = node end
69                      end
70                 end
71         end
72         return item
73 end
74
75 function worktable:craftguide_formspec(meta, pagenum, item, recipe_num, filter)
76         local inv_size = meta:get_int("inv_size")
77         local npp, i, s = 8*3, 0, 0
78         local pagemax = math.floor((inv_size - 1) / npp + 1)
79
80         if     pagenum > pagemax then pagenum = 1
81         elseif pagenum == 0      then pagenum = pagemax end
82
83         local formspec = [[ size[8,6.6;]
84                         tablecolumns[color;text;color;text]
85                         tableoptions[background=#00000000;highlight=#00000000;border=false]
86                         button[5.5,0;0.7,0.95;prev;<]
87                         button[7.3,0;0.7,0.95;next;>]
88                         button[4,0.2;0.7,0.5;search;?]
89                         button[4.6,0.2;0.7,0.5;clearfilter;X]
90                         button[0,0;1.5,1;backcraft;< Back]
91                         tooltip[search;Search]
92                         tooltip[clearfilter;Reset] ]] ..
93                         "table[6.1,0.18;1.1,0.5;pagenum;#FFFF00,"..tostring(pagenum)..
94                         ",#FFFFFF,/ "..tostring(pagemax).."]"..
95                         "field[1.8,0.32;2.6,1;filter;;"..filter.."]"..xbg
96
97         for _, name in pairs(self:craftguide_items(meta, filter)) do
98                 if s < (pagenum - 1) * npp then
99                         s = s + 1
100                 else if i >= npp then break end
101                         formspec = formspec.."item_image_button["..(i%8)..","..
102                                              (math.floor(i/8)+1)..";1,1;"..name..";"..name..";]"
103                         i = i + 1
104                 end
105         end
106
107         if item and minetest.registered_items[item] then
108                 --print(dump(minetest.get_all_craft_recipes(item)))
109                 local items_num = #minetest.get_all_craft_recipes(item)
110                 if recipe_num > items_num then recipe_num = 1 end
111
112                 if items_num > 1 then formspec = formspec..
113                         "button[0,6;1.6,1;alternate;Alternate]"..
114                         "label[0,5.5;Recipe "..recipe_num.." of "..items_num.."]"
115                 end
116                 
117                 local type = minetest.get_all_craft_recipes(item)[recipe_num].type
118                 if type == "cooking" then formspec = formspec..
119                         "image[3.75,4.6;0.5,0.5;default_furnace_fire_fg.png]"
120                 end
121
122                 local items = minetest.get_all_craft_recipes(item)[recipe_num].items
123                 local width = minetest.get_all_craft_recipes(item)[recipe_num].width
124                 if width == 0 then width = math.min(3, #items) end
125                 -- Lua 5.3 removed `table.maxn`, use `xdecor.maxn` in case of breakage.
126                 local rows = math.ceil(table.maxn(items) / width)
127
128                 local function is_group(item)
129                         if item:sub(1,6) == "group:" then return "\nG" end
130                         return ""
131                 end
132
133                 for i, v in pairs(items) do formspec = formspec..
134                         "item_image_button["..((i-1) % width + 4.5)..","..
135                         (math.floor((i-1) / width + (6 - math.min(2, rows))))..";1,1;"..
136                         self:get_recipe(v)..";"..self:get_recipe(v)..";"..is_group(v).."]"
137                 end
138
139                 local output = minetest.get_all_craft_recipes(item)[recipe_num].output
140                 formspec = formspec.."item_image_button[2.5,5;1,1;"..output..";"..item..";]"..
141                                      "image[3.5,5;1,1;gui_furnace_arrow_bg.png^[transformR90]"
142         end
143
144         meta:set_string("formspec", formspec)
145 end
146
147 function worktable:craftguide_items(meta, filter)
148         local items_list = {}
149         for name, def in pairs(minetest.registered_items) do
150                 if not (def.groups.not_in_creative_inventory == 1) and
151                                 minetest.get_craft_recipe(name).items and
152                                 def.description and def.description ~= "" and
153                                 (not filter or def.name:find(filter, 1, true) or
154                                         def.description:lower():find(filter, 1, true)) then
155                         items_list[#items_list+1] = name
156                 end
157         end
158
159         meta:set_int("inv_size", #items_list)
160         table.sort(items_list)
161         return items_list
162 end
163
164 function worktable:get_output(inv, input, name)
165         if inv:is_empty("input") then
166                 inv:set_list("forms", {}) return
167         end
168
169         local output = {}
170         for _, n in pairs(self.defs) do
171                 local count = math.min(n[2] * input:get_count(), input:get_stack_max())
172                 local item = name.."_"..n[1]
173                 if not n[3] then item = "stairs:"..n[1].."_"..name:match(":(.*)") end
174                 output[#output+1] = item.." "..count
175         end
176         inv:set_list("forms", output)
177 end
178
179 function worktable.formspecs(meta, id)
180         local formspecs = {
181                 -- Main formspec.
182                 [[ label[0.9,1.23;Cut]
183                    label[0.9,2.23;Repair]
184                    box[-0.05,1;2.05,0.9;#555555]
185                    box[-0.05,2;2.05,0.9;#555555]
186                    button[0,0;2,1;craft;Crafting]
187                    button[2,0;2,1;storage;Storage]
188                    image[3,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
189                    image[0,1;1,1;worktable_saw.png]
190                    image[0,2;1,1;worktable_anvil.png]
191                    image[3,2;1,1;hammer_layout.png]
192                    list[context;input;2,1;1,1;]
193                    list[context;tool;2,2;1,1;]
194                    list[context;hammer;3,2;1,1;]
195                    list[context;forms;4,0;4,3;] ]],
196                 -- Crafting formspec.
197                 [[ image[5,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
198                    button[0,0;1.5,1;back;< Back]
199                    list[current_player;craft;2,0;3,3;]
200                    list[current_player;craftpreview;6,1;1,1;]
201                    listring[current_player;main]
202                    listring[current_player;craft] ]],
203                 -- Storage formspec.
204                 [[ list[context;storage;0,1;8,2;]
205                    button[0,0;1.5,1;back;< Back]
206                    listring[context;storage]
207                    listring[current_player;main] ]]
208         }
209
210         meta:set_string("formspec", "size[8,7;]list[current_player;main;0,3.25;8,4;]"..
211                         formspecs[id]..xbg..default.get_hotbar_bg(0,3.25))
212 end
213
214 function worktable.construct(pos)
215         local meta = minetest.get_meta(pos)
216         local inv = meta:get_inventory()
217
218         inv:set_size("tool", 1)
219         inv:set_size("input", 1)
220         inv:set_size("hammer", 1)
221         inv:set_size("forms", 4*3)
222         inv:set_size("storage", 8*2)
223
224         meta:set_string("infotext", "Work Table")
225         worktable.formspecs(meta, 1)
226 end
227
228 function worktable.fields(pos, _, fields)
229         if fields.quit then return end
230         local meta = minetest.get_meta(pos)
231         local formspec = meta:to_table().fields.formspec
232         local filter = formspec:match("filter;;([%w_:]+)") or ""
233         local pagenum = tonumber(formspec:match("#FFFF00,(%d+)")) or 1
234
235         if fields.back then
236                 worktable.formspecs(meta, 1)
237         elseif fields.craft then
238                 worktable.formspecs(meta, 2)
239         elseif fields.storage then
240                 worktable.formspecs(meta, 3)
241         elseif fields.craftguide or fields.clearfilter then
242                 worktable:craftguide_items(meta, nil)
243                 worktable:craftguide_formspec(meta, 1, nil, 1, "")
244         elseif fields.alternate then
245                 local item = formspec:match("image_button%[.*;([%w_:]+);.*%]") or ""
246                 local recipe_num = tonumber(formspec:match("Recipe%s(%d+)")) or 1
247                 recipe_num = recipe_num + 1
248                 worktable:craftguide_formspec(meta, pagenum, item, recipe_num, filter)
249         elseif fields.search then
250                 worktable:craftguide_items(meta, fields.filter:lower())
251                 worktable:craftguide_formspec(meta, 1, nil, 1, fields.filter:lower())
252         elseif fields.prev or fields.next then
253                 if fields.prev then pagenum = pagenum - 1
254                 else pagenum = pagenum + 1 end
255                 worktable:craftguide_formspec(meta, pagenum, nil, 1, filter)
256         else for item in pairs(fields) do
257                  if minetest.get_craft_recipe(item).items then
258                         worktable:craftguide_formspec(meta, pagenum, item, 1, filter)
259                  end
260              end
261         end
262 end
263
264 function worktable.dig(pos)
265         local inv = minetest.get_meta(pos):get_inventory()
266         return inv:is_empty("input") and inv:is_empty("hammer") and
267                 inv:is_empty("tool") and inv:is_empty("storage")
268 end
269
270 function worktable.timer(pos)
271         local timer = minetest.get_node_timer(pos)
272         local inv = minetest.get_meta(pos):get_inventory()
273         local tool = inv:get_stack("tool", 1)
274         local hammer = inv:get_stack("hammer", 1)
275
276         if tool:is_empty() or hammer:is_empty() or tool:get_wear() == 0 then
277                 timer:stop() return
278         end
279
280         -- Tool's wearing range: 0-65535 | 0 = new condition.
281         tool:add_wear(-500)
282         hammer:add_wear(700)
283
284         inv:set_stack("tool", 1, tool)
285         inv:set_stack("hammer", 1, hammer)
286
287         return true
288 end
289
290 function worktable.put(_, listname, _, stack)
291         local stackname = stack:get_name()
292         if (listname == "tool" and stack:get_wear() > 0 and
293                         worktable.repairable_tools:find(stackname:match(":(%w+)"))) or
294                         (listname == "input" and minetest.registered_nodes[stackname.."_cube"]) or
295                         (listname == "hammer" and stackname == "xdecor:hammer") or
296                         listname == "storage" then
297                 return stack:get_count()
298         end
299         return 0
300 end
301
302 function worktable.take(_, listname, _, stack, player)
303         if listname == "forms" then
304                 local inv = player:get_inventory()
305                 if inv:room_for_item("main", stack:get_name()) then return -1 end
306                 return 0
307         end
308         return stack:get_count()
309 end
310
311 function worktable.move(_, _, _, to_list, _, count)
312         if to_list == "storage" then return count end
313         return 0
314 end
315
316 function worktable.on_put(pos, listname, _, stack)
317         local inv = minetest.get_meta(pos):get_inventory()
318         if listname == "input" then
319                 local input = inv:get_stack("input", 1)
320                 worktable:get_output(inv, input, stack:get_name())
321         elseif listname == "tool" or listname == "hammer" then
322                 local timer = minetest.get_node_timer(pos)
323                 timer:start(3.0)
324         end
325 end
326
327 function worktable.on_take(pos, listname, index, stack)
328         local inv = minetest.get_meta(pos):get_inventory()
329         local input = inv:get_stack("input", 1)
330
331         if listname == "input" then
332                 if stack:get_name() == input:get_name() then
333                         worktable:get_output(inv, input, stack:get_name())
334                 else inv:set_list("forms", {}) end
335         elseif listname == "forms" then
336                 input:take_item(math.ceil(stack:get_count() / worktable.defs[index][2]))
337                 inv:set_stack("input", 1, input)
338                 worktable:get_output(inv, input, input:get_name())
339         end
340 end
341
342 xdecor.register("worktable", {
343         description = "Work Table",
344         groups = {cracky=2, choppy=2, oddly_breakable_by_hand=1},
345         sounds = default.node_sound_wood_defaults(),
346         tiles = {
347                 "xdecor_worktable_top.png", "xdecor_worktable_top.png",
348                 "xdecor_worktable_sides.png", "xdecor_worktable_sides.png",
349                 "xdecor_worktable_front.png", "xdecor_worktable_front.png"
350         },
351         on_rotate = screwdriver.rotate_simple,
352         can_dig = worktable.dig,
353         on_timer = worktable.timer,
354         on_construct = worktable.construct,
355         on_receive_fields = worktable.fields,
356         on_metadata_inventory_put = worktable.on_put,
357         on_metadata_inventory_take = worktable.on_take,
358         on_metadata_inventory_move = worktable.on_move,
359         allow_metadata_inventory_put = worktable.put,
360         allow_metadata_inventory_take = worktable.take,
361         allow_metadata_inventory_move = worktable.move
362 })
363
364 for _, d in pairs(worktable.defs) do
365 for node in pairs(minetest.registered_nodes) do
366         local def = minetest.registered_nodes[node]
367         if worktable:nodes(def) and d[3] then
368                 local groups, tiles = {}, {}
369                 groups.not_in_creative_inventory = 1
370
371                 for k, v in pairs(def.groups) do
372                         if k ~= "wood" and k ~= "stone" and k ~= "level" then
373                                 groups[k] = v
374                         end
375                 end
376
377                 if def.tiles then
378                         if #def.tiles > 1 and not def.drawtype:find("glass") then
379                                 tiles = def.tiles
380                         else tiles = {def.tiles[1]} end
381                 else
382                         tiles = {def.tile_images[1]}
383                 end
384
385                 if not minetest.registered_nodes["stairs:slab_"..node:match(":(.*)")] then
386                         stairs.register_stair_and_slab(node:match(":(.*)"), node, groups, tiles,
387                                 def.description.." Stair", def.description.." Slab", def.sounds)
388                 end
389
390                 minetest.register_node(":"..node.."_"..d[1], {
391                         description = def.description.." "..d[1]:gsub("^%l", string.upper),
392                         paramtype = "light",
393                         paramtype2 = "facedir",
394                         drawtype = "nodebox",
395                         sounds = def.sounds,
396                         tiles = tiles,
397                         groups = groups,
398                         -- `unpack` has been changed to `table.unpack` in newest Lua versions.
399                         node_box = xdecor.pixelbox(16, {unpack(d, 3)}),
400                         sunlight_propagates = true,
401                         on_place = minetest.rotate_node,
402                         on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
403                                 local player_name = clicker:get_player_name()
404                                 if minetest.is_protected(pos, player_name) then
405                                         minetest.record_protection_violation(pos, player_name) return
406                                 end
407
408                                 local newnode, combined = def.name, false
409                                 if clicker:get_player_control().sneak then
410                                         local wield_item = clicker:get_wielded_item():get_name()
411                                         for _, x in pairs(worktable.nodebox_blender) do
412                                                 if wield_item == newnode.."_"..x[1] then
413                                                         if not x[2] then x[2] = x[1] end
414                                                         local pointed_nodebox = minetest.get_node(pos).name:match("(%w+)$")
415
416                                                         if x[2] == pointed_nodebox then
417                                                                 if x[3] then newnode = newnode.."_"..worktable.defs[x[3]][1] end
418                                                                 combined = true
419                                                                 minetest.set_node(pos, {name=newnode, param2=node.param2})
420                                                         end
421                                                 end
422                                         end
423                                 else
424                                         minetest.item_place_node(itemstack, clicker, pointed_thing)
425                                 end
426
427                                 if combined and not minetest.setting_getbool("creative_mode") then
428                                         itemstack:take_item()
429                                 end
430                                 return itemstack
431                         end
432                 })
433         end
434         if node:match(":mese") then
435                 if d[3] then minetest.register_alias(node.."_"..d[1], "default:glass_"..d[1])
436                 else minetest.register_alias("stairs:"..d[1].."_"..node:match(":(.*)"), "stairs:"..d[1].."_glass") end
437         elseif worktable:nodes(def) and not d[3] then
438                 minetest.register_alias(node.."_"..d[1], "stairs:"..d[1].."_"..node:match(":(.*)"))
439         end
440 end
441 end
442