peregrin5

excavateNether v3

Jul 21st, 2019
1,489
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.87 KB | None | 0 0
  1. ----------------------------------------------
  2. -- Nether Excavation Turtle
  3. -- 1) Upload code to your turtle with "pastebin get Bb9DsaFX excavateNether"
  4. -- 2) Dig down to the edge of a lava lake (level 32).
  5. -- 3) Place chest behind your turtle. Place one bucket of lava into the first slot of its inventory.
  6. -- 4) Run code using "excavateNether <bore size>" replacing <bore size> with the width of the hole you wish to dig
  7. -- 5) It will draw as much lava as is necessary to mine and bore upwards and deposit items in the chest as it gets full.
  8.  
  9. -- Note: If you run this program in the overworld you WILL lose your turtle.
  10. ----------------------------------------------
  11. -- Credit to leftler for his lava refueling code: https://pastebin.com/nFSUKiYE
  12. ----------------------------------------------
  13. -- V1 - basic dig up Function
  14. -- V2 - added lava refueling
  15. -- V3 - added findCeiling function to start digging faster
  16. ----------------------------------------------
  17.  
  18. local tArgs = { ... }
  19. if #tArgs ~= 1 then
  20.     print( "Usage: excavateNether <diameter>" )
  21.     return
  22. end
  23.  
  24. local size = tonumber( tArgs[1] )
  25.  
  26. -- Mine in a quarry pattern until we hit something we can't dig
  27. if size < 1 then
  28.     print( "Excavate diameter must be positive" )
  29.     return
  30. end
  31.  
  32. local height = 0
  33. local unloaded = 0
  34. local collected = 0
  35.  
  36. local xPos,zPos = 0,0
  37. local xDir,zDir = 0,1
  38.  
  39. local goTo -- Filled in further down
  40. local refuel -- Filled in further down
  41.  
  42. local function unload( _bKeepOneFuelStack )
  43.     print( "Unloading items..." )
  44.     for n=1,16 do
  45.         local nCount = turtle.getItemCount(n)
  46.         if nCount > 0 then
  47.             turtle.select(n)
  48.             local bDrop = true
  49.             if _bKeepOneFuelStack and turtle.refuel(0) then
  50.                 bDrop = false
  51.                 _bKeepOneFuelStack = false
  52.             end
  53.             if bDrop then
  54.                 turtle.drop()
  55.                 unloaded = unloaded + nCount
  56.             end
  57.         end
  58.     end
  59.     collected = 0
  60.     turtle.select(1)
  61. end
  62.  
  63. local function returnSupplies()
  64.     local x,y,z,xd,zd = xPos,height,zPos,xDir,zDir
  65.     print( "Returning to surface..." )
  66.     goTo( 0,0,0,0,-1 )
  67.  
  68.     local fuelNeeded = 2*(x+y+z) + 1
  69.     if not refuel( fuelNeeded ) then
  70.         unload( true )
  71.         print( "Waiting for fuel" )
  72.         while not refuel( fuelNeeded ) do
  73.             os.pullEvent( "turtle_inventory" )
  74.         end
  75.     else
  76.         unload( true )
  77.     end
  78.  
  79.     print( "Resuming mining..." )
  80.     goTo( x,y,z,xd,zd )
  81. end
  82.  
  83. local function collect()
  84.     local bFull = true
  85.     local nTotalItems = 0
  86.     for n=1,16 do
  87.         local nCount = turtle.getItemCount(n)
  88.         if nCount == 0 then
  89.             bFull = false
  90.         end
  91.         nTotalItems = nTotalItems + nCount
  92.     end
  93.  
  94.     if nTotalItems > collected then
  95.         collected = nTotalItems
  96.         if math.fmod(collected + unloaded, 50) == 0 then
  97.             print( "Mined "..(collected + unloaded).." items." )
  98.         end
  99.     end
  100.  
  101.     if bFull then
  102.         print( "No empty slots left." )
  103.         return false
  104.     end
  105.     return true
  106. end
  107.  
  108. function refuel( ammount )
  109.     local fuelLevel = turtle.getFuelLevel()
  110.     if fuelLevel == "unlimited" then
  111.         return true
  112.     end
  113.  
  114.     local needed = ammount or (xPos + zPos + height + 2)
  115.     if turtle.getFuelLevel() < needed then
  116.         local fueled = false
  117.         for n=1,16 do
  118.             if turtle.getItemCount(n) > 0 then
  119.                 turtle.select(n)
  120.                 if turtle.refuel(1) then
  121.                     while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
  122.                         turtle.refuel(1)
  123.                     end
  124.                     if turtle.getFuelLevel() >= needed then
  125.                         turtle.select(1)
  126.                         return true
  127.                     end
  128.                 end
  129.             end
  130.         end
  131.         turtle.select(1)
  132.         return false
  133.     end
  134.  
  135.     return true
  136. end
  137.  
  138. local function tryForwards()
  139.     if not refuel() then
  140.         print( "Not enough Fuel" )
  141.         returnSupplies()
  142.     end
  143.  
  144.     while not turtle.forward() do
  145.         if turtle.detect() then
  146.             if turtle.dig() then
  147.                 if not collect() then
  148.                     returnSupplies()
  149.                 end
  150.             else
  151.                 return false
  152.             end
  153.         elseif turtle.attack() then
  154.             if not collect() then
  155.                 returnSupplies()
  156.             end
  157.         else
  158.             sleep( 0.5 )
  159.         end
  160.     end
  161.  
  162.     xPos = xPos + xDir
  163.     zPos = zPos + zDir
  164.     return true
  165. end
  166.  
  167. local function tryUp()
  168.     if not refuel() then
  169.         print( "Not enough Fuel" )
  170.         returnSupplies()
  171.     end
  172.  
  173.     while not turtle.up() do
  174.         if turtle.detectUp() then
  175.             if turtle.digUp() then
  176.                 if not collect() then
  177.                     returnSupplies()
  178.                 end
  179.             else
  180.                 return false
  181.             end
  182.         elseif turtle.attackUp() then
  183.             if not collect() then
  184.                 returnSupplies()
  185.             end
  186.         else
  187.             sleep( 0.5 )
  188.         end
  189.     end
  190.  
  191.     height = height + 1
  192.     if math.fmod( height, 10 ) == 0 then
  193.         print( "Ascended "..height.." metres." )
  194.     end
  195.  
  196.     return true
  197. end
  198.  
  199. local function turnLeft()
  200.     turtle.turnLeft()
  201.     xDir, zDir = -zDir, xDir
  202. end
  203.  
  204. local function turnRight()
  205.     turtle.turnRight()
  206.     xDir, zDir = zDir, -xDir
  207. end
  208.  
  209. function goTo( x, y, z, xd, zd )
  210.     while height > y do
  211.         if turtle.down() then
  212.             height = height - 1
  213.         elseif turtle.digDown() or turtle.attackDown() then
  214.             collect()
  215.         else
  216.             sleep( 0.5 )
  217.         end
  218.     end
  219.  
  220.     if xPos > x then
  221.         while xDir ~= -1 do
  222.             turnLeft()
  223.         end
  224.         while xPos > x do
  225.             if turtle.forward() then
  226.                 xPos = xPos - 1
  227.             elseif turtle.dig() or turtle.attack() then
  228.                 collect()
  229.             else
  230.                 sleep( 0.5 )
  231.             end
  232.         end
  233.     elseif xPos < x then
  234.         while xDir ~= 1 do
  235.             turnLeft()
  236.         end
  237.         while xPos < x do
  238.             if turtle.forward() then
  239.                 xPos = xPos + 1
  240.             elseif turtle.dig() or turtle.attack() then
  241.                 collect()
  242.             else
  243.                 sleep( 0.5 )
  244.             end
  245.         end
  246.     end
  247.  
  248.     if zPos > z then
  249.         while zDir ~= -1 do
  250.             turnLeft()
  251.         end
  252.         while zPos > z do
  253.             if turtle.forward() then
  254.                 zPos = zPos - 1
  255.             elseif turtle.dig() or turtle.attack() then
  256.                 collect()
  257.             else
  258.                 sleep( 0.5 )
  259.             end
  260.         end
  261.     elseif zPos < z then
  262.         while zDir ~= 1 do
  263.             turnLeft()
  264.         end
  265.         while zPos < z do
  266.             if turtle.forward() then
  267.                 zPos = zPos + 1
  268.             elseif turtle.dig() or turtle.attack() then
  269.                 collect()
  270.             else
  271.                 sleep( 0.5 )
  272.             end
  273.         end
  274.     end
  275.  
  276.     while height < y do
  277.         if turtle.up() then
  278.             height = height + 1
  279.         elseif turtle.digUp() or turtle.attackUp() then
  280.             collect()
  281.         else
  282.             sleep( 0.5 )
  283.         end
  284.     end
  285.  
  286.     while zDir ~= zd or xDir ~= xd do
  287.         turnLeft()
  288.     end
  289. end
  290.  
  291.  
  292. -- Function to find nether ceiling first.
  293. local function findCeiling()
  294.     while turtle.detectUp() == false do
  295.       if turtle.up() then
  296.             height = height + 1
  297.         end
  298.     end
  299. end
  300.  
  301. ----
  302. -- Lava Refuel Program (leftler's code)
  303.  
  304. local fuelReq = 93*size*size+93
  305. local lavaBuckets = (fuelReq*0.001)*0.5
  306.  
  307. --Number of blocks to travel to get the lava
  308. local maxDistance = math.ceil(lavaBuckets)+math.ceil(0.25*size)
  309.  
  310. --local counter for how far the turtle has travle
  311. local traveled = 0
  312.  
  313. --A helper function to try and move up, it will attempt to dig if it is blocked.
  314. local function moveUp()
  315.   if turtle.up() == false then
  316.     --Failed to move, see if we can dig our way down
  317.     while turtle.digUp() do
  318.           --Keep digging till we can't dig any more, in case gravel is falling.
  319.         end
  320.         if turtle.up() == false then
  321.           print("I am either blocked or out of fuel.")
  322.           return false
  323.         end
  324.   end
  325.   return true
  326. end
  327.  
  328. --A helper function to try and move forward, it will attempt to dig if it is blocked.
  329. local function moveForward()
  330.   if turtle.forward() == false then
  331.     --Failed to move, see if we can dig our way forward
  332.     while turtle.dig() do
  333.           --Keep digging till we can't dig any more, in case gravel is falling.
  334.         end
  335.         if turtle.forward() == false then
  336.           print("I am either blocked or out of fuel.")
  337.           return false
  338.         end
  339.   end
  340.   return true
  341. end
  342.  
  343. ------
  344. -- Program Start
  345. ------
  346.  
  347. if turtle.refuel() == false then
  348.   print("The turtle must have at least one lava bucket to start with.")
  349.   return
  350. elseif turtle.getFuelLevel() == "unlimited" then
  351.   print("This turtle does not use fuel to move")
  352.   return
  353. end
  354.  
  355. for i = 1, maxDistance do
  356.   if moveForward() == false then
  357.     print("Could not move forward the full requested distance, starting U-Turn early.")
  358.         break
  359.   end
  360.   traveled = traveled + 1
  361.  
  362.   --grab the fule
  363.   turtle.placeDown()
  364.   if turtle.refuel() == false then
  365.     --Whatever we picked up is invalid for fuel, put it back down
  366.     turtle.placeDown()
  367.   end
  368. end
  369.  
  370. --Turn around move a block over and get more lava
  371. turtle.turnRight()
  372. moveForward()
  373. turtle.turnRight()
  374.  
  375. --Travel back the same number of blocks we came
  376. for i = 1, traveled do
  377.   turtle.placeDown()
  378.   turtle.refuel()
  379.  
  380.   if moveForward() == false then
  381.     --We are stuck, attempt to go up a level to get home
  382.         if drop > 0 then
  383.           repeat
  384.             moveUp()
  385.             drop = drop - 1
  386.                 lastMove = moveForward()
  387.           until lastMove == true or drop == 0
  388.  
  389.           if lastMove == false then
  390.             print("I am stuck!")
  391.           end
  392.         end
  393.   end
  394. end
  395.  
  396. turtle.turnRight()
  397. moveForward()
  398. turtle.turnRight()
  399.  
  400. print( "Finding ceiling... ")
  401.  
  402. findCeiling()
  403.  
  404. print( "Excavating..." )
  405.  
  406. local reseal = false
  407. turtle.select(1)
  408. if turtle.digUp() then
  409.     reseal = true
  410. end
  411.  
  412. local alternate = 0
  413. local done = false
  414. while not done do
  415.     for n=1,size do
  416.         for m=1,size-1 do
  417.             if not tryForwards() then
  418.                 done = true
  419.                 break
  420.             end
  421.         end
  422.         if done then
  423.             break
  424.         end
  425.         if n<size then
  426.             if math.fmod(n + alternate,2) == 0 then
  427.                 turnLeft()
  428.                 if not tryForwards() then
  429.                     done = true
  430.                     break
  431.                 end
  432.                 turnLeft()
  433.             else
  434.                 turnRight()
  435.                 if not tryForwards() then
  436.                     done = true
  437.                     break
  438.                 end
  439.                 turnRight()
  440.             end
  441.         end
  442.     end
  443.     if done then
  444.         break
  445.     end
  446.  
  447.     if size > 1 then
  448.         if math.fmod(size,2) == 0 then
  449.             turnRight()
  450.         else
  451.             if alternate == 0 then
  452.                 turnLeft()
  453.             else
  454.                 turnRight()
  455.             end
  456.             alternate = 1 - alternate
  457.         end
  458.     end
  459.  
  460.     if not tryUp() then
  461.         done = true
  462.         break
  463.     end
  464. end
  465.  
  466. print( "Returning to surface..." )
  467.  
  468. -- Return to where we started
  469. goTo( 0,0,0,0,-1 )
  470. unload( false )
  471. goTo( 0,0,0,0,1 )
  472.  
  473. -- Seal the hole
  474. if reseal then
  475.     turtle.placeUp()
  476. end
  477.  
  478. print( "Mined "..(collected + unloaded).." items total." )
Advertisement
Add Comment
Please, Sign In to add comment