Advertisement
HarvDad

shaft

Mar 14th, 2014
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.47 KB | None | 0 0
  1. -- shaft
  2. -- Digs a vertical shaft straight down to the depth specified by the user
  3. -- If supplied, ladder is placed
  4. -- Written by HarvDad, March 2014
  5.  
  6. args = {...}
  7. nArgs = #args
  8.  
  9. print("shaft: Rev 2.2")
  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.  
  23. if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
  24. print("Digs a vertical shaft straight down.")
  25. print("This program assumes turtle is pre-fueled.")
  26. print("Usage: shaft <depth>")
  27. print("Specify the number of blocks to dig down.")
  28. print("If desired, place ladder in slot ", ladderSlot, ".")
  29. return
  30. end
  31.  
  32. if nArgs ~= 1 then
  33. print("Usage: shaft <depth>")
  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 (targetDepth < 0) then
  44. goingUp = true
  45. end
  46.  
  47. depth = 0
  48.  
  49.  
  50. function left()
  51. if face == 0 then face = 1 turtle.turnLeft() return end
  52. if face == 1 then face = 2 turtle.turnLeft() return end
  53. if face == 2 then face = 3 turtle.turnLeft() return end
  54. if face == 3 then face = 0 turtle.turnLeft() return end
  55. print("function left\(\): Bad face value: ", face)
  56. end
  57.  
  58. function right()
  59. if face == 0 then face = 3 turtle.turnRight() return end
  60. if face == 1 then face = 0 turtle.turnRight() return end
  61. if face == 2 then face = 1 turtle.turnRight() return end
  62. if face == 3 then face = 2 turtle.turnRight() return end
  63. print("function right\(\): Bad face value: ", face)
  64. end
  65.  
  66. function patchWalls()
  67. if turtle.getItemCount(patchSlot) < 4 then
  68. gatherPatchMaterial()
  69. end
  70.  
  71. turtle.select(patchSlot)
  72.  
  73. turtle.place()
  74. left()
  75.  
  76. turtle.place()
  77. left()
  78.  
  79. turtle.place()
  80. left()
  81.  
  82. turtle.place()
  83. left()
  84. end
  85.  
  86. function up()
  87. attackCount = 0
  88. while turtle.detectUp() do -- This loop added in case of falling sand or whatever
  89. turtle.digUp()
  90. end
  91. for i=1,10 do -- This loop tries to handle pests (mob) that might be in the way
  92. if turtle.up() then
  93. break
  94. end
  95. sleep(2)
  96. end
  97. if attackCount > 1 then
  98. turtle.attackUp()
  99. end
  100.  
  101. gatherPatchMaterial()
  102.  
  103. patchWalls()
  104.  
  105. setFace(north)
  106. depth = depth-1
  107. y = y+1
  108. end
  109.  
  110. function down()
  111. attackCount = 0
  112.  
  113. if depth >= targetDepth then
  114. print("Hit target depth")
  115. return
  116. end
  117.  
  118. if turtle.detectDown() then
  119. turtle.digDown()
  120. end
  121. for i=1,10 do -- This loop trys to handle pests (mobs) that might be in the way
  122. if turtle.down() then
  123. break
  124. end
  125. turtle.attackDown()
  126. attackCount = attackCount + 1
  127. sleep(2)
  128. end
  129.  
  130. if attackCount > 1 then
  131. turtle.suckDown()
  132. turtle.suckUp()
  133. end
  134.  
  135. gatherPatchMaterial()
  136.  
  137. turtle.select(patchSlot)
  138. if not turtle.detect() then
  139. turtle.place()
  140. end
  141.  
  142. turtle.turnLeft()
  143. if not turtle.detect() then
  144. turtle.place()
  145. end
  146.  
  147. turtle.turnLeft()
  148. if not turtle.detect() then
  149. turtle.place()
  150. end
  151.  
  152. turtle.turnLeft()
  153. if not turtle.detect() then
  154. turtle.place()
  155. end
  156.  
  157. turtle.turnLeft()
  158. depth = depth+1
  159. y = y-1
  160. end
  161.  
  162. function rise(nBlocks)
  163. local i
  164. for i=1,nBlocks do
  165. turtle.up()
  166. y = y+1
  167. end
  168. end
  169.  
  170. function gatherPatchMaterial()
  171. local i
  172. patchCount = turtle.getItemCount(patchSlot)
  173.  
  174. if patchCount < 5 then
  175. -- print("Attempting to refill slot ", patchSlot)
  176. for i=2,13 do
  177. turtle.select(i)
  178. if turtle.compareTo(patchSlot) then
  179. turtle.transferTo(patchSlot, 64-patchCount)
  180. -- print("Transferred ", 64 - patchCount, " cobble to slot ", patchSlot)
  181. end
  182. end
  183. end
  184. turtle.select(patchSlot)
  185. end
  186.  
  187. function setFace(f)
  188. if f == 0 then
  189. if face == 0 then return end
  190. if face == 1 then right() return end
  191. if face == 2 then right() right() return end
  192. if face == 3 then left() return end
  193. end
  194.  
  195. if f == 1 then
  196. if face == 0 then left() return end
  197. if face == 1 then return end
  198. if face == 2 then right() return end
  199. if face == 3 then right() right() return end
  200. end
  201.  
  202. if f == 2 then
  203. if face == 0 then left() left() return end
  204. if face == 1 then left() return end
  205. if face == 2 then return end
  206. if face == 3 then right() return end
  207. end
  208.  
  209. if f == 3 then
  210. if face == 0 then right() return end
  211. if face == 1 then left() left() return end
  212. if face == 2 then left() return end
  213. if face == 3 then return end
  214. end
  215. end
  216.  
  217. function placeLadderUp()
  218. if turtle.getItemCount(ladderSlot) < 1 then
  219. ladderSlot = ladderSlot - 1
  220. if turtle.getItemCount(ladderSlot) < 1 then
  221. ladderSlot = ladderSlot - 1
  222. if turtle.getItemCount(ladderSlot) < 1 then
  223. ladderSlot = ladderSlot - 1
  224. if turtle.getItemCount(ladderSlot) < 1 then
  225. end
  226. end
  227. end
  228. end
  229. turtle.select(ladderSlot)
  230. turtle.placeUp()
  231. end
  232.  
  233. function homeUp()
  234. setFace(north)
  235. turtle.select(ladderSlot)
  236. turtle.suckUp() -- in case we have monster guts on top of us
  237. while y < 0 do
  238. turtle.up()
  239. if not turtle.detectDown() then
  240. turtle.placeDown()
  241. end
  242. y = y+1
  243. end
  244. end
  245.  
  246. function homeDown()
  247. setFace(north)
  248. turtle.select(ladderSlot)
  249. turtle.suckDown() -- in case we have monster guts on top of us
  250. while y > 0 do
  251. turtle.down()
  252. if not turtle.detectDown() then
  253. placeLadderUp()
  254. end
  255. y = y-1
  256. end
  257. end
  258.  
  259. -- Main program loop
  260.  
  261. if goingUp then
  262. while depth > targetDepth do
  263. up()
  264. end
  265. homeDown()
  266. else
  267. while depth < targetDepth do
  268. down()
  269. end
  270. homeUp()
  271. end
  272.  
  273.  
  274.  
  275. setFace(north)
  276. print(missionMessage)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement