Advertisement
masa-

CC Turtle: Digger (LxWxH) v0.5.0a1

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