]> git.lizzy.rs Git - xdecor.git/blob - worktable.lua
Move enchant variables in table
[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:sub(7)..":white"
38                 elseif minetest.registered_items["default:"..item:sub(7)] then
39                         item = item:gsub("group:", "default:")
40                 else
41                         for node, def in pairs(minetest.registered_items) do
42                                 if def.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
134         return id_category[tab_id] or id_category[1]
135 end
136
137 function worktable.craftguide_main_list(meta, filter, tab_id)
138         local items_list = {}
139         for name, def in pairs(tab_category(tab_id)) do
140                 if not (def.groups.not_in_creative_inventory == 1) and
141                                 minetest.get_craft_recipe(name).items and
142                                 def.description and def.description ~= "" and
143                                 (not filter or def.name:find(filter, 1, true)) then
144                         items_list[#items_list+1] = name
145                 end
146         end
147
148         meta:set_int("inv_size", #items_list)
149         table.sort(items_list)
150         return items_list
151 end
152
153 worktable.formspecs = {
154         crafting = function(meta)
155                 meta:set_string("formspec", [[ size[8,7;]
156                         image[5,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
157                         image[0.06,2.12;0.8,0.8;trash_icon.png]
158                         button[0,0;1.5,1;back;< Back]
159                         button[0,0.85;1.5,1;craftguide;Guide]
160                         list[context;trash;0,2;1,1;]
161                         list[current_player;main;0,3.3;8,4;]
162                         list[current_player;craft;2,0;3,3;]
163                         list[current_player;craftpreview;6,1;1,1;]
164                         listring[current_player;main]
165                         listring[current_player;craft] ]]
166                         ..xbg..default.get_hotbar_bg(0,3.3))
167         end,
168         storage = function(meta)
169                 meta:set_string("formspec", [[ size[8,7]
170                         image[7.06,0.12;0.8,0.8;trash_icon.png]
171                         list[context;trash;7,0;1,1;]
172                         list[context;storage;0,1;8,2;]
173                         list[current_player;main;0,3.25;8,4;]
174                         listring[context;storage]
175                         listring[current_player;main]
176                         button[0,0;1.5,1;back;< Back] ]]
177                         ..xbg..default.get_hotbar_bg(0,3.25))
178         end,
179         main = function(meta)
180                 meta:set_string("formspec", [[ size[8,7;]
181                         label[0.9,1.23;Cut]
182                         label[0.9,2.23;Repair]
183                         box[-0.05,1;2.05,0.9;#555555]
184                         box[-0.05,2;2.05,0.9;#555555]
185                         image[3,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
186                         image[0,1;1,1;worktable_saw.png]
187                         image[0,2;1,1;worktable_anvil.png]
188                         image[3,2;1,1;hammer_layout.png]
189                         list[context;input;2,1;1,1;]
190                         list[context;tool;2,2;1,1;]
191                         list[context;hammer;3,2;1,1;]
192                         list[context;forms;4,0;4,3;]
193                         list[current_player;main;0,3.25;8,4;]
194                         button[0,0;2,1;craft;Crafting]
195                         button[2,0;2,1;storage;Storage] ]]
196                         ..xbg..default.get_hotbar_bg(0,3.25))
197         end
198 }
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("trash", 1)
206         inv:set_size("input", 1)
207         inv:set_size("hammer", 1)
208         inv:set_size("forms", 4*3)
209         inv:set_size("storage", 8*2)
210         meta:set_string("infotext", "Work Table")
211
212         worktable.formspecs.main(meta)
213 end
214
215 function worktable.fields(pos, _, fields)
216         if fields.quit then return end
217         local meta = minetest.get_meta(pos)
218         local formspec = meta:to_table().fields.formspec
219         local filter = formspec:match("filter;;([%w_:]+)") or ""
220         local pagenum = tonumber(formspec:match("#FFFF00,(%d+)")) or 1
221         local tab_id = tonumber(formspec:match("tabheader%[.*;(%d+)%;.*%]")) or 1
222
223         if fields.back then
224                 worktable.formspecs.main(meta)
225         elseif fields.craft or fields.backcraft then
226                 worktable.formspecs.crafting(meta)
227         elseif fields.storage then
228                 worktable.formspecs.storage(meta)
229         elseif fields.craftguide or fields.clearfilter then
230                 worktable.craftguide_main_list(meta, nil, 1)
231                 worktable.craftguide_formspec(meta, 1, nil, 1, "", 1)
232         elseif fields.alternate then
233                 local item = formspec:match("item_image%[.*;([%w_:]+)%]") or ""
234                 local recipe_num = tonumber(formspec:match("Recipe%s(%d+)")) or 1
235                 recipe_num = recipe_num + 1
236                 worktable.craftguide_formspec(meta, pagenum, item, recipe_num, filter, tab_id)
237         elseif fields.search then
238                 worktable.craftguide_main_list(meta, fields.filter:lower(), tab_id)
239                 worktable.craftguide_formspec(meta, 1, nil, 1, fields.filter:lower(), tab_id)
240         elseif fields.tabs then
241                 worktable.craftguide_main_list(meta, filter, tonumber(fields.tabs))
242                 worktable.craftguide_formspec(meta, 1, nil, 1, filter, tonumber(fields.tabs))
243         elseif fields.prev or fields.next then
244                 if fields.prev then
245                         pagenum = pagenum - 1
246                 else
247                         pagenum = pagenum + 1
248                 end
249                 worktable.craftguide_formspec(meta, pagenum, nil, 1, filter, tab_id)
250         else
251                 for item in pairs(fields) do
252                         if item:match(".-:") and minetest.get_craft_recipe(item).items then
253                                 worktable.craftguide_formspec(meta, pagenum, item, 1, filter, tab_id)
254                         end
255                 end
256         end
257 end
258
259 function worktable.dig(pos)
260         local inv = minetest.get_meta(pos):get_inventory()
261         return inv:is_empty("input") and inv:is_empty("hammer") and
262                 inv:is_empty("tool") and inv:is_empty("storage")
263 end
264
265 function worktable.allowed(table, element)
266         if table then
267                 for _, value in pairs(table) do
268                         if value == element then
269                                 return true
270                         end
271                 end
272         end
273         return false
274 end
275
276 local function trash_delete(pos)
277         local inv = minetest.get_meta(pos):get_inventory()
278         minetest.after(0, function()
279                 inv:set_stack("trash", 1, "")
280         end)
281 end
282
283 function worktable.put(pos, listname, _, stack)
284         local stackname = stack:get_name()
285         local mod, node = stackname:match("(.*):(.*)")
286
287         if (listname == "tool" and stack:get_wear() > 0 and stackname ~= "xdecor:hammer") or
288                         (listname == "input" and worktable.allowed(nodes[mod], node)) or
289                         (listname == "hammer" and stackname == "xdecor:hammer") or
290                         listname == "storage" or listname == "trash" then
291                 if listname == "trash" then trash_delete(pos) end
292                 return stack:get_count()
293         end
294
295         return 0
296 end
297
298 function worktable.take(_, listname, _, stack, player)
299         if listname == "forms" then
300                 local inv = player:get_inventory()
301                 if inv:room_for_item("main", stack:get_name()) then
302                         return -1
303                 end
304                 return 0
305         end
306         return stack:get_count()
307 end
308
309 function worktable.move(pos, _, _, to_list, _, count)
310         if to_list == "storage" then
311                 return count
312         elseif to_list == "trash" then
313                 trash_delete(pos)
314                 return count
315         end
316         return 0
317 end
318
319 function worktable.get_output(inv, stack)
320         if inv:is_empty("input") then
321                 inv:set_list("forms", {})
322                 return
323         end
324
325         local input, output = inv:get_stack("input", 1), {}
326         for _, n in pairs(def) do
327                 local count = math.min(n[2] * input:get_count(), input:get_stack_max())
328                 output[#output+1] = stack:get_name().."_"..n[1].." "..count
329         end
330
331         inv:set_list("forms", output)
332 end
333
334 function worktable.on_put(pos, listname, _, stack)
335         if listname == "input" then
336                 local inv = minetest.get_meta(pos):get_inventory()
337                 worktable.get_output(inv, stack)
338         end
339 end
340
341 function worktable.on_take(pos, listname, index, stack)
342         local inv = minetest.get_meta(pos):get_inventory()
343         local inputstack = inv:get_stack("input", 1)
344
345         if listname == "input" then
346                 if stack:get_name() == inputstack:get_name() then
347                         worktable.get_output(inv, stack)
348                 else
349                         inv:set_list("forms", {})
350                 end
351         elseif listname == "forms" then
352                 inputstack:take_item(math.ceil(stack:get_count() / def[index][2]))
353                 inv:set_stack("input", 1, inputstack)
354                 worktable.get_output(inv, inputstack)
355         end
356 end
357
358 xdecor.register("worktable", {
359         description = "Work Table",
360         groups = {cracky=2, choppy=2, oddly_breakable_by_hand=1},
361         sounds = default.node_sound_wood_defaults(),
362         tiles = {
363                 "xdecor_worktable_top.png", "xdecor_worktable_top.png",
364                 "xdecor_worktable_sides.png", "xdecor_worktable_sides.png",
365                 "xdecor_worktable_front.png", "xdecor_worktable_front.png"
366         },
367         on_rotate = screwdriver.rotate_simple,
368         can_dig = worktable.dig,
369         on_construct = worktable.construct,
370         on_receive_fields = worktable.fields,
371         on_metadata_inventory_put = worktable.on_put,
372         on_metadata_inventory_take = worktable.on_take,
373         allow_metadata_inventory_put = worktable.put,
374         allow_metadata_inventory_take = worktable.take,
375         allow_metadata_inventory_move = worktable.move
376 })
377
378 for _, d in pairs(def) do
379 for mod, n in pairs(nodes) do
380 for _, name in pairs(n) do
381         local ndef = minetest.registered_nodes[mod..":"..name]
382         if ndef then
383                 local groups, tiles, light = {}, {}
384                 groups.not_in_creative_inventory = 1
385
386                 for k, v in pairs(ndef.groups) do
387                         if k ~= "wood" and k ~= "stone" and k ~= "level" then
388                                 groups[k] = v
389                         end
390                 end
391
392                 if #ndef.tiles > 1 and not ndef.drawtype:find("glass") then
393                         tiles = ndef.tiles
394                 else
395                         tiles = {ndef.tiles[1]}
396                 end
397
398                 if ndef.light_source > 3 then
399                         light = ndef.light_source - 1
400                 end
401
402                 minetest.register_node(":"..mod..":"..name.."_"..d[1], {
403                         description = ndef.description.." "..d[1]:gsub("^%l", string.upper),
404                         paramtype = "light",
405                         paramtype2 = "facedir",
406                         drawtype = "nodebox",
407                         light_source = light,
408                         sounds = ndef.sounds,
409                         tiles = tiles,
410                         groups = groups,
411                         node_box = {type = "fixed", fixed = d[3]},
412                         sunlight_propagates = true,
413                         on_place = minetest.rotate_node
414                 })
415         end
416 end
417 end
418 end
419
420 minetest.register_abm({
421         nodenames = {"xdecor:worktable"},
422         interval = 3, chance = 1,
423         action = function(pos)
424                 local inv = minetest.get_meta(pos):get_inventory()
425                 local tool = inv:get_stack("tool", 1)
426                 local hammer = inv:get_stack("hammer", 1)
427
428                 if tool:is_empty() or hammer:is_empty() or tool:get_wear() == 0 then
429                         return
430                 end
431
432                 -- Wear : 0-65535 | 0 = new condition.
433                 tool:add_wear(-500)
434                 hammer:add_wear(700)
435
436                 inv:set_stack("tool", 1, tool)
437                 inv:set_stack("hammer", 1, hammer)
438         end
439 })
440