SoundChaos

Depthscavate

Aug 8th, 2019
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.25 KB | None | 0 0
  1. local tArgs = { ... }
  2. if #tArgs ~= 1 and #tArgs ~= 2 then
  3.     print( "Usage: excavate <diameter>" )
  4.     return
  5. end
  6.  
  7. -- JUSTIN CODE: Variable to drop all cobblestone after every 8 blocks
  8. local l = 1
  9.  
  10. -- get depth argument
  11. local maxDepth = tonumber( tArgs[2] )
  12. if #tArgs == 1 then
  13.     print( "No valid depth given, I will dig to bedrock" )
  14. end
  15.  
  16. -- Mine in a quarry pattern until we hit something we can't dig
  17. local size = tonumber( tArgs[1] )
  18. if size < 1 then
  19.     print( "Excavate diameter must be positive" )
  20.     return
  21. end
  22.    
  23. local depth = 0
  24. local unloaded = 0
  25. local collected = 0
  26.  
  27. local xPos,zPos = 0,0
  28. local xDir,zDir = 0,1
  29.  
  30. local goTo -- Filled in further down
  31. local refuel -- Filled in further down
  32.    
  33.  
  34. local function unload( _bKeepOneFuelStack )
  35.     print( "Unloading items..." )
  36.     for n=3,16 do
  37.         local nCount = turtle.getItemCount(n)
  38.         if nCount > 0 then
  39.             turtle.select(n)           
  40.             local bDrop = true
  41.             if _bKeepOneFuelStack and turtle.refuel(0) then
  42.                 bDrop = false
  43.                 _bKeepOneFuelStack = false
  44.             end        
  45.             if bDrop then
  46.                 turtle.drop()
  47.                 unloaded = unloaded + nCount
  48.             end
  49.         end
  50.     end
  51.     collected = 0
  52.     turtle.select(1)
  53. end
  54.  
  55. local function returnSupplies()
  56.     local x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
  57.     print( "Returning to surface..." )
  58.     goTo( 0,0,0,0,-1 )
  59.    
  60.     local fuelNeeded = 2*(x+y+z) + 1
  61.     if not refuel( fuelNeeded ) then
  62.         unload( true )
  63.         print( "Waiting for fuel" )
  64.         while not refuel( fuelNeeded ) do
  65.             sleep(1)
  66.         end
  67.     else
  68.         unload( true ) 
  69.     end
  70.    
  71.     print( "Resuming mining..." )
  72.     goTo( x,y,z,xd,zd )
  73. end
  74.  
  75.  
  76.  
  77. local function collect()   
  78.     local bFull = true
  79.     local nTotalItems = 0
  80.     for n=1,16 do
  81.         local nCount = turtle.getItemCount(n)
  82.         if nCount == 0 then
  83.             bFull = false
  84.         end
  85.         nTotalItems = nTotalItems + nCount
  86.     end
  87.    
  88.     --JUSTIN CODE SECTION FOR VARIABLE ABOVE
  89.     -----------------------------------------------------------------------------------------
  90.     l = l + 1
  91.     if l == 31 then
  92.         local u = 3 -- this is for the repeat loop to drop any block that is the same as slot 2
  93.         turtle.select(2) -- select slot 2
  94.         repeat
  95.             if turtle.compareTo(u) then -- if slot 2 is equal to U
  96.                 turtle.select(u) -- Select slot U
  97.                 turtle.drop() -- drop slot U
  98.                 turtle.select(2) -- select slot two again  
  99.             end
  100.             u = u + 1 -- change u to the next slot
  101.         until u == 17
  102.         turtle.select(1)
  103.         l = 1
  104.     end
  105.     -----------------------------------------------------------------------------------------
  106.    
  107.     if nTotalItems > collected then
  108.         collected = nTotalItems
  109.         if math.fmod(collected + unloaded, 50) == 0 then
  110.             print( "Mined "..(collected + unloaded).." items." )
  111.         end
  112.     end
  113.    
  114.     if bFull then
  115.         print( "No empty slots left." )
  116.         return false
  117.     end
  118.     return true
  119. end
  120.  
  121. function refuel( ammount )
  122.     local fuelLevel = turtle.getFuelLevel()
  123.     if fuelLevel == "unlimited" then
  124.         return true
  125.     end
  126.    
  127.     local needed = ammount or (xPos + zPos + depth + 2)
  128.     if turtle.getFuelLevel() < needed then
  129.         local fueled = false
  130.         for n=1,16 do
  131.             if turtle.getItemCount(n) > 0 then
  132.                 turtle.select(n)
  133.                 if turtle.refuel(1) then
  134.                     while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
  135.                         turtle.refuel(1)
  136.                     end
  137.                     if turtle.getFuelLevel() >= needed then
  138.                         turtle.select(1)
  139.                         return true
  140.                     end
  141.                 end
  142.             end
  143.         end
  144.         turtle.select(1)
  145.         return false
  146.     end
  147.    
  148.     return true
  149. end
  150.  
  151. local function tryForwards()
  152.     if not refuel() then
  153.         print( "Not enough Fuel" )
  154.         returnSupplies()
  155.     end
  156.    
  157.     while not turtle.forward() do
  158.         if turtle.detect() then
  159.             if turtle.dig() then
  160.                 if not collect() then
  161.                     returnSupplies()
  162.                 end
  163.             else
  164.                 return false
  165.             end
  166.         elseif turtle.attack() or turtle.attackUp() then
  167.             if not collect() then
  168.                 returnSupplies()
  169.             end
  170.         else
  171.             sleep( 0.5 )
  172.         end
  173.     end
  174.    
  175.     xPos = xPos + xDir
  176.     zPos = zPos + zDir
  177.     return true
  178. end
  179.  
  180. local function tryDown()
  181.     if not refuel() then
  182.         print( "Not enough Fuel" )
  183.         returnSupplies()
  184.     end
  185.    
  186.     -- Custom change: Check if depth = maxDepth,
  187.     if #tArgs == 2 then
  188.         if depth + 1 == maxDepth then
  189.             return false
  190.         end
  191.     end
  192.    
  193.     while not turtle.down() do
  194.         if turtle.detectDown() then
  195.             if turtle.digDown() then
  196.                 if not collect() then
  197.                     returnSupplies()
  198.                 end
  199.             else
  200.                 return false
  201.             end
  202.         elseif turtle.attackDown() then
  203.             if not collect() then
  204.                 returnSupplies()
  205.             end
  206.         else
  207.             sleep( 0.5 )
  208.         end
  209.     end
  210.  
  211.     depth = depth + 1
  212.     if math.fmod( depth, 10 ) == 0 then
  213.         print( "Descended "..depth.." metres." )
  214.     end
  215.  
  216.     return true
  217. end
  218.  
  219. local function turnLeft()
  220.     turtle.turnLeft()
  221.     xDir, zDir = -zDir, xDir
  222. end
  223.  
  224. local function turnRight()
  225.     turtle.turnRight()
  226.     xDir, zDir = zDir, -xDir
  227. end
  228.  
  229. function goTo( x, y, z, xd, zd )
  230.     while depth > y do
  231.         if turtle.up() then
  232.             depth = depth - 1
  233.         elseif turtle.digUp() or turtle.attackUp() then
  234.             collect()
  235.         else
  236.             sleep( 0.5 )
  237.         end
  238.     end
  239.  
  240.     if xPos > x then
  241.         while xDir ~= -1 do
  242.             turnLeft()
  243.         end
  244.         while xPos > x do
  245.             if turtle.forward() then
  246.                 xPos = xPos - 1
  247.             elseif turtle.dig() or turtle.attack() then
  248.                 collect()
  249.             else
  250.                 sleep( 0.5 )
  251.             end
  252.         end
  253.     elseif xPos < x then
  254.         while xDir ~= 1 do
  255.             turnLeft()
  256.         end
  257.         while xPos < x do
  258.             if turtle.forward() then
  259.                 xPos = xPos + 1
  260.             elseif turtle.dig() or turtle.attack() then
  261.                 collect()
  262.             else
  263.                 sleep( 0.5 )
  264.             end
  265.         end
  266.     end
  267.    
  268.     if zPos > z then
  269.         while zDir ~= -1 do
  270.             turnLeft()
  271.         end
  272.         while zPos > z do
  273.             if turtle.forward() then
  274.                 zPos = zPos - 1
  275.             elseif turtle.dig() or turtle.attack() then
  276.                 collect()
  277.             else
  278.                 sleep( 0.5 )
  279.             end
  280.         end
  281.     elseif zPos < z then
  282.         while zDir ~= 1 do
  283.             turnLeft()
  284.         end
  285.         while zPos < z do
  286.             if turtle.forward() then
  287.                 zPos = zPos + 1
  288.             elseif turtle.dig() or turtle.attack() then
  289.                 collect()
  290.             else
  291.                 sleep( 0.5 )
  292.             end
  293.         end
  294.     end
  295.    
  296.     while depth < y do
  297.         if turtle.down() then
  298.             depth = depth + 1
  299.         elseif turtle.digDown() or turtle.attackDown() then
  300.             collect()
  301.         else
  302.             sleep( 0.5 )
  303.         end
  304.     end
  305.    
  306.     while zDir ~= zd or xDir ~= xd do
  307.         turnLeft()
  308.     end
  309. end
  310.  
  311. if not refuel() then
  312.     print( "Out of Fuel" )
  313.     return
  314. end
  315.  
  316. print( "Excavating..." )
  317.  
  318. local reseal = false
  319. turtle.select(1)
  320. if turtle.digDown() then
  321.     reseal = true
  322. end
  323.  
  324. local alternate = 0
  325. local done = false
  326. while not done do
  327.     for n=1,size do
  328.         for m=1,size-1 do
  329.             if not tryForwards() then
  330.                 done = true
  331.                 break
  332.             end
  333.         end
  334.         if done then
  335.             break
  336.         end
  337.         if n<size then
  338.             if math.fmod(n + alternate,2) == 0 then
  339.                 turnLeft()
  340.                 if not tryForwards() then
  341.                     done = true
  342.                     break
  343.                 end
  344.                 turnLeft()
  345.             else
  346.                 turnRight()
  347.                 if not tryForwards() then
  348.                     done = true
  349.                     break
  350.                 end
  351.                 turnRight()
  352.             end
  353.         end
  354.     end
  355.     if done then
  356.         break
  357.     end
  358.    
  359.     if size > 1 then
  360.         if math.fmod(size,2) == 0 then
  361.             turnRight()
  362.         else
  363.             if alternate == 0 then
  364.                 turnLeft()
  365.             else
  366.                 turnRight()
  367.             end
  368.             alternate = 1 - alternate
  369.         end
  370.     end
  371.    
  372.     if not tryDown() then
  373.         done = true
  374.         break
  375.     end
  376. end
  377.  
  378. print( "Returning to surface..." )
  379.  
  380. -- Return to where we started
  381. goTo( 0,0,0,0,-1 )
  382. unload( false )
  383. goTo( 0,0,0,0,1 )
  384.  
  385. -- Seal the hole
  386. if reseal then
  387.     turtle.placeDown()
  388. end
  389.  
  390. print( "Mined "..(collected + unloaded).." items total." )
Advertisement
Add Comment
Please, Sign In to add comment