Advertisement
Guest User

t3

a guest
Nov 17th, 2019
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.87 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. if length < 1 then
  15.     print( "Tunnel length must be positive" )
  16.     return
  17. end
  18.  
  19. local height = tonumber( tArgs[2] )
  20. height = height-1
  21. if height < 2 then
  22.     print( "Tunnel height must be over 1" )
  23.     return
  24. end
  25.  
  26. local depth = 0
  27. local collected = 0
  28.  
  29. local function collect()
  30.     collected = collected + 1
  31.     if math.fmod(collected, 25) == 0 then
  32.         print( "Mined "..collected.." items." )
  33.     end
  34. end
  35.  
  36. local function tryDig()
  37.     while turtle.detect() do
  38.         if turtle.dig() then
  39.             collect()
  40.             sleep(0.5)
  41.         else
  42.             return false
  43.         end
  44.     end
  45.     return true
  46. end
  47.  
  48. local function tryDigUp()
  49.     while turtle.detectUp() do
  50.         if turtle.digUp() then
  51.             collect()
  52.             sleep(0.5)
  53.         else
  54.             return false
  55.         end
  56.     end
  57.     return true
  58. end
  59.  
  60. local function tryDigDown()
  61.     while turtle.detectDown() do
  62.         if turtle.digDown() then
  63.             collect()
  64.             --sleep(0.5)
  65.         else
  66.             return false
  67.         end
  68.     end
  69.     return true
  70. end
  71.  
  72. local function refuel()
  73.     local fuelLevel = turtle.getFuelLevel()
  74.     if fuelLevel == "unlimited" or fuelLevel > 0 then
  75.         return
  76.     end
  77.  
  78.     local function tryRefuel()
  79.         for n=2,16 do
  80.             if turtle.getItemCount(n) > 0 then
  81.                 turtle.select(n)
  82.                
  83.                 if turtle.refuel(1) then
  84.                     turtle.select(1)
  85.                     return true
  86.                 end
  87.             end
  88.         end
  89.         turtle.select(1)
  90.         return false
  91.     end
  92.  
  93.     if not tryRefuel() then
  94.         print( "Add more fuel to continue." )
  95.         while not tryRefuel() do
  96.             os.pullEvent( "turtle_inventory" )
  97.         end
  98.         print( "Resuming Tunnel." )
  99.     end
  100. end
  101.  
  102. local function tryUp()
  103.     --refuel()
  104.     while not turtle.up() do
  105.         if turtle.detectUp() then
  106.             if not tryDigUp() then
  107.                 return false
  108.             end
  109.         elseif turtle.attackUp() then
  110.             collect()
  111.         else
  112.             sleep( 0.5 )
  113.         end
  114.     end
  115.     return true
  116. end
  117.  
  118. local function tryDown()
  119.     refuel()
  120.     while not turtle.down() do
  121.         if turtle.detectDown() then
  122.             if not tryDigDown() then
  123.                 return false
  124.             end
  125.         elseif turtle.attackDown() then
  126.             collect()
  127.         else
  128.             sleep( 0.5 )
  129.         end
  130.     end
  131.     return true
  132. end
  133.  
  134. local function tryForward()
  135.     --refuel()
  136.     while not turtle.forward() do
  137.         if turtle.detect() then
  138.             if not tryDig() then
  139.                 return false
  140.             end
  141.         elseif turtle.attack() then
  142.             collect()
  143.         else
  144.             sleep( 0.5 )
  145.         end
  146.     end
  147.     return true
  148. end
  149.  
  150. local function checkInvFill()
  151.     --altered refuel to ignore slot 1 (chests)
  152.     --turtle.select(16)
  153.     --print("checking inv")
  154.     local c = turtle.getItemCount(16)
  155.     --print(c)
  156.     --print(c>0)
  157.     if c>0 then
  158.         print("Detected Full Inventory...")
  159.         turtle.turnRight()
  160.         turtle.select(1)
  161.         turtle.place()
  162.         --chest placed
  163.         --empty all but slot 1(chests)
  164.         for s=2,16 do
  165.             turtle.select(s)
  166.             turtle.drop()
  167.         end
  168.         turtle.turnLeft()
  169.     end
  170.     turtle.select(1)
  171. end
  172.  
  173. print( "Tunnelling..." )
  174.  
  175.  
  176. for n=1,length do
  177.     turtle.select(2)
  178.     turtle.placeDown()
  179.    
  180.     tryDigUp()
  181.     turtle.turnLeft()
  182.     --links hochgraben
  183.     for h=1,height do
  184.         tryDig()
  185.         tryUp()
  186.        
  187.     end
  188.    
  189.     tryDig()
  190.     --tryDigUp()
  191.     --tryUp()
  192.     --tryDig()
  193.     --tryDigUp()
  194.     --tryUp()
  195.     --tryDig()
  196.     turtle.turnRight()
  197.     turtle.turnRight()
  198.    
  199.     tryDig()
  200.    
  201.     for h=1, height do
  202.         tryDown()
  203.         turtle.dig()
  204.     end
  205.    
  206.    
  207.     --tryDown()
  208.     --tryDig()
  209.     --tryDown()
  210.     --tryDig()
  211.     --tryDown()
  212.     --tryDig()
  213.    
  214.     checkInvFill()
  215.    
  216.     turtle.turnLeft()
  217.    
  218.     if n<length then
  219.         tryDig()
  220.         if not tryForward() then
  221.             print( "Aborting Tunnel." )
  222.             break
  223.         end
  224.     else
  225.         print( "Tunnel complete." )
  226.     end
  227.  
  228. end
  229.  
  230. --[[
  231. print( "Returning to start..." )
  232.  
  233. -- Return to where we started
  234. turtle.turnLeft()
  235. turtle.turnLeft()
  236. while depth > 0 do
  237.     if turtle.forward() then
  238.         depth = depth - 1
  239.     else
  240.         turtle.dig()
  241.     end
  242. end
  243. turtle.turnRight()
  244. turtle.turnRight()
  245. ]]
  246.  
  247. print( "Tunnel complete." )
  248. print( "Mined "..collected.." items total." )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement