-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.lua
428 lines (383 loc) · 12.4 KB
/
utils.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
local M = {}
-- An empty table used in super() for example
M.NOT_PROVIDED = {}
function M.sizeOfTable(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
function M.tableToString(obj)
local function toString(o)
if type(o) == 'table' then
local s = '{ '
for k, v in pairs(o) do
if type(k) ~= 'number' then
k = '"' .. k .. '"'
end
s = s .. '[' .. k .. '] = ' .. toString(v) .. ','
end
return s .. '} '
else
return tostring(o)
end
end
return toString(obj)
end
-- http://lua-users.org/wiki/CopyTable
function M.shallowCopy(orig)
local origType = type(orig)
local copy
if origType == 'table' then
copy = {}
for origKey, origValue in pairs(orig) do
copy[origKey] = origValue
end
else -- number, string, boolean, etc
copy = orig
end
return copy
end
--[[
Creates a child class table inherited from baseClass.
]]
function M.inheritsFrom(baseClass)
if not baseClass then
error("baseClass should not be nil", 2)
end
local childClass = {}
baseClass.__index = baseClass
childClass = setmetatable(childClass, baseClass)
return childClass
end
--[[
Tests if a value is an instance of a class.
Checks inheritance. Inheritance should be made by the inheritsFrom
method above.
]]
---@param value any
---@param klass any
---@return boolean
function M.isInstance(value, klass)
if type(value) == "table" then
local curClass = getmetatable(value)
while curClass do
if curClass == klass then
return true
else
curClass = getmetatable(curClass)
end
end
end
return false
end
function M.printErr(message)
io.stderr:write(message)
io.stderr:write("\n")
end
--[[
Returns the first available slot in the inventory
Returns 0 if it is full
]]
function M.firstAvailableSlot(transposer, sideInventory)
local stackIterator = transposer.getAllStacks(sideInventory)
local curStack = stackIterator()
local curSlot = 1
while curStack ~= nil do
if next(curStack) == nil then
return curSlot
end
curStack = stackIterator()
curSlot = curSlot + 1
end
return 0
end
---@alias FluidInfo { label: string, amount: integer, slot: integer }
---Returns the first fluid in the tank
---@param transposer any
---@param sideTank integer
---@return FluidInfo?
function M.firstFluid(transposer, sideTank)
local res = transposer.getFluidInTank(sideTank)
for _, slotInfo in ipairs(res) do
if slotInfo.amount > 0 then
return {
label = slotInfo.label,
amount = slotInfo.amount,
slot = 1
}
end
end
end
---@alias StackInfo { label: string, name: string, slot: integer, size: integer, maxSize: integer }
--[[
Returns the first stack in the inventory
e.g. It returns {
name = "minecraft:cobblestone",
label = "Cobblestone",
slot = 5,
size = 24,
maxSize = 64
}
Returns nil if the inventory is empty
]]
---@param transposer any
---@param sideInventory integer
---@return StackInfo?
function M.firstStack(transposer, sideInventory)
local stackIterator = transposer.getAllStacks(sideInventory)
local curStack = stackIterator()
local curSlot = 1
while curStack ~= nil do
if next(curStack) ~= nil then
return {
name = curStack.name,
label = curStack.label,
slot = curSlot,
size = curStack.size,
maxSize = curStack.maxSize,
}
end
curStack = stackIterator()
curSlot = curSlot + 1
end
end
---Gets raw data of the first stack returned by getAllStacks() API
---@param transposer any
---@param sideInventory integer
---@return table?
function M.firstStackRaw(transposer, sideInventory)
local stackIterator = transposer.getAllStacks(sideInventory)
local curStack = stackIterator()
local curSlot = 1
while curStack ~= nil do
if next(curStack) ~= nil then
return curStack
end
curStack = stackIterator()
curSlot = curSlot + 1
end
end
---@param transposer any
---@param side integer
function M.isChestEmpty(transposer, side)
return M.firstStack(transposer, side) == nil
end
---@param transposer any
---@param side integer
function M.isTankEmpty(transposer, side)
return M.firstFluid(transposer, side) == nil
end
---@alias ItemStacksInfo { name: string, label: string, slots: integer[], sizes: integer[], maxSize: integer}
--[[
Collects info about stacks in an inventory
e.g. It returns {
Cobblestone = {
name = "minecraft:cobblestone"
label = "Cobblestone",
slots = { 1, 5 },
sizes = { 28, 64 },
maxSize = 64,
}
}
Returns an empty table if the inventory is empty
]]
---@param transposer any
---@param side integer
---@param filter (fun(name: string, label: string, size: integer, maxSize: integer): boolean)?
---@return table<string, ItemStacksInfo>
function M.collectStacksInfo(transposer, side, filter)
if not filter then
filter = function() return true end
end
local stackIterator = transposer.getAllStacks(side)
local stacksInfo = {}
local slot = 1
while true do
local curStack = stackIterator()
if curStack == nil then
break
end
if next(curStack) ~= nil then
---@type string
local itemLabel = curStack.label
---@type string
local itemName = curStack.name
---@type integer
local stackSize = curStack.size
---@type integer
local maxStackSize = curStack.maxSize
if filter(itemName, itemLabel, stackSize, maxStackSize) then
local entry = stacksInfo[itemLabel]
if entry == nil then
entry = { name = itemName, label = itemLabel, slots = {}, sizes = {}, maxSize = maxStackSize }
stacksInfo[itemLabel] = entry
end
table.insert(entry.slots, slot)
table.insert(entry.sizes, stackSize)
end
end
slot = slot + 1
end
return stacksInfo
end
--[[
Combines incomplete stacks but does not sort them
e.g. 32,28,4,64 -> 64,64
]]
---@param transposer any
---@param side integer
---@param verbose boolean?
---@return table<string, ItemStacksInfo> # stacks info after combination
function M.combineStacks(transposer, side, verbose)
local stacksInfo = M.collectStacksInfo(transposer, side)
for itemName, entry in pairs(stacksInfo) do
local slots = entry.slots
local numStacks = #slots
local sizes = entry.sizes
local maxSize = entry.maxSize
local iIncomplete = 1
while true do
-- Skips full and empty stacks
while iIncomplete < numStacks and (sizes[iIncomplete] == maxSize or sizes[iIncomplete] == 0) do
iIncomplete = iIncomplete + 1
end
if iIncomplete >= numStacks then
break
end
-- Fills up one stack at a time
-- Finds a stack after the incomplete stack to combine with
local iSrc = iIncomplete + 1
while iSrc <= numStacks and sizes[iIncomplete] < maxSize do
-- Does not break full stacks
if sizes[iSrc] ~= maxSize then
local incompleteAmt = sizes[iIncomplete]
local moveSize = math.min(sizes[iSrc], maxSize - incompleteAmt)
if moveSize > 0 then
transposer.transferItem(side, side, moveSize, slots[iSrc], slots[iIncomplete])
sizes[iIncomplete] = incompleteAmt + moveSize
sizes[iSrc] = sizes[iSrc] - moveSize
if verbose then
print(string.format("Combined %d %s with other %d",
moveSize, itemName, incompleteAmt))
end
end
end
iSrc = iSrc + 1
end
iIncomplete = iSrc - 1 -- i was advanced one more time when the stack is filled up
end
end
-- Rescans to remove stacks with size 0
return M.collectStacksInfo(transposer, side)
end
--[[
Gets patterns info from an ME interface with an extra table for
faster pattern lookup for an output item from an input item
e.g. returns
{
["Reinforced Slate"] = {
inputs = { ["Blank Slate"] = 1 },
outputAmount = 1
}
},
{
["Blank Slate"] = { "Reinforced Slate" }
}
]]
---@alias PatternInfo {inputs: string[], outputAmount: integer}
---@alias PatternsLookup table<string, string[]> reverse lookup table of patterns
---@param meInterface any
---@return table<string, PatternInfo>
---@return PatternsLookup
function M.getPatternsInfo(meInterface)
local patternsInfo = {}
for i = 1, 36 do -- 36 is the max number of patterns
local pattern = meInterface.getInterfacePattern(i)
if pattern ~= nil then
local inputs = pattern.inputs
local outputs = pattern.outputs
local output = outputs[1]
if output ~= nil then
local patternInputs = {}
for _, t in ipairs(inputs) do
local inputName = t.name
if inputName ~= nil then
-- Breaks when an input is nil, so
-- AE patterns should not have empty slots
-- in between
patternInputs[inputName] = t.count
else
break
end
end
patternsInfo[output.name] = {
inputs = patternInputs,
outputAmount = output.count
}
else
print("Invalid pattern: no outputs")
end
end
end
local inputToPatterns = {}
for output, entry in pairs(patternsInfo) do
for input, _ in pairs(entry.inputs) do
if inputToPatterns[input] == nil then
inputToPatterns[input] = {}
end
local numPatterns = #inputToPatterns[input]
inputToPatterns[input][numPatterns + 1] = output
end
end
return patternsInfo, inputToPatterns
end
local function testFirstAvailableSlot()
local component = require "component"
local transposer = component.proxy("3b059f55-3d7d-4c85-ac1d-7a561234d49c")
local slot = M.firstAvailableSlot(transposer, 2)
print("First available slot is " .. slot)
end
local function testIsChestEmpty()
local component = require "component"
local sides = require "sides"
print(M.isChestEmpty(component.composer, sides.top))
end
local function testIsTankEmpty()
local component = require "component"
local sides = require "sides"
print(M.isTankEmpty(component.composer, sides.south))
end
local function testGetPatternsInfo()
local component = require "component"
local patterns, inputToPatterns = M.getPatternsInfo(component.me_interface)
print("Patterns:")
print(M.tableToString(patterns))
print("Input to Patterns:")
print(M.tableToString(inputToPatterns))
end
local function testCollectStacksFilter()
local component = require "component"
local sides = require "sides"
print(M.tableToString(M.collectStacksInfo(
component.transposer, sides.top, function (_, label)
return label == "Cobblestone" or label == "ZPM Machine Casing"
end)
))
end
local function testCombineStacks()
local component = require "component"
local sides = require "sides"
local transposer = component.transposer
local side = sides.top
print(M.tableToString(
M.combineStacks(transposer, side, true)
))
end
local function testFirstFluid()
local component = require "component"
local sides = require "sides"
local transposer = component.transposer
local side = sides.west
print(M.firstFluid(transposer, side))
end
return M