Youseikun

AustinKK's quarry w/filter list

Nov 26th, 2020 (edited)
1,371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 31.24 KB | None | 0 0
  1. -- ********************************************************************************************************************************************************* --
  2. -- **                                                                                                                                                     ** --
  3. -- **   Minecraft Mining Turtle Ore Quarry by AustinKK                                                                                               ** --
  4. -- **     w/minor edits by Youseikun                                                                                                                      ** --
  5. -- **                                                                                                                                                     ** --
  6. -- **   Instructions on how to use:                                                                                                                       ** --
  7. -- **   Must use ender chests                                                                                                                             ** --
  8. -- **   where the first slot is an ender chest with the refueling items, and the second chest accepts the quarried items                                  ** --
  9. -- **   The first argument is the squared size of the quarry and the second is the y height of the turtle                                                 ** --
  10. -- ********************************************************************************************************************************************************* --
  11. -- Enumeration to store the the different types of message that can be written
  12. messageLevel = { DEBUG=0, INFO=1, WARNING=2, ERROR=3, FATAL=4 }
  13.  
  14. -- Enumeration to store names for the 6 directions
  15. direction = { FORWARD=0, RIGHT=1, BACK=2, LEFT=3, UP=4, DOWN=5 }
  16.  
  17. local messageOutputLevel = messageLevel.INFO
  18. local fuelLevelToRefuelAt = 5
  19. local refuelItemsToUseWhenRefuelling = 1
  20. local emergencyFuelToRetain = 3
  21. local maximumGravelStackSupported = 25 -- The number of stacked gravel or sand blocks supported
  22. local nonSeamBlocks
  23. local bottomLayer = 2 -- The y co-ords of the layer immediately above bedrock
  24. local returningToStart = false
  25. local lookForChests = false -- Determines if chests should be located as part of the quarrying
  26. 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)
  27. local turtleId
  28.  
  29. -- Variables to store the current location and orientation of the turtle. x is right, left, y is up, down and
  30. -- z is forward, back with relation to the starting orientation. Y is the actual turtle level, x and z are
  31. -- in relation to the starting point (i.e. the starting point is (0, 0))
  32. local currX
  33. local currY
  34. local currZ
  35. local currOrient
  36.  
  37. -- Command line parameters
  38. local startHeight -- Represents the height (y co-ord) that the turtle started at
  39. local quarryWidth -- Represents the length of the mines that the turtle will dig
  40.  
  41.  
  42. -- List of blocks to ignore while mining
  43. local IGNORE_LIST = {
  44.   "minecraft:stone",
  45.   "minecraft:dirt",
  46.   "minecraft:cobblestone",
  47.   "chisel:limestone2",
  48.   "rustic:slate",
  49.   "biomesoplenty:flesh",
  50.   "minecraft:netherrack",
  51.   "tconstruct:ore",
  52.   "minecraft:air"
  53. }
  54.  
  55. -- ********************************************************************************** --
  56. -- Writes an output message
  57. -- ********************************************************************************** --
  58. function writeMessage(message, msgLevel)
  59.   if (msgLevel >= messageOutputLevel) then
  60.         print(message)
  61.         if (turtleId == nil) then
  62.           rednet.broadcast(message)
  63.         else
  64.           -- Broadcast the message (prefixed with the turtle's id)
  65.           rednet.broadcast("[".. turtleId.."] "..message)
  66.         end
  67.   end
  68. end
  69.  
  70. -- ********************************************************************************** --
  71. -- Ensures that the turtle has fuel
  72. -- ********************************************************************************** --
  73. function ensureFuel()
  74.  
  75.   -- Determine whether a refuel is required
  76.   local fuelLevel = turtle.getFuelLevel()
  77.   if (fuelLevel ~= "unlimited") then
  78.     if (fuelLevel < fuelLevelToRefuelAt) then
  79.       -- Need to refuel
  80.       local fuelAmount = turtle.getItemCount(16)
  81.       local grabAmount = 64 - fuelAmount
  82.       if (grabAmount ~= 0) then
  83.         turtle.digUp()
  84.         turtle.select(1)
  85.         turtle.placeUp()
  86.         turtle.select(16)
  87.         turtle.suckUp(grabAmount)
  88.         turtle.refuel(63)
  89.       else
  90.         turtle.select(16)
  91.         turtle.refuel(63)
  92.       end
  93.  
  94.       turtle.select(1)
  95.       --if (turtle.getItemCount(1) ~= 0) then
  96.       --  turtle.drop()
  97.       --end
  98.       turtle.digUp()
  99.     end
  100.   end
  101. end      
  102.  
  103. -- ********************************************************************************** --
  104. -- Checks that the turtle has inventory space by checking for spare slots and returning
  105. -- to the starting point to empty out if it doesn't.
  106. --
  107. -- Takes the position required to move to in order to empty the turtle's inventory
  108. -- should it be full as arguments
  109. -- ********************************************************************************** --
  110. function ensureInventorySpace()
  111.   -- If the last inventory slot is full, then need to return to the start and empty
  112.   if (turtle.getItemCount(lastEmptySlot) > 0) then
  113.  
  114.     -- Return to the starting point and empty the inventory, then go back to mining
  115.     emptyInventory()
  116.   end
  117. end
  118.  
  119. -- ********************************************************************************** --
  120. -- Function that is called when the turtle has returned to the start in order to
  121. -- empty its inventory into the chest and also pick up any fuel that is
  122. -- available
  123. -- ********************************************************************************** --
  124. function emptyInventory()
  125.  
  126.   local slotLoop = 3
  127.  
  128.   turtle.digUp()
  129.   turtle.select(2)
  130.   turtle.placeUp()
  131.  
  132.  
  133.   while (slotLoop < 16) do
  134.     turtle.select(slotLoop)
  135.     turtle.dropUp()
  136.     slotLoop = slotLoop + 1
  137.   end
  138.  
  139.  
  140.   turtle.select(2)
  141.   turtle.digUp()
  142.   turtle.select(1)
  143.  
  144. end
  145.  
  146. -- ********************************************************************************** --
  147. -- Function to move to the starting point, call a function that is passed in
  148. -- and return to the same location (if required)
  149. -- ********************************************************************************** --
  150. function returnToStartAndUnload(returnBackToMiningPoint)
  151.  
  152.   writeMessage("returnToStartAndUnload called", messageLevel.DEBUG)
  153.   returningToStart = true
  154.  
  155.   -- Store the current location and orientation so that it can be returned to
  156.   local storedX = currX
  157.   local storedY = currY
  158.   local storedZ = currZ
  159.   local storedOrient = currOrient
  160.  
  161.   -- First direction to move is straight up. Do this because the inventory is full and
  162.   -- therefore don't want to pass through any blocks that contain ores. Know that all
  163.   -- blocks above don't contain ores, so is safe to go that way
  164.   -- (Note, there is a rare edge case where the inventory becomes full after moving
  165.   -- forward and before checking the block above, this is quite rare and given the
  166.   -- likelihood of it not being able be added to the inventory (even though slot 15
  167.   -- is full), and is deemed an acceptable risk)
  168.   if (currY < startHeight) then
  169.         while (currY < startHeight) do
  170.           turtleUp()
  171.         end
  172.   elseif (currY > startHeight) then
  173.         -- This should never happen
  174.         writeMessage("Current height is greater than start height in returnToStartAndUnload", messageLevel.ERROR)
  175.   end
  176.   -- Move back to the correct X position
  177.   if (currX > 0) then
  178.         turtleSetOrientation(direction.LEFT)
  179.         while (currX > 0) do
  180.           turtleForward()
  181.         end
  182.   elseif (currX < 0) then
  183.         -- This should never happen
  184.         writeMessage("Current x is less than 0 in returnToStartAndUnload", messageLevel.ERROR)
  185.   end
  186.   -- Move back to the correct Z position
  187.   if (currZ > 0) then
  188.         turtleSetOrientation(direction.BACK)
  189.         while (currZ > 0) do
  190.           turtleForward()
  191.         end
  192.   elseif (currZ < 0) then
  193.         -- This should never happen
  194.         writeMessage("Current z is less than 0 in returnToStartAndUnload", messageLevel.ERROR)
  195.   end
  196.   emptyInventory()
  197.   turtle.select(1)
  198. end
  199.  
  200. -- ********************************************************************************** --
  201. -- Empties a chest's contents
  202. -- ********************************************************************************** --
  203. function emptyChest(suckFn)
  204.  
  205.   local prevInventoryCount = {}
  206.   local inventoryLoop
  207.   local chestEmptied = false
  208.  
  209.   -- Record the number of items in each of the inventory slots
  210.   for inventoryLoop = 1, 16 do
  211.         prevInventoryCount[inventoryLoop] = turtle.getItemCount(inventoryLoop)
  212.   end
  213.  
  214.   while (chestEmptied == false) do
  215.         -- Pick up the next item
  216.         suckFn()
  217.  
  218.         -- Determine the number of items in each of the inventory slots now
  219.         local newInventoryCount = {}
  220.         for inventoryLoop = 1, 16 do
  221.           newInventoryCount[inventoryLoop] = turtle.getItemCount(inventoryLoop)
  222.         end
  223.  
  224.         -- Now, determine whether there have been any items taken from the chest
  225.         local foundDifferentItemCount = false
  226.         inventoryLoop = 1
  227.         while ((foundDifferentItemCount == false) and (inventoryLoop <= 16)) do
  228.           if (prevInventoryCount[inventoryLoop] ~= newInventoryCount[inventoryLoop]) then
  229.                 foundDifferentItemCount = true
  230.           else
  231.                 inventoryLoop = inventoryLoop + 1
  232.           end
  233.         end
  234.  
  235.         -- If no items have been found with a different item count, then the chest has been emptied
  236.         chestEmptied = not foundDifferentItemCount
  237.  
  238.         if (chestEmptied == false) then
  239.           prevInventoryCount = newInventoryCount
  240.           -- Check that there is sufficient inventory space as may have picked up a block
  241.           ensureInventorySpace()
  242.         end
  243.   end
  244.  
  245.   writeMessage("Finished emptying chest", messageLevel.DEBUG)
  246. end
  247.  
  248.  
  249. -- ********************************************************************************** --
  250. -- Generic function to move the Turtle (pushing through any gravel or other
  251. -- things such as mobs that might get in the way).
  252. --
  253. -- The only thing that should stop the turtle moving is bedrock. Where this is
  254. -- found, the function will return after 15 seconds returning false
  255. -- ********************************************************************************** --
  256. function moveTurtle(moveFn, detectFn, digFn, attackFn, compareFn, suckFn, maxDigCount)
  257.  
  258.   ensureFuel()
  259.  
  260.   -- If we are looking for chests, then check that this isn't a chest before moving
  261.   if (lookForChests == true) then
  262.         if (isChestBlock(compareFn) == true) then
  263.           -- Have found a chest, empty it before continuing
  264.           emptyChest (suckFn)
  265.         end
  266.   end
  267.  
  268.   -- Flag to determine whether digging has been tried yet. If it has
  269.   -- then pause briefly before digging again to allow sand or gravel to
  270.   -- drop
  271.   local digCount = 0
  272.   local moveSuccess = moveFn()
  273.  
  274.   while ((moveSuccess == false) and (digCount < maxDigCount)) do
  275.  
  276.         -- If there is a block in front, dig it
  277.         if (detectFn() == true) then
  278.          
  279.                 -- If we've already tried digging, then pause before digging again to let
  280.                 -- any sand or gravel drop
  281.                 if(digCount > 0) then
  282.                   sleep(0.4)
  283.                 end
  284.  
  285.                 digFn()
  286.                 digCount = digCount + 1
  287.         else
  288.            -- Am being stopped from moving by a mob, attack it
  289.            attackFn()
  290.         end
  291.  
  292.         -- Try the move again
  293.         moveSuccess = moveFn()
  294.   end
  295.  
  296.   -- Return the move success
  297.   return moveSuccess
  298.  
  299. end
  300.  
  301. -- ********************************************************************************** --
  302. -- Move the turtle forward one block (updating the turtle's position)
  303. -- ********************************************************************************** --
  304. function turtleForward()
  305.   local returnVal = moveTurtle(turtle.forward, turtle.detect, turtle.dig, turtle.attack, turtle.compare, turtle.suck, maximumGravelStackSupported)
  306.   if (returnVal == true) then
  307.         -- Update the current co-ordinates
  308.         if (currOrient == direction.FORWARD) then
  309.           currZ = currZ + 1
  310.         elseif (currOrient == direction.LEFT) then
  311.           currX = currX - 1
  312.         elseif (currOrient == direction.BACK) then
  313.           currZ = currZ - 1
  314.         elseif (currOrient == direction.RIGHT) then
  315.           currX = currX + 1
  316.         else
  317.           writeMessage ("Invalid currOrient in turtleForward function", messageLevel.ERROR)
  318.         end
  319.  
  320.         -- Check that there is sufficient inventory space as may have picked up a block
  321.         ensureInventorySpace()
  322.   end
  323.  
  324.   return returnVal
  325. end
  326.  
  327. -- ********************************************************************************** --
  328. -- Move the turtle up one block (updating the turtle's position)
  329. -- ********************************************************************************** --
  330. function turtleUp()
  331.   local returnVal = moveTurtle(turtle.up, turtle.detectUp, turtle.digUp, turtle.attackUp, turtle.compareUp, turtle.suckUp, maximumGravelStackSupported)
  332.   if (returnVal == true) then
  333.         currY = currY + 1
  334.  
  335.         -- Check that there is sufficient inventory space as may have picked up a block
  336.         ensureInventorySpace()
  337.   end
  338.   return returnVal
  339. end
  340.  
  341. -- ********************************************************************************** --
  342. -- Move the turtle down one block (updating the turtle's position)
  343. -- ********************************************************************************** --
  344. function turtleDown()
  345.   local returnVal
  346.  
  347.   -- Because the turtle is digging down, can fail fast (only allow 1 dig attempt).
  348.   returnVal = moveTurtle(turtle.down, turtle.detectDown, turtle.digDown, turtle.attackDown, turtle.compareDown, turtle.suckDown, 1)
  349.   if (returnVal == true) then
  350.         currY = currY - 1
  351.  
  352.         -- Check that there is sufficient inventory space as may have picked up a block
  353.         ensureInventorySpace()
  354.   end
  355.   return returnVal
  356. end
  357.  
  358. -- ********************************************************************************** --
  359. -- Move the turtle back one block (updating the turtle's position)
  360. -- ********************************************************************************** --
  361. function turtleBack()
  362.   -- First try to move back using the standard function
  363.   local returnVal = turtle.back()
  364.  
  365.   -- Moving back didn't work (might be a block or a mob in the way). Turn round and move
  366.   -- forward instead (whereby anything in the way can be cleared)
  367.   if(returnVal == false) then
  368.         turtle.turnRight()
  369.         turtle.turnRight()
  370.         returnVal = turtleForward()
  371.         turtle.turnRight()
  372.         turtle.turnRight()
  373.   end
  374.  
  375.   if (returnVal == true) then
  376.         -- Update the current co-ordinates
  377.         if (currOrient == direction.FORWARD) then
  378.           currZ = currZ - 1
  379.         elseif (currOrient == direction.LEFT) then
  380.           currX = currX + 1
  381.         elseif (currOrient == direction.BACK) then
  382.           currZ = currZ + 1
  383.         elseif (currOrient == direction.RIGHT) then
  384.           currX = currX - 1
  385.         else
  386.           writeMessage ("Invalid currOrient in turtleBack function", messageLevel.ERROR)
  387.         end
  388.  
  389.         -- Check that there is sufficient inventory space as may have picked up a block
  390.         ensureInventorySpace()
  391.   end
  392.  
  393.   return returnVal
  394. end
  395.  
  396. -- ********************************************************************************** --
  397. -- Turns the turtle (updating the current orientation at the same time)
  398. -- ********************************************************************************** --
  399. function turtleTurn(turnDir)
  400.  
  401.   if (turnDir == direction.LEFT) then
  402.         if (currOrient == direction.FORWARD) then
  403.           currOrient = direction.LEFT
  404.           turtle.turnLeft()
  405.         elseif (currOrient == direction.LEFT) then
  406.           currOrient = direction.BACK
  407.           turtle.turnLeft()
  408.         elseif (currOrient == direction.BACK) then
  409.           currOrient = direction.RIGHT
  410.           turtle.turnLeft()
  411.         elseif (currOrient == direction.RIGHT) then
  412.           currOrient = direction.FORWARD
  413.           turtle.turnLeft()
  414.         else
  415.           writeMessage ("Invalid currOrient in turtleTurn function", messageLevel.ERROR)
  416.         end
  417.   elseif (turnDir == direction.RIGHT) then
  418.         if (currOrient == direction.FORWARD) then
  419.           currOrient = direction.RIGHT
  420.           turtle.turnRight()
  421.         elseif (currOrient == direction.LEFT) then
  422.           currOrient = direction.FORWARD
  423.           turtle.turnRight()
  424.         elseif (currOrient == direction.BACK) then
  425.           currOrient = direction.LEFT
  426.           turtle.turnRight()
  427.         elseif (currOrient == direction.RIGHT) then
  428.           currOrient = direction.BACK
  429.           turtle.turnRight()
  430.         else
  431.           writeMessage ("Invalid currOrient in turtleTurn function", messageLevel.ERROR)
  432.         end
  433.   else
  434.         writeMessage ("Invalid turnDir in turtleTurn function", messageLevel.ERROR)
  435.   end
  436. end
  437.  
  438. -- ********************************************************************************** --
  439. -- Sets the turtle to a specific orientation, irrespective of its current orientation
  440. -- ********************************************************************************** --
  441. function turtleSetOrientation(newOrient)
  442.  
  443.   if (currOrient ~= newOrient) then
  444.         if (currOrient == direction.FORWARD) then
  445.           if (newOrient == direction.RIGHT) then
  446.                 turtle.turnRight()
  447.                 currOrient = newOrient
  448.           elseif (newOrient == direction.BACK) then
  449.                 turtle.turnRight()
  450.                 turtle.turnRight()
  451.                 currOrient = newOrient
  452.           elseif (newOrient == direction.LEFT) then
  453.                 turtle.turnLeft()
  454.                 currOrient = newOrient
  455.           else
  456.                 writeMessage ("Invalid newOrient in turtleSetOrientation function", messageLevel.ERROR)
  457.           end
  458.         elseif (currOrient == direction.RIGHT) then
  459.           if (newOrient == direction.BACK) then
  460.                 turtle.turnRight()
  461.                 currOrient = newOrient
  462.           elseif (newOrient == direction.LEFT) then
  463.                 turtle.turnRight()
  464.                 turtle.turnRight()
  465.                 currOrient = newOrient
  466.           elseif (newOrient == direction.FORWARD) then
  467.                 turtle.turnLeft()
  468.                 currOrient = newOrient
  469.           else
  470.                 writeMessage ("Invalid newOrient in turtleSetOrientation function", messageLevel.ERROR)
  471.           end
  472.         elseif (currOrient == direction.BACK) then
  473.           if (newOrient == direction.LEFT) then
  474.                 turtle.turnRight()
  475.                 currOrient = newOrient
  476.           elseif (newOrient == direction.FORWARD) then
  477.                 turtle.turnRight()
  478.                 turtle.turnRight()
  479.                 currOrient = newOrient
  480.           elseif (newOrient == direction.RIGHT) then
  481.                 turtle.turnLeft()
  482.                 currOrient = newOrient
  483.           else
  484.                 writeMessage ("Invalid newOrient in turtleSetOrientation function", messageLevel.ERROR)
  485.           end
  486.         elseif (currOrient == direction.LEFT) then
  487.           if (newOrient == direction.FORWARD) then
  488.                 turtle.turnRight()
  489.                 currOrient = newOrient
  490.           elseif (newOrient == direction.RIGHT) then
  491.                 turtle.turnRight()
  492.                 turtle.turnRight()
  493.                 currOrient = newOrient
  494.           elseif (newOrient == direction.BACK) then
  495.                 turtle.turnLeft()
  496.                 currOrient = newOrient
  497.           else
  498.                 writeMessage ("Invalid newOrient in turtleSetOrientation function", messageLevel.ERROR)
  499.           end
  500.         else
  501.           writeMessage ("Invalid currOrient in turtleTurn function", messageLevel.ERROR)
  502.         end
  503.   end
  504. end
  505.  
  506. -- ********************************************************************************** --
  507. -- Determines if a particular block is considered a noise block or not. A noise
  508. -- block is one that is a standard block in the game (stone, dirt, gravel etc.) and
  509. -- is one to ignore when a seam is being dug out. Function works by comparing the block
  510. -- in question against a set of blocks in the turtle's inventory which are known not to
  511. -- be noise blocks. The first parameter is the number of slots containing noise blocks
  512. -- (these must be at the start of the turtle's inventory), and the second param is the
  513. -- function to use to compare the block for a noise block
  514. -- ********************************************************************************** --
  515. function isNoiseBlock(detectFn, compareFn)
  516.  
  517.   local returnVal = false
  518.   local success, block = detectFn()
  519.   local seamLoop = 1
  520.  
  521.   if success then
  522.     for filterIndex = 1, #IGNORE_LIST, 1 do
  523.       if(block.name == IGNORE_LIST[filterIndex]) then
  524.         returnVal = true
  525.       end
  526.     end
  527.   else
  528.     retunVal = true
  529.   end
  530.  
  531.   --If return is true turtle doesn't mine the block
  532.   return returnVal
  533.  
  534. end
  535.  
  536. -- ********************************************************************************** --
  537. -- Determines if a particular block is a chest. Returns false if it is not a chest
  538. -- or chests are not being detected
  539. -- ********************************************************************************** --
  540. function isChestBlock(compareFn)
  541.  
  542.   -- Check the block in the appropriate direction to see whether it is a chest. Only
  543.   -- do this if we are looking for chests
  544.   local returnVal = false
  545.   if (lookForChests == true) then
  546.         turtle.select(15)
  547.         returnVal = compareFn()
  548.   end
  549.  
  550.   -- Return the calculated value
  551.   return returnVal
  552.  
  553. end
  554.  
  555. -- ********************************************************************************** --
  556. -- Function to calculate the number of non seam blocks in the turtle's inventory. This
  557. -- is all of the blocks at the start of the inventory (before the first empty slot is
  558. -- found
  559. -- ********************************************************************************** --
  560. function determineNonSeamBlocksCount()
  561.   -- Determine the location of the first empty inventory slot. All items before this represent
  562.   -- noise items.
  563.   local foundFirstBlankInventorySlot = false
  564.   nonSeamBlocks = 1
  565.   while ((nonSeamBlocks < 16) and (foundFirstBlankInventorySlot == false)) do
  566.         if (turtle.getItemCount(nonSeamBlocks) > 0) then
  567.           nonSeamBlocks = nonSeamBlocks + 1
  568.         else
  569.           foundFirstBlankInventorySlot = true
  570.         end
  571.   end
  572.   nonSeamBlocks = nonSeamBlocks - 1
  573.  
  574.   -- Determine whether a chest was provided, and hence whether we should support
  575.   -- looking for chests
  576.   if (turtle.getItemCount(15) > 0) then
  577.         lookForChests = true
  578.         lastEmptySlot = 14
  579.         writeMessage("Looking for chests...", messageLevel.DEBUG)
  580.   else
  581.         lastEmptySlot = 15
  582.         writeMessage("Ignoring chests...", messageLevel.DEBUG)
  583.   end
  584. end
  585.  
  586. -- ********************************************************************************** --
  587. -- Creates a quarry mining out only ores and leaving behind any noise blocks
  588. -- ********************************************************************************** --
  589. function createQuarry()
  590.  
  591.   -- Determine the starting layer. The turtle mines in layers of 3, and the bottom layer
  592.   -- is the layer directly above bedrock.
  593.   --
  594.   -- The actual layer that the turtle operates in is the middle of these three layers,
  595.   -- so determine the top layer
  596.   local firstMiningLayer = startHeight + ((bottomLayer - startHeight - 2) % 3) - 1
  597.  
  598.   -- If the top layer is up, then ignore it and move to the next layer
  599.   if (firstMiningLayer > currY) then
  600.         firstMiningLayer = firstMiningLayer - 3
  601.   end
  602.  
  603.   local miningLoop
  604.   local firstTimeThru = true
  605.   local onNearSideOfQuarry = true
  606.   local diggingAway = true
  607.  
  608.   -- Loop over each mining row
  609.   for miningLoop = firstMiningLayer, bottomLayer, -3 do
  610.         writeMessage("Mining Layer: "..miningLoop, messageLevel.INFO)
  611.  
  612.         -- Move to the correct level to start mining
  613.         if (currY > miningLoop) then
  614.           while (currY > miningLoop) do
  615.                 turtleDown()
  616.           end
  617.         end
  618.  
  619.         -- Move turtle into the correct orientation to start mining
  620.         if (firstTimeThru == true) then
  621.           turtleTurn(direction.RIGHT)
  622.           firstTimeThru = false
  623.         else
  624.           turtleTurn(direction.LEFT)
  625.           turtleTurn(direction.LEFT)
  626.         end
  627.  
  628.         local firstRow = true
  629.         local mineRows
  630.         for mineRows = 1, quarryWidth do
  631.           -- If this is not the first row, then get into position to mine the next row
  632.           if (firstRow == true) then
  633.                 firstRow = false
  634.           else
  635.                 -- Move into position for mining the next row
  636.                 if (onNearSideOfQuarry == diggingAway) then
  637.                   turtleTurn(direction.RIGHT)
  638.                 else
  639.                   turtleTurn(direction.LEFT)
  640.                 end
  641.  
  642.                 turtleForward()
  643.                 if (isChestBlock(turtle.compareUp) == true) then
  644.                   -- There is a chest block above. Move back and approach
  645.                   -- from the side to ensure that we don't need to return to
  646.                   -- start through the chest itself (potentially losing items)
  647.                   turtleBack()
  648.                   turtleUp()
  649.                   emptyChest(turtle.suck)
  650.                   turtleDown()
  651.                   turtleForward()
  652.                 end
  653.  
  654.                 -- Move into final position for mining the next row
  655.                 if (onNearSideOfQuarry == diggingAway) then
  656.                   turtleTurn(direction.RIGHT)
  657.                 else
  658.                   turtleTurn(direction.LEFT)
  659.                 end
  660.           end
  661.  
  662.           -- Dig to the other side of the quarry
  663.           local blocksMined
  664.           for blocksMined = 0, (quarryWidth - 1) do
  665.                 -- Move forward and check for ores
  666.                 if(blocksMined > 0) then
  667.                   -- Only move forward if this is not the first space
  668.                   turtleForward()
  669.                 end
  670.                
  671.                 -- Check upwards for a chest block if this is not the first block of the
  672.                 -- row (in which case it will have already been checked)
  673.                 if(blocksMined > 0) then
  674.                   if (isChestBlock(turtle.compareUp) == true) then
  675.                         -- There is a chest block above. Move back and approach
  676.                         -- from the side to ensure that we don't need to return to
  677.                         -- start through the chest itself (potentially losing items)
  678.                         turtleBack()
  679.                         turtleUp()
  680.                         emptyChest(turtle.suck)
  681.                         turtleDown()
  682.                         turtleForward()
  683.                   end
  684.                 end
  685.          
  686.                 if (isNoiseBlock(turtle.inspectUp, turtle.compareUp) == false) then
  687.                   turtle.digUp()
  688.                   ensureInventorySpace()
  689.                 end
  690.  
  691.                 -- Check down, if this is the bedrock layer, use an alternate approach.
  692.                 -- If this is not the bedrock layer, then just check down
  693.                 if (miningLoop == bottomLayer) then
  694.                   -- Just above bedrock layer, dig down until can't dig any lower, and then
  695.                   -- come back up. This replicates how the quarry functions
  696.                   local moveDownSuccess = turtleDown()
  697.                   while (moveDownSuccess == true) do
  698.                         moveDownSuccess = turtleDown()
  699.                   end
  700.  
  701.                   -- Have now hit bedrock, move back to the mining layer
  702.                   writeMessage("Moving back to mining layer", messageLevel.DEBUG)
  703.                   writeMessage("currY: "..currY..", bottomLayer: "..bottomLayer, messageLevel.DEBUG)
  704.                   while (currY < bottomLayer) do
  705.                         turtleUp()
  706.                   end
  707.                 else
  708.                   -- Check the block down for being a chest
  709.                   if (isChestBlock(turtle.compareDown) == true) then
  710.                         emptyChest(turtle.suckDown)
  711.                   end
  712.  
  713.                   -- Check the block down and mine if necessary
  714.                   if (isNoiseBlock(turtle.inspectDown, turtle.compareDown) == false) then
  715.                         turtle.digDown()
  716.                         ensureInventorySpace()
  717.                   end
  718.                 end
  719.           end
  720.  
  721.           -- Am now at the other side of the quarry
  722.           onNearSideOfQuarry = not onNearSideOfQuarry
  723.         end
  724.  
  725.         -- If we were digging away from the starting point, will be digging
  726.         -- back towards it on the next layer
  727.         diggingAway = not diggingAway
  728.   end
  729.  
  730.   -- Return to the start
  731.   returnToStartAndUnload(false)
  732.  
  733.   -- Face forward
  734.   turtleSetOrientation(direction.FORWARD)
  735. end
  736.  
  737. -- ********************************************************************************** --
  738. -- Main Function                                                                                
  739. -- ********************************************************************************** --
  740. -- Process the input arguments - storing them to global variables
  741. local args = { ... }
  742. local paramsOK = true
  743. turtleId = os.getComputerLabel()
  744. --rednet.open("right")
  745. if (#args == 1) then
  746.   quarryWidth = tonumber(args[1])
  747.   local x, y, z = gps.locate(5)
  748.   startHeight = y
  749.   if (startHeight == nil) then
  750.         writeMessage("Can't locate GPS", messageLevel.FATAL)
  751.         paramsOK = false
  752.   end
  753. elseif (#args == 2) then
  754.   quarryWidth = tonumber(args[1])
  755.   startHeight = tonumber(args[2])
  756. else
  757.   writeMessage("Usage: OreQuarry <diameter> <turtleY>", messageLevel.FATAL)
  758.   paramsOK = false
  759. end
  760. if (paramsOK == true) then
  761.   if ((startHeight < 6) or (startHeight > 128)) then
  762.         writeMessage("turtleY must be between 6 and 128", messageLevel.FATAL)
  763.         paramsOK = false
  764.   end
  765.  
  766.   if ((quarryWidth < 2) or (quarryWidth > 64)) then
  767.         writeMessage("diameter must be between 2 and 64", messageLevel.FATAL)
  768.         paramsOK = false
  769.   end
  770. end
  771.  
  772. if (paramsOK == true) then
  773.   writeMessage("---------------------------------", messageLevel.INFO)
  774.   writeMessage("** Ore Quarry v0.2 by AustinKK **", messageLevel.INFO)
  775.   writeMessage("    ** w/ edits by Youseikun **  ", messageLevel.INFO)
  776.   writeMessage("---------------------------------", messageLevel.INFO)
  777.  
  778.   -- Set the turtle's starting position
  779.   currX = 0
  780.   currY = startHeight
  781.   currZ = 0
  782.   currOrient = direction.FORWARD
  783.  
  784.   -- Calculate which blocks in the inventory signify noise blocks
  785.   determineNonSeamBlocksCount()
  786.  
  787.   if ((nonSeamBlocks == 0) or (nonSeamBlocks == 15)) then
  788.         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)
  789.   else
  790.         -- Create a Quary
  791.         createQuarry()
  792.   end
  793. end
Add Comment
Please, Sign In to add comment