Lost_NInja

Turtle Counter

Dec 17th, 2012
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.40 KB | None | 0 0
  1. -- Simple program that mines forward along a 1x2 passageway until
  2. -- it detects a block corresponding to the block in (tstslt) then it returns
  3. -- to the start. It then outputs the number of blocks to the required block
  4. -- v03
  5. -- by LN
  6.  
  7. local travld = 0 -- How many blocks we travel
  8. local dist = 0 -- As above
  9. local done = 0 -- when 1 indicates block matching tstslt matched
  10. local job = "Counter"
  11. local tstslt = 16 -- Slot to compare against (1-16)
  12.  
  13. local function refuel()
  14.     local fuelLevel = turtle.getFuelLevel()
  15.     if fuelLevel == "unlimited" or fuelLevel > 0 then
  16.         return
  17.     end
  18.    
  19.     local function tryRefuel()
  20.         for n=1,16 do
  21.             if turtle.getItemCount(n) > 0 then
  22.                 turtle.select(n)
  23.                 if turtle.refuel(1) then
  24.                     turtle.select(1)
  25.                     return true
  26.                 end
  27.             end
  28.         end
  29.         turtle.select(1)
  30.         return false
  31.     end
  32.    
  33.     if not tryRefuel() then
  34.         print( "Add more fuel to continue." )
  35.         while not tryRefuel() do
  36.             sleep(1)
  37.         end
  38.         print( "Resuming..." ..job)
  39.     end
  40. end
  41.  
  42. local function tryDigDown()
  43.     while turtle.detectDown() do
  44.         turtle.digDown()
  45.         sleep(0.5)
  46.     end
  47. end
  48.  
  49. local function tryDig()
  50.     while turtle.detect() do
  51.         turtle.dig()
  52.         sleep(0.5)
  53.     end
  54. end
  55.  
  56. local function tryForward()
  57.     refuel()
  58.     while not turtle.forward() do
  59.         if turtle.detect() then
  60.             if not tryDig() then
  61.                 return false
  62.             end
  63.         elseif turtle.attack() then
  64.         else
  65.             sleep( 0.5 )
  66.         end
  67.     end
  68.     return true
  69. end
  70.  
  71. local function testDown()
  72. end
  73.  
  74. local function reverse()
  75.     turtle.turnLeft()
  76.     turtle.turnLeft()
  77. end
  78.  
  79. local function travel()
  80.     while done ~= 1 do
  81.         turtle.select(16)
  82.         if turtle.compareDown() then
  83.             done = 1
  84. --          travld = travld + 1
  85.             print("True")
  86.         else
  87.             done = 0
  88.             print("False")
  89.             if (done == 0) then
  90.                 tryDigDown()
  91.                 tryDig()
  92.                 tryForward()
  93.                 travld = travld + 1
  94.             end
  95.         end
  96.     turtle.select(1)
  97.     print("Done = " .. done)
  98.     end
  99. end
  100.  
  101. local function rtb()
  102. reverse()
  103. dist = travld
  104. while dist > 0 do
  105.     tryForward()
  106.     dist = dist - 1
  107. end
  108. reverse()
  109. end
  110.  
  111. -- Program...
  112.  
  113.    
  114. travel()
  115. rtb()
  116. print("This Turtle Counted a total of: " .. travld .. " blocks before RTB.")
Advertisement
Add Comment
Please, Sign In to add comment