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