Advertisement
xKevinn

Untitled

Feb 13th, 2013
49
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. local abort = false
  348. local traversal = 1 -- Counts x-z layers we're rasterizing. Used to determine turning directions at end of columns and layers
  349. local lowestY = 1-sizeY
  350. local bDigBelow, bDigAbove = false, false -- initially false
  351. while true do -- This loops digging layers
  352.   print("Main loop traversal="..traversal.." tY="..tY.." lowestY="..lowestY)
  353.  
  354.   if (traversal==1) then --special case since turtle initially starts NOT on a layer that it just dug out.
  355.     if ((tY - lowestY) == 0) then
  356.         bDigBelow, bDigAbove = false, false
  357.     elseif ((tY - lowestY) == 1) then
  358.         bDigBelow, bDigAbove = true, false
  359.     elseif ((tY - lowestY) >= 2) then
  360.         bDigBelow, bDigAbove = true, true
  361.         if not goDown() then
  362.           -- Turtle can't dig down, adjust lowestY because we can't go as deep as planned
  363.           lowestY = tY - 1
  364.         end
  365.     else
  366.         -- Error: turtle is not in an expected place
  367.         print("Error: Turtle vertical position is not what we expect on 1st traversal. Aborting, please debug.")
  368.         abort = true
  369.         break
  370.     end
  371.   else
  372.     -- Not our first traversal, and turtle should now be on the last layer it dug out.
  373.     if ((tY - lowestY) == 1) then
  374.         bDigBelow, bDigAbove = true, false
  375.     elseif ((tY - lowestY) == 2) then
  376.         bDigBelow, bDigAbove = true, false
  377.         if not goDown() then
  378.           -- Turtle can't go down, adjust lowestY because we can't go as deep as planned
  379.           lowestY = tY - 1
  380.         end
  381.     elseif ((tY - lowestY) >= 3) then
  382.         bDigBelow, bDigAbove = true, true
  383.         -- Try to descend 2, if either fails, adjust lowestY to just below where turtle is able to get to, and
  384.         -- cancel the need to digAbove
  385.         for j=1,2 do
  386.             if not goDown() then
  387.               -- Turtle can't dig down, adjust lowestY because we can't go as deep as planned
  388.               lowestY = tY - 1
  389.               bDigAbove = false
  390.             end
  391.         end
  392.     else
  393.         -- Error: turtle is not in an expected place
  394.         print("Error: Turtle vertical position is not what we expect on traversal>1. Aborting, please debug.")
  395.         abort = true
  396.         break
  397.     end
  398.   end
  399.  
  400.  
  401.  
  402.   for column=1,sizeX  do  -- This loops sizeX times digging out columns
  403.     for block=1,(sizeZ-1) do -- this loops (sizeZ-1) times doing digDown and goForward to do all but the end of each column
  404.      
  405.       -- Since we're about to do a potentially ore-digging move, check for free space in inventory.
  406.       -- hasFreeSlot() calls goUnload if necessary
  407.      
  408.       if bDigBelow and turtle.detectDown() then
  409.         if not turtle.digDown() then
  410.           -- Turtle can't dig down, but we're not moving down so this is not a fatal error.
  411.           -- It might be bedrock below turtle, but that's not a concern until turtle is level with it.
  412.         end
  413.       end
  414.      
  415.       if bDigAbove and turtle.detectUp() then
  416.         if not turtle.digUp() then
  417.           -- Turtle can't dig up. This is actually concerning since we don't want to get him trapped under bedrock.
  418.           -- Because of the danger of entrapment, we're ending our quarrying here.
  419.           print("Turtle below undiggable block, backing out and returning home.")
  420.           turnRight()
  421.           turnRight()
  422.           if not goForward() then
  423.             -- This failure we care about, because there is something blocking us that we
  424.             -- can't dig, attack or otherwise resolve. Bust out of our digging loops and attempt to return home
  425.             print("Fatal Error during column goForward tX="..tX.." tZ="..tZ.." tY="..tY)
  426.           end
  427.           abort = true
  428.           break
  429.         end
  430.         sleep(0.5) -- wait to see if anything falls on turtle from above (sand, gravel)
  431.        
  432.         -- First dig up was successful, so we haven't got an undiggable block above, and undiggables can't fall, so we can
  433.         -- safely loop trying to dig up as long as something is falling on us (gravel, sand)
  434.         while turtle.detectUp() do
  435.             if bDebug then print("in while turtrle.detectUp() loop.") end
  436.             if not turtle.digUp() then
  437.                 -- whatever is up there, we couldn't dig it, but it's not bedrock. Just move on...
  438.                 break
  439.             end
  440.             sleep(0.5)
  441.         end
  442.       end
  443.      
  444.       if not goForward() then
  445.         -- This failure we care about, because there is something blocking us that we
  446.         -- can't dig, attack or otherwise resolve. Bust out of our digging loops and attempt to return home
  447.         print("Fatal Error during column goForward tX="..tX.." tZ="..tZ.." tY="..tY)
  448.         abort = true
  449.         break
  450.       end
  451.     end -- end of block loop
  452.    
  453.     -- If movement failed while traversing column, escape out
  454.     if abort then
  455.       -- unwinding
  456.       break
  457.     end
  458.    
  459.     -- Dig out the last block of this column
  460.     if bDigBelow and turtle.detectDown() then
  461.       if not turtle.digDown() then
  462.         -- Turtle can't dig down, but we're not moving down so this is not a fatal error.
  463.         -- It might be bedrock below turtle, but that's not a concern until turtle is level with it.
  464.       end
  465.     end
  466.    
  467.     -- Do last digUp in this column, if required by bDigAbove
  468.     if bDigAbove and turtle.detectUp() then
  469.         if not turtle.digUp() then
  470.           -- Turtle can't dig up. This is actually concerning since we don't want to get him trapped under bedrock.
  471.           -- Because of the danger of entrapment, we're ending our quarrying here.
  472.           print("Turtle below undiggable block, backing out and returning home.")
  473.           turnRight()
  474.           turnRight()
  475.           if not goForward() then
  476.             -- This failure we care about, because there is something blocking us that we
  477.             -- can't dig, attack or otherwise resolve. Bust out of our digging loops and attempt to return home
  478.             print("Fatal Error during column goForward tX="..tX.." tZ="..tZ.." tY="..tY)
  479.           end
  480.           abort = true
  481.           break
  482.         end
  483.         sleep(0.5) -- wait to see if anything falls on turtle from above (sand, gravel)
  484.        
  485.         -- First dig up was successful, so we haven't got an undiggable block above, and undiggables can't fall, so we can
  486.         -- safely loop trying to dig up as long as something is falling on us (gravel, sand)
  487.         while turtle.detectUp() do
  488.             if bDebug then print("in while turtrle.detectUp() loop.") end
  489.             if not turtle.digUp() then
  490.                 -- whatever is up there, we couldn't dig it, but it's not bedrock. Just move on...
  491.                 break
  492.             end
  493.             sleep(0.5)
  494.         end
  495.       end
  496.    
  497.     -- Turtle just finished the column, figure out if we need to advance to a new column, or
  498.     -- if we've finished the layer. If we need to turn to start a new column, we have to figure out which
  499.     -- direction to turn
  500.     if (column<sizeX) then
  501.       -- Turtle is at the end of a z column, but not the end of the whole x-z layer traversal
  502.      
  503.       -- FYI: These odd/even values are based on 1-based block, column, traversal numbers not 0-based tX, tZ, tY
  504.       -- sorry if that's confusing, but Lua convention is to start loop indicies at 1
  505.       local evenCol = ((column%2)==0)
  506.       local evenWidth = ((sizeX%2)==0)
  507.       local evenLayer = ((traversal%2)==0)
  508.       local backtrackingLayer = (evenWidth and evenLayer)
  509.      
  510.       if ((not evenCol and not backtrackingLayer) or (evenCol and backtrackingLayer)) then
  511.         turnRight() -- turn towards next row
  512.         if not goForward() then
  513.           print("Fatal Error during goForward from column "..column.." to column "..(column+1).." tX="..tX.." tZ="..tZ.." tY="..tY)
  514.           abort = true
  515.           break
  516.         end
  517.        
  518.         -- Danger check to see if we've moved under an undiggable block
  519.         if bDigAbove and turtle.detectUp() and not turtle.digUp() then
  520.           print("Turtle below undiggable block, backing out 1 and returning home.")
  521.           turnRight()
  522.           turnRight()
  523.           if not goForward() then
  524.             -- This failure we care about, because there is something blocking us that we
  525.             -- can't dig, attack or otherwise resolve. Bust out of our digging loops and attempt to return home
  526.             print("Fatal Error during column goForward tX="..tX.." tZ="..tZ.." tY="..tY)
  527.           end
  528.           abort = true
  529.           break
  530.         end
  531.        
  532.         turnRight()
  533.       else
  534.         turnLeft()
  535.         if not goForward() then
  536.           print("Fatal Error during goForward from column "..column.." to column "..(column+1).." tX="..tX.." tZ="..tZ.." tY="..tY)
  537.           abort = true
  538.           break
  539.         end
  540.        
  541.         -- Danger check to see if we've moved under an undiggable block
  542.         if bDigAbove and turtle.detectUp() and not turtle.digUp() then
  543.           print("Turtle below undiggable block, backing out 1 and returning home.")
  544.           turnRight()
  545.           turnRight()
  546.           if not goForward() then
  547.             -- This failure we care about, because there is something blocking us that we
  548.             -- can't dig, attack or otherwise resolve. Bust out of our digging loops and attempt to return home
  549.             print("Fatal Error during column goForward tX="..tX.." tZ="..tZ.." tY="..tY)
  550.           end
  551.           abort = true
  552.           break
  553.         end
  554.        
  555.         turnLeft()
  556.       end
  557.       -- Turtle is now ready to start the next column
  558.     else
  559.       -- Turtle is at the end of the layer, rotate 180
  560.       turnRight()
  561.       turnRight()
  562.     end
  563.   end -- end of column loop
  564.  
  565.   if abort then
  566.     print("Abort breaking out of while true loop.")
  567.     -- abort in progress, unwinding out of loops
  568.     break
  569.   end
  570.  
  571.   -- See if we're done yet
  572.   if ((tY - lowestY) == 0) or (((tY - lowestY) == 1) and (bDigBelow == true)) then
  573.     -- We're done. We've finished digging our lowest layer either by digging forward or with digDown.
  574.     done = true
  575.     break
  576.   end
  577.  
  578.   -- If we got past the last if-then, we are not done, so we need to descend in preparation for next layer traversal
  579.   if bDigBelow then
  580.     -- 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
  581.     -- First we try to descend through the dug-down layer, but since that could have bedrock (we we skimming above it not caring)
  582.     -- we need to test to see if we can descend into that layer at our current tX,tZ location
  583.     if not goDown() then
  584.       print("Turtle finished a traversal and was digging below. Turtle can't go further down, we're done quarrying.")
  585.       abort = true
  586.       break
  587.     end
  588.   end
  589.  
  590.   traversal = traversal + 1
  591. end -- end of while not done loop
  592.  
  593. -- Quarrying ended, either normally or because of encountering undiggable block. Try to return to 0,0,0
  594. if not goHome(0,0,0) then
  595.   -- Can't even get back home :-( Notify the user
  596.   print("Turtle was not able to safely get back to starting location")
  597.   abort = true
  598. else
  599.     orient(0,-1)
  600.  
  601.     -- Drop everything
  602.     -- Drop items. Turtle will not empty the slot designated as the refuel slot.
  603.     orient(0,1)
  604. end
  605.  
  606. if abort then
  607.   print("Quarrying ended due to encounter with undiggable block.")
  608. else
  609.   print("Quarrying complete to desired depth.")
  610. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement