MigasRocha

Quarry Nova 2023

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