]> git.lizzy.rs Git - xdecor.git/blob - worktable.lua
73f14fc72b658d4351a4f6839d2b287850878516
[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.description:find("Ore") and not def.name:find("wool") 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
43         armor, shield
44 ]]
45
46 function worktable.get_recipe(item)
47         if item:find("^group:") then
48                 if item:find("wool$") or item:find("dye$") then
49                         item = item:sub(7)..":white"
50                 elseif minetest.registered_items["default:"..item:sub(7)] then
51                         item = item:gsub("group:", "default:")
52                 else
53                         for node, def in pairs(minetest.registered_items) do
54                                 if def.groups[item:match("[^,:]+$")] then
55                                         item = node
56                                 end
57                         end
58                 end
59         end
60         return item
61 end
62
63 function worktable.craftguide_formspec(meta, pagenum, item, recipe_num, filter, tab_id)
64         local inv_size = meta:get_int("inv_size")
65         local npp, i, s = 8*3, 0, 0
66         local pagemax = math.floor((inv_size - 1) / npp + 1)
67
68         if pagenum > pagemax then
69                 pagenum = 1
70         elseif pagenum == 0 then
71                 pagenum = pagemax
72         end
73
74         local formspec = [[ size[8,6.6;]
75                         tablecolumns[color;text;color;text]
76                         tableoptions[background=#00000000;highlight=#00000000;border=false]
77                         button[5.5,0;0.7,1;prev;<]
78                         button[7.3,0;0.7,1;next;>]
79                         button[4,0.2;0.7,0.5;search;?]
80                         button[4.6,0.2;0.7,0.5;clearfilter;X]
81                         button[0,0;1.5,1;backcraft;< Back]
82                         tooltip[search;Search]
83                         tooltip[clearfilter;Reset] ]]
84                         .."tabheader[0,0;tabs;All,Nodes,Tools,Items;"..tostring(tab_id)..";true;false]"..
85                         "table[6.1,0.2;1.1,0.5;pagenum;#FFFF00,"..tostring(pagenum)..
86                         ",#FFFFFF,/ "..tostring(pagemax).."]"..
87                         "field[1.8,0.32;2.6,1;filter;;"..filter.."]"..xbg
88
89         for _, name in pairs(worktable.craftguide_main_list(meta, filter, tab_id)) do
90                 if s < (pagenum - 1) * npp then
91                         s = s + 1
92                 else
93                         if i >= npp then break end
94                         formspec = formspec.."item_image_button["..(i%8)..","..
95                                         (math.floor(i/8)+1)..";1,1;"..name..";"..name..";]"
96                         i = i + 1
97                 end
98         end
99
100         if item and minetest.registered_items[item] then
101                 --print(dump(minetest.get_all_craft_recipes(item)))
102                 local items_num = #minetest.get_all_craft_recipes(item)
103                 if recipe_num > items_num then recipe_num = 1 end
104
105                 if items_num > 1 then
106                         formspec = formspec.."button[0,6;1.6,1;alternate;Alternate]"..
107                                         "label[0,5.5;Recipe "..recipe_num.." of "..items_num.."]"
108                 end
109                 
110                 local type = minetest.get_all_craft_recipes(item)[recipe_num].type
111                 if type == "cooking" then
112                         formspec = formspec.."image[3.75,4.6;0.5,0.5;default_furnace_fire_fg.png]"
113                 end
114
115                 local items = minetest.get_all_craft_recipes(item)[recipe_num].items
116                 local width = minetest.get_all_craft_recipes(item)[recipe_num].width
117                 local yield = minetest.get_all_craft_recipes(item)[recipe_num].output:match("%s(%d+)") or ""
118                 if width == 0 then width = math.min(3, #items) end
119                 local rows = math.ceil(table.maxn(items) / width)
120
121                 local function is_group(item)
122                         if item:find("^group:") then return "G" end
123                         return ""
124                 end
125
126                 for i, v in pairs(items) do
127                         formspec = formspec.."item_image_button["..((i-1) % width + 4.5)..","..
128                                 (math.floor((i-1) / width + (6 - math.min(2, rows))))..";1,1;"..
129                                 worktable.get_recipe(v)..";"..worktable.get_recipe(v)..";"..is_group(v).."]"
130                 end
131
132                 formspec = formspec.."item_image_button[2.5,5;1,1;"..item..";"..item..";"..yield.."]"..
133                                 "image[3.5,5;1,1;gui_furnace_arrow_bg.png^[transformR90]"
134         end
135
136         meta:set_string("formspec", formspec)
137 end
138
139 local function tab_category(tab_id)
140         local id_category = {
141                 minetest.registered_items,
142                 minetest.registered_nodes,
143                 minetest.registered_tools,
144                 minetest.registered_craftitems
145         }
146
147         return id_category[tab_id] or id_category[1]
148 end
149
150 function worktable.craftguide_main_list(meta, filter, tab_id)
151         local items_list = {}
152         for name, def in pairs(tab_category(tab_id)) do
153                 if not (def.groups.not_in_creative_inventory == 1) and
154                                 minetest.get_craft_recipe(name).items and
155                                 def.description and def.description ~= "" and
156                                 (not filter or def.name:find(filter, 1, true)) then
157                         items_list[#items_list+1] = name
158                 end
159         end
160
161         meta:set_int("inv_size", #items_list)
162         table.sort(items_list)
163         return items_list
164 end
165
166 worktable.formspecs = {
167         crafting = function(meta)
168                 meta:set_string("formspec", [[ size[8,7;]
169                         image[5,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
170                         image[0.06,2.12;0.8,0.8;trash_icon.png]
171                         button[0,0;1.5,1;back;< Back]
172                         button[0,0.85;1.5,1;craftguide;Guide]
173                         list[context;trash;0,2;1,1;]
174                         list[current_player;main;0,3.3;8,4;]
175                         list[current_player;craft;2,0;3,3;]
176                         list[current_player;craftpreview;6,1;1,1;]
177                         listring[current_player;main]
178                         listring[current_player;craft] ]]
179                         ..xbg..default.get_hotbar_bg(0,3.3))
180         end,
181         storage = function(meta)
182                 meta:set_string("formspec", [[ size[8,7]
183                         image[7.06,0.12;0.8,0.8;trash_icon.png]
184                         list[context;trash;7,0;1,1;]
185                         list[context;storage;0,1;8,2;]
186                         list[current_player;main;0,3.25;8,4;]
187                         listring[context;storage]
188                         listring[current_player;main]
189                         button[0,0;1.5,1;back;< Back] ]]
190                         ..xbg..default.get_hotbar_bg(0,3.25))
191         end,
192         main = function(meta)
193                 meta:set_string("formspec", [[ size[8,7;]
194                         label[0.9,1.23;Cut]
195                         label[0.9,2.23;Repair]
196                         box[-0.05,1;2.05,0.9;#555555]
197                         box[-0.05,2;2.05,0.9;#555555]
198                         image[3,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
199                         image[0,1;1,1;worktable_saw.png]
200                         image[0,2;1,1;worktable_anvil.png]
201                         image[3,2;1,1;hammer_layout.png]
202                         list[context;input;2,1;1,1;]
203                         list[context;tool;2,2;1,1;]
204                         list[context;hammer;3,2;1,1;]
205                         list[context;forms;4,0;4,3;]
206                         list[current_player;main;0,3.25;8,4;]
207                         button[0,0;2,1;craft;Crafting]
208                         button[2,0;2,1;storage;Storage] ]]
209                         ..xbg..default.get_hotbar_bg(0,3.25))
210         end
211 }
212
213 function worktable.construct(pos)
214         local meta = minetest.get_meta(pos)
215         local inv = meta:get_inventory()
216
217         inv:set_size("tool", 1)
218         inv:set_size("trash", 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         meta:set_string("infotext", "Work Table")
224
225         worktable.formspecs.main(meta)
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         local tab_id = tonumber(formspec:match("tabheader%[.*;(%d+)%;.*%]")) or 1
235
236         if fields.back then
237                 worktable.formspecs.main(meta)
238         elseif fields.craft or fields.backcraft then
239                 worktable.formspecs.crafting(meta)
240         elseif fields.storage then
241                 worktable.formspecs.storage(meta)
242         elseif fields.craftguide or fields.clearfilter then
243                 worktable.craftguide_main_list(meta, nil, tab_id)
244                 worktable.craftguide_formspec(meta, 1, nil, 1, "", tab_id)
245         elseif fields.alternate then
246                 local item = formspec:match("item_image_button%[.*;([%w_:]+);.*%]") or ""
247                 local recipe_num = tonumber(formspec:match("Recipe%s(%d+)")) or 1
248                 recipe_num = recipe_num + 1
249                 worktable.craftguide_formspec(meta, pagenum, item, recipe_num, filter, tab_id)
250         elseif fields.search then
251                 worktable.craftguide_main_list(meta, fields.filter:lower(), tab_id)
252                 worktable.craftguide_formspec(meta, 1, nil, 1, fields.filter:lower(), tab_id)
253         elseif fields.tabs then
254                 worktable.craftguide_main_list(meta, filter, tonumber(fields.tabs))
255                 worktable.craftguide_formspec(meta, 1, nil, 1, filter, tonumber(fields.tabs))
256         elseif fields.prev or fields.next then
257                 if fields.prev then
258                         pagenum = pagenum - 1
259                 else
260                         pagenum = pagenum + 1
261                 end
262                 worktable.craftguide_formspec(meta, pagenum, nil, 1, filter, tab_id)
263         else
264                 for item in pairs(fields) do
265                         if item:find(":") and minetest.get_craft_recipe(item).items then
266                                 worktable.craftguide_formspec(meta, pagenum, item, 1, filter, tab_id)
267                         end
268                 end
269         end
270 end
271
272 function worktable.dig(pos)
273         local inv = minetest.get_meta(pos):get_inventory()
274         return inv:is_empty("input") and inv:is_empty("hammer") and
275                 inv:is_empty("tool") and inv:is_empty("storage")
276 end
277
278 local function trash_delete(pos)
279         local inv = minetest.get_meta(pos):get_inventory()
280         minetest.after(0, function()
281                 inv:set_stack("trash", 1, "")
282         end)
283 end
284
285 function worktable.put(pos, listname, _, stack)
286         local stackname = stack:get_name()
287         if listname == "tool" and stack:get_wear() > 0 and
288                         worktable.repairable_tools:find(stackname:match(":(%w+)")) then
289                 return stack:get_count()
290         end
291         if (listname == "input" and worktable.nodes(minetest.registered_nodes[stackname])) or
292                         (listname == "hammer" and stackname == "xdecor:hammer") or
293                         listname == "storage" or listname == "trash" then
294                 if listname == "trash" then trash_delete(pos) end
295                 return stack:get_count()
296         end
297
298         return 0
299 end
300
301 function worktable.take(_, listname, _, stack, player)
302         if listname == "forms" then
303                 local inv = player:get_inventory()
304                 if inv:room_for_item("main", stack:get_name()) then
305                         return -1
306                 end
307                 return 0
308         end
309         return stack:get_count()
310 end
311
312 function worktable.move(pos, _, _, to_list, _, count)
313         if to_list == "storage" then
314                 return count
315         elseif to_list == "trash" then
316                 trash_delete(pos)
317                 return count
318         end
319         return 0
320 end
321
322 function worktable.get_output(inv, input, name)
323         if inv:is_empty("input") then
324                 inv:set_list("forms", {})
325                 return
326         end
327
328         local output = {}
329         for _, n in pairs(worktable.defs) do
330                 local count = math.min(n[2] * input:get_count(), input:get_stack_max())
331                 local item = name.."_"..n[1]
332                 if not n[3] then item = "stairs:"..n[1].."_"..name:match(":(.*)") end
333
334                 output[#output+1] = item.." "..count
335         end
336
337         inv:set_list("forms", output)
338 end
339
340 function worktable.on_put(pos, listname, _, stack)
341         if listname == "input" then
342                 local inv = minetest.get_meta(pos):get_inventory()
343                 local input = inv:get_stack("input", 1)
344                 worktable.get_output(inv, input, stack:get_name())
345         end
346 end
347
348 function worktable.on_take(pos, listname, index, stack)
349         local inv = minetest.get_meta(pos):get_inventory()
350         local input = inv:get_stack("input", 1)
351
352         if listname == "input" then
353                 if stack:get_name() == input:get_name() then
354                         worktable.get_output(inv, input, stack:get_name())
355                 else
356                         inv:set_list("forms", {})
357                 end
358         elseif listname == "forms" then
359                 input:take_item(math.ceil(stack:get_count() / worktable.defs[index][2]))
360                 inv:set_stack("input", 1, input)
361                 worktable.get_output(inv, input, input:get_name())
362         end
363 end
364
365 xdecor.register("worktable", {
366         description = "Work Table",
367         groups = {cracky=2, choppy=2, oddly_breakable_by_hand=1},
368         sounds = default.node_sound_wood_defaults(),
369         tiles = {
370                 "xdecor_worktable_top.png", "xdecor_worktable_top.png",
371                 "xdecor_worktable_sides.png", "xdecor_worktable_sides.png",
372                 "xdecor_worktable_front.png", "xdecor_worktable_front.png"
373         },
374         on_rotate = screwdriver.rotate_simple,
375         can_dig = worktable.dig,
376         on_construct = worktable.construct,
377         on_receive_fields = worktable.fields,
378         on_metadata_inventory_put = worktable.on_put,
379         on_metadata_inventory_take = worktable.on_take,
380         allow_metadata_inventory_put = worktable.put,
381         allow_metadata_inventory_take = worktable.take,
382         allow_metadata_inventory_move = worktable.move
383 })
384
385 for node in pairs(minetest.registered_nodes) do
386 for _, d in pairs(worktable.defs) do
387         local def = minetest.registered_nodes[node]
388         if worktable.nodes(def) and d[3] then
389                 local groups, tiles = {}, {def.tiles[1]}
390                 groups.not_in_creative_inventory = 1
391
392                 for k, v in pairs(def.groups) do
393                         if k ~= "wood" and k ~= "stone" and k ~= "level" then
394                                 groups[k] = v
395                         end
396                 end
397
398                 if #def.tiles > 1 and not def.drawtype:find("glass") then
399                         tiles = def.tiles
400                 end
401
402                 stairs.register_stair_and_slab(node:match(":(.*)"), node, groups, tiles,
403                         def.description.." Stair", def.description.." Slab", def.sounds)
404
405                 minetest.register_node(":"..node.."_"..d[1], {
406                         description = def.description.." "..d[1]:gsub("^%l", string.upper),
407                         paramtype = "light",
408                         paramtype2 = "facedir",
409                         drawtype = "nodebox",
410                         sounds = def.sounds,
411                         tiles = tiles,
412                         groups = groups,
413                         node_box = xdecor.pixelnodebox(16, {unpack(d, 3)}),
414                         sunlight_propagates = true,
415                         on_place = minetest.rotate_node,
416                         on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
417                                 local pointed_nodebox = minetest.get_node(pos).name:match("(%w+)$")
418                                 local wield_item = clicker:get_wielded_item():get_name()
419                                 local player_name = clicker:get_player_name()
420                                 local newnode = def.name
421
422                                 if minetest.is_protected(pos, player_name) then
423                                         minetest.record_protection_violation(pos, player_name)
424                                         return
425                                 end
426
427                                 local T = {
428                                         {"nanoslab",   nil,          2},
429                                         {"micropanel", nil,          3},
430                                         {"cube",       nil,          6},
431                                         {"cube",       "panel",      9},
432                                         {"cube",       "outerstair", 11},
433                                         {"cube",       "halfstair",  7},
434                                         {"cube",       "innerstair", nil},
435                                         {"panel",      nil,          7},
436                                         {"panel",      "outerstair", 12},
437                                         {"halfstair",  nil,          11},
438                                         {"halfstair",  "outerstair", nil}
439                                 }
440
441                                 for _, x in pairs(T) do
442                                         if wield_item == def.name.."_"..x[1] then
443                                                 if not x[2] then x[2] = x[1] end
444                                                 if x[2] == pointed_nodebox then
445                                                         if x[3] then newnode = def.name.."_"..worktable.defs[x[3]][1] end
446                                                 end
447                                         end
448                                 end
449
450                                 if clicker:get_player_control().sneak then
451                                         if not minetest.registered_nodes[newnode] then return end
452                                         minetest.set_node(pos, {name=newnode, param2=node.param2})
453                                 else
454                                         minetest.item_place_node(ItemStack(wield_item), clicker, pointed_thing)
455                                 end
456
457                                 if not minetest.setting_getbool("creative_mode") then
458                                         itemstack:take_item()
459                                 end
460
461                                 return itemstack
462                         end
463                 })
464         end
465         if node:find("meselamp") then
466                 if d[3] then
467                         minetest.register_alias("default:meselamp_"..d[1], "default:glass_"..d[1])
468                 else
469                         minetest.register_alias("stairs:"..d[1].."_meselamp", "stairs:"..d[1].."_glass")
470                 end
471         elseif worktable.nodes(def) and not d[3] then
472                 minetest.register_alias(node.."_"..d[1], "stairs:"..d[1].."_"..node:match(":(.*)"))
473         end
474 end
475 end
476
477 minetest.register_abm({
478         nodenames = {"xdecor:worktable"},
479         interval = 3, chance = 1,
480         action = function(pos)
481                 local inv = minetest.get_meta(pos):get_inventory()
482                 local tool = inv:get_stack("tool", 1)
483                 local hammer = inv:get_stack("hammer", 1)
484
485                 if tool:is_empty() or hammer:is_empty() or tool:get_wear() == 0 then
486                         return
487                 end
488
489                 -- Wear : 0-65535 | 0 = new condition.
490                 tool:add_wear(-500)
491                 hammer:add_wear(700)
492
493                 inv:set_stack("tool", 1, tool)
494                 inv:set_stack("hammer", 1, hammer)
495         end
496 })
497