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