Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- *********************************
- -- Quarry Turtle Program
- -- By Jharakn
- -- *********************************
- -- Change Log
- -- 0.1: first release
- -- 0.2: update startup to allow turtle to move down to first solid ground 12/01/13
- -- 0.3: pushed the os.getComputerLabel() in the broadcast function to a global variable on suspicion that too many calls of it was causing server instability.
- -- 0.4 (27/03/13): updated fuelcheck function to not crash if the server it set up that computercraft turtles use no fuel.
- -- 0.5 (04/04/13): changed the gps check at home location to move under the chest not above it to prevent clash my my mining barge.
- -- *********************** --
- -- Create Global Variables --
- -- *********************** --
- -- using computercraft GPS nomenclature so x is north/south y is east/west and z is up/down
- current = {x=0, y=0, z=0, d=0}
- home = {x=0, y=0, z=0}
- --Quarry point that the turtle was last at, might be a little out of date due to the writescheduler
- quarry = {x=0, y=0, z=0, d=0}
- -- defined at startup these boundaries mark the edge of the quarry
- boundary = {xMax=0, xMin=0, yMax=0, yMin=0}
- -- these directions will be updated with the gps function, numbers set to absolute
- -- values based on north/easet/south/west values
- direction = {forward=0, right=0, back=0, left =0}
- -- various operation flags for the turtle
- flag = {xReverse=false, yReverse=false, atHome=false, quarrying=true, quarryComplete=false}
- -- misc variables for the turtle
- filterSlots = 1
- bedrockCounter = 1
- bedrockMessageCounter = 1
- -- misc variables that are not written to the save file
- writeSchedulerCounter = 0
- turtleName = os.getComputerLabel()
- -- ********************************************************************** --
- -- close the program dispaying error message the function was called with --
- -- ********************************************************************** --
- function errorStop(displayMessage)
- broadcast("Error - "..displayMessage)
- print("")
- print("turtle encountered an error and stopped")
- print("message recieved was:")
- print(displayMessage)
- print("")
- print("press r to restart")
- while true do
- local event, character = os.pullEvent()
- if event == "char" and character == "r" then break end
- end
- os.reboot()
- end
- -- ****************************************************** --
- -- prints a debug message of most of the global variables --
- -- call with true to end the program too --
- -- ****************************************************** --
- function debugger(endProgram)
- term.clear()
- term.setCursorPos(1, 1)
- print("x coords")
- print("xMin:", boundary.xMin, " current.x:", current.x, " xMax:", boundary.xMax)
- print("y coords")
- print("yMin:", boundary.yMin, " current.y:", current.y, " yMax:", boundary.yMax)
- print("flag.xReverse=", flag.xReverse, " flag.yReverse=", flag.yReverse)
- print("current.d:", current.d)
- if endProgram == true then
- errorStop("debugger wanted to close program")
- end
- end
- -- ************************************ --
- -- halts the program until c is pressed --
- -- ************************************ --
- function halt()
- while true do
- local event, character = os.pullEvent()
- if event == "char" and character == "c" then break end
- end
- end
- -- **************************************************************** --
- -- Broadcasts a message the function was called with to the rednet --
- -- **************************************************************** --
- function broadcast(message)
- print(message)
- rednet.open("right")
- if (turtleName == nil) then
- rednet.broadcast(message)
- else
- -- Broadcast the message (prefixed with the turtle's id)
- rednet.broadcast("[".. turtleName.."] "..message)
- end
- rednet.close("right")
- end
- -- ************************************************************ --
- -- a variaty of tests to ensure the turtle hasn't wandered off --
- -- ************************************************************ --
- function positionCheck()
- --5 is added to the boundarys just in case the turtle is picked up and dropped outside the boundarys of the quarry by a player
- if current.x < (boundary.xMin -5) then
- errorStop("turtle Moved outside its boundaries")
- end
- if current.x < (boundary.xMax +5) then
- errorStop("turtle Moved outside its boundaries")
- end
- if current.y < (boundary.yMin -5) then
- errorStop("turtle Moved outside its boundaries")
- end
- if current.y < (boundary.xMax +5) then
- errorStop("turtle Moved outside its boundaries")
- end
- local zCheck = false
- if current.z >= 7 then
- for i = 7, 199, 3 do
- if current.z == i then
- zCheck = true
- break
- end
- if zCheck == false then
- errorStop("turtle Quarrying on wrong level")
- end
- end
- end
- end
- -- **************************** --
- -- Move Up and update current.z --
- -- **************************** --
- function moveUp()
- local counter = 0
- while turtle.up() == false do
- turtle.digUp()
- turtle.attackUp()
- counter = counter + 1
- if counter == 30 then break end
- end
- if counter == 100 then
- errorStop("timeout on dig up")
- else
- current.z = current.z + 1
- end
- end
- -- ************************************************* --
- -- Move Down and update current.z, call with true to --
- -- make the function terminate early and return --
- -- false if it can't move for detecting bedrock --
- -- ************************************************* --
- function moveDown(bedrockDetect)
- local counter = 0
- local returnVar = true
- while turtle.down() == false do
- turtle.digDown()
- turtle.attackDown()
- counter = counter + 1
- if bedrockDetect == true and counter == 2 then
- returnVar = false
- break
- end
- if counter == 30 then break end
- end
- if counter == 100 then
- errorStop("timeout on dig down")
- else
- if returnVar == true then
- current.z = current.z - 1
- end
- return returnVar
- end
- end
- -- ********************************************** --
- -- Move Forward and update current.x or current.y --
- -- ********************************************** --
- function moveForward()
- local counter = 0
- while turtle.forward() == false do
- turtle.dig()
- turtle.attack()
- counter = counter + 1
- if counter == 30 then break end
- end
- if counter == 100 then
- errorStop("timeout on dig forward")
- else
- if current.d == 1 then current.y = current.y + 1 end
- if current.d == 2 then current.x = current.x + 1 end
- if current.d == 3 then current.y = current.y - 1 end
- if current.d == 4 then current.x = current.x - 1 end
- end
- end
- -- ****************************************************************** --
- -- Turn the turtle based on input and current.d and update current.d --
- -- ****************************************************************** --
- function turn(turnToDirection)
- while current.d ~= turnToDirection do
- if current.d < turnToDirection then
- if current.d == 1 and turnToDirection == 4 then
- turtle.turnLeft()
- current.d = 4
- else
- turtle.turnRight()
- current.d = current.d + 1
- end
- else
- if current.d == 4 and turnToDirection == 1 then
- turtle.turnRight()
- current.d = 1
- else
- turtle.turnLeft()
- current.d = current.d - 1
- end
- end
- end
- end
- -- ************************************************************** --
- -- Goto as specific location and turn to face the right direction --
- -- if reverse is true the turtle matches x then y then z rather --
- -- than z then y then x --
- -- ************************************************************** --
- function goto(x, y, z, d, reverse)
- if reverse == false then
- while current.z ~= z do -- match z location
- if current.z < z then
- moveUp()
- writeScheduler()
- fuelCheck()
- elseif current.z > z then
- moveDown(false)
- writeScheduler()
- fuelCheck()
- end
- end
- elseif reverse == true then
- while current.x ~= x do -- match x location
- if current.x < x then
- turn(2)
- moveForward()
- writeScheduler()
- fuelCheck()
- elseif current.x > x then
- turn(4)
- moveForward()
- writeScheduler()
- fuelCheck()
- end
- end
- else
- errorStop("goto function incorrectly called")
- end
- while current.y ~= y do -- match y location
- if current.y < y then
- turn(1)
- moveForward()
- writeScheduler()
- fuelCheck()
- elseif current.y > y then
- turn(3)
- moveForward()
- writeScheduler()
- fuelCheck()
- end
- end
- if reverse == false then
- while current.x ~= x do -- match x location
- if current.x < x then
- turn(2)
- moveForward()
- writeScheduler()
- fuelCheck()
- elseif current.x > x then
- turn(4)
- moveForward()
- writeScheduler()
- fuelCheck()
- end
- end
- elseif reverse == true then
- while current.z ~= z do -- match z location
- if current.z < z then
- moveUp()
- writeScheduler()
- fuelCheck()
- elseif current.z > z then
- moveDown(false)
- writeScheduler()
- fuelCheck()
- end
- end
- else
- errorStop("goto function incorrectly called")
- end
- turn(d) -- turn to face the right direction
- end
- -- *************************************************************************************************** --
- -- Use the GPS function to detect turtle position and move forward and back to detect facing direction --
- -- if called with true will update direction table --
- -- *************************************************************************************************** --
- function GPS(updateDirection)
- local forward = 0
- local right = 0
- local back = 0
- local left = 0
- rednet.open("right")
- local x1, y1, z1 = gps.locate(5,false)
- if x1 == nil then
- errorStop("no GPS signal")
- end
- --If the turtle finds itself below layer 5 move back up to layer 5, then run the gps again
- --otherwise it might get blocked by bedrock
- if z1 < 5 then
- local counter = z1
- while counter ~= 5 do
- moveUp()
- counter = counter + 1
- end
- x1, y1, z1 = gps.locate(5,false)
- if x1 == nil then
- errorStop("no GPS signal")
- end
- end
- moveForward()
- local x2, y2, z2 = gps.locate(5,false)
- if x2 == nil then
- errorStop("no GPS signal")
- end
- turtle.back()
- rednet.close("right")
- if y1 - y2 == -1 then --have taken two measurments can use the change to work out which direction the turtle moved in
- current.d = 1; forward = 1; right = 2; back = 3; left = 4
- elseif x1 - x2 == -1 then
- current.d = 2; forward = 2; right = 3; back = 4; left = 1
- elseif y1 - y2 == 1 then
- current.d = 3; forward = 3; right = 4; back = 1; left = 2
- elseif x1 - x2 == 1 then
- current.d = 4; forward = 4; right = 1; back = 2; left = 3
- else
- errorStop("both GPS samples are identical, what?!?!?")
- end
- current.x = x1; current.y = y1; current.z = z1
- if updateDirection == true then
- direction.forward = forward; direction.right = right; direction.back = back; direction.left = left
- end
- end
- -- ************************************************************** --
- -- write the gloabal variables to a file named QuarryInstructions --
- -- NOTE: this function should always be called with pcall --
- -- or a server lag spike might crash the program --
- -- ************************************************************** --
- function writeVariables()
- local writeTable = {} --build a table to serialise into the new file with all the global variables
- writeTable["current_x"] = current.x; writeTable["current_y"] = current.y; writeTable["current_z"] = current.z; writeTable["current_d"] = current.d
- writeTable["home_x"] = home.x; writeTable["home_y"] = home.y; writeTable["home_z"] = home.z
- writeTable["quarry_x"] = quarry.x; writeTable["quarry_y"] = quarry.y; writeTable["quarry_z"] = quarry.z; writeTable["quarry_d"] = quarry.d
- writeTable["boundary_xMax"] = boundary.xMax; writeTable["boundary_xMin"] = boundary.xMin; writeTable["boundary_yMax"] = boundary.yMax; writeTable["boundary_yMin"] = boundary.yMin
- writeTable["direction_forward"] = direction.forward; writeTable["direction_right"] = direction.right; writeTable["direction_back"] = direction.back; writeTable["direction_left"] = direction.left
- writeTable["flag_xReverse"] = flag.xReverse; writeTable["flag_yReverse"] = flag.yReverse; writeTable["flag_atHome"] = flag.atHome; writeTable["flag_quarrying"] = flag.quarrying; writeTable["flag_quarryComplete"] = flag.quarryComplete
- writeTable["filterSlots"] = filterSlots; writeTable["bedrockCounter"] = bedrockCounter; writeTable["bedrockMessageCounter"] = bedrockMessageCounter
- local file = fs.open("QuarryInstructions","w") --serialise and write the writeTable variable
- file.write(textutils.serialize(writeTable))
- file.close()
- end
- -- ******************************************************************** --
- -- calls the write function ever nth time, designed to prevent constant --
- -- write calls generating server lag --
- -- ******************************************************************** --
- function writeScheduler()
- --adjust this variable for delay to instruction saving, higher value = less server lag but a greater error when the turtle is restarting
- local saveDelay = 10
- writeSchedulerCounter = writeSchedulerCounter + 1
- if writeSchedulerCounter == saveDelay then
- if pcall(writeVariables) == false then
- broadcast("Warning - Write failure")
- end
- writeSchedulerCounter = 0
- end
- end
- -- *********************************************************************************************************************************** --
- -- Start the program, checking for an existing QuarryInstructions file, if it exists it will detect its location with gps and continue --
- -- otherwise it will ask the user for a new instruction set, check its gps location and begin --
- -- *********************************************************************************************************************************** --
- function startup()
- if fs.exists("QuarryInstructions") == true then --existing instructions startup
- --Read the Quarry instructions file and update global variables
- local file = fs.open("QuarryInstructions","r")
- local data = file.readAll()
- file.close()
- local readTable = {}
- readTable = textutils.unserialize(data)
- current.x = readTable["current_x"]; current.y = readTable["current_y"]; current.z = readTable["current_z"]; current.d = readTable["current_d"]
- home.x = readTable["home_x"]; home.y = readTable["home_y"]; home.z = readTable["home_z"]
- quarry.x = readTable["quarry_x"]; quarry.y = readTable["quarry_y"]; quarry.z = readTable["quarry_z"]; quarry.d = readTable["quarry_d"]
- boundary.xMax = readTable["boundary_xMax"]; boundary.xMin = readTable["boundary_xMin"]; boundary.yMax = readTable["boundary_yMax"]; boundary.yMin = readTable["boundary_yMin"]
- direction.forward = readTable["direction_forward"]; direction.right = readTable["direction_right"]; direction.back = readTable["direction_back"]; direction.left = readTable["direction_left"]
- flag.xReverse = readTable["flag_xReverse"]; flag.yReverse = readTable["flag_yReverse"]; flag.atHome = readTable["flag_atHome"]; flag.quarrying = readTable["flag_quarrying"]; flag.quarryComplete = readTable["flag_quarryComplete"]
- filterSlots = readTable["filterSlots"]; bedrockCounter = readTable["bedrockCounter"]; bedrockMessageCounter = readTable["bedrockMessageCounter"]
- --examine the atHome flag, if true a gps cycle might cause the turtle to destroy a chest so we move down a space and run the gps instead
- if flag.atHome == true then
- moveDown(false)
- GPS()
- moveUp()
- elseif flag.atHome == false then
- GPS()
- else
- errorStop("corrupted GPS flag at Startup")
- end
- --examine the quarrying flag, if its true return to the last known quarrying point and continue
- --if its false we can assume it was doing an unload cycle, so do that which will automatically
- --return to the quarry point on completion
- if flag.quarrying == true then
- goto(quarry.x, quarry.y, quarry.z, quarry.d, false)
- elseif flag.quarrying == false then
- unloadCycle()
- else
- errorStop("corrupted quarry flag at startup")
- end
- broadcast("Resuming existing Quarry")
- else -- new quarry startup
- term.clear() --ask the user to enter a quarry size
- term.setCursorPos(1, 1)
- print("**********************************")
- print("** Quarrying Program by Jharakn **")
- print("**********************************")
- print("")
- print("No previous instruction set found, starting new quarry:")
- print("Please enter a Quarry Diameter")
- local quarryDiameter = (tonumber(read()) - 1) --need a data validation step here
- print("Thank you, starting operation")
- sleep(3)
- term.clear()
- term.setCursorPos(1, 1)
- GPS(true) --grab the turtle location and update its direction table
- home.x = current.x; home.y = current.y; home.z = current.z --make the home locations
- if current.d == 1 then --update the boundaries and flags based on the turtles current direction
- boundary.xMax = current.x + quarryDiameter
- boundary.xMin = current.x
- boundary.yMax = current.y + quarryDiameter
- boundary.yMin = current.y
- flag.xReverse = false
- flag.yReverse = false
- flag.atHome = false; flag.Quarrying = true; flag.quarryComplete = false
- elseif current.d == 2 then
- boundary.xMax = current.x + quarryDiameter
- boundary.xMin = current.x
- boundary.yMax = current.y
- boundary.yMin = current.y - quarryDiameter
- flag.xReverse = false
- flag.yReverse = true
- flag.atHome = false; flag.Quarrying = true; flag.quarryComplete = false
- elseif current.d == 3 then
- boundary.xMax = current.x
- boundary.xMin = current.x - quarryDiameter
- boundary.yMax = current.y
- boundary.yMin = current.y - quarryDiameter
- flag.xReverse = true
- flag.yReverse = true
- flag.atHome = false; flag.Quarrying = true; flag.quarryComplete = false
- elseif current.d == 4 then
- boundary.xMax = current.x
- boundary.xMin = current.x - quarryDiameter
- boundary.yMax = current.y + quarryDiameter
- boundary.yMin = current.y
- flag.xReverse = true
- flag.yReverse = false
- flag.atHome = false; flag.Quarrying = true; flag.quarryComplete = false
- else
- errorStop("corrupted current.d value in startup")
- end
- local i = 1
- while turtle.getItemCount(i) ~= 0 do
- i = i + 1
- end
- filterSlots = i - 1 -- because it will count the first empty slot and you the last full one which was the previous number
- -- Move down until it hits solid ground (needed since my mining barge floats in the air)
- while turtle.detectDown() == false do
- moveDown()
- end
- local startLayer = 7
- while (current.z - startLayer) >= 3 do -- work out which level the turtle need to start on to terminate part 1 at level 5
- startLayer = startLayer + 3
- end
- while current.z ~= startLayer do
- moveDown(false)
- end
- broadcast("Building New Quarry")
- end
- end
- -- **************************************************************************** --
- -- checks above and below the turtle for filtered blocks as defined at startup --
- -- if there not filtered the turtle mines them --
- -- **************************************************************************** --
- function oreCheck()
- local mineUp = true
- local mineDown = true
- if current.z == 5 then --Bedrock cleanup section
- while moveDown(true) == true do sleep(0) end
- while current.z ~= 5 do moveUp() end
- --Broadcaster for bedrock to stop the turtle being so quiet when its clearing bedrock
- local bedrockDone = bedrockCounter / ((boundary.xMax - boundary.xMin + 1) * (boundary.yMax - boundary.yMin + 1) + 1)
- if bedrockDone >= 0.1 and bedrockMessageCounter == 1 then
- broadcast("("..os.time()..")Clearing Bedrock, 10% complete")
- bedrockMessageCounter = 2
- end
- if bedrockDone >= 0.2 and bedrockMessageCounter == 2 then
- broadcast("Clearing Bedrock, 20% complete")
- bedrockMessageCounter = 3
- end
- if bedrockDone >= 0.3 and bedrockMessageCounter == 3 then
- broadcast("("..os.time()..")Clearing Bedrock, 30% complete")
- bedrockMessageCounter = 4
- end
- if bedrockDone >= 0.4 and bedrockMessageCounter == 4 then
- broadcast("("..os.time()..")Clearing Bedrock, 40% complete")
- bedrockMessageCounter = 5
- end
- if bedrockDone >= 0.5 and bedrockMessageCounter == 5 then
- broadcast("("..os.time()..")Clearing Bedrock, 50% complete")
- bedrockMessageCounter = 6
- end
- if bedrockDone >= 0.6 and bedrockMessageCounter == 6 then
- broadcast("("..os.time()..")Clearing Bedrock, 60% complete")
- bedrockMessageCounter = 7
- end
- if bedrockDone >= 0.7 and bedrockMessageCounter == 7 then
- broadcast("("..os.time()..")Clearing Bedrock, 70% complete")
- bedrockMessageCounter = 8
- end
- if bedrockDone >= 0.8 and bedrockMessageCounter == 8 then
- broadcast("("..os.time()..")Clearing Bedrock, 80% complete")
- bedrockMessageCounter = 9
- end
- if bedrockDone >= 0.9 and bedrockMessageCounter == 9 then
- broadcast("("..os.time()..")Clearing Bedrock, 90% complete")
- bedrockMessageCounter = nil
- end
- bedrockCounter = bedrockCounter + 1
- else -- Normal quarrying section
- mineUp = turtle.detectUp(); mineDown = turtle.detectDown() --add air to make a potential filter block
- if mineUp == true or mineDown == true then
- for i = 1,filterSlots do
- turtle.select(i)
- if turtle.compareUp() == true then mineUp = false end
- if turtle.compareDown() == true then mineDown = false end
- end
- if mineUp == true then turtle.digUp() end
- if mineDown == true then turtle.digDown() end
- end
- if mineUp == true or mineDown == true then
- broadcast("("..os.time()..")Found Ore @ Level "..current.z)
- end
- end
- end
- -- *************************************************************************************** --
- -- Refuels the turtle if low on fuel from slot 16, returns false if slot 16 is running low --
- -- *************************************************************************************** --
- function fuelCheck()
- --server might be set up that turtles need no fuel check that
- if turtle.getFuelLevel() ~= nil then
- if turtle.getFuelLevel() < 10 then
- turtle.select(16)
- turtle.refuel(1)
- turtle.select(1)
- end
- if turtle.getItemCount(16) < 10 then
- broadcast("Fuel Low - returning home")
- return false
- else
- return true
- end
- end
- end
- -- ********************************************************************************************** --
- -- checks slot 15 in the inventory, if it has an item the turtle needs unloading so returns false --
- -- ********************************************************************************************** --
- function invCheck()
- if turtle.getItemCount(15) == 0 then
- return true
- else
- broadcast("Inventory full - returning home")
- return false
- end
- end
- -- ************************************************************************************************* --
- -- Returns home to unload its inventory and top up its fuel supplies then return to the mining point --
- -- ************************************************************************************************* --
- function unloadCycle()
- --update the turtle objective just in case the chunk unloads whilst its traveling home
- --and commit these changes to memory this will also update the quarry locations so its
- --got the freshest locations
- flag.quarrying = false
- pcall(writeVariables)
- --return home
- goto(home.x, home.y, home.z, direction.back, false)
- --update flag.atHome so if it loads again at this point it won't eat its output chest
- flag.atHome = true
- pcall(writeVariables)
- --turn to face unload chest and unload filter items till just 1 remain each
- turn(direction.back)
- for i = 1,filterSlots do
- turtle.select(i)
- if turtle.drop(turtle.getItemCount(i)-1) == false then
- errorStop("Output chest has overflowed")
- end
- end
- --output all of the remaining items except fuel in slot 16
- for i = filterSlots + 1, 15 do
- turtle.select(i)
- if turtle.getItemCount(i) ~= 0 then
- if turtle.drop() == false then
- errorStop("Output chest has overflowed")
- end
- end
- end
- -- turn to face the fuel chest and top off slot 16
- turn(direction.left)
- turtle.select(15)
- if turtle.suck() == false then
- broadcast("Warning - Fuel chest empty")
- end
- turtle.transferTo(16, turtle.getItemSpace(16))
- turtle.drop()
- turtle.select(1)
- -- update the turtle objective and return to the quarry point
- flag.quarrying = true
- flag.atHome = false
- pcall(writeVariables)
- goto(quarry.x, quarry.y, quarry.z, quarry.d, true)
- end
- -- **************************** --
- -- MAIN PROGRAM --
- -- **************************** --
- ----------------------
- -- Phase 1: Startup --
- ----------------------
- broadcast("Starting Program")
- if fuelCheck() == false then --preflight checks, needs at least 2 fuel for the gps function called in startup()
- errorStop("turtle has no fuel in slot 16")
- end
- startup()
- pcall(writeVariables)
- --debugger(true)
- ---------------------------
- -- Phase 2: Build Quarry --
- ---------------------------
- if flag.quarryComplete == false then
- local yMoveSkip = true --yMoveSkip used to prevent a move in the y direction when starting a new z layer
- oreCheck()
- while flag.quarryComplete == false do -- loop for z
- if flag.yReverse == false then
- while current.y ~= boundary.yMax do --loop for yReverse == false
- if yMoveSkip == false then
- turn(1)
- moveForward()
- quarry.x = current.x; quarry.y = current.y; quarry.z = current.z; quarry.d = current.d
- writeScheduler()
- oreCheck()
- if fuelCheck() == false or invCheck() == false then unloadCycle() end
- end
- if flag.xReverse == false then
- turn(2)
- while current.x ~= boundary.xMax do --loop for xReverse == false (in yReverse == false)
- moveForward()
- quarry.x = current.x; quarry.y = current.y; quarry.z = current.z; quarry.d = current.d
- writeScheduler()
- oreCheck()
- if fuelCheck() == false or invCheck() == false then unloadCycle() end
- end
- elseif flag.xReverse == true then
- turn(4)
- while current.x ~= boundary.xMin do --loop for xReverse == true (in yReverse == false)
- moveForward()
- quarry.x = current.x; quarry.y = current.y; quarry.z = current.z; quarry.d = current.d
- writeScheduler()
- oreCheck()
- if fuelCheck() == false or invCheck() == false then unloadCycle() end
- end
- else
- errorStop("xReverse flag corrupted")
- end
- if flag.xReverse == true then -- flip the xReverse flag
- flag.xReverse = false
- else
- flag.xReverse = true
- end
- if yMoveSkip == true then yMoveSkip = false end -- remove the yMoveSkip
- end
- elseif flag.yReverse == true then
- while current.y ~= boundary.yMin do --loop for yReverse == true
- if yMoveSkip == false then
- turn(3)
- moveForward()
- quarry.x = current.x; quarry.y = current.y; quarry.z = current.z; quarry.d = current.d
- writeScheduler()
- oreCheck()
- if fuelCheck() == false or invCheck() == false then unloadCycle() end
- end
- if flag.xReverse == false then
- turn(2)
- while current.x ~= boundary.xMax do --loop for xReverse == false (in yReverse == true)
- moveForward()
- quarry.x = current.x; quarry.y = current.y; quarry.z = current.z; quarry.d = current.d
- writeScheduler()
- oreCheck()
- if fuelCheck() == false or invCheck() == false then unloadCycle() end
- end
- elseif flag.xReverse == true then
- turn(4)
- while current.x ~=boundary.xMin do --loop for xReverse == true (in yReverse == true)
- moveForward()
- quarry.x = current.x; quarry.y = current.y; quarry.z = current.z; quarry.d = current.d
- writeScheduler()
- oreCheck()
- if fuelCheck() == false or invCheck() == false then unloadCycle() end
- end
- else
- errorStop("xReverse flag corrupted")
- end
- if flag.xReverse == true then -- flip the xReverse flag
- flag.xReverse = false
- else
- flag.xReverse = true
- end
- if yMoveSkip == true then yMoveSkip = false end -- remove the yMoveSkip
- end
- else
- errorStop("yReverse flag corrupted")
- end
- if flag.yReverse == false then -- flip the yReverse flag
- flag.yReverse = true
- else
- flag.yReverse = false
- end
- --If the loop compleates at lvl 5 then the quarry is completed
- if current.z == 5 then flag.quarryComplete = true end
- -- keep moving the turtle until it hits z level 5
- if current.z ~= 5 then
- moveDown(false)
- end
- if current.z ~= 5 then
- moveDown(false)
- end
- if current.z ~= 5 then
- moveDown(false)
- end
- yMoveSkip = true
- quarry.x = current.x; quarry.y = current.y; quarry.z = current.z; quarry.d = current.d
- writeScheduler()
- oreCheck()
- pcall(writeVariables)
- broadcast("Starting Quarrying on level "..current.z)
- end
- end
- -----------------------
- -- Phase 3: shutdown --
- -----------------------
- broadcast("Quarry finished - returning home")
- goto(home.x, home.y, home.z, direction.back, false)
- --final unload of inventory items
- for i = 1,filterSlots do
- turtle.select(i)
- if turtle.drop(turtle.getItemCount(i)-1) == false then
- errorStop("Output chest has overflowed")
- end
- end
- for i = filterSlots + 1, 15 do
- turtle.select(i)
- if turtle.getItemCount(i) ~= 0 then
- if turtle.drop() == false then
- errorStop("Output chest has overflowed")
- end
- end
- end
- --remove the QuarryInstructions file as its no longer needed
- fs.delete("QuarryInstructions")
- turn(direction.forward)
- broadcast("Turtle Finished")
Advertisement
Add Comment
Please, Sign In to add comment