MigasRocha

Auto Quarry 2

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