Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1.  
  2. local tArgs = { ... }
  3. if #tArgs ~= 1 then
  4. print( "Usage: tunnel <length>" )
  5. return
  6. end
  7.  
  8. -- Mine in a quarry pattern until we hit something we can't dig
  9. local length = tonumber( tArgs[1] )
  10. if length < 1 then
  11. print( "Tunnel length must be positive" )
  12. return
  13. end
  14.  
  15. local depth = 0
  16. local collected = 0
  17. local data = turtle.getItemDetail(2)
  18. local torchlen = 0;
  19.  
  20. local function collect()
  21. collected = collected + 1
  22. if math.fmod(collected, 25) == 0 then
  23. print( "Mined "..collected.." items." )
  24. end
  25. end
  26.  
  27. local function tryDig()
  28. while turtle.detect() do
  29. if turtle.dig() then
  30. collect()
  31. sleep(0.5)
  32. else
  33. return false
  34. end
  35. end
  36. return true
  37. end
  38.  
  39. local function tryDigUp()
  40. while turtle.detectUp() do
  41. if turtle.digUp() then
  42. collect()
  43. sleep(0.5)
  44. else
  45. return false
  46. end
  47. end
  48. return true
  49. end
  50.  
  51. local function tryDigDown()
  52. while turtle.detectDown() do
  53. if turtle.digDown() then
  54. collect()
  55. sleep(0.5)
  56. else
  57. return false
  58. end
  59. end
  60. return true
  61. end
  62.  
  63. local function refuel()
  64. local fuelLevel = turtle.getFuelLevel()
  65. if fuelLevel == "unlimited" or fuelLevel > 0 then
  66. return
  67. end
  68.  
  69. local function tryRefuel()
  70. for n=1,16 do
  71. if turtle.getItemCount(n) > 0 then
  72. turtle.select(n)
  73. if turtle.refuel(1) then
  74. turtle.select(1)
  75. return true
  76. end
  77. end
  78. end
  79. turtle.select(1)
  80. return false
  81. end
  82.  
  83. if not tryRefuel() then
  84. print( "Add more fuel to continue." )
  85. while not tryRefuel() do
  86. os.pullEvent( "turtle_inventory" )
  87. end
  88. print( "Resuming Tunnel." )
  89. end
  90. end
  91.  
  92. local function tryUp()
  93. refuel()
  94. while not turtle.up() do
  95. if turtle.detectUp() then
  96. if not tryDigUp() then
  97. return false
  98. end
  99. elseif turtle.attackUp() then
  100. collect()
  101. else
  102. sleep( 0.5 )
  103. end
  104. end
  105. return true
  106. end
  107.  
  108. local function tryDown()
  109. refuel()
  110. while not turtle.down() do
  111. if turtle.detectDown() then
  112. if not tryDigDown() then
  113. return false
  114. end
  115. elseif turtle.attackDown() then
  116. collect()
  117. else
  118. sleep( 0.5 )
  119. end
  120. end
  121. return true
  122. end
  123.  
  124. local function tryForward()
  125. refuel()
  126. while not turtle.forward() do
  127. if turtle.detect() then
  128. if not tryDig() then
  129. return false
  130. end
  131. elseif turtle.attack() then
  132. collect()
  133. else
  134. sleep( 0.5 )
  135. end
  136. end
  137. return true
  138. end
  139.  
  140. print( "Tunnelling..." )
  141.  
  142. for n=1,length do
  143. turtle.placeDown()
  144. tryDigUp()
  145. turtle.turnLeft()
  146. tryDig()
  147. tryUp()
  148. tryDig()
  149. turtle.turnRight()
  150. turtle.turnRight()
  151. tryDig()
  152. tryDown()
  153. tryDig()
  154. turtle.turnLeft()
  155. data = turtle.getItemDetail(2)
  156. torchlen = torchlen + 1
  157. if data.name == "minecraft:cobblestone" then
  158. if data.count == 61 then
  159. turtle.select(2)
  160. turtle.drop(60)
  161. turtle.select(1)
  162. end
  163. end
  164. if torchlen == 10 then
  165. turtle.select(16)
  166. turtle.turnLeft()
  167. turtle.turnLeft()
  168. turtle.place()
  169. turtle.turnLeft()
  170. turtle.turnLeft()
  171. turtle.select(1)
  172. torchlen = 0
  173. end
  174.  
  175. if n<length then
  176. tryDig()
  177. if not tryForward() then
  178. print( "Aborting Tunnel." )
  179. break
  180. end
  181. else
  182. print( "Tunnel complete." )
  183. end
  184.  
  185. end
  186.  
  187. --[[
  188. print( "Returning to start..." )
  189.  
  190. -- Return to where we started
  191. turtle.turnLeft()
  192. turtle.turnLeft()
  193. while depth > 0 do
  194. if turtle.forward() then
  195. depth = depth - 1
  196. else
  197. turtle.dig()
  198. end
  199. end
  200. turtle.turnRight()
  201. turtle.turnRight()
  202. ]]
  203.  
  204. print( "Tunnel complete." )
  205. print( "Mined "..collected.." items total." )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement