gundami

Untitled

Oct 24th, 2025
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. local relay = peripheral.find("redstone_relay")
  2.  
  3. if not ship then error("Ship peripheral not found!") end
  4. if not relay then error("Redstone relay not found!") end
  5.  
  6. -- 航点列表
  7. local waypoints = {
  8. {x = -20934, z = -12588},
  9. {x = -16832, z = -14592},
  10. {x = -10776, z = -13304}
  11. }
  12.  
  13. local currentAction = "stop"
  14. local threshold = 2 -- 角度阈值,小于这个停止转向
  15. local arrivalDistance = 5 -- 到达目标判定距离
  16.  
  17. -- 左右转函数
  18. local function turnLeft()
  19. if currentAction ~= "left" then
  20. relay.setOutput("left", true)
  21. relay.setOutput("right", false)
  22. currentAction = "left"
  23. end
  24. end
  25.  
  26. local function turnRight()
  27. if currentAction ~= "right" then
  28. relay.setOutput("left", false)
  29. relay.setOutput("back", true)
  30. currentAction = "right"
  31. end
  32. end
  33.  
  34. local function stopTurn()
  35. if currentAction ~= "stop" then
  36. relay.setOutput("left", false)
  37. relay.setOutput("right", false)
  38. currentAction = "stop"
  39. end
  40. end
  41.  
  42. -- 主循环,依次导航航点
  43. for i, waypoint in ipairs(waypoints) do
  44. print(string.format("Navigating to waypoint %d: (%.2f, %.2f)", i, waypoint.x, waypoint.z))
  45.  
  46. while true do
  47. local pos = ship.getWorldspacePosition()
  48. local euler = ship.getEulerAnglesYXZ()
  49. local yaw = euler.y
  50. local yawDeg = math.deg(yaw)
  51. local yaw360 = (270 - yawDeg) % 360 -- 北=0,顺时针增大
  52.  
  53. local dx = waypoint.x - pos.x
  54. local dz = waypoint.z - pos.z
  55. local distance = math.sqrt(dx*dx + dz*dz)
  56.  
  57. -- 到达判定
  58. if distance <= arrivalDistance then
  59. stopTurn()
  60. print(string.format("✓ Waypoint %d reached", i))
  61. sleep(1)
  62. break
  63. end
  64.  
  65. -- 目标角度(世界坐标,北为0°)
  66. local targetAngle = math.deg(math.atan2(dx, -dz))
  67. if targetAngle < 0 then targetAngle = targetAngle + 360 end
  68.  
  69. -- 相对于船头的偏转角
  70. local relativeAngle = targetAngle - yaw360
  71. if relativeAngle > 180 then relativeAngle = relativeAngle - 360 end
  72. if relativeAngle < -180 then relativeAngle = relativeAngle + 360 end
  73.  
  74. -- 控制左右转
  75. if relativeAngle > threshold then
  76. turnRight()
  77. elseif relativeAngle < -threshold then
  78. turnLeft()
  79. else
  80. stopTurn()
  81. end
  82.  
  83. -- 打印状态
  84. term.clear()
  85. term.setCursorPos(1,1)
  86. print(string.format("Waypoint %d/%d", i, #waypoints))
  87. print(string.format("Pos: (%.2f, %.2f)", pos.x, pos.z))
  88. print(string.format("Yaw 0~360°: %.1f°", yaw360))
  89. print(string.format("Target angle: %.1f°", targetAngle))
  90. print(string.format("Relative angle (right+ / left-): %.1f°", relativeAngle))
  91. print(string.format("Distance to target: %.2f", distance))
  92. print("Current action: "..currentAction)
  93.  
  94. sleep(0.5)
  95. end
  96. end
  97.  
  98. print("\nAll waypoints reached!")
  99. stopTurn()
  100.  
Advertisement
Add Comment
Please, Sign In to add comment