Advertisement
Guest User

turtle 3x1 tunnel

a guest
Dec 17th, 2012
508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.39 KB | None | 0 0
  1.  
  2. local tArgs = { ... }
  3. if #tArgs ~= 2 then
  4.     print( "Usage: t <length> <place torches y/n>" )
  5.    
  6.     return
  7. end
  8.  
  9. -- Mine 3x1 tunnel until we hit end
  10. local size = tonumber( tArgs[1] )
  11. if size < 1 then
  12.     print( "Excavate diameter must be positive" )
  13.     return
  14. end
  15.  
  16. local placeTorches = false
  17. local slotStart = 1
  18.  
  19. if string.lower(tArgs[2]) == 'y' then
  20.     placeTorches = true
  21.     slotStart = 2
  22. end
  23.    
  24. local depth = 0
  25. local unloaded = 0
  26. local collected = 0
  27.  
  28. local xPos,zPos = 0,0
  29. local xDir,zDir = 0,1
  30.  
  31. local goTo -- Filled in further down
  32. local refuel -- Filled in further down
  33.  
  34.  
  35. local function unload()
  36.     print( "Unloading items..." )
  37.     for n=slotStart,16 do
  38.         unloaded = unloaded + turtle.getItemCount(n)
  39.         turtle.select(n)
  40.         turtle.drop()
  41.     end
  42.     collected = 0
  43.     turtle.select(1)
  44. end
  45.  
  46. local function returnSupplies()
  47.     local x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
  48.     print( "Returning to surface..." )
  49.     goTo( 0,0,0,0,-1 )
  50.    
  51.     local fuelNeeded = x+y+z + x+y+z + 1
  52.     if not refuel( fuelNeeded ) then
  53.         unload()
  54.         print( "Waiting for fuel" )
  55.         while not refuel( fuelNeeded ) do
  56.             sleep(1)
  57.         end
  58.     else
  59.         unload()   
  60.     end
  61.    
  62.    
  63.     while not torchCheck() do
  64.         print( "Waiting for torches" )
  65.         while not torchCheck() do
  66.             sleep(1)
  67.         end
  68.     end
  69.    
  70.     print( "Resuming mining..." )
  71.     goTo( x,y,z,xd,zd )
  72. end
  73.  
  74. local function collect()   
  75.     local bFull = true
  76.     local nTotalItems = 0
  77.     for n=1,16 do
  78.         local nCount = turtle.getItemCount(n)
  79.         if nCount == 0 then
  80.             bFull = false
  81.         end
  82.         nTotalItems = nTotalItems + nCount
  83.     end
  84.    
  85.     if nTotalItems > collected then
  86.         collected = nTotalItems
  87.         if math.fmod(collected + unloaded, 50) == 0 then
  88.             print( "Mined "..(collected + unloaded).." items." )
  89.         end
  90.     end
  91.    
  92.     if bFull then
  93.         print( "No empty slots left." )
  94.         return false
  95.     end
  96.     return true
  97. end
  98.  
  99. function torchCheck()
  100.     if placeTorches  then
  101.         if turtle.getItemCount(1) > 0 then
  102.             return true
  103.         else
  104.             return false
  105.         end
  106.     else
  107.         return true
  108.     end
  109.    
  110. end
  111.  
  112. function refuel( ammount )
  113.     local fuelLevel = turtle.getFuelLevel()
  114.     if fuelLevel == "unlimited" then
  115.         return true
  116.     end
  117.    
  118.     local needed = ammount or (xPos + zPos + depth + 1)
  119.     if turtle.getFuelLevel() < needed then
  120.         local fueled = false
  121.         for n=1,16 do
  122.             if turtle.getItemCount(n) > 0 then
  123.                 turtle.select(n)
  124.                 if turtle.refuel(1) then
  125.                     while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
  126.                         turtle.refuel(1)
  127.                     end
  128.                     if turtle.getFuelLevel() >= needed then
  129.                         turtle.select(1)
  130.                         return true
  131.                     end
  132.                 end
  133.             end
  134.         end
  135.         turtle.select(1)
  136.         return false
  137.     end
  138.    
  139.     return true
  140. end
  141.  
  142. local function tryDigUp()
  143.     --Dig Up
  144.     while turtle.detectUp() do
  145.         if turtle.detectUp() then
  146.             if turtle.digUp() then
  147.                 if not collect() then
  148.                     returnSupplies()
  149.                 end
  150.             else
  151.                 return false
  152.             end
  153.         elseif turtle.attackUp() then
  154.             if not collect() then
  155.                 returnSupplies()
  156.             end
  157.         else
  158.             sleep( 0.1 )
  159.         end
  160.     end
  161.     return true
  162. end
  163.  
  164. local function tryDigUp()
  165.     --Dig Up
  166.     while turtle.detectUp() do
  167.         if turtle.detectUp() then
  168.             if turtle.digUp() then
  169.                 if not collect() then
  170.                     returnSupplies()
  171.                 end
  172.             else
  173.                 return false
  174.             end
  175.         elseif turtle.attackUp() then
  176.             if not collect() then
  177.                 returnSupplies()
  178.             end
  179.         else
  180.             sleep( 0.1 )
  181.         end
  182.     end
  183.     return true
  184. end
  185.  
  186. local function tryDigDown()
  187.     --Dig Down
  188.     while turtle.detectDown() do
  189.         if turtle.detectDown() then
  190.             if turtle.digDown() then
  191.                 if not collect() then
  192.                     returnSupplies()
  193.                 end
  194.             else
  195.                 return false
  196.             end
  197.         elseif turtle.attackDown() then
  198.             if not collect() then
  199.                 returnSupplies()
  200.             end
  201.         else
  202.             sleep( 0.1 )
  203.         end
  204.     end
  205.     return true
  206. end
  207.  
  208. local function tryDigForward()
  209.     while turtle.detect() do
  210.         if turtle.detect() then
  211.             if turtle.dig() then
  212.                 if not collect() then
  213.                     returnSupplies()
  214.                 end
  215.            
  216.             else
  217.                 return false
  218.             end
  219.         elseif turtle.attack() then
  220.             if not collect() then
  221.                 returnSupplies()
  222.             end
  223.         else
  224.             sleep( 0.1 )
  225.         end
  226.     end
  227.     return true
  228. end
  229.  
  230.  
  231. local function tryForwards()
  232.     if not refuel() then
  233.         print( "Not enough Fuel" )
  234.         returnSupplies()
  235.     end
  236.    
  237.     tryDigForward()
  238.  
  239.     while not turtle.forward() do
  240.         if not tryDigForward() then
  241.             return false
  242.         end
  243.     end
  244.    
  245.     xPos = xPos + xDir
  246.     zPos = zPos + zDir
  247.     return true
  248. end
  249.  
  250. local function tryUp()
  251.     if not refuel() then
  252.         print( "Not enough Fuel" )
  253.         returnSupplies()
  254.     end
  255.    
  256.     tryDigUp()
  257.    
  258.     while not turtle.up() do
  259.         if not tryDigUp() then
  260.             return false
  261.         end
  262.     end
  263.    
  264.     depth = depth - 1
  265.     if math.fmod( depth, 10 ) == 0 then
  266.         print( "Descended "..depth.." metres." )
  267.     end
  268.     return true
  269. end
  270.  
  271. local function tryDown()
  272.     if not refuel() then
  273.         print( "Not enough Fuel" )
  274.         returnSupplies()
  275.     end
  276.    
  277.     tryDigDown()
  278.     while not turtle.down() do
  279.         if not tryDigDown() then
  280.             return false
  281.         end
  282.     end
  283.    
  284.     --print("Moved down one");
  285.    
  286.     depth = depth + 1  
  287.     if math.fmod( depth, 10 ) == 0 then
  288.         print( "Descended "..depth.." metres." )
  289.     end
  290.     return true
  291. end
  292.  
  293. local function turnLeft()
  294.     turtle.turnLeft()
  295.     xDir, zDir = -zDir, xDir
  296. end
  297.  
  298. local function turnRight()
  299.     turtle.turnRight()
  300.     xDir, zDir = zDir, -xDir
  301. end
  302.  
  303. local function placeTorch()
  304.     turtle.select(1)
  305.     turtle.placeDown()
  306. end
  307.  
  308. function goTo( x, y, z, xd, zd )
  309.     while depth > y do
  310.         if turtle.up() then
  311.             depth = depth - 1
  312.         elseif turtle.digUp() or turtle.attackUp() then
  313.             collect()
  314.         else
  315.             sleep( 0.5 )
  316.         end
  317.     end
  318.  
  319.     if xPos > x then
  320.         while xDir ~= -1 do
  321.             turnLeft()
  322.         end
  323.         while xPos > x do
  324.             if turtle.forward() then
  325.                 xPos = xPos - 1
  326.             elseif turtle.dig() or turtle.attack() then
  327.                 collect()
  328.             else
  329.                 sleep( 0.5 )
  330.             end
  331.         end
  332.     elseif xPos < x then
  333.         while xDir ~= 1 do
  334.             turnLeft()
  335.         end
  336.         while xPos < x do
  337.             if turtle.forward() then
  338.                 xPos = xPos + 1
  339.             elseif turtle.dig() or turtle.attack() then
  340.                 collect()
  341.             else
  342.                 sleep( 0.5 )
  343.             end
  344.         end
  345.     end
  346.    
  347.     if zPos > z then
  348.         while zDir ~= -1 do
  349.             turnLeft()
  350.         end
  351.         while zPos > z do
  352.             if turtle.forward() then
  353.                 zPos = zPos - 1
  354.             elseif turtle.dig() or turtle.attack() then
  355.                 collect()
  356.             else
  357.                 sleep( 0.5 )
  358.             end
  359.         end
  360.     elseif zPos < z then
  361.         while zDir ~= 1 do
  362.             turnLeft()
  363.         end
  364.         while zPos < z do
  365.             if turtle.forward() then
  366.                 zPos = zPos + 1
  367.             elseif turtle.dig() or turtle.attack() then
  368.                 collect()
  369.             else
  370.                 sleep( 0.5 )
  371.             end
  372.         end
  373.     end
  374.    
  375.     while depth < y do
  376.         if turtle.down() then
  377.             depth = depth + 1
  378.         elseif turtle.digDown() or turtle.attackDown() then
  379.             collect()
  380.         else
  381.             sleep( 0.5 )
  382.         end
  383.     end
  384.    
  385.     while zDir ~= zd or xDir ~= xd do
  386.         turnLeft()
  387.     end
  388. end
  389.  
  390. if not refuel() then
  391.     print( "Out of Fuel" )
  392.     return
  393. end
  394.  
  395. print( "Tunneling..." )
  396.  
  397. turtle.select(1)
  398.  
  399. local alternate = 0
  400.  
  401. if placeTorches then
  402.     while not torchCheck() do
  403.         print( "Waiting for torches" )
  404.         while not torchCheck() do
  405.             sleep(1)
  406.         end
  407.     end
  408. end
  409.  
  410. tryUp()
  411. tryDigUp()
  412.  
  413. for n=1,size do
  414.     if n<size then
  415.         tryForwards()
  416.         tryDigUp()
  417.         tryDigDown()
  418.         if placeTorches then
  419.             if math.fmod(n+9,10) == 0 then
  420.                 placeTorch()
  421.             end
  422.         end
  423.     else
  424.         print( "Tunnel complete." )
  425.     end
  426.  
  427. end
  428.  
  429. if placeTorches then
  430.     placeTorch()
  431. end
  432.  
  433.  
  434.  
  435. print( "Returning to start..." )
  436.  
  437. -- Return to where we started
  438. goTo( 0,0,0,0,-1 )
  439. unload()
  440. goTo( 0,0,0,0,1 )
  441.  
  442.  
  443.  
  444. print( "Mined "..(collected + unloaded).." items total." )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement