| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationConnection.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.Collections.Generic; |
| | | 10 | | using System.Collections.ObjectModel; |
| | | 11 | | using FixedMathSharp; |
| | | 12 | | using GridForge.Spatial; |
| | | 13 | | using SwiftCollections.Utility; |
| | | 14 | | |
| | | 15 | | namespace Trailblazer.Pathing; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Describes one directed, source-map-owned physical connection or shortcut. |
| | | 19 | | /// </summary> |
| | | 20 | | public sealed class NavigationConnection : IEquatable<NavigationConnection> |
| | | 21 | | { |
| | | 22 | | private readonly NavigationCellAddress[] _witnesses; |
| | | 23 | | private readonly ReadOnlyCollection<NavigationCellAddress> _witnessView; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// The stable map-local connection identifier. |
| | | 27 | | /// </summary> |
| | | 28 | | public string Id { get; } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// The local source-cell index in the owning map. |
| | | 32 | | /// </summary> |
| | | 33 | | public VoxelIndex SourceIndex { get; } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// The durable destination-cell address. |
| | | 37 | | /// </summary> |
| | | 38 | | public NavigationCellAddress Destination { get; } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// The exact world-space point where traversal leaves the source cell. |
| | | 42 | | /// </summary> |
| | | 43 | | public Vector3d EntryAnchor { get; } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// The exact world-space point where traversal enters the destination cell. |
| | | 47 | | /// </summary> |
| | | 48 | | public Vector3d ExitAnchor { get; } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// The inclusive horizontal radius clearance through the complete connection corridor. |
| | | 52 | | /// </summary> |
| | | 53 | | public Fixed64 PortalRadiusClearance { get; } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// The inclusive vertical clearance through the complete connection corridor. |
| | | 57 | | /// </summary> |
| | | 58 | | public Fixed64 PortalHeightClearance { get; } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// The cells whose presence, blockage, and clearance certify this connection. |
| | | 62 | | /// </summary> |
| | 66816 | 63 | | public IReadOnlyList<NavigationCellAddress> Witnesses => _witnessView; |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// The non-negative surcharge added after geometric travel cost. |
| | | 67 | | /// </summary> |
| | | 68 | | public Fixed64 AdditionalCost { get; } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Indicates that the complete connection cost certifies a Euclidean lower bound. |
| | | 72 | | /// </summary> |
| | | 73 | | public bool IsLowerBoundCertified { get; } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Creates one complete directed connection definition. |
| | | 77 | | /// </summary> |
| | 4550 | 78 | | public NavigationConnection( |
| | 4550 | 79 | | string id, |
| | 4550 | 80 | | VoxelIndex sourceIndex, |
| | 4550 | 81 | | NavigationCellAddress destination, |
| | 4550 | 82 | | Vector3d entryAnchor, |
| | 4550 | 83 | | Vector3d exitAnchor, |
| | 4550 | 84 | | Fixed64 portalRadiusClearance, |
| | 4550 | 85 | | Fixed64 portalHeightClearance, |
| | 4550 | 86 | | IEnumerable<NavigationCellAddress>? witnesses = null, |
| | 4550 | 87 | | Fixed64 additionalCost = default, |
| | 4550 | 88 | | bool isLowerBoundCertified = false) |
| | | 89 | | { |
| | 4550 | 90 | | SwiftThrowHelper.ThrowIfArgument( |
| | 4550 | 91 | | string.IsNullOrWhiteSpace(id), |
| | 4550 | 92 | | nameof(id), |
| | 4550 | 93 | | "Connection ID cannot be null, empty, or whitespace."); |
| | 4550 | 94 | | SwiftThrowHelper.ThrowIfArgument( |
| | 4550 | 95 | | portalRadiusClearance < Fixed64.Zero, |
| | 4550 | 96 | | nameof(portalRadiusClearance), |
| | 4550 | 97 | | "Portal radius clearance must be non-negative."); |
| | 4550 | 98 | | SwiftThrowHelper.ThrowIfArgument( |
| | 4550 | 99 | | portalHeightClearance <= Fixed64.Zero, |
| | 4550 | 100 | | nameof(portalHeightClearance), |
| | 4550 | 101 | | "Portal height clearance must be positive."); |
| | 4549 | 102 | | SwiftThrowHelper.ThrowIfArgument( |
| | 4549 | 103 | | additionalCost < Fixed64.Zero, |
| | 4549 | 104 | | nameof(additionalCost), |
| | 4549 | 105 | | "Additional cost must be non-negative."); |
| | 4549 | 106 | | SwiftThrowHelper.ThrowIfArgument( |
| | 4549 | 107 | | string.IsNullOrWhiteSpace(destination.MapId), |
| | 4549 | 108 | | nameof(destination), |
| | 4549 | 109 | | "Destination must contain a valid map ID."); |
| | | 110 | | |
| | 4549 | 111 | | Id = id; |
| | 4549 | 112 | | SourceIndex = sourceIndex; |
| | 4549 | 113 | | Destination = destination; |
| | 4549 | 114 | | EntryAnchor = entryAnchor; |
| | 4549 | 115 | | ExitAnchor = exitAnchor; |
| | 4549 | 116 | | PortalRadiusClearance = portalRadiusClearance; |
| | 4549 | 117 | | PortalHeightClearance = portalHeightClearance; |
| | 4549 | 118 | | AdditionalCost = additionalCost; |
| | 4549 | 119 | | IsLowerBoundCertified = isLowerBoundCertified; |
| | 4549 | 120 | | _witnesses = CopyWitnesses(witnesses); |
| | 10378 | 121 | | for (int i = 0; i < _witnesses.Length; i++) |
| | | 122 | | { |
| | 640 | 123 | | SwiftThrowHelper.ThrowIfArgument( |
| | 640 | 124 | | string.IsNullOrWhiteSpace(_witnesses[i].MapId), |
| | 640 | 125 | | nameof(witnesses), |
| | 640 | 126 | | "Every witness must contain a valid map ID."); |
| | | 127 | | } |
| | 4549 | 128 | | _witnessView = Array.AsReadOnly(_witnesses); |
| | 4549 | 129 | | } |
| | | 130 | | |
| | | 131 | | /// <inheritdoc/> |
| | | 132 | | public bool Equals(NavigationConnection? other) |
| | | 133 | | { |
| | 289 | 134 | | if (ReferenceEquals(this, other)) |
| | 9 | 135 | | return true; |
| | 280 | 136 | | if (other is null |
| | 280 | 137 | | || !string.Equals(Id, other.Id, StringComparison.Ordinal) |
| | 280 | 138 | | || !SourceIndex.Equals(other.SourceIndex) |
| | 280 | 139 | | || !Destination.Equals(other.Destination) |
| | 280 | 140 | | || EntryAnchor != other.EntryAnchor |
| | 280 | 141 | | || ExitAnchor != other.ExitAnchor |
| | 280 | 142 | | || PortalRadiusClearance != other.PortalRadiusClearance |
| | 280 | 143 | | || PortalHeightClearance != other.PortalHeightClearance |
| | 280 | 144 | | || AdditionalCost != other.AdditionalCost |
| | 280 | 145 | | || IsLowerBoundCertified != other.IsLowerBoundCertified |
| | 280 | 146 | | || _witnesses.Length != other._witnesses.Length) |
| | | 147 | | { |
| | 35 | 148 | | return false; |
| | | 149 | | } |
| | | 150 | | |
| | 496 | 151 | | for (int i = 0; i < _witnesses.Length; i++) |
| | | 152 | | { |
| | 4 | 153 | | if (!_witnesses[i].Equals(other._witnesses[i])) |
| | 1 | 154 | | return false; |
| | | 155 | | } |
| | | 156 | | |
| | 244 | 157 | | return true; |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | /// <inheritdoc/> |
| | 13 | 161 | | public override bool Equals(object? obj) => Equals(obj as NavigationConnection); |
| | | 162 | | |
| | | 163 | | /// <inheritdoc/> |
| | | 164 | | public override int GetHashCode() |
| | | 165 | | { |
| | 6 | 166 | | int idHash = SwiftHashTools.GetDeterministicStringEqualityComparer().GetHashCode(Id); |
| | 6 | 167 | | int hash = SwiftHashTools.CombineHashCodes(idHash, SourceIndex.GetHashCode()); |
| | 6 | 168 | | hash = SwiftHashTools.CombineHashCodes(hash, Destination.GetHashCode()); |
| | 6 | 169 | | hash = SwiftHashTools.CombineHashCodes(hash, EntryAnchor.GetHashCode()); |
| | 6 | 170 | | hash = SwiftHashTools.CombineHashCodes(hash, ExitAnchor.GetHashCode()); |
| | 6 | 171 | | hash = SwiftHashTools.CombineHashCodes(hash, PortalRadiusClearance.GetHashCode()); |
| | 6 | 172 | | hash = SwiftHashTools.CombineHashCodes(hash, PortalHeightClearance.GetHashCode()); |
| | 6 | 173 | | hash = SwiftHashTools.CombineHashCodes(hash, AdditionalCost.GetHashCode()); |
| | 6 | 174 | | hash = SwiftHashTools.CombineHashCodes(hash, IsLowerBoundCertified ? 1 : 0); |
| | 20 | 175 | | for (int i = 0; i < _witnesses.Length; i++) |
| | 4 | 176 | | hash = SwiftHashTools.CombineHashCodes(hash, _witnesses[i].GetHashCode()); |
| | 6 | 177 | | return hash; |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | private static NavigationCellAddress[] CopyWitnesses( |
| | | 181 | | IEnumerable<NavigationCellAddress>? witnesses) |
| | | 182 | | { |
| | 4549 | 183 | | if (witnesses == null) |
| | 4335 | 184 | | return Array.Empty<NavigationCellAddress>(); |
| | | 185 | | |
| | 214 | 186 | | if (witnesses is ICollection<NavigationCellAddress> collection) |
| | | 187 | | { |
| | 202 | 188 | | var copy = new NavigationCellAddress[collection.Count]; |
| | 202 | 189 | | collection.CopyTo(copy, 0); |
| | 202 | 190 | | return copy; |
| | | 191 | | } |
| | | 192 | | |
| | 12 | 193 | | var values = new SwiftCollections.SwiftList<NavigationCellAddress>(); |
| | 72 | 194 | | foreach (NavigationCellAddress witness in witnesses) |
| | 24 | 195 | | values.Add(witness); |
| | 12 | 196 | | return values.ToArray(); |
| | | 197 | | } |
| | | 198 | | } |