677Kbeezy

677Kbeezy

Mar 31st, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. -- Arguements and Arg Checking
  2. local tArgs = { ... }
  3. if #tArgs ~= 1 then
  4. print( "Usage: tunnel <length>" )
  5. return
  6. end
  7. local length = tonumber( tArgs[1] )
  8. if length < 1 then
  9. print( "Tunnel length must be positive" )
  10. return
  11. end
  12.  
  13. -- Collected block number / Increaser
  14. local collected = 0
  15. local function collect()
  16. collected = collected + 1
  17. if math.fmod(collected, 25) == 0 then
  18. print( "Mined "..collected.." blocks." )
  19. end
  20. end
  21.  
  22.  
  23. -- Digging functions
  24. local function tryDig()
  25. while turtle.dig() do
  26. collect()
  27. sleep(0.5)
  28. if not turtle.detect() then
  29. return true
  30. end
  31. end
  32. return not turtle.detect()
  33. end
  34.  
  35. local function tryDigUp()
  36. while turtle.digUp() do
  37. collect()
  38. sleep(0.5)
  39. if not turtle.detectUp() then
  40. return true
  41. end
  42. end
  43. return not turtle.detectUp()
  44. end
  45.  
  46. -- Functions for dropping off items into a chest from slot 1.
  47. local function depositChest()
  48. turtle.up()
  49. turtle.turnRight()
  50. turtle.forward()
  51. turtle.select(1)
  52. turtle.placeDown()
  53. sleep(0.5)
  54. for slot=2,16 do
  55. turtle.select(slot)
  56. turtle.dropDown(turtle.getItemCount(slot))
  57. sleep(0.5)
  58. end
  59. turtle.select(1)
  60. end
  61.  
  62. local function previousState()
  63. turtle.turnLeft()
  64. sleep(0.5)
  65. turtle.turnLeft()
  66. sleep(0.5)
  67. turtle.forward()
  68. sleep(0.5)
  69. turtle.down()
  70. sleep(0.5)
  71. turtle.turnRight()
  72. end
  73.  
  74. local function checkSpace()
  75. local space = 0
  76. for slot=2,16 do
  77. space = space + turtle.getItemSpace(slot)
  78. end
  79. if space == 0 then
  80. depositChest()
  81. previousState()
  82. return false
  83. else
  84. return true
  85. end
  86. end
  87.  
  88. -- Main Program for digging the tunnel
  89. print( "Tunnelling..." )
  90. for n=1,length do
  91. turtle.select(2)
  92. turtle.placeDown()
  93. if not checkSpace() then break end
  94. tryDigUp()
  95. turtle.turnLeft()
  96. tryDig()
  97. turtle.up()
  98. tryDig()
  99. tryDigUp()
  100. turtle.up()
  101. tryDig()
  102. turtle.turnRight()
  103. turtle.turnRight()
  104. tryDig()
  105. turtle.down()
  106. tryDig()
  107. turtle.down()
  108. tryDig()
  109. turtle.turnLeft()
  110.  
  111. if n<length then
  112. tryDig()
  113. if not turtle.forward() then
  114. print( "Aborting Tunnel." )
  115. break
  116. end
  117. else
  118. print( "Tunnel complete." )
  119. end
  120. end
Add Comment
Please, Sign In to add comment