| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationAreaRule.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 one immutable query-specific admission and enter-cost rule. |
| | | 16 | | /// </summary> |
| | | 17 | | public readonly struct NavigationAreaRule : IEquatable<NavigationAreaRule> |
| | | 18 | | { |
| | | 19 | | private readonly bool _isDenied; |
| | | 20 | | |
| | | 21 | | /// <summary>Creates an immutable navigation-area rule.</summary> |
| | | 22 | | public NavigationAreaRule(bool isAllowed, Fixed64 additionalEnterCost) |
| | | 23 | | { |
| | 733 | 24 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange( |
| | 733 | 25 | | additionalEnterCost < Fixed64.Zero, |
| | 733 | 26 | | actualValue: null, |
| | 733 | 27 | | nameof(additionalEnterCost), |
| | 733 | 28 | | "Additional enter cost must be non-negative."); |
| | | 29 | | |
| | 732 | 30 | | _isDenied = !isAllowed; |
| | 732 | 31 | | AdditionalEnterCost = additionalEnterCost; |
| | 732 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary>Gets whether the query may enter this area.</summary> |
| | 228456 | 35 | | public bool IsAllowed => !_isDenied; |
| | | 36 | | |
| | | 37 | | /// <summary>Gets the non-negative query-specific enter-cost surcharge.</summary> |
| | | 38 | | public Fixed64 AdditionalEnterCost { get; } |
| | | 39 | | |
| | | 40 | | /// <inheritdoc/> |
| | | 41 | | public bool Equals(NavigationAreaRule other) => |
| | 16 | 42 | | _isDenied == other._isDenied && AdditionalEnterCost == other.AdditionalEnterCost; |
| | | 43 | | |
| | | 44 | | /// <inheritdoc/> |
| | 2 | 45 | | public override bool Equals(object? obj) => obj is NavigationAreaRule other && Equals(other); |
| | | 46 | | |
| | | 47 | | /// <inheritdoc/> |
| | | 48 | | public override int GetHashCode() => |
| | 2 | 49 | | SwiftHashTools.CombineHashCodes(_isDenied ? 1 : 0, AdditionalEnterCost.GetHashCode()); |
| | | 50 | | |
| | | 51 | | /// <summary>Tests two rules for equality.</summary> |
| | 1 | 52 | | public static bool operator ==(NavigationAreaRule left, NavigationAreaRule right) => left.Equals(right); |
| | | 53 | | |
| | | 54 | | /// <summary>Tests two rules for inequality.</summary> |
| | 1 | 55 | | public static bool operator !=(NavigationAreaRule left, NavigationAreaRule right) => !left.Equals(right); |
| | | 56 | | } |