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