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