Advertisement
bob558

3x3-m

Aug 4th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  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.  
  22. collected = collected + 1
  23. if math.fmod(collected, 25) == 0 then
  24. print("Mined "..collected.." items.")
  25. end
  26. end
  27.  
  28. local function tryDig()
  29. while robot.detect() do
  30. if robot.swing() then
  31. collect()
  32. os.sleep(0.5)
  33. else
  34. return false
  35. end
  36. end
  37. return true
  38. end
  39.  
  40. local function tryDigUp()
  41. while robot.detectUp() do
  42. if robot.swingUp() then
  43. collect()
  44. os.sleep(0.5)
  45. else
  46. return false
  47. end
  48. end
  49. return true
  50. end
  51.  
  52. local function tryDigDown()
  53. while robot.detectDown() do
  54. if robot.swingDown() then
  55. collect()
  56. os.sleep(0.5)
  57. else
  58. return false
  59. end
  60. end
  61. return true
  62. end
  63.  
  64. local function tryUp()
  65. while not robot.up() do
  66. if robot.detectUp() then
  67. if not tryDigUp() then
  68. return false
  69. end
  70. elseif robot.swingkUp() then
  71. collect()
  72. else
  73. os.sleep(0.5)
  74. end
  75. end
  76. return true
  77. end
  78.  
  79. local function tryDown()
  80. while not robot.down() do
  81. if robot.detectDown() then
  82. if not tryDigDown() then
  83. return false
  84. end
  85. elseif robot.swingDown() then
  86. collect()
  87. else
  88. os.sleep(0.5)
  89. end
  90. end
  91. return true
  92. end
  93.  
  94. local function tryForward()
  95. while not robot.forward() do
  96. if robot.detect() then
  97. if not tryDig() then
  98. return false
  99. end
  100. elseif robot.attack() then
  101. collect()
  102. else
  103. os.sleep(0.5)
  104. end
  105. end
  106. return true
  107. end
  108.  
  109. print("Tunnelling...")
  110.  
  111. for n=1,length do
  112. robot.placeDown()
  113. tryDigUp()
  114. robot.turnLeft()
  115. tryDig()
  116. tryUp()
  117. tryDig()
  118. robot.turnRight()
  119. robot.turnRight()
  120. tryDig()
  121. tryDown()
  122. tryDig()
  123. robot.turnLeft()
  124.  
  125. if n<length then
  126. tryDig()
  127. if not tryForward() then
  128. print("Aborting Tunnel.")
  129. break
  130. end
  131. else
  132. print("Tunnel complete.")
  133. end
  134.  
  135. end
  136.  
  137. print("Tunnel complete.")
  138. print("Mined "..collected.." items total.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement