]> git.lizzy.rs Git - xdecor.git/blob - src/workbench.lua
ad69361fe27ba4058a932122545017888ee1687c
[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
74         inv:set_list("forms", output)
75 end
76
77 local formspecs = {
78         -- Main formspec
79         [[ label[0.9,1.23;Cut]
80            label[0.9,2.23;Repair]
81            box[-0.05,1;2.05,0.9;#555555]
82            box[-0.05,2;2.05,0.9;#555555]
83            button[0,0;2,1;craft;Crafting]
84            button[2,0;2,1;storage;Storage]
85            image[3,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
86            image[0,1;1,1;worktable_saw.png]
87            image[0,2;1,1;worktable_anvil.png]
88            image[3,2;1,1;hammer_layout.png]
89            list[context;input;2,1;1,1;]
90            list[context;tool;2,2;1,1;]
91            list[context;hammer;3,2;1,1;]
92            list[context;forms;4,0;4,3;]
93            listring[current_player;main]
94            listring[context;tool]
95            listring[current_player;main]
96            listring[context;hammer]
97            listring[current_player;main]
98            listring[context;forms]
99            listring[current_player;main]
100            listring[context;input] ]],
101         -- Crafting formspec
102         [[ image[5,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
103            button[0,0;1.5,1;back;< Back]
104            list[current_player;craft;2,0;3,3;]
105            list[current_player;craftpreview;6,1;1,1;]
106            listring[current_player;main]
107            listring[current_player;craft] ]],
108         -- Storage formspec
109         [[ list[context;storage;0,1;8,2;]
110            button[0,0;1.5,1;back;< Back]
111            listring[context;storage]
112            listring[current_player;main] ]]
113 }
114
115 function workbench:set_formspec(meta, id)
116         meta:set_string("formspec",
117                         "size[8,7;]list[current_player;main;0,3.25;8,4;]"..
118                         formspecs[id]..xbg..default.get_hotbar_bg(0,3.25))
119 end
120
121 function workbench.construct(pos)
122         local meta = minetest.get_meta(pos)
123         local inv = meta:get_inventory()
124
125         inv:set_size("tool", 1)
126         inv:set_size("input", 1)
127         inv:set_size("hammer", 1)
128         inv:set_size("forms", 4*3)
129         inv:set_size("storage", 8*2)
130
131         meta:set_string("infotext", "Work Bench")
132         workbench:set_formspec(meta, 1)
133 end
134
135 function workbench.fields(pos, _, fields)
136         if fields.quit then return end
137         local meta = minetest.get_meta(pos)
138         local id = fields.back and 1 or
139                    fields.craft and 2 or
140                    fields.storage and 3
141         if not id then return end
142         workbench:set_formspec(meta, id)
143 end
144
145 function workbench.dig(pos)
146         local inv = minetest.get_meta(pos):get_inventory()
147         return inv:is_empty("input") and inv:is_empty("hammer") and
148                inv:is_empty("tool") and inv:is_empty("storage")
149 end
150
151 function workbench.timer(pos)
152         local timer = minetest.get_node_timer(pos)
153         local inv = minetest.get_meta(pos):get_inventory()
154         local tool = inv:get_stack("tool", 1)
155         local hammer = inv:get_stack("hammer", 1)
156
157         if tool:is_empty() or hammer:is_empty() or tool:get_wear() == 0 then
158                 timer:stop()
159                 return
160         end
161
162         -- Tool's wearing range: 0-65535 | 0 = new condition
163         tool:add_wear(-500)
164         hammer:add_wear(700)
165
166         inv:set_stack("tool", 1, tool)
167         inv:set_stack("hammer", 1, hammer)
168         return true
169 end
170
171 function workbench.put(_, listname, _, stack)
172         local stackname = stack:get_name()
173         if (listname == "tool" and stack:get_wear() > 0 and
174             workbench:repairable(stackname)) or
175            (listname == "input" and registered_nodes[stackname.."_cube"]) or
176            (listname == "hammer" and stackname == "xdecor:hammer") or
177             listname == "storage" then
178                 return stack:get_count()
179         end
180         return 0
181 end
182
183 function workbench.move(_, from_list, _, to_list, _, count)
184         return (to_list == "storage" and from_list ~= "forms") and count or 0
185 end
186
187 function workbench.on_put(pos, listname, _, stack)
188         local inv = minetest.get_meta(pos):get_inventory()
189         if listname == "input" then
190                 local input = inv:get_stack("input", 1)
191                 workbench:get_output(inv, input, stack:get_name())
192         elseif listname == "tool" or listname == "hammer" then
193                 local timer = minetest.get_node_timer(pos)
194                 timer:start(3.0)
195         end
196 end
197
198 function workbench.on_take(pos, listname, index, stack, player)
199         local inv = minetest.get_meta(pos):get_inventory()
200         local input = inv:get_stack("input", 1)
201         local inputname = input:get_name()
202         local stackname = stack:get_name()
203
204         if listname == "input" then
205                 if stackname == inputname and registered_nodes[inputname.."_cube"] then
206                         workbench:get_output(inv, input, stackname)
207                 else
208                         inv:set_list("forms", {})
209                 end
210         elseif listname == "forms" then
211                 local fromstack = inv:get_stack(listname, index)
212                 if not fromstack:is_empty() and fromstack:get_name() ~= stackname then
213                         local player_inv = player:get_inventory()
214                         if player_inv:room_for_item("main", fromstack) then
215                                 player_inv:add_item("main", fromstack)
216                         end
217                 end
218
219                 input:take_item(ceil(stack:get_count() / workbench.defs[index][2]))
220                 inv:set_stack("input", 1, input)
221                 workbench:get_output(inv, input, inputname)
222         end
223 end
224
225 xdecor.register("workbench", {
226         description = "Work Bench",
227         groups = {cracky=2, choppy=2, oddly_breakable_by_hand=1},
228         sounds = default.node_sound_wood_defaults(),
229         tiles = {"xdecor_workbench_top.png",   "xdecor_workbench_top.png",
230                  "xdecor_workbench_sides.png", "xdecor_workbench_sides.png",
231                  "xdecor_workbench_front.png", "xdecor_workbench_front.png"},
232         on_rotate = screwdriver.rotate_simple,
233         can_dig = workbench.dig,
234         on_timer = workbench.timer,
235         on_construct = workbench.construct,
236         on_receive_fields = workbench.fields,
237         on_metadata_inventory_put = workbench.on_put,
238         on_metadata_inventory_take = workbench.on_take,
239         allow_metadata_inventory_put = workbench.put,
240         allow_metadata_inventory_move = workbench.move
241 })
242
243 for _, d in pairs(workbench.defs) do
244 for i=1, #nodes do
245         local node = nodes[i]
246         local def = registered_nodes[node]
247
248         if d[3] then
249                 local groups = {}
250                 local tiles
251                 groups.not_in_creative_inventory = 1
252
253                 for k, v in pairs(def.groups) do
254                         if k ~= "wood" and k ~= "stone" and k ~= "level" then
255                                 groups[k] = v
256                         end
257                 end
258
259                 if def.tiles then
260                         if #def.tiles > 1 and (def.drawtype:sub(1,5) ~= "glass") then
261                                 tiles = def.tiles
262                         else
263                                 tiles = {def.tiles[1]}
264                         end
265                 else
266                         tiles = {def.tile_images[1]}
267                 end
268
269                 if not registered_nodes["stairs:slab_"..node:match(":(.*)")] then
270                         stairs.register_stair_and_slab(node:match(":(.*)"), node,
271                                 groups, tiles, def.description.." Stair",
272                                 def.description.." Slab", def.sounds)
273                 end
274
275                 minetest.register_node(":"..node.."_"..d[1], {
276                         description = def.description.." "..d[1]:gsub("^%l", string.upper),
277                         paramtype = "light",
278                         paramtype2 = "facedir",
279                         drawtype = "nodebox",
280                         sounds = def.sounds,
281                         tiles = tiles,
282                         groups = groups,
283                         -- `unpack` has been changed to `table.unpack` in newest Lua versions
284                         node_box = xdecor.pixelbox(16, {unpack(d, 3)}),
285                         sunlight_propagates = true,
286                         on_place = minetest.rotate_node
287                 })
288         end
289 end
290 end
291
292 -- Craft items
293
294 minetest.register_tool("xdecor:hammer", {
295         description = "Hammer",
296         inventory_image = "xdecor_hammer.png",
297         wield_image = "xdecor_hammer.png",
298         on_use = function() do return end end
299 })
300
301 -- Recipes
302
303 minetest.register_craft({
304         output = "xdecor:hammer",
305         recipe = {
306                 {"default:steel_ingot", "group:stick", "default:steel_ingot"},
307                 {"", "group:stick", ""}
308         }
309 })
310
311 minetest.register_craft({
312         output = "xdecor:workbench",
313         recipe = {
314                 {"group:wood", "group:wood"},
315                 {"group:wood", "group:wood"}
316         }
317 })