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