lost_RD

tunnel 3x3 placing torches

Dec 23rd, 2012
739
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.36 KB | None | 0 0
  1. local tArgs = { ... }
  2. if not (#tArgs>0 and #tArgs<3) then
  3.   print( "Usage: tunnel <length> <distance (optional)>" )
  4.   return
  5. end
  6.  
  7. local length = tonumber( tArgs[1] )
  8. if length < 1 then
  9.   print( "Tunnel length must be positive" )
  10.   return
  11. end
  12.  
  13. local destination = tonumber( tArgs[2] )
  14. if destination < 0 then
  15.   print("Assuming 0")
  16.   destination = 0
  17. end
  18.  
  19. print("Moving " .. destination .. " blocks to destination...")
  20. n=0
  21. while n < destination do
  22.   --print("Destination = "..destination)
  23.   if not turtle.forward() then
  24.     destination = n
  25.     print("Destination = "..destination)
  26.     break
  27.   else
  28.     n = n + 1
  29.   end
  30. end
  31.  
  32. local depth = destination
  33. local collected = 0
  34.  
  35. local function collect()
  36.   collected = collected + 1
  37.   if math.fmod(collected, 64) == 0 then
  38.     print( "Mined "  ..  collected/64  ..  " stacks." )
  39.  end
  40. end
  41.  
  42. local function tryDig()
  43.   while turtle.dig() do
  44.     collect()
  45.     sleep(0.5)
  46.     if not turtle.detect() then
  47.       return true
  48.     end
  49.   end
  50.   return not turtle.detect()
  51. end
  52.  
  53. local function tryForward()
  54.   while not turtle.forward() do
  55.     turtle.dig()
  56.     collect()
  57.     sleep(0.5)
  58.   end
  59. end
  60.  
  61. local function tryDigUp()
  62.   while turtle.digUp() do
  63.     collect()
  64.     sleep(0.5)
  65.     if not turtle.detectUp() then
  66.       return true
  67.     end
  68.   end
  69.   return not turtle.detectUp()
  70. end
  71.  
  72. local function tryDigDown()
  73.   while turtle.digDown() do
  74.     collect()
  75.     sleep(0.5)
  76.     if not turtle.detectDown() then
  77.       return true
  78.     end
  79.   end
  80.   return not turtle.detectDown()
  81. end
  82.  
  83. local function storeItems(a)
  84.   print("Distance to chest: "..depth)
  85.   turtle.turnLeft()
  86.   turtle.turnLeft()
  87.   for i = 1,a do
  88.     turtle.forward()
  89.   end
  90.   print("Storing items...")
  91.   for i = 2,15 do
  92.     turtle.select(i)
  93.     turtle.drop()
  94.   end
  95.   print("Returning to end of tunnel...")
  96.   turtle.turnRight()
  97.   turtle.turnRight()
  98.   for i = 1,a do
  99.     turtle.forward()
  100.   end
  101. end
  102.  
  103. local function returnHome(depth)
  104.   print("Returning home, "..depth.." blocks to travel...")
  105.   turtle.turnLeft()
  106.   turtle.turnLeft()
  107.   while depth > 0 do
  108.     if turtle.forward() then
  109.       depth = depth - 1
  110.     else
  111.       turtle.dig()
  112.     end
  113.   end
  114.   print("Storing items...")
  115.   for i = 2,15 do
  116.     turtle.select(i)
  117.     turtle.drop()
  118.   end
  119.   turtle.select(1)
  120.   turtle.turnRight()
  121.   turtle.turnRight()
  122. end
  123.  
  124. local function returnForFuel(b)
  125.   print("Distance to chest: "..depth)
  126.   turtle.turnLeft()
  127.   turtle.turnLeft()
  128.   for i = 1,b do
  129.     turtle.forward()
  130.   end
  131.   print("Awaiting fuel...")
  132.   turtle.select(2)
  133.   while turtle.getFuelLevel() < 2*depth do
  134.     for i=2,15 do
  135.       turtle.select(i)
  136.       turtle.refuel(turtle.getItemCount())
  137.     end
  138.     sleep(10)
  139.   end
  140.   print("Returning to end of tunnel...")
  141.   turtle.turnRight()
  142.   turtle.turnRight()
  143.   for i = 1,b do
  144.     turtle.forward()
  145.   end
  146. end
  147.  
  148. local function placeBlockDown()
  149.   turtle.select(1)
  150.   turtle.placeDown()
  151. end
  152.  
  153. local function placeTorch(d)
  154.   if d%5 == 0 then
  155.     turtle.select(16)
  156.     turtle.place()
  157.   end
  158. end
  159.  
  160. print( "Tunnelling..." )
  161.  
  162. local progressMarker = 10
  163. while depth < destination + length do
  164.   if turtle.getFuelLevel() < 2*depth then
  165.     returnForFuel(depth)
  166.   end
  167.   -- Advance
  168.   tryDig()
  169.   if not turtle.forward() then
  170.     print( "Aborting Tunnel, could not dig" )
  171.     break
  172.   else
  173.     depth = depth + 1
  174.     progress = math.ceil(((depth-destination)*100)/length)
  175.     if progress > progressMarker then
  176.       print("Depth: "..depth..", "..progress.."%")
  177.       progressMarker = progressMarker + 10
  178.     end
  179.   end
  180.   -- Dig sequence
  181.   tryDigUp()
  182.   turtle.turnLeft()
  183.   tryDig()
  184.   turtle.forward()
  185.   turtle.turnRight()
  186.   turtle.turnRight()
  187.   tryDigUp()
  188.   tryDig()
  189.   turtle.up()
  190.   tryDigUp()
  191.   tryDig()
  192.   turtle.forward()
  193.   tryDigUp()
  194.   tryDigDown()
  195.   tryDig()
  196.   turtle.forward()
  197.   tryDigUp()
  198.   tryDigDown()
  199.   turtle.down()
  200.   placeBlockDown()
  201.   turtle.back()
  202.   placeTorch(depth-destination)
  203.   turtle.turnLeft()
  204.   -- Check for full
  205.   local checkingSlots = true
  206.   local slotsOccupied = 1
  207.   while checkingSlots == true do
  208.     if turtle.getItemCount(slotsOccupied+1) > 0 then
  209.       slotsOccupied = slotsOccupied + 1
  210.     else
  211.       checkingSlots = false
  212.     end
  213.     if slotsOccupied > 14 then
  214.       checkingSlots = false
  215.       storeItems(depth)
  216.     end
  217.   end
  218. end
  219.  
  220. returnHome(depth)
  221.  
  222. print( "Mined "..collected.." blocks total." )
Advertisement
Add Comment
Please, Sign In to add comment