DrFair

Turtle Quarry nofuel

Jun 9th, 2013
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. Args = {...}
  2.  
  3. x,y,z = 0,0,0
  4. dir = { [1]="+X", [2]="+Y", [3]="-X", [4]="-Y" }
  5. nDir = 1
  6. atBedrock = false
  7. dirDown = true
  8. dirFor = true
  9.  
  10. function printUsage()
  11. print("Usage:")
  12. print("quarry <LENGTH> <WIDE> <left/right>")
  13. print("Quarries a <LENGTH> long and <WIDE> wide hole to bedrock.")
  14. print("Place enderchest in slot 1.")
  15. end
  16.  
  17. function dropOff()
  18. print("Dropping off items.")
  19. turtle.digUp()
  20. turtle.select(1)
  21. if turtle.placeUp() then
  22. for i=2,16 do
  23. turtle.select(i)
  24. turtle.dropUp()
  25. end
  26. turtle.select(1)
  27. turtle.digUp()
  28. else
  29. print("Failed to drop off.")
  30. end
  31. end
  32.  
  33. function hasSpace()
  34. local space = false
  35. for i=1,16 do
  36. if turtle.getItemCount(i) == 0 then
  37. space = true
  38. end
  39. end
  40. if not space then dropOff() end
  41. end
  42.  
  43. function goDirFor(bool)
  44. if bool then
  45. if dir[nDir] == "+X" then x = x + 1 elseif
  46. dir[nDir] == "-X" then x = x - 1 elseif
  47. dir[nDir] == "+Y" then y = y + 1 elseif
  48. dir[nDir] == "-Y" then y = y - 1 end
  49. else
  50. if dir[nDir] == "+X" then x = x - 1 elseif
  51. dir[nDir] == "-X" then x = x + 1 elseif
  52. dir[nDir] == "+Y" then y = y - 1 elseif
  53. dir[nDir] == "-Y" then y = y + 1 end
  54. end
  55. end
  56.  
  57. function mineFor()
  58. while turtle.detect() do
  59. turtle.dig()
  60. os.sleep(0.5)
  61. end
  62. hasSpace()
  63. return true
  64. end
  65.  
  66. function mineUp()
  67. while turtle.detectUp() do
  68. turtle.digUp()
  69. os.sleep(0.5)
  70. end
  71. hasSpace()
  72. return true
  73. end
  74.  
  75. function mineDown()
  76. turtle.digDown()
  77. hasSpace()
  78. return true
  79. end
  80.  
  81. function goFor()
  82. if turtle.forward() then
  83. goDirFor(true)
  84. return true
  85. else
  86. return false
  87. end
  88. end
  89.  
  90. function goBack()
  91. if turtle.back() then
  92. goDirFor(false)
  93. return true
  94. else
  95. return false
  96. end
  97. end
  98.  
  99. function goUp()
  100. if turtle.up() then
  101. z = z + 1
  102. return true
  103. else
  104. return false
  105. end
  106. end
  107.  
  108. function goDown()
  109. if turtle.down() then
  110. z = z - 1
  111. return true
  112. else
  113. return false
  114. end
  115. end
  116.  
  117. function turnRight()
  118. turtle.turnRight()
  119. if nDir < 4 then
  120. nDir = nDir + 1
  121. else
  122. nDir = 1
  123. end
  124. end
  125.  
  126. function turnLeft()
  127. turtle.turnLeft()
  128. if nDir > 1 then
  129. nDir = nDir - 1
  130. else
  131. nDir = 4
  132. end
  133. end
  134.  
  135. function stripFor()
  136. mineUp()
  137. if turtle.detectUp() then
  138. while turtle.detectUp() do
  139. goBack()
  140. mineUp()
  141. end
  142. goUp()
  143. end
  144. mineFor()
  145. while turtle.detect() do
  146. goUp()
  147. mineFor()
  148. end
  149. mineDown()
  150. if turtle.detectDown() and not atBedrock then
  151. atBedrock = true
  152. print("Hit bedrock.")
  153. end
  154. return goFor()
  155. end
  156.  
  157. function turn()
  158. print("Turning around.")
  159. if tRight then
  160. turnRight()
  161. while not stripFor() do
  162. os.sleep(0.5)
  163. end
  164. turnRight()
  165. else
  166. turnLeft()
  167. while not stripFor() do
  168. os.sleep(0.5)
  169. end
  170. turnLeft()
  171. end
  172. tRight = not tRight
  173. dirFor = not dirFor
  174. end
  175.  
  176. function stripDown()
  177. for i=1,3 do
  178. mineDown()
  179. mineUp()
  180. if turtle.detectDown() and not atBedrock then
  181. atBedrock = true
  182. print("Hit bedrock.")
  183. break
  184. end
  185. goDown()
  186. end
  187. turnRight()
  188. turnRight()
  189. dirFor = not dirFor
  190. end
  191.  
  192. function turnTo(str)
  193. while dir[nDir] ~= str do
  194. turnRight()
  195. end
  196. end
  197.  
  198. function resetPos()
  199. while z ~= 0 do
  200. goUp()
  201. end
  202. turnTo("-X")
  203. while x ~= 0 do
  204. goFor()
  205. end
  206. if y < 0 then
  207. turnTo("+Y")
  208. else
  209. turnTo("-Y")
  210. end
  211. while y ~= 0 do
  212. goFor()
  213. end
  214. turnTo("+X")
  215. end
  216.  
  217. function quarry(dx,dy)
  218. while not atBedrock do
  219. for i=1,dy do
  220. if dirFor then
  221. while x < dx do
  222. stripFor()
  223. end
  224. if i < dy then
  225. turn()
  226. end
  227. else
  228. while x > 0 do
  229. stripFor()
  230. end
  231. if i < dy then
  232. turn()
  233. end
  234. end
  235. end
  236. if not atBedrock then
  237. stripDown()
  238. end
  239. end
  240. print("Going back to start.")
  241. mineDown()
  242. mineUp()
  243. resetPos()
  244. dropOff()
  245. print("Job done.")
  246. end
  247.  
  248. if #Args == 0 then
  249. printUsage()
  250. else
  251. if tonumber(Args[1]) == 0 or tonumber(Args[2]) == 0 then
  252. printUsage()
  253. else
  254. if Args[3] == "right" or Args[3] == "left" then
  255. if Args[3] == "right" then
  256. tRight = true
  257. else
  258. tRight = false
  259. end
  260. quarry(tonumber(Args[1])-1,tonumber(Args[2]))
  261. else
  262. printUsage()
  263. end
  264. end
  265. end
Advertisement
Add Comment
Please, Sign In to add comment