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