Jharakn

Mining Turtle Mk 3

Jul 3rd, 2013
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.10 KB | None | 0 0
  1. -- ###########################################################################
  2. -- General Digging Program ###################################################
  3. -- By Jharakn ################################################################
  4. -- ###########################################################################
  5.  
  6. -- Changelog --
  7. -- Ver 0.1 alpha
  8. -- Ver 0.2 moved bedrock clearance to lvl 6
  9.  
  10.  
  11. -- ###########################################################################
  12. -- ###########################################################################
  13. -- ##
  14. -- Global Variables ##
  15. -- ##
  16.  
  17. --define the location and boundary global variables
  18. position = {x=0, y=0, z=0}
  19. boundary = {xMin=0, yMin=0, zMin=0, xMax=0, yMax=0, zMax=0}
  20. -- define the direction the turtle is facing
  21. facing = "forward"
  22.  
  23. --define the various flags for quarrying
  24. xInvert = nil
  25. yInvert = nil
  26. zInvert = nil
  27. newLayer = nil
  28. filterSlots = nil
  29. clearBedrock = nil
  30.  
  31. -- ###########################################################################
  32. -- ###########################################################################
  33. -- ##
  34. -- Error Handling and Com Chatter Functions ##
  35. -- ##
  36.  
  37. function crash(message)
  38. term.clear()
  39. term.setCursorPos(1,1)
  40. print(message)
  41. print("")
  42. print("Press Any Key to Restart")
  43. while true do
  44. local event, character = os.pullEvent()
  45. if event == "char" then os.reboot() end
  46. end
  47. end
  48.  
  49. function debugPrint()
  50. term.clear()
  51. term.setCursorPos(1,1)
  52. print("x locations")
  53. print(boundary.xMin.." -- "..position.x.." -- "..boundary.xMax)
  54. print("y locations")
  55. print(boundary.yMin.." -- "..position.y.." -- "..boundary.yMax)
  56. print("z locations")
  57. print(boundary.zMin.." -- "..position.z.." -- "..boundary.zMax)
  58. sleep(10)
  59. end
  60.  
  61. function display(message, clearscreen)
  62. if clearscreen then
  63. term.clear()
  64. term.setCursorPos(1,1)
  65. end
  66.  
  67. print("["..os.time().."] "..message)
  68. end
  69.  
  70. -- ###########################################################################
  71. -- ###########################################################################
  72. -- ##
  73. -- Turning and Basic Movement ##
  74. -- ##
  75.  
  76. --turns the turtle the the called direction updating the facing variable
  77. function turn(direction)
  78. if direction == "forward" then
  79. if facing == "forward" then
  80. --facing the right direction don't need to do anything
  81. elseif facing == "right" then
  82. turtle.turnLeft()
  83. facing = "forward"
  84. elseif facing == "back" then
  85. turtle.turnLeft()
  86. turtle.turnLeft()
  87. facing = "forward"
  88. elseif facing == "left" then
  89. turtle.turnRight()
  90. facing = "forward"
  91. else
  92. crash("incorrect facing value in turn function")
  93. end
  94. elseif direction == "right" then
  95. if facing == "forward" then
  96. turtle.turnRight()
  97. facing = "right"
  98. elseif facing == "right" then
  99. --facing the right direction don't need to do anything
  100. elseif facing == "back" then
  101. turtle.turnLeft()
  102. facing = "right"
  103. elseif facing == "left" then
  104. turtle.turnLeft()
  105. turtle.turnLeft()
  106. facing = "right"
  107. else
  108. crash("incorrect facing value in turn function")
  109. end
  110. elseif direction == "back" then
  111. if facing == "forward" then
  112. turtle.turnLeft()
  113. turtle.turnLeft()
  114. facing = "back"
  115. elseif facing == "right" then
  116. turtle.turnRight()
  117. facing = "back"
  118. elseif facing == "back" then
  119. --facing the right direction don't need to do anything
  120. elseif facing == "left" then
  121. turtle.turnLeft()
  122. facing = "back"
  123. else
  124. crash("incorrect facing value in turn function")
  125. end
  126. elseif direction == "left" then
  127. if facing == "forward" then
  128. turtle.turnLeft()
  129. facing = "left"
  130. elseif facing == "right" then
  131. turtle.turnLeft()
  132. turtle.turnLeft()
  133. facing = "left"
  134. elseif facing == "back" then
  135. turtle.turnRight()
  136. facing = "left"
  137. elseif facing == "left" then
  138. --facing the right direction don't need to do anything
  139. else
  140. crash("incorrect facing value in turn function")
  141. end
  142. else
  143. crash("incorrect direction value in turn function")
  144. end
  145. end
  146.  
  147. --dig and move forward one space updating the position based on the current facing direction
  148. function digForward()
  149. local counter = 0
  150. while true do
  151. if turtle.forward() then
  152. if facing == "forward" then position.x = position.x + 1 end
  153. if facing == "right" then position.y = position.y + 1 end
  154. if facing == "back" then position.x = position.x - 1 end
  155. if facing == "left" then position.y = position.y - 1 end
  156. do return true end
  157. break
  158. else
  159. turtle.dig()
  160. turtle.attack()
  161. counter = counter + 1
  162. end
  163. if counter == 100 then
  164. do return false end
  165. break
  166. end
  167. end
  168. end
  169.  
  170. --dig and move up one space updating the position
  171. function digUp()
  172. local counter = 0
  173. while true do
  174. if turtle.up() then
  175. position.z = position.z + 1
  176. do return true end
  177. break
  178. else
  179. turtle.digUp()
  180. turtle.attackUp()
  181. counter = counter + 1
  182. end
  183. if counter == 100 then
  184. do return false end
  185. break
  186. end
  187. end
  188. end
  189.  
  190. --dig and move down one space updating the position
  191. function digDown()
  192. local counter = 0
  193. while true do
  194. if turtle.down() then
  195. position.z = position.z - 1
  196. do return true end
  197. break
  198. else
  199. turtle.digDown()
  200. turtle.attackDown()
  201. counter = counter + 1
  202. end
  203. if counter == 100 then
  204. do return false end
  205. break
  206. end
  207. end
  208. end
  209.  
  210. -- ###########################################################################
  211. -- ###########################################################################
  212. -- ##
  213. -- Inventory, Fuel and Space Checking ##
  214. -- ##
  215.  
  216. --check the turtles fuel end fill it up if needing
  217. function checkFuel()
  218. local counter = 0
  219. --check the turtles current fuel level if its less that 100 get more
  220. if turtle.getFuelLevel() < 100 then
  221. --chat about it
  222. display("Getting More Fuel", false)
  223. --select the fuel chest
  224. turtle.select(16)
  225. --try to place the chest above the turtle, if it fails mine or attack above to clear the space
  226. while true do
  227. if turtle.placeUp() then
  228. break
  229. else
  230. counter = counter + 1
  231. turtle.digUp()
  232. turtle.attackUp()
  233. --if it can't mine or clear above the turtle then crash
  234. if counter == 100 then crash("error placing chest") end
  235. end
  236. end
  237. --suck some fuel from the chest
  238. turtle.suckUp()
  239. --count how much fuel its taken and return all but 1 of the fuel back to the chest refueling with whats left
  240. --if it sucked nothing the fuel chest is empty so return home to prevent getting lost
  241. if turtle.getItemCount(16) > 1 then
  242. turtle.dropUp(turtle.getItemCount(16) - 1)
  243. turtle.refuel()
  244. elseif turtle.getItemCount(16) == 0 then
  245. print("Fuel chest empty - returning home")
  246. --need a return home function
  247. else
  248. turtle.refuel()
  249. end
  250. --select slot 16 again and pick the chest back up in that space
  251. turtle.select(16)
  252. if turtle.digUp() == false then crash("error picking up chest") end
  253. --select the first slot after the filters to being mining
  254. turtle.select(1)
  255. end
  256. end
  257.  
  258. --check the turtles inventory and empty it if its full
  259. function checkSpace(force)
  260. local counter = 0
  261. -- if theres stuff in slot 14 the inventory must be full
  262. if turtle.getItemCount(14) ~= 0 or force == true then
  263. --chat about it
  264. display("Emptying the Inventory", false)
  265. --select the unload chest
  266. turtle.select(15)
  267. --try to place the chest above the turtle, if it fails mine or attack above to clear the space
  268. while true do
  269. if turtle.placeUp() then
  270. break
  271. else
  272. counter = counter + 1
  273. turtle.digUp()
  274. turtle.attackUp()
  275. --if it can't mine or clear above the turtle then crash
  276. if counter == 100 then crash("error placing chest") end
  277. end
  278. end
  279. --unload the inventory except the filter slots to the chest
  280. for i = filterSlots + 1, 14 do
  281. turtle.select(i)
  282. turtle.dropUp()
  283. end
  284. --select slot 15 again and pick the chest back up in that space
  285. turtle.select(15)
  286. if turtle.digUp() == false then crash("error picking up chest") end
  287. --select the first slot after the filters to being mining
  288. turtle.select(1)
  289. end
  290. end
  291.  
  292. --clears the blocks above and below the turtle, if they don't match the filter or there is none
  293. function digAroundTurtle()
  294. --if filterSlots is 0 there is not filter so always clear out
  295. if filterSlots == 0 then
  296. --check were not digging outside of our boundarys
  297. if position.z < boundary.zMax then
  298. --if we detect something mine it
  299. while turtle.detectUp() do
  300. turtle.digUp()
  301. end
  302. end
  303. --check were not digging outside of our boundarys
  304. if position.z > boundary.zMin then
  305. --if we detect something mine it
  306. while turtle.detectDown() do
  307. turtle.digDown()
  308. end
  309. end
  310. --if there is a filter slots value check the blocks with the filters to see if they should be mined
  311. else
  312. local canDigUp = false
  313. local canDigDown = false
  314.  
  315. if position.z < boundary.zMax then canDigUp = true end
  316. if position.z > boundary.zMin then canDigDown = true end
  317.  
  318. if turtle.detectUp() or turtle.detectDown() then
  319. for i = 1, filterSlots do
  320. turtle.select(i)
  321. if turtle.compareUp() then canDigUp = false end
  322. if turtle.compareDown() then canDigDown = false end
  323. end
  324. end
  325.  
  326. if canDigUp then turtle.digUp() end
  327. if canDigDown then turtle.digDown() end
  328. end
  329. end
  330.  
  331. -- ###########################################################################
  332. -- ###########################################################################
  333. -- ##
  334. -- Complex Movement ##
  335. -- ##
  336.  
  337. --dig out a row travaling forwards or backwards depending on the xInvert variable
  338. function digRow()
  339. --if xInvert is true were digging backwards towards boundary.xMin
  340. if xInvert then
  341. while position.x ~= boundary.xMin do
  342. --check the fuel and inventory space
  343. checkFuel()
  344. checkSpace()
  345. --turn to face the right direction
  346. turn("back")
  347. if digForward() == false then crash("could not dig properly") end
  348. digAroundTurtle()
  349. --clear down to bedrock if the flag is true
  350. if clearBedrock then
  351. digBedrock()
  352. end
  353. end
  354. --flip the xInvert for the next row
  355. xInvert = false
  356. --if xInvert is false were digging forwards towards boundary.xMax
  357. else
  358. while position.x ~= boundary.xMax do
  359. --check the fuel and inventory space
  360. checkFuel()
  361. checkSpace()
  362. --turn to face the right direction
  363. turn("forward")
  364. if digForward() == false then crash("could not dig properly") end
  365. digAroundTurtle()
  366. --clear down to bedrock if the flag is true
  367. if clearBedrock then
  368. digBedrock()
  369. end
  370. end
  371. --flip the xInvert for the next row
  372. xInvert = true
  373. end
  374. end
  375.  
  376. --dig out a layer traveling either right or left depending on the yInvert variable and running the digRow() function after every turn
  377. function digLayer()
  378. --if yInvert is true were digging forwards towards boundary.yMin
  379. if yInvert then
  380. while position.y ~= boundary.yMin do
  381. --use newLayer to determine if were starting a new layer, if true flip it to false, if false move a space to the left
  382. if newLayer then
  383. newLayer = false
  384. else
  385. --check the fuel and inventory space
  386. checkFuel()
  387. checkSpace()
  388. --turn to face the right direction
  389. turn("left")
  390. if digForward() == false then crash("could not dig properly") end
  391. digAroundTurtle()
  392. --Clear the bedrock if required
  393. if clearBedrock then
  394. digBedrock()
  395. end
  396. end
  397. digRow()
  398. end
  399. --flip the yInvert for the next row
  400. yInvert = false
  401. --if yInvert is false were digging forwards towards boundary.yMax
  402. else
  403. while position.y ~= boundary.yMax do
  404. --use newLayer to determine if were starting a new layer, if true flip it to false, if false move a space to the right
  405. if newLayer then
  406. newLayer = false
  407. else
  408. --check the fuel and inventory space
  409. checkFuel()
  410. checkSpace()
  411. --turn to face the right direction
  412. turn("right")
  413. if digForward() == false then crash("could not dig properly") end
  414. digAroundTurtle()
  415. --Clear the bedrock if required
  416. if clearBedrock then
  417. digBedrock()
  418. end
  419. end
  420. digRow()
  421. end
  422. --flip the yInvert for the next row
  423. yInvert = true
  424. end
  425. end
  426.  
  427. function digBedrock()
  428. local counter = 0
  429.  
  430. turtle.digDown()
  431. while turtle.down() do
  432. position.z = position.z - 1
  433. turtle.digDown()
  434. counter = counter + 1
  435. end
  436. while counter ~= 0 do
  437. if digUp() == false then crash("could not dig properly") end
  438. counter = counter - 1
  439. end
  440. end
  441.  
  442. function goto(x, y, z)
  443. --return to z level 0
  444. while position.z ~= z do
  445. if position.z < z then
  446. if digUp() == false then crash("could not dig properly") end
  447. --check the fuel and inventory space
  448. checkFuel()
  449. checkSpace()
  450. end
  451. if position.z > z then
  452. if digDown() == false then crash("could not dig properly") end
  453. --check the fuel and inventory space
  454. checkFuel()
  455. checkSpace()
  456. end
  457. print(position.z)
  458. end
  459.  
  460. --return to y level
  461. while position.y ~= y do
  462. if position.y < y then
  463. turn("right")
  464. if digForward() == false then crash("could not dig properly") end
  465. --check the fuel and inventory space
  466. checkFuel()
  467. checkSpace()
  468. end
  469. if position.y > y then
  470. turn("left")
  471. if digForward() == false then crash("could not dig properly") end
  472. --check the fuel and inventory space
  473. checkFuel()
  474. checkSpace()
  475. end
  476. end
  477.  
  478. --return to x level
  479. while position.x ~= x do
  480. if position.x < x then
  481. turn("forward")
  482. if digForward() == false then crash("could not dig properly") end
  483. --check the fuel and inventory space
  484. checkFuel()
  485. checkSpace()
  486. end
  487. if position.x > x then
  488. turn("back")
  489. if digForward() == false then crash("could not dig properly") end
  490. --check the fuel and inventory space
  491. checkFuel()
  492. checkSpace()
  493. end
  494. end
  495. end
  496.  
  497. -- ###########################################################################
  498. -- ###########################################################################
  499. -- ##
  500. -- Room and Quarrying Functions ##
  501. -- ##
  502.  
  503. -- dig a quarry
  504. function digQuarry()
  505. --assign the recovery hight so if the turtle crashes it can return to where it started from
  506. local recoveryHeight = position.z
  507.  
  508. --grab some fuel if needed
  509. checkFuel()
  510.  
  511. --move down until you hit solid ground
  512. while turtle.detectDown() == false do
  513. if digDown() == false then crash("could not dig properly") end
  514. end
  515.  
  516. --work out what the nearest point would be so you hit lvl 6 moving down 3 spaces at a time
  517. local quarryStart = 6
  518. while quarryStart < position.z do
  519. quarryStart = quarryStart + 3
  520. end
  521.  
  522. --move to that point
  523. while position.z ~= quarryStart do
  524. if position.z < quarryStart then
  525. if digUp() == false then crash("could not dig properly") end
  526. elseif position.z > quarryStart then
  527. crash("Quarry Start point is lower than position.z")
  528. end
  529. end
  530.  
  531. --part 2 dig down to z level 6
  532. while position.z > boundary.zMin do
  533. if newLayer == false then
  534. if digDown() == false then crash("could not dig properly") end
  535. if digDown() == false then crash("could not dig properly") end
  536. if digDown() == false then crash("could not dig properly") end
  537. digAroundTurtle()
  538. newLayer = true
  539. end
  540. --save/update the recovery file
  541. createRecovery(position.z, recoveryHeight)
  542. display("Starting to Quarry at lvl: "..position.z, false)
  543. digLayer()
  544. end
  545.  
  546. --part 3 clear out the bedrock
  547.  
  548. --clear the bedrock where the turtle is sat
  549. digBedrock()
  550. --run the layer again clearing the bedrock this time
  551. newLayer = true
  552. clearBedrock = true
  553. display("Clearing the Bedrock", false)
  554. digLayer()
  555. clearBedrock = false
  556. end
  557.  
  558. --dig a room
  559. function digRoom()
  560. --grab some fuel if needed
  561. checkFuel()
  562.  
  563. --dig up a space to maximise the dig area
  564. if digUp() == false then crash("could not dig properly") end
  565.  
  566. while position.z < boundary.zMax do
  567. if newLayer == false then
  568. --dig up a space
  569. if digUp() == false then crash("could not dig properly") end
  570.  
  571. --dig up another 2 spaces if the boundarys allow it
  572. if position.z ~= boundary.zMax then
  573. if digUp() == false then crash("could not dig properly") end
  574. end
  575. if position.z ~= boundary.zMax then
  576. if digUp() == false then crash("could not dig properly") end
  577. end
  578. digAroundTurtle()
  579. newLayer = true
  580. end
  581. digLayer()
  582. end
  583. end
  584.  
  585. --dig a strip
  586. function digStrip()
  587. --grab some fuel if needed
  588. checkFuel()
  589.  
  590. --dig up a space to maximise the dig area
  591. if digUp() == false then crash("could not dig properly") end
  592.  
  593. while position.z < boundary.zMax do
  594. if newLayer == false then
  595. --dig up a space
  596. if digUp() == false then crash("could not dig properly") end
  597.  
  598. --dig up another 2 spaces if the boundarys allow it
  599. if position.z ~= boundary.zMax then
  600. if digUp() == false then crash("could not dig properly") end
  601. end
  602. if position.z ~= boundary.zMax then
  603. if digUp() == false then crash("could not dig properly") end
  604. end
  605. end
  606. digAroundTurtle()
  607. digRow()
  608. newLayer = false
  609.  
  610. end
  611. end
  612.  
  613. -- ###########################################################################
  614. -- ###########################################################################
  615. -- ##
  616. -- Startup and Recovery Functions ##
  617. -- ##
  618.  
  619. --start the program select what mode you want and ask questions to fill out the various variables
  620. function startup()
  621. local mode = nil
  622.  
  623. local quarryHeight
  624. local quarrySize
  625. local roomWidth
  626. local roomDepth
  627. local roomHight
  628. local stripLength
  629. local stripHight
  630.  
  631.  
  632. term.clear()
  633. term.setCursorPos(1,1)
  634. print("----------------------------")
  635. print("Multi-Purpose Digger Program")
  636. print("----------------------------")
  637. print("")
  638. sleep(1)
  639. print("(q)arry, (r)oom or (s)trip mode?")
  640. mode = read()
  641. if mode == "q" or mode == "r" or mode == "s" then
  642. --everythings ok
  643. else
  644. print("Unknown Command - Press Any Key To Restart")
  645. while true do
  646. local event, character = os.pullEvent()
  647. if event == "char" then os.reboot() end
  648. end
  649. end
  650.  
  651. if turtle.getItemCount(15) == 0 then
  652. print("Missing Offload Chest in Slot 15 - Press Any Key To Restart")
  653. while true do
  654. local event, character = os.pullEvent()
  655. if event == "char" then os.reboot() end
  656. end
  657. elseif turtle.getItemCount(16) == 0 then
  658. print("Missing Fuel Chest in Slot 16 - Press Any Key To Restart")
  659. while true do
  660. local event, character = os.pullEvent()
  661. if event == "char" then os.reboot() end
  662. end
  663. end
  664.  
  665.  
  666. if mode == "q" then
  667. term.clear()
  668. term.setCursorPos(1,1)
  669. print("----------------------------")
  670. print("Multi-Purpose Digger Program")
  671. print("----------------------------")
  672. print("")
  673. print("*** Quarry Mode ***")
  674. print("")
  675. print("")
  676. sleep(1)
  677. print("Please Enter Current Height Level")
  678. quarryHeight = tonumber(read())
  679. print("Please Enter Quarry Dimensions")
  680. quarrySize = tonumber(read())
  681.  
  682. --set the x boundaries of the excavation
  683. boundary.xMin = 0
  684. boundary.xMax = quarrySize - 1
  685. --set the y boundaries of the excavation
  686. boundary.yMin = 0
  687. boundary.yMax = quarrySize - 1
  688. --set the z boundaries of the excavation
  689. boundary.zMin = 6
  690. boundary.zMax = quarryHeight
  691.  
  692. --set position.z to the current height
  693. position.z = quarryHeight
  694.  
  695. --develop the filter
  696. if turtle.getItemCount(1) == 0 then
  697. print("Not Filtering Against Any Items, Is This OK? Y/N")
  698. local responce = read()
  699. if responce == "n" or responce == "N" then
  700. print("Press Any Key To Restart")
  701. while true do
  702. local event, character = os.pullEvent()
  703. if event == "char" then os.reboot() end
  704. end
  705. elseif responce == "y" or responce == "Y" then
  706. filterSlots = 0
  707. else
  708. print("Unknown Responce - Press Any Key To Restart")
  709. while true do
  710. local event, character = os.pullEvent()
  711. if event == "char" then os.reboot() end
  712. end
  713. end
  714. else
  715. local counter = 1
  716. while turtle.getItemCount(counter) ~= 0 do
  717. counter = counter + 1
  718. end
  719. filterSlots = counter - 1
  720. end
  721.  
  722. xInvert = false
  723. yInvert = false
  724. zInvert = true
  725. newLayer = true
  726. clearBedrock = false
  727.  
  728. return "quarry"
  729.  
  730. elseif mode == "r" then
  731. term.clear()
  732. term.setCursorPos(1,1)
  733. print("----------------------------")
  734. print("Multi-Purpose Digger Program")
  735. print("----------------------------")
  736. print("")
  737. print("*** Room Mode ***")
  738. print("")
  739. print("")
  740. sleep(1)
  741. print("Please Enter Room Width")
  742. roomWidth = tonumber(read())
  743. print("Please Enter Room Depth")
  744. roomDepth = tonumber(read())
  745. print("Please Enter Room Height")
  746. roomHight = tonumber(read())
  747.  
  748. --set the x boundaries of the excavation
  749. boundary.xMin = 0
  750. boundary.xMax = roomDepth - 1
  751. --set the y boundaries of the excavation
  752. boundary.yMin = 0
  753. boundary.yMax = roomWidth - 1
  754. --set the z boundaries of the excavation
  755. boundary.zMin = 0
  756. boundary.zMax = roomHight - 1
  757.  
  758. xInvert = false
  759. yInvert = false
  760. zInvert = false
  761. newLayer = true
  762. filterSlots = 0
  763. clearBedrock = false
  764.  
  765. return "room"
  766.  
  767. elseif mode == "s" then
  768. term.clear()
  769. term.setCursorPos(1,1)
  770. print("----------------------------")
  771. print("Multi-Purpose Digger Program")
  772. print("----------------------------")
  773. print("")
  774. print("*** Strip Mode ***")
  775. print("")
  776. print("")
  777. sleep(1)
  778. print("Please Enter Strip Length")
  779. stripLength = tonumber(read())
  780. print("Please Enter Strip Height")
  781. stripHight = tonumber(read())
  782.  
  783. --set the x boundaries of the excavation
  784. boundary.xMin = 0
  785. boundary.xMax = stripLength - 1
  786. --Don't set the y boundaries, there unused for this mode
  787. boundary.yMin = 0
  788. boundary.yMax = 0
  789. --set the z boundaries of the excavation
  790. boundary.zMin = 0
  791. boundary.zMax = stripHight - 1
  792.  
  793. xInvert = false
  794. yInvert = false
  795. zInvert = false
  796. newLayer = true
  797. filterSlots = 0
  798. clearBedrock = false
  799.  
  800. return "strip"
  801. end
  802. end
  803.  
  804. function createRecovery(currentHeight, recoveryHeight)
  805. --create a table called data and fill it with the current height and the recover height the function was called with
  806. local data = {}
  807. data["currentHeight"] = currentHeight
  808. data["recoveryHeight"] = recoveryHeight
  809.  
  810. --serialise the data table and write it to a file called "recovery.info"
  811. local file = fs.open("recovery.info","w")
  812. file.write(textutils.serialize(data))
  813. file.close()
  814. end
  815.  
  816. function recover()
  817. --if the "recovery.info" file exists then load the data into memory, delete the file and goto that point
  818. if fs.exists("recovery.info") then
  819. local file = fs.open("recovery.info","r")
  820. local data = file.readAll()
  821. file.close()
  822. fs.delete("recovery.info")
  823.  
  824. local recovery = {}
  825. recovery = textutils.unserialize(data)
  826. position.z = recovery.currentHeight
  827.  
  828. display("Recovery File Detected, Moving to Recovery Point", true)
  829. goto(0,0,recovery.recoveryHeight)
  830.  
  831. print("Arrived at Recovery Point - Press Any Key To Restart")
  832. while true do
  833. local event, character = os.pullEvent()
  834. if event == "char" then os.reboot() end
  835. end
  836. end
  837. end
  838.  
  839. -- ###########################################################################
  840. -- ###########################################################################
  841. -- ##
  842. -- Begin Program ##
  843. -- ##
  844.  
  845. local returnHeight = 0
  846.  
  847. recover()
  848.  
  849. local operation = startup()
  850. if operation == "quarry" then
  851. returnHeight = position.z
  852. display("Starting Quarry", true)
  853. digQuarry()
  854. elseif operation == "room" then
  855. display("Digging Room", true)
  856. digRoom()
  857. elseif operation == "strip" then
  858. display("Digging Strip", true)
  859. digStrip()
  860. end
  861.  
  862. --empty the inventory
  863. checkSpace(true)
  864.  
  865. --return home
  866. goto(0, 0, returnHeight)
  867.  
  868. --clear the recovery file if it exists
  869. if fs.exists("recovery.info") then fs.delete("recovery.info") end
  870.  
  871. --Print message saying all done
  872. display("Task Complete", true)
Advertisement
Add Comment
Please, Sign In to add comment