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