Fix issue 2 (#4)

See #2

Co-authored-by: Anton Reinhard <s1509337@msx.tu-dresden.de>
Reviewed-on: Rubydragon/fusion-speedrun#4
This commit is contained in:
Anton Reinhard 2023-09-30 13:20:05 +02:00
parent 97ea7e396e
commit c66736cf44
2 changed files with 69 additions and 14 deletions

View File

@ -1,6 +1,6 @@
pastebin get XdpnRKDf mine pastebin get gGy2HWcF mine
refuel all refuel all
mining mine
# quarry program # quarry program
quarry -oreQuarry true -enderChest 16 -doRefuel false -dumpCompareItems false -invert false -rednet false -dim 16 16 70 quarry -oreQuarry true -enderChest 16 -doRefuel false -dumpCompareItems false -invert false -rednet false -dim 16 16 70

View File

@ -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" xRelPos = 0 -- "positive = forward"
yRelPos = 0 -- "positive = right" yRelPos = 0 -- "positive = right"
@ -18,6 +21,12 @@ function printPos()
print("At (", xRelPos, ", ", yRelPos, ", ", zRelPos, ") facing (", facing.x, ", ", facing.y, ")") print("At (", xRelPos, ", ", yRelPos, ", ", zRelPos, ") facing (", facing.x, ", ", facing.y, ")")
end end
function startupTimer()
print("Waiting ", startupWait, "s before starting... ")
sleep(startupWait)
print("Starting!")
end
function setupResume() function setupResume()
if fs.exists("startup") then if fs.exists("startup") then
return return
@ -26,14 +35,12 @@ function setupResume()
file.writeLine( --The below is on the left because spacing file.writeLine( --The below is on the left because spacing
[[ [[
print("Resuming Miner") print("Resuming Miner")
function deleteStuff()
fs.delete("startup")
end
local event local event
if fs.exists("state") then if fs.exists("state") then
os.run({},"]]..shell.getRunningProgram()..[[") os.run({},"]]..shell.getRunningProgram()..[[")
else else
print("Never mind, no save file found") print("Never mind, no save file found")
fs.delete("startup")
end end
]]) ]])
file.close() file.close()
@ -51,6 +58,7 @@ end
function readState() function readState()
if not fs.exists("state") then if not fs.exists("state") then
initCompareSlots()
return return
end end
print("Reading state after restart") print("Reading state after restart")
@ -63,10 +71,13 @@ function readState()
facing.y = tonumber(file.readLine()) facing.y = tonumber(file.readLine())
holeCounter.line = tonumber(file.readLine()) holeCounter.line = tonumber(file.readLine())
holeCounter.hole = tonumber(file.readLine()) holeCounter.hole = tonumber(file.readLine())
maxCompareSlot = tonumber(file.readLine())
printPos()
file.close() file.close()
print("Read state:")
printPos()
startupTimer()
end end
function writeState() function writeState()
@ -82,6 +93,7 @@ function writeState()
file.writeLine(tostring(facing.y)) file.writeLine(tostring(facing.y))
file.writeLine(tostring(holeCounter.line)) file.writeLine(tostring(holeCounter.line))
file.writeLine(tostring(holeCounter.hole)) file.writeLine(tostring(holeCounter.hole))
file.writeLine(tostring(maxCompareSlot))
file.close() file.close()
end end
@ -210,12 +222,46 @@ function moveTo(x, y)
end end
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() function blockIsWorth()
for i = 1, compareSlotMax do if (maxCompareSlot <= 0) then
turtle.select(i) -- special case
return true
end
lastCompareSlot = currentCompareSlot
while true do
if turtle.compare() then if turtle.compare() then
return false return false
end 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 end
return true return true
@ -284,6 +330,7 @@ function shouldReturnItems()
return true return true
end end
-- moves to the chest and deposits items, but only when deemed necessary by shouldReturnItems
function returnItems() function returnItems()
if not shouldReturnItems() then if not shouldReturnItems() then
return return
@ -295,12 +342,22 @@ function returnItems()
faceTo(-1, 0) faceTo(-1, 0)
-- already facing right direction, put items -- 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.select(slot)
turtle.drop() turtle.drop()
end end
turtle.select(1) -- necessary so the next mined block doesn't go in random places
turtle.select(currentCompareSlot)
end end
function moveToNext() function moveToNext()
@ -345,8 +402,6 @@ end
setupResume() setupResume()
readState() readState()
print("Starting...")
while true do while true do
moveToNext() moveToNext()
digHole() digHole()