Advertisement
visiongaming43

Untitled

May 30th, 2022
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.76 KB | None | 0 0
  1. local modem = peripheral.find("modem") or error("TURTLE HAS NO MODEM")
  2. if (gps.locate(2, false) == nil) then
  3. error("GPS.LOCATE() IS NOT FUNCTIONING PROPERLY")
  4. end
  5.  
  6. local HEADING = nil
  7. local TURTLE_X, TURTLE_Y, TURTLE_Z = nil, nil, nil
  8.  
  9. -- North, East, South, West correlates to (1, 2, 3, 4)
  10. local ACCEPTABLE_HEADINGS = {
  11. [1] = "-z",
  12. [2] = "+x",
  13. [3] = "+z",
  14. [4] = "-x"
  15. }
  16.  
  17. function saveToFile()
  18. local TURTLE_FILE = fs.open("TURTLE_STATS", "w")
  19. local info = {TURTLE_X, TURTLE_Y, TURTLE_Z, HEADING}
  20. for key, line in ipairs(info) do
  21. TURTLE_FILE.writeLine(info[key])
  22. end
  23. TURTLE_FILE.close()
  24. end
  25.  
  26. function clearPath()
  27. while (turtle.detect()) do
  28. turtle.dig()
  29. end
  30. end
  31.  
  32. function locateCoordinates()
  33. TURTLE_X, TURTLE_Y, TURTLE_Z = gps.locate(2, false)
  34. end
  35.  
  36. function getCoordinates()
  37. return TURTLE_X, TURTLE_Y, TURTLE_Z
  38. end
  39.  
  40. function getCoordinatesFromFile()
  41. local TURTLE_FILE = fs.open("TURTLE_STATS", "r")
  42. TURTLE_X = TURTLE_FILE.readLine()
  43. TURTLE_Y = TURTLE_FILE.readLine()
  44. TURTLE_X = TURTLE_FILE.readLine()
  45. TURTLE_FILE.close()
  46. return TURTLE_X, TURTLE_Y, TURTLE_Z
  47. end
  48.  
  49. function updateStats()
  50. locateCoordinates()
  51. local vectorOne = vector.new(getCoordinates())
  52. clearPath()
  53. if (forward()) then
  54. locateCoordinates()
  55. local vectorTwo = vector.new(getCoordinates())
  56. local resultVector = vectorTwo:sub(vectorOne)
  57. HEADING = ((math.abs(resultVector.x) * 3 - resultVector.x) + (math.abs(resultVector.z) * 2 + resultVector.z))
  58. back()
  59. else
  60. error("TURTLE DID NOT PROPERLY MOVE FORWARD")
  61. end
  62. end
  63.  
  64. function getHeadingFromFile()
  65. local TURTLE_FILE = fs.open("TURTLE_STATS", "r")
  66. for a = 1, 3 do
  67. TURTLE_FILE.readLine()
  68. end
  69. HEADING = TURTLE_FILE.readLine()
  70. return HEADING
  71. end
  72.  
  73. function faceHeading(finalHeading)
  74. local previousIndex
  75. if (HEADING == 1) then
  76. previousIndex = #ACCEPTABLE_HEADINGS
  77. else
  78. previousIndex = HEADING - 1
  79. end
  80. if (finalHeading == previousIndex) then
  81. turnLeft()
  82. return
  83. end
  84. local difference = math.abs(HEADING - finalHeading)
  85. while (difference > 0) do
  86. turnRight()
  87. difference = difference - 1
  88. end
  89. end
  90.  
  91. function turnLeft()
  92. turtle.turnLeft()
  93. if (HEADING == 1) then
  94. HEADING = 4
  95. else
  96. HEADING = HEADING - 1
  97. end
  98. saveToFile()
  99. end
  100.  
  101. function turnRight()
  102. turtle.turnRight()
  103. if (HEADING == 4) then
  104. HEADING = 1
  105. else
  106. HEADING = HEADING + 1
  107. end
  108. saveToFile()
  109. end
  110.  
  111. function forward()
  112. if (turtle.forward()) then
  113. if (HEADING == 1) then
  114. TURTLE_Z = TURTLE_Z - 1
  115. elseif (HEADING == 2) then
  116. TURTLE_X = TURTLE_X + 1
  117. elseif (HEADING == 3) then
  118. TURTLE_Z = TURTLE_Z + 1
  119. elseif (HEADING == 4) then
  120. TURTLE_X = TURTLE_X - 1
  121. end
  122. saveToFile()
  123. return true
  124. end
  125. return false
  126. end
  127.  
  128. function back()
  129. if (turtle.back()) then
  130. if (HEADING == 1) then
  131. TURTLE_Z = TURTLE_Z + 1
  132. elseif (HEADING == 2) then
  133. TURTLE_X = TURTLE_X - 1
  134. elseif (HEADING == 3) then
  135. TURTLE_Z = TURTLE_Z - 1
  136. elseif (HEADING == 4) then
  137. TURTLE_X = TURTLE_X + 1
  138. end
  139. saveToFile()
  140. return true
  141. end
  142. return false
  143. end
  144.  
  145. function up()
  146. if (turtle.up()) then
  147. TURTLE_Y = TURTLE_Y + 1
  148. saveToFile()
  149. return true
  150. end
  151. return false
  152. end
  153.  
  154. function down()
  155. if (turtle.down()) then
  156. TURTLE_Y = TURTLE_Y - 1
  157. saveToFile()
  158. return true
  159. end
  160. return false
  161. end
  162.  
  163. function translateY(amount)
  164. local command
  165. if (amount < 0) then
  166. command = down
  167. else
  168. command = up
  169. end
  170. amount = math.abs(amount)
  171. while (amount > 0) do
  172. command()
  173. amount = amount - 1
  174. end
  175. end
  176.  
  177. function translateX(amount)
  178. faceHeading(2)
  179. local command
  180. if (amount < 0) then
  181. command = back
  182. else
  183. command = forward
  184. end
  185. amount = math.abs(amount)
  186. while (amount > 0) do
  187. command()
  188. amount = amount - 1
  189. end
  190. end
  191.  
  192. function translateZ(amount)
  193. faceHeading(3)
  194. local command
  195. if (amount < 0) then
  196. command = back
  197. else
  198. command = forward
  199. end
  200. amount = math.abs(amount)
  201. while (amount > 0) do
  202. command()
  203. amount = amount - 1
  204. end
  205. end
  206.  
  207. function goToY(endY)
  208. local amount = endY - TURTLE_Y
  209. translateY(amount)
  210. end
  211.  
  212. function goToX(endX)
  213. local amount = endX - TURTLE_X
  214. translateX(amount)
  215. end
  216.  
  217. function goToZ(endZ)
  218. local amount = endZ - TURTLE_Z
  219. translateZ(amount)
  220. end
  221.  
  222. function moveTo(x, y, z)
  223. local startVector = vector.new(getCoordinates())
  224. local endVector = vector.new(x, y, z)
  225. local midVector = endVector:sub(startVector)
  226. -- Take care of y first, then x, then z
  227. translateY(midVector.y)
  228. translateX(midVector.x)
  229. translateZ(midVector.z)
  230. end
  231.  
  232. return { HEADING = HEADING, TURTLE_X = TURTLE_X, TURTLE_Y = TURTLE_Y, TURTLE_Z = TURTLE_Z, ACCEPTABLE_HEADINGS = ACCEPTABLE_HEADINGS, saveToFile = saveToFile, clearPath = clearPath, locateCoordinates = locateCoordinates, getCoordinates = getCoordinates, getCoordinatesFromFile = getCoordinatesFromFile, updateStats = updateStats, getHeadingFromFile = getHeadingFromFile, faceHeading = faceHeading, turnLeft = turnLeft, turnRight = turnRight, forward = forward, back = back, up = up, down = down, translateY = translateY, translateX = translateX, translateZ = translateZ, goToY = goToY, goToX = goToX, goToZ = goToZ, moveTo = moveTo }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement