Miner only returns when inventory is too full
This commit is contained in:
parent
61677086e0
commit
a33e28a071
@ -15,38 +15,120 @@ holeCounter = {
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
-- helper functions for movement
|
||||
function forward()
|
||||
if turtle.forward() then
|
||||
xRelPos = xRelPos + facing.x
|
||||
yRelPos = yRelPos + facing.y
|
||||
else
|
||||
if not turtle.dig() then
|
||||
turtle.attack()
|
||||
end
|
||||
writeState()
|
||||
|
||||
if turtle.forward() then
|
||||
return true
|
||||
end
|
||||
|
||||
xRelPos = xRelPos - facing.x
|
||||
yRelPos = yRelPos - facing.y
|
||||
writeState()
|
||||
|
||||
turtle.dig()
|
||||
return false
|
||||
end
|
||||
|
||||
function upwards()
|
||||
if turtle.up() then
|
||||
zRelPos = zRelPos + 1
|
||||
writeState()
|
||||
if turtle.up() then
|
||||
return true
|
||||
end
|
||||
zRelPos = zRelPos - 1
|
||||
writeState()
|
||||
turtle.digUp()
|
||||
return false
|
||||
end
|
||||
|
||||
function downwards()
|
||||
if turtle.down() then
|
||||
zRelPos = zRelPos - 1
|
||||
writeState()
|
||||
if turtle.down() then
|
||||
return true
|
||||
end
|
||||
zRelPos = zRelPos + 1
|
||||
writeState()
|
||||
turtle.digDown()
|
||||
return false
|
||||
end
|
||||
|
||||
function turnRight()
|
||||
turtle.turnRight()
|
||||
if facing.x == 1 and facing.y == 0 then -- facing forward
|
||||
facing.x = 0
|
||||
facing.y = 1
|
||||
@ -62,10 +144,11 @@ function turnRight()
|
||||
else
|
||||
print("turnRight: This should never happen")
|
||||
end
|
||||
writeState()
|
||||
turtle.turnRight()
|
||||
end
|
||||
|
||||
function turnLeft()
|
||||
turtle.turnLeft()
|
||||
if facing.x == 1 and facing.y == 0 then -- facing forward
|
||||
facing.x = 0
|
||||
facing.y = -1
|
||||
@ -81,6 +164,8 @@ function turnLeft()
|
||||
else
|
||||
print("turnLeft: This should never happen")
|
||||
end
|
||||
writeState()
|
||||
turtle.turnLeft()
|
||||
end
|
||||
|
||||
function faceTo(x, y)
|
||||
@ -126,7 +211,6 @@ function moveTo(x, y)
|
||||
end
|
||||
|
||||
function blockIsWorth()
|
||||
|
||||
for i = 1, compareSlotMax do
|
||||
turtle.select(i)
|
||||
if turtle.compare() then
|
||||
@ -137,9 +221,8 @@ function blockIsWorth()
|
||||
return true
|
||||
end
|
||||
|
||||
function checkForItems(z)
|
||||
|
||||
dir = math.fmod(z, 2)
|
||||
function checkForItems()
|
||||
dir = math.fmod(zRelPos, 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
|
||||
-- -> two turns per mined level
|
||||
@ -174,13 +257,12 @@ end
|
||||
|
||||
-- Digs straight down right where it is, taking resources with it
|
||||
function digHole()
|
||||
z = 0
|
||||
print("Digging Hole (", holeCounter.line, ", ", holeCounter.hole, ")")
|
||||
|
||||
-- go down
|
||||
turtle.digDown()
|
||||
while downwards() do
|
||||
z = z + 1
|
||||
checkForItems(z)
|
||||
checkForItems()
|
||||
turtle.digDown()
|
||||
end
|
||||
|
||||
@ -195,7 +277,18 @@ function digHole()
|
||||
end
|
||||
end
|
||||
|
||||
function shouldReturnItems()
|
||||
if turtle.getItemCount(11) == 0 then
|
||||
return false
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
function returnItems()
|
||||
if not shouldReturnItems() then
|
||||
return
|
||||
end
|
||||
|
||||
-- chest coordinates are -1, 0, so go to 0, 0 facing "backwards"
|
||||
moveTo(0, 0)
|
||||
|
||||
@ -206,9 +299,17 @@ function returnItems()
|
||||
turtle.select(slot)
|
||||
turtle.drop()
|
||||
end
|
||||
|
||||
turtle.select(1)
|
||||
end
|
||||
|
||||
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
|
||||
yPos = 1
|
||||
|
||||
@ -241,16 +342,14 @@ function moveToNext()
|
||||
|
||||
end
|
||||
|
||||
setupResume()
|
||||
readState()
|
||||
|
||||
print("Starting...")
|
||||
|
||||
-- startup
|
||||
while not xRelPos == 1 do
|
||||
forward()
|
||||
end
|
||||
|
||||
while true do
|
||||
print("Digging Hole (", holeCounter.line, ", ", holeCounter.hole, ")")
|
||||
moveToNext()
|
||||
digHole()
|
||||
print("Returning to chest")
|
||||
returnItems()
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user