Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Program to branch mine
- shell.run("pastebin get XTZuS6iC refuelAPI")
- shell.run("pastebin get 0R4BMKkp inventoryChecker")
- shell.run("pastebin get bxsJUwq1 blockVein")
- os.loadAPI("refuelAPI")
- os.loadAPI("inventoryChecker")
- os.loadAPI("blockVein")
- local rotations = {
- left = {turtle.turnLeft, turtle.turnRight},
- right = { turtle.turnRight, turtle.turnLeft}
- }
- local lengths = {numOfBranches, branchLength, branchOffset, branchGap }
- local offsetDirection = ""
- local distances = {distanceInBranch, distanceToBranch, branchOffset}
- distances[1] = 0
- distances[2] = 0
- distances[3] = 0
- local orientation = {"forward"}
- local flipflop = "up" -- holds the value of which block the turtle should go to in a branch, the top block or bottom (1x2). Since the turtle digs branches in an S(nake) shape,
- --Y-Level [4] -------------------Branch-----------------------------------------
- -- [3] -- up-------------up---------------up------------up-----------up--
- -- [2] -- down-----------down-------------down----------down---------down
- -- [1] ------------------------------------------------------------------
- local function prompt()
- print("Place a chest below the turtle \n Press any key to continue...")
- read()
- print("Number of branches?")
- lengths[1] = tonumber(read())
- print("Length of branches?")
- lengths[2] = tonumber(read())
- print("Gap between branches? (5 Recommended)")
- lengths[4] = tonumber(read())
- print("Offset your start position by how many blocks? (If new branch mine, input 0)")
- lengths[3] = tonumber(read())
- print("In which direction do you want the turtle to create branches? (left/right)")
- offsetDirection = read()
- end
- local function move(moveType, fuelCheck, distanceTableIndex)
- -- If turtle has made a move, then add that move to the total move sum indices
- if moveType() and distanceTableIndex then
- distances[distanceTableIndex] = distances[distanceTableIndex] + 1
- end
- -- If this move was instantiated with a fuelCheck argument, then do the fuelCheck function given
- if fuelCheck then
- fuelCheck()
- end
- end
- -- Function to dig, created to be sand/gravel/etc resistant
- local function dig(digFunction, detectFunction, inventoryCheck)
- while detectFunction() do
- digFunction()
- sleep(0.4)
- end
- if inventoryCheck then
- inventoryCheck()
- end
- end
- local function calcDistFromOrigin()
- return (distances[1] + distances[2] + distances[3])
- end
- local function snake(fuelCheck, inventoryCheck)
- if (flipflop == "up") then
- dig(turtle.digUp, turtle.detectUp, inventoryCheck)
- move(turtle.up, fuelCheck)
- flipflop = "down"
- else
- dig(turtle.digDown,turtle.detectDown, inventoryCheck)
- move(turtle.down, fuelCheck)
- flipflop = "up"
- end
- end
- -- function to traverse in or out of an existing branch
- local function traverseBranch(direction, distanceTableIndex, fuelCheck, inventoryCheck)
- if (direction == "in") then
- for _ = 0, distances[distanceTableIndex]-1 do
- move(turtle.forward, fuelCheck)
- end
- elseif (direction == "out") then
- for _ = 0, distances[distanceTableIndex]-1 do
- move(turtle.back)
- end
- end
- end
- -- Function to return to the original position
- function returnToBranchSpot()
- if (flipflop == "down") then
- turtle.up()
- end
- if ( distances[2] + distances[3] > 0) then
- move(turtle.turnLeft)
- for _ = 1, (distances[2] + distances[3]) do
- turtle.forward()
- end
- if (orientation[1] == "forward") then
- turtle.turnRight()
- end
- end
- traverseBranch("in", 1, fuelCheck)
- blockVein.backIntoVein()
- end
- -- Function to return to the original position
- function returnToOrigin(isRefueling)
- blockVein.backOutOfVein(isRefueling)
- traverseBranch("out", 1)
- if ( (distances[2] + distances[3]) > 0) then
- move(rotations[offsetDirection][1])
- for _ = 1, (distances[2] + distances[3]) do
- turtle.back()
- end
- move(rotations[offsetDirection][2])
- end
- if (flipflop == "down") then
- turtle.down()
- end
- end
- function checkInventory()
- if inventoryChecker.hasFullInventory() then
- returnToOrigin()
- for i = 16, 1, -1 do
- turtle.select(i)
- turtle.dropDown()
- end
- returnToBranchSpot()
- end
- end
- local function refuelIfNeeded()
- local fuelLevel = turtle.getFuelLevel()
- local distanceFromOrigin = calcDistFromOrigin() + 5 -- +5 for grace moves that the turtle will use up to return to the starting position
- if (not refuelAPI.checkFuel(distanceFromOrigin)) then
- -- Attempt to fuel using inventory items, *no set limit to amount that will be used for fuel*
- print("Attempting to refuel turtle using fuel within it's inventory")
- local fueledAmount = refuelAPI.fuelFromInventory()
- if (not (fueledAmount + fuelLevel > distanceFromOrigin)) then
- -- If fuel level is still too low, return to starting position
- print("Returning to start due to not enough fuel")
- returnToOrigin()
- os.exit()
- end
- end
- end
- local function mineBranch(branchLength, distanceTableIndex, fuelCheck, inventoryCheck)
- for i = 0, branchLength do
- dig(turtle.dig, turtle.detect, inventoryCheck)
- move(turtle.forward, fuelCheck, distanceTableIndex)
- blockVein.scanAdjBlocks(fuelCheck, inventoryCheck)
- snake(fuelCheck, inventoryCheck)
- blockVein.scanAdjBlocks(fuelCheck, inventoryCheck)
- end
- end
- local function offsetBranch(branchOffset, fuelCheck, inventoryCheck, offsetDirection)
- if branchOffset > 0 then
- move(rotations[offsetDirection][1])
- orientation[1] = offsetDirection
- mineBranch(branchOffset, 2, fuelCheck, inventoryCheck)
- move(rotations[offsetDirection][2])
- orientation[1] = "forward"
- end
- end
- local function dumpItems()
- for i = 16, 1, -1 do
- turtle.select(i)
- turtle.dropDown()
- end
- end
- local function init(numOfBranches, branchLength, branchOffset, branchGap, offsetDirection, fuelCheck, inventoryCheck)
- blockVein.setBlocksToMine("forge:ores", 2)
- offsetBranch(branchOffset, fuelCheck, inventoryCheck, offsetDirection)
- for _ = 1, numOfBranches do
- mineBranch(branchLength, 1, fuelCheck, inventoryCheck)
- traverseBranch("out", 1, fuelCheck, inventoryCheck)
- distances[1] = 0
- offsetBranch(branchGap, fuelCheck, inventoryCheck, offsetDirection)
- --gapBranch(branchGap, fuelCheck, inventoryCheck)
- end
- returnToOrigin(false )
- dumpItems()
- end
- prompt()
- init(lengths[1], lengths[2], lengths[3], lengths[4], offsetDirection, refuelIfNeeded, checkInventory)
Advertisement
Add Comment
Please, Sign In to add comment