-- Remember to put a chest directly behind the turtle at it's current location. -- It will deliver it's resources to this chest when full -- Y Level to Branch-mine at local BranchMineLevel = 5 -- How many blocks on the ignore list -- that you put in the first number of slots local IgnoreSlots = 4 -- Set this to true if it is ok for the turtle to mine strait down to the y level for the Branch mine local MineDown = true local TunnelLength = 1000 -- Set this to false if you don't have a working GPS system local UseGPS = true --ALL THIS MUST BE FILLED IN IF YOU DON"T USE GPS local StartX = 0 local StartY = 0 local StartZ = 0 -- EX: "SOUTH", "NORTH", "EAST", "WEST" local ForwardFacingDirection = "" --END NO GPS FILL INFORMATION --DO NOT TOUCH ANYTHING BELOW THIS LINE local mov = {} local rot = {} local dig = {} local PositionInBranchMine = 0 local RotatePosition = "FORWARD" local InsertedIntoLeftWall = false local InsertedIntoRightWall = false local InsertionAmount = 0 local XX, YY, ZZ = 0; local PrimaryAxis = "" local SecondaryAxis = "" local function Refuel() currentFuelRequired = 10 + ((PositionInBranchMine/TunnelLength)*20) if turtle.getFuelLevel() < currentFuelRequired then turtle.select(16) return turtle.refuel(currentFuelRequired) else return true end end local function readLines(sPath) local file = fs.open(sPath, "r") -- open the file if file then -- check if it's open local tLines = {} -- table to store the lines local sLine = file.readLine() -- read a line from the file while sLine do -- while there's a line in the file table.insert(tLines, sLine) -- add it to the table sLine = file.readLine() -- get the next line end file.close() -- close the file return tLines -- return the table with the lines end return nil -- there was an error opening the file, return nil to let the user know end local function writeLines(sPath, tLines) local file = fs.open(sPath, "w") -- open the file if file then -- check if the file is open for _, sLine in ipairs(tLines) do -- for each line in the table file.writeLine(sLine) -- write the line end file.close() -- close the file end end function filereadline(filename, line) local tLines = readLines(filename) -- read the lines from the file (using the previous functions) if not tLines then -- if there was an error return nil -- return nil/error end return tLines[line] -- return the line end function filewriteline(filename, line, text) local tLines = readLines(filename) -- read the lines from the file (using the previous functions) if tLines then -- if there was no error tLines[line] = text -- set the line writeLines(filename, tLines) -- write the lines back to the file (using the previous functions) end end local function incrementCoordinates(incX,incY,incZ) XX = XX + incX YY = YY + incY ZZ = ZZ + incZ filewriteline("BranchMineData", 7, XX) filewriteline("BranchMineData", 8, YY) filewriteline("BranchMineData", 9, ZZ) end --directionInteger 0 = forward, 1 = left, 2 = backward, 3 = right, 4 = up, 5 = down local function updateCoordinates(directionInteger) --Displace the direction by the current Rotation if directionInteger <= 3 then if directionInteger == 0 then if string.match(RotatePosition,"LEFT") then directionInteger = 1 elseif string.match(RotatePosition,"BACKWARD") then directionInteger = 2 elseif string.match(RotatePosition,"RIGHT") then directionInteger = 3 end elseif directionInteger == 2 then if string.match(RotatePosition,"LEFT") then directionInteger = 3 elseif string.match(RotatePosition,"BACKWARD") then directionInteger = 0 elseif string.match(RotatePosition,"RIGHT") then directionInteger = 1 end end end local incrementPrimary = true local incrementBy = 0 local incrementX = 0 local incrementY = 0 local incrementZ = 0 --if we are moving forward if directionInteger == 0 then incrementPrimary = true incrementBy = 1 --if we are moving left elseif directionInteger == 1 then incrementPrimary = false incrementBy = 1 --if we are moving backward elseif directionInteger == 2 then incrementPrimary = true incrementBy = -1 --if we are moving right elseif directionInteger == 3 then incrementPrimary =false incrementBy = -1 elseif directionInteger == 4 then incrementY = 1 elseif directionInteger == 5 then incrementY = -1 end if incrementPrimary then if PrimaryAxis == "X" then incrementX = incrementBy elseif PrimaryAxis == "-X" then incrementX = -1 * incrementBy elseif PrimaryAxis == "Z" then incrementZ = incrementBy elseif PrimaryAxis == "-Z" then incrementZ = -1 * incrementBy end else if SecondaryAxis == "X" then incrementX = incrementBy elseif SecondaryAxis == "-X" then incrementX = -1 * incrementBy elseif SecondaryAxis == "Z" then incrementZ = incrementBy elseif SecondaryAxis == "-Z" then incrementZ = -1 * incrementBy end end incrementCoordinates(incrementX,incrementY,incrementZ) end mov.forward = function(times) local boolean v = false for i=1,times do if turtle.detect() == false then while not turtle.forward() do Refuel() sleep(1) end updateCoordinates(0) v = true else v = false return v end end return v end mov.back = function(times) local boolean v = false for i=1,times do while not turtle.back() do Refuel() sleep(1) end updateCoordinates(2) v = true end return v end mov.up = function(times) local boolean v = false for i=1,times do if turtle.detectUp() == false then while not turtle.up() do Refuel() sleep(1) end updateCoordinates(4) v = true else v = false return v end end return v end mov.down = function(times) local boolean v = false for i=1,times do if turtle.detectDown() == false then while not turtle.down() do Refuel() sleep(1) end updateCoordinates(5) v = true else v = false return v end end return v end mov.place = function() while not turtle.place() do sleep(1) end return true end rot.right = function() while not turtle.turnRight() do sleep(1) end if RotatePosition == "FORWARD" then RotatePosition = "RIGHT" elseif RotatePosition == "RIGHT" then RotatePosition = "BACKWARD" elseif RotatePosition == "BACKWARD" then RotatePosition = "LEFT" elseif RotatePosition == "LEFT" then RotatePosition = "FORWARD" end filewriteline("BranchMineData", 5, RotatePosition) return true end rot.left = function() while not turtle.turnLeft() do sleep(1) end if RotatePosition == "FORWARD" then RotatePosition = "LEFT" elseif RotatePosition == "LEFT" then RotatePosition = "BACKWARD" elseif RotatePosition == "BACKWARD" then RotatePosition = "RIGHT" elseif RotatePosition == "RIGHT" then RotatePosition = "FORWARD" end filewriteline("BranchMineData", 5, RotatePosition) return true end -- RotateTo is a number where 0 = Forward, 1 = Left, 2 = Backward, and 3 = Right local function look(RotateTo) --This rids one odd occurance if RotatePosition == "RIGHT" and RotateTo == 0 then rot.left() return end -- rotateNumber = 0 if RotatePosition == "LEFT" then rotateNumber = 1 elseif RotatePosition == "BACKWARD" then rotateNumber = 2 elseif RotatePosition == "RIGHT" then rotateNumber = 3 end rotateRight = rotateNumber - RotateTo if rotateRight >= 0 then for i = 1,rotateRight do rot.right() end else for i = 1,math.abs(rotateRight) do rot.left() end end end dig.forward = function() if turtle.detect() then while not turtle.dig() do sleep(1) end return true else print("No Block to mine forward") return false end end dig.up = function() if turtle.detectUp() then while not turtle.digUp() do sleep(1) end return true else print("No Block to mine up") return false end end dig.down = function() if turtle.detectDown() then while not turtle.digDown() do sleep(1) end return true else print("No Block to mine down") return false end end local function DigMoveForward() if mov.forward(1) == false then dig.forward() sleep(0.5) DigMoveForward() end end local function DigMoveUp() if mov.up(1) == false then dig.up() sleep(0.5) DigMoveUp() end end local function DigMoveDown() if mov.down(1) == false then dig.down() sleep(0.5) DigMoveDown() end end -- Begin BranchMining Code if UseGPS then if rednet.open("right") == nil then print("You had GPS Enabled but there is not a modem on this turtle") return end end -- Mine to BranchMining level local function MineToMine() local x, y, z = 0 if UseGPS then x, y, z = gps.locate(5) else x = XX y = YY z = ZZ end if MineDown then x = tonumber(x) y = tonumber(y) z = tonumber(z) if y > BranchMineLevel then MineCount = y - BranchMineLevel for i = 1,MineCount do if mov.down(1) then -- Print("Coords: ",x," ",y - i," ",z) else dig.down() mov.down(1) -- Print("Coords: ",x," ",y - i," ",z) end end elseif y < BranchMineLevel then MineCount = BranchMineLevel - y for i = 1,MineCount do if mov.up(1) then -- Print("Coords: ",x," ",y - i," ",z) else dig.up() mov.up(1) -- Print("Coords: ",x," ",y - i," ",z) end end end end for i = 1,tonumber(PositionInBranchMine) do DigMoveForward() end end local function DepositChest() print("Depositing the Load...") for i = IgnoreSlots + 1,15 do turtle.select(i) turtle.drop() end end local function GetCoalChest() look(1) -- look left turtle.select(15) if turtle.suck() == false then rot.right() return false else if turtle.compareTo(16) then turtle.transferTo(16) turtle.drop() rot.right() return true else turtle.drop() rot.right() return false end end end local function ReturnBack() local xC, yC, zC = 0 if UseGPS then xC, yC, zC = gps.locate(5) else xC = XX yC = YY zC = ZZ end look(2) -- look backward Distance = 0 xC = tonumber(xC) yC = tonumber(yC) zC = tonumber(zC) StartX = tonumber(StartX) StartY = tonumber(StartY) StartZ = tonumber(StartZ) if xC < StartX then Distance = StartX - xC elseif xC > StartX then Distance = xC - StartX elseif zC < StartZ then Distance = StartZ - zC elseif zC > StartZ then Distance = zC - StartZ end for i = 1,Distance do if mov.forward(1) == false then dig.forward() mov.forward(1) end end look(0) -- look forward filewriteline("BranchMineData", 5, RotatePosition) if UseGPS then xC, yC, zC = gps.locate(5) else xC = XX yC = YY zC = ZZ end xC = tonumber(xC) yC = tonumber(yC) zC = tonumber(zC) StartX = tonumber(StartX) StartY = tonumber(StartY) StartZ = tonumber(StartZ) if StartY > yC then mov.up(StartY - yC) elseif StartY < yC then mov.down(yC - StartY) end DepositChest() if GetCoalChest() == false then if Refuel() == false then print("***Out of fuel***") print("***NO FUEL IN CHEST, PROGRAM HALT***") return else print("Returning to Mine") MineToMine() StartMining() end else print("Returning to Mine") MineToMine() StartMining() end end local function Finish() local xC, yC, zC = 0 if UseGPS then xC, yC, zC = gps.locate(5) else xC = XX yC = YY zC = ZZ end look(2) -- look Backward RotatePosition = "BACKWARD" filewriteline("BranchMineData", 5, RotatePosition) Distance = 0 if xC < StartX then Distance = StartX - xC elseif xC > StartX then Distance = xC - StartX elseif zC < StartZ then Distance = StartZ - zC elseif zC > StartZ then Distance = zC - StartZ end for i = 1,Distance do if mov.forward(1) == false then dig.forward() mov.forward(1) end end look(0) -- look Forward RotatePosition = "FORWARD" filewriteline("BranchMineData", 5, RotatePosition) if UseGPS then xC, yC, zC = gps.locate(5) else xC = XX yC = YY zC = ZZ end if StartY > yC then mov.up(StartY - yC) elseif StartY < yC then mov.down(yC - StartY) end DepositChest() print("Finished!") end local function checkDown() TimesChecked = 0 for i = 1,tonumber(IgnoreSlots) do turtle.select(i) if turtle.compareDown() == false then TimesChecked = TimesChecked + 1 if TimesChecked == IgnoreSlots then dig.down() end else return end end end local function checkUp() TimesChecked = 0 for i = 1,tonumber(IgnoreSlots) do turtle.select(i) if turtle.compareUp() == false then TimesChecked = TimesChecked + 1 if TimesChecked == IgnoreSlots then dig.up() end else return end end end local function checkFront(leftWall,shouldCheckUp) TimesChecked = 0 for i = 1,tonumber(IgnoreSlots) do turtle.select(i) if turtle.detect() and turtle.compare() == false then TimesChecked = TimesChecked + 1 if TimesChecked == IgnoreSlots then DigMoveForward() InsertionAmount = InsertionAmount + 1 InsertedIntoLeftWall = leftWall if leftWall == false then InsertedIntoRightWall = true else InsertedIntoRightWall = false end filewriteline("BranchMineData", 10, InsertedIntoLeftWall) filewriteline("BranchMineData", 11, InsertedIntoRightWall) filewriteline("BranchMineData", 12, InsertionAmount) filewriteline("BranchMineData", 13, shouldCheckUp) if shouldCheckUp then checkUp() else checkDown() end return checkFront(leftWall, shouldCheckUp) end else if InsertedIntoLeftWall then look(3) --look right for i = 1,tonumber(InsertionAmount) do DigMoveForward() end InsertedIntoRightWall = false InsertedIntoLeftWall = false InsertionAmount = 0 elseif InsertedIntoRightWall then look(1) --look left for i = 1,tonumber(InsertionAmount) do DigMoveForward() end InsertedIntoRightWall = false InsertedIntoLeftWall = false InsertionAmount = 0 else filewriteline("BranchMineData", 10, InsertedIntoLeftWall) filewriteline("BranchMineData", 11, InsertedIntoRightWall) filewriteline("BranchMineData", 12, InsertionAmount) return false end filewriteline("BranchMineData", 10, InsertedIntoLeftWall) filewriteline("BranchMineData", 11, InsertedIntoRightWall) filewriteline("BranchMineData", 12, InsertionAmount) return true end end end local function checkOresLEVEL1() look(1) --look left checkFront(true, false) look(3) --look Right checkFront(false, false) look(0) --look forward checkDown() end local function checkOresLEVEL2() look(1) --look left checkFront(true, true) look(3) --look Right checkFront(false, true) look(0) --look forward checkUp() end local function InventoryFull() SlotsFull = 0 for i=1,16 do if turtle.getItemCount(i) > 0 then SlotsFull = SlotsFull + 1 end end if SlotsFull == 16 then return true; else return false; end end function StartMining() level2 = false BlocksTillEnd = TunnelLength - PositionInBranchMine for i = 1,tonumber(BlocksTillEnd) do if Refuel() == false then print("***Out of fuel***") ReturnBack() return end if InventoryFull() then print("***Inventory Full***") ReturnBack() return end DigMoveForward() if level2 == false then checkOresLEVEL1() DigMoveUp() checkOresLEVEL2() level2 = true PositionInBranchMine = PositionInBranchMine + 1 filewriteline("BranchMineData", 4, PositionInBranchMine) print("Position in BranchMine:",PositionInBranchMine) else checkOresLEVEL2() DigMoveDown() checkOresLEVEL1() level2 = false PositionInBranchMine = PositionInBranchMine + 1 filewriteline("BranchMineData", 4, PositionInBranchMine) print("Position in BranchMine:",PositionInBranchMine) end end Finish() end -- Create or read data file if fs.exists("BranchMineData") then readLines("BranchMineData") StartX = tonumber(filereadline("BranchMineData", 1)) StartY = tonumber(filereadline("BranchMineData", 2)) StartZ = tonumber(filereadline("BranchMineData", 3)) PositionInBranchMine = tonumber(filereadline("BranchMineData", 4)) RotatePosition = filereadline("BranchMineData", 5) print("Read Saved data. Start Position: "..StartX.." "..StartY.." "..StartZ..".") print("With a mine position of: "..PositionInBranchMine.." .") print("RotatePosition: "..RotatePosition..".") if UseGPS == false then ForwardFacingDirection = filereadline("BranchMineData", 6) XX = filereadline("BranchMineData", 7) YY = filereadline("BranchMineData", 8) ZZ = filereadline("BranchMineData", 9) if ForwardFacingDirection == "SOUTH" then PrimaryAxis = "Z" SecondaryAxis = "X" elseif ForwardFacingDirection == "NORTH" then PrimaryAxis = "-Z" SecondaryAxis = "-X" elseif ForwardFacingDirection == "EAST" then PrimaryAxis = "X" SecondaryAxis = "-Z" elseif ForwardFacingDirection == "WEST" then PrimaryAxis = "-X" SecondaryAxis = "Z" end print("ForwardFacingDirection: "..ForwardFacingDirection..",") end InsertedIntoLeftWall = filereadline("BranchMineData", 10) InsertedIntoRightWall = filereadline("BranchMineData", 11) InsertionAmount = filereadline("BranchMineData", 12) local shouldCheckUp = filereadline("BranchMineData", 13) if string.match(InsertedIntoLeftWall, "true") or string.match(InsertedIntoRightWall, "true") then if turtle.detect() == false then DigMoveForward() InsertionAmount = InsertionAmount + 1 if string.match(shouldCheckUp,"true") then shouldCheckUp = true checkUp() else shouldCheckUp = false checkDown() end checkFront(InsertedIntoLeftWall,shouldCheckUp) else if string.match(shouldCheckUp,"true") then shouldCheckUp = true checkUp() else shouldCheckUp = false checkDown() end checkFront(InsertedIntoLeftWall,shouldCheckUp) end end sleep(1) ReturnBack() else local x, y, z = 0 if UseGPS then x, y, z = gps.locate(5) else x = StartX y = StartY z = StartZ end if x == nil then print("Could not obtain a GPS Connection.") print("Please make sure you have a working GPS system.") return else print("Current Coords: ",x," ",y," ",z) StartX = x StartY = y StartZ = z end local tLines = {} tLines[1] = StartX tLines[2] = StartY tLines[3] = StartZ tLines[4] = PositionInBranchMine tLines[5] = RotatePosition if UseGPS == false then if string.match(ForwardFacingDirection, "SOUTH") or string.match(ForwardFacingDirection, "NORTH") or string.match(ForwardFacingDirection, "EAST") or string.match(ForwardFacingDirection, "WEST") then tLines[6] = ForwardFacingDirection XX = x YY = y ZZ = z tLines[7] = XX tLines[8] = YY tLines[9] = ZZ if ForwardFacingDirection == "SOUTH" then PrimaryAxis = "Z" SecondaryAxis = "X" elseif ForwardFacingDirection == "NORTH" then PrimaryAxis = "-Z" SecondaryAxis = "-X" elseif ForwardFacingDirection == "EAST" then PrimaryAxis = "X" SecondaryAxis = "-Z" elseif ForwardFacingDirection == "WEST" then PrimaryAxis = "-X" SecondaryAxis = "Z" end else print("ForwardFacingDirection not correctly formatted.") return end else tLines[6] = 0 tLines[7] = 0 tLines[8] = 0 tLines[9] = 0 end tLines[10] = InsertedIntoLeftWall tLines[11] = InsertedIntoRightWall tLines[12] = InsertionAmount tLines[13] = false writeLines("BranchMineData", tLines) -- Check fuel local FuelCount = turtle.getItemCount(16) if FuelCount == 0 then print("please put fuel in the last slot of the Turtle.") return else if FuelCount < 10 then print("Please put at least 10 of the fuel you are using in the Turtle.") return end end Refuel() MineToMine() StartMining() end