DrFair

Turtle Quarry

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