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