Advertisement
xKevinn

Untitled

Feb 13th, 2013
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2.  
  3.   Rectangular Quarry Program 1.1b
  4.   by Adam Smith "shiphorns"
  5.   January 2013
  6.  
  7.   1.0b -    Original public release
  8.   1.1b -    Fixes bug with turtle using the wrong axis order when trying to return home after hitting
  9.             and undiggable block. I erroneously had it trying to do moveTo(0,0,0) instead of goHome()
  10.             which would result in the turtle trying to move in the x-direction first, and possibly
  11.             getting blocked by bedrock when trying to move home.
  12.  
  13. --]]
  14. local tArgs = { ... }
  15. local sizeZ -- Quarry is this long in direction turtle is initially facing, including block turtle is on
  16. local sizeX -- Quarry is this wide to the right of where turtle is facing, including block turtle is on
  17. local sizeY -- Quarry removes this many layers of blocks including layer where turtle starts
  18. local bDebug= false
  19.  
  20. local goUnload  -- Forward declaration
  21.  
  22. if (#tArgs == 1) then
  23.     sizeZ,sizeX,sizeY = tonumber(tArgs[1]),tonumber(tArgs[1]),256
  24. elseif (#tArgs == 2) then
  25.     sizeZ,sizeX,sizeY = tonumber(tArgs[1]),tonumber(tArgs[2]),256
  26. elseif (#tArgs >= 3) then
  27.     sizeZ,sizeX,sizeY = tonumber(tArgs[1]),tonumber(tArgs[2]),tonumber(tArgs[3])
  28.     if (#tArgs > 3) then
  29.         bDebug = (tonumber(tArgs[4])==1)
  30.     end
  31. else
  32.     print( "Usage: quarry <sq. size> <optional width > <optional fixed depth> <optional 1 for debug mode>" )
  33.     return
  34. end
  35.  
  36. -- Validate dimensions
  37. if (sizeX<2 or sizeZ<2 or sizeY<1) then
  38.   print( "Dimensions given must be at least 2L x 2W x 1D. Efficiency is optimal if fixed depth is a multiple of 3." )
  39.   return
  40. end
  41.  
  42. local minFuel = math.ceil((math.ceil(sizeY/3)*(sizeX*sizeY)+(2*sizeY))/1200)
  43. local maxFuel = "TBD"
  44.  
  45. print("Place fuel reserves in slot 1 (upper left) if desired and hit any key to start.")
  46. os.pullEvent("char")
  47.  
  48. local tX,tZ,tY = 0,0,0    -- Place the turtle starts is considered block 0,0,0 in the turtle's local coordinate system
  49. local xDir,zDir = 0,1     -- Turtle is considered as initially facing positive z direction, regardless of global world facing direction
  50. local refuelSlot = 1      -- Turtle can fuel from any slot, but it will never dump this slot's contents so this is where fuel should be placed
  51.  
  52. -- Notice that all coordinates formated as 0,0,0 are in X,Z,Y order, NOT alphabetical X,Y,Z order, where Y is up/down axis
  53. -- Y axis is always the minecraft world Y-axis, but X and Z in this turtle's local coordinate system won't necessarily match his
  54. -- orientation in the global world system (turtle's system is relative to how he is initially facing, +Z is his facing direction)
  55.  
  56. local function checkFuel(bMovingAwayFromOrigin)
  57.     if bDebug then print("checkFuel()") end
  58.     -- This function returns true only if there is enough fuel left to move 1 block in any direction,
  59.     -- and still have enough left over for a return trip to 0,0,0 that might be needed for refuel or at
  60.     -- the end of the quarrying. This ensures the turtle is never stranded in the quarry.
  61.     local fuelLevel = turtle.getFuelLevel()
  62.    
  63.     if (fuelLevel == "unlimited") then
  64.         -- Server has fuel requirement turned off in configs
  65.         return true
  66.     end
  67.    
  68.     -- If the turtle is attempting to move away from its starting location, it is going to
  69.     -- consume the normal 1 fuel cost to move, but it will also add +1 to the cost of the
  70.     -- trip to return home to dump/refuel/finish. If we know the turtle is moving closer to
  71.     -- home, there is no extra cost since it is effectively part of the return trip.
  72.     local fuelNeeded = math.abs(tX)+math.abs(tY)+math.abs(tZ)
  73.     if (bMovingAwayFromOrigin == nil or bMovingAwayFromOrigin == true) then
  74.         -- Turtle is moving away from 0,0,0 or direction is unspecified (assume worst case), add 2 fuel
  75.         fuelNeeded = fuelNeeded + 2
  76.     end
  77.  
  78.     if (fuelLevel >= fuelNeeded) then
  79.         -- Turtle has enough fuel to do the next 1-block movement, plus enough to
  80.         -- return home from there.
  81.         return true
  82.     end
  83.  
  84.     -- If we get here, turtle does not have enough fuel for the move plus a return to base
  85.     -- First we will try to refuel from anything we find in the turtle's inventory. Failing that
  86.     -- We will return to home and prompt the user to add fuel
  87.    
  88.     local slot = 1
  89.     turtle.select(slot)
  90.    
  91.     if bDebug then print("Entering while true do in checkFuel") end
  92.     while true do
  93.         if turtle.refuel(1) then
  94.             -- Found fuel in current slot, consume 1, see if it's enough, if not loop again
  95.             if (turtle.getFuelLevel()>=fuelNeeded) then
  96.                 print("Refueled from inventory, resuming quarrying...")
  97.                 return true
  98.             end
  99.         else
  100.             -- Couldn't refuel from currently-selected slot, try next slot. If there are no more slots, ask for player help.
  101.             if (slot < 16) then
  102.                 slot = slot + 1
  103.                 turtle.select(slot)
  104.             else
  105.                 -- There are no more slots to look in, reset selection so that we're ready to loop over all slots again, and so that the
  106.                 -- player sees slot 1 highlighted (fastest for turtle to find fuel in). Return to 0,0,0 if we can (in case turtle is
  107.                 -- under lava or otherwise inaccessible), prompt player to add fuel.
  108.  
  109.                 return goUnload(true)
  110.             end
  111.         end
  112.     end
  113. end
  114.  
  115. local function turnLeft()
  116.   turtle.turnLeft()
  117.   xDir,zDir = -zDir,xDir
  118.   return true
  119. end
  120.  
  121. local function turnRight()
  122.   turtle.turnRight()
  123.   xDir,zDir = zDir,-xDir
  124.   return true
  125. end
  126.  
  127. local function goForward(bCheckFuel)
  128.     if bDebug then print("goForward()") end
  129.     -- Can't move without fuel. checkFuel() will wait on player if necessary.
  130.     if (bCheckFuel==true or bCheckFuel==nil) then
  131.     checkFuel((xDir>0 and tX>=0) or (xDir<0 and tX<=0) or (zDir>0 and tZ>=0) or (zDir<0 and tZ<=0)) -- Passes boolean true if moving away from 0,0,0
  132.   end
  133.  
  134.     local tries = 3
  135.     while not turtle.forward() do
  136.         if bDebug then print("goForward: while not turtle.forward() do tries="..tries) end
  137.         if turtle.detect() then
  138.             if bDebug then print("goForward: detect") end
  139.             if not turtle.dig() then
  140.                 print("Undiggable block encountered. Will retry in 5 seconds.")
  141.                 -- Turtle is blocked. In case this is a temporary glitch, we try 3 times before conceding hard failure
  142.                 tries = tries - 1
  143.                 if (tries <= 0) then
  144.                   return false
  145.                 else
  146.                     if bDebug then print("goForward: sleep(5)") end
  147.                     sleep(5) -- Wait 5 seconds, hope the problem resolves itself
  148.                 end
  149.             end
  150.         elseif turtle.attack() then
  151.             if bDebug then print("goForward: attack") end
  152.             -- Had to attack player or mob. You can add additional code here such
  153.             -- as turtle.suck() if you want to collect killed mob loot. This is a quarry program
  154.             -- to collect ores, not rotten flesh and bones, so this block is empty.
  155.         else
  156.             -- Unknown obstruction, possibly a player in
  157.             -- peaceful or creative mode. Try again in 0.5 seconds and hope it's gone.
  158.             if bDebug then
  159.                 print("goForward: sleep(0.5) else block")
  160.                 print("Turtle fuel="..turtle.getFuelLevel())
  161.             end
  162.             sleep(0.5)
  163.         end
  164.     end
  165.  
  166.     tX = tX + xDir  -- If we're moving in the xDir, this will change tX by + or - 1
  167.     tZ = tZ + zDir  -- If we're moving in the zDir, this will change tZ by + or - 1
  168.  
  169.     return true -- Turtle moved successfully
  170. end
  171.  
  172. local function goDown(bCheckFuel)
  173.     if bDebug then print("goDown()") end
  174.     -- Can't move without fuel. checkFuel() will wait on player if necessary.
  175.     if (bCheckFuel==true or bCheckFuel==nil) then
  176.     checkFuel(tY<=0)    -- Passes boolean true if moving away from 0,0,0
  177.   end
  178.  
  179.   local tries = 3
  180.     while not turtle.down() do
  181.         if bDebug then print("goDown: while not turtle.down() do tries="..tries) end
  182.         if turtle.detectDown() then
  183.             if bDebug then print("goDown: detectDown") end
  184.             if not turtle.digDown() then
  185.                 print("Undiggable block encountered. Will retry in 5 seconds")
  186.                 -- Turtle is blocked. In case this is a temporary glitch, we try 3 times before conceding hard failure
  187.                 tries = tries - 1
  188.                 if (tries <= 0) then
  189.                   return false
  190.                 else
  191.                     if bDebug then print("goDown: sleep(5)") end
  192.                   sleep(5) -- Wait 5 seconds, hope the problem resolves itself
  193.                 end
  194.             end
  195.         elseif turtle.attackDown() then
  196.             if bDebug then print("goDown: attack") end
  197.             -- Had to attack player or mob. You can add additional code here such
  198.             -- as turtle.suck() if you want to collect killed mob loot. This is a quarry program
  199.             -- to collect ores, not rotten flesh and bones, so this block is empty.
  200.         else
  201.             -- Unknown obstruction, possibly a player in
  202.             -- peaceful or creative mode. Try again in 0.5 seconds and hope it's gone.
  203.             if bDebug then print("goDown: sleep(0.5)") end
  204.             sleep(0.5)
  205.         end
  206.     end
  207.  
  208.     tY = tY - 1
  209.     return true -- Turtle moved successfully
  210. end
  211.  
  212. local function goUp(bCheckFuel)
  213.     if bDebug then print("goUp()") end
  214.    
  215.   -- Can't move without fuel. checkFuel() will wait on player if necessary.
  216.   if (bCheckFuel==true or bCheckFuel==nil) then
  217.     checkFuel(tY>=0)    -- Passes boolean true if moving away from 0,0,0
  218.   end
  219.  
  220.   local tries = 3
  221.     while not turtle.up() do
  222.         if bDebug then print("goUp: while not loop tries="..tries) end
  223.         if turtle.detectUp() then
  224.             if bDebug then print("goUp: detectUp") end
  225.               if not turtle.digUp() then
  226.                         print("Undiggable block encountered. Will retry in 5 seconds.")
  227.                         -- Turtle is blocked. In case this is a temporary glitch, we try 3 times before conceding hard failure
  228.                 tries = tries - 1
  229.                 if (tries <= 0) then
  230.                   return false
  231.                 else
  232.                   sleep(10) -- Wait 10 seconds, hope the problem resolves itself
  233.                 end
  234.             end
  235.         elseif turtle.attackUp() then
  236.             if bDebug then print("goUp: attack") end
  237.             -- Had to attack player or mob. You can add additional code here such
  238.             -- as turtle.suck() if you want to collect killed mob loot. This is a quarry program
  239.             -- to collect ores, not rotten flesh and bones, so this block is empty.
  240.         else
  241.             -- Unknown obstruction, possibly a player in
  242.             -- peaceful or creative mode. Try again in 0.5 seconds and hope it's gone.
  243.             if bDebug then print("goUp: sleep(0.5)") end
  244.             sleep(0.5)
  245.         end
  246.     end
  247.  
  248.     tY = tY + 1
  249.     return true -- Turtle moved successfully
  250. end
  251.  
  252. local function orient(targetXdir, targetZdir)
  253.     -- One of the supplied directions should be -1 or +1, the other should be 0.
  254.     if ((targetXdir ~= 0) and (targetZdir ~= 0)) or ((targetXdir==0) and (targetZdir==0)) then
  255.         print("orient() given mutually exclusive values: "..targetXdir..", "..targetZdir)
  256.         return false
  257.     end
  258.    
  259.     if (((targetXdir ~= 0) and (math.abs(targetXdir) ~= 1)) or ((targetZdir ~= 0) and (math.abs(targetZdir) ~= 1))) then
  260.         print("orient() given bad values: "..targetXdir..", "..targetZdir)
  261.         return false
  262.     end
  263.    
  264.     if (targetXdir ~= 0) and (targetXdir ~= xDir) then
  265.         -- x axis alignment requested, and differs from current alignment
  266.         if (xDir ~= 0) then
  267.             -- Turtle is x-axis aligned 180 from target
  268.             turnLeft()
  269.             turnLeft()
  270.         elseif (zDir == targetXdir) then
  271.             turnRight()
  272.         else
  273.             turnLeft()
  274.         end
  275.      elseif (targetZdir ~= 0) and (targetZdir ~= zDir) then
  276.          -- z axis alignment requested, and differs from current alignment
  277.         if (zDir ~= 0) then
  278.             -- Turtle is z-axis aligned 180 from target
  279.             turnLeft()
  280.             turnLeft()
  281.         elseif (xDir == targetZdir) then
  282.             turnLeft()
  283.         else
  284.             turnRight()
  285.         end
  286.     end
  287.    
  288.     return true
  289. end
  290.  
  291.  
  292. local function moveTo(targetX,targetZ,targetY)
  293.  
  294.     local fuelNeeded = math.abs(tX-targetX)+math.abs(tY-targetY)+math.abs(tZ-targetZ)
  295.   if not (turtle.getFuelLevel()>=fuelNeeded) then
  296.     print("Error: Turtle ended up in the unexpected state of not having enough fuel to return home.")
  297.     return false
  298.   end
  299.  
  300.     -- If not at tZ==targetZ, move the right direction until tZ==targetZ
  301.     if (tZ>targetZ) then orient(0,-1) end
  302.     if (tZ<targetZ) then orient(0,1) end
  303.     while (tZ~=targetZ) do
  304.         if bDebug then print("moveTo while tZ~=targetZ tZ="..tZ.." targetZ="..targetZ) end
  305.         if not goForward(false) then
  306.             -- Critical movement fail, bail
  307.             return false
  308.         end
  309.     end
  310.  
  311.   -- If not at tX==targetX, move the right direction until tX==targetX
  312.     if (tX>targetX) then orient(-1,0) end
  313.     if (tX<targetX) then orient(1,0) end
  314.     while (tX~=targetX) do
  315.         if bDebug then print("moveTo while tX~=targetX tX="..tX.." targetX="..targetX) end
  316.         if not goForward(false) then
  317.             -- Critical movement fail, bail
  318.             return false
  319.         end
  320.     end
  321.  
  322.     while (tY<targetY) do
  323.         if bDebug then print("moveTo while tY<targetY tY="..tY.." targetY="..targetY) end
  324.         if not goUp(false) then
  325.             -- Critical movement fail, bail
  326.             return false
  327.         end
  328.     end
  329.  
  330.     while (tY>targetY) do
  331.         if bDebug then print("moveTo while tY>targetY tY="..tY.." targetY="..targetY) end
  332.         if not goDown(false) then
  333.             -- Critical movement fail, bail
  334.             return false
  335.         end
  336.     end
  337.  
  338.   return true
  339. end
  340.  
  341. --[[
  342.  
  343.   START OF THE MAIN PROGRAM
  344.  
  345. --]]
  346.  
  347. checkFreeSlot()
  348.  
  349. local abort = false
  350. local traversal = 1 -- Counts x-z layers we're rasterizing. Used to determine turning directions at end of columns and layers
  351. local lowestY = 1-sizeY
  352. local bDigBelow, bDigAbove = false, false -- initially false
  353. while true do -- This loops digging layers
  354.   print("Main loop traversal="..traversal.." tY="..tY.." lowestY="..lowestY)
  355.  
  356.   if (traversal==1) then --special case since turtle initially starts NOT on a layer that it just dug out.
  357.     if ((tY - lowestY) == 0) then
  358.         bDigBelow, bDigAbove = false, false
  359.     elseif ((tY - lowestY) == 1) then
  360.         bDigBelow, bDigAbove = true, false
  361.     elseif ((tY - lowestY) >= 2) then
  362.         bDigBelow, bDigAbove = true, true
  363.         if not goDown() then
  364.           -- Turtle can't dig down, adjust lowestY because we can't go as deep as planned
  365.           lowestY = tY - 1
  366.         end
  367.     else
  368.         -- Error: turtle is not in an expected place
  369.         print("Error: Turtle vertical position is not what we expect on 1st traversal. Aborting, please debug.")
  370.         abort = true
  371.         break
  372.     end
  373.   else
  374.     -- Not our first traversal, and turtle should now be on the last layer it dug out.
  375.     if ((tY - lowestY) == 1) then
  376.         bDigBelow, bDigAbove = true, false
  377.     elseif ((tY - lowestY) == 2) then
  378.         bDigBelow, bDigAbove = true, false
  379.         if not goDown() then
  380.           -- Turtle can't go down, adjust lowestY because we can't go as deep as planned
  381.           lowestY = tY - 1
  382.         end
  383.     elseif ((tY - lowestY) >= 3) then
  384.         bDigBelow, bDigAbove = true, true
  385.         -- Try to descend 2, if either fails, adjust lowestY to just below where turtle is able to get to, and
  386.         -- cancel the need to digAbove
  387.         for j=1,2 do
  388.             if not goDown() then
  389.               -- Turtle can't dig down, adjust lowestY because we can't go as deep as planned
  390.               lowestY = tY - 1
  391.               bDigAbove = false
  392.             end
  393.         end
  394.     else
  395.         -- Error: turtle is not in an expected place
  396.         print("Error: Turtle vertical position is not what we expect on traversal>1. Aborting, please debug.")
  397.         abort = true
  398.         break
  399.     end
  400.   end
  401.  
  402.  
  403.  
  404.   for column=1,sizeX  do  -- This loops sizeX times digging out columns
  405.     for block=1,(sizeZ-1) do -- this loops (sizeZ-1) times doing digDown and goForward to do all but the end of each column
  406.      
  407.       -- Since we're about to do a potentially ore-digging move, check for free space in inventory.
  408.       -- hasFreeSlot() calls goUnload if necessary
  409.       if not checkFreeSlot() then
  410.         print("Error: checkFreeSlot failure.")
  411.         abort = true
  412.         break
  413.       end
  414.      
  415.       if bDigBelow and turtle.detectDown() then
  416.         if not turtle.digDown() then
  417.           -- Turtle can't dig down, but we're not moving down so this is not a fatal error.
  418.           -- It might be bedrock below turtle, but that's not a concern until turtle is level with it.
  419.         end
  420.       end
  421.      
  422.       if bDigAbove and turtle.detectUp() then
  423.         if not turtle.digUp() then
  424.           -- Turtle can't dig up. This is actually concerning since we don't want to get him trapped under bedrock.
  425.           -- Because of the danger of entrapment, we're ending our quarrying here.
  426.           print("Turtle below undiggable block, backing out and returning home.")
  427.           turnRight()
  428.           turnRight()
  429.           if not goForward() then
  430.             -- This failure we care about, because there is something blocking us that we
  431.             -- can't dig, attack or otherwise resolve. Bust out of our digging loops and attempt to return home
  432.             print("Fatal Error during column goForward tX="..tX.." tZ="..tZ.." tY="..tY)
  433.           end
  434.           abort = true
  435.           break
  436.         end
  437.         sleep(0.5) -- wait to see if anything falls on turtle from above (sand, gravel)
  438.        
  439.         -- First dig up was successful, so we haven't got an undiggable block above, and undiggables can't fall, so we can
  440.         -- safely loop trying to dig up as long as something is falling on us (gravel, sand)
  441.         while turtle.detectUp() do
  442.             if bDebug then print("in while turtrle.detectUp() loop.") end
  443.             if not turtle.digUp() then
  444.                 -- whatever is up there, we couldn't dig it, but it's not bedrock. Just move on...
  445.                 break
  446.             end
  447.             sleep(0.5)
  448.         end
  449.       end
  450.      
  451.       if not goForward() then
  452.         -- This failure we care about, because there is something blocking us that we
  453.         -- can't dig, attack or otherwise resolve. Bust out of our digging loops and attempt to return home
  454.         print("Fatal Error during column goForward tX="..tX.." tZ="..tZ.." tY="..tY)
  455.         abort = true
  456.         break
  457.       end
  458.     end -- end of block loop
  459.    
  460.     -- If movement failed while traversing column, escape out
  461.     if abort then
  462.       -- unwinding
  463.       break
  464.     end
  465.    
  466.     -- Dig out the last block of this column
  467.     if bDigBelow and turtle.detectDown() then
  468.       if not turtle.digDown() then
  469.         -- Turtle can't dig down, but we're not moving down so this is not a fatal error.
  470.         -- It might be bedrock below turtle, but that's not a concern until turtle is level with it.
  471.       end
  472.     end
  473.    
  474.     -- Do last digUp in this column, if required by bDigAbove
  475.     if bDigAbove and turtle.detectUp() then
  476.         if not turtle.digUp() then
  477.           -- Turtle can't dig up. This is actually concerning since we don't want to get him trapped under bedrock.
  478.           -- Because of the danger of entrapment, we're ending our quarrying here.
  479.           print("Turtle below undiggable block, backing out and returning home.")
  480.           turnRight()
  481.           turnRight()
  482.           if not goForward() then
  483.             -- This failure we care about, because there is something blocking us that we
  484.             -- can't dig, attack or otherwise resolve. Bust out of our digging loops and attempt to return home
  485.             print("Fatal Error during column goForward tX="..tX.." tZ="..tZ.." tY="..tY)
  486.           end
  487.           abort = true
  488.           break
  489.         end
  490.         sleep(0.5) -- wait to see if anything falls on turtle from above (sand, gravel)
  491.        
  492.         -- First dig up was successful, so we haven't got an undiggable block above, and undiggables can't fall, so we can
  493.         -- safely loop trying to dig up as long as something is falling on us (gravel, sand)
  494.         while turtle.detectUp() do
  495.             if bDebug then print("in while turtrle.detectUp() loop.") end
  496.             if not turtle.digUp() then
  497.                 -- whatever is up there, we couldn't dig it, but it's not bedrock. Just move on...
  498.                 break
  499.             end
  500.             sleep(0.5)
  501.         end
  502.       end
  503.    
  504.     -- Turtle just finished the column, figure out if we need to advance to a new column, or
  505.     -- if we've finished the layer. If we need to turn to start a new column, we have to figure out which
  506.     -- direction to turn
  507.     if (column<sizeX) then
  508.       -- Turtle is at the end of a z column, but not the end of the whole x-z layer traversal
  509.      
  510.       -- FYI: These odd/even values are based on 1-based block, column, traversal numbers not 0-based tX, tZ, tY
  511.       -- sorry if that's confusing, but Lua convention is to start loop indicies at 1
  512.       local evenCol = ((column%2)==0)
  513.       local evenWidth = ((sizeX%2)==0)
  514.       local evenLayer = ((traversal%2)==0)
  515.       local backtrackingLayer = (evenWidth and evenLayer)
  516.      
  517.       if ((not evenCol and not backtrackingLayer) or (evenCol and backtrackingLayer)) then
  518.         turnRight() -- turn towards next row
  519.         if not goForward() then
  520.           print("Fatal Error during goForward from column "..column.." to column "..(column+1).." tX="..tX.." tZ="..tZ.." tY="..tY)
  521.           abort = true
  522.           break
  523.         end
  524.        
  525.         -- Danger check to see if we've moved under an undiggable block
  526.         if bDigAbove and turtle.detectUp() and not turtle.digUp() then
  527.           print("Turtle below undiggable block, backing out 1 and returning home.")
  528.           turnRight()
  529.           turnRight()
  530.           if not goForward() then
  531.             -- This failure we care about, because there is something blocking us that we
  532.             -- can't dig, attack or otherwise resolve. Bust out of our digging loops and attempt to return home
  533.             print("Fatal Error during column goForward tX="..tX.." tZ="..tZ.." tY="..tY)
  534.           end
  535.           abort = true
  536.           break
  537.         end
  538.        
  539.         turnRight()
  540.       else
  541.         turnLeft()
  542.         if not goForward() then
  543.           print("Fatal Error during goForward from column "..column.." to column "..(column+1).." tX="..tX.." tZ="..tZ.." tY="..tY)
  544.           abort = true
  545.           break
  546.         end
  547.        
  548.         -- Danger check to see if we've moved under an undiggable block
  549.         if bDigAbove and turtle.detectUp() and not turtle.digUp() then
  550.           print("Turtle below undiggable block, backing out 1 and returning home.")
  551.           turnRight()
  552.           turnRight()
  553.           if not goForward() then
  554.             -- This failure we care about, because there is something blocking us that we
  555.             -- can't dig, attack or otherwise resolve. Bust out of our digging loops and attempt to return home
  556.             print("Fatal Error during column goForward tX="..tX.." tZ="..tZ.." tY="..tY)
  557.           end
  558.           abort = true
  559.           break
  560.         end
  561.        
  562.         turnLeft()
  563.       end
  564.       -- Turtle is now ready to start the next column
  565.     else
  566.       -- Turtle is at the end of the layer, rotate 180
  567.       turnRight()
  568.       turnRight()
  569.     end
  570.   end -- end of column loop
  571.  
  572.   if abort then
  573.     print("Abort breaking out of while true loop.")
  574.     -- abort in progress, unwinding out of loops
  575.     break
  576.   end
  577.  
  578.   -- See if we're done yet
  579.   if ((tY - lowestY) == 0) or (((tY - lowestY) == 1) and (bDigBelow == true)) then
  580.     -- We're done. We've finished digging our lowest layer either by digging forward or with digDown.
  581.     done = true
  582.     break
  583.   end
  584.  
  585.   -- If we got past the last if-then, we are not done, so we need to descend in preparation for next layer traversal
  586.   if bDigBelow then
  587.     -- We were digging below us on the traversal we just finished, so we need to drop down 2 levels to be on an undug layer
  588.     -- First we try to descend through the dug-down layer, but since that could have bedrock (we we skimming above it not caring)
  589.     -- we need to test to see if we can descend into that layer at our current tX,tZ location
  590.     if not goDown() then
  591.       print("Turtle finished a traversal and was digging below. Turtle can't go further down, we're done quarrying.")
  592.       abort = true
  593.       break
  594.     end
  595.   end
  596.  
  597.   traversal = traversal + 1
  598. end -- end of while not done loop
  599.  
  600. -- Quarrying ended, either normally or because of encountering undiggable block. Try to return to 0,0,0
  601. if not goHome(0,0,0) then
  602.   -- Can't even get back home :-( Notify the user
  603.   print("Turtle was not able to safely get back to starting location")
  604.   abort = true
  605. else
  606.     orient(0,-1)
  607.  
  608.     -- Drop everything
  609.     -- Drop items. Turtle will not empty the slot designated as the refuel slot.
  610.     orient(0,1)
  611. end
  612.  
  613. if abort then
  614.   print("Quarrying ended due to encounter with undiggable block.")
  615. else
  616.   print("Quarrying complete to desired depth.")
  617. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement