MrCervelo

dig tunnel

Jan 24th, 2013
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.15 KB | None | 0 0
  1. function hasTorches()
  2.   if ( turtle.getItemCount(16) > 0 ) then
  3.     return true
  4.   else
  5.     return false
  6.   end
  7. end
  8.  
  9. function invFull()
  10.   full=true
  11.   for i=1,16 do
  12.     full = ( turtle.getItemCount(i) > 0 and full )
  13.   end
  14.   return full
  15. end
  16.  
  17. function checkInv()
  18.   if ( invFull() ) then
  19.     print("Inventory Full, Please Empty")
  20.     while ( invFull() ) do
  21.       sleep(1)
  22.     end
  23.   end
  24. end
  25.  
  26. function moveUp()
  27.   while ( not turtle.up() ) do
  28.     turtle.digUp()
  29.     checkInv()
  30.   end
  31. end
  32.  
  33. function moveDown()
  34.   while ( not turtle.down() ) do
  35.     turtle.digDown()
  36.     checkInv()
  37.   end
  38. end
  39.  
  40. function moveForward()
  41.   while ( not turtle.forward() ) do
  42.     turtle.dig()
  43.     checkInv()
  44.   end
  45. end
  46.  
  47. function digForward()
  48.   while ( turtle.detect() ) do
  49.     turtle.dig()
  50.     checkInv()
  51.   end
  52. end
  53.  
  54. function rotate()
  55.   for i=1,2 do
  56.     turtle.turnLeft()
  57.   end
  58. end
  59.  
  60. function digTunnel(height, putTorch)
  61.   for i=1,height-1 do
  62.     checkFuel()
  63.     digForward()
  64.     moveUp()
  65.     if ( height-1 == i and putTorch ) then
  66.       placeTorch()
  67.     end
  68.   end
  69.   moveForward()
  70.   for i=1,height-1 do
  71.     checkFuel()
  72.     digForward()
  73.     moveDown()
  74.   end
  75.   moveForward()
  76. end
  77.  
  78. function placeTorch()
  79.   if ( not hasTorches() ) then
  80.     print("No More Torches, Please replenish")
  81.     while ( not hasTorches() ) do
  82.       sleep(1)
  83.     end
  84.   end
  85.   turtle.select(16)
  86.   turtle.placeDown()
  87.   turtle.select(1)
  88. end
  89.  
  90. function checkFuel()
  91.   if ( turtle.getFuelLevel() < 10 ) then
  92.     print("Fuel Level is less than 10, please place fuel in slot 1")
  93.     turtle.select(1)
  94.     while ( turtle.getFuelLevel() < 10 ) do
  95.       if ( turtle.getItemCount(1) > 0 ) then
  96.         turtle.refuel()
  97.       end
  98.       sleep(1)
  99.     end
  100.     print("Turtle Refuled, Fuel Level = " .. turtle.getFuelLevel())
  101.   end
  102. end
  103.  
  104. args = { ... }
  105. tunnelHeight=tonumber(args[1])
  106. tunnelLength=tonumber(args[2])
  107. for i=1,tunnelLength/2 do
  108.   if ( i % 3 == 1 ) then
  109.     digTunnel(tunnelHeight, true)
  110.   else
  111.     digTunnel(tunnelHeight, false)
  112.   end
  113.   if ( i % 10 == 0 ) then
  114.     output=string.format("%.2f%% of the Tunnel Completed.", i/(tunnelLength/2)*100)
  115.     print(output)
  116.   end
  117. end
Advertisement
Add Comment
Please, Sign In to add comment