DrFair

Turtle Quarry nofuel chest

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