Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Branch Mining Turtle (with Recursion!)
- -- Mines and follows ores (and returns!)
- -- Variables
- turtle.select(1)
- term.clear()
- term.setCursorPos(1,1)
- print(os.getComputerLabel() .. " at your service!")
- print()
- print("Please place blocks as follows:")
- print("Slot 1 - Enderchest")
- print("Slot 2 - Stone (Smooth)")
- print("Slot 3 - Dirt")
- print("Slot 4 - Gravel")
- print("Slot 5 - Fuel (coal or lava)")
- print()
- print("Press [ENTER] when ready.")
- read(input)
- -- Starting Out
- for s = 1,5 do
- if turtle.getItemCount(s) < 1 then
- print("Fill your slots!")
- quit()
- end
- end
- continue = false
- continueStepCount = 0
- continueYCount = 0
- facing = 0
- currentY = 0
- currentX = 0
- currentZ = 0
- maxChase = 100
- currentChase = 0
- -- Functions
- function trackUp()
- if turtle.up() then
- currentZ = currentZ + 1
- return true;
- else
- return false;
- end
- end
- function trackDown()
- if turtle.down() then
- currentZ = currentZ - 1
- return true;
- else
- return false;
- end
- end
- function trackForward()
- if turtle.forward() then
- if facing == 0 then
- currentY = currentY + 1
- elseif facing == 1 then
- currentX = currentX + 1
- elseif facing == 2 then
- currentY = currentY - 1
- elseif facing == 3 then
- currentX = currentX - 1
- end
- return true;
- else
- return false;
- end
- end
- function trackBackward()
- if turtle.back() then
- if facing == 0 then
- currentY = currentY - 1
- elseif facing == 1 then
- currentX = currentX - 1
- elseif facing == 2 then
- currentY = currentY + 1
- elseif facing == 3 then
- currentX = currentX + 1
- end
- return true;
- else
- return false;
- end
- end
- function trackRight()
- turtle.turnRight()
- facing = facing + 1;
- if facing == 4 then
- facing = 0
- end
- end
- function trackLeft()
- turtle.turnLeft()
- facing = facing - 1;
- if facing == -1 then
- facing = 3
- end
- end
- function tfuel()
- if turtle.getItemCount(16) > 0 then emptyOres() end
- if turtle.getFuelLevel() < 16 then
- turtle.select(5)
- turtle.refuel(1)
- end
- end
- local function turnAround()
- trackRight()
- trackRight()
- end
- local function attemptForward()
- while trackForward() == false do
- turtle.dig()
- os.sleep(.5)
- end
- end
- function emptyOres()
- while turtle.detect() == true do
- turtle.dig()
- os.sleep(.5)
- end
- turtle.select(1)
- turtle.place()
- local i
- for i = 6, 16 do
- turtle.select(i)
- turtle.drop()
- end
- turtle.select(1)
- turtle.dig()
- os.sleep(.5)
- end
- local function checkUp()
- if turtle.detectUp() == false then
- return false;
- end
- local i
- for i = 2, 5 do
- turtle.select(i)
- if turtle.compareUp() == true then
- return false;
- end
- end
- return true;
- end
- local function checkDown()
- if turtle.detectDown() == false then
- return false;
- end
- local i
- for i = 2, 5 do
- turtle.select(i)
- if turtle.compareDown() == true then
- return false;
- end
- end
- return true;
- end
- local function checkForward()
- if turtle.detect() == false then
- return false;
- end
- local i
- for i = 2, 5 do
- turtle.select(i)
- if turtle.compare() == true then
- return false;
- end
- end
- return true;
- end
- -- Recursive ore detection and "chase".
- -- 0 is none, 1 is below, 2 is above, 3 is behind
- local function detectAndChaseOre(arrivalFrom)
- local retries = 10
- tfuel()
- if arrivalFrom == 0 then
- currentChase = 0
- end
- if checkUp() == true then
- retries = 10
- while currentChase < maxChase and trackUp() == false and retries > 0 do
- turtle.digUp()
- os.sleep(.5)
- retries = retries - 1
- end
- if retries ~= 0 and currentChase < maxChase then
- currentChase = currentChase + 1
- detectAndChaseOre(1)
- end
- end
- if checkDown() == true then
- retries = 10
- while currentChase < maxChase and trackDown() == false and retries > 0 do
- turtle.digDown()
- os.sleep(.5)
- retries = retries - 1
- end
- if retries ~= 0 and currentChase < maxChase then
- currentChase = currentChase + 1
- detectAndChaseOre(2)
- end
- end
- -- check all 4 directions
- local rotation
- for rotation = 0, 3 do
- if checkForward() == true then
- retries = 10
- while currentChase < maxChase and trackForward() == false and retries > 0 do
- turtle.dig()
- os.sleep(.5)
- retries = retries - 1
- end
- if retries ~= 0 and currentChase < maxChase then
- currentChase = currentChase + 1
- detectAndChaseOre(3)
- end
- end
- trackRight()
- end
- if arrivalFrom == 1 then
- trackDown()
- elseif arrivalFrom == 2 then
- trackUp()
- elseif arrivalFrom == 3 then
- trackBackward()
- end
- end
- local function digBranch()
- local branchLength
- for branchLength = 0, 20 do
- while attemptForward() == false do
- turtle.dig()
- end
- writeStatusDisplay()
- detectAndChaseOre(0)
- end
- turnAround()
- for branchLength = 0, 20 do
- while attemptForward() == false do
- turtle.dig()
- end
- tfuel()
- end
- end
- function writeContinueFile(theStepCount, theYCount)
- h = fs.open("r86brancont", "w")
- h.writeLine(currentY)
- h.writeLine(currentZ)
- h.writeLine(theStepCount)
- h.writeLine(theYCount)
- h.close()
- end
- function writeStatusDisplay()
- term.clear()
- term.setCursorPos(1,1)
- print("Current Fuel: " .. turtle.getFuelLevel())
- print("Current Length: " .. (currentY + 1) .. "/52")
- print("Current Height: " .. (math.abs(currentZ) + 1) .. "/9")
- end
- function digMainChamber()
- local stepCount = 0
- local YCount = 0
- if math.abs(currentZ) % 2 == 1 then
- stepCount = -2
- end
- if continue == true then
- stepCount = continueStepCount
- YCount = continueYCount
- continue = false
- end
- local chamberLength
- for chamberLength = YCount, 51 do
- if stepCount % 4 == 0 then
- writeContinueFile(stepCount, chamberLength)
- trackLeft()
- digBranch()
- digBranch()
- trackRight()
- end
- while attemptForward() == false do
- turtle.dig()
- end
- stepCount = stepCount + 1
- writeStatusDisplay()
- detectAndChaseOre(0)
- end
- end
- if fs.exists("r86brancont") then
- continue = nil
- while continue ~= "y" and continue ~= "Y" and continue ~= "n" and continue ~= "N" do
- print("Detected previous mining operation")
- print("Continue previous operation? (Y/N)")
- continue = read(input)
- end
- if continue == "Y" or continue == "y" then
- h = fs.open("r86brancont", "r")
- contY = tonumber(h.readLine())
- contZ = tonumber(h.readLine())
- continueStepCount = tonumber(h.readLine())
- continueYCount = tonumber(h.readLine())
- h.close()
- if contY ~= nil and contZ ~= nil and continueStepCount ~= nil and continueYCount ~= nil then
- while currentY ~= contY do
- while trackForward() == false do
- turtle.dig()
- os.sleep(.5)
- end
- end
- while currentZ ~= contZ do
- while trackDown() == false do
- turtle.digDown()
- os.sleep(.5)
- end
- end
- if contZ % 2 == 1 then
- turnAround()
- end
- continue = true
- else
- print("Unable to continue, file malformation detected")
- fs.delete("r86brancont")
- end
- elseif continue == "N" or continue == "n" then
- print("Starting new mining operation")
- fs.delete("r86brancont")
- end
- end
- local totalHeight
- for totalHeight = math.abs(currentZ),8 do
- digMainChamber()
- turnAround()
- turtle.digDown()
- trackDown()
- end
- for totalHeight = 0,8 do
- trackUp()
- end
- turnAround()
- fs.delete("r86brancont")
- emptyOres()
Advertisement
Add Comment
Please, Sign In to add comment