| | | 1 | | using FixedMathSharp; |
| | | 2 | | using GridForge.Grids; |
| | | 3 | | using System; |
| | | 4 | | using System.Runtime.CompilerServices; |
| | | 5 | | using Trailblazer.Navigation.MovementGroups; |
| | | 6 | | using Trailblazer.Pathing; |
| | | 7 | | |
| | | 8 | | namespace Trailblazer.Navigation.Steering; |
| | | 9 | | |
| | | 10 | | public partial class NavSteering |
| | | 11 | | { |
| | | 12 | | #region Simulation Lifecycle |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Initializes the object by setting up its defaults, events, traversal state, and movement controller. |
| | | 16 | | /// </summary> |
| | | 17 | | protected virtual void OnInitialize(Fixed64 radius) |
| | | 18 | | { |
| | 202 | 19 | | UpdateOwnerRadius(radius); |
| | | 20 | | |
| | 202 | 21 | | LeaveMovementGroup(); |
| | | 22 | | |
| | 202 | 23 | | _stoppedFrameCount = 0; |
| | 202 | 24 | | _autoStopFrameCount = 0; |
| | | 25 | | |
| | 202 | 26 | | StopMultiplier = DefaultDirectStop; |
| | | 27 | | |
| | 202 | 28 | | _shouldRequestPathThisFrame = false; |
| | 202 | 29 | | _hasLineOfSightPath = false; |
| | 202 | 30 | | _shouldMove = false; |
| | | 31 | | |
| | 202 | 32 | | _isStuck = false; |
| | 202 | 33 | | _stuckFrameCount = 0; |
| | 202 | 34 | | _repathTries = 0; |
| | | 35 | | |
| | 202 | 36 | | _isAtDestination = false; |
| | | 37 | | |
| | 202 | 38 | | _currentRequest = null; |
| | 202 | 39 | | _trailGuide = null; |
| | 202 | 40 | | _requestedDestination = Vector3d.Zero; |
| | 202 | 41 | | _movementGroupSession.Reset(); |
| | 202 | 42 | | _movementGroupMode = MovementGroupTravelMode.None; |
| | 202 | 43 | | _currentRouteHasResolvedTopology = false; |
| | 202 | 44 | | _currentRouteUsesGuideTopology = false; |
| | 202 | 45 | | _currentRouteRequestsClimbIntent = false; |
| | 202 | 46 | | _currentRouteTopologyVersion = 0; |
| | 202 | 47 | | } |
| | | 48 | | |
| | | 49 | | internal virtual void UpdateOwnerRadius(Fixed64 radius) |
| | | 50 | | { |
| | | 51 | | // Fatter objects can afford to land imprecisely |
| | 244 | 52 | | _agentRadius = radius; |
| | 244 | 53 | | _closingDistance = FixedMath.Round(radius + ResolveVoxelSize()); |
| | 244 | 54 | | } |
| | | 55 | | |
| | | 56 | | internal void Reset() |
| | | 57 | | { |
| | 3 | 58 | | ReleaseTrailGuide(); |
| | 3 | 59 | | OnInitialize(_agentRadius); |
| | 3 | 60 | | } |
| | | 61 | | |
| | | 62 | | private Fixed64 ResolveVoxelSize() |
| | | 63 | | { |
| | 249 | 64 | | if (_context != null) |
| | 249 | 65 | | return _context.VoxelSize; |
| | 0 | 66 | | if (_currentRequest != null) |
| | 0 | 67 | | return _currentRequest.Context.VoxelSize; |
| | | 68 | | |
| | 0 | 69 | | return GridWorld.DefaultVoxelSize; |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | private TrailblazerWorldContext ResolveContext() |
| | | 73 | | { |
| | 799 | 74 | | TrailblazerWorldContext? context = _context ?? _currentRequest?.Context; |
| | 799 | 75 | | if (context != null) |
| | | 76 | | { |
| | 799 | 77 | | PathRequestContextResolver.ThrowIfUnusable(context); |
| | 799 | 78 | | return context; |
| | | 79 | | } |
| | | 80 | | |
| | 0 | 81 | | throw new InvalidOperationException("NavSteering requires an explicit TrailblazerWorldContext."); |
| | | 82 | | } |
| | | 83 | | |
| | 150 | 84 | | private MovementGroupCoordinatorState MovementGroups => ResolveContext().Navigation.MovementGroups; |
| | | 85 | | |
| | | 86 | | private void RemoveMovementGroupSession() |
| | | 87 | | { |
| | 328 | 88 | | ResolveContext().Navigation.MovementGroups.Remove(_movementGroupSession); |
| | 328 | 89 | | } |
| | | 90 | | |
| | 47 | 91 | | private int StuckFrameThresholdForContext => ResolveFrameRate() / 4; |
| | | 92 | | |
| | 12 | 93 | | private int AutoPauseStopTimeForContext => ResolveFrameRate() / 8; |
| | | 94 | | |
| | | 95 | | private int ResolveFrameRate() |
| | | 96 | | { |
| | 59 | 97 | | if (_context != null) |
| | 59 | 98 | | return _context.FrameRate; |
| | 0 | 99 | | if (_currentRequest != null) |
| | 0 | 100 | | return _currentRequest.Context.FrameRate; |
| | | 101 | | |
| | 0 | 102 | | return TrailblazerClock.DefaultFrameRate; |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Called every simulation step to handle agent steering and movement logic. |
| | | 107 | | /// </summary> |
| | | 108 | | public virtual Vector3d GetHeading(ISteer vessel) |
| | | 109 | | { |
| | 309 | 110 | | CacheOwner(vessel); |
| | | 111 | | |
| | 309 | 112 | | if (!CanMove) |
| | 1 | 113 | | return Vector3d.Zero; |
| | | 114 | | |
| | 308 | 115 | | if (!ShouldMove || IsAtDestination) |
| | 204 | 116 | | return FinalizeIdleHeading(vessel.Speed); |
| | | 117 | | |
| | 104 | 118 | | if (!TryEnsureCurrentRequest(out Vector3d heading)) |
| | 1 | 119 | | return heading; |
| | | 120 | | |
| | 103 | 121 | | bool usesVolumeGuidance = UsesVolumeGuidance(); |
| | 103 | 122 | | UpdateMovementGroupState(vessel.Position); |
| | | 123 | | |
| | 103 | 124 | | if (!TryPrepareMovementPathForHeading(vessel.Position, usesVolumeGuidance)) |
| | 3 | 125 | | return Vector3d.Zero; |
| | | 126 | | |
| | 100 | 127 | | UpdateTargetDirection(vessel); |
| | 100 | 128 | | if (ShouldArriveWithoutTrailGuide()) |
| | | 129 | | { |
| | 2 | 130 | | Arrive(); |
| | 2 | 131 | | return Vector3d.Zero; |
| | | 132 | | } |
| | | 133 | | |
| | 98 | 134 | | if (!CheckStuckStatus(vessel.Position, vessel.Speed, vessel.StuckThresholdSpeed)) |
| | | 135 | | { |
| | 1 | 136 | | TrailblazerLogger.DebugChannel.Info($"Stuck agent arriving!"); |
| | 1 | 137 | | Arrive(); |
| | 1 | 138 | | return Vector3d.Zero; |
| | | 139 | | } |
| | | 140 | | |
| | 97 | 141 | | UpdateTrailGuideProgress(vessel.Acceleration, vessel.Speed); |
| | 97 | 142 | | return FinalizeHeadingFrame(); |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | /// <summary> |
| | | 146 | | /// Periodically called to initiate a pathfinding query based on the current position and destination. |
| | | 147 | | /// Note: This will run once on the next `Simulate` call after calling `ApplyPathRequest` |
| | | 148 | | /// </summary> |
| | | 149 | | protected virtual bool ValidateMovementPath(Vector3d origin) |
| | | 150 | | { |
| | | 151 | | // Unit-size change detection must run before the shouldRequestPath gate. Without this, |
| | | 152 | | // external TrySetUnitSize calls between frames are silently ignored when |
| | | 153 | | // _shouldRequestPathThisFrame is already false, and no repath ever triggers. |
| | 93 | 154 | | if (_currentRequest!.UnitSize != _lastUnitSize) |
| | | 155 | | { |
| | 1 | 156 | | _lastUnitSize = _currentRequest.UnitSize; |
| | 1 | 157 | | _shouldRequestPathThisFrame = true; |
| | | 158 | | } |
| | | 159 | | |
| | 93 | 160 | | if (!_shouldRequestPathThisFrame) |
| | 21 | 161 | | return true; |
| | 72 | 162 | | _shouldRequestPathThisFrame = false; |
| | | 163 | | |
| | | 164 | | // update origin |
| | 72 | 165 | | bool ok = _currentRequest.TrySetOrigin(origin); |
| | 72 | 166 | | if (!ok || !_currentRequest.HasValidEndpoints) |
| | | 167 | | { |
| | 1 | 168 | | PublishRouteTopology(hasResolvedTopology: false, usesGuideTopology: false, requestsClimbIntent: false); |
| | 1 | 169 | | TrailblazerLogger.Channel.Warn($"Path request is using invalid endpoints."); |
| | 1 | 170 | | return false; |
| | | 171 | | } |
| | | 172 | | |
| | | 173 | | // shortcut if no path needed |
| | 71 | 174 | | if (_currentRequest.HasZeroDisplacement) |
| | | 175 | | { |
| | 1 | 176 | | PublishRouteTopology(hasResolvedTopology: true, usesGuideTopology: false, requestsClimbIntent: false); |
| | 1 | 177 | | return _repathTries == 0; |
| | | 178 | | } |
| | | 179 | | |
| | 70 | 180 | | if (_currentRequest is VolumePathRequest volumeRequest) |
| | | 181 | | { |
| | 12 | 182 | | _hasLineOfSightPath = IsVolumeDestinationInSight( |
| | 12 | 183 | | _currentRequest.Context, |
| | 12 | 184 | | origin, |
| | 12 | 185 | | Destination, |
| | 12 | 186 | | _currentRequest.UnitSize, |
| | 12 | 187 | | _currentRequest.AllowUnwalkableEndpoints, |
| | 12 | 188 | | volumeRequest.Medium, |
| | 12 | 189 | | _currentRequest.StartNode, |
| | 12 | 190 | | _currentRequest.EndNode); |
| | | 191 | | |
| | 12 | 192 | | _pathCheckCooldown = PathRecheckCooldownFrames; |
| | 12 | 193 | | if (_hasLineOfSightPath) |
| | | 194 | | { |
| | 5 | 195 | | ReleaseTrailGuide(); |
| | 5 | 196 | | PublishRouteTopology(hasResolvedTopology: true, usesGuideTopology: false, requestsClimbIntent: false); |
| | 5 | 197 | | return true; |
| | | 198 | | } |
| | | 199 | | } |
| | | 200 | | else |
| | | 201 | | { |
| | 58 | 202 | | _hasLineOfSightPath = IsDestinationInSight( |
| | 58 | 203 | | _currentRequest.Context, |
| | 58 | 204 | | origin, |
| | 58 | 205 | | Destination, |
| | 58 | 206 | | _currentRequest.UnitSize, |
| | 58 | 207 | | _currentRequest.AllowUnwalkableEndpoints); |
| | 58 | 208 | | if (_hasLineOfSightPath) |
| | | 209 | | { |
| | 31 | 210 | | PublishRouteTopology(hasResolvedTopology: true, usesGuideTopology: false, requestsClimbIntent: false); |
| | 31 | 211 | | return true; // no path required |
| | | 212 | | } |
| | | 213 | | } |
| | | 214 | | |
| | | 215 | | // request guide |
| | 34 | 216 | | ReleaseTrailGuide(); |
| | 34 | 217 | | _pathCheckCooldown = PathRecheckCooldownFrames; |
| | 34 | 218 | | if (!_currentRequest.IsValid || !_currentRequest.Context.Guides.RequestGuide(_currentRequest, out _trailGuide)) |
| | | 219 | | { |
| | 1 | 220 | | PublishRouteTopology(hasResolvedTopology: false, usesGuideTopology: false, requestsClimbIntent: false); |
| | 1 | 221 | | TrailblazerLogger.Channel.Warn($"Unable to retrieve a guide from {origin} to {Destination}."); |
| | 1 | 222 | | return false; |
| | | 223 | | } |
| | | 224 | | |
| | 33 | 225 | | PublishRouteTopology( |
| | 33 | 226 | | hasResolvedTopology: true, |
| | 33 | 227 | | usesGuideTopology: true, |
| | 33 | 228 | | requestsClimbIntent: GuidedClimbIntentResolver.Resolve(_currentRequest)); |
| | 33 | 229 | | return true; |
| | | 230 | | } |
| | | 231 | | |
| | | 232 | | /// <summary> |
| | | 233 | | /// Computes the steering direction toward the destination or along the path. |
| | | 234 | | /// </summary> |
| | | 235 | | protected virtual Vector3d FindTargetDirection(Vector3d position) |
| | | 236 | | { |
| | 91 | 237 | | Vector3d targetDirection = Vector3d.Zero; |
| | 91 | 238 | | if (HasLineOfSightPath) |
| | 46 | 239 | | targetDirection = Destination - position; |
| | 45 | 240 | | else if (HasTrailGuide) |
| | | 241 | | { |
| | 44 | 242 | | if (_trailGuide is IWaypointGuide waypointGuide) |
| | 38 | 243 | | targetDirection = waypointGuide.GetCurrentWaypointDirection(position); |
| | | 244 | | else |
| | 6 | 245 | | _trailGuide!.TryGetMovementDirection(position, out targetDirection); |
| | | 246 | | } |
| | | 247 | | |
| | 91 | 248 | | if (targetDirection == Vector3d.Zero) |
| | | 249 | | { |
| | 24 | 250 | | TrailblazerLogger.DebugChannel.Info($"No viable movement direction found."); |
| | 24 | 251 | | return Vector3d.Zero; |
| | | 252 | | } |
| | | 253 | | |
| | | 254 | | // This is now the direction we want to be travelling in |
| | 67 | 255 | | return targetDirection.Normalize(out _distanceToTarget); |
| | | 256 | | } |
| | | 257 | | |
| | | 258 | | /// <summary> |
| | | 259 | | /// Returns true if we’re within closing distance _and_ our heading has flipped, |
| | | 260 | | /// or if we’re very close relative to voxel size. |
| | | 261 | | /// </summary> |
| | | 262 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 263 | | public bool ShouldAdvanceToNextWaypoint() |
| | | 264 | | { |
| | 17 | 265 | | return (_distanceToTarget < _closingDistance |
| | 17 | 266 | | && Vector3d.Dot(TargetDirection, LastTargetDirection) < Fixed64.Epsilon) |
| | 17 | 267 | | || _distanceToTarget < _closingDistance * ResolveVoxelSize(); |
| | | 268 | | } |
| | | 269 | | |
| | | 270 | | /// <summary> |
| | | 271 | | /// Evaluates the agent's current movement direction and velocity, updating stuck and arrival state. |
| | | 272 | | /// </summary> |
| | | 273 | | protected virtual bool CheckStuckStatus( |
| | | 274 | | Vector3d position, |
| | | 275 | | Fixed64 speed, |
| | | 276 | | Fixed64 stuckThreshold) |
| | | 277 | | { |
| | 98 | 278 | | if (!CanAutoStop) |
| | 9 | 279 | | return true; |
| | | 280 | | |
| | 89 | 281 | | if (stuckThreshold <= Fixed64.Zero || speed >= stuckThreshold) |
| | 42 | 282 | | return ResetStuckStatus(); |
| | | 283 | | |
| | 47 | 284 | | _stuckFrameCount++; |
| | 47 | 285 | | if (_stuckFrameCount <= StuckFrameThresholdForContext) |
| | 41 | 286 | | return true; |
| | | 287 | | |
| | 6 | 288 | | return _repathTries < StuckRepathTries |
| | 6 | 289 | | ? TryRecoverFromStuck(position) |
| | 6 | 290 | | : DeclareHardStuck(); |
| | | 291 | | } |
| | | 292 | | |
| | | 293 | | private Vector3d FinalizeIdleHeading(Fixed64 speed) |
| | | 294 | | { |
| | 204 | 295 | | _targetDirection = Vector3d.Zero; |
| | 204 | 296 | | if (speed <= Fixed64.Epsilon) |
| | 203 | 297 | | _stoppedFrameCount++; |
| | | 298 | | |
| | 204 | 299 | | return FinalizeHeadingFrame(); |
| | | 300 | | } |
| | | 301 | | |
| | | 302 | | private bool TryEnsureCurrentRequest(out Vector3d heading) |
| | | 303 | | { |
| | 104 | 304 | | if (_currentRequest != null) |
| | | 305 | | { |
| | 103 | 306 | | heading = Vector3d.Zero; |
| | 103 | 307 | | return true; |
| | | 308 | | } |
| | | 309 | | |
| | 1 | 310 | | Arrive(); |
| | 1 | 311 | | heading = TargetDirection; |
| | 1 | 312 | | return false; |
| | | 313 | | } |
| | | 314 | | |
| | | 315 | | private bool TryPrepareMovementPathForHeading(Vector3d position, bool usesVolumeGuidance) |
| | | 316 | | { |
| | 103 | 317 | | if ((CanPathfind || usesVolumeGuidance) && !ValidateMovementPath(position)) |
| | | 318 | | { |
| | 2 | 319 | | HandleInvalidPath("Invalid path detected!"); |
| | 2 | 320 | | return false; |
| | | 321 | | } |
| | | 322 | | |
| | 101 | 323 | | RefreshLineOfSightState(position); |
| | 101 | 324 | | if (usesVolumeGuidance && !HasLineOfSightPath && !HasTrailGuide && !ValidateMovementPath(position)) |
| | | 325 | | { |
| | 1 | 326 | | HandleInvalidPath("Invalid volume path detected!"); |
| | 1 | 327 | | return false; |
| | | 328 | | } |
| | | 329 | | |
| | 100 | 330 | | return true; |
| | | 331 | | } |
| | | 332 | | |
| | | 333 | | private void RefreshLineOfSightState(Vector3d position) |
| | | 334 | | { |
| | 101 | 335 | | if (_pathCheckCooldown > 0) |
| | 71 | 336 | | return; |
| | | 337 | | |
| | 30 | 338 | | if (_currentRequest is VolumePathRequest volumeRequest) |
| | | 339 | | { |
| | 3 | 340 | | _hasLineOfSightPath = IsVolumeDestinationInSight( |
| | 3 | 341 | | _currentRequest.Context, |
| | 3 | 342 | | position, |
| | 3 | 343 | | Destination, |
| | 3 | 344 | | _currentRequest.UnitSize, |
| | 3 | 345 | | _currentRequest.AllowUnwalkableEndpoints, |
| | 3 | 346 | | volumeRequest.Medium, |
| | 3 | 347 | | _currentRequest.StartNode, |
| | 3 | 348 | | _currentRequest.EndNode); |
| | | 349 | | |
| | 3 | 350 | | if (_hasLineOfSightPath) |
| | | 351 | | { |
| | 1 | 352 | | ReleaseTrailGuide(); |
| | 1 | 353 | | PublishRouteTopology(hasResolvedTopology: true, usesGuideTopology: false, requestsClimbIntent: false); |
| | | 354 | | } |
| | | 355 | | } |
| | | 356 | | else |
| | | 357 | | { |
| | 27 | 358 | | IPathRequest currentRequest = _currentRequest!; |
| | 27 | 359 | | _hasLineOfSightPath = IsDestinationInSight( |
| | 27 | 360 | | currentRequest.Context, |
| | 27 | 361 | | position, |
| | 27 | 362 | | Destination, |
| | 27 | 363 | | currentRequest.UnitSize, |
| | 27 | 364 | | currentRequest.AllowUnwalkableEndpoints); |
| | | 365 | | |
| | 27 | 366 | | if (_hasLineOfSightPath) |
| | 27 | 367 | | PublishRouteTopology(hasResolvedTopology: true, usesGuideTopology: false, requestsClimbIntent: false); |
| | | 368 | | } |
| | | 369 | | |
| | 30 | 370 | | _pathCheckCooldown = PathRecheckCooldownFrames; |
| | 30 | 371 | | } |
| | | 372 | | |
| | | 373 | | private void HandleInvalidPath(string debugMessage) |
| | | 374 | | { |
| | 3 | 375 | | TrailblazerLogger.DebugChannel.Info($"{debugMessage}"); |
| | 3 | 376 | | Events.OnInvalidPath?.Invoke(); |
| | 3 | 377 | | Arrive(); |
| | 3 | 378 | | } |
| | | 379 | | |
| | | 380 | | private void UpdateTargetDirection(ISteer vessel) |
| | | 381 | | { |
| | 100 | 382 | | _lastTargetDirection = _targetDirection; |
| | 100 | 383 | | _targetDirection = FindTargetDirection(vessel.Position); |
| | 100 | 384 | | _targetDirection += ComputeCombinedSteering( |
| | 100 | 385 | | vessel.Position, |
| | 100 | 386 | | vessel.Velocity, |
| | 100 | 387 | | vessel.Speed, |
| | 100 | 388 | | vessel.Radius, |
| | 100 | 389 | | vessel.GlobalId); |
| | 100 | 390 | | } |
| | | 391 | | |
| | | 392 | | private bool ShouldArriveWithoutTrailGuide() |
| | | 393 | | { |
| | 100 | 394 | | if (HasTrailGuide) |
| | 53 | 395 | | return false; |
| | | 396 | | |
| | 47 | 397 | | Fixed64 moveAmount = FixedMath.Clamp01(TargetDirection.Magnitude); |
| | 47 | 398 | | bool reachedTarget = _distanceToTarget < _closingDistance * GetActiveStopMultiplier(); |
| | 47 | 399 | | bool noInput = moveAmount == Fixed64.Zero; |
| | 47 | 400 | | return reachedTarget || (!IsStuck && noInput); |
| | | 401 | | } |
| | | 402 | | |
| | | 403 | | private void UpdateTrailGuideProgress(Vector3d acceleration, Fixed64 speed) |
| | | 404 | | { |
| | 97 | 405 | | if (TargetDirection == Vector3d.Zero) |
| | 26 | 406 | | return; |
| | | 407 | | |
| | 71 | 408 | | if (_trailGuide is IWaypointGuide waypointGuide && ShouldAdvanceToNextWaypoint()) |
| | 17 | 409 | | waypointGuide.AdvanceWaypoint(); |
| | | 410 | | |
| | 71 | 411 | | if (HasTrailGuide) |
| | 31 | 412 | | SetDeceleration(acceleration, speed); |
| | 71 | 413 | | } |
| | | 414 | | |
| | | 415 | | private Vector3d FinalizeHeadingFrame() |
| | | 416 | | { |
| | 301 | 417 | | _autoStopFrameCount--; |
| | 301 | 418 | | _pathCheckCooldown--; |
| | | 419 | | |
| | 301 | 420 | | Events.OnStartTraversal?.Invoke(TargetDirection); |
| | 301 | 421 | | return TargetDirection; |
| | | 422 | | } |
| | | 423 | | |
| | | 424 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 425 | | private bool ResetStuckStatus() |
| | | 426 | | { |
| | 42 | 427 | | _isStuck = false; |
| | 42 | 428 | | _stuckFrameCount = 0; |
| | 42 | 429 | | _repathTries = 0; |
| | 42 | 430 | | return true; |
| | | 431 | | } |
| | | 432 | | |
| | | 433 | | private bool TryRecoverFromStuck(Vector3d position) |
| | | 434 | | { |
| | 5 | 435 | | _hasLineOfSightPath = false; |
| | | 436 | | |
| | 5 | 437 | | if (IsInGroup) |
| | 0 | 438 | | LeaveMovementGroup(); |
| | | 439 | | |
| | 5 | 440 | | if (TryApplyFallbackDirection(position)) |
| | 1 | 441 | | return true; |
| | | 442 | | |
| | 4 | 443 | | PreparePathRetry(); |
| | 4 | 444 | | _repathTries++; |
| | 4 | 445 | | return true; |
| | | 446 | | } |
| | | 447 | | |
| | | 448 | | private bool TryApplyFallbackDirection(Vector3d position) |
| | | 449 | | { |
| | 5 | 450 | | if (!HasTrailGuide || _trailGuide!.TryGetFallbackDirection(position, out Vector3d fallback) == false) |
| | 4 | 451 | | return false; |
| | | 452 | | |
| | 1 | 453 | | _targetDirection = fallback; |
| | 1 | 454 | | _repathTries++; |
| | 1 | 455 | | _stuckFrameCount = 0; |
| | 1 | 456 | | return true; |
| | | 457 | | } |
| | | 458 | | |
| | | 459 | | private void PreparePathRetry() |
| | | 460 | | { |
| | 4 | 461 | | _targetDirection = Vector3d.Zero; |
| | 4 | 462 | | _shouldRequestPathThisFrame = true; |
| | 4 | 463 | | DisposeCurrentTrailGuide(); |
| | 4 | 464 | | } |
| | | 465 | | |
| | | 466 | | private bool DeclareHardStuck() |
| | | 467 | | { |
| | 1 | 468 | | _isStuck = true; |
| | 1 | 469 | | DisposeCurrentTrailGuide(); |
| | 1 | 470 | | Events.OnIsStuck?.Invoke(); |
| | 1 | 471 | | return false; |
| | | 472 | | } |
| | | 473 | | |
| | | 474 | | private void DisposeCurrentTrailGuide() |
| | | 475 | | { |
| | 5 | 476 | | if (_trailGuide == null) |
| | 5 | 477 | | return; |
| | | 478 | | |
| | 0 | 479 | | (_currentRequest?.Context ?? ResolveContext()).Guides.ReturnGuide(_trailGuide, true); |
| | 0 | 480 | | _trailGuide = null; |
| | 0 | 481 | | } |
| | | 482 | | |
| | | 483 | | /// <summary> |
| | | 484 | | /// Adjusts the target direction to decelerate the object as it approaches its destination based on the specified |
| | | 485 | | /// acceleration and current speed. |
| | | 486 | | /// </summary> |
| | | 487 | | /// <remarks> |
| | | 488 | | /// This method is intended to be overridden in derived classes to customize deceleration behavior. |
| | | 489 | | /// It modulates the target direction to ensure smooth slowing as the object nears its target. |
| | | 490 | | /// </remarks> |
| | | 491 | | /// <param name="acceleration"> |
| | | 492 | | /// The acceleration vector used to determine the deceleration rate. |
| | | 493 | | /// If the vector is zero, a default braking power is used. |
| | | 494 | | /// </param> |
| | | 495 | | /// <param name="speed">The current speed of the object, used to calculate the distance required to slow down.</para |
| | | 496 | | protected virtual void SetDeceleration(Vector3d acceleration, Fixed64 speed) |
| | | 497 | | { |
| | | 498 | | // Scaling direction before passing to the motor lets us |
| | | 499 | | // modulate movement before acceleration is applied |
| | 32 | 500 | | Fixed64 deceleration = acceleration != Vector3d.Zero |
| | 32 | 501 | | ? acceleration.Magnitude |
| | 32 | 502 | | : BrakingPower; |
| | 32 | 503 | | Fixed64 slowDistance = speed / deceleration; |
| | 32 | 504 | | if (DistanceToTarget > Fixed64.Epsilon && DistanceToTarget <= slowDistance) |
| | | 505 | | { |
| | 1 | 506 | | Fixed64 closingSpeed = DistanceToTarget / slowDistance; |
| | 1 | 507 | | _targetDirection *= closingSpeed; // reduce magnitude = slow down |
| | | 508 | | } |
| | 32 | 509 | | } |
| | | 510 | | |
| | | 511 | | /// <summary> |
| | | 512 | | /// Triggers the arrival event and resets internal movement tracking. |
| | | 513 | | /// </summary> |
| | | 514 | | public void Arrive() |
| | | 515 | | { |
| | 24 | 516 | | StopMove(); |
| | | 517 | | |
| | 24 | 518 | | ReleaseTrailGuide(); |
| | 24 | 519 | | _currentRequest = null; |
| | 24 | 520 | | _requestedDestination = Vector3d.Zero; |
| | 24 | 521 | | _distanceToTarget = Fixed64.Zero; |
| | 24 | 522 | | _isAtDestination = true; |
| | 24 | 523 | | _destination = Vector3d.Zero; |
| | 24 | 524 | | _targetDirection = Vector3d.Zero; |
| | | 525 | | |
| | 24 | 526 | | Events.OnArrive?.Invoke(); |
| | 1 | 527 | | } |
| | | 528 | | |
| | | 529 | | /// <summary> |
| | | 530 | | /// Resets the movement and pathfinding logic, halting the agent. |
| | | 531 | | /// </summary> |
| | | 532 | | public virtual void StopMove() |
| | | 533 | | { |
| | 26 | 534 | | if (!_shouldMove) |
| | 2 | 535 | | return; |
| | | 536 | | |
| | 24 | 537 | | _autoStopFrameCount = 0; |
| | 24 | 538 | | _stuckFrameCount = 0; |
| | 24 | 539 | | _stoppedFrameCount = 0; |
| | | 540 | | |
| | 24 | 541 | | _shouldMove = false; |
| | 24 | 542 | | _shouldRequestPathThisFrame = false; |
| | 24 | 543 | | _hasLineOfSightPath = false; |
| | 24 | 544 | | PublishRouteTopology(hasResolvedTopology: false, usesGuideTopology: false, requestsClimbIntent: false, force: tr |
| | 24 | 545 | | LeaveMovementGroup(); |
| | | 546 | | |
| | 24 | 547 | | Events.OnStopMove?.Invoke(); |
| | 1 | 548 | | } |
| | | 549 | | |
| | | 550 | | #endregion |
| | | 551 | | } |