| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationCell.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 FixedMathSharp; |
| | | 10 | | using SwiftCollections.Utility; |
| | | 11 | | |
| | | 12 | | namespace Trailblazer.Pathing; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Stores the complete immutable semantic payload for one authored navigation cell. |
| | | 16 | | /// </summary> |
| | | 17 | | public readonly struct NavigationCell : IEquatable<NavigationCell> |
| | | 18 | | { |
| | | 19 | | internal const TraversalMedia KnownMedia = |
| | | 20 | | TraversalMedia.Solid | TraversalMedia.Gas | TraversalMedia.Liquid; |
| | | 21 | | |
| | | 22 | | internal const TraversalCapability KnownCapabilities = |
| | | 23 | | TraversalCapability.Jump |
| | | 24 | | | TraversalCapability.Climb |
| | | 25 | | | TraversalCapability.Swim |
| | | 26 | | | TraversalCapability.Fly |
| | | 27 | | | TraversalCapability.Teleport; |
| | | 28 | | |
| | | 29 | | internal const NavigationCellFlags KnownFlags = |
| | | 30 | | NavigationCellFlags.TransitionSourceHint |
| | | 31 | | | NavigationCellFlags.TransitionDestinationHint |
| | | 32 | | | NavigationCellFlags.ClimbSurfaceHint; |
| | | 33 | | |
| | | 34 | | internal static bool IsKnownMedium(TraversalMedium medium) => |
| | 3799837 | 35 | | medium == TraversalMedium.Solid |
| | 3799837 | 36 | | || medium == TraversalMedium.Gas |
| | 3799837 | 37 | | || medium == TraversalMedium.Liquid; |
| | | 38 | | |
| | 598039 | 39 | | internal static TraversalMedia ToMedia(TraversalMedium medium) => medium switch |
| | 598039 | 40 | | { |
| | 341795 | 41 | | TraversalMedium.Solid => TraversalMedia.Solid, |
| | 134183 | 42 | | TraversalMedium.Gas => TraversalMedia.Gas, |
| | 122060 | 43 | | TraversalMedium.Liquid => TraversalMedia.Liquid, |
| | 1 | 44 | | _ => TraversalMedia.None |
| | 598039 | 45 | | }; |
| | | 46 | | |
| | | 47 | | internal bool SupportsMedium(TraversalMedium medium) => |
| | 560609 | 48 | | (Media & ToMedia(medium)) != 0; |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// The traversal media supported by this cell. |
| | | 52 | | /// </summary> |
| | | 53 | | public TraversalMedia Media { get; } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// The complete set of capabilities an agent must possess to enter this cell. |
| | | 57 | | /// </summary> |
| | | 58 | | public TraversalCapability RequiredCapabilities { get; } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// The stable host-defined navigation area used by query-specific policies. |
| | | 62 | | /// </summary> |
| | | 63 | | public NavigationAreaId Area { get; } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// The non-negative cost charged when an edge enters this cell. |
| | | 67 | | /// </summary> |
| | | 68 | | public Fixed64 EnterCost { get; } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// The inclusive horizontal radius clearance at the cell anchor. |
| | | 72 | | /// </summary> |
| | | 73 | | public Fixed64 RadiusClearance { get; } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// The inclusive vertical clearance above the cell anchor. |
| | | 77 | | /// </summary> |
| | | 78 | | public Fixed64 HeightClearance { get; } |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Optional transition-generation hints. |
| | | 82 | | /// </summary> |
| | | 83 | | public NavigationCellFlags Flags { get; } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Creates a complete authored navigation-cell payload. |
| | | 87 | | /// </summary> |
| | | 88 | | public NavigationCell( |
| | | 89 | | TraversalMedia media, |
| | | 90 | | TraversalCapability requiredCapabilities, |
| | | 91 | | NavigationAreaId area, |
| | | 92 | | Fixed64 enterCost, |
| | | 93 | | Fixed64 radiusClearance, |
| | | 94 | | Fixed64 heightClearance, |
| | | 95 | | NavigationCellFlags flags = NavigationCellFlags.None) |
| | | 96 | | { |
| | 1745 | 97 | | SwiftThrowHelper.ThrowIfArgument( |
| | 1745 | 98 | | media == TraversalMedia.None || (media & ~KnownMedia) != 0, |
| | 1745 | 99 | | nameof(media), |
| | 1745 | 100 | | "Media must contain at least one known traversal-medium bit."); |
| | 1743 | 101 | | SwiftThrowHelper.ThrowIfArgument( |
| | 1743 | 102 | | (requiredCapabilities & ~KnownCapabilities) != 0, |
| | 1743 | 103 | | nameof(requiredCapabilities), |
| | 1743 | 104 | | "Required capabilities contain an unknown bit."); |
| | 1742 | 105 | | SwiftThrowHelper.ThrowIfArgument( |
| | 1742 | 106 | | enterCost < Fixed64.Zero, |
| | 1742 | 107 | | nameof(enterCost), |
| | 1742 | 108 | | "Enter cost must be non-negative."); |
| | 1741 | 109 | | SwiftThrowHelper.ThrowIfArgument( |
| | 1741 | 110 | | radiusClearance < Fixed64.Zero, |
| | 1741 | 111 | | nameof(radiusClearance), |
| | 1741 | 112 | | "Radius clearance must be non-negative."); |
| | 1741 | 113 | | SwiftThrowHelper.ThrowIfArgument( |
| | 1741 | 114 | | heightClearance < Fixed64.Zero, |
| | 1741 | 115 | | nameof(heightClearance), |
| | 1741 | 116 | | "Height clearance must be non-negative."); |
| | 1741 | 117 | | SwiftThrowHelper.ThrowIfArgument( |
| | 1741 | 118 | | (flags & ~KnownFlags) != 0, |
| | 1741 | 119 | | nameof(flags), |
| | 1741 | 120 | | "Cell flags contain an unknown bit."); |
| | | 121 | | |
| | 1741 | 122 | | Media = media; |
| | 1741 | 123 | | RequiredCapabilities = requiredCapabilities; |
| | 1741 | 124 | | Area = area; |
| | 1741 | 125 | | EnterCost = enterCost; |
| | 1741 | 126 | | RadiusClearance = radiusClearance; |
| | 1741 | 127 | | HeightClearance = heightClearance; |
| | 1741 | 128 | | Flags = flags; |
| | 1741 | 129 | | } |
| | | 130 | | |
| | | 131 | | /// <inheritdoc/> |
| | | 132 | | public bool Equals(NavigationCell other) => |
| | 47 | 133 | | Media == other.Media |
| | 47 | 134 | | && RequiredCapabilities == other.RequiredCapabilities |
| | 47 | 135 | | && Area == other.Area |
| | 47 | 136 | | && EnterCost == other.EnterCost |
| | 47 | 137 | | && RadiusClearance == other.RadiusClearance |
| | 47 | 138 | | && HeightClearance == other.HeightClearance |
| | 47 | 139 | | && Flags == other.Flags; |
| | | 140 | | |
| | | 141 | | /// <inheritdoc/> |
| | 23 | 142 | | public override bool Equals(object? obj) => obj is NavigationCell other && Equals(other); |
| | | 143 | | |
| | | 144 | | /// <inheritdoc/> |
| | | 145 | | public override int GetHashCode() |
| | | 146 | | { |
| | 16 | 147 | | int hash = SwiftHashTools.CombineHashCodes((int)Media, (int)RequiredCapabilities); |
| | 16 | 148 | | hash = SwiftHashTools.CombineHashCodes(hash, Area.GetHashCode()); |
| | 16 | 149 | | hash = SwiftHashTools.CombineHashCodes(hash, EnterCost.GetHashCode()); |
| | 16 | 150 | | hash = SwiftHashTools.CombineHashCodes(hash, RadiusClearance.GetHashCode()); |
| | 16 | 151 | | hash = SwiftHashTools.CombineHashCodes(hash, HeightClearance.GetHashCode()); |
| | 16 | 152 | | return SwiftHashTools.CombineHashCodes(hash, (int)Flags); |
| | | 153 | | } |
| | | 154 | | |
| | | 155 | | /// <summary> |
| | | 156 | | /// Tests two cell payloads for value equality. |
| | | 157 | | /// </summary> |
| | 1 | 158 | | public static bool operator ==(NavigationCell left, NavigationCell right) => left.Equals(right); |
| | | 159 | | |
| | | 160 | | /// <summary> |
| | | 161 | | /// Tests two cell payloads for value inequality. |
| | | 162 | | /// </summary> |
| | 1 | 163 | | public static bool operator !=(NavigationCell left, NavigationCell right) => !left.Equals(right); |
| | | 164 | | } |