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