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