| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationWorkMeter.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System; |
| | | 9 | | |
| | | 10 | | namespace Trailblazer.Pathing; |
| | | 11 | | |
| | | 12 | | /// <summary>Tracks deterministic work consumed by one complete navigation query.</summary> |
| | | 13 | | internal sealed class NavigationWorkMeter |
| | | 14 | | { |
| | | 15 | | private NavigationWorkBudget _budget; |
| | | 16 | | private int _lookupReservationFloor; |
| | | 17 | | private int _guideLookupAndCoveredLimit; |
| | | 18 | | private int _guideEdgeAndConnectionLimit; |
| | | 19 | | private int _guidePortalLimit; |
| | | 20 | | private int _guidePrismLimit; |
| | | 21 | | |
| | 36172 | 22 | | internal NavigationWorkMeter(NavigationWorkBudget budget) => Reset(budget); |
| | | 23 | | |
| | | 24 | | internal int LookupProbes { get; private set; } |
| | | 25 | | |
| | | 26 | | internal int EndpointCandidates { get; private set; } |
| | | 27 | | |
| | | 28 | | internal int ExpandedNodes { get; private set; } |
| | | 29 | | |
| | | 30 | | internal int EvaluatedEdges { get; private set; } |
| | | 31 | | |
| | | 32 | | internal int ConnectionLegs { get; private set; } |
| | | 33 | | |
| | | 34 | | internal int TransitionCandidates { get; private set; } |
| | | 35 | | |
| | | 36 | | internal int TransitionPairs { get; private set; } |
| | | 37 | | |
| | | 38 | | internal int PrimaryVolumeCandidates { get; private set; } |
| | | 39 | | |
| | | 40 | | internal int ShortcutVolumeCandidates { get; private set; } |
| | | 41 | | |
| | | 42 | | internal long VolumeUnionChecks { get; private set; } |
| | | 43 | | |
| | | 44 | | internal int SuccessfulDependencyMerges { get; private set; } |
| | | 45 | | |
| | | 46 | | internal int TraceIntervals { get; private set; } |
| | | 47 | | |
| | | 48 | | internal int CoveredVoxelIntervals { get; private set; } |
| | | 49 | | |
| | | 50 | | internal int SimplificationRays { get; private set; } |
| | | 51 | | |
| | 45128 | 52 | | internal int RemainingLookupProbes => IsGuideSampleBridge |
| | 45128 | 53 | | ? Math.Min( |
| | 45128 | 54 | | _budget.MaxLookupProbes - LookupProbes - _lookupReservationFloor, |
| | 45128 | 55 | | _guideLookupAndCoveredLimit - LookupProbes - CoveredVoxelIntervals) |
| | 45128 | 56 | | : _budget.MaxLookupProbes - LookupProbes - _lookupReservationFloor; |
| | | 57 | | |
| | | 58 | | internal int RemainingEndpointCandidates => |
| | 5378 | 59 | | _budget.MaxEndpointCandidates - EndpointCandidates; |
| | | 60 | | |
| | 6902 | 61 | | internal int RemainingExpandedNodes => _budget.MaxExpandedNodes - ExpandedNodes; |
| | | 62 | | |
| | 38994 | 63 | | internal int RemainingEvaluatedEdges => IsGuideSampleBridge |
| | 38994 | 64 | | ? Math.Min( |
| | 38994 | 65 | | _budget.MaxEvaluatedEdges - EvaluatedEdges, |
| | 38994 | 66 | | _guideEdgeAndConnectionLimit - EvaluatedEdges - ConnectionLegs) |
| | 38994 | 67 | | : _budget.MaxEvaluatedEdges - EvaluatedEdges; |
| | | 68 | | |
| | 650 | 69 | | internal int RemainingConnectionLegs => IsGuideSampleBridge |
| | 650 | 70 | | ? Math.Min( |
| | 650 | 71 | | _budget.MaxConnectionLegs - ConnectionLegs, |
| | 650 | 72 | | _guideEdgeAndConnectionLimit - EvaluatedEdges - ConnectionLegs) |
| | 650 | 73 | | : _budget.MaxConnectionLegs - ConnectionLegs; |
| | | 74 | | |
| | | 75 | | internal int RemainingTransitionCandidates => |
| | 1786 | 76 | | _budget.MaxTransitionCandidates - TransitionCandidates; |
| | | 77 | | |
| | 224 | 78 | | internal int RemainingTransitionPairs => _budget.MaxTransitionPairs - TransitionPairs; |
| | | 79 | | |
| | 2666 | 80 | | internal int RemainingTraceIntervals => _budget.MaxTraceIntervals - TraceIntervals; |
| | | 81 | | |
| | 9900 | 82 | | internal int RemainingCoveredVoxelIntervals => IsGuideSampleBridge |
| | 9900 | 83 | | ? Math.Min( |
| | 9900 | 84 | | _budget.MaxCoveredVoxelIntervals - CoveredVoxelIntervals, |
| | 9900 | 85 | | _guideLookupAndCoveredLimit - LookupProbes - CoveredVoxelIntervals) |
| | 9900 | 86 | | : _budget.MaxCoveredVoxelIntervals - CoveredVoxelIntervals; |
| | | 87 | | |
| | 3432 | 88 | | internal long RemainingGridCandidateWork => IsGuideSampleBridge |
| | 3432 | 89 | | ? (long)_guideLookupAndCoveredLimit - LookupProbes - CoveredVoxelIntervals |
| | 3432 | 90 | | : checked((long)RemainingLookupProbes + RemainingCoveredVoxelIntervals); |
| | | 91 | | |
| | 107669 | 92 | | internal bool IsGuideSampleBridge => _guideLookupAndCoveredLimit >= 0; |
| | | 93 | | |
| | | 94 | | internal int GuidePortalChecks { get; private set; } |
| | | 95 | | |
| | | 96 | | internal int GuidePrismChecks { get; private set; } |
| | | 97 | | |
| | | 98 | | internal int RemainingSimplificationRays => |
| | 326 | 99 | | _budget.MaxSimplificationRays - SimplificationRays; |
| | | 100 | | |
| | | 101 | | internal bool TryConsumeLookupProbes(int count) |
| | | 102 | | { |
| | 32928 | 103 | | if (count < 0 || count > RemainingLookupProbes) |
| | 16 | 104 | | return false; |
| | 32912 | 105 | | LookupProbes += count; |
| | 32912 | 106 | | return true; |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | internal bool TrySetLookupReservationFloor(int count) |
| | | 110 | | { |
| | 117 | 111 | | if (count < 0 || count > _budget.MaxLookupProbes - LookupProbes) |
| | 5 | 112 | | return false; |
| | 112 | 113 | | _lookupReservationFloor = count; |
| | 112 | 114 | | return true; |
| | | 115 | | } |
| | | 116 | | |
| | 694 | 117 | | internal void ReleaseLookupReservationFloor() => _lookupReservationFloor = 0; |
| | | 118 | | |
| | | 119 | | internal bool TryConsumeEndpointCandidates(int count) |
| | | 120 | | { |
| | 3256 | 121 | | if (count < 0 || count > RemainingEndpointCandidates) |
| | 2 | 122 | | return false; |
| | 3254 | 123 | | EndpointCandidates += count; |
| | 3254 | 124 | | return true; |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | internal bool TryConsumeExpandedNodes(int count) |
| | | 128 | | { |
| | 6478 | 129 | | if (count < 0 || count > RemainingExpandedNodes) |
| | 5 | 130 | | return false; |
| | 6473 | 131 | | ExpandedNodes += count; |
| | 6473 | 132 | | return true; |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | internal bool TryConsumeEvaluatedEdges(int count) |
| | | 136 | | { |
| | 38632 | 137 | | if (count < 0 || count > RemainingEvaluatedEdges) |
| | 12 | 138 | | return false; |
| | 38620 | 139 | | EvaluatedEdges += count; |
| | 38620 | 140 | | return true; |
| | | 141 | | } |
| | | 142 | | |
| | | 143 | | internal bool TryConsumeConnectionLegs(int count) |
| | | 144 | | { |
| | 447 | 145 | | if (count < 0 || count > RemainingConnectionLegs) |
| | 6 | 146 | | return false; |
| | 441 | 147 | | ConnectionLegs += count; |
| | 441 | 148 | | return true; |
| | | 149 | | } |
| | | 150 | | |
| | | 151 | | internal bool TryConsumeTransitionCandidates(int count) |
| | | 152 | | { |
| | 1598 | 153 | | if (count < 0 || count > RemainingTransitionCandidates) |
| | 6 | 154 | | return false; |
| | 1592 | 155 | | TransitionCandidates += count; |
| | 1592 | 156 | | return true; |
| | | 157 | | } |
| | | 158 | | |
| | | 159 | | internal bool TryConsumeTransitionPairs(int count) |
| | | 160 | | { |
| | 219 | 161 | | if (count < 0 || count > RemainingTransitionPairs) |
| | 3 | 162 | | return false; |
| | 216 | 163 | | TransitionPairs += count; |
| | 216 | 164 | | return true; |
| | | 165 | | } |
| | | 166 | | |
| | | 167 | | internal void RecordVolumeCandidate(bool isPrimary) |
| | | 168 | | { |
| | 2647 | 169 | | if (isPrimary) |
| | 1440 | 170 | | PrimaryVolumeCandidates++; |
| | | 171 | | else |
| | 1207 | 172 | | ShortcutVolumeCandidates++; |
| | 1207 | 173 | | } |
| | | 174 | | |
| | 2100 | 175 | | internal void RecordVolumeUnionCheck() => VolumeUnionChecks++; |
| | | 176 | | |
| | | 177 | | internal void RecordSuccessfulDependencyMerge() => |
| | 26 | 178 | | SuccessfulDependencyMerges++; |
| | | 179 | | |
| | | 180 | | internal bool TryConsumeTraceIntervals(int count) |
| | | 181 | | { |
| | 1336 | 182 | | if (count < 0 || count > RemainingTraceIntervals) |
| | 3 | 183 | | return false; |
| | 1333 | 184 | | TraceIntervals += count; |
| | 1333 | 185 | | return true; |
| | | 186 | | } |
| | | 187 | | |
| | | 188 | | internal bool TryConsumeCoveredVoxelIntervals(int count) |
| | | 189 | | { |
| | 3436 | 190 | | if (count < 0 || count > RemainingCoveredVoxelIntervals) |
| | 3 | 191 | | return false; |
| | 3433 | 192 | | CoveredVoxelIntervals += count; |
| | 3433 | 193 | | return true; |
| | | 194 | | } |
| | | 195 | | |
| | | 196 | | internal bool TryConsumeSimplificationRays(int count) |
| | | 197 | | { |
| | 38 | 198 | | if (count < 0 || count > RemainingSimplificationRays) |
| | 3 | 199 | | return false; |
| | 35 | 200 | | SimplificationRays += count; |
| | 35 | 201 | | return true; |
| | | 202 | | } |
| | | 203 | | |
| | | 204 | | internal bool TryConsumeGuidePortalChecks(int count) |
| | | 205 | | { |
| | 87 | 206 | | if (!IsGuideSampleBridge |
| | 87 | 207 | | || count < 0 |
| | 87 | 208 | | || count > _guidePortalLimit - GuidePortalChecks) |
| | | 209 | | { |
| | 3 | 210 | | return false; |
| | | 211 | | } |
| | 84 | 212 | | GuidePortalChecks += count; |
| | 84 | 213 | | return true; |
| | | 214 | | } |
| | | 215 | | |
| | | 216 | | internal bool TryConsumeGuidePrismChecks(int count) |
| | | 217 | | { |
| | 14 | 218 | | if (!IsGuideSampleBridge |
| | 14 | 219 | | || count < 0 |
| | 14 | 220 | | || count > _guidePrismLimit - GuidePrismChecks) |
| | | 221 | | { |
| | 3 | 222 | | return false; |
| | | 223 | | } |
| | 11 | 224 | | GuidePrismChecks += count; |
| | 11 | 225 | | return true; |
| | | 226 | | } |
| | | 227 | | |
| | | 228 | | internal void ResetForGuideSample( |
| | | 229 | | int lookupAndCoveredLimit, |
| | | 230 | | int edgeAndConnectionLimit, |
| | | 231 | | int portalLimit, |
| | | 232 | | int prismLimit, |
| | | 233 | | int traceIntervalLimit) |
| | | 234 | | { |
| | 124 | 235 | | var budget = new NavigationWorkBudget( |
| | 124 | 236 | | lookupAndCoveredLimit, |
| | 124 | 237 | | maxEndpointCandidates: 0, |
| | 124 | 238 | | maxExpandedNodes: 0, |
| | 124 | 239 | | edgeAndConnectionLimit, |
| | 124 | 240 | | edgeAndConnectionLimit, |
| | 124 | 241 | | maxTransitionCandidates: 0, |
| | 124 | 242 | | maxTransitionPairs: 0, |
| | 124 | 243 | | maxStagedLegAttempts: 0, |
| | 124 | 244 | | traceIntervalLimit, |
| | 124 | 245 | | lookupAndCoveredLimit, |
| | 124 | 246 | | maxSimplificationRays: 0); |
| | 124 | 247 | | Reset(budget); |
| | 124 | 248 | | _guideLookupAndCoveredLimit = lookupAndCoveredLimit; |
| | 124 | 249 | | _guideEdgeAndConnectionLimit = edgeAndConnectionLimit; |
| | 124 | 250 | | _guidePortalLimit = portalLimit; |
| | 124 | 251 | | _guidePrismLimit = prismLimit; |
| | 124 | 252 | | } |
| | | 253 | | |
| | | 254 | | internal void Reset(NavigationWorkBudget budget) |
| | | 255 | | { |
| | 20141 | 256 | | _budget = budget; |
| | 20141 | 257 | | LookupProbes = 0; |
| | 20141 | 258 | | EndpointCandidates = 0; |
| | 20141 | 259 | | ExpandedNodes = 0; |
| | 20141 | 260 | | EvaluatedEdges = 0; |
| | 20141 | 261 | | ConnectionLegs = 0; |
| | 20141 | 262 | | TransitionCandidates = 0; |
| | 20141 | 263 | | TransitionPairs = 0; |
| | 20141 | 264 | | PrimaryVolumeCandidates = 0; |
| | 20141 | 265 | | ShortcutVolumeCandidates = 0; |
| | 20141 | 266 | | VolumeUnionChecks = 0; |
| | 20141 | 267 | | SuccessfulDependencyMerges = 0; |
| | 20141 | 268 | | TraceIntervals = 0; |
| | 20141 | 269 | | CoveredVoxelIntervals = 0; |
| | 20141 | 270 | | SimplificationRays = 0; |
| | 20141 | 271 | | GuidePortalChecks = 0; |
| | 20141 | 272 | | GuidePrismChecks = 0; |
| | 20141 | 273 | | _lookupReservationFloor = 0; |
| | 20141 | 274 | | _guideLookupAndCoveredLimit = -1; |
| | 20141 | 275 | | _guideEdgeAndConnectionLimit = -1; |
| | 20141 | 276 | | _guidePortalLimit = 0; |
| | 20141 | 277 | | _guidePrismLimit = 0; |
| | 20141 | 278 | | } |
| | | 279 | | } |