Advertisement
Guest User

Untitled

a guest
Sep 1st, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.20 KB | None | 0 0
  1. args = { ... }
  2. -- size depth [startDepth]
  3.  
  4. direction = {}
  5. direction.FORWARD = 0
  6. direction.RIGHT = 1
  7. direction.BACKWARD = 2
  8. direction.LEFT = 3
  9.  
  10. task = {}
  11. task.MINESHAFT = 0
  12. task.MINEXZ = 1
  13. task.MINEXZ_TURN = 2
  14. task.NEXT_LEVEL = 3
  15. task.NEXT_LEVEL_TURN = 4
  16. task.FINISH = 5
  17. task.UNLOAD = 6
  18. task.DIG = 7
  19.  
  20. turnDirection = {
  21. LEFT = -1,
  22. RIGHT = 1
  23. }
  24.  
  25. local size = 0
  26. local depth = 0
  27. local startDepth = 0
  28.  
  29. local facing = direction.FORWARD
  30. local currTask = task.MINESHAFT
  31. local xzTurnMoves = 0
  32. local mineDirection = 0
  33. local mineX = 1
  34. local mineZ = 1
  35. local nextLevel = 1 -- counter for digging down to the next mining level
  36. local level = 1
  37.  
  38. local stepsForward = 0
  39. local stepsRight = 0
  40. local stepsDown = 0
  41.  
  42. local savedForward = 0
  43. local savedRight = 0
  44. local savedDown = 0
  45. local savedFacing = 0
  46.  
  47. local isPaused = false
  48.  
  49. function save()
  50.  
  51. local file = fs.open("snapshot", "w")
  52. file.writeLine(currTask)
  53. file.writeLine(size)
  54. file.writeLine(depth)
  55. file.writeLine(startDepth)
  56. file.writeLine(nextLevel)
  57. file.writeLine(mineDirection)
  58. file.writeLine(mineX)
  59. file.writeLine(mineZ)
  60. file.writeLine(xzTurnMoves)
  61. file.writeLine(level)
  62. file.writeLine(stepsForward)
  63. file.writeLine(stepsRight)
  64. file.writeLine(stepsDown)
  65. file.writeLine(savedForward)
  66. file.writeLine(savedRight)
  67. file.writeLine(savedDown)
  68. file.writeLine(facing)
  69. file.close()
  70. end
  71.  
  72.  
  73. function resume()
  74. local file = fs.open("snapshot", "r")
  75.  
  76. if file == nil then
  77. print("Failed to open snapshot file. Cannot resume.")
  78. return
  79. end
  80.  
  81. currTask = tonumber(file.readLine())
  82. size = tonumber(file.readLine())
  83. depth = tonumber(file.readLine())
  84. startDepth = tonumber(file.readLine())
  85. nextLevel = tonumber(file.readLine())
  86. mineDirection = tonumber(file.readLine())
  87. mineX = tonumber(file.readLine())
  88. mineZ = tonumber(file.readLine())
  89. xzTurnMoves = tonumber(file.readLine())
  90. level = tonumber(file.readLine())
  91. stepsForward = tonumber(file.readLine())
  92. stepsRight = tonumber(file.readLine())
  93. stepsDown = tonumber(file.readLine())
  94. savedForward = tonumber(file.readLine())
  95. savedRight = tonumber(file.readLine())
  96. savedDown = tonumber(file.readLine())
  97. facing = tonumber(file.readLine())
  98.  
  99. file.close()
  100. end
  101.  
  102.  
  103. if args[1] == "resume" then
  104. resume()
  105. elseif args[1] == nil or args[2] == nil then
  106. print("size depth [startDepth]")
  107. return
  108. else
  109.  
  110. size = tonumber(args[1])
  111. depth = tonumber(args[2])
  112. startDepth = 0
  113.  
  114. if args[3] ~= nil then
  115. startDepth = tonumber(args[3])
  116. end
  117. end
  118.  
  119.  
  120. function updateFacing(turnDir)
  121.  
  122. facing = facing + turnDir
  123.  
  124. if facing > 3 then
  125. facing = 0
  126. elseif facing < 0 then
  127. facing = 3
  128. end
  129.  
  130. end
  131.  
  132.  
  133. function forward()
  134.  
  135. while not turtle.forward() do
  136. os.sleep(0)
  137. turtle.dig()
  138. end
  139.  
  140. if facing == direction.FORWARD then
  141. stepsForward = stepsForward + 1
  142. elseif facing == direction.RIGHT then
  143. stepsRight = stepsRight + 1
  144. elseif facing == direction.BACKWARD then
  145. stepsForward = stepsForward - 1
  146. elseif facing == direction.LEFT then
  147. stepsRight = stepsRight - 1
  148. end
  149.  
  150. save()
  151.  
  152. end
  153.  
  154. function down()
  155. count = 0
  156. while not turtle.down() do
  157. os.sleep(0)
  158. turtle.digDown()
  159. if not turtle.digDown() and turtle.detectDown() then
  160. currTask = task.FINISH
  161. stepsDown = stepsDown - 1
  162. print("Failed to dig down. Going home.")
  163. break
  164. end
  165. count = count + 1
  166. end
  167. stepsDown = stepsDown + 1
  168.  
  169. save()
  170. end
  171.  
  172.  
  173. function up()
  174. while not turtle.up() do
  175. os.sleep(0)
  176. turtle.digUp()
  177. end
  178. stepsDown = stepsDown - 1
  179.  
  180. save()
  181. end
  182.  
  183. function turnLeft()
  184. turtle.turnLeft()
  185. updateFacing(turnDirection.LEFT)
  186.  
  187. save()
  188. end
  189.  
  190.  
  191. function turnRight()
  192. turtle.turnRight()
  193. updateFacing(turnDirection.RIGHT)
  194.  
  195. save()
  196. end
  197.  
  198.  
  199. function setFacing(faceDir)
  200. while facing ~= faceDir do
  201. turnRight();
  202. end
  203. end
  204.  
  205. function savePosition()
  206. savedForward = stepsForward
  207. savedRight = stepsRight
  208. savedDown = stepsDown
  209. savedFacing = facing
  210. end
  211.  
  212.  
  213. function refuel()
  214.  
  215. for turn=1, 4 do
  216. local hasInventory = false
  217.  
  218. for slot=1, 16 do
  219. turtle.select(slot)
  220. turtle.refuel(slot)
  221. if turtle.suck() then
  222. hasInventory = true
  223. end
  224. while turtle.refuel(slot) do
  225. end
  226. end
  227.  
  228. if hasInventory then
  229. for slot=1, 16 do
  230. turtle.select(slot)
  231. turtle.drop()
  232. end
  233. end
  234.  
  235. turnLeft()
  236. end
  237.  
  238. if checkNeedsFuel() then
  239. currTask = task.FINISH
  240. end
  241.  
  242. end
  243.  
  244. function returnToDig()
  245. while savedDown ~= stepsDown do
  246. if savedDown < stepsDown then
  247. up()
  248. elseif savedDown > stepsDown then
  249. down()
  250. end
  251. end
  252.  
  253. if savedRight < stepsRight then
  254. setFacing(direction.LEFT)
  255. elseif savedRight > stepsRight then
  256. setFacing(direction.RIGHT)
  257. end
  258.  
  259. while savedRight ~= stepsRight do
  260. forward()
  261. end
  262.  
  263. if savedForward < stepsForward then
  264. setFacing(direction.BACKWARD)
  265. elseif savedForward > stepsForward then
  266. setFacing(direction.FORWARD)
  267. end
  268.  
  269. while savedForward ~= stepsForward do
  270. forward()
  271. end
  272.  
  273. setFacing(savedFacing)
  274.  
  275. currTask = task.MINEXZ
  276. end
  277.  
  278.  
  279. function returnHome()
  280. savePosition()
  281.  
  282. if stepsForward > 0 then
  283. setFacing(direction.BACKWARD)
  284. elseif stepsForward < 0 then
  285. setFacing(direction.FORWARD)
  286. end
  287.  
  288. while stepsForward ~= 0 do
  289. forward()
  290. end
  291.  
  292. if stepsRight > 0 then
  293. setFacing(direction.LEFT)
  294. elseif stepsRight < 0 then
  295. setFacing(direction.RIGHT)
  296. end
  297.  
  298. while stepsRight ~= 0 do
  299. forward()
  300. end
  301.  
  302. while stepsDown ~= 0 do
  303. if stepsDown > 0 then
  304. up()
  305. elseif stepsDown < 0 then
  306. down()
  307. end
  308. end
  309.  
  310. setFacing(direction.BACKWARD)
  311.  
  312. for slot=1, 16 do
  313. turtle.select(slot)
  314. turtle.drop()
  315. end
  316.  
  317.  
  318. if currTask == task.FINISH then
  319. setFacing(direction.FORWARD)
  320. return
  321. end
  322.  
  323. if currTask == task.UNLOAD then
  324. currTask = task.DIG
  325. refuel()
  326. end
  327.  
  328. end
  329.  
  330.  
  331. function isInventoryFull()
  332. local used = 0
  333.  
  334. for slot=1, 16 do
  335. used = used + turtle.getItemCount(slot)
  336. end
  337.  
  338. if used > 400 then
  339. return true
  340. end
  341.  
  342. return false
  343. end
  344.  
  345. function estimateDistanceHome()
  346. return math.abs(stepsForward) + math.abs(stepsRight) + math.abs(stepsDown) + 16;
  347. end
  348.  
  349. function checkNeedsFuel()
  350. local fuel = turtle.getFuelLevel()
  351.  
  352. if fuel ~= nil and fuel <= estimateDistanceHome() + 10 then
  353. return true
  354. end
  355.  
  356. return false
  357. end
  358.  
  359. function mineXZ()
  360.  
  361. local turnFlag = mineDirection % 2 == 1
  362.  
  363. turtle.digUp()
  364. turtle.digDown()
  365. forward()
  366. turtle.digUp()
  367. turtle.digDown()
  368.  
  369. if checkNeedsFuel() then
  370. currTask = task.UNLOAD
  371. return
  372. end
  373.  
  374. if isInventoryFull() then
  375. currTask = task.UNLOAD
  376. return
  377. end
  378.  
  379. mineZ = mineZ + 1
  380.  
  381. if mineZ >= size then
  382. mineZ = 1
  383. mineX = mineX + 1
  384. mineDirection = mineDirection + 1
  385.  
  386. if mineX > size then
  387. currTask = task.NEXT_LEVEL_TURN
  388. else
  389. currTask = task.MINEXZ_TURN
  390. end
  391. save()
  392. return
  393. end
  394.  
  395. if mineX > size then
  396. mineX = 1
  397. mineZ = 1
  398. --mineDirection = 0
  399. currTask = task.NEXT_LEVEL
  400. save()
  401. end
  402.  
  403. end
  404.  
  405. function mineXZ_Turn(moveForward)
  406.  
  407. local turnFlag = mineDirection % 2 == 1
  408.  
  409. if xzTurnMoves > 4 then
  410. xzTurnMoves = 0
  411. currTask = task.MINEXZ
  412. return
  413. end
  414.  
  415. if turnFlag then
  416. if xzTurnMoves == 0 then
  417. turnLeft()
  418. end
  419.  
  420. if xzTurnMoves == 4 then
  421. turnLeft()
  422. end
  423. else
  424. if xzTurnMoves == 0 then
  425. turnRight()
  426. end
  427.  
  428. if xzTurnMoves == 4 then
  429. turnRight()
  430. end
  431. end
  432.  
  433. if xzTurnMoves == 1 and moveForward then
  434. forward()
  435. end
  436.  
  437. if xzTurnMoves == 2 then
  438. turtle.digUp()
  439. end
  440.  
  441. if xzTurnMoves == 3 then
  442. turtle.digDown()
  443. end
  444.  
  445. xzTurnMoves = xzTurnMoves + 1
  446. end
  447.  
  448.  
  449. function digMineShaft()
  450. if stepsDown == startDepth then
  451. currTask = task.MINEXZ
  452. return
  453. end
  454.  
  455. down()
  456.  
  457. end
  458.  
  459.  
  460. function mineNextLevel()
  461.  
  462. if level >= depth then
  463. currTask = task.FINISH
  464. return
  465. end
  466.  
  467. if nextLevel > 3 then
  468.  
  469. mineX = 1
  470. mineZ = 1
  471. mineDirection = mineDirection + 1
  472.  
  473. level = level + 1
  474. currTask = task.MINEXZ
  475. nextLevel = 1
  476. turtle.digDown()
  477. return
  478. end
  479.  
  480. nextLevel = nextLevel + 1
  481. down()
  482. end
  483.  
  484. function processMessages()
  485. rednet.open("right")
  486. local senderId, message = rednet.receive(0.1)
  487. if message == nil then
  488. return
  489. end
  490.  
  491. print("Received " .. message)
  492.  
  493. if message == "finishMining" then
  494. isPaused = false
  495. currTask = task.FINISH
  496. print("Received command to finish.")
  497. end
  498.  
  499. if message == "unloadMining" then
  500. isPaused = false
  501. currTask = task.UNLOAD
  502. print("Received command to unload inventory at home base.")
  503. end
  504.  
  505. if message == "pauseMining" then
  506. isPaused = true
  507. save()
  508. end
  509.  
  510. if message == "resumeMining" then
  511. isPaused = false
  512. end
  513.  
  514. rednet.send(senderId, "acknowledged")
  515.  
  516. end
  517.  
  518. while true do
  519.  
  520. if not isPaused then
  521.  
  522. if currTask == task.MINESHAFT then
  523. digMineShaft()
  524. end
  525.  
  526. if currTask == task.MINEXZ then
  527. mineXZ()
  528. end
  529.  
  530. if currTask == task.MINEXZ_TURN then
  531. mineXZ_Turn(true)
  532. end
  533.  
  534. if currTask == task.NEXT_LEVEL then
  535. mineNextLevel()
  536. end
  537.  
  538. if currTask == task.NEXT_LEVEL_TURN then
  539. mineXZ_Turn(false)
  540.  
  541. if currTask == task.MINEXZ then
  542. currTask = task.NEXT_LEVEL
  543. end
  544. end
  545.  
  546. if currTask == task.UNLOAD then
  547. returnHome()
  548. end
  549.  
  550. if currTask == task.DIG then
  551. returnToDig()
  552. end
  553.  
  554. if currTask == task.FINISH then
  555. break
  556. end
  557.  
  558. end
  559.  
  560. processMessages()
  561.  
  562. if not isPaused then
  563. save()
  564. end
  565.  
  566. end
  567.  
  568. returnHome()
  569. setFacing(direction.FORWARD)
  570. save()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement