Advertisement
rocke97

Untitled

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