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