| | | 1 | | using SwiftCollections; |
| | | 2 | | using System; |
| | | 3 | | using System.Runtime.CompilerServices; |
| | | 4 | | |
| | | 5 | | namespace Trailblazer.Pathing; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Maps authoring tokens into chart cells and generated transition media. |
| | | 9 | | /// </summary> |
| | | 10 | | public sealed class TraversalLegend |
| | | 11 | | { |
| | 46 | 12 | | private readonly SwiftDictionary<string, TraversalLegendEntry> _entries = |
| | 46 | 13 | | new(8, StringComparer.Ordinal); |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Creates a built-in traversal legend with predefined token mappings. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <returns>A traversal legend with built-in entries.</returns> |
| | | 19 | | public static TraversalLegend CreateBuiltIn() |
| | | 20 | | { |
| | 45 | 21 | | var legend = new TraversalLegend(); |
| | | 22 | | // These tokens intentionally contribute no chart traversal and no generated transition anchor. |
| | 45 | 23 | | legend.Register(string.Empty, TraversalLegendEntry.SkipCell()); |
| | 45 | 24 | | legend.Register(".", TraversalLegendEntry.SkipCell()); |
| | 45 | 25 | | legend.Register("X", TraversalLegendEntry.SkipCell()); |
| | 45 | 26 | | legend.Register("S", TraversalLegendEntry.Solid(NavigationChartCell.Solid)); |
| | 45 | 27 | | legend.Register("SC", new TraversalLegendEntry(new NavigationChartCell( |
| | 45 | 28 | | TraversalMedia.Solid, |
| | 45 | 29 | | flags: NavigationChartCellFlags.ClimbSurfaceHint))); |
| | 45 | 30 | | legend.Register("G", TraversalLegendEntry.Gas()); |
| | 45 | 31 | | legend.Register("L", TraversalLegendEntry.Liquid()); |
| | 45 | 32 | | legend.Register("LC", new TraversalLegendEntry( |
| | 45 | 33 | | new NavigationChartCell( |
| | 45 | 34 | | TraversalMedia.Solid | TraversalMedia.Liquid, |
| | 45 | 35 | | flags: NavigationChartCellFlags.ClimbSurfaceHint), |
| | 45 | 36 | | TraversalMedia.Solid | TraversalMedia.Liquid)); |
| | 45 | 37 | | legend.Register("SG", new TraversalLegendEntry( |
| | 45 | 38 | | NavigationChartCell.SolidGas, |
| | 45 | 39 | | TraversalMedia.Solid | TraversalMedia.Gas)); |
| | 45 | 40 | | legend.Register("SL", new TraversalLegendEntry( |
| | 45 | 41 | | NavigationChartCell.SolidLiquid, |
| | 45 | 42 | | TraversalMedia.Solid | TraversalMedia.Liquid)); |
| | 45 | 43 | | return legend; |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Registers a new token mapping in the legend. |
| | | 48 | | /// Tokens are normalized by trimming whitespace, and cannot include the transition marker character '!'. |
| | | 49 | | /// </summary> |
| | | 50 | | /// <param name="token">The token to register.</param> |
| | | 51 | | /// <param name="entry">The legend entry associated with the token.</param> |
| | | 52 | | /// <returns>True if the token was successfully registered; false if the token already exists in the legend.</return |
| | | 53 | | /// <exception cref="ArgumentException">Thrown if the token contains invalid characters.</exception> |
| | | 54 | | public bool Register(string token, TraversalLegendEntry entry) |
| | | 55 | | { |
| | 454 | 56 | | string normalizedToken = NormalizeToken(token); |
| | 454 | 57 | | if (normalizedToken.Contains('!')) |
| | 1 | 58 | | throw new ArgumentException("Legend tokens cannot include transition marker characters.", nameof(token)); |
| | | 59 | | |
| | 453 | 60 | | if (_entries.ContainsKey(normalizedToken)) |
| | 1 | 61 | | return false; |
| | | 62 | | |
| | 452 | 63 | | _entries.Add(normalizedToken, entry); |
| | 452 | 64 | | return true; |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Attempts to retrieve the legend entry associated with the specified token. |
| | | 69 | | /// </summary> |
| | | 70 | | /// <param name="token">The token to look up in the legend.</param> |
| | | 71 | | /// <param name="entry">When this method returns, contains the legend entry associated with the specified token, if |
| | | 72 | | /// <returns>True if the token was found in the legend; otherwise, false.</returns> |
| | | 73 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 74 | | public bool TryGetEntry(string token, out TraversalLegendEntry entry) => |
| | 122 | 75 | | _entries.TryGetValue(NormalizeToken(token), out entry); |
| | | 76 | | |
| | | 77 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 576 | 78 | | private static string NormalizeToken(string token) => token?.Trim() ?? string.Empty; |
| | | 79 | | } |