Advertisement
Darkbro100

Untitled

Dec 30th, 2022
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.13 KB | None | 0 0
  1. -- Turtle Self-tracking System created by Latias1290.
  2.  
  3. local xPos, yPos, zPos = nil
  4. local face = 1
  5. local cal = false
  6.  
  7. Direction = {
  8. West = 1,
  9. North = 2,
  10. East = 3,
  11. South = 4
  12. }
  13.  
  14. function setLocation() -- get gps using other computers
  15. -- calculate facing direction
  16. face = getFacing()
  17.  
  18. -- set coords after facing has been found out
  19. xPos, yPos, zPos = gps.locate()
  20.  
  21. -- set cabllibrated to true
  22. cal = true
  23. end
  24.  
  25. function getFacing()
  26. local loc1 = vector.new(gps.locate())
  27. if not turtle.forward() then
  28. for i = 0, 9 do
  29. if turtle.forward() then
  30. break
  31. else
  32. turtle.dig()
  33. end
  34. end
  35. end
  36.  
  37. local loc2 = vector.new(gps.locate())
  38. local heading = loc2 - loc1
  39.  
  40. return ((heading.x + math.abs(heading.x) * 2) + (heading.z + math.abs(heading.z) * 3))
  41. end
  42.  
  43. function manSetLocation(x, y, z) -- manually set location
  44. xPos = x
  45. yPos = y
  46. zPos = z
  47. cal = true
  48. end
  49.  
  50. function getLocation() -- return the location
  51. if xPos ~= nil then
  52. return xPos, yPos, zPos, face
  53. else
  54. return nil
  55. end
  56. end
  57.  
  58. function getDirFromMotion(motion)
  59. local xMot = motion[1]
  60. local zMot = motion[2]
  61.  
  62. if xMot == 1 and zMot == 0 then
  63. return Direction.East
  64. elseif xMot == -1 and zMot == 0 then
  65. return Direction.West
  66. elseif xMot == 0 and zMot == 1 then
  67. return Direction.South
  68. elseif xMot == 0 and zMot == -1 then
  69. return Direction.North
  70. end
  71. end
  72.  
  73. function getMotion(dir)
  74. if dir == Direction.East then
  75. return {1, 0}
  76. elseif dir == Direction.West then
  77. return {-1, 0}
  78. elseif dir == Direction.South then
  79. return {0, 1}
  80. elseif dir == Direction.North then
  81. return {0, -1}
  82. end
  83. end
  84.  
  85. function faceDir(dir)
  86. if dir == 1 and face == 4 then
  87. turnRight()
  88. elseif dir == 4 and face == 1 then
  89. turnLeft()
  90. else
  91. local diff = dir - face
  92. while diff > 0 do
  93. turnRight()
  94. diff = diff - 1
  95. end
  96.  
  97. while diff < 0 do
  98. turnLeft()
  99. diff = diff + 1
  100. end
  101. end
  102.  
  103. print("After doing corrections: " .. face)
  104. end
  105.  
  106. function turnLeft() -- turn left
  107. if not cal then
  108. print("Not Calibrated")
  109. return
  110. end
  111.  
  112. turtle.turnLeft()
  113.  
  114. face = face - 1
  115. if face <= 0 then
  116. face = 4
  117. end
  118. end
  119.  
  120. function turnRight() -- turn right
  121. if not cal then
  122. print("Not Calibrated")
  123. return
  124. end
  125.  
  126. turtle.turnRight()
  127.  
  128. face = face + 1
  129. if face >= 5 then
  130. face = 0
  131. end
  132. end
  133.  
  134. function forward() -- go forward
  135. if not cal then
  136. print("Not Calibrated")
  137. return
  138. end
  139.  
  140. if turtle.forward() then
  141. local mot = getMotion(face)
  142. xPos = xPos + mot[1]
  143. zPos = zPos + mot[2]
  144. end
  145. end
  146.  
  147. function back() -- go back
  148. if not cal then
  149. print("Not Calibrated")
  150. return
  151. end
  152.  
  153. if turtle.back() then
  154. local mot = getMotion(face)
  155. xPos = xPos - mot[1]
  156. zPos = zPos - mot[2]
  157. end
  158. end
  159.  
  160. function up() -- go up
  161. if not cal then
  162. print("Not Calibrated")
  163. return
  164. end
  165.  
  166. if turtle.up() then
  167. yPos = yPos + 1
  168. end
  169. end
  170.  
  171. function down() -- go down
  172. if not cal then
  173. print("Not Calibrated")
  174. return
  175. end
  176.  
  177. if turtle.down() then
  178. yPos = yPos - 1
  179. end
  180. end
  181.  
  182. function jump() -- perform a jump. useless? yup!
  183. up()
  184. down()
  185. end
  186.  
  187. local scanner_radius = 8
  188. local scanner_width = scanner_radius * 2 + 1
  189.  
  190. local function scanned_at(x, y, z, scanned)
  191. return scanned[scanner_width ^ 2 * (x + scanner_radius) + scanner_width * (y + scanner_radius) + (z + scanner_radius) + 1]
  192. end
  193.  
  194. local function heuristic(nodeA, nodeB)
  195. return math.sqrt((nodeB.x - nodeA.x)^2 + (nodeB.y - nodeA.y)^2 + (nodeB.z - nodeA.z)^2)
  196. end
  197.  
  198. local function nodeObject(vec, gScore, fScore, block)
  199. return {vec=vec, gScore=gScore, fScore=fScore, block=block}
  200. end
  201.  
  202. local function grabLowest(openSet, fScore)
  203. local lowestScore = nil
  204. local lowest = nil
  205.  
  206. for key, val in pairs(openSet) do
  207. local score = fScore[key]
  208.  
  209. if lowestScore == nil or score < lowestScore then
  210. lowestScore = score
  211. lowest = key
  212. end
  213. end
  214.  
  215. return lowest, lowestScore
  216. end
  217.  
  218. local function vectorMatches(v1, v2)
  219. return v1.x == v2.x and v1.y == v2.y and v1.z == v2.z
  220. end
  221.  
  222. local function getNeighbors(current, scanned)
  223.  
  224. local above = vector.new(current.x, current.y + 1, current.z)
  225. local below = vector.new(current.x, current.y - 1, current.z)
  226.  
  227. local neighbors = {}
  228. local aboveBlock = scanned_at(above.x, above.y, above.z, scanned)
  229. local belowBlock = scanned_at(below.x, below.y, below.z, scanned)
  230.  
  231. if not aboveBlock or aboveBlock.name == "minecraft:air" then
  232. table.insert(neighbors, above)
  233. end
  234.  
  235. if not belowBlock or belowBlock.name == "minecraft:air" then
  236. table.insert(neighbors, below)
  237. end
  238.  
  239. for i = 1, 4 do
  240. local mot = getMotion(i)
  241. local xMot = mot[1]
  242. local zMot = mot[2]
  243.  
  244. local vec = vector.new(current.x + xMot, current.y, current.z + zMot)
  245. local block = scanned_at(vec.x, vec.y, vec.z, scanned)
  246. if not block or block.name == "minecraft:air" then
  247. table.insert(neighbors, vec)
  248. end
  249. end
  250.  
  251. return neighbors
  252. end
  253.  
  254. local function reconstruct_path(cameFrom, current)
  255. local path = {}
  256. table.insert(path, current)
  257.  
  258. while cameFrom[current] do
  259. current = cameFrom[current]
  260. table.insert(path, 1, current)
  261. end
  262.  
  263. return path
  264. end
  265.  
  266. function aStar(goalVec, blockScanner)
  267.  
  268. -- grab starting position
  269. local startX, startY, startZ, _ = getLocation()
  270. local start = vector.new(startX, startY - 1, startZ)
  271.  
  272. local scanned = blockScanner.scan()
  273. local openSet = {}
  274. local cameFrom = {}
  275. local gScore = {}
  276. local fScore = {}
  277.  
  278. gScore[start] = 0
  279. fScore[start] = heuristic(start, goalVec)
  280.  
  281. openSet[start] = true
  282.  
  283. local next = next
  284.  
  285. while next(openSet) ~= nil do
  286. local current, currentFScore = grabLowest(openSet, fScore)
  287.  
  288. if vectorMatches(current, goalVec) then
  289. return reconstruct_path(cameFrom, current)
  290. end
  291.  
  292. openSet[current] = nil
  293. local neighbors = getNeighbors(current, scanned)
  294.  
  295. for _, neighbor in ipairs(neighbors) do
  296. local tentG = gScore[current] + 1 --turtle can only ever move 1 block, so distance will be 1
  297. if gScore[neighbor] == nil or tentG < gScore[neighbor] then
  298. cameFrom[neighbor] = current
  299. gScore[neighbor] = tentG
  300. fScore[neighbor] = tentG + heuristic(neighbor, goalVec)
  301. if openSet[neighbor] == nil then
  302. openSet[neighbor] = true
  303. end
  304. end
  305. end
  306. end
  307.  
  308. print("PATH FAILED")
  309. return {}
  310. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement