Advertisement
xKevinn

Untitled

Feb 13th, 2013
64
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. function goUnload(bNeedsFuel)
  342.     if bDebug then print("goUnload()") end
  343.     -- Save position and orientation
  344.     local saveX = tX
  345.     local saveZ = tZ
  346.     local saveY = tY
  347.     local saveXdir = xDir
  348.     local saveZdir = zDir
  349.    
  350.     if not goHome() then
  351.     -- Critical failure to move
  352.         return false
  353.     end
  354.    
  355.     orient(0,-1)
  356.    
  357.     orient(0,1)
  358.    
  359.     -- Select first empty slot, might be a now-empty fuel slot, we don't really care
  360.     for i=1,16 do
  361.         if (turtle.getItemCount(i)==0) then
  362.           turtle.select(i)
  363.           break
  364.         end
  365.     end
  366.    
  367.    
  368.     -- Since we had to bring the turtle all the way home, calculate
  369.     -- the fuel needed to get back to where turtle left off mining, do at least one
  370.     -- full layer's worth of work, plus approximately enough to get back home again. It would be
  371.     -- silly to leave base with anything less than that, since the fuel would go nearly all to moving
  372.     -- the turtle through already-mined space doing no work...
  373.     local fuelNeeded = 2 * (math.abs(tX-saveX) + math.abs(tY-saveY) + math.abs(tZ-saveZ)) + (sizeX * sizeZ)
  374.    
  375.     while (turtle.getFuelLevel() < fuelNeeded) do
  376.        
  377.         if bDebug then print("Entering while true do in goUnload fuel check stage") end
  378.        
  379.         -- Scan inventory for fuel
  380.         local slot = 1
  381.         turtle.select(slot)
  382.         local bRefueled = false
  383.        
  384.         while true do
  385.             if turtle.refuel(1) then
  386.                 -- Found fuel in current slot, consume 1, see if it's enough, if not loop again
  387.                 print("Consuming fuel item from slot "..slot)
  388.                 if (turtle.getFuelLevel()>=fuelNeeded) then
  389.                     print("Refueled from inventory, resuming quarrying...")
  390.                     bRefueled = true
  391.                     break
  392.                 end
  393.             else
  394.                 -- Couldn't refuel from currently-selected slot, try next slot. If there are no more slots, ask for player help.
  395.                 if (slot < 16) then
  396.                     slot = slot + 1
  397.                     turtle.select(slot)
  398.                 else
  399.                     -- 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
  400.                     slot = 1
  401.                     break
  402.                 end
  403.             end
  404.         end
  405.        
  406.         if not bRefueled then
  407.             turtle.select(1)
  408.             print("Please add more fuel items to the turtle and press any key. Has:"..turtle.getFuelLevel().." Needs:"..fuelNeeded)
  409.             os.pullEvent("char")    -- suspend code execution awaiting user keypress
  410.         end
  411.     end
  412.    
  413.     if not moveTo(saveX,saveZ,saveY) then
  414.         -- Critical failure to move
  415.         return false
  416.     end
  417.    
  418.     orient(saveXdir,saveZdir)
  419.    
  420.     return true
  421. end
  422.  
  423. local function checkFreeSlot()
  424.   -- This function will return true if the designated refuelSlot is empty, because if there is no fuel reserve there, there
  425.   -- is no reason not to allow item collection into this slot.
  426.     for i=1,16 do
  427.         if turtle.getItemCount(i)==0 then
  428.           return true
  429.         end
  430.     end
  431.  
  432.   -- Turtle does not have empty slot, goUnload
  433.     if not goUnload() then
  434.         return false
  435.     end
  436.  
  437.     return true
  438. end
  439.  
  440. --[[
  441.  
  442.   START OF THE MAIN PROGRAM
  443.  
  444. --]]
  445.  
  446. checkFreeSlot()
  447.  
  448. local abort = false
  449. local traversal = 1 -- Counts x-z layers we're rasterizing. Used to determine turning directions at end of columns and layers
  450. local lowestY = 1-sizeY
  451. local bDigBelow, bDigAbove = false, false -- initially false
  452. while true do -- This loops digging layers
  453.   print("Main loop traversal="..traversal.." tY="..tY.." lowestY="..lowestY)
  454.  
  455.   if (traversal==1) then --special case since turtle initially starts NOT on a layer that it just dug out.
  456.     if ((tY - lowestY) == 0) then
  457.         bDigBelow, bDigAbove = false, false
  458.     elseif ((tY - lowestY) == 1) then
  459.         bDigBelow, bDigAbove = true, false
  460.     elseif ((tY - lowestY) >= 2) then
  461.         bDigBelow, bDigAbove = true, true
  462.         if not goDown() then
  463.           -- Turtle can't dig down, adjust lowestY because we can't go as deep as planned
  464.           lowestY = tY - 1
  465.         end
  466.     else
  467.         -- Error: turtle is not in an expected place
  468.         print("Error: Turtle vertical position is not what we expect on 1st traversal. Aborting, please debug.")
  469.         abort = true
  470.         break
  471.     end
  472.   else
  473.     -- Not our first traversal, and turtle should now be on the last layer it dug out.
  474.     if ((tY - lowestY) == 1) then
  475.         bDigBelow, bDigAbove = true, false
  476.     elseif ((tY - lowestY) == 2) then
  477.         bDigBelow, bDigAbove = true, false
  478.         if not goDown() then
  479.           -- Turtle can't go down, adjust lowestY because we can't go as deep as planned
  480.           lowestY = tY - 1
  481.         end
  482.     elseif ((tY - lowestY) >= 3) then
  483.         bDigBelow, bDigAbove = true, true
  484.         -- Try to descend 2, if either fails, adjust lowestY to just below where turtle is able to get to, and
  485.         -- cancel the need to digAbove
  486.         for j=1,2 do
  487.             if not goDown() then
  488.               -- Turtle can't dig down, adjust lowestY because we can't go as deep as planned
  489.               lowestY = tY - 1
  490.               bDigAbove = false
  491.             end
  492.         end
  493.     else
  494.         -- Error: turtle is not in an expected place
  495.         print("Error: Turtle vertical position is not what we expect on traversal>1. Aborting, please debug.")
  496.         abort = true
  497.         break
  498.     end
  499.   end
  500.  
  501.  
  502.  
  503.   for column=1,sizeX  do  -- This loops sizeX times digging out columns
  504.     for block=1,(sizeZ-1) do -- this loops (sizeZ-1) times doing digDown and goForward to do all but the end of each column
  505.      
  506.       -- Since we're about to do a potentially ore-digging move, check for free space in inventory.
  507.       -- hasFreeSlot() calls goUnload if necessary
  508.       if not checkFreeSlot() then
  509.         print("Error: checkFreeSlot failure.")
  510.         abort = true
  511.         break
  512.       end
  513.      
  514.       if bDigBelow and turtle.detectDown() then
  515.         if not turtle.digDown() then
  516.           -- Turtle can't dig down, but we're not moving down so this is not a fatal error.
  517.           -- It might be bedrock below turtle, but that's not a concern until turtle is level with it.
  518.         end
  519.       end
  520.      
  521.       if bDigAbove and turtle.detectUp() then
  522.         if not turtle.digUp() then
  523.           -- Turtle can't dig up. This is actually concerning since we don't want to get him trapped under bedrock.
  524.           -- Because of the danger of entrapment, we're ending our quarrying here.
  525.           print("Turtle below undiggable block, backing out and returning home.")
  526.           turnRight()
  527.           turnRight()
  528.           if not goForward() then
  529.             -- This failure we care about, because there is something blocking us that we
  530.             -- can't dig, attack or otherwise resolve. Bust out of our digging loops and attempt to return home
  531.             print("Fatal Error during column goForward tX="..tX.." tZ="..tZ.." tY="..tY)
  532.           end
  533.           abort = true
  534.           break
  535.         end
  536.         sleep(0.5) -- wait to see if anything falls on turtle from above (sand, gravel)
  537.        
  538.         -- First dig up was successful, so we haven't got an undiggable block above, and undiggables can't fall, so we can
  539.         -- safely loop trying to dig up as long as something is falling on us (gravel, sand)
  540.         while turtle.detectUp() do
  541.             if bDebug then print("in while turtrle.detectUp() loop.") end
  542.             if not turtle.digUp() then
  543.                 -- whatever is up there, we couldn't dig it, but it's not bedrock. Just move on...
  544.                 break
  545.             end
  546.             sleep(0.5)
  547.         end
  548.       end
  549.      
  550.       if not goForward() then
  551.         -- This failure we care about, because there is something blocking us that we
  552.         -- can't dig, attack or otherwise resolve. Bust out of our digging loops and attempt to return home
  553.         print("Fatal Error during column goForward tX="..tX.." tZ="..tZ.." tY="..tY)
  554.         abort = true
  555.         break
  556.       end
  557.     end -- end of block loop
  558.    
  559.     -- If movement failed while traversing column, escape out
  560.     if abort then
  561.       -- unwinding
  562.       break
  563.     end
  564.    
  565.     -- Dig out the last block of this column
  566.     if bDigBelow and turtle.detectDown() then
  567.       if not turtle.digDown() then
  568.         -- Turtle can't dig down, but we're not moving down so this is not a fatal error.
  569.         -- It might be bedrock below turtle, but that's not a concern until turtle is level with it.
  570.       end
  571.     end
  572.    
  573.     -- Do last digUp in this column, if required by bDigAbove
  574.     if bDigAbove and turtle.detectUp() then
  575.         if not turtle.digUp() then
  576.           -- Turtle can't dig up. This is actually concerning since we don't want to get him trapped under bedrock.
  577.           -- Because of the danger of entrapment, we're ending our quarrying here.
  578.           print("Turtle below undiggable block, backing out and returning home.")
  579.           turnRight()
  580.           turnRight()
  581.           if not goForward() then
  582.             -- This failure we care about, because there is something blocking us that we
  583.             -- can't dig, attack or otherwise resolve. Bust out of our digging loops and attempt to return home
  584.             print("Fatal Error during column goForward tX="..tX.." tZ="..tZ.." tY="..tY)
  585.           end
  586.           abort = true
  587.           break
  588.         end
  589.         sleep(0.5) -- wait to see if anything falls on turtle from above (sand, gravel)
  590.        
  591.         -- First dig up was successful, so we haven't got an undiggable block above, and undiggables can't fall, so we can
  592.         -- safely loop trying to dig up as long as something is falling on us (gravel, sand)
  593.         while turtle.detectUp() do
  594.             if bDebug then print("in while turtrle.detectUp() loop.") end
  595.             if not turtle.digUp() then
  596.                 -- whatever is up there, we couldn't dig it, but it's not bedrock. Just move on...
  597.                 break
  598.             end
  599.             sleep(0.5)
  600.         end
  601.       end
  602.    
  603.     -- Turtle just finished the column, figure out if we need to advance to a new column, or
  604.     -- if we've finished the layer. If we need to turn to start a new column, we have to figure out which
  605.     -- direction to turn
  606.     if (column<sizeX) then
  607.       -- Turtle is at the end of a z column, but not the end of the whole x-z layer traversal
  608.      
  609.       -- FYI: These odd/even values are based on 1-based block, column, traversal numbers not 0-based tX, tZ, tY
  610.       -- sorry if that's confusing, but Lua convention is to start loop indicies at 1
  611.       local evenCol = ((column%2)==0)
  612.       local evenWidth = ((sizeX%2)==0)
  613.       local evenLayer = ((traversal%2)==0)
  614.       local backtrackingLayer = (evenWidth and evenLayer)
  615.      
  616.       if ((not evenCol and not backtrackingLayer) or (evenCol and backtrackingLayer)) then
  617.         turnRight() -- turn towards next row
  618.         if not goForward() then
  619.           print("Fatal Error during goForward from column "..column.." to column "..(column+1).." tX="..tX.." tZ="..tZ.." tY="..tY)
  620.           abort = true
  621.           break
  622.         end
  623.        
  624.         -- Danger check to see if we've moved under an undiggable block
  625.         if bDigAbove and turtle.detectUp() and not turtle.digUp() then
  626.           print("Turtle below undiggable block, backing out 1 and returning home.")
  627.           turnRight()
  628.           turnRight()
  629.           if not goForward() then
  630.             -- This failure we care about, because there is something blocking us that we
  631.             -- can't dig, attack or otherwise resolve. Bust out of our digging loops and attempt to return home
  632.             print("Fatal Error during column goForward tX="..tX.." tZ="..tZ.." tY="..tY)
  633.           end
  634.           abort = true
  635.           break
  636.         end
  637.        
  638.         turnRight()
  639.       else
  640.         turnLeft()
  641.         if not goForward() then
  642.           print("Fatal Error during goForward from column "..column.." to column "..(column+1).." tX="..tX.." tZ="..tZ.." tY="..tY)
  643.           abort = true
  644.           break
  645.         end
  646.        
  647.         -- Danger check to see if we've moved under an undiggable block
  648.         if bDigAbove and turtle.detectUp() and not turtle.digUp() then
  649.           print("Turtle below undiggable block, backing out 1 and returning home.")
  650.           turnRight()
  651.           turnRight()
  652.           if not goForward() then
  653.             -- This failure we care about, because there is something blocking us that we
  654.             -- can't dig, attack or otherwise resolve. Bust out of our digging loops and attempt to return home
  655.             print("Fatal Error during column goForward tX="..tX.." tZ="..tZ.." tY="..tY)
  656.           end
  657.           abort = true
  658.           break
  659.         end
  660.        
  661.         turnLeft()
  662.       end
  663.       -- Turtle is now ready to start the next column
  664.     else
  665.       -- Turtle is at the end of the layer, rotate 180
  666.       turnRight()
  667.       turnRight()
  668.     end
  669.   end -- end of column loop
  670.  
  671.   if abort then
  672.     print("Abort breaking out of while true loop.")
  673.     -- abort in progress, unwinding out of loops
  674.     break
  675.   end
  676.  
  677.   -- See if we're done yet
  678.   if ((tY - lowestY) == 0) or (((tY - lowestY) == 1) and (bDigBelow == true)) then
  679.     -- We're done. We've finished digging our lowest layer either by digging forward or with digDown.
  680.     done = true
  681.     break
  682.   end
  683.  
  684.   -- If we got past the last if-then, we are not done, so we need to descend in preparation for next layer traversal
  685.   if bDigBelow then
  686.     -- 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
  687.     -- First we try to descend through the dug-down layer, but since that could have bedrock (we we skimming above it not caring)
  688.     -- we need to test to see if we can descend into that layer at our current tX,tZ location
  689.     if not goDown() then
  690.       print("Turtle finished a traversal and was digging below. Turtle can't go further down, we're done quarrying.")
  691.       abort = true
  692.       break
  693.     end
  694.   end
  695.  
  696.   traversal = traversal + 1
  697. end -- end of while not done loop
  698.  
  699. -- Quarrying ended, either normally or because of encountering undiggable block. Try to return to 0,0,0
  700. if not goHome(0,0,0) then
  701.   -- Can't even get back home :-( Notify the user
  702.   print("Turtle was not able to safely get back to starting location")
  703.   abort = true
  704. else
  705.     orient(0,-1)
  706.  
  707.     -- Drop everything
  708.     -- Drop items. Turtle will not empty the slot designated as the refuel slot.
  709.     orient(0,1)
  710. end
  711.  
  712. if abort then
  713.   print("Quarrying ended due to encounter with undiggable block.")
  714. else
  715.   print("Quarrying complete to desired depth.")
  716. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement