Advertisement
BIL123123

dig 3x3

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