gundami

Untitled

Oct 24th, 2025
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 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 = 364, z = 139},
  9. {x = -125, z = 38},
  10. {x = 177, z = -368}
  11. }
  12.  
  13. local currentAction = "forward"
  14.  
  15. -- 控制函数
  16. local function moveForward()
  17. if currentAction ~= "forward" then
  18. relay.setOutput("left", false)
  19. relay.setOutput("back", false)
  20. relay.setOutput("top", false) -- 保持 forward
  21. currentAction = "forward"
  22. end
  23. end
  24.  
  25. local function turnLeft()
  26. relay.setOutput("left", true)
  27. relay.setOutput("back", false)
  28. currentAction = "left"
  29. end
  30.  
  31. local function turnRight()
  32. relay.setOutput("left", false)
  33. relay.setOutput("back", true)
  34. currentAction = "right"
  35. end
  36.  
  37. local function stopAll()
  38. relay.setOutput("left", false)
  39. relay.setOutput("back", false)
  40. relay.setOutput("top", true) -- 停止 forward
  41. currentAction = "stop"
  42. end
  43.  
  44. -- 主循环,依次导航航点
  45. for i, waypoint in ipairs(waypoints) do
  46. print(string.format("Navigating to waypoint %d: (%.2f, %.2f)", i, waypoint.x, waypoint.z))
  47.  
  48. local arrived = false
  49. while not arrived do
  50. local pos = ship.getWorldspacePosition()
  51. local euler = ship.getEulerAnglesYXZ()
  52. local yaw = euler.y
  53. local yawDeg = math.deg(yaw)
  54. local yaw360 = (270 - yawDeg) % 360 -- 北=0,顺时针增大
  55.  
  56. local dx = waypoint.x - pos.x
  57. local dz = waypoint.z - pos.z
  58. local distance = math.sqrt(dx*dx + dz*dz)
  59.  
  60. -- 根据距离调整阈值和更新间隔
  61. local threshold, updateInterval
  62. if distance > 200 then
  63. threshold = 20
  64. updateInterval = 10
  65. else
  66. threshold = 10
  67. updateInterval = 1
  68. end
  69.  
  70. -- 根据航点位置调整到达判定距离
  71. local arrivalDistance
  72. if i == 1 or i == #waypoints then
  73. arrivalDistance = 16
  74. else
  75. arrivalDistance = 50
  76. end
  77.  
  78. -- 到达判定
  79. if distance <= arrivalDistance then
  80. stopTurn()
  81. arrived = true
  82. else
  83. -- 目标角度(世界坐标,北为0°)
  84. local targetAngle = math.deg(math.atan2(dx, -dz))
  85. if targetAngle < 0 then targetAngle = targetAngle + 360 end
  86.  
  87. -- 相对于船头的偏转角(右正左负)
  88. local relativeAngle = targetAngle - yaw360
  89. if relativeAngle > 180 then relativeAngle = relativeAngle - 360 end
  90. if relativeAngle < -180 then relativeAngle = relativeAngle + 360 end
  91.  
  92. -- 控制左右转
  93. if relativeAngle > threshold then
  94. turnRight()
  95. elseif relativeAngle < -threshold then
  96. turnLeft()
  97. else
  98. moveForward()
  99. end
  100. end
  101.  
  102. -- 打印状态
  103. term.clear()
  104. term.setCursorPos(1,1)
  105. print(string.format("Waypoint %d/%d", i, #waypoints))
  106. print(string.format("Pos: (%.2f, %.2f)", pos.x, pos.z))
  107. print(string.format("Yaw 0~360°: %.1f°", yaw360))
  108. print(string.format("Target angle: %.1f°", math.deg(math.atan2(dx, -dz))))
  109. print(string.format("Relative angle (right+ / left-): %.1f°", relativeAngle or 0))
  110. print(string.format("Distance to target: %.2f", distance))
  111. print("Current action: "..currentAction)
  112. print(string.format("Threshold: %d°, Update interval: %ds, Arrival distance: %.1f", threshold, updateInterval, arrivalDistance))
  113.  
  114. sleep(updateInterval)
  115. end
  116. end
  117.  
  118. -- 到达最后一个航点后停止
  119. stopAll()
  120. print("\nAll waypoints reached! Ship stopped.")
  121.  
Advertisement
Add Comment
Please, Sign In to add comment