| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationNativeSurfaceEdgeEnumerator.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using GridForge.Grids.Topology; |
| | | 9 | | using GridForge.Spatial; |
| | | 10 | | |
| | | 11 | | namespace Trailblazer.Pathing; |
| | | 12 | | |
| | | 13 | | /// <summary>Enumerates physically present native surface neighbors in canonical address order.</summary> |
| | | 14 | | internal struct NavigationNativeSurfaceEdgeEnumerator |
| | | 15 | | { |
| | | 16 | | private readonly NavigationWorldGraph? _graph; |
| | | 17 | | private readonly NavigationMapInstance? _instance; |
| | | 18 | | private readonly int _mapOrdinal; |
| | | 19 | | private readonly VoxelIndex _sourceIndex; |
| | | 20 | | private readonly bool _structural; |
| | | 21 | | private int _directionIndex; |
| | | 22 | | |
| | | 23 | | internal NavigationNativeSurfaceEdgeEnumerator( |
| | | 24 | | NavigationWorldGraph graph, |
| | | 25 | | int mapOrdinal, |
| | | 26 | | NavigationMapInstance instance, |
| | | 27 | | VoxelIndex sourceIndex, |
| | | 28 | | bool structural = false) |
| | | 29 | | { |
| | 541148 | 30 | | _graph = graph; |
| | 541148 | 31 | | _instance = instance; |
| | 541148 | 32 | | _mapOrdinal = mapOrdinal; |
| | 541148 | 33 | | _sourceIndex = sourceIndex; |
| | 541148 | 34 | | _structural = structural; |
| | 541148 | 35 | | _directionIndex = 0; |
| | 541148 | 36 | | Current = default; |
| | 541148 | 37 | | } |
| | | 38 | | |
| | | 39 | | internal NavigationGraphEdge Current { get; private set; } |
| | | 40 | | |
| | | 41 | | internal bool MoveNext() |
| | | 42 | | { |
| | 307903 | 43 | | if (_graph == null || _instance == null) |
| | 1 | 44 | | return false; |
| | | 45 | | |
| | 307902 | 46 | | GridTopologyKind topology = _instance.Map.GridBinding.Configuration.TopologyKind; |
| | 307902 | 47 | | int directionCount = NavigationMap.GetNativeSurfaceDirectionCount(topology); |
| | 819273 | 48 | | while (_directionIndex < directionCount) |
| | | 49 | | { |
| | 669795 | 50 | | int directionIndex = _directionIndex++; |
| | 669795 | 51 | | VoxelIndex offset = NavigationMap.GetNativeSurfaceOffset(topology, directionIndex); |
| | 669795 | 52 | | var targetIndex = new VoxelIndex( |
| | 669795 | 53 | | _sourceIndex.x + offset.x, |
| | 669795 | 54 | | _sourceIndex.y + offset.y, |
| | 669795 | 55 | | _sourceIndex.z + offset.z); |
| | 669795 | 56 | | if (!_instance.Map.GridBinding.IsValidIndex(targetIndex) |
| | 669795 | 57 | | || !_graph.TryGetNodeRef(_mapOrdinal, targetIndex, out NavigationNodeRef target) |
| | 669795 | 58 | | || (_structural |
| | 669795 | 59 | | ? !_instance.TryGetEffectiveCell(target.CellSlot, out _) |
| | 669795 | 60 | | : !_graph.TryGetNodeState(target, out NavigationNodeState state) |
| | 669795 | 61 | | || !state.IsPresent)) |
| | | 62 | | { |
| | | 63 | | continue; |
| | | 64 | | } |
| | | 65 | | |
| | 158424 | 66 | | Current = new NavigationGraphEdge( |
| | 158424 | 67 | | target, |
| | 158424 | 68 | | NavigationGraphEdgeKind.Native, |
| | 158424 | 69 | | _instance.Map.GetNativePortalTemplate(directionIndex), |
| | 158424 | 70 | | directionIndex); |
| | 158424 | 71 | | return true; |
| | | 72 | | } |
| | | 73 | | |
| | 149478 | 74 | | Current = default; |
| | 149478 | 75 | | return false; |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | internal NavigationSurfaceEdgeAdvanceStatus AdvanceOne( |
| | | 79 | | MaintenanceWorkMeter meter, |
| | | 80 | | ref int edgeStepRemaining) |
| | | 81 | | { |
| | 1358751 | 82 | | if (_graph == null || _instance == null) |
| | 2 | 83 | | return NavigationSurfaceEdgeAdvanceStatus.Complete; |
| | 1358749 | 84 | | GridTopologyKind topology = _instance.Map.GridBinding.Configuration.TopologyKind; |
| | 1358749 | 85 | | int directionCount = NavigationMap.GetNativeSurfaceDirectionCount(topology); |
| | 1358749 | 86 | | if (_directionIndex >= directionCount) |
| | | 87 | | { |
| | 184693 | 88 | | Current = default; |
| | 184693 | 89 | | return NavigationSurfaceEdgeAdvanceStatus.Complete; |
| | | 90 | | } |
| | 1174056 | 91 | | if (edgeStepRemaining == 0 || !meter.TryConsumeSurfaceComponentEdges(1)) |
| | 213 | 92 | | return NavigationSurfaceEdgeAdvanceStatus.Blocked; |
| | 1173843 | 93 | | edgeStepRemaining--; |
| | 1173843 | 94 | | int directionIndex = _directionIndex++; |
| | 1173843 | 95 | | VoxelIndex offset = NavigationMap.GetNativeSurfaceOffset(topology, directionIndex); |
| | 1173843 | 96 | | var targetIndex = new VoxelIndex( |
| | 1173843 | 97 | | _sourceIndex.x + offset.x, |
| | 1173843 | 98 | | _sourceIndex.y + offset.y, |
| | 1173843 | 99 | | _sourceIndex.z + offset.z); |
| | 1173843 | 100 | | if (!_instance.Map.GridBinding.IsValidIndex(targetIndex) |
| | 1173843 | 101 | | || !_graph.TryGetNodeRef(_mapOrdinal, targetIndex, out NavigationNodeRef target) |
| | 1173843 | 102 | | || (_structural |
| | 1173843 | 103 | | ? !_instance.TryGetEffectiveCell(target.CellSlot, out _) |
| | 1173843 | 104 | | : !_graph.TryGetNodeState(target, out NavigationNodeState state) |
| | 1173843 | 105 | | || !state.IsPresent)) |
| | | 106 | | { |
| | 543330 | 107 | | return NavigationSurfaceEdgeAdvanceStatus.Pending; |
| | | 108 | | } |
| | 630513 | 109 | | Current = new NavigationGraphEdge( |
| | 630513 | 110 | | target, |
| | 630513 | 111 | | NavigationGraphEdgeKind.Native, |
| | 630513 | 112 | | _instance.Map.GetNativePortalTemplate(directionIndex), |
| | 630513 | 113 | | directionIndex); |
| | 630513 | 114 | | return NavigationSurfaceEdgeAdvanceStatus.Edge; |
| | | 115 | | } |
| | | 116 | | } |