naschorr

mine-return

May 16th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. -- Attempt to refuel the turtle
  2. local function attemptRefuel()
  3. if(turtle.getFuelLevel() >= 1) then
  4. return
  5. end
  6.  
  7. local index = 1
  8. local hasRefueled = false
  9. while(index <= 16 and not hasRefueled) do
  10. turtle.select(index)
  11. if(turtle.refuel(0)) then
  12. turtle.refuel(1)
  13. hasRefueled = true
  14. end
  15. index = index + 1
  16. end
  17. end
  18.  
  19. -- Move in a direction 1 or more times
  20. local function move(direction, distance)
  21. local distance = tonumber(distance) or 1
  22. attemptRefuel()
  23.  
  24. if(direction == "up") then
  25. turtle.up()
  26. elseif(direction == "down") then
  27. turtle.down()
  28. elseif(direction == "forward") then
  29. turtle.forward()
  30. elseif(direction == "back") then
  31. turtle.back()
  32. else
  33. print("Direction: " .. direction .. " not a valid movement")
  34. return false
  35. end
  36.  
  37. if(distance > 1) then
  38. move(direction, distance - 1)
  39. end
  40. return true
  41. end
  42.  
  43. -- Dig out a block and move into it
  44. local function digMove(direction)
  45. if(direction == "up") then
  46. turtle.digUp()
  47. move("up")
  48. elseif(direction == "down") then
  49. turtle.digDown()
  50. move("down")
  51. elseif(direction == "forward") then
  52. turtle.dig()
  53. move("forward")
  54. elseif(direction == "back") then
  55. turtle.turnLeft()
  56. turtle.turnLeft()
  57. turtle.dig()
  58. turtle.turnLeft()
  59. turtle.turnLeft()
  60. move("back")
  61. else
  62. print("Direction: " .. direction .. " not a valid movement")
  63. return false
  64. end
  65. return true
  66. end
  67.  
  68. -- Dig out a 3x3 vertical square
  69. local function tunnelSquare()
  70. -- Dig out adjacent left and right blocks
  71. local function tunnelAdjacentLR()
  72. turtle.turnLeft()
  73. turtle.dig()
  74. turtle.turnLeft()
  75. turtle.turnLeft()
  76. turtle.dig()
  77. turtle.turnLeft()
  78. end
  79.  
  80. digMove("forward")
  81. for i=1, 3 do
  82. tunnelAdjacentLR()
  83. if(i < 3) then
  84. digMove("up")
  85. end
  86. end
  87. move("down", 2)
  88. end
  89.  
  90. -- Tunnel
  91. local function tunnel(distance, tunnelFunc)
  92. -- Don't bother with tunnelling negative distances
  93. if(distance < 1) then
  94. return
  95. end
  96.  
  97. -- Default to tunnelSquare
  98. local tunnelFunc = tunnelFunc or tunnelSquare
  99.  
  100. -- Tunnel over a distance using specified tunneling function
  101. for i=1, distance do
  102. tunnelFunc()
  103. end
  104. end
  105.  
  106. -- Display usage info
  107. local function help()
  108. local name = "<name>"
  109. if(shell) then
  110. name = shell.getRunningProgram()
  111. end
  112.  
  113. print("Usage: " .. name .. " <distance to tunnel >= 3> [shaft center spacing (-1 for no return)]")
  114. end
  115.  
  116. -- Main
  117. local function main(distance, spacing)
  118. local distance = tonumber(distance) or 0
  119. local spacing = tonumber(spacing) or 0
  120.  
  121. -- Handle args
  122. if(not distance) then
  123. help()
  124. return
  125. elseif(distance < 3) then
  126. help()
  127. return
  128. end
  129.  
  130. -- Initial tunneling distance
  131. tunnel(distance)
  132.  
  133. -- Don't return if a negative spacing has been given
  134. if(spacing < 0) then
  135. return
  136. end
  137.  
  138. -- Tunnel over to return shaft
  139. move("back")
  140. turtle.turnLeft()
  141. if(spacing > 0) then
  142. tunnel(spacing + 1)
  143. move("back")
  144. end
  145.  
  146. -- Move to edge of return shaft
  147. turtle.turnLeft()
  148. move("forward")
  149.  
  150. -- Tunnel/move back to start
  151. if(spacing == 0) then
  152. move("forward", distance - 2)
  153. else
  154. tunnel(distance - 2)
  155. end
  156.  
  157. -- Spin back around
  158. turtle.turnLeft()
  159. turtle.turnLeft()
  160. end
  161.  
  162. local arg = {...}
  163. main(arg[1], arg[2])
Advertisement
Add Comment
Please, Sign In to add comment