gundami

Untitled

Oct 24th, 2025 (edited)
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.93 KB | None | 0 0
  1. -- ==========================================
  2. -- CONFIG
  3. -- ==========================================
  4. local ROUTE_NAME = "AE Express"
  5. local API_URL = "https://map.earthcivils.top/route-map/route-api"
  6. -- ==========================================
  7. local relay = peripheral.find("redstone_relay")
  8.  
  9. if not ship then error("Ship peripheral not found!") end
  10. if not relay then error("Redstone relay not found!") end
  11.  
  12. -- Waypoints (full route)
  13. local waypoints = {
  14. {x = -2232, z = -12121, waitTime = 0},
  15. {x = -2234, z = -12453, waitTime = 0},
  16. {x = -2496, z = -12587, waitTime = 600},
  17. {x = -2948, z = -12212, waitTime = 0},
  18. {x = -8929, z = -11816, waitTime = 0},
  19. {x = -17232, z = -14640, waitTime = 0},
  20. {x = -19816, z = -15584, waitTime = 0},
  21. {x = -21360, z = -15208, waitTime = 600},
  22. {x = -22048, z = -14184, waitTime = 0},
  23. {x = -25575, z = -12897, waitTime = 0},
  24. {x = -25723, z = -12718, waitTime = 0},
  25. {x = -25812, z = -12589, waitTime = 600},
  26. {x = -25592, z = -12120, waitTime = 0},
  27. {x = -23288, z = -9576, waitTime = 0},
  28. {x = -24168, z = -7308, waitTime = 0},
  29. {x = -24028, z = -6910, waitTime = 0},
  30. {x = -23829, z = -6751, waitTime = 600},
  31. {x = -22816, z = -6680, waitTime = 0}
  32. }
  33.  
  34. local currentAction = "forward"
  35.  
  36. -- ==========================================
  37. -- API post function
  38. -- ==========================================
  39. local function pushToAPI(status)
  40. local pos = ship.getWorldspacePosition()
  41. local data = {
  42. routes = {{
  43. name = ROUTE_NAME,
  44. checkpoints = waypoints,
  45. shipPosition = { x = math.floor(pos.x), z = math.floor(pos.z) },
  46. status = status
  47. }}
  48. }
  49. local jsonData = textutils.serializeJSON(data)
  50. local r = http.post(API_URL, jsonData, {["Content-Type"] = "application/json"})
  51. if r then r.close() end
  52. end
  53.  
  54. -- ==========================================
  55. -- Ship control
  56. -- ==========================================
  57. local function moveForward()
  58. if currentAction ~= "forward" then
  59. relay.setOutput("left", false)
  60. relay.setOutput("back", false)
  61. relay.setOutput("top", false)
  62. currentAction = "forward"
  63. end
  64. end
  65.  
  66. local function turnLeft()
  67. relay.setOutput("left", true)
  68. relay.setOutput("back", false)
  69. currentAction = "left"
  70. end
  71.  
  72. local function turnRight()
  73. relay.setOutput("left", false)
  74. relay.setOutput("back", true)
  75. currentAction = "right"
  76. end
  77.  
  78. local function stopAll()
  79. relay.setOutput("left", false)
  80. relay.setOutput("back", false)
  81. relay.setOutput("top", true)
  82. currentAction = "stop"
  83. end
  84.  
  85. -- ==========================================
  86. -- ASCII compass
  87. -- ==========================================
  88. local function drawCompass(yaw, targetAngle)
  89. local width = 41
  90. local half = math.floor(width / 2)
  91. local relativeAngle = targetAngle - yaw
  92. if relativeAngle > 180 then relativeAngle = relativeAngle - 360 end
  93. if relativeAngle < -180 then relativeAngle = relativeAngle + 360 end
  94. local offset = math.floor(relativeAngle / (180 / half))
  95. local compass = {}
  96.  
  97. for i = -half, half do
  98. if i == 0 then table.insert(compass, "^")
  99. elseif i == offset then table.insert(compass, "*")
  100. elseif i % 5 == 0 then table.insert(compass, "|")
  101. else table.insert(compass, "-") end
  102. end
  103.  
  104. print("Compass:")
  105. print(table.concat(compass))
  106. print("[^ ship | * target]")
  107. end
  108.  
  109. -- ==========================================
  110. -- Find nearest start point (1000 block rule)
  111. -- ==========================================
  112. local function findStartingWaypoint()
  113. local pos = ship.getWorldspacePosition()
  114. local nearestIndex, nearestDist = 1, math.huge
  115. for i, wp in ipairs(waypoints) do
  116. local dx, dz = wp.x - pos.x, wp.z - pos.z
  117. local dist = math.sqrt(dx*dx + dz*dz)
  118. if dist < nearestDist then nearestDist, nearestIndex = dist, i end
  119. end
  120. print("Nearest WP: #"..nearestIndex.." dist "..nearestDist)
  121. if nearestDist <= 1000 then
  122. return math.min(nearestIndex + 1, #waypoints)
  123. else
  124. return nearestIndex
  125. end
  126. end
  127.  
  128. local function reverseWaypoints()
  129. local i, j = 1, #waypoints
  130. while i < j do waypoints[i], waypoints[j] = waypoints[j], waypoints[i] i=i+1 j=j-1 end
  131. end
  132.  
  133. -- ==========================================
  134. -- MAIN LOOP — round trip forever
  135. -- ==========================================
  136. local startIndex = findStartingWaypoint()
  137.  
  138. while true do
  139. for i = startIndex, #waypoints do
  140. local wp = waypoints[i]
  141. local arrived = false
  142.  
  143. while not arrived do
  144. local pos = ship.getWorldspacePosition()
  145. local yaw360
  146. do
  147. local e = ship.getEulerAnglesYXZ()
  148. local yaw = e.y
  149. yaw360 = (270 - math.deg(yaw)) % 360 -- DO NOT CHANGE
  150. end
  151.  
  152. local dx, dz = wp.x - pos.x, wp.z - pos.z
  153. local distance = math.sqrt(dx*dx + dz*dz)
  154.  
  155. -- ANGLE MATH — DO NOT CHANGE
  156. local targetAngle = math.deg(math.atan2(dx, -dz))
  157. if targetAngle < 0 then targetAngle = targetAngle + 360 end
  158. local relativeAngle = targetAngle - yaw360
  159. if relativeAngle > 180 then relativeAngle = relativeAngle - 360 end
  160. if relativeAngle < -180 then relativeAngle = relativeAngle + 360 end
  161.  
  162. -- Dynamic threshold
  163. local threshold = (distance > 200) and 8 or 3
  164. local interval = (distance > 200) and 2 or 0.5
  165. local arrivalDist = ((i == 1) or (i == #waypoints)) and 16 or 50
  166.  
  167. -- ===== ARRIVAL =====
  168. if distance <= arrivalDist then
  169. stopAll()
  170. print("✓ Reached WP "..i)
  171.  
  172. -- ✅ set idle when reach ANY waypoint
  173. pushToAPI("idle")
  174.  
  175. sleep(wp.waitTime or 0)
  176. sleep(2)
  177. arrived = true
  178. else
  179. -- ===== Steering (UNCHANGED) =====
  180. if math.abs(relativeAngle) < threshold then moveForward()
  181. elseif relativeAngle > 0 then turnRight()
  182. else turnLeft() end
  183.  
  184. pushToAPI("active")
  185. end
  186.  
  187. -- HUD
  188. term.clear()
  189. term.setCursorPos(1,1)
  190. print(string.format("WP %d/%d", i, #waypoints))
  191. print(string.format("Pos: %.1f, %.1f", pos.x, pos.z))
  192. print(string.format("Yaw: %.1f Target: %.1f", yaw360, targetAngle))
  193. print(string.format("Rel: %.1f Dist: %.1f", relativeAngle, distance))
  194. print("Action:", currentAction)
  195. print("")
  196. drawCompass(yaw360, targetAngle)
  197. sleep(interval)
  198. end
  199. end
  200.  
  201. -- reverse and go back
  202. print("Leg complete — reversing route.")
  203. reverseWaypoints()
  204. startIndex = 1
  205. end
  206.  
Advertisement
Add Comment
Please, Sign In to add comment