From 05bde0603cda5d91712615d0e2706b0e2347aa40 Mon Sep 17 00:00:00 2001 From: Botond Hende Date: Fri, 2 May 2025 22:50:53 +0200 Subject: sorter working --- inv_utils.lua | 42 +++++++++++++++++++++++++++++++++++++++++- sorter.lua | 43 ++++++++++++++++++++++++++++++------------- 2 files changed, 71 insertions(+), 14 deletions(-) diff --git a/inv_utils.lua b/inv_utils.lua index 9a6e280..3a3feed 100644 --- a/inv_utils.lua +++ b/inv_utils.lua @@ -16,6 +16,46 @@ local function hasItemsInTurtle() return false end +local function fillTurtle() + local success = true + turtle.select(1) + while success do + success = turtle.suck() + end +end + +local function emptyTurtle() + for ii = 1, 16 do + turtle.select(ii) + turtle.drop() + end +end + +local function hasIdenticalItemsInStorage(storage, slot) + local detail = turtle.getItemDetail(slot) + if detail == nil then + return false + end + + for _, item in pairs(storage.list()) do + if item.name == detail.name then + return true + end + end + + return false +end + +local function storeIdenticalItemsInStorage() + local storage = peripheral.wrap("front") + for ii = 1, 16 do + if hasIdenticalItemsInStorage(storage, ii) then + turtle.select(ii) + turtle.drop() + end + end +end + local function getEmptySlotInStorage(storage) return 5 -- TODO end @@ -28,4 +68,4 @@ local function swapSlotsInStorage(storage, slotA, slotB) end -return { hasItemsInStorage = hasItemsInStorage, hasItemsInTurtle = hasItemsInTurtle, swapSlotsInStorage = swapSlotsInStorage } +return { hasItemsInStorage = hasItemsInStorage, hasItemsInTurtle = hasItemsInTurtle, fillTurtle = fillTurtle, emptyTurtle = emptyTurtle, hadIdenticalItemsInStorage = hasIdenticalItemsInStorage, storeIdenticalItemsInStorage = storeIdenticalItemsInStorage, swapSlotsInStorage = swapSlotsInStorage } diff --git a/sorter.lua b/sorter.lua index ca4fe2b..c5ac286 100644 --- a/sorter.lua +++ b/sorter.lua @@ -105,39 +105,56 @@ local function runErrands() 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 + 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() + + if not inv_utils.hasItemsInTurtle() then + break + end + 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) + 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 = true --runDiagnostics() + local result = runDiagnostics() if not result then return nil end write("Start? (y/N) ") - local response = "y"--read() + local response = read() local firstChar = string.sub(response, 1, 1) if firstChar == "y" or firstChar == "Y" then print("Started sorting.") - --refuel.refuel() + refuel.refuel() runErrands() else print("Aborted.") return nil end + + os.shutdown() end return { run = run } -- cgit v1.2.3-70-g09d2