Advertisement
Guest User

Untitled

a guest
Nov 25th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 35.03 KB | None | 0 0
  1. --[[
  2.  
  3.   Rectangular Quarry Program 1.2b
  4.   by Adam Smith "shiphorns"
  5.   March 7 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.   1.2b -    Fix for turtle getting stuck if turtle simultaneous encounters bedrock in front and above it.
  13.  
  14. --]]
  15. local tArgs = { ... }
  16. local sizeZ -- Quarry is this long in direction turtle is initially facing, including block turtle is on
  17. local sizeX -- Quarry is this wide to the right of where turtle is facing, including block turtle is on
  18. local sizeY -- Quarry removes this many layers of blocks including layer where turtle starts
  19. local bDebug= false
  20.  
  21. local goUnload  -- Forward declaration
  22.  
  23. if (#tArgs == 1) then
  24.         sizeZ,sizeX,sizeY = tonumber(tArgs[1]),tonumber(tArgs[1]),256
  25. elseif (#tArgs == 2) then
  26.         sizeZ,sizeX,sizeY = tonumber(tArgs[1]),tonumber(tArgs[2]),256
  27. elseif (#tArgs >= 3) then
  28.         sizeZ,sizeX,sizeY = tonumber(tArgs[1]),tonumber(tArgs[2]),tonumber(tArgs[3])
  29.         if (#tArgs > 3) then
  30.                 bDebug = (tonumber(tArgs[4])==1)
  31.         end
  32. else
  33.         print( "Usage: quarry <sq. size> <optional width > <optional fixed depth> <optional 1 for debug mode>" )
  34.         return
  35. end
  36.  
  37. -- Validate dimensions
  38. if (sizeX<2 or sizeZ<2 or sizeY<1) then
  39.   print( "Dimensions given must be at least 2L x 2W x 1D. Efficiency is optimal if fixed depth is a multiple of 3." )
  40.   return
  41. end
  42.  
  43. local minFuel = math.ceil((math.ceil(sizeY/3)*(sizeX*sizeY)+(2*sizeY))/1200)
  44. local maxFuel = "TBD"
  45.  
  46. print("Place fuel reserves in slot 1 (upper left) if desired and hit any key to start.")
  47. os.pullEvent("key")
  48.  
  49. local tX,tZ,tY = 0,0,0    -- Place the turtle starts is considered block 0,0,0 in the turtle's local coordinate system
  50. local xDir,zDir = 0,1     -- Turtle is considered as initially facing positive z direction, regardless of global world facing direction
  51. 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
  52.  
  53. -- 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
  54. -- 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
  55. -- orientation in the global world system (turtle's system is relative to how he is initially facing, +Z is his facing direction)
  56.  
  57. local function checkFuel(bMovingAwayFromOrigin)
  58.         if bDebug then print("checkFuel()") end
  59.         -- This function returns true only if there is enough fuel left to move 1 block in any direction,
  60.         -- and still have enough left over for a return trip to 0,0,0 that might be needed for refuel or at
  61.         -- the end of the quarrying. This ensures the turtle is never stranded in the quarry.
  62.         local fuelLevel = turtle.getFuelLevel()
  63.        
  64.         if (fuelLevel == "unlimited") then
  65.                 -- Server has fuel requirement turned off in configs
  66.                 return true
  67.         end
  68.        
  69.         -- If the turtle is attempting to move away from its starting location, it is going to
  70.         -- consume the normal 1 fuel cost to move, but it will also add +1 to the cost of the
  71.         -- trip to return home to dump/refuel/finish. If we know the turtle is moving closer to
  72.         -- home, there is no extra cost since it is effectively part of the return trip.
  73.         local fuelNeeded = math.abs(tX)+math.abs(tY)+math.abs(tZ)
  74.         if (bMovingAwayFromOrigin == nil or bMovingAwayFromOrigin == true) then
  75.                 -- Turtle is moving away from 0,0,0 or direction is unspecified (assume worst case), add 2 fuel
  76.                 fuelNeeded = fuelNeeded + 2
  77.         end
  78.  
  79.         if (fuelLevel >= fuelNeeded) then
  80.                 -- Turtle has enough fuel to do the next 1-block movement, plus enough to
  81.                 -- return home from there.
  82.                 return true
  83.         end
  84.  
  85.         -- If we get here, turtle does not have enough fuel for the move plus a return to base
  86.         -- First we will try to refuel from anything we find in the turtle's inventory. Failing that
  87.         -- We will return to home and prompt the user to add fuel
  88.        
  89.         local slot = 1
  90.         turtle.select(slot)
  91.        
  92.         if bDebug then print("Entering while true do in checkFuel") end
  93.         while true do
  94.                 if turtle.refuel(1) then
  95.                         -- Found fuel in current slot, consume 1, see if it's enough, if not loop again
  96.                         if (turtle.getFuelLevel()>=fuelNeeded) then
  97.                                 print("Refueled from inventory, resuming quarrying...")
  98.                                 return true
  99.                         end
  100.                 else
  101.                         -- Couldn't refuel from currently-selected slot, try next slot. If there are no more slots, ask for player help.
  102.                         if (slot < 16) then
  103.                                 slot = slot + 1
  104.                                 turtle.select(slot)
  105.                         else
  106.                                 -- 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
  107.                                 -- player sees slot 1 highlighted (fastest for turtle to find fuel in). Return to 0,0,0 if we can (in case turtle is
  108.                                 -- under lava or otherwise inaccessible), prompt player to add fuel.
  109.  
  110.                                 return goUnload(true)
  111.                         end
  112.                 end
  113.         end
  114. end
  115.  
  116. local function turnLeft()
  117.   turtle.turnLeft()
  118.   xDir,zDir = -zDir,xDir
  119.   return true
  120. end
  121.  
  122. local function turnRight()
  123.   turtle.turnRight()
  124.   xDir,zDir = zDir,-xDir
  125.   return true
  126. end
  127.  
  128. local function goForward(bCheckFuel)
  129.         if bDebug then print("goForward()") end
  130.         -- Can't move without fuel. checkFuel() will wait on player if necessary.
  131.         if (bCheckFuel==true or bCheckFuel==nil) then
  132.     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
  133.   end
  134.  
  135.         local tries = 3
  136.         while not turtle.forward() do
  137.                 if bDebug then print("goForward: while not turtle.forward() do tries="..tries) end
  138.                 if turtle.detect() then
  139.                         if bDebug then print("goForward: detect") end
  140.                         if not turtle.dig() then
  141.                                 print("Undiggable block encountered. Will retry in 5 seconds.")
  142.                                 -- Turtle is blocked. In case this is a temporary glitch, we try 3 times before conceding hard failure
  143.                                 tries = tries - 1
  144.                                 if (tries <= 0) then
  145.                                   return false
  146.                                 else
  147.                                         if bDebug then print("goForward: sleep(5)") end
  148.                                         sleep(5) -- Wait 5 seconds, hope the problem resolves itself
  149.                                 end
  150.                         end
  151.                 elseif turtle.attack() then
  152.                         if bDebug then print("goForward: attack") end
  153.                         -- Had to attack player or mob. You can add additional code here such
  154.                         -- as turtle.suck() if you want to collect killed mob loot. This is a quarry program
  155.                         -- to collect ores, not rotten flesh and bones, so this block is empty.
  156.                 else
  157.                         -- Unknown obstruction, possibly a player in
  158.                         -- peaceful or creative mode. Try again in 0.5 seconds and hope it's gone.
  159.                         if bDebug then
  160.                                 print("goForward: sleep(0.5) else block")
  161.                                 print("Turtle fuel="..turtle.getFuelLevel())
  162.                         end
  163.                         sleep(0.5)
  164.                 end
  165.         end
  166.  
  167.         tX = tX + xDir  -- If we're moving in the xDir, this will change tX by + or - 1
  168.         tZ = tZ + zDir  -- If we're moving in the zDir, this will change tZ by + or - 1
  169.  
  170.         return true -- Turtle moved successfully
  171. end
  172.  
  173. local function goDown(bCheckFuel)
  174.         if bDebug then print("goDown()") end
  175.         -- Can't move without fuel. checkFuel() will wait on player if necessary.
  176.         if (bCheckFuel==true or bCheckFuel==nil) then
  177.     checkFuel(tY<=0)    -- Passes boolean true if moving away from 0,0,0
  178.   end
  179.  
  180.   local tries = 3
  181.         while not turtle.down() do
  182.                 if bDebug then print("goDown: while not turtle.down() do tries="..tries) end
  183.                 if turtle.detectDown() then
  184.                         if bDebug then print("goDown: detectDown") end
  185.                         if not turtle.digDown() then
  186.                                 print("Undiggable block encountered. Will retry in 5 seconds")
  187.                                 -- Turtle is blocked. In case this is a temporary glitch, we try 3 times before conceding hard failure
  188.                                 tries = tries - 1
  189.                                 if (tries <= 0) then
  190.                                   return false
  191.                                 else
  192.                                         if bDebug then print("goDown: sleep(5)") end
  193.                                   sleep(5) -- Wait 5 seconds, hope the problem resolves itself
  194.                                 end
  195.                         end
  196.                 elseif turtle.attackDown() then
  197.                         if bDebug then print("goDown: attack") end
  198.                         -- Had to attack player or mob. You can add additional code here such
  199.                         -- as turtle.suck() if you want to collect killed mob loot. This is a quarry program
  200.                         -- to collect ores, not rotten flesh and bones, so this block is empty.
  201.                 else
  202.                         -- Unknown obstruction, possibly a player in
  203.                         -- peaceful or creative mode. Try again in 0.5 seconds and hope it's gone.
  204.                         if bDebug then print("goDown: sleep(0.5)") end
  205.                         sleep(0.5)
  206.                 end
  207.         end
  208.  
  209.         tY = tY - 1
  210.         return true -- Turtle moved successfully
  211. end
  212.  
  213. local function goUp(bCheckFuel)
  214.         if bDebug then print("goUp()") end
  215.        
  216.   -- Can't move without fuel. checkFuel() will wait on player if necessary.
  217.   if (bCheckFuel==true or bCheckFuel==nil) then
  218.     checkFuel(tY>=0)    -- Passes boolean true if moving away from 0,0,0
  219.   end
  220.  
  221.   local tries = 3
  222.         while not turtle.up() do
  223.                 if bDebug then print("goUp: while not loop tries="..tries) end
  224.                 if turtle.detectUp() then
  225.                         if bDebug then print("goUp: detectUp") end
  226.                           if not turtle.digUp() then
  227.                                                 print("Undiggable block encountered. Will retry in 5 seconds.")
  228.                                                 -- Turtle is blocked. In case this is a temporary glitch, we try 3 times before conceding hard failure
  229.                                 tries = tries - 1
  230.                                 if (tries <= 0) then
  231.                                   return false
  232.                                 else
  233.                                   sleep(10) -- Wait 10 seconds, hope the problem resolves itself
  234.                                 end
  235.                         end
  236.                 elseif turtle.attackUp() then
  237.                         if bDebug then print("goUp: attack") end
  238.                         -- Had to attack player or mob. You can add additional code here such
  239.                         -- as turtle.suck() if you want to collect killed mob loot. This is a quarry program
  240.                         -- to collect ores, not rotten flesh and bones, so this block is empty.
  241.                 else
  242.                         -- Unknown obstruction, possibly a player in
  243.                         -- peaceful or creative mode. Try again in 0.5 seconds and hope it's gone.
  244.                         if bDebug then print("goUp: sleep(0.5)") end
  245.                         sleep(0.5)
  246.                 end
  247.         end
  248.  
  249.         tY = tY + 1
  250.         return true -- Turtle moved successfully
  251. end
  252.  
  253. local function orient(targetXdir, targetZdir)
  254.         -- One of the supplied directions should be -1 or +1, the other should be 0.
  255.     if ((targetXdir ~= 0) and (targetZdir ~= 0)) or ((targetXdir==0) and (targetZdir==0)) then
  256.         print("orient() given mutually exclusive values: "..targetXdir..", "..targetZdir)
  257.         return false
  258.     end
  259.    
  260.     if (((targetXdir ~= 0) and (math.abs(targetXdir) ~= 1)) or ((targetZdir ~= 0) and (math.abs(targetZdir) ~= 1))) then
  261.         print("orient() given bad values: "..targetXdir..", "..targetZdir)
  262.         return false
  263.     end
  264.    
  265.     if (targetXdir ~= 0) and (targetXdir ~= xDir) then
  266.         -- x axis alignment requested, and differs from current alignment
  267.         if (xDir ~= 0) then
  268.             -- Turtle is x-axis aligned 180 from target
  269.             turnLeft()
  270.             turnLeft()
  271.         elseif (zDir == targetXdir) then
  272.             turnRight()
  273.         else
  274.             turnLeft()
  275.         end
  276.      elseif (targetZdir ~= 0) and (targetZdir ~= zDir) then
  277.          -- z axis alignment requested, and differs from current alignment
  278.         if (zDir ~= 0) then
  279.             -- Turtle is z-axis aligned 180 from target
  280.             turnLeft()
  281.             turnLeft()
  282.         elseif (xDir == targetZdir) then
  283.             turnLeft()
  284.         else
  285.             turnRight()
  286.         end
  287.     end
  288.    
  289.     return true
  290. end
  291.  
  292. local function goHome()
  293.   -- This is similar to moveTo(0,0,0) but axis ordering of movement is reversed, so that turtle takes
  294.   -- the same path to and from home location and where it left off. Also, this function passes false to
  295.   -- goDown, goUp and goForward to make them skip the per-move fuel check, because making that check
  296.   -- could result in circular function calling: goHome()->goFoward()->checkFuel()->goHome()->goFoward()->checkFuel().. etc.
  297.   -- This function is set up to move along Y-axis first, then X, then finally Z, unless bReverse is true
  298.   -- Note: The order doesn't matter much when digging out a space, but can matter when building something
  299.   -- so that you don't dig a tunnel through what you're building.
  300.  
  301.  
  302.         while (tY<0) do
  303.                 if bDebug then print("goHome while tY<0 tY="..tY) end
  304.                 if not goUp(false) then
  305.                         -- Critical movement fail, bail
  306.                         return false
  307.                 end
  308.         end
  309.  
  310.         while (tY>0) do
  311.                 if bDebug then print("goHome while tY>0 tY="..tY) end
  312.                 if not goDown(false) then
  313.                         -- Critical movement fail, bail
  314.                         return false
  315.                 end
  316.         end
  317.  
  318.         -- If not at tX==targetX, move the right direction until tX==targetX
  319.         if (tX>0) then orient(-1,0) end
  320.         if (tX<0) then orient(1,0) end
  321.         while (tX~=0) do
  322.                 if bDebug then print("goHome while tX~=0 tX="..tX) end
  323.                 if not goForward(false) then
  324.                 -- Critical movement fail, bail
  325.                 return false
  326.                 end
  327.         end
  328.  
  329.         -- If not at tZ==targetZ, move the right direction until tZ==targetZ
  330.         if (tZ>0) then orient(0,-1) end
  331.         if (tZ<0) then orient(0,1) end
  332.         while (tZ~=0) do
  333.                 if bDebug then print("goHome while tZ~=0 tZ="..tZ) end
  334.                 if not goForward(false) then
  335.                         -- Critical movement fail, bail
  336.                         return false
  337.                 end
  338.         end
  339.  
  340.         return true
  341. end
  342.  
  343. local function moveTo(targetX,targetZ,targetY)
  344.  
  345.  
  346.         -- If not at tZ==targetZ, move the right direction until tZ==targetZ
  347.         if (tZ>targetZ) then orient(0,-1) end
  348.         if (tZ<targetZ) then orient(0,1) end
  349.         while (tZ~=targetZ) do
  350.                 if bDebug then print("moveTo while tZ~=targetZ tZ="..tZ.." targetZ="..targetZ) end
  351.                 if not goForward(false) then
  352.                         -- Critical movement fail, bail
  353.                         return false
  354.                 end
  355.         end
  356.  
  357.   -- If not at tX==targetX, move the right direction until tX==targetX
  358.         if (tX>targetX) then orient(-1,0) end
  359.         if (tX<targetX) then orient(1,0) end
  360.         while (tX~=targetX) do
  361.                 if bDebug then print("moveTo while tX~=targetX tX="..tX.." targetX="..targetX) end
  362.                 if not goForward(false) then
  363.                         -- Critical movement fail, bail
  364.                         return false
  365.                 end
  366.         end
  367.  
  368.         while (tY<targetY) do
  369.                 if bDebug then print("moveTo while tY<targetY tY="..tY.." targetY="..targetY) end
  370.                 if not goUp(false) then
  371.                         -- Critical movement fail, bail
  372.                         return false
  373.                 end
  374.         end
  375.  
  376.         while (tY>targetY) do
  377.                 if bDebug then print("moveTo while tY>targetY tY="..tY.." targetY="..targetY) end
  378.                 if not goDown(false) then
  379.                         -- Critical movement fail, bail
  380.                         return false
  381.                 end
  382.         end
  383.  
  384.   return true
  385. end
  386.  
  387. function goUnload(bNeedsFuel)
  388.         if bDebug then print("goUnload()") end
  389.         -- Save position and orientation
  390.         local saveX = tX
  391.         local saveZ = tZ
  392.         local saveY = tY
  393.         local saveXdir = xDir
  394.         local saveZdir = zDir
  395.        
  396.         if not goHome() then
  397.         -- Critical failure to move
  398.                 return false
  399.         end
  400.        
  401.         orient(0,-1)
  402.        
  403.         -- Drop items. Turtle will not empty the slot designated as the refuel slot.
  404.         for i=1,16 do
  405.                 if (i ~= refuelSlot) then
  406.                   turtle.select(i)
  407.                   turtle.drop()
  408.                 end
  409.         end
  410.        
  411.         orient(0,1)
  412.        
  413.         -- Select first empty slot, might be a now-empty fuel slot, we don't really care
  414.         for i=1,16 do
  415.                 if (turtle.getItemCount(i)==0) then
  416.                   turtle.select(i)
  417.                   break
  418.                 end
  419.         end
  420.        
  421.        
  422.         -- Since we had to bring the turtle all the way home, calculate
  423.         -- the fuel needed to get back to where turtle left off mining, do at least one
  424.         -- full layer's worth of work, plus approximately enough to get back home again. It would be
  425.         -- silly to leave base with anything less than that, since the fuel would go nearly all to moving
  426.         -- the turtle through already-mined space doing no work...
  427.         local fuelNeeded = 2 * (math.abs(tX-saveX) + math.abs(tY-saveY) + math.abs(tZ-saveZ)) + (sizeX * sizeZ)
  428.        
  429.         while (turtle.getFuelLevel() < fuelNeeded) do
  430.                
  431.                 if bDebug then print("Entering while true do in goUnload fuel check stage") end
  432.                
  433.                 -- Scan inventory for fuel
  434.                 local slot = 1
  435.                 turtle.select(slot)
  436.                 local bRefueled = false
  437.                
  438.                 while true do
  439.                         if turtle.refuel(1) then
  440.                                 -- Found fuel in current slot, consume 1, see if it's enough, if not loop again
  441.                                 print("Consuming fuel item from slot "..slot)
  442.                                 if (turtle.getFuelLevel()>=fuelNeeded) then
  443.                                         print("Refueled from inventory, resuming quarrying...")
  444.                                         bRefueled = true
  445.                                         break
  446.                                 end
  447.                         else
  448.                                 -- Couldn't refuel from currently-selected slot, try next slot. If there are no more slots, ask for player help.
  449.                                 if (slot < 16) then
  450.                                         slot = slot + 1
  451.                                         turtle.select(slot)
  452.                                 else
  453.                                         -- 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
  454.                                         slot = 1
  455.                                         break
  456.                                 end
  457.                         end
  458.                 end
  459.                
  460.                 if not bRefueled then
  461.                         turtle.select(1)
  462.                         print("Please add more fuel items to the turtle and press any key. Has:"..turtle.getFuelLevel().." Needs:"..fuelNeeded)
  463.                         os.pullEvent("key")    -- suspend code execution awaiting user keypress
  464.                 end
  465.         end
  466.        
  467.         if not moveTo(saveX,saveZ,saveY) then
  468.                 -- Critical failure to move
  469.                 return false
  470.         end
  471.        
  472.         orient(saveXdir,saveZdir)
  473.        
  474.         return true
  475. end
  476.  
  477. local function checkFreeSlot()
  478.   -- This function will return true if the designated refuelSlot is empty, because if there is no fuel reserve there, there
  479.   -- is no reason not to allow item collection into this slot.
  480.         for i=1,16 do
  481.                 if turtle.getItemCount(i)==0 then
  482.                   return true
  483.                 end
  484.         end
  485.  
  486.   -- Turtle does not have empty slot, goUnload
  487.         if not goUnload() then
  488.                 return false
  489.         end
  490.  
  491.         return true
  492. end
  493.  
  494. --[[
  495.  
  496.   START OF THE MAIN PROGRAM
  497.  
  498. --]]
  499.  
  500. checkFreeSlot()
  501.  
  502. local abort = false
  503. local traversal = 1 -- Counts x-z layers we're rasterizing. Used to determine turning directions at end of columns and layers
  504. local lowestY = 1-sizeY
  505. local bDigBelow, bDigAbove = false, false -- initially false
  506. while true do -- This loops digging layers
  507.   print("Main loop traversal="..traversal.." tY="..tY.." lowestY="..lowestY)
  508.  
  509.   if (traversal==1) then --special case since turtle initially starts NOT on a layer that it just dug out.
  510.         if ((tY - lowestY) == 0) then
  511.                 bDigBelow, bDigAbove = false, false
  512.         elseif ((tY - lowestY) == 1) then
  513.                 bDigBelow, bDigAbove = true, false
  514.         elseif ((tY - lowestY) >= 2) then
  515.                 bDigBelow, bDigAbove = true, true
  516.                 if not goDown() then
  517.           -- Turtle can't dig down, adjust lowestY because we can't go as deep as planned
  518.                   lowestY = tY - 1
  519.                 end
  520.         else
  521.                 -- Error: turtle is not in an expected place
  522.                 print("Error: Turtle vertical position is not what we expect on 1st traversal. Aborting, please debug.")
  523.                 abort = true
  524.                 break
  525.         end
  526.   else
  527.         -- Not our first traversal, and turtle should now be on the last layer it dug out.
  528.         if ((tY - lowestY) == 1) then
  529.                 bDigBelow, bDigAbove = true, false
  530.         elseif ((tY - lowestY) == 2) then
  531.                 bDigBelow, bDigAbove = true, false
  532.                 if not goDown() then
  533.           -- Turtle can't go down, adjust lowestY because we can't go as deep as planned
  534.                   lowestY = tY - 1
  535.                 end
  536.         elseif ((tY - lowestY) >= 3) then
  537.                 bDigBelow, bDigAbove = true, true
  538.                 -- Try to descend 2, if either fails, adjust lowestY to just below where turtle is able to get to, and
  539.                 -- cancel the need to digAbove
  540.                 for j=1,2 do
  541.                         if not goDown() then
  542.                           -- Turtle can't dig down, adjust lowestY because we can't go as deep as planned
  543.                           lowestY = tY - 1
  544.                           bDigAbove = false
  545.                         end
  546.                 end
  547.         else
  548.                 -- Error: turtle is not in an expected place
  549.                 print("Error: Turtle vertical position is not what we expect on traversal>1. Aborting, please debug.")
  550.                 abort = true
  551.                 break
  552.         end
  553.   end
  554.  
  555.  
  556.  
  557.   for column=1,sizeX  do  -- This loops sizeX times digging out columns
  558.     for block=1,(sizeZ-1) do -- this loops (sizeZ-1) times doing digDown and goForward to do all but the end of each column
  559.      
  560.       -- Since we're about to do a potentially ore-digging move, check for free space in inventory.
  561.       -- hasFreeSlot() calls goUnload if necessary
  562.       if not checkFreeSlot() then
  563.         print("Error: checkFreeSlot failure.")
  564.         abort = true
  565.         break
  566.       end
  567.      
  568.       if bDigBelow and turtle.detectDown() then
  569.         if not turtle.digDown() then
  570.           -- Turtle can't dig down, but we're not moving down so this is not a fatal error.
  571.           -- It might be bedrock below turtle, but that's not a concern until turtle is level with it.
  572.         end
  573.       end
  574.      
  575.       if bDigAbove and turtle.detectUp() then
  576.         if not turtle.digUp() then
  577.           -- Turtle can't dig up. This is actually concerning since we don't want to get him trapped under bedrock.
  578.           -- Because of the danger of entrapment, we're ending our quarrying here.
  579.           print("Turtle below undiggable block, backing out and returning home.")
  580.           turnRight()
  581.           turnRight()
  582.           if not goForward() then
  583.             -- This failure we care about, because there is something blocking us that we
  584.             -- can't dig, attack or otherwise resolve. Bust out of our digging loops and attempt to return home
  585.             print("Fatal Error during column goForward tX="..tX.." tZ="..tZ.." tY="..tY)
  586.           end
  587.                   abort = true
  588.           break
  589.         end
  590.                 sleep(0.5) -- wait to see if anything falls on turtle from above (sand, gravel)
  591.                
  592.                 -- First dig up was successful, so we haven't got an undiggable block above, and undiggables can't fall, so we can
  593.                 -- safely loop trying to dig up as long as something is falling on us (gravel, sand)
  594.                 while turtle.detectUp() do
  595.                         if bDebug then print("in while turtrle.detectUp() loop.") end
  596.                         if not turtle.digUp() then
  597.                                 -- whatever is up there, we couldn't dig it, but it's not bedrock. Just move on...
  598.                                 break
  599.                         end
  600.                         sleep(0.5)
  601.                 end
  602.       end
  603.      
  604.       if not goForward() then
  605.         -- This failure we care about, because there is something blocking us that we
  606.         -- can't dig, attack or otherwise resolve. Bust out of our digging loops and attempt to return home
  607.         -- after first checking to be sure the turtle is not under bedrock (if digging above)
  608.         print("Fatal Error during column goForward tX="..tX.." tZ="..tZ.." tY="..tY)
  609.         abort = true
  610.         break
  611.       end
  612.     end -- end of block loop
  613.    
  614.     -- If movement failed while traversing column, escape out, backing out from under bedrock if needed
  615.     if abort then
  616.       if bDigAbove and turtle.detectUp() then
  617.         if not turtle.digUp() then
  618.           -- Turtle can't dig up. This is actually concerning since we don't want to get him trapped under bedrock.
  619.           -- Because of the danger of entrapment, we're ending our quarrying here.
  620.           print("Turtle below undiggable block, backing out and returning home.")
  621.           turnRight()
  622.           turnRight()
  623.           if not goForward() then
  624.             -- This failure we care about, because there is something blocking us that we
  625.             -- can't dig, attack or otherwise resolve. Bust out of our digging loops and attempt to return home
  626.             print("Fatal Error during column goForward tX="..tX.." tZ="..tZ.." tY="..tY)
  627.           end
  628.         end
  629.       end
  630.    
  631.       -- unwinding
  632.       break
  633.     end
  634.    
  635.     -- Dig out the last block of this column
  636.     if bDigBelow and turtle.detectDown() then
  637.       if not turtle.digDown() then
  638.         -- Turtle can't dig down, but we're not moving down so this is not a fatal error.
  639.         -- It might be bedrock below turtle, but that's not a concern until turtle is level with it.
  640.       end
  641.     end
  642.    
  643.     -- Do last digUp in this column, if required by bDigAbove
  644.     if bDigAbove and turtle.detectUp() then
  645.         if not turtle.digUp() then
  646.           -- Turtle can't dig up. This is actually concerning since we don't want to get him trapped under bedrock.
  647.           -- Because of the danger of entrapment, we're ending our quarrying here.
  648.           print("Turtle below undiggable block, backing out and returning home.")
  649.           turnRight()
  650.           turnRight()
  651.           if not goForward() then
  652.             -- This failure we care about, because there is something blocking us that we
  653.             -- can't dig, attack or otherwise resolve. Bust out of our digging loops and attempt to return home
  654.             print("Fatal Error during column goForward tX="..tX.." tZ="..tZ.." tY="..tY)
  655.           end
  656.                   abort = true
  657.           break
  658.         end
  659.                 sleep(0.5) -- wait to see if anything falls on turtle from above (sand, gravel)
  660.                
  661.                 -- First dig up was successful, so we haven't got an undiggable block above, and undiggables can't fall, so we can
  662.                 -- safely loop trying to dig up as long as something is falling on us (gravel, sand)
  663.                 while turtle.detectUp() do
  664.                         if bDebug then print("in while turtrle.detectUp() loop.") end
  665.                         if not turtle.digUp() then
  666.                                 -- whatever is up there, we couldn't dig it, but it's not bedrock. Just move on...
  667.                                 break
  668.                         end
  669.                         sleep(0.5)
  670.                 end
  671.     end
  672.    
  673.     -- Turtle just finished the column, figure out if we need to advance to a new column, or
  674.     -- if we've finished the layer. If we need to turn to start a new column, we have to figure out which
  675.     -- direction to turn
  676.     if (column<sizeX) then
  677.       -- Turtle is at the end of a z column, but not the end of the whole x-z layer traversal
  678.      
  679.       -- FYI: These odd/even values are based on 1-based block, column, traversal numbers not 0-based tX, tZ, tY
  680.       -- sorry if that's confusing, but Lua convention is to start loop indicies at 1
  681.       local evenCol = ((column%2)==0)
  682.       local evenWidth = ((sizeX%2)==0)
  683.       local evenLayer = ((traversal%2)==0)
  684.       local backtrackingLayer = (evenWidth and evenLayer)
  685.      
  686.       if ((not evenCol and not backtrackingLayer) or (evenCol and backtrackingLayer)) then
  687.         turnRight() -- turn towards next row
  688.         if not goForward() then
  689.           print("Fatal Error during goForward from column "..column.." to column "..(column+1).." tX="..tX.." tZ="..tZ.." tY="..tY)
  690.           abort = true
  691.           break
  692.         end
  693.        
  694.         -- Danger check to see if we've moved under an undiggable block
  695.         if bDigAbove and turtle.detectUp() and not turtle.digUp() then
  696.           print("Turtle below undiggable block, backing out 1 and returning home.")
  697.           turnRight()
  698.           turnRight()
  699.           if not goForward() then
  700.             -- This failure we care about, because there is something blocking us that we
  701.             -- can't dig, attack or otherwise resolve. Bust out of our digging loops and attempt to return home
  702.             print("Fatal Error during column goForward tX="..tX.." tZ="..tZ.." tY="..tY)
  703.           end
  704.           abort = true
  705.           break
  706.         end
  707.        
  708.         turnRight()
  709.       else
  710.         turnLeft()
  711.         if not goForward() then
  712.           print("Fatal Error during goForward from column "..column.." to column "..(column+1).." tX="..tX.." tZ="..tZ.." tY="..tY)
  713.           abort = true
  714.           break
  715.         end
  716.        
  717.         -- Danger check to see if we've moved under an undiggable block
  718.         if bDigAbove and turtle.detectUp() and not turtle.digUp() then
  719.           print("Turtle below undiggable block, backing out 1 and returning home.")
  720.           turnRight()
  721.           turnRight()
  722.           if not goForward() then
  723.             -- This failure we care about, because there is something blocking us that we
  724.             -- can't dig, attack or otherwise resolve. Bust out of our digging loops and attempt to return home
  725.             print("Fatal Error during column goForward tX="..tX.." tZ="..tZ.." tY="..tY)
  726.           end
  727.           abort = true
  728.           break
  729.         end
  730.        
  731.         turnLeft()
  732.       end
  733.       -- Turtle is now ready to start the next column
  734.     else
  735.       -- Turtle is at the end of the layer, rotate 180
  736.       turnRight()
  737.       turnRight()
  738.     end
  739.   end -- end of column loop
  740.  
  741.   if abort then
  742.         print("Abort breaking out of while true loop.")
  743.     -- abort in progress, unwinding out of loops
  744.     break
  745.   end
  746.  
  747.   -- See if we're done yet
  748.   if ((tY - lowestY) == 0) or (((tY - lowestY) == 1) and (bDigBelow == true)) then
  749.     -- We're done. We've finished digging our lowest layer either by digging forward or with digDown.
  750.     done = true
  751.     break
  752.   end
  753.  
  754.   -- If we got past the last if-then, we are not done, so we need to descend in preparation for next layer traversal
  755.   if bDigBelow then
  756.     -- 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
  757.     -- First we try to descend through the dug-down layer, but since that could have bedrock (we we skimming above it not caring)
  758.     -- we need to test to see if we can descend into that layer at our current tX,tZ location
  759.     if not goDown() then
  760.       print("Turtle finished a traversal and was digging below. Turtle can't go further down, we're done quarrying.")
  761.       abort = true
  762.       break
  763.     end
  764.   end
  765.  
  766.   traversal = traversal + 1
  767. end -- end of while not done loop
  768.  
  769. -- Quarrying ended, either normally or because of encountering undiggable block. Try to return to 0,0,0
  770. if not goHome(0,0,0) then
  771.   -- Can't even get back home :-( Notify the user
  772.   print("Turtle was not able to safely get back to starting location")
  773.   abort = true
  774. else
  775.         orient(0,-1)
  776.  
  777.         -- Drop everything
  778.         -- Drop items. Turtle will not empty the slot designated as the refuel slot.
  779.   print("Unloading all contents...")
  780.         for i=1,16 do
  781.                 turtle.select(i)
  782.                 turtle.drop()
  783.         end
  784.         orient(0,1)
  785. end
  786.  
  787. if abort then
  788.   print("Quarrying ended due to encounter with undiggable block.")
  789. else
  790.   print("Quarrying complete to desired depth.")
  791. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement