SHARE
TWEET

tunnel [OpenComputers]

Doob Aug 1st, 2015 348 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local robot = require('robot')
  2. local computer = require('component').computer
  3.  
  4. local tArgs = {...}
  5.  
  6. if #tArgs ~= 1 then
  7.   print("Usage: tunnel <length>")
  8.   return
  9. end
  10.  
  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 robot.detect() do
  29.     if robot.swing() then
  30.       collect()
  31.       os.sleep(0.5)
  32.     else
  33.       return false
  34.     end
  35.   end
  36.   return true
  37. end
  38.  
  39. local function tryDigUp()
  40.   while robot.detectUp() do
  41.     if robot.swingUp() then
  42.       collect()
  43.       os.sleep(0.5)
  44.     else
  45.       return false
  46.     end
  47.   end
  48.   return true
  49. end
  50.  
  51. local function tryDigDown()
  52.   while robot.detectDown() do
  53.     if robot.swingDown() then
  54.       collect()
  55.       os.sleep(0.5)
  56.     else
  57.       return false
  58.     end
  59.   end
  60.   return true
  61. end
  62.  
  63. local function tryUp()
  64.   while not robot.up() do
  65.     if robot.detectUp() then
  66.       if not tryDigUp() then
  67.         return false
  68.       end
  69.     elseif robot.swingkUp() then
  70.       collect()
  71.     else
  72.       os.sleep(0.5)
  73.     end
  74.   end
  75.   return true
  76. end
  77.  
  78. local function tryDown()
  79.   while not robot.down() do
  80.     if robot.detectDown() then
  81.       if not tryDigDown() then
  82.         return false
  83.       end
  84.     elseif robot.swingDown() then
  85.       collect()
  86.     else
  87.       os.sleep(0.5)
  88.     end
  89.   end
  90.   return true
  91. end
  92.  
  93. local function tryForward()
  94.   while not robot.forward() do
  95.     if robot.detect() then
  96.       if not tryDig() then
  97.         return false
  98.       end
  99.     elseif robot.attack() then
  100.       collect()
  101.     else
  102.       os.sleep(0.5)
  103.     end
  104.   end
  105.   return true
  106. end
  107.  
  108. print("Tunnelling...")
  109.  
  110. for n=1,length do
  111.   robot.placeDown()
  112.   tryDigUp()
  113.   robot.turnLeft()
  114.   tryDig()
  115.   tryUp()
  116.   tryDig()
  117.   robot.turnRight()
  118.   robot.turnRight()
  119.   tryDig()
  120.   tryDown()
  121.   tryDig()
  122.   robot.turnLeft()
  123.  
  124.   if n<length then
  125.     tryDig()
  126.     if not tryForward() then
  127.       print("Aborting Tunnel.")
  128.       break
  129.     end
  130.   else
  131.     print("Tunnel complete.")
  132.   end
  133.  
  134. end
  135.  
  136. print("Tunnel complete.")
  137. print("Mined "..collected.." items total.")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top