Advertisement
ArsKvsh

[CC] htunnel

Aug 10th, 2019
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.57 KB | None | 0 0
  1. if not turtle then
  2.     printError( "Requires a Turtle" )
  3.     return
  4. end
  5.  
  6. local tArgs = { ... }
  7. if #tArgs ~= 2 then
  8.     print( "Usage: tunnel <length> <height>" )
  9.     return
  10. end
  11.  
  12. -- Mine in a quarry pattern until we hit something we can't dig
  13. local length = tonumber( tArgs[1] )
  14. local height = tonumber( tArgs[2] )
  15.  
  16. if length < 1 then
  17.     print( "Tunnel length must be positive" )
  18.     return
  19. end
  20.  
  21. if height < 1 then
  22.     print( "Tunnel height must be positive" )
  23.     return
  24. end
  25.  
  26. local collected = 0
  27.  
  28. local function collect()
  29.     collected = collected + 1
  30.     if math.fmod(collected, 25) == 0 then
  31.         print( "Mined "..collected.." items." )
  32.     end
  33. end
  34.  
  35. local function tryDig()
  36.     while turtle.detect() do
  37.         if turtle.dig() then
  38.             collect()
  39.             sleep(0.5)
  40.         else
  41.             return false
  42.         end
  43.     end
  44.     return true
  45. end
  46.  
  47. local function tryDigUp()
  48.     while turtle.detectUp() do
  49.         if turtle.digUp() then
  50.             collect()
  51.             sleep(0.5)
  52.         else
  53.             return false
  54.         end
  55.     end
  56.     return true
  57. end
  58.  
  59. local function tryDigDown()
  60.     while turtle.detectDown() do
  61.         if turtle.digDown() then
  62.             collect()
  63.             sleep(0.5)
  64.         else
  65.             return false
  66.         end
  67.     end
  68.     return true
  69. end
  70.  
  71. local function refuel()
  72.     local fuelLevel = turtle.getFuelLevel()
  73.     if fuelLevel == "unlimited" or fuelLevel > 0 then
  74.         return
  75.     end
  76.  
  77.     local function tryRefuel()
  78.         for n=1,16 do
  79.             if turtle.getItemCount(n) > 0 then
  80.                 turtle.select(n)
  81.                 if turtle.refuel(1) then
  82.                     turtle.select(1)
  83.                     return true
  84.                 end
  85.             end
  86.         end
  87.         turtle.select(1)
  88.         return false
  89.     end
  90.  
  91.     if not tryRefuel() then
  92.         print( "Add more fuel to continue." )
  93.         while not tryRefuel() do
  94.             os.pullEvent( "turtle_inventory" )
  95.         end
  96.         print( "Resuming Tunnel." )
  97.     end
  98. end
  99.  
  100. local function tryUp()
  101.     refuel()
  102.     while not turtle.up() do
  103.         if turtle.detectUp() then
  104.             if not tryDigUp() then
  105.                 return false
  106.             end
  107.         elseif turtle.attackUp() then
  108.             collect()
  109.         else
  110.             sleep( 0.5 )
  111.         end
  112.     end
  113.     return true
  114. end
  115.  
  116. local function tryDown()
  117.     refuel()
  118.     while not turtle.down() do
  119.         if turtle.detectDown() then
  120.             if not tryDigDown() then
  121.                 return false
  122.             end
  123.         elseif turtle.attackDown() then
  124.             collect()
  125.         else
  126.             sleep( 0.5 )
  127.         end
  128.     end
  129.     return true
  130. end
  131.  
  132. local function tryForward()
  133.     refuel()
  134.     while not turtle.forward() do
  135.         if turtle.detect() then
  136.             if not tryDig() then
  137.                 return false
  138.             end
  139.         elseif turtle.attack() then
  140.             collect()
  141.         else
  142.             sleep( 0.5 )
  143.         end
  144.     end
  145.     return true
  146. end
  147.  
  148. print( "Tunnelling..." )
  149.  
  150. for l=1,length do
  151.     turtle.turnLeft()
  152.     for h=1,height-1 do
  153.         tryDig()
  154.         tryUp()
  155.     end
  156.     tryDig()
  157.     turtle.turnRight()
  158.     turtle.turnRight()
  159.     tryDig()
  160.     for h=1,height-1 do
  161.         tryDown()
  162.         tryDig()
  163.     end
  164.     turtle.turnLeft()
  165.     if l<length then
  166.         tryDig()
  167.         if not tryForward() then
  168.             print( "Aborting Tunnel." )
  169.             break
  170.         end
  171.     else
  172.         print( "Tunnel complete." )
  173.     end
  174.  
  175. end
  176.  
  177. print( "Tunnel complete." )
  178. print( "Mined "..collected.." items total." )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement