Advertisement
masa-

CC Turtle: Digger (LxWx1H) v0.2.6

Feb 18th, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 25.72 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: " .. args[0] .. " <length> [width] [dump inventory when done (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 tunnelDirection = 0   -- Direction that the tunnels will be dug
  45. local distanceTraveled = 0  -- Total distance moved
  46. local numBlocks = 0         -- Total number of blocks mined
  47. local numAttacks = 0        -- Total number of times that we attacked mobs
  48.  
  49.  
  50. -- Dig the block in front of the turtle until no block is detected anymore
  51. function digUntilClear(face)
  52.     if face == nil then
  53.         face = "front"
  54.     end
  55.  
  56.     if face == "up" then
  57.         -- If there is a block above, dig it. Loop in case of falling sand/gravel.
  58.         while turtle.detectUp() == true do
  59.             if turtle.digUp() == true then
  60.                 numBlocks = numBlocks + 1
  61.             else
  62.                 print("digUntilClear(\"up\"): Could not dig the block above! Aborting...")
  63.                 return false
  64.             end
  65.             sleep(0.5)
  66.         end
  67.     elseif face == "down" then
  68.         -- If there is a block below, dig it.
  69.         while turtle.detectDown() == true do
  70.             if turtle.digDown() == true then
  71.                 numBlocks = numBlocks + 1
  72.             else
  73.                 print("digUntilClear(\"down\"): Could not dig the block below! Aborting...")
  74.                 return false
  75.             end
  76.             sleep(0.5)
  77.         end
  78.     else -- front
  79.         -- If there is a block in front, dig it. Loop in case of falling sand/gravel.
  80.         while turtle.detect() == true do
  81.             if turtle.dig() == true then
  82.                 numBlocks = numBlocks + 1
  83.             else
  84.                 print("digUntilClear(): Could not dig the block in front! Aborting...")
  85.                 return false
  86.             end
  87.             sleep(0.5)
  88.         end
  89.     end
  90.  
  91.     return true
  92. end
  93.  
  94.  
  95. -- Attack as long as the attack succeeds (= mobs in front)
  96. function attackUntilClear()
  97.     local tmp1 = 0
  98.  
  99.     -- Attack if there are mobs in front of the turtle
  100.     while turtle.attack() == true do
  101.         numAttacks = numAttacks + 1
  102.         -- Failsafe limit
  103.         tmp1 = tmp1 + 1
  104.         if tmp1 > 100 then
  105.             print("attackUntilClear(): Hit the failsafe limit (100) of attacks!")
  106.             return false
  107.         end
  108.     end
  109.  
  110.     return true
  111. end
  112.  
  113.  
  114. -- Move forward one block
  115. function moveForward(dist)
  116.     local tmp2 = 0
  117.  
  118.     if dist <= 0 then
  119.         return true
  120.     end
  121.  
  122.     for i = 1, dist do
  123.         tmp2 = 0
  124.         -- Attack while we can't move forward (because someONE is blocking us)
  125.         while turtle.forward() == false do
  126.             if attackUntilClear() == false then
  127.                 print("moveForward(dist = " .. dist .. "): attackUntilClear() returned false")
  128.                 return false
  129.             end
  130.  
  131.             -- Failsafe limit
  132.             tmp2 = tmp2 + 1
  133.             if tmp2 > 100 then
  134.                 print("moveForward(dist = " .. dist .. "): Hit the failsafe limit (100) of trying to move")
  135.                 return false
  136.             end
  137.         end
  138.  
  139.         if orientation == 0 then
  140.             posX = posX + 1
  141.         elseif orientation == 1 then
  142.             posZ = posZ + 1
  143.         elseif orientation == 2 then
  144.             posX = posX - 1
  145.         elseif orientation == 3 then
  146.             posZ = posZ - 1
  147.         else
  148.             print("moveForward(dist = " .. dist .. "): Invalid orientation!")
  149.             return false
  150.         end
  151.  
  152.         distanceTraveled = distanceTraveled + 1
  153.     end
  154.  
  155.     return true
  156. end
  157.  
  158.  
  159. function turnRight()
  160.     turtle.turnRight()
  161.     orientation = (orientation + 1) % 4
  162. end
  163.  
  164.  
  165. function turnLeft()
  166.     turtle.turnLeft()
  167.     orientation = (orientation - 1) % 4
  168. end
  169.  
  170.  
  171. function reOrient(dir)
  172.     if dir == orientation then
  173.         return true
  174.     end
  175.  
  176.     if dir > orientation then
  177.         if (dir - orientation) <= 2 then
  178.             while orientation ~= dir do
  179.                 turnRight()
  180.             end
  181.         else
  182.             while orientation ~= dir do
  183.                 turnLeft()
  184.             end
  185.         end
  186.     else -- orientation >= dir
  187.         if (orientation - dir) <= 2 then
  188.             while orientation ~= dir do
  189.                 turnLeft()
  190.             end
  191.         else
  192.             while orientation ~= dir do
  193.                 turnRight()
  194.             end
  195.         end
  196.     end
  197.  
  198.     return true
  199. end
  200.  
  201.  
  202. function digLeftAndRight(first, turn)
  203.     if turn == nil then
  204.         turn = "forward"
  205.     end
  206.     if first == nil then
  207.         first = "left"
  208.     end
  209.  
  210.     -- Dig to the left first
  211.     if first == "left" then
  212.         -- Dig the blocks that are to the left and to the right
  213.         turnLeft()
  214.         if digUntilClear() == false then
  215.             print("digLeftAndRight(first = " .. first .. ", turn = " .. turn .. "): digUntilClear() (left)")
  216.             return false
  217.         end
  218.  
  219.         turnRight()
  220.         turnRight()
  221.         if digUntilClear() == false then
  222.             print("digLeftAndRight(first = " .. first .. ", turn = " .. turn .. "): digUntilClear() (right)")
  223.             return false
  224.         end
  225.  
  226.         -- Which way do we want to be left facing
  227.         if turn == "forward" or turn == nil then
  228.             turnLeft()
  229.         elseif turn == "backward" then
  230.             turnRight()
  231.         -- else: stay facing right
  232.         end
  233.     elseif first == "right" then -- right first
  234.         -- Dig the blocks that are to the left and to the right
  235.         turnRight()
  236.         if digUntilClear() == false then
  237.             print("digLeftAndRight(first = " .. first .. ", turn = " .. turn .. "): digUntilClear() (right)")
  238.             return false
  239.         end
  240.  
  241.         turnLeft()
  242.         turnLeft()
  243.         if digUntilClear() == false then
  244.             print("digLeftAndRight(first = " .. first .. ", turn = " .. turn .. "): digUntilClear() (left)")
  245.             return false
  246.         end
  247.  
  248.         -- Which way do we want to be left facing
  249.         if turn == "forward" then
  250.             turnRight()
  251.         elseif turn == "backward" then
  252.             turnLeft()
  253.         -- else: stay facing left
  254.         end
  255.     else
  256.         print("digLeftAndRight(first = " .. first .. ", turn = " .. turn .. "): Invalid 'first' value")
  257.         return false
  258.     end
  259.  
  260.     return true
  261. end
  262.  
  263.  
  264. -- Dig a 3 wide, 1 high tunnel
  265. -- len: Length of the tunnel we want to dig. We start outside of it.
  266. -- lr: Do we want to dig to the left or to the right first (parameter to digLeftAndRight() )
  267. -- turn: Which way do we want to face after the digLeftAndRight() finishes.
  268. function digAndMoveForward3Wx1H(len, first, turn)
  269.     if len <= 0 then
  270.         return true
  271.     end
  272.  
  273.     for i = 1, len do
  274.         -- Dig the block in front
  275.         if digUntilClear() == false then
  276.             print("digAndMoveForward3Wx1H(): digUntilClear() (front, i = " .. i .. ")")
  277.             return false
  278.         end
  279.  
  280.         -- Move to the digged spot
  281.         if moveForward(1) == false then
  282.             print("digAndMoveForward3Wx1H(): moveForward(1) (i = " .. i .. ")")
  283.             return false
  284.         end
  285.  
  286.         -- Dig the blocks that are to the left and to the right
  287.         if i < len then
  288.             -- Reorient to the forward direction
  289.             if digLeftAndRight(first, "forward") == false then
  290.                 print("digAndMoveForward3Wx1H(): digLeftAndRight(first = " .. first .. ") (i = " .. i .. ")")
  291.                 return false
  292.             end
  293.         else
  294.             -- Last block, reorient to the requested direction
  295.             if digLeftAndRight(first, turn) == false then
  296.                 print("digAndMoveForward3Wx1H(): digLeftAndRight(first = " .. first .. ", turn = " .. turn .. ") (last)")
  297.                 return false
  298.             end
  299.         end
  300.     end
  301.  
  302.     return true
  303. end
  304.  
  305.  
  306. -- Dig a 2 wide, 1 high tunnel (to the right)
  307. function digAndMoveForward2Wrx1H(len, turn)
  308.     if len <= 0 then
  309.         return true
  310.     end
  311.     if turn == nil then
  312.         turn = "forward"
  313.     end
  314.  
  315.     for i = 1, len do
  316.         -- Dig the block directly in front
  317.         if digUntilClear() == false then
  318.             print("digAndMoveForward2Wrx1H(): digUntilClear() (front, i = " .. i .. ")")
  319.             return false
  320.         end
  321.  
  322.         -- Move to the digged spot
  323.         if moveForward(1) == false then
  324.             print("digAndMoveForward2Wrx1H(): moveForward(1) (i = " .. i .. ")")
  325.             return false
  326.         end
  327.  
  328.         -- Dig the block that is to the right
  329.         turnRight()
  330.         if digUntilClear() == false then
  331.             print("digAndMoveForward2Wrx1H(): digUntilClear() (right, i = " .. i .. ")")
  332.             return false
  333.         end
  334.  
  335.         if i < len or turn == "forward" then
  336.             -- Reorient to the forward direction
  337.             turnLeft()
  338.         elseif turn == "backward" then
  339.             -- Turn around (= backwards)
  340.             turnRight()
  341.         -- else: leave facing right
  342.         end
  343.     end
  344.  
  345.     return true
  346. end
  347.  
  348.  
  349. -- Dig a 2 wide, 1 high tunnel (to the left)
  350. function digAndMoveForward2Wlx1H(len, turn)
  351.     if len <= 0 then
  352.         return true
  353.     end
  354.     if turn == nil then
  355.         turn = "forward"
  356.     end
  357.  
  358.     for i = 1, len do
  359.         -- Dig the block directly in front
  360.         if digUntilClear() == false then
  361.             print("digAndMoveForward2Wlx1H(): digUntilClear() (front, i = " .. i .. ")")
  362.             return false
  363.         end
  364.  
  365.         -- Move to the digged spot
  366.         if moveForward(1) == false then
  367.             print("digAndMoveForward2Wlx1H(): moveForward(1) (i = " .. i .. ")")
  368.             return false
  369.         end
  370.  
  371.         -- Dig the block that is to the left
  372.         turnLeft()
  373.         if digUntilClear() == false then
  374.             print("digAndMoveForward2Wlx1H(): digUntilClear() (left, i = " .. i .. ")")
  375.             return false
  376.         end
  377.  
  378.         if i < len or turn == "forward" then
  379.             -- Reorient to the forward direction
  380.             turnRight()
  381.         elseif turn == "backward" then
  382.             -- Turn around (= backwards)
  383.             turnLeft()
  384.         -- else: leave facing left
  385.         end
  386.     end
  387.  
  388.     return true
  389. end
  390.  
  391.  
  392. -- Dig and move forward in a straight line (1 high)
  393. function digAndMoveForward1Wx1H(len)
  394.     if len <= 0 then
  395.         return true
  396.     end
  397.  
  398.     for i = 1, len do
  399.         if digUntilClear() == false then
  400.             print("digAndMoveForward1Wx1H(len = " .. len .. "): digUntilClear() (i = " .. i .. ")")
  401.             return false
  402.         end
  403.  
  404.         if moveForward(1) == false then
  405.             print("digAndMoveForward1Wx1H(len = " .. len .. "): moveForward(1) (i = " .. i .. ")")
  406.             return false
  407.         end
  408.     end
  409.  
  410.     return true
  411. end
  412.  
  413.  
  414. -- Dig and move forward in a straight line (2 high, up)
  415. function digAndMoveForward1Wx2Hu(len)
  416.     if len <= 0 then
  417.         return true
  418.     end
  419.  
  420.     for i = 1, len do
  421.         if digUntilClear() == false then
  422.             print("digAndMoveForward1Wx2Hu(len = " .. len .. "): digUntilClear() (i = " .. i .. ")")
  423.             return false
  424.         end
  425.  
  426.         if moveForward(1) == false then
  427.             print("digAndMoveForward1Wx2Hu(len = " .. len .. "): moveForward(1) (i = " .. i .. ")")
  428.             return false
  429.         end
  430.  
  431.         if digUntilClear("up") == false then
  432.             print("digAndMoveForward1Wx2Hu(len = " .. len .. "): digUntilClear(\"up\") (i = " .. i .. ")")
  433.             return false
  434.         end
  435.     end
  436.  
  437.     return true
  438. end
  439.  
  440.  
  441. -- Dig and move forward in a straight line (2 high, down)
  442. function digAndMoveForward1Wx2Hd(len)
  443.     if len <= 0 then
  444.         return true
  445.     end
  446.  
  447.     for i = 1, len do
  448.         if digUntilClear() == false then
  449.             print("digAndMoveForward1Wx2Hd(len = " .. len .. "): digUntilClear() (i = " .. i .. ")")
  450.             return false
  451.         end
  452.  
  453.         if moveForward(1) == false then
  454.             print("digAndMoveForward1Wx2Hd(len = " .. len .. "): moveForward(1) (i = " .. i .. ")")
  455.             return false
  456.         end
  457.  
  458.         if digUntilClear("down") == false then
  459.             print("digAndMoveForward1Wx2Hd(len = " .. len .. "): digUntilClear(\"down\") (i = " .. i .. ")")
  460.             return false
  461.         end
  462.     end
  463.  
  464.     return true
  465. end
  466.  
  467.  
  468. -- Move to the starting position for 1-wide-dig-to-the-right tunnels
  469. function moveTo1WStartPos()
  470.     -- Corner case: 1x1 area
  471.     if posX < 0 and length == 1 then
  472.         return true
  473.     end
  474.     -- The tunnels go "back and forth" from the starting position/orientation
  475.     if tunnelDirection == 0 then
  476.         -- We only have to move (and turn!) if we have dug something already
  477.         -- ie. if this is the first tunnel and 1 wide, then we should be on the start position already
  478.         if doneZ > 0 then
  479.             reOrient(1)
  480.  
  481.             if digAndMoveForward1Wx1H(doneZ - posZ) == false then
  482.                 print("moveTo1WStartPos(): digAndMoveForward1Wx1H(doneZ - posZ)")
  483.                 return false
  484.             end
  485.         end
  486.  
  487.         -- We are at the starting end of the area
  488.         if posX == 0 then
  489.             reOrient(0)
  490.         elseif posX == (length - 1) then -- We are at the back end of the area
  491.             reOrient(2)
  492.         else
  493.             print("moveTo1WStartPos(): Invalid position while trying to reOrient()")
  494.             return false
  495.         end
  496.     -- The tunnels go "sideways/left and right" from the starting position/orientation
  497.     elseif tunnelDirection == 1 then
  498.         -- We only have to move (and turn!) if we have dug something already
  499.         -- ie. if this is the first tunnel and 1 wide, then we should be on the position already
  500.         if doneX > 0 then
  501.             reOrient(0)
  502.  
  503.             if digAndMoveForward1Wx1H(doneX - posX) == false then
  504.                 print("moveTo1WStartPos(): digAndMoveForward1Wx1H(doneX - posX)")
  505.                 return false
  506.             end
  507.         end
  508.  
  509.         -- We are at the starting end of the area
  510.         if posZ == 0 then
  511.             reOrient(1)
  512.         elseif posZ == (width - 1) then -- We are at the back end of the area
  513.             reOrient(3)
  514.         else
  515.             print("moveTo2WrStartPos(): Invalid position while trying to reOrient()")
  516.             return false
  517.         end
  518.     end
  519.  
  520.     return true
  521. end
  522.  
  523.  
  524. -- Move to the starting position for 2-wide-dig-to-the-right tunnels
  525. function moveTo2WrStartPos()
  526.     -- The tunnels go "back and forth" from the starting position/orientation
  527.     if tunnelDirection == 0 then
  528.         reOrient(1)
  529.  
  530.         -- We are at the starting end of the area
  531.         if posX == 0 then
  532.             if digAndMoveForward1Wx1H(doneZ - posZ) == false then
  533.                 print("moveTo2WrStartPos(): digAndMoveForward1Wx1H(doneZ - posZ)")
  534.                 return false
  535.             end
  536.             -- Dig the block to the right of the start position
  537.             if digUntilClear() == false then
  538.                 print("moveTo2WrStartPos(): digUntilClear() returned false")
  539.                 return false
  540.             end
  541.         elseif posX == (length - 1) then -- We are at the back end of the area
  542.             if digAndMoveForward1Wx1H(doneZ - posZ + 1) == false then
  543.                 print("moveTo2WrStartPos(): digAndMoveForward1Wx1H(doneZ - posZ + 1)")
  544.                 return false
  545.             end
  546.         else
  547.             print("moveTo2WrStartPos(): Invalid position while trying to reOrient()")
  548.             return false
  549.         end
  550.  
  551.         -- We are at the starting end of the area
  552.         if posX == 0 then
  553.             reOrient(0)
  554.         elseif posX == (length - 1) then -- We are at the back end of the area
  555.             reOrient(2)
  556.         else
  557.             print("moveTo2WrStartPos(): Invalid position while trying to reOrient()")
  558.             return false
  559.         end
  560.     -- The tunnels go "sideways/left and right" from the starting position/orientation
  561.     elseif tunnelDirection == 1 then
  562.         reOrient(0)
  563.  
  564.         -- We are at the starting end of the area
  565.         if posZ == 0 then
  566.             if digAndMoveForward1Wx1H(doneX - posX + 1) == false then
  567.                 print("moveTo2WrStartPos(): digAndMoveForward1Wx1H(doneX - posX + 1)")
  568.                 return false
  569.             end
  570.             reOrient(1)
  571.         elseif posZ == (width - 1) then -- We are at the back end of the area
  572.             if digAndMoveForward1Wx1H(doneX - posX) == false then
  573.                 print("moveTo2WrStartPos(): digAndMoveForward1Wx1H(doneX - posX)")
  574.                 return false
  575.             end
  576.             -- Dig the block to the right of the start position
  577.             if digUntilClear() == false then
  578.                 print("moveTo2WrStartPos(): digUntilClear() returned false")
  579.                 return false
  580.             end
  581.             reOrient(3)
  582.         else
  583.             print("moveTo2WrStartPos(): Invalid position while trying to reOrient()")
  584.             return false
  585.         end
  586.     end
  587.  
  588.     return true
  589. end
  590.  
  591.  
  592. -- Move to the starting position for 2-wide-dig-to-the-left tunnels
  593. function moveTo2WlStartPos()
  594.     -- The tunnels go "back and forth" from the starting position/orientation
  595.     if tunnelDirection == 0 then
  596.         reOrient(1)
  597.  
  598.         -- We are at the starting end of the area
  599.         if posX == 0 then
  600.             if digAndMoveForward1Wx1H(doneZ - posZ + 1) == false then
  601.                 print("moveTo2WlStartPos(): digAndMoveForward1Wx1H(doneZ - posZ + 1)")
  602.                 return false
  603.             end
  604.         elseif posX == (length - 1) then -- We are at the back end of the area
  605.             if digAndMoveForward1Wx1H(doneZ - posZ) == false then
  606.                 print("moveTo2WlStartPos(): digAndMoveForward1Wx1H(doneZ - posZ)")
  607.                 return false
  608.             end
  609.             -- Dig the block to the left of the start position
  610.             if digUntilClear() == false then
  611.                 print("moveTo2WlStartPos(): digUntilClear() returned false")
  612.                 return false
  613.             end
  614.         else
  615.             print("moveTo2WlStartPos(): Invalid position while trying to reOrient()")
  616.             return false
  617.         end
  618.  
  619.         -- We are at the starting end of the area
  620.         if posX == 0 then
  621.             reOrient(0)
  622.         elseif posX == (length - 1) then -- We are at the back end of the area
  623.             reOrient(2)
  624.         else
  625.             print("moveTo2WlStartPos(): Invalid position while trying to reOrient()")
  626.             return false
  627.         end
  628.     -- The tunnels go "sideways/left and right" from the starting position/orientation
  629.     elseif tunnelDirection == 1 then
  630.         reOrient(0)
  631.  
  632.         -- We are at the starting end of the area
  633.         if posZ == 0 then
  634.             if digAndMoveForward1Wx1H(doneX - posX) == false then
  635.                 print("moveTo2WlStartPos(): digAndMoveForward1Wx1H(doneX - posX)")
  636.                 return false
  637.             end
  638.             -- Dig the block to the left of the start position
  639.             if digUntilClear() == false then
  640.                 print("moveTo2WlStartPos(): digUntilClear() returned false")
  641.                 return false
  642.             end
  643.             reOrient(1)
  644.         elseif posZ == (width - 1) then -- We are at the back end of the area
  645.             if digAndMoveForward1Wx1H(doneX - posX + 1) == false then
  646.                 print("moveTo2WlStartPos(): digAndMoveForward1Wx1H(doneX - posX + 1)")
  647.                 return false
  648.             end
  649.             reOrient(3)
  650.         else
  651.             print("moveTo2WlStartPos(): Invalid position while trying to reOrient()")
  652.             return false
  653.         end
  654.     end
  655.  
  656.     return true
  657. end
  658.  
  659.  
  660. -- Move to the starting position for 3-wide tunnels
  661. function moveTo3WStartPos()
  662.     -- The tunnels go "back and forth" from the starting position/orientation
  663.     if tunnelDirection == 0 then
  664.         reOrient(1)
  665.  
  666.         if digAndMoveForward1Wx1H(doneZ - posZ + 1) == false then
  667.             print("moveTo3WStartPos(): digAndMoveForward1Wx1H(doneZ - posZ + 1)")
  668.             return false
  669.         end
  670.         -- Dig the block to the right of the start position
  671.         if digUntilClear() == false then
  672.             print("moveTo2WrStartPos(): digUntilClear() returned false")
  673.             return false
  674.         end
  675.  
  676.         -- We are at the starting end of the area
  677.         if posX == 0 then
  678.             reOrient(0)
  679.         elseif posX == (length - 1) then -- We are at the back end of the area
  680.             reOrient(2)
  681.         else
  682.             print("moveTo3WStartPos(): Invalid position while trying to reOrient()")
  683.             return false
  684.         end
  685.     -- The tunnels go "sideways/left and right" from the starting position/orientation
  686.     elseif tunnelDirection == 1 then
  687.         reOrient(0)
  688.  
  689.         if digAndMoveForward1Wx1H(doneX - posX + 1) == false then
  690.             print("moveTo3WStartPos(): digAndMoveForward1Wx1H(doneX - posX + 1)")
  691.             return false
  692.         end
  693.         -- Dig the block to the right of the start position
  694.         if digUntilClear() == false then
  695.             print("moveTo2WrStartPos(): digUntilClear() returned false")
  696.             return false
  697.         end
  698.  
  699.         -- We are at the starting end of the area
  700.         if posZ == 0 then
  701.             reOrient(1)
  702.         elseif posZ == (width - 1) then -- We are at the back end of the area
  703.             reOrient(3)
  704.         else
  705.             print("moveTo3WStartPos(): Invalid position while trying to reOrient()")
  706.             return false
  707.         end
  708.     end
  709.  
  710.     return true
  711. end
  712.  
  713.  
  714. -- Select the first slot so that the items get filled to the inventory starting from the first slot
  715. turtle.select(1)
  716.  
  717. -- Corner case check: only move if the area is bigger than 1x1.
  718. -- With 1x1 areas, the block gets dug in the end of the 1-wide tunneling phase.
  719. if length > 1 then
  720.     if digAndMoveForward1Wx1H(1) == false then
  721.         print("digAndMoveForward1Wx1H(1): Error while trying to move to the starting corner of the area")
  722.         return false
  723.     end
  724. end
  725.  
  726. -- We prefer to dig the tunnels in the longer dimension's direction, to avoid zig-zag
  727. if length >= width then
  728.     tunnelDirection = 0
  729.     -- Dig tunnels until the requested width has been dug
  730.     -- First, dig as many 3-wide tunnels as we can
  731.     while (width - doneZ) >= 3 do
  732.         if moveTo3WStartPos() == false then
  733.             print("Area: dir: 0; 3-wides; moveTo3WStartPos()")
  734.             return false
  735.         end
  736.         if posX == 0 then
  737.             if digAndMoveForward3Wx1H(length - 1, "left", "right") == false then
  738.                 print("Area: dir: 0; 3-wides; digAndMoveForward3Wx1H(" .. (length - 1) .. ", \"left\", \"right\")")
  739.                 return false
  740.             end
  741.         elseif posX == (length - 1) then
  742.             if digAndMoveForward3Wx1H(length - 1, "right", "left") == false then
  743.                 print("Area: dir: 0; 3-wides; digAndMoveForward3Wx1H(" .. (length - 1) .. ", \"right\", \"left\")")
  744.                 return false
  745.             end
  746.         else
  747.             print("Error: Invalid position while digging 3-wide tunnels")
  748.             return false
  749.         end
  750.  
  751.         doneZ = doneZ + 3
  752.     end
  753.     -- Then dig 0 or 1 2-wide tunnels
  754.     while (width - doneZ) >= 2 do
  755.         if posX == 0 then
  756.             if moveTo2WrStartPos() == false then
  757.                 print("Area: dir: 0; 2-wides; moveTo2WrStartPos()")
  758.                 return false
  759.             end
  760.             if digAndMoveForward2Wrx1H(length - 1, "right") == false then
  761.                 print("Area: dir: 0; 2-wides; digAndMoveForward2Wrx1H(" .. (length - 1) .. ", \"right\")")
  762.                 return false
  763.             end
  764.         elseif posX == (length - 1) then
  765.             if moveTo2WlStartPos() == false then
  766.                 print("Area: dir: 0; 2-wides; moveTo2WlStartPos()")
  767.                 return false
  768.             end
  769.             if digAndMoveForward2Wlx1H(length - 1, "left") == false then
  770.                 print("Area: dir: 0; 2-wides; digAndMoveForward2Wlx1H(" .. (length - 1) .. ", \"left\")")
  771.                 return false
  772.             end
  773.         else
  774.             print("Error: Invalid position while digging 2-wide tunnels")
  775.             return false
  776.         end
  777.  
  778.         doneZ = doneZ + 2
  779.     end
  780.     -- And finally dig 0 or 1 1-wide tunnels
  781.     while (width - doneZ) > 0 do
  782.         -- The same function for the start position as the 2Wr
  783.         if moveTo1WStartPos() == false then
  784.             print("Area: dir: 0; 1-wides; moveTo1WStartPos()")
  785.             return false
  786.         end
  787.         if digAndMoveForward1Wx1H(length - 2) == false then
  788.             print("Area: dir: 0; 1-wides; digAndMoveForward1Wx1H(" .. (length - 1) .. ")")
  789.             return false
  790.         end
  791.         if digUntilClear() == false then
  792.             print("Error: digUntilClear() returned false (while digging the last block)")
  793.             return false
  794.         end
  795.         doneZ = doneZ + 1
  796.     end
  797. else
  798.     tunnelDirection = 1
  799.     -- Dig tunnels until the requested length has been dug (we are digging the tunnels "sideways",
  800.     -- because the requested width is larger than the length)
  801.     -- First, dig as many 3-wide tunnels as we can
  802.     while (length - doneX) >= 3 do
  803.         if moveTo3WStartPos() == false then
  804.             print("Area: dir: 1; 3-wides; moveTo3WStartPos()")
  805.             return false
  806.         end
  807.         if posZ == 0 then
  808.             if digAndMoveForward3Wx1H(width - 1, "right", "left") == false then
  809.                 print("Area: dir: 1; 3-wides; digAndMoveForward3Wx1H(" .. (width - 1) .. ", \"right\", \"left\")")
  810.                 return false
  811.             end
  812.         elseif posZ == (width - 1) then
  813.             if digAndMoveForward3Wx1H(width - 1, "left", "right") == false then
  814.                 print("Area: dir: 1; 3-wides; digAndMoveForward3Wx1H(" .. (width - 1) .. ", \"left\", \"right\")")
  815.                 return false
  816.             end
  817.         else
  818.             print("Error: Invalid position while digging 3-wide tunnels")
  819.             return false
  820.         end
  821.         doneX = doneX + 3
  822.     end
  823.     -- Then dig 0 or 1 2-wide tunnels
  824.     while (length - doneX) >= 2 do
  825.         if posZ == 0 then
  826.             if moveTo2WlStartPos() == false then
  827.                 print("Area: dir: 1; 2-wides; moveTo2WlStartPos()")
  828.                 return false
  829.             end
  830.             if digAndMoveForward2Wlx1H(width - 1, "left") == false then
  831.                 print("Area: dir: 1; 2-wides; digAndMoveForward2Wlx1H(" .. (width - 1) ..", \"left\")")
  832.                 return false
  833.             end
  834.         elseif posZ == (width - 1) then
  835.             if moveTo2WrStartPos() == false then
  836.                 print("Area: dir: 1; 2-wides; moveTo2WrStartPos()")
  837.                 return false
  838.             end
  839.             if digAndMoveForward2Wrx1H(width - 1, "right") == false then
  840.                 print("Area: dir: 1; 2-wides; digAndMoveForward2Wrx1H(" .. (width - 1) .. ", \"right\")")
  841.                 return false
  842.             end
  843.         else
  844.             print("Error: Invalid orientation while digging 2-wide tunnels")
  845.             return false
  846.         end
  847.         doneX = doneX + 2
  848.     end
  849.     -- And finally dig 0 or 1 1-wide tunnels
  850.     while (length - doneX) > 0 do
  851.         -- The same function for the start position as the 2Wr
  852.         if moveTo1WStartPos() == false then
  853.             print("Area: dir: 1; 1-wides; moveTo1WStartPos()")
  854.             return false
  855.         end
  856.         if digAndMoveForward1Wx1H(width - 2) == false then
  857.             print("Area: dir: 1; 1-wides; digAndMoveForward1Wx1H(" .. (width - 1) .. ")")
  858.             return false
  859.         end
  860.         if digUntilClear() == false then
  861.             print("Error: digUntilClear() returned false (while digging the last block)")
  862.             return false
  863.         end
  864.         doneX = doneX + 1
  865.     end
  866. end
  867.  
  868.  
  869. function moveToX(X)
  870.     if X < posX then
  871.         reOrient(2)
  872.         if digAndMoveForward1Wx1H(posX - X) == false then
  873.             print("moveToX(): digAndMoveForward1Wx1H(posX - X = " .. (posX - X) .. ")")
  874.             return false
  875.         end
  876.     elseif posX < X then
  877.         reOrient(0)
  878.         if digAndMoveForward1Wx1H(X - posX) == false then
  879.             print("moveToX(): digAndMoveForward1Wx1H(X - posX = " .. (X - posX) .. ")")
  880.             return false
  881.         end
  882.     end
  883.  
  884.     return true
  885. end
  886.  
  887.  
  888. function moveToZ(Z)
  889.     if Z < posZ then
  890.         reOrient(3)
  891.         if digAndMoveForward1Wx1H(posZ - Z) == false then
  892.             print("moveToZ(): digAndMoveForward1Wx1H(posZ - Z = " .. (posZ - Z) .. ")")
  893.             return false
  894.         end
  895.     elseif posZ < Z then
  896.         reOrient(1)
  897.         if digAndMoveForward1Wx1H(Z - posZ) == false then
  898.             print("moveToZ(): digAndMoveForward1Wx1H(Z - posZ = " .. (Z - posZ) .. ")")
  899.             return false
  900.         end
  901.     end
  902.  
  903.     return true
  904. end
  905.  
  906. if length > 1 or width > 1 then
  907.     -- Return to the starting corner
  908.     if orientation == 0 or orientation == 3 then
  909.         moveToZ(0)
  910.         moveToX(0)
  911.     else
  912.         moveToX(0)
  913.         moveToZ(0)
  914.     end
  915. end
  916.  
  917.  
  918. if posX == 0 then
  919.     reOrient(2)
  920.     if digAndMoveForward1Wx1H(1) == false then
  921.         print("digAndMoveForward1Wx1H(1): Error while trying to move to the starting corner of the area")
  922.         return false
  923.     end
  924. elseif posX ~= -1 then
  925.     print("Error: Wrong position while trying to move to the starting spot")
  926.     return false
  927. end
  928.  
  929. if dumpinventory == "true" then
  930. --  reOrient(2)
  931.     -- If there is a block behind the starting position, assume it is a chest
  932.     -- and dump the inventory to it.
  933.     if turtle.detect() then
  934.         for i = 1, 16 do
  935.             turtle.select(i)
  936.             turtle.drop()
  937. --          if turtle.drop() == false then
  938. --              break
  939. --          end
  940.         end
  941.     end
  942. end
  943.  
  944. -- And finally reorient
  945. reOrient(0)
  946.  
  947. -- And print a summary
  948. print("Done.")
  949. print("Mined " .. numBlocks .. " blocks.")
  950. print("Moved a total of " .. distanceTraveled .. " blocks.")
  951. print("Attacked mobs " .. numAttacks .. " times.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement