| | | 1 | | using FixedMathSharp; |
| | | 2 | | using SwiftCollections; |
| | | 3 | | |
| | | 4 | | namespace Trailblazer.Pathing; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Builds generated chart-to-volume transitions from chart cell metadata. |
| | | 8 | | /// </summary> |
| | | 9 | | internal static class GeneratedTraversalTransitionBuilder |
| | | 10 | | { |
| | | 11 | | private const int DefaultGeneratedClimbPathCost = 1; |
| | | 12 | | |
| | 1 | 13 | | private static readonly (int Dx, int Dy, int Dz)[] PerpendicularNeighborOffsets = |
| | 1 | 14 | | { |
| | 1 | 15 | | (1, 0, 0), |
| | 1 | 16 | | (-1, 0, 0), |
| | 1 | 17 | | (0, 1, 0), |
| | 1 | 18 | | (0, -1, 0), |
| | 1 | 19 | | (0, 0, 1), |
| | 1 | 20 | | (0, 0, -1) |
| | 1 | 21 | | }; |
| | | 22 | | |
| | | 23 | | internal static TraversalTransition[] BuildTransitions( |
| | | 24 | | NavigationChart chart, |
| | | 25 | | string transitionIdPrefix) |
| | | 26 | | { |
| | 930 | 27 | | SwiftList<TraversalTransition> transitions = new(); |
| | 930 | 28 | | int[] generatedIndices = chart.GetGeneratedTransitionIndices(); |
| | 1976 | 29 | | for (int i = 0; i < generatedIndices.Length; i++) |
| | | 30 | | { |
| | 58 | 31 | | chart.DecodeIndex(generatedIndices[i], out int x, out int y, out int z); |
| | 812 | 32 | | for (int neighborOffsetIndex = 0; neighborOffsetIndex < PerpendicularNeighborOffsets.Length; neighborOffsetI |
| | | 33 | | { |
| | 348 | 34 | | (int dx, int dy, int dz) = PerpendicularNeighborOffsets[neighborOffsetIndex]; |
| | 348 | 35 | | int neighborX = x + dx; |
| | 348 | 36 | | int neighborY = y + dy; |
| | 348 | 37 | | int neighborZ = z + dz; |
| | 348 | 38 | | if (!chart.IsInBounds(neighborX, neighborY, neighborZ)) |
| | | 39 | | continue; |
| | | 40 | | |
| | 100 | 41 | | NavigationChartCell neighborCell = chart.GetCell(neighborX, neighborY, neighborZ); |
| | 100 | 42 | | if (!ShouldBuildCandidatePair( |
| | 100 | 43 | | x, |
| | 100 | 44 | | y, |
| | 100 | 45 | | z, |
| | 100 | 46 | | neighborX, |
| | 100 | 47 | | neighborY, |
| | 100 | 48 | | neighborZ, |
| | 100 | 49 | | neighborCell)) |
| | | 50 | | { |
| | | 51 | | continue; |
| | | 52 | | } |
| | | 53 | | |
| | 71 | 54 | | TraversalTransition[] pairTransitions = BuildTransitionsForPair( |
| | 71 | 55 | | chart, |
| | 71 | 56 | | transitionIdPrefix, |
| | 71 | 57 | | x, |
| | 71 | 58 | | y, |
| | 71 | 59 | | z, |
| | 71 | 60 | | neighborX, |
| | 71 | 61 | | neighborY, |
| | 71 | 62 | | neighborZ); |
| | 290 | 63 | | for (int pairTransitionIndex = 0; pairTransitionIndex < pairTransitions.Length; pairTransitionIndex++) |
| | 74 | 64 | | transitions.Add(pairTransitions[pairTransitionIndex]); |
| | | 65 | | } |
| | | 66 | | } |
| | | 67 | | |
| | 930 | 68 | | return transitions.ToArray(); |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | internal static TraversalTransition[] BuildTransitionsForPair( |
| | | 72 | | NavigationChart chart, |
| | | 73 | | string transitionIdPrefix, |
| | | 74 | | int firstX, |
| | | 75 | | int firstY, |
| | | 76 | | int firstZ, |
| | | 77 | | int secondX, |
| | | 78 | | int secondY, |
| | | 79 | | int secondZ) |
| | | 80 | | { |
| | 197 | 81 | | if (!chart.IsInBounds(firstX, firstY, firstZ) |
| | 197 | 82 | | || !chart.IsInBounds(secondX, secondY, secondZ)) |
| | | 83 | | { |
| | 1 | 84 | | return System.Array.Empty<TraversalTransition>(); |
| | | 85 | | } |
| | | 86 | | |
| | 196 | 87 | | NavigationChartCell firstCell = chart.GetCell(firstX, firstY, firstZ); |
| | 196 | 88 | | NavigationChartCell secondCell = chart.GetCell(secondX, secondY, secondZ); |
| | 196 | 89 | | SwiftList<TraversalTransition> transitions = new(4); |
| | | 90 | | |
| | 196 | 91 | | if (TryBuildTransitionsForPair( |
| | 196 | 92 | | chart, |
| | 196 | 93 | | transitionIdPrefix, |
| | 196 | 94 | | firstX, |
| | 196 | 95 | | firstY, |
| | 196 | 96 | | firstZ, |
| | 196 | 97 | | firstCell, |
| | 196 | 98 | | secondX, |
| | 196 | 99 | | secondY, |
| | 196 | 100 | | secondZ, |
| | 196 | 101 | | secondCell, |
| | 196 | 102 | | out TraversalTransition chartToVolumeTransition, |
| | 196 | 103 | | out TraversalTransition volumeToChartTransition)) |
| | | 104 | | { |
| | 39 | 105 | | transitions.Add(chartToVolumeTransition); |
| | 39 | 106 | | transitions.Add(volumeToChartTransition); |
| | | 107 | | } |
| | | 108 | | |
| | 196 | 109 | | AddGeneratedClimbTransitionsForPair( |
| | 196 | 110 | | transitions, |
| | 196 | 111 | | chart, |
| | 196 | 112 | | transitionIdPrefix, |
| | 196 | 113 | | firstX, |
| | 196 | 114 | | firstY, |
| | 196 | 115 | | firstZ, |
| | 196 | 116 | | firstCell, |
| | 196 | 117 | | secondX, |
| | 196 | 118 | | secondY, |
| | 196 | 119 | | secondZ, |
| | 196 | 120 | | secondCell); |
| | | 121 | | |
| | 196 | 122 | | return transitions.Count == 0 |
| | 196 | 123 | | ? System.Array.Empty<TraversalTransition>() |
| | 196 | 124 | | : transitions.ToArray(); |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | internal static string[] GetPotentialTransitionIdsForPair( |
| | | 128 | | string transitionIdPrefix, |
| | | 129 | | int firstX, |
| | | 130 | | int firstY, |
| | | 131 | | int firstZ, |
| | | 132 | | int secondX, |
| | | 133 | | int secondY, |
| | | 134 | | int secondZ) |
| | | 135 | | { |
| | 75 | 136 | | return new[] |
| | 75 | 137 | | { |
| | 75 | 138 | | CreateGeneratedTransitionId(transitionIdPrefix, TraversalTransitionType.Takeoff, firstX, firstY, firstZ, sec |
| | 75 | 139 | | CreateGeneratedTransitionId(transitionIdPrefix, TraversalTransitionType.Takeoff, secondX, secondY, secondZ, |
| | 75 | 140 | | CreateGeneratedTransitionId(transitionIdPrefix, TraversalTransitionType.Landing, firstX, firstY, firstZ, sec |
| | 75 | 141 | | CreateGeneratedTransitionId(transitionIdPrefix, TraversalTransitionType.Landing, secondX, secondY, secondZ, |
| | 75 | 142 | | CreateGeneratedTransitionId(transitionIdPrefix, TraversalTransitionType.SwimEntry, firstX, firstY, firstZ, s |
| | 75 | 143 | | CreateGeneratedTransitionId(transitionIdPrefix, TraversalTransitionType.SwimEntry, secondX, secondY, secondZ |
| | 75 | 144 | | CreateGeneratedTransitionId(transitionIdPrefix, TraversalTransitionType.SwimExit, firstX, firstY, firstZ, se |
| | 75 | 145 | | CreateGeneratedTransitionId(transitionIdPrefix, TraversalTransitionType.SwimExit, secondX, secondY, secondZ, |
| | 75 | 146 | | CreateGeneratedTransitionId(transitionIdPrefix, TraversalTransitionType.Climb, firstX, firstY, firstZ, secon |
| | 75 | 147 | | CreateGeneratedTransitionId(transitionIdPrefix, TraversalTransitionType.Climb, secondX, secondY, secondZ, fi |
| | 75 | 148 | | }; |
| | | 149 | | } |
| | | 150 | | |
| | | 151 | | internal static bool CanBuildTransitionsForPairFromChartData( |
| | | 152 | | NavigationChart chart, |
| | | 153 | | int firstX, |
| | | 154 | | int firstY, |
| | | 155 | | int firstZ, |
| | | 156 | | int secondX, |
| | | 157 | | int secondY, |
| | | 158 | | int secondZ) |
| | | 159 | | { |
| | 19 | 160 | | if (!chart.IsInBounds(firstX, firstY, firstZ) |
| | 19 | 161 | | || !chart.IsInBounds(secondX, secondY, secondZ)) |
| | | 162 | | { |
| | 1 | 163 | | return false; |
| | | 164 | | } |
| | | 165 | | |
| | 18 | 166 | | NavigationChartCell firstCell = chart.GetCell(firstX, firstY, firstZ); |
| | 18 | 167 | | NavigationChartCell secondCell = chart.GetCell(secondX, secondY, secondZ); |
| | 18 | 168 | | return HasSingleBoundaryCandidate(firstCell.GeneratedTransitionMedia, secondCell.GeneratedTransitionMedia) |
| | 18 | 169 | | || HasGeneratedClimbTransitionsForPair(firstCell, secondCell); |
| | | 170 | | } |
| | | 171 | | |
| | | 172 | | private static void AddGeneratedClimbTransitionsForPair( |
| | | 173 | | SwiftList<TraversalTransition> transitions, |
| | | 174 | | NavigationChart chart, |
| | | 175 | | string transitionIdPrefix, |
| | | 176 | | int firstX, |
| | | 177 | | int firstY, |
| | | 178 | | int firstZ, |
| | | 179 | | NavigationChartCell firstCell, |
| | | 180 | | int secondX, |
| | | 181 | | int secondY, |
| | | 182 | | int secondZ, |
| | | 183 | | NavigationChartCell secondCell) |
| | | 184 | | { |
| | 196 | 185 | | bool firstIsClimbSurface = IsClimbSurface(firstCell); |
| | 196 | 186 | | bool secondIsClimbSurface = IsClimbSurface(secondCell); |
| | 196 | 187 | | if (!firstIsClimbSurface && !secondIsClimbSurface) |
| | 107 | 188 | | return; |
| | | 189 | | |
| | 89 | 190 | | Vector3d firstPosition = chart.GetWorldPosition(firstX, firstY, firstZ); |
| | 89 | 191 | | Vector3d secondPosition = chart.GetWorldPosition(secondX, secondY, secondZ); |
| | | 192 | | |
| | 89 | 193 | | if (firstIsClimbSurface && secondIsClimbSurface) |
| | | 194 | | { |
| | 20 | 195 | | transitions.Add(CreateClimbTransition( |
| | 20 | 196 | | transitionIdPrefix, |
| | 20 | 197 | | firstX, |
| | 20 | 198 | | firstY, |
| | 20 | 199 | | firstZ, |
| | 20 | 200 | | secondX, |
| | 20 | 201 | | secondY, |
| | 20 | 202 | | secondZ, |
| | 20 | 203 | | firstPosition, |
| | 20 | 204 | | secondPosition, |
| | 20 | 205 | | requestsClimbIntent: true)); |
| | 20 | 206 | | transitions.Add(CreateClimbTransition( |
| | 20 | 207 | | transitionIdPrefix, |
| | 20 | 208 | | secondX, |
| | 20 | 209 | | secondY, |
| | 20 | 210 | | secondZ, |
| | 20 | 211 | | firstX, |
| | 20 | 212 | | firstY, |
| | 20 | 213 | | firstZ, |
| | 20 | 214 | | secondPosition, |
| | 20 | 215 | | firstPosition, |
| | 20 | 216 | | requestsClimbIntent: true)); |
| | 20 | 217 | | return; |
| | | 218 | | } |
| | | 219 | | |
| | 69 | 220 | | if (firstIsClimbSurface |
| | 69 | 221 | | && IsClimbTransitionSeam(firstCell) |
| | 69 | 222 | | && secondCell.HasSolid) |
| | | 223 | | { |
| | 16 | 224 | | transitions.Add(CreateClimbTransition( |
| | 16 | 225 | | transitionIdPrefix, |
| | 16 | 226 | | secondX, |
| | 16 | 227 | | secondY, |
| | 16 | 228 | | secondZ, |
| | 16 | 229 | | firstX, |
| | 16 | 230 | | firstY, |
| | 16 | 231 | | firstZ, |
| | 16 | 232 | | secondPosition, |
| | 16 | 233 | | firstPosition, |
| | 16 | 234 | | requestsClimbIntent: true)); |
| | 16 | 235 | | transitions.Add(CreateClimbTransition( |
| | 16 | 236 | | transitionIdPrefix, |
| | 16 | 237 | | firstX, |
| | 16 | 238 | | firstY, |
| | 16 | 239 | | firstZ, |
| | 16 | 240 | | secondX, |
| | 16 | 241 | | secondY, |
| | 16 | 242 | | secondZ, |
| | 16 | 243 | | firstPosition, |
| | 16 | 244 | | secondPosition, |
| | 16 | 245 | | requestsClimbIntent: false)); |
| | 16 | 246 | | return; |
| | | 247 | | } |
| | | 248 | | |
| | 53 | 249 | | if (secondIsClimbSurface |
| | 53 | 250 | | && IsClimbTransitionSeam(secondCell) |
| | 53 | 251 | | && firstCell.HasSolid) |
| | | 252 | | { |
| | 1 | 253 | | transitions.Add(CreateClimbTransition( |
| | 1 | 254 | | transitionIdPrefix, |
| | 1 | 255 | | firstX, |
| | 1 | 256 | | firstY, |
| | 1 | 257 | | firstZ, |
| | 1 | 258 | | secondX, |
| | 1 | 259 | | secondY, |
| | 1 | 260 | | secondZ, |
| | 1 | 261 | | firstPosition, |
| | 1 | 262 | | secondPosition, |
| | 1 | 263 | | requestsClimbIntent: true)); |
| | 1 | 264 | | transitions.Add(CreateClimbTransition( |
| | 1 | 265 | | transitionIdPrefix, |
| | 1 | 266 | | secondX, |
| | 1 | 267 | | secondY, |
| | 1 | 268 | | secondZ, |
| | 1 | 269 | | firstX, |
| | 1 | 270 | | firstY, |
| | 1 | 271 | | firstZ, |
| | 1 | 272 | | secondPosition, |
| | 1 | 273 | | firstPosition, |
| | 1 | 274 | | requestsClimbIntent: false)); |
| | | 275 | | } |
| | 53 | 276 | | } |
| | | 277 | | |
| | | 278 | | private static bool IsClimbSurface(NavigationChartCell cell) => |
| | 554 | 279 | | (cell.Flags & NavigationChartCellFlags.ClimbSurfaceHint) != 0; |
| | | 280 | | |
| | | 281 | | private static bool IsClimbTransitionSeam(NavigationChartCell cell) => |
| | 70 | 282 | | (cell.Flags & NavigationChartCellFlags.ClimbTransitionHint) != 0; |
| | | 283 | | |
| | | 284 | | private static bool IsGeneratedTransitionCandidate(NavigationChartCell cell) => |
| | 100 | 285 | | cell.CanGenerateTransition || IsClimbSurface(cell); |
| | | 286 | | |
| | | 287 | | private static bool HasGeneratedClimbTransitionsForPair( |
| | | 288 | | NavigationChartCell firstCell, |
| | | 289 | | NavigationChartCell secondCell) |
| | | 290 | | { |
| | 13 | 291 | | bool firstIsClimbSurface = IsClimbSurface(firstCell); |
| | 13 | 292 | | bool secondIsClimbSurface = IsClimbSurface(secondCell); |
| | 13 | 293 | | if (!firstIsClimbSurface && !secondIsClimbSurface) |
| | 12 | 294 | | return false; |
| | | 295 | | |
| | 1 | 296 | | return (firstIsClimbSurface && secondIsClimbSurface) |
| | 1 | 297 | | || (firstIsClimbSurface && IsClimbTransitionSeam(firstCell) && secondCell.HasSolid) |
| | 1 | 298 | | || (secondIsClimbSurface && IsClimbTransitionSeam(secondCell) && firstCell.HasSolid); |
| | | 299 | | } |
| | | 300 | | |
| | | 301 | | private static bool ShouldBuildCandidatePair( |
| | | 302 | | int firstX, |
| | | 303 | | int firstY, |
| | | 304 | | int firstZ, |
| | | 305 | | int secondX, |
| | | 306 | | int secondY, |
| | | 307 | | int secondZ, |
| | | 308 | | NavigationChartCell secondCell) |
| | | 309 | | { |
| | 100 | 310 | | if (!IsGeneratedTransitionCandidate(secondCell)) |
| | 42 | 311 | | return true; |
| | | 312 | | |
| | 58 | 313 | | return IsLexicographicallyBefore( |
| | 58 | 314 | | firstX, |
| | 58 | 315 | | firstY, |
| | 58 | 316 | | firstZ, |
| | 58 | 317 | | secondX, |
| | 58 | 318 | | secondY, |
| | 58 | 319 | | secondZ); |
| | | 320 | | } |
| | | 321 | | |
| | | 322 | | private static bool IsLexicographicallyBefore( |
| | | 323 | | int firstX, |
| | | 324 | | int firstY, |
| | | 325 | | int firstZ, |
| | | 326 | | int secondX, |
| | | 327 | | int secondY, |
| | | 328 | | int secondZ) |
| | | 329 | | { |
| | 58 | 330 | | return firstX < secondX |
| | 58 | 331 | | || (firstX == secondX && firstY < secondY) |
| | 58 | 332 | | || (firstX == secondX && firstY == secondY && firstZ < secondZ); |
| | | 333 | | } |
| | | 334 | | |
| | | 335 | | private static TraversalTransition CreateClimbTransition( |
| | | 336 | | string transitionIdPrefix, |
| | | 337 | | int sourceX, |
| | | 338 | | int sourceY, |
| | | 339 | | int sourceZ, |
| | | 340 | | int destinationX, |
| | | 341 | | int destinationY, |
| | | 342 | | int destinationZ, |
| | | 343 | | Vector3d sourcePosition, |
| | | 344 | | Vector3d destinationPosition, |
| | | 345 | | bool requestsClimbIntent) |
| | | 346 | | { |
| | 74 | 347 | | return new TraversalTransition( |
| | 74 | 348 | | CreateGeneratedTransitionId( |
| | 74 | 349 | | transitionIdPrefix, |
| | 74 | 350 | | TraversalTransitionType.Climb, |
| | 74 | 351 | | sourceX, |
| | 74 | 352 | | sourceY, |
| | 74 | 353 | | sourceZ, |
| | 74 | 354 | | destinationX, |
| | 74 | 355 | | destinationY, |
| | 74 | 356 | | destinationZ), |
| | 74 | 357 | | TraversalTransitionType.Climb, |
| | 74 | 358 | | TraversalTransitionAnchor.Solid(sourcePosition), |
| | 74 | 359 | | TraversalTransitionAnchor.Solid(destinationPosition), |
| | 74 | 360 | | DefaultGeneratedClimbPathCost, |
| | 74 | 361 | | false, |
| | 74 | 362 | | requestsClimbIntent); |
| | | 363 | | } |
| | | 364 | | |
| | | 365 | | private static bool TryBuildTransitionsForPair( |
| | | 366 | | NavigationChart chart, |
| | | 367 | | string transitionIdPrefix, |
| | | 368 | | int firstX, |
| | | 369 | | int firstY, |
| | | 370 | | int firstZ, |
| | | 371 | | NavigationChartCell firstCell, |
| | | 372 | | int secondX, |
| | | 373 | | int secondY, |
| | | 374 | | int secondZ, |
| | | 375 | | NavigationChartCell secondCell, |
| | | 376 | | out TraversalTransition chartToVolumeTransition, |
| | | 377 | | out TraversalTransition volumeToChartTransition) |
| | | 378 | | { |
| | 196 | 379 | | chartToVolumeTransition = default; |
| | 196 | 380 | | volumeToChartTransition = default; |
| | | 381 | | |
| | 196 | 382 | | if (!TryResolveSingleBoundaryCandidate( |
| | 196 | 383 | | firstCell.GeneratedTransitionMedia, |
| | 196 | 384 | | secondCell.GeneratedTransitionMedia, |
| | 196 | 385 | | chart.GetWorldPosition(firstX, firstY, firstZ), |
| | 196 | 386 | | chart.GetWorldPosition(secondX, secondY, secondZ), |
| | 196 | 387 | | out Vector3d chartPosition, |
| | 196 | 388 | | out Vector3d volumePosition, |
| | 196 | 389 | | out TraversalMedium volumeMedium)) |
| | | 390 | | { |
| | 157 | 391 | | return false; |
| | | 392 | | } |
| | | 393 | | |
| | 39 | 394 | | NavigationChartCell chartCell = chartPosition == chart.GetWorldPosition(firstX, firstY, firstZ) |
| | 39 | 395 | | ? firstCell |
| | 39 | 396 | | : secondCell; |
| | | 397 | | |
| | 39 | 398 | | return TryBuildChartVolumeTransitionPair( |
| | 39 | 399 | | chart, |
| | 39 | 400 | | transitionIdPrefix, |
| | 39 | 401 | | chartPosition, |
| | 39 | 402 | | volumePosition, |
| | 39 | 403 | | volumeMedium, |
| | 39 | 404 | | chartCell, |
| | 39 | 405 | | out chartToVolumeTransition, |
| | 39 | 406 | | out volumeToChartTransition); |
| | | 407 | | } |
| | | 408 | | |
| | | 409 | | private static bool TryResolveSingleBoundaryCandidate( |
| | | 410 | | TraversalMedia firstTransitionMedia, |
| | | 411 | | TraversalMedia secondTransitionMedia, |
| | | 412 | | Vector3d firstPosition, |
| | | 413 | | Vector3d secondPosition, |
| | | 414 | | out Vector3d chartPosition, |
| | | 415 | | out Vector3d volumePosition, |
| | | 416 | | out TraversalMedium volumeMedium) |
| | | 417 | | { |
| | 196 | 418 | | chartPosition = default; |
| | 196 | 419 | | volumePosition = default; |
| | 196 | 420 | | volumeMedium = TraversalMedium.Unknown; |
| | | 421 | | |
| | 196 | 422 | | int candidateCount = 0; |
| | | 423 | | |
| | 196 | 424 | | TryAddChartVolumeCandidate( |
| | 196 | 425 | | firstTransitionMedia, |
| | 196 | 426 | | secondTransitionMedia, |
| | 196 | 427 | | TraversalMedia.Gas, |
| | 196 | 428 | | TraversalMedium.Gas, |
| | 196 | 429 | | firstPosition, |
| | 196 | 430 | | secondPosition, |
| | 196 | 431 | | ref candidateCount, |
| | 196 | 432 | | ref chartPosition, |
| | 196 | 433 | | ref volumePosition, |
| | 196 | 434 | | ref volumeMedium); |
| | 196 | 435 | | TryAddChartVolumeCandidate( |
| | 196 | 436 | | firstTransitionMedia, |
| | 196 | 437 | | secondTransitionMedia, |
| | 196 | 438 | | TraversalMedia.Liquid, |
| | 196 | 439 | | TraversalMedium.Liquid, |
| | 196 | 440 | | firstPosition, |
| | 196 | 441 | | secondPosition, |
| | 196 | 442 | | ref candidateCount, |
| | 196 | 443 | | ref chartPosition, |
| | 196 | 444 | | ref volumePosition, |
| | 196 | 445 | | ref volumeMedium); |
| | | 446 | | |
| | 196 | 447 | | return candidateCount == 1; |
| | | 448 | | } |
| | | 449 | | |
| | | 450 | | private static bool HasSingleBoundaryCandidate( |
| | | 451 | | TraversalMedia firstTransitionMedia, |
| | | 452 | | TraversalMedia secondTransitionMedia) |
| | | 453 | | { |
| | 18 | 454 | | int candidateCount = 0; |
| | 18 | 455 | | if (((firstTransitionMedia & TraversalMedia.Solid) != 0) |
| | 18 | 456 | | && ((secondTransitionMedia & TraversalMedia.Gas) != 0)) |
| | | 457 | | { |
| | 1 | 458 | | candidateCount++; |
| | | 459 | | } |
| | | 460 | | |
| | 18 | 461 | | if (((secondTransitionMedia & TraversalMedia.Solid) != 0) |
| | 18 | 462 | | && ((firstTransitionMedia & TraversalMedia.Gas) != 0)) |
| | | 463 | | { |
| | 1 | 464 | | candidateCount++; |
| | | 465 | | } |
| | | 466 | | |
| | 18 | 467 | | if (((firstTransitionMedia & TraversalMedia.Solid) != 0) |
| | 18 | 468 | | && ((secondTransitionMedia & TraversalMedia.Liquid) != 0)) |
| | | 469 | | { |
| | 2 | 470 | | candidateCount++; |
| | | 471 | | } |
| | | 472 | | |
| | 18 | 473 | | if (((secondTransitionMedia & TraversalMedia.Solid) != 0) |
| | 18 | 474 | | && ((firstTransitionMedia & TraversalMedia.Liquid) != 0)) |
| | | 475 | | { |
| | 1 | 476 | | candidateCount++; |
| | | 477 | | } |
| | | 478 | | |
| | 18 | 479 | | return candidateCount == 1; |
| | | 480 | | } |
| | | 481 | | |
| | | 482 | | private static void TryAddChartVolumeCandidate( |
| | | 483 | | TraversalMedia firstTransitionMedia, |
| | | 484 | | TraversalMedia secondTransitionMedia, |
| | | 485 | | TraversalMedia candidateVolumeKind, |
| | | 486 | | TraversalMedium candidateVolumeMedium, |
| | | 487 | | Vector3d firstPosition, |
| | | 488 | | Vector3d secondPosition, |
| | | 489 | | ref int candidateCount, |
| | | 490 | | ref Vector3d chartPosition, |
| | | 491 | | ref Vector3d volumePosition, |
| | | 492 | | ref TraversalMedium volumeMedium) |
| | | 493 | | { |
| | 392 | 494 | | bool firstCanBeChart = (firstTransitionMedia & TraversalMedia.Solid) != 0; |
| | 392 | 495 | | bool secondCanBeChart = (secondTransitionMedia & TraversalMedia.Solid) != 0; |
| | 392 | 496 | | bool firstCanBeVolume = (firstTransitionMedia & candidateVolumeKind) != 0; |
| | 392 | 497 | | bool secondCanBeVolume = (secondTransitionMedia & candidateVolumeKind) != 0; |
| | | 498 | | |
| | 392 | 499 | | if (firstCanBeChart && secondCanBeVolume) |
| | | 500 | | { |
| | 37 | 501 | | candidateCount++; |
| | 37 | 502 | | chartPosition = firstPosition; |
| | 37 | 503 | | volumePosition = secondPosition; |
| | 37 | 504 | | volumeMedium = candidateVolumeMedium; |
| | | 505 | | } |
| | | 506 | | |
| | 392 | 507 | | if (secondCanBeChart && firstCanBeVolume) |
| | | 508 | | { |
| | 6 | 509 | | candidateCount++; |
| | 6 | 510 | | chartPosition = secondPosition; |
| | 6 | 511 | | volumePosition = firstPosition; |
| | 6 | 512 | | volumeMedium = candidateVolumeMedium; |
| | | 513 | | } |
| | 392 | 514 | | } |
| | | 515 | | |
| | | 516 | | private static bool TryBuildChartVolumeTransitionPair( |
| | | 517 | | NavigationChart chart, |
| | | 518 | | string transitionIdPrefix, |
| | | 519 | | Vector3d chartPosition, |
| | | 520 | | Vector3d volumePosition, |
| | | 521 | | TraversalMedium volumeMedium, |
| | | 522 | | NavigationChartCell chartCell, |
| | | 523 | | out TraversalTransition chartToVolumeTransition, |
| | | 524 | | out TraversalTransition volumeToChartTransition) |
| | | 525 | | { |
| | 39 | 526 | | chartToVolumeTransition = default; |
| | 39 | 527 | | volumeToChartTransition = default; |
| | | 528 | | |
| | | 529 | | TraversalTransitionType entryType; |
| | | 530 | | TraversalTransitionType exitType; |
| | | 531 | | TraversalTransitionAnchor volumeAnchor; |
| | | 532 | | |
| | | 533 | | switch (volumeMedium) |
| | | 534 | | { |
| | | 535 | | case TraversalMedium.Gas: |
| | 2 | 536 | | entryType = TraversalTransitionType.Takeoff; |
| | 2 | 537 | | exitType = TraversalTransitionType.Landing; |
| | 2 | 538 | | volumeAnchor = TraversalTransitionAnchor.Gas(volumePosition); |
| | 2 | 539 | | break; |
| | | 540 | | case TraversalMedium.Liquid: |
| | 37 | 541 | | entryType = TraversalTransitionType.SwimEntry; |
| | 37 | 542 | | exitType = TraversalTransitionType.SwimExit; |
| | 37 | 543 | | volumeAnchor = TraversalTransitionAnchor.Liquid(volumePosition); |
| | 37 | 544 | | break; |
| | | 545 | | default: |
| | 0 | 546 | | return false; |
| | | 547 | | } |
| | | 548 | | |
| | 39 | 549 | | chart.TryWorldToIndex(chartPosition, out int chartX, out int chartY, out int chartZ); |
| | 39 | 550 | | chart.TryWorldToIndex(volumePosition, out int volumeX, out int volumeY, out int volumeZ); |
| | | 551 | | |
| | 39 | 552 | | TraversalTransitionAnchor chartAnchor = TraversalTransitionAnchor.Solid(chartPosition); |
| | 39 | 553 | | chartToVolumeTransition = new TraversalTransition( |
| | 39 | 554 | | CreateGeneratedTransitionId( |
| | 39 | 555 | | transitionIdPrefix, |
| | 39 | 556 | | entryType, |
| | 39 | 557 | | chartX, |
| | 39 | 558 | | chartY, |
| | 39 | 559 | | chartZ, |
| | 39 | 560 | | volumeX, |
| | 39 | 561 | | volumeY, |
| | 39 | 562 | | volumeZ), |
| | 39 | 563 | | entryType, |
| | 39 | 564 | | chartAnchor, |
| | 39 | 565 | | volumeAnchor); |
| | 39 | 566 | | volumeToChartTransition = new TraversalTransition( |
| | 39 | 567 | | CreateGeneratedTransitionId( |
| | 39 | 568 | | transitionIdPrefix, |
| | 39 | 569 | | exitType, |
| | 39 | 570 | | volumeX, |
| | 39 | 571 | | volumeY, |
| | 39 | 572 | | volumeZ, |
| | 39 | 573 | | chartX, |
| | 39 | 574 | | chartY, |
| | 39 | 575 | | chartZ), |
| | 39 | 576 | | exitType, |
| | 39 | 577 | | volumeAnchor, |
| | 39 | 578 | | chartAnchor, |
| | 39 | 579 | | requestsClimbIntent: ShouldRequestClimbIntentOnLiquidExit(volumeMedium, chartCell), |
| | 39 | 580 | | preserveClimbIntentOnFollowup: ShouldRequestClimbIntentOnLiquidExit(volumeMedium, chartCell)); |
| | 39 | 581 | | return true; |
| | | 582 | | } |
| | | 583 | | |
| | | 584 | | private static bool ShouldRequestClimbIntentOnLiquidExit( |
| | | 585 | | TraversalMedium volumeMedium, |
| | | 586 | | NavigationChartCell chartCell) |
| | | 587 | | { |
| | 78 | 588 | | return volumeMedium == TraversalMedium.Liquid |
| | 78 | 589 | | && IsClimbSurface(chartCell) |
| | 78 | 590 | | && chartCell.SupportsMedium(TraversalMedium.Liquid); |
| | | 591 | | } |
| | | 592 | | |
| | | 593 | | private static string CreateGeneratedTransitionId( |
| | | 594 | | string transitionIdPrefix, |
| | | 595 | | TraversalTransitionType transitionType, |
| | | 596 | | int sourceX, |
| | | 597 | | int sourceY, |
| | | 598 | | int sourceZ, |
| | | 599 | | int destinationX, |
| | | 600 | | int destinationY, |
| | | 601 | | int destinationZ) |
| | | 602 | | { |
| | 902 | 603 | | return |
| | 902 | 604 | | $"{transitionIdPrefix}:{transitionType}:{sourceY}_{sourceX}_{sourceZ}->{destinationY}_{destinationX}_{destin |
| | | 605 | | } |
| | | 606 | | } |