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