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