nSun

tunnel2

Mar 24th, 2014
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. term.clear()
  2.  
  3. local lengthSet = false
  4. local bridgeSet = false
  5. local input
  6. local length
  7. local bridge
  8.  
  9. print("Profondeur ? (int)")
  10. while lengthSet == false do
  11. input = io.read()
  12. input = tonumber( input )
  13. if input > 0 then
  14. length = tonumber( input )
  15. lengthSet = true
  16. else
  17. print("La profondeur doit ĂȘtre un entier non nul")
  18. end
  19. end
  20.  
  21. print("Pont ? (Y/N)")
  22. while bridgeSet == false do
  23. input = io.read()
  24. if input == "Y" or input == "y" then
  25. bridge = true
  26. bridgeSet = true
  27. elseif input == "N" or input == "n" then
  28. bridge = false
  29. bridgeSet = true
  30. else
  31. print("Oui ou non (Y/N)")
  32. end
  33. end
  34.  
  35. local depth = 0
  36. local collected = 0
  37. local steps = 0
  38.  
  39. local function collect()
  40. collected = collected + 1
  41. if math.fmod(collected, 1) == 0 then
  42. term.clear()
  43. term.setCursorPos(1,1)
  44. print( "Mined "..collected.." items." )
  45. print("Steps so far :" .. steps)
  46. end
  47. end
  48.  
  49. local function tryDig()
  50. while turtle.detect() do
  51. if turtle.dig() then
  52. collect()
  53. sleep(0.3)
  54. else
  55. return false
  56. end
  57. end
  58. return true
  59. end
  60.  
  61. local function tryDigUp()
  62. while turtle.detectUp() do
  63. if turtle.digUp() then
  64. collect()
  65. sleep(0.3)
  66. else
  67. return false
  68. end
  69. end
  70. return true
  71. end
  72.  
  73. local function refuel()
  74. local fuelLevel = turtle.getFuelLevel()
  75. if fuelLevel == "unlimited" or fuelLevel > 0 then
  76. return
  77. end
  78.  
  79. local function tryRefuel()
  80. for n=1,16 do
  81. if turtle.getItemCount(n) > 0 then
  82. turtle.select(n)
  83. if turtle.refuel(1) then
  84. turtle.select(1)
  85. return true
  86. end
  87. end
  88. end
  89. turtle.select(1)
  90. return false
  91. end
  92.  
  93. if not tryRefuel() then
  94. print( "Add more fuel to continue." )
  95. while not tryRefuel() do
  96. sleep(1)
  97. end
  98. print( "Resuming Tunnel." )
  99. end
  100. end
  101.  
  102. local function tryUp()
  103. refuel()
  104. while not turtle.up() do
  105. if turtle.detectUp() then
  106. if not tryDigUp() then
  107. return false
  108. end
  109. elseif turtle.attackUp() then
  110. collect()
  111. else
  112. sleep( 0.3 )
  113. end
  114. end
  115. return true
  116. end
  117.  
  118. local function tryDown()
  119. refuel()
  120. while not turtle.down() do
  121. if turtle.detectDown() then
  122. if not tryDigDown() then
  123. return false
  124. end
  125. elseif turtle.attackDown() then
  126. collect()
  127. else
  128. sleep( 0.3 )
  129. end
  130. end
  131. return true
  132. end
  133.  
  134. local function tryForward()
  135. refuel()
  136. while not turtle.forward() do
  137. if turtle.detect() then
  138. if not tryDig() then
  139. return false
  140. end
  141. elseif turtle.attack() then
  142. collect()
  143. else
  144. sleep( 0.3 )
  145. end
  146. end
  147. steps = steps +1
  148. return true
  149. end
  150.  
  151. print( "Tunnelling..." )
  152.  
  153. for n=1,length do
  154. if bridge then
  155. turtle.placeDown()
  156. end
  157. tryDigUp()
  158. turtle.turnLeft()
  159. tryDig()
  160. tryUp()
  161. tryDig()
  162. turtle.turnRight()
  163. turtle.turnRight()
  164. tryDig()
  165. tryDown()
  166. tryDig()
  167. turtle.turnLeft()
  168.  
  169. if n<length then
  170. tryDig()
  171. if not tryForward() then
  172. print( "Aborting Tunnel." )
  173. break
  174. end
  175. else
  176. print( "Tunnel complete." )
  177. end
  178.  
  179. end
  180.  
  181. --[[
  182. print( "Returning to start..." )
  183.  
  184. -- Return to where we started
  185. turtle.turnLeft()
  186. turtle.turnLeft()
  187. while depth > 0 do
  188. if turtle.forward() then
  189. depth = depth - 1
  190. else
  191. turtle.dig()
  192. end
  193. end
  194. turtle.turnRight()
  195. turtle.turnRight()
  196. ]]
  197.  
  198. print( "Tunnel complete." )
  199. print( "Mined "..collected.." items total." )
  200. print("Steps done in total :" .. steps)
Advertisement
Add Comment
Please, Sign In to add comment