rocke97

Untitled

Feb 20th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.35 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 = 0
  21. local goTo -- Filled in further down
  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. unloaded = unloaded + nCount
  58. end
  59. end
  60. end
  61. collected = 0
  62. turtle.select(1)
  63. end
  64.  
  65. local function returnSupplies(stop)
  66. local x,y,z,xd,zd = xPos,yPos,zPos,xDir,zDir
  67. print( "Returning to surface..." )
  68. goTo( 0,0,0,0,-1 )
  69.  
  70. local fuelNeeded = 2*(x+y+z) + 1
  71. if not refuel( fuelNeeded ) or not stop then
  72. unload( true )
  73. print( "Waiting for fuel" )
  74. while not refuel( fuelNeeded ) do
  75. os.pullEvent( "turtle_inventory" )
  76. end
  77. else
  78. unload( true )
  79. end
  80.  
  81. print( "Resuming mining..." )
  82. goTo( x,y,z,xd,zd )
  83. end
  84.  
  85. local function tryDig()
  86. while turtle.detect() do
  87. if turtle.dig() 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 tryDigUp()
  98. while turtle.detectUp() do
  99. if turtle.digUp() 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 tryDigDown()
  110. while turtle.detectDown() do
  111. if turtle.digDown() then
  112. collect()
  113. sleep(0.5)
  114. else
  115. return false
  116. end
  117. end
  118. return true
  119. end
  120.  
  121. local function refuel()
  122. local fuelLevel = turtle.getFuelLevel()
  123. if fuelLevel == "unlimited" or fuelLevel > 0 then
  124. return
  125. end
  126.  
  127. local function tryRefuel()
  128. for n=1,16 do
  129. if turtle.getItemCount(n) > 0 then
  130. turtle.select(n)
  131. if turtle.refuel(1) then
  132. turtle.select(1)
  133. return true
  134. end
  135. end
  136. end
  137. turtle.he(1)
  138. return false
  139. end
  140.  
  141. if not tryRefuel() then
  142. print( "Add more fuel to continue." )
  143. while not tryRefuel() do
  144. os.pullEvent( "turtle_inventory" )
  145. end
  146. print( "Resuming Tunnel." )
  147. end
  148. end
  149.  
  150. local function tryUp()
  151. refuel()
  152. while not turtle.up() do
  153. if turtle.detectUp() then
  154. if not tryDigUp() then
  155. return false
  156. end
  157. elseif turtle.attackUp() then
  158. collect()
  159. else
  160. sleep( 0.5 )
  161. end
  162. end
  163. return true
  164. end
  165.  
  166. local function tryDown()
  167. refuel()
  168. while not turtle.down() do
  169. if turtle.detectDown() then
  170. if not tryDigDown() then
  171. return false
  172. end
  173. elseif turtle.attackDown() then
  174. collect()
  175. else
  176. sleep( 0.5 )
  177. end
  178. end
  179. return true
  180. end
  181.  
  182. local function tryForward()
  183. refuel()
  184. while not turtle.forward() do
  185. if turtle.detect() then
  186. if not tryDig() then
  187. return false
  188. end
  189. elseif turtle.attack() then
  190. collect()
  191. else
  192. sleep( 0.5 )
  193. end
  194. end
  195. return true
  196. end
  197.  
  198. function goTo( x, y, z, xd, zd )
  199. while depth > y do
  200. if turtle.up() then
  201. depth = depth - 1
  202. elseif turtle.digUp() or turtle.attackUp() then
  203. collect()
  204. else
  205. sleep( 0.5 )
  206. end
  207. end
  208.  
  209. if xPos > x then
  210. while xDir ~= -1 do
  211. turnLeft()
  212. end
  213. while xPos > x do
  214. if turtle.forward() then
  215. xPos = xPos - 1
  216. elseif turtle.dig() or turtle.attack() then
  217. collect()
  218. else
  219. sleep( 0.5 )
  220. end
  221. end
  222. elseif xPos < x then
  223. while xDir ~= 1 do
  224. turnLeft()
  225. end
  226. while xPos < x do
  227. if turtle.forward() then
  228. xPos = xPos + 1
  229. elseif turtle.dig() or turtle.attack() then
  230. collect()
  231. else
  232. sleep( 0.5 )
  233. end
  234. end
  235. end
  236.  
  237. if zPos > z then
  238. while zDir ~= -1 do
  239. turnLeft()
  240. end
  241. while zPos > z do
  242. if turtle.forward() then
  243. zPos = zPos - 1
  244. elseif turtle.dig() or turtle.attack() then
  245. collect()
  246. else
  247. sleep( 0.5 )
  248. end
  249. end
  250. elseif zPos < z then
  251. while zDir ~= 1 do
  252. turnLeft()
  253. end
  254. while zPos < z do
  255. if turtle.forward() then
  256. zPos = zPos + 1
  257. elseif turtle.dig() or turtle.attack() then
  258. collect()
  259. else
  260. sleep( 0.5 )
  261. end
  262. end
  263. end
  264.  
  265. while depth < y do
  266. if turtle.down() then
  267. depth = depth + 1
  268. elseif turtle.digDown() or turtle.attackDown() then
  269. collect()
  270. else
  271. sleep( 0.5 )
  272. end
  273. end
  274.  
  275. while zDir ~= zd or xDir ~= xd do
  276. turnLeft()
  277. end
  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. end
  310. else
  311. print( "Tunnel complete." )
  312. returnSupplies(true)
  313. goTo(0, 0, 0, 0, 0)
  314. end
  315.  
  316. end
  317.  
  318. print( "Tunnel complete." )
  319. print( "Mined "..collected.." items total." )
Advertisement
Add Comment
Please, Sign In to add comment