From 05bde0603cda5d91712615d0e2706b0e2347aa40 Mon Sep 17 00:00:00 2001
From: Botond Hende <nettingman@gmail.com>
Date: Fri, 2 May 2025 22:50:53 +0200
Subject: sorter working

---
 sorter.lua | 43 ++++++++++++++++++++++++++++++-------------
 1 file changed, 30 insertions(+), 13 deletions(-)

(limited to 'sorter.lua')

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