Advertisement
Guest User

Tuurtle Excavate 3x speed

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