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
|
local refuel = require("modules.refuel")
local inv_utils = require("modules.inv_utils")
local str_utils = require("modules.str_utils")
local nav = require("modules.nav")
local DATA_PATH = "/data/sorter_data.txt"
local CAT_PATH = "/data/sorter_cat.txt"
local function runDiagnostics()
local canStart = true
print("Running diagnostics...")
print(string.format("Fuel level: %s", turtle.getFuelLevel()))
local fuelStorage = peripheral.wrap("front")
local hasInStorage = refuel.hasFuelInStorage(fuelStorage) and "yes" or "no"
print(string.format("Fuel in storage: %s", hasInStorage))
local dataExists = fs.exists(DATA_PATH)
if not dataExists then
canStart = false
end
print(string.format("Data path exists: %s", dataExists and "yes" or "no"))
print(string.format("Category path exists: %s", fs.exists(CAT_PATH) and "yes" or "no"))
local hasItemsInUnsorted = inv_utils.hasItemsInStorage(peripheral.wrap("top"))
if not hasItemsInUnsorted then
canStart = false
end
print(string.format("Items in unsorted storage: %s", hasItemsInUnsorted and "yes" or "no"))
local hasItemsInLeftover = inv_utils.hasItemsInStorage(peripheral.wrap("bottom"))
if hasItemsInLeftover then
canStart = false
end
print(string.format("Items in leftover storage: %s", hasItemsInLeftover and "yes" or "no"))
local hasItemsInTurtle = inv_utils.hasItemsInTurtle()
if hasItemsInTurtle then
canStart = false
end
print(string.format("Items in turtle: %s", hasItemsInTurtle and "yes" or "no"))
if canStart then
print("Ready to start sorting.")
else
print("Can not start sorting.")
end
return canStart
end
local function getState()
local state = {}
local file = fs.open(DATA_PATH, "r")
local content = file.readAll()
file.close()
state.coords = {}
local coord_count = 0
for idx, elem in pairs(str_utils.split(content)) do
if idx == 1 then
state.safe_y = tonumber(elem)
else
local modulo = (idx - 2) % 4
if modulo == 0 then
table.insert(state.coords, { 0, 0, 0, 0 })
coord_count = coord_count + 1
end
state.coords[coord_count][modulo + 1] = tonumber(elem)
end
end
state.park_pos = state.coords[1]
state.start_pos = state.coords[2]
state.sort_pos = state.coords[3]
state.leftover_pos = state.coords[4]
for ii = 1, 4 do
table.remove(state.coords, 1)
end
state.categories = { }
if fs.exists(CAT_PATH) then
local file2 = fs.open(CAT_PATH, "r")
local cat_content = file2.readAll()
file2.close()
for _, cat in pairs(str_utils.split(cat_content, "---")) do
local categ = {}
for _, entry in pairs(str_utils.split(cat)) do
table.insert(categ, entry)
end
table.insert(state.categories, categ)
end
end
return state
end
local function move(a, b, safe_y, unsafe)
nav.travel(a[1], a[2], a[3], a[4], b[1], b[2], b[3], b[4], safe_y, unsafe)
end
local function debugPrintPos(a)
print(a[1], a[2], a[3], a[4])
end
local function runErrands()
local state = getState()
move(state.park_pos, state.start_pos, state.safe_y, true)
move(state.start_pos, state.sort_pos, state.safe_y)
inv_utils.fillTurtle()
local oneMoreRound = inv_utils.hasItemsInTurtle()
while (oneMoreRound) do
local current_pos = state.sort_pos
for _, next_pos in pairs(state.coords) do
move(current_pos, next_pos, state.safe_y)
current_pos = next_pos
inv_utils.storeIdenticalItemsInStorage(state)
if not inv_utils.hasItemsInTurtle() then
break
end
end
if inv_utils.hasItemsInTurtle() then
move(current_pos, state.leftover_pos, state.safe_y)
inv_utils.emptyTurtle()
move(state.leftover_pos, state.sort_pos, state.safe_y)
else
move(current_pos, state.sort_pos, state.safe_y)
end
inv_utils.fillTurtle()
oneMoreRound = inv_utils.hasItemsInTurtle()
end
move(state.sort_pos, state.start_pos, state.safe_y)
move(state.start_pos, state.park_pos, state.safe_y, true)
end
local function run()
local result = runDiagnostics()
if not result then
return nil
end
write("Start? (y/N) ")
local response = read()
local firstChar = string.sub(response, 1, 1)
if firstChar == "y" or firstChar == "Y" then
print("Started sorting.")
refuel.refuel()
runErrands()
else
print("Aborted.")
return nil
end
os.shutdown()
end
return { run = run }
|