Advertisement
masa-

CC Turtle: Digger (LxWxH) v0.4.0a2

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