Advertisement
bcbwilla

Quarry

Nov 15th, 2013
1,300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.50 KB | None | 0 0
  1. local tArgs = { ... }
  2. if #tArgs < 1 or #tArgs > 2 then
  3.     print( "Usage: quarry <diameter> <shelfDepth>" )
  4.     return
  5. end
  6.  
  7. -- Mine in a quarry pattern until we hit something we can't dig
  8. local shelfDepth
  9. if #tArgs == 2 then
  10.     shelfDepth = tonumber( tArgs[2] )
  11. else
  12.     shelfDepth = 1
  13. end
  14.  
  15. local size = tonumber( tArgs[1] )
  16. if size < 1 then
  17.     print( "Quarry diameter must be positive" )
  18.     return
  19. end
  20.    
  21. local depth = 0
  22. local unloaded = 0
  23. local collected = 0
  24.  
  25. local xPos,zPos = 0,0
  26. local xDir,zDir = 0,1
  27.  
  28. local goTo -- Filled in further down
  29. local refuel -- Filled in further down
  30.  
  31. local function unload( _bKeepOneFuelStack )
  32.     print( "Unloading items..." )
  33.     for n=1,16 do
  34.         local nCount = turtle.getItemCount(n)
  35.         if nCount > 0 then
  36.             turtle.select(n)           
  37.             local bDrop = true
  38.             if _bKeepOneFuelStack and turtle.refuel(0) then
  39.                 bDrop = false
  40.                 _bKeepOneFuelStack = false
  41.             end        
  42.             if bDrop then
  43.                 turtle.drop()
  44.                 unloaded = unloaded + nCount
  45.             end
  46.         end
  47.     end
  48.     collected = 0
  49.     turtle.select(1)
  50. end
  51.  
  52. local function returnSupplies()
  53.     local x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
  54.     print( "Returning to surface..." )
  55.     goTo( 0,0,0,0,-1 )
  56.    
  57.     local fuelNeeded = 2*(x+y+z) + 1
  58.     if not refuel( fuelNeeded ) then
  59.         unload( true )
  60.         print( "Waiting for fuel" )
  61.         while not refuel( fuelNeeded ) do
  62.             sleep(1)
  63.         end
  64.     else
  65.         unload( true ) 
  66.     end
  67.    
  68.     print( "Resuming mining..." )
  69.     goTo( x,y,z,xd,zd )
  70. end
  71.  
  72. local function collect()   
  73.     local bFull = true
  74.     local nTotalItems = 0
  75.     for n=1,16 do
  76.         local nCount = turtle.getItemCount(n)
  77.         if nCount == 0 then
  78.             bFull = false
  79.         end
  80.         nTotalItems = nTotalItems + nCount
  81.     end
  82.    
  83.     if nTotalItems > collected then
  84.         collected = nTotalItems
  85.         if math.fmod(collected + unloaded, 50) == 0 then
  86.             print( "Mined "..(collected + unloaded).." items." )
  87.         end
  88.     end
  89.    
  90.     if bFull then
  91.         print( "No empty slots left." )
  92.         return false
  93.     end
  94.     return true
  95. end
  96.  
  97. function refuel( ammount )
  98.     local fuelLevel = turtle.getFuelLevel()
  99.     if fuelLevel == "unlimited" then
  100.         return true
  101.     end
  102.    
  103.     local needed = ammount or (xPos + zPos + depth + 1)
  104.     if turtle.getFuelLevel() < needed then
  105.         local fueled = false
  106.         for n=1,16 do
  107.             if turtle.getItemCount(n) > 0 then
  108.                 turtle.select(n)
  109.                 if turtle.refuel(1) then
  110.                     while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
  111.                         turtle.refuel(1)
  112.                     end
  113.                     if turtle.getFuelLevel() >= needed then
  114.                         turtle.select(1)
  115.                         return true
  116.                     end
  117.                 end
  118.             end
  119.         end
  120.         turtle.select(1)
  121.         return false
  122.     end
  123.    
  124.     return true
  125. end
  126.  
  127. local function tryForwards()
  128.     if not refuel() then
  129.         print( "Not enough Fuel" )
  130.         returnSupplies()
  131.     end
  132.    
  133.     while not turtle.forward() do
  134.         if turtle.detect() then
  135.             if turtle.dig() then
  136.                 if not collect() then
  137.                     returnSupplies()
  138.                 end
  139.             else
  140.                 return false
  141.             end
  142.         elseif turtle.attack() then
  143.             if not collect() then
  144.                 returnSupplies()
  145.             end
  146.         else
  147.             sleep( 0.5 )
  148.         end
  149.     end
  150.    
  151.     xPos = xPos + xDir
  152.     zPos = zPos + zDir
  153.     return true
  154. end
  155.  
  156. local function tryDown()
  157.     if not refuel() then
  158.         print( "Not enough Fuel" )
  159.         returnSupplies()
  160.     end
  161.    
  162.     while not turtle.down() do
  163.         if turtle.detectDown() then
  164.             if turtle.digDown() then
  165.                 if not collect() then
  166.                     returnSupplies()
  167.                 end
  168.             else
  169.                 return false
  170.             end
  171.         elseif turtle.attackDown() then
  172.             if not collect() then
  173.                 returnSupplies()
  174.             end
  175.         else
  176.             sleep( 0.5 )
  177.         end
  178.     end
  179.  
  180.     depth = depth + 1
  181.     if math.fmod( depth, 10 ) == 0 then
  182.         print( "Descended "..depth.." metres." )
  183.     end
  184.  
  185.     return true
  186. end
  187.  
  188. local function turnLeft()
  189.     turtle.turnLeft()
  190.     xDir, zDir = -zDir, xDir
  191. end
  192.  
  193. local function turnRight()
  194.     turtle.turnRight()
  195.     xDir, zDir = zDir, -xDir
  196. end
  197.  
  198. function goTo( x, y, z, xd, zd )
  199.     while depth > y do
  200.         if turtle.up() then
  201.             depth = depth - 1
  202.         elseif turtle.digUp() or turtle.attackUp() then
  203.             collect()
  204.         else
  205.             sleep( 0.5 )
  206.         end
  207.     end
  208.  
  209.     if xPos > x then
  210.         while xDir ~= -1 do
  211.             turnLeft()
  212.         end
  213.         while xPos > x do
  214.             if turtle.forward() then
  215.                 xPos = xPos - 1
  216.             elseif turtle.dig() or turtle.attack() then
  217.                 collect()
  218.             else
  219.                 sleep( 0.5 )
  220.             end
  221.         end
  222.     elseif xPos < x then
  223.         while xDir ~= 1 do
  224.             turnLeft()
  225.         end
  226.         while xPos < x do
  227.             if turtle.forward() then
  228.                 xPos = xPos + 1
  229.             elseif turtle.dig() or turtle.attack() then
  230.                 collect()
  231.             else
  232.                 sleep( 0.5 )
  233.             end
  234.         end
  235.     end
  236.    
  237.     if zPos > z then
  238.         while zDir ~= -1 do
  239.             turnLeft()
  240.         end
  241.         while zPos > z do
  242.             if turtle.forward() then
  243.                 zPos = zPos - 1
  244.             elseif turtle.dig() or turtle.attack() then
  245.                 collect()
  246.             else
  247.                 sleep( 0.5 )
  248.             end
  249.         end
  250.     elseif zPos < z then
  251.         while zDir ~= 1 do
  252.             turnLeft()
  253.         end
  254.         while zPos < z do
  255.             if turtle.forward() then
  256.                 zPos = zPos + 1
  257.             elseif turtle.dig() or turtle.attack() then
  258.                 collect()
  259.             else
  260.                 sleep( 0.5 )
  261.             end
  262.         end
  263.     end
  264.    
  265.     while depth < y do
  266.         if turtle.down() then
  267.             depth = depth + 1
  268.         elseif turtle.digDown() or turtle.attackDown() then
  269.             collect()
  270.         else
  271.             sleep( 0.5 )
  272.         end
  273.     end
  274.    
  275.     while zDir ~= zd or xDir ~= xd do
  276.         turnLeft()
  277.     end
  278. end
  279.  
  280. if not refuel() then
  281.     print( "Out of Fuel" )
  282.     return
  283. end
  284.  
  285. print( "Quarrying..." )
  286.  
  287. local reseal = false
  288. turtle.select(1)
  289. if turtle.digDown() then
  290.     reseal = true
  291. end
  292.  
  293. local alternate = 0
  294. local done = false
  295. local diameter = size
  296. while not done do
  297.     for j=1,shelfDepth do
  298.         for n=1,diameter do
  299.             for m=1,diameter-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<diameter 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 j == shelfDepth then
  332.                 if math.fmod(size,2) == 0 then
  333.                     turnRight()
  334.                     turnRight()
  335.                     tryForwards()
  336.                     turnLeft()
  337.                     tryForwards()
  338.                 else
  339.                     if alternate == 0 then
  340.                         turnLeft()
  341.                         turnLeft()
  342.                         tryForwards()
  343.                         turnRight()
  344.                         tryForwards()
  345.                     else
  346.                         turnRight()
  347.                         turnRight()
  348.                         tryForwards()
  349.                         turnLeft()
  350.                         tryForwards()
  351.                     end
  352.                     alternate = 1 - alternate
  353.                 end
  354.             else
  355.                 if math.fmod(size,2) == 0 then
  356.                     turnRight()
  357.                 else
  358.                     if alternate == 0 then
  359.                         turnLeft()
  360.                     else
  361.                         turnRight()
  362.                     end
  363.                     alternate = 1 - alternate              
  364.                 end
  365.             end
  366.         end
  367.         if not tryDown() then
  368.             done = true
  369.             break
  370.         end
  371.     end
  372.     diameter = diameter - 2
  373.     if diameter <= 2 then
  374.         done = true
  375.         break
  376.     end
  377. end  
  378.  
  379. print( "Returning to surface..." )
  380.  
  381. -- Return to where we started
  382. goTo( 0,0,0,0,-1 )
  383. unload( false )
  384. goTo( 0,0,0,0,1 )
  385.  
  386. -- Seal the hole
  387. if reseal then
  388.     turtle.placeDown()
  389. end
  390.  
  391. print( "Mined "..(collected + unloaded).." items total." )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement