Advertisement
kk258966

tunnel

Apr 7th, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.39 KB | None | 0 0
  1. local tArgs = { ... }
  2. if #tArgs ~= 2 then
  3. print( "Usage: t <length> <place torches y/n>" )
  4.  
  5. return
  6. end
  7.  
  8. -- Mine 3x1 tunnel until we hit end
  9. local size = tonumber( tArgs[1] )
  10. if size < 1 then
  11. print( "Excavate diameter must be positive" )
  12. return
  13. end
  14.  
  15. local placeTorches = false
  16. local slotStart = 1
  17.  
  18. if string.lower(tArgs[2]) == 'y' then
  19. placeTorches = true
  20. slotStart = 2
  21. end
  22.  
  23. local depth = 0
  24. local unloaded = 0
  25. local collected = 0
  26.  
  27. local xPos,zPos = 0,0
  28. local xDir,zDir = 0,1
  29.  
  30. local goTo -- Filled in further down
  31. local refuel -- Filled in further down
  32.  
  33.  
  34. local function unload()
  35. print( "Unloading items..." )
  36. for n=slotStart,16 do
  37. unloaded = unloaded + turtle.getItemCount(n)
  38. turtle.select(n)
  39. turtle.drop()
  40. end
  41. collected = 0
  42. turtle.select(1)
  43. end
  44.  
  45. local function returnSupplies()
  46. local x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
  47. print( "Returning to surface..." )
  48. goTo( 0,0,0,0,-1 )
  49.  
  50. local fuelNeeded = x+y+z + x+y+z + 1
  51. if not refuel( fuelNeeded ) then
  52. unload()
  53. print( "Waiting for fuel" )
  54. while not refuel( fuelNeeded ) do
  55. sleep(1)
  56. end
  57. else
  58. unload()
  59. end
  60.  
  61.  
  62. while not torchCheck() do
  63. print( "Waiting for torches" )
  64. while not torchCheck() do
  65. sleep(1)
  66. end
  67. end
  68.  
  69. print( "Resuming mining..." )
  70. goTo( x,y,z,xd,zd )
  71. end
  72.  
  73. local function collect()
  74. local bFull = true
  75. local nTotalItems = 0
  76. for n=1,16 do
  77. local nCount = turtle.getItemCount(n)
  78. if nCount == 0 then
  79. bFull = false
  80. end
  81. nTotalItems = nTotalItems + nCount
  82. end
  83.  
  84. if nTotalItems > collected then
  85. collected = nTotalItems
  86. if math.fmod(collected + unloaded, 50) == 0 then
  87. print( "Mined "..(collected + unloaded).." items." )
  88. end
  89. end
  90.  
  91. if bFull then
  92. print( "No empty slots left." )
  93. return false
  94. end
  95. return true
  96. end
  97.  
  98. function torchCheck()
  99. if placeTorches then
  100. if turtle.getItemCount(1) > 0 then
  101. return true
  102. else
  103. return false
  104. end
  105. else
  106. return true
  107. end
  108.  
  109. end
  110.  
  111. function refuel( ammount )
  112. local fuelLevel = turtle.getFuelLevel()
  113. if fuelLevel == "unlimited" then
  114. return true
  115. end
  116.  
  117. local needed = ammount or (xPos + zPos + depth + 1)
  118. if turtle.getFuelLevel() < needed then
  119. local fueled = false
  120. for n=1,16 do
  121. if turtle.getItemCount(n) > 0 then
  122. turtle.select(n)
  123. if turtle.refuel(1) then
  124. while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
  125. turtle.refuel(1)
  126. end
  127. if turtle.getFuelLevel() >= needed then
  128. turtle.select(1)
  129. return true
  130. end
  131. end
  132. end
  133. end
  134. turtle.select(1)
  135. return false
  136. end
  137.  
  138. return true
  139. end
  140.  
  141. local function tryDigUp()
  142. --Dig Up
  143. while turtle.detectUp() do
  144. if turtle.detectUp() then
  145. if turtle.digUp() then
  146. if not collect() then
  147. returnSupplies()
  148. end
  149. else
  150. return false
  151. end
  152. elseif turtle.attackUp() then
  153. if not collect() then
  154. returnSupplies()
  155. end
  156. else
  157. sleep( 0.1 )
  158. end
  159. end
  160. return true
  161. end
  162.  
  163. local function tryDigUp()
  164. --Dig Up
  165. while turtle.detectUp() do
  166. if turtle.detectUp() then
  167. if turtle.digUp() then
  168. if not collect() then
  169. returnSupplies()
  170. end
  171. else
  172. return false
  173. end
  174. elseif turtle.attackUp() then
  175. if not collect() then
  176. returnSupplies()
  177. end
  178. else
  179. sleep( 0.1 )
  180. end
  181. end
  182. return true
  183. end
  184.  
  185. local function tryDigDown()
  186. --Dig Down
  187. while turtle.detectDown() do
  188. if turtle.detectDown() then
  189. if turtle.digDown() then
  190. if not collect() then
  191. returnSupplies()
  192. end
  193. else
  194. return false
  195. end
  196. elseif turtle.attackDown() then
  197. if not collect() then
  198. returnSupplies()
  199. end
  200. else
  201. sleep( 0.1 )
  202. end
  203. end
  204. return true
  205. end
  206.  
  207. local function tryDigForward()
  208. while turtle.detect() do
  209. if turtle.detect() then
  210. if turtle.dig() then
  211. if not collect() then
  212. returnSupplies()
  213. end
  214.  
  215. else
  216. return false
  217. end
  218. elseif turtle.attack() then
  219. if not collect() then
  220. returnSupplies()
  221. end
  222. else
  223. sleep( 0.1 )
  224. end
  225. end
  226. return true
  227. end
  228.  
  229.  
  230. local function tryForwards()
  231. if not refuel() then
  232. print( "Not enough Fuel" )
  233. returnSupplies()
  234. end
  235.  
  236. tryDigForward()
  237.  
  238. while not turtle.forward() do
  239. if not tryDigForward() then
  240. return false
  241. end
  242. end
  243.  
  244. xPos = xPos + xDir
  245. zPos = zPos + zDir
  246. return true
  247. end
  248.  
  249. local function tryUp()
  250. if not refuel() then
  251. print( "Not enough Fuel" )
  252. returnSupplies()
  253. end
  254.  
  255. tryDigUp()
  256.  
  257. while not turtle.up() do
  258. if not tryDigUp() then
  259. return false
  260. end
  261. end
  262.  
  263. depth = depth - 1
  264. if math.fmod( depth, 10 ) == 0 then
  265. print( "Descended "..depth.." metres." )
  266. end
  267. return true
  268. end
  269.  
  270. local function tryDown()
  271. if not refuel() then
  272. print( "Not enough Fuel" )
  273. returnSupplies()
  274. end
  275.  
  276. tryDigDown()
  277. while not turtle.down() do
  278. if not tryDigDown() then
  279. return false
  280. end
  281. end
  282.  
  283. --print("Moved down one");
  284.  
  285. depth = depth + 1
  286. if math.fmod( depth, 10 ) == 0 then
  287. print( "Descended "..depth.." metres." )
  288. end
  289. return true
  290. end
  291.  
  292. local function turnLeft()
  293. turtle.turnLeft()
  294. xDir, zDir = -zDir, xDir
  295. end
  296.  
  297. local function turnRight()
  298. turtle.turnRight()
  299. xDir, zDir = zDir, -xDir
  300. end
  301.  
  302. local function placeTorch()
  303. turtle.select(1)
  304. turtle.placeDown()
  305. end
  306.  
  307. function goTo( x, y, z, xd, zd )
  308. while depth > y do
  309. if turtle.up() then
  310. depth = depth - 1
  311. elseif turtle.digUp() or turtle.attackUp() then
  312. collect()
  313. else
  314. sleep( 0.5 )
  315. end
  316. end
  317.  
  318. if xPos > x then
  319. while xDir ~= -1 do
  320. turnLeft()
  321. end
  322. while xPos > x do
  323. if turtle.forward() then
  324. xPos = xPos - 1
  325. elseif turtle.dig() or turtle.attack() then
  326. collect()
  327. else
  328. sleep( 0.5 )
  329. end
  330. end
  331. elseif xPos < x then
  332. while xDir ~= 1 do
  333. turnLeft()
  334. end
  335. while xPos < x do
  336. if turtle.forward() then
  337. xPos = xPos + 1
  338. elseif turtle.dig() or turtle.attack() then
  339. collect()
  340. else
  341. sleep( 0.5 )
  342. end
  343. end
  344. end
  345.  
  346. if zPos > z then
  347. while zDir ~= -1 do
  348. turnLeft()
  349. end
  350. while zPos > z do
  351. if turtle.forward() then
  352. zPos = zPos - 1
  353. elseif turtle.dig() or turtle.attack() then
  354. collect()
  355. else
  356. sleep( 0.5 )
  357. end
  358. end
  359. elseif zPos < z then
  360. while zDir ~= 1 do
  361. turnLeft()
  362. end
  363. while zPos < z do
  364. if turtle.forward() then
  365. zPos = zPos + 1
  366. elseif turtle.dig() or turtle.attack() then
  367. collect()
  368. else
  369. sleep( 0.5 )
  370. end
  371. end
  372. end
  373.  
  374. while depth < y do
  375. if turtle.down() then
  376. depth = depth + 1
  377. elseif turtle.digDown() or turtle.attackDown() then
  378. collect()
  379. else
  380. sleep( 0.5 )
  381. end
  382. end
  383.  
  384. while zDir ~= zd or xDir ~= xd do
  385. turnLeft()
  386. end
  387. end
  388.  
  389. if not refuel() then
  390. print( "Out of Fuel" )
  391. return
  392. end
  393.  
  394. print( "Tunneling..." )
  395.  
  396. turtle.select(1)
  397.  
  398. local alternate = 0
  399.  
  400. if placeTorches then
  401. while not torchCheck() do
  402. print( "Waiting for torches" )
  403. while not torchCheck() do
  404. sleep(1)
  405. end
  406. end
  407. end
  408.  
  409. tryUp()
  410. tryDigUp()
  411.  
  412. for n=1,size do
  413. if n<size then
  414. tryForwards()
  415. tryDigUp()
  416. tryDigDown()
  417. if placeTorches then
  418. if math.fmod(n+4,5) == 0 then
  419. placeTorch()
  420. end
  421. end
  422. else
  423. print( "Tunnel complete." )
  424. end
  425.  
  426. end
  427.  
  428. if placeTorches then
  429. placeTorch()
  430. end
  431.  
  432.  
  433.  
  434. print( "Returning to start..." )
  435.  
  436. -- Return to where we started
  437. goTo( 0,0,0,0,-1 )
  438. unload()
  439. goTo( 0,0,0,0,1 )
  440.  
  441.  
  442.  
  443. print( "Mined "..(collected + unloaded).." items total." )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement