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 }