SHOW:
|
|
- or go back to the newest paste.
1 | local args ={...} | |
2 | local component = require("component") | |
3 | local robot = require("robot") | |
4 | local sides = require("sides") | |
5 | -- Main output side | |
6 | local f = sides.forward | |
7 | -- Main input sides | |
8 | local u = sides.up | |
9 | local d = sides.down | |
10 | -- Controllers named | |
11 | inv_controller = component.inventory_controller | |
12 | -- What to craft | |
13 | toCraft = args[1] | |
14 | --Amount to craft | |
15 | amount = tonumber(args[2]) | |
16 | if amount==nil then | |
17 | amount=1 | |
18 | end | |
19 | -- Craftin formulas | |
20 | formulas = {} | |
21 | - | formulas["Item Barrel"] = { |
21 | + | |
22 | - | [1] = 'Oak Wood', |
22 | + | |
23 | - | [2] = 'Oak Wood Slab', |
23 | + | |
24 | - | [3] = 'Oak Wood', |
24 | + | |
25 | - | [5] = 'Oak Wood', |
25 | + | |
26 | - | [6] = 'Emptyness is hard', |
26 | + | |
27 | - | [7] = 'Oak Wood', |
27 | + | |
28 | - | [9] = 'Oak Wood', |
28 | + | |
29 | - | [10] = 'Oak Wood', |
29 | + | |
30 | - | [11] = 'Oak Wood', |
30 | + | |
31 | } | |
32 | formulas["Triple Compressed Cobblestone"]={ | |
33 | [1] = 'Double Compressed Cobblestone', | |
34 | [2] = 'Double Compressed Cobblestone', | |
35 | [3] = 'Double Compressed Cobblestone', | |
36 | [5] = 'Double Compressed Cobblestone', | |
37 | [6] = 'Double Compressed Cobblestone', | |
38 | [7] = 'Double Compressed Cobblestone', | |
39 | [9] = 'Double Compressed Cobblestone', | |
40 | [10] = 'Double Compressed Cobblestone', | |
41 | [11] = 'Double Compressed Cobblestone', | |
42 | } | |
43 | formulas["Quadruple Compressed Cobblestone"]={ | |
44 | [1] = 'Triple Compressed Cobblestone', | |
45 | [2] = 'Triple Compressed Cobblestone', | |
46 | [3] = 'Triple Compressed Cobblestone', | |
47 | [5] = 'Triple Compressed Cobblestone', | |
48 | [6] = 'Triple Compressed Cobblestone', | |
49 | [7] = 'Triple Compressed Cobblestone', | |
50 | [9] = 'Triple Compressed Cobblestone', | |
51 | [10] = 'Triple Compressed Cobblestone', | |
52 | [11] = 'Triple Compressed Cobblestone', | |
53 | } | |
54 | ||
55 | formulas["Compressed Cobblestone"]={ | |
56 | [1] = 'Cobblestone', | |
57 | [2] = 'Cobblestone', | |
58 | [3] = 'Cobblestone', | |
59 | [5] = 'Cobblestone', | |
60 | [6] = 'Cobblestone', | |
61 | [7] = 'Cobblestone', | |
62 | [9] = 'Cobblestone', | |
63 | [10] = 'Cobblestone', | |
64 | [11] = 'Cobblestone', | |
65 | } | |
66 | craftingSlots = { | |
67 | [0] = 1, | |
68 | [1] = 2, | |
69 | [2] = 3, | |
70 | [4] = 5, | |
71 | [5] = 6, | |
72 | [6] = 7, | |
73 | [7] = 9, | |
74 | [8] = 10, | |
75 | [9] = 11 | |
76 | } | |
77 | materials_list = {} | |
78 | function moveStuff(material,to_slot,amount) | |
79 | local input_chest = inv_controller.getAllStacks(d).getAll() | |
80 | i = 1 | |
81 | for slot_key, item in pairs(input_chest) do | |
82 | if item then | |
83 | itemName = item.label | |
84 | if itemName == material then | |
85 | robot.select(to_slot) | |
86 | inv_controller.suckFromSlot(d,i,amount) | |
87 | return | |
88 | end | |
89 | - | materials_formula = {} |
89 | + | |
90 | end | |
91 | i = i+1 | |
92 | end | |
93 | end | |
94 | function clearTable() | |
95 | for key, slot in pairs(craftingSlots) do | |
96 | robot.select(slot) | |
97 | robot.drop() | |
98 | end | |
99 | robot.select(1) | |
100 | end | |
101 | function dump(o) | |
102 | if type(o) == 'table' then | |
103 | local s = '{ ' | |
104 | for k,v in pairs(o) do | |
105 | if type(k) ~= 'number' then k = '"'..k..'"' end | |
106 | s = s .. '['..k..'] = ' .. dump(v) .. ',' | |
107 | end | |
108 | return s .. '} ' | |
109 | else | |
110 | return tostring(o) | |
111 | end | |
112 | end | |
113 | function innventoryList(side) | |
114 | inv_list = inv_controller.getAllStacks(side).getAll() | |
115 | i = 1 | |
116 | inv_list_array = {} | |
117 | for key, inv_slot in pairs(inv_list) do | |
118 | if type(inv_slot) == 'table' then | |
119 | if inv_slot.label then | |
120 | if inv_slot.label == 'Air' then | |
121 | ||
122 | else | |
123 | if type(inv_list_array[inv_slot.label])=='table' then | |
124 | inv_list_array[inv_slot.label]['totalcount'] = inv_list_array[inv_slot.label]['totalcount'] + inv_slot.size | |
125 | inv_list_array[inv_slot.label]['slots'][i] = {} | |
126 | inv_list_array[inv_slot.label]['slots'][i] = inv_slot.size | |
127 | else | |
128 | inv_list_array[inv_slot.label] = {} | |
129 | inv_list_array[inv_slot.label]['totalcount'] = inv_slot.size | |
130 | inv_list_array[inv_slot.label]['slots'] = {} | |
131 | inv_list_array[inv_slot.label]['slots'][i] = {} | |
132 | inv_list_array[inv_slot.label]['slots'][i] = inv_slot.size | |
133 | end | |
134 | end | |
135 | ||
136 | end | |
137 | end | |
138 | i = i + 1 | |
139 | end | |
140 | return inv_list_array | |
141 | end | |
142 | function countMaterialsInInventory(list,inv_list_array,direction) | |
143 | ||
144 | materials_status = {} | |
145 | for material, amount_needed in pairs(list) do | |
146 | materials_status[material] = {} | |
147 | if inv_list_array[material] then | |
148 | inv_count = inv_list_array[material]['totalcount'] | |
149 | if inv_count >= amount_needed then | |
150 | materials_status[material]['have'] = inv_count | |
151 | materials_status[material]['needed'] = 0 | |
152 | else | |
153 | materials_status[material]['have'] = inv_count | |
154 | materials_status[material]['needed'] = amount_needed - inv_count | |
155 | end | |
156 | else | |
157 | materials_status[material]['have'] = 0 | |
158 | materials_status[material]['needed'] = amount_needed | |
159 | end | |
160 | end | |
161 | return materials_status | |
162 | end | |
163 | function countMaterials(mainMaterial) | |
164 | if formulas[mainMaterial] then | |
165 | materials_list = countToList(mainMaterial) | |
166 | end | |
167 | end | |
168 | function countToList(materialName,material_list) | |
169 | material_list = {} | |
170 | if(formulas[materialName]) then | |
171 | for slot, material in pairs(formulas[materialName]) do | |
172 | if type(material_list[material])=='table' then | |
173 | if material_list[material]['count'] then | |
174 | material_list[material]['count'] = material_list[material]['count'] + 1 | |
175 | else | |
176 | material_list[material]['count'] = 1 | |
177 | end | |
178 | else | |
179 | material_list[material] = {} | |
180 | material_list[material]['count'] = 1 | |
181 | end | |
182 | if formulas[material] then | |
183 | -- materials_list[material]['submaterials'] = {} | |
184 | materials_list = material_list | |
185 | material_list[material]['submaterials'] = countToList(material,materials_list) | |
186 | end | |
187 | end | |
188 | end | |
189 | return material_list | |
190 | end | |
191 | function findMissing(materials_list,inv_list,missing_materials,multiplier) | |
192 | missing_materials = {} | |
193 | for material,materialInfo in pairs(materials_list) do | |
194 | if multiplier then | |
195 | multiplier = multiplier * materialInfo['count'] | |
196 | else | |
197 | multiplier = materialInfo['count'] | |
198 | end | |
199 | if inv_list[material] then | |
200 | ||
201 | print(dump(inv_list[material])) | |
202 | if inv_list[material]['totalcount'] then | |
203 | total = inv_list[material]['totalcount'] | |
204 | if total < materialInfo['count'] then | |
205 | if materialInfo['submaterials'] then | |
206 | missing_materials[material]['count'] = findMissing(materialInfo['submaterials'],inv_list,missing_materials,multiplier) | |
207 | else | |
208 | if missing_materials[material] then | |
209 | missing_materials[material]['count'] = materials_missing[material]['count'] + 1 | |
210 | else | |
211 | missing_materials[material] = {} | |
212 | fullamount = multiplier | |
213 | fullamount = fullamount - total | |
214 | missing_materials[material]['count'] = fullamount | |
215 | end | |
216 | end | |
217 | end | |
218 | end | |
219 | else | |
220 | if materialInfo['submaterials'] then | |
221 | missing_materials[material] = {} | |
222 | ||
223 | missing_materials[material] = findMissing(materialInfo['submaterials'],inv_list,missing_materials,multiplier) | |
224 | else | |
225 | if missing_materials[material] then | |
226 | missing_materials[material['count']] = multiplier | |
227 | else | |
228 | missing_materials[material] = {} | |
229 | missing_materials[material]['count'] = multiplier | |
230 | end | |
231 | end | |
232 | end | |
233 | end | |
234 | return missing_materials | |
235 | end | |
236 | function craftStuff(thingName,amount) | |
237 | countMaterials(thingName) | |
238 | inv_list = innventoryList(d) | |
239 | materials_missing = findMissing(materials_list,inv_list) | |
240 | print(dump(materials_missing)) | |
241 | end | |
242 | while true do | |
243 | craftStuff(toCraft,amount) | |
244 | return | |
245 | end |