From c66736cf4463ca1a56a2b059706dd43d013013d5 Mon Sep 17 00:00:00 2001
From: Anton Reinhard <rubydragon@noreply.code.woubery.com>
Date: Sat, 30 Sep 2023 13:20:05 +0200
Subject: [PATCH] Fix issue 2 (#4)

See #2

Co-authored-by: Anton Reinhard <s1509337@msx.tu-dresden.de>
Reviewed-on: https://code.woubery.com/Rubydragon/fusion-speedrun/pulls/4
---
 New Attempts/Commands.md |  4 +-
 New Attempts/Mining.lua  | 79 ++++++++++++++++++++++++++++++++++------
 2 files changed, 69 insertions(+), 14 deletions(-)

diff --git a/New Attempts/Commands.md b/New Attempts/Commands.md
index 2b87476..de3dbfb 100644
--- a/New Attempts/Commands.md	
+++ b/New Attempts/Commands.md	
@@ -1,6 +1,6 @@
-pastebin get XdpnRKDf mine
+pastebin get gGy2HWcF mine
 refuel all
-mining
+mine
 
 # quarry program
 quarry -oreQuarry true -enderChest 16 -doRefuel false -dumpCompareItems false -invert false -rednet false -dim 16 16 70
diff --git a/New Attempts/Mining.lua b/New Attempts/Mining.lua
index 5a3f81e..8b320bc 100644
--- a/New Attempts/Mining.lua	
+++ b/New Attempts/Mining.lua	
@@ -1,4 +1,7 @@
-compareSlotMax = 1   -- all slots up to this one are used for comparing, order the blocks in decreasing probability
+maxCompareSlot = 0   -- all slots up to this one are used for comparing, initialized in initCompareSlots
+currentCompareSlot = 1  -- current compare slot (does not have to be saved)
+
+startupWait = 5 -- time in seconds to wait after reading state before resuming
 
 xRelPos = 0 -- "positive = forward"
 yRelPos = 0 -- "positive = right"
@@ -18,6 +21,12 @@ function printPos()
     print("At (", xRelPos, ", ", yRelPos, ", ", zRelPos, ") facing (", facing.x, ", ", facing.y, ")")
 end
 
+function startupTimer()
+    print("Waiting ", startupWait, "s before starting... ")
+    sleep(startupWait)
+    print("Starting!")
+end
+
 function setupResume()
     if fs.exists("startup") then
         return
@@ -26,14 +35,12 @@ function setupResume()
     file.writeLine( --The below is on the left because spacing
 [[
 print("Resuming Miner")
-function deleteStuff()
-    fs.delete("startup")
-end
 local event
 if fs.exists("state") then
     os.run({},"]]..shell.getRunningProgram()..[[")
 else
     print("Never mind, no save file found")
+    fs.delete("startup")
 end
 ]])
     file.close()
@@ -51,6 +58,7 @@ end
 
 function readState()
     if not fs.exists("state") then
+        initCompareSlots()
         return
     end
     print("Reading state after restart")
@@ -63,10 +71,13 @@ function readState()
     facing.y = tonumber(file.readLine())
     holeCounter.line = tonumber(file.readLine())
     holeCounter.hole = tonumber(file.readLine())
-
-    printPos()
+    maxCompareSlot = tonumber(file.readLine())
 
     file.close()
+    print("Read state:")
+    printPos()
+
+    startupTimer()
 end
 
 function writeState()
@@ -82,6 +93,7 @@ function writeState()
     file.writeLine(tostring(facing.y))
     file.writeLine(tostring(holeCounter.line))
     file.writeLine(tostring(holeCounter.hole))
+    file.writeLine(tostring(maxCompareSlot))
 
     file.close()
 end
@@ -210,12 +222,46 @@ function moveTo(x, y)
     end
 end
 
+-- should only be called once when manually starting, not when restarting
+function initCompareSlots()
+    print("Setting up compare slots...")
+    
+    while true do
+        if (turtle.getItemCount(maxCompareSlot + 1) == 0) then
+            break
+        end
+        maxCompareSlot = maxCompareSlot + 1
+    end
+
+    print("Found ", maxCompareSlot, " compare slots!")
+end
+
+-- checks slots 1..maxCompareSlot against currently looked at block
+-- make sure that the selected slot == currentCompareSlot at all times
 function blockIsWorth()
-    for i = 1, compareSlotMax do
-        turtle.select(i)
+    if (maxCompareSlot <= 0) then
+        -- special case
+        return true
+    end
+
+    lastCompareSlot = currentCompareSlot
+    while true do
         if turtle.compare() then
             return false
         end
+
+        -- cycle currentCompareSlot, fmod would work perfectly if indices started at 0 -_-
+        nextCompareSlot = currentCompareSlot + 1
+        if nextCompareSlot > maxCompareSlot then
+            nextCompareSlot = 1
+        end
+
+        if (nextCompareSlot == lastCompareSlot) then
+            break
+        end
+
+        currentCompareSlot = nextCompareSlot
+        turtle.select(currentCompareSlot)
     end
 
     return true
@@ -284,6 +330,7 @@ function shouldReturnItems()
     return true
 end
 
+-- moves to the chest and deposits items, but only when deemed necessary by shouldReturnItems
 function returnItems()
     if not shouldReturnItems() then
         return
@@ -295,12 +342,22 @@ function returnItems()
     faceTo(-1, 0)
 
     -- already facing right direction, put items
-    for slot = compareSlotMax + 1, 16 do
+    for slot = 1, maxCompareSlot do
+        -- put away compare slots' items except one
+        turtle.select(slot)
+        turtle.drop(turtle.getItemCount(slot) - 1)
+    end
+    for slot = maxCompareSlot + 1, 16 do
+        -- then drop everything else
+        if (turtle.getItemCount(slot) == 0) then
+            break
+        end
         turtle.select(slot)
         turtle.drop()
     end
 
-    turtle.select(1)
+    -- necessary so the next mined block doesn't go in random places
+    turtle.select(currentCompareSlot)
 end
 
 function moveToNext()
@@ -345,8 +402,6 @@ end
 setupResume()
 readState()
 
-print("Starting...")
-
 while true do
     moveToNext()
     digHole()