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