Advertisement
Guest User

Computercraft Turtle Quarry

a guest
May 17th, 2013
37,913
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.13 KB | None | 0 0
  1. -- Usage (program name) <Width> <Depth> <Length> <Direction>
  2. -- Place a chest with enough space for what you are digging behind the turtle before beginning
  3. -- Put any fuel in the first slot, if you don't want to have to refuel, use refuel<amount> before running this
  4. -- Make sure you keep the chunk(s) the turtle is digging in loaded, otherwise it will break
  5.  
  6.  
  7. local tArgs = { ... }
  8. if #tArgs ~= 4 then
  9.     print( "Usage: excavate <Width> <Depth> <Length> <Direction>" )
  10.     print( "Direction - 0 for down, 1 for up" )
  11.     return
  12. end
  13.  
  14.  
  15. local size1    = tonumber( tArgs[1] )
  16. local size2    = tonumber( tArgs[2] )
  17. local size3    = tonumber( tArgs[3] )
  18. local upordown = tonumber( tArgs[4] )
  19. if size1 < 1 then
  20.     print( "X size must be positive" )
  21.     return
  22. end
  23. if size2 < 1 then
  24.     print( "Depth must be positive" )
  25.     return
  26. end
  27. if size3 < 1 then
  28.     print( "Z size must be positive" )
  29.     return
  30. end
  31. if upordown~=0 and upordown~=1 then
  32.     print( "Invalid direction" )
  33.     return
  34. end
  35.  
  36.    
  37. local depth = 0
  38. local unloaded = 0
  39. local collected = 0
  40.  
  41. local xPos,zPos = 0,0
  42. local xDir,zDir = 0,1
  43.  
  44. local goTo -- Filled in further down
  45. local refuel -- Filled in further down
  46.  
  47. local function unload()
  48.     print( "Unloading items..." )
  49.     for n=2,16 do
  50.         unloaded = unloaded + turtle.getItemCount(n)
  51.         turtle.select(n)
  52.         turtle.drop()
  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 = x+y+z + x+y+z + 1
  64.     if not refuel( fuelNeeded ) then
  65.         unload()
  66.         print( "Waiting for fuel" )
  67.         while not refuel( fuelNeeded ) do
  68.             sleep(1)
  69.         end
  70.     else
  71.         unload()   
  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,16 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 or (xPos + zPos + depth + 1)
  110.     if turtle.getFuelLevel() < needed then
  111.         local fueled = false
  112.         for n=1,16 do
  113.             if turtle.getItemCount(n) > 0 then
  114.                 turtle.select(n)
  115.                 if turtle.refuel(1) then
  116.                     while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
  117.                         turtle.refuel(1)
  118.                     end
  119.                     if turtle.getFuelLevel() >= needed then
  120.                         turtle.select(1)
  121.                         return true
  122.                     end
  123.                 end
  124.             end
  125.         end
  126.         turtle.select(1)
  127.         return false
  128.     end
  129.    
  130.     return true
  131. end
  132.  
  133. local function tryForwards()
  134.     if not refuel() then
  135.         print( "Not enough Fuel" )
  136.         returnSupplies()
  137.     end
  138.    
  139.     while not turtle.forward() do
  140.         if turtle.detect() then
  141.             if turtle.dig() then
  142.                 if not collect() then
  143.                     returnSupplies()
  144.                 end
  145.             else
  146.                 return false
  147.             end
  148.         elseif turtle.attack() then
  149.             if not collect() then
  150.                 returnSupplies()
  151.             end
  152.         else
  153.             sleep( 0.5 )
  154.         end
  155.     end
  156.    
  157.     xPos = xPos + xDir
  158.     zPos = zPos + zDir
  159.     return true
  160. end
  161.  
  162. local function tryDown()
  163.     if not refuel() then
  164.         print( "Not enough Fuel" )
  165.         returnSupplies()
  166.     end
  167.    
  168.     while not turtle.down() do
  169.         if turtle.detectDown() then
  170.             if turtle.digDown() then
  171.                 if not collect() then
  172.                     returnSupplies()
  173.                 end
  174.             else
  175.                 return false
  176.             end
  177.         elseif turtle.attackDown() then
  178.             if not collect() then
  179.                 returnSupplies()
  180.             end
  181.         else
  182.             sleep( 0.5 )
  183.         end
  184.     end
  185.  
  186.     depth = depth + 1
  187.  
  188.     return true
  189. end
  190.  
  191. local function tryUp()
  192.     if not refuel() then
  193.         print( "Not enough Fuel" )
  194.         returnSupplies()
  195.     end
  196.    
  197.     while not turtle.up() do
  198.         if turtle.detectUp() then
  199.             if turtle.digUp() then
  200.                 if not collect() then
  201.                     returnSupplies()
  202.                 end
  203.             else
  204.                 return false
  205.             end
  206.         elseif turtle.attackUp() then
  207.             if not collect() then
  208.                 returnSupplies()
  209.             end
  210.         else
  211.             sleep( 0.5 )
  212.         end
  213.     end
  214.  
  215.     depth = depth - 1
  216.  
  217.     return true
  218. end
  219.  
  220. local function turnLeft()
  221.     turtle.turnLeft()
  222.     xDir, zDir = -zDir, xDir
  223. end
  224.  
  225. local function turnRight()
  226.     turtle.turnRight()
  227.     xDir, zDir = zDir, -xDir
  228. end
  229.  
  230. function goTo( x, y, z, xd, zd )
  231.     while depth > y do
  232.         if turtle.up() then
  233.             depth = depth - 1
  234.         elseif turtle.digUp() or turtle.attackUp() then
  235.             collect()
  236.         else
  237.             sleep( 0.5 )
  238.         end
  239.     end
  240.  
  241.     if xPos > x then
  242.         while xDir ~= -1 do
  243.             turnLeft()
  244.         end
  245.         while xPos > x do
  246.             if turtle.forward() then
  247.                 xPos = xPos - 1
  248.             elseif turtle.dig() or turtle.attack() then
  249.                 collect()
  250.             else
  251.                 sleep( 0.5 )
  252.             end
  253.         end
  254.     elseif xPos < x then
  255.         while xDir ~= 1 do
  256.             turnLeft()
  257.         end
  258.         while xPos < x do
  259.             if turtle.forward() then
  260.                 xPos = xPos + 1
  261.             elseif turtle.dig() or turtle.attack() then
  262.                 collect()
  263.             else
  264.                 sleep( 0.5 )
  265.             end
  266.         end
  267.     end
  268.    
  269.     if zPos > z then
  270.         while zDir ~= -1 do
  271.             turnLeft()
  272.         end
  273.         while zPos > z do
  274.             if turtle.forward() then
  275.                 zPos = zPos - 1
  276.             elseif turtle.dig() or turtle.attack() then
  277.                 collect()
  278.             else
  279.                 sleep( 0.5 )
  280.             end
  281.         end
  282.     elseif zPos < z then
  283.         while zDir ~= 1 do
  284.             turnLeft()
  285.         end
  286.         while zPos < z do
  287.             if turtle.forward() then
  288.                 zPos = zPos + 1
  289.             elseif turtle.dig() or turtle.attack() then
  290.                 collect()
  291.             else
  292.                 sleep( 0.5 )
  293.             end
  294.         end
  295.     end
  296.    
  297.     while depth < y do
  298.         if turtle.down() then
  299.             depth = depth + 1
  300.         elseif turtle.digDown() or turtle.attackDown() then
  301.             collect()
  302.         else
  303.             sleep( 0.5 )
  304.         end
  305.     end
  306.    
  307.     while zDir ~= zd or xDir ~= xd do
  308.         turnLeft()
  309.     end
  310. end
  311.  
  312. if not refuel() then
  313.     print( "Out of Fuel" )
  314.     return
  315. end
  316.  
  317. print( "Excavating..." )
  318.  
  319. local reseal = false
  320. turtle.select(1)
  321. if upordown == 0 then
  322.     if turtle.digDown() then
  323.     end
  324. elseif upordown == 1 then
  325.     if turtle.digUp() then
  326.     end
  327. end
  328.  
  329. local alternate = 0
  330. local done = 0
  331. while done~=size2 do
  332.     for varx=1,size1 do
  333.         for varz=1,size3-1 do
  334.             if not tryForwards() then
  335.                 done = size2
  336.                 break
  337.             end
  338.         end
  339.         if done==size2 then
  340.             break
  341.         end
  342.         if varx<size1 then
  343.             if math.fmod(varx + alternate,2) == 0 then
  344.                 turnLeft()
  345.                 if not tryForwards() then
  346.                     done = size2
  347.                     break
  348.                 end
  349.                 turnLeft()
  350.             else
  351.                 turnRight()
  352.                 if not tryForwards() then
  353.                     done = size2
  354.                     break
  355.                 end
  356.                 turnRight()
  357.             end
  358.         end
  359.     end
  360.     if done==size2 then
  361.         break
  362.     end
  363.    
  364.     if size1 > 1 then
  365.         goTo( 0,depth,0,0,1 )
  366.     end
  367.     if done ~= size2-1 then
  368.         if upordown == 0 then
  369.             if not tryDown() then
  370.                 done = size2
  371.                 break
  372.             end
  373.         elseif upordown == 1 then
  374.             if not tryUp() then
  375.                 done = size2
  376.                 break
  377.             end
  378.         end
  379.     end
  380.     done = done + 1
  381. end
  382.  
  383. print( "Returning to surface..." )
  384.  
  385. -- Return to where we started
  386. goTo( 0,0,0,0,-1 )
  387. unload()
  388. goTo( 0,0,0,0,1 )
  389.  
  390. -- Seal the hole
  391. if reseal then
  392.     turtle.placeDown()
  393. end
  394.  
  395. print( "Mined "..(collected + unloaded).." items total." )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement