Advertisement
masa-

CC Turtle: Digger (LxWx1H) v0.3.0a1

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