]> git.lizzy.rs Git - xdecor.git/blob - worktable.lua
Work Table : use pixel nodeboxes
[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, 8,  8  },{
35                                0,  15, 8,  16, 16, 16 }}},
36         {"cube",        4,  {{ 0,  0,  8,  8,  8,  16 }}},
37         {"panel",       4,  {{ 0,  0,  0,  16, 8,  8  }}},
38         {"slab",        2,  {{ 0,  0,  0,  16, 8,  16 }}},
39         {"doublepanel", 2,  {{ 0,  0,  0,  16, 8,  8  },{
40                                0,  8,  8,  16, 16, 16 }}},
41         {"halfstair",   2,  {{ 0,  0,  0,  8,  8,  16 },{
42                                0,  8,  8,  8,  16, 16 }}},
43         {"outerstair",  1,  {{ 0,  0,  0,  16, 8,  16 },{
44                                0,  8,  8,  8,  16, 16 }}},
45         {"stair",       1,  {{ 0,  0,  0,  16, 8,  16 },{
46                                0,  8,  8,  16, 16, 16 }}},
47         {"innerstair",  1,  {{ 0,  0,  0,  16, 8,  16 },{
48                                0,  8,  8,  16, 16, 16 },{
49                                0,  8,  0,  8,  16, 8  }}}
50 }
51
52 function worktable.get_recipe(item)
53         if item:find("^group:") then
54                 if item:find("wool$") or item:find("dye$") then
55                         item = item:sub(7)..":white"
56                 elseif minetest.registered_items["default:"..item:sub(7)] then
57                         item = item:gsub("group:", "default:")
58                 else
59                         for node, def in pairs(minetest.registered_items) do
60                                 if def.groups[item:match("[^,:]+$")] then
61                                         item = node
62                                 end
63                         end
64                 end
65         end
66         return item
67 end
68
69 function worktable.craftguide_formspec(meta, pagenum, item, recipe_num, filter, tab_id)
70         local inv_size = meta:get_int("inv_size")
71         local npp, i, s = 8*3, 0, 0
72         local pagemax = math.floor((inv_size - 1) / npp + 1)
73
74         if pagenum > pagemax then
75                 pagenum = 1
76         elseif pagenum == 0 then
77                 pagenum = pagemax
78         end
79
80         local formspec = [[ size[8,6.6;]
81                         tablecolumns[color;text;color;text]
82                         tableoptions[background=#00000000;highlight=#00000000;border=false]
83                         button[5.5,0;0.7,1;prev;<]
84                         button[7.3,0;0.7,1;next;>]
85                         button[4,0.2;0.7,0.5;search;?]
86                         button[4.6,0.2;0.7,0.5;clearfilter;X]
87                         button[0,0;1.5,1;backcraft;< Back]
88                         tooltip[search;Search]
89                         tooltip[clearfilter;Reset] ]]
90                         .."tabheader[0,0;tabs;All,Nodes,Tools,Items;"..tostring(tab_id)..";true;false]"..
91                         "table[6.1,0.2;1.1,0.5;pagenum;#FFFF00,"..tostring(pagenum)..
92                         ",#FFFFFF,/ "..tostring(pagemax).."]"..
93                         "field[1.8,0.32;2.6,1;filter;;"..filter.."]"..xbg
94
95         for _, name in pairs(worktable.craftguide_main_list(meta, filter, tab_id)) do
96                 if s < (pagenum - 1) * npp then
97                         s = s + 1
98                 else
99                         if i >= npp then break end
100                         formspec = formspec.."item_image_button["..(i%8)..","..
101                                         (math.floor(i/8)+1)..";1,1;"..name..";"..name..";]"
102                         i = i + 1
103                 end
104         end
105
106         if item and minetest.registered_items[item] then
107                 --print(dump(minetest.get_all_craft_recipes(item)))
108                 local items_num = #minetest.get_all_craft_recipes(item)
109                 if recipe_num > items_num then recipe_num = 1 end
110
111                 if items_num > 1 then
112                         formspec = formspec.."button[0,6;1.6,1;alternate;Alternate]"..
113                                         "label[0,5.5;Recipe "..recipe_num.." of "..items_num.."]"
114                 end
115                 
116                 local type = minetest.get_all_craft_recipes(item)[recipe_num].type
117                 if type == "cooking" then
118                         formspec = formspec.."image[3.75,4.6;0.5,0.5;default_furnace_fire_fg.png]"
119                 end
120
121                 local items = minetest.get_all_craft_recipes(item)[recipe_num].items
122                 local width = minetest.get_all_craft_recipes(item)[recipe_num].width
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[2.5,5;1,1;"..item.."]"..
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%[.*;([%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
302         if (listname == "tool" and stack:get_wear() > 0 and stackname ~= "xdecor:hammer") or
303                         (listname == "input" and worktable.allowed(nodes[mod], node)) or
304                         (listname == "hammer" and stackname == "xdecor:hammer") or
305                         listname == "storage" or listname == "trash" then
306                 if listname == "trash" then trash_delete(pos) end
307                 return stack:get_count()
308         end
309
310         return 0
311 end
312
313 function worktable.take(_, listname, _, stack, player)
314         if listname == "forms" then
315                 local inv = player:get_inventory()
316                 if inv:room_for_item("main", stack:get_name()) then
317                         return -1
318                 end
319                 return 0
320         end
321         return stack:get_count()
322 end
323
324 function worktable.move(pos, _, _, to_list, _, count)
325         if to_list == "storage" then
326                 return count
327         elseif to_list == "trash" then
328                 trash_delete(pos)
329                 return count
330         end
331         return 0
332 end
333
334 function worktable.get_output(inv, stack)
335         if inv:is_empty("input") then
336                 inv:set_list("forms", {})
337                 return
338         end
339
340         local input, output = inv:get_stack("input", 1), {}
341         for _, n in pairs(defs) do
342                 local count = math.min(n[2] * input:get_count(), input:get_stack_max())
343                 output[#output+1] = stack:get_name().."_"..n[1].." "..count
344         end
345
346         inv:set_list("forms", output)
347 end
348
349 function worktable.on_put(pos, listname, _, stack)
350         if listname == "input" then
351                 local inv = minetest.get_meta(pos):get_inventory()
352                 worktable.get_output(inv, stack)
353         end
354 end
355
356 function worktable.on_take(pos, listname, index, stack)
357         local inv = minetest.get_meta(pos):get_inventory()
358         local inputstack = inv:get_stack("input", 1)
359
360         if listname == "input" then
361                 if stack:get_name() == inputstack:get_name() then
362                         worktable.get_output(inv, stack)
363                 else
364                         inv:set_list("forms", {})
365                 end
366         elseif listname == "forms" then
367                 inputstack:take_item(math.ceil(stack:get_count() / defs[index][2]))
368                 inv:set_stack("input", 1, inputstack)
369                 worktable.get_output(inv, inputstack)
370         end
371 end
372
373 xdecor.register("worktable", {
374         description = "Work Table",
375         groups = {cracky=2, choppy=2, oddly_breakable_by_hand=1},
376         sounds = default.node_sound_wood_defaults(),
377         tiles = {
378                 "xdecor_worktable_top.png", "xdecor_worktable_top.png",
379                 "xdecor_worktable_sides.png", "xdecor_worktable_sides.png",
380                 "xdecor_worktable_front.png", "xdecor_worktable_front.png"
381         },
382         on_rotate = screwdriver.rotate_simple,
383         can_dig = worktable.dig,
384         on_construct = worktable.construct,
385         on_receive_fields = worktable.fields,
386         on_metadata_inventory_put = worktable.on_put,
387         on_metadata_inventory_take = worktable.on_take,
388         allow_metadata_inventory_put = worktable.put,
389         allow_metadata_inventory_take = worktable.take,
390         allow_metadata_inventory_move = worktable.move
391 })
392
393 for _, d in pairs(defs) do
394 for mod, n in pairs(nodes) do
395 for name in n:gmatch("[%w_]+") do
396         local ndef = minetest.registered_nodes[mod..":"..name]
397         if ndef then
398                 local groups, tiles, light = {}, {}
399                 groups.not_in_creative_inventory = 1
400
401                 for k, v in pairs(ndef.groups) do
402                         if k ~= "wood" and k ~= "stone" and k ~= "level" then
403                                 groups[k] = v
404                         end
405                 end
406
407                 if #ndef.tiles > 1 and not ndef.drawtype:find("glass") then
408                         tiles = ndef.tiles
409                 else
410                         tiles = {ndef.tiles[1]}
411                 end
412
413                 if ndef.light_source > 3 then
414                         light = ndef.light_source - 1
415                 end
416
417                 minetest.register_node(":"..mod..":"..name.."_"..d[1], {
418                         description = ndef.description.." "..d[1]:gsub("^%l", string.upper),
419                         paramtype = "light",
420                         paramtype2 = "facedir",
421                         drawtype = "nodebox",
422                         light_source = light,
423                         sounds = ndef.sounds,
424                         tiles = tiles,
425                         groups = groups,
426                         node_box = xdecor.pixelnodebox(16, d[3]),
427                         sunlight_propagates = true,
428                         on_place = minetest.rotate_node
429                 })
430         end
431 end
432 end
433 end
434
435 minetest.register_abm({
436         nodenames = {"xdecor:worktable"},
437         interval = 3, chance = 1,
438         action = function(pos)
439                 local inv = minetest.get_meta(pos):get_inventory()
440                 local tool = inv:get_stack("tool", 1)
441                 local hammer = inv:get_stack("hammer", 1)
442
443                 if tool:is_empty() or hammer:is_empty() or tool:get_wear() == 0 then
444                         return
445                 end
446
447                 -- Wear : 0-65535 | 0 = new condition.
448                 tool:add_wear(-500)
449                 hammer:add_wear(700)
450
451                 inv:set_stack("tool", 1, tool)
452                 inv:set_stack("hammer", 1, hammer)
453         end
454 })
455