| | | 1 | | using FixedMathSharp; |
| | | 2 | | using GridForge.Grids; |
| | | 3 | | using GridForge.Spatial; |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | |
| | | 7 | | namespace Trailblazer.Pathing; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Context-owned pathing API for chart registration, live chart state, and local pathing queries. |
| | | 11 | | /// </summary> |
| | | 12 | | public sealed class TrailblazerPathingService |
| | | 13 | | { |
| | | 14 | | private readonly TrailblazerWorldContext _context; |
| | | 15 | | |
| | | 16 | | private bool _disposed; |
| | | 17 | | |
| | 961 | 18 | | internal TrailblazerPathingService(TrailblazerWorldContext context) |
| | | 19 | | { |
| | 961 | 20 | | _context = context; |
| | 961 | 21 | | State = new PathingWorldState(context); |
| | 961 | 22 | | } |
| | | 23 | | |
| | | 24 | | internal PathingWorldState State { get; } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Gets a snapshot of all charts registered to this context. |
| | | 28 | | /// </summary> |
| | | 29 | | public IEnumerable<NavigationChart> AllCharts |
| | | 30 | | { |
| | | 31 | | get |
| | | 32 | | { |
| | 5 | 33 | | using (EnterUsableState()) |
| | 3 | 34 | | return PathManager.AllCharts; |
| | 3 | 35 | | } |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <inheritdoc cref="PathManager.Register(NavigationChart,bool)"/> |
| | | 39 | | public bool Register(NavigationChart chart, bool initializeChart = true) |
| | | 40 | | { |
| | 795 | 41 | | using (EnterUsableState()) |
| | 795 | 42 | | return PathManager.Register(_context.World, chart, initializeChart); |
| | 795 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <inheritdoc cref="PathManager.Register(TraversalBuildResult,bool)"/> |
| | | 46 | | public bool Register(TraversalBuildResult buildResult, bool initializeChart = true) |
| | | 47 | | { |
| | 4 | 48 | | using (EnterUsableState()) |
| | 4 | 49 | | return PathManager.Register(_context.World, buildResult, initializeChart); |
| | 4 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <inheritdoc cref="PathManager.IsChartRegistered(string)"/> |
| | | 53 | | public bool IsChartRegistered(string name) |
| | | 54 | | { |
| | 9 | 55 | | using (EnterUsableState()) |
| | 9 | 56 | | return PathManager.IsChartRegistered(name); |
| | 9 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <inheritdoc cref="PathManager.TryGetNavigationChart(string,out NavigationChart)"/> |
| | | 60 | | public bool TryGetNavigationChart(string name, out NavigationChart chart) |
| | | 61 | | { |
| | 1 | 62 | | using (EnterUsableState()) |
| | 1 | 63 | | return PathManager.TryGetNavigationChart(name, out chart); |
| | 1 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <inheritdoc cref="PathManager.TryGetNavigationChartRegistration(string,out NavigationChartRegistration)"/> |
| | | 67 | | public bool TryGetNavigationChartRegistration(string name, out NavigationChartRegistration registration) |
| | | 68 | | { |
| | 1 | 69 | | using (EnterUsableState()) |
| | 1 | 70 | | return PathManager.TryGetNavigationChartRegistration(name, out registration); |
| | 1 | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <inheritdoc cref="PathManager.IsChartInitialized(string)"/> |
| | | 74 | | public bool IsChartInitialized(string name) |
| | | 75 | | { |
| | 4 | 76 | | using (EnterUsableState()) |
| | 4 | 77 | | return PathManager.IsChartInitialized(name); |
| | 4 | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <inheritdoc cref="PathManager.IsChartInitialized(NavigationChart)"/> |
| | | 81 | | public bool IsChartInitialized(NavigationChart chart) |
| | | 82 | | { |
| | 6 | 83 | | using (EnterUsableState()) |
| | 6 | 84 | | return PathManager.IsChartInitialized(chart); |
| | 6 | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <inheritdoc cref="PathManager.InitializeAllCharts()"/> |
| | | 88 | | public void InitializeAllCharts() |
| | | 89 | | { |
| | 1 | 90 | | using (EnterUsableState()) |
| | 1 | 91 | | PathManager.InitializeAllCharts(_context.World); |
| | 1 | 92 | | } |
| | | 93 | | |
| | | 94 | | /// <inheritdoc cref="PathManager.InitializeChart(string)"/> |
| | | 95 | | public void InitializeChart(string chartKey) |
| | | 96 | | { |
| | 1 | 97 | | using (EnterUsableState()) |
| | 1 | 98 | | PathManager.InitializeChart(_context.World, chartKey); |
| | 1 | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <inheritdoc cref="PathManager.UnloadChart(string)"/> |
| | | 102 | | public void UnloadChart(string chartKey) |
| | | 103 | | { |
| | 3 | 104 | | using (EnterUsableState()) |
| | 3 | 105 | | PathManager.UnloadChart(_context.World, chartKey); |
| | 3 | 106 | | } |
| | | 107 | | |
| | | 108 | | /// <inheritdoc cref="PathManager.UnloadChart(NavigationChart)"/> |
| | | 109 | | public void UnloadChart(NavigationChart chart) |
| | | 110 | | { |
| | 2 | 111 | | using (EnterUsableState()) |
| | 2 | 112 | | PathManager.UnloadChart(_context.World, chart); |
| | 2 | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <inheritdoc cref="PathManager.TryGetEffectiveCell(GridWorld,Vector3d,out NavigationChartCell)"/> |
| | | 116 | | public bool TryGetEffectiveCell(Vector3d worldPosition, out NavigationChartCell cell) |
| | | 117 | | { |
| | 10 | 118 | | using (EnterUsableState()) |
| | 10 | 119 | | return PathManager.TryGetEffectiveCell(_context.World, worldPosition, out cell); |
| | 10 | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <inheritdoc cref="PathManager.TryGetEffectiveCell(WorldVoxelIndex,out NavigationChartCell)"/> |
| | | 123 | | public bool TryGetEffectiveCell(WorldVoxelIndex voxelIndex, out NavigationChartCell cell) |
| | | 124 | | { |
| | 1 | 125 | | using (EnterUsableState()) |
| | 1 | 126 | | return PathManager.TryGetEffectiveCell(voxelIndex, out cell); |
| | 1 | 127 | | } |
| | | 128 | | |
| | | 129 | | /// <inheritdoc cref="PathManager.TryGetEffectiveChartOwner(GridWorld,Vector3d,out string?)"/> |
| | | 130 | | public bool TryGetEffectiveChartOwner(Vector3d worldPosition, out string? chartName) |
| | | 131 | | { |
| | 4 | 132 | | using (EnterUsableState()) |
| | 4 | 133 | | return PathManager.TryGetEffectiveChartOwner(_context.World, worldPosition, out chartName); |
| | 4 | 134 | | } |
| | | 135 | | |
| | | 136 | | /// <inheritdoc cref="PathManager.TryGetEffectiveChartOwner(WorldVoxelIndex,out string?)"/> |
| | | 137 | | public bool TryGetEffectiveChartOwner(WorldVoxelIndex voxelIndex, out string? chartName) |
| | | 138 | | { |
| | 1 | 139 | | using (EnterUsableState()) |
| | 1 | 140 | | return PathManager.TryGetEffectiveChartOwner(voxelIndex, out chartName); |
| | 1 | 141 | | } |
| | | 142 | | |
| | | 143 | | /// <inheritdoc cref="PathManager.TryUpdateChartCell(string,int,int,int,NavigationChartCell)"/> |
| | | 144 | | public bool TryUpdateChartCell(string chartName, int x, int y, int z, NavigationChartCell cell) |
| | | 145 | | { |
| | 1 | 146 | | using (EnterUsableState()) |
| | 1 | 147 | | return PathManager.TryUpdateChartCell(_context.World, chartName, x, y, z, cell); |
| | 1 | 148 | | } |
| | | 149 | | |
| | | 150 | | /// <inheritdoc cref="PathManager.TryUpdateChartCell(string,Vector3d,NavigationChartCell)"/> |
| | | 151 | | public bool TryUpdateChartCell(string chartName, Vector3d worldPosition, NavigationChartCell cell) |
| | | 152 | | { |
| | 2 | 153 | | using (EnterUsableState()) |
| | 2 | 154 | | return PathManager.TryUpdateChartCell(_context.World, chartName, worldPosition, cell); |
| | 2 | 155 | | } |
| | | 156 | | |
| | | 157 | | /// <inheritdoc cref="PathManager.ApplyChartUpdates(string,IReadOnlyList{NavigationChartCellUpdate})"/> |
| | | 158 | | public int ApplyChartUpdates(string chartName, IReadOnlyList<NavigationChartCellUpdate> updates) |
| | | 159 | | { |
| | 1 | 160 | | using (EnterUsableState()) |
| | 1 | 161 | | return PathManager.ApplyChartUpdates(_context.World, chartName, updates); |
| | 1 | 162 | | } |
| | | 163 | | |
| | | 164 | | /// <summary> |
| | | 165 | | /// Flushes pending grid event rebuild work for this context. |
| | | 166 | | /// </summary> |
| | | 167 | | public void FlushPendingGridChanges() |
| | | 168 | | { |
| | 2300 | 169 | | EnsureUsable(); |
| | 2300 | 170 | | State.ExternalGridBridge.FlushPendingGridChanges(); |
| | 2300 | 171 | | } |
| | | 172 | | |
| | | 173 | | /// <summary> |
| | | 174 | | /// Gets diagnostics for this context's external-grid bridge. |
| | | 175 | | /// </summary> |
| | | 176 | | internal ExternalGridBridgeDiagnosticsSnapshot GetExternalGridBridgeDiagnosticsSnapshot() |
| | | 177 | | { |
| | 2 | 178 | | EnsureUsable(); |
| | 2 | 179 | | return State.ExternalGridBridge.GetDiagnosticsSnapshot(); |
| | | 180 | | } |
| | | 181 | | |
| | | 182 | | internal bool TryGetMaxSearchSize(Voxel start, Voxel end, out int maxSearchSize) |
| | | 183 | | { |
| | 1278 | 184 | | EnsureUsable(); |
| | 1278 | 185 | | GridWorld world = _context.World; |
| | 1278 | 186 | | if (!world.TryGetGrid(start.WorldIndex.GridIndex, out VoxelGrid? startGrid) |
| | 1278 | 187 | | || !world.TryGetGrid(end.WorldIndex.GridIndex, out VoxelGrid? endGrid)) |
| | | 188 | | { |
| | 0 | 189 | | maxSearchSize = 0; |
| | 0 | 190 | | return false; |
| | | 191 | | } |
| | | 192 | | |
| | 1278 | 193 | | maxSearchSize = startGrid == endGrid |
| | 1278 | 194 | | ? startGrid!.Size |
| | 1278 | 195 | | : startGrid!.Size + endGrid!.Size; |
| | 1278 | 196 | | return true; |
| | | 197 | | } |
| | | 198 | | |
| | | 199 | | internal bool NeedsPath( |
| | | 200 | | Vector3d startPosition, |
| | | 201 | | Vector3d endPosition, |
| | | 202 | | Fixed64 unitSize, |
| | | 203 | | bool includeEnd = false) |
| | | 204 | | { |
| | 85 | 205 | | using (EnterUsableState()) |
| | 85 | 206 | | return PathManager.NeedsPath(_context.World, startPosition, endPosition, unitSize, includeEnd); |
| | 85 | 207 | | } |
| | | 208 | | |
| | | 209 | | internal void HandleGridChanged(GridEventInfo eventInfo) |
| | | 210 | | { |
| | 1 | 211 | | using (EnterUsableState()) |
| | 1 | 212 | | PathManagerExternalGridBridge.HandleGridChanged(eventInfo); |
| | 1 | 213 | | } |
| | | 214 | | |
| | | 215 | | /// <summary> |
| | | 216 | | /// Clears this context's registered charts, live partitions, transition registry, volume rules, and guide caches. |
| | | 217 | | /// </summary> |
| | | 218 | | public void Reset() |
| | | 219 | | { |
| | 2 | 220 | | EnsureUsable(); |
| | 2 | 221 | | PathManager.ResetPathingState(State, resetScopedRegistries: true, flushGuideCache: true); |
| | 2 | 222 | | } |
| | | 223 | | |
| | | 224 | | internal void Dispose() |
| | | 225 | | { |
| | 961 | 226 | | if (_disposed) |
| | 0 | 227 | | return; |
| | | 228 | | |
| | 961 | 229 | | State.ExternalGridBridge.Dispose(); |
| | 961 | 230 | | PathManager.ResetPathingState(State, resetScopedRegistries: true, flushGuideCache: true); |
| | 961 | 231 | | State.Dispose(); |
| | 961 | 232 | | _disposed = true; |
| | 961 | 233 | | } |
| | | 234 | | |
| | | 235 | | private IDisposable EnterUsableState() |
| | | 236 | | { |
| | 938 | 237 | | EnsureUsable(); |
| | 936 | 238 | | return PathManager.EnterState(State); |
| | | 239 | | } |
| | | 240 | | |
| | | 241 | | private void EnsureUsable() |
| | | 242 | | { |
| | 4520 | 243 | | if (_disposed || _context.IsDisposed) |
| | 1 | 244 | | throw new ObjectDisposedException(nameof(TrailblazerWorldContext)); |
| | 4519 | 245 | | if (!_context.World.IsActive) |
| | 1 | 246 | | throw new InvalidOperationException("TrailblazerPathingService is bound to an inactive GridWorld."); |
| | 4518 | 247 | | } |
| | | 248 | | } |