Advertisement
jhnphm

TurtleLight

Feb 23rd, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.30 KB | None | 0 0
  1.  
  2.    
  3. local depth = 0
  4. local collected = 0
  5. local slot
  6.  
  7. local function dump()
  8.     turtle.dig()
  9.     turtle.select(14)
  10.     turtle.place()
  11.     for i=1,12 do
  12.         turtle.select(i)
  13.         turtle.drop()
  14.     end
  15.     turtle.select(14)
  16.     turtle.dig()
  17. end
  18.  
  19. local function collect()
  20.     collected = collected + 1
  21.     if math.fmod(collected, 25) == 0 then
  22.         print( "Mined "..collected.." items." )
  23.     end
  24.     if math.fmod(collected, 128) == 0 then
  25.         dump()
  26.     end
  27. end
  28.  
  29. local function tryDig()
  30.     while turtle.detect() do
  31.         if turtle.dig() then
  32.             collect()
  33.             --sleep(0.5)
  34.         else
  35.             return false
  36.         end
  37.     end
  38.     return true
  39. end
  40.  
  41. local function tryDigUp()
  42.     while turtle.detectUp() do
  43.         if turtle.digUp() then
  44.             collect()
  45.             --sleep(0.5)
  46.         else
  47.             return false
  48.         end
  49.     end
  50.     return true
  51. end
  52.  
  53. local function tryDigDown()
  54.     while turtle.detectDown() do
  55.         if turtle.digDown() then
  56.             collect()
  57.             --sleep(0.5)
  58.         else
  59.             return false
  60.         end
  61.     end
  62.     return true
  63. end
  64.  
  65. local function refuel()
  66.     local fuelLevel = turtle.getFuelLevel()
  67.     if fuelLevel == "unlimited" or fuelLevel > 0 then
  68.         return
  69.     end
  70.    
  71.     local function tryRefuel()
  72.         turtle.dig()
  73.         turtle.select(16)
  74.         turtle.place()
  75.         turtle.suck()
  76.         if turtle.refuel() then
  77.             turtle.dig()
  78.             return true
  79.         else
  80.             turtle.dig()
  81.             return false
  82.         end
  83.     end
  84.    
  85.     if not tryRefuel() then
  86.         print( "Add more fuel to continue." )
  87.         while not tryRefuel() do
  88.             sleep(1)
  89.         end
  90.         print( "Resuming Tunnel." )
  91.     end
  92. end
  93.  
  94. local function tryUp()
  95.     refuel()
  96.     while not turtle.up() do
  97.         if turtle.detectUp() then
  98.             if not tryDigUp() then
  99.                 return false
  100.             end
  101.         elseif turtle.attackUp() then
  102.             collect()
  103.         else
  104.             sleep( 0.5 )
  105.         end
  106.     end
  107.     return true
  108. end
  109.  
  110. local function tryDown()
  111.     refuel()
  112.     while not turtle.down() do
  113.         if turtle.detectDown() then
  114.             if not tryDigDown() then
  115.                 return false
  116.             end
  117.         elseif turtle.attackDown() then
  118.             collect()
  119.         else
  120.             sleep( 0.5 )
  121.         end
  122.     end
  123.     return true
  124. end
  125.  
  126. local function tryForward()
  127.     refuel()
  128.     while not turtle.forward() do
  129.         if turtle.detect() then
  130.             if not tryDig() then
  131.                 return false
  132.             end
  133.         elseif turtle.attack() then
  134.             collect()
  135.         else
  136.             sleep( 0.5 )
  137.         end
  138.     end
  139.     return true
  140. end
  141.  
  142.  
  143. for i=1,47-1 do
  144.     if i % 4 == 0 then
  145.         turtle.turnLeft()
  146.         for j=1,47-1 do
  147.             tryForward()
  148.             if j % 4 == 0 then
  149.                 tryDigDown()
  150.                 for k = 1,16 do
  151.                     if turtle.getItemCount(k) > 0 then
  152.                         turtle.select(k)
  153.                         break
  154.                     end
  155.                 end
  156.                 turtle.placeDown()
  157.             end
  158.         end
  159.         turtle.turnRight()
  160.         turtle.turnRight()
  161.         for j=1,47-1 do
  162.             tryForward()
  163.         end
  164.         turtle.turnLeft()
  165.     end
  166.     tryForward()
  167. end
  168.  
  169.  
  170. print( "Tunnel complete." )
  171. --[[
  172. print( "Returning to start..." )
  173.  
  174. -- Return to where we started
  175. turtle.turnLeft()
  176. turtle.turnLeft()
  177. while depth > 0 do
  178.     if turtle.forward() then
  179.         depth = depth - 1
  180.     else
  181.         turtle.dig()
  182.     end
  183. end
  184. turtle.turnRight()
  185. turtle.turnRight()
  186. ]]
  187.  
  188. print( "Tunnel complete." )
  189. print( "Mined "..collected.." items total." )
  190. -- vim: set ft=lua:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement