| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationMapTokenImporter.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 | | using System.Globalization; |
| | | 10 | | using FixedMathSharp; |
| | | 11 | | using GridForge.Configuration; |
| | | 12 | | using GridForge.Grids.Topology; |
| | | 13 | | using GridForge.Spatial; |
| | | 14 | | |
| | | 15 | | namespace Trailblazer.Pathing; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Imports X/Y/Z rectangular token maps into immutable addressed navigation maps. |
| | | 19 | | /// </summary> |
| | | 20 | | public static class NavigationMapTokenImporter |
| | | 21 | | { |
| | | 22 | | /// <summary> |
| | | 23 | | /// Parses a rectangular token volume and emits explicit canonical cells and addressed |
| | | 24 | | /// semantic transitions. Token coverage is not retained as a default-cell mask. |
| | | 25 | | /// </summary> |
| | | 26 | | public static NavigationMap ImportRectangular( |
| | | 27 | | string mapId, |
| | | 28 | | GridConfiguration gridConfiguration, |
| | | 29 | | string[,,] source, |
| | | 30 | | NavigationTokenLegend? legend = null, |
| | | 31 | | string? transitionIdPrefix = null) |
| | | 32 | | { |
| | 30 | 33 | | SwiftThrowHelper.ThrowIfNull(source, nameof(source)); |
| | 29 | 34 | | var builder = new NavigationMapBuilder(mapId, gridConfiguration); |
| | 29 | 35 | | SwiftThrowHelper.ThrowIfArgument( |
| | 29 | 36 | | builder.GridBinding.Configuration.TopologyKind != GridTopologyKind.RectangularPrism, |
| | 29 | 37 | | nameof(gridConfiguration), |
| | 29 | 38 | | "Token import requires rectangular-prism topology."); |
| | 29 | 39 | | SwiftThrowHelper.ThrowIfArgument( |
| | 29 | 40 | | source.GetLength(0) != builder.GridBinding.Width |
| | 29 | 41 | | || source.GetLength(1) != builder.GridBinding.Height |
| | 29 | 42 | | || source.GetLength(2) != builder.GridBinding.Length, |
| | 29 | 43 | | nameof(source), |
| | 29 | 44 | | "Token dimensions must match the normalized grid address space in X/Y/Z order."); |
| | | 45 | | |
| | 26 | 46 | | NavigationTokenLegend resolvedLegend = legend ?? NavigationTokenLegend.CreateBuiltIn(); |
| | 26 | 47 | | string resolvedPrefix = string.IsNullOrWhiteSpace(transitionIdPrefix) |
| | 26 | 48 | | ? mapId |
| | 26 | 49 | | : transitionIdPrefix!; |
| | 26 | 50 | | var parsed = new ParsedTokenCell[ |
| | 26 | 51 | | builder.GridBinding.Width, |
| | 26 | 52 | | builder.GridBinding.Height, |
| | 26 | 53 | | builder.GridBinding.Length]; |
| | | 54 | | |
| | 124 | 55 | | for (int x = 0; x < parsed.GetLength(0); x++) |
| | | 56 | | { |
| | 162 | 57 | | for (int y = 0; y < parsed.GetLength(1); y++) |
| | | 58 | | { |
| | 168 | 59 | | for (int z = 0; z < parsed.GetLength(2); z++) |
| | | 60 | | { |
| | 47 | 61 | | ParsedTokenCell cell = ParseCell(source[x, y, z], x, y, z, resolvedLegend); |
| | 39 | 62 | | parsed[x, y, z] = cell; |
| | 39 | 63 | | if (cell.Entry.EmitsCell) |
| | 35 | 64 | | builder.AddCell(new VoxelIndex(x, y, z), cell.Cell); |
| | | 65 | | } |
| | | 66 | | } |
| | | 67 | | } |
| | | 68 | | |
| | 18 | 69 | | GenerateTransitions(builder, parsed, mapId, resolvedPrefix); |
| | 18 | 70 | | return builder.Build(); |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | private static ParsedTokenCell ParseCell( |
| | | 74 | | string rawToken, |
| | | 75 | | int x, |
| | | 76 | | int y, |
| | | 77 | | int z, |
| | | 78 | | NavigationTokenLegend legend) |
| | | 79 | | { |
| | 47 | 80 | | string token = rawToken?.Trim() ?? string.Empty; |
| | 47 | 81 | | bool hasMarker = false; |
| | 47 | 82 | | int markerIndex = token.IndexOf('!'); |
| | 47 | 83 | | if (markerIndex >= 0) |
| | | 84 | | { |
| | 28 | 85 | | SwiftThrowHelper.ThrowIfArgument( |
| | 28 | 86 | | markerIndex != token.Length - 1 || token.LastIndexOf('!') != markerIndex, |
| | 28 | 87 | | nameof(rawToken), |
| | 28 | 88 | | $"Invalid token '{token}' at [{x}, {y}, {z}]. Only one trailing '!' marker is supported."); |
| | 27 | 89 | | hasMarker = true; |
| | 27 | 90 | | token = token[..^1].TrimEnd(); |
| | 27 | 91 | | SwiftThrowHelper.ThrowIfArgument( |
| | 27 | 92 | | token.Length == 0, |
| | 27 | 93 | | nameof(rawToken), |
| | 27 | 94 | | $"Invalid token '{rawToken}' at [{x}, {y}, {z}]. A marker requires a base token."); |
| | | 95 | | } |
| | | 96 | | |
| | 45 | 97 | | bool hasInlineCost = false; |
| | 45 | 98 | | Fixed64 inlineCost = default; |
| | 45 | 99 | | int costSeparatorIndex = token.LastIndexOf('_'); |
| | 45 | 100 | | if (costSeparatorIndex >= 0) |
| | | 101 | | { |
| | 5 | 102 | | string costText = token[(costSeparatorIndex + 1)..]; |
| | 5 | 103 | | bool parsed = Fixed64.TryParse(costText, CultureInfo.InvariantCulture, out inlineCost); |
| | 5 | 104 | | SwiftThrowHelper.ThrowIfArgument( |
| | 5 | 105 | | !parsed || inlineCost < Fixed64.Zero || costSeparatorIndex == 0, |
| | 5 | 106 | | nameof(rawToken), |
| | 5 | 107 | | $"Invalid non-negative fixed-point cost in token '{rawToken}' at [{x}, {y}, {z}]."); |
| | 2 | 108 | | hasInlineCost = true; |
| | 2 | 109 | | token = token[..costSeparatorIndex].TrimEnd(); |
| | | 110 | | } |
| | | 111 | | |
| | 42 | 112 | | bool known = legend.TryGetEntry(token, out NavigationTokenLegendEntry entry); |
| | 42 | 113 | | SwiftThrowHelper.ThrowIfArgument( |
| | 42 | 114 | | !known, |
| | 42 | 115 | | nameof(rawToken), |
| | 42 | 116 | | $"Unknown navigation token '{rawToken}' at [{x}, {y}, {z}]."); |
| | 41 | 117 | | SwiftThrowHelper.ThrowIfArgument( |
| | 41 | 118 | | hasMarker |
| | 41 | 119 | | && (!entry.EmitsCell |
| | 41 | 120 | | || (entry.TransitionMedia == TraversalMedia.None |
| | 41 | 121 | | && (entry.Cell.Flags & NavigationCellFlags.ClimbSurfaceHint) == 0)), |
| | 41 | 122 | | nameof(rawToken), |
| | 41 | 123 | | $"Token '{rawToken}' at [{x}, {y}, {z}] cannot generate transitions."); |
| | | 124 | | |
| | 39 | 125 | | if (!entry.EmitsCell) |
| | 4 | 126 | | return default; |
| | | 127 | | |
| | 35 | 128 | | return new ParsedTokenCell( |
| | 35 | 129 | | entry, |
| | 35 | 130 | | entry.CreateCell(inlineCost, hasInlineCost, hasMarker), |
| | 35 | 131 | | hasMarker); |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | private static void GenerateTransitions( |
| | | 135 | | NavigationMapBuilder builder, |
| | | 136 | | ParsedTokenCell[,,] cells, |
| | | 137 | | string mapId, |
| | | 138 | | string transitionIdPrefix) |
| | | 139 | | { |
| | 108 | 140 | | for (int x = 0; x < cells.GetLength(0); x++) |
| | | 141 | | { |
| | 146 | 142 | | for (int y = 0; y < cells.GetLength(1); y++) |
| | | 143 | | { |
| | 152 | 144 | | for (int z = 0; z < cells.GetLength(2); z++) |
| | | 145 | | { |
| | 39 | 146 | | VoxelIndex source = new(x, y, z); |
| | 39 | 147 | | if (x + 1 < cells.GetLength(0)) |
| | 18 | 148 | | GenerateTransitionPair(builder, cells[x, y, z], cells[x + 1, y, z], source, new VoxelIndex(x + 1 |
| | 39 | 149 | | if (y + 1 < cells.GetLength(1)) |
| | 2 | 150 | | GenerateTransitionPair(builder, cells[x, y, z], cells[x, y + 1, z], source, new VoxelIndex(x, y |
| | 39 | 151 | | if (z + 1 < cells.GetLength(2)) |
| | 2 | 152 | | GenerateTransitionPair(builder, cells[x, y, z], cells[x, y, z + 1], source, new VoxelIndex(x, y, |
| | | 153 | | } |
| | | 154 | | } |
| | | 155 | | } |
| | 18 | 156 | | } |
| | | 157 | | |
| | | 158 | | private static void GenerateTransitionPair( |
| | | 159 | | NavigationMapBuilder builder, |
| | | 160 | | ParsedTokenCell first, |
| | | 161 | | ParsedTokenCell second, |
| | | 162 | | VoxelIndex firstIndex, |
| | | 163 | | VoxelIndex secondIndex, |
| | | 164 | | string mapId, |
| | | 165 | | string prefix) |
| | | 166 | | { |
| | 22 | 167 | | if (!first.Entry.EmitsCell || !second.Entry.EmitsCell) |
| | 4 | 168 | | return; |
| | | 169 | | |
| | 18 | 170 | | TryAddMediaTransitionPair(builder, first, second, firstIndex, secondIndex, mapId, prefix); |
| | 18 | 171 | | TryAddClimbTransitionPair(builder, first, second, firstIndex, secondIndex, mapId, prefix); |
| | 18 | 172 | | } |
| | | 173 | | |
| | | 174 | | private static void TryAddMediaTransitionPair( |
| | | 175 | | NavigationMapBuilder builder, |
| | | 176 | | ParsedTokenCell first, |
| | | 177 | | ParsedTokenCell second, |
| | | 178 | | VoxelIndex firstIndex, |
| | | 179 | | VoxelIndex secondIndex, |
| | | 180 | | string mapId, |
| | | 181 | | string prefix) |
| | | 182 | | { |
| | 18 | 183 | | TraversalMedia firstMarkerMedia = first.HasMarker ? first.Entry.TransitionMedia : TraversalMedia.None; |
| | 18 | 184 | | TraversalMedia secondMarkerMedia = second.HasMarker ? second.Entry.TransitionMedia : TraversalMedia.None; |
| | 18 | 185 | | int candidateCount = 0; |
| | 18 | 186 | | VoxelIndex solidIndex = default; |
| | 18 | 187 | | VoxelIndex volumeIndex = default; |
| | 18 | 188 | | TraversalMedium volumeMedium = default; |
| | | 189 | | |
| | 18 | 190 | | TrySelectBoundary(firstMarkerMedia, secondMarkerMedia, TraversalMedia.Gas, TraversalMedium.Gas, firstIndex, seco |
| | 18 | 191 | | TrySelectBoundary(firstMarkerMedia, secondMarkerMedia, TraversalMedia.Liquid, TraversalMedium.Liquid, firstIndex |
| | 18 | 192 | | if (candidateCount != 1) |
| | 7 | 193 | | return; |
| | | 194 | | |
| | 11 | 195 | | TraversalTransitionType entryType = volumeMedium == TraversalMedium.Gas |
| | 11 | 196 | | ? TraversalTransitionType.Takeoff |
| | 11 | 197 | | : TraversalTransitionType.SwimEntry; |
| | 11 | 198 | | TraversalTransitionType exitType = volumeMedium == TraversalMedium.Gas |
| | 11 | 199 | | ? TraversalTransitionType.Landing |
| | 11 | 200 | | : TraversalTransitionType.SwimExit; |
| | 11 | 201 | | TraversalCapability capability = volumeMedium == TraversalMedium.Gas |
| | 11 | 202 | | ? TraversalCapability.Fly |
| | 11 | 203 | | : TraversalCapability.Swim; |
| | 11 | 204 | | ParsedTokenCell solid = solidIndex == firstIndex ? first : second; |
| | 11 | 205 | | TraversalTransitionLocomotionHints exitHints = |
| | 11 | 206 | | volumeMedium == TraversalMedium.Liquid |
| | 11 | 207 | | && (solid.Cell.Media & TraversalMedia.Liquid) != 0 |
| | 11 | 208 | | && (solid.Cell.Flags & NavigationCellFlags.ClimbSurfaceHint) != 0 |
| | 11 | 209 | | ? TraversalTransitionLocomotionHints.RequestClimb |
| | 11 | 210 | | | TraversalTransitionLocomotionHints.PreserveClimbAfterCompletion |
| | 11 | 211 | | : TraversalTransitionLocomotionHints.None; |
| | 11 | 212 | | builder.AddTransition(CreateTransition(prefix, entryType, solidIndex, TraversalMedium.Solid, volumeIndex, volume |
| | 11 | 213 | | builder.AddTransition(CreateTransition( |
| | 11 | 214 | | prefix, |
| | 11 | 215 | | exitType, |
| | 11 | 216 | | volumeIndex, |
| | 11 | 217 | | volumeMedium, |
| | 11 | 218 | | solidIndex, |
| | 11 | 219 | | TraversalMedium.Solid, |
| | 11 | 220 | | mapId, |
| | 11 | 221 | | capability, |
| | 11 | 222 | | locomotionHints: exitHints)); |
| | 11 | 223 | | } |
| | | 224 | | |
| | | 225 | | private static void TrySelectBoundary( |
| | | 226 | | TraversalMedia firstMedia, |
| | | 227 | | TraversalMedia secondMedia, |
| | | 228 | | TraversalMedia volumeFlag, |
| | | 229 | | TraversalMedium volumeMediumCandidate, |
| | | 230 | | VoxelIndex firstIndex, |
| | | 231 | | VoxelIndex secondIndex, |
| | | 232 | | ref int candidateCount, |
| | | 233 | | ref VoxelIndex solidIndex, |
| | | 234 | | ref VoxelIndex volumeIndex, |
| | | 235 | | ref TraversalMedium volumeMedium) |
| | | 236 | | { |
| | 36 | 237 | | if ((firstMedia & TraversalMedia.Solid) != 0 && (secondMedia & volumeFlag) != 0) |
| | | 238 | | { |
| | 4 | 239 | | candidateCount++; |
| | 4 | 240 | | solidIndex = firstIndex; |
| | 4 | 241 | | volumeIndex = secondIndex; |
| | 4 | 242 | | volumeMedium = volumeMediumCandidate; |
| | | 243 | | } |
| | | 244 | | |
| | 36 | 245 | | if ((secondMedia & TraversalMedia.Solid) != 0 && (firstMedia & volumeFlag) != 0) |
| | | 246 | | { |
| | 7 | 247 | | candidateCount++; |
| | 7 | 248 | | solidIndex = secondIndex; |
| | 7 | 249 | | volumeIndex = firstIndex; |
| | 7 | 250 | | volumeMedium = volumeMediumCandidate; |
| | | 251 | | } |
| | 36 | 252 | | } |
| | | 253 | | |
| | | 254 | | private static void TryAddClimbTransitionPair( |
| | | 255 | | NavigationMapBuilder builder, |
| | | 256 | | ParsedTokenCell first, |
| | | 257 | | ParsedTokenCell second, |
| | | 258 | | VoxelIndex firstIndex, |
| | | 259 | | VoxelIndex secondIndex, |
| | | 260 | | string mapId, |
| | | 261 | | string prefix) |
| | | 262 | | { |
| | 18 | 263 | | bool firstClimb = (first.Cell.Flags & NavigationCellFlags.ClimbSurfaceHint) != 0; |
| | 18 | 264 | | bool secondClimb = (second.Cell.Flags & NavigationCellFlags.ClimbSurfaceHint) != 0; |
| | 18 | 265 | | bool connect = (firstClimb && secondClimb) |
| | 18 | 266 | | || (firstClimb && first.HasMarker && (second.Cell.Media & TraversalMedia.Solid) != 0) |
| | 18 | 267 | | || (secondClimb && second.HasMarker && (first.Cell.Media & TraversalMedia.Solid) != 0); |
| | 18 | 268 | | if (!connect) |
| | 15 | 269 | | return; |
| | | 270 | | |
| | 3 | 271 | | TraversalTransitionLocomotionHints firstToSecondHints = |
| | 3 | 272 | | TraversalTransitionLocomotionHints.RequestClimb |
| | 3 | 273 | | | (secondClimb |
| | 3 | 274 | | ? TraversalTransitionLocomotionHints.PreserveClimbAfterCompletion |
| | 3 | 275 | | : TraversalTransitionLocomotionHints.None); |
| | 3 | 276 | | TraversalTransitionLocomotionHints secondToFirstHints = |
| | 3 | 277 | | TraversalTransitionLocomotionHints.RequestClimb |
| | 3 | 278 | | | (firstClimb |
| | 3 | 279 | | ? TraversalTransitionLocomotionHints.PreserveClimbAfterCompletion |
| | 3 | 280 | | : TraversalTransitionLocomotionHints.None); |
| | 3 | 281 | | builder.AddTransition(CreateTransition( |
| | 3 | 282 | | prefix, |
| | 3 | 283 | | TraversalTransitionType.Climb, |
| | 3 | 284 | | firstIndex, |
| | 3 | 285 | | TraversalMedium.Solid, |
| | 3 | 286 | | secondIndex, |
| | 3 | 287 | | TraversalMedium.Solid, |
| | 3 | 288 | | mapId, |
| | 3 | 289 | | TraversalCapability.Climb, |
| | 3 | 290 | | Fixed64.One, |
| | 3 | 291 | | firstToSecondHints)); |
| | 3 | 292 | | builder.AddTransition(CreateTransition( |
| | 3 | 293 | | prefix, |
| | 3 | 294 | | TraversalTransitionType.Climb, |
| | 3 | 295 | | secondIndex, |
| | 3 | 296 | | TraversalMedium.Solid, |
| | 3 | 297 | | firstIndex, |
| | 3 | 298 | | TraversalMedium.Solid, |
| | 3 | 299 | | mapId, |
| | 3 | 300 | | TraversalCapability.Climb, |
| | 3 | 301 | | Fixed64.One, |
| | 3 | 302 | | secondToFirstHints)); |
| | 3 | 303 | | } |
| | | 304 | | |
| | | 305 | | private static TraversalTransitionDefinition CreateTransition( |
| | | 306 | | string prefix, |
| | | 307 | | TraversalTransitionType type, |
| | | 308 | | VoxelIndex source, |
| | | 309 | | TraversalMedium sourceMedium, |
| | | 310 | | VoxelIndex destination, |
| | | 311 | | TraversalMedium destinationMedium, |
| | | 312 | | string mapId, |
| | | 313 | | TraversalCapability capability, |
| | | 314 | | Fixed64 actionCost = default, |
| | | 315 | | TraversalTransitionLocomotionHints locomotionHints = TraversalTransitionLocomotionHints.None) => |
| | 28 | 316 | | new( |
| | 28 | 317 | | CreateTransitionId(prefix, type, source, sourceMedium, destination, destinationMedium), |
| | 28 | 318 | | type, |
| | 28 | 319 | | source, |
| | 28 | 320 | | sourceMedium, |
| | 28 | 321 | | new NavigationCellAddress(mapId, destination), |
| | 28 | 322 | | destinationMedium, |
| | 28 | 323 | | capability, |
| | 28 | 324 | | actionCost, |
| | 28 | 325 | | locomotionHints: locomotionHints); |
| | | 326 | | |
| | | 327 | | private static string CreateTransitionId( |
| | | 328 | | string prefix, |
| | | 329 | | TraversalTransitionType type, |
| | | 330 | | VoxelIndex source, |
| | | 331 | | TraversalMedium sourceMedium, |
| | | 332 | | VoxelIndex destination, |
| | | 333 | | TraversalMedium destinationMedium) => |
| | 28 | 334 | | string.Concat( |
| | 28 | 335 | | prefix, |
| | 28 | 336 | | ":", type.ToString(), ":", |
| | 28 | 337 | | source.x.ToString(CultureInfo.InvariantCulture), "_", |
| | 28 | 338 | | source.y.ToString(CultureInfo.InvariantCulture), "_", |
| | 28 | 339 | | source.z.ToString(CultureInfo.InvariantCulture), "_", sourceMedium.ToString(), "->", |
| | 28 | 340 | | destination.x.ToString(CultureInfo.InvariantCulture), "_", |
| | 28 | 341 | | destination.y.ToString(CultureInfo.InvariantCulture), "_", |
| | 28 | 342 | | destination.z.ToString(CultureInfo.InvariantCulture), "_", destinationMedium.ToString()); |
| | | 343 | | |
| | | 344 | | private readonly struct ParsedTokenCell |
| | | 345 | | { |
| | | 346 | | internal NavigationTokenLegendEntry Entry { get; } |
| | | 347 | | internal NavigationCell Cell { get; } |
| | | 348 | | internal bool HasMarker { get; } |
| | | 349 | | |
| | | 350 | | internal ParsedTokenCell( |
| | | 351 | | NavigationTokenLegendEntry entry, |
| | | 352 | | NavigationCell cell, |
| | | 353 | | bool hasMarker) |
| | | 354 | | { |
| | 35 | 355 | | Entry = entry; |
| | 35 | 356 | | Cell = cell; |
| | 35 | 357 | | HasMarker = hasMarker; |
| | 35 | 358 | | } |
| | | 359 | | } |
| | | 360 | | } |