mdc_tjc

mc_tunnel

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