Advertisement
Guest User

projectDigsALot

a guest
Feb 20th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.13 KB | None | 0 0
  1.  
  2. local tArgs = { ... }
  3. if #tArgs ~= 1 then
  4.     print( "Usage: tunnel <length>" )
  5.     return
  6. end
  7.  
  8. -- Mine in a quarry pattern until we hit something we can't dig
  9. local length = tonumber( tArgs[1] )
  10. if length < 1 then
  11.     print( "Tunnel length must be positive" )
  12.     return
  13. end
  14.    
  15. local depth = 0
  16. local collected = 0
  17.  
  18. local function collect()
  19.     collected = collected + 1
  20.     if math.fmod(collected, 25) == 0 then
  21.         print( "Mined "..collected.." items." )
  22.     end
  23. end
  24.  
  25. local function tryDig()
  26.     while turtle.detect() do
  27.         if turtle.dig() then
  28.             collect()
  29.             sleep(0.5)
  30.         else
  31.             return false
  32.         end
  33.     end
  34.     return true
  35. end
  36.  
  37. local function tryDigUp()
  38.     while turtle.detectUp() do
  39.         if turtle.digUp() then
  40.             collect()
  41.             sleep(0.5)
  42.         else
  43.             return false
  44.         end
  45.     end
  46.     return true
  47. end
  48.  
  49. local function tryDigDown()
  50.     while turtle.detectDown() do
  51.         if turtle.digDown() then
  52.             collect()
  53.             sleep(0.5)
  54.         else
  55.             return false
  56.         end
  57.     end
  58.     return true
  59. end
  60.  
  61. local function refuel()
  62.     local fuelLevel = turtle.getFuelLevel()
  63.     if fuelLevel == "unlimited" or fuelLevel > 0 then
  64.         return
  65.     end
  66.    
  67.     local function tryRefuel()
  68.         for n=1,16 do
  69.             if turtle.getItemCount(n) > 0 then
  70.                 turtle.select(n)
  71.                 if turtle.refuel(1) then
  72.                     turtle.select(1)
  73.                     return true
  74.                 end
  75.             end
  76.         end
  77.         turtle.select(1)
  78.         return false
  79.     end
  80.    
  81.     if not tryRefuel() then
  82.         print( "Add more fuel to continue." )
  83.         while not tryRefuel() do
  84.             os.pullEvent( "turtle_inventory" )
  85.         end
  86.         print( "Resuming Tunnel." )
  87.     end
  88. end
  89.  
  90. local function tryUp()
  91.     refuel()
  92.     while not turtle.up() do
  93.         if turtle.detectUp() then
  94.             if not tryDigUp() then
  95.                 return false
  96.             end
  97.         elseif turtle.attackUp() then
  98.             collect()
  99.         else
  100.             sleep( 0.5 )
  101.         end
  102.     end
  103.     return true
  104. end
  105.  
  106. local function tryDown()
  107.     refuel()
  108.     while not turtle.down() do
  109.         if turtle.detectDown() then
  110.             if not tryDigDown() then
  111.                 return false
  112.             end
  113.         elseif turtle.attackDown() then
  114.             collect()
  115.         else
  116.             sleep( 0.5 )
  117.         end
  118.     end
  119.     return true
  120. end
  121.  
  122. local function tryForward()
  123.     refuel()
  124.     while not turtle.forward() do
  125.         if turtle.detect() then
  126.             if not tryDig() then
  127.                 return false
  128.             end
  129.         elseif turtle.attack() then
  130.             collect()
  131.         else
  132.             sleep( 0.5 )
  133.         end
  134.     end
  135.     return true
  136. end
  137.  
  138. local function branch()
  139.     for i = 1, 7, 1 do
  140.         tryDig()
  141.         tryForward()
  142.         tryDigUp()
  143.     end
  144.     tryUp()
  145.     turtle.turnRight()
  146.     tryDig()
  147.     turtle.select(2)
  148.     turtle.place()
  149.     turtle.select(1)
  150.  tryDown()
  151.    
  152.     turtle.turnRight()
  153.     for i = 1, 8, 1 do
  154.         tryForward()
  155.     end
  156.    
  157. end
  158.  
  159. local function sideBranches()
  160.     turtle.turnLeft()
  161.     tryForward()
  162.     branch()
  163.     tryForward()
  164.     branch()
  165.     turtle.turnRight()
  166.    
  167. end
  168.  
  169. local function placeTorch()
  170.     turtle.turnRight()
  171.     tryUp()
  172.     turtle.select(2)
  173.     turtle.place()
  174.     turtle.select(1)
  175.     tryDown()
  176.     turtle.turnLeft()
  177.    
  178. end
  179.  
  180. local function checkInv()
  181.     if turtle.getItemCount(12) > 0 then
  182.         return true
  183.     end
  184.     return false
  185. end
  186.  
  187. local function returnToPoint(distance)
  188.    
  189.     while distance > 0 do
  190.         if turtle.forward() then
  191.             distance = distance - 1
  192.         else
  193.             turtle.dig()
  194.         end
  195.     end
  196. end
  197.  
  198.  
  199. local function turnAround()
  200.     turtle.turnRight()
  201.     turtle.turnRight()
  202.    
  203. end
  204.  
  205.  
  206. local function mineMain()
  207.     turtle.placeDown()
  208.     tryDigUp()
  209.     turtle.turnLeft()
  210.     tryDig()
  211.     tryUp()
  212.     tryDig()
  213.     turnAround()
  214.     tryDig()
  215.     tryDown()
  216.     tryDig()
  217.     turtle.turnLeft()
  218.    
  219. end
  220.  
  221. local function placeChest()
  222.     turtle.turnLeft()
  223.     tryForward()
  224.     turtle.select(3)
  225.     tryDig()
  226.     turtle.place()
  227.     turtle.select(1)
  228.     tryUp()
  229.     tryDig()
  230.     tryDown()
  231.     turnAround()
  232.     tryForward()
  233.     turtle.turnLeft()
  234.    
  235. end
  236.  
  237. function putAwayInventory()
  238.     for i=4,16 do
  239.         if turtle.getItemCount(i) > 0 then
  240.             turtle.select(i)
  241.             turtle.dropDown()
  242.         end
  243.     end
  244.     turtle.select(1)
  245. end
  246.  
  247. print( "Tunnelling..." )
  248.  
  249. nextBranch = 1
  250. nextTorch = 2
  251. tryDig()
  252. tryForward()
  253. mineMain()
  254.  
  255. for n=1,length do
  256.     mineMain()
  257.     if n > 1 and n < 4 then
  258.         placeChest()
  259.     end
  260.     if n == nextBranch then
  261.         sideBranches()
  262.         nextBranch = nextBranch + 3
  263.     end
  264.     if n == nextTorch then
  265.         placeTorch()
  266.         nextTorch = nextTorch + 6
  267.     end
  268.     if checkInv() then
  269.         numBlocks = n - 2  
  270.         turnAround()
  271.         returnToPoint(numBlocks)
  272.         turtle.turnRight()
  273.         tryUp()
  274.         tryForward()
  275.         tryForward()
  276.         putAwayInventory()
  277.         turnAround()
  278.         tryForward()
  279.         tryForward()
  280.         tryDown()
  281.         turtle.turnLeft()
  282.         returnToPoint(numBlocks)
  283.        
  284.     end
  285.     if n<length then
  286.         tryDig()
  287.         if not tryForward() then
  288.             print( "Aborting Tunnel." )
  289.             break
  290.         end
  291.         depth = depth + 1
  292.     else
  293.         print( "Tunnel complete." )
  294.         turnAround()
  295.         depth = depth - 2
  296.         returnToPoint(depth)
  297.         turtle.turnRight()
  298.         tryUp()
  299.         tryForward()
  300.         tryForward()
  301.         putAwayInventory()
  302.         turnAround()
  303.         tryForward()
  304.         tryForward()
  305.         tryDown()
  306.         turtle.turnRight()
  307.         tryForward()
  308.         tryForward()
  309.         turnAround()
  310.        
  311.        
  312.     end
  313.  
  314. end
  315.  
  316. --[[
  317. print( "Returning to start..." )
  318.  
  319. -- Return to where we started
  320. turtle.turnLeft()
  321. turtle.turnLeft()
  322. while depth > 0 do
  323.     if turtle.forward() then
  324.         depth = depth - 1
  325.     else
  326.         turtle.dig()
  327.     end
  328. end
  329. turtle.turnRight()
  330. turtle.turnRight()
  331. ]]
  332.  
  333. print( "Tunnel complete." )
  334. print( "Mined "..collected.." items total." )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement