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