DrFair

Turtle Quarry Mk2

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