| | | 1 | | //======================================================================= |
| | | 2 | | // TrailblazerWorldContext.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 FixedMathSharp; |
| | | 10 | | using GridForge.Grids; |
| | | 11 | | using SwiftCollections; |
| | | 12 | | using Trailblazer.Heightmaps; |
| | | 13 | | using Trailblazer.Navigation; |
| | | 14 | | using Trailblazer.Pathing; |
| | | 15 | | |
| | | 16 | | namespace Trailblazer; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Owns Trailblazer runtime state for one explicit <see cref="GridWorld"/>. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <remarks> |
| | | 22 | | /// This is the context-first host API for multi-world Trailblazer usage. It owns world lifetime, |
| | | 23 | | /// deterministic clock state, pathing state, reachability snapshots, and guide caches. |
| | | 24 | | /// </remarks> |
| | | 25 | | public sealed class TrailblazerWorldContext : IDisposable |
| | | 26 | | { |
| | 1 | 27 | | private static readonly object _worldOwnershipLock = new(); |
| | | 28 | | |
| | 1 | 29 | | private static readonly SwiftHashSet<GridWorld> _ownedWorlds = new(); |
| | | 30 | | |
| | 989 | 31 | | private readonly TrailblazerClock _clock = new(); |
| | | 32 | | |
| | 989 | 33 | | private readonly TrailblazerLifecycleHooks _hooks = new(); |
| | | 34 | | |
| | | 35 | | private readonly bool _ownsWorld; |
| | | 36 | | |
| | | 37 | | private bool _disposed; |
| | | 38 | | |
| | 989 | 39 | | private TrailblazerWorldContext( |
| | 989 | 40 | | GridWorld world, |
| | 989 | 41 | | bool ownsWorld, |
| | 989 | 42 | | TrailblazerWorldContextSettings settings) |
| | | 43 | | { |
| | 989 | 44 | | World = world; |
| | 989 | 45 | | Settings = settings; |
| | 989 | 46 | | _ownsWorld = ownsWorld; |
| | 989 | 47 | | Pathing = new TrailblazerPathingService(this); |
| | 989 | 48 | | Guides = new TrailblazerGuideService(this); |
| | 989 | 49 | | Navigation = new TrailblazerNavigationService(this); |
| | 989 | 50 | | Heightmaps = new TrailblazerHeightmapService(this); |
| | 989 | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Gets the explicit GridForge world owned or referenced by this context. |
| | | 55 | | /// </summary> |
| | | 56 | | public GridWorld World { get; } |
| | | 57 | | |
| | | 58 | | /// <summary>Gets the immutable finite runtime ceilings for this context.</summary> |
| | | 59 | | public TrailblazerWorldContextSettings Settings { get; } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Gets this context's world-local pathing service. |
| | | 63 | | /// </summary> |
| | | 64 | | public TrailblazerPathingService Pathing { get; } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Gets this context's world-local path guide service. |
| | | 68 | | /// </summary> |
| | | 69 | | public TrailblazerGuideService Guides { get; } |
| | | 70 | | |
| | | 71 | | internal TrailblazerNavigationService Navigation { get; } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Gets this context's world-local heightmap registry and sampling service. |
| | | 75 | | /// </summary> |
| | | 76 | | public TrailblazerHeightmapService Heightmaps { get; } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Gets whether this context has been disposed. |
| | | 80 | | /// </summary> |
| | 23421 | 81 | | public bool IsDisposed => _disposed; |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Gets this context's fixed simulation frame rate. |
| | | 85 | | /// </summary> |
| | | 86 | | public int FrameRate |
| | | 87 | | { |
| | | 88 | | get |
| | | 89 | | { |
| | 101 | 90 | | ThrowIfDisposed(); |
| | 100 | 91 | | return _clock.FrameRate; |
| | | 92 | | } |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Gets this context's fixed simulation time step. |
| | | 97 | | /// </summary> |
| | | 98 | | public Fixed64 DeltaTime |
| | | 99 | | { |
| | | 100 | | get |
| | | 101 | | { |
| | 6411 | 102 | | ThrowIfDisposed(); |
| | 6411 | 103 | | return _clock.DeltaTime; |
| | | 104 | | } |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | /// <summary> |
| | | 108 | | /// Gets the reciprocal of this context's fixed simulation time step. |
| | | 109 | | /// </summary> |
| | | 110 | | public Fixed64 InvDeltaTime |
| | | 111 | | { |
| | | 112 | | get |
| | | 113 | | { |
| | 4669 | 114 | | ThrowIfDisposed(); |
| | 4669 | 115 | | return _clock.InvDeltaTime; |
| | | 116 | | } |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | /// <summary> |
| | | 120 | | /// Gets this context's simulated frame count. |
| | | 121 | | /// </summary> |
| | | 122 | | public int FrameCount |
| | | 123 | | { |
| | | 124 | | get |
| | | 125 | | { |
| | 6010 | 126 | | ThrowIfDisposed(); |
| | 6010 | 127 | | return _clock.FrameCount; |
| | | 128 | | } |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | /// <summary> |
| | | 132 | | /// Gets this context's total simulated time. |
| | | 133 | | /// </summary> |
| | | 134 | | public Fixed64 TotalTime |
| | | 135 | | { |
| | | 136 | | get |
| | | 137 | | { |
| | 56 | 138 | | ThrowIfDisposed(); |
| | 56 | 139 | | return _clock.TotalTime; |
| | | 140 | | } |
| | | 141 | | } |
| | | 142 | | |
| | | 143 | | /// <summary> |
| | | 144 | | /// Gets this context's accumulated visualization time. |
| | | 145 | | /// </summary> |
| | | 146 | | public Fixed64 AccumulatedTime |
| | | 147 | | { |
| | | 148 | | get |
| | | 149 | | { |
| | 3 | 150 | | ThrowIfDisposed(); |
| | 3 | 151 | | return _clock.AccumulatedTime; |
| | | 152 | | } |
| | | 153 | | } |
| | | 154 | | |
| | | 155 | | /// <summary> |
| | | 156 | | /// Gets whether this context's visualization accumulation will reset on the next visualize call. |
| | | 157 | | /// </summary> |
| | | 158 | | public bool ResetAccumulation |
| | | 159 | | { |
| | | 160 | | get |
| | | 161 | | { |
| | 4 | 162 | | ThrowIfDisposed(); |
| | 4 | 163 | | return _clock.ResetAccumulation; |
| | | 164 | | } |
| | | 165 | | } |
| | | 166 | | |
| | | 167 | | /// <summary> |
| | | 168 | | /// Gets this context's visualization accumulation expressed in simulation frames. |
| | | 169 | | /// </summary> |
| | | 170 | | public Fixed64 ExpectedAccumulation |
| | | 171 | | { |
| | | 172 | | get |
| | | 173 | | { |
| | 3 | 174 | | ThrowIfDisposed(); |
| | 3 | 175 | | return _clock.ExpectedAccumulation; |
| | | 176 | | } |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | /// <summary> |
| | | 180 | | /// Attaches a context to a host-owned <see cref="GridWorld"/>. |
| | | 181 | | /// </summary> |
| | | 182 | | /// <param name="world">The active world to bind.</param> |
| | | 183 | | /// <param name="takeOwnership">True when disposing this context should dispose the supplied world.</param> |
| | | 184 | | /// <param name="settings">Optional immutable context ceilings; defaults to the recommended finite settings.</param> |
| | | 185 | | /// <returns>A context bound to <paramref name="world"/>.</returns> |
| | | 186 | | public static TrailblazerWorldContext Attach( |
| | | 187 | | GridWorld world, |
| | | 188 | | bool takeOwnership = false, |
| | | 189 | | TrailblazerWorldContextSettings? settings = null) |
| | | 190 | | { |
| | 41 | 191 | | SwiftThrowHelper.ThrowIfNull(world, nameof(world)); |
| | 40 | 192 | | SwiftThrowHelper.ThrowIfTrue( |
| | 40 | 193 | | !world.IsActive, |
| | 40 | 194 | | nameof(TrailblazerWorldContext), |
| | 40 | 195 | | "TrailblazerWorldContext requires an active GridWorld."); |
| | | 196 | | |
| | 39 | 197 | | return CreateRegistered(world, takeOwnership, settings ?? TrailblazerWorldContextSettings.Default); |
| | | 198 | | } |
| | | 199 | | |
| | | 200 | | /// <summary> |
| | | 201 | | /// Creates a context with an owned <see cref="GridWorld"/>. |
| | | 202 | | /// </summary> |
| | | 203 | | /// <param name="spatialGridCellSize">Spatial hash cell size for the created world.</param> |
| | | 204 | | /// <param name="settings">Optional immutable context ceilings; defaults to the recommended finite settings.</param> |
| | | 205 | | /// <returns>A context that owns its created world.</returns> |
| | | 206 | | public static TrailblazerWorldContext CreateOwned( |
| | | 207 | | int spatialGridCellSize = GridWorld.DefaultSpatialGridCellSize, |
| | | 208 | | TrailblazerWorldContextSettings? settings = null) |
| | | 209 | | { |
| | 951 | 210 | | return CreateRegistered( |
| | 951 | 211 | | new GridWorld(spatialGridCellSize), |
| | 951 | 212 | | ownsWorld: true, |
| | 951 | 213 | | settings ?? TrailblazerWorldContextSettings.Default); |
| | | 214 | | } |
| | | 215 | | |
| | | 216 | | /// <summary> |
| | | 217 | | /// Advances this context's deterministic simulation clock and ordered simulate hooks. |
| | | 218 | | /// </summary> |
| | | 219 | | public void Simulate() |
| | | 220 | | { |
| | 12186 | 221 | | ThrowIfDisposed(); |
| | 12185 | 222 | | _clock.Simulate(); |
| | 12185 | 223 | | Pathing.MaintainNavigationGraph(_clock.FrameCount); |
| | 12185 | 224 | | _hooks.InvokeSimulate(); |
| | 12185 | 225 | | } |
| | | 226 | | |
| | | 227 | | /// <summary> |
| | | 228 | | /// Runs this context's late-simulation step. |
| | | 229 | | /// </summary> |
| | | 230 | | public void LateSimulate() |
| | | 231 | | { |
| | 2 | 232 | | ThrowIfDisposed(); |
| | 2 | 233 | | _clock.LateSimulate(); |
| | 2 | 234 | | _hooks.InvokeLateSimulate(); |
| | 2 | 235 | | } |
| | | 236 | | |
| | | 237 | | /// <summary> |
| | | 238 | | /// Runs this context's visualization accumulation step. |
| | | 239 | | /// </summary> |
| | | 240 | | public void Visualize() |
| | | 241 | | { |
| | 5 | 242 | | ThrowIfDisposed(); |
| | 5 | 243 | | _clock.Visualize(); |
| | 5 | 244 | | _hooks.InvokeVisualize(); |
| | 5 | 245 | | } |
| | | 246 | | |
| | | 247 | | /// <summary> |
| | | 248 | | /// Resets this context's deterministic clock and context-local lifecycle hooks. |
| | | 249 | | /// </summary> |
| | | 250 | | public void Reset() |
| | | 251 | | { |
| | 22 | 252 | | ThrowIfDisposed(); |
| | 22 | 253 | | _clock.Reset(); |
| | 22 | 254 | | Navigation.Reset(); |
| | 22 | 255 | | Heightmaps.Reset(); |
| | 22 | 256 | | Pathing.Reset(); |
| | 22 | 257 | | _hooks.InvokeReset(); |
| | 22 | 258 | | } |
| | | 259 | | |
| | | 260 | | /// <summary> |
| | | 261 | | /// Updates this context's fixed simulation frame rate. |
| | | 262 | | /// </summary> |
| | | 263 | | /// <param name="frameRate">The new frame rate. Must be greater than zero.</param> |
| | | 264 | | public void SetFrameRate(int frameRate) |
| | | 265 | | { |
| | 11 | 266 | | ThrowIfDisposed(); |
| | 11 | 267 | | _clock.SetFrameRate(frameRate); |
| | 9 | 268 | | _hooks.InvokeFrameRateChanged(); |
| | 9 | 269 | | } |
| | | 270 | | |
| | | 271 | | /// <summary> |
| | | 272 | | /// Calculates the frame index containing the specified fixed-point timestamp. |
| | | 273 | | /// </summary> |
| | | 274 | | /// <param name="timestamp">The timestamp to resolve.</param> |
| | | 275 | | /// <returns>The zero-based frame index for the timestamp.</returns> |
| | | 276 | | public int GetFrameFromTime(Fixed64 timestamp) |
| | | 277 | | { |
| | 1 | 278 | | ThrowIfDisposed(); |
| | 1 | 279 | | return _clock.GetFrameFromTime(timestamp); |
| | | 280 | | } |
| | | 281 | | |
| | | 282 | | internal IDisposable RegisterOnSimulate(string owner, int order, Action callback) |
| | | 283 | | { |
| | 10 | 284 | | ThrowIfDisposed(); |
| | 10 | 285 | | return _hooks.RegisterOnSimulate(owner, order, callback); |
| | | 286 | | } |
| | | 287 | | |
| | | 288 | | internal IDisposable RegisterOnLateSimulate(string owner, int order, Action callback) |
| | | 289 | | { |
| | 1 | 290 | | ThrowIfDisposed(); |
| | 1 | 291 | | return _hooks.RegisterOnLateSimulate(owner, order, callback); |
| | | 292 | | } |
| | | 293 | | |
| | | 294 | | internal IDisposable RegisterOnVisualize(string owner, int order, Action callback) |
| | | 295 | | { |
| | 1 | 296 | | ThrowIfDisposed(); |
| | 1 | 297 | | return _hooks.RegisterOnVisualize(owner, order, callback); |
| | | 298 | | } |
| | | 299 | | |
| | | 300 | | internal IDisposable RegisterOnReset(string owner, int order, Action callback) |
| | | 301 | | { |
| | 2 | 302 | | ThrowIfDisposed(); |
| | 2 | 303 | | return _hooks.RegisterOnReset(owner, order, callback); |
| | | 304 | | } |
| | | 305 | | |
| | | 306 | | internal IDisposable RegisterOnFrameRateChanged(string owner, int order, Action callback) |
| | | 307 | | { |
| | 1 | 308 | | ThrowIfDisposed(); |
| | 1 | 309 | | return _hooks.RegisterOnFrameRateChanged(owner, order, callback); |
| | | 310 | | } |
| | | 311 | | |
| | | 312 | | /// <inheritdoc/> |
| | | 313 | | public void Dispose() |
| | | 314 | | { |
| | 992 | 315 | | lock (_worldOwnershipLock) |
| | | 316 | | { |
| | 992 | 317 | | if (_disposed) |
| | 4 | 318 | | return; |
| | | 319 | | |
| | 988 | 320 | | Pathing.Dispose(); |
| | 988 | 321 | | Heightmaps.Dispose(); |
| | 988 | 322 | | _disposed = true; |
| | 988 | 323 | | ReleaseWorldOwnership(this); |
| | | 324 | | |
| | 988 | 325 | | if (_ownsWorld && World.IsActive) |
| | 977 | 326 | | World.Dispose(); |
| | 988 | 327 | | } |
| | 992 | 328 | | } |
| | | 329 | | |
| | | 330 | | private static TrailblazerWorldContext CreateRegistered( |
| | | 331 | | GridWorld world, |
| | | 332 | | bool ownsWorld, |
| | | 333 | | TrailblazerWorldContextSettings settings) |
| | | 334 | | { |
| | 990 | 335 | | lock (_worldOwnershipLock) |
| | | 336 | | { |
| | 990 | 337 | | ThrowIfWorldOwned(world); |
| | 989 | 338 | | TrailblazerWorldContext context = new(world, ownsWorld, settings); |
| | 989 | 339 | | _ownedWorlds.Add(world); |
| | 989 | 340 | | return context; |
| | | 341 | | } |
| | 989 | 342 | | } |
| | | 343 | | |
| | | 344 | | private static void ThrowIfWorldOwned(GridWorld world) |
| | | 345 | | { |
| | 990 | 346 | | SwiftThrowHelper.ThrowIfTrue( |
| | 990 | 347 | | _ownedWorlds.Contains(world), |
| | 990 | 348 | | message: "GridWorld is already attached to an active TrailblazerWorldContext."); |
| | 989 | 349 | | } |
| | | 350 | | |
| | | 351 | | private static void ReleaseWorldOwnership(TrailblazerWorldContext context) => |
| | 988 | 352 | | _ownedWorlds.Remove(context.World); |
| | | 353 | | |
| | | 354 | | internal static void ThrowIfUnusable(TrailblazerWorldContext? context) |
| | | 355 | | { |
| | 16861 | 356 | | if (context == null) |
| | 1 | 357 | | throw new ArgumentNullException(nameof(context), "An explicit TrailblazerWorldContext is required."); |
| | 16860 | 358 | | if (context.IsDisposed) |
| | 1 | 359 | | throw new ObjectDisposedException(nameof(TrailblazerWorldContext)); |
| | 16859 | 360 | | SwiftThrowHelper.ThrowIfTrue( |
| | 16859 | 361 | | !context.World.IsActive, |
| | 16859 | 362 | | message: "An active TrailblazerWorldContext is required."); |
| | 16858 | 363 | | } |
| | | 364 | | |
| | | 365 | | private void ThrowIfDisposed() |
| | | 366 | | { |
| | 29499 | 367 | | SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(TrailblazerWorldContext)); |
| | 29499 | 368 | | SwiftThrowHelper.ThrowIfTrue( |
| | 29499 | 369 | | !World.IsActive, |
| | 29499 | 370 | | message: "TrailblazerWorldContext is bound to an inactive GridWorld."); |
| | 29497 | 371 | | } |
| | | 372 | | } |