diff options
Diffstat (limited to 'sorter.lua')
-rw-r--r-- | sorter.lua | 81 |
1 files changed, 79 insertions, 2 deletions
@@ -1,5 +1,7 @@ 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" @@ -37,6 +39,14 @@ local function runDiagnostics() 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 @@ -46,17 +56,84 @@ local function runDiagnostics() 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 + + 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) + + 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 + end + + if inv_utils.hasItemsInTurtle() then + move(current_pos, state.leftover_pos, state.safe_y) + move(state.leftover_pos, state.start_pos, state.safe_y) + else + move(current_pos, state.start_pos, state.safe_y) + end + + move(state.start_pos, state.park_pos, state.safe_y, true) +end + local function run() - local result = runDiagnostics() + local result = true --runDiagnostics() if not result then return nil end write("Start? (y/N) ") - local response = read() + local response = "y"--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 |