Miner only returns when inventory is too full

This commit is contained in:
Anton Reinhard 2023-05-30 12:37:52 +02:00
parent 61677086e0
commit a33e28a071

View File

@ -15,38 +15,120 @@ holeCounter = {
} }
function printPos() function printPos()
print("At (", xRelPos, ", ", yRelPos, ") facing (", facing.x, ", ", facing.y, ")") print("At (", xRelPos, ", ", yRelPos, ", ", zRelPos, ") facing (", facing.x, ", ", facing.y, ")")
end
function setupResume()
if fs.exists("startup") then
return
end
local file = fs.open("startup", "w") --Startup File
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")
end
]])
file.close()
end
-- get all lines from a file, returns an empty
-- list/table if the file does not exist
function lines_from(file)
local lines = {}
for line in io.lines(file) do
lines[#lines + 1] = line
end
return lines
end
function readState()
if not fs.exists("state") then
return
end
print("Reading state after restart")
local file = fs.open("state", "r")
xRelPos = tonumber(file.readLine())
yRelPos = tonumber(file.readLine())
zRelPos = tonumber(file.readLine())
facing.x = tonumber(file.readLine())
facing.y = tonumber(file.readLine())
holeCounter.line = tonumber(file.readLine())
holeCounter.hole = tonumber(file.readLine())
printPos()
file.close()
end
function writeState()
if fs.exists("state") then
fs.delete("state")
end
file = fs.open("state", "w")
file.writeLine(tostring(xRelPos))
file.writeLine(tostring(yRelPos))
file.writeLine(tostring(zRelPos))
file.writeLine(tostring(facing.x))
file.writeLine(tostring(facing.y))
file.writeLine(tostring(holeCounter.line))
file.writeLine(tostring(holeCounter.hole))
file.close()
end end
-- helper functions for movement -- helper functions for movement
function forward() function forward()
xRelPos = xRelPos + facing.x
yRelPos = yRelPos + facing.y
writeState()
if turtle.forward() then if turtle.forward() then
xRelPos = xRelPos + facing.x return true
yRelPos = yRelPos + facing.y
else
if not turtle.dig() then
turtle.attack()
end
end end
xRelPos = xRelPos - facing.x
yRelPos = yRelPos - facing.y
writeState()
turtle.dig()
return false
end end
function upwards() function upwards()
zRelPos = zRelPos + 1
writeState()
if turtle.up() then if turtle.up() then
zRelPos = zRelPos + 1
return true return true
end end
zRelPos = zRelPos - 1
writeState()
turtle.digUp()
return false return false
end end
function downwards() function downwards()
zRelPos = zRelPos - 1
writeState()
if turtle.down() then if turtle.down() then
zRelPos = zRelPos - 1
return true return true
end end
zRelPos = zRelPos + 1
writeState()
turtle.digDown()
return false return false
end
function turnRight() function turnRight()
turtle.turnRight()
if facing.x == 1 and facing.y == 0 then -- facing forward if facing.x == 1 and facing.y == 0 then -- facing forward
facing.x = 0 facing.x = 0
facing.y = 1 facing.y = 1
@ -62,10 +144,11 @@ function turnRight()
else else
print("turnRight: This should never happen") print("turnRight: This should never happen")
end end
writeState()
turtle.turnRight()
end end
function turnLeft() function turnLeft()
turtle.turnLeft()
if facing.x == 1 and facing.y == 0 then -- facing forward if facing.x == 1 and facing.y == 0 then -- facing forward
facing.x = 0 facing.x = 0
facing.y = -1 facing.y = -1
@ -81,6 +164,8 @@ function turnLeft()
else else
print("turnLeft: This should never happen") print("turnLeft: This should never happen")
end end
writeState()
turtle.turnLeft()
end end
function faceTo(x, y) function faceTo(x, y)
@ -126,7 +211,6 @@ function moveTo(x, y)
end end
function blockIsWorth() function blockIsWorth()
for i = 1, compareSlotMax do for i = 1, compareSlotMax do
turtle.select(i) turtle.select(i)
if turtle.compare() then if turtle.compare() then
@ -137,9 +221,8 @@ function blockIsWorth()
return true return true
end end
function checkForItems(z) function checkForItems()
dir = math.fmod(zRelPos, 2)
dir = math.fmod(z, 2)
-- alternatingly check relative facing (left, forward, right) and (right, forward, left) blocks, so at the end we can face backwards when going up and check without turning at all -- alternatingly check relative facing (left, forward, right) and (right, forward, left) blocks, so at the end we can face backwards when going up and check without turning at all
-- -> two turns per mined level -- -> two turns per mined level
@ -174,13 +257,12 @@ end
-- Digs straight down right where it is, taking resources with it -- Digs straight down right where it is, taking resources with it
function digHole() function digHole()
z = 0 print("Digging Hole (", holeCounter.line, ", ", holeCounter.hole, ")")
-- go down -- go down
turtle.digDown() turtle.digDown()
while downwards() do while downwards() do
z = z + 1 checkForItems()
checkForItems(z)
turtle.digDown() turtle.digDown()
end end
@ -195,7 +277,18 @@ function digHole()
end end
end end
function shouldReturnItems()
if turtle.getItemCount(11) == 0 then
return false
end
return true
end
function returnItems() function returnItems()
if not shouldReturnItems() then
return
end
-- chest coordinates are -1, 0, so go to 0, 0 facing "backwards" -- chest coordinates are -1, 0, so go to 0, 0 facing "backwards"
moveTo(0, 0) moveTo(0, 0)
@ -206,9 +299,17 @@ function returnItems()
turtle.select(slot) turtle.select(slot)
turtle.drop() turtle.drop()
end end
turtle.select(1)
end end
function moveToNext() function moveToNext()
if not (zRelPos == 0) then
return -- we're resuming, don't move, don't increase hole
end
print("Moving to the next hole")
xPos = 1 xPos = 1
yPos = 1 yPos = 1
@ -241,16 +342,14 @@ function moveToNext()
end end
setupResume()
readState()
print("Starting...") print("Starting...")
-- startup
while not xRelPos == 1 do
forward()
end
while true do while true do
print("Digging Hole (", holeCounter.line, ", ", holeCounter.hole, ")")
moveToNext() moveToNext()
digHole() digHole()
print("Returning to chest")
returnItems() returnItems()
end end