-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpadestalManagement
executable file
·213 lines (190 loc) · 5.32 KB
/
padestalManagement
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
local padestals
local PADESTALS_ORDER_FILE = "padestals.dat"
local m = peripheral.wrap("right")
function table.getSingleKey(tab)
if table.size(tab) ~= 1 then error("This method expects exactly 1 key in the table, was "..#tab) end
for key in pairs(tab) do
return key
end
end
function removeFromArray(array, object)
if array == nil then error("Array is nil") end
local keyToRemove
repeat
keyToRemove = nil
for key, value in pairs(array) do
if value == object then
keyToRemove = key
break
end
end
table.remove(array, keyToRemove)
until keyToRemove ~= nil
end
function table.size(t)
local count = 0
for _ in pairs(t) do
count = count + 1
end
return count
end
function shallowCopy(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in pairs(orig) do
copy[orig_key] = orig_value
end
else -- number, string, boolean, etc
copy = orig
end
return copy
end
function table.arrayToSet(array)
local set = {}
for k, v in pairs(array) do
set[v] = 1
end
return set
end
function listMethods(o)
local count = 0
for i,v in pairs(o) do
print(i)
count = count + 1
if count >= 7 then
local res = io.read()
if res == "q" then
return
end
count = 0
end
end
end
function listValues(o)
local count = 0
for i,v in pairs(o) do
print(v)
count = count + 1
if count >= 7 then
local res = io.read()
if res == "q" then
return
end
count = 0
end
end
end
-- nacte seznam ender chest pod padestaly
function listPadestals()
local padestals = {}
local peripheralSides = peripheral.getNames()
for _, side in pairs(peripheralSides) do
if peripheral.getType(side) == 'ender_chest' then
table.insert(padestals, side)
end
end
return padestals
end
function withPeripheralsDo(peripheralSides,fun)
local collect = {}
for _, side in pairs(peripheralSides) do
local result = fun(side)
if result ~= nil then
collect[side] = result
end
end
return collect
end
function opStack_getSize(stack)
if stack == nil then
return 0
end
return stack.qty
end
function chestOperation_detectItemInPadestal(side)
--print('Processing side'..side)
local chest = peripheral.wrap(side)
chest.condenseItems()
local countSlot27 = opStack_getSize(chest.getStackInSlot(27))
if countSlot27 > 0 then
error("Slot 27 in ender chest is not empty...")
end
chest.pullItem('up',1,1,27)
countSlot27 = opStack_getSize(chest.getStackInSlot(27))
if countSlot27 > 0 then
return 1
else
return nil
end
end
function saveTable(t,name)
local file = fs.open(name,"w")
file.write(textutils.serialize(t))
file.close()
end
function loadTable(name)
local file = fs.open(name,"r")
local data = file.readAll()
file.close()
return textutils.unserialize(data)
end
function _orderPadestals()
local notOrderedPadestals = shallowCopy(padestals)
local limit = 5000
local padestalOrder = {}
while #padestalOrder < #padestals and limit > 0 do
local padestalsWithItems = withPeripheralsDo(notOrderedPadestals,chestOperation_detectItemInPadestal)
print("Padestals found:")
listMethods(padestalsWithItems)
if table.size(padestalsWithItems) > 1 then
print("Error: Item was detected in 2 padestals at the same time. Ignoring this attempt!")
elseif table.size(padestalsWithItems) == 1 then
local identifiedPadestal = table.getSingleKey(padestalsWithItems)
table.insert(padestalOrder, identifiedPadestal)
removeFromArray(notOrderedPadestals, identifiedPadestal)
print("Identified "..identifiedPadestal)
end
limit = limit - 1
end
return padestalOrder
end
function initializePadestalOrder(padestals)
local padestalOrder
local savedPadestalsInvalid = false
if fs.exists(PADESTALS_ORDER_FILE) then
local loadedOrder = loadTable(PADESTALS_ORDER_FILE)
--verify all padestals computer has are in order file and vise versa
local computerPadestalsSet = table.arrayToSet(padestals)
local loadedPadestalsSet = table.arrayToSet(loadedOrder)
local computerPadestalsNotInLoaded = false
local loadedPadestalsNotInComputer = false
for key in pairs(computerPadestalsSet) do
if loadedPadestalsSet[key] == nil then
computerPadestalsNotInLoaded = true
end
end
for key in pairs(loadedPadestalsSet) do
if computerPadestalsSet[key] == nil then
loadedPadestalsNotInComputer = true
end
end
if computerPadestalsNotInLoaded or loadedPadestalsNotInComputer then
padestalOrder = nil
savedPadestalsInvalid = true
else
padestalOrder = loadedOrder
end
end
if padestalOrder == nil then
if savedPadestalsInvalid then
print("Warning: padestals saved in the file are no longer matching padestals of the computer. Reinitialization needed.")
end
print("Padestal order initialization - put dirt onto padestals in order, which will imply symetry of padestals. This order will be stored in computer and will be loaded next time. Before putting next item onto padestal wait for previous item to disappear")
padestalOrder = _orderPadestals()
saveTable(padestalOrder, PADESTALS_ORDER_FILE)
end
end
padestals = listPadestals()
padestalOrder = initializePadestalOrder(padestals)