Advertisement
HarvDad

chute

May 2nd, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.55 KB | None | 0 0
  1. -- chute
  2. -- Creates a ladder chute to the height or depth specified
  3. -- If supplied, ladder is placed
  4. -- Written by HarvDad, March 2014
  5.  
  6. args = {...}
  7. nArgs = #args
  8.  
  9. print("chute: Rev 1.1")
  10. x = 0
  11. y = 0
  12. z = 0
  13. face = 0
  14. minimumFuel = 100
  15. minLevel = 6
  16. missionMessage = "Mission complete."
  17. abort = false
  18. local currentFuelLevel = turtle.getFuelLevel()
  19. patchSlot = 1
  20. ladderSlot = 16
  21. goingUp = false
  22. useLadders = true
  23.  
  24. if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
  25. print("Creates a ladder chute straight up or down.")
  26. print("This program assumes turtle is pre-fueled.")
  27. print("Usage: chute <height> <useLadders>")
  28. print("Place ladder in slot ", ladderSlot, ".")
  29. return
  30. end
  31.  
  32. if nArgs ~= 2 then
  33. print("Usage: chute <height> <useLadders>")
  34. return
  35. end
  36.  
  37. targetDepth = tonumber(args[1])
  38. if targetDepth == nil then
  39. print("\"", args[1], "\" is not a valid depth")
  40. return
  41. end
  42.  
  43. if args[2] == "true" then
  44. useLadders = true
  45. elseif args[2] == "false" then
  46. useLadders = false
  47. else
  48. print("Second argument must be true or false")
  49. return
  50. end
  51.  
  52. if (targetDepth < 0) then
  53. targetDepth = targetDepth + 1
  54. goingUp = true
  55. end
  56.  
  57. depth = 0
  58.  
  59.  
  60. function left()
  61. if face == 0 then face = 1 turtle.turnLeft() return end
  62. if face == 1 then face = 2 turtle.turnLeft() return end
  63. if face == 2 then face = 3 turtle.turnLeft() return end
  64. if face == 3 then face = 0 turtle.turnLeft() return end
  65. print("function left\(\): Bad face value: ", face)
  66. end
  67.  
  68. function right()
  69. if face == 0 then face = 3 turtle.turnRight() return end
  70. if face == 1 then face = 0 turtle.turnRight() return end
  71. if face == 2 then face = 1 turtle.turnRight() return end
  72. if face == 3 then face = 2 turtle.turnRight() return end
  73. print("function right\(\): Bad face value: ", face)
  74. end
  75.  
  76. function patchWalls()
  77. if turtle.getItemCount(patchSlot) < 4 then
  78. gatherPatchMaterial()
  79. end
  80.  
  81. turtle.select(patchSlot)
  82.  
  83. left()
  84. turtle.place()
  85. right()
  86. turtle.place()
  87. right()
  88. turtle.place()
  89. left()
  90. end
  91.  
  92. function up()
  93. attackCount = 0
  94. while turtle.detectUp() do -- This loop added in case of falling sand or whatever
  95. turtle.digUp()
  96. end
  97. for i=1,10 do -- This loop tries to handle pests (mob) that might be in the way
  98. if turtle.up() then
  99. break
  100. end
  101. sleep(2)
  102. end
  103. if attackCount > 1 then
  104. turtle.attackUp()
  105. end
  106.  
  107. gatherPatchMaterial()
  108.  
  109. patchWalls()
  110.  
  111. setFace(north)
  112. depth = depth-1
  113. y = y+1
  114. end
  115.  
  116. function down()
  117. attackCount = 0
  118.  
  119. if depth >= targetDepth then
  120. print("Hit target depth")
  121. return
  122. end
  123.  
  124. if turtle.detectDown() then
  125. turtle.digDown()
  126. end
  127. for i=1,10 do -- This loop trys to handle pests (mobs) that might be in the way
  128. if turtle.down() then
  129. break
  130. end
  131. turtle.attackDown()
  132. attackCount = attackCount + 1
  133. sleep(2)
  134. end
  135.  
  136. if attackCount > 1 then
  137. turtle.suckDown()
  138. turtle.suckUp()
  139. end
  140.  
  141. gatherPatchMaterial()
  142.  
  143. turtle.select(patchSlot)
  144. if not turtle.detect() then
  145. turtle.place()
  146. end
  147.  
  148. turtle.turnLeft()
  149. if not turtle.detect() then
  150. turtle.place()
  151. end
  152.  
  153. turtle.turnLeft()
  154. if not turtle.detect() then
  155. turtle.place()
  156. end
  157.  
  158. turtle.turnLeft()
  159. if not turtle.detect() then
  160. turtle.place()
  161. end
  162.  
  163. turtle.turnLeft()
  164. depth = depth+1
  165. y = y-1
  166. end
  167.  
  168. function rise(nBlocks)
  169. local i
  170. for i=1,nBlocks do
  171. turtle.up()
  172. y = y+1
  173. end
  174. end
  175.  
  176. function gatherPatchMaterial()
  177. local i
  178. patchCount = turtle.getItemCount(patchSlot)
  179.  
  180. if patchCount < 5 then
  181. -- print("Attempting to refill slot ", patchSlot)
  182. for i=2,13 do
  183. turtle.select(i)
  184. if turtle.compareTo(patchSlot) then
  185. turtle.transferTo(patchSlot, 64-patchCount)
  186. -- print("Transferred ", 64 - patchCount, " cobble to slot ", patchSlot)
  187. end
  188. end
  189. end
  190. turtle.select(patchSlot)
  191. end
  192.  
  193. function setFace(f)
  194. if f == 0 then
  195. if face == 0 then return end
  196. if face == 1 then right() return end
  197. if face == 2 then right() right() return end
  198. if face == 3 then left() return end
  199. end
  200.  
  201. if f == 1 then
  202. if face == 0 then left() return end
  203. if face == 1 then return end
  204. if face == 2 then right() return end
  205. if face == 3 then right() right() return end
  206. end
  207.  
  208. if f == 2 then
  209. if face == 0 then left() left() return end
  210. if face == 1 then left() return end
  211. if face == 2 then return end
  212. if face == 3 then right() return end
  213. end
  214.  
  215. if f == 3 then
  216. if face == 0 then right() return end
  217. if face == 1 then left() left() return end
  218. if face == 2 then left() return end
  219. if face == 3 then return end
  220. end
  221. end
  222.  
  223. moreLadder = true
  224.  
  225. function placeLadder()
  226. if not useLadders then
  227. return
  228. end
  229.  
  230. if turtle.getItemCount(ladderSlot) > 0 then
  231. turtle.select(ladderSlot)
  232. elseif turtle.getItemCount(ladderSlot-1) > 0 then
  233. turtle.select(ladderSlot-1)
  234. elseif turtle.getItemCount(ladderSlot-2) > 0 then
  235. turtle.select(ladderSlot-2)
  236. else
  237. moreLadder = false
  238. end
  239.  
  240. -- print("placeLadder: Ladder count = ", turtle.getItemCount(ladderSlot), " moreLadder = ", moreLadder)
  241. if moreLadder then
  242. turtle.place()
  243. end
  244. end
  245.  
  246. function placeLadderUp()
  247. if turtle.getItemCount(ladderSlot) < 1 then
  248. ladderSlot = ladderSlot - 1
  249. if turtle.getItemCount(ladderSlot) < 1 then
  250. ladderSlot = ladderSlot - 1
  251. if turtle.getItemCount(ladderSlot) < 1 then
  252. ladderSlot = ladderSlot - 1
  253. if turtle.getItemCount(ladderSlot) < 1 then
  254. end
  255. end
  256. end
  257. end
  258. turtle.select(ladderSlot)
  259. turtle.placeUp()
  260. end
  261.  
  262. function homeUp()
  263. setFace(north)
  264. turtle.select(ladderSlot)
  265. turtle.suckUp() -- in case we have monster guts on top of us
  266. while y < 0 do
  267. turtle.up()
  268. if not turtle.detectDown() then
  269. turtle.placeDown()
  270. end
  271. y = y+1
  272. end
  273. end
  274.  
  275. function ladderDown()
  276. setFace(north)
  277. turtle.back()
  278. z = z - 1
  279. firstLadder = true
  280. while y >= 0 do
  281. placeLadder()
  282. if turtle.getItemCount(patchSlot) < 4 then
  283. gatherPatchMaterial()
  284. end
  285. if (not firstLadder and y ~= 0) then
  286. turtle.select(patchSlot)
  287. turtle.placeUp()
  288. end
  289. firstLadder = false
  290. if y > 0 then
  291. turtle.down()
  292. y = y - 1
  293. else
  294. break
  295. end
  296. end
  297. end
  298.  
  299. function homeDown()
  300. setFace(north)
  301. turtle.select(ladderSlot)
  302. turtle.suckDown() -- in case we have monster guts on top of us
  303. while y > 0 do
  304. turtle.down()
  305. if not turtle.detectDown() then
  306. placeLadderUp()
  307. end
  308. y = y-1
  309. end
  310. end
  311.  
  312. -- Main program loop
  313.  
  314. if goingUp then
  315. patchWalls()
  316. while depth > targetDepth do
  317. up()
  318. end
  319. ladderDown()
  320. else
  321. while depth < targetDepth do
  322. down()
  323. end
  324. homeUp()
  325. end
  326.  
  327.  
  328.  
  329. setFace(north)
  330. print(missionMessage)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement