summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--inv_utils.lua12
-rw-r--r--sorter.lua81
-rw-r--r--str_utils.lua12
3 files changed, 102 insertions, 3 deletions
diff --git a/inv_utils.lua b/inv_utils.lua
index 872647d..9a6e280 100644
--- a/inv_utils.lua
+++ b/inv_utils.lua
@@ -6,6 +6,16 @@ local function hasItemsInStorage(storage)
return false
end
+local function hasItemsInTurtle()
+ for ii = 1, 16 do
+ if turtle.getItemCount(ii) > 0 then
+ return true
+ end
+ end
+
+ return false
+end
+
local function getEmptySlotInStorage(storage)
return 5 -- TODO
end
@@ -18,4 +28,4 @@ local function swapSlotsInStorage(storage, slotA, slotB)
end
-return { hasItemsInStorage = hasItemsInStorage, swapSlotsInStorage = swapSlotsInStorage }
+return { hasItemsInStorage = hasItemsInStorage, hasItemsInTurtle = hasItemsInTurtle, swapSlotsInStorage = swapSlotsInStorage }
diff --git a/sorter.lua b/sorter.lua
index 6c50de5..ca4fe2b 100644
--- a/sorter.lua
+++ b/sorter.lua
@@ -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
diff --git a/str_utils.lua b/str_utils.lua
new file mode 100644
index 0000000..c45f16a
--- /dev/null
+++ b/str_utils.lua
@@ -0,0 +1,12 @@
+local function split(inputstr, sep)
+ if sep == nil then
+ sep = "%s"
+ end
+ local t = {}
+ for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
+ table.insert(t, str)
+ end
+ return t
+end
+
+return { split = split }