naschorr

mine-return-2

May 16th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 KB | None | 0 0
  1. -- Globals
  2. local arg = {...}
  3. local SQUARE_SIZE = 3
  4. local DOWN_SQUARE = 0
  5. local UP_SQUARE = 1
  6. local squareType = DOWN_SQUARE
  7. local CONTAINER_SUBSTRINGS = {"chest"}
  8.  
  9. -- Determines if the block in front of a turtle is a chest
  10. local function isChest()
  11. local status, data = turtle.inspect()
  12.  
  13. if(not status) then
  14. print("No chest available")
  15. return false
  16. end
  17.  
  18. local itemName = data["name"]:match(":(.+)"):lower()
  19. for index, container in pairs(CONTAINER_SUBSTRINGS) do
  20. if(itemName:find(container)) then
  21. return true
  22. end
  23. end
  24.  
  25. return false
  26. end
  27.  
  28. -- Attempt to refuel the turtle
  29. local function attemptRefuel()
  30. if(turtle.getFuelLevel() >= 1) then
  31. return
  32. end
  33.  
  34. local index = 1
  35. local hasRefueled = false
  36. while(index <= 16 and not hasRefueled) do
  37. turtle.select(index)
  38. if(turtle.refuel(0)) then
  39. turtle.refuel(1)
  40. hasRefueled = true
  41. end
  42. index = index + 1
  43. end
  44. end
  45.  
  46. -- Attempt to dump non-fuel items into a chest in front of the turtle
  47. local function attemptDumpInv()
  48. if(not isChest()) then
  49. return
  50. end
  51.  
  52. for index=1, 16 do
  53. turtle.select(index)
  54. if(not turtle.refuel(0)) then
  55. turtle.drop()
  56. end
  57. end
  58. end
  59.  
  60. -- Move in a direction 1 or more times
  61. local function move(direction, distance)
  62. local distance = tonumber(distance) or 1
  63. attemptRefuel()
  64.  
  65. local result
  66. if(direction == "up") then
  67. result = turtle.up()
  68. elseif(direction == "down") then
  69. result = turtle.down()
  70. elseif(direction == "forward") then
  71. result = turtle.forward()
  72. elseif(direction == "back") then
  73. result = turtle.back()
  74. else
  75. print("Direction: " .. direction .. " not a valid movement")
  76. result = false
  77. end
  78.  
  79. if(not result) then
  80. return result
  81. end
  82.  
  83. if(distance > 1) then
  84. move(direction, distance - 1)
  85. end
  86.  
  87. return true
  88. end
  89.  
  90. -- Dig out a block and move into it
  91. local function digMove(direction)
  92. local result
  93. if(direction == "up") then
  94. turtle.digUp()
  95. result = move("up")
  96. elseif(direction == "down") then
  97. turtle.digDown()
  98. result = move("down")
  99. elseif(direction == "forward") then
  100. turtle.dig()
  101. result = move("forward")
  102. elseif(direction == "back") then
  103. turtle.turnLeft()
  104. turtle.turnLeft()
  105. turtle.dig()
  106. turtle.turnLeft()
  107. turtle.turnLeft()
  108. result = move("back")
  109. else
  110. print("Direction: " .. direction .. " not a valid movement")
  111. return false
  112. end
  113.  
  114. -- Usually gets stuck on sand/gravel so dig it back out
  115. if(not result) then
  116. digMove(direction)
  117. end
  118.  
  119. return true
  120. end
  121.  
  122. -- Dig out a 3x3 vertical square
  123. local function tunnelSquare()
  124. -- Dig out adjacent left and right blocks
  125. local function tunnelAdjacentLR()
  126. local function verifyDig()
  127. local status = turtle.inspect()
  128. while(status) do
  129. turtle.dig()
  130. status = turtle.inspect()
  131. end
  132. end
  133.  
  134. turtle.turnLeft()
  135. verifyDig()
  136. turtle.turnLeft()
  137. turtle.turnLeft()
  138. verifyDig()
  139. turtle.turnLeft()
  140. end
  141.  
  142. digMove("forward") -- Move into next vertical square
  143. for i=1, SQUARE_SIZE do
  144. tunnelAdjacentLR()
  145. if(i < 3) then
  146. if(DOWN_SQUARE == squareType) then
  147. digMove("up")
  148. else
  149. digMove("down")
  150. end
  151. end
  152. end
  153.  
  154. if(DOWN_SQUARE == squareType) then
  155. squareType = UP_SQUARE
  156. else
  157. squareType = DOWN_SQUARE
  158. end
  159. end
  160.  
  161. -- Tunnel
  162. local function tunnel(distance, tunnelFunc)
  163. -- Don't bother with tunnelling negative distances
  164. if(distance < 1) then
  165. return
  166. end
  167.  
  168. -- Default to tunnelSquare
  169. local tunnelFunc = tunnelFunc or tunnelSquare
  170.  
  171. -- Tunnel over a distance using specified tunneling function
  172. for i=1, distance do
  173. tunnelFunc()
  174. end
  175. end
  176.  
  177. -- Display usage info
  178. local function help()
  179. local name = "<name>"
  180. if(shell) then
  181. name = shell.getRunningProgram()
  182. end
  183.  
  184. print("Usage: " .. name .. " <distance to tunnel >= 3> [shaft center spacing (- left, + right)]\nAlso, if a chest is placed to the turtle's right it'll dump non-fuel items into that")
  185. end
  186.  
  187. -- Main
  188. local function main(distance, spacing)
  189. -- Handle args
  190. local distance = tonumber(distance) or 0
  191. local spacing = tonumber(spacing) or 0
  192.  
  193. if(distance < 3) then
  194. help()
  195. return
  196. end
  197.  
  198. -- Initial tunneling distance
  199. tunnel(distance)
  200.  
  201. -- Tunnel over to return shaft
  202. move("back")
  203. if(spacing > 0) then
  204. turtle.turnRight()
  205. move("forward")
  206. tunnel(spacing)
  207. move("back")
  208. turtle.turnRight()
  209. elseif(spacing < 0) then
  210. turtle.turnLeft()
  211. move("forward")
  212. tunnel(spacing * -1)
  213. move("back")
  214. turtle.turnLeft()
  215. else
  216. turtle.turnLeft()
  217. turtle.turnLeft()
  218. end
  219.  
  220. -- Move to edge of return shaft
  221. move("forward")
  222.  
  223. -- Tunnel/move back to start
  224. if(spacing == 0) then
  225. move("forward", distance - 2)
  226. else
  227. tunnel(distance - 3)
  228. move("forward")
  229. end
  230.  
  231. if(UP_SQUARE == squareType) then
  232. move("down", 2)
  233. end
  234.  
  235. -- Spin back around, dump non-fuel into adjacent chest
  236. turtle.turnLeft()
  237. attemptDumpInv()
  238. turtle.turnLeft()
  239. end
  240.  
  241. main(arg[1], arg[2])
Advertisement
Add Comment
Please, Sign In to add comment