thaile4ever

tunnel2

Dec 28th, 2012
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. local targs = { ... }
  2.  
  3. if #targs ~= 1 then
  4. print( "Usage: tunnel <distance>" )
  5. return
  6. end
  7.  
  8. local digFor = tonumber( targs[1] )
  9.  
  10. if digFor < 1 then
  11. print("Excavate distance must be positive" )
  12. end
  13.  
  14. local distance = 0;
  15.  
  16. local torchCount = turtle.getItemCount(1)
  17. if torchCount == 0 then
  18. print("Please place torches in first slot")
  19. end
  20.  
  21. local height = 0
  22. local direction = 0
  23.  
  24. local function checkFuel()
  25. local fuelNeeded = (distance*2) + (digFor-distance)*6 + 1
  26. if (turtle.getFuelLevel() < fuelNeeded)
  27. print("Need more fuel")
  28. return
  29. end
  30. end
  31.  
  32. local function full()
  33. for n=2, 16 do
  34. if nCount == 0 then
  35. return false
  36. end
  37. end
  38. return true
  39. end
  40.  
  41. local function dumpItems()
  42. for n=2,16 do
  43. turtle.select(n)
  44. turtle.drop()
  45. end
  46. turtle.select(1)
  47. end
  48.  
  49. local function returnCargo()
  50. if direction = 1 then
  51. turtle.turnLeft()
  52. elseif direction = 2 then
  53. turtle.turnRight()
  54. else
  55. turtle.turnLeft()
  56. turtle.turnLeft()
  57. end
  58.  
  59. for h=0, height do
  60. turtle.down()
  61. end
  62.  
  63. for n=0, distance do
  64. turtle.forward()
  65. end
  66.  
  67. dumpItems()
  68.  
  69.  
  70. turtle.turnLeft()
  71. turtle.turnLeft()
  72.  
  73. checkFuel()
  74.  
  75. for n=0, distance do
  76. turtle.forward()
  77. end
  78. for h=0, height do
  79. turtle.up()
  80. end
  81.  
  82. if direction = 1 then
  83. turtle.turnLeft()
  84. elseif direction = 2 then
  85. turtle.turnRight()
  86. end
  87. end
  88.  
  89.  
  90. local function digRow()
  91. while turtle.detect() then
  92. if turtle.dig() then
  93. if full() then
  94. returnCargo()
  95. end
  96. end
  97. end
  98.  
  99. turtle.turnLeft()
  100. direction = 1
  101.  
  102. while turtle.detect() then
  103. if turtle.dig() then
  104. if full() then
  105. returnCargo()
  106. end
  107. end
  108. end
  109.  
  110. turtle.turnRight()
  111. turtle.turnRight()
  112. direction = 2
  113.  
  114. while turtle.detect() then
  115. if turtle.dig() then
  116. if full() then
  117. returnCargo()
  118. end
  119. end
  120. end
  121.  
  122. turtle.turnLeft()
  123. direction = 0
  124. end
  125.  
  126. local function tryUp()
  127.  
  128. end
  129.  
  130. local function tryForward()
  131. while not turtle.forward() do -- if we can't more forward
  132. if turtle.detect() then -- check if something there
  133. if turtle.dig() then -- attempt to dig
  134. if full() then
  135. returnCargo()
  136. end
  137. else
  138. return false
  139. end
  140. elseif turtle.attack() then
  141. if full() then
  142. returnCargo()
  143. end
  144. else
  145. sleep( 1 )
  146. end
  147. end
  148.  
  149. distance = distance + 1
  150. return true
  151. end
  152.  
  153. local function tryUp()
  154. while not turtle.up() do -- if we can't more forward
  155. if turtle.detect() then -- check if something there
  156. if turtle.digUp() then -- attempt to dig
  157. if full() then
  158. returnCargo()
  159. end
  160. else
  161. return false
  162. end
  163. elseif turtle.attack() then
  164. if full() then
  165. returnCargo()
  166. end
  167. else
  168. sleep( 1 )
  169. end
  170. end
  171.  
  172. height = height + 1
  173. return true
  174. end
  175.  
  176.  
  177. checkFuel() -- make sure we have fuel
  178.  
  179. local done = false
  180. while not done do
  181. for n=1, digFor do
  182. if not tryForward() then
  183. done = true
  184. break
  185. end
  186.  
  187. digRow()
  188.  
  189. if not tryUp() then
  190. done = true
  191. break
  192. end
  193.  
  194. digRow()
  195.  
  196.  
  197. if not tryUp() then
  198. done = true
  199. break
  200. end
  201.  
  202. digRow()
  203.  
  204. turtle.down()
  205. turtle.down()
  206. height = 0
  207. end
  208.  
  209. print( "Done Digging" )
  210.  
  211. turtle.turnLeft()
  212. turtle.turnLeft()
  213. for n=0, distance do
  214. turtle.forward()
  215. end
  216.  
  217. dumpItems()
Advertisement
Add Comment
Please, Sign In to add comment