Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- 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 = 1
- -- 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 = 120
- -- Set this to false if you don't have a working GPS system
- local UseGPS = false
- --ALL THIS MUST BE FILLED IN IF YOU DON"T USE GPS
- local StartX = 697
- local StartY = 125
- local StartZ = 1396
- -- EX: "SOUTH", "NORTH", "EAST", "WEST"
- local ForwardFacingDirection = "WEST"
- --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 = StartX
- local YY = StartY
- local ZZ = StartZ
- local PrimaryAxis = ""
- local SecondaryAxis = ""
- local function Refuel()
- currentFuelRequired = (10 + (TunnelLength-PositionInBranchMine)*2)/40
- 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
- YY = YY + 1
- 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
- YY = YY - 1
- 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
- local function DepositChest()
- rot.right()
- rot.right()
- if not turtle.detect() then
- turtle.select(1)
- turtle.place()
- end
- print("Depositing the Load...")
- for i = IgnoreSlots + 1,15 do
- turtle.select(i)
- turtle.drop()
- end
- rot.right()
- rot.right()
- 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")
- for i=1,PositionInBranchMine do
- DigMoveDown()
- end
- StartMining()
- end
- else
- print("Returning to Mine")
- for i=1,PositionInBranchMine do
- DigMoveDown()
- end
- 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()
- DigMoveForward()
- DigMoveForward()
- DigMoveForward()
- DigMoveForward()
- print("Finished!")
- 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()
- BlocksTillEnd = TunnelLength - PositionInBranchMine
- while BlocksTillEnd > 0 do
- if Refuel() == false then
- print("***Out of fuel***")
- ReturnBack()
- return
- end
- if InventoryFull() then
- print("***Inventory Full***")
- ReturnBack()
- return
- end
- dig.forward()
- DigMoveDown()
- DigMoveDown()
- DigMoveDown()
- DigMoveDown()
- PositionInBranchMine = PositionInBranchMine + 4
- BlocksTillEnd = TunnelLength - PositionInBranchMine
- end
- Finish()
- end
- 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()
- StartMining()
Advertisement
Add Comment
Please, Sign In to add comment