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