Advertisement
masa-

CC Turtle: Digger (LxWx1H) v0.3.0a2

Mar 22nd, 2013
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 19.67 KB | None | 0 0
  1. local args = { ... }
  2.  
  3. local length = 0
  4. local width = 0
  5. local posX = -1         -- Relative distance from the starting corner. We start one block outside of the area.
  6. local posZ = 0
  7. local orientation = 0   -- 0: starting orientation; 1: right; 2: back; 3: left
  8. --local doneX = 0           -- Already cleared area, length
  9. --local doneZ = 0           -- Already cleared area, width
  10. local doneRows = 0      -- Already cleared area on the current layer
  11. local smallerDimension = 0
  12. local largerDimension = 0
  13. local tunnelingDirection = 0    -- Direction that the tunnels will be dug (axis)
  14. local nextTunnelDirection = 0   -- The direction of the next tunnel (alternates with +-2 when going back and forth)
  15. local tunnelingOrder = 0    -- In which direction are we creating the new tunnels (= the direction perpendicular to the tunnels)
  16. local distanceTraveled = 0  -- Total distance moved
  17. local numBlocks = 0         -- Total number of blocks mined
  18. local numAttacks = 0        -- Total number of times that we attacked mobs
  19. local fl = turtle.getFuelLevel()
  20. local dumpInventory = false
  21.  
  22. if #args < 1 then
  23.     print("Usage: progname <length> [width] [dumpInventory (true|false)]")
  24.     return
  25. end
  26.  
  27. length = tonumber( args[1] )
  28.  
  29. if #args >= 2 then
  30.     width = tonumber( args[2] )
  31. else
  32.     width = length
  33. end
  34.  
  35. if #args == 3 then
  36.     dumpInventory = args[3]
  37. end
  38.  
  39. if length < 1 then
  40.     print("Error: Length must be at least 1")
  41.     return
  42. end
  43.  
  44. if width < 1 then
  45.     print("Error: Width must be at least 1")
  46.     return
  47. end
  48.  
  49. -- FIXME: We should use a better, runtime fuel check
  50. --if fl < (length * 2) then
  51. --  print("Error: Not enough fuel to complete the action (need at least " .. (length * 2) .. ")")
  52. --  return
  53. --end
  54.  
  55.  
  56. -- Dig the block in front of the turtle until no block is detected anymore
  57. function digUntilClear(face)
  58.     if face == nil then
  59.         face = "front"
  60.     end
  61.  
  62.     if face == "up" then
  63.         -- If there is a block above, dig it. Loop in case of falling sand/gravel.
  64.         while turtle.detectUp() == true do
  65.             if turtle.digUp() == true then
  66.                 numBlocks = numBlocks + 1
  67.             else
  68.                 print("digUntilClear(\"up\"): Could not dig the block above! Aborting...")
  69.                 return false
  70.             end
  71.             sleep(0.5)
  72.         end
  73.     elseif face == "down" then
  74.         -- If there is a block below, dig it.
  75.         while turtle.detectDown() == true do
  76.             if turtle.digDown() == true then
  77.                 numBlocks = numBlocks + 1
  78.             else
  79.                 print("digUntilClear(\"down\"): Could not dig the block below! Aborting...")
  80.                 return false
  81.             end
  82.             sleep(0.5)
  83.         end
  84.     else -- front
  85.         -- If there is a block in front, dig it. Loop in case of falling sand/gravel.
  86.         while turtle.detect() == true do
  87.             if turtle.dig() == true then
  88.                 numBlocks = numBlocks + 1
  89.             else
  90.                 print("digUntilClear(): Could not dig the block in front! Aborting...")
  91.                 return false
  92.             end
  93.             sleep(0.5)
  94.         end
  95.     end
  96.  
  97.     return true
  98. end
  99.  
  100.  
  101. -- Attack as long as the attack succeeds (= mobs in front)
  102. function attackUntilClear()
  103.     local tmp1 = 0
  104.  
  105.     -- Attack if there are mobs in front of the turtle
  106.     while turtle.attack() == true do
  107.         numAttacks = numAttacks + 1
  108.         -- Failsafe limit
  109.         tmp1 = tmp1 + 1
  110.         if tmp1 > 100 then
  111.             print("attackUntilClear(): Hit the failsafe limit (100) of attacks!")
  112.             return false
  113.         end
  114.     end
  115.  
  116.     return true
  117. end
  118.  
  119.  
  120. -- Move forward dist blocks
  121. function moveForward(dist)
  122.     local tmp2 = 0
  123.  
  124.     if dist <= 0 then
  125.         return true
  126.     end
  127.  
  128.     for i = 1, dist do
  129.         tmp2 = 0
  130.         -- Attack while we can't move forward (because someONE is blocking us)
  131.         while turtle.forward() == false do
  132.             if attackUntilClear() == false then
  133.                 print("moveForward(dist = " .. dist .. "): attackUntilClear() returned false")
  134.                 return false
  135.             end
  136.  
  137.             -- Failsafe limit
  138.             tmp2 = tmp2 + 1
  139.             if tmp2 > 100 then
  140.                 print("moveForward(dist = " .. dist .. "): Hit the failsafe limit (100) of trying to move")
  141.                 return false
  142.             end
  143.         end
  144.  
  145.         if orientation == 0 then
  146.             posX = posX + 1
  147.         elseif orientation == 1 then
  148.             posZ = posZ + 1
  149.         elseif orientation == 2 then
  150.             posX = posX - 1
  151.         elseif orientation == 3 then
  152.             posZ = posZ - 1
  153.         else
  154.             print("moveForward(dist = " .. dist .. "): Invalid orientation!")
  155.             return false
  156.         end
  157.  
  158.         distanceTraveled = distanceTraveled + 1
  159.     end
  160.  
  161.     return true
  162. end
  163.  
  164.  
  165. function turnRight()
  166.     turtle.turnRight()
  167.  
  168.     if orientation == 3 then
  169.         orientation = 0
  170.     else
  171.         orientation = orientation + 1
  172.     end
  173. end
  174.  
  175.  
  176. function turnLeft()
  177.     turtle.turnLeft()
  178.  
  179.     if orientation == 0 then
  180.         orientation = 3
  181.     else
  182.         orientation = orientation - 1
  183.     end
  184. end
  185.  
  186.  
  187. function reOrient(dir)
  188.     if dir == orientation then
  189.         return true
  190.     end
  191.  
  192.     if dir > orientation then
  193.         if (dir - orientation) <= 2 then
  194.             while orientation ~= dir do
  195.                 turnRight()
  196.             end
  197.         else
  198.             while orientation ~= dir do
  199.                 turnLeft()
  200.             end
  201.         end
  202.     else -- orientation >= dir
  203.         if (orientation - dir) <= 2 then
  204.             while orientation ~= dir do
  205.                 turnLeft()
  206.             end
  207.         else
  208.             while orientation ~= dir do
  209.                 turnRight()
  210.             end
  211.         end
  212.     end
  213.  
  214.     return true
  215. end
  216.  
  217.  
  218. function digLeftAndRight(first, turn)
  219.     if turn == nil then
  220.         turn = "forward"
  221.     end
  222.     if first == nil then
  223.         first = "left"
  224.     end
  225.  
  226.     -- Dig to the left first
  227.     if first == "left" then
  228.         -- Dig the blocks that are to the left and to the right
  229.         turnLeft()
  230.  
  231.         if digUntilClear() == false then
  232.             print("digLeftAndRight(first = " .. first .. ", turn = " .. turn .. "): digUntilClear() (left)")
  233.             return false
  234.         end
  235.  
  236.         turnRight()
  237.         turnRight()
  238.  
  239.         if digUntilClear() == false then
  240.             print("digLeftAndRight(first = " .. first .. ", turn = " .. turn .. "): digUntilClear() (right)")
  241.             return false
  242.         end
  243.  
  244.         -- Which way do we want to be left facing
  245.         if turn == "forward" or turn == nil then
  246.             turnLeft()
  247.         elseif turn == "backward" then
  248.             turnRight()
  249.         -- else: stay facing right
  250.         end
  251.     elseif first == "right" then -- right first
  252.         -- Dig the blocks that are to the left and to the right
  253.         turnRight()
  254.  
  255.         if digUntilClear() == false then
  256.             print("digLeftAndRight(first = " .. first .. ", turn = " .. turn .. "): digUntilClear() (right)")
  257.             return false
  258.         end
  259.  
  260.         turnLeft()
  261.         turnLeft()
  262.  
  263.         if digUntilClear() == false then
  264.             print("digLeftAndRight(first = " .. first .. ", turn = " .. turn .. "): digUntilClear() (left)")
  265.             return false
  266.         end
  267.  
  268.         -- Which way do we want to be left facing
  269.         if turn == "forward" then
  270.             turnRight()
  271.         elseif turn == "backward" then
  272.             turnLeft()
  273.         -- else: stay facing left
  274.         end
  275.     else
  276.         print("digLeftAndRight(first = " .. first .. ", turn = " .. turn .. "): Invalid 'first' value")
  277.         return false
  278.     end
  279.  
  280.     return true
  281. end
  282.  
  283.  
  284. -- Dig and move forward in a straight line (1 high)
  285. function digAndMoveForward1W(len, ud)
  286.     if len <= 0 then return true end
  287.     if ud == nil then ud = "none" end
  288.  
  289.     for i = 1, len do
  290.         if digUntilClear() == false then
  291.             print("digAndMoveForward1W(len = " .. len .. ", ud = \"" .. ud .. "\"): digUntilClear() (i = " .. i .. ")")
  292.             return false
  293.         end
  294.  
  295.         if moveForward(1) == false then
  296.             print("digAndMoveForward1W(len = " .. len .. ", ud = \"" .. ud .. "\): moveForward(1) (i = " .. i .. ")")
  297.             return false
  298.         end
  299.  
  300.         if ud == "up" then
  301.             if digUntilClear("up") == false then
  302.                 print("digAndMoveForward1W(len = " .. len .. ", ud = \"" .. ud .. "\): digUntilClear(\"up\") (i = " .. i .. ")")
  303.                 return false
  304.             end
  305.         elseif ud == "down" then
  306.             if digUntilClear("down") == false then
  307.                 print("digAndMoveForward1W(len = " .. len .. ", ud = \"" .. ud .. "\): digUntilClear(\"down\") (i = " .. i .. ")")
  308.                 return false
  309.             end
  310.         end
  311.     end
  312.  
  313.     return true
  314. end
  315.  
  316.  
  317. -- Dig a 2 wide, 1 high tunnel (dir determines if we dig to the left or to the right)
  318. function digAndMoveForward2Wx1H(len, dir, turn)
  319.     if len <= 0 then return true end
  320.     if dir == nil then dir = "right" end
  321.     if turn == nil then turn = "forward" end
  322.  
  323.     for i = 1, len do
  324.         -- Dig the block directly in front
  325.         if digUntilClear() == false then
  326.             print("digAndMoveForward2Wx1H(): digUntilClear() (front, i = " .. i .. ")")
  327.             return false
  328.         end
  329.  
  330.         -- Move to the digged spot
  331.         if moveForward(1) == false then
  332.             print("digAndMoveForward2Wx1H(): moveForward(1) (i = " .. i .. ")")
  333.             return false
  334.         end
  335.  
  336.         if dir == "right" then
  337.             -- Dig the block that is to the right
  338.             turnRight()
  339.             if digUntilClear() == false then
  340.                 print("digAndMoveForward2Wx1H(): digUntilClear() (right, i = " .. i .. ")")
  341.                 return false
  342.             end
  343.  
  344.             if i < len or turn == "forward" then
  345.                 -- Reorient to the forward direction
  346.                 turnLeft()
  347.             elseif turn == "backward" then
  348.                 -- Turn around (= backwards)
  349.                 turnRight()
  350.             -- else: leave facing right
  351.             end
  352.         else
  353.             -- Dig the block that is to the left
  354.             turnLeft()
  355.             if digUntilClear() == false then
  356.                 print("digAndMoveForward2Wx1H(): digUntilClear() (left, i = " .. i .. ")")
  357.                 return false
  358.             end
  359.  
  360.             if i < len or turn == "forward" then
  361.                 -- Reorient to the forward direction
  362.                 turnRight()
  363.             elseif turn == "backward" then
  364.                 -- Turn around (= backwards)
  365.                 turnLeft()
  366.             -- else: leave facing left
  367.             end
  368.         end
  369.     end
  370.  
  371.     return true
  372. end
  373.  
  374.  
  375. -- Dig a 3 wide, 1 high tunnel
  376. -- len: Length of the tunnel we want to dig. We start outside of it.
  377. -- first: Do we want to dig to the left or to the right first (parameter to digLeftAndRight() )
  378. -- turn: Which way do we want to face after the digLeftAndRight() finishes.
  379. function digAndMoveForward3Wx1H(len, first, turn)
  380.     if len <= 0 then
  381.         return true
  382.     end
  383.  
  384.     for i = 1, len do
  385.         -- Dig the block in front
  386.         if digUntilClear() == false then
  387.             print("digAndMoveForward3Wx1H(): digUntilClear() (front, i = " .. i .. ")")
  388.             return false
  389.         end
  390.  
  391.         -- Move to the digged spot
  392.         if moveForward(1) == false then
  393.             print("digAndMoveForward3Wx1H(): moveForward(1) (i = " .. i .. ")")
  394.             return false
  395.         end
  396.  
  397.         -- Dig the blocks that are to the left and to the right
  398.         if i < len then
  399.             -- Reorient to the forward direction
  400.             if digLeftAndRight(first, "forward") == false then
  401.                 print("digAndMoveForward3Wx1H(): digLeftAndRight(first = " .. first .. ") (i = " .. i .. ")")
  402.                 return false
  403.             end
  404.         else
  405.             -- Last block, reorient to the requested direction
  406.             if digLeftAndRight(first, turn) == false then
  407.                 print("digAndMoveForward3Wx1H(): digLeftAndRight(first = " .. first .. ", turn = " .. turn .. ") (last)")
  408.                 return false
  409.             end
  410.         end
  411.     end
  412.  
  413.     return true
  414. end
  415.  
  416.  
  417. -- Move to the requested X coordinate
  418. function moveToX(X)
  419.     if X < posX then
  420.         reOrient(2)
  421.         if digAndMoveForward1W(posX - X, "none") == false then
  422.             print("moveToX(): digAndMoveForward1W(posX - X = " .. (posX - X) .. ", \"none\")")
  423.             return false
  424.         end
  425.     elseif posX < X then
  426.         reOrient(0)
  427.         if digAndMoveForward1W(X - posX, "none") == false then
  428.             print("moveToX(): digAndMoveForward1W(X - posX = " .. (X - posX) .. ", \"none\")")
  429.             return false
  430.         end
  431.     end
  432.  
  433.     return true
  434. end
  435.  
  436.  
  437. -- Move to the requested Z coordinate
  438. function moveToZ(Z)
  439.     if Z < posZ then
  440.         reOrient(3)
  441.         if digAndMoveForward1W(posZ - Z, "none") == false then
  442.             print("moveToZ(): digAndMoveForward1W(posZ - Z = " .. (posZ - Z) .. ", \"none\")")
  443.             return false
  444.         end
  445.     elseif posZ < Z then
  446.         reOrient(1)
  447.         if digAndMoveForward1W(Z - posZ, "none") == false then
  448.             print("moveToZ(): digAndMoveForward1W(Z - posZ = " .. (Z - posZ) .. ", \"none\")")
  449.             return false
  450.         end
  451.     end
  452.  
  453.     return true
  454. end
  455.  
  456.  
  457. -- Get the tunnel axis direction
  458. -- The tunnels will be dug along the longer dimension,
  459. -- so that we can avoid unnecessary zig-zag motion.
  460. -- This only needs to be called once
  461. function getTunnelingDirection()
  462.     if width > length then
  463.         return 1
  464.     end
  465.  
  466.     return 0
  467. end
  468.  
  469.  
  470. -- Get the next tunnel's digging direction
  471. -- This should be called when moving to position for the next tunnel
  472. function getNextTunnelDirection()
  473.     -- Tunnels go along the X-axis: diggingDirection is 0 or 2
  474.     if tunnelingDirection == 0 then
  475.         -- We are closer to the starting corner than the back corner in X-direction
  476.         if posX <= (length - 1 - posX) then
  477.             return 0
  478.         else
  479.             return 2
  480.         end
  481.     else -- Tunnels go along the Z-axis: diggingDirection is 1 or 3
  482.         -- We are closer to the starting corner than the back corner in Z-direction
  483.         if posZ <= (width - 1 - posZ) then
  484.             return 1
  485.         else
  486.             return 3
  487.         end
  488.     end
  489.  
  490.     return false
  491. end
  492.  
  493.  
  494. -- Get tunneling order (starting from the front or the back end of the area?)
  495. -- This should be called when starting a new layer (Y-coordinate changes)
  496. function getTunnelingOrder()
  497.     -- Tunnels go along the X-axis: diggingDirection is 0 or 2
  498.     if tunnelingDirection == 0 then
  499.         -- We are closer to the starting corner than the back corner in X-direction
  500.         if posZ <= (width - 1 - posZ) then
  501.             return 1
  502.         else
  503.             return 3
  504.         end
  505.     else -- Tunnels go along the Z-axis: diggingDirection is 1 or 3
  506.         -- We are closer to the starting corner than the back corner in Z-direction
  507.         if posX <= (length - 1 - posX) then
  508.             return 0
  509.         else
  510.             return 2
  511.         end
  512.     end
  513.  
  514.     return false
  515. end
  516.  
  517.  
  518. -- Move to the starting position for the next tunnel (so that the tunnel
  519. -- will align with the already dug area)
  520. function moveToTunnelStartPos()
  521.     local Z = 0
  522.     local X = 0
  523.  
  524.     -- Tunnels along the X-axis
  525.     if tunnelingDirection == 0 then
  526.         -- At least 3 blocks still to go
  527.         if width >= (doneRows + 3) then
  528.             -- Tunnels go from the starting corner to the right
  529.             if tunnelingOrder == 1 then
  530.                 Z = doneRows + 1 -- the middle of the next 3-wide tunnel
  531.             else -- Tunnels go from the right towards the starting corner
  532.                 Z = width - doneRows - 2 -- the middle of the next 3-wide tunnel
  533.             end
  534.         else -- 1 or 2 blocks to go
  535.             -- Tunnels go from the start corner to the right
  536.             if tunnelingOrder == 1 then
  537.                 Z = doneRows -- one block into the undug part
  538.             else -- Tunnels go from the right towards the starting corner
  539.                 Z = width - doneRows - 1 -- one block into the undug part
  540.             end
  541.         end
  542.  
  543.         moveToZ(Z)
  544.         -- 2 or 3 blocks to go, dig the block next to the starting position
  545.         if width > (doneRows + 1) then
  546.             reOrient(tunnelingOrder)
  547.             digUntilClear()
  548.         end
  549.     else -- Tunnels along the Z-axis
  550.         -- At least 3 blocks still to go
  551.         if length >= (doneRows + 3) then
  552.             -- Tunnels go from the starting corner towards the back
  553.             if tunnelingOrder == 0 then
  554.                 X = doneRows + 1 -- the middle of the next 3-wide tunnel
  555.             else -- Tunnels go from the back towards the starting corner
  556.                 X = length - doneRows - 2 -- the middle of the next 3-wide tunnel
  557.             end
  558.         else -- 1 or 2 blocks to go
  559.             -- Tunnels go from the starting corner towards the back
  560.             if tunnelingOrder == 0 then
  561.                 X = doneRows -- one block into the undug part
  562.             else -- Tunnels go from the back towards the starting corner
  563.                 X = length - doneRows - 1 -- one block into the undug part
  564.             end
  565.         end
  566.  
  567.         moveToX(X)
  568.         -- 2 or 3 blocks to go, dig the block next to the starting position
  569.         if length > (doneRows + 1) then
  570.             reOrient(tunnelingOrder)
  571.             digUntilClear()
  572.         end
  573.     end
  574.  
  575.     return true
  576. end
  577.  
  578.  
  579. function digTunnel(len)
  580.     if (smallerDimension - doneRows) >= 3 then
  581.         --digAndMoveForward3Wx1H(len, first, turn)
  582.         -- Optimize the turns based on the tunneling order and digging direction
  583.         if nextTunnelDirection == 0 then
  584.             if tunnelingOrder == 1 then
  585.                 -- +X, +Z
  586.                 digAndMoveForward3Wx1H(len, "left", "none")
  587.             else
  588.                 -- +X, -Z
  589.                 digAndMoveForward3Wx1H(len, "right", "none")
  590.             end
  591.         elseif nextTunnelDirection == 2 then
  592.             if tunnelingOrder == 1 then
  593.                 -- -X, +Z
  594.                 digAndMoveForward3Wx1H(len, "right", "none")
  595.             else
  596.                 -- -X, -Z
  597.                 digAndMoveForward3Wx1H(len, "left", "none")
  598.             end
  599.         elseif nextTunnelDirection == 1 then
  600.             if tunnelingOrder == 0 then
  601.                 -- +Z, +X
  602.                 digAndMoveForward3Wx1H(len, "right", "none")
  603.             else
  604.                 -- +Z, -X
  605.                 digAndMoveForward3Wx1H(len, "left", "none")
  606.             end
  607.         elseif nextTunnelDirection == 3 then
  608.             if tunnelingOrder == 0 then
  609.                 -- -Z, +X
  610.                 digAndMoveForward3Wx1H(len, "left", "none")
  611.             else
  612.                 -- -Z, -X
  613.                 digAndMoveForward3Wx1H(len, "right", "none")
  614.             end
  615.         end
  616.  
  617.         doneRows = doneRows + 3
  618.     elseif (smallerDimension - doneRows) == 2 then
  619.         --digAndMoveForward2Wx1H(len, dir, turn)
  620.         -- Optimize the turns based on the tunneling order and digging direction
  621.         if nextTunnelDirection == 0 then
  622.             if tunnelingOrder == 1 then
  623.                 -- +X, +Z
  624.                 digAndMoveForward2Wx1H(len, "right", "none")
  625.             else
  626.                 -- +X, -Z
  627.                 digAndMoveForward2Wx1H(len, "left", "none")
  628.             end
  629.         elseif nextTunnelDirection == 2 then
  630.             if tunnelingOrder == 1 then
  631.                 -- -X, +Z
  632.                 digAndMoveForward2Wx1H(len, "left", "none")
  633.             else
  634.                 -- -X, -Z
  635.                 digAndMoveForward2Wx1H(len, "right", "none")
  636.             end
  637.         elseif nextTunnelDirection == 1 then
  638.             if tunnelingOrder == 0 then
  639.                 -- +Z, +X
  640.                 digAndMoveForward2Wx1H(len, "left", "none")
  641.             else
  642.                 -- +Z, -X
  643.                 digAndMoveForward2Wx1H(len, "right", "none")
  644.             end
  645.         elseif nextTunnelDirection == 3 then
  646.             if tunnelingOrder == 0 then
  647.                 -- -Z, +X
  648.                 digAndMoveForward2Wx1H(len, "right", "none")
  649.             else
  650.                 -- -Z, -X
  651.                 digAndMoveForward2Wx1H(len, "left", "none")
  652.             end
  653.         end
  654.  
  655.         doneRows = doneRows + 2
  656.     elseif (smallerDimension - doneRows) == 1 then
  657.         --digAndMoveForward1W(len, ud)
  658.         digAndMoveForward1W(len, "none")
  659.         doneRows = doneRows + 1
  660.     else
  661.         print("digTunnel(" .. length .. "): length less than 1")
  662.     end
  663.  
  664.     return true
  665. end
  666.  
  667.  
  668. tunnelingDirection = getTunnelingDirection()
  669. tunnelingOrder = getTunnelingOrder()
  670.  
  671.  
  672. -- Select the first slot so that the items get filled to the inventory starting from the first slot
  673. turtle.select(1)
  674.  
  675.  
  676. -- Corner case check: only dig the one block if the area is 1x1.
  677. if length == 1 and width == 1 then
  678.     digUntilClear()
  679.     doneRows = 1
  680. else -- normal, larger than 1x1 areas: move to the starting corner 0, 0
  681.     moveToX(0)
  682. end
  683.  
  684.  
  685. if length >= width then
  686.     smallerDimension = width
  687.     largerDimension = length
  688. else
  689.     smallerDimension = length
  690.     largerDimension = width
  691. end
  692.  
  693.  
  694. while (smallerDimension - doneRows) > 0 do
  695.     nextTunnelDirection = getNextTunnelDirection()
  696.  
  697.     print("posX: " .. posX)
  698.     print("posZ: " .. posZ)
  699.     print("tunnelingDirection: " .. tunnelingDirection)
  700.     print("tunnelingOrder: " .. tunnelingOrder)
  701.     print("nextTunnelDirection: " .. nextTunnelDirection)
  702.  
  703.     moveToTunnelStartPos()
  704.     reOrient(nextTunnelDirection)
  705.     digTunnel(largerDimension - 1)
  706. end
  707.  
  708.  
  709. if length > 1 or width > 1 then
  710.     -- Return to the starting corner
  711.     if orientation == 0 or orientation == 3 then
  712.         if moveToZ(0) == false then
  713.             print("Error while returning to the starting corner (Z)")
  714.             return false
  715.         end
  716.         if moveToX(0) == false then
  717.             print("Error while returning to the starting corner (X)")
  718.             return false
  719.         end
  720.     else
  721.         if moveToX(0) == false then
  722.             print("Error while returning to the starting corner (X)")
  723.             return false
  724.         end
  725.         if moveToZ(0) == false then
  726.             print("Error while returning to the starting corner (Z)")
  727.             return false
  728.         end
  729.     end
  730. end
  731.  
  732.  
  733. if posX == 0 then
  734.     reOrient(2)
  735.     if digAndMoveForward1W(1, "none") == false then
  736.         print("digAndMoveForward1W(1, \"none\"): Error while trying to move to the starting corner of the area")
  737.         return false
  738.     end
  739. elseif posX ~= -1 then
  740.     print("Error: Wrong position while trying to move to the starting spot")
  741.     return false
  742. end
  743.  
  744. if dumpInventory == "true" then
  745. --  reOrient(2)
  746.     -- If there is a block behind the starting position, assume it is a chest
  747.     -- and dump the inventory to it.
  748.     if turtle.detect() then
  749.         for i = 1, 16 do
  750.             if turtle.getItemCount(i) > 0 then
  751.                 turtle.select(i)
  752.                 turtle.drop()
  753.             end
  754. --          if turtle.drop() == false then
  755. --              break
  756. --          end
  757.         end
  758.     end
  759. end
  760.  
  761. -- And finally reorient
  762. reOrient(0)
  763.  
  764. -- And print a summary
  765. print("Done.")
  766. print("Mined " .. numBlocks .. " blocks.")
  767. print("Moved a total of " .. distanceTraveled .. " blocks.")
  768. print("Attacked mobs " .. numAttacks .. " times.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement