| | | 1 | | //======================================================================= |
| | | 2 | | // PathQuery.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 complete immutable caller intent for one navigation query. |
| | | 16 | | /// </summary> |
| | | 17 | | public readonly struct PathQuery : IEquatable<PathQuery> |
| | | 18 | | { |
| | | 19 | | /// <summary>Gets the requested start endpoint.</summary> |
| | | 20 | | public NavigationEndpoint Start { get; } |
| | | 21 | | |
| | | 22 | | /// <summary>Gets the requested destination endpoint.</summary> |
| | | 23 | | public NavigationEndpoint End { get; } |
| | | 24 | | |
| | | 25 | | /// <summary>Gets the exact agent profile used to evaluate traversal.</summary> |
| | | 26 | | public NavigationAgentProfile Agent { get; } |
| | | 27 | | |
| | | 28 | | /// <summary>Gets the exact query-specific navigation-area policy identity.</summary> |
| | | 29 | | public NavigationAreaPolicyKey AreaPolicy { get; } |
| | | 30 | | |
| | | 31 | | /// <summary>Gets the exact start medium and allowed target media.</summary> |
| | | 32 | | public TraversalIntent Traversal { get; } |
| | | 33 | | |
| | | 34 | | /// <summary>Gets the selected search algorithm.</summary> |
| | | 35 | | public PathAlgorithm Algorithm { get; } |
| | | 36 | | |
| | | 37 | | /// <summary>Gets the finite work budget shared by the complete query.</summary> |
| | | 38 | | public NavigationWorkBudget Budget { get; } |
| | | 39 | | |
| | | 40 | | /// <summary>Gets whether explicit or generated traversal transitions may be used.</summary> |
| | | 41 | | public bool AllowTransitions { get; } |
| | | 42 | | |
| | | 43 | | /// <summary>Gets the options used when <see cref="Algorithm"/> is <see cref="PathAlgorithm.FlowField"/>.</summary> |
| | | 44 | | public FlowFieldQueryOptions FlowField { get; } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Creates complete immutable navigation query intent. |
| | | 48 | | /// </summary> |
| | | 49 | | /// <exception cref="ArgumentException">Thrown when a nested value is invalid or incompatible with the algorithm.</e |
| | | 50 | | /// <exception cref="ArgumentOutOfRangeException">Thrown when the algorithm is unknown.</exception> |
| | | 51 | | public PathQuery( |
| | | 52 | | NavigationEndpoint start, |
| | | 53 | | NavigationEndpoint end, |
| | | 54 | | NavigationAgentProfile agent, |
| | | 55 | | NavigationAreaPolicyKey areaPolicy, |
| | | 56 | | TraversalIntent traversal, |
| | | 57 | | PathAlgorithm algorithm, |
| | | 58 | | NavigationWorkBudget budget, |
| | | 59 | | bool allowTransitions, |
| | | 60 | | FlowFieldQueryOptions flowField = default) |
| | | 61 | | { |
| | 2031 | 62 | | agent.Validate(nameof(agent)); |
| | 2030 | 63 | | areaPolicy.Validate(nameof(areaPolicy)); |
| | 2030 | 64 | | traversal.Validate(nameof(traversal)); |
| | 2030 | 65 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange( |
| | 2030 | 66 | | algorithm is not PathAlgorithm.AStar and not PathAlgorithm.FlowField, |
| | 2030 | 67 | | (int)algorithm, |
| | 2030 | 68 | | nameof(algorithm), |
| | 2030 | 69 | | "Path algorithm is unknown."); |
| | 2029 | 70 | | SwiftThrowHelper.ThrowIfArgument( |
| | 2029 | 71 | | algorithm == PathAlgorithm.AStar && flowField != default, |
| | 2029 | 72 | | nameof(flowField), |
| | 2029 | 73 | | "A* queries cannot carry flow-field-specific options."); |
| | 2028 | 74 | | SwiftThrowHelper.ThrowIfArgument( |
| | 2028 | 75 | | (traversal.TargetMedia & ~agent.AllowedMedia) != 0, |
| | 2028 | 76 | | nameof(traversal), |
| | 2028 | 77 | | "Target media must be a subset of the agent's allowed media."); |
| | | 78 | | |
| | 2027 | 79 | | Start = start; |
| | 2027 | 80 | | End = end; |
| | 2027 | 81 | | Agent = agent; |
| | 2027 | 82 | | AreaPolicy = areaPolicy; |
| | 2027 | 83 | | Traversal = traversal; |
| | 2027 | 84 | | Algorithm = algorithm; |
| | 2027 | 85 | | Budget = budget; |
| | 2027 | 86 | | AllowTransitions = allowTransitions; |
| | 2027 | 87 | | FlowField = flowField; |
| | 2027 | 88 | | } |
| | | 89 | | |
| | | 90 | | internal PathQuery WithStartState( |
| | | 91 | | Vector3d position, |
| | 87 | 92 | | TraversalMedium startMedium) => new( |
| | 87 | 93 | | new NavigationEndpoint( |
| | 87 | 94 | | position, |
| | 87 | 95 | | Start.MapId, |
| | 87 | 96 | | Start.Resolution, |
| | 87 | 97 | | Start.MaxResolutionDistance), |
| | 87 | 98 | | End, |
| | 87 | 99 | | Agent, |
| | 87 | 100 | | AreaPolicy, |
| | 87 | 101 | | new TraversalIntent(startMedium, Traversal.TargetMedia), |
| | 87 | 102 | | Algorithm, |
| | 87 | 103 | | Budget, |
| | 87 | 104 | | AllowTransitions, |
| | 87 | 105 | | FlowField); |
| | | 106 | | |
| | | 107 | | /// <inheritdoc/> |
| | | 108 | | public bool Equals(PathQuery other) => |
| | 1070 | 109 | | Start == other.Start |
| | 1070 | 110 | | && End == other.End |
| | 1070 | 111 | | && Agent == other.Agent |
| | 1070 | 112 | | && AreaPolicy == other.AreaPolicy |
| | 1070 | 113 | | && Traversal == other.Traversal |
| | 1070 | 114 | | && Algorithm == other.Algorithm |
| | 1070 | 115 | | && Budget == other.Budget |
| | 1070 | 116 | | && AllowTransitions == other.AllowTransitions |
| | 1070 | 117 | | && FlowField == other.FlowField; |
| | | 118 | | |
| | | 119 | | /// <inheritdoc/> |
| | 79 | 120 | | public override bool Equals(object? obj) => obj is PathQuery other && Equals(other); |
| | | 121 | | |
| | | 122 | | /// <inheritdoc/> |
| | | 123 | | public override int GetHashCode() |
| | | 124 | | { |
| | 1432 | 125 | | int hash = SwiftHashTools.CombineHashCodes(Start.GetHashCode(), End.GetHashCode()); |
| | 1432 | 126 | | hash = SwiftHashTools.CombineHashCodes(hash, Agent.GetHashCode()); |
| | 1432 | 127 | | hash = SwiftHashTools.CombineHashCodes(hash, AreaPolicy.GetHashCode()); |
| | 1432 | 128 | | hash = SwiftHashTools.CombineHashCodes(hash, Traversal.GetHashCode()); |
| | 1432 | 129 | | hash = SwiftHashTools.CombineHashCodes(hash, (int)Algorithm); |
| | 1432 | 130 | | hash = SwiftHashTools.CombineHashCodes(hash, Budget.GetHashCode()); |
| | 1432 | 131 | | hash = SwiftHashTools.CombineHashCodes(hash, AllowTransitions ? 1 : 0); |
| | 1432 | 132 | | return SwiftHashTools.CombineHashCodes(hash, FlowField.GetHashCode()); |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | /// <summary>Returns whether two queries have exactly equal intent.</summary> |
| | 991 | 136 | | public static bool operator ==(PathQuery left, PathQuery right) => left.Equals(right); |
| | | 137 | | |
| | | 138 | | /// <summary>Returns whether two queries differ.</summary> |
| | 1 | 139 | | public static bool operator !=(PathQuery left, PathQuery right) => !left.Equals(right); |
| | | 140 | | } |