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