Advertisement
rocke97

Untitled

Feb 20th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 KB | None | 0 0
  1. local tArgs = { ... }
  2. if #tArgs ~= 1 then
  3. print( "Usage: tunnel <length>" )
  4. return
  5. end
  6.  
  7. -- Mine in a quarry pattern until we hit something we can't dig
  8. local length = tonumber( tArgs[1] )
  9. if length < 1 then
  10. print( "Tunnel length must be positive" )
  11. return
  12. end
  13.  
  14. local depth = 0
  15. local collected = 0
  16. local xPos = 0
  17. local yPos = 0
  18. local zPos = 0
  19. local xDir = 0
  20. local zDir = 1
  21. local goTo
  22.  
  23. local function collect()
  24. collected = collected + 1
  25. local bFull = true
  26. local nTotalItems = 0
  27. for n=1,16 do
  28. local nCount = turtle.getItemCount(n)
  29. if nCount == 0 then
  30. bFull = false
  31. end
  32. nTotalItems = nTotalItems + nCount
  33. end
  34.  
  35. if math.fmod(collected, 25) == 0 then
  36. print( "Mined "..collected.." items." )
  37. end
  38.  
  39. if (bFull) then
  40. returnSupplies(false)
  41. end
  42. end
  43.  
  44. local function unload( _bKeepOneFuelStack )
  45. print( "Unloading items..." )
  46. for n=1,16 do
  47. local nCount = turtle.getItemCount(n)
  48. if nCount > 0 then
  49. turtle.select(n)
  50. local bDrop = true
  51. if _bKeepOneFuelStack and turtle.refuel(0) then
  52. bDrop = false
  53. _bKeepOneFuelStack = false
  54. end
  55. if bDrop then
  56. turtle.drop()
  57. end
  58. end
  59. end
  60. collected = 0
  61. turtle.select(1)
  62. end
  63.  
  64. local function returnSupplies(stop)
  65. local x,y,z,xd,zd = xPos,yPos,zPos,xDir,zDir
  66. print( "Returning to surface..." )
  67. goTo( 0,0,0,0,-1 )
  68. unload( true )
  69. print( "Resuming mining..." )
  70. goTo( x,y,z,xd,zd )
  71. end
  72.  
  73. local function tryDig()
  74. while turtle.detect() do
  75. if turtle.dig() then
  76. collect()
  77. sleep(0.5)
  78. else
  79. return false
  80. end
  81. end
  82. return true
  83. end
  84.  
  85. local function tryDigUp()
  86. while turtle.detectUp() do
  87. if turtle.digUp() then
  88. collect()
  89. sleep(0.5)
  90. else
  91. return false
  92. end
  93. end
  94. return true
  95. end
  96.  
  97. local function tryDigDown()
  98. while turtle.detectDown() do
  99. if turtle.digDown() then
  100. collect()
  101. sleep(0.5)
  102. else
  103. return false
  104. end
  105. end
  106. return true
  107. end
  108.  
  109. local function refuel()
  110. local fuelLevel = turtle.getFuelLevel()
  111. if fuelLevel == "unlimited" or fuelLevel > 0 then
  112. return
  113. end
  114.  
  115. local function tryRefuel()
  116. for n=1,16 do
  117. if turtle.getItemCount(n) > 0 then
  118. turtle.select(n)
  119. if turtle.refuel(1) then
  120. turtle.select(1)
  121. return true
  122. end
  123. end
  124. end
  125. turtle.he(1)
  126. return false
  127. end
  128.  
  129. if not tryRefuel() then
  130. print( "Add more fuel to continue." )
  131. while not tryRefuel() do
  132. os.pullEvent( "turtle_inventory" )
  133. end
  134. print( "Resuming Tunnel." )
  135. end
  136. end
  137.  
  138. local function tryUp()
  139. refuel()
  140. while not turtle.up() do
  141. if turtle.detectUp() then
  142. if not tryDigUp() then
  143. return false
  144. end
  145. elseif turtle.attackUp() then
  146. collect()
  147. else
  148. sleep( 0.5 )
  149. end
  150. end
  151. return true
  152. end
  153.  
  154. local function tryDown()
  155. refuel()
  156. while not turtle.down() do
  157. if turtle.detectDown() then
  158. if not tryDigDown() then
  159. return false
  160. end
  161. elseif turtle.attackDown() then
  162. collect()
  163. else
  164. sleep( 0.5 )
  165. end
  166. end
  167. return true
  168. end
  169.  
  170. local function tryForward()
  171. refuel()
  172. while not turtle.forward() do
  173. if turtle.detect() then
  174. if not tryDig() then
  175. return false
  176. end
  177. elseif turtle.attack() then
  178. collect()
  179. else
  180. sleep( 0.5 )
  181. end
  182. end
  183. return true
  184. end
  185.  
  186. function goTo( x, y, z, xd, zd )
  187. print("found")
  188. while yPos < y do
  189. if turtle.up() then
  190. yPos = yPos + 1
  191. elseif turtle.digUp() or turtle.attackUp() then
  192. collect()
  193. else
  194. sleep( 0.5 )
  195. end
  196. end
  197.  
  198. if xPos > x then
  199. while xDir ~= -1 do
  200. print("loop 1")
  201. turtle.turnLeft()
  202. xDir, zDir = -zDir, xDir
  203. end
  204. while xPos > x do
  205. if turtle.forward() then
  206. xPos = xPos - 1
  207. elseif turtle.dig() or turtle.attack() then
  208. collect()
  209. else
  210. sleep( 0.5 )
  211. end
  212. end
  213. elseif xPos < x then
  214. while xDir ~= 1 do
  215. print("loop 2")
  216. turtle.turnLeft()
  217. xDir, zDir = -zDir, xDir
  218. end
  219. while xPos < x do
  220. if turtle.forward() then
  221. xPos = xPos + 1
  222. elseif turtle.dig() or turtle.attack() then
  223. collect()
  224. else
  225. sleep( 0.5 )
  226. end
  227. end
  228. end
  229.  
  230. if zPos > z then
  231. while zDir ~= -1 do
  232. print("loop 3")
  233. turtle.turnLeft()
  234. xDir, zDir = -zDir, xDir
  235. end
  236. while zPos > z do
  237. if turtle.forward() then
  238. zPos = zPos - 1
  239. elseif turtle.dig() or turtle.attack() then
  240. collect()
  241. else
  242. sleep( 0.5 )
  243. end
  244. end
  245. elseif zPos < z then
  246. while zDir ~= 1 do
  247. print("loop 4")
  248. turtle.turnLeft()
  249. xDir, zDir = -zDir, xDir
  250. end
  251. while zPos < z do
  252. if turtle.forward() then
  253. zPos = zPos + 1
  254. elseif turtle.dig() or turtle.attack() then
  255. collect()
  256. else
  257. sleep( 0.5 )
  258. end
  259. end
  260. end
  261.  
  262. while yPos > y do
  263. if turtle.down() then
  264. yPos = yPos - 1
  265. elseif turtle.digDown() or turtle.attackDown() then
  266. collect()
  267. else
  268. sleep( 0.5 )
  269. end
  270. end
  271.  
  272. while zDir ~= zd or xDir ~= xd do
  273. print("loop 5")
  274. turtle.turnLeft()
  275. xDir, zDir = -zDir, xDir
  276. end
  277. print ("goTo is done")
  278. end
  279.  
  280. print( "Tunnelling..." )
  281.  
  282. for n=1,length do
  283. turtle.placeDown()
  284. tryDigUp()
  285. turtle.turnLeft()
  286. xDir, zDir = -zDir, xDir
  287. tryDig()
  288. tryUp()
  289. yPos = yPos + 1
  290. tryDig()
  291. turtle.turnRight()
  292. xDir, zDir = zDir, -xDir
  293. turtle.turnRight()
  294. xDir, zDir = zDir, -xDir
  295. tryDig()
  296. tryDown()
  297. yPos = yPos - 1
  298. tryDig()
  299. turtle.turnLeft()
  300. xDir, zDir = -zDir, xDir
  301.  
  302. if n<length then
  303. tryDig()
  304. if not tryForward() then
  305. print( "Aborting Tunnel." )
  306. returnSupplies(true)
  307. goTo(0, 0, 0, 0, 0)
  308. break
  309. else
  310. xPos = xPos + 1
  311. end
  312. else
  313. print( "Tunnel complete." )
  314. returnSupplies(true)
  315. goTo(0, 0, 0, 0, 0)
  316. end
  317.  
  318. end
  319.  
  320. print( "Tunnel complete." )
  321. print( "Mined "..collected.." items total." )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement