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