| | | 1 | | using FixedMathSharp; |
| | | 2 | | using GridForge; |
| | | 3 | | using GridForge.Grids; |
| | | 4 | | using GridForge.Spatial; |
| | | 5 | | using GridForge.Utility; |
| | | 6 | | using SwiftCollections; |
| | | 7 | | using SwiftCollections.Pool; |
| | | 8 | | using System; |
| | | 9 | | using System.Collections.Generic; |
| | | 10 | | using System.Runtime.CompilerServices; |
| | | 11 | | using System.Threading; |
| | | 12 | | |
| | | 13 | | namespace Trailblazer.Pathing; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Implements context-scoped chart registration, initialization, validation, |
| | | 17 | | /// neighbor discovery, and related pathing operations. |
| | | 18 | | /// </summary> |
| | | 19 | | internal static class PathManager |
| | | 20 | | { |
| | | 21 | | [ThreadStatic] |
| | | 22 | | private static PathingWorldState? _activeState; |
| | | 23 | | |
| | | 24 | | #region Pools |
| | | 25 | | |
| | 1 | 26 | | internal static readonly SwiftHashSetPool<SolidChartPartition> PartitionSetPool = new(); |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Pool of reusable <see cref="SolidChartPartition"/> instances used for partitioning the navigation grid. |
| | | 30 | | /// </summary> |
| | 2442 | 31 | | internal static SwiftObjectPool<SolidChartPartition> PartitionPool => ActiveState.PartitionPool; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Pool of reusable <see cref="VolumeChartPartition"/> instances used for authored raw-volume traversal. |
| | | 35 | | /// </summary> |
| | 607 | 36 | | internal static SwiftObjectPool<VolumeChartPartition> VolumeChartPartitionPool => ActiveState.VolumeChartPartitionPo |
| | | 37 | | |
| | | 38 | | #endregion |
| | | 39 | | |
| | | 40 | | #region Properties |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Gets an enumerable collection of all currently registered navigation charts. |
| | | 44 | | /// </summary> |
| | | 45 | | public static IEnumerable<NavigationChart> AllCharts |
| | | 46 | | { |
| | | 47 | | get |
| | | 48 | | { |
| | 66 | 49 | | _navigationChartMapLock.EnterReadLock(); |
| | | 50 | | try |
| | | 51 | | { |
| | 66 | 52 | | if (_navigationChartMap.Count == 0) |
| | 4 | 53 | | return Array.Empty<NavigationChart>(); |
| | | 54 | | |
| | 62 | 55 | | NavigationChart[] charts = new NavigationChart[_navigationChartMap.Count]; |
| | 62 | 56 | | int index = 0; |
| | 310 | 57 | | foreach (NavigationChartRegistration registration in _navigationChartMap.Values) |
| | 93 | 58 | | charts[index++] = registration.Chart; |
| | 62 | 59 | | return charts; |
| | | 60 | | } |
| | 132 | 61 | | finally { _navigationChartMapLock.ExitReadLock(); } |
| | 66 | 62 | | } |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Internal dictionary of all registered navigation charts, keyed by their unique names. |
| | | 67 | | /// </summary> |
| | | 68 | | private static SwiftDictionary<string, NavigationChartRegistration> _navigationChartMap => |
| | 10457 | 69 | | ActiveState.NavigationChartMap; |
| | | 70 | | |
| | | 71 | | private static SwiftDictionary<WorldVoxelIndex, ResolvedChartVoxelState> _resolvedChartVoxelStates => |
| | 14715 | 72 | | ActiveState.ResolvedChartVoxelStates; |
| | | 73 | | |
| | | 74 | | private static SwiftDictionary<ushort, SwiftDictionary<string, int>> _initializedChartTouchCountsByGridIndex => |
| | 8781 | 75 | | ActiveState.InitializedChartTouchCountsByGridIndex; |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Lock for managing concurrent access to <c>_navigationChartMap</c> operations. |
| | | 79 | | /// Ensures thread safety for read/write operations. |
| | | 80 | | /// </summary> |
| | 17760 | 81 | | private static ReaderWriterLockSlim _navigationChartMapLock => ActiveState.NavigationChartMapLock; |
| | | 82 | | |
| | | 83 | | private static int _nextChartRegistrationOrder |
| | | 84 | | { |
| | 906 | 85 | | get => ActiveState.NextChartRegistrationOrder; |
| | 2538 | 86 | | set => ActiveState.NextChartRegistrationOrder = value; |
| | | 87 | | } |
| | | 88 | | |
| | 235432 | 89 | | internal static PathingWorldState ActiveState => _activeState ?? throw new InvalidOperationException( |
| | 235432 | 90 | | "Trailblazer pathing operations require an explicit TrailblazerWorldContext."); |
| | | 91 | | |
| | | 92 | | internal static bool TryGetActiveState(out PathingWorldState? state) |
| | | 93 | | { |
| | 12672 | 94 | | if (_activeState != null) |
| | | 95 | | { |
| | 11763 | 96 | | state = _activeState; |
| | 11763 | 97 | | return true; |
| | | 98 | | } |
| | | 99 | | |
| | 909 | 100 | | state = null; |
| | 909 | 101 | | return false; |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | internal static IDisposable EnterState(PathingWorldState state) |
| | | 105 | | { |
| | 10565 | 106 | | return new PathingWorldStateScope(state); |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | private sealed class PathingWorldStateScope : IDisposable |
| | | 110 | | { |
| | | 111 | | private readonly PathingWorldState? _previousState; |
| | | 112 | | |
| | 10565 | 113 | | public PathingWorldStateScope(PathingWorldState state) |
| | | 114 | | { |
| | 10565 | 115 | | _previousState = _activeState; |
| | 10565 | 116 | | _activeState = state; |
| | 10565 | 117 | | } |
| | | 118 | | |
| | | 119 | | public void Dispose() |
| | | 120 | | { |
| | 10564 | 121 | | _activeState = _previousState; |
| | 10564 | 122 | | } |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | #endregion |
| | | 126 | | |
| | | 127 | | #region Lifecycle Hooks |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// Gets whether Trailblazer currently has an active configured grid world. |
| | | 131 | | /// </summary> |
| | 714 | 132 | | public static bool HasConfiguredWorld => _activeState != null; |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Gets the active configured grid world. |
| | | 136 | | /// </summary> |
| | 1 | 137 | | public static GridWorld ConfiguredWorld => GetConfiguredWorld(); |
| | | 138 | | |
| | | 139 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 140 | | private static void LinkWorld(GridWorld world) |
| | | 141 | | { |
| | 2246 | 142 | | if (_activeState != null) |
| | | 143 | | { |
| | 2246 | 144 | | if (!ReferenceEquals(_activeState.World, world)) |
| | 1 | 145 | | throw new InvalidOperationException("The supplied GridWorld does not belong to the active Trailblazer pa |
| | | 146 | | |
| | 2245 | 147 | | return; |
| | | 148 | | } |
| | | 149 | | |
| | 0 | 150 | | throw new InvalidOperationException( |
| | 0 | 151 | | "PathManager operations require TrailblazerWorldContext.Pathing to select the owning context."); |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 753 | 155 | | private static GridWorld GetConfiguredWorld() => ActiveState.World; |
| | | 156 | | |
| | | 157 | | internal static void Tick() |
| | | 158 | | { |
| | 1 | 159 | | if (HasConfiguredWorld) |
| | 1 | 160 | | ActiveState.ExternalGridBridge.FlushPendingGridChanges(); |
| | 1 | 161 | | } |
| | | 162 | | |
| | | 163 | | /// <summary> |
| | | 164 | | /// Clears all registered maps, partitions, and guide pools. |
| | | 165 | | /// </summary> |
| | | 166 | | public static void Reset() |
| | | 167 | | { |
| | 713 | 168 | | if (!HasConfiguredWorld) |
| | 48 | 169 | | return; |
| | | 170 | | |
| | 665 | 171 | | ResetPathingState(ActiveState, resetScopedRegistries: true, flushGuideCache: true); |
| | 665 | 172 | | } |
| | | 173 | | |
| | | 174 | | /// <summary> |
| | | 175 | | /// Clears all registered maps, partitions, and guide pools. |
| | | 176 | | /// </summary> |
| | | 177 | | public static void Reset(GridWorld world) |
| | | 178 | | { |
| | 1 | 179 | | LinkWorld(world); |
| | 1 | 180 | | ResetPathingState(ActiveState, resetScopedRegistries: true, flushGuideCache: true); |
| | 1 | 181 | | } |
| | | 182 | | |
| | | 183 | | internal static void ResetPathingState( |
| | | 184 | | PathingWorldState state, |
| | | 185 | | bool resetScopedRegistries, |
| | | 186 | | bool flushGuideCache) |
| | | 187 | | { |
| | 1632 | 188 | | using (EnterState(state)) |
| | | 189 | | { |
| | 1632 | 190 | | if (resetScopedRegistries) |
| | | 191 | | { |
| | 1632 | 192 | | VolumeMediumRules.Reset(); |
| | 1632 | 193 | | TraversalTransitionRegistry.Reset(); |
| | | 194 | | } |
| | | 195 | | |
| | 1632 | 196 | | ClearLiveGridState(state.World); |
| | 1632 | 197 | | PathManagerExternalGridBridge.ResetDiagnostics(); |
| | | 198 | | |
| | 1632 | 199 | | _navigationChartMapLock.EnterWriteLock(); |
| | | 200 | | try |
| | | 201 | | { |
| | 1632 | 202 | | _navigationChartMap.Clear(); |
| | 1632 | 203 | | _nextChartRegistrationOrder = 0; |
| | 1632 | 204 | | } |
| | | 205 | | finally |
| | | 206 | | { |
| | 1632 | 207 | | _navigationChartMapLock.ExitWriteLock(); |
| | 1632 | 208 | | } |
| | | 209 | | |
| | 1632 | 210 | | _resolvedChartVoxelStates.Clear(); |
| | 1632 | 211 | | _initializedChartTouchCountsByGridIndex.Clear(); |
| | 1632 | 212 | | ClearActiveAuthoredVolumeMediumCounts(); |
| | | 213 | | |
| | 1632 | 214 | | if (flushGuideCache && PathGuideFactory.IsPooling) |
| | 38 | 215 | | PathGuideFactory.FlushCache(true); |
| | | 216 | | |
| | 1632 | 217 | | SolidPartitionReachability.Invalidate(); |
| | 1632 | 218 | | } |
| | 1632 | 219 | | } |
| | | 220 | | |
| | | 221 | | #endregion |
| | | 222 | | |
| | | 223 | | #region Navigation Map Management |
| | | 224 | | |
| | | 225 | | /// <summary> |
| | | 226 | | /// Attempts to register a new navigation chart with the manager. |
| | | 227 | | /// </summary> |
| | | 228 | | /// <param name="chart">The map to register.</param> |
| | | 229 | | /// <param name="initializeChart">Whether to initialize the chart after registration succeeds.</param> |
| | | 230 | | /// <returns>True if successful, false if a duplicate name exists.</returns> |
| | | 231 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="chart"/> is null.</exception> |
| | | 232 | | /// <exception cref="ArgumentException"> |
| | | 233 | | /// Thrown when <paramref name="chart"/>'s interval does not match the owning world's voxel size. |
| | | 234 | | /// </exception> |
| | | 235 | | public static bool Register(NavigationChart chart, bool initializeChart = true) |
| | | 236 | | { |
| | 97 | 237 | | PathingWorldState state = ActiveState; |
| | 97 | 238 | | using (EnterState(state)) |
| | 97 | 239 | | return Register(state.World, chart, initializeChart); |
| | 96 | 240 | | } |
| | | 241 | | |
| | | 242 | | /// <summary> |
| | | 243 | | /// Attempts to register a new navigation chart with the manager. |
| | | 244 | | /// </summary> |
| | | 245 | | /// <param name="world">The grid world context for the chart.</param> |
| | | 246 | | /// <param name="chart">The map to register.</param> |
| | | 247 | | /// <param name="initializeChart">Whether to initialize the chart after registration succeeds.</param> |
| | | 248 | | /// <returns>True if successful, false if a duplicate name exists.</returns> |
| | | 249 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="chart"/> is null.</exception> |
| | | 250 | | /// <exception cref="ArgumentException"> |
| | | 251 | | /// Thrown when <paramref name="chart"/>'s interval does not match <paramref name="world"/>'s voxel size. |
| | | 252 | | /// </exception> |
| | | 253 | | public static bool Register(GridWorld world, NavigationChart chart, bool initializeChart = true) |
| | | 254 | | { |
| | 895 | 255 | | SwiftThrowHelper.ThrowIfNull(chart, nameof(chart)); |
| | 895 | 256 | | ThrowIfDirectWorldRegisterCall(); |
| | 893 | 257 | | LinkWorld(world); |
| | | 258 | | |
| | 892 | 259 | | return RegisterChartInternal( |
| | 892 | 260 | | world, |
| | 892 | 261 | | chart, |
| | 892 | 262 | | generatedTransitionIdPrefix: chart.Name, |
| | 892 | 263 | | precomputedGeneratedTransitions: null, |
| | 892 | 264 | | initializeChart); |
| | | 265 | | } |
| | | 266 | | |
| | | 267 | | /// <summary> |
| | | 268 | | /// Attempts to register the chart and generated transitions produced by a traversal authoring build. |
| | | 269 | | /// </summary> |
| | | 270 | | /// <param name="buildResult">The build result to register.</param> |
| | | 271 | | /// <param name="initializeChart">Whether to initialize the built chart after registration succeeds.</param> |
| | | 272 | | /// <returns>True when the chart and all generated transitions are registered successfully; otherwise, false.</retur |
| | | 273 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="buildResult"/> is null.</exception> |
| | | 274 | | public static bool Register(TraversalBuildResult buildResult, bool initializeChart = true) |
| | | 275 | | { |
| | 12 | 276 | | PathingWorldState state = ActiveState; |
| | 12 | 277 | | using (EnterState(state)) |
| | 12 | 278 | | return Register(state.World, buildResult, initializeChart); |
| | 12 | 279 | | } |
| | | 280 | | |
| | | 281 | | /// <summary> |
| | | 282 | | /// Attempts to register the chart and generated transitions produced by a traversal authoring build. |
| | | 283 | | /// </summary> |
| | | 284 | | /// <param name="world">The grid world context for the chart.</param> |
| | | 285 | | /// <param name="buildResult">The build result to register.</param> |
| | | 286 | | /// <param name="initializeChart">Whether to initialize the built chart after registration succeeds.</param> |
| | | 287 | | /// <returns>True when the chart and all generated transitions are registered successfully; otherwise, false.</retur |
| | | 288 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="buildResult"/> is null.</exception> |
| | | 289 | | public static bool Register(GridWorld world, TraversalBuildResult buildResult, bool initializeChart = true) |
| | | 290 | | { |
| | 16 | 291 | | SwiftThrowHelper.ThrowIfNull(buildResult, nameof(buildResult)); |
| | 16 | 292 | | ThrowIfDirectWorldRegisterCall(); |
| | 16 | 293 | | LinkWorld(world); |
| | | 294 | | |
| | 16 | 295 | | return RegisterChartInternal( |
| | 16 | 296 | | world, |
| | 16 | 297 | | buildResult.Chart, |
| | 16 | 298 | | buildResult.GeneratedTransitionIdPrefix, |
| | 16 | 299 | | buildResult.GeneratedTransitions, |
| | 16 | 300 | | initializeChart); |
| | | 301 | | } |
| | | 302 | | |
| | | 303 | | private static void ThrowIfDirectWorldRegisterCall() |
| | | 304 | | { |
| | 911 | 305 | | if (_activeState != null) |
| | 909 | 306 | | return; |
| | | 307 | | |
| | 2 | 308 | | throw new InvalidOperationException( |
| | 2 | 309 | | "PathManager.Register(world, ...) is no longer a multi-world registration API. " + |
| | 2 | 310 | | "Create a TrailblazerWorldContext for that GridWorld and call context.Pathing.Register(...), " + |
| | 2 | 311 | | "or initialize the single default facade and call PathManager.Register(chart)."); |
| | | 312 | | } |
| | | 313 | | |
| | | 314 | | private static bool RegisterChartInternal( |
| | | 315 | | GridWorld world, |
| | | 316 | | NavigationChart chart, |
| | | 317 | | string generatedTransitionIdPrefix, |
| | | 318 | | TraversalTransition[]? precomputedGeneratedTransitions, |
| | | 319 | | bool initializeChart) |
| | | 320 | | { |
| | 908 | 321 | | _navigationChartMapLock.EnterWriteLock(); |
| | | 322 | | try |
| | | 323 | | { |
| | 908 | 324 | | if (_navigationChartMap.ContainsKey(chart.Name)) |
| | 1 | 325 | | return false; |
| | | 326 | | |
| | 907 | 327 | | ValidateChartVoxelSizeCompatibility(world, chart); |
| | | 328 | | |
| | 906 | 329 | | var registration = new NavigationChartRegistration( |
| | 906 | 330 | | chart, |
| | 906 | 331 | | unchecked(++_nextChartRegistrationOrder), |
| | 906 | 332 | | generatedTransitionIdPrefix); |
| | 906 | 333 | | _navigationChartMap.Add(chart.Name, registration); |
| | 906 | 334 | | } |
| | 1816 | 335 | | finally { _navigationChartMapLock.ExitWriteLock(); } |
| | | 336 | | |
| | 906 | 337 | | if (!TryRegisterManagedGeneratedTransitions(chart.Name, precomputedGeneratedTransitions)) |
| | | 338 | | { |
| | 1 | 339 | | RemoveChartFromRegistry(chart.Name); |
| | 1 | 340 | | return false; |
| | | 341 | | } |
| | | 342 | | |
| | 905 | 343 | | if (initializeChart) |
| | 886 | 344 | | InitializeChart(world, chart.Name); |
| | | 345 | | |
| | 905 | 346 | | return true; |
| | 1 | 347 | | } |
| | | 348 | | |
| | | 349 | | private static void ValidateChartVoxelSizeCompatibility(GridWorld world, NavigationChart chart) |
| | | 350 | | { |
| | 907 | 351 | | if (chart.Interval == world.VoxelSize) |
| | 906 | 352 | | return; |
| | | 353 | | |
| | 1 | 354 | | throw new ArgumentException( |
| | 1 | 355 | | $"Navigation chart '{chart.Name}' uses interval {chart.Interval}, but the owning GridWorld uses voxel size { |
| | 1 | 356 | | "Rebuild the chart with the context voxel size before registration.", |
| | 1 | 357 | | nameof(chart)); |
| | | 358 | | } |
| | | 359 | | |
| | | 360 | | /// <summary> |
| | | 361 | | /// Checks if a navigation map is already registered under the specified name. |
| | | 362 | | /// </summary> |
| | | 363 | | /// <param name="name">The map name to check.</param> |
| | | 364 | | /// <returns>True if registered; otherwise, false.</returns> |
| | | 365 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 366 | | public static bool IsChartRegistered(string name) |
| | | 367 | | { |
| | 18 | 368 | | _navigationChartMapLock.EnterReadLock(); |
| | 18 | 369 | | try { return _navigationChartMap.ContainsKey(name); } |
| | 36 | 370 | | finally { _navigationChartMapLock.ExitReadLock(); } |
| | 18 | 371 | | } |
| | | 372 | | |
| | | 373 | | /// <summary> |
| | | 374 | | /// Attempts to retrieve a registered navigation chart by name. |
| | | 375 | | /// </summary> |
| | | 376 | | /// <param name="name">The name of the map.</param> |
| | | 377 | | /// <param name="chart">The retrieved navigation chart.</param> |
| | | 378 | | /// <returns>True if the map exists; otherwise, false.</returns> |
| | | 379 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 380 | | public static bool TryGetNavigationChart(string name, out NavigationChart chart) |
| | | 381 | | { |
| | 1048 | 382 | | _navigationChartMapLock.EnterReadLock(); |
| | | 383 | | try |
| | | 384 | | { |
| | 1048 | 385 | | if (_navigationChartMap.TryGetValue(name, out NavigationChartRegistration registration)) |
| | | 386 | | { |
| | 1046 | 387 | | chart = registration.Chart; |
| | 1046 | 388 | | return true; |
| | | 389 | | } |
| | | 390 | | |
| | 2 | 391 | | chart = null!; |
| | 2 | 392 | | return false; |
| | | 393 | | } |
| | | 394 | | finally |
| | | 395 | | { |
| | 1048 | 396 | | _navigationChartMapLock.ExitReadLock(); |
| | 1048 | 397 | | } |
| | 1048 | 398 | | } |
| | | 399 | | |
| | | 400 | | /// <summary> |
| | | 401 | | /// Attempts to retrieve live registration state for a chart by name. |
| | | 402 | | /// </summary> |
| | | 403 | | /// <param name="name">The registered chart name.</param> |
| | | 404 | | /// <param name="registration">The live chart registration.</param> |
| | | 405 | | /// <returns>True when a registration exists; otherwise, false.</returns> |
| | | 406 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 407 | | public static bool TryGetNavigationChartRegistration( |
| | | 408 | | string name, |
| | | 409 | | out NavigationChartRegistration registration) |
| | | 410 | | { |
| | 2323 | 411 | | _navigationChartMapLock.EnterReadLock(); |
| | 2323 | 412 | | try { return TryGetNavigationChartRegistration_NoLock(name, out registration); } |
| | 4646 | 413 | | finally { _navigationChartMapLock.ExitReadLock(); } |
| | 2323 | 414 | | } |
| | | 415 | | |
| | | 416 | | /// <summary> |
| | | 417 | | /// Gets whether a registered chart is currently initialized into live voxel state. |
| | | 418 | | /// </summary> |
| | | 419 | | /// <param name="name">The registered chart name.</param> |
| | | 420 | | /// <returns>True when the chart has an initialized live registration; otherwise, false.</returns> |
| | | 421 | | public static bool IsChartInitialized(string name) |
| | | 422 | | { |
| | 160 | 423 | | return TryGetNavigationChartRegistration(name, out NavigationChartRegistration registration) |
| | 160 | 424 | | && registration.IsInitialized; |
| | | 425 | | } |
| | | 426 | | |
| | | 427 | | /// <summary> |
| | | 428 | | /// Gets whether an authored chart's current registration is initialized. |
| | | 429 | | /// </summary> |
| | | 430 | | /// <param name="chart">The authored chart to inspect.</param> |
| | | 431 | | /// <returns>True when the chart is registered and initialized; otherwise, false.</returns> |
| | | 432 | | public static bool IsChartInitialized(NavigationChart chart) |
| | | 433 | | { |
| | 33 | 434 | | return chart != null && IsChartInitialized(chart.Name); |
| | | 435 | | } |
| | | 436 | | |
| | | 437 | | private static bool TryGetNavigationChartRegistration_NoLock( |
| | | 438 | | string name, |
| | | 439 | | out NavigationChartRegistration registration) |
| | | 440 | | { |
| | 4534 | 441 | | if (_navigationChartMap.TryGetValue(name, out registration)) |
| | 4521 | 442 | | return true; |
| | | 443 | | |
| | 13 | 444 | | registration = null!; |
| | 13 | 445 | | return false; |
| | | 446 | | } |
| | | 447 | | |
| | | 448 | | /// <summary> |
| | | 449 | | /// Attempts to retrieve the winning effective authored cell at the provided voxel. |
| | | 450 | | /// </summary> |
| | | 451 | | /// <param name="voxelIndex">The voxel to inspect.</param> |
| | | 452 | | /// <param name="cell">The effective authored cell currently winning overlap resolution.</param> |
| | | 453 | | /// <returns>True when the voxel currently has an effective authored chart cell; otherwise, false.</returns> |
| | | 454 | | public static bool TryGetEffectiveCell(WorldVoxelIndex voxelIndex, out NavigationChartCell cell) |
| | | 455 | | { |
| | 3 | 456 | | if (TryGetResolvedChartVoxelState(voxelIndex, out ResolvedChartVoxelState? state)) |
| | | 457 | | { |
| | 2 | 458 | | cell = state!.EffectiveCell; |
| | 2 | 459 | | return true; |
| | | 460 | | } |
| | | 461 | | |
| | 1 | 462 | | cell = NavigationChartCell.Empty; |
| | 1 | 463 | | return false; |
| | | 464 | | } |
| | | 465 | | |
| | | 466 | | /// <summary> |
| | | 467 | | /// Attempts to retrieve the winning effective authored cell at the provided world position. |
| | | 468 | | /// </summary> |
| | | 469 | | /// <param name="world">The grid world context to search.</param> |
| | | 470 | | /// <param name="worldPosition">The world position to inspect.</param> |
| | | 471 | | /// <param name="cell">The effective authored cell currently winning overlap resolution.</param> |
| | | 472 | | /// <returns>True when the position resolves to a voxel with an effective authored chart cell; otherwise, false.</re |
| | | 473 | | public static bool TryGetEffectiveCell(GridWorld world, Vector3d worldPosition, out NavigationChartCell cell) |
| | | 474 | | { |
| | 25 | 475 | | LinkWorld(world); |
| | 25 | 476 | | if (TryGetResolvedChartVoxelState(world, worldPosition, out _, out ResolvedChartVoxelState? state)) |
| | | 477 | | { |
| | 15 | 478 | | cell = state!.EffectiveCell; |
| | 15 | 479 | | return true; |
| | | 480 | | } |
| | | 481 | | |
| | 10 | 482 | | cell = NavigationChartCell.Empty; |
| | 10 | 483 | | return false; |
| | | 484 | | } |
| | | 485 | | |
| | | 486 | | /// <summary> |
| | | 487 | | /// Attempts to retrieve the winning effective authored cell at the provided world position using the configured wor |
| | | 488 | | /// </summary> |
| | | 489 | | public static bool TryGetEffectiveCell(Vector3d worldPosition, out NavigationChartCell cell) |
| | | 490 | | { |
| | 15 | 491 | | return TryGetEffectiveCell(GetConfiguredWorld(), worldPosition, out cell); |
| | | 492 | | } |
| | | 493 | | |
| | | 494 | | /// <summary> |
| | | 495 | | /// Attempts to retrieve the chart currently winning overlap resolution at the provided voxel. |
| | | 496 | | /// </summary> |
| | | 497 | | /// <param name="voxelIndex">The voxel to inspect.</param> |
| | | 498 | | /// <param name="chartName">The effective chart owner.</param> |
| | | 499 | | /// <returns>True when the voxel currently has an effective chart owner; otherwise, false.</returns> |
| | | 500 | | public static bool TryGetEffectiveChartOwner(WorldVoxelIndex voxelIndex, out string? chartName) |
| | | 501 | | { |
| | 3 | 502 | | if (TryGetResolvedChartVoxelState(voxelIndex, out ResolvedChartVoxelState? state)) |
| | | 503 | | { |
| | 2 | 504 | | chartName = state!.EffectiveChartOwner; |
| | 2 | 505 | | return true; |
| | | 506 | | } |
| | | 507 | | |
| | 1 | 508 | | chartName = null; |
| | 1 | 509 | | return false; |
| | | 510 | | } |
| | | 511 | | |
| | | 512 | | /// <summary> |
| | | 513 | | /// Attempts to retrieve the chart currently winning overlap resolution at the provided world position. |
| | | 514 | | /// </summary> |
| | | 515 | | /// <param name="world">The grid world context to search.</param> |
| | | 516 | | /// <param name="worldPosition">The world position to inspect.</param> |
| | | 517 | | /// <param name="chartName">The effective chart owner.</param> |
| | | 518 | | /// <returns>True when the position resolves to a voxel with an effective chart owner; otherwise, false.</returns> |
| | | 519 | | public static bool TryGetEffectiveChartOwner(GridWorld world, Vector3d worldPosition, out string? chartName) |
| | | 520 | | { |
| | 10 | 521 | | LinkWorld(world); |
| | 10 | 522 | | if (TryGetResolvedChartVoxelState(world, worldPosition, out _, out ResolvedChartVoxelState? state)) |
| | | 523 | | { |
| | 7 | 524 | | chartName = state!.EffectiveChartOwner; |
| | 7 | 525 | | return true; |
| | | 526 | | } |
| | | 527 | | |
| | 3 | 528 | | chartName = null; |
| | 3 | 529 | | return false; |
| | | 530 | | } |
| | | 531 | | |
| | | 532 | | /// <summary> |
| | | 533 | | /// Attempts to retrieve the chart currently winning overlap resolution at the provided world position using the con |
| | | 534 | | /// </summary> |
| | | 535 | | public static bool TryGetEffectiveChartOwner(Vector3d worldPosition, out string? chartName) |
| | | 536 | | { |
| | 6 | 537 | | return TryGetEffectiveChartOwner(GetConfiguredWorld(), worldPosition, out chartName); |
| | | 538 | | } |
| | | 539 | | |
| | | 540 | | /// <summary> |
| | | 541 | | /// Attempts to retrieve the closest currently active directed transition of the requested type. |
| | | 542 | | /// </summary> |
| | | 543 | | /// <param name="world">The grid world context to search.</param> |
| | | 544 | | /// <param name="worldPosition">The position to measure from.</param> |
| | | 545 | | /// <param name="transitionType">The directed handoff family to search.</param> |
| | | 546 | | /// <param name="transition"> |
| | | 547 | | /// The closest active directed transition. Bidirectional registrations may return the reversed |
| | | 548 | | /// directed view when that source anchor is closer. |
| | | 549 | | /// </param> |
| | | 550 | | /// <returns>True when at least one active directed transition of that type exists; otherwise, false.</returns> |
| | | 551 | | public static bool TryGetClosestActiveTransition( |
| | | 552 | | GridWorld world, |
| | | 553 | | Vector3d worldPosition, |
| | | 554 | | TraversalTransitionType transitionType, |
| | | 555 | | out TraversalTransition transition) |
| | | 556 | | { |
| | 8 | 557 | | LinkWorld(world); |
| | 8 | 558 | | int[] sourceGridIndices = TraversalTransitionQuery.GetSourceGridIndices(transitionType); |
| | 8 | 559 | | if (sourceGridIndices.Length == 0) |
| | | 560 | | { |
| | 1 | 561 | | transition = default; |
| | 1 | 562 | | return false; |
| | | 563 | | } |
| | | 564 | | |
| | 7 | 565 | | bool found = false; |
| | 7 | 566 | | transition = default; |
| | 7 | 567 | | Fixed64 closestDistanceSq = Fixed64.Zero; |
| | 7 | 568 | | int originGridIndex = -1; |
| | | 569 | | |
| | 7 | 570 | | if (world.TryGetGrid(worldPosition, out VoxelGrid? originGrid)) |
| | | 571 | | { |
| | 6 | 572 | | originGridIndex = originGrid!.GridIndex; |
| | 6 | 573 | | EvaluateClosestTransitionCandidates( |
| | 6 | 574 | | TraversalTransitionQuery.GetDirectedTransitionsFromSourceGrid(originGridIndex, transitionType), |
| | 6 | 575 | | worldPosition, |
| | 6 | 576 | | ref found, |
| | 6 | 577 | | ref transition, |
| | 6 | 578 | | ref closestDistanceSq); |
| | | 579 | | |
| | 6 | 580 | | if (found && closestDistanceSq == Fixed64.Zero) |
| | 3 | 581 | | return true; |
| | | 582 | | } |
| | | 583 | | |
| | 18 | 584 | | for (int i = 0; i < sourceGridIndices.Length; i++) |
| | | 585 | | { |
| | 5 | 586 | | int sourceGridIndex = sourceGridIndices[i]; |
| | 5 | 587 | | if (sourceGridIndex == originGridIndex |
| | 5 | 588 | | || !world.TryGetGrid(sourceGridIndex, out VoxelGrid? sourceGrid) |
| | 5 | 589 | | || (found && GetBoundsDistanceSq(worldPosition, sourceGrid!.BoundsMin, sourceGrid.BoundsMax) >= closestD |
| | | 590 | | { |
| | | 591 | | continue; |
| | | 592 | | } |
| | | 593 | | |
| | 2 | 594 | | EvaluateClosestTransitionCandidates( |
| | 2 | 595 | | TraversalTransitionQuery.GetDirectedTransitionsFromSourceGrid(sourceGridIndex, transitionType), |
| | 2 | 596 | | worldPosition, |
| | 2 | 597 | | ref found, |
| | 2 | 598 | | ref transition, |
| | 2 | 599 | | ref closestDistanceSq); |
| | | 600 | | |
| | 2 | 601 | | if (found && closestDistanceSq == Fixed64.Zero) |
| | | 602 | | break; |
| | | 603 | | } |
| | | 604 | | |
| | 4 | 605 | | return found; |
| | | 606 | | } |
| | | 607 | | |
| | | 608 | | /// <summary> |
| | | 609 | | /// Attempts to retrieve the closest currently active directed transition of the requested type using the configured |
| | | 610 | | /// </summary> |
| | | 611 | | public static bool TryGetClosestActiveTransition( |
| | | 612 | | Vector3d worldPosition, |
| | | 613 | | TraversalTransitionType transitionType, |
| | | 614 | | out TraversalTransition transition) |
| | | 615 | | { |
| | 8 | 616 | | return TryGetClosestActiveTransition(GetConfiguredWorld(), worldPosition, transitionType, out transition); |
| | | 617 | | } |
| | | 618 | | |
| | | 619 | | /// <summary> |
| | | 620 | | /// Initializes all registered navigation charts by materializing their authored surface and volume partitions. |
| | | 621 | | /// </summary> |
| | | 622 | | public static void InitializeAllCharts() |
| | | 623 | | { |
| | 1 | 624 | | InitializeAllCharts(GetConfiguredWorld()); |
| | 1 | 625 | | } |
| | | 626 | | |
| | | 627 | | /// <summary> |
| | | 628 | | /// Initializes all registered navigation charts by materializing their authored surface and volume partitions. |
| | | 629 | | /// </summary> |
| | | 630 | | public static void InitializeAllCharts(GridWorld world) |
| | | 631 | | { |
| | 2 | 632 | | LinkWorld(world); |
| | 10 | 633 | | foreach (NavigationChart chart in AllCharts) |
| | 3 | 634 | | InitializeChart(world, chart.Name); |
| | 2 | 635 | | } |
| | | 636 | | |
| | | 637 | | private static void EvaluateClosestTransitionCandidates( |
| | | 638 | | TraversalTransition[] candidates, |
| | | 639 | | Vector3d worldPosition, |
| | | 640 | | ref bool found, |
| | | 641 | | ref TraversalTransition closestTransition, |
| | | 642 | | ref Fixed64 closestDistanceSq) |
| | | 643 | | { |
| | 44 | 644 | | for (int i = 0; i < candidates.Length; i++) |
| | | 645 | | { |
| | 14 | 646 | | Fixed64 candidateDistanceSq = (candidates[i].Source.Position - worldPosition).SqrMagnitude; |
| | 14 | 647 | | if (!found || candidateDistanceSq < closestDistanceSq) |
| | | 648 | | { |
| | 12 | 649 | | found = true; |
| | 12 | 650 | | closestDistanceSq = candidateDistanceSq; |
| | 12 | 651 | | closestTransition = candidates[i]; |
| | | 652 | | } |
| | | 653 | | } |
| | 8 | 654 | | } |
| | | 655 | | |
| | | 656 | | private static Fixed64 GetBoundsDistanceSq( |
| | | 657 | | Vector3d worldPosition, |
| | | 658 | | Vector3d boundsMin, |
| | | 659 | | Vector3d boundsMax) |
| | | 660 | | { |
| | 1 | 661 | | Fixed64 xDistance = GetAxisDistanceToBounds(worldPosition.x, boundsMin.x, boundsMax.x); |
| | 1 | 662 | | Fixed64 yDistance = GetAxisDistanceToBounds(worldPosition.y, boundsMin.y, boundsMax.y); |
| | 1 | 663 | | Fixed64 zDistance = GetAxisDistanceToBounds(worldPosition.z, boundsMin.z, boundsMax.z); |
| | 1 | 664 | | return xDistance * xDistance + yDistance * yDistance + zDistance * zDistance; |
| | | 665 | | } |
| | | 666 | | |
| | | 667 | | private static Fixed64 GetAxisDistanceToBounds(Fixed64 value, Fixed64 boundsMin, Fixed64 boundsMax) |
| | | 668 | | { |
| | 3 | 669 | | if (value < boundsMin) |
| | 1 | 670 | | return boundsMin - value; |
| | | 671 | | |
| | 2 | 672 | | if (value > boundsMax) |
| | 1 | 673 | | return value - boundsMax; |
| | | 674 | | |
| | 1 | 675 | | return Fixed64.Zero; |
| | | 676 | | } |
| | | 677 | | |
| | | 678 | | /// <summary> |
| | | 679 | | /// Applies one authored cell mutation to a registered chart using chart-local indices. |
| | | 680 | | /// </summary> |
| | | 681 | | /// <returns> |
| | | 682 | | /// <c>true</c> when the target cell was in bounds and the authored payload changed; otherwise, <c>false</c>. |
| | | 683 | | /// </returns> |
| | | 684 | | public static bool TryUpdateChartCell(string chartName, int x, int y, int z, NavigationChartCell cell) |
| | | 685 | | { |
| | 15 | 686 | | return TryUpdateChartCell(GetConfiguredWorld(), chartName, x, y, z, cell); |
| | | 687 | | } |
| | | 688 | | |
| | | 689 | | /// <summary> |
| | | 690 | | /// Applies one authored cell mutation to a registered chart using chart-local indices. |
| | | 691 | | /// </summary> |
| | | 692 | | /// <returns> |
| | | 693 | | /// <c>true</c> when the target cell was in bounds and the authored payload changed; otherwise, <c>false</c>. |
| | | 694 | | /// </returns> |
| | | 695 | | public static bool TryUpdateChartCell(GridWorld world, string chartName, int x, int y, int z, NavigationChartCell ce |
| | | 696 | | { |
| | 16 | 697 | | LinkWorld(world); |
| | 16 | 698 | | if (!TryGetNavigationChartRegistration(chartName, out NavigationChartRegistration registration)) |
| | 1 | 699 | | return false; |
| | | 700 | | |
| | 15 | 701 | | return TryUpdateChartCell(world, registration, x, y, z, cell); |
| | | 702 | | } |
| | | 703 | | |
| | | 704 | | private static bool TryUpdateChartCell( |
| | | 705 | | GridWorld world, |
| | | 706 | | NavigationChartRegistration registration, |
| | | 707 | | int x, |
| | | 708 | | int y, |
| | | 709 | | int z, |
| | | 710 | | NavigationChartCell cell) |
| | | 711 | | { |
| | 21 | 712 | | NavigationChart chart = registration.Chart; |
| | 21 | 713 | | SwiftHashSet<SolidChartPartition> partitionsToRebind = PartitionSetPool.Rent(); |
| | 21 | 714 | | SwiftHashSet<string> invalidatedChartKeys = SwiftHashSetPool<string>.Shared.Rent(); |
| | 21 | 715 | | SwiftHashSet<string> managedChartsToRefresh = SwiftHashSetPool<string>.Shared.Rent(); |
| | | 716 | | try |
| | | 717 | | { |
| | 21 | 718 | | bool changed = TryApplyChartCellUpdate( |
| | 21 | 719 | | world, |
| | 21 | 720 | | registration, |
| | 21 | 721 | | x, |
| | 21 | 722 | | y, |
| | 21 | 723 | | z, |
| | 21 | 724 | | cell, |
| | 21 | 725 | | partitionsToRebind, |
| | 21 | 726 | | invalidatedChartKeys, |
| | 21 | 727 | | managedChartsToRefresh); |
| | | 728 | | |
| | 21 | 729 | | if (changed) |
| | 18 | 730 | | RefreshManagedTransitionsForVoxel( |
| | 18 | 731 | | world, |
| | 18 | 732 | | chart.GetWorldPosition(x, y, z), |
| | 18 | 733 | | managedChartsToRefresh); |
| | | 734 | | |
| | 21 | 735 | | RebindAndInvalidate(partitionsToRebind, invalidatedChartKeys); |
| | 21 | 736 | | return changed; |
| | | 737 | | } |
| | | 738 | | finally |
| | | 739 | | { |
| | 21 | 740 | | PartitionSetPool.Release(partitionsToRebind); |
| | 21 | 741 | | SwiftHashSetPool<string>.Shared.Release(invalidatedChartKeys); |
| | 21 | 742 | | SwiftHashSetPool<string>.Shared.Release(managedChartsToRefresh); |
| | 21 | 743 | | } |
| | 21 | 744 | | } |
| | | 745 | | |
| | | 746 | | /// <summary> |
| | | 747 | | /// Applies one authored cell mutation to a registered chart using a world-space position. |
| | | 748 | | /// </summary> |
| | | 749 | | /// <returns> |
| | | 750 | | /// <c>true</c> when the position resolves inside the chart and the authored payload changed; otherwise, <c>false</c |
| | | 751 | | /// </returns> |
| | | 752 | | public static bool TryUpdateChartCell(string chartName, Vector3d worldPosition, NavigationChartCell cell) |
| | | 753 | | { |
| | 5 | 754 | | return TryUpdateChartCell(GetConfiguredWorld(), chartName, worldPosition, cell); |
| | | 755 | | } |
| | | 756 | | |
| | | 757 | | /// <summary> |
| | | 758 | | /// Applies one authored cell mutation to a registered chart using a world-space position. |
| | | 759 | | /// </summary> |
| | | 760 | | /// <returns> |
| | | 761 | | /// <c>true</c> when the position resolves inside the chart and the authored payload changed; otherwise, <c>false</c |
| | | 762 | | /// </returns> |
| | | 763 | | public static bool TryUpdateChartCell(GridWorld world, string chartName, Vector3d worldPosition, NavigationChartCell |
| | | 764 | | { |
| | 7 | 765 | | LinkWorld(world); |
| | 7 | 766 | | if (!TryGetNavigationChartRegistration(chartName, out NavigationChartRegistration registration) |
| | 7 | 767 | | || !registration.Chart.TryWorldToIndex(worldPosition, out int x, out int y, out int z)) |
| | | 768 | | { |
| | 1 | 769 | | return false; |
| | | 770 | | } |
| | | 771 | | |
| | 6 | 772 | | return TryUpdateChartCell(world, registration, x, y, z, cell); |
| | | 773 | | } |
| | | 774 | | |
| | | 775 | | /// <summary> |
| | | 776 | | /// Applies a sparse batch of authored cell mutations to a registered chart. |
| | | 777 | | /// </summary> |
| | | 778 | | /// <param name="chartName">The registered chart to mutate.</param> |
| | | 779 | | /// <param name="updates">The sparse set of cell changes to apply in order.</param> |
| | | 780 | | /// <returns>The number of authored cell mutations that changed the chart payload.</returns> |
| | | 781 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="updates"/> is null.</exception> |
| | | 782 | | public static int ApplyChartUpdates(string chartName, IReadOnlyList<NavigationChartCellUpdate> updates) |
| | | 783 | | { |
| | 4 | 784 | | return ApplyChartUpdates(GetConfiguredWorld(), chartName, updates); |
| | | 785 | | } |
| | | 786 | | |
| | | 787 | | /// <summary> |
| | | 788 | | /// Applies a sparse batch of authored cell mutations to a registered chart. |
| | | 789 | | /// </summary> |
| | | 790 | | /// <param name="world">The grid world context for the chart.</param> |
| | | 791 | | /// <param name="chartName">The registered chart to mutate.</param> |
| | | 792 | | /// <param name="updates">The sparse set of cell changes to apply in order.</param> |
| | | 793 | | /// <returns>The number of authored cell mutations that changed the chart payload.</returns> |
| | | 794 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="updates"/> is null.</exception> |
| | | 795 | | public static int ApplyChartUpdates(GridWorld world, string chartName, IReadOnlyList<NavigationChartCellUpdate> upda |
| | | 796 | | { |
| | 5 | 797 | | SwiftThrowHelper.ThrowIfNull(updates, nameof(updates)); |
| | 4 | 798 | | LinkWorld(world); |
| | | 799 | | |
| | 4 | 800 | | if (updates.Count == 0 |
| | 4 | 801 | | || !TryGetNavigationChartRegistration(chartName, out NavigationChartRegistration registration)) |
| | | 802 | | { |
| | 2 | 803 | | return 0; |
| | | 804 | | } |
| | | 805 | | |
| | 2 | 806 | | NavigationChart chart = registration.Chart; |
| | 2 | 807 | | SwiftHashSet<SolidChartPartition> partitionsToRebind = PartitionSetPool.Rent(); |
| | 2 | 808 | | SwiftHashSet<string> invalidatedChartKeys = SwiftHashSetPool<string>.Shared.Rent(); |
| | 2 | 809 | | SwiftHashSet<string> managedChartsToRefresh = SwiftHashSetPool<string>.Shared.Rent(); |
| | | 810 | | try |
| | | 811 | | { |
| | 2 | 812 | | int changedCount = 0; |
| | 12 | 813 | | for (int i = 0; i < updates.Count; i++) |
| | | 814 | | { |
| | 4 | 815 | | managedChartsToRefresh.Clear(); |
| | 4 | 816 | | NavigationChartCellUpdate update = updates[i]; |
| | 4 | 817 | | if (TryApplyChartCellUpdate( |
| | 4 | 818 | | world, |
| | 4 | 819 | | registration, |
| | 4 | 820 | | update.X, |
| | 4 | 821 | | update.Y, |
| | 4 | 822 | | update.Z, |
| | 4 | 823 | | update.Cell, |
| | 4 | 824 | | partitionsToRebind, |
| | 4 | 825 | | invalidatedChartKeys, |
| | 4 | 826 | | managedChartsToRefresh)) |
| | | 827 | | { |
| | 2 | 828 | | changedCount++; |
| | 2 | 829 | | RefreshManagedTransitionsForVoxel( |
| | 2 | 830 | | world, |
| | 2 | 831 | | chart.GetWorldPosition(update.X, update.Y, update.Z), |
| | 2 | 832 | | managedChartsToRefresh); |
| | | 833 | | } |
| | | 834 | | } |
| | | 835 | | |
| | 2 | 836 | | RebindAndInvalidate(partitionsToRebind, invalidatedChartKeys); |
| | 2 | 837 | | return changedCount; |
| | | 838 | | } |
| | | 839 | | finally |
| | | 840 | | { |
| | 2 | 841 | | PartitionSetPool.Release(partitionsToRebind); |
| | 2 | 842 | | SwiftHashSetPool<string>.Shared.Release(invalidatedChartKeys); |
| | 2 | 843 | | SwiftHashSetPool<string>.Shared.Release(managedChartsToRefresh); |
| | 2 | 844 | | } |
| | 2 | 845 | | } |
| | | 846 | | |
| | | 847 | | /// <summary> |
| | | 848 | | /// Initializes a specific navigation chart by materializing its authored surface and volume partitions. |
| | | 849 | | /// </summary> |
| | | 850 | | /// <param name="chartKey">The name of the map to initialize.</param> |
| | | 851 | | public static void InitializeChart(string chartKey) |
| | | 852 | | { |
| | 27 | 853 | | InitializeChart(GetConfiguredWorld(), chartKey); |
| | 27 | 854 | | } |
| | | 855 | | |
| | | 856 | | /// <summary> |
| | | 857 | | /// Initializes a specific navigation chart by materializing its authored surface and volume partitions. |
| | | 858 | | /// </summary> |
| | | 859 | | /// <param name="world">The grid world context for the chart.</param> |
| | | 860 | | /// <param name="chartKey">The name of the map to initialize.</param> |
| | | 861 | | public static void InitializeChart(GridWorld world, string chartKey) |
| | | 862 | | { |
| | 969 | 863 | | LinkWorld(world); |
| | 969 | 864 | | if (string.IsNullOrEmpty(chartKey) |
| | 969 | 865 | | || !TryGetNavigationChartRegistration(chartKey, out NavigationChartRegistration registration) |
| | 969 | 866 | | || registration.IsInitialized) |
| | | 867 | | { |
| | 24 | 868 | | return; |
| | | 869 | | } |
| | | 870 | | |
| | 945 | 871 | | NavigationChart chart = registration.Chart; |
| | 945 | 872 | | PathManagerExternalGridBridge.FlushPendingGridChanges(); |
| | | 873 | | |
| | 945 | 874 | | SwiftHashSet<SolidChartPartition> partitionsToRebind = PartitionSetPool.Rent(); |
| | 945 | 875 | | SwiftHashSet<string> affectedChartKeys = SwiftHashSetPool<string>.Shared.Rent(); |
| | 945 | 876 | | SwiftHashSet<WorldVoxelIndex> touchedVoxelIndices = SwiftHashSetPool<WorldVoxelIndex>.Shared.Rent(); |
| | | 877 | | try |
| | | 878 | | { |
| | 8170 | 879 | | foreach ((Vector3d pos, NavigationChartCell cell) in chart.GetAuthoredCells()) |
| | | 880 | | { |
| | 3140 | 881 | | if (!world.TryGetVoxel(pos, out Voxel? voxel)) |
| | | 882 | | continue; |
| | | 883 | | |
| | 2942 | 884 | | touchedVoxelIndices.Add(voxel!.WorldIndex); |
| | | 885 | | |
| | 2942 | 886 | | if (!_resolvedChartVoxelStates.TryGetValue(voxel.WorldIndex, out ResolvedChartVoxelState state)) |
| | | 887 | | { |
| | 2928 | 888 | | state = new ResolvedChartVoxelState(); |
| | 2928 | 889 | | _resolvedChartVoxelStates[voxel.WorldIndex] = state; |
| | | 890 | | } |
| | 14 | 891 | | else if (state.HasAnyOwners) |
| | 14 | 892 | | state.AddChartOwnersTo(affectedChartKeys); |
| | | 893 | | |
| | 2942 | 894 | | NavigationChartCell previousEffectiveCell = state.EffectiveCell; |
| | 2942 | 895 | | state.AddOwner(chart.Name, cell, chart.Priority, registration.RegistrationOrder); |
| | 2942 | 896 | | ApplyResolvedVoxelState(world, voxel, state, previousEffectiveCell, partitionsToRebind); |
| | 2942 | 897 | | TrackInitializedChartGridTouch(voxel.GridIndex, chart.Name); |
| | | 898 | | } |
| | | 899 | | |
| | 945 | 900 | | BindCollectedSolidPartitions(partitionsToRebind); |
| | | 901 | | |
| | 945 | 902 | | registration.IsInitialized = true; |
| | 945 | 903 | | affectedChartKeys.Add(chart.Name); |
| | 945 | 904 | | SolidPartitionReachability.Invalidate(); |
| | | 905 | | |
| | 945 | 906 | | RefreshManagedManualTransitionsForVoxels(touchedVoxelIndices); |
| | 945 | 907 | | RefreshManagedGeneratedTransitionsForCharts(world, affectedChartKeys); |
| | | 908 | | |
| | 3806 | 909 | | foreach (string affectedChartKey in affectedChartKeys) |
| | 958 | 910 | | PathGuideFactory.InvalidateCacheFor(affectedChartKey); |
| | | 911 | | } |
| | | 912 | | finally |
| | | 913 | | { |
| | 945 | 914 | | PartitionSetPool.Release(partitionsToRebind); |
| | 945 | 915 | | SwiftHashSetPool<string>.Shared.Release(affectedChartKeys); |
| | 945 | 916 | | SwiftHashSetPool<WorldVoxelIndex>.Shared.Release(touchedVoxelIndices); |
| | 945 | 917 | | } |
| | 945 | 918 | | } |
| | | 919 | | |
| | | 920 | | /// <summary> |
| | | 921 | | /// Unloads the navigation chart identified by the specified key from the given world. |
| | | 922 | | /// </summary> |
| | | 923 | | /// <param name="chartKey"> |
| | | 924 | | /// The unique key identifying the navigation chart to unload. |
| | | 925 | | /// If the key does not correspond to a loaded chart, no action is taken.</param> |
| | | 926 | | public static void UnloadChart(string chartKey) |
| | | 927 | | { |
| | 186 | 928 | | UnloadChart(GetConfiguredWorld(), chartKey); |
| | 186 | 929 | | } |
| | | 930 | | |
| | | 931 | | /// <summary> |
| | | 932 | | /// Unloads the navigation chart identified by the specified key from the given world. |
| | | 933 | | /// </summary> |
| | | 934 | | /// <param name="world">The world instance from which to unload the navigation chart.</param> |
| | | 935 | | /// <param name="chartKey"> |
| | | 936 | | /// The unique key identifying the navigation chart to unload. |
| | | 937 | | /// If the key does not correspond to a loaded chart, no action is taken.</param> |
| | | 938 | | public static void UnloadChart(GridWorld world, string chartKey) |
| | | 939 | | { |
| | 189 | 940 | | LinkWorld(world); |
| | 189 | 941 | | if (!TryGetNavigationChartRegistration(chartKey, out NavigationChartRegistration registration)) |
| | 1 | 942 | | return; |
| | | 943 | | |
| | 188 | 944 | | UnloadChart(world, registration); |
| | 188 | 945 | | } |
| | | 946 | | |
| | | 947 | | /// <summary> |
| | | 948 | | /// Unloads a navigation map by name and releases associated partitions. |
| | | 949 | | /// </summary> |
| | | 950 | | /// <param name="chart">The navigation chart to unload.</param> |
| | | 951 | | public static void UnloadChart(NavigationChart chart) |
| | | 952 | | { |
| | 13 | 953 | | UnloadChart(GetConfiguredWorld(), chart); |
| | 13 | 954 | | } |
| | | 955 | | |
| | | 956 | | /// <summary> |
| | | 957 | | /// Unloads a navigation map by name and releases associated partitions. |
| | | 958 | | /// </summary> |
| | | 959 | | /// <param name="world">The grid world context for the chart.</param> |
| | | 960 | | /// <param name="chart">The navigation chart to unload.</param> |
| | | 961 | | public static void UnloadChart(GridWorld world, NavigationChart chart) |
| | | 962 | | { |
| | 15 | 963 | | LinkWorld(world); |
| | 15 | 964 | | if (chart == null) |
| | 1 | 965 | | return; |
| | | 966 | | |
| | 14 | 967 | | if (!TryGetNavigationChartRegistration(chart.Name, out NavigationChartRegistration registration)) |
| | 1 | 968 | | return; |
| | | 969 | | |
| | 13 | 970 | | UnloadChart(world, registration); |
| | 13 | 971 | | } |
| | | 972 | | |
| | | 973 | | private static void UnloadChart(GridWorld world, NavigationChartRegistration registration) |
| | | 974 | | { |
| | 201 | 975 | | NavigationChart chart = registration.Chart; |
| | 201 | 976 | | string[] generatedTransitionIds = RemoveManagedGeneratedTransitions(chart.Name); |
| | | 977 | | |
| | 201 | 978 | | if (!registration.IsInitialized) |
| | | 979 | | { |
| | 7 | 980 | | RemoveChartFromRegistry(chart.Name); |
| | 7 | 981 | | TraversalTransitionRegistry.UnregisterRange(generatedTransitionIds); |
| | 7 | 982 | | return; |
| | | 983 | | } |
| | | 984 | | |
| | | 985 | | // invalidate any survey results currently using this chart |
| | 194 | 986 | | PathGuideFactory.InvalidateCacheFor(chart.Name); |
| | | 987 | | |
| | 194 | 988 | | SwiftHashSet<SolidChartPartition> partitionsToRebind = PartitionSetPool.Rent(); |
| | 194 | 989 | | SwiftHashSet<string> affectedChartKeys = SwiftHashSetPool<string>.Shared.Rent(); |
| | 194 | 990 | | SwiftHashSet<WorldVoxelIndex> touchedVoxelIndices = SwiftHashSetPool<WorldVoxelIndex>.Shared.Rent(); |
| | | 991 | | try |
| | | 992 | | { |
| | 194 | 993 | | affectedChartKeys.Add(chart.Name); |
| | 4492 | 994 | | foreach ((Vector3d position, _) in chart.GetAuthoredCells()) |
| | | 995 | | { |
| | 2052 | 996 | | if (!world.TryGetVoxel(position, out Voxel? voxel)) |
| | | 997 | | continue; |
| | | 998 | | |
| | 1861 | 999 | | touchedVoxelIndices.Add(voxel!.WorldIndex); |
| | | 1000 | | |
| | 1861 | 1001 | | if (!_resolvedChartVoxelStates.TryGetValue(voxel.WorldIndex, out ResolvedChartVoxelState state) |
| | 1861 | 1002 | | || !state.ContainsOwner(chart.Name)) |
| | | 1003 | | { |
| | | 1004 | | continue; |
| | | 1005 | | } |
| | | 1006 | | |
| | 1861 | 1007 | | state.AddChartOwnersTo(affectedChartKeys); |
| | | 1008 | | |
| | 1861 | 1009 | | NavigationChartCell previousEffectiveCell = state.EffectiveCell; |
| | 1861 | 1010 | | state.RemoveOwner(chart.Name); |
| | 1861 | 1011 | | ApplyResolvedVoxelState(world, voxel, state, previousEffectiveCell, partitionsToRebind); |
| | 1861 | 1012 | | UntrackInitializedChartGridTouch(voxel.GridIndex, chart.Name); |
| | | 1013 | | |
| | 1861 | 1014 | | if (!state.HasAnyOwners) |
| | 1854 | 1015 | | _resolvedChartVoxelStates.Remove(voxel.WorldIndex); |
| | | 1016 | | } |
| | | 1017 | | |
| | 194 | 1018 | | BindCollectedSolidPartitions(partitionsToRebind); |
| | | 1019 | | |
| | 194 | 1020 | | TraversalTransitionRegistry.UnregisterRange(generatedTransitionIds); |
| | 194 | 1021 | | registration.IsInitialized = false; |
| | 194 | 1022 | | RemoveChartFromRegistry(chart.Name); |
| | 194 | 1023 | | SolidPartitionReachability.Invalidate(); |
| | | 1024 | | |
| | 194 | 1025 | | RefreshManagedManualTransitionsForVoxels(touchedVoxelIndices); |
| | 194 | 1026 | | RefreshManagedGeneratedTransitionsForCharts(world, affectedChartKeys, chart.Name); |
| | | 1027 | | |
| | 788 | 1028 | | foreach (string affectedChartKey in affectedChartKeys) |
| | | 1029 | | { |
| | 200 | 1030 | | if (affectedChartKey == chart.Name) |
| | | 1031 | | continue; |
| | | 1032 | | |
| | 6 | 1033 | | PathGuideFactory.InvalidateCacheFor(affectedChartKey); |
| | | 1034 | | } |
| | | 1035 | | } |
| | | 1036 | | finally |
| | | 1037 | | { |
| | 194 | 1038 | | PartitionSetPool.Release(partitionsToRebind); |
| | 194 | 1039 | | SwiftHashSetPool<string>.Shared.Release(affectedChartKeys); |
| | 194 | 1040 | | SwiftHashSetPool<WorldVoxelIndex>.Shared.Release(touchedVoxelIndices); |
| | 194 | 1041 | | } |
| | 194 | 1042 | | } |
| | | 1043 | | |
| | | 1044 | | #endregion |
| | | 1045 | | |
| | | 1046 | | #region Pathfinding Utilities |
| | | 1047 | | |
| | | 1048 | | internal static int RebuildInitializedChartsAgainstExternalGridRequests( |
| | | 1049 | | ExternalGridChartRebuildRequest[] rebuildRequests) |
| | | 1050 | | { |
| | 466 | 1051 | | return RebuildInitializedChartsAgainstExternalGridRequests(GetConfiguredWorld(), rebuildRequests); |
| | | 1052 | | } |
| | | 1053 | | |
| | | 1054 | | internal static int RebuildInitializedChartsAgainstExternalGridRequests( |
| | | 1055 | | GridWorld world, |
| | | 1056 | | ExternalGridChartRebuildRequest[] rebuildRequests) |
| | | 1057 | | { |
| | 469 | 1058 | | if (rebuildRequests == null || rebuildRequests.Length == 0) |
| | 1 | 1059 | | return 0; |
| | | 1060 | | |
| | 468 | 1061 | | NavigationChart[] initializedCharts = GetInitializedChartsAffectedByExternalGridRequestsSnapshot(rebuildRequests |
| | 468 | 1062 | | if (initializedCharts.Length == 0) |
| | 444 | 1063 | | return 0; |
| | | 1064 | | |
| | 24 | 1065 | | RebuildInitializedChartsAgainstCurrentGrids(world, initializedCharts); |
| | 24 | 1066 | | return initializedCharts.Length; |
| | | 1067 | | } |
| | | 1068 | | |
| | | 1069 | | internal static int RebuildInitializedChartsAgainstExternalGridBounds( |
| | | 1070 | | GridWorld world, |
| | | 1071 | | ushort gridIndex, |
| | | 1072 | | Vector3d boundsMin, |
| | | 1073 | | Vector3d boundsMax, |
| | | 1074 | | bool useLiveGridTouchIndex) |
| | | 1075 | | { |
| | 2 | 1076 | | ExternalGridChartRebuildRequest[] rebuildRequests = |
| | 2 | 1077 | | { |
| | 2 | 1078 | | new( |
| | 2 | 1079 | | gridIndex, |
| | 2 | 1080 | | boundsMin, |
| | 2 | 1081 | | boundsMax, |
| | 2 | 1082 | | includeLiveGridTouches: useLiveGridTouchIndex, |
| | 2 | 1083 | | includeAuthoredCellsInBounds: !useLiveGridTouchIndex) |
| | 2 | 1084 | | }; |
| | | 1085 | | |
| | 2 | 1086 | | return RebuildInitializedChartsAgainstExternalGridRequests(world, rebuildRequests); |
| | | 1087 | | } |
| | | 1088 | | |
| | | 1089 | | private static void RebuildInitializedChartsAgainstCurrentGrids(GridWorld world, NavigationChart[] chartsToRebuild) |
| | | 1090 | | { |
| | 24 | 1091 | | SuppressManagedGeneratedTransitionsForCharts(chartsToRebuild); |
| | | 1092 | | |
| | 152 | 1093 | | for (int i = 0; i < chartsToRebuild.Length; i++) |
| | 52 | 1094 | | ClearInitializedChartLiveStatePreservingRegistration(world, chartsToRebuild[i]); |
| | | 1095 | | |
| | 152 | 1096 | | for (int i = 0; i < chartsToRebuild.Length; i++) |
| | 52 | 1097 | | InitializeChart(world, chartsToRebuild[i].Name); |
| | | 1098 | | |
| | 24 | 1099 | | RefreshManagedGeneratedTransitionsForCharts(world, GetInitializedChartsSnapshot()); |
| | 24 | 1100 | | TraversalTransitionRegistry.RefreshManagedManualTransitions(); |
| | 24 | 1101 | | } |
| | | 1102 | | |
| | | 1103 | | private static void SuppressManagedGeneratedTransitionsForCharts(NavigationChart[] charts) |
| | | 1104 | | { |
| | 24 | 1105 | | SwiftList<string> transitionIds = new(); |
| | 24 | 1106 | | _navigationChartMapLock.EnterReadLock(); |
| | | 1107 | | try |
| | | 1108 | | { |
| | 152 | 1109 | | for (int i = 0; i < charts.Length; i++) |
| | | 1110 | | { |
| | 52 | 1111 | | NavigationChart chart = charts[i]; |
| | 52 | 1112 | | if (!TryGetNavigationChartRegistration_NoLock(chart.Name, out NavigationChartRegistration registration) |
| | 52 | 1113 | | || registration.TransitionIds.Count == 0) |
| | | 1114 | | { |
| | | 1115 | | continue; |
| | | 1116 | | } |
| | | 1117 | | |
| | 12 | 1118 | | foreach (string transitionId in registration.TransitionIds) |
| | 4 | 1119 | | transitionIds.Add(transitionId); |
| | | 1120 | | } |
| | 24 | 1121 | | } |
| | | 1122 | | finally |
| | | 1123 | | { |
| | 24 | 1124 | | _navigationChartMapLock.ExitReadLock(); |
| | 24 | 1125 | | } |
| | | 1126 | | |
| | 24 | 1127 | | if (transitionIds.Count == 0) |
| | 22 | 1128 | | return; |
| | | 1129 | | |
| | 2 | 1130 | | TraversalTransitionRegistry.SetManagedTransitionsSuppressed( |
| | 2 | 1131 | | transitionIds.ToArray(), |
| | 2 | 1132 | | suppressed: true); |
| | 2 | 1133 | | } |
| | | 1134 | | |
| | | 1135 | | private static NavigationChart[] GetInitializedChartsSnapshot() |
| | | 1136 | | { |
| | 26 | 1137 | | _navigationChartMapLock.EnterReadLock(); |
| | | 1138 | | try |
| | | 1139 | | { |
| | 26 | 1140 | | if (_navigationChartMap.Count == 0) |
| | 1 | 1141 | | return Array.Empty<NavigationChart>(); |
| | | 1142 | | |
| | 25 | 1143 | | SwiftList<NavigationChartRegistration> initializedCharts = new(); |
| | 168 | 1144 | | foreach (NavigationChartRegistration registration in _navigationChartMap.Values) |
| | | 1145 | | { |
| | 59 | 1146 | | if (registration.IsInitialized) |
| | 56 | 1147 | | initializedCharts.Add(registration); |
| | | 1148 | | } |
| | | 1149 | | |
| | 25 | 1150 | | return BuildInitializedChartSelectionSnapshot(initializedCharts); |
| | | 1151 | | } |
| | | 1152 | | finally |
| | | 1153 | | { |
| | 26 | 1154 | | _navigationChartMapLock.ExitReadLock(); |
| | 26 | 1155 | | } |
| | 26 | 1156 | | } |
| | | 1157 | | |
| | | 1158 | | private static NavigationChart[] GetInitializedChartsIntersectingBoundsSnapshot( |
| | | 1159 | | Vector3d boundsMin, |
| | | 1160 | | Vector3d boundsMax) |
| | | 1161 | | { |
| | 2 | 1162 | | _navigationChartMapLock.EnterReadLock(); |
| | | 1163 | | try |
| | | 1164 | | { |
| | 2 | 1165 | | if (_navigationChartMap.Count == 0) |
| | 1 | 1166 | | return Array.Empty<NavigationChart>(); |
| | | 1167 | | |
| | 1 | 1168 | | SwiftList<NavigationChartRegistration> initializedCharts = new(); |
| | 8 | 1169 | | foreach (NavigationChartRegistration registration in _navigationChartMap.Values) |
| | | 1170 | | { |
| | 3 | 1171 | | NavigationChart chart = registration.Chart; |
| | 3 | 1172 | | if (!registration.IsInitialized |
| | 3 | 1173 | | || !DoBoundsOverlap(chart.MinBounds, chart.MaxBounds, boundsMin, boundsMax)) |
| | | 1174 | | { |
| | | 1175 | | continue; |
| | | 1176 | | } |
| | | 1177 | | |
| | 1 | 1178 | | initializedCharts.Add(registration); |
| | | 1179 | | } |
| | | 1180 | | |
| | 1 | 1181 | | return BuildInitializedChartSelectionSnapshot(initializedCharts); |
| | | 1182 | | } |
| | | 1183 | | finally |
| | | 1184 | | { |
| | 2 | 1185 | | _navigationChartMapLock.ExitReadLock(); |
| | 2 | 1186 | | } |
| | 2 | 1187 | | } |
| | | 1188 | | |
| | | 1189 | | private static NavigationChart[] GetInitializedChartsTouchingGridSnapshot(ushort gridIndex) |
| | | 1190 | | { |
| | 2 | 1191 | | _navigationChartMapLock.EnterReadLock(); |
| | | 1192 | | try |
| | | 1193 | | { |
| | 2 | 1194 | | if (_navigationChartMap.Count == 0) |
| | 1 | 1195 | | return Array.Empty<NavigationChart>(); |
| | | 1196 | | |
| | 1 | 1197 | | SwiftDictionary<string, NavigationChartRegistration> selectedCharts = new(4, StringComparer.Ordinal); |
| | 1 | 1198 | | AddInitializedChartsTouchingGrid_NoLock(gridIndex, selectedCharts); |
| | 1 | 1199 | | return BuildInitializedChartSelectionSnapshot_NoLock(selectedCharts); |
| | | 1200 | | } |
| | | 1201 | | finally |
| | | 1202 | | { |
| | 2 | 1203 | | _navigationChartMapLock.ExitReadLock(); |
| | 2 | 1204 | | } |
| | 2 | 1205 | | } |
| | | 1206 | | |
| | | 1207 | | private static NavigationChart[] GetInitializedChartsWithAuthoredCellsIntersectingBoundsSnapshot( |
| | | 1208 | | Vector3d boundsMin, |
| | | 1209 | | Vector3d boundsMax) |
| | | 1210 | | { |
| | 2 | 1211 | | _navigationChartMapLock.EnterReadLock(); |
| | | 1212 | | try |
| | | 1213 | | { |
| | 2 | 1214 | | if (_navigationChartMap.Count == 0) |
| | 1 | 1215 | | return Array.Empty<NavigationChart>(); |
| | | 1216 | | |
| | 1 | 1217 | | SwiftDictionary<string, NavigationChartRegistration> selectedCharts = new(4, StringComparer.Ordinal); |
| | 1 | 1218 | | AddInitializedChartsWithAuthoredCellsIntersectingBounds_NoLock(boundsMin, boundsMax, selectedCharts); |
| | 1 | 1219 | | return BuildInitializedChartSelectionSnapshot_NoLock(selectedCharts); |
| | | 1220 | | } |
| | | 1221 | | finally |
| | | 1222 | | { |
| | 2 | 1223 | | _navigationChartMapLock.ExitReadLock(); |
| | 2 | 1224 | | } |
| | 2 | 1225 | | } |
| | | 1226 | | |
| | | 1227 | | private static NavigationChart[] GetInitializedChartsAffectedByExternalGridRequestsSnapshot( |
| | | 1228 | | ExternalGridChartRebuildRequest[] rebuildRequests) |
| | | 1229 | | { |
| | 468 | 1230 | | _navigationChartMapLock.EnterReadLock(); |
| | | 1231 | | try |
| | | 1232 | | { |
| | 468 | 1233 | | if (_navigationChartMap.Count == 0) |
| | 13 | 1234 | | return Array.Empty<NavigationChart>(); |
| | | 1235 | | |
| | 455 | 1236 | | SwiftDictionary<string, NavigationChartRegistration> selectedCharts = new(8, StringComparer.Ordinal); |
| | 1840 | 1237 | | for (int i = 0; i < rebuildRequests.Length; i++) |
| | | 1238 | | { |
| | 465 | 1239 | | ExternalGridChartRebuildRequest rebuildRequest = rebuildRequests[i]; |
| | 465 | 1240 | | if (!rebuildRequest.HasSelectionCriteria) |
| | | 1241 | | continue; |
| | | 1242 | | |
| | 465 | 1243 | | if (rebuildRequest.IncludeLiveGridTouches) |
| | 21 | 1244 | | AddInitializedChartsTouchingGrid_NoLock(rebuildRequest.GridIndex, selectedCharts); |
| | | 1245 | | |
| | 465 | 1246 | | if (rebuildRequest.IncludeAuthoredCellsInBounds) |
| | | 1247 | | { |
| | 445 | 1248 | | AddInitializedChartsWithAuthoredCellsIntersectingBounds_NoLock( |
| | 445 | 1249 | | rebuildRequest.BoundsMin, |
| | 445 | 1250 | | rebuildRequest.BoundsMax, |
| | 445 | 1251 | | selectedCharts); |
| | | 1252 | | } |
| | | 1253 | | } |
| | | 1254 | | |
| | 455 | 1255 | | return BuildInitializedChartSelectionSnapshot_NoLock(selectedCharts); |
| | | 1256 | | } |
| | | 1257 | | finally |
| | | 1258 | | { |
| | 468 | 1259 | | _navigationChartMapLock.ExitReadLock(); |
| | 468 | 1260 | | } |
| | 468 | 1261 | | } |
| | | 1262 | | |
| | | 1263 | | private static void AddInitializedChartsTouchingGrid_NoLock( |
| | | 1264 | | ushort gridIndex, |
| | | 1265 | | SwiftDictionary<string, NavigationChartRegistration> selectedCharts) |
| | | 1266 | | { |
| | 22 | 1267 | | if (!_initializedChartTouchCountsByGridIndex.TryGetValue(gridIndex, out SwiftDictionary<string, int> chartTouche |
| | 22 | 1268 | | || chartTouches.Count == 0) |
| | | 1269 | | { |
| | 2 | 1270 | | return; |
| | | 1271 | | } |
| | | 1272 | | |
| | 134 | 1273 | | foreach (KeyValuePair<string, int> pair in chartTouches) |
| | | 1274 | | { |
| | 47 | 1275 | | if (pair.Value <= 0 |
| | 47 | 1276 | | || !_navigationChartMap.TryGetValue(pair.Key, out NavigationChartRegistration registration) |
| | 47 | 1277 | | || !registration.IsInitialized) |
| | | 1278 | | { |
| | | 1279 | | continue; |
| | | 1280 | | } |
| | | 1281 | | |
| | 47 | 1282 | | selectedCharts[registration.Chart.Name] = registration; |
| | | 1283 | | } |
| | 20 | 1284 | | } |
| | | 1285 | | |
| | | 1286 | | private static void AddInitializedChartsWithAuthoredCellsIntersectingBounds_NoLock( |
| | | 1287 | | Vector3d boundsMin, |
| | | 1288 | | Vector3d boundsMax, |
| | | 1289 | | SwiftDictionary<string, NavigationChartRegistration> selectedCharts) |
| | | 1290 | | { |
| | 1806 | 1291 | | foreach (NavigationChartRegistration registration in _navigationChartMap.Values) |
| | | 1292 | | { |
| | 457 | 1293 | | NavigationChart chart = registration.Chart; |
| | 457 | 1294 | | if (!registration.IsInitialized |
| | 457 | 1295 | | || !DoBoundsOverlap(chart.MinBounds, chart.MaxBounds, boundsMin, boundsMax) |
| | 457 | 1296 | | || !ChartHasAuthoredCellInsideBounds(chart, boundsMin, boundsMax)) |
| | | 1297 | | { |
| | | 1298 | | continue; |
| | | 1299 | | } |
| | | 1300 | | |
| | 8 | 1301 | | selectedCharts[chart.Name] = registration; |
| | | 1302 | | } |
| | 446 | 1303 | | } |
| | | 1304 | | |
| | | 1305 | | private static NavigationChart[] BuildInitializedChartSelectionSnapshot_NoLock( |
| | | 1306 | | SwiftDictionary<string, NavigationChartRegistration> selectedCharts) |
| | | 1307 | | { |
| | 457 | 1308 | | if (selectedCharts.Count == 0) |
| | 431 | 1309 | | return Array.Empty<NavigationChart>(); |
| | | 1310 | | |
| | 26 | 1311 | | NavigationChartRegistration[] snapshot = new NavigationChartRegistration[selectedCharts.Count]; |
| | 26 | 1312 | | int index = 0; |
| | 162 | 1313 | | foreach (NavigationChartRegistration registration in selectedCharts.Values) |
| | 55 | 1314 | | snapshot[index++] = registration; |
| | | 1315 | | |
| | 26 | 1316 | | Array.Sort(snapshot, CompareRegistrationsByRegistrationOrder); |
| | 26 | 1317 | | return CopyCharts(snapshot); |
| | | 1318 | | } |
| | | 1319 | | |
| | | 1320 | | private static bool ChartHasAuthoredCellInsideBounds( |
| | | 1321 | | NavigationChart chart, |
| | | 1322 | | Vector3d boundsMin, |
| | | 1323 | | Vector3d boundsMax) |
| | | 1324 | | { |
| | 28 | 1325 | | foreach ((Vector3d position, _) in chart.GetAuthoredCells()) |
| | | 1326 | | { |
| | 9 | 1327 | | if (IsPositionInsideBounds(position, boundsMin, boundsMax)) |
| | 8 | 1328 | | return true; |
| | | 1329 | | } |
| | | 1330 | | |
| | 1 | 1331 | | return false; |
| | 8 | 1332 | | } |
| | | 1333 | | |
| | | 1334 | | private static void ClearInitializedChartLiveStatePreservingRegistration(GridWorld world, NavigationChart chart) |
| | | 1335 | | { |
| | 53 | 1336 | | if (chart == null |
| | 53 | 1337 | | || !TryGetNavigationChartRegistration(chart.Name, out NavigationChartRegistration registration)) |
| | | 1338 | | { |
| | 0 | 1339 | | return; |
| | | 1340 | | } |
| | | 1341 | | |
| | 53 | 1342 | | ClearInitializedChartLiveStatePreservingRegistration(world, registration); |
| | 53 | 1343 | | } |
| | | 1344 | | |
| | | 1345 | | private static void ClearInitializedChartLiveStatePreservingRegistration( |
| | | 1346 | | GridWorld world, |
| | | 1347 | | NavigationChartRegistration registration) |
| | | 1348 | | { |
| | 53 | 1349 | | NavigationChart chart = registration.Chart; |
| | 53 | 1350 | | PathGuideFactory.InvalidateCacheFor(chart.Name); |
| | | 1351 | | |
| | 53 | 1352 | | SwiftHashSet<SolidChartPartition> partitionsToRebind = PartitionSetPool.Rent(); |
| | 53 | 1353 | | SwiftList<WorldVoxelIndex> resolvedVoxelIndicesToRemove = new(); |
| | | 1354 | | try |
| | | 1355 | | { |
| | 334 | 1356 | | foreach (KeyValuePair<WorldVoxelIndex, ResolvedChartVoxelState> pair in _resolvedChartVoxelStates) |
| | | 1357 | | { |
| | 114 | 1358 | | ResolvedChartVoxelState state = pair.Value; |
| | 114 | 1359 | | if (!state.ContainsOwner(chart.Name)) |
| | | 1360 | | continue; |
| | | 1361 | | |
| | 50 | 1362 | | NavigationChartCell previousEffectiveCell = state.EffectiveCell; |
| | 50 | 1363 | | state.RemoveOwner(chart.Name); |
| | | 1364 | | |
| | 50 | 1365 | | bool hasLiveVoxel = world.TryGetGridAndVoxel(pair.Key, out _, out Voxel? voxel); |
| | 50 | 1366 | | if (hasLiveVoxel) |
| | | 1367 | | { |
| | 44 | 1368 | | ApplyResolvedVoxelState(world, voxel!, state, previousEffectiveCell, partitionsToRebind); |
| | 44 | 1369 | | UntrackInitializedChartGridTouch(voxel!.GridIndex, chart.Name); |
| | | 1370 | | } |
| | | 1371 | | |
| | 50 | 1372 | | if (!state.HasAnyOwners |
| | 50 | 1373 | | || !hasLiveVoxel) |
| | | 1374 | | { |
| | 50 | 1375 | | resolvedVoxelIndicesToRemove.Add(pair.Key); |
| | | 1376 | | } |
| | | 1377 | | } |
| | | 1378 | | |
| | 206 | 1379 | | for (int i = 0; i < resolvedVoxelIndicesToRemove.Count; i++) |
| | 50 | 1380 | | _resolvedChartVoxelStates.Remove(resolvedVoxelIndicesToRemove[i]); |
| | | 1381 | | |
| | 53 | 1382 | | BindCollectedSolidPartitions(partitionsToRebind); |
| | | 1383 | | |
| | 53 | 1384 | | registration.IsInitialized = false; |
| | 53 | 1385 | | SolidPartitionReachability.Invalidate(); |
| | 53 | 1386 | | } |
| | | 1387 | | finally |
| | | 1388 | | { |
| | 53 | 1389 | | PartitionSetPool.Release(partitionsToRebind); |
| | 53 | 1390 | | } |
| | 53 | 1391 | | } |
| | | 1392 | | |
| | | 1393 | | private static void ClearLiveGridState(GridWorld world) |
| | | 1394 | | { |
| | 5312 | 1395 | | foreach (KeyValuePair<WorldVoxelIndex, ResolvedChartVoxelState> pair in _resolvedChartVoxelStates) |
| | | 1396 | | { |
| | 1024 | 1397 | | if (!world.TryGetGridAndVoxel(pair.Key, out _, out Voxel? voxel)) |
| | | 1398 | | continue; |
| | | 1399 | | |
| | 1023 | 1400 | | RemoveLivePathingPartitions(voxel!); |
| | | 1401 | | } |
| | | 1402 | | |
| | 1632 | 1403 | | ClearLiveGridState(); |
| | 1632 | 1404 | | } |
| | | 1405 | | |
| | | 1406 | | private static void ClearLiveGridState() |
| | | 1407 | | { |
| | 1632 | 1408 | | _resolvedChartVoxelStates.Clear(); |
| | 1632 | 1409 | | _initializedChartTouchCountsByGridIndex.Clear(); |
| | 1632 | 1410 | | ClearActiveAuthoredVolumeMediumCounts(); |
| | 1632 | 1411 | | SolidPartitionReachability.Invalidate(); |
| | 1632 | 1412 | | } |
| | | 1413 | | |
| | | 1414 | | private static bool DoBoundsOverlap( |
| | | 1415 | | Vector3d firstMin, |
| | | 1416 | | Vector3d firstMax, |
| | | 1417 | | Vector3d secondMin, |
| | | 1418 | | Vector3d secondMax) |
| | | 1419 | | { |
| | 15 | 1420 | | return firstMin.x <= secondMax.x |
| | 15 | 1421 | | && firstMax.x >= secondMin.x |
| | 15 | 1422 | | && firstMin.y <= secondMax.y |
| | 15 | 1423 | | && firstMax.y >= secondMin.y |
| | 15 | 1424 | | && firstMin.z <= secondMax.z |
| | 15 | 1425 | | && firstMax.z >= secondMin.z; |
| | | 1426 | | } |
| | | 1427 | | |
| | | 1428 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1429 | | private static bool IsPositionInsideBounds( |
| | | 1430 | | Vector3d position, |
| | | 1431 | | Vector3d boundsMin, |
| | | 1432 | | Vector3d boundsMax) |
| | | 1433 | | { |
| | 9 | 1434 | | return position.x >= boundsMin.x |
| | 9 | 1435 | | && position.x <= boundsMax.x |
| | 9 | 1436 | | && position.y >= boundsMin.y |
| | 9 | 1437 | | && position.y <= boundsMax.y |
| | 9 | 1438 | | && position.z >= boundsMin.z |
| | 9 | 1439 | | && position.z <= boundsMax.z; |
| | | 1440 | | } |
| | | 1441 | | |
| | | 1442 | | private static void RemoveLivePathingPartitions(Voxel voxel) |
| | | 1443 | | { |
| | 1023 | 1444 | | if (voxel.TryGetPartition<SolidChartPartition>(out _)) |
| | 628 | 1445 | | voxel.TryRemovePartition<SolidChartPartition>(); |
| | | 1446 | | |
| | 1023 | 1447 | | if (voxel.TryGetPartition<VolumeChartPartition>(out _)) |
| | 417 | 1448 | | voxel.TryRemovePartition<VolumeChartPartition>(); |
| | 1023 | 1449 | | } |
| | | 1450 | | |
| | | 1451 | | private static NavigationChart[] BuildInitializedChartSelectionSnapshot( |
| | | 1452 | | SwiftList<NavigationChartRegistration> registrations) |
| | | 1453 | | { |
| | 27 | 1454 | | if (registrations.Count == 0) |
| | 1 | 1455 | | return Array.Empty<NavigationChart>(); |
| | | 1456 | | |
| | 26 | 1457 | | NavigationChartRegistration[] snapshot = registrations.ToArray(); |
| | 26 | 1458 | | Array.Sort(snapshot, CompareRegistrationsByRegistrationOrder); |
| | 26 | 1459 | | return CopyCharts(snapshot); |
| | | 1460 | | } |
| | | 1461 | | |
| | | 1462 | | private static NavigationChart[] CopyCharts(NavigationChartRegistration[] registrations) |
| | | 1463 | | { |
| | 52 | 1464 | | NavigationChart[] charts = new NavigationChart[registrations.Length]; |
| | 328 | 1465 | | for (int i = 0; i < registrations.Length; i++) |
| | 112 | 1466 | | charts[i] = registrations[i].Chart; |
| | | 1467 | | |
| | 52 | 1468 | | return charts; |
| | | 1469 | | } |
| | | 1470 | | |
| | | 1471 | | private static int CompareRegistrationsByRegistrationOrder( |
| | | 1472 | | NavigationChartRegistration left, |
| | | 1473 | | NavigationChartRegistration right) |
| | | 1474 | | { |
| | 107 | 1475 | | return left.RegistrationOrder.CompareTo(right.RegistrationOrder); |
| | | 1476 | | } |
| | | 1477 | | |
| | | 1478 | | private static void TrackInitializedChartGridTouch(ushort gridIndex, string chartName) |
| | | 1479 | | { |
| | 2947 | 1480 | | if (!_initializedChartTouchCountsByGridIndex.TryGetValue(gridIndex, out SwiftDictionary<string, int> chartTouche |
| | | 1481 | | { |
| | 458 | 1482 | | chartTouches = new SwiftDictionary<string, int>(4, StringComparer.Ordinal); |
| | 458 | 1483 | | _initializedChartTouchCountsByGridIndex[gridIndex] = chartTouches; |
| | | 1484 | | } |
| | | 1485 | | |
| | 2947 | 1486 | | chartTouches.TryGetValue(chartName, out int touchCount); |
| | 2947 | 1487 | | chartTouches[chartName] = touchCount + 1; |
| | 2947 | 1488 | | } |
| | | 1489 | | |
| | | 1490 | | private static void UntrackInitializedChartGridTouch(ushort gridIndex, string chartName) |
| | | 1491 | | { |
| | 1912 | 1492 | | if (!_initializedChartTouchCountsByGridIndex.TryGetValue(gridIndex, out SwiftDictionary<string, int> chartTouche |
| | 1912 | 1493 | | || !chartTouches.TryGetValue(chartName, out int touchCount)) |
| | | 1494 | | { |
| | 1 | 1495 | | return; |
| | | 1496 | | } |
| | | 1497 | | |
| | 1911 | 1498 | | if (touchCount <= 1) |
| | 238 | 1499 | | chartTouches.Remove(chartName); |
| | | 1500 | | else |
| | 1673 | 1501 | | chartTouches[chartName] = touchCount - 1; |
| | | 1502 | | |
| | 1911 | 1503 | | if (chartTouches.Count == 0) |
| | 178 | 1504 | | _initializedChartTouchCountsByGridIndex.Remove(gridIndex); |
| | 1911 | 1505 | | } |
| | | 1506 | | |
| | | 1507 | | private static void TrackInitializedChartGridTouchDelta( |
| | | 1508 | | ushort gridIndex, |
| | | 1509 | | string chartName, |
| | | 1510 | | NavigationChartCell previousCell, |
| | | 1511 | | NavigationChartCell currentCell) |
| | | 1512 | | { |
| | 15 | 1513 | | if (previousCell.HasTraversalData == currentCell.HasTraversalData) |
| | 4 | 1514 | | return; |
| | | 1515 | | |
| | 11 | 1516 | | if (previousCell.HasTraversalData) |
| | 6 | 1517 | | UntrackInitializedChartGridTouch(gridIndex, chartName); |
| | | 1518 | | |
| | 11 | 1519 | | if (currentCell.HasTraversalData) |
| | 5 | 1520 | | TrackInitializedChartGridTouch(gridIndex, chartName); |
| | 11 | 1521 | | } |
| | | 1522 | | |
| | 1 | 1523 | | private static readonly (int Dx, int Dy, int Dz)[] ManagedGeneratedNeighborOffsets = |
| | 1 | 1524 | | { |
| | 1 | 1525 | | (1, 0, 0), |
| | 1 | 1526 | | (-1, 0, 0), |
| | 1 | 1527 | | (0, 1, 0), |
| | 1 | 1528 | | (0, -1, 0), |
| | 1 | 1529 | | (0, 0, 1), |
| | 1 | 1530 | | (0, 0, -1) |
| | 1 | 1531 | | }; |
| | | 1532 | | |
| | | 1533 | | private static bool TryRegisterManagedGeneratedTransitions( |
| | | 1534 | | string chartName, |
| | | 1535 | | TraversalTransition[]? precomputedGeneratedTransitions) |
| | | 1536 | | { |
| | 907 | 1537 | | if (!TryGetNavigationChartRegistration(chartName, out NavigationChartRegistration registration)) |
| | 1 | 1538 | | return false; |
| | | 1539 | | |
| | 906 | 1540 | | NavigationChart chart = registration.Chart; |
| | 906 | 1541 | | TraversalTransition[] generatedTransitions = precomputedGeneratedTransitions |
| | 906 | 1542 | | ?? GeneratedTraversalTransitionBuilder.BuildTransitions(chart, registration.TransitionIdPrefix); |
| | 906 | 1543 | | int transitionCount = generatedTransitions.Length; |
| | 906 | 1544 | | if (transitionCount > 0 |
| | 906 | 1545 | | && !TraversalTransitionRegistry.RegisterGeneratedRange( |
| | 906 | 1546 | | generatedTransitions, |
| | 906 | 1547 | | chart.Priority, |
| | 906 | 1548 | | startSuppressed: true)) |
| | | 1549 | | { |
| | 1 | 1550 | | return false; |
| | | 1551 | | } |
| | | 1552 | | |
| | 905 | 1553 | | string[] registeredTransitionIds = transitionCount == 0 |
| | 905 | 1554 | | ? Array.Empty<string>() |
| | 905 | 1555 | | : new string[transitionCount]; |
| | 1918 | 1556 | | for (int i = 0; i < transitionCount; i++) |
| | 54 | 1557 | | registeredTransitionIds[i] = generatedTransitions[i].Id; |
| | | 1558 | | |
| | 905 | 1559 | | RememberManagedGeneratedTransitions(chart.Name, registeredTransitionIds, transitionCount); |
| | 905 | 1560 | | return true; |
| | | 1561 | | } |
| | | 1562 | | |
| | | 1563 | | private static void RememberManagedGeneratedTransitions( |
| | | 1564 | | string chartName, |
| | | 1565 | | string[] transitionIds, |
| | | 1566 | | int transitionCount) |
| | | 1567 | | { |
| | 906 | 1568 | | _navigationChartMapLock.EnterWriteLock(); |
| | | 1569 | | try |
| | | 1570 | | { |
| | 906 | 1571 | | if (!TryGetNavigationChartRegistration_NoLock(chartName, out NavigationChartRegistration registration)) |
| | 1 | 1572 | | return; |
| | | 1573 | | |
| | 905 | 1574 | | registration.TransitionIds.Clear(); |
| | 1918 | 1575 | | for (int i = 0; i < transitionCount; i++) |
| | 54 | 1576 | | registration.TransitionIds.Add(transitionIds[i]); |
| | 905 | 1577 | | } |
| | 1812 | 1578 | | finally { _navigationChartMapLock.ExitWriteLock(); } |
| | 906 | 1579 | | } |
| | | 1580 | | |
| | | 1581 | | private static string[] RemoveManagedGeneratedTransitions(string chartName) |
| | | 1582 | | { |
| | 203 | 1583 | | _navigationChartMapLock.EnterWriteLock(); |
| | | 1584 | | try |
| | | 1585 | | { |
| | 203 | 1586 | | if (!TryGetNavigationChartRegistration_NoLock(chartName, out NavigationChartRegistration registration)) |
| | 1 | 1587 | | return Array.Empty<string>(); |
| | | 1588 | | |
| | 202 | 1589 | | string[] transitionIds = CopyTransitionIds(registration.TransitionIds); |
| | 202 | 1590 | | registration.TransitionIds.Clear(); |
| | 202 | 1591 | | return transitionIds; |
| | | 1592 | | } |
| | 406 | 1593 | | finally { _navigationChartMapLock.ExitWriteLock(); } |
| | 203 | 1594 | | } |
| | | 1595 | | |
| | | 1596 | | private static bool TryGetManagedGeneratedTransitionState( |
| | | 1597 | | string chartName, |
| | | 1598 | | out NavigationChartRegistration state) |
| | | 1599 | | { |
| | 1041 | 1600 | | _navigationChartMapLock.EnterReadLock(); |
| | 1041 | 1601 | | try { return TryGetNavigationChartRegistration_NoLock(chartName, out state); } |
| | 2082 | 1602 | | finally { _navigationChartMapLock.ExitReadLock(); } |
| | 1041 | 1603 | | } |
| | | 1604 | | |
| | | 1605 | | private static void RefreshManagedGeneratedTransitionsForCharts( |
| | | 1606 | | GridWorld world, |
| | | 1607 | | SwiftHashSet<string> chartNames, |
| | | 1608 | | string? excludedChartName = null) |
| | | 1609 | | { |
| | 1164 | 1610 | | if (chartNames == null || chartNames.Count == 0) |
| | 1 | 1611 | | return; |
| | | 1612 | | |
| | 4750 | 1613 | | foreach (string chartName in chartNames) |
| | | 1614 | | { |
| | 1212 | 1615 | | if (string.IsNullOrEmpty(chartName) |
| | 1212 | 1616 | | || string.Equals(chartName, excludedChartName, StringComparison.Ordinal)) |
| | | 1617 | | { |
| | | 1618 | | continue; |
| | | 1619 | | } |
| | | 1620 | | |
| | 1018 | 1621 | | RefreshManagedGeneratedTransitionsForChart(world, chartName); |
| | | 1622 | | } |
| | 1163 | 1623 | | } |
| | | 1624 | | |
| | | 1625 | | private static void RefreshManagedGeneratedTransitionsForCharts(GridWorld world, NavigationChart[] charts) |
| | | 1626 | | { |
| | 24 | 1627 | | SwiftHashSet<string> chartNames = SwiftHashSetPool<string>.Shared.Rent(); |
| | | 1628 | | try |
| | | 1629 | | { |
| | 156 | 1630 | | for (int i = 0; i < charts.Length; i++) |
| | 54 | 1631 | | chartNames.Add(charts[i].Name); |
| | | 1632 | | |
| | 24 | 1633 | | RefreshManagedGeneratedTransitionsForCharts(world, chartNames); |
| | 24 | 1634 | | } |
| | | 1635 | | finally |
| | | 1636 | | { |
| | 24 | 1637 | | SwiftHashSetPool<string>.Shared.Release(chartNames); |
| | 24 | 1638 | | } |
| | 24 | 1639 | | } |
| | | 1640 | | |
| | | 1641 | | private static void RefreshManagedTransitionsForVoxel( |
| | | 1642 | | GridWorld world, |
| | | 1643 | | Vector3d worldPosition, |
| | | 1644 | | SwiftHashSet<string> chartNames) |
| | | 1645 | | { |
| | 20 | 1646 | | if (world.TryGetVoxel(worldPosition, out Voxel? voxel)) |
| | 19 | 1647 | | TraversalTransitionRegistry.RefreshManagedManualTransitionsForVoxel(voxel!.WorldIndex); |
| | | 1648 | | |
| | 20 | 1649 | | RefreshManagedGeneratedTransitionsForVoxel(world, worldPosition, chartNames); |
| | 20 | 1650 | | } |
| | | 1651 | | |
| | | 1652 | | private static void RefreshManagedManualTransitionsForVoxels(SwiftHashSet<WorldVoxelIndex> voxelIndices) |
| | | 1653 | | { |
| | 1139 | 1654 | | if (voxelIndices == null || voxelIndices.Count == 0) |
| | 18 | 1655 | | return; |
| | | 1656 | | |
| | 11848 | 1657 | | foreach (WorldVoxelIndex voxelIndex in voxelIndices) |
| | 4803 | 1658 | | TraversalTransitionRegistry.RefreshManagedManualTransitionsForVoxel(voxelIndex); |
| | 1121 | 1659 | | } |
| | | 1660 | | |
| | | 1661 | | private static void RefreshManagedGeneratedTransitionsForChart(GridWorld world, string chartName) |
| | | 1662 | | { |
| | 1019 | 1663 | | if (!TryGetNavigationChart(chartName, out NavigationChart chart) |
| | 1019 | 1664 | | || !TryGetManagedGeneratedTransitionState(chartName, out NavigationChartRegistration state)) |
| | | 1665 | | { |
| | 1 | 1666 | | return; |
| | | 1667 | | } |
| | | 1668 | | |
| | 1018 | 1669 | | SwiftHashSet<string> desiredTransitionIds = SwiftHashSetPool<string>.Shared.Rent(); |
| | 1018 | 1670 | | SwiftHashSet<string> activeTransitionIds = SwiftHashSetPool<string>.Shared.Rent(); |
| | | 1671 | | try |
| | | 1672 | | { |
| | 1018 | 1673 | | TraversalTransition[] missingTransitions = CollectManagedGeneratedTransitionsForChart( |
| | 1018 | 1674 | | world, |
| | 1018 | 1675 | | chart, |
| | 1018 | 1676 | | state, |
| | 1018 | 1677 | | desiredTransitionIds, |
| | 1018 | 1678 | | activeTransitionIds); |
| | | 1679 | | |
| | 1018 | 1680 | | ApplyManagedGeneratedTransitionDelta( |
| | 1018 | 1681 | | chartName, |
| | 1018 | 1682 | | state, |
| | 1018 | 1683 | | desiredTransitionIds, |
| | 1018 | 1684 | | activeTransitionIds, |
| | 1018 | 1685 | | missingTransitions); |
| | 1018 | 1686 | | } |
| | | 1687 | | finally |
| | | 1688 | | { |
| | 1018 | 1689 | | SwiftHashSetPool<string>.Shared.Release(desiredTransitionIds); |
| | 1018 | 1690 | | SwiftHashSetPool<string>.Shared.Release(activeTransitionIds); |
| | 1018 | 1691 | | } |
| | 1018 | 1692 | | } |
| | | 1693 | | |
| | | 1694 | | private static TraversalTransition[] CollectManagedGeneratedTransitionsForChart( |
| | | 1695 | | GridWorld world, |
| | | 1696 | | NavigationChart chart, |
| | | 1697 | | NavigationChartRegistration state, |
| | | 1698 | | SwiftHashSet<string> desiredTransitionIds, |
| | | 1699 | | SwiftHashSet<string> activeTransitionIds) |
| | | 1700 | | { |
| | 1021 | 1701 | | SwiftList<TraversalTransition> missingTransitions = new(); |
| | 1021 | 1702 | | int[] generatedIndices = chart.GetGeneratedTransitionIndices(); |
| | 2138 | 1703 | | for (int i = 0; i < generatedIndices.Length; i++) |
| | | 1704 | | { |
| | 48 | 1705 | | chart.DecodeIndex(generatedIndices[i], out int x, out int y, out int z); |
| | 672 | 1706 | | for (int neighborOffsetIndex = 0; neighborOffsetIndex < ManagedGeneratedNeighborOffsets.Length; neighborOffs |
| | | 1707 | | { |
| | 288 | 1708 | | (int dx, int dy, int dz) = ManagedGeneratedNeighborOffsets[neighborOffsetIndex]; |
| | 288 | 1709 | | int neighborX = x + dx; |
| | 288 | 1710 | | int neighborY = y + dy; |
| | 288 | 1711 | | int neighborZ = z + dz; |
| | 288 | 1712 | | if (!chart.IsInBounds(neighborX, neighborY, neighborZ)) |
| | | 1713 | | continue; |
| | | 1714 | | |
| | 86 | 1715 | | NavigationChartCell neighborCell = chart.GetCell(neighborX, neighborY, neighborZ); |
| | 86 | 1716 | | if (!ShouldCollectManagedGeneratedPair( |
| | 86 | 1717 | | x, |
| | 86 | 1718 | | y, |
| | 86 | 1719 | | z, |
| | 86 | 1720 | | neighborX, |
| | 86 | 1721 | | neighborY, |
| | 86 | 1722 | | neighborZ, |
| | 86 | 1723 | | neighborCell)) |
| | | 1724 | | { |
| | | 1725 | | continue; |
| | | 1726 | | } |
| | | 1727 | | |
| | 60 | 1728 | | CollectManagedGeneratedTransitionsForPair( |
| | 60 | 1729 | | world, |
| | 60 | 1730 | | chart, |
| | 60 | 1731 | | state, |
| | 60 | 1732 | | x, |
| | 60 | 1733 | | y, |
| | 60 | 1734 | | z, |
| | 60 | 1735 | | neighborX, |
| | 60 | 1736 | | neighborY, |
| | 60 | 1737 | | neighborZ, |
| | 60 | 1738 | | desiredTransitionIds, |
| | 60 | 1739 | | activeTransitionIds, |
| | 60 | 1740 | | missingTransitions); |
| | | 1741 | | } |
| | | 1742 | | } |
| | | 1743 | | |
| | 1021 | 1744 | | return missingTransitions.Count == 0 |
| | 1021 | 1745 | | ? Array.Empty<TraversalTransition>() |
| | 1021 | 1746 | | : missingTransitions.ToArray(); |
| | | 1747 | | } |
| | | 1748 | | |
| | | 1749 | | private static void RefreshManagedGeneratedTransitionsForVoxel( |
| | | 1750 | | GridWorld world, |
| | | 1751 | | Vector3d worldPosition, |
| | | 1752 | | SwiftHashSet<string> chartNames) |
| | | 1753 | | { |
| | 22 | 1754 | | if (chartNames == null || chartNames.Count == 0) |
| | 1 | 1755 | | return; |
| | | 1756 | | |
| | 92 | 1757 | | foreach (string chartName in chartNames) |
| | | 1758 | | { |
| | 25 | 1759 | | if (string.IsNullOrEmpty(chartName) |
| | 25 | 1760 | | || !TryGetNavigationChart(chartName, out NavigationChart chart) |
| | 25 | 1761 | | || !TryGetManagedGeneratedTransitionState(chartName, out NavigationChartRegistration state) |
| | 25 | 1762 | | || !chart.TryWorldToIndex(worldPosition, out int x, out int y, out int z)) |
| | | 1763 | | { |
| | | 1764 | | continue; |
| | | 1765 | | } |
| | | 1766 | | |
| | 23 | 1767 | | RefreshManagedGeneratedTransitionsForVoxel(world, chartName, chart, state, x, y, z); |
| | | 1768 | | } |
| | 21 | 1769 | | } |
| | | 1770 | | |
| | | 1771 | | private static void RefreshManagedGeneratedTransitionsForVoxel( |
| | | 1772 | | GridWorld world, |
| | | 1773 | | string chartName, |
| | | 1774 | | NavigationChart chart, |
| | | 1775 | | NavigationChartRegistration state, |
| | | 1776 | | int x, |
| | | 1777 | | int y, |
| | | 1778 | | int z) |
| | | 1779 | | { |
| | 322 | 1780 | | for (int i = 0; i < ManagedGeneratedNeighborOffsets.Length; i++) |
| | | 1781 | | { |
| | 138 | 1782 | | (int dx, int dy, int dz) = ManagedGeneratedNeighborOffsets[i]; |
| | 138 | 1783 | | int neighborX = x + dx; |
| | 138 | 1784 | | int neighborY = y + dy; |
| | 138 | 1785 | | int neighborZ = z + dz; |
| | 138 | 1786 | | if (!chart.IsInBounds(neighborX, neighborY, neighborZ)) |
| | | 1787 | | continue; |
| | | 1788 | | |
| | 71 | 1789 | | if (neighborX < x |
| | 71 | 1790 | | || (neighborX == x && neighborY < y) |
| | 71 | 1791 | | || (neighborX == x && neighborY == y && neighborZ < z)) |
| | | 1792 | | { |
| | 38 | 1793 | | RefreshManagedGeneratedTransitionsForPair( |
| | 38 | 1794 | | world, |
| | 38 | 1795 | | chartName, |
| | 38 | 1796 | | chart, |
| | 38 | 1797 | | state, |
| | 38 | 1798 | | neighborX, |
| | 38 | 1799 | | neighborY, |
| | 38 | 1800 | | neighborZ, |
| | 38 | 1801 | | x, |
| | 38 | 1802 | | y, |
| | 38 | 1803 | | z); |
| | | 1804 | | } |
| | | 1805 | | else |
| | | 1806 | | { |
| | 33 | 1807 | | RefreshManagedGeneratedTransitionsForPair( |
| | 33 | 1808 | | world, |
| | 33 | 1809 | | chartName, |
| | 33 | 1810 | | chart, |
| | 33 | 1811 | | state, |
| | 33 | 1812 | | x, |
| | 33 | 1813 | | y, |
| | 33 | 1814 | | z, |
| | 33 | 1815 | | neighborX, |
| | 33 | 1816 | | neighborY, |
| | 33 | 1817 | | neighborZ); |
| | | 1818 | | } |
| | | 1819 | | } |
| | 23 | 1820 | | } |
| | | 1821 | | |
| | | 1822 | | private static void RefreshManagedGeneratedTransitionsForPair( |
| | | 1823 | | GridWorld world, |
| | | 1824 | | string chartName, |
| | | 1825 | | NavigationChart chart, |
| | | 1826 | | NavigationChartRegistration state, |
| | | 1827 | | int firstX, |
| | | 1828 | | int firstY, |
| | | 1829 | | int firstZ, |
| | | 1830 | | int secondX, |
| | | 1831 | | int secondY, |
| | | 1832 | | int secondZ) |
| | | 1833 | | { |
| | 71 | 1834 | | string[] potentialTransitionIds = GeneratedTraversalTransitionBuilder.GetPotentialTransitionIdsForPair( |
| | 71 | 1835 | | state.TransitionIdPrefix, |
| | 71 | 1836 | | firstX, |
| | 71 | 1837 | | firstY, |
| | 71 | 1838 | | firstZ, |
| | 71 | 1839 | | secondX, |
| | 71 | 1840 | | secondY, |
| | 71 | 1841 | | secondZ); |
| | | 1842 | | |
| | 71 | 1843 | | if (!CanResolveManagedGeneratedPairAnchors(world, chart, firstX, firstY, firstZ, secondX, secondY, secondZ)) |
| | | 1844 | | { |
| | 12 | 1845 | | if (GeneratedTraversalTransitionBuilder.CanBuildTransitionsForPairFromChartData( |
| | 12 | 1846 | | chart, |
| | 12 | 1847 | | firstX, |
| | 12 | 1848 | | firstY, |
| | 12 | 1849 | | firstZ, |
| | 12 | 1850 | | secondX, |
| | 12 | 1851 | | secondY, |
| | 12 | 1852 | | secondZ)) |
| | | 1853 | | { |
| | 0 | 1854 | | TraversalTransitionRegistry.SetManagedTransitionsSuppressed( |
| | 0 | 1855 | | potentialTransitionIds, |
| | 0 | 1856 | | suppressed: true); |
| | | 1857 | | } |
| | | 1858 | | else |
| | | 1859 | | { |
| | 12 | 1860 | | string[] obsoleteSuppressedTransitionIds = GetObsoleteManagedGeneratedTransitionIds( |
| | 12 | 1861 | | state, |
| | 12 | 1862 | | potentialTransitionIds, |
| | 12 | 1863 | | Array.Empty<TraversalTransition>()); |
| | 12 | 1864 | | if (obsoleteSuppressedTransitionIds.Length > 0) |
| | | 1865 | | { |
| | 0 | 1866 | | TraversalTransitionRegistry.UnregisterRange(obsoleteSuppressedTransitionIds); |
| | 0 | 1867 | | RemoveManagedGeneratedTransitionIds(chartName, obsoleteSuppressedTransitionIds); |
| | | 1868 | | } |
| | | 1869 | | } |
| | | 1870 | | |
| | 12 | 1871 | | return; |
| | | 1872 | | } |
| | | 1873 | | |
| | 59 | 1874 | | TraversalTransition[] desiredTransitions = GeneratedTraversalTransitionBuilder.BuildTransitionsForPair( |
| | 59 | 1875 | | chart, |
| | 59 | 1876 | | state.TransitionIdPrefix, |
| | 59 | 1877 | | firstX, |
| | 59 | 1878 | | firstY, |
| | 59 | 1879 | | firstZ, |
| | 59 | 1880 | | secondX, |
| | 59 | 1881 | | secondY, |
| | 59 | 1882 | | secondZ); |
| | | 1883 | | |
| | 59 | 1884 | | string[] obsoleteTransitionIds = GetObsoleteManagedGeneratedTransitionIds( |
| | 59 | 1885 | | state, |
| | 59 | 1886 | | potentialTransitionIds, |
| | 59 | 1887 | | desiredTransitions); |
| | 59 | 1888 | | if (obsoleteTransitionIds.Length > 0) |
| | | 1889 | | { |
| | 1 | 1890 | | TraversalTransitionRegistry.UnregisterRange(obsoleteTransitionIds); |
| | 1 | 1891 | | RemoveManagedGeneratedTransitionIds(chartName, obsoleteTransitionIds); |
| | | 1892 | | } |
| | | 1893 | | |
| | 59 | 1894 | | TraversalTransition[] missingTransitions = GetMissingManagedGeneratedTransitions(state, desiredTransitions); |
| | 59 | 1895 | | if (missingTransitions.Length > 0 |
| | 59 | 1896 | | && TraversalTransitionRegistry.RegisterGeneratedRange( |
| | 59 | 1897 | | missingTransitions, |
| | 59 | 1898 | | state.Priority, |
| | 59 | 1899 | | startSuppressed: true)) |
| | | 1900 | | { |
| | 2 | 1901 | | AddManagedGeneratedTransitionIds(chartName, missingTransitions); |
| | | 1902 | | } |
| | | 1903 | | |
| | 59 | 1904 | | if (desiredTransitions.Length == 0) |
| | 57 | 1905 | | return; |
| | | 1906 | | |
| | 2 | 1907 | | string[] desiredTransitionIds = CopyTransitionIds(desiredTransitions); |
| | 2 | 1908 | | bool shouldBeActive = IsManagedGeneratedPairActive( |
| | 2 | 1909 | | world, |
| | 2 | 1910 | | chartName, |
| | 2 | 1911 | | chart, |
| | 2 | 1912 | | firstX, |
| | 2 | 1913 | | firstY, |
| | 2 | 1914 | | firstZ, |
| | 2 | 1915 | | secondX, |
| | 2 | 1916 | | secondY, |
| | 2 | 1917 | | secondZ); |
| | 2 | 1918 | | TraversalTransitionRegistry.SetManagedTransitionsSuppressed( |
| | 2 | 1919 | | desiredTransitionIds, |
| | 2 | 1920 | | suppressed: !shouldBeActive); |
| | 2 | 1921 | | } |
| | | 1922 | | |
| | | 1923 | | private static string[] GetObsoleteManagedGeneratedTransitionIds( |
| | | 1924 | | NavigationChartRegistration state, |
| | | 1925 | | string[] potentialTransitionIds, |
| | | 1926 | | TraversalTransition[] desiredTransitions) |
| | | 1927 | | { |
| | 73 | 1928 | | if (potentialTransitionIds.Length == 0) |
| | 1 | 1929 | | return Array.Empty<string>(); |
| | | 1930 | | |
| | 72 | 1931 | | SwiftHashSet<string> desiredTransitionIds = SwiftHashSetPool<string>.Shared.Rent(); |
| | | 1932 | | try |
| | | 1933 | | { |
| | 154 | 1934 | | for (int i = 0; i < desiredTransitions.Length; i++) |
| | 5 | 1935 | | desiredTransitionIds.Add(desiredTransitions[i].Id); |
| | | 1936 | | |
| | 72 | 1937 | | SwiftList<string> obsoleteTransitionIds = new(); |
| | 1566 | 1938 | | for (int i = 0; i < potentialTransitionIds.Length; i++) |
| | | 1939 | | { |
| | 711 | 1940 | | string transitionId = potentialTransitionIds[i]; |
| | 711 | 1941 | | if (state.TransitionIds.Contains(transitionId) |
| | 711 | 1942 | | && !desiredTransitionIds.Contains(transitionId)) |
| | | 1943 | | { |
| | 2 | 1944 | | obsoleteTransitionIds.Add(transitionId); |
| | | 1945 | | } |
| | | 1946 | | } |
| | | 1947 | | |
| | 72 | 1948 | | return obsoleteTransitionIds.Count == 0 |
| | 72 | 1949 | | ? Array.Empty<string>() |
| | 72 | 1950 | | : obsoleteTransitionIds.ToArray(); |
| | | 1951 | | } |
| | | 1952 | | finally |
| | | 1953 | | { |
| | 72 | 1954 | | SwiftHashSetPool<string>.Shared.Release(desiredTransitionIds); |
| | 72 | 1955 | | } |
| | 72 | 1956 | | } |
| | | 1957 | | |
| | | 1958 | | private static TraversalTransition[] GetMissingManagedGeneratedTransitions( |
| | | 1959 | | NavigationChartRegistration state, |
| | | 1960 | | TraversalTransition[] desiredTransitions) |
| | | 1961 | | { |
| | 60 | 1962 | | if (desiredTransitions.Length == 0) |
| | 57 | 1963 | | return Array.Empty<TraversalTransition>(); |
| | | 1964 | | |
| | 3 | 1965 | | SwiftList<TraversalTransition> missingTransitions = new(); |
| | 16 | 1966 | | for (int i = 0; i < desiredTransitions.Length; i++) |
| | | 1967 | | { |
| | 5 | 1968 | | TraversalTransition transition = desiredTransitions[i]; |
| | 5 | 1969 | | if (!state.TransitionIds.Contains(transition.Id)) |
| | 4 | 1970 | | missingTransitions.Add(transition); |
| | | 1971 | | } |
| | | 1972 | | |
| | 3 | 1973 | | return missingTransitions.Count == 0 |
| | 3 | 1974 | | ? Array.Empty<TraversalTransition>() |
| | 3 | 1975 | | : missingTransitions.ToArray(); |
| | | 1976 | | } |
| | | 1977 | | |
| | | 1978 | | private static void ApplyManagedGeneratedTransitionDelta( |
| | | 1979 | | string chartName, |
| | | 1980 | | NavigationChartRegistration state, |
| | | 1981 | | SwiftHashSet<string> desiredTransitionIds, |
| | | 1982 | | SwiftHashSet<string> activeTransitionIds, |
| | | 1983 | | TraversalTransition[] missingTransitions) |
| | | 1984 | | { |
| | 1019 | 1985 | | if (missingTransitions != null |
| | 1019 | 1986 | | && missingTransitions.Length > 0 |
| | 1019 | 1987 | | && TraversalTransitionRegistry.RegisterGeneratedRange( |
| | 1019 | 1988 | | missingTransitions, |
| | 1019 | 1989 | | state.Priority, |
| | 1019 | 1990 | | startSuppressed: true)) |
| | | 1991 | | { |
| | 1 | 1992 | | AddManagedGeneratedTransitionIds(chartName, missingTransitions); |
| | | 1993 | | } |
| | | 1994 | | |
| | 1019 | 1995 | | string[] obsoleteTransitionIds = GetObsoleteManagedGeneratedTransitionIds(state, desiredTransitionIds); |
| | 1019 | 1996 | | if (obsoleteTransitionIds.Length > 0) |
| | | 1997 | | { |
| | 1 | 1998 | | TraversalTransitionRegistry.UnregisterRange(obsoleteTransitionIds); |
| | 1 | 1999 | | RemoveManagedGeneratedTransitionIds(chartName, obsoleteTransitionIds); |
| | | 2000 | | } |
| | | 2001 | | |
| | 1019 | 2002 | | SyncManagedGeneratedTransitionSuppressions(state, activeTransitionIds); |
| | 1019 | 2003 | | } |
| | | 2004 | | |
| | | 2005 | | private static string[] GetObsoleteManagedGeneratedTransitionIds( |
| | | 2006 | | NavigationChartRegistration state, |
| | | 2007 | | SwiftHashSet<string> desiredTransitionIds) |
| | | 2008 | | { |
| | 1019 | 2009 | | if (state.TransitionIds.Count == 0) |
| | 1000 | 2010 | | return Array.Empty<string>(); |
| | | 2011 | | |
| | 19 | 2012 | | SwiftList<string> obsoleteTransitionIds = new(); |
| | 166 | 2013 | | foreach (string transitionId in state.TransitionIds) |
| | | 2014 | | { |
| | 64 | 2015 | | if (!desiredTransitionIds.Contains(transitionId)) |
| | 1 | 2016 | | obsoleteTransitionIds.Add(transitionId); |
| | | 2017 | | } |
| | | 2018 | | |
| | 19 | 2019 | | return obsoleteTransitionIds.Count == 0 |
| | 19 | 2020 | | ? Array.Empty<string>() |
| | 19 | 2021 | | : obsoleteTransitionIds.ToArray(); |
| | | 2022 | | } |
| | | 2023 | | |
| | | 2024 | | private static void SyncManagedGeneratedTransitionSuppressions( |
| | | 2025 | | NavigationChartRegistration state, |
| | | 2026 | | SwiftHashSet<string> activeTransitionIds) |
| | | 2027 | | { |
| | 1019 | 2028 | | if (state.TransitionIds.Count == 0) |
| | 1000 | 2029 | | return; |
| | | 2030 | | |
| | 19 | 2031 | | SwiftList<string> transitionsToSuppress = new(); |
| | 19 | 2032 | | SwiftList<string> transitionsToUnsuppress = new(); |
| | 164 | 2033 | | foreach (string transitionId in state.TransitionIds) |
| | | 2034 | | { |
| | 63 | 2035 | | if (activeTransitionIds.Contains(transitionId)) |
| | 57 | 2036 | | transitionsToUnsuppress.Add(transitionId); |
| | | 2037 | | else |
| | 6 | 2038 | | transitionsToSuppress.Add(transitionId); |
| | | 2039 | | } |
| | | 2040 | | |
| | 19 | 2041 | | if (transitionsToSuppress.Count > 0) |
| | 3 | 2042 | | TraversalTransitionRegistry.SetManagedTransitionsSuppressed( |
| | 3 | 2043 | | transitionsToSuppress.ToArray(), |
| | 3 | 2044 | | suppressed: true); |
| | | 2045 | | |
| | 19 | 2046 | | if (transitionsToUnsuppress.Count > 0) |
| | 16 | 2047 | | TraversalTransitionRegistry.SetManagedTransitionsSuppressed( |
| | 16 | 2048 | | transitionsToUnsuppress.ToArray(), |
| | 16 | 2049 | | suppressed: false); |
| | 19 | 2050 | | } |
| | | 2051 | | |
| | | 2052 | | private static void CollectManagedGeneratedTransitionsForPair( |
| | | 2053 | | GridWorld world, |
| | | 2054 | | NavigationChart chart, |
| | | 2055 | | NavigationChartRegistration state, |
| | | 2056 | | int firstX, |
| | | 2057 | | int firstY, |
| | | 2058 | | int firstZ, |
| | | 2059 | | int secondX, |
| | | 2060 | | int secondY, |
| | | 2061 | | int secondZ, |
| | | 2062 | | SwiftHashSet<string> desiredTransitionIds, |
| | | 2063 | | SwiftHashSet<string> activeTransitionIds, |
| | | 2064 | | SwiftList<TraversalTransition> missingTransitions) |
| | | 2065 | | { |
| | 61 | 2066 | | if (!CanResolveManagedGeneratedPairAnchors(world, chart, firstX, firstY, firstZ, secondX, secondY, secondZ)) |
| | | 2067 | | { |
| | 2 | 2068 | | if (GeneratedTraversalTransitionBuilder.CanBuildTransitionsForPairFromChartData( |
| | 2 | 2069 | | chart, |
| | 2 | 2070 | | firstX, |
| | 2 | 2071 | | firstY, |
| | 2 | 2072 | | firstZ, |
| | 2 | 2073 | | secondX, |
| | 2 | 2074 | | secondY, |
| | 2 | 2075 | | secondZ)) |
| | | 2076 | | { |
| | 2 | 2077 | | AddPotentialManagedGeneratedTransitionIds( |
| | 2 | 2078 | | state.TransitionIdPrefix, |
| | 2 | 2079 | | firstX, |
| | 2 | 2080 | | firstY, |
| | 2 | 2081 | | firstZ, |
| | 2 | 2082 | | secondX, |
| | 2 | 2083 | | secondY, |
| | 2 | 2084 | | secondZ, |
| | 2 | 2085 | | desiredTransitionIds); |
| | | 2086 | | } |
| | | 2087 | | |
| | 2 | 2088 | | return; |
| | | 2089 | | } |
| | | 2090 | | |
| | 59 | 2091 | | TraversalTransition[] pairTransitions = GeneratedTraversalTransitionBuilder.BuildTransitionsForPair( |
| | 59 | 2092 | | chart, |
| | 59 | 2093 | | state.TransitionIdPrefix, |
| | 59 | 2094 | | firstX, |
| | 59 | 2095 | | firstY, |
| | 59 | 2096 | | firstZ, |
| | 59 | 2097 | | secondX, |
| | 59 | 2098 | | secondY, |
| | 59 | 2099 | | secondZ); |
| | 59 | 2100 | | if (pairTransitions.Length == 0) |
| | 27 | 2101 | | return; |
| | | 2102 | | |
| | 32 | 2103 | | bool isActive = IsManagedGeneratedPairActive( |
| | 32 | 2104 | | world, |
| | 32 | 2105 | | chart.Name, |
| | 32 | 2106 | | chart, |
| | 32 | 2107 | | firstX, |
| | 32 | 2108 | | firstY, |
| | 32 | 2109 | | firstZ, |
| | 32 | 2110 | | secondX, |
| | 32 | 2111 | | secondY, |
| | 32 | 2112 | | secondZ); |
| | 192 | 2113 | | for (int i = 0; i < pairTransitions.Length; i++) |
| | | 2114 | | { |
| | 64 | 2115 | | TraversalTransition transition = pairTransitions[i]; |
| | 64 | 2116 | | desiredTransitionIds.Add(transition.Id); |
| | 64 | 2117 | | if (isActive) |
| | 60 | 2118 | | activeTransitionIds.Add(transition.Id); |
| | | 2119 | | |
| | 64 | 2120 | | if (!state.TransitionIds.Contains(transition.Id)) |
| | 4 | 2121 | | missingTransitions.Add(transition); |
| | | 2122 | | } |
| | 32 | 2123 | | } |
| | | 2124 | | |
| | | 2125 | | private static bool CanResolveManagedGeneratedPairAnchors( |
| | | 2126 | | GridWorld world, |
| | | 2127 | | NavigationChart chart, |
| | | 2128 | | int firstX, |
| | | 2129 | | int firstY, |
| | | 2130 | | int firstZ, |
| | | 2131 | | int secondX, |
| | | 2132 | | int secondY, |
| | | 2133 | | int secondZ) |
| | | 2134 | | { |
| | 132 | 2135 | | return world.TryGetVoxel(chart.GetWorldPosition(firstX, firstY, firstZ), out _) |
| | 132 | 2136 | | && world.TryGetVoxel(chart.GetWorldPosition(secondX, secondY, secondZ), out _); |
| | | 2137 | | } |
| | | 2138 | | |
| | | 2139 | | private static void AddPotentialManagedGeneratedTransitionIds( |
| | | 2140 | | string transitionIdPrefix, |
| | | 2141 | | int firstX, |
| | | 2142 | | int firstY, |
| | | 2143 | | int firstZ, |
| | | 2144 | | int secondX, |
| | | 2145 | | int secondY, |
| | | 2146 | | int secondZ, |
| | | 2147 | | SwiftHashSet<string> desiredTransitionIds) |
| | | 2148 | | { |
| | 2 | 2149 | | string[] potentialTransitionIds = GeneratedTraversalTransitionBuilder.GetPotentialTransitionIdsForPair( |
| | 2 | 2150 | | transitionIdPrefix, |
| | 2 | 2151 | | firstX, |
| | 2 | 2152 | | firstY, |
| | 2 | 2153 | | firstZ, |
| | 2 | 2154 | | secondX, |
| | 2 | 2155 | | secondY, |
| | 2 | 2156 | | secondZ); |
| | | 2157 | | |
| | 44 | 2158 | | for (int i = 0; i < potentialTransitionIds.Length; i++) |
| | 20 | 2159 | | desiredTransitionIds.Add(potentialTransitionIds[i]); |
| | 2 | 2160 | | } |
| | | 2161 | | |
| | | 2162 | | private static bool ShouldCollectManagedGeneratedPair( |
| | | 2163 | | int firstX, |
| | | 2164 | | int firstY, |
| | | 2165 | | int firstZ, |
| | | 2166 | | int secondX, |
| | | 2167 | | int secondY, |
| | | 2168 | | int secondZ, |
| | | 2169 | | NavigationChartCell secondCell) |
| | | 2170 | | { |
| | 86 | 2171 | | if (!IsManagedGeneratedTransitionCandidate(secondCell)) |
| | 34 | 2172 | | return true; |
| | | 2173 | | |
| | 52 | 2174 | | return firstX < secondX |
| | 52 | 2175 | | || (firstX == secondX && firstY < secondY) |
| | 52 | 2176 | | || (firstX == secondX && firstY == secondY && firstZ < secondZ); |
| | | 2177 | | } |
| | | 2178 | | |
| | | 2179 | | private static bool IsManagedGeneratedTransitionCandidate(NavigationChartCell cell) |
| | | 2180 | | { |
| | 86 | 2181 | | return cell.CanGenerateTransition |
| | 86 | 2182 | | || (cell.Flags & NavigationChartCellFlags.ClimbSurfaceHint) != 0; |
| | | 2183 | | } |
| | | 2184 | | |
| | | 2185 | | private static bool IsManagedGeneratedPairActive( |
| | | 2186 | | GridWorld world, |
| | | 2187 | | string chartName, |
| | | 2188 | | NavigationChart chart, |
| | | 2189 | | int firstX, |
| | | 2190 | | int firstY, |
| | | 2191 | | int firstZ, |
| | | 2192 | | int secondX, |
| | | 2193 | | int secondY, |
| | | 2194 | | int secondZ) |
| | | 2195 | | { |
| | 34 | 2196 | | if (!IsChartInitialized(chartName)) |
| | 3 | 2197 | | return false; |
| | | 2198 | | |
| | 31 | 2199 | | return IsChartEffectiveOwnerAtPosition(world, chartName, chart.GetWorldPosition(firstX, firstY, firstZ)) |
| | 31 | 2200 | | && IsChartEffectiveOwnerAtPosition(world, chartName, chart.GetWorldPosition(secondX, secondY, secondZ)); |
| | | 2201 | | } |
| | | 2202 | | |
| | | 2203 | | private static bool IsChartEffectiveOwnerAtPosition(GridWorld world, string chartName, Vector3d worldPosition) |
| | | 2204 | | { |
| | 64 | 2205 | | if (!TryGetResolvedChartVoxelState(world, worldPosition, out _, out ResolvedChartVoxelState? state)) |
| | 1 | 2206 | | return false; |
| | | 2207 | | |
| | 63 | 2208 | | return string.Equals(state!.EffectiveChartOwner, chartName, StringComparison.Ordinal); |
| | | 2209 | | } |
| | | 2210 | | |
| | | 2211 | | private static void AddManagedGeneratedTransitionIds( |
| | | 2212 | | string chartName, |
| | | 2213 | | TraversalTransition[] transitions) |
| | | 2214 | | { |
| | 5 | 2215 | | _navigationChartMapLock.EnterWriteLock(); |
| | | 2216 | | try |
| | | 2217 | | { |
| | 5 | 2218 | | if (!TryGetNavigationChartRegistration_NoLock(chartName, out NavigationChartRegistration state)) |
| | 1 | 2219 | | return; |
| | | 2220 | | |
| | 20 | 2221 | | for (int i = 0; i < transitions.Length; i++) |
| | 6 | 2222 | | state.TransitionIds.Add(transitions[i].Id); |
| | 4 | 2223 | | } |
| | 10 | 2224 | | finally { _navigationChartMapLock.ExitWriteLock(); } |
| | 5 | 2225 | | } |
| | | 2226 | | |
| | | 2227 | | private static void RemoveManagedGeneratedTransitionIds( |
| | | 2228 | | string chartName, |
| | | 2229 | | string[] transitionIds) |
| | | 2230 | | { |
| | 4 | 2231 | | _navigationChartMapLock.EnterWriteLock(); |
| | | 2232 | | try |
| | | 2233 | | { |
| | 4 | 2234 | | if (!TryGetNavigationChartRegistration_NoLock(chartName, out NavigationChartRegistration state)) |
| | 1 | 2235 | | return; |
| | | 2236 | | |
| | 14 | 2237 | | for (int i = 0; i < transitionIds.Length; i++) |
| | 4 | 2238 | | state.TransitionIds.Remove(transitionIds[i]); |
| | 3 | 2239 | | } |
| | 8 | 2240 | | finally { _navigationChartMapLock.ExitWriteLock(); } |
| | 4 | 2241 | | } |
| | | 2242 | | |
| | | 2243 | | private static string[] CopyTransitionIds(SwiftHashSet<string> transitionIds) |
| | | 2244 | | { |
| | 202 | 2245 | | if (transitionIds.Count == 0) |
| | 196 | 2246 | | return Array.Empty<string>(); |
| | | 2247 | | |
| | 6 | 2248 | | string[] copy = new string[transitionIds.Count]; |
| | 6 | 2249 | | int index = 0; |
| | 38 | 2250 | | foreach (string transitionId in transitionIds) |
| | 13 | 2251 | | copy[index++] = transitionId; |
| | | 2252 | | |
| | 6 | 2253 | | return copy; |
| | | 2254 | | } |
| | | 2255 | | |
| | | 2256 | | private static string[] CopyTransitionIds(TraversalTransition[] transitions) |
| | | 2257 | | { |
| | 2 | 2258 | | string[] ids = new string[transitions.Length]; |
| | 12 | 2259 | | for (int i = 0; i < transitions.Length; i++) |
| | 4 | 2260 | | ids[i] = transitions[i].Id; |
| | | 2261 | | |
| | 2 | 2262 | | return ids; |
| | | 2263 | | } |
| | | 2264 | | |
| | | 2265 | | private static void RemoveChartFromRegistry(string chartName) |
| | | 2266 | | { |
| | 202 | 2267 | | _navigationChartMapLock.EnterWriteLock(); |
| | 404 | 2268 | | try { _navigationChartMap.Remove(chartName); } |
| | 404 | 2269 | | finally { _navigationChartMapLock.ExitWriteLock(); } |
| | 202 | 2270 | | } |
| | | 2271 | | |
| | | 2272 | | private static bool TryGetResolvedChartVoxelState( |
| | | 2273 | | GridWorld world, |
| | | 2274 | | Vector3d worldPosition, |
| | | 2275 | | out WorldVoxelIndex voxelIndex, |
| | | 2276 | | out ResolvedChartVoxelState? state) |
| | | 2277 | | { |
| | 99 | 2278 | | if (world.TryGetVoxel(worldPosition, out Voxel? voxel)) |
| | | 2279 | | { |
| | 94 | 2280 | | voxelIndex = voxel!.WorldIndex; |
| | 94 | 2281 | | return TryGetResolvedChartVoxelState(voxelIndex, out state); |
| | | 2282 | | } |
| | | 2283 | | |
| | 5 | 2284 | | voxelIndex = default; |
| | 5 | 2285 | | state = null; |
| | 5 | 2286 | | return false; |
| | | 2287 | | } |
| | | 2288 | | |
| | | 2289 | | private static bool TryGetResolvedChartVoxelState( |
| | | 2290 | | WorldVoxelIndex voxelIndex, |
| | | 2291 | | out ResolvedChartVoxelState? state) |
| | | 2292 | | { |
| | 100 | 2293 | | if (_resolvedChartVoxelStates.TryGetValue(voxelIndex, out state) |
| | 100 | 2294 | | && state != null |
| | 100 | 2295 | | && state.HasAnyOwners |
| | 100 | 2296 | | && !string.IsNullOrEmpty(state.EffectiveChartOwner)) |
| | | 2297 | | { |
| | 89 | 2298 | | return true; |
| | | 2299 | | } |
| | | 2300 | | |
| | 11 | 2301 | | state = null; |
| | 11 | 2302 | | return false; |
| | | 2303 | | } |
| | | 2304 | | |
| | | 2305 | | private static bool TryApplyChartCellUpdate( |
| | | 2306 | | GridWorld world, |
| | | 2307 | | NavigationChartRegistration registration, |
| | | 2308 | | int x, |
| | | 2309 | | int y, |
| | | 2310 | | int z, |
| | | 2311 | | NavigationChartCell cell, |
| | | 2312 | | SwiftHashSet<SolidChartPartition> partitionsToRebind, |
| | | 2313 | | SwiftHashSet<string> invalidatedChartKeys, |
| | | 2314 | | SwiftHashSet<string> managedChartsToRefresh) |
| | | 2315 | | { |
| | 26 | 2316 | | NavigationChart chart = registration.Chart; |
| | 26 | 2317 | | if (!chart.TrySetCell(x, y, z, cell, out NavigationChartCell previousCell)) |
| | 5 | 2318 | | return false; |
| | | 2319 | | |
| | 21 | 2320 | | TrackManagedChartRefresh(chart, managedChartsToRefresh); |
| | | 2321 | | |
| | 21 | 2322 | | if (!registration.IsInitialized) |
| | 5 | 2323 | | return true; |
| | | 2324 | | |
| | 16 | 2325 | | if (!TryGetChartUpdateVoxelContext( |
| | 16 | 2326 | | world, |
| | 16 | 2327 | | chart, |
| | 16 | 2328 | | x, |
| | 16 | 2329 | | y, |
| | 16 | 2330 | | z, |
| | 16 | 2331 | | managedChartsToRefresh, |
| | 16 | 2332 | | out Voxel? voxel, |
| | 16 | 2333 | | out ResolvedChartVoxelState? state, |
| | 16 | 2334 | | out NavigationChartCell previousEffectiveCell, |
| | 16 | 2335 | | out string? previousEffectiveOwner)) |
| | | 2336 | | { |
| | 1 | 2337 | | return true; |
| | | 2338 | | } |
| | | 2339 | | |
| | 15 | 2340 | | TryUpdateResolvedVoxelStateForChartCell(registration, cell, voxel!.WorldIndex, ref state); |
| | 15 | 2341 | | TrackInitializedChartGridTouchDelta(voxel.GridIndex, chart.Name, previousCell, cell); |
| | | 2342 | | |
| | 15 | 2343 | | ApplyResolvedVoxelState(world, voxel, state, previousEffectiveCell, partitionsToRebind); |
| | 15 | 2344 | | CollectEffectiveStateInvalidations( |
| | 15 | 2345 | | previousEffectiveOwner, |
| | 15 | 2346 | | previousEffectiveCell, |
| | 15 | 2347 | | state?.EffectiveChartOwner ?? string.Empty, |
| | 15 | 2348 | | state?.EffectiveCell ?? NavigationChartCell.Empty, |
| | 15 | 2349 | | invalidatedChartKeys); |
| | 15 | 2350 | | if (state != null && state.HasAnyOwners) |
| | 10 | 2351 | | state.AddChartOwnersTo(managedChartsToRefresh); |
| | | 2352 | | |
| | 15 | 2353 | | return true; |
| | | 2354 | | } |
| | | 2355 | | |
| | | 2356 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 2357 | | private static void TrackManagedChartRefresh( |
| | | 2358 | | NavigationChart chart, |
| | | 2359 | | SwiftHashSet<string> managedChartsToRefresh) |
| | | 2360 | | { |
| | 21 | 2361 | | managedChartsToRefresh.Add(chart.Name); |
| | 21 | 2362 | | } |
| | | 2363 | | |
| | | 2364 | | private static bool TryGetChartUpdateVoxelContext( |
| | | 2365 | | GridWorld world, |
| | | 2366 | | NavigationChart chart, |
| | | 2367 | | int x, |
| | | 2368 | | int y, |
| | | 2369 | | int z, |
| | | 2370 | | SwiftHashSet<string> managedChartsToRefresh, |
| | | 2371 | | out Voxel? voxel, |
| | | 2372 | | out ResolvedChartVoxelState? state, |
| | | 2373 | | out NavigationChartCell previousEffectiveCell, |
| | | 2374 | | out string? previousEffectiveOwner) |
| | | 2375 | | { |
| | 16 | 2376 | | state = null; |
| | 16 | 2377 | | previousEffectiveCell = NavigationChartCell.Empty; |
| | 16 | 2378 | | previousEffectiveOwner = null; |
| | | 2379 | | |
| | 16 | 2380 | | Vector3d position = chart.GetWorldPosition(x, y, z); |
| | 16 | 2381 | | if (!world.TryGetVoxel(position, out voxel)) |
| | 1 | 2382 | | return false; |
| | | 2383 | | |
| | 15 | 2384 | | _resolvedChartVoxelStates.TryGetValue(voxel!.WorldIndex, out state); |
| | 15 | 2385 | | if (state != null && state.HasAnyOwners) |
| | | 2386 | | { |
| | 12 | 2387 | | state.AddChartOwnersTo(managedChartsToRefresh); |
| | 12 | 2388 | | previousEffectiveCell = state.EffectiveCell; |
| | 12 | 2389 | | previousEffectiveOwner = state.EffectiveChartOwner; |
| | | 2390 | | } |
| | | 2391 | | |
| | 15 | 2392 | | return true; |
| | | 2393 | | } |
| | | 2394 | | |
| | | 2395 | | private static void TryUpdateResolvedVoxelStateForChartCell( |
| | | 2396 | | NavigationChartRegistration registration, |
| | | 2397 | | NavigationChartCell cell, |
| | | 2398 | | WorldVoxelIndex voxelIndex, |
| | | 2399 | | ref ResolvedChartVoxelState? state) |
| | | 2400 | | { |
| | 18 | 2401 | | NavigationChart chart = registration.Chart; |
| | 18 | 2402 | | if (cell.HasTraversalData) |
| | | 2403 | | { |
| | 10 | 2404 | | state ??= new ResolvedChartVoxelState(); |
| | 10 | 2405 | | state.AddOwner(chart.Name, cell, chart.Priority, registration.RegistrationOrder); |
| | 10 | 2406 | | _resolvedChartVoxelStates[voxelIndex] = state; |
| | 10 | 2407 | | return; |
| | | 2408 | | } |
| | | 2409 | | |
| | 8 | 2410 | | if (state == null || !state.ContainsOwner(chart.Name)) |
| | 1 | 2411 | | return; |
| | | 2412 | | |
| | 7 | 2413 | | state.RemoveOwner(chart.Name); |
| | 7 | 2414 | | if (!state.HasAnyOwners) |
| | 6 | 2415 | | _resolvedChartVoxelStates.Remove(voxelIndex); |
| | 7 | 2416 | | } |
| | | 2417 | | |
| | | 2418 | | private static void RebindAndInvalidate( |
| | | 2419 | | SwiftHashSet<SolidChartPartition> partitionsToRebind, |
| | | 2420 | | SwiftHashSet<string> invalidatedChartKeys) |
| | | 2421 | | { |
| | 23 | 2422 | | BindCollectedSolidPartitions(partitionsToRebind); |
| | | 2423 | | |
| | 23 | 2424 | | if (partitionsToRebind.Count > 0 || invalidatedChartKeys.Count > 0) |
| | 14 | 2425 | | SolidPartitionReachability.Invalidate(); |
| | | 2426 | | |
| | 78 | 2427 | | foreach (string chartKey in invalidatedChartKeys) |
| | 16 | 2428 | | PathGuideFactory.InvalidateCacheFor(chartKey); |
| | 23 | 2429 | | } |
| | | 2430 | | |
| | | 2431 | | private static void CollectEffectiveStateInvalidations( |
| | | 2432 | | string? previousEffectiveOwner, |
| | | 2433 | | NavigationChartCell previousEffectiveCell, |
| | | 2434 | | string? currentEffectiveOwner, |
| | | 2435 | | NavigationChartCell currentEffectiveCell, |
| | | 2436 | | SwiftHashSet<string> invalidatedChartKeys) |
| | | 2437 | | { |
| | 15 | 2438 | | if (previousEffectiveCell.Equals(currentEffectiveCell) |
| | 15 | 2439 | | && string.Equals(previousEffectiveOwner, currentEffectiveOwner, StringComparison.Ordinal)) |
| | | 2440 | | { |
| | 1 | 2441 | | return; |
| | | 2442 | | } |
| | | 2443 | | |
| | 14 | 2444 | | if (!string.IsNullOrEmpty(previousEffectiveOwner)) |
| | 11 | 2445 | | invalidatedChartKeys.Add(previousEffectiveOwner); |
| | | 2446 | | |
| | 14 | 2447 | | if (!string.IsNullOrEmpty(currentEffectiveOwner)) |
| | 9 | 2448 | | invalidatedChartKeys.Add(currentEffectiveOwner); |
| | 14 | 2449 | | } |
| | | 2450 | | |
| | | 2451 | | internal static bool HasAuthoredVolumeMedium(TraversalMedium medium) |
| | | 2452 | | { |
| | 1 | 2453 | | return HasAuthoredVolumeMedium(ActiveState, medium); |
| | | 2454 | | } |
| | | 2455 | | |
| | | 2456 | | internal static bool HasAuthoredVolumeMedium(PathingWorldState state, TraversalMedium medium) |
| | | 2457 | | { |
| | 454 | 2458 | | return medium switch |
| | 454 | 2459 | | { |
| | 259 | 2460 | | TraversalMedium.Gas => state.ActiveAuthoredGasCellCount > 0, |
| | 194 | 2461 | | TraversalMedium.Liquid => state.ActiveAuthoredLiquidCellCount > 0, |
| | 1 | 2462 | | _ => false |
| | 454 | 2463 | | }; |
| | | 2464 | | } |
| | | 2465 | | |
| | | 2466 | | private static void ApplyResolvedVoxelState( |
| | | 2467 | | GridWorld world, |
| | | 2468 | | Voxel voxel, |
| | | 2469 | | ResolvedChartVoxelState? state, |
| | | 2470 | | NavigationChartCell previousEffectiveCell, |
| | | 2471 | | SwiftHashSet<SolidChartPartition> partitionsToRebind) |
| | | 2472 | | { |
| | 4862 | 2473 | | NavigationChartCell effectiveCell = state?.EffectiveCell ?? NavigationChartCell.Empty; |
| | 4862 | 2474 | | UpdateActiveVolumeMediumCounts(previousEffectiveCell, effectiveCell); |
| | | 2475 | | |
| | 4862 | 2476 | | bool solidPresenceChanged = previousEffectiveCell.HasSolid != effectiveCell.HasSolid; |
| | | 2477 | | |
| | 4862 | 2478 | | if (effectiveCell.HasSolid) |
| | | 2479 | | { |
| | 2456 | 2480 | | if (!voxel.TryGetPartition(out SolidChartPartition? solidPartition)) |
| | | 2481 | | { |
| | 2442 | 2482 | | solidPartition = PartitionPool.Rent(); |
| | 2442 | 2483 | | solidPartition.SetOwner(ActiveState); |
| | 2442 | 2484 | | voxel.TryAddPartition(solidPartition); |
| | | 2485 | | } |
| | 14 | 2486 | | else if (solidPartition!.OwnerState == null) |
| | 0 | 2487 | | solidPartition.SetOwner(ActiveState); |
| | | 2488 | | |
| | 2456 | 2489 | | solidPartition!.ApplyAuthoredState(state, state?.EffectiveChartOwner, effectiveCell); |
| | 2456 | 2490 | | if (solidPresenceChanged) |
| | 2442 | 2491 | | CollectSolidPartitionsForRebind(world, voxel, partitionsToRebind); |
| | | 2492 | | } |
| | 2406 | 2493 | | else if (previousEffectiveCell.HasSolid && voxel.TryGetPartition<SolidChartPartition>(out _)) |
| | | 2494 | | { |
| | 1808 | 2495 | | voxel.TryRemovePartition<SolidChartPartition>(); |
| | 1808 | 2496 | | CollectSolidPartitionsForRebind(world, voxel, partitionsToRebind); |
| | | 2497 | | } |
| | | 2498 | | |
| | 4862 | 2499 | | if (effectiveCell.HasVolume) |
| | | 2500 | | { |
| | 612 | 2501 | | if (!voxel.TryGetPartition(out VolumeChartPartition? volumePartition)) |
| | | 2502 | | { |
| | 607 | 2503 | | volumePartition = VolumeChartPartitionPool.Rent(); |
| | 607 | 2504 | | volumePartition.SetOwner(ActiveState); |
| | 607 | 2505 | | voxel.TryAddPartition(volumePartition); |
| | | 2506 | | } |
| | 5 | 2507 | | else if (volumePartition!.OwnerState == null) |
| | 0 | 2508 | | volumePartition.SetOwner(ActiveState); |
| | | 2509 | | |
| | 612 | 2510 | | volumePartition!.ApplyAuthoredState(state, state?.EffectiveChartOwner, effectiveCell); |
| | | 2511 | | } |
| | 4250 | 2512 | | else if (previousEffectiveCell.HasVolume && voxel.TryGetPartition<VolumeChartPartition>(out _)) |
| | 189 | 2513 | | voxel.TryRemovePartition<VolumeChartPartition>(); |
| | 4250 | 2514 | | } |
| | | 2515 | | |
| | | 2516 | | private static void UpdateActiveVolumeMediumCounts( |
| | | 2517 | | NavigationChartCell previousEffectiveCell, |
| | | 2518 | | NavigationChartCell currentEffectiveCell) |
| | | 2519 | | { |
| | 4862 | 2520 | | if (previousEffectiveCell.SupportsMedium(TraversalMedium.Gas)) |
| | 170 | 2521 | | AdjustActiveAuthoredGasCellCount(-1); |
| | | 2522 | | |
| | 4862 | 2523 | | if (previousEffectiveCell.SupportsMedium(TraversalMedium.Liquid)) |
| | 24 | 2524 | | AdjustActiveAuthoredLiquidCellCount(-1); |
| | | 2525 | | |
| | 4862 | 2526 | | if (currentEffectiveCell.SupportsMedium(TraversalMedium.Gas)) |
| | 449 | 2527 | | AdjustActiveAuthoredGasCellCount(1); |
| | | 2528 | | |
| | 4862 | 2529 | | if (currentEffectiveCell.SupportsMedium(TraversalMedium.Liquid)) |
| | 163 | 2530 | | AdjustActiveAuthoredLiquidCellCount(1); |
| | 4862 | 2531 | | } |
| | | 2532 | | |
| | | 2533 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 2534 | | private static void ClearActiveAuthoredVolumeMediumCounts() |
| | | 2535 | | { |
| | 3264 | 2536 | | PathingWorldState state = ActiveState; |
| | 3264 | 2537 | | state.ActiveAuthoredGasCellCount = 0; |
| | 3264 | 2538 | | state.ActiveAuthoredLiquidCellCount = 0; |
| | 3264 | 2539 | | } |
| | | 2540 | | |
| | | 2541 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 2542 | | private static void AdjustActiveAuthoredGasCellCount(int delta) |
| | | 2543 | | { |
| | 619 | 2544 | | ActiveState.ActiveAuthoredGasCellCount += delta; |
| | 619 | 2545 | | } |
| | | 2546 | | |
| | | 2547 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 2548 | | private static void AdjustActiveAuthoredLiquidCellCount(int delta) |
| | | 2549 | | { |
| | 187 | 2550 | | ActiveState.ActiveAuthoredLiquidCellCount += delta; |
| | 187 | 2551 | | } |
| | | 2552 | | |
| | | 2553 | | private static void CollectSolidPartitionsForRebind( |
| | | 2554 | | GridWorld world, |
| | | 2555 | | Voxel voxel, |
| | | 2556 | | SwiftHashSet<SolidChartPartition> partitionsToRebind) |
| | | 2557 | | { |
| | 4250 | 2558 | | if (!world.TryGetGrid(voxel.WorldIndex.GridIndex, out VoxelGrid? grid)) |
| | 0 | 2559 | | return; |
| | | 2560 | | |
| | 4250 | 2561 | | if (voxel.TryGetPartition(out SolidChartPartition? currentPartition)) |
| | 2442 | 2562 | | partitionsToRebind.Add(currentPartition!); |
| | | 2563 | | |
| | 229500 | 2564 | | foreach (SpatialDirection direction in SpatialAwareness.AllDirections) |
| | | 2565 | | { |
| | 110500 | 2566 | | if (voxel.TryGetNeighborFromDirection(grid!, direction, out Voxel? neighborVoxel, useCache: true) |
| | 110500 | 2567 | | && neighborVoxel!.TryGetPartition(out SolidChartPartition? neighborPartition)) |
| | | 2568 | | { |
| | 10497 | 2569 | | partitionsToRebind.Add(neighborPartition!); |
| | | 2570 | | } |
| | | 2571 | | } |
| | 4250 | 2572 | | } |
| | | 2573 | | |
| | | 2574 | | private static void BindCollectedSolidPartitions(SwiftHashSet<SolidChartPartition> partitionsToRebind) |
| | | 2575 | | { |
| | 1215 | 2576 | | PathingWorldState activeState = ActiveState; |
| | 10572 | 2577 | | foreach (SolidChartPartition partition in partitionsToRebind) |
| | | 2578 | | { |
| | 4071 | 2579 | | if (partition.IsPartitioned && ReferenceEquals(partition.OwnerState, activeState)) |
| | 2483 | 2580 | | partition.BindNeighbors(); |
| | | 2581 | | } |
| | 1215 | 2582 | | } |
| | | 2583 | | |
| | | 2584 | | #endregion |
| | | 2585 | | |
| | | 2586 | | #region Public Utility Methods |
| | | 2587 | | |
| | | 2588 | | /// <summary> |
| | | 2589 | | /// Determines the maximum number of voxels to search based on the start and end voxel's grid sizes. |
| | | 2590 | | /// </summary> |
| | | 2591 | | /// <param name="world">The grid world.</param> |
| | | 2592 | | /// <param name="start">The start voxel.</param> |
| | | 2593 | | /// <param name="end">The end voxel.</param> |
| | | 2594 | | /// <param name="maxSearchSize">The output max search size.</param> |
| | | 2595 | | /// <returns>True if both voxels belong to valid grids; otherwise, false.</returns> |
| | | 2596 | | public static bool TryGetMaxSearchSize(GridWorld world, Voxel start, Voxel end, out int maxSearchSize) |
| | | 2597 | | { |
| | 3 | 2598 | | LinkWorld(world); |
| | 3 | 2599 | | if (!world.TryGetGrid(start.WorldIndex.GridIndex, out VoxelGrid? startGrid) |
| | 3 | 2600 | | || !world.TryGetGrid(end.WorldIndex.GridIndex, out VoxelGrid? endGrid)) |
| | | 2601 | | { |
| | 1 | 2602 | | maxSearchSize = 0; |
| | 1 | 2603 | | return false; |
| | | 2604 | | } |
| | | 2605 | | |
| | 2 | 2606 | | maxSearchSize = startGrid == endGrid |
| | 2 | 2607 | | ? startGrid!.Size |
| | 2 | 2608 | | : startGrid!.Size + endGrid!.Size; |
| | 2 | 2609 | | return true; |
| | | 2610 | | } |
| | | 2611 | | |
| | | 2612 | | /// <summary> |
| | | 2613 | | /// Determines the maximum number of voxels to search based on the start and end voxel's grid sizes using the config |
| | | 2614 | | /// </summary> |
| | | 2615 | | public static bool TryGetMaxSearchSize(Voxel start, Voxel end, out int maxSearchSize) |
| | | 2616 | | { |
| | 3 | 2617 | | return TryGetMaxSearchSize(GetConfiguredWorld(), start, end, out maxSearchSize); |
| | | 2618 | | } |
| | | 2619 | | |
| | | 2620 | | /// <summary> |
| | | 2621 | | /// Checks if a path is needed between the start and end positions based on traced voxels and unit size. |
| | | 2622 | | /// </summary> |
| | | 2623 | | /// <param name="world">The grid world.</param> |
| | | 2624 | | /// <param name="startPos">The starting position.</param> |
| | | 2625 | | /// <param name="endPos">The destination position.</param> |
| | | 2626 | | /// <param name="unitSize">The size of the navigating unit.</param> |
| | | 2627 | | /// <param name="includeEnd">Whether to permit unwalkable voxels.</param> |
| | | 2628 | | /// <returns>True if a path is required; otherwise, false.</returns> |
| | | 2629 | | public static bool NeedsPath( |
| | | 2630 | | GridWorld world, |
| | | 2631 | | Vector3d startPos, |
| | | 2632 | | Vector3d endPos, |
| | | 2633 | | Fixed64 unitSize, |
| | | 2634 | | bool includeEnd = false) |
| | | 2635 | | { |
| | 88 | 2636 | | LinkWorld(world); |
| | 323 | 2637 | | foreach (GridVoxelSet gridVoxelSet in GridTracer.TraceLine(world, startPos, endPos)) |
| | | 2638 | | { |
| | 677 | 2639 | | foreach (Voxel voxel in gridVoxelSet.Voxels) |
| | | 2640 | | { |
| | | 2641 | | // A path is required if a voxel doesn't exist in the traced line |
| | 265 | 2642 | | if (!voxel.TryGetPartition(out SolidChartPartition? partition)) |
| | 28 | 2643 | | return true; |
| | | 2644 | | |
| | 237 | 2645 | | if (!includeEnd && !voxel.IsBlocked && partition!.IsImpassable(unitSize)) |
| | 1 | 2646 | | return true; |
| | | 2647 | | } |
| | | 2648 | | } |
| | 59 | 2649 | | return false; |
| | 29 | 2650 | | } |
| | | 2651 | | |
| | | 2652 | | /// <summary> |
| | | 2653 | | /// Checks if a path is needed between the start and end positions using the configured world. |
| | | 2654 | | /// </summary> |
| | | 2655 | | public static bool NeedsPath( |
| | | 2656 | | Vector3d startPos, |
| | | 2657 | | Vector3d endPos, |
| | | 2658 | | Fixed64 unitSize, |
| | | 2659 | | bool includeEnd = false) |
| | | 2660 | | { |
| | 3 | 2661 | | return NeedsPath(GetConfiguredWorld(), startPos, endPos, unitSize, includeEnd); |
| | | 2662 | | } |
| | | 2663 | | |
| | | 2664 | | #endregion |
| | | 2665 | | } |