]> git.lizzy.rs Git - xdecor.git/blob - src/workbench.lua
Add settings and compatibility with moreblocks and stairs
[xdecor.git] / src / workbench.lua
1 local workbench = {}
2 WB = {}
3 screwdriver = screwdriver or {}
4 local min, ceil = math.min, math.ceil
5 local registered_nodes = minetest.registered_nodes
6
7 -- Nodes allowed to be cut
8 -- Only the regular, solid blocks without metas or explosivity can be cut
9 local nodes = {}
10 for node, def in pairs(registered_nodes) do
11         if xdecor.stairs_valid_def(def) then
12                 nodes[#nodes+1] = node
13         end
14 end
15
16 -- Optionally, you can register custom cuttable nodes in the workbench
17 WB.custom_nodes_register = {
18         -- "default:leaves",
19 }
20
21 setmetatable(nodes, {
22         __concat = function(t1, t2)
23                 for i=1, #t2 do
24                         t1[#t1+1] = t2[i]
25                 end
26                 return t1
27         end
28 })
29
30 nodes = nodes..WB.custom_nodes_register
31
32 -- Nodeboxes definitions
33 workbench.defs = {
34         -- Name       Yield   X  Y   Z  W   H  L
35         {"nanoslab",    16, { 0, 0,  0, 8,  1, 8  }},
36         {"micropanel",  16, { 0, 0,  0, 16, 1, 8  }},
37         {"microslab",   8,  { 0, 0,  0, 16, 1, 16 }},
38         {"thinstair",   8,  { 0, 7,  0, 16, 1, 8  },
39                             { 0, 15, 8, 16, 1, 8  }},
40         {"cube",        4,  { 0, 0,  0, 8,  8, 8  }},
41         {"panel",       4,  { 0, 0,  0, 16, 8, 8  }},
42         {"slab",        2,  nil                   },
43         {"doublepanel", 2,  { 0, 0,  0, 16, 8, 8  },
44                             { 0, 8,  8, 16, 8, 8  }},
45         {"halfstair",   2,  { 0, 0,  0, 8,  8, 16 },
46                             { 0, 8,  8, 8,  8, 8  }},
47         {"outerstair",  1,  { 0, 0,  0, 16, 8, 16 },
48                             { 0, 8,  8, 8,  8, 8  }},
49         {"stair",       1,  nil                   },
50         {"innerstair",  1,  { 0, 0,  0, 16, 8, 16 },
51                             { 0, 8,  8, 16, 8, 8  },
52                             { 0, 8,  0, 8,  8, 8  }}
53 }
54
55 -- Tools allowed to be repaired
56 function workbench:repairable(stack)
57         local tools = {"pick", "axe", "shovel", "sword", "hoe", "armor", "shield"}
58         for _, t in pairs(tools) do
59                 if stack:find(t) then return true end
60         end
61         return false
62 end
63
64 function workbench:get_output(inv, input, name)
65         local output = {}
66         for i=1, #self.defs do
67                 local nbox = self.defs[i]
68                 local count = min(nbox[2] * input:get_count(), input:get_stack_max())
69                 local item = name.."_"..nbox[1]
70                 item = nbox[3] and item or "stairs:"..nbox[1].."_"..name:match(":(.*)")
71                 output[#output+1] = item.." "..count
72         end
73         inv:set_list("forms", output)
74 end
75
76 local formspecs = {
77         -- Main formspec
78         [[ label[0.9,1.23;Cut]
79            label[0.9,2.23;Repair]
80            box[-0.05,1;2.05,0.9;#555555]
81            box[-0.05,2;2.05,0.9;#555555]
82            button[0,0;2,1;craft;Crafting]
83            button[2,0;2,1;storage;Storage]
84            image[3,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
85            image[0,1;1,1;worktable_saw.png]
86            image[0,2;1,1;worktable_anvil.png]
87            image[3,2;1,1;hammer_layout.png]
88            list[context;input;2,1;1,1;]
89            list[context;tool;2,2;1,1;]
90            list[context;hammer;3,2;1,1;]
91            list[context;forms;4,0;4,3;]
92            listring[current_player;main]
93            listring[context;tool]
94            listring[current_player;main]
95            listring[context;hammer]
96            listring[current_player;main]
97            listring[context;forms]
98            listring[current_player;main]
99            listring[context;input] ]],
100         -- Crafting formspec
101         [[ image[5,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
102            button[0,0;1.5,1;back;< Back]
103            list[current_player;craft;2,0;3,3;]
104            list[current_player;craftpreview;6,1;1,1;]
105            listring[current_player;main]
106            listring[current_player;craft] ]],
107         -- Storage formspec
108         [[ list[context;storage;0,1;8,2;]
109            button[0,0;1.5,1;back;< Back]
110            listring[context;storage]
111            listring[current_player;main] ]]
112 }
113
114 function workbench:set_formspec(meta, id)
115         meta:set_string("formspec",
116                         "size[8,7;]list[current_player;main;0,3.25;8,4;]"..
117                         formspecs[id]..xbg..default.get_hotbar_bg(0,3.25))
118 end
119
120 function workbench.construct(pos)
121         local meta = minetest.get_meta(pos)
122         local inv = meta:get_inventory()
123
124         inv:set_size("tool", 1)
125         inv:set_size("input", 1)
126         inv:set_size("hammer", 1)
127         inv:set_size("forms", 4*3)
128         inv:set_size("storage", 8*2)
129
130         meta:set_string("infotext", "Work Bench")
131         workbench:set_formspec(meta, 1)
132 end
133
134 function workbench.fields(pos, _, fields)
135         if fields.quit then return end
136         local meta = minetest.get_meta(pos)
137         local id = fields.back and 1 or
138                    fields.craft and 2 or
139                    fields.storage and 3
140         if not id then return end
141         workbench:set_formspec(meta, id)
142 end
143
144 function workbench.dig(pos)
145         local inv = minetest.get_meta(pos):get_inventory()
146         return inv:is_empty("input") and inv:is_empty("hammer") and
147                inv:is_empty("tool") and inv:is_empty("storage")
148 end
149
150 function workbench.timer(pos)
151         local timer = minetest.get_node_timer(pos)
152         local inv = minetest.get_meta(pos):get_inventory()
153         local tool = inv:get_stack("tool", 1)
154         local hammer = inv:get_stack("hammer", 1)
155
156         if tool:is_empty() or hammer:is_empty() or tool:get_wear() == 0 then
157                 timer:stop()
158                 return
159         end
160
161         -- Tool's wearing range: 0-65535 | 0 = new condition
162         tool:add_wear(-500)
163         hammer:add_wear(700)
164
165         inv:set_stack("tool", 1, tool)
166         inv:set_stack("hammer", 1, hammer)
167         return true
168 end
169
170 function workbench.put(_, listname, _, stack)
171         local stackname = stack:get_name()
172         if (listname == "tool" and stack:get_wear() > 0 and
173             workbench:repairable(stackname)) or
174            (listname == "input" and registered_nodes[stackname.."_cube"]) or
175            (listname == "hammer" and stackname == "xdecor:hammer") or
176             listname == "storage" then
177                 return stack:get_count()
178         end
179         return 0
180 end
181
182 function workbench.move(_, from_list, _, to_list, _, count)
183         return (to_list == "storage" and from_list ~= "forms") and count or 0
184 end
185
186 function workbench.on_put(pos, listname, _, stack)
187         local inv = minetest.get_meta(pos):get_inventory()
188         if listname == "input" then
189                 local input = inv:get_stack("input", 1)
190                 workbench:get_output(inv, input, stack:get_name())
191         elseif listname == "tool" or listname == "hammer" then
192                 local timer = minetest.get_node_timer(pos)
193                 timer:start(3.0)
194         end
195 end
196
197 function workbench.on_take(pos, listname, index, stack, player)
198         local inv = minetest.get_meta(pos):get_inventory()
199         local input = inv:get_stack("input", 1)
200         local inputname = input:get_name()
201         local stackname = stack:get_name()
202
203         if listname == "input" then
204                 if stackname == inputname then
205                         workbench:get_output(inv, input, stackname)
206                 else
207                         inv:set_list("forms", {})
208                 end
209         elseif listname == "forms" then
210                 local fromstack = inv:get_stack(listname, index)
211                 if not fromstack:is_empty() and fromstack:get_name() ~= stackname then
212                         local player_inv = player:get_inventory()
213                         if player_inv:room_for_item("main", fromstack) then
214                                 player_inv:add_item("main", fromstack)
215                         end
216                 end
217
218                 input:take_item(ceil(stack:get_count() / workbench.defs[index][2]))
219                 inv:set_stack("input", 1, input)
220                 workbench:get_output(inv, input, inputname)
221         end
222 end
223
224 xdecor.register("workbench", {
225         description = "Work Bench",
226         groups = {cracky=2, choppy=2, oddly_breakable_by_hand=1},
227         sounds = default.node_sound_wood_defaults(),
228         tiles = {"xdecor_workbench_top.png",   "xdecor_workbench_top.png",
229                  "xdecor_workbench_sides.png", "xdecor_workbench_sides.png",
230                  "xdecor_workbench_front.png", "xdecor_workbench_front.png"},
231         on_rotate = screwdriver.rotate_simple,
232         can_dig = workbench.dig,
233         on_timer = workbench.timer,
234         on_construct = workbench.construct,
235         on_receive_fields = workbench.fields,
236         on_metadata_inventory_put = workbench.on_put,
237         on_metadata_inventory_take = workbench.on_take,
238         allow_metadata_inventory_put = workbench.put,
239         allow_metadata_inventory_move = workbench.move
240 })
241
242 for _, d in pairs(workbench.defs) do
243 for i=1, #nodes do
244         local node = nodes[i]
245         local def = registered_nodes[node]
246
247         if d[3] then
248                 local groups = {}
249                 local tiles
250                 groups.not_in_creative_inventory = 1
251
252                 for k, v in pairs(def.groups) do
253                         if k ~= "wood" and k ~= "stone" and k ~= "level" then
254                                 groups[k] = v
255                         end
256                 end
257
258                 if def.tiles then
259                         if #def.tiles > 1 and (def.drawtype:sub(1,5) ~= "glass") then
260                                 tiles = def.tiles
261                         else
262                                 tiles = {def.tiles[1]}
263                         end
264                 else
265                         tiles = {def.tile_images[1]}
266                 end
267
268                 if not registered_nodes["stairs:slab_"..node:match(":(.*)")] then
269                         stairs.register_stair_and_slab(node:match(":(.*)"), node,
270                                 groups, tiles, def.description.." Stair",
271                                 def.description.." Slab", def.sounds)
272                 end
273
274                 minetest.register_node(":"..node.."_"..d[1], {
275                         description = def.description.." "..d[1]:gsub("^%l", string.upper),
276                         paramtype = "light",
277                         paramtype2 = "facedir",
278                         drawtype = "nodebox",
279                         sounds = def.sounds,
280                         tiles = tiles,
281                         groups = groups,
282                         -- `unpack` has been changed to `table.unpack` in newest Lua versions
283                         node_box = xdecor.pixelbox(16, {unpack(d, 3)}),
284                         sunlight_propagates = true,
285                         on_place = minetest.rotate_node
286                 })
287         end
288 end
289 end
290
291 -- Craft items
292
293 minetest.register_tool("xdecor:hammer", {
294         description = "Hammer",
295         inventory_image = "xdecor_hammer.png",
296         wield_image = "xdecor_hammer.png",
297         on_use = function() do return end end
298 })
299
300 -- Recipes
301
302 minetest.register_craft({
303         output = "xdecor:hammer",
304         recipe = {
305                 {"default:steel_ingot", "group:stick", "default:steel_ingot"},
306                 {"", "group:stick", ""}
307         }
308 })
309
310 minetest.register_craft({
311         output = "xdecor:workbench",
312         recipe = {
313                 {"group:wood", "group:wood"},
314                 {"group:wood", "group:wood"}
315         }
316 })