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