andrelisboa1

excavateplus

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