Advertisement
rocke97

Untitled

Feb 20th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.73 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. turtle.turnLeft()
  189. turtle.turnLeft()
  190. while yPos < y do
  191. if turtle.up() then
  192. yPos = yPos + 1
  193. elseif turtle.digUp() or turtle.attackUp() then
  194. collect()
  195. else
  196. sleep( 0.5 )
  197. end
  198. end
  199.  
  200. if xPos > x then
  201. -- while xDir ~= -1 do
  202. -- print("loop 1")
  203. -- turtle.turnLeft()
  204. -- xDir, zDir = -zDir, xDir
  205. -- end
  206. while xPos > x do
  207. if turtle.forward() then
  208. xPos = xPos - 1
  209. elseif turtle.dig() or turtle.attack() then
  210. collect()
  211. else
  212. sleep( 0.5 )
  213. end
  214. end
  215. elseif xPos < x then
  216. -- while xDir ~= 1 do
  217. -- print("loop 2")
  218. -- turtle.turnLeft()
  219. -- xDir, zDir = -zDir, xDir
  220. -- end
  221. while xPos < x do
  222. if turtle.forward() then
  223. xPos = xPos + 1
  224. elseif turtle.dig() or turtle.attack() then
  225. collect()
  226. else
  227. sleep( 0.5 )
  228. end
  229. end
  230. end
  231.  
  232. -- if zPos > z then
  233. -- while zDir ~= -1 do
  234. -- print("loop 3")
  235. -- turtle.turnLeft()
  236. -- xDir, zDir = -zDir, xDir
  237. -- end
  238. -- while zPos > z do
  239. -- if turtle.forward() then
  240. -- zPos = zPos - 1
  241. -- elseif turtle.dig() or turtle.attack() then
  242. -- collect()
  243. -- else
  244. -- sleep( 0.5 )
  245. -- end
  246. -- end
  247. -- elseif zPos < z then
  248. -- while zDir ~= 1 do
  249. -- print("loop 4")
  250. -- turtle.turnLeft()
  251. -- xDir, zDir = -zDir, xDir
  252. -- end
  253. -- while zPos < z do
  254. -- if turtle.forward() then
  255. -- zPos = zPos + 1
  256. -- elseif turtle.dig() or turtle.attack() then
  257. -- collect()
  258. -- else
  259. -- sleep( 0.5 )
  260. -- end
  261. -- end
  262. -- end
  263.  
  264. while yPos > y do
  265. if turtle.down() then
  266. yPos = yPos - 1
  267. elseif turtle.digDown() or turtle.attackDown() then
  268. collect()
  269. else
  270. sleep( 0.5 )
  271. end
  272. end
  273.  
  274. -- while zDir ~= zd or xDir ~= xd do
  275. -- print("loop 5")
  276. -- turtle.turnLeft()
  277. -- xDir, zDir = -zDir, xDir
  278. -- end
  279. print ("goTo is done")
  280. end
  281.  
  282. print( "Tunnelling..." )
  283.  
  284. for n=1,length do
  285. turtle.placeDown()
  286. tryDigUp()
  287. turtle.turnLeft()
  288. xDir, zDir = -zDir, xDir
  289. tryDig()
  290. tryUp()
  291. yPos = yPos + 1
  292. tryDig()
  293. turtle.turnRight()
  294. xDir, zDir = zDir, -xDir
  295. turtle.turnRight()
  296. xDir, zDir = zDir, -xDir
  297. tryDig()
  298. tryDown()
  299. yPos = yPos - 1
  300. tryDig()
  301. turtle.turnLeft()
  302. xDir, zDir = -zDir, xDir
  303.  
  304. if n<length then
  305. tryDig()
  306. if not tryForward() then
  307. print( "Aborting Tunnel." )
  308. returnSupplies(true)
  309. turtle.turnLeft()
  310. turnLeft.turnLeft()
  311. break
  312. else
  313. xPos = xPos + 1
  314. end
  315. else
  316. print( "Tunnel complete." )
  317. returnSupplies(true)
  318. turtle.turnLeft()
  319. turtle.turnLeft()
  320. end
  321.  
  322. end
  323.  
  324. print( "Tunnel complete." )
  325. print( "Mined "..collected.." items total." )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement