RJ-Bradach

branchminer

Jun 29th, 2021 (edited)
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.34 KB | Gaming | 0 0
  1. -- Program to branch mine
  2.  
  3. shell.run("pastebin get XTZuS6iC refuelAPI")
  4. shell.run("pastebin get 0R4BMKkp inventoryChecker")
  5. shell.run("pastebin get bxsJUwq1 blockVein")
  6.  
  7. os.loadAPI("refuelAPI")
  8. os.loadAPI("inventoryChecker")
  9. os.loadAPI("blockVein")
  10.  
  11.  
  12. local rotations = {
  13.     left = {turtle.turnLeft, turtle.turnRight},
  14.     right = { turtle.turnRight, turtle.turnLeft}
  15. }
  16.  
  17.  
  18.  
  19. local lengths = {numOfBranches, branchLength, branchOffset, branchGap }
  20. local offsetDirection = ""
  21. local distances = {distanceInBranch, distanceToBranch, branchOffset}
  22.  
  23. distances[1] = 0
  24. distances[2] = 0
  25. distances[3] = 0
  26.  
  27. local orientation = {"forward"}
  28. 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,
  29. --Y-Level   [4] -------------------Branch-----------------------------------------
  30. --          [3] -- up-------------up---------------up------------up-----------up--
  31. --          [2] -- down-----------down-------------down----------down---------down
  32. --          [1] ------------------------------------------------------------------
  33.  
  34. local function prompt()
  35.     print("Place a chest below the turtle \n Press any key to continue...")
  36.     read()
  37.     print("Number of branches?")
  38.     lengths[1] = tonumber(read())
  39.     print("Length of branches?")
  40.     lengths[2] = tonumber(read())
  41.     print("Gap between branches? (5 Recommended)")
  42.     lengths[4] = tonumber(read())
  43.     print("Offset your start position by how many blocks? (If new branch mine, input 0)")
  44.     lengths[3] = tonumber(read())
  45.     print("In which direction do you want the turtle to create branches? (left/right)")
  46.     offsetDirection = read()
  47. end
  48.  
  49. local function move(moveType, fuelCheck, distanceTableIndex)
  50.     -- If turtle has made a move, then add that move to the total move sum indices
  51.     if moveType() and distanceTableIndex then
  52.         distances[distanceTableIndex] = distances[distanceTableIndex] + 1
  53.     end
  54.     -- If this move was instantiated with a fuelCheck argument, then do the fuelCheck function given
  55.     if fuelCheck then
  56.         fuelCheck()
  57.     end
  58. end
  59.  
  60. -- Function to dig, created to be sand/gravel/etc resistant
  61. local function dig(digFunction, detectFunction, inventoryCheck)
  62.  
  63.     while detectFunction() do
  64.         digFunction()
  65.         sleep(0.4)
  66.     end
  67.     if inventoryCheck then
  68.         inventoryCheck()
  69.     end
  70. end
  71.  
  72. local function calcDistFromOrigin()
  73.     return (distances[1] + distances[2] + distances[3])
  74. end
  75.  
  76. local function snake(fuelCheck, inventoryCheck)
  77.     if (flipflop == "up") then
  78.         dig(turtle.digUp, turtle.detectUp, inventoryCheck)
  79.         move(turtle.up, fuelCheck)
  80.         flipflop = "down"
  81.     else
  82.         dig(turtle.digDown,turtle.detectDown, inventoryCheck)
  83.         move(turtle.down, fuelCheck)
  84.         flipflop = "up"
  85.     end
  86. end
  87.  
  88.  
  89. -- function to  traverse in or out of an existing branch
  90. local function traverseBranch(direction, distanceTableIndex, fuelCheck, inventoryCheck)
  91.     if (direction == "in") then
  92.         for _ = 0, distances[distanceTableIndex]-1 do
  93.             move(turtle.forward, fuelCheck)
  94.         end
  95.     elseif (direction == "out") then
  96.         for _ = 0, distances[distanceTableIndex]-1 do
  97.             move(turtle.back)
  98.         end
  99.     end
  100. end
  101.  
  102.  
  103. -- Function to return to the original position
  104. function returnToBranchSpot()
  105.     if (flipflop == "down") then
  106.         turtle.up()
  107.     end
  108.     if ( distances[2] + distances[3] > 0) then
  109.             move(turtle.turnLeft)
  110.             for _ = 1, (distances[2] + distances[3]) do
  111.                 turtle.forward()
  112.             end
  113.             if (orientation[1] == "forward") then
  114.                 turtle.turnRight()
  115.             end
  116.     end
  117.     traverseBranch("in", 1, fuelCheck)
  118.     blockVein.backIntoVein()
  119. end
  120.  
  121. -- Function to return to the original position
  122.  function returnToOrigin(isRefueling)
  123.     blockVein.backOutOfVein(isRefueling)
  124.     traverseBranch("out", 1)
  125.     if ( (distances[2] + distances[3]) > 0) then
  126.  
  127.         move(rotations[offsetDirection][1])
  128.         for _ = 1, (distances[2] + distances[3]) do
  129.             turtle.back()
  130.         end
  131.         move(rotations[offsetDirection][2])
  132.     end
  133.     if (flipflop == "down") then
  134.         turtle.down()
  135.     end
  136. end
  137.  
  138. function checkInventory()
  139.     if inventoryChecker.hasFullInventory() then
  140.         returnToOrigin()
  141.         for i = 16, 1, -1 do
  142.             turtle.select(i)
  143.             turtle.dropDown()
  144.         end
  145.         returnToBranchSpot()   
  146.     end
  147. end
  148.  
  149. local function refuelIfNeeded()
  150.     local fuelLevel = turtle.getFuelLevel()
  151.     local distanceFromOrigin = calcDistFromOrigin() + 5 -- +5 for grace moves that the turtle will use up to return to the starting position
  152.     if (not refuelAPI.checkFuel(distanceFromOrigin)) then
  153.         -- Attempt to fuel using inventory items, *no set limit to amount that will be used for fuel*
  154.         print("Attempting to refuel turtle using fuel within it's inventory")
  155.         local fueledAmount = refuelAPI.fuelFromInventory()
  156.         if (not (fueledAmount + fuelLevel > distanceFromOrigin)) then
  157.             -- If fuel level is still too low, return to starting position
  158.             print("Returning to start due to not enough fuel")
  159.             returnToOrigin()
  160.             os.exit()
  161.         end
  162.     end
  163.        
  164. end
  165.  
  166. local function mineBranch(branchLength, distanceTableIndex, fuelCheck, inventoryCheck)
  167.     for i = 0, branchLength do
  168.         dig(turtle.dig, turtle.detect, inventoryCheck)
  169.         move(turtle.forward, fuelCheck, distanceTableIndex)
  170.         blockVein.scanAdjBlocks(fuelCheck, inventoryCheck)
  171.         snake(fuelCheck, inventoryCheck)
  172.         blockVein.scanAdjBlocks(fuelCheck, inventoryCheck)
  173.     end
  174. end
  175.  
  176. local function offsetBranch(branchOffset, fuelCheck, inventoryCheck, offsetDirection)
  177.     if branchOffset > 0 then
  178.         move(rotations[offsetDirection][1])
  179.         orientation[1] = offsetDirection
  180.         mineBranch(branchOffset, 2, fuelCheck, inventoryCheck)
  181.         move(rotations[offsetDirection][2])
  182.         orientation[1] = "forward"
  183.     end
  184. end
  185.  
  186. local function dumpItems()
  187.     for i = 16, 1, -1 do
  188.         turtle.select(i)
  189.         turtle.dropDown()
  190.     end
  191. end
  192.  
  193.  
  194. local function init(numOfBranches, branchLength, branchOffset, branchGap, offsetDirection, fuelCheck, inventoryCheck)
  195.     blockVein.setBlocksToMine("forge:ores", 2)
  196.     offsetBranch(branchOffset, fuelCheck, inventoryCheck, offsetDirection)
  197.     for _ = 1, numOfBranches do
  198.         mineBranch(branchLength, 1, fuelCheck, inventoryCheck)
  199.         traverseBranch("out", 1, fuelCheck, inventoryCheck)
  200.         distances[1] = 0
  201.         offsetBranch(branchGap, fuelCheck, inventoryCheck, offsetDirection)
  202.         --gapBranch(branchGap, fuelCheck, inventoryCheck)
  203.    
  204.     end
  205.     returnToOrigin(false )
  206.     dumpItems()
  207. end
  208.  
  209. prompt()
  210. init(lengths[1], lengths[2], lengths[3], lengths[4], offsetDirection, refuelIfNeeded, checkInventory)
Tags: minecraft
Advertisement
Add Comment
Please, Sign In to add comment