gundami

Untitled

Oct 24th, 2025 (edited)
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. local relay = peripheral.find("redstone_relay")
  2.  
  3. local waypoints = {
  4. {x = 364, z = 139},
  5. {x = -125, z = 38},
  6. {x = 177, z = -368}
  7. }
  8.  
  9. local currentAction = "forward"
  10.  
  11. local function moveForward()
  12. if currentAction ~= "forward" then
  13. relay.setOutput("top", false)
  14. relay.setOutput("back", false)
  15. relay.setOutput("left", false)
  16. currentAction = "forward"
  17. end
  18. end
  19.  
  20. local function turnLeft()
  21. if currentAction ~= "left" then
  22. relay.setOutput("top", false)
  23. relay.setOutput("back", false)
  24. relay.setOutput("left", true)
  25. currentAction = "left"
  26. end
  27. end
  28.  
  29. local function turnRight()
  30. if currentAction ~= "right" then
  31. relay.setOutput("top", false)
  32. relay.setOutput("left", false)
  33. relay.setOutput("back", true)
  34. currentAction = "right"
  35. end
  36. end
  37.  
  38. local function stopEngine()
  39. if currentAction ~= "stop" then
  40. relay.setOutput("top", true)
  41. relay.setOutput("back", false)
  42. relay.setOutput("left", false)
  43. currentAction = "stop"
  44. end
  45. end
  46.  
  47. local function normalizeAngle(angle)
  48. while angle > math.pi do angle = angle - 2*math.pi end
  49. while angle < -math.pi do angle = angle + 2*math.pi end
  50. return angle
  51. end
  52.  
  53. local function navigateToPoint(currentX, currentZ, currentYaw, targetX, targetZ)
  54. local arrivalDistance = 20
  55. local angleThreshold = math.rad(15)
  56.  
  57. local dx = targetX - currentX
  58. local dz = targetZ - currentZ
  59. local distance = math.sqrt(dx*dx + dz*dz)
  60.  
  61. if distance < arrivalDistance then
  62. stopEngine()
  63. return true
  64. end
  65.  
  66. -- FIXED: Use -dx and -dz to correct coordinate system
  67. local targetHeading = math.atan2(-dx, -dz)
  68.  
  69. local headingDiff = normalizeAngle(targetHeading - currentYaw)
  70.  
  71. if math.abs(headingDiff) < angleThreshold then
  72. moveForward()
  73. elseif headingDiff > 0 then
  74. turnLeft()
  75. else
  76. turnRight()
  77. end
  78.  
  79. return false, headingDiff, targetHeading
  80. end
  81.  
  82. print("======================================")
  83. print(" Valkyrien Skies Auto Navigation")
  84. print("======================================")
  85.  
  86. if not ship then
  87. print("Error: Ship peripheral not found!")
  88. return
  89. end
  90.  
  91. if not relay then
  92. print("Error: Redstone relay not found!")
  93. return
  94. end
  95.  
  96. print("System initialized")
  97. print(string.format("Total waypoints: %d", #waypoints))
  98. print("\nPress any key to start navigation...")
  99. os.pullEvent("key")
  100.  
  101. for i, waypoint in ipairs(waypoints) do
  102. print(string.format("\n========== Waypoint %d/%d ==========", i, #waypoints))
  103. print(string.format("Target: (%d, %d)", waypoint.x, waypoint.z))
  104.  
  105. local arrived = false
  106.  
  107. while not arrived do
  108. local pos = ship.getWorldspacePosition()
  109. local euler = ship.getEulerAnglesYXZ()
  110. local yaw = euler.y
  111.  
  112. local arr, headingDiff, targetHeading = navigateToPoint(
  113. pos.x, pos.z,
  114. yaw,
  115. waypoint.x, waypoint.z
  116. )
  117. arrived = arr
  118.  
  119. local dx = waypoint.x - pos.x
  120. local dz = waypoint.z - pos.z
  121. local distance = math.sqrt(dx*dx + dz*dz)
  122. print(string.format("Pos:(%.0f,%.0f) Dist:%.0f Yaw:%.1f° Target:%.1f° Dev:%.1f° [%s]",
  123. pos.x, pos.z, distance, math.deg(yaw), math.deg(targetHeading), math.deg(headingDiff), currentAction))
  124.  
  125. sleep(0.5)
  126. end
  127.  
  128. print(string.format("✓ Waypoint %d reached", i))
  129.  
  130. if i < #waypoints then
  131. print("Waiting for 3 seconds...")
  132. sleep(3)
  133. end
  134. end
  135.  
  136. print("\n======================================")
  137. print(" All waypoints completed!")
  138. print("======================================")
  139. stopEngine()
Advertisement
Add Comment
Please, Sign In to add comment