nocv

Untitled

Oct 14th, 2017
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. -- Fetch beginning position.
  2. local startPos = npos.getPosition()
  3. print("Starting position:")
  4. print(startPos)
  5.  
  6. -- Find refuel/stop waypoint position.
  7. local forwardStart = 5
  8. for fscnt=1,forwardStart,1 do
  9. npos.move(npos.directions.FORWARD, true)
  10. end
  11. local routeWaypoint = npos.getPosition()
  12. print("Return waypoint:")
  13. print(routeWaypoint)
  14. npos.moveTo(startPos)
  15.  
  16. local exiting = false
  17.  
  18. -- Get back to our house safely.
  19. function navigateHome()
  20. print("Moving to house entrance")
  21. npos.moveTo(routeWaypoint)
  22. print("Moving to home")
  23. npos.moveTo(startPos)
  24. end
  25.  
  26. -- Get out of our house.
  27. function exitHome()
  28. print("Exiting the house")
  29. npos.moveTo(routeWaypoint)
  30. end
  31.  
  32. -- Check if we need to unload.
  33. function isUnloadRequired()
  34. for i=1,16,1 do
  35. if(turtle.getItemDetail(i) == nil) then
  36. return false
  37. end
  38. end
  39. return true
  40. end
  41.  
  42. -- Select an empty slot.
  43. function selectEmptySlot()
  44. for i=1,16,1 do
  45. if(turtle.getItemDetail(i) == nil) then
  46. turtle.select(i)
  47. return true
  48. end
  49. end
  50. return false
  51. end
  52.  
  53. -- Check our inventory/fuel status.
  54. function blockCheck()
  55. local cpos = npos.getPosition()
  56.  
  57. if(turtle.getFuelLevel() < 5000 or isUnloadRequired()) then
  58. print("Making a drop and refuel run")
  59. navigateHome()
  60.  
  61. -- Spit everything out.
  62. npos.setOrientation(npos.orientations.SOUTH)
  63. print("Dropping items...")
  64. for dx=1,16,1 do
  65. turtle.select(dx)
  66.  
  67. if(turtle.getItemDetail(dx) == nil) then
  68. print(dx .. ": Nothing here, cant drop...")
  69. else
  70. if(turtle.drop() == false) then
  71. print(dx .. ": Can't drop anymore. stopping...")
  72. exiting = true
  73. return true
  74. else
  75. print(dx .. ": Dropped")
  76. end
  77. end
  78. end
  79.  
  80. -- Check if we need to refuel.
  81. npos.setOrientation(npos.orientations.WEST)
  82. if(turtle.getFuelLevel() < 5000) then
  83. selectEmptySlot()
  84. while(turtle.getFuelLevel() < 15000) do
  85. if(turtle.suck() == true) then
  86. local refuelSuccess = false
  87. for rfi=1,16,1 do
  88. turtle.select(rfi)
  89. if(turtle.refuel()) then
  90. refuelSuccess = true
  91. end
  92. end
  93.  
  94. if(refuelSuccess == false) then
  95. print("Couldn't refuel after fetching from fuel chest")
  96. exiting = true
  97. return true
  98. end
  99. else
  100. print("Couldn't refuel anymore, stopping...")
  101. exiting = true
  102. return true
  103. end
  104. end
  105. end
  106. end
  107.  
  108. -- Resume where we left off.
  109. exitHome()
  110. npos.moveTo(cpos)
  111. npos.setOrientation(cpos.o)
  112. end
  113.  
  114. function excavateChunk()
  115. local isForward = true
  116. local startPos = npos.getPosition()
  117.  
  118. w = 16
  119. h = 16
  120. l = 16
  121.  
  122. for ch=1,16,1 do
  123. for cw=1,16,1 do
  124. for cl=1,15,1 do
  125. npos.move(npos.directions.FORWARD, true)
  126. blockCheck()
  127.  
  128. if(exiting == true) then return true end
  129. end
  130.  
  131. if(cw < w) then
  132. if(isForward) then
  133. npos.turnRight()
  134. npos.move(npos.directions.FORWARD, true)
  135. blockCheck()
  136. if(exiting == true) then return true end
  137. npos.turnRight()
  138.  
  139. isForward = false
  140. else
  141. npos.turnLeft()
  142. npos.move(npos.directions.FORWARD, true)
  143. blockCheck()
  144. if(exiting == true) then return true end
  145. npos.turnLeft()
  146.  
  147. isForward = true
  148. end
  149. end
  150. end
  151.  
  152. if(ch < h) then
  153. npos.move(npos.directions.UP, true)
  154. blockCheck()
  155. if(exiting == true) then return true end
  156. npos.turnLeft()
  157. npos.turnLeft()
  158. end
  159. end
  160.  
  161. print("Looks like we're done with this chunk, going home.")
  162. navigateHome()
  163. end
  164.  
  165. local args = { ... }
  166. local chunkForward = tonumber(args[1])
  167. local chunkLateral = tonumber(args[2])
  168.  
  169. -- Leave the comfort of our warm, warm home.
  170. exitHome()
  171.  
  172. -- Move right the given amount of chunks we're mining from here.
  173. if(chunkLateral > 0) then
  174. print("Moving laterally a chunk or so")
  175. npos.turnRight()
  176. for cl=1,chunkLateral,1 do
  177. for cli=1,16,1 do
  178. npos.move(npos.directions.FORWARD, true)
  179. end
  180. end
  181. npos.setOrientation(startPos.o)
  182. npos.turnLeft()
  183. end
  184.  
  185. -- Move forward the given amount of chunks we're mining from here.
  186. if(chunkForward > 0) then
  187. for cf=1,chunkForward,1 do
  188. print("Moving forward a chunk or so")
  189. for cfi=1,16,1 do
  190. npos.move(npos.directions.FORWARD, true)
  191. end
  192. end
  193. end
  194.  
  195. -- Move backwards our 'forward start' amount.
  196. print("Resetting to pre-forward start offset")
  197. npos.turnLeft()
  198. npos.turnLeft()
  199. for fsr=1,forwardStart,1 do npos.move(npos.directions.FORWARD, true) end
  200. npos.turnLeft()
  201. npos.turnLeft()
  202.  
  203. -- We good to go.
  204. print("Kicking off excavation...")
  205. excavateChunk()
  206. print("Done.")
Add Comment
Please, Sign In to add comment