Advertisement
Guest User

tunnel3

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