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