]> git.lizzy.rs Git - xdecor.git/blob - src/workbench.lua
Fix "xpanes_space.png" texture not found (#125)
[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[i] = 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.allow_put(pos, listname, index, stack, player)
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.on_put(pos, listname, index, stack, player)
201         local inv = minetest.get_meta(pos):get_inventory()
202         if listname == "input" then
203                 local input = inv:get_stack("input", 1)
204                 workbench:get_output(inv, input, stack:get_name())
205         elseif listname == "tool" or listname == "hammer" then
206                 local timer = minetest.get_node_timer(pos)
207                 timer:start(3.0)
208         end
209 end
210
211 function workbench.allow_move(pos, from_list, from_index, to_list, to_index, count, player)
212         return (to_list == "storage" and from_list ~= "forms") and count or 0
213 end
214
215 function workbench.on_move(pos, from_list, from_index, to_list, to_index, count, player)
216         local meta = minetest.get_meta(pos)
217         local inv = meta:get_inventory()
218         local from_stack = inv:get_stack(from_list, from_index)
219         local to_stack = inv:get_stack(to_list, to_index)
220
221         workbench.on_take(pos, from_list, from_index, from_stack, player)
222         workbench.on_put(pos, to_list, to_index, to_stack, player)
223 end
224
225 function workbench.allow_take(pos, listname, index, stack, player)
226         return stack:get_count()
227 end
228
229 function workbench.on_take(pos, listname, index, stack, player)
230         local inv = minetest.get_meta(pos):get_inventory()
231         local input = inv:get_stack("input", 1)
232         local inputname = input:get_name()
233         local stackname = stack:get_name()
234
235         if listname == "input" then
236                 if stackname == inputname and registered_nodes[inputname .. "_cube"] then
237                         workbench:get_output(inv, input, stackname)
238                 else
239                         inv:set_list("forms", {})
240                 end
241         elseif listname == "forms" then
242                 local fromstack = inv:get_stack(listname, index)
243                 if not fromstack:is_empty() and fromstack:get_name() ~= stackname then
244                         local player_inv = player:get_inventory()
245                         if player_inv:room_for_item("main", fromstack) then
246                                 player_inv:add_item("main", fromstack)
247                         end
248                 end
249
250                 input:take_item(ceil(stack:get_count() / workbench.defs[index][2]))
251                 inv:set_stack("input", 1, input)
252                 workbench:get_output(inv, input, inputname)
253         end
254 end
255
256 xdecor.register("workbench", {
257         description = "Work Bench",
258         groups = {cracky = 2, choppy = 2, oddly_breakable_by_hand = 1},
259         sounds = default.node_sound_wood_defaults(),
260         tiles = {
261                 "xdecor_workbench_top.png","xdecor_workbench_top.png",
262                 "xdecor_workbench_sides.png", "xdecor_workbench_sides.png",
263                 "xdecor_workbench_front.png", "xdecor_workbench_front.png"
264         },
265         on_rotate = screwdriver.rotate_simple,
266         can_dig = workbench.dig,
267         on_timer = workbench.timer,
268         on_construct = workbench.construct,
269         on_receive_fields = workbench.fields,
270         on_metadata_inventory_put = workbench.on_put,
271         on_metadata_inventory_take = workbench.on_take,
272         on_metadata_inventory_move = workbench.on_move,
273         allow_metadata_inventory_put = workbench.allow_put,
274         allow_metadata_inventory_take = workbench.allow_take,
275         allow_metadata_inventory_move = workbench.allow_move
276 })
277
278 for _, d in ipairs(workbench.defs) do
279 for i = 1, #nodes do
280         local node = nodes[i]
281         local mod_name, item_name = node:match("^(.-):(.*)")
282         local def = registered_nodes[node]
283
284         if item_name and d[3] then
285                 local groups = {}
286                 local tiles
287                 groups.not_in_creative_inventory = 1
288
289                 for k, v in pairs(def.groups) do
290                         if k ~= "wood" and k ~= "stone" and k ~= "level" then
291                                 groups[k] = v
292                         end
293                 end
294
295                 if def.tiles then
296                         if #def.tiles > 1 and (def.drawtype:sub(1,5) ~= "glass") then
297                                 tiles = def.tiles
298                         else
299                                 tiles = {def.tiles[1]}
300                         end
301                 else
302                         tiles = {def.tile_images[1]}
303                 end
304
305                 if not registered_nodes["stairs:slab_" .. item_name] then
306                         stairs.register_stair_and_slab(item_name, node,
307                                 groups, tiles, def.description .. " Stair",
308                                 def.description .. " Slab", def.sounds)
309                 end
310
311                 minetest.register_node(":" .. node .. "_" .. d[1], {
312                         description = def.description .. " " .. d[1]:gsub("^%l", string.upper),
313                         paramtype = "light",
314                         paramtype2 = "facedir",
315                         drawtype = "nodebox",
316                         sounds = def.sounds,
317                         tiles = tiles,
318                         groups = groups,
319                         -- `unpack` has been changed to `table.unpack` in newest Lua versions
320                         node_box = xdecor.pixelbox(16, {unpack(d, 3)}),
321                         sunlight_propagates = true,
322                         on_place = minetest.rotate_node
323                 })
324
325         elseif item_name and mod_name then
326                 minetest.register_alias_force(
327                         ("%s:%s_innerstair"):format(mod_name, item_name),
328                         ("stairs:stair_inner_%s"):format(item_name)
329                 )
330                 minetest.register_alias_force(
331                         ("%s:%s_outerstair"):format(mod_name, item_name),
332                         ("stairs:stair_outer_%s"):format(item_name)
333                 )
334         end
335 end
336 end
337
338 -- Craft items
339
340 minetest.register_tool("xdecor:hammer", {
341         description = "Hammer",
342         inventory_image = "xdecor_hammer.png",
343         wield_image = "xdecor_hammer.png",
344         on_use = function() do
345                 return end
346         end
347 })
348
349 -- Recipes
350
351 minetest.register_craft({
352         output = "xdecor:hammer",
353         recipe = {
354                 {"default:steel_ingot", "group:stick", "default:steel_ingot"},
355                 {"", "group:stick", ""}
356         }
357 })
358
359 minetest.register_craft({
360         output = "xdecor:workbench",
361         recipe = {
362                 {"group:wood", "group:wood"},
363                 {"group:wood", "group:wood"}
364         }
365 })