Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function updateResumingFlag()
- if (resuming == true) then
- if ((resumeMiningState == currMiningState) and (resumeX == currX) and (resumeY == currY) and (resumeZ == currZ) and (resumeOrient == currOrient)) then
- resuming = false
- end
- end
- end
- function saveLocation()
- -- Write the x, y, z and orientation to the file
- if ((supportResume == true) and (resuming == false)) then
- local outputFile = io.open(oreQuarryLocation, "w")
- outputFile:write(currMiningState)
- outputFile:write("\n")
- outputFile:write(currX)
- outputFile:write("\n")
- outputFile:write(currY)
- outputFile:write("\n")
- outputFile:write(currZ)
- outputFile:write("\n")
- outputFile:write(currOrient)
- outputFile:write("\n")
- outputFile:close()
- end
- end
- -- Variables used to support a resume
- local startupParamsFile = "OreQuarryParams.txt"
- local oreQuarryLocation = "OreQuarryLocation.txt"
- local returnToStartFile = "OreQuarryReturn.txt"
- local startupBackup = "startup_bak"
- local supportResume = true -- Determines whether the turtle is being run in the mode that supports resume
- local resuming = false -- Determines whether the turtle is currently in the process of resuming
- local resumeX
- local resumeY
- local resumeZ
- local resumeOrient
- local resumeMiningState
- -- Variables to store the current location and orientation of the turtle. x is right, left, y is up, down and
- -- z is forward, back with relation to the starting orientation. Y is the actual turtle level, x and z are
- -- in relation to the starting point (i.e. the starting point is (0, 0))
- local currX
- local currY
- local currZ
- local currOrient
- local currMiningState = miningState.START
- -- Command line parameters
- local startHeight -- Represents the height (y co-ord) that the turtle started at
- local quarryWidth -- Represents the length of the mines that the turtle will dig
- if (resuming == true) then
- -- Get the stored parameters from the necessary file
- local resumeFile = fs.open(returnToStartFile, "r")
- if (resumeFile ~= nil) then
- -- Restore the parameters from the file
- local beenAtZero = resumeFile.readLine()
- if (beenAtZero == "y") then
- haveBeenAtZeroZeroOnLayer = true
- else
- haveBeenAtZeroZeroOnLayer = false
- end
- local miningPointFlag = resumeFile.readLine()
- if (miningPointFlag == "y") then
- returnBackToMiningPoint = true
- else
- returnBackToMiningPoint = false
- end
- currX = readNumber(resumeFile)
- currY = readNumber(resumeFile)
- currZ = readNumber(resumeFile)
- currOrient = readNumber(resumeFile)
- levelToReturnTo = readNumber(resumeFile)
- prevMiningState = readNumber(resumeFile)
- orientationAtZeroZero = readNumber(resumeFile)
- resumeFile.close()
- else
- writeMessage("Failed to read return to start file", messageLevel.ERROR)
- end
- elseif (supportResume == true) then
- local outputFile = io.open(returnToStartFile, "w")
- if (haveBeenAtZeroZeroOnLayer == true) then
- outputFile:write("y\n")
- else
- outputFile:write("n\n")
- end
- if (returnBackToMiningPoint == true) then
- outputFile:write("y\n")
- else
- outputFile:write("n\n")
- end
- outputFile:write(currX)
- outputFile:write("\n")
- outputFile:write(currY)
- outputFile:write("\n")
- outputFile:write(currZ)
- outputFile:write("\n")
- outputFile:write(currOrient)
- outputFile:write("\n")
- outputFile:write(levelToReturnTo)
- outputFile:write("\n")
- outputFile:write(prevMiningState)
- outputFile:write("\n")
- outputFile:write(orientationAtZeroZero)
- outputFile:write("\n")
- outputFile:close()
- end
- storedX = currX
- storedY = currY
- storedZ = currZ
- storedOrient = currOrient
- -- Store the current location and orientation so that it can be returned to
- currMiningState = miningState.EMPTYINVENTORY
- writeMessage("last item count = "..turtle.getItemCount(lastEmptySlot), messageLevel.DEBUG)
- if ((turtle.getItemCount(lastEmptySlot) > 0) or (returnBackToMiningPoint == false)) then
- writeMessage("Heading back to surface", messageLevel.INFO)
- -- Move down to the correct layer to return via
- if (currY > levelToReturnTo) then
- while (currY > levelToReturnTo) do
- turtleDown()
- end
- elseif (currY < levelToReturnTo) then
- while (currY < levelToReturnTo) do
- turtleUp()
- end
- end
- if ((haveBeenAtZeroZeroOnLayer == false) or (orientationAtZeroZero == direction.FORWARD)) then
- -- Move back to the correct X position first
- if (currX > 0) then
- turtleSetOrientation(direction.LEFT)
- while (currX > 0) do
- turtleForward()
- end
- elseif (currX < 0) then
- -- This should never happen
- writeMessage("Current x is less than 0 in returnToStartAndUnload", messageLevel.ERROR)
- end
- -- Then move back to the correct Z position
- if (currZ > 0) then
- turtleSetOrientation(direction.BACK)
- while (currZ > 0) do
- turtleForward()
- end
- elseif (currZ < 0) then
- -- This should never happen
- writeMessage("Current z is less than 0 in returnToStartAndUnload", messageLevel.ERROR)
- end
- else
- -- Move back to the correct Z position first
- if (currZ > 0) then
- turtleSetOrientation(direction.BACK)
- while (currZ > 0) do
- turtleForward()
- end
- elseif (currZ < 0) then
- -- This should never happen
- writeMessage("Current z is less than 0 in returnToStartAndUnload", messageLevel.ERROR)
- end
- -- Then move back to the correct X position
- if (currX > 0) then
- turtleSetOrientation(direction.LEFT)
- while (currX > 0) do
- turtleForward()
- end
- elseif (currX < 0) then
- -- This should never happen
- writeMessage("Current x is less than 0 in returnToStartAndUnload", messageLevel.ERROR)
- end
- end
- -- Return to the starting layer
- if (currY < startHeight) then
- while (currY < startHeight) do
- turtleUp()
- end
- elseif (currY > startHeight) then
- -- This should never happen
- writeMessage("Current height is greater than start height in returnToStartAndUnload", messageLevel.ERROR)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement