Advertisement
masa-

CC Turtle: Digger Vertical v0.1.0a1

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