DrFair

Quarry Mk5 nofuel (resume)

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