Advertisement
Guest User

Untitled

a guest
May 28th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 33.41 KB | None | 0 0
  1. --[[
  2. This program contains mining functions.
  3. version 1.1
  4. --]]
  5.  
  6.  
  7. -- Enumeration to store the the different types of message that can be written
  8. MessageLevel = { DEBUG=0, INFO=1, WARNING=2, ERROR=3, FATAL=4 }
  9. local messageOutputLevel = MessageLevel.INFO
  10.  
  11. -- Strata dimensions (X is on the RIGHT side; Z is in front)
  12. local STRATA_Z_FRONT = 10
  13. local STRATA_X_RIGHT = 20
  14.  
  15. -- Slots (needs to be initialized by each command)
  16. local Slots
  17.  
  18. -- Fuel needed for mining a full strata
  19. local FUEL_NEEDED_STRATA = 4000
  20.  
  21. -- Fuel required at fuel check
  22. local FUEL_NEEDED_AT = 4000
  23.  
  24. -- MAX Nb Tries to move
  25. local MNB_FAILED_MOVE = 35
  26.  
  27. -- LAST SLOT (must remain empty)
  28. local SLOT_LAST = 16
  29.  
  30.  
  31. -- allows disabling inventory check during
  32. -- making the shaft and transport runs
  33. local doInventoryCheck = true
  34.  
  35.  
  36. local FILENAME_MESSAGES = "messages.txt"
  37. local FILENAME_POSITION = "position.txt"
  38.  
  39. -- File run at startup
  40. local FILENAME_STARTUP = "startup"
  41.  
  42. -- File identifying the bot was rebooted prematurely
  43. -- (chunk unloaded, etc.)
  44. local FILENAME_REBOOT = "reboot_token"
  45.  
  46. -- File identifying the strata mined
  47. local FILENAME_STRATA = "strata.txt"
  48.  
  49.  
  50. -- HOME position
  51. local posHOME = { x = 0, y = 0, z = 0}
  52.  
  53. -- Variables to store the current _relative_ position of the turtle
  54. local posCurr = { x = 0, y = 0, z = 0 }
  55.  
  56.  
  57. -- movement vectors
  58. -- We use a relative x,y,z coordinate system but the axes
  59. -- do NOT align with absolute minecraft axes and positions.
  60. -- These vectors are based on the starting position
  61. -- and orientation of the turtle.
  62. local vecMOVEMENT = {
  63. FRONT1 = { x=0, y=0, z = -1},
  64. FRONT2 = { x=0, y=0, z = -2},
  65. FRONT3 = { x=0, y=0, z = -3},
  66.  
  67. BACK1 = { x=0, y=0, z = 1},
  68. BACK2 = { x=0, y=0, z = 2},
  69. BACK3 = { x=0, y=0, z = 3},
  70.  
  71. RIGHT1 = { x = 1, y=0, z=0},
  72. RIGHT2 = { x = 2, y=0, z=0},
  73. RIGHT3 = { x = 3, y=0, z=0},
  74.  
  75. LEFT1 = { x = -1, y=0, z=0},
  76. LEFT2 = { x = -2, y=0, z=0},
  77. LEFT3 = { x = -3, y=0, z=0},
  78.  
  79. UP1 = { x=0, y = 1, z=0},
  80. UP2 = { x=0, y = 2, z=0},
  81. UP3 = { x=0, y = 3, z=0},
  82.  
  83. DOWN1 = { x=0, y = -1, z=0},
  84. }
  85.  
  86.  
  87. -- Orientation enum (clockwise)
  88. local Orientation = { FRONT=0, RIGHT=1, BACK=2, LEFT=3 }
  89. local NB_ORIENTATIONS = 4
  90.  
  91. -- Variables to store the current cardinal orientation of the turtle
  92. local orientationCurr = Orientation.FRONT
  93. local vecForwardMovement1 = getMovementVector1(orientationCurr)
  94.  
  95.  
  96. -- ********************************************************************************** --
  97. -- Returns the movement vector1 of the given Orientation
  98. -- ********************************************************************************** --
  99. function getMovementVector1(orientation)
  100. if (orientation == Orientation.FRONT) then
  101. return vecMOVEMENT.FRONT1
  102. elseif (orientation == Orientation.BACK) then
  103. return vecMOVEMENT.BACK1
  104. elseif (orientation == Orientation.RIGHT) then
  105. return vecMOVEMENT.RIGHT1
  106. elseif (orientation == Orientation.LEFT) then
  107. return vecMOVEMENT.LEFT1
  108. else
  109. writeMessage("FATAL: getMovementVector1(): orientation invalid: " .. tostring(orientation), MessageLevel.FATAL)
  110. emergencyExit()
  111. end
  112. end
  113.  
  114.  
  115. -- ********************************************************************************** --
  116. -- Mark the mining program startup with some files to take
  117. -- corrective actions when a premature reboot happens.
  118. -- ********************************************************************************** --
  119. function openAppendOrCreate(filename)
  120. local file = io.open(filename, "a")
  121.  
  122. if file == nil then
  123. file = io.open(filename, "w")
  124. end
  125.  
  126. return file
  127. end
  128.  
  129.  
  130. -- ********************************************************************************** --
  131. -- Mark the mining program startup with some files to take
  132. -- corrective actions when a premature reboot happens.
  133. -- ********************************************************************************** --
  134. function startup()
  135. -- Write a new startup file to resume the turtle
  136. file = io.open(FILENAME_STARTUP, "w")
  137. file:write("\nshell.run(\"")
  138. file:write(shell.getRunningProgram())
  139. file:write("\")\n")
  140. file:close()
  141.  
  142. -- touch file to detect reboots
  143. file = openAppendOrCreate(FILENAME_REBOOT)
  144. file:close()
  145. end
  146.  
  147.  
  148. -- ********************************************************************************** --
  149. -- Mark the mining program startup with some file to take
  150. -- corrective actions when a premature reboot happens.
  151. -- ********************************************************************************** --
  152. function finish()
  153. -- delete reboot file
  154. fs.delete(FILENAME_REBOOT)
  155.  
  156. -- delete startup file
  157. fs.delete(FILENAME_STARTUP)
  158. end
  159.  
  160.  
  161. -- ********************************************************************************** --
  162. -- Write the current location to a file
  163. -- ********************************************************************************** --
  164. function savePositionAndOrientation()
  165. local file = io.open(FILENAME_POSITION , "w")
  166. file:write(textutils.serialize(posCurr) .. "\n")
  167. file:write(orientationCurr)
  168. file:write("\n")
  169. file:close()
  170. end
  171.  
  172.  
  173. -- ********************************************************************************** --
  174. -- Read our location from file
  175. -- ********************************************************************************** --
  176. function loadPositionAndOrientation()
  177. writeMessage("Attempting to load position", MessageLevel.FATAL)
  178.  
  179. if not fs.exists(FILENAME_POSITION) then
  180. writeMessage("File not found: " .. FILENAME_POSITION, MessageLevel.FATAL)
  181. return false
  182. end
  183.  
  184. local file = io.open(FILENAME_POSITION , "r")
  185. posCurr = textutils.unserialize(file:read())
  186. writeMessage("posCurr=" .. posToString(posCurr), MessageLevel.FATAL)
  187. orientationCurr = tonumber(file:read())
  188. writeMessage("orientationCurr=" .. toString(orientationCurr), MessageLevel.FATAL)
  189. file:close()
  190.  
  191. vecForwardMovement1 = getMovementVector1(orientationCurr)
  192.  
  193. return true
  194. end
  195.  
  196.  
  197. -- ********************************************************************************** --
  198. -- Appends a message to a file
  199. -- ********************************************************************************** --
  200. function appendFile(filename, msg)
  201. local file = openAppendOrCreate(filename)
  202. file:write(msg .. "\n")
  203. file:close()
  204. end
  205.  
  206.  
  207. -- ********************************************************************************** --
  208. -- Writes an output message
  209. -- ********************************************************************************** --
  210. function writeMessage(message, msgLevel)
  211. local level = tonumber(msgLevel)
  212. if not level then
  213. level = 999
  214. end
  215.  
  216. if (level >= messageOutputLevel) then
  217. print(message)
  218.  
  219. if FILENAME_MESSAGES ~= nil then
  220. -- Open file, write message and close file (flush doesn't seem to work!)
  221. local file = openAppendOrCreate(FILENAME_MESSAGES)
  222.  
  223. coords = "(" .. posToString(posCurr) .. ") "
  224.  
  225. file:write(coords .. message)
  226. file:write("\n")
  227. file:close()
  228. end
  229. end
  230. end
  231.  
  232.  
  233. -- ********************************************************************************** --
  234. -- Writes a position to string (x, y, z)
  235. -- ********************************************************************************** --
  236. function posToString(pos)
  237. return tostring(posCurr.x) .. "," .. tostring(posCurr.y) .. "," .. tostring(posCurr.z)
  238. end
  239.  
  240.  
  241. -- ********************************************************************************** --
  242. -- Selects the same item as specified slot.
  243. -- ********************************************************************************** --
  244. function selectSameAs(slot)
  245. -- Try to get same item but elsewhere
  246. for i = 1, 16 do
  247. if i ~= slot then
  248. turtle.select(i)
  249. if turtle.compareTo(slot) then
  250. writeMessage("Found item " .. slot .. " also in slot " .. i, MessageLevel.DEBUG)
  251. return true, slot
  252. end
  253. end
  254. end
  255.  
  256. -- didn't find anything use slot if possible
  257. if turtle.getItemCount(slot) > 1 then
  258. writeMessage("Using item " .. slot .. ": not other slot found", MessageLevel.DEBUG)
  259. turtle.select(slot)
  260. return true, slot
  261. else
  262. writeMessage("NOT consuming item " .. slot .. ": only 1 left", MessageLevel.DEBUG)
  263. return false, 0
  264. end
  265. end
  266.  
  267.  
  268. -- ********************************************************************************** --
  269. -- Place same item as specified slot.
  270. -- ********************************************************************************** --
  271. function placeSameAs(slot, fnPlace, fnDetect)
  272. if fnDetect() then
  273. return true
  274. end
  275.  
  276. if fnPlace == nil then
  277. fnPlace = turtle.place
  278. end
  279.  
  280. local success
  281. local slotChosen
  282. success, slotChosen = selectSameAs(slot)
  283.  
  284. if not success then
  285. turtle.select(slot)
  286. end
  287.  
  288. return fnPlace()
  289. end
  290.  
  291.  
  292. -- ********************************************************************************** --
  293. -- Function to turn the Turtle in the given orientation.
  294. -- ********************************************************************************** --
  295. function changeOrientation(newOrient)
  296. if (orientationCurr == newOrient) then
  297. return
  298. end
  299.  
  300. -- turn left?
  301. if (newOrient - orientationCurr + NB_ORIENTATIONS) % NB_ORIENTATIONS == NB_ORIENTATIONS-1 then
  302. turtle.turnLeft()
  303. orientationCurr = newOrient
  304. end
  305.  
  306. while orientationCurr ~= newOrient do
  307. turtle.turnRight()
  308. orientationCurr = (orientationCurr + 1) % NB_ORIENTATIONS
  309. end
  310.  
  311. vecForwardMovement1 = getMovementVector1(orientationCurr)
  312.  
  313. savePositionAndOrientation()
  314. end
  315.  
  316.  
  317. -- ********************************************************************************** --
  318. -- Functions to move the Turtle forward n spaces in the current orientation
  319. -- (or 1 if no n is specified)
  320. -- pushing through any gravel or other
  321. -- things such as mobs that might get in the way.
  322. -- ********************************************************************************** --
  323. function digAndMoveForward(n)
  324. preMoveChecks()
  325. return digAndMoveForwardNoCheck(n)
  326. end
  327.  
  328. function digAndMoveForwardNoCheck(n)
  329. n = tonumber(n)
  330. if n == nil then
  331. n = 1
  332. end
  333.  
  334. for i = 1,n do
  335. local success = digG(turtle.detect, turtle.dig, turtle.forward, turtle.attack, vecForwardMovement1)
  336. if not success then
  337. writeMessage("digAndMoveForwardNoCheck: could not move", MessageLevel.DEBUG)
  338. return false
  339. end
  340. end
  341.  
  342. return true
  343. end
  344.  
  345.  
  346. -- ********************************************************************************** --
  347. -- Functions to move the Turtle up n spaces (or 1 if no n is specified)
  348. -- pushing through any gravel or other
  349. -- things such as mobs that might get in the way.
  350. -- ********************************************************************************** --
  351. function digAndMoveUp(n)
  352. preMoveChecks()
  353. return digAndMoveUpNoCheck(n)
  354. end
  355.  
  356. function digAndMoveUpNoCheck(n)
  357. n = tonumber(n)
  358. if n == nil then
  359. n = 1
  360. end
  361.  
  362. for i = 1,n do
  363. local success = digG(turtle.detectUp, turtle.digUp, turtle.up, turtle.attackUp, vecMOVEMENT.UP1)
  364. if not success then
  365. return false
  366. end
  367. end
  368.  
  369. return true
  370. end
  371.  
  372.  
  373. -- ********************************************************************************** --
  374. -- Functions to move the Turtle down n spaces (or 1 if no n is specified)
  375. -- Pushing through any gravel or other
  376. -- things such as mobs that might get in the way.
  377. -- ********************************************************************************** --
  378. function digAndMoveDown(n)
  379. preMoveChecks()
  380. return digAndMoveDownNoCheck(n)
  381. end
  382.  
  383. function digAndMoveDownNoCheck(n)
  384. n = tonumber(n)
  385. if n == nil then
  386. n = 1
  387. end
  388.  
  389. for i = 1,n do
  390. local success = digG(turtle.detectDown, turtle.digDown, turtle.down, turtle.attackDown, vecMOVEMENT.DOWN1)
  391. if not success then
  392. return false
  393. end
  394. end
  395.  
  396. return true
  397. end
  398.  
  399.  
  400. -- ********************************************************************************** --
  401. -- Generic function to dig 1 space
  402. -- pushing through any gravel or other
  403. -- things such as mobs that might get in the way.
  404. -- ********************************************************************************** --
  405. function digG(fnDetect, fnDig, fnMove, fnAttack, vecMovement1)
  406.  
  407. local nbTries = 0
  408. local success = false
  409. local successMove = false
  410.  
  411. -- we do not try to be clever with gravel, we just retry
  412. while not successMove and nbTries < MNB_FAILED_MOVE do
  413. nbTries = nbTries + 1
  414.  
  415. successMove = fnMove()
  416.  
  417. if not successMove then
  418. success = false
  419.  
  420. if fnDetect() then
  421. success = fnDig()
  422. end
  423.  
  424. if not success then
  425. if fnAttack() then
  426. writeMessage("Turtle hit mob!", MessageLevel.INFO)
  427. end
  428. end
  429. end
  430. end
  431.  
  432. if successMove then
  433. -- update current position
  434. posCurr = posAdd(posCurr, vecMovement1)
  435. savePositionAndOrientation()
  436. else
  437. writeMessage("Turtle unable to move for " .. MNB_FAILED_MOVE .. " tries", MessageLevel.DEBUG)
  438. end
  439.  
  440. return successMove
  441. end
  442.  
  443.  
  444. -- ********************************************************************************** --
  445. -- Function to move to a position (no checks)
  446. -- ********************************************************************************** --
  447. function digToNoCheck(pos)
  448. while posCurr.y < pos.y do
  449. digAndMoveUpNoCheck()
  450. end
  451.  
  452. while posCurr.y > pos.y do
  453. digAndMoveDownNoCheck()
  454. end
  455.  
  456. changeOrientation(Orientation.RIGHT)
  457. while posCurr.x < pos.x do
  458. digAndMoveForwardNoCheck()
  459. end
  460.  
  461. changeOrientation(Orientation.BACK)
  462. while posCurr.z < pos.z do
  463. digAndMoveForwardNoCheck()
  464. end
  465.  
  466. changeOrientation(Orientation.LEFT)
  467. while posCurr.x > pos.x do
  468. digAndMoveForwardNoCheck()
  469. end
  470.  
  471. changeOrientation(Orientation.FRONT)
  472. while posCurr.z > pos.z do
  473. digAndMoveForwardNoCheck()
  474. end
  475. end
  476.  
  477.  
  478. -- ********************************************************************************** --
  479. -- Function to move to a position
  480. -- ********************************************************************************** --
  481. function digTo(pos)
  482.  
  483. while posCurr.y < pos.y do
  484. digAndMoveUp()
  485. end
  486.  
  487. while posCurr.y > pos.y do
  488. digAndMoveDown()
  489. end
  490.  
  491. changeOrientation(Orientation.RIGHT)
  492. while posCurr.x < pos.x do
  493. digAndMoveForward()
  494. end
  495.  
  496. changeOrientation(Orientation.LEFT)
  497. while posCurr.x > pos.x do
  498. digAndMoveForward()
  499. end
  500.  
  501. changeOrientation(Orientation.FRONT)
  502. while posCurr.z > pos.z do
  503. digAndMoveForward()
  504. end
  505.  
  506. changeOrientation(Orientation.BACK)
  507. while posCurr.z < pos.z do
  508. digAndMoveForward()
  509. end
  510. end
  511.  
  512.  
  513. -- ********************************************************************************** --
  514. -- Function to perform an emergency return home
  515. -- ********************************************************************************** --
  516. function emergencyExit()
  517. writeMessage("FATAL: Emergency exit", MessageLevel.FATAL)
  518.  
  519. local success = loadPositionAndOrientation()
  520. if not success then
  521. writeMessage("FATAL: Cannot load return coordinates", MessageLevel.FATAL)
  522. return
  523. end
  524.  
  525. digToNoCheck(posHOME)
  526.  
  527. changeOrientation(Orientation.FRONT)
  528.  
  529. finish()
  530. error()
  531. end
  532.  
  533.  
  534. -- ********************************************************************************** --
  535. -- Performs pre-move checks
  536. -- ********************************************************************************** --
  537. function preMoveChecks()
  538. if not checkFuel() then
  539. writeMessage("Not enough fuel to continue: " .. turtle.getFuelLevel(), MessageLevel.FATAL)
  540. emergencyExit()
  541. end
  542.  
  543. ensureInventorySpace()
  544. end
  545.  
  546.  
  547. -- ********************************************************************************** --
  548. -- Check fuel gauge and return whether we have enough.
  549. -- ********************************************************************************** --
  550. function checkFuel()
  551. -- Determine whether a refuel is required
  552. local fuelLevel = turtle.getFuelLevel()
  553.  
  554. if (fuelLevel == "unlimited") then
  555. return true
  556. end
  557.  
  558. if (fuelLevel > FUEL_NEEDED_AT) then
  559. return true
  560. end
  561.  
  562. return false
  563. end
  564.  
  565.  
  566. -- ********************************************************************************** --
  567. -- Checks that the turtle has inventory space by checking for spare slots.
  568. -- ********************************************************************************** --
  569. function ensureInventorySpace()
  570. if doInventoryCheck then
  571. local count = turtle.getItemCount(SLOT_LAST)
  572. if count > 0 then
  573. writeMessage("Turtle must empty inventory: last slot count = " .. count, MessageLevel.INFO)
  574.  
  575. doInventoryCheck = false
  576. success = emptyInventoryAndReturn()
  577. doInventoryCheck = true
  578.  
  579. return success
  580. end
  581. end
  582. end
  583.  
  584.  
  585. -- ********************************************************************************** --
  586. -- Empty the inventory at the starting point then return
  587. -- ********************************************************************************** --
  588. function emptyInventoryAndReturn()
  589. -- Return to the starting point and empty the inventory
  590. -- then go back to mining
  591. local posOld = posCurr
  592. local orientationOld = orientationCurr
  593.  
  594. -- empty turtle
  595. digTo(posHOME)
  596. emptyInventory()
  597.  
  598. -- get back to initial position
  599. digTo(posOld)
  600. changeOrientation(orientationOld)
  601. end
  602.  
  603.  
  604. -- ********************************************************************************** --
  605. -- Empty the turtle's inventory
  606. -- Expects a chest at the top pf the starting position
  607. -- ********************************************************************************** --
  608. function emptyInventory()
  609.  
  610. -- drop unwanted first
  611. dumpUnwantedBlocks()
  612.  
  613. for i = Slots.FIRST_INVENTORY, SLOT_LAST do
  614. if turtle.getItemCount(i) > 0 then
  615. turtle.select(i)
  616. turtle.dropUp()
  617. end
  618. end
  619.  
  620. -- very important to reselect first empty slot
  621. -- otherwise the next dig will fill the last slot
  622. turtle.select(Slots.FIRST_INVENTORY)
  623. end
  624.  
  625.  
  626. -- ********************************************************************************** --
  627. -- Check all is ok before launching and set required states
  628. -- ********************************************************************************** --
  629. function preLaunch()
  630. -- perform internal validation
  631. if Slots == nil then
  632. writeMessage("Slots not defined", MessageLevel.FATAL)
  633. error()
  634. else
  635. if not Slots.FIRST_INVENTORY then
  636. writeMessage("Slots.FIRST_INVENTORY is undefined", MessageLevel.FATAL)
  637. error()
  638. end
  639. end
  640.  
  641. local fuel = turtle.getFuelLevel()
  642. print ("Fuel level: " .. fuel)
  643. if not checkFuel() then
  644. writeMessage("Not enough fuel", MessageLevel.FATAL)
  645. error()
  646. end
  647.  
  648. -- delete prior files
  649. fs.delete(FILENAME_MESSAGES)
  650. fs.delete(FILENAME_POSITION)
  651. fs.delete(FILENAME_STARTUP)
  652. fs.delete(FILENAME_REBOOT)
  653. fs.delete(FILENAME_STRATA)
  654.  
  655. -- reset position and orientation
  656. posCurr = posHOME
  657. orientationCurr = Orientation.FRONT
  658. savePositionAndOrientation()
  659.  
  660. -- make sure we fill inventory from the start
  661. turtle.select(Slots.FIRST_INVENTORY)
  662.  
  663. -- await final confirmation
  664. print "Press enter (Ctrl-T to abort)"
  665. io.read()
  666. end
  667.  
  668.  
  669. -- ********************************************************************************** --
  670. -- Returns true if the positions are equal (orientation is not compared)
  671. -- ********************************************************************************** --
  672. function posEquals(pos1, pos2)
  673. return pos1.x == pos2.x and pos1.y == pos2.y and pos1.z == pos2.z
  674. end
  675.  
  676.  
  677. -- ********************************************************************************** --
  678. -- Copy a position vector.
  679. -- ********************************************************************************** --
  680. function posCopy(pos)
  681. local vec = { x = pos.x, y = pos.y, z = pos.z }
  682. return vec
  683. end
  684.  
  685.  
  686. -- ********************************************************************************** --
  687. -- Adds two position vectors.
  688. -- ********************************************************************************** --
  689. function posAdd(pos1, pos2)
  690. local vec = { x = pos1.x + pos2.x, y = pos1.y + pos2.y, z = pos1.z + pos2.z }
  691. return vec
  692. end
  693.  
  694.  
  695. -- ********************************************************************************** --
  696. -- Subtract two position vectors.
  697. -- ********************************************************************************** --
  698. function posDiff(pos1, pos2)
  699. local vec = { x = pos1.x - pos2.x, y = pos1.y-pos2.y, z = pos1.z-pos2.z }
  700. return vec
  701. end
  702.  
  703.  
  704. -- ********************************************************************************** --
  705. -- Compute the distance between two positions
  706. -- ********************************************************************************** --
  707. function posDist(pos1, pos2)
  708. local vec = posDiff(pos1, pos2)
  709. local dist = math.sqrt((vec.x*vec.x) + (vec.y*vec.y) + (vec.z*vec.z))
  710. return dist
  711. end
  712.  
  713.  
  714. -- ********************************************************************************** --
  715. -- Compute the distance between two positions considering only the x and z coords
  716. -- ********************************************************************************** --
  717. function posDistPlane(pos1, pos2)
  718. local vec = posDiff(pos1, pos2)
  719.  
  720. local dist = math.sqrt((vec.x*vec.x) + (vec.z*vec.z))
  721. return dist
  722. end
  723.  
  724.  
  725. -- ********************************************************************************** --
  726. -- Dump unwanted blocks (if Slots.UNWANTED_BLOCKS is defined)
  727. -- ********************************************************************************** --
  728. function dumpUnwantedBlocks()
  729. if Slots.UNWANTED_BLOCKS then
  730. -- Dump unwanted blocks
  731. for item = Slots.FIRST_INVENTORY, SLOT_LAST do
  732. if turtle.getItemCount(item) > 0 then
  733. turtle.select(item)
  734. for unwanted = Slots.UNWANTED_BLOCKS, Slots.FIRST_INVENTORY-1 do
  735. if turtle.compareTo(unwanted) then
  736. writeMessage("dumpUnwantedBlocks: UNWANTED FOUND!", MessageLevel.DEBUG)
  737. turtle.dropDown()
  738. end
  739. end
  740. end
  741. end
  742.  
  743. turtle.select(1)
  744. end
  745. end
  746.  
  747.  
  748. -- ********************************************************************************** --
  749. -- Drill to bedrock
  750. -- ********************************************************************************** --
  751. function mineDrill()
  752. Slots = { FIRST_INVENTORY = 1 }
  753.  
  754. print "*****************************"
  755. print "*****************************"
  756. print "*****************************"
  757.  
  758. preLaunch()
  759.  
  760. -- mine down
  761. local moveSuccess = true
  762. while moveSuccess do
  763. -- dig in front
  764. turtle.dig()
  765. -- go down
  766. moveSuccess = digAndMoveDown()
  767. end
  768.  
  769. digTo(posHOME)
  770. changeOrientation(Orientation.FRONT)
  771. end
  772.  
  773.  
  774. -- ********************************************************************************** --
  775. -- Mine corridor x meters deep
  776. -- ********************************************************************************** --
  777. function mineCorridor(x)
  778. Slots = { FIRST_INVENTORY = 1 }
  779.  
  780. preLaunch()
  781.  
  782. for i = 1, x do
  783. changeOrientation(Orientation.FRONT)
  784. digAndMoveForward()
  785. turtle.digUp()
  786. turtle.digDown()
  787.  
  788. end
  789. end
  790.  
  791.  
  792. -- ********************************************************************************** --
  793. -- Mine tunnel 3 by 3 by x
  794. -- ********************************************************************************** --
  795. function mineTunnel(x)
  796. Slots = { COBBLESTONE = 1,
  797. FIRST_INVENTORY = 2 }
  798.  
  799. print "*****************************"
  800. print "*****************************"
  801. print "*****************************"
  802. print "Arrange slots as follows:"
  803. print "Slot 1: cobblestone"
  804.  
  805. preLaunch()
  806.  
  807. -- spiral
  808. for i = 1, x do
  809. -- mid center
  810. changeOrientation(Orientation.FRONT)
  811. digAndMoveForward()
  812.  
  813. changeOrientation(Orientation.RIGHT)
  814.  
  815. -- bottom center
  816. digAndMoveDown()
  817. placeSameAs(Slots.COBBLESTONE, turtle.placeDown, turtle.detectDown)
  818.  
  819. -- bottom right
  820. digAndMoveForward()
  821. placeSameAs(Slots.COBBLESTONE, turtle.placeDown, turtle.detectDown)
  822. placeSameAs(Slots.COBBLESTONE, turtle.place, turtle.detect)
  823.  
  824. -- mid right
  825. digAndMoveUp()
  826. placeSameAs(Slots.COBBLESTONE, turtle.place, turtle.detect)
  827.  
  828. -- top right
  829. digAndMoveUp()
  830. placeSameAs(Slots.COBBLESTONE, turtle.placeUp, turtle.detectUp)
  831. placeSameAs(Slots.COBBLESTONE, turtle.place, turtle.detect)
  832.  
  833. changeOrientation(Orientation.LEFT)
  834.  
  835. -- top center
  836. digAndMoveForward()
  837. placeSameAs(Slots.COBBLESTONE, turtle.placeUp, turtle.detectUp)
  838.  
  839. -- top left
  840. digAndMoveForward()
  841. placeSameAs(Slots.COBBLESTONE, turtle.placeUp, turtle.detectUp)
  842. placeSameAs(Slots.COBBLESTONE, turtle.place, turtle.detect)
  843.  
  844. -- mid left
  845. digAndMoveDown()
  846. placeSameAs(Slots.COBBLESTONE, turtle.place, turtle.detect)
  847.  
  848. -- bottom left
  849. digAndMoveDown()
  850. placeSameAs(Slots.COBBLESTONE, turtle.place, turtle.detect)
  851. placeSameAs(Slots.COBBLESTONE, turtle.placeDown, turtle.detectDown)
  852.  
  853. -- go back to mid center
  854. digAndMoveUp()
  855. changeOrientation(Orientation.RIGHT)
  856. digAndMoveForward()
  857. changeOrientation(Orientation.FRONT)
  858. end
  859. end
  860.  
  861.  
  862. -- ********************************************************************************** --
  863. -- Pose blocks in a track
  864. -- ********************************************************************************** --
  865. function mineTrack(len)
  866. Slots = { ITEM = 1,
  867. FIRST_INVENTORY = 2 }
  868.  
  869. print "*****************************"
  870. print "*****************************"
  871. print "*****************************"
  872. print "Arrange slots as follows:"
  873. print "Slot 1: item to place"
  874.  
  875. preLaunch()
  876.  
  877. for i = 1, len do
  878. digAndMoveForward()
  879. turtle.digDown()
  880. placeSameAs(Slots.ITEM, turtle.placeDown, turtle.detectDown)
  881. end
  882. end
  883.  
  884.  
  885. -- ********************************************************************************** --
  886. -- Pose stairs going down
  887. -- ********************************************************************************** --
  888. function mineStairsDown(len)
  889. Slots = { ITEM = 1,
  890. FIRST_INVENTORY = 2 }
  891.  
  892. print "*****************************"
  893. print "*****************************"
  894. print "*****************************"
  895. print "Arrange slots as follows:"
  896. print "Slot 1: material to place as stairs"
  897.  
  898. preLaunch()
  899.  
  900. for i = 1, len do
  901. digAndMoveForward()
  902.  
  903. -- make head room
  904. digAndMoveUp(2)
  905. turtle.digUp()
  906. digAndMoveDown(2)
  907.  
  908. -- pose/dig stairs proper
  909. digAndMoveDown(2)
  910. changeOrientation(Orientation.BACK)
  911. placeSameAs(Slots.ITEM, turtle.place, turtle.detect)
  912. digAndMoveUp()
  913. placeSameAs(Slots.ITEM, turtle.placeDown, turtle.detectDown)
  914. changeOrientation(Orientation.FRONT)
  915. end
  916. end
  917.  
  918.  
  919. -- ********************************************************************************** --
  920. -- Pose stairs going up
  921. -- ********************************************************************************** --
  922. function mineStairsUp(len)
  923. Slots = { ITEM = 1,
  924. FIRST_INVENTORY = 2 }
  925.  
  926. print "*****************************"
  927. print "*****************************"
  928. print "*****************************"
  929. print "Arrange slots as follows:"
  930. print "Slot 1: material to place as stairs"
  931.  
  932. preLaunch()
  933.  
  934. for i = 1, len do
  935. digAndMoveForward()
  936. placeSameAs(Slots.ITEM, turtle.placeDown, turtle.detectDown)
  937. digAndMoveUp()
  938. placeSameAs(Slots.ITEM, turtle.placeDown, turtle.detectDown)
  939.  
  940. -- make head room
  941. digAndMoveUp(3)
  942. turtle.digUp()
  943. digAndMoveDown(3)
  944.  
  945. end
  946. end
  947.  
  948.  
  949. -- ********************************************************************************** --
  950. -- Mine all stratas down
  951. -- ********************************************************************************** --
  952. function mineAll()
  953. Slots = { BUCKET = 1,
  954. UNWANTED_BLOCKS = 2,
  955. FIRST_INVENTORY = 3 }
  956.  
  957. print "*****************************"
  958. print "*****************************"
  959. print "*****************************"
  960. print "ASSUMES there is a CHEST ABOVE the turtle"
  961. print "Turtle will mine going RIGHT than FORWARD"
  962. print "Arrange slots as follows:"
  963. print "Slot 1: empty bucket"
  964. print "Slot 2: cobblestone/netherrack"
  965.  
  966. preLaunch()
  967.  
  968. digTo(posHOME)
  969. emptyInventory()
  970.  
  971. -- will have to decrement counter
  972. -- going FRONT, z becomes smaller
  973. fromZ = posHOME.z
  974. toZ = posHOME.z - STRATA_Z_FRONT
  975.  
  976. fromX = posHOME.x
  977. toX = posHOME.x + STRATA_X_RIGHT
  978.  
  979. fromY = posHOME.y
  980.  
  981. local msg = "Mining all"
  982. writeMessage(msg, MessageLevel.INFO)
  983. appendFile(FILENAME_STRATA, msg)
  984.  
  985. -- go to starting position
  986. posStarting = { x = fromX, y = fromY, z = fromZ }
  987. digTo(posStarting)
  988.  
  989. -- Moving to the FRONT (step -1)
  990. for iz = fromZ, toZ, -1 do
  991. -- LEFT -> RIGHT (step 4)
  992. for ix = fromX, toX, 4 do
  993. writeMessage("Fuel level: " .. turtle.getFuelLevel(), MessageLevel.INFO)
  994.  
  995. dumpUnwantedBlocks()
  996.  
  997. -- get in position
  998. pos = { x = ix, y = fromY, z = iz }
  999. digTo(pos)
  1000. changeOrientation(Orientation.RIGHT)
  1001.  
  1002. local moveSuccess = true
  1003. while moveSuccess do
  1004. -- dig RIGHT
  1005. turtle.dig()
  1006.  
  1007. -- collect lava/water
  1008. turtle.select(Slots.BUCKET)
  1009. local placeSuccess = turtle.placeDown()
  1010. if (placeSuccess) then
  1011. turtle.refuel()
  1012. -- in case of water, must empty
  1013. turtle.digDown() -- break block
  1014. turtle.placeDown() -- quickly empty
  1015. end
  1016.  
  1017. -- make sure all items are stacked
  1018. turtle.select(1)
  1019.  
  1020. -- go down
  1021. moveSuccess = digAndMoveDown()
  1022. end
  1023.  
  1024. dumpUnwantedBlocks()
  1025.  
  1026.  
  1027. -- prep ascension
  1028. -- move up in case bedrock is uneven
  1029. digAndMoveUp()
  1030. digAndMoveForward()
  1031. digAndMoveForward()
  1032.  
  1033. -- go down to get the missed blocks
  1034. moveSuccess = digAndMoveDown()
  1035. turtle.dig()
  1036.  
  1037. -- mine up
  1038. while posCurr.y < fromY do
  1039. -- dig RIGHT
  1040. turtle.dig()
  1041.  
  1042. -- collect lava
  1043. turtle.select(Slots.BUCKET)
  1044. local placeSuccess = turtle.placeUp()
  1045. if (placeSuccess) then
  1046. turtle.refuel()
  1047. -- in case of water, must empty
  1048. turtle.digDown() -- break block
  1049. turtle.placeDown() -- quickly empty
  1050. end
  1051.  
  1052. -- make sure all items are stacked
  1053. turtle.select(1)
  1054.  
  1055. -- go up
  1056. moveSuccess = digAndMoveUp()
  1057. end
  1058.  
  1059. turtle.dig()
  1060.  
  1061. msg = "Finished carrot: " .. iz .. ", " .. ix
  1062. writeMessage(msg)
  1063. appendFile(FILENAME_STRATA, msg)
  1064. end
  1065. end
  1066.  
  1067. digTo(posHOME)
  1068.  
  1069. dumpUnwantedBlocks()
  1070. emptyInventory()
  1071.  
  1072. changeOrientation(Orientation.FRONT)
  1073. end
  1074.  
  1075.  
  1076. -- ********************************************************************************** --
  1077. -- Print commands
  1078. -- ********************************************************************************** --
  1079. function printCommands()
  1080. print("Commands:")
  1081. print(" emergency Turtle returns HOME")
  1082. print(" all Mine all stratas")
  1083. print(" drill Drill to bedrock")
  1084. print(" tunnel x Mine x meters ahead")
  1085. print(" corridor x Mine x meters ahead")
  1086. print(" track x Lays item for x meters")
  1087.  
  1088. print("... press enter ...")
  1089. io.read()
  1090.  
  1091. print(" stairsUp x Stairs up x steps")
  1092. print(" stairsDn x Stairs down x steps")
  1093. end
  1094.  
  1095.  
  1096. -- ********************************************************************************** --
  1097. -- Main program
  1098. -- ********************************************************************************** --
  1099. function main(tArgs)
  1100. if #tArgs < 1 then
  1101. printCommands()
  1102. return
  1103. end
  1104. local command = tArgs[1]
  1105.  
  1106. writeMessage("Orientation = " .. orientationCurr, MessageLevel.DEBUG)
  1107. writeMessage("Current position = " .. textutils.serialize(posCurr), MessageLevel.DEBUG)
  1108.  
  1109. if command == "emergency" then
  1110. emergencyExit()
  1111. return
  1112.  
  1113. elseif command == "all" then
  1114. mineAll()
  1115.  
  1116. elseif command == "drill" then
  1117. mineDrill()
  1118.  
  1119. elseif command == "tunnel" then
  1120. local x = tonumber(tArgs[2])
  1121. mineTunnel(x)
  1122.  
  1123. elseif command == "corridor" then
  1124. local x = tonumber(tArgs[2])
  1125. mineCorridor(x)
  1126.  
  1127. elseif command == "track" then
  1128. local x = tonumber(tArgs[2])
  1129. mineTrack(x)
  1130.  
  1131. elseif command == "stairsUp" then
  1132. local x = tonumber(tArgs[2])
  1133. mineStairsUp(x)
  1134.  
  1135. elseif command == "stairsDn" then
  1136. local x = tonumber(tArgs[2])
  1137. mineStairsDown(x)
  1138.  
  1139. else
  1140. printCommands()
  1141. end
  1142. end
  1143.  
  1144.  
  1145. -- ********************************************************************************** --
  1146. -- startup script
  1147. -- ********************************************************************************** --
  1148.  
  1149. -- check if we have been rebooted
  1150. if fs.exists(FILENAME_REBOOT) then
  1151. writeMessage("Turtle rebooted prematurely", MessageLevel.FATAL)
  1152. writeMessage("Engaging emergency procedures in 60 seconds", MessageLevel.FATAL)
  1153. writeMessage("(ctrl-T to abort and delete reboot_token)", MessageLevel.FATAL)
  1154. sleep(60)
  1155. emergencyExit()
  1156. error()
  1157. end
  1158.  
  1159. startup()
  1160.  
  1161. local args = { ... }
  1162.  
  1163. -- wrap call to main so we can catch errors and cleanup
  1164. local status, msg = pcall(main, args)
  1165. if not status then
  1166. if msg ~= nil then
  1167. writeMessage(tostring(msg), MessageLevel.FATAL)
  1168. else
  1169. writeMessage("ERROR in main() but no error message was provided", MessageLevel.FATAL)
  1170. end
  1171.  
  1172. emergencyExit()
  1173. end
  1174.  
  1175. -- cleanup
  1176. finish()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement