| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationAgentProfile.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 | | /// Defines immutable geometry and traversal capabilities for a navigation query. |
| | | 16 | | /// </summary> |
| | | 17 | | public readonly struct NavigationAgentProfile : IEquatable<NavigationAgentProfile> |
| | | 18 | | { |
| | | 19 | | private const TraversalCapability KnownCapabilities = |
| | | 20 | | TraversalCapability.Jump |
| | | 21 | | | TraversalCapability.Climb |
| | | 22 | | | TraversalCapability.Swim |
| | | 23 | | | TraversalCapability.Fly |
| | | 24 | | | TraversalCapability.Teleport; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Gets the authoritative body geometry for the agent. |
| | | 28 | | /// </summary> |
| | | 29 | | public KinematicBodyShape Shape { get; } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Gets the maximum upward surface step allowed in world units. |
| | | 33 | | /// </summary> |
| | | 34 | | public Fixed64 MaxStepUp { get; } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Gets the maximum downward surface drop allowed in world units. |
| | | 38 | | /// </summary> |
| | | 39 | | public Fixed64 MaxDropDown { get; } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Gets the non-negative distance at which the agent is considered to have arrived. |
| | | 43 | | /// </summary> |
| | | 44 | | public Fixed64 ArrivalRadius { get; } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Gets the traversal media this agent may enter. |
| | | 48 | | /// </summary> |
| | | 49 | | public TraversalMedia AllowedMedia { get; } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Gets the additional traversal abilities available to the agent. |
| | | 53 | | /// </summary> |
| | | 54 | | public TraversalCapability Capabilities { get; } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Creates an immutable navigation agent profile. |
| | | 58 | | /// </summary> |
| | | 59 | | /// <exception cref="ArgumentException">Thrown when the body shape is invalid.</exception> |
| | | 60 | | /// <exception cref="ArgumentOutOfRangeException">Thrown when a distance is negative or flags contain unknown bits.< |
| | | 61 | | public NavigationAgentProfile( |
| | | 62 | | KinematicBodyShape shape, |
| | | 63 | | Fixed64 maxStepUp, |
| | | 64 | | Fixed64 maxDropDown, |
| | | 65 | | Fixed64 arrivalRadius, |
| | | 66 | | TraversalMedia allowedMedia, |
| | | 67 | | TraversalCapability capabilities) |
| | | 68 | | { |
| | 1614 | 69 | | shape.Validate(nameof(shape)); |
| | 1613 | 70 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange( |
| | 1613 | 71 | | maxStepUp < Fixed64.Zero, |
| | 1613 | 72 | | actualValue: null, |
| | 1613 | 73 | | nameof(maxStepUp), |
| | 1613 | 74 | | "Maximum step-up distance cannot be negative."); |
| | 1612 | 75 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange( |
| | 1612 | 76 | | maxDropDown < Fixed64.Zero, |
| | 1612 | 77 | | actualValue: null, |
| | 1612 | 78 | | nameof(maxDropDown), |
| | 1612 | 79 | | "Maximum drop-down distance cannot be negative."); |
| | 1611 | 80 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange( |
| | 1611 | 81 | | arrivalRadius < Fixed64.Zero, |
| | 1611 | 82 | | actualValue: null, |
| | 1611 | 83 | | nameof(arrivalRadius), |
| | 1611 | 84 | | "Arrival radius cannot be negative."); |
| | 1610 | 85 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange( |
| | 1610 | 86 | | (allowedMedia & ~NavigationCell.KnownMedia) != 0, |
| | 1610 | 87 | | (int)allowedMedia, |
| | 1610 | 88 | | nameof(allowedMedia), |
| | 1610 | 89 | | "Allowed media contains unknown flag bits."); |
| | 1609 | 90 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange( |
| | 1609 | 91 | | (capabilities & ~KnownCapabilities) != 0, |
| | 1609 | 92 | | (int)capabilities, |
| | 1609 | 93 | | nameof(capabilities), |
| | 1609 | 94 | | "Traversal capabilities contain unknown flag bits."); |
| | | 95 | | |
| | 1608 | 96 | | Shape = shape; |
| | 1608 | 97 | | MaxStepUp = maxStepUp; |
| | 1608 | 98 | | MaxDropDown = maxDropDown; |
| | 1608 | 99 | | ArrivalRadius = arrivalRadius; |
| | 1608 | 100 | | AllowedMedia = allowedMedia; |
| | 1608 | 101 | | Capabilities = capabilities; |
| | 1608 | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <inheritdoc/> |
| | | 105 | | public bool Equals(NavigationAgentProfile other) => |
| | 2887 | 106 | | Shape == other.Shape |
| | 2887 | 107 | | && MaxStepUp == other.MaxStepUp |
| | 2887 | 108 | | && MaxDropDown == other.MaxDropDown |
| | 2887 | 109 | | && ArrivalRadius == other.ArrivalRadius |
| | 2887 | 110 | | && AllowedMedia == other.AllowedMedia |
| | 2887 | 111 | | && Capabilities == other.Capabilities; |
| | | 112 | | |
| | | 113 | | /// <inheritdoc/> |
| | 212 | 114 | | public override bool Equals(object? obj) => obj is NavigationAgentProfile other && Equals(other); |
| | | 115 | | |
| | | 116 | | /// <inheritdoc/> |
| | | 117 | | public override int GetHashCode() |
| | | 118 | | { |
| | 3372 | 119 | | int hash = SwiftHashTools.CombineHashCodes(Shape.GetHashCode(), MaxStepUp.GetHashCode()); |
| | 3372 | 120 | | hash = SwiftHashTools.CombineHashCodes(hash, MaxDropDown.GetHashCode()); |
| | 3372 | 121 | | hash = SwiftHashTools.CombineHashCodes(hash, ArrivalRadius.GetHashCode()); |
| | 3372 | 122 | | hash = SwiftHashTools.CombineHashCodes(hash, (int)AllowedMedia); |
| | 3372 | 123 | | return SwiftHashTools.CombineHashCodes(hash, (int)Capabilities); |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | /// <summary> |
| | | 127 | | /// Returns whether two profiles have exactly equal values. |
| | | 128 | | /// </summary> |
| | 2498 | 129 | | public static bool operator ==(NavigationAgentProfile left, NavigationAgentProfile right) => left.Equals(right); |
| | | 130 | | |
| | | 131 | | /// <summary> |
| | | 132 | | /// Returns whether two profiles differ. |
| | | 133 | | /// </summary> |
| | 172 | 134 | | public static bool operator !=(NavigationAgentProfile left, NavigationAgentProfile right) => !left.Equals(right); |
| | | 135 | | |
| | 732 | 136 | | internal bool IsValid => Shape.IsValid; |
| | | 137 | | |
| | | 138 | | internal void Validate(string parameterName) |
| | | 139 | | { |
| | 25174 | 140 | | Shape.Validate(parameterName); |
| | 25173 | 141 | | } |
| | | 142 | | } |