Negasus

ExUp

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