]> git.lizzy.rs Git - xdecor.git/blob - workbench.lua
Crafting Guide : don't create items list whenever a new formspec is shown
[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)
118         elseif fields.backcraft then workbench:formspecs(meta, 1) end -- Legacy code for older formspecs.
119 end
120
121 function workbench.dig(pos)
122         local inv = minetest.get_meta(pos):get_inventory()
123         return inv:is_empty("input") and inv:is_empty("hammer") and
124                 inv:is_empty("tool") and inv:is_empty("storage")
125 end
126
127 function workbench.timer(pos)
128         local timer = minetest.get_node_timer(pos)
129         local inv = minetest.get_meta(pos):get_inventory()
130         local tool = inv:get_stack("tool", 1)
131         local hammer = inv:get_stack("hammer", 1)
132
133         if tool:is_empty() or hammer:is_empty() or tool:get_wear() == 0 then
134                 timer:stop() return
135         end
136
137         -- Tool's wearing range: 0-65535 | 0 = new condition.
138         tool:add_wear(-500)
139         hammer:add_wear(700)
140
141         inv:set_stack("tool", 1, tool)
142         inv:set_stack("hammer", 1, hammer)
143         return true
144 end
145
146 function workbench.put(_, listname, _, stack)
147         local stackname = stack:get_name()
148         if (listname == "tool" and stack:get_wear() > 0 and workbench:repairable(stackname)) or
149            (listname == "input" and minetest.registered_nodes[stackname.."_cube"]) or
150            (listname == "hammer" and stackname == "xdecor:hammer") or
151             listname == "storage" then
152                 return stack:get_count()
153         end
154         return 0
155 end
156
157 function workbench.take(_, listname, _, stack, player)
158         if listname == "forms" then
159                 local inv = player:get_inventory()
160                 if inv:room_for_item("main", stack:get_name()) then return -1 end
161                 return 0
162         end
163         return stack:get_count()
164 end
165
166 function workbench.move(_, _, _, to_list, _, count)
167         if to_list == "storage" then return count end
168         return 0
169 end
170
171 function workbench.on_put(pos, listname, _, stack)
172         local inv = minetest.get_meta(pos):get_inventory()
173         if listname == "input" then
174                 local input = inv:get_stack("input", 1)
175                 workbench:get_output(inv, input, stack:get_name())
176         elseif listname == "tool" or listname == "hammer" then
177                 local timer = minetest.get_node_timer(pos)
178                 timer:start(3.0)
179         end
180 end
181
182 function workbench.on_take(pos, listname, index, stack)
183         local inv = minetest.get_meta(pos):get_inventory()
184         local input = inv:get_stack("input", 1)
185
186         if listname == "input" then
187                 if stack:get_name() == input:get_name() then
188                         workbench:get_output(inv, input, stack:get_name())
189                 else
190                         inv:set_list("forms", {})
191                 end
192         elseif listname == "forms" then
193                 input:take_item(math.ceil(stack:get_count() / workbench.defs[index][2]))
194                 inv:set_stack("input", 1, input)
195                 workbench:get_output(inv, input, input:get_name())
196         end
197 end
198
199 xdecor.register("workbench", {
200         description = "Work Bench",
201         groups = {cracky=2, choppy=2, oddly_breakable_by_hand=1},
202         sounds = default.node_sound_wood_defaults(),
203         tiles = {"xdecor_workbench_top.png",   "xdecor_workbench_top.png",
204                  "xdecor_workbench_sides.png", "xdecor_workbench_sides.png",
205                  "xdecor_workbench_front.png", "xdecor_workbench_front.png"},
206         on_rotate = screwdriver.rotate_simple,
207         can_dig = workbench.dig,
208         on_timer = workbench.timer,
209         on_construct = workbench.construct,
210         on_receive_fields = workbench.fields,
211         on_metadata_inventory_put = workbench.on_put,
212         on_metadata_inventory_take = workbench.on_take,
213         allow_metadata_inventory_put = workbench.put,
214         allow_metadata_inventory_take = workbench.take,
215         allow_metadata_inventory_move = workbench.move
216 })
217
218 for _, d in pairs(workbench.defs) do
219 for node in pairs(minetest.registered_nodes) do
220         local def = minetest.registered_nodes[node]
221         if workbench:nodes(def) and d[3] then
222                 local groups, tiles = {}, {}
223                 groups.not_in_creative_inventory = 1
224
225                 for k, v in pairs(def.groups) do
226                         if k ~= "wood" and k ~= "stone" and k ~= "level" then
227                                 groups[k] = v
228                         end
229                 end
230
231                 if def.tiles then
232                         if #def.tiles > 1 and not (def.drawtype:sub(1,5) == "glass") then
233                                 tiles = def.tiles
234                         else
235                                 tiles = {def.tiles[1]}
236                         end
237                 else
238                         tiles = {def.tile_images[1]}
239                 end
240
241                 if not minetest.registered_nodes["stairs:slab_"..node:match(":(.*)")] then
242                         stairs.register_stair_and_slab(node:match(":(.*)"), node, groups, tiles,
243                                 def.description.." Stair", def.description.." Slab", def.sounds)
244                 end
245
246                 minetest.register_node(":"..node.."_"..d[1], {
247                         description = def.description.." "..d[1]:gsub("^%l", string.upper),
248                         paramtype = "light",
249                         paramtype2 = "facedir",
250                         drawtype = "nodebox",
251                         sounds = def.sounds,
252                         tiles = tiles,
253                         groups = groups,
254                         -- `unpack` has been changed to `table.unpack` in newest Lua versions.
255                         node_box = xdecor.pixelbox(16, {unpack(d, 3)}),
256                         sunlight_propagates = true,
257                         on_place = minetest.rotate_node
258                 })
259         end
260         if node:match(":mese") then
261                 if d[3] then minetest.register_alias(node.."_"..d[1], "default:glass_"..d[1])
262                 else minetest.register_alias("stairs:"..d[1].."_"..node:match(":(.*)"), "stairs:"..d[1].."_glass") end
263         elseif workbench:nodes(def) and not d[3] then
264                 minetest.register_alias(node.."_"..d[1], "stairs:"..d[1].."_"..node:match(":(.*)"))
265         end
266 end
267 end
268
269 minetest.register_alias("xdecor:worktable", "xdecor:workbench")
270