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