From 2f5e88fd789db0dc5c6f0db0c173ce818bd6ab37 Mon Sep 17 00:00:00 2001 From: Botond Hende Date: Fri, 2 May 2025 14:31:17 +0200 Subject: sorter wip --- inv_utils.lua | 10 ++++++++- refuel.lua | 2 +- sorter.lua | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 sorter.lua diff --git a/inv_utils.lua b/inv_utils.lua index 830e39e..872647d 100644 --- a/inv_utils.lua +++ b/inv_utils.lua @@ -1,3 +1,11 @@ +local function hasItemsInStorage(storage) + for _, _ in pairs(storage.list()) do + return true + end + + return false +end + local function getEmptySlotInStorage(storage) return 5 -- TODO end @@ -10,4 +18,4 @@ local function swapSlotsInStorage(storage, slotA, slotB) end -return { swapSlotsInStorage = swapSlotsInStorage } +return { hasItemsInStorage = hasItemsInStorage, swapSlotsInStorage = swapSlotsInStorage } diff --git a/refuel.lua b/refuel.lua index 6d873fd..fdac6b6 100644 --- a/refuel.lua +++ b/refuel.lua @@ -17,4 +17,4 @@ local function refuel() turtle.drop() end -return { refuel = refuel } +return { refuel = refuel, hasFuelInStorage = hasFuelInStorage } diff --git a/sorter.lua b/sorter.lua new file mode 100644 index 0000000..6c50de5 --- /dev/null +++ b/sorter.lua @@ -0,0 +1,66 @@ +local refuel = require("modules.refuel") +local inv_utils = require("modules.inv_utils") + +local DATA_PATH = "/data/sorter_data.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")) + + 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")) + + if canStart then + print("Ready to start sorting.") + else + print("Can not start sorting.") + end + + return canStart +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.") + else + print("Aborted.") + return nil + end +end + +return { run = run } -- cgit v1.2.3-70-g09d2