Jharakn

Miner

Jan 6th, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.66 KB | None | 0 0
  1. -- *********************************
  2. -- Room Miner Turtle Program
  3. -- By Jharakn
  4. -- *********************************
  5.  
  6.  
  7.  
  8.  
  9.  
  10. -- *********************** --
  11. -- Create Global Variables --
  12. -- *********************** --
  13.  
  14. -- using computercraft GPS nomenclature so x is north/south y is east/west and z is up/down
  15.  
  16. current = {x=0, y=0, z=0, d=0}
  17.  
  18. home = {x=0, y=0, z=0}
  19.  
  20. --dig point that the turtle was last at, might be a little out of date due to the writescheduler
  21. dig = {x=0, y=0, z=0, d=0}
  22.  
  23. -- defined at startup these boundaries mark the edge of the room
  24. boundary = {xMax=0, xMin=0, yMax=0, yMin=0, zMax=0, zMin=0}
  25.  
  26.  
  27. -- these directions will be updated with the gps function, numbers set to absolute
  28. -- values based on north/easet/south/west values
  29. direction = {forward=0, right=0, back=0, left =0}
  30.  
  31. -- various operation flags for the turtle
  32. flag = {xReverse=false, yReverse=false, atHome=false, digging=true, digComplete=false}
  33.  
  34. -- misc variables for the turtle
  35. filterSlots = 1
  36.  
  37. -- misc variables that are not written to the save file
  38. writeSchedulerCounter = 0
  39.  
  40.  
  41. -- ********************************************************************** --
  42. -- close the program dispaying error message the function was called with --
  43. -- ********************************************************************** --
  44. function errorStop(displayMessage)
  45.  
  46. broadcast("Error - "..displayMessage)
  47.  
  48. print("")
  49. print("turtle encountered an error and stopped")
  50. print("message recieved was:")
  51. print(displayMessage)
  52. print("")
  53. print("press r to restart")
  54. while true do
  55. local event, character = os.pullEvent()
  56. if event == "char" and character == "r" then break end
  57. end
  58. os.reboot()
  59. end
  60.  
  61. -- ****************************************************** --
  62. -- prints a debug message of most of the global variables --
  63. -- call with true to end the program too --
  64. -- ****************************************************** --
  65. function debugger(endProgram)
  66. term.clear()
  67. term.setCursorPos(1, 1)
  68. print("x coords")
  69. print("xMin:", boundary.xMin, " current.x:", current.x, " xMax:", boundary.xMax)
  70. print("y coords")
  71. print("yMin:", boundary.yMin, " current.y:", current.y, " yMax:", boundary.yMax)
  72. print("zMin:", boundary.zMin, " current.z:", current.z, " zMax:", boundary.zMax)
  73. print("flag.xReverse=", flag.xReverse, " flag.yReverse=", flag.yReverse)
  74. print("current.d:", current.d)
  75.  
  76. if endProgram == true then
  77. errorStop("debugger wanted to close program")
  78. end
  79. end
  80.  
  81. -- ************************************ --
  82. -- halts the program until c is pressed --
  83. -- ************************************ --
  84. function halt()
  85. while true do
  86. local event, character = os.pullEvent()
  87. if event == "char" and character == "c" then break end
  88. end
  89. end
  90.  
  91. -- **************************************************************** --
  92. -- Broadcasts a message the function was called with to the rednet --
  93. -- **************************************************************** --
  94. function broadcast(message)
  95. print(message)
  96. rednet.open("right")
  97. if (os.getComputerLabel() == nil) then
  98. rednet.broadcast(message)
  99. else
  100. -- Broadcast the message (prefixed with the turtle's id)
  101. rednet.broadcast("[".. os.getComputerLabel().."] "..message)
  102. end
  103. rednet.close("right")
  104. end
  105.  
  106. -- ************************************************************ --
  107. -- a variaty of tests to ensure the turtle hasn't wandered off --
  108. -- ************************************************************ --
  109. function positionCheck()
  110. --5 is added to the boundarys just in case the turtle is picked up and dropped outside the boundarys of the room by a player
  111. if current.x < (boundary.xMin -5) then
  112. errorStop("turtle Moved outside its boundaries")
  113. end
  114. if current.x < (boundary.xMax +5) then
  115. errorStop("turtle Moved outside its boundaries")
  116. end
  117. if current.y < (boundary.yMin -5) then
  118. errorStop("turtle Moved outside its boundaries")
  119. end
  120. if current.y < (boundary.xMax +5) then
  121. errorStop("turtle Moved outside its boundaries")
  122. end
  123. end
  124.  
  125. -- **************************** --
  126. -- Move Up and update current.z --
  127. -- **************************** --
  128. function moveUp()
  129. local counter = 0
  130. while turtle.up() == false do
  131. turtle.digUp()
  132. turtle.attackUp()
  133. counter = counter + 1
  134. if counter == 30 then break end
  135. end
  136. if counter == 100 then
  137. errorStop("timeout on dig up")
  138. else
  139. current.z = current.z + 1
  140. end
  141. end
  142.  
  143. -- ************************************************* --
  144. -- Move Down and update current.z, call with true to --
  145. -- make the function terminate early and return --
  146. -- false if it can't move for detecting bedrock --
  147. -- ************************************************* --
  148. function moveDown(bedrockDetect)
  149. local counter = 0
  150. local returnVar = true
  151. while turtle.down() == false do
  152. turtle.digDown()
  153. turtle.attackDown()
  154. counter = counter + 1
  155. if bedrockDetect == true and counter == 2 then
  156. returnVar = false
  157. break
  158. end
  159. if counter == 30 then break end
  160. end
  161. if counter == 100 then
  162. errorStop("timeout on dig down")
  163. else
  164. if returnVar == true then
  165. current.z = current.z - 1
  166. end
  167. return returnVar
  168. end
  169. end
  170.  
  171. -- ********************************************** --
  172. -- Move Forward and update current.x or current.y --
  173. -- ********************************************** --
  174. function moveForward()
  175. local counter = 0
  176. while turtle.forward() == false do
  177. turtle.dig()
  178. turtle.attack()
  179. counter = counter + 1
  180. if counter == 30 then break end
  181. end
  182. if counter == 100 then
  183. errorStop("timeout on dig forward")
  184. else
  185. if current.d == 1 then current.y = current.y + 1 end
  186. if current.d == 2 then current.x = current.x + 1 end
  187. if current.d == 3 then current.y = current.y - 1 end
  188. if current.d == 4 then current.x = current.x - 1 end
  189. end
  190. end
  191.  
  192. -- ****************************************************************** --
  193. -- Turn the turtle based on input and current.d and update current.d --
  194. -- ****************************************************************** --
  195. function turn(turnToDirection)
  196. while current.d ~= turnToDirection do
  197. if current.d < turnToDirection then
  198. if current.d == 1 and turnToDirection == 4 then
  199. turtle.turnLeft()
  200. current.d = 4
  201. else
  202. turtle.turnRight()
  203. current.d = current.d + 1
  204. end
  205. else
  206. if current.d == 4 and turnToDirection == 1 then
  207. turtle.turnRight()
  208. current.d = 1
  209. else
  210. turtle.turnLeft()
  211. current.d = current.d - 1
  212. end
  213. end
  214. end
  215. end
  216.  
  217. -- ************************************************************** --
  218. -- Goto as specific location and turn to face the right direction --
  219. -- if reverse is true the turtle matches x then y then z rather --
  220. -- than z then y then x --
  221. -- ************************************************************** --
  222. function goto(x, y, z, d, reverse)
  223. if reverse == false then
  224. while current.z ~= z do -- match z location
  225. if current.z < z then
  226. moveUp()
  227. writeScheduler()
  228. fuelCheck()
  229. elseif current.z > z then
  230. moveDown(false)
  231. writeScheduler()
  232. fuelCheck()
  233. end
  234. end
  235. elseif reverse == true then
  236. while current.x ~= x do -- match x location
  237. if current.x < x then
  238. turn(2)
  239. moveForward()
  240. writeScheduler()
  241. fuelCheck()
  242. elseif current.x > x then
  243. turn(4)
  244. moveForward()
  245. writeScheduler()
  246. fuelCheck()
  247. end
  248. end
  249. else
  250. errorStop("goto function incorrectly called")
  251. end
  252. while current.y ~= y do -- match y location
  253. if current.y < y then
  254. turn(1)
  255. moveForward()
  256. writeScheduler()
  257. fuelCheck()
  258. elseif current.y > y then
  259. turn(3)
  260. moveForward()
  261. writeScheduler()
  262. fuelCheck()
  263. end
  264. end
  265. if reverse == false then
  266. while current.x ~= x do -- match x location
  267. if current.x < x then
  268. turn(2)
  269. moveForward()
  270. writeScheduler()
  271. fuelCheck()
  272. elseif current.x > x then
  273. turn(4)
  274. moveForward()
  275. writeScheduler()
  276. fuelCheck()
  277. end
  278. end
  279. elseif reverse == true then
  280. while current.z ~= z do -- match z location
  281. if current.z < z then
  282. moveUp()
  283. writeScheduler()
  284. fuelCheck()
  285. elseif current.z > z then
  286. moveDown(false)
  287. writeScheduler()
  288. fuelCheck()
  289. end
  290. end
  291. else
  292. errorStop("goto function incorrectly called")
  293. end
  294. turn(d) -- turn to face the right direction
  295. end
  296.  
  297. -- *************************************************************************************************** --
  298. -- Use the GPS function to detect turtle position and move forward and back to detect facing direction --
  299. -- if called with true will update direction table --
  300. -- *************************************************************************************************** --
  301. function GPS(updateDirection)
  302.  
  303. local forward = 0
  304. local right = 0
  305. local back = 0
  306. local left = 0
  307.  
  308. rednet.open("right")
  309. local x1, y1, z1 = gps.locate(5,false)
  310. if x1 == nil then
  311. errorStop("no GPS signal")
  312. end
  313.  
  314. --If the turtle finds itself below layer 5 move back up to layer 5, then run the gps again
  315. --otherwise it might get blocked by bedrock
  316. if z1 < 5 then
  317. local counter = z1
  318. while counter ~= 5 do
  319. moveUp()
  320. counter = counter + 1
  321. end
  322. x1, y1, z1 = gps.locate(5,false)
  323. if x1 == nil then
  324. errorStop("no GPS signal")
  325. end
  326. end
  327.  
  328. moveForward()
  329.  
  330. local x2, y2, z2 = gps.locate(5,false)
  331. if x2 == nil then
  332. errorStop("no GPS signal")
  333. end
  334. turtle.back()
  335. rednet.close("right")
  336.  
  337. if y1 - y2 == -1 then --have taken two measurments can use the change to work out which direction the turtle moved in
  338. current.d = 1; forward = 1; right = 2; back = 3; left = 4
  339. elseif x1 - x2 == -1 then
  340. current.d = 2; forward = 2; right = 3; back = 4; left = 1
  341. elseif y1 - y2 == 1 then
  342. current.d = 3; forward = 3; right = 4; back = 1; left = 2
  343. elseif x1 - x2 == 1 then
  344. current.d = 4; forward = 4; right = 1; back = 2; left = 3
  345. else
  346. errorStop("both GPS samples are identical, what?!?!?")
  347. end
  348.  
  349. current.x = x1; current.y = y1; current.z = z1
  350.  
  351. if updateDirection == true then
  352. direction.forward = forward; direction.right = right; direction.back = back; direction.left = left
  353. end
  354. end
  355.  
  356. -- ************************************************************** --
  357. -- write the gloabal variables to a file named MinerInstructions --
  358. -- NOTE: this function should always be called with pcall --
  359. -- or a server lag spike might crash the program --
  360. -- ************************************************************** --
  361. function writeVariables()
  362. local writeTable = {} --build a table to serialise into the new file with all the global variables
  363. writeTable["current_x"] = current.x; writeTable["current_y"] = current.y; writeTable["current_z"] = current.z; writeTable["current_d"] = current.d
  364. writeTable["home_x"] = home.x; writeTable["home_y"] = home.y; writeTable["home_z"] = home.z
  365. writeTable["dig_x"] = dig.x; writeTable["dig_y"] = dig.y; writeTable["dig_z"] = dig.z; writeTable["dig_d"] = dig.d
  366. writeTable["boundary_xMax"] = boundary.xMax; writeTable["boundary_xMin"] = boundary.xMin; writeTable["boundary_yMax"] = boundary.yMax; writeTable["boundary_yMin"] = boundary.yMin; writeTable["boundary_zMax"] = boundary.zMax; writeTable["boundary_zMin"] = boundary.zMin
  367. writeTable["direction_forward"] = direction.forward; writeTable["direction_right"] = direction.right; writeTable["direction_back"] = direction.back; writeTable["direction_left"] = direction.left
  368. writeTable["flag_xReverse"] = flag.xReverse; writeTable["flag_yReverse"] = flag.yReverse; writeTable["flag_atHome"] = flag.atHome; writeTable["flag_digging"] = flag.digging; writeTable["flag_digComplete"] = flag.digComplete
  369. writeTable["filterSlots"] = filterSlots
  370.  
  371. local file = fs.open("MinerInstructions","w") --serialise and write the writeTable variable
  372. file.write(textutils.serialize(writeTable))
  373. file.close()
  374. end
  375.  
  376. -- ******************************************************************** --
  377. -- calls the write function ever nth time, designed to prevent constant --
  378. -- write calls generating server lag --
  379. -- ******************************************************************** --
  380. function writeScheduler()
  381. --adjust this variable for delay to instruction saving, higher value = less server lag but a greater error when the turtle is restarting
  382. local saveDelay = 10
  383.  
  384. writeSchedulerCounter = writeSchedulerCounter + 1
  385. if writeSchedulerCounter == saveDelay then
  386. if pcall(writeVariables) == false then
  387. broadcast("Warning - Write failure")
  388. end
  389. writeSchedulerCounter = 0
  390. end
  391. end
  392.  
  393. -- *********************************************************************************************************************************** --
  394. -- Start the program, checking for an existing MinerInstructions file, if it exists it will detect its location with gps and continue --
  395. -- otherwise it will ask the user for a new instruction set, check its gps location and begin --
  396. -- *********************************************************************************************************************************** --
  397. function startup()
  398. if fs.exists("MinerInstructions") == true then --existing instructions startup
  399. --Read the dig instructions file and update global variables
  400. local file = fs.open("MinerInstructions","r")
  401. local data = file.readAll()
  402. file.close()
  403. local readTable = {}
  404. readTable = textutils.unserialize(data)
  405. current.x = readTable["current_x"]; current.y = readTable["current_y"]; current.z = readTable["current_z"]; current.d = readTable["current_d"]
  406. home.x = readTable["home_x"]; home.y = readTable["home_y"]; home.z = readTable["home_z"]
  407. dig.x = readTable["dig_x"]; dig.y = readTable["dig_y"]; dig.z = readTable["dig_z"]; dig.d = readTable["dig_d"]
  408. boundary.xMax = readTable["boundary_xMax"]; boundary.xMin = readTable["boundary_xMin"]; boundary.yMax = readTable["boundary_yMax"]; boundary.yMin = readTable["boundary_yMin"]; boundary.zMax = readTable["boundary_zMax"]; boundary.zMin = readTable["boundary_zMin"]
  409. direction.forward = readTable["direction_forward"]; direction.right = readTable["direction_right"]; direction.back = readTable["direction_back"]; direction.left = readTable["direction_left"]
  410. flag.xReverse = readTable["flag_xReverse"]; flag.yReverse = readTable["flag_yReverse"]; flag.atHome = readTable["flag_atHome"]; flag.digging = readTable["flag_digging"]; flag.digComplete = readTable["flag_digComplete"]
  411. filterSlots = readTable["filterSlots"]
  412.  
  413. --examine the atHome flag, if true a gps cycle might cause the turtle to destroy a chest so we move up a space and run the gps instead
  414. if flag.atHome == true then
  415. moveUp()
  416. GPS()
  417. moveDown(false)
  418. elseif flag.atHome == false then
  419. GPS()
  420. else
  421. errorStop("corrupted GPS flag at Startup")
  422. end
  423.  
  424. --examine the digging flag, if its true return to the last known dig point and continue
  425. --if its false we can assume it was doing an unload cycle, so do that which will automatically
  426. --return to the dig point on completion
  427. if flag.digging == true then
  428. goto(dig.x, dig.y, dig.z, dig.d, false)
  429. elseif flag.digging == false then
  430. unloadCycle()
  431. else
  432. errorStop("corrupted dig flag at startup")
  433. end
  434.  
  435. broadcast("Resuming existing dig")
  436.  
  437. else -- new room startup
  438. term.clear() --ask the user to enter a room size
  439. term.setCursorPos(1, 1)
  440. print("***********************************")
  441. print("** Room Miner Program by Jharakn **")
  442. print("***********************************")
  443. print("")
  444. print("No previous instruction set found, starting new room:")
  445. print("Please enter your room width")
  446. local digWidth = (tonumber(read()) - 1) --need a data validation step here
  447. print("Thank you, please enter your room depth")
  448. local digDepth = (tonumber(read()) - 1) --need a data validation step here
  449. print("Thank you, finally please enter a room Height")
  450. local digHeight = (tonumber(read()) - 1) --need a data validation step here
  451. print("Thank you, Starting Program")
  452. sleep(3)
  453. term.clear()
  454. term.setCursorPos(1, 1)
  455.  
  456. GPS(true) --grab the turtle location and update its direction table
  457.  
  458. home.x = current.x; home.y = current.y; home.z = current.z --make the home locations
  459. dig.x = current.x; dig.y = current.y; dig.z = current.z; dig.d = current.d --make the dig locations here too
  460.  
  461. if current.d == 1 then --update the boundaries and flags based on the turtles current direction
  462. boundary.xMax = current.x + digWidth
  463. boundary.xMin = current.x
  464. boundary.yMax = current.y + digDepth
  465. boundary.yMin = current.y
  466. boundary.zMax = current.z + digHeight
  467. boundary.zMin = current.z
  468. flag.xReverse = false
  469. flag.yReverse = false
  470. flag.atHome = false; flag.digging = true; flag.digComplete = false
  471. elseif current.d == 2 then
  472. boundary.xMax = current.x + digDepth
  473. boundary.xMin = current.x
  474. boundary.yMax = current.y
  475. boundary.yMin = current.y - digWidth
  476. boundary.zMax = current.z + digHeight
  477. boundary.zMin = current.z
  478. flag.xReverse = false
  479. flag.yReverse = true
  480. flag.atHome = false; flag.digging = true; flag.digComplete = false
  481. elseif current.d == 3 then
  482. boundary.xMax = current.x
  483. boundary.xMin = current.x - digWidth
  484. boundary.yMax = current.y
  485. boundary.yMin = current.y - digDepth
  486. boundary.zMax = current.z + digHeight
  487. boundary.zMin = current.z
  488. flag.xReverse = true
  489. flag.yReverse = true
  490. flag.atHome = false; flag.digging = true; flag.digComplete = false
  491. elseif current.d == 4 then
  492. boundary.xMax = current.x
  493. boundary.xMin = current.x - digDepth
  494. boundary.yMax = current.y + digWidth
  495. boundary.yMin = current.y
  496. boundary.zMax = current.z + digHeight
  497. boundary.zMin = current.z
  498. flag.xReverse = true
  499. flag.yReverse = false
  500. flag.atHome = false; flag.digging = true; flag.digComplete = false
  501. else
  502. errorStop("corrupted current.d value in startup")
  503. end
  504. moveUp()
  505. broadcast("Digging New Room")
  506. end
  507. end
  508.  
  509. -- ************************************************************************ --
  510. -- will check the layers above and below the turtle, if they are within the --
  511. -- boundaries they will be dug out --
  512. -- ************************************************************************ --
  513. function digger()
  514. --detect below the turtle and dig
  515. local counter = 0
  516. while turtle.detectDown() == true do
  517. turtle.digDown()
  518. turtle.attackDown()
  519. counter = counter + 1
  520. if counter == 100 then
  521. errorStop("digger timed out on dig down")
  522. end
  523. end
  524.  
  525. --detect above the turtle and dig only if the layer above is within the boundarys
  526. if current.z + 1 <= boundary.zMax then
  527. local counter = 0
  528. while turtle.detectUp() == true do
  529. turtle.digUp()
  530. turtle.attackUp()
  531. counter = counter + 1
  532. if counter == 100 then
  533. errorStop("digger timed out on dig up")
  534. end
  535. end
  536. end
  537. end
  538. -- *************************************************************************************** --
  539. -- Refuels the turtle if low on fuel from slot 16, returns false if slot 16 is running low --
  540. -- *************************************************************************************** --
  541. function fuelCheck()
  542. if turtle.getFuelLevel() < 10 then
  543. turtle.select(16)
  544. turtle.refuel(1)
  545. turtle.select(1)
  546. end
  547. if turtle.getItemCount(16) < 10 then
  548. broadcast("Fuel Low - returning home")
  549. return false
  550. else
  551. return true
  552. end
  553. end
  554.  
  555. -- ********************************************************************************************** --
  556. -- checks slot 15 in the inventory, if it has an item the turtle needs unloading so returns false --
  557. -- ********************************************************************************************** --
  558. function invCheck()
  559. if turtle.getItemCount(15) == 0 then
  560. return true
  561. else
  562. broadcast("Inventory full - returning home")
  563. return false
  564. end
  565. end
  566.  
  567. -- ************************************************************************************************* --
  568. -- Returns home to unload its inventory and top up its fuel supplies then return to the mining point --
  569. -- ************************************************************************************************* --
  570. function unloadCycle()
  571.  
  572. --update the turtle objective just in case the chunk unloads whilst its traveling home
  573. --and commit these changes to memory this will also update the dig locations so its
  574. --got the freshest locations
  575. flag.digging = false
  576. pcall(writeVariables)
  577.  
  578. --return home
  579. goto(home.x, home.y, home.z, direction.back, false)
  580.  
  581. --update flag.atHome so if it loads again at this point it won't eat its output chest
  582. flag.atHome = true
  583. pcall(writeVariables)
  584.  
  585. --output all of the items except fuel in slot 16
  586. for i = 1, 15 do
  587. turtle.select(i)
  588. if turtle.getItemCount(i) ~= 0 then
  589. if turtle.drop() == false then --if it can't drop in the chest drop on the floor instead
  590. broadcast("Warning - Output Chest Full")
  591. turtle.dropDown()
  592. end
  593. end
  594. end
  595.  
  596. -- turn to face the fuel chest and top off slot 16
  597. turn(direction.left)
  598. turtle.select(15)
  599. if turtle.suck() == false then
  600. broadcast("Warning - Fuel chest empty")
  601. end
  602. turtle.transferTo(16, turtle.getItemSpace(16))
  603. turtle.drop()
  604. turtle.select(1)
  605.  
  606. -- update the turtle objective and return to the dig point
  607. flag.digging = true
  608. flag.atHome = false
  609. pcall(writeVariables)
  610. goto(dig.x, dig.y, dig.z, dig.d, true)
  611. end
  612.  
  613.  
  614.  
  615.  
  616.  
  617.  
  618.  
  619. -- **************************** --
  620. -- MAIN PROGRAM --
  621. -- **************************** --
  622.  
  623. ----------------------
  624. -- Phase 1: Startup --
  625. ----------------------
  626. broadcast("Starting Program")
  627. if fuelCheck() == false then --preflight checks, needs at least 2 fuel for the gps function called in startup()
  628. errorStop("turtle has no fuel in slot 16")
  629. end
  630.  
  631. startup()
  632. pcall(writeVariables)
  633.  
  634. --debugger(true)
  635.  
  636. ---------------------------
  637. -- Phase 2: Build Room --
  638. ---------------------------
  639. local yMoveSkip = true --yMoveSkip used to prevent a move in the y direction when starting a new z layer
  640. if flag.digComplete == false then digger() end --Dig the very first square the turtle is sat at
  641. while flag.digComplete == false do -- loop for z
  642. if flag.yReverse == false then
  643. while current.y ~= boundary.yMax do --loop for yReverse == false
  644. if yMoveSkip == false then
  645. turn(1)
  646. moveForward()
  647. dig.x = current.x; dig.y = current.y; dig.z = current.z; dig.d = current.d
  648. writeScheduler()
  649. digger()
  650. if fuelCheck() == false or invCheck() == false then unloadCycle() end
  651. end
  652.  
  653. if flag.xReverse == false then
  654. turn(2)
  655. while current.x ~= boundary.xMax do --loop for xReverse == false (in yReverse == false)
  656. moveForward()
  657. dig.x = current.x; dig.y = current.y; dig.z = current.z; dig.d = current.d
  658. writeScheduler()
  659. digger()
  660. if fuelCheck() == false or invCheck() == false then unloadCycle() end
  661. end
  662. elseif flag.xReverse == true then
  663. turn(4)
  664. while current.x ~= boundary.xMin do --loop for xReverse == true (in yReverse == false)
  665. moveForward()
  666. dig.x = current.x; dig.y = current.y; dig.z = current.z; dig.d = current.d
  667. writeScheduler()
  668. digger()
  669. if fuelCheck() == false or invCheck() == false then unloadCycle() end
  670. end
  671. else
  672. errorStop("xReverse flag corrupted")
  673. end
  674.  
  675. if flag.xReverse == true then -- flip the xReverse flag
  676. flag.xReverse = false
  677. else
  678. flag.xReverse = true
  679. end
  680.  
  681. if yMoveSkip == true then yMoveSkip = false end -- remove the yMoveSkip
  682. end
  683. elseif flag.yReverse == true then
  684. while current.y ~= boundary.yMin do --loop for yReverse == true
  685. if yMoveSkip == false then
  686. turn(3)
  687. moveForward()
  688. dig.x = current.x; dig.y = current.y; dig.z = current.z; dig.d = current.d
  689. writeScheduler()
  690. digger()
  691. if fuelCheck() == false or invCheck() == false then unloadCycle() end
  692. end
  693.  
  694. if flag.xReverse == false then
  695. turn(2)
  696. while current.x ~= boundary.xMax do --loop for xReverse == false (in yReverse == true)
  697. moveForward()
  698. dig.x = current.x; dig.y = current.y; dig.z = current.z; dig.d = current.d
  699. writeScheduler()
  700. digger()
  701. if fuelCheck() == false or invCheck() == false then unloadCycle() end
  702. end
  703. elseif flag.xReverse == true then
  704. turn(4)
  705. while current.x ~=boundary.xMin do --loop for xReverse == true (in yReverse == true)
  706. moveForward()
  707. dig.x = current.x; dig.y = current.y; dig.z = current.z; dig.d = current.d
  708. writeScheduler()
  709. digger()
  710. if fuelCheck() == false or invCheck() == false then unloadCycle() end
  711. end
  712. else
  713. errorStop("xReverse flag corrupted")
  714. end
  715.  
  716. if flag.xReverse == true then -- flip the xReverse flag
  717. flag.xReverse = false
  718. else
  719. flag.xReverse = true
  720. end
  721.  
  722. if yMoveSkip == true then yMoveSkip = false end -- remove the yMoveSkip
  723. end
  724. else
  725. errorStop("yReverse flag corrupted")
  726. end
  727.  
  728. if flag.yReverse == false then -- flip the yReverse flag
  729. flag.yReverse = true
  730. else
  731. flag.yReverse = false
  732. end
  733.  
  734. --If the loop compleates at boundary.zMax + 1 (because of digger) then were all done
  735. if current.z + 1 >= boundary.zMax then flag.digComplete = true end
  736.  
  737. -- keep moving the turtle until unless it hits the zMax
  738. if current.z + 1 <= boundary.zMax then moveUp() end
  739. if current.z + 1 <= boundary.zMax then moveUp() end
  740. if current.z + 1 <= boundary.zMax then moveUp() end
  741.  
  742. yMoveSkip = true
  743. dig.x = current.x; dig.y = current.y; dig.z = current.z; dig.d = current.d
  744. writeScheduler()
  745. digger()
  746. pcall(writeVariables)
  747.  
  748. broadcast("Starting digging on level "..current.z)
  749. end
  750.  
  751.  
  752.  
  753. -----------------------
  754. -- Phase 3: shutdown --
  755. -----------------------
  756. broadcast("Room finished - returning home")
  757. goto(home.x, home.y, home.z, direction.back, false)
  758.  
  759. --final unload of inventory items
  760. for i = 1, 15 do
  761. turtle.select(i)
  762. if turtle.getItemCount(i) ~= 0 then
  763. if turtle.drop() == false then --if it can't drop in the chest drop on the floor instead
  764. broadcast("Warning - Output Chest Full")
  765. turtle.dropDown()
  766. end
  767. end
  768. end
  769.  
  770. --remove the Miner Instructions file as its no longer needed
  771. fs.delete("MinerInstructions")
  772.  
  773. broadcast("Turtle Finished")
Advertisement
Add Comment
Please, Sign In to add comment