Advertisement
masa-

CC Turtle: Digger (LxWxH) v0.5.1a1

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