Advertisement
NTins

Hell Excavate

Aug 20th, 2014
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.45 KB | None | 0 0
  1.  
  2. local tArgs = { ... }
  3. if #tArgs ~= 1 then
  4.     print( "Usage: excavate <diameter>" )
  5.     return
  6. end
  7.  
  8. if turtle.getItemCount(1) < 0 then
  9.     print("Put bucket in slot 1")
  10.     return
  11. elseif turtle.getItemCount(1) > 0 then
  12.     print("It needs only 1 bucket")
  13.     return
  14. end
  15.  
  16. -- Mine in a quarry pattern until we hit something we can't dig
  17. local size = tonumber( tArgs[1] )
  18. if size < 1 then
  19.     print( "Excavate diameter must be positive" )
  20.     return
  21. end
  22.    
  23. local depth = 0
  24. local unloaded = 0
  25. local collected = 0
  26.  
  27. local xPos,zPos = 0,0
  28. local xDir,zDir = 0,1
  29.  
  30. local goTo -- Filled in further down
  31. local refuel -- Filled in further down
  32.  
  33. local function unload( _bKeepOneFuelStack )
  34.     print( "Unloading items..." )
  35.     for n=2,16 do
  36.         local nCount = turtle.getItemCount(n)
  37.         if nCount > 0 then
  38.             turtle.select(n)           
  39.             local bDrop = true
  40.             if _bKeepOneFuelStack and turtle.refuel(0) then
  41.                 bDrop = false
  42.                 _bKeepOneFuelStack = false
  43.             end        
  44.             if bDrop then
  45.                 turtle.drop()
  46.                 unloaded = unloaded + nCount
  47.             end
  48.         end
  49.     end
  50.     collected = 0
  51.     turtle.select(2)
  52. end
  53.  
  54. local function returnSupplies()
  55.     local x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
  56.     print( "Returning to surface..." )
  57.     goTo( 0,0,0,0,-1 )
  58.    
  59.     local fuelNeeded = 2*(x+y+z) + 1
  60.     if not refuel( fuelNeeded ) then
  61.         unload( true )
  62.         print( "Waiting for fuel" )
  63.         while not refuel( fuelNeeded ) do
  64.             sleep(1)
  65.         end
  66.     else
  67.         unload( true ) 
  68.     end
  69.    
  70.     print( "Resuming mining..." )
  71.     goTo( x,y,z,xd,zd )
  72. end
  73.  
  74. local function collect()   
  75.     local bFull = true
  76.     local nTotalItems = 0
  77.     for n=2,16 do
  78.         local nCount = turtle.getItemCount(n)
  79.         if nCount == 0 then
  80.             bFull = false
  81.         end
  82.         nTotalItems = nTotalItems + nCount
  83.     end
  84.    
  85.     if nTotalItems > collected then
  86.         collected = nTotalItems
  87.         if math.fmod(collected + unloaded, 50) == 0 then
  88.             print( "Mined "..(collected + unloaded).." items." )
  89.         end
  90.     end
  91.    
  92.     if bFull then
  93.         print( "No empty slots left." )
  94.         return false
  95.     end
  96.     return true
  97. end
  98.  
  99. function refuel( ammount )
  100.     local fuelLevel = turtle.getFuelLevel()
  101.     if fuelLevel == "unlimited" then
  102.         return true
  103.     end
  104.    
  105.     local needed = ammount or (xPos + zPos + depth + 2)
  106.     if turtle.getFuelLevel() < needed then
  107.         local fueled = false
  108.         for n=2,16 do
  109.             if turtle.getItemCount(n) > 0 then
  110.                 turtle.select(n)
  111.                 if turtle.refuel(1) then
  112.                     while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
  113.                         turtle.refuel(1)
  114.                     end
  115.                     if turtle.getFuelLevel() >= needed then
  116.                         turtle.select(2)
  117.                         return true
  118.                     end
  119.                 end
  120.             end
  121.         end
  122.         turtle.select(2)
  123.         return false
  124.     end
  125.    
  126.     return true
  127. end
  128.  
  129. local function tryForwards()
  130.     if not refuel() then
  131.         print( "Not enough Fuel" )
  132.         returnSupplies()
  133.     end
  134.     turtle.select(1)
  135.     if turtle.place() then
  136.         turtle.refuel()
  137.     end
  138.     turtle.select(2)
  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.     if math.fmod( depth, 10 ) == 0 then
  188.         print( "Descended "..depth.." metres." )
  189.     end
  190.  
  191.     return true
  192. end
  193.  
  194. local function turnLeft()
  195.     turtle.turnLeft()
  196.     xDir, zDir = -zDir, xDir
  197. end
  198.  
  199. local function turnRight()
  200.     turtle.turnRight()
  201.     xDir, zDir = zDir, -xDir
  202. end
  203.  
  204. function goTo( x, y, z, xd, zd )
  205.     while depth > y do
  206.         if turtle.up() then
  207.             depth = depth - 1
  208.         elseif turtle.digUp() or turtle.attackUp() then
  209.             collect()
  210.         else
  211.             sleep( 0.5 )
  212.         end
  213.     end
  214.  
  215.     if 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.     elseif xPos < x then
  229.         while xDir ~= 1 do
  230.             turnLeft()
  231.         end
  232.         while xPos < x do
  233.             if turtle.forward() then
  234.                 xPos = xPos + 1
  235.             elseif turtle.dig() or turtle.attack() then
  236.                 collect()
  237.             else
  238.                 sleep( 0.5 )
  239.             end
  240.         end
  241.     end
  242.    
  243.     if 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.     elseif zPos < z then
  257.         while zDir ~= 1 do
  258.             turnLeft()
  259.         end
  260.         while zPos < z do
  261.             if turtle.forward() then
  262.                 zPos = zPos + 1
  263.             elseif turtle.dig() or turtle.attack() then
  264.                 collect()
  265.             else
  266.                 sleep( 0.5 )
  267.             end
  268.         end
  269.     end
  270.    
  271.     while depth < y do
  272.         if turtle.down() then
  273.             depth = depth + 1
  274.         elseif turtle.digDown() or turtle.attackDown() then
  275.             collect()
  276.         else
  277.             sleep( 0.5 )
  278.         end
  279.     end
  280.    
  281.     while zDir ~= zd or xDir ~= xd do
  282.         turnLeft()
  283.     end
  284. end
  285.  
  286. if not refuel() then
  287.     print( "Out of Fuel" )
  288.     return
  289. end
  290.  
  291. print( "Excavating..." )
  292.  
  293. local reseal = false
  294. turtle.select(2)
  295. if turtle.digDown() then
  296.     reseal = true
  297. end
  298.  
  299. local alternate = 0
  300. local done = false
  301. while not done do
  302.     for n=1,size do
  303.         for m=1,size-1 do
  304.             if not tryForwards() then
  305.                 done = true
  306.                 break
  307.             end
  308.         end
  309.         if done then
  310.             break
  311.         end
  312.         if n<size then
  313.             if math.fmod(n + alternate,2) == 0 then
  314.                 turnLeft()
  315.                 if not tryForwards() then
  316.                     done = true
  317.                     break
  318.                 end
  319.                 turnLeft()
  320.             else
  321.                 turnRight()
  322.                 if not tryForwards() then
  323.                     done = true
  324.                     break
  325.                 end
  326.                 turnRight()
  327.             end
  328.         end
  329.     end
  330.     if done then
  331.         break
  332.     end
  333.    
  334.     if size > 1 then
  335.         if math.fmod(size,2) == 0 then
  336.             turnRight()
  337.         else
  338.             if alternate == 0 then
  339.                 turnLeft()
  340.             else
  341.                 turnRight()
  342.             end
  343.             alternate = 1 - alternate
  344.         end
  345.     end
  346.    
  347.     if not tryDown() then
  348.         done = true
  349.         break
  350.     end
  351. end
  352.  
  353. print( "Returning to surface..." )
  354.  
  355. -- Return to where we started
  356. goTo( 0,0,0,0,-1 )
  357. unload( false )
  358. goTo( 0,0,0,0,1 )
  359.  
  360. -- Seal the hole
  361. if reseal then
  362.     turtle.placeDown()
  363. end
  364.  
  365. print( "Mined "..(collected + unloaded).." items total." )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement