Lxr3517

excavate

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