Advertisement
IMarvinTPA

TallTunnel

Dec 28th, 2012
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.61 KB | None | 0 0
  1. --pastebin get JWrTxV1z MakeRoom
  2. --pastebin get SpEd82Jp StripLayer
  3. --pastebin get 7JMXTNu5 TallTunnel
  4. local tArgs = { ... }
  5.  
  6. if #tArgs ~= 2 then
  7.         print( "Usage: TallTunnel <length> <height>" )
  8.         print( "Slot 1 should be a non-wood floor block" )
  9.         print( "Slot 2 should be a fuel" )
  10.         print( "Slot 3 should be a stack of chests" )
  11.         print( "Slot 4 should be a stack of torches" )
  12.         return
  13. end
  14.  
  15. -- Mine in a quarry pattern until we hit something we can't dig
  16. local length = tonumber( tArgs[1] )
  17. local tall = tonumber( tArgs[2] )
  18.  
  19. if length < 1 then
  20.         print( "Tunnel length must be positive" )
  21.         return
  22. end
  23.  
  24. if tall < 1 then
  25.         print( "Tunnel height must be positive" )
  26.         return
  27. end
  28.        
  29. local depth = 0
  30. local collected = 0
  31. local unloaded = 0
  32.  
  33. local function collect()
  34.         collected = collected + 1
  35.         if math.fmod(collected, 25) == 0 then
  36.                 print( "Mined "..collected.." items." )
  37.         end
  38. end
  39.  
  40. local function isFull()
  41.         local bFull = true
  42.         local nTotalItems = 0
  43.         for n=1,16 do
  44.                 local nCount = turtle.getItemCount(n)
  45.                 if nCount == 0 then
  46.                         bFull = false
  47.                 end
  48.                 nTotalItems = nTotalItems + nCount
  49.         end
  50.              
  51.         if bFull then
  52.                 print( "No empty slots left." )
  53.                 return true
  54.         end
  55.         return false
  56. end
  57.  
  58. local function unload()
  59.         print( "Unloading items..." )
  60.         turtle.turnRight()
  61.         turtle.turnRight()
  62.         turtle.select(3)
  63.         turtle.place()
  64.         --1: Flooring
  65.         --2: Fuel
  66.         --3: Chests
  67.         --4: Torches
  68.         for n=5,16 do
  69.                 unloaded = unloaded + turtle.getItemCount(n)
  70.                 turtle.select(n)
  71.                 turtle.drop()
  72.         end
  73.         --collected = 0
  74.         turtle.select(1)
  75.         turtle.turnRight()
  76.         turtle.turnRight()     
  77. end
  78.  
  79.  
  80. local function tryDig()
  81.         while turtle.detect() do
  82.                 if turtle.dig() then
  83.                         collect()
  84.                         sleep(0.5)
  85.                 else
  86.                         return false
  87.                 end
  88.         end
  89.         return true
  90. end
  91.  
  92. local function tryDigUp()
  93.         while turtle.detectUp() do
  94.                 if turtle.digUp() then
  95.                         collect()
  96.                         sleep(0.5)
  97.                 else
  98.                         return false
  99.                 end
  100.         end
  101.         return true
  102. end
  103.  
  104. local function refuel()
  105.         local fuelLevel = turtle.getFuelLevel()
  106.         if fuelLevel == "unlimited" or fuelLevel > 0 then
  107.                 return
  108.         end
  109.        
  110.         local function tryRefuel()
  111.                 for n=2,16 do
  112.                     if n ~= 3 and n ~= 4 then
  113.                         if turtle.getItemCount(n) > 0 then
  114.                                 turtle.select(n)
  115.                                 if turtle.refuel(1) then
  116.                                         turtle.select(1)
  117.                                         return true
  118.                                 end
  119.                         end
  120.                     end
  121.                 end
  122.                 turtle.select(1)
  123.                 return false
  124.         end
  125.        
  126.         if not tryRefuel() then
  127.                 print( "Add more fuel to continue." )
  128.                 while not tryRefuel() do
  129.                         sleep(1)
  130.                 end
  131.                 print( "Resuming Tunnel." )
  132.         end
  133. end
  134.  
  135. local function tryUp()
  136.         refuel()
  137.         while not turtle.up() do
  138.                 if turtle.detectUp() then
  139.                         if not tryDigUp() then
  140.                                 return false
  141.                         end
  142.                 elseif turtle.attackUp() then
  143.                         collect()
  144.                 else
  145.                         sleep( 0.5 )
  146.                 end
  147.         end
  148.         return true
  149. end
  150.  
  151. local function tryDown()
  152.         refuel()
  153.         while not turtle.down() do
  154.                 if turtle.detectDown() then
  155.                         if not tryDigDown() then
  156.                                 return false
  157.                         end
  158.                 elseif turtle.attackDown() then
  159.                         collect()
  160.                 else
  161.                         sleep( 0.5 )
  162.                 end
  163.         end
  164.         return true
  165. end
  166.  
  167. local function tryForward()
  168.         refuel()
  169.         if isFull() then
  170.             unload()
  171.         end
  172.         while not turtle.forward() do
  173.                 if turtle.detect() then
  174.                         if not tryDig() then
  175.                                 return false
  176.                         end
  177.                 elseif turtle.attack() then
  178.                         collect()
  179.                 else
  180.                         sleep( 0.5 )
  181.                 end
  182.         end
  183.         return true
  184. end
  185.  
  186. local function hallway(hallLength)
  187.     for n=1,hallLength do
  188.         turtle.placeDown()
  189.         turtle.turnLeft()
  190.         tryDig()
  191.         for m=2, tall do
  192.             tryDigUp()
  193.             tryUp()
  194.             tryDig()
  195.         end
  196.         turtle.turnRight()
  197.         turtle.turnRight()
  198.         tryDig()
  199.         for m=2, tall do
  200.             tryDown()
  201.         tryDig()
  202.         end
  203.         if math.fmod(n, 6) == 0 then
  204.             turtle.select(4)
  205.             turtle.place()
  206.             turtle.select(1)
  207.         end
  208.         turtle.turnLeft()
  209.        
  210.         if n<hallLength then
  211.                 tryDig()
  212.                 if not tryForward() then
  213.                         return false
  214.                 end
  215.         else
  216.                 return true
  217.         end
  218.     end
  219. end
  220.  
  221. print( "Tunnelling..." )
  222.  
  223.  
  224. hallway(length)
  225.  
  226.  
  227. print( "Tunnel complete." )
  228. print( "Mined "..collected.." items total." )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement