LayZee

ComputerCraft Bo's Excavate

Jun 26th, 2013
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.31 KB | None | 0 0
  1.  
  2. local tArgs = { ... }
  3. if #tArgs ~= 1 then
  4.     print( "Usage: excavate <diameter>" )
  5.     return
  6. end
  7.  
  8. -- Mine in a quarry pattern until we hit something we can't dig
  9. local size = tonumber( tArgs[1] )
  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.             sleep(1)
  57.         end
  58.     else
  59.         unload( true ) 
  60.     end
  61.  
  62.     if depth == 10 then
  63.         error("Finished mining")
  64.     end
  65.    
  66.     print( "Resuming mining..." )
  67.     goTo( x,y,z,xd,zd )
  68. end
  69.  
  70. local function collect()   
  71.     local bFull = true
  72.     local nTotalItems = 0
  73.     for n=1,16 do
  74.         local nCount = turtle.getItemCount(n)
  75.         if nCount == 0 then
  76.             bFull = false
  77.         end
  78.         nTotalItems = nTotalItems + nCount
  79.     end
  80.    
  81.     if nTotalItems > collected then
  82.         collected = nTotalItems
  83.         if math.fmod(collected + unloaded, 50) == 0 then
  84.             print( "Mined "..(collected + unloaded).." items." )
  85.         end
  86.     end
  87.    
  88.     if bFull then
  89.         print( "No empty slots left." )
  90.         return false
  91.     end
  92.     return true
  93. end
  94.  
  95. function refuel( ammount )
  96.     local fuelLevel = turtle.getFuelLevel()
  97.     if fuelLevel == "unlimited" then
  98.         return true
  99.     end
  100.    
  101.     local needed = ammount or (xPos + zPos + depth + 1)
  102.     if turtle.getFuelLevel() < needed then
  103.         local fueled = false
  104.         for n=1,16 do
  105.             if turtle.getItemCount(n) > 0 then
  106.                 turtle.select(n)
  107.                 if turtle.refuel(1) then
  108.                     while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
  109.                         turtle.refuel(1)
  110.                     end
  111.                     if turtle.getFuelLevel() >= needed then
  112.                         turtle.select(1)
  113.                         return true
  114.                     end
  115.                 end
  116.             end
  117.         end
  118.         turtle.select(1)
  119.         return false
  120.     end
  121.    
  122.     return true
  123. end
  124.  
  125. local function tryForwards()
  126.     if not refuel() then
  127.         print( "Not enough Fuel" )
  128.         returnSupplies()
  129.     end
  130.    
  131.     while not turtle.forward() do
  132.         if turtle.detect() then
  133.             if turtle.dig() then
  134.                 if not collect() then
  135.                     returnSupplies()
  136.                 end
  137.             else
  138.                 return false
  139.             end
  140.         elseif turtle.attack() then
  141.             if not collect() then
  142.                 returnSupplies()
  143.             end
  144.         else
  145.             sleep( 0.5 )
  146.         end
  147.     end
  148.    
  149.     xPos = xPos + xDir
  150.     zPos = zPos + zDir
  151.     return true
  152. end
  153.  
  154. local function tryDown()
  155.     if depth == 10 then
  156.         returnSupplies()
  157.     end
  158.  
  159.     if not refuel() then
  160.         print( "Not enough Fuel" )
  161.         returnSupplies()
  162.     end
  163.    
  164.     while not turtle.down() do
  165.         if turtle.detectDown() then
  166.             if turtle.digDown() then
  167.                 if not collect() then
  168.                     returnSupplies()
  169.                 end
  170.             else
  171.                 return false
  172.             end
  173.         elseif turtle.attackDown() then
  174.             if not collect() then
  175.                 returnSupplies()
  176.             end
  177.         else
  178.             sleep( 0.5 )
  179.         end
  180.     end
  181.  
  182.     depth = depth + 1
  183.     if math.fmod( depth, 10 ) == 0 then
  184.         print( "Descended "..depth.." metres." )
  185.     end
  186.  
  187.     return true
  188. end
  189.  
  190. local function turnLeft()
  191.     turtle.turnLeft()
  192.     xDir, zDir = -zDir, xDir
  193. end
  194.  
  195. local function turnRight()
  196.     turtle.turnRight()
  197.     xDir, zDir = zDir, -xDir
  198. end
  199.  
  200. function goTo( x, y, z, xd, zd )
  201.     while depth > y do
  202.         if turtle.up() then
  203.             depth = depth - 1
  204.         elseif turtle.digUp() or turtle.attackUp() then
  205.             collect()
  206.         else
  207.             sleep( 0.5 )
  208.         end
  209.     end
  210.  
  211.     if xPos > x then
  212.         while xDir ~= -1 do
  213.             turnLeft()
  214.         end
  215.         while xPos > x do
  216.             if turtle.forward() then
  217.                 xPos = xPos - 1
  218.             elseif turtle.dig() or turtle.attack() then
  219.                 collect()
  220.             else
  221.                 sleep( 0.5 )
  222.             end
  223.         end
  224.     elseif xPos < x then
  225.         while xDir ~= 1 do
  226.             turnLeft()
  227.         end
  228.         while xPos < x do
  229.             if turtle.forward() then
  230.                 xPos = xPos + 1
  231.             elseif turtle.dig() or turtle.attack() then
  232.                 collect()
  233.             else
  234.                 sleep( 0.5 )
  235.             end
  236.         end
  237.     end
  238.    
  239.     if zPos > z then
  240.         while zDir ~= -1 do
  241.             turnLeft()
  242.         end
  243.         while zPos > z do
  244.             if turtle.forward() then
  245.                 zPos = zPos - 1
  246.             elseif turtle.dig() or turtle.attack() then
  247.                 collect()
  248.             else
  249.                 sleep( 0.5 )
  250.             end
  251.         end
  252.     elseif zPos < z then
  253.         while zDir ~= 1 do
  254.             turnLeft()
  255.         end
  256.         while zPos < z do
  257.             if turtle.forward() then
  258.                 zPos = zPos + 1
  259.             elseif turtle.dig() or turtle.attack() then
  260.                 collect()
  261.             else
  262.                 sleep( 0.5 )
  263.             end
  264.         end
  265.     end
  266.    
  267.     while depth < y do
  268.         if turtle.down() then
  269.             depth = depth + 1
  270.         elseif turtle.digDown() or turtle.attackDown() then
  271.             collect()
  272.         else
  273.             sleep( 0.5 )
  274.         end
  275.     end
  276.    
  277.     while zDir ~= zd or xDir ~= xd do
  278.         turnLeft()
  279.     end
  280. end
  281.  
  282. if not refuel() then
  283.     print( "Out of Fuel" )
  284.     return
  285. end
  286.  
  287. print( "Excavating..." )
  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." )
Advertisement
Add Comment
Please, Sign In to add comment