Advertisement
HarvDad

tube

Mar 13th, 2014
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.55 KB | None | 0 0
  1. -- tube
  2. -- Creates a 2x2 tunnel of the length specified by the user
  3. -- All sides are sealed for protection
  4. -- Torches, if supplied, will be placed on the right-hand wall at 10 block intervals
  5. -- Written by HarvDad, March 2014
  6.  
  7. args = {...}
  8. nArgs = #args
  9.  
  10. print("tube: Rev 3.1")
  11. x = 0
  12. y = 0
  13. z = 0
  14. face = 0
  15. minimumFuel = 100
  16. missionMessage = "Mission complete."
  17. abort = false
  18. patchSlot = 1
  19. torchSlot = 16
  20. local currentFuelLevel = turtle.getFuelLevel()
  21. zipTube = false
  22. usage = "Usage: tube <length> [zip]"
  23.  
  24. if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
  25. print("Creates a tunnel with sealed sides, top, and bottom.")
  26. print("If zip tube, tunnel is 1x2, else 2x2")
  27. print("This programs assumes the turtle is pre-fueled.")
  28. print("Place patch material (like cobblestone) in slot ", patchSlot)
  29. print("If torches are desired, place torches in slot ", torchSlot)
  30. print(usage)
  31. return
  32. end
  33.  
  34. if nArgs ~= 1 and nArgs ~= 2 then
  35. print("Usage: tube <length>")
  36. return
  37. end
  38.  
  39. if nArgs == 2 then
  40. if args[2] == "zip" then
  41. zipTube = true
  42. else
  43. print("Usage: tube <length>")
  44. end
  45. end
  46.  
  47. length = tonumber(args[1])
  48. if length == nil then
  49. print("\"", args[1], "\" is not a valid length")
  50. return
  51. end
  52. if length < 1 then
  53. print("length must be a positive number greater than zero")
  54. end
  55.  
  56. targetArea = length
  57. areaCovered = 1;
  58.  
  59. -- The following 'face' directions are relative to the starting position of the turtle in this program
  60. north = 0
  61. west = 1
  62. south = 2
  63. east = 3
  64.  
  65.  
  66. function left()
  67. if face == 0 then face = 1 turtle.turnLeft() return end
  68. if face == 1 then face = 2 turtle.turnLeft() return end
  69. if face == 2 then face = 3 turtle.turnLeft() return end
  70. if face == 3 then face = 0 turtle.turnLeft() return end
  71. print("function left\(\): Bad face value: ", face)
  72. end
  73.  
  74. function right()
  75. if face == 0 then face = 3 turtle.turnRight() return end
  76. if face == 1 then face = 0 turtle.turnRight() return end
  77. if face == 2 then face = 1 turtle.turnRight() return end
  78. if face == 3 then face = 2 turtle.turnRight() return end
  79. print("function right\(\): Bad face value: ", face)
  80. end
  81.  
  82. function up()
  83. local success = false
  84.  
  85. repeat
  86. while turtle.detectUp() do -- This loop added in case of falling sand or whatever
  87. turtle.digUp()
  88. end
  89. success = turtle.up()
  90. until success
  91. y = y+1
  92. end
  93.  
  94. function rise(nBlocks)
  95. local i
  96. for i=1,nBlocks do
  97. up()
  98. end
  99. end
  100.  
  101. function descend(nBlocks)
  102. local i
  103. for i=1,nBlocks do
  104. down()
  105. end
  106. end
  107.  
  108. function down()
  109. if turtle.detectDown() then
  110. turtle.digDown()
  111. end
  112. turtle.down()
  113. y = y-1
  114. end
  115.  
  116. function forward(keepTrackOfArea)
  117. while turtle.detect() do -- This loop added in case of falling sand or whatever
  118. turtle.dig()
  119. end
  120. for i=1,10 do -- This loop trys to handle pests (mob) that might be in the way
  121. if not turtle.forward() then
  122. if turtle.detect() then
  123. dig()
  124. else
  125. turtle.attack()
  126. sleep(2)
  127. end
  128. else
  129. break
  130. end
  131. end
  132. if keepTrackOfArea then
  133. areaCovered = areaCovered + 1
  134. end
  135.  
  136. if face == 0 then z = z+1 return end
  137. if face == 1 then x = x-1 return end
  138. if face == 2 then z = z-1 return end
  139. if face == 3 then x = x+1 return end
  140. end
  141.  
  142. function gatherPatchMaterial()
  143. local i
  144. patchCount = turtle.getItemCount(patchSlot)
  145.  
  146. if patchCount < 3 then
  147. -- print("Attempting to refill slot ", patchSlot)
  148. for i=2,14 do
  149. turtle.select(i)
  150. if turtle.compareTo(patchSlot) then
  151. turtle.transferTo(patchSlot, 64-patchCount)
  152. -- print("Transferred ", 64 - patchCount, " cobble to slot ", patchSlot)
  153. end
  154. end
  155. end
  156. turtle.select(patchSlot)
  157. end
  158.  
  159. function patch()
  160. gatherPatchMaterial()
  161. turtle.select(patchSlot)
  162. turtle.place()
  163. end
  164.  
  165. function patchUp()
  166. gatherPatchMaterial()
  167. turtle.select(patchSlot)
  168. turtle.placeUp()
  169. end
  170.  
  171. function patchDown()
  172. gatherPatchMaterial()
  173. turtle.select(patchSlot)
  174. turtle.placeDown()
  175. end
  176.  
  177. function patchUpper()
  178. patchUp()
  179. left()
  180. patch()
  181. right()
  182. right()
  183. patch()
  184. left()
  185. end
  186.  
  187. function patchLower()
  188. patchDown()
  189. left()
  190. patch()
  191. right()
  192. right()
  193. patch()
  194. left()
  195. end
  196.  
  197. function patchTube()
  198. patchUp()
  199. patchDown()
  200. left()
  201. patch()
  202. right()
  203. right()
  204. patch()
  205. left()
  206. end
  207.  
  208. addTorch = false
  209. torchSpacing = 6
  210.  
  211. function mineForward()
  212. forward()
  213. if not turtle.detectDown() then
  214. patchDown()
  215. end
  216. right()
  217. if not turtle.detect() then
  218. patch()
  219. end
  220. up()
  221. if not turtle.detect() then
  222. patch()
  223. end
  224. left()
  225. if not turtle.detectUp() then
  226. patchUp()
  227. end
  228.  
  229. if addTorch then
  230. if (torchSpot % torchSpacing) == 0 then
  231. left()
  232. turtle.select(torchSlot)
  233. turtle.place()
  234. right()
  235. end
  236. torchSpot = torchSpot + 1
  237. end
  238.  
  239. turtle.down()
  240. end
  241.  
  242. function setFace(f)
  243. if f == 0 then
  244. if face == 0 then return end
  245. if face == 1 then right() return end
  246. if face == 2 then right() right() return end
  247. if face == 3 then left() return end
  248. end
  249.  
  250. if f == 1 then
  251. if face == 0 then left() return end
  252. if face == 1 then return end
  253. if face == 2 then right() return end
  254. if face == 3 then right() right() return end
  255. end
  256.  
  257. if f == 2 then
  258. if face == 0 then left() left() return end
  259. if face == 1 then left() return end
  260. if face == 2 then return end
  261. if face == 3 then right() return end
  262. end
  263.  
  264. if f == 3 then
  265. if face == 0 then right() return end
  266. if face == 1 then left() left() return end
  267. if face == 2 then left() return end
  268. if face == 3 then return end
  269. end
  270. end
  271.  
  272. function checkFuel()
  273. if currentFuelLevel == "unlimited" then
  274. return true
  275. end
  276.  
  277. if currentFuelLevel < minimumFuel then
  278. if not turtle.refuel(200) then
  279. areaCovered = targetArea
  280. missionMessage = "Mission aborted due to low fuel."
  281. abort = true
  282. return false
  283. end
  284. end
  285. return true
  286. end
  287.  
  288. function home(targetY)
  289. -- print("home:face ", face, ", x = ", x, ", z = ", z)
  290. if x < 0 then
  291. setFace(east)
  292. while x < 0 do
  293. forward()
  294. end
  295. else
  296. if x > 0 then
  297. setFace(west)
  298. while x > 0 do
  299. forward()
  300. end
  301. end
  302. end
  303.  
  304. if z < 0 then
  305. setFace(north)
  306. while z < 0 do
  307. forward()
  308. end
  309. else
  310. if z > 0 then
  311. setFace(south)
  312. while z > 0 do
  313. forward()
  314. end
  315. end
  316. end
  317. end
  318.  
  319. function poopTorch()
  320. left()
  321. left()
  322. turtle.select(torchSlot)
  323. turtle.place()
  324. right()
  325. right()
  326. turtle.select(patchSlot)
  327. end
  328.  
  329. -- MAIN PROGRAM
  330.  
  331. turtle.select(patchSlot)
  332.  
  333. print("Current Fuel Level: ", currentFuelLevel)
  334.  
  335. if currentFuelLevel ~= "unlimited" then
  336. if currentFuelLevel < minimumFuel then
  337. if not turtle.refuel() then
  338. print("No fuel")
  339. return
  340. end
  341. end
  342. end
  343.  
  344. if zipTube then
  345. print("Starting 1x2 zip tube...")
  346. for i=1,length do
  347. forward()
  348. patchLower()
  349. end
  350.  
  351. patch()
  352. up()
  353. patch()
  354.  
  355. left()
  356. left()
  357. patchUpper()
  358. for i=1,length-1 do
  359. forward()
  360. patchUpper()
  361. if (i % torchSpacing) == 0 then
  362. poopTorch()
  363. end
  364. end
  365. down()
  366. else
  367. print("Starting 2x2 double-wide tube...")
  368. for i=1,length do
  369. mineForward()
  370. end
  371.  
  372. setFace(north)
  373. if not turtle.detect() then
  374. patch()
  375. end
  376. rise(1)
  377. if not turtle.detect() then
  378. patch()
  379. end
  380. descend(1)
  381. if not turtle.detect() then
  382. patch()
  383. end
  384. setFace(west)
  385. mineForward()
  386. if not turtle.detect() then
  387. patch()
  388. end
  389. rise(1)
  390. if not turtle.detect() then
  391. patch()
  392. end
  393. descend(1)
  394. setFace(south)
  395. addTorch = true
  396. torchSpot = 7
  397.  
  398. for i=1,length-1 do
  399. mineForward()
  400. checkFuel()
  401. if abort then
  402. break
  403. end
  404. end
  405. end
  406.  
  407. home(0)
  408. setFace(north)
  409.  
  410. print(missionMessage, " Current fuel level is ", turtle.getFuelLevel())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement