Advertisement
masa-

CC Turtle: Digger (3Wx1H) v0.2

Feb 16th, 2013
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.07 KB | None | 0 0
  1. local args = { ... }
  2.  
  3. local fl = turtle.getFuelLevel()
  4. local dumpinventory = false
  5.  
  6. if #args < 1 then
  7.     print("Usage: digger3Wx1H <length> [dump inventory when done (true|false)]")
  8.     return
  9. end
  10.  
  11. local length = tonumber( args[1] )
  12.  
  13. if #args == 2 then
  14.     dumpinventory = args[2]
  15. end
  16.  
  17. if length < 1 then
  18.     print("Error: Length must be at least 1")
  19.     return
  20. end
  21.  
  22. if fl < ((length - 1) * 2) then
  23.     print("Error: Not enough fuel to complete the action (need at least " .. ((length - 1) * 2) .. ")")
  24.     return
  25. end
  26.  
  27. local posX = 0
  28. local distanceTraveled = 0
  29. local numBlocks = 0
  30. local numAttacks = 0
  31.  
  32.  
  33. -- Dig the block in front of the turtle until no block is detected anymore
  34. function digUntilClear()
  35.     -- If there is a block in front, dig it. Loop in case of falling sand/gravel.
  36.     while turtle.detect() == true do
  37.         if turtle.dig() == true then
  38.             numBlocks = numBlocks + 1
  39.         else
  40.             print("Error: Could not dig the block! Aborting...")
  41.             return false
  42.         end
  43.         sleep(0.5)
  44.     end
  45.  
  46.     return true
  47. end
  48.  
  49.  
  50. -- Attack as long as the attack succeeds (= mobs in front)
  51. function attackUntilClear()
  52.     local tmp1 = 0
  53.  
  54.     -- Attack if there are mobs in front of the turtle
  55.     while turtle.attack() == true do
  56.         numAttacks = numAttacks + 1
  57.         -- Failsafe limit
  58.         tmp1 = tmp1 + 1
  59.         if tmp1 > 100 then
  60.             print("Error: Hit the failsafe limit (100) of attacks!")
  61.             return false
  62.         end
  63.     end
  64.  
  65.     return true
  66. end
  67.  
  68. -- Move forward one block
  69. function moveForward()
  70.     local tmp2 = 0
  71.  
  72.     -- Attack while we can't move forward (because someONE is blocking us)
  73.     while turtle.forward() == false do
  74.         if attackUntilClear() == false then
  75.             return false
  76.         end
  77.  
  78.         -- Failsafe limit
  79.         tmp2 = tmp2 + 1
  80.         if tmp2 > 100 then
  81.             print("Error: Hit the failsafe limit (100) of trying to move")
  82.             return false
  83.         end
  84.     end
  85.  
  86.     return true
  87. end
  88.  
  89.  
  90. -- Dig a 3 wide area and move forward
  91. function digAndMoveForward3W(fw)
  92.     -- Dig the block in front
  93.     if digUntilClear() == false then
  94.         return false
  95.     end
  96.  
  97.     -- Move to the digged spot
  98.     if moveForward() == false then
  99.         return false
  100.     end
  101.  
  102.     -- Dig the blocks that are to the left and to the right
  103.     turtle.turnLeft()
  104.     if digUntilClear() == false then
  105.         return false
  106.     end
  107.  
  108.     turtle.turnRight()
  109.     turtle.turnRight()
  110.     if digUntilClear() == false then
  111.         return false
  112.     end
  113.  
  114.     if fw == true then
  115.         -- Turn back to the forward direction
  116.         turtle.turnLeft()
  117.     else
  118.         -- Turn around (= backwards)
  119.         turtle.turnRight()
  120.     end
  121.  
  122.     return true
  123. end
  124.  
  125.  
  126. -- (Dig and) move forward in a straight line (used for the return trip)
  127. function digAndMoveForward1W()
  128.     if digUntilClear() == false then
  129.         return false
  130.     end
  131.  
  132.     if moveForward() == false then
  133.         return false
  134.     end
  135.  
  136.     return true
  137. end
  138.  
  139. -- Select the first slot so that the items get filled to the inventory starting from the first slot
  140. turtle.select(1)
  141.  
  142.  
  143. -- Dig and move forward for every block except the last one
  144. for i = 1, (length - 1) do
  145.     if digAndMoveForward3W(true) == false then
  146.         break
  147.     end
  148.  
  149.     posX = posX + 1
  150.     distanceTraveled = distanceTraveled + 1
  151. end
  152.  
  153.  
  154. -- After the last row of blocks, we orient backwards for the return trip
  155. if digAndMoveForward3W(false) == true then
  156.     posX = posX + 1
  157.     distanceTraveled = distanceTraveled + 1
  158. end
  159.  
  160.  
  161. -- Return back to the starting position
  162. for i = 1, posX do
  163.     if digAndMoveForward1W() == false then
  164.         print("Error: Got stuck at distance " .. distanceTraveled)
  165.         print("  blocks from start position while returning.")
  166.         return
  167.     end
  168.     posX = posX - 1
  169.     distanceTraveled = distanceTraveled + 1
  170. end
  171.  
  172. if dumpinventory == "true" then
  173.     -- If there is a block behind the starting position, assume it is a chest
  174.     -- and dump the inventory to it.
  175.     if turtle.detect() then
  176.         for i = 1, 16 do
  177.             turtle.select(i)
  178.             turtle.drop()
  179. --          if turtle.drop() == false then
  180. --              break
  181. --          end
  182.         end
  183.     end
  184. end
  185.  
  186. -- And finally reorient
  187. turtle.turnRight()
  188. turtle.turnRight()
  189.  
  190. -- And print a summary
  191. print("Done.")
  192. print("Mined " .. numBlocks .. " blocks.")
  193. print("Moved a total of " .. distanceTraveled .. " blocks.")
  194. print("Attacked mobs " .. numAttacks .. " times.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement