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