| | | 1 | | //======================================================================= |
| | | 2 | | // TraversalEvaluator.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 FixedMathSharp; |
| | | 9 | | using GridForge.Grids.Topology; |
| | | 10 | | |
| | | 11 | | namespace Trailblazer.Pathing; |
| | | 12 | | |
| | | 13 | | /// <summary>Reports the allocation-free result of one traversal evaluation.</summary> |
| | | 14 | | internal enum TraversalEvaluationStatus : byte |
| | | 15 | | { |
| | | 16 | | Passable = 0, |
| | | 17 | | Impassable = 1, |
| | | 18 | | CostOverflow = 2 |
| | | 19 | | } |
| | | 20 | | |
| | | 21 | | /// <summary>Reports progress through one bounded explicit-connection evaluation.</summary> |
| | | 22 | | internal enum TraversalExplicitEdgeStatus : byte |
| | | 23 | | { |
| | | 24 | | Pending = 0, |
| | | 25 | | Passable = 1, |
| | | 26 | | Impassable = 2, |
| | | 27 | | CostOverflow = 3 |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary>Returns one evaluated edge's exact cost and resolved portal evidence.</summary> |
| | | 31 | | internal readonly struct TraversalEdgeEvidence |
| | | 32 | | { |
| | | 33 | | internal TraversalEdgeEvidence(NavigationNodeRef dependencyNode) |
| | | 34 | | { |
| | | 35 | | this = default; |
| | | 36 | | DependencyNode = dependencyNode; |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | internal TraversalEdgeEvidence( |
| | | 40 | | Fixed64 cost, |
| | | 41 | | GridNavigationPortal portal, |
| | | 42 | | Vector3d sourcePortalAnchor, |
| | | 43 | | Vector3d targetPortalAnchor, |
| | | 44 | | NavigationCellAddress sourceAddress, |
| | | 45 | | NavigationCellAddress targetAddress, |
| | | 46 | | Vector3d sourceFootAnchor, |
| | | 47 | | Vector3d targetFootAnchor, |
| | | 48 | | in GridCellPrism sourcePrism, |
| | | 49 | | in GridCellPrism targetPrism, |
| | | 50 | | NavigationNodeRef dependencyNode) |
| | | 51 | | { |
| | | 52 | | Cost = cost; |
| | | 53 | | Portal = portal; |
| | | 54 | | SourcePortalAnchor = sourcePortalAnchor; |
| | | 55 | | TargetPortalAnchor = targetPortalAnchor; |
| | | 56 | | SourceAddress = sourceAddress; |
| | | 57 | | TargetAddress = targetAddress; |
| | | 58 | | SourceFootAnchor = sourceFootAnchor; |
| | | 59 | | TargetFootAnchor = targetFootAnchor; |
| | | 60 | | SourcePrism = sourcePrism; |
| | | 61 | | TargetPrism = targetPrism; |
| | | 62 | | DependencyNode = dependencyNode; |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | internal Fixed64 Cost { get; } |
| | | 66 | | |
| | | 67 | | internal GridNavigationPortal Portal { get; } |
| | | 68 | | |
| | | 69 | | internal Vector3d SourcePortalAnchor { get; } |
| | | 70 | | |
| | | 71 | | internal Vector3d TargetPortalAnchor { get; } |
| | | 72 | | |
| | | 73 | | internal NavigationCellAddress SourceAddress { get; } |
| | | 74 | | |
| | | 75 | | internal NavigationCellAddress TargetAddress { get; } |
| | | 76 | | |
| | | 77 | | internal Vector3d SourceFootAnchor { get; } |
| | | 78 | | |
| | | 79 | | internal Vector3d TargetFootAnchor { get; } |
| | | 80 | | |
| | | 81 | | internal GridCellPrism SourcePrism { get; } |
| | | 82 | | |
| | | 83 | | internal GridCellPrism TargetPrism { get; } |
| | | 84 | | |
| | | 85 | | internal NavigationNodeRef DependencyNode { get; } |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary>Retains scalar state while explicit corridor legs are evaluated.</summary> |
| | | 89 | | internal struct TraversalExplicitEdgeWork |
| | | 90 | | { |
| | | 91 | | internal NavigationExplicitConnectionRecord Record; |
| | | 92 | | internal NavigationNodeRef Target; |
| | | 93 | | internal NavigationPagedSequence<GridNavigationPortal>.Enumerator Portals; |
| | | 94 | | internal NavigationCellAddress PreviousAddress; |
| | | 95 | | internal GridCellPrism PreviousPrism; |
| | | 96 | | internal Vector3d SourceFootAnchor; |
| | | 97 | | internal Vector3d TargetFootAnchor; |
| | | 98 | | internal Vector3d PreviousFootAnchor; |
| | | 99 | | internal Fixed64 TargetEnterCost; |
| | | 100 | | internal Fixed64 TargetAreaEnterCost; |
| | | 101 | | internal int WitnessIndex; |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary>Evaluates nodes and native edges against one resolved immutable query profile.</summary> |
| | | 105 | | internal readonly struct TraversalEvaluator |
| | | 106 | | { |
| | | 107 | | private readonly NavigationWorldGraph _graph; |
| | | 108 | | private readonly NavigationAgentProfile _profile; |
| | | 109 | | private readonly NavigationAreaPolicy _areaPolicy; |
| | | 110 | | private readonly TraversalMedia _medium; |
| | | 111 | | private readonly TraversalMedium _exactMedium; |
| | | 112 | | |
| | | 113 | | internal TraversalEvaluator( |
| | | 114 | | NavigationWorldGraph graph, |
| | | 115 | | NavigationAgentProfile profile, |
| | | 116 | | NavigationAreaPolicy areaPolicy, |
| | | 117 | | TraversalMedium medium) |
| | | 118 | | { |
| | 22871 | 119 | | SwiftThrowHelper.ThrowIfNull(graph, nameof(graph)); |
| | 22871 | 120 | | SwiftThrowHelper.ThrowIfNull(areaPolicy, nameof(areaPolicy)); |
| | 22871 | 121 | | profile.Validate(nameof(profile)); |
| | 22871 | 122 | | TraversalMedia resolvedMedium = NavigationCell.ToMedia(medium); |
| | 22871 | 123 | | SwiftThrowHelper.ThrowIfArgument( |
| | 22871 | 124 | | resolvedMedium == TraversalMedia.None, |
| | 22871 | 125 | | nameof(medium), |
| | 22871 | 126 | | "Traversal medium must resolve to one exact authored medium."); |
| | | 127 | | |
| | 22871 | 128 | | _graph = graph; |
| | 22871 | 129 | | _profile = profile; |
| | 22871 | 130 | | _areaPolicy = areaPolicy; |
| | 22871 | 131 | | _medium = resolvedMedium; |
| | 22871 | 132 | | _exactMedium = medium; |
| | 22871 | 133 | | } |
| | | 134 | | |
| | 78755 | 135 | | internal NavigationAgentProfile Profile => _profile; |
| | | 136 | | |
| | 2885 | 137 | | internal NavigationAreaPolicy AreaPolicy => _areaPolicy; |
| | | 138 | | |
| | 4773 | 139 | | internal NavigationWorldGraph Graph => _graph; |
| | | 140 | | |
| | | 141 | | internal bool TryGetPassableNodeState( |
| | | 142 | | NavigationNodeRef node, |
| | | 143 | | out NavigationNodeState state) => |
| | 15417 | 144 | | TryGetPassableNode(node, out state, out _); |
| | | 145 | | |
| | | 146 | | internal TraversalEvaluationStatus EvaluateEdge( |
| | | 147 | | NavigationNodeRef source, |
| | | 148 | | in NavigationGraphEdge edge, |
| | | 149 | | out TraversalEdgeEvidence evidence) |
| | | 150 | | { |
| | 102895 | 151 | | return edge.Kind switch |
| | 102895 | 152 | | { |
| | 20029 | 153 | | NavigationGraphEdgeKind.Explicit => EvaluateExplicitEdge( |
| | 20029 | 154 | | source, |
| | 20029 | 155 | | edge, |
| | 20029 | 156 | | out evidence), |
| | 40057 | 157 | | NavigationGraphEdgeKind.Seam => EvaluateAutomaticSeam( |
| | 40057 | 158 | | source, |
| | 40057 | 159 | | edge, |
| | 40057 | 160 | | out evidence), |
| | 42809 | 161 | | _ => EvaluateNative(source, edge, out evidence) |
| | 102895 | 162 | | }; |
| | | 163 | | } |
| | | 164 | | |
| | | 165 | | internal TraversalExplicitEdgeStatus BeginExplicitEdge( |
| | | 166 | | NavigationNodeRef source, |
| | | 167 | | in NavigationGraphEdge edge, |
| | | 168 | | out TraversalExplicitEdgeWork work) |
| | | 169 | | { |
| | 20211 | 170 | | work = default; |
| | 20211 | 171 | | NavigationExplicitConnectionRecord record = edge.ExplicitConnection; |
| | 20211 | 172 | | bool hasSourceAddress = _graph.TryGetNodeAddress( |
| | 20211 | 173 | | source, |
| | 20211 | 174 | | out NavigationCellAddress sourceAddress); |
| | 20211 | 175 | | bool hasTargetAddress = _graph.TryGetNodeAddress( |
| | 20211 | 176 | | edge.Target, |
| | 20211 | 177 | | out NavigationCellAddress targetAddress); |
| | 20211 | 178 | | bool hasSourcePrism = _graph.TryGetSeamPrism( |
| | 20211 | 179 | | sourceAddress, |
| | 20211 | 180 | | out GridCellPrism sourcePrism); |
| | | 181 | | System.Diagnostics.Debug.Assert( |
| | | 182 | | record != null |
| | | 183 | | && record.IsActive |
| | | 184 | | && edge.Kind == NavigationGraphEdgeKind.Explicit |
| | | 185 | | && hasSourceAddress |
| | | 186 | | && hasTargetAddress |
| | | 187 | | && record.NavigationPortals.Count == record.Definition.Witnesses.Count + 1 |
| | | 188 | | && hasSourcePrism |
| | | 189 | | && sourceAddress.Equals(record.Source) |
| | | 190 | | && targetAddress.Equals(record.Destination)); |
| | 20211 | 191 | | if (!TryGetPassableNode(source, out NavigationNodeState sourceState, out _) |
| | 20211 | 192 | | || !TryGetPassableNode( |
| | 20211 | 193 | | edge.Target, |
| | 20211 | 194 | | out NavigationNodeState targetState, |
| | 20211 | 195 | | out NavigationAreaRule targetRule)) |
| | | 196 | | { |
| | 1 | 197 | | return TraversalExplicitEdgeStatus.Impassable; |
| | | 198 | | } |
| | | 199 | | |
| | 20210 | 200 | | KinematicBodyShape shape = _profile.Shape; |
| | 20210 | 201 | | NavigationConnection connection = record.Definition; |
| | 20210 | 202 | | if (shape.Radius > connection.PortalRadiusClearance |
| | 20210 | 203 | | || shape.Height > connection.PortalHeightClearance) |
| | | 204 | | { |
| | 7 | 205 | | return TraversalExplicitEdgeStatus.Impassable; |
| | | 206 | | } |
| | | 207 | | |
| | 20203 | 208 | | work = new TraversalExplicitEdgeWork |
| | 20203 | 209 | | { |
| | 20203 | 210 | | Record = record, |
| | 20203 | 211 | | Target = edge.Target, |
| | 20203 | 212 | | Portals = record.NavigationPortals.GetEnumerator(), |
| | 20203 | 213 | | PreviousAddress = sourceAddress, |
| | 20203 | 214 | | PreviousPrism = sourcePrism, |
| | 20203 | 215 | | SourceFootAnchor = sourceState.FootAnchor, |
| | 20203 | 216 | | TargetFootAnchor = targetState.FootAnchor, |
| | 20203 | 217 | | PreviousFootAnchor = sourceState.FootAnchor, |
| | 20203 | 218 | | TargetEnterCost = targetState.Cell.EnterCost, |
| | 20203 | 219 | | TargetAreaEnterCost = targetRule.AdditionalEnterCost |
| | 20203 | 220 | | }; |
| | 20203 | 221 | | return TraversalExplicitEdgeStatus.Pending; |
| | | 222 | | } |
| | | 223 | | |
| | | 224 | | internal TraversalExplicitEdgeStatus AdvanceExplicitEdge( |
| | | 225 | | ref TraversalExplicitEdgeWork work, |
| | | 226 | | out TraversalEdgeEvidence evidence) |
| | | 227 | | { |
| | 20557 | 228 | | evidence = default; |
| | 20557 | 229 | | NavigationExplicitConnectionRecord record = work.Record; |
| | 20557 | 230 | | NavigationConnection connection = record.Definition; |
| | 20557 | 231 | | bool final = work.WitnessIndex >= connection.Witnesses.Count; |
| | 20557 | 232 | | NavigationCellAddress nextAddress = final |
| | 20557 | 233 | | ? record.Destination |
| | 20557 | 234 | | : connection.Witnesses[work.WitnessIndex]; |
| | | 235 | | NavigationNodeRef dependencyNode; |
| | | 236 | | Vector3d nextFootAnchor; |
| | 20557 | 237 | | if (final) |
| | | 238 | | { |
| | 20186 | 239 | | dependencyNode = work.Target; |
| | 20186 | 240 | | evidence = new TraversalEdgeEvidence(dependencyNode); |
| | 20186 | 241 | | nextFootAnchor = work.TargetFootAnchor; |
| | | 242 | | } |
| | | 243 | | else |
| | | 244 | | { |
| | 371 | 245 | | bool hasDependencyNode = _graph.TryGetNodeRef(nextAddress, out dependencyNode); |
| | | 246 | | System.Diagnostics.Debug.Assert(hasDependencyNode); |
| | 371 | 247 | | evidence = new TraversalEdgeEvidence(dependencyNode); |
| | 371 | 248 | | if (!TryGetPassableNode( |
| | 371 | 249 | | dependencyNode, |
| | 371 | 250 | | out NavigationNodeState nextState, |
| | 371 | 251 | | out _)) |
| | | 252 | | { |
| | 5 | 253 | | return TraversalExplicitEdgeStatus.Impassable; |
| | | 254 | | } |
| | 366 | 255 | | nextFootAnchor = nextState.FootAnchor; |
| | | 256 | | } |
| | | 257 | | |
| | 20552 | 258 | | bool hasNextPrism = _graph.TryGetSeamPrism(nextAddress, out GridCellPrism nextPrism); |
| | 20552 | 259 | | bool hasPortal = work.Portals.MoveNext(); |
| | | 260 | | System.Diagnostics.Debug.Assert(hasNextPrism && hasPortal); |
| | 20552 | 261 | | GridNavigationPortal portal = work.Portals.Current; |
| | 20552 | 262 | | KinematicBodyShape shape = _profile.Shape; |
| | | 263 | | System.Diagnostics.Debug.Assert(portal.IsValid); |
| | 20552 | 264 | | bool resolvedProfile = portal.TryResolveProfile( |
| | 20552 | 265 | | shape.Radius, |
| | 20552 | 266 | | shape.Height, |
| | 20552 | 267 | | out Vector3d sourcePortalAnchor, |
| | 20552 | 268 | | out Vector3d targetPortalAnchor); |
| | | 269 | | System.Diagnostics.Debug.Assert(resolvedProfile); |
| | | 270 | | System.Diagnostics.Debug.Assert( |
| | | 271 | | portal.FaceKind != VoxelContactFaceKind.Horizontal |
| | | 272 | | || (GridCellGeometry.TryGetNavigationPortalTraversalParameters( |
| | | 273 | | work.PreviousPrism, |
| | | 274 | | nextPrism, |
| | | 275 | | portal, |
| | | 276 | | sourcePortalAnchor, |
| | | 277 | | targetPortalAnchor, |
| | | 278 | | shape.Radius, |
| | | 279 | | shape.Height, |
| | | 280 | | out Fixed64 sourceParameter, |
| | | 281 | | out Fixed64 targetParameter) |
| | | 282 | | && sourceParameter == Fixed64.Zero |
| | | 283 | | && targetParameter == Fixed64.One), |
| | | 284 | | "published explicit corridors retain the exact validated profile crossing"); |
| | | 285 | | |
| | 20552 | 286 | | TraversalEvaluationStatus vertical = EvaluateVerticalDelta( |
| | 20552 | 287 | | work.PreviousFootAnchor, |
| | 20552 | 288 | | nextFootAnchor); |
| | 20552 | 289 | | if (vertical != TraversalEvaluationStatus.Passable) |
| | 2 | 290 | | return TraversalExplicitEdgeStatus.Impassable; |
| | | 291 | | |
| | 20550 | 292 | | Fixed64 cost = Fixed64.Zero; |
| | 20550 | 293 | | if (final |
| | 20550 | 294 | | && !TryGetExplicitConnectionCost( |
| | 20550 | 295 | | work.SourceFootAnchor, |
| | 20550 | 296 | | connection.EntryAnchor, |
| | 20550 | 297 | | record.CorridorCost, |
| | 20550 | 298 | | connection.ExitAnchor, |
| | 20550 | 299 | | work.TargetFootAnchor, |
| | 20550 | 300 | | connection.AdditionalCost, |
| | 20550 | 301 | | work.TargetEnterCost, |
| | 20550 | 302 | | work.TargetAreaEnterCost, |
| | 20550 | 303 | | out cost)) |
| | | 304 | | { |
| | 3 | 305 | | return TraversalExplicitEdgeStatus.CostOverflow; |
| | | 306 | | } |
| | | 307 | | |
| | 20547 | 308 | | evidence = new TraversalEdgeEvidence( |
| | 20547 | 309 | | cost, |
| | 20547 | 310 | | portal, |
| | 20547 | 311 | | sourcePortalAnchor, |
| | 20547 | 312 | | targetPortalAnchor, |
| | 20547 | 313 | | work.PreviousAddress, |
| | 20547 | 314 | | nextAddress, |
| | 20547 | 315 | | work.PreviousFootAnchor, |
| | 20547 | 316 | | nextFootAnchor, |
| | 20547 | 317 | | work.PreviousPrism, |
| | 20547 | 318 | | nextPrism, |
| | 20547 | 319 | | dependencyNode); |
| | 20547 | 320 | | work.PreviousAddress = nextAddress; |
| | 20547 | 321 | | work.PreviousPrism = nextPrism; |
| | 20547 | 322 | | work.PreviousFootAnchor = nextFootAnchor; |
| | 20547 | 323 | | if (!final) |
| | 364 | 324 | | work.WitnessIndex++; |
| | 20547 | 325 | | return final |
| | 20547 | 326 | | ? TraversalExplicitEdgeStatus.Passable |
| | 20547 | 327 | | : TraversalExplicitEdgeStatus.Pending; |
| | | 328 | | } |
| | | 329 | | |
| | | 330 | | private TraversalEvaluationStatus EvaluateNative( |
| | | 331 | | NavigationNodeRef source, |
| | | 332 | | in NavigationGraphEdge edge, |
| | | 333 | | out TraversalEdgeEvidence evidence) |
| | | 334 | | { |
| | 42809 | 335 | | evidence = default; |
| | 42809 | 336 | | if (!TryGetPassableNode(source, out NavigationNodeState sourceState, out _) |
| | 42809 | 337 | | || !TryGetPassableNode( |
| | 42809 | 338 | | edge.Target, |
| | 42809 | 339 | | out NavigationNodeState targetState, |
| | 42809 | 340 | | out NavigationAreaRule targetRule)) |
| | | 341 | | { |
| | 34 | 342 | | return TraversalEvaluationStatus.Impassable; |
| | | 343 | | } |
| | | 344 | | |
| | 42775 | 345 | | KinematicBodyShape shape = _profile.Shape; |
| | 42775 | 346 | | GridNavigationPortal template = edge.NativePortal; |
| | | 347 | | System.Diagnostics.Debug.Assert(template.IsValid); |
| | 42775 | 348 | | if (shape.Radius > template.MaximumHorizontalRadius |
| | 42775 | 349 | | || shape.Height > template.MaximumBodyHeight) |
| | | 350 | | { |
| | 3 | 351 | | return TraversalEvaluationStatus.Impassable; |
| | | 352 | | } |
| | | 353 | | |
| | 42772 | 354 | | bool hasSourceAddress = _graph.TryGetNodeAddress( |
| | 42772 | 355 | | source, |
| | 42772 | 356 | | out NavigationCellAddress sourceAddress); |
| | 42772 | 357 | | bool hasTargetAddress = _graph.TryGetNodeAddress( |
| | 42772 | 358 | | edge.Target, |
| | 42772 | 359 | | out NavigationCellAddress targetAddress); |
| | 42772 | 360 | | bool hasSourcePrism = _graph.TryGetSeamPrism( |
| | 42772 | 361 | | sourceAddress, |
| | 42772 | 362 | | out GridCellPrism sourcePrism); |
| | 42772 | 363 | | bool hasTargetPrism = _graph.TryGetSeamPrism( |
| | 42772 | 364 | | targetAddress, |
| | 42772 | 365 | | out GridCellPrism targetPrism); |
| | | 366 | | System.Diagnostics.Debug.Assert( |
| | | 367 | | hasSourceAddress && hasTargetAddress && hasSourcePrism && hasTargetPrism); |
| | 42772 | 368 | | bool translated = template.TryTranslate( |
| | 42772 | 369 | | sourceState.Center, |
| | 42772 | 370 | | out GridNavigationPortal retainedPortal); |
| | | 371 | | System.Diagnostics.Debug.Assert(translated); |
| | 42772 | 372 | | bool createdPortal = GridCellGeometry.TryCreateNavigationPortal( |
| | 42772 | 373 | | sourcePrism, |
| | 42772 | 374 | | targetPrism, |
| | 42772 | 375 | | out GridNavigationPortal portal); |
| | | 376 | | System.Diagnostics.Debug.Assert(createdPortal && retainedPortal.IsValid); |
| | 42772 | 377 | | bool resolvedProfile = portal.TryResolveProfile( |
| | 42772 | 378 | | shape.Radius, |
| | 42772 | 379 | | shape.Height, |
| | 42772 | 380 | | out Vector3d sourcePortalFoot, |
| | 42772 | 381 | | out Vector3d targetPortalFoot); |
| | | 382 | | System.Diagnostics.Debug.Assert(resolvedProfile); |
| | | 383 | | |
| | 42772 | 384 | | if (!TryGetPortalTraversalCost( |
| | 42772 | 385 | | useCeilingDistance: true, |
| | 42772 | 386 | | sourceState.FootAnchor, |
| | 42772 | 387 | | sourcePortalFoot, |
| | 42772 | 388 | | targetPortalFoot, |
| | 42772 | 389 | | targetState.FootAnchor, |
| | 42772 | 390 | | targetState.Cell.EnterCost, |
| | 42772 | 391 | | targetRule.AdditionalEnterCost, |
| | 42772 | 392 | | out Fixed64 total)) |
| | | 393 | | { |
| | 5 | 394 | | return TraversalEvaluationStatus.CostOverflow; |
| | | 395 | | } |
| | | 396 | | |
| | 42767 | 397 | | evidence = new TraversalEdgeEvidence( |
| | 42767 | 398 | | total, |
| | 42767 | 399 | | portal, |
| | 42767 | 400 | | sourcePortalFoot, |
| | 42767 | 401 | | targetPortalFoot, |
| | 42767 | 402 | | sourceAddress, |
| | 42767 | 403 | | targetAddress, |
| | 42767 | 404 | | sourceState.FootAnchor, |
| | 42767 | 405 | | targetState.FootAnchor, |
| | 42767 | 406 | | sourcePrism, |
| | 42767 | 407 | | targetPrism, |
| | 42767 | 408 | | default); |
| | 42767 | 409 | | return TraversalEvaluationStatus.Passable; |
| | | 410 | | } |
| | | 411 | | |
| | | 412 | | private TraversalEvaluationStatus EvaluateExplicitEdge( |
| | | 413 | | NavigationNodeRef source, |
| | | 414 | | in NavigationGraphEdge edge, |
| | | 415 | | out TraversalEdgeEvidence evidence) |
| | | 416 | | { |
| | 20029 | 417 | | evidence = default; |
| | 20029 | 418 | | Fixed64 cost = Fixed64.Zero; |
| | 20029 | 419 | | TraversalExplicitEdgeStatus status = BeginExplicitEdge(source, edge, out TraversalExplicitEdgeWork work); |
| | 40128 | 420 | | while (status == TraversalExplicitEdgeStatus.Pending) |
| | | 421 | | { |
| | 20099 | 422 | | status = AdvanceExplicitEdge(ref work, out TraversalEdgeEvidence step); |
| | 20099 | 423 | | cost = step.Cost; |
| | | 424 | | } |
| | 20029 | 425 | | TraversalEvaluationStatus result = status switch |
| | 20029 | 426 | | { |
| | 20022 | 427 | | TraversalExplicitEdgeStatus.Passable => TraversalEvaluationStatus.Passable, |
| | 1 | 428 | | TraversalExplicitEdgeStatus.CostOverflow => TraversalEvaluationStatus.CostOverflow, |
| | 6 | 429 | | _ => TraversalEvaluationStatus.Impassable |
| | 20029 | 430 | | }; |
| | 20029 | 431 | | if (result == TraversalEvaluationStatus.Passable) |
| | 20022 | 432 | | evidence = new TraversalEdgeEvidence( |
| | 20022 | 433 | | cost, |
| | 20022 | 434 | | default, |
| | 20022 | 435 | | default, |
| | 20022 | 436 | | default, |
| | 20022 | 437 | | default, |
| | 20022 | 438 | | default, |
| | 20022 | 439 | | default, |
| | 20022 | 440 | | default, |
| | 20022 | 441 | | default, |
| | 20022 | 442 | | default, |
| | 20022 | 443 | | default); |
| | 20029 | 444 | | return result; |
| | | 445 | | } |
| | | 446 | | |
| | | 447 | | private TraversalEvaluationStatus EvaluateAutomaticSeam( |
| | | 448 | | NavigationNodeRef source, |
| | | 449 | | in NavigationGraphEdge edge, |
| | | 450 | | out TraversalEdgeEvidence evidence) |
| | | 451 | | { |
| | 40057 | 452 | | evidence = default; |
| | 40057 | 453 | | NavigationAutomaticSeamRef seam = edge.AutomaticSeam; |
| | 40057 | 454 | | bool hasSourceAddress = _graph.TryGetNodeAddress( |
| | 40057 | 455 | | source, |
| | 40057 | 456 | | out NavigationCellAddress sourceAddress); |
| | 40057 | 457 | | bool hasTargetAddress = _graph.TryGetNodeAddress( |
| | 40057 | 458 | | edge.Target, |
| | 40057 | 459 | | out NavigationCellAddress targetAddress); |
| | | 460 | | System.Diagnostics.Debug.Assert( |
| | | 461 | | edge.Kind == NavigationGraphEdgeKind.Seam |
| | | 462 | | && seam.Pair != null |
| | | 463 | | && _graph.AutomaticSeams.IsActive(seam) |
| | | 464 | | && hasSourceAddress |
| | | 465 | | && sourceAddress.Equals(seam.Source) |
| | | 466 | | && hasTargetAddress |
| | | 467 | | && targetAddress.Equals(seam.Destination)); |
| | 40057 | 468 | | if (!TryGetPassableNode(source, out NavigationNodeState sourceState, out _) |
| | 40057 | 469 | | || !TryGetPassableNode( |
| | 40057 | 470 | | edge.Target, |
| | 40057 | 471 | | out NavigationNodeState targetState, |
| | 40057 | 472 | | out NavigationAreaRule targetRule)) |
| | | 473 | | { |
| | 2 | 474 | | return TraversalEvaluationStatus.Impassable; |
| | | 475 | | } |
| | | 476 | | |
| | 40055 | 477 | | KinematicBodyShape shape = _profile.Shape; |
| | 40055 | 478 | | GridNavigationPortal retainedPortal = seam.Portal; |
| | | 479 | | System.Diagnostics.Debug.Assert(retainedPortal.IsValid); |
| | 40055 | 480 | | if (shape.Radius > retainedPortal.MaximumHorizontalRadius |
| | 40055 | 481 | | || shape.Height > retainedPortal.MaximumBodyHeight) |
| | | 482 | | { |
| | 1 | 483 | | return TraversalEvaluationStatus.Impassable; |
| | | 484 | | } |
| | 40054 | 485 | | bool hasSourcePrism = _graph.TryGetSeamPrism( |
| | 40054 | 486 | | sourceAddress, |
| | 40054 | 487 | | out GridCellPrism sourcePrism); |
| | 40054 | 488 | | bool hasTargetPrism = _graph.TryGetSeamPrism( |
| | 40054 | 489 | | targetAddress, |
| | 40054 | 490 | | out GridCellPrism targetPrism); |
| | 40054 | 491 | | bool createdPortal = GridCellGeometry.TryCreateNavigationPortal( |
| | 40054 | 492 | | sourcePrism, |
| | 40054 | 493 | | targetPrism, |
| | 40054 | 494 | | out GridNavigationPortal portal); |
| | | 495 | | System.Diagnostics.Debug.Assert( |
| | | 496 | | hasSourcePrism |
| | | 497 | | && hasTargetPrism |
| | | 498 | | && createdPortal |
| | | 499 | | && retainedPortal.IsValid); |
| | 40054 | 500 | | bool resolvedProfile = portal.TryResolveProfile( |
| | 40054 | 501 | | shape.Radius, |
| | 40054 | 502 | | shape.Height, |
| | 40054 | 503 | | out Vector3d sourcePortalFoot, |
| | 40054 | 504 | | out Vector3d targetPortalFoot); |
| | | 505 | | System.Diagnostics.Debug.Assert(resolvedProfile); |
| | 40054 | 506 | | TraversalEvaluationStatus vertical = EvaluateVerticalDelta( |
| | 40054 | 507 | | sourceState.FootAnchor, |
| | 40054 | 508 | | targetState.FootAnchor); |
| | 40054 | 509 | | if (vertical != TraversalEvaluationStatus.Passable) |
| | 1 | 510 | | return vertical; |
| | 40053 | 511 | | if (!TryGetPortalTraversalCost( |
| | 40053 | 512 | | portal.FaceKind == VoxelContactFaceKind.Vertical, |
| | 40053 | 513 | | sourceState.FootAnchor, |
| | 40053 | 514 | | sourcePortalFoot, |
| | 40053 | 515 | | targetPortalFoot, |
| | 40053 | 516 | | targetState.FootAnchor, |
| | 40053 | 517 | | targetState.Cell.EnterCost, |
| | 40053 | 518 | | targetRule.AdditionalEnterCost, |
| | 40053 | 519 | | out Fixed64 total)) |
| | | 520 | | { |
| | 1 | 521 | | return TraversalEvaluationStatus.CostOverflow; |
| | | 522 | | } |
| | | 523 | | |
| | 40052 | 524 | | evidence = new TraversalEdgeEvidence( |
| | 40052 | 525 | | total, |
| | 40052 | 526 | | portal, |
| | 40052 | 527 | | sourcePortalFoot, |
| | 40052 | 528 | | targetPortalFoot, |
| | 40052 | 529 | | sourceAddress, |
| | 40052 | 530 | | targetAddress, |
| | 40052 | 531 | | sourceState.FootAnchor, |
| | 40052 | 532 | | targetState.FootAnchor, |
| | 40052 | 533 | | sourcePrism, |
| | 40052 | 534 | | targetPrism, |
| | 40052 | 535 | | default); |
| | 40052 | 536 | | return TraversalEvaluationStatus.Passable; |
| | | 537 | | } |
| | | 538 | | |
| | | 539 | | internal static bool TryGetExplicitConnectionCost( |
| | | 540 | | Vector3d sourceFootAnchor, |
| | | 541 | | Vector3d entryAnchor, |
| | | 542 | | Fixed64 corridorCost, |
| | | 543 | | Vector3d exitAnchor, |
| | | 544 | | Vector3d targetFootAnchor, |
| | | 545 | | Fixed64 additionalCost, |
| | | 546 | | Fixed64 targetEnterCost, |
| | | 547 | | Fixed64 targetAreaEnterCost, |
| | | 548 | | out Fixed64 total) |
| | | 549 | | { |
| | 20194 | 550 | | total = Fixed64.Zero; |
| | 20194 | 551 | | return Vector3d.TryGetDistance(sourceFootAnchor, entryAnchor, out Fixed64 sourceDistance) |
| | 20194 | 552 | | && Vector3d.TryGetDistance(exitAnchor, targetFootAnchor, out Fixed64 targetDistance) |
| | 20194 | 553 | | && Fixed64.TryAdd(sourceDistance, corridorCost, out total) |
| | 20194 | 554 | | && Fixed64.TryAdd(total, targetDistance, out total) |
| | 20194 | 555 | | && Fixed64.TryAdd(total, additionalCost, out total) |
| | 20194 | 556 | | && Fixed64.TryAdd(total, targetEnterCost, out total) |
| | 20194 | 557 | | && Fixed64.TryAdd(total, targetAreaEnterCost, out total); |
| | | 558 | | } |
| | | 559 | | |
| | | 560 | | internal static bool TryGetPortalTraversalCost( |
| | | 561 | | bool useCeilingDistance, |
| | | 562 | | Vector3d sourceFootAnchor, |
| | | 563 | | Vector3d sourcePortalFoot, |
| | | 564 | | Vector3d targetPortalFoot, |
| | | 565 | | Vector3d targetFootAnchor, |
| | | 566 | | Fixed64 targetEnterCost, |
| | | 567 | | Fixed64 targetAreaEnterCost, |
| | | 568 | | out Fixed64 total) |
| | | 569 | | { |
| | 82832 | 570 | | total = Fixed64.Zero; |
| | 82832 | 571 | | bool hasSourceDistance = useCeilingDistance |
| | 82832 | 572 | | ? NavigationDistanceMath.TryCeiling( |
| | 82832 | 573 | | sourceFootAnchor, |
| | 82832 | 574 | | sourcePortalFoot, |
| | 82832 | 575 | | out Fixed64 sourceDistance) |
| | 82832 | 576 | | : Vector3d.TryGetDistance( |
| | 82832 | 577 | | sourceFootAnchor, |
| | 82832 | 578 | | sourcePortalFoot, |
| | 82832 | 579 | | out sourceDistance); |
| | 82832 | 580 | | bool hasTargetDistance = useCeilingDistance |
| | 82832 | 581 | | ? NavigationDistanceMath.TryCeiling( |
| | 82832 | 582 | | targetPortalFoot, |
| | 82832 | 583 | | targetFootAnchor, |
| | 82832 | 584 | | out Fixed64 targetDistance) |
| | 82832 | 585 | | : Vector3d.TryGetDistance( |
| | 82832 | 586 | | targetPortalFoot, |
| | 82832 | 587 | | targetFootAnchor, |
| | 82832 | 588 | | out targetDistance); |
| | 82832 | 589 | | return hasSourceDistance |
| | 82832 | 590 | | && hasTargetDistance |
| | 82832 | 591 | | && Fixed64.TryAdd(sourceDistance, targetDistance, out total) |
| | 82832 | 592 | | && Fixed64.TryAdd(total, targetEnterCost, out total) |
| | 82832 | 593 | | && Fixed64.TryAdd(total, targetAreaEnterCost, out total); |
| | | 594 | | } |
| | | 595 | | |
| | | 596 | | private TraversalEvaluationStatus EvaluateVerticalDelta( |
| | | 597 | | Vector3d sourceFootAnchor, |
| | | 598 | | Vector3d targetFootAnchor) |
| | | 599 | | { |
| | 60606 | 600 | | bool hasVerticalDelta = Fixed64.TrySubtract( |
| | 60606 | 601 | | targetFootAnchor.Y, |
| | 60606 | 602 | | sourceFootAnchor.Y, |
| | 60606 | 603 | | out Fixed64 verticalDelta); |
| | | 604 | | System.Diagnostics.Debug.Assert(hasVerticalDelta); |
| | 60606 | 605 | | if (verticalDelta > Fixed64.Zero) |
| | | 606 | | { |
| | 21 | 607 | | return verticalDelta <= _profile.MaxStepUp |
| | 21 | 608 | | ? TraversalEvaluationStatus.Passable |
| | 21 | 609 | | : TraversalEvaluationStatus.Impassable; |
| | | 610 | | } |
| | 60585 | 611 | | if (verticalDelta >= Fixed64.Zero) |
| | 60574 | 612 | | return TraversalEvaluationStatus.Passable; |
| | 11 | 613 | | bool hasDrop = Fixed64.TrySubtract(Fixed64.Zero, verticalDelta, out Fixed64 drop); |
| | | 614 | | System.Diagnostics.Debug.Assert(hasDrop); |
| | 11 | 615 | | return drop <= _profile.MaxDropDown |
| | 11 | 616 | | ? TraversalEvaluationStatus.Passable |
| | 11 | 617 | | : TraversalEvaluationStatus.Impassable; |
| | | 618 | | } |
| | | 619 | | |
| | | 620 | | internal bool TryGetPassableNode( |
| | | 621 | | NavigationNodeRef node, |
| | | 622 | | out NavigationNodeState state, |
| | | 623 | | out NavigationAreaRule areaRule) |
| | | 624 | | { |
| | 228676 | 625 | | areaRule = default; |
| | 228676 | 626 | | if (!_graph.TryGetNodeState(node, _exactMedium, out state) |
| | 228676 | 627 | | || !state.IsPresent |
| | 228676 | 628 | | || state.ObstacleCount != 0 |
| | 228676 | 629 | | || (_profile.AllowedMedia & _medium) != _medium |
| | 228676 | 630 | | || (state.Cell.Media & _medium) != _medium |
| | 228676 | 631 | | || (_profile.Capabilities & state.Cell.RequiredCapabilities) |
| | 228676 | 632 | | != state.Cell.RequiredCapabilities |
| | 228676 | 633 | | || !_areaPolicy.TryGetRule(state.Cell.Area, out areaRule) |
| | 228676 | 634 | | || !areaRule.IsAllowed |
| | 228676 | 635 | | || _profile.Shape.Radius > state.Cell.RadiusClearance |
| | 228676 | 636 | | || _profile.Shape.Height > state.Cell.HeightClearance) |
| | | 637 | | { |
| | 233 | 638 | | state = default; |
| | 233 | 639 | | areaRule = default; |
| | 233 | 640 | | return false; |
| | | 641 | | } |
| | | 642 | | |
| | 228443 | 643 | | return true; |
| | | 644 | | } |
| | | 645 | | } |