DrFair

Turtle Quarry Mk3 nofuel

Aug 5th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.17 KB | None | 0 0
  1. Args = {...}
  2.  
  3. function printData()
  4. local path = shell.resolve("quarrytemp")
  5. data = { ["dir"] = textutils.serialize(dir), ["nDir"] = nDir, ["x"] = x, ["y"] = y, ["z"] = z, ["atBedrock"] = atBedrock, ["dirDown"] = dirDown, ["dirFor"] = dirFor, ["dirBack"] = dirBack, ["tRight"] = tRight, ["qDirRight"] = qDirRight, ["demx"] = demx, ["demy"] = demy }
  6. if fs.exists("quarrytemp") then
  7. fs.delete("quarrytemp")
  8. end
  9. local file = fs.open(path,"w")
  10. file.write(textutils.serialize(data))
  11. file.close()
  12. end
  13.  
  14. function getData()
  15. local path = shell.resolve("quarrytemp")
  16. if fs.exists("quarrytemp") then
  17. local file = fs.open(path,"r")
  18. data = textutils.unserialize(file.readAll())
  19. dir = textutils.unserialize(data["dir"])
  20. nDir = data["nDir"]
  21. x,y,z = data["x"],data["y"],data["z"]
  22. atBedrock = data["atBedrock"]
  23. dirDown = data["dirDown"]
  24. dirFor = data["dirFor"]
  25. dirBack = data["dirBack"]
  26. tRight = data["tRight"]
  27. qDirRight = data["qDirRight"]
  28. demx = data["demx"]
  29. demy = data["demy"]
  30. return true
  31. else
  32. print("Cannot continue, no data found.")
  33. return false
  34. end
  35. end
  36.  
  37. function printUsage()
  38. print("Usage:")
  39. print("quarry <LENGTH> <WIDE> <left/right>")
  40. print("Quarries a <LENGTH> long and <WIDE> wide hole to bedrock.")
  41. print("quarry continue")
  42. print("Continues from where it left off.")
  43. print("Place enderchest in slot 1.")
  44. end
  45.  
  46. function dropOff()
  47. print("Dropping off items.")
  48. turtle.digUp()
  49. turtle.select(1)
  50. if turtle.placeUp() then
  51. for i=2,16 do
  52. turtle.select(i)
  53. turtle.dropUp()
  54. end
  55. turtle.select(1)
  56. turtle.digUp()
  57. else
  58. print("Failed to drop off.")
  59. end
  60. end
  61.  
  62. function hasSpace()
  63. local space = false
  64. for i=1,16 do
  65. if turtle.getItemCount(i) == 0 then
  66. space = true
  67. end
  68. end
  69. if not space then dropOff() end
  70. end
  71.  
  72. function goDirFor(bool)
  73. if bool then
  74. if dir[nDir] == "+X" then x = x + 1 elseif
  75. dir[nDir] == "-X" then x = x - 1 elseif
  76. dir[nDir] == "+Y" then y = y + 1 elseif
  77. dir[nDir] == "-Y" then y = y - 1 end
  78. else
  79. if dir[nDir] == "+X" then x = x - 1 elseif
  80. dir[nDir] == "-X" then x = x + 1 elseif
  81. dir[nDir] == "+Y" then y = y - 1 elseif
  82. dir[nDir] == "-Y" then y = y + 1 end
  83. end
  84. end
  85.  
  86. function mineFor()
  87. while turtle.detect() do
  88. turtle.dig()
  89. os.sleep(0.5)
  90. end
  91. hasSpace()
  92. return true
  93. end
  94.  
  95. function mineUp()
  96. while turtle.detectUp() do
  97. turtle.digUp()
  98. os.sleep(0.5)
  99. end
  100. hasSpace()
  101. return true
  102. end
  103.  
  104. function mineDown()
  105. turtle.digDown()
  106. hasSpace()
  107. return true
  108. end
  109.  
  110. function goFor()
  111. if turtle.forward() then
  112. goDirFor(true)
  113. printData()
  114. return true
  115. else
  116. return false
  117. end
  118. end
  119.  
  120. function goBack()
  121. if turtle.back() then
  122. goDirFor(false)
  123. printData()
  124. return true
  125. else
  126. return false
  127. end
  128. end
  129.  
  130. function goUp()
  131. if turtle.up() then
  132. z = z + 1
  133. printData()
  134. return true
  135. else
  136. return false
  137. end
  138. end
  139.  
  140. function goDown()
  141. if turtle.down() then
  142. z = z - 1
  143. printData()
  144. return true
  145. else
  146. return false
  147. end
  148. end
  149.  
  150. function turnRight()
  151. turtle.turnRight()
  152. if nDir < 4 then
  153. nDir = nDir + 1
  154. else
  155. nDir = 1
  156. end
  157. end
  158.  
  159. function turnLeft()
  160. turtle.turnLeft()
  161. if nDir > 1 then
  162. nDir = nDir - 1
  163. else
  164. nDir = 4
  165. end
  166. end
  167.  
  168. function stripFor()
  169. mineUp()
  170. if turtle.detectUp() then
  171. while turtle.detectUp() do
  172. goBack()
  173. mineUp()
  174. end
  175. goUp()
  176. end
  177. mineFor()
  178. while turtle.detect() do
  179. goUp()
  180. mineFor()
  181. end
  182. local n = 0
  183. while turtle.detectDown() do
  184. mineDown()
  185. n = n + 1
  186. if n >= 10 then break end
  187. end
  188. if turtle.detectDown() and not atBedrock then
  189. atBedrock = true
  190. print("Hit bedrock.")
  191. end
  192. return goFor()
  193. end
  194.  
  195. function turn()
  196. print("Turning around.")
  197. if tRight then
  198. turnRight()
  199. while not stripFor() do
  200. os.sleep(0.5)
  201. end
  202. turnRight()
  203. else
  204. turnLeft()
  205. while not stripFor() do
  206. os.sleep(0.5)
  207. end
  208. turnLeft()
  209. end
  210. tRight = not tRight
  211. dirFor = not dirFor
  212. end
  213.  
  214. function stripDown()
  215. for i=1,3 do
  216. mineUp()
  217. local n = 0
  218. while turtle.detectDown() do
  219. mineDown()
  220. n = n + 1
  221. if n >= 10 then break end
  222. end
  223. if turtle.detectDown() and not atBedrock then
  224. atBedrock = true
  225. print("Hit bedrock.")
  226. break
  227. end
  228. goDown()
  229. end
  230. turnRight()
  231. turnRight()
  232. dirFor = not dirFor
  233. dirBack = not dirBack
  234. end
  235.  
  236. function turnTo(str)
  237. while dir[nDir] ~= str do
  238. turnRight()
  239. end
  240. end
  241.  
  242. function resetPos()
  243. while z ~= 0 do
  244. goUp()
  245. end
  246. turnTo("-X")
  247. while x ~= 0 do
  248. goFor()
  249. end
  250. if y < 0 then
  251. turnTo("+Y")
  252. else
  253. turnTo("-Y")
  254. end
  255. while y ~= 0 do
  256. goFor()
  257. end
  258. turnTo("+X")
  259. end
  260.  
  261. function quarry()
  262. while not atBedrock do
  263. if qDirRight then
  264. if dirFor then
  265. while x < demx do
  266. stripFor()
  267. end
  268. else
  269. while x > 0 do
  270. stripFor()
  271. end
  272. end
  273. if dirBack then
  274. if y > 0 then
  275. turn()
  276. else
  277. stripDown()
  278. end
  279. else
  280. if y < demy then
  281. turn()
  282. else
  283. stripDown()
  284. end
  285. end
  286. else
  287. if dirFor then
  288. while x < demx do
  289. stripFor()
  290. end
  291. else
  292. while x > 0 do
  293. stripFor()
  294. end
  295. end
  296. if dirBack then
  297. if y > 0 then
  298. turn()
  299. else
  300. stripDown()
  301. end
  302. else
  303. if y < demy then
  304. turn()
  305. else
  306. stripDown()
  307. end
  308. end
  309. end
  310. end
  311. print("Going back to start.")
  312. mineDown()
  313. mineUp()
  314. resetPos()
  315. fs.delete("quarrytemp")
  316. dropOff()
  317. print("Job done.")
  318. end
  319.  
  320. if #Args == 0 then
  321. printUsage()
  322. else
  323. if Args[1] == "continue" then
  324. if getData() then
  325. print("Continue with quarry dimensions "..(demx+1).."x"..(demy+1)..".")
  326. quarry()
  327. end
  328. elseif tonumber(Args[1]) == 0 or tonumber(Args[2]) == 0 then
  329. printUsage()
  330. else
  331. if Args[3] == "right" or Args[3] == "left" then
  332. nDir = 1
  333. x,y,z = 0,0,0
  334. atBedrock = false
  335. dirDown = true
  336. dirFor = true
  337. dirBack = false
  338. if Args[3] == "right" then
  339. tRight = true
  340. qDirRight = true
  341. dir = { [1]="+X", [2]="+Y", [3]="-X", [4]="-Y" }
  342. else
  343. tRight = false
  344. qDirRight = false
  345. dir = { [1]="+X", [2]="-Y", [3]="-X", [4]="+Y" }
  346. end
  347. demx = tonumber(Args[1]) - 1
  348. demy = tonumber(Args[2]) - 1
  349. quarry()
  350. else
  351. printUsage()
  352. end
  353. end
  354. end
Advertisement
Add Comment
Please, Sign In to add comment