| | | 1 | | //======================================================================= |
| | | 2 | | // Navigator.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System; |
| | | 9 | | using System.Runtime.CompilerServices; |
| | | 10 | | using Chronicler; |
| | | 11 | | using FixedMathSharp; |
| | | 12 | | using GridForge.Grids; |
| | | 13 | | using Trailblazer.Navigation.Motor; |
| | | 14 | | using Trailblazer.Navigation.Steering; |
| | | 15 | | using Trailblazer.Navigation.Turning; |
| | | 16 | | using Trailblazer.Pathing; |
| | | 17 | | |
| | | 18 | | namespace Trailblazer.Navigation; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Base class for an object that owns movement, traversal state, and simulation flow. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <remarks> |
| | | 24 | | /// This class acts as a bridge between the simulation logic and the entity's external representation. |
| | | 25 | | /// It defines common traversal behaviors and lifecycle methods that can be extended by concrete implementations. |
| | | 26 | | /// </remarks> |
| | | 27 | | [Serializable] |
| | | 28 | | public abstract partial class Navigator : INavigate, IRecordable |
| | | 29 | | { |
| | | 30 | | #region Fields |
| | | 31 | | |
| | | 32 | | /// <inheritdoc cref="Position"/> |
| | | 33 | | protected Vector3d _position; |
| | | 34 | | |
| | | 35 | | /// <inheritdoc cref="LastPosition"/> |
| | | 36 | | protected Vector3d _lastPosition; |
| | | 37 | | |
| | | 38 | | /// <inheritdoc cref="Rotation"/> |
| | 277 | 39 | | protected FixedQuaternion _rotation = FixedQuaternion.Identity; |
| | | 40 | | |
| | | 41 | | /// <inheritdoc cref="Velocity"/> |
| | | 42 | | protected Vector3d _velocity; |
| | | 43 | | |
| | | 44 | | /// <inheritdoc cref="Speed"/> |
| | | 45 | | protected Fixed64 _speed; |
| | | 46 | | |
| | | 47 | | /// <inheritdoc cref="Acceleration"/> |
| | | 48 | | protected Vector3d _acceleration; |
| | | 49 | | |
| | | 50 | | /// <inheritdoc cref="NavigationProfile"/> |
| | | 51 | | private NavigationAgentProfile _navigationProfile; |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Stable runtime identity used for occupancy and steering coordination. |
| | | 55 | | /// </summary> |
| | | 56 | | /// <remarks> |
| | | 57 | | /// By default this is allocated deterministically from object setup order. |
| | | 58 | | /// Hosts can override it during setup |
| | | 59 | | /// when a broader simulation stack already owns stable agent ids. |
| | | 60 | | /// </remarks> |
| | | 61 | | protected Guid _globalId; |
| | | 62 | | |
| | 277 | 63 | | private byte _occupantGroupId = 1; |
| | | 64 | | |
| | | 65 | | private bool _isLockedOn; |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// The controller responsible for managing the object's desired movement direction. |
| | | 69 | | /// </summary> |
| | | 70 | | protected NavSteering? _steering; |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// The controller responsible for managing the object's rotation towards the movement direction. |
| | | 74 | | /// </summary> |
| | | 75 | | protected NavTurning? _turning; |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// The controller responsible for managing the object's movement and physics interactions. |
| | | 79 | | /// </summary> |
| | | 80 | | protected NavMotor? _motor; |
| | | 81 | | |
| | | 82 | | private Fixed64 _stuckThresholdSpeed; |
| | | 83 | | |
| | | 84 | | private bool _isGuided; |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// The change in position to apply during the current simulation frame. |
| | | 88 | | /// </summary> |
| | | 89 | | protected Vector3d _positionDelta; |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// The change in rotation to apply during the current simulation frame. |
| | | 93 | | /// </summary> |
| | 277 | 94 | | protected FixedQuaternion _rotationDelta = FixedQuaternion.Identity; |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// The velocity change computed during the simulation frame. |
| | | 98 | | /// </summary> |
| | | 99 | | protected Vector3d _velocityDelta; |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Indicates whether the Navigator has been set. |
| | | 103 | | /// </summary> |
| | | 104 | | protected bool _isSet; |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// Indicates whether the Navigator has been initialized. |
| | | 108 | | /// </summary> |
| | | 109 | | protected bool _isInitialized; |
| | | 110 | | |
| | | 111 | | /// <inheritdoc cref="TrekCondition"/> |
| | 277 | 112 | | protected TrekCondition _frameCondition = new(); |
| | | 113 | | |
| | | 114 | | /// <inheritdoc cref="TrekRequest"/> |
| | 277 | 115 | | protected TrekRequest _frameRequest = new(); |
| | | 116 | | |
| | | 117 | | private NavigationTransitionInstruction? _pendingTransition; |
| | | 118 | | |
| | | 119 | | private NavigationCommittedCellState? _lastCommittedCell; |
| | | 120 | | |
| | | 121 | | private TrailblazerWorldContext? _context; |
| | | 122 | | |
| | 277 | 123 | | private NavigatorHeightmapGroundingSettings _heightmapGrounding = new(); |
| | | 124 | | |
| | | 125 | | #endregion |
| | | 126 | | |
| | | 127 | | #region State - Identity / Transform |
| | | 128 | | |
| | | 129 | | /// <inheritdoc/> |
| | 1428 | 130 | | public Vector3d Position => _position; |
| | | 131 | | |
| | | 132 | | /// <inheritdoc/> |
| | 108 | 133 | | public Vector3d LastPosition => _lastPosition; |
| | | 134 | | |
| | | 135 | | /// <inheritdoc/> |
| | 118 | 136 | | public FixedQuaternion Rotation => _rotation; |
| | | 137 | | |
| | | 138 | | /// <inheritdoc/> |
| | | 139 | | public Vector3d Forward { get; protected set; } |
| | | 140 | | |
| | | 141 | | /// <inheritdoc/> |
| | 315 | 142 | | public Vector3d Velocity => _velocity; |
| | | 143 | | |
| | | 144 | | /// <inheritdoc/> |
| | 243 | 145 | | public Fixed64 Speed => _speed; |
| | | 146 | | |
| | | 147 | | /// <inheritdoc/> |
| | 89 | 148 | | public Vector3d Acceleration => _acceleration; |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Minimum velocity threshold used to determine if the object is considered stuck. |
| | | 152 | | /// </summary> |
| | 88 | 153 | | public Fixed64 StuckThresholdSpeed => _stuckThresholdSpeed; |
| | | 154 | | |
| | | 155 | | /// <summary> |
| | | 156 | | /// Indicates whether the Navigator is currently active and ready for simulation. |
| | | 157 | | /// </summary> |
| | 275 | 158 | | public bool IsActive => _isSet && _isInitialized; |
| | | 159 | | |
| | | 160 | | /// <summary> |
| | | 161 | | /// Gets the world context this navigator is bound to. |
| | | 162 | | /// </summary> |
| | 8 | 163 | | public TrailblazerWorldContext? Context => _context; |
| | | 164 | | |
| | | 165 | | #endregion |
| | | 166 | | |
| | | 167 | | #region State - Controllers |
| | | 168 | | |
| | | 169 | | /// <summary> |
| | | 170 | | /// The controller responsible for managing the object's desired movement direction. |
| | | 171 | | /// </summary> |
| | 315 | 172 | | public NavSteering? Steering => _steering; |
| | | 173 | | |
| | | 174 | | /// <summary> |
| | | 175 | | /// The controller responsible for managing the object's rotation towards the movement direction. |
| | | 176 | | /// </summary> |
| | 67 | 177 | | public NavTurning? Turning => _turning; |
| | | 178 | | |
| | | 179 | | /// <summary> |
| | | 180 | | /// The controller responsible for managing the object's movement and physics interactions. |
| | | 181 | | /// </summary> |
| | 146 | 182 | | public NavMotor? Motor => _motor; |
| | | 183 | | |
| | | 184 | | /// <summary> |
| | | 185 | | /// Indicates whether the current traversal session owns navigation guidance. |
| | | 186 | | /// </summary> |
| | 130 | 187 | | public bool IsGuided => _isGuided; |
| | | 188 | | |
| | | 189 | | #endregion |
| | | 190 | | |
| | | 191 | | #region Settings |
| | | 192 | | |
| | | 193 | | /// <summary> |
| | | 194 | | /// Gets the exact immutable navigation profile supplied during setup. |
| | | 195 | | /// </summary> |
| | 1040 | 196 | | public NavigationAgentProfile NavigationProfile => _navigationProfile; |
| | | 197 | | |
| | | 198 | | /// <inheritdoc/> |
| | 830 | 199 | | public KinematicBodyShape BodyShape => NavigationProfile.Shape; |
| | | 200 | | |
| | | 201 | | /// <inheritdoc/> |
| | 489 | 202 | | public Fixed64 Radius => BodyShape.Radius; |
| | | 203 | | |
| | | 204 | | /// <summary> |
| | | 205 | | /// Gets the derived world-space foot point used by navigation and locomotion. |
| | | 206 | | /// </summary> |
| | 207 | 207 | | public Vector3d FootPosition => Position + Vector3d.Down * BodyShape.RootToFootOffsetY; |
| | | 208 | | |
| | | 209 | | /// <summary> |
| | | 210 | | /// Gets the opt-in heightmap grounding settings owned by this navigator. |
| | | 211 | | /// </summary> |
| | 40 | 212 | | public NavigatorHeightmapGroundingSettings HeightmapGrounding => _heightmapGrounding; |
| | | 213 | | |
| | | 214 | | /// <summary> |
| | | 215 | | /// Gets or sets a value indicating whether the object is currently locked on to a target. |
| | | 216 | | /// </summary> |
| | 14 | 217 | | public bool IsLockedOn { get => _isLockedOn; set => _isLockedOn = value; } |
| | | 218 | | |
| | | 219 | | /// <summary>Gets the exact transition action currently awaiting host completion.</summary> |
| | 45 | 220 | | public NavigationTransitionInstruction? PendingTransition => _pendingTransition; |
| | | 221 | | |
| | | 222 | | /// <summary>Gets the effective navigation cell resolved after the latest motion commit.</summary> |
| | 49 | 223 | | public NavigationCommittedCellState? LastCommittedCell => _lastCommittedCell; |
| | | 224 | | |
| | | 225 | | /// <summary>Occurs after committed motion enters a different effective cell or leaves navigation data.</summary> |
| | | 226 | | public event Action<NavigationCommittedCellState?>? CommittedCellChanged; |
| | | 227 | | |
| | | 228 | | #endregion |
| | | 229 | | |
| | | 230 | | #region Voxel Occupancy |
| | | 231 | | |
| | | 232 | | /// <inheritdoc cref="_globalId"/> |
| | 1300 | 233 | | public Guid GlobalId => _globalId; |
| | | 234 | | |
| | | 235 | | /// <inheritdoc /> |
| | 6 | 236 | | public byte OccupantGroupId { get => _occupantGroupId; set => _occupantGroupId = value; } |
| | | 237 | | |
| | | 238 | | #endregion |
| | | 239 | | |
| | | 240 | | #region Setup / Initialization |
| | | 241 | | |
| | | 242 | | /// <summary> |
| | | 243 | | /// Initializes a new unbound navigator. Bind it to a context before setup. |
| | | 244 | | /// </summary> |
| | 10 | 245 | | protected Navigator() { } |
| | | 246 | | |
| | | 247 | | /// <summary> |
| | | 248 | | /// Initializes a new navigator bound to the supplied world context. |
| | | 249 | | /// </summary> |
| | 272 | 250 | | protected Navigator(TrailblazerWorldContext context) |
| | | 251 | | { |
| | 272 | 252 | | BindContext(context); |
| | 272 | 253 | | } |
| | | 254 | | |
| | | 255 | | /// <summary> |
| | | 256 | | /// Binds this navigator to a world context before setup. |
| | | 257 | | /// </summary> |
| | | 258 | | public virtual void BindContext(TrailblazerWorldContext context) |
| | | 259 | | { |
| | 280 | 260 | | TrailblazerWorldContext.ThrowIfUnusable(context); |
| | | 261 | | |
| | 280 | 262 | | if (ReferenceEquals(_context, context)) |
| | 1 | 263 | | return; |
| | | 264 | | |
| | 279 | 265 | | SwiftThrowHelper.ThrowIfTrue( |
| | 279 | 266 | | _isSet || _isInitialized, |
| | 279 | 267 | | message: "Navigator context cannot be changed after setup. Call Reset() before rebinding."); |
| | | 268 | | |
| | 278 | 269 | | _context = context; |
| | 278 | 270 | | _steering?.BindContext(context); |
| | 278 | 271 | | _motor?.BindContext(context); |
| | 278 | 272 | | _turning?.BindContext(context); |
| | 3 | 273 | | } |
| | | 274 | | |
| | | 275 | | /// <summary> |
| | | 276 | | /// Initializes and activates the object with the specified condition, position, and optional parameters. |
| | | 277 | | /// </summary> |
| | | 278 | | /// <param name="context">The world context that owns this navigator.</param> |
| | | 279 | | /// <param name="condition">The condition that determines how the object is initialized and activated.</param> |
| | | 280 | | /// <param name="position">The position in world coordinates where the object will be placed.</param> |
| | | 281 | | /// <param name="rotation">The optional rotation to apply to the object. If null, a default rotation is used.</param |
| | | 282 | | /// <param name="velocity">The optional initial velocity of the object. If null, the object is initialized with zero |
| | | 283 | | /// <param name="navigationProfile">The exact body geometry and traversal capabilities owned by the object.</param> |
| | | 284 | | /// <param name="globalId">The optional global identifier for the object. If null, a new identifier may be generated |
| | | 285 | | public virtual void Activate( |
| | | 286 | | TrailblazerWorldContext context, |
| | | 287 | | TrekCondition condition, |
| | | 288 | | Vector3d position, |
| | | 289 | | NavigationAgentProfile navigationProfile, |
| | | 290 | | FixedQuaternion? rotation = null, |
| | | 291 | | Vector3d? velocity = null, |
| | | 292 | | Guid? globalId = null) |
| | | 293 | | { |
| | 2 | 294 | | BindContext(context); |
| | 2 | 295 | | Activate(condition, position, navigationProfile, rotation, velocity, globalId); |
| | 2 | 296 | | } |
| | | 297 | | |
| | | 298 | | /// <summary> |
| | | 299 | | /// Initializes and activates the object with the specified condition, position, and optional parameters. |
| | | 300 | | /// </summary> |
| | | 301 | | /// <param name="condition">The condition that determines how the object is initialized and activated.</param> |
| | | 302 | | /// <param name="position">The position in world coordinates where the object will be placed.</param> |
| | | 303 | | /// <param name="rotation">The optional rotation to apply to the object. If null, a default rotation is used.</param |
| | | 304 | | /// <param name="velocity">The optional initial velocity of the object. If null, the object is initialized with zero |
| | | 305 | | /// <param name="navigationProfile">The exact body geometry and traversal capabilities owned by the object.</param> |
| | | 306 | | /// <param name="globalId">The optional global identifier for the object. If null, a new identifier may be generated |
| | | 307 | | public virtual void Activate( |
| | | 308 | | TrekCondition condition, |
| | | 309 | | Vector3d position, |
| | | 310 | | NavigationAgentProfile navigationProfile, |
| | | 311 | | FixedQuaternion? rotation = null, |
| | | 312 | | Vector3d? velocity = null, |
| | | 313 | | Guid? globalId = null) |
| | | 314 | | { |
| | 23 | 315 | | Setup(position, navigationProfile, rotation, velocity, globalId); |
| | 21 | 316 | | Initialize(condition); |
| | 21 | 317 | | } |
| | | 318 | | |
| | | 319 | | /// <summary> |
| | | 320 | | /// Sets the initial position, exact navigation profile, rotation, velocity, and optional stable identity. |
| | | 321 | | /// </summary> |
| | | 322 | | /// <param name="context">The world context that owns this navigator.</param> |
| | | 323 | | /// <param name="position">Initial world-space position.</param> |
| | | 324 | | /// <param name="rotation">Optional starting rotation.</param> |
| | | 325 | | /// <param name="velocity">Optional initial velocity.</param> |
| | | 326 | | /// <param name="navigationProfile">The exact body geometry and traversal capabilities owned by the object.</param> |
| | | 327 | | /// <param name="globalId">Optional host-provided stable identity. When omitted, Trailblazer assigns one determinist |
| | | 328 | | public virtual void Setup( |
| | | 329 | | TrailblazerWorldContext context, |
| | | 330 | | Vector3d position, |
| | | 331 | | NavigationAgentProfile navigationProfile, |
| | | 332 | | FixedQuaternion? rotation = null, |
| | | 333 | | Vector3d? velocity = null, |
| | | 334 | | Guid? globalId = null) |
| | | 335 | | { |
| | 4 | 336 | | BindContext(context); |
| | 4 | 337 | | Setup(position, navigationProfile, rotation, velocity, globalId); |
| | 4 | 338 | | } |
| | | 339 | | |
| | | 340 | | /// <summary> |
| | | 341 | | /// Sets the initial position, exact navigation profile, rotation, velocity, and optional stable identity. |
| | | 342 | | /// </summary> |
| | | 343 | | /// <param name="position">Initial world-space position.</param> |
| | | 344 | | /// <param name="rotation">Optional starting rotation.</param> |
| | | 345 | | /// <param name="velocity">Optional initial velocity.</param> |
| | | 346 | | /// <param name="navigationProfile">The exact body geometry and traversal capabilities owned by the object.</param> |
| | | 347 | | /// <param name="globalId">Optional host-provided stable identity. When omitted, Trailblazer assigns one determinist |
| | | 348 | | public virtual void Setup( |
| | | 349 | | Vector3d position, |
| | | 350 | | NavigationAgentProfile navigationProfile, |
| | | 351 | | FixedQuaternion? rotation = null, |
| | | 352 | | Vector3d? velocity = null, |
| | | 353 | | Guid? globalId = null) |
| | | 354 | | { |
| | 277 | 355 | | SwiftThrowHelper.ThrowIfTrue( |
| | 277 | 356 | | _isSet || _isInitialized, |
| | 277 | 357 | | message: "Navigator is already set up. Call Reset() before setting it up again."); |
| | | 358 | | |
| | 273 | 359 | | EnsureContextForSetup(); |
| | 272 | 360 | | navigationProfile.Validate(nameof(navigationProfile)); |
| | | 361 | | |
| | 272 | 362 | | SwiftThrowHelper.ThrowIfArgument( |
| | 272 | 363 | | globalId.HasValue && globalId.Value == Guid.Empty, |
| | 272 | 364 | | paramName: nameof(globalId), |
| | 272 | 365 | | message: "Navigator globalId cannot be Guid.Empty."); |
| | | 366 | | |
| | 271 | 367 | | _globalId = globalId ?? GenerateGUID(); |
| | | 368 | | |
| | 271 | 369 | | _lastPosition = _position = position; |
| | 271 | 370 | | _rotation = rotation ?? FixedQuaternion.Identity; |
| | 271 | 371 | | if (_rotation != FixedQuaternion.Identity) |
| | 117 | 372 | | Forward = _rotation.Rotate(Vector3d.Forward); |
| | | 373 | | else |
| | 154 | 374 | | Forward = Vector3d.Forward; |
| | 271 | 375 | | _velocity = velocity ?? Vector3d.Zero; |
| | 271 | 376 | | _navigationProfile = navigationProfile; |
| | | 377 | | |
| | 271 | 378 | | _isSet = true; |
| | 271 | 379 | | } |
| | | 380 | | |
| | | 381 | | /// <summary> |
| | | 382 | | /// Initializes the object by setting up its defaults, events, traversal state, and movement controller. |
| | | 383 | | /// </summary> |
| | | 384 | | public virtual void Initialize(TrekCondition condition) |
| | | 385 | | { |
| | 252 | 386 | | TrailblazerWorldContext context = RequireContext(); |
| | | 387 | | |
| | 251 | 388 | | _frameCondition = condition.Clone(); |
| | | 389 | | |
| | 251 | 390 | | _steering = new NavSteering(context); |
| | 251 | 391 | | _steering.BindPendingTransitionOwner(this); |
| | | 392 | | |
| | 251 | 393 | | _motor = new NavMotor(context, _frameCondition, CreateLocomotionProfile()); |
| | 251 | 394 | | _motor.SetVelocity(Velocity); |
| | | 395 | | |
| | 251 | 396 | | _turning = new NavTurning(context, Radius); |
| | | 397 | | |
| | 251 | 398 | | CheckVoxelOccupancy(true); |
| | | 399 | | |
| | 251 | 400 | | _isInitialized = true; |
| | 251 | 401 | | } |
| | | 402 | | |
| | | 403 | | /// <summary> |
| | | 404 | | /// Creates the locomotion profile used when this object initializes its motor. |
| | | 405 | | /// </summary> |
| | | 406 | | /// <remarks> |
| | | 407 | | /// Override this to install a smaller or custom locomotion set per object type while preserving |
| | | 408 | | /// the default profile for callers that do not opt in. |
| | | 409 | | /// </remarks> |
| | | 410 | | protected virtual LocomotionProfile CreateLocomotionProfile() |
| | | 411 | | { |
| | 290 | 412 | | return LocomotionProfile.CreateDefault(); |
| | | 413 | | } |
| | | 414 | | |
| | | 415 | | /// <summary> |
| | | 416 | | /// Resets the state of the object to its initial configuration, clearing any active conditions, requests, and inter |
| | | 417 | | /// </summary> |
| | | 418 | | /// <remarks> |
| | | 419 | | /// Call this method to reinitialize the object for reuse or to clear any ongoing operations. |
| | | 420 | | /// After calling this method, the object will be in the same state as after construction, and any previous state or |
| | | 421 | | /// intent will be lost. |
| | | 422 | | /// This method is intended to be overridden in derived classes to extend the reset behavior as needed. |
| | | 423 | | /// </remarks> |
| | | 424 | | public virtual void Reset() |
| | | 425 | | { |
| | 29 | 426 | | _frameCondition.Reset(); |
| | 29 | 427 | | _frameRequest.Reset(); |
| | 29 | 428 | | _isGuided = false; |
| | 29 | 429 | | _pendingTransition = null; |
| | 29 | 430 | | _lastCommittedCell = null; |
| | 29 | 431 | | _heightmapGrounding.Reset(); |
| | 29 | 432 | | _steering?.UnbindPendingTransitionOwner(this); |
| | 29 | 433 | | _steering?.Reset(); |
| | | 434 | | |
| | 29 | 435 | | if (_context != null && !_context.IsDisposed && _context.World.IsActive) |
| | 26 | 436 | | GridOccupantManager.TryDeregister(_context.World, this); |
| | | 437 | | |
| | 29 | 438 | | _isSet = false; |
| | 29 | 439 | | _isInitialized = false; |
| | 29 | 440 | | _context = null; |
| | 29 | 441 | | } |
| | | 442 | | |
| | | 443 | | #endregion |
| | | 444 | | |
| | | 445 | | #region Host Bindings |
| | | 446 | | |
| | | 447 | | /// <summary> |
| | | 448 | | /// Prewarms the steering movement-group coordinator from this object's currently loaded state. |
| | | 449 | | /// </summary> |
| | | 450 | | /// <remarks> |
| | | 451 | | /// This is primarily useful after loading several grouped navigators through Chronicler. Call it once |
| | | 452 | | /// for each loaded object before the next simulation frame if you want movement-group formation state |
| | | 453 | | /// available immediately. If it is skipped, grouped steering will still rejoin lazily on the next update. |
| | | 454 | | /// </remarks> |
| | | 455 | | public virtual void PrewarmMovementGroup() |
| | | 456 | | { |
| | 3 | 457 | | SwiftThrowHelper.ThrowIfTrue( |
| | 3 | 458 | | !IsActive, |
| | 3 | 459 | | message: "Navigator must be Setup and Initialized before prewarming movement groups."); |
| | | 460 | | |
| | 1 | 461 | | Steering!.PrewarmMovementGroup(this); |
| | 1 | 462 | | } |
| | | 463 | | |
| | | 464 | | #endregion |
| | | 465 | | |
| | | 466 | | #region Input / Travel Requests |
| | | 467 | | |
| | | 468 | | /// <summary> |
| | | 469 | | /// Constructs and applies a traversal request using high-level navigation input values. |
| | | 470 | | /// </summary> |
| | | 471 | | /// <param name="direction">Desired direction of travel.</param> |
| | | 472 | | /// <param name="rate">Rate of travel (walk, run, etc.).</param> |
| | | 473 | | /// <param name="isRequestingJump">Whether the agent is requesting a jump action.</param> |
| | | 474 | | /// <param name="isRequestingFlight">Whether the agent is requesting to fly or glide.</param> |
| | | 475 | | /// <param name="isRequestingSwim">Whether the agent is requesting active swim control while in liquid.</param> |
| | | 476 | | /// <param name="isRequestingClimb">Whether the agent is requesting to climb or maintain climb intent.</param> |
| | | 477 | | /// <param name="facingDirection">Optional world-space facing direction to use instead of facing along the movement |
| | | 478 | | /// <param name="canAffordJump">Frame-owned jump affordability answer for this request.</param> |
| | | 479 | | public virtual void ApplyInputTrekRequest( |
| | | 480 | | Vector3d? direction = null, |
| | | 481 | | TrekRate? rate = null, |
| | | 482 | | Vector3d? facingDirection = null, |
| | | 483 | | bool? isRequestingFlight = null, |
| | | 484 | | bool? isRequestingSwim = null, |
| | | 485 | | bool? isRequestingClimb = null, |
| | | 486 | | bool? isRequestingJump = null, |
| | | 487 | | bool canAffordJump = true) |
| | | 488 | | { |
| | 18 | 489 | | if (!IsActive) return; |
| | | 490 | | |
| | 16 | 491 | | Steering!.StopMove(); |
| | 16 | 492 | | _isGuided = false; |
| | 16 | 493 | | _pendingTransition = null; |
| | 16 | 494 | | _frameRequest.SetRequest( |
| | 16 | 495 | | direction: direction ?? Vector3d.Zero, |
| | 16 | 496 | | rate: rate ?? TrekRate.Stationary, |
| | 16 | 497 | | isRequestingJump: isRequestingJump ?? false, |
| | 16 | 498 | | isRequestingFlight: isRequestingFlight ?? false, |
| | 16 | 499 | | isRequestingSwim: isRequestingSwim ?? false, |
| | 16 | 500 | | isRequestingClimb: isRequestingClimb ?? false, |
| | 16 | 501 | | facingDirection: facingDirection, |
| | 16 | 502 | | canAffordJump: canAffordJump |
| | 16 | 503 | | ); |
| | 16 | 504 | | } |
| | | 505 | | |
| | | 506 | | /// <summary> |
| | | 507 | | /// Applies complete immutable graph-backed A* or flow-field intent. |
| | | 508 | | /// </summary> |
| | | 509 | | /// <param name="query">The exact query intent whose start point equals the current derived foot position.</param> |
| | | 510 | | /// <param name="rate">Desired movement rate.</param> |
| | | 511 | | /// <param name="isRequestingFlight">Whether the object intends to fly or glide during traversal.</param> |
| | | 512 | | /// <param name="isRequestingSwim">Whether the object intends to actively swim during traversal.</param> |
| | | 513 | | /// <param name="isRequestingClimb">Whether the object explicitly intends to climb during traversal.</param> |
| | | 514 | | /// <param name="isRequestingJump">Whether the object intends to jump during traversal.</param> |
| | | 515 | | /// <param name="canAffordJump">Frame-owned jump affordability answer for this request.</param> |
| | | 516 | | /// <param name="groupId">Optional shared movement-group identifier.</param> |
| | | 517 | | /// <exception cref="ArgumentException">Thrown when the query does not match the Navigator's exact navigation profil |
| | | 518 | | public virtual void ApplyGuidedTrekRequest( |
| | | 519 | | PathQuery query, |
| | | 520 | | TrekRate? rate = null, |
| | | 521 | | bool? isRequestingFlight = null, |
| | | 522 | | bool? isRequestingSwim = null, |
| | | 523 | | bool? isRequestingClimb = null, |
| | | 524 | | bool? isRequestingJump = null, |
| | | 525 | | bool canAffordJump = true, |
| | | 526 | | int groupId = -1) |
| | | 527 | | { |
| | 105 | 528 | | if (!IsActive) |
| | 1 | 529 | | return; |
| | | 530 | | |
| | 104 | 531 | | ValidateGuidedSurfaceQuery(query); |
| | 101 | 532 | | _pendingTransition = null; |
| | 101 | 533 | | _isGuided = true; |
| | 101 | 534 | | _frameRequest.SetRequest( |
| | 101 | 535 | | direction: Vector3d.Zero, |
| | 101 | 536 | | rate: rate ?? TrekRate.Stationary, |
| | 101 | 537 | | isRequestingJump: isRequestingJump ?? false, |
| | 101 | 538 | | isRequestingFlight: isRequestingFlight ?? false, |
| | 101 | 539 | | isRequestingSwim: isRequestingSwim ?? false, |
| | 101 | 540 | | isRequestingClimb: isRequestingClimb ?? false, |
| | 101 | 541 | | facingDirection: null, |
| | 101 | 542 | | canAffordJump: canAffordJump); |
| | | 543 | | |
| | 101 | 544 | | Steering!.ApplyPathQuery(query, groupId); |
| | 101 | 545 | | } |
| | | 546 | | |
| | | 547 | | /// <summary> |
| | | 548 | | /// Configures explicit heightmap grounding for concrete navigators that opt in during traversal checks. |
| | | 549 | | /// </summary> |
| | | 550 | | /// <param name="mode">Heightmap grounding behavior to use when <see cref="TryApplyHeightmapGrounding"/> is called.< |
| | | 551 | | /// <param name="layerName">Optional initial layer preference used before an active layer is established.</param> |
| | | 552 | | /// <param name="groundOffset">Optional extra root offset above the sampled ground Y.</param> |
| | | 553 | | /// <param name="snapTolerance">Optional maximum root-Y correction allowed for positional projection.</param> |
| | | 554 | | public virtual void ConfigureHeightmapGrounding( |
| | | 555 | | HeightmapGroundingMode mode, |
| | | 556 | | string? layerName = null, |
| | | 557 | | Fixed64? groundOffset = null, |
| | | 558 | | Fixed64? snapTolerance = null) |
| | | 559 | | { |
| | 27 | 560 | | _heightmapGrounding.Configure(mode, layerName, groundOffset, snapTolerance); |
| | 25 | 561 | | } |
| | | 562 | | |
| | | 563 | | /// <summary> |
| | | 564 | | /// Sets the frame-owned jump affordability snapshot used by <see cref="NavMotor"/> on the next traversal step. |
| | | 565 | | /// </summary> |
| | | 566 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 567 | | public virtual void SetFrameJumpAffordability(bool canAffordJump) => _frameRequest.CanAffordJump = canAffordJump; |
| | | 568 | | |
| | | 569 | | /// <summary> |
| | | 570 | | /// Called to make the agent jump if allowed and in a valid state. |
| | | 571 | | /// </summary> |
| | | 572 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 573 | | public virtual void ToggleGuidedJump(bool status) => _frameRequest.IsRequestingJump = status; |
| | | 574 | | |
| | | 575 | | /// <summary> |
| | | 576 | | /// Called to toggle controlled flight if supported by the installed locomotion profile. |
| | | 577 | | /// </summary> |
| | | 578 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 579 | | public virtual void ToggleGuidedFlight(bool status) => _frameRequest.IsRequestingFlight = status; |
| | | 580 | | |
| | | 581 | | /// <summary> |
| | | 582 | | /// Called to toggle active swim control if supported by the installed locomotion profile. |
| | | 583 | | /// </summary> |
| | | 584 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 585 | | public virtual void ToggleGuidedSwim(bool status) => _frameRequest.IsRequestingSwim = status; |
| | | 586 | | |
| | | 587 | | /// <summary> |
| | | 588 | | /// Called to toggle climb intent if supported by the installed locomotion profile. |
| | | 589 | | /// </summary> |
| | | 590 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 591 | | public virtual void ToggleGuidedClimb(bool status) |
| | 1 | 592 | | => _frameRequest.IsRequestingClimb = status; |
| | | 593 | | |
| | | 594 | | /// <summary>Completes the exact pending semantic action owned by the active guide.</summary> |
| | | 595 | | public virtual NavigationGuideStatus CompletePendingTransition( |
| | | 596 | | in NavigationTransitionInstruction instruction) |
| | | 597 | | { |
| | 12 | 598 | | if (_pendingTransition == null || Steering == null) |
| | 3 | 599 | | return NavigationGuideStatus.Stale; |
| | | 600 | | |
| | 9 | 601 | | NavigationGuideStatus status = Steering.CompletePendingTransition(instruction); |
| | 9 | 602 | | if (status != NavigationGuideStatus.Success) |
| | 4 | 603 | | return status; |
| | | 604 | | |
| | 5 | 605 | | TraversalTransitionLocomotionHints hints = _pendingTransition.Value.LocomotionHints; |
| | 5 | 606 | | _pendingTransition = null; |
| | 5 | 607 | | ApplyTransitionLocomotionHints(hints, pending: false); |
| | 5 | 608 | | return NavigationGuideStatus.Success; |
| | | 609 | | } |
| | | 610 | | |
| | | 611 | | internal void NotifySteeringSessionEnded() |
| | | 612 | | { |
| | 40 | 613 | | _isGuided = false; |
| | 40 | 614 | | if (_pendingTransition == null) |
| | 34 | 615 | | return; |
| | | 616 | | |
| | 6 | 617 | | _pendingTransition = null; |
| | 6 | 618 | | ApplyTransitionLocomotionHints(TraversalTransitionLocomotionHints.None, pending: false); |
| | 6 | 619 | | } |
| | | 620 | | |
| | | 621 | | /// <summary> |
| | | 622 | | /// Changes the speed at which the object is currently traveling without altering direction. |
| | | 623 | | /// </summary> |
| | | 624 | | /// <param name="rate">New traversal rate to apply (walk, run, etc.).</param> |
| | | 625 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 626 | | public virtual void SetGuidedTrekRate(TrekRate rate) => _frameRequest.Rate = rate; |
| | | 627 | | |
| | | 628 | | #endregion |
| | | 629 | | |
| | | 630 | | #region Simulation Lifecycle |
| | | 631 | | |
| | | 632 | | /// <summary> |
| | | 633 | | /// Attempts to resolve the direction the object should try to face for the current frame. |
| | | 634 | | /// </summary> |
| | | 635 | | protected virtual bool TryGetTurnDirection(TrekRequest request, out Vector3d turnDirection) |
| | | 636 | | { |
| | 30 | 637 | | if (request.FacingDirection.HasValue && request.FacingDirection.Value != Vector3d.Zero) |
| | | 638 | | { |
| | 2 | 639 | | turnDirection = request.FacingDirection.Value; |
| | 2 | 640 | | return true; |
| | | 641 | | } |
| | | 642 | | |
| | | 643 | | // Lock-on strafing/backpedaling keeps the current facing unless the host |
| | | 644 | | // explicitly supplies a facing override or the request is treated as sprinting. |
| | 28 | 645 | | if (!IsGuided |
| | 28 | 646 | | && IsLockedOn |
| | 28 | 647 | | && request.Rate != TrekRate.Fast) |
| | | 648 | | { |
| | 1 | 649 | | turnDirection = Vector3d.Zero; |
| | 1 | 650 | | return false; |
| | | 651 | | } |
| | | 652 | | |
| | 27 | 653 | | turnDirection = request.Direction; |
| | 27 | 654 | | return turnDirection != Vector3d.Zero; |
| | | 655 | | } |
| | | 656 | | |
| | | 657 | | /// <summary> |
| | | 658 | | /// Runs simulation logic for this object (input handling, steering, etc.). |
| | | 659 | | /// </summary> |
| | | 660 | | public virtual void Simulate() |
| | | 661 | | { |
| | 31 | 662 | | SwiftThrowHelper.ThrowIfTrue( |
| | 31 | 663 | | !IsActive, |
| | 31 | 664 | | message: "Navigator must be Setup and Initialized before Simulate()."); |
| | | 665 | | |
| | 30 | 666 | | Vector3d heading = Vector3d.Zero; |
| | 30 | 667 | | if (IsGuided) |
| | | 668 | | { |
| | 24 | 669 | | if (_pendingTransition is NavigationTransitionInstruction pending) |
| | | 670 | | { |
| | 4 | 671 | | if (Steering!.PendingTransitionGuideStatus == NavigationGuideStatus.Stale) |
| | 2 | 672 | | Steering.StopMove(); |
| | | 673 | | else |
| | 2 | 674 | | ApplyTransitionLocomotionHints(pending.LocomotionHints, pending: true); |
| | | 675 | | } |
| | | 676 | | else |
| | | 677 | | { |
| | 20 | 678 | | heading = Steering!.GetHeading(this, out NavigationTransitionInstruction? transition); |
| | 20 | 679 | | if (transition.HasValue) |
| | | 680 | | { |
| | 17 | 681 | | _pendingTransition = transition; |
| | 17 | 682 | | ApplyTransitionLocomotionHints(transition.Value.LocomotionHints, pending: true); |
| | 17 | 683 | | heading = Vector3d.Zero; |
| | | 684 | | } |
| | | 685 | | } |
| | | 686 | | } |
| | | 687 | | |
| | 30 | 688 | | _frameRequest.SetTransientState( |
| | 30 | 689 | | origin: Position, |
| | 30 | 690 | | footPosition: FootPosition, |
| | 30 | 691 | | rotation: Rotation, |
| | 30 | 692 | | direction: IsGuided ? heading : null |
| | 30 | 693 | | ); |
| | | 694 | | |
| | 30 | 695 | | if (TryGetTurnDirection(_frameRequest, out Vector3d turnDirection)) |
| | 6 | 696 | | Turning!.RequestTurnDirection(Forward, turnDirection); |
| | | 697 | | |
| | 30 | 698 | | if (Motor!.TryTraversal(_frameRequest, out Vector3d vDelta, out Vector3d pDelta, out FixedQuaternion rDelta)) |
| | | 699 | | { |
| | 26 | 700 | | AddVelocityDelta(vDelta); |
| | 26 | 701 | | AddPositionDelta(pDelta); |
| | 26 | 702 | | ApplyRotationDelta(rDelta); |
| | | 703 | | } |
| | | 704 | | |
| | 30 | 705 | | if (Turning!.TrySimulateTurn(_position, _lastPosition, Forward, _rotation, out FixedQuaternion appliedRotation)) |
| | 4 | 706 | | _rotation = appliedRotation; |
| | 30 | 707 | | } |
| | | 708 | | |
| | | 709 | | /// <summary> |
| | | 710 | | /// Finalizes traversal by updating movement calculations and applying corrections. |
| | | 711 | | /// </summary> |
| | | 712 | | /// <remarks> |
| | | 713 | | /// Should be called once every rendering/player interfacing frame, |
| | | 714 | | /// after physics bodies apply velocity changes. |
| | | 715 | | /// </remarks> |
| | | 716 | | public virtual void CommitFrameMotion() |
| | | 717 | | { |
| | 34 | 718 | | SwiftThrowHelper.ThrowIfTrue( |
| | 34 | 719 | | !IsActive, |
| | 34 | 720 | | message: "Navigator must be Setup and Initialized before CommitFrameMotion()."); |
| | | 721 | | |
| | 33 | 722 | | _lastPosition = _position; |
| | 33 | 723 | | _position += _positionDelta + _velocityDelta; |
| | | 724 | | |
| | 33 | 725 | | CheckVoxelOccupancy(); |
| | | 726 | | |
| | 33 | 727 | | if (_rotationDelta != FixedQuaternion.Identity) |
| | | 728 | | { |
| | 1 | 729 | | _rotation *= _rotationDelta; |
| | 1 | 730 | | _rotationDelta = FixedQuaternion.Identity; |
| | | 731 | | } |
| | | 732 | | |
| | 33 | 733 | | if (_rotation != FixedQuaternion.Identity) |
| | 6 | 734 | | Forward = _rotation.Rotate(Vector3d.Forward); |
| | | 735 | | else |
| | 27 | 736 | | Forward = Vector3d.Forward; |
| | | 737 | | |
| | 33 | 738 | | CheckTrekCondition(); |
| | | 739 | | |
| | 33 | 740 | | Vector3d previousVelocity = _velocity; |
| | 33 | 741 | | TrailblazerWorldContext context = RequireContext(); |
| | 33 | 742 | | Fixed64 invDelta = context.InvDeltaTime; |
| | 33 | 743 | | _velocity = (Position - LastPosition) * invDelta; |
| | 33 | 744 | | _speed = _velocity != Vector3d.Zero ? _velocity.Magnitude : Fixed64.Zero; |
| | 33 | 745 | | _acceleration = (_velocity - previousVelocity) * invDelta; |
| | | 746 | | |
| | 33 | 747 | | if (Steering!.ShouldMove && _acceleration != Vector3d.Zero) |
| | 6 | 748 | | _stuckThresholdSpeed = (_acceleration / context.FrameRate).Magnitude; |
| | | 749 | | else |
| | 27 | 750 | | _stuckThresholdSpeed = Fixed64.Zero; |
| | | 751 | | |
| | 33 | 752 | | _positionDelta = Vector3d.Zero; |
| | 33 | 753 | | _velocityDelta = Vector3d.Zero; |
| | | 754 | | |
| | 33 | 755 | | Motor!.FinalizeTraversal(Position, LastPosition, Rotation, _frameCondition, newFootPosition: FootPosition); |
| | | 756 | | |
| | | 757 | | // If the object is currently following a guided path, |
| | | 758 | | // reset only the transient request state to preserve path-following values. |
| | 33 | 759 | | if (IsGuided) |
| | 11 | 760 | | _frameRequest.ResetTransient(); |
| | | 761 | | else |
| | 22 | 762 | | _frameRequest.Reset(); |
| | | 763 | | |
| | 33 | 764 | | RebuildCommittedCellState(emitChange: true); |
| | 33 | 765 | | } |
| | | 766 | | |
| | | 767 | | /// <summary> |
| | | 768 | | /// Notifies the object that a collision occurred so collision-driven subsystem responses can run on the next simula |
| | | 769 | | /// </summary> |
| | | 770 | | public virtual void NotifyCollision() |
| | | 771 | | { |
| | 3 | 772 | | if (!IsActive) return; |
| | | 773 | | |
| | 1 | 774 | | Turning!.NotifyCollision(); |
| | 1 | 775 | | } |
| | | 776 | | |
| | | 777 | | #endregion |
| | | 778 | | |
| | | 779 | | #region Traversal Condition Management |
| | | 780 | | |
| | | 781 | | /// <summary> |
| | | 782 | | /// Updates the object to a grounded state using a host-provided platform snapshot plus surface settings. |
| | | 783 | | /// Inert snapshots still describe the contacted surface but opt out of moving-platform carry logic. |
| | | 784 | | /// </summary> |
| | | 785 | | public virtual void SetGroundContact( |
| | | 786 | | Fixed64 surfaceLevel, |
| | | 787 | | PlatformSnapshot platform = default, |
| | | 788 | | Fixed64? surfaceFriction = null, |
| | | 789 | | MotionTransfer motionTransfer = MotionTransfer.None, |
| | | 790 | | Fixed64? ceilingLevel = null, |
| | | 791 | | bool updateMotorState = false) |
| | | 792 | | { |
| | 27 | 793 | | SetTrekCondition( |
| | 27 | 794 | | medium: TraversalMedium.Solid, |
| | 27 | 795 | | surfaceLevel: surfaceLevel, |
| | 27 | 796 | | surfaceCondition: new GroundCondition() |
| | 27 | 797 | | { |
| | 27 | 798 | | Platform = platform, |
| | 27 | 799 | | SurfaceFriction = surfaceFriction ?? Fixed64.Zero, |
| | 27 | 800 | | MotionTransferState = motionTransfer |
| | 27 | 801 | | }, |
| | 27 | 802 | | ceilingLevel: ceilingLevel, |
| | 27 | 803 | | updateMotorState: updateMotorState); |
| | 27 | 804 | | } |
| | | 805 | | |
| | | 806 | | /// <summary> |
| | | 807 | | /// Updates the object to an airborne state while preserving the last known ground condition unless an override is p |
| | | 808 | | /// </summary> |
| | | 809 | | public virtual void SetAirborne( |
| | | 810 | | Fixed64? surfaceLevel = null, |
| | | 811 | | GroundCondition? launchCondition = null, |
| | | 812 | | Fixed64? ceilingLevel = null, |
| | | 813 | | bool updateMotorState = false) |
| | | 814 | | { |
| | 2 | 815 | | SetTrekCondition( |
| | 2 | 816 | | medium: TraversalMedium.Gas, |
| | 2 | 817 | | surfaceLevel: surfaceLevel, |
| | 2 | 818 | | surfaceCondition: launchCondition, |
| | 2 | 819 | | replaceGroundContact: launchCondition.HasValue, |
| | 2 | 820 | | ceilingLevel: ceilingLevel, |
| | 2 | 821 | | updateMotorState: updateMotorState); |
| | 2 | 822 | | } |
| | | 823 | | |
| | | 824 | | /// <summary> |
| | | 825 | | /// Updates the object to a water-contact state and clears any grounded platform contact. |
| | | 826 | | /// </summary> |
| | | 827 | | public virtual void SetWaterContact( |
| | | 828 | | Fixed64 surfaceLevel, |
| | | 829 | | Fixed64? ceilingLevel = null, |
| | | 830 | | bool updateMotorState = false) |
| | | 831 | | { |
| | 1 | 832 | | SetTrekCondition( |
| | 1 | 833 | | medium: TraversalMedium.Liquid, |
| | 1 | 834 | | surfaceLevel: surfaceLevel, |
| | 1 | 835 | | surfaceCondition: null, |
| | 1 | 836 | | ceilingLevel: ceilingLevel, |
| | 1 | 837 | | updateMotorState: updateMotorState); |
| | 1 | 838 | | } |
| | | 839 | | |
| | | 840 | | /// <summary> |
| | | 841 | | /// Updates the scout’s traversal state, including its current medium and surface information. |
| | | 842 | | /// </summary> |
| | | 843 | | /// <remarks> |
| | | 844 | | /// Make sure to update this before the next <see cref="CommitFrameMotion"/> so <see cref="NavMotor.FinalizeTraversa |
| | | 845 | | /// If the motor must see the new snapshot before the next <see cref="Simulate"/>, either pass |
| | | 846 | | /// <paramref name="updateMotorState"/> as <c>true</c> or call <see cref="SyncCurrentTrekConditionToMotor"/>. |
| | | 847 | | /// </remarks> |
| | | 848 | | /// <param name="medium">The traversal medium (e.g., ground, air, water).</param> |
| | | 849 | | /// <param name="surfaceLevel">The vertical surface level, if applicable.</param> |
| | | 850 | | /// <param name="surfaceCondition">The ground state data, if applicable.</param> |
| | | 851 | | /// <param name="replaceGroundContact">Whether to replace the current ground contact platform. This should be true |
| | | 852 | | /// <param name="ceilingLevel">The vertical ceiling level, if applicable.</param> |
| | | 853 | | /// <param name="updateMotorState">Flags whether or not to update the motor's internal surface state. Otherwise, it |
| | | 854 | | public virtual void SetTrekCondition( |
| | | 855 | | TraversalMedium? medium = null, |
| | | 856 | | Fixed64? surfaceLevel = null, |
| | | 857 | | GroundCondition? surfaceCondition = null, |
| | | 858 | | bool replaceGroundContact = true, |
| | | 859 | | Fixed64? ceilingLevel = null, |
| | | 860 | | bool updateMotorState = false) |
| | | 861 | | { |
| | 34 | 862 | | if (!IsActive) |
| | 1 | 863 | | return; |
| | | 864 | | |
| | 33 | 865 | | _frameCondition.Medium = medium ?? _frameCondition.Medium; |
| | 33 | 866 | | _frameCondition.SurfaceLevel = surfaceLevel ?? _frameCondition.SurfaceLevel; |
| | 33 | 867 | | if (replaceGroundContact) |
| | 29 | 868 | | _frameCondition.GroundState = surfaceCondition; |
| | 33 | 869 | | _frameCondition.CeilingLevel = ceilingLevel ?? _frameCondition.CeilingLevel; |
| | | 870 | | |
| | 33 | 871 | | if (updateMotorState) |
| | 13 | 872 | | SyncCurrentTrekConditionToMotor(); |
| | 33 | 873 | | } |
| | | 874 | | |
| | | 875 | | /// <summary> |
| | | 876 | | /// Pushes the current traversal snapshot into the motor before the next traversal phase begins. |
| | | 877 | | /// </summary> |
| | | 878 | | public virtual void SyncCurrentTrekConditionToMotor() |
| | | 879 | | { |
| | 16 | 880 | | SwiftThrowHelper.ThrowIfTrue( |
| | 16 | 881 | | !IsActive, |
| | 16 | 882 | | message: "Navigator must be Setup and Initialized before syncing traversal state to the motor."); |
| | | 883 | | |
| | 15 | 884 | | Motor!.SyncTraversalState(_frameCondition); |
| | 15 | 885 | | } |
| | | 886 | | |
| | | 887 | | /// <summary> |
| | | 888 | | /// Replaces the current traversal state with the given one. |
| | | 889 | | /// </summary> |
| | | 890 | | /// <param name="state">The new traversal condition to apply.</param> |
| | | 891 | | /// <param name="updateMotorState">Flags whether or not to immediately sync the new traversal snapshot into the moto |
| | | 892 | | public virtual void ReplaceTrekCondition(TrekCondition state, bool updateMotorState) |
| | | 893 | | { |
| | 4 | 894 | | SwiftThrowHelper.ThrowIfTrue( |
| | 4 | 895 | | updateMotorState && !IsActive, |
| | 4 | 896 | | message: "Navigator must be Setup and Initialized before syncing traversal state to the motor."); |
| | | 897 | | |
| | 3 | 898 | | _frameCondition = state.Clone(); |
| | 3 | 899 | | if (updateMotorState) |
| | 1 | 900 | | SyncCurrentTrekConditionToMotor(); |
| | 3 | 901 | | } |
| | | 902 | | |
| | | 903 | | /// <summary> |
| | | 904 | | /// Checks and updates the current traversal condition. |
| | | 905 | | /// </summary> |
| | | 906 | | public abstract void CheckTrekCondition(); |
| | | 907 | | |
| | | 908 | | #endregion |
| | | 909 | | |
| | | 910 | | #region Deltas - Position / Velocity / Rotation |
| | | 911 | | |
| | | 912 | | /// <summary> |
| | | 913 | | /// Applies a positional delta to the current position and updates the last known position accordingly. |
| | | 914 | | /// </summary> |
| | | 915 | | /// <remarks> |
| | | 916 | | /// This method adjusts both the current and last positions to maintain consistent velocity calculations. |
| | | 917 | | /// Use this method to apply external position changes without affecting velocity tracking. |
| | | 918 | | /// </remarks> |
| | | 919 | | /// <param name="delta"> |
| | | 920 | | /// The vector representing the positional change to apply. If the vector is <see cref="Vector3d.Zero"/>, no change |
| | | 921 | | /// </param> |
| | | 922 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 923 | | public virtual void AddPositionDelta(Vector3d delta) |
| | | 924 | | { |
| | 57 | 925 | | if (delta == Vector3d.Zero) return; |
| | | 926 | | |
| | 3 | 927 | | _positionDelta += delta; |
| | | 928 | | // shift last position so it doesn't alter object's velocity |
| | 3 | 929 | | _lastPosition += delta; |
| | 3 | 930 | | } |
| | | 931 | | |
| | | 932 | | /// <summary> |
| | | 933 | | /// Applies the specified rotation delta to the current rotation state. |
| | | 934 | | /// </summary> |
| | | 935 | | /// <param name="delta"> |
| | | 936 | | /// The rotation delta to apply. Must not be <see cref="FixedQuaternion.Identity"/>; otherwise, no change is made. |
| | | 937 | | /// </param> |
| | | 938 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 939 | | public virtual void ApplyRotationDelta(FixedQuaternion delta) |
| | | 940 | | { |
| | 55 | 941 | | if (delta == FixedQuaternion.Identity) return; |
| | | 942 | | |
| | 1 | 943 | | _rotationDelta *= delta; |
| | 1 | 944 | | } |
| | | 945 | | |
| | | 946 | | /// <summary> |
| | | 947 | | /// Adds the specified velocity change to the current velocity delta. |
| | | 948 | | /// </summary> |
| | | 949 | | /// <param name="delta">The velocity change to add. If this value is <see cref="Vector3d.Zero"/>, no change is appli |
| | | 950 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 951 | | public virtual void AddVelocityDelta(Vector3d delta) |
| | | 952 | | { |
| | 30 | 953 | | if (delta == Vector3d.Zero) return; |
| | | 954 | | |
| | | 955 | | // assume a mass of 1...for now |
| | 26 | 956 | | _velocityDelta += delta; |
| | 26 | 957 | | } |
| | | 958 | | |
| | | 959 | | #endregion |
| | | 960 | | |
| | | 961 | | #region Utilities |
| | | 962 | | |
| | | 963 | | /// <summary> |
| | | 964 | | /// Generates a new globally unique identifier (GUID) for use in object identification or tracking. |
| | | 965 | | /// </summary> |
| | | 966 | | /// <remarks>Override this method to customize GUID generation logic if a different strategy is required by derived |
| | | 967 | | /// <returns>A new <see cref="Guid"/> value that is guaranteed to be unique across space and time.</returns> |
| | | 968 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 969 | | protected virtual Guid GenerateGUID() |
| | | 970 | | { |
| | 265 | 971 | | TrailblazerWorldContext context = RequireContext(); |
| | 265 | 972 | | return context.Navigation.CreateNavigatorId(); |
| | | 973 | | } |
| | | 974 | | |
| | | 975 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 976 | | private TrailblazerWorldContext EnsureContextForSetup() |
| | | 977 | | { |
| | 273 | 978 | | if (_context != null) |
| | 272 | 979 | | return RequireContext(); |
| | | 980 | | |
| | 1 | 981 | | throw new InvalidOperationException( |
| | 1 | 982 | | "Navigator requires a TrailblazerWorldContext before setup. Pass a context to the constructor, " + |
| | 1 | 983 | | "call BindContext(context), or use Setup(context, ...)."); |
| | | 984 | | } |
| | | 985 | | |
| | | 986 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 987 | | private TrailblazerWorldContext RequireContext() |
| | | 988 | | { |
| | 1331 | 989 | | SwiftThrowHelper.ThrowIfTrue( |
| | 1331 | 990 | | _context == null, |
| | 1331 | 991 | | message: "Navigator requires a TrailblazerWorldContext before simulation."); |
| | | 992 | | |
| | 1330 | 993 | | TrailblazerWorldContext.ThrowIfUnusable(_context); |
| | 1330 | 994 | | return _context; |
| | | 995 | | } |
| | | 996 | | |
| | | 997 | | private void RebuildCommittedCellState(bool emitChange) |
| | | 998 | | { |
| | 64 | 999 | | NavigationCommittedCellResolveStatus status = TryResolveCommittedCellState( |
| | 64 | 1000 | | out NavigationCommittedCellState resolved); |
| | 64 | 1001 | | if (status == NavigationCommittedCellResolveStatus.Unavailable) |
| | 3 | 1002 | | return; |
| | | 1003 | | |
| | 61 | 1004 | | NavigationCommittedCellState? previous = _lastCommittedCell; |
| | 61 | 1005 | | NavigationCommittedCellState? current = status == NavigationCommittedCellResolveStatus.Resolved |
| | 61 | 1006 | | ? resolved |
| | 61 | 1007 | | : null; |
| | 61 | 1008 | | _lastCommittedCell = current; |
| | 61 | 1009 | | _steering?.ConsumePendingCommittedAreaPolicy(); |
| | | 1010 | | |
| | 61 | 1011 | | if (emitChange && !RepresentsSameCellEntry(previous, current)) |
| | 21 | 1012 | | CommittedCellChanged?.Invoke(current); |
| | 50 | 1013 | | } |
| | | 1014 | | |
| | | 1015 | | private NavigationCommittedCellResolveStatus TryResolveCommittedCellState( |
| | | 1016 | | out NavigationCommittedCellState state) |
| | | 1017 | | { |
| | 64 | 1018 | | TrailblazerWorldContext context = RequireContext(); |
| | 64 | 1019 | | if (!NavigatorOccupancyTracker.TryResolveVoxel( |
| | 64 | 1020 | | context.World, |
| | 64 | 1021 | | Position, |
| | 64 | 1022 | | out VoxelGrid? grid, |
| | 64 | 1023 | | out Voxel? voxel)) |
| | | 1024 | | { |
| | 2 | 1025 | | state = default; |
| | 2 | 1026 | | return NavigationCommittedCellResolveStatus.NoCell; |
| | | 1027 | | } |
| | | 1028 | | |
| | 62 | 1029 | | NavigationCommittedCellResolveStatus status = context.Pathing.TryResolveCommittedCell( |
| | 62 | 1030 | | grid!.Configuration.ToGridKey(), |
| | 62 | 1031 | | voxel!.WorldIndex, |
| | 62 | 1032 | | out NavigationCellAddress address, |
| | 62 | 1033 | | out NavigationAreaId area, |
| | 62 | 1034 | | out long graphVersion); |
| | 62 | 1035 | | if (status != NavigationCommittedCellResolveStatus.Resolved) |
| | | 1036 | | { |
| | 29 | 1037 | | state = default; |
| | 29 | 1038 | | return status; |
| | | 1039 | | } |
| | | 1040 | | |
| | 33 | 1041 | | state = new NavigationCommittedCellState( |
| | 33 | 1042 | | address, |
| | 33 | 1043 | | area, |
| | 33 | 1044 | | _frameCondition.Medium, |
| | 33 | 1045 | | graphVersion, |
| | 33 | 1046 | | _steering?.AreaPolicyForCommit, |
| | 33 | 1047 | | context.FrameCount); |
| | 33 | 1048 | | return NavigationCommittedCellResolveStatus.Resolved; |
| | | 1049 | | } |
| | | 1050 | | |
| | | 1051 | | private static bool RepresentsSameCellEntry( |
| | | 1052 | | NavigationCommittedCellState? first, |
| | | 1053 | | NavigationCommittedCellState? second) |
| | | 1054 | | { |
| | 32 | 1055 | | if (!first.HasValue || !second.HasValue) |
| | 26 | 1056 | | return first.HasValue == second.HasValue; |
| | | 1057 | | |
| | 6 | 1058 | | NavigationCommittedCellState left = first.Value; |
| | 6 | 1059 | | NavigationCommittedCellState right = second.Value; |
| | 6 | 1060 | | return left.Address == right.Address |
| | 6 | 1061 | | && left.Area == right.Area |
| | 6 | 1062 | | && left.Medium == right.Medium; |
| | | 1063 | | } |
| | | 1064 | | |
| | | 1065 | | private void ValidateGuidedSurfaceQuery(PathQuery query) |
| | | 1066 | | { |
| | 104 | 1067 | | SwiftThrowHelper.ThrowIfArgument( |
| | 104 | 1068 | | query.Agent != NavigationProfile |
| | 104 | 1069 | | || query.Start.Position != FootPosition |
| | 104 | 1070 | | || query.Traversal.StartMedium != _frameCondition.Medium, |
| | 104 | 1071 | | paramName: nameof(query), |
| | 104 | 1072 | | message: "Guided queries must use the Navigator profile, current foot position, and current traversal medium |
| | 101 | 1073 | | } |
| | | 1074 | | |
| | | 1075 | | private void ApplyTransitionLocomotionHints( |
| | | 1076 | | TraversalTransitionLocomotionHints hints, |
| | | 1077 | | bool pending) |
| | | 1078 | | { |
| | 30 | 1079 | | _frameRequest.IsRequestingClimb = pending |
| | 30 | 1080 | | ? (hints & TraversalTransitionLocomotionHints.RequestClimb) != 0 |
| | 30 | 1081 | | : (hints & TraversalTransitionLocomotionHints.PreserveClimbAfterCompletion) != 0; |
| | 30 | 1082 | | } |
| | | 1083 | | |
| | | 1084 | | #endregion |
| | | 1085 | | |
| | | 1086 | | #region Occupancy Mangement |
| | | 1087 | | |
| | | 1088 | | /// <summary> |
| | | 1089 | | /// Checks and updates the occupancy status of the current and previous voxels based on the object's position. |
| | | 1090 | | /// </summary> |
| | | 1091 | | /// <remarks> |
| | | 1092 | | /// This method ensures that the object is registered as an occupant in the correct voxel and |
| | | 1093 | | /// removed from the previous voxel if the position has changed. |
| | | 1094 | | /// It should be called whenever the object's position may have changed to maintain accurate occupancy tracking. |
| | | 1095 | | /// </remarks> |
| | | 1096 | | /// <param name="init">Indicates whether the occupancy check is being performed during initialization. |
| | | 1097 | | /// If set to <see langword="true"/>, the check is performed regardless of position changes. |
| | | 1098 | | /// </param> |
| | | 1099 | | protected virtual void CheckVoxelOccupancy(bool init = false) |
| | | 1100 | | { |
| | 315 | 1101 | | NavigatorOccupancyTracker.Update( |
| | 315 | 1102 | | RequireContext().World, |
| | 315 | 1103 | | this, |
| | 315 | 1104 | | Position, |
| | 315 | 1105 | | init ? Position : LastPosition, |
| | 315 | 1106 | | init); |
| | 315 | 1107 | | } |
| | | 1108 | | |
| | | 1109 | | #endregion |
| | | 1110 | | } |