| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationCellAddress.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 GridForge.Spatial; |
| | | 10 | | using SwiftCollections.Utility; |
| | | 11 | | |
| | | 12 | | namespace Trailblazer.Pathing; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Identifies one durable navigation cell by ordinal map ID and topology-local index. |
| | | 16 | | /// </summary> |
| | | 17 | | public readonly struct NavigationCellAddress : |
| | | 18 | | IEquatable<NavigationCellAddress>, |
| | | 19 | | IComparable<NavigationCellAddress> |
| | | 20 | | { |
| | | 21 | | /// <summary> |
| | | 22 | | /// The stable host-owned map identifier. |
| | | 23 | | /// </summary> |
| | | 24 | | public string MapId { get; } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// The topology-local X/Q, Y/layer, Z/R cell index. |
| | | 28 | | /// </summary> |
| | | 29 | | public VoxelIndex Index { get; } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Creates a durable cell address. |
| | | 33 | | /// </summary> |
| | | 34 | | public NavigationCellAddress(string mapId, VoxelIndex index) |
| | | 35 | | { |
| | 5661840 | 36 | | SwiftThrowHelper.ThrowIfArgument( |
| | 5661840 | 37 | | string.IsNullOrWhiteSpace(mapId), |
| | 5661840 | 38 | | nameof(mapId), |
| | 5661840 | 39 | | "Map ID cannot be null, empty, or whitespace."); |
| | | 40 | | |
| | 5661840 | 41 | | MapId = mapId; |
| | 5661840 | 42 | | Index = index; |
| | 5661840 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <inheritdoc/> |
| | | 46 | | public int CompareTo(NavigationCellAddress other) |
| | | 47 | | { |
| | 993340 | 48 | | int result = StringComparer.Ordinal.Compare(MapId, other.MapId); |
| | 993340 | 49 | | return result != 0 ? result : Index.CompareTo(other.Index); |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <inheritdoc/> |
| | | 53 | | public bool Equals(NavigationCellAddress other) => |
| | 2839518 | 54 | | string.Equals(MapId, other.MapId, StringComparison.Ordinal) |
| | 2839518 | 55 | | && Index.Equals(other.Index); |
| | | 56 | | |
| | | 57 | | /// <inheritdoc/> |
| | | 58 | | public override bool Equals(object? obj) => |
| | 572 | 59 | | obj is NavigationCellAddress other && Equals(other); |
| | | 60 | | |
| | | 61 | | /// <inheritdoc/> |
| | | 62 | | public override int GetHashCode() |
| | | 63 | | { |
| | 504130 | 64 | | int mapHash = MapId == null |
| | 504130 | 65 | | ? 0 |
| | 504130 | 66 | | : SwiftHashTools.GetDeterministicStringEqualityComparer().GetHashCode(MapId); |
| | 504130 | 67 | | return SwiftHashTools.CombineHashCodes(mapHash, Index.GetHashCode()); |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <inheritdoc/> |
| | 2 | 71 | | public override string ToString() => $"{MapId}:{Index}"; |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Tests two addresses for value equality. |
| | | 75 | | /// </summary> |
| | | 76 | | public static bool operator ==(NavigationCellAddress left, NavigationCellAddress right) => |
| | 6586 | 77 | | left.Equals(right); |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Tests two addresses for value inequality. |
| | | 81 | | /// </summary> |
| | | 82 | | public static bool operator !=(NavigationCellAddress left, NavigationCellAddress right) => |
| | 2437 | 83 | | !left.Equals(right); |
| | | 84 | | } |