poke5555

Ore Quarry

Feb 19th, 2013
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 42.93 KB | None | 0 0
  1. --************--
  2. --* Poke5555 *--
  3. --************--
  4.  
  5. -- Enumeration to store the the different types of message that can be written
  6. messageLevel = { DEBUG=0, INFO=1, WARNING=2, ERROR=3, FATAL=4 }
  7.  
  8. -- Enumeration to store names for the 6 directions
  9. direction = { FORWARD=0, RIGHT=1, BACK=2, LEFT=3, UP=4, DOWN=5 }
  10.  
  11. local messageOutputLevel = messageLevel.INFO
  12. local messageOutputFileName
  13. local fuelLevelToRefuelAt = 5
  14. local refuelItemsToUseWhenRefuelling = 63
  15. local emergencyFuelToRetain = 0
  16. local maximumGravelStackSupported = 25 -- The number of stacked gravel or sand blocks supported
  17. local noiseBlocksCount
  18. local bottomLayer = 5 -- The y co-ords of the layer immediately above bedrock
  19. local returningToStart = false
  20. local lookForChests = false -- Determines if chests should be located as part of the quarrying
  21. local miningOffset -- The offset to the mining layer. This is set depending on whether chests are being looked for or not
  22. local lastEmptySlot -- The last inventory slot that was empty when the program started (is either 15 if not looking for chests or 14 if we are)
  23. local turtleId
  24. local currentlySelectedSlot = 0 -- The slot that the last noise block was found in
  25. local lastMoveNeededDig = true -- Determines whether the last move needed a dig first
  26. local haveBeenAtZeroZeroOnLayer -- Determines whether the turtle has been at (0, 0) in this mining layer
  27. local orientationAtZeroZero -- The turtle's orientation when it was at (0, 0)
  28. local levelToReturnTo -- The level that the turtle should return to in order to head back to the start to unload
  29.  
  30. -- Variables to store the current location and orientation of the turtle. x is right, left, y is up, down and
  31. -- z is forward, back with relation to the starting orientation. Y is the actual turtle level, x and z are
  32. -- in relation to the starting point (i.e. the starting point is (0, 0))
  33. local currX
  34. local currY
  35. local currZ
  36. local currOrient
  37.  
  38. -- Command line parameters
  39. local startHeight -- Represents the height (y co-ord) that the turtle started at
  40. local quarryWidth -- Represents the length of the mines that the turtle will dig
  41.  
  42. -- ********************************************************************************** --
  43. -- Writes an output message
  44. -- ********************************************************************************** --
  45. function writeMessage(message, msgLevel)
  46. if (msgLevel >= messageOutputLevel) then
  47. print(message)
  48. if (turtleId == nil) then
  49. rednet.broadcast(message)
  50. else
  51. -- Broadcast the message (prefixed with the turtle's id)
  52. rednet.broadcast("[".. turtleId.."] "..message)
  53. end
  54.  
  55. if (messageOutputFileName ~= nil) then
  56. -- Open file, write message and close file (flush doesn't seem to work!)
  57. local outputFile = io.open(messageOutputFileName, "a")
  58. outputFile:write(message)
  59. outputFile:write("\n")
  60. outputFile:close()
  61. end
  62. end
  63. end
  64.  
  65. -- ********************************************************************************** --
  66. -- Ensures that the turtle has fuel
  67. -- ********************************************************************************** --
  68. function ensureFuel()
  69.  
  70. -- Determine whether a refuel is required
  71. local fuelLevel = turtle.getFuelLevel()
  72. if (fuelLevel ~= "unlimited") then
  73. if (fuelLevel < fuelLevelToRefuelAt) then
  74. -- Need to refuel
  75. turtle.select(16)
  76. currentlySelectedSlot = 16
  77. local fuelItems = turtle.getItemCount(16)
  78.  
  79. -- Do we need to impact the emergency fuel to continue? (always
  80. -- keep one fuel item in slot 16)
  81. if (fuelItems == 0) then
  82. writeMessage("Completely out of fuel!", messageLevel.FATAL)
  83. elseif (fuelItems == 1) then
  84. writeMessage("Out of Fuel!", messageLevel.ERROR)
  85. turtle.refuel()
  86. elseif (fuelItems <= (emergencyFuelToRetain + 1)) then
  87. writeMessage("Consuming emergency fuel supply. "..(fuelItems - 2).." emergency fuel items remain", messageLevel.WARNING)
  88. turtle.refuel(1)
  89. else
  90. -- Refuel the lesser of the refuelItemsToUseWhenRefuelling and the number of items more than
  91. -- the emergency fuel level
  92. if (fuelItems - (emergencyFuelToRetain + 1) < refuelItemsToUseWhenRefuelling) then
  93. turtle.refuel(fuelItems - (emergencyFuelToRetain + 1))
  94. else
  95. turtle.refuel(refuelItemsToUseWhenRefuelling)
  96. end
  97. end
  98. end
  99. end
  100. end
  101.  
  102. -- ********************************************************************************** --
  103. -- Checks that the turtle has inventory space by checking for spare slots and returning
  104. -- to the starting point to empty out if it doesn't.
  105. --
  106. -- Takes the position required to move to in order to empty the turtle's inventory
  107. -- should it be full as arguments
  108. -- ********************************************************************************** --
  109. function ensureInventorySpace()
  110.  
  111. -- If already returning to start, then don't need to do anything
  112. if (returningToStart == false) then
  113.  
  114. -- If the last inventory slot is full, then need to return to the start and empty
  115. if (turtle.getItemCount(lastEmptySlot) > 0) then
  116.  
  117. -- Return to the starting point and empty the inventory, then go back to mining
  118. returnToStartAndUnload(true)
  119. end
  120. end
  121. end
  122.  
  123. -- ********************************************************************************** --
  124. -- Function to move to the starting point, call a function that is passed in
  125. -- and return to the same location (if required)
  126. -- ********************************************************************************** --
  127. function returnToStartAndUnload(returnBackToMiningPoint)
  128.  
  129. writeMessage("returnToStartAndUnload called", messageLevel.DEBUG)
  130. returningToStart = true
  131.  
  132. -- Store the current location and orientation so that it can be returned to
  133. local storedX = currX
  134. local storedY = currY
  135. local storedZ = currZ
  136. local storedOrient = currOrient
  137.  
  138. writeMessage("Return to start, return level: "..levelToReturnTo, messageLevel.DEBUG)
  139.  
  140. -- Move down to the correct layer to return via
  141. if (currY > levelToReturnTo) then
  142. while (currY > levelToReturnTo) do
  143. turtleDown()
  144. end
  145. elseif (currY < levelToReturnTo) then
  146. while (currY < levelToReturnTo) do
  147. turtleUp()
  148. end
  149. end
  150.  
  151. if ((haveBeenAtZeroZeroOnLayer == false) or (orientationAtZeroZero == direction.FORWARD)) then
  152. -- Move back to the correct X position first
  153. if (currX > 0) then
  154. turtleSetOrientation(direction.LEFT)
  155. while (currX > 0) do
  156. turtleForward()
  157. end
  158. elseif (currX < 0) then
  159. -- This should never happen
  160. writeMessage("Current x is less than 0 in returnToStartAndUnload", messageLevel.ERROR)
  161. end
  162.  
  163. -- Then move back to the correct Z position
  164. if (currZ > 0) then
  165. turtleSetOrientation(direction.BACK)
  166. while (currZ > 0) do
  167. turtleForward()
  168. end
  169. elseif (currZ < 0) then
  170. -- This should never happen
  171. writeMessage("Current z is less than 0 in returnToStartAndUnload", messageLevel.ERROR)
  172. end
  173. else
  174. -- Move back to the correct Z position first
  175. if (currZ > 0) then
  176. turtleSetOrientation(direction.BACK)
  177. while (currZ > 0) do
  178. turtleForward()
  179. end
  180. elseif (currZ < 0) then
  181. -- This should never happen
  182. writeMessage("Current z is less than 0 in returnToStartAndUnload", messageLevel.ERROR)
  183. end
  184.  
  185. -- Then move back to the correct X position
  186. if (currX > 0) then
  187. turtleSetOrientation(direction.LEFT)
  188. while (currX > 0) do
  189. turtleForward()
  190. end
  191. elseif (currX < 0) then
  192. -- This should never happen
  193. writeMessage("Current x is less than 0 in returnToStartAndUnload", messageLevel.ERROR)
  194. end
  195. end
  196.  
  197. -- Return to the starting layer
  198. if (currY < startHeight) then
  199. while (currY < startHeight) do
  200. turtleUp()
  201. end
  202. elseif (currY > startHeight) then
  203. -- This should never happen
  204. writeMessage("Current height is greater than start height in returnToStartAndUnload", messageLevel.ERROR)
  205. end
  206.  
  207. -- Empty the inventory
  208. local slotLoop = 1
  209.  
  210. -- Face the chest
  211. turtleSetOrientation(direction.BACK)
  212.  
  213. -- Loop over each of the slots (except the 16th one which stores fuel)
  214. while (slotLoop < 16) do
  215. -- If this is one of the slots that contains a noise block, empty all blocks except
  216. -- one
  217. turtle.select(slotLoop) -- Don't bother updating selected slot variable as it will set later in this function
  218. if ((slotLoop <= noiseBlocksCount) or ((slotLoop == 15) and (lastEmptySlot == 14))) then
  219. writeMessage("Dropping (n-1) from slot "..slotLoop.." ["..turtle.getItemCount(slotLoop).."]", messageLevel.DEBUG)
  220. turtle.drop(turtle.getItemCount(slotLoop) - 1)
  221. else
  222. -- Not a noise block, drop all of the items in this slot
  223. writeMessage("Dropping (all) from slot "..slotLoop.." ["..turtle.getItemCount(slotLoop).."]", messageLevel.DEBUG)
  224. turtle.drop()
  225. end
  226.  
  227. slotLoop = slotLoop + 1
  228. end
  229.  
  230. -- While we are here, refill the fuel items if there is capacity
  231. if (turtle.getItemCount(16) < 64) then
  232. turtleSetOrientation(direction.LEFT)
  233. turtle.select(16) -- Don't bother updating selected slot variable as it will set later in this function
  234. local currFuelItems = turtle.getItemCount(16)
  235. turtle.suck()
  236. while ((currFuelItems ~= turtle.getItemCount(16)) and (turtle.getItemCount(16) < 64)) do
  237. currFuelItems = turtle.getItemCount(16)
  238. turtle.suck()
  239. end
  240.  
  241. slotLoop = noiseBlocksCount + 1
  242. -- Have now picked up all the items that we can. If we have also picked up some
  243. -- additional fuel in some of the other slots, then drop it again
  244. while (slotLoop <= lastEmptySlot) do
  245. -- Drop any items found in this slot
  246. turtle.select(slotLoop) -- Don't bother updating selected slot variable as it will set later in this function
  247. turtle.drop()
  248. slotLoop = slotLoop + 1
  249. end
  250. end
  251.  
  252. -- Select the 1st slot because sometimes when leaving the 15th or 16th slots selected it can result
  253. -- in that slot being immediately filled (resulting in the turtle returning to base again too soon)
  254. turtle.select(1)
  255. currentlySelectedSlot = 1
  256.  
  257. -- If required, move back to the point that we were mining at before returning to the start
  258. if (returnBackToMiningPoint == true) then
  259. -- Return back to the required layer
  260. while (currY > levelToReturnTo) do
  261. turtleDown()
  262. end
  263.  
  264. if ((haveBeenAtZeroZeroOnLayer == false) or (orientationAtZeroZero == direction.FORWARD)) then
  265. -- Move back to the correct Z position first
  266. writeMessage("Stored Z: "..storedZ..", currZ: "..currZ, messageLevel.DEBUG)
  267. if (storedZ > currZ) then
  268. writeMessage("Orienting forward", messageLevel.DEBUG)
  269. writeMessage("Moving in z direction", messageLevel.DEBUG)
  270. turtleSetOrientation(direction.FORWARD)
  271. while (storedZ > currZ) do
  272. turtleForward()
  273. end
  274. elseif (storedZ < currZ) then
  275. -- This should never happen
  276. writeMessage("Stored z is less than current z in returnToStartAndUnload", messageLevel.ERROR)
  277. end
  278.  
  279. -- Then move back to the correct X position
  280. if (storedX > currX) then
  281. writeMessage("Stored X: "..storedX..", currX: "..currX, messageLevel.DEBUG)
  282. writeMessage("Orienting right", messageLevel.DEBUG)
  283. writeMessage("Moving in x direction", messageLevel.DEBUG)
  284. turtleSetOrientation(direction.RIGHT)
  285. while (storedX > currX) do
  286. turtleForward()
  287. end
  288. elseif (storedX < currX) then
  289. -- This should never happen
  290. writeMessage("Stored x is less than current x in returnToStartAndUnload", messageLevel.ERROR)
  291. end
  292. else
  293. -- Move back to the correct X position first
  294. if (storedX > currX) then
  295. writeMessage("Stored X: "..storedX..", currX: "..currX, messageLevel.DEBUG)
  296. writeMessage("Orienting right", messageLevel.DEBUG)
  297. writeMessage("Moving in x direction", messageLevel.DEBUG)
  298. turtleSetOrientation(direction.RIGHT)
  299. while (storedX > currX) do
  300. turtleForward()
  301. end
  302. elseif (storedX < currX) then
  303. -- This should never happen
  304. writeMessage("Stored x is less than current x in returnToStartAndUnload", messageLevel.ERROR)
  305. end
  306.  
  307. -- Then move back to the correct Z position
  308. writeMessage("Stored Z: "..storedZ..", currZ: "..currZ, messageLevel.DEBUG)
  309. if (storedZ > currZ) then
  310. writeMessage("Orienting forward", messageLevel.DEBUG)
  311. writeMessage("Moving in z direction", messageLevel.DEBUG)
  312. turtleSetOrientation(direction.FORWARD)
  313. while (storedZ > currZ) do
  314. turtleForward()
  315. end
  316. elseif (storedZ < currZ) then
  317. -- This should never happen
  318. writeMessage("Stored z is less than current z in returnToStartAndUnload", messageLevel.ERROR)
  319. end
  320. end
  321.  
  322. -- Move back to the correct layer
  323. if (storedY < currY) then
  324. while (storedY < currY) do
  325. turtleDown()
  326. end
  327. elseif (storedY > currY) then
  328. while (storedY > currY) do
  329. turtleUp()
  330. end
  331. end
  332.  
  333. -- Finally, set the correct orientation
  334. turtleSetOrientation(storedOrient)
  335.  
  336. writeMessage("Have returned to the mining point", messageLevel.DEBUG)
  337. end
  338.  
  339. returningToStart = false
  340.  
  341. end
  342.  
  343. -- ********************************************************************************** --
  344. -- Empties a chest's contents
  345. -- ********************************************************************************** --
  346. function emptyChest(suckFn)
  347.  
  348. local prevInventoryCount = {}
  349. local inventoryLoop
  350. local chestEmptied = false
  351.  
  352. -- Record the number of items in each of the inventory slots
  353. for inventoryLoop = 1, 16 do
  354. prevInventoryCount[inventoryLoop] = turtle.getItemCount(inventoryLoop)
  355. end
  356.  
  357. while (chestEmptied == false) do
  358. -- Pick up the next item
  359. suckFn()
  360.  
  361. -- Determine the number of items in each of the inventory slots now
  362. local newInventoryCount = {}
  363. for inventoryLoop = 1, 16 do
  364. newInventoryCount[inventoryLoop] = turtle.getItemCount(inventoryLoop)
  365. end
  366.  
  367. -- Now, determine whether there have been any items taken from the chest
  368. local foundDifferentItemCount = false
  369. inventoryLoop = 1
  370. while ((foundDifferentItemCount == false) and (inventoryLoop <= 16)) do
  371. if (prevInventoryCount[inventoryLoop] ~= newInventoryCount[inventoryLoop]) then
  372. foundDifferentItemCount = true
  373. else
  374. inventoryLoop = inventoryLoop + 1
  375. end
  376. end
  377.  
  378. -- If no items have been found with a different item count, then the chest has been emptied
  379. chestEmptied = not foundDifferentItemCount
  380.  
  381. if (chestEmptied == false) then
  382. prevInventoryCount = newInventoryCount
  383. -- Check that there is sufficient inventory space as may have picked up a block
  384. ensureInventorySpace()
  385. end
  386. end
  387.  
  388. writeMessage("Finished emptying chest", messageLevel.DEBUG)
  389. end
  390.  
  391.  
  392. -- ********************************************************************************** --
  393. -- Generic function to move the Turtle (pushing through any gravel or other
  394. -- things such as mobs that might get in the way).
  395. --
  396. -- The only thing that should stop the turtle moving is bedrock. Where this is
  397. -- found, the function will return after 15 seconds returning false
  398. -- ********************************************************************************** --
  399. function moveTurtle(moveFn, detectFn, digFn, attackFn, compareFn, suckFn, maxDigCount)
  400.  
  401. ensureFuel()
  402.  
  403. -- Flag to determine whether digging has been tried yet. If it has
  404. -- then pause briefly before digging again to allow sand or gravel to
  405. -- drop
  406. local digCount = 0
  407. local moveSuccess = false
  408.  
  409. if (lastMoveNeededDig == false) then
  410. -- Didn't need to dig last time the turtle moved, so try moving first
  411. moveSuccess = moveFn()
  412.  
  413. -- Don't need to set the last move needed dig. It is already false, if
  414. -- move success is now true, then it won't be changed
  415. else
  416. -- If we are looking for chests, then check that this isn't a chest before trying to dig it
  417. if (lookForChests == true) then
  418. if (isNoiseBlock(compareFn) == false) then
  419. if (detectFn() == true) then
  420. -- Determine if it is a chest before digging it
  421. if (isChestBlock(compareFn) == true) then
  422. -- Have found a chest, empty it before continuing
  423. emptyChest (suckFn)
  424. end
  425. end
  426. end
  427. end
  428.  
  429. -- Try to dig (without doing a detect as it is quicker)
  430. local digSuccess = digFn()
  431. if (digSuccess == true) then
  432. digCount = 1
  433. end
  434. moveSuccess = moveFn()
  435.  
  436. if (moveSuccess == true) then
  437. lastMoveNeededDig = digSuccess
  438. end
  439. end
  440.  
  441. -- Loop until we've successfully moved
  442. if (moveSuccess == false) then
  443. while ((moveSuccess == false) and (digCount < maxDigCount)) do
  444.  
  445. -- If there is a block in front, dig it
  446. if (detectFn() == true) then
  447.  
  448. -- If we've already tried digging, then pause before digging again to let
  449. -- any sand or gravel drop, otherwise check for a chest before digging
  450. if(digCount == 0) then
  451. -- Am about to dig a block - check that it is not a chest if necessary
  452. -- If we are looking for chests, then check that this isn't a chest before moving
  453. if (lookForChests == true) then
  454. if (isNoiseBlock(compareFn) == false) then
  455. if (detectFn() == true) then
  456. -- Determine if it is a chest before digging it
  457. if (isChestBlock(compareFn) == true) then
  458. -- Have found a chest, empty it before continuing
  459. emptyChest (suckFn)
  460. end
  461. end
  462. end
  463. end
  464. else
  465. sleep(0.1)
  466. end
  467.  
  468. digFn()
  469. digCount = digCount + 1
  470. else
  471. -- Am being stopped from moving by a mob, attack it
  472. attackFn()
  473. end
  474.  
  475. -- Try the move again
  476. moveSuccess = moveFn()
  477. end
  478.  
  479. if (digCount == 0) then
  480. lastMoveNeededDig = false
  481. else
  482. lastMoveNeededDig = true
  483. end
  484. end
  485.  
  486. -- Return the move success
  487. return moveSuccess
  488.  
  489. end
  490.  
  491. -- ********************************************************************************** --
  492. -- Move the turtle forward one block (updating the turtle's position)
  493. -- ********************************************************************************** --
  494. function turtleForward()
  495. local returnVal = moveTurtle(turtle.forward, turtle.detect, turtle.dig, turtle.attack, turtle.compare, turtle.suck, maximumGravelStackSupported)
  496. if (returnVal == true) then
  497. -- Update the current co-ordinates
  498. if (currOrient == direction.FORWARD) then
  499. currZ = currZ + 1
  500. elseif (currOrient == direction.LEFT) then
  501. currX = currX - 1
  502. elseif (currOrient == direction.BACK) then
  503. currZ = currZ - 1
  504. elseif (currOrient == direction.RIGHT) then
  505. currX = currX + 1
  506. else
  507. writeMessage ("Invalid currOrient in turtleForward function", messageLevel.ERROR)
  508. end
  509.  
  510. -- Check that there is sufficient inventory space as may have picked up a block
  511. ensureInventorySpace()
  512. end
  513.  
  514. return returnVal
  515. end
  516.  
  517. -- ********************************************************************************** --
  518. -- Move the turtle up one block (updating the turtle's position)
  519. -- ********************************************************************************** --
  520. function turtleUp()
  521. local returnVal = moveTurtle(turtle.up, turtle.detectUp, turtle.digUp, turtle.attackUp, turtle.compareUp, turtle.suckUp, maximumGravelStackSupported)
  522. if (returnVal == true) then
  523. currY = currY + 1
  524.  
  525. -- Check that there is sufficient inventory space as may have picked up a block
  526. ensureInventorySpace()
  527. end
  528. return returnVal
  529. end
  530.  
  531. -- ********************************************************************************** --
  532. -- Move the turtle down one block (updating the turtle's position)
  533. -- ********************************************************************************** --
  534. function turtleDown()
  535. local returnVal
  536.  
  537. -- Because the turtle is digging down, can fail fast (only allow 1 dig attempt).
  538. returnVal = moveTurtle(turtle.down, turtle.detectDown, turtle.digDown, turtle.attackDown, turtle.compareDown, turtle.suckDown, 1)
  539. if (returnVal == true) then
  540. currY = currY - 1
  541.  
  542. -- Check that there is sufficient inventory space as may have picked up a block
  543. ensureInventorySpace()
  544. end
  545. return returnVal
  546. end
  547.  
  548. -- ********************************************************************************** --
  549. -- Move the turtle back one block (updating the turtle's position)
  550. -- ********************************************************************************** --
  551. function turtleBack()
  552. -- First try to move back using the standard function
  553. local returnVal = turtle.back()
  554.  
  555. -- Moving back didn't work (might be a block or a mob in the way). Turn round and move
  556. -- forward instead (whereby anything in the way can be cleared)
  557. if(returnVal == false) then
  558. turtle.turnRight()
  559. turtle.turnRight()
  560. returnVal = turtleForward()
  561. turtle.turnRight()
  562. turtle.turnRight()
  563. end
  564.  
  565. if (returnVal == true) then
  566. -- Update the current co-ordinates
  567. if (currOrient == direction.FORWARD) then
  568. currZ = currZ - 1
  569. elseif (currOrient == direction.LEFT) then
  570. currX = currX + 1
  571. elseif (currOrient == direction.BACK) then
  572. currZ = currZ + 1
  573. elseif (currOrient == direction.RIGHT) then
  574. currX = currX - 1
  575. else
  576. writeMessage ("Invalid currOrient in turtleBack function", messageLevel.ERROR)
  577. end
  578.  
  579. -- Check that there is sufficient inventory space as may have picked up a block
  580. ensureInventorySpace()
  581. end
  582.  
  583. return returnVal
  584. end
  585.  
  586. -- ********************************************************************************** --
  587. -- Turns the turtle (updating the current orientation at the same time)
  588. -- ********************************************************************************** --
  589. function turtleTurn(turnDir)
  590.  
  591. if (turnDir == direction.LEFT) then
  592. if (currOrient == direction.FORWARD) then
  593. currOrient = direction.LEFT
  594. turtle.turnLeft()
  595. elseif (currOrient == direction.LEFT) then
  596. currOrient = direction.BACK
  597. turtle.turnLeft()
  598. elseif (currOrient == direction.BACK) then
  599. currOrient = direction.RIGHT
  600. turtle.turnLeft()
  601. elseif (currOrient == direction.RIGHT) then
  602. currOrient = direction.FORWARD
  603. turtle.turnLeft()
  604. else
  605. writeMessage ("Invalid currOrient in turtleTurn function", messageLevel.ERROR)
  606. end
  607. elseif (turnDir == direction.RIGHT) then
  608. if (currOrient == direction.FORWARD) then
  609. currOrient = direction.RIGHT
  610. turtle.turnRight()
  611. elseif (currOrient == direction.LEFT) then
  612. currOrient = direction.FORWARD
  613. turtle.turnRight()
  614. elseif (currOrient == direction.BACK) then
  615. currOrient = direction.LEFT
  616. turtle.turnRight()
  617. elseif (currOrient == direction.RIGHT) then
  618. currOrient = direction.BACK
  619. turtle.turnRight()
  620. else
  621. writeMessage ("Invalid currOrient in turtleTurn function", messageLevel.ERROR)
  622. end
  623. else
  624. writeMessage ("Invalid turnDir in turtleTurn function", messageLevel.ERROR)
  625. end
  626. end
  627.  
  628. -- ********************************************************************************** --
  629. -- Sets the turtle to a specific orientation, irrespective of its current orientation
  630. -- ********************************************************************************** --
  631. function turtleSetOrientation(newOrient)
  632.  
  633. if (currOrient ~= newOrient) then
  634. if (currOrient == direction.FORWARD) then
  635. if (newOrient == direction.RIGHT) then
  636. turtle.turnRight()
  637. currOrient = newOrient
  638. elseif (newOrient == direction.BACK) then
  639. turtle.turnRight()
  640. turtle.turnRight()
  641. currOrient = newOrient
  642. elseif (newOrient == direction.LEFT) then
  643. turtle.turnLeft()
  644. currOrient = newOrient
  645. else
  646. writeMessage ("Invalid newOrient in turtleSetOrientation function", messageLevel.ERROR)
  647. end
  648. elseif (currOrient == direction.RIGHT) then
  649. if (newOrient == direction.BACK) then
  650. turtle.turnRight()
  651. currOrient = newOrient
  652. elseif (newOrient == direction.LEFT) then
  653. turtle.turnRight()
  654. turtle.turnRight()
  655. currOrient = newOrient
  656. elseif (newOrient == direction.FORWARD) then
  657. turtle.turnLeft()
  658. currOrient = newOrient
  659. else
  660. writeMessage ("Invalid newOrient in turtleSetOrientation function", messageLevel.ERROR)
  661. end
  662. elseif (currOrient == direction.BACK) then
  663. if (newOrient == direction.LEFT) then
  664. turtle.turnRight()
  665. currOrient = newOrient
  666. elseif (newOrient == direction.FORWARD) then
  667. turtle.turnRight()
  668. turtle.turnRight()
  669. currOrient = newOrient
  670. elseif (newOrient == direction.RIGHT) then
  671. turtle.turnLeft()
  672. currOrient = newOrient
  673. else
  674. writeMessage ("Invalid newOrient in turtleSetOrientation function", messageLevel.ERROR)
  675. end
  676. elseif (currOrient == direction.LEFT) then
  677. if (newOrient == direction.FORWARD) then
  678. turtle.turnRight()
  679. currOrient = newOrient
  680. elseif (newOrient == direction.RIGHT) then
  681. turtle.turnRight()
  682. turtle.turnRight()
  683. currOrient = newOrient
  684. elseif (newOrient == direction.BACK) then
  685. turtle.turnLeft()
  686. currOrient = newOrient
  687. else
  688. writeMessage ("Invalid newOrient in turtleSetOrientation function", messageLevel.ERROR)
  689. end
  690. else
  691. writeMessage ("Invalid currOrient in turtleTurn function", messageLevel.ERROR)
  692. end
  693. end
  694. end
  695.  
  696. -- ********************************************************************************** --
  697. -- Determines if a particular block is considered a noise block or not. A noise
  698. -- block is one that is a standard block in the game (stone, dirt, gravel etc.) and
  699. -- is one to ignore as not being an ore. Function works by comparing the block
  700. -- in question against a set of blocks in the turtle's inventory which are known not to
  701. -- be noise blocks. Param is the function to use to compare the block for a noise block
  702. -- ********************************************************************************** --
  703. function isNoiseBlock(compareFn)
  704.  
  705. -- Consider air to be a noise block
  706. local returnVal = false
  707. local seamLoop = 1
  708. local prevSelectedSlot
  709.  
  710. -- If the currently selected slot is a noise block, then compare against this first
  711. -- so that the slot doesn't need to be selected again (there is a 0.05s cost to do
  712. -- this even if it is the currently selected slot)
  713. if (currentlySelectedSlot <= noiseBlocksCount) then
  714. returnVal = compareFn()
  715. end
  716.  
  717. if (returnVal == false) then
  718. prevSelectedSlot = currentlySelectedSlot
  719. while((returnVal == false) and (seamLoop <= noiseBlocksCount)) do
  720. if (seamLoop ~= prevSelectedSlot) then
  721. turtle.select(seamLoop)
  722. currentlySelectedSlot = seamLoop
  723. returnVal = compareFn()
  724. end
  725. seamLoop = seamLoop + 1
  726. end
  727. end
  728.  
  729. -- Return the calculated value
  730. return returnVal
  731.  
  732. end
  733.  
  734. -- ********************************************************************************** --
  735. -- Determines if a particular block is a chest. Returns false if it is not a chest
  736. -- or chests are not being detected
  737. -- ********************************************************************************** --
  738. function isChestBlock(compareFn)
  739.  
  740. -- Check the block in the appropriate direction to see whether it is a chest. Only
  741. -- do this if we are looking for chests
  742. local returnVal = false
  743. if (lookForChests == true) then
  744. turtle.select(15)
  745. currentlySelectedSlot = 15
  746. returnVal = compareFn()
  747. end
  748.  
  749. -- Return the calculated value
  750. return returnVal
  751.  
  752. end
  753.  
  754. -- ********************************************************************************** --
  755. -- Function to calculate the number of non seam blocks in the turtle's inventory. This
  756. -- is all of the blocks at the start of the inventory (before the first empty slot is
  757. -- found
  758. -- ********************************************************************************** --
  759. function determineNoiseBlocksCountCount()
  760. -- Determine the location of the first empty inventory slot. All items before this represent
  761. -- noise items.
  762. local foundFirstBlankInventorySlot = false
  763. noiseBlocksCount = 1
  764. while ((noiseBlocksCount < 16) and (foundFirstBlankInventorySlot == false)) do
  765. if (turtle.getItemCount(noiseBlocksCount) > 0) then
  766. noiseBlocksCount = noiseBlocksCount + 1
  767. else
  768. foundFirstBlankInventorySlot = true
  769. end
  770. end
  771. noiseBlocksCount = noiseBlocksCount - 1
  772.  
  773. -- Determine whether a chest was provided, and hence whether we should support
  774. -- looking for chests
  775. if (turtle.getItemCount(15) > 0) then
  776. lookForChests = true
  777. lastEmptySlot = 14
  778. miningOffset = 0
  779. writeMessage("Looking for chests...", messageLevel.DEBUG)
  780. else
  781. lastEmptySlot = 15
  782. miningOffset = 1
  783. writeMessage("Ignoring chests...", messageLevel.DEBUG)
  784. end
  785. end
  786.  
  787. -- ********************************************************************************** --
  788. -- Creates a quarry mining out only ores and leaving behind any noise blocks
  789. -- ********************************************************************************** --
  790. function createQuarry()
  791.  
  792. -- Determine the top mining layer layer. The turtle mines in layers of 3, and the bottom layer
  793. -- is the layer directly above bedrock.
  794. --
  795. -- The actual layer that the turtle operates in is the middle of these three layers,
  796. -- so determine the top layer
  797. local topMiningLayer = startHeight + ((bottomLayer - startHeight - 2) % 3) - 1 + miningOffset
  798.  
  799. -- If the top layer is up, then ignore it and move to the next layer
  800. if (topMiningLayer > currY) then
  801. topMiningLayer = topMiningLayer - 3
  802. end
  803.  
  804. local startedLayerToRight = true -- Only used where the quarry is of an odd width
  805.  
  806. -- Loop over each mining row
  807. local miningLevel
  808. for miningLevel = (bottomLayer + miningOffset), topMiningLayer, 3 do
  809. writeMessage("Mining Layer: "..miningLevel, messageLevel.INFO)
  810. haveBeenAtZeroZeroOnLayer = false
  811.  
  812. -- While the initial shaft is being dug out, set the level to return to in order to unload
  813. -- to the just take the turtle straight back up
  814. if (miningLevel == (bottomLayer + miningOffset)) then
  815. levelToReturnTo = startHeight
  816. end
  817.  
  818. -- Move to the correct level to start mining
  819. if (currY > miningLevel) then
  820. while (currY > miningLevel) do
  821. turtleDown()
  822. end
  823. elseif (currY < miningLevel) then
  824. while (currY < miningLevel) do
  825. turtleUp()
  826. end
  827. end
  828.  
  829. -- Set the layer to return via when returning to the surface as the one below the currently
  830. -- mined one
  831. if (miningLevel == (bottomLayer + miningOffset)) then
  832. levelToReturnTo = (bottomLayer + miningOffset)
  833. else
  834. levelToReturnTo = miningLevel - 3
  835. end
  836.  
  837. -- Move turtle into the correct orientation to start mining (if this is the
  838. -- first row to be mined, then don't need to turn, otherwise turn towards the next
  839. -- mining section)
  840.  
  841. writeMessage("Mining Level: "..miningLevel..", Bottom Layer: "..bottomLayer..", Mining Offset: "..miningOffset, messageLevel.DEBUG)
  842.  
  843. if (miningLevel > (bottomLayer + miningOffset)) then
  844. -- Turn towards the next mining layer
  845. if (quarryWidth % 2 == 0) then
  846. -- An even width quarry, always turn right
  847. turtleTurn(direction.RIGHT)
  848. else
  849. -- Turn the opposite direction to that which we turned before
  850. if (startedLayerToRight == true) then
  851. turtleTurn(direction.LEFT)
  852. startedLayerToRight = false
  853. else
  854. turtleTurn(direction.RIGHT)
  855. startedLayerToRight = true
  856. end
  857. end
  858. end
  859.  
  860. local mineRows
  861. local onNearSideOfQuarry = true
  862. local diggingAway = true
  863. for mineRows = 1, quarryWidth do
  864.  
  865. -- If this is not the first row, then get into position to mine the next row
  866. if ((mineRows == 1) and (lookForChests == false)) then
  867. -- Not looking for chests, check the block below for being an ore. Only do this
  868. -- if we're not looking for chests since the program doesn't support chests in
  869. -- bedrock
  870. if (isNoiseBlock(turtle.compareDown) == false) then
  871. turtle.digDown()
  872. ensureInventorySpace()
  873. end
  874. elseif (mineRows > 1) then
  875. -- Move into position for mining the next row
  876. if (onNearSideOfQuarry == diggingAway) then
  877. if (startedLayerToRight == true) then
  878. turtleTurn(direction.LEFT)
  879. else
  880. turtleTurn(direction.RIGHT)
  881. end
  882. else
  883. if (startedLayerToRight == true) then
  884. turtleTurn(direction.RIGHT)
  885. else
  886. turtleTurn(direction.LEFT)
  887. end
  888. end
  889.  
  890. turtleForward()
  891.  
  892. -- Before making the final turn, check the block below. Do this
  893. -- now because if it is a chest, then we want to back up and
  894. -- approach it from the side (so that we don't lose items if we
  895. -- have to return to the start through it).
  896. --
  897. -- This is the point at which it is safe to back up without moving
  898. -- out of the quarry area (unless at bedrock in which case don't bother
  899. -- as we'll be digging down anyway)
  900. if (miningLevel ~= bottomLayer) then
  901. if (isNoiseBlock(turtle.compareDown) == false) then
  902. -- If we are not looking for chests, then just dig it (it takes
  903. -- less time to try to dig and fail as it does to do detect and
  904. -- only dig if there is a block there)
  905. if (lookForChests == false) then
  906. turtle.digDown()
  907. ensureInventorySpace()
  908. elseif (turtle.detectDown() == true) then
  909. if (isChestBlock(turtle.compareDown) == true) then
  910. -- There is a chest block below. Move back and approach
  911. -- from the side to ensure that we don't need to return to
  912. -- start through the chest itself (potentially losing items)
  913. turtleBack()
  914. turtleDown()
  915. emptyChest(turtle.suck)
  916. turtleUp()
  917. turtleForward()
  918. turtle.digDown()
  919. ensureInventorySpace()
  920. else
  921. turtle.digDown()
  922. ensureInventorySpace()
  923. end
  924. end
  925. end
  926. end
  927.  
  928. -- Move into final position for mining the next row
  929. if (onNearSideOfQuarry == diggingAway) then
  930. if (startedLayerToRight == true) then
  931. turtleTurn(direction.LEFT)
  932. else
  933. turtleTurn(direction.RIGHT)
  934. end
  935. else
  936. if (startedLayerToRight == true) then
  937. turtleTurn(direction.RIGHT)
  938. else
  939. turtleTurn(direction.LEFT)
  940. end
  941. end
  942. end
  943.  
  944. -- Dig to the other side of the quarry
  945. local blocksMined
  946. for blocksMined = 0, (quarryWidth - 1) do
  947. if (blocksMined > 0) then
  948. -- Only move forward if this is not the first space
  949. turtleForward()
  950. end
  951.  
  952. -- If the current block is (0,0), then record the fact that the
  953. -- turtle has been through this block and what it's orientation was and update the layer
  954. -- that it should return via to get back to the surface (it no longer needs to go down
  955. -- a level to prevent losing ores).
  956. if ((currX == 0) and (currZ == 0)) then
  957. -- Am at (0, 0). Remember this, and what direction I was facing so that the quickest route
  958. -- to the surface can be taken
  959. levelToReturnTo = miningLevel
  960. haveBeenAtZeroZeroOnLayer = true
  961. orientationAtZeroZero = currOrient
  962. end
  963.  
  964. -- If currently at bedrock, just move down until the turtle can't go any
  965. -- further. This allows the blocks within the bedrock to be mined
  966. if (miningLevel == bottomLayer) then
  967. -- Temporarily turn off looking for chests to increase bedrock mining speed (this
  968. -- means that the program doesn't support chests below level 5 - but I think
  969. -- they they don't exist anyway)
  970. local lookForChestsPrev = lookForChests
  971. lookForChests = false
  972.  
  973. -- Manually set the flag to determine whether the turtle should try to move first or
  974. -- dig first. At bedrock, is very rarely any space
  975.  
  976. -- Just above bedrock layer, dig down until can't dig any lower, and then
  977. -- come back up. This replicates how the quarry functions
  978. lastMoveNeededDig = true
  979. local moveDownSuccess = turtleDown()
  980. while (moveDownSuccess == true) do
  981. moveDownSuccess = turtleDown()
  982. end
  983.  
  984. -- Know that we are moving back up through air, therefore set the flag to force the
  985. -- turtle to try moving first
  986. lastMoveNeededDig = false
  987.  
  988. -- Have now hit bedrock, move back to the mining layer
  989. while (currY < bottomLayer) do
  990. turtleUp()
  991. end
  992.  
  993. -- Now back at the level above bedrock, again reset the flag to tell the turtle to
  994. -- try digging again (because it is rare to find air at bedrock level)
  995. lastMoveNeededDig = false
  996.  
  997. -- Reset the look for chests value
  998. lookForChests = lookForChestsPrev
  999. elseif ((blocksMined > 0) and ((currX ~= 0) or (currZ ~= 0))) then
  1000. -- This isn't the first block of the row, nor are we at (0, 0) so we need to check the
  1001. -- block below
  1002.  
  1003. -- Check the block down for being a noise block (don't need to check the first
  1004. -- block as it has already been checked in the outer loop)
  1005. if (isNoiseBlock(turtle.compareDown) == false) then
  1006. -- If we are not looking for chests, then just dig it (it takes
  1007. -- less time to try to dig and fail as it does to do detect and
  1008. -- only dig if there is a block there)
  1009. if (lookForChests == false) then
  1010. turtle.digDown()
  1011. ensureInventorySpace()
  1012. elseif (turtle.detectDown() == true) then
  1013. if (isChestBlock(turtle.compareDown) == true) then
  1014. -- There is a chest block below. Move back and approach
  1015. -- from the side to ensure that we don't need to return to
  1016. -- start through the chest itself (potentially losing items)
  1017. turtleBack()
  1018. turtleDown()
  1019. emptyChest(turtle.suck)
  1020. turtleUp()
  1021. turtleForward()
  1022. turtle.digDown()
  1023. ensureInventorySpace()
  1024. else
  1025. turtle.digDown()
  1026. ensureInventorySpace()
  1027. end
  1028. end
  1029. end
  1030. end
  1031.  
  1032. -- Check the block above for ores (if we're not a (0, 0) in which case
  1033. -- we know it's air)
  1034. if ((currX ~= 0) or (currZ ~= 0)) then
  1035. if (isNoiseBlock(turtle.compareUp) == false) then
  1036. -- If we are not looking for chests, then just dig it (it takes
  1037. -- less time to try to dig and fail as it does to do detect and
  1038. -- only dig if there is a block there)
  1039. if (lookForChests == false) then
  1040. turtle.digUp()
  1041. ensureInventorySpace()
  1042. elseif (turtle.detectUp() == true) then
  1043. -- Determine if it is a chest before digging it
  1044. if (isChestBlock(turtle.compareUp) == true) then
  1045. -- There is a chest block above. Empty it before digging it
  1046. emptyChest(turtle.suckUp)
  1047. turtle.digUp()
  1048. ensureInventorySpace()
  1049. else
  1050. turtle.digUp()
  1051. ensureInventorySpace()
  1052. end
  1053. end
  1054. end
  1055. end
  1056. end
  1057.  
  1058. -- Am now at the other side of the quarry
  1059. onNearSideOfQuarry = not onNearSideOfQuarry
  1060. end
  1061.  
  1062. -- If we were digging away from the starting point, will be digging
  1063. -- back towards it on the next layer
  1064. diggingAway = not diggingAway
  1065. end
  1066.  
  1067. -- Return to the start
  1068. returnToStartAndUnload(false)
  1069.  
  1070. -- Face forward
  1071. turtleSetOrientation(direction.FORWARD)
  1072. end
  1073.  
  1074. -- ********************************************************************************** --
  1075. -- Main Function
  1076. -- ********************************************************************************** --
  1077. -- Process the input arguments - storing them to global variables
  1078. local args = { ... }
  1079. local paramsOK = true
  1080.  
  1081. turtleId = os.getComputerLabel()
  1082. rednet.open("right")
  1083.  
  1084. if (#args == 1) then
  1085. quarryWidth = tonumber(args[1])
  1086. local x, y, z = gps.locate(5)
  1087. startHeight = y
  1088. if (startHeight == nil) then
  1089. writeMessage("Can't locate GPS", messageLevel.FATAL)
  1090. paramsOK = false
  1091. end
  1092. elseif (#args == 2) then
  1093. quarryWidth = tonumber(args[1])
  1094. startHeight = tonumber(args[2])
  1095. else
  1096. writeMessage("Usage: OreQuarry <diameter> <turtleY>", messageLevel.FATAL)
  1097. paramsOK = false
  1098. end
  1099.  
  1100. if (paramsOK == true) then
  1101. if ((startHeight < 6) or (startHeight > 128)) then
  1102. writeMessage("turtleY must be between 6 and 128", messageLevel.FATAL)
  1103. paramsOK = false
  1104. end
  1105.  
  1106. if ((quarryWidth < 2) or (quarryWidth > 64)) then
  1107. writeMessage("diameter must be between 2 and 64", messageLevel.FATAL)
  1108. paramsOK = false
  1109. end
  1110. end
  1111.  
  1112. if (paramsOK == true) then
  1113. writeMessage("----------------------------------", messageLevel.INFO)
  1114. writeMessage("** Ore Quarry v0.53 by Poke5555 **", messageLevel.INFO)
  1115. writeMessage("----------------------------------", messageLevel.INFO)
  1116.  
  1117. -- Set the turtle's starting position
  1118. currX = 0
  1119. currY = startHeight
  1120. currZ = 0
  1121. currOrient = direction.FORWARD
  1122.  
  1123. -- Calculate which blocks in the inventory signify noise blocks
  1124. determineNoiseBlocksCountCount()
  1125.  
  1126. if ((noiseBlocksCount == 0) or (noiseBlocksCount > 13)) then
  1127. writeMessage("No noise blocks have been been added. Please place blocks that the turtle should not mine (e.g. Stone, Dirt, Gravel etc.) in the first few slots of the turtle\'s inventory. The first empty slot signifies the end of the noise blocks.", messageLevel.FATAL)
  1128. else
  1129. -- Create a Quarry
  1130. turtle.select(1)
  1131. currentlySelectedSlot = 1
  1132. createQuarry()
  1133. end
  1134. end
Advertisement
Add Comment
Please, Sign In to add comment