Advertisement
masa-

CC Turtle: Digger (1x1) v0.2

Feb 16th, 2013
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.25 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: digger1x1 <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. function digAndMoveForward1W()
  90.     if digUntilClear() == false then
  91.         return false
  92.     end
  93.  
  94.     if moveForward() == false then
  95.         return false
  96.     end
  97.  
  98.     return true
  99. end
  100.  
  101. -- Select the first slot so that the items get filled to the inventory starting from the first slot
  102. turtle.select(1)
  103.  
  104. -- Dig and move forward for every block except the last one
  105. for i = 1, (length - 1) do
  106.     if digAndMoveForward1W() == false then
  107.         break
  108.     end
  109.  
  110.     posX = posX + 1
  111.     distanceTraveled = distanceTraveled + 1
  112. end
  113.  
  114. -- Just dig the last block
  115. digUntilClear()
  116.  
  117. -- Return back to the starting position
  118. turtle.turnRight()
  119. turtle.turnRight()
  120.  
  121. for i = 1, posX do
  122.     if digAndMoveForward1W() == false then
  123.         print("Error: Got stuck at distance " .. distanceTraveled)
  124.         print("  blocks from start position while returning.")
  125.         return
  126.     end
  127.     posX = posX - 1
  128.     distanceTraveled = distanceTraveled + 1
  129. end
  130.  
  131. if dumpinventory == true then
  132.     -- If there is a block behind the starting position, assume it is a chest
  133.     -- and dump the inventory to it.
  134.     if turtle.detect() then
  135.         for i = 1, 16 do
  136.             turtle.select(i)
  137.             turtle.drop()
  138. --          if turtle.drop() == false then
  139. --              break
  140. --          end
  141.         end
  142.     end
  143. end
  144.  
  145. -- And finally reorient
  146. turtle.turnRight()
  147. turtle.turnRight()
  148.  
  149. -- And print a summary
  150. print("Done.")
  151. print("Mined " .. numBlocks .. " blocks.")
  152. print("Moved a total of " .. distanceTraveled .. " blocks.")
  153. print("Attacked mobs " .. numAttacks .. " times.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement