| | | 1 | | //======================================================================= |
| | | 2 | | // PathQueryRecord.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 Chronicler; |
| | | 10 | | using FixedMathSharp; |
| | | 11 | | using Trailblazer.Navigation; |
| | | 12 | | |
| | | 13 | | namespace Trailblazer.Pathing; |
| | | 14 | | |
| | | 15 | | /// <summary>Records exact durable graph-query intent.</summary> |
| | | 16 | | public sealed class PathQueryRecord : IRecordable |
| | | 17 | | { |
| | | 18 | | private const int CurrentSchemaVersion = 1; |
| | | 19 | | |
| | | 20 | | private int SchemaVersion; |
| | | 21 | | |
| | | 22 | | private Vector3d StartPosition; |
| | | 23 | | |
| | | 24 | | private string? StartMapId; |
| | | 25 | | |
| | | 26 | | private EndpointResolutionPolicy StartResolution; |
| | | 27 | | |
| | | 28 | | private Fixed64 StartMaxResolutionDistance; |
| | | 29 | | |
| | | 30 | | private Vector3d EndPosition; |
| | | 31 | | |
| | | 32 | | private string? EndMapId; |
| | | 33 | | |
| | | 34 | | private EndpointResolutionPolicy EndResolution; |
| | | 35 | | |
| | | 36 | | private Fixed64 EndMaxResolutionDistance; |
| | | 37 | | |
| | 46 | 38 | | private NavigationAgentProfileRecord Agent = new(); |
| | | 39 | | |
| | | 40 | | private string? AreaPolicyId; |
| | | 41 | | |
| | | 42 | | private long AreaPolicyRevision; |
| | | 43 | | |
| | | 44 | | private TraversalMedium StartMedium; |
| | | 45 | | |
| | | 46 | | private TraversalMedia TargetMedia; |
| | | 47 | | |
| | | 48 | | private PathAlgorithm Algorithm; |
| | | 49 | | |
| | | 50 | | private int MaxLookupProbes; |
| | | 51 | | |
| | | 52 | | private int MaxEndpointCandidates; |
| | | 53 | | |
| | | 54 | | private int MaxExpandedNodes; |
| | | 55 | | |
| | | 56 | | private int MaxEvaluatedEdges; |
| | | 57 | | |
| | | 58 | | private int MaxConnectionLegs; |
| | | 59 | | |
| | | 60 | | private int MaxTransitionCandidates; |
| | | 61 | | |
| | | 62 | | private int MaxTransitionPairs; |
| | | 63 | | |
| | | 64 | | private int MaxStagedLegAttempts; |
| | | 65 | | |
| | | 66 | | private int MaxTraceIntervals; |
| | | 67 | | |
| | | 68 | | private int MaxCoveredVoxelIntervals; |
| | | 69 | | |
| | | 70 | | private int MaxSimplificationRays; |
| | | 71 | | |
| | | 72 | | private bool AllowTransitions; |
| | | 73 | | |
| | | 74 | | private Fixed64 ExtraIntegrationCost; |
| | | 75 | | |
| | | 76 | | /// <summary>Creates an empty shell for a populate-existing-instance Chronicler load.</summary> |
| | 6 | 77 | | public PathQueryRecord() |
| | | 78 | | { |
| | 6 | 79 | | } |
| | | 80 | | |
| | | 81 | | /// <summary>Creates a record containing one exact immutable query.</summary> |
| | 80 | 82 | | public PathQueryRecord(PathQuery query) => Capture(query); |
| | | 83 | | |
| | | 84 | | /// <summary>Gets the exact query after construction or a successful load.</summary> |
| | | 85 | | public PathQuery? Query { get; private set; } |
| | | 86 | | |
| | | 87 | | private void Capture(PathQuery query) |
| | | 88 | | { |
| | 62 | 89 | | SchemaVersion = CurrentSchemaVersion; |
| | 62 | 90 | | PathQuery value = query; |
| | 62 | 91 | | Query = value; |
| | 62 | 92 | | StartPosition = value.Start.Position; |
| | 62 | 93 | | StartMapId = value.Start.MapId; |
| | 62 | 94 | | StartResolution = value.Start.Resolution; |
| | 62 | 95 | | StartMaxResolutionDistance = value.Start.MaxResolutionDistance; |
| | 62 | 96 | | EndPosition = value.End.Position; |
| | 62 | 97 | | EndMapId = value.End.MapId; |
| | 62 | 98 | | EndResolution = value.End.Resolution; |
| | 62 | 99 | | EndMaxResolutionDistance = value.End.MaxResolutionDistance; |
| | 62 | 100 | | Agent.Capture(value.Agent); |
| | 62 | 101 | | AreaPolicyId = value.AreaPolicy.PolicyId; |
| | 62 | 102 | | AreaPolicyRevision = value.AreaPolicy.Revision; |
| | 62 | 103 | | StartMedium = value.Traversal.StartMedium; |
| | 62 | 104 | | TargetMedia = value.Traversal.TargetMedia; |
| | 62 | 105 | | Algorithm = value.Algorithm; |
| | 62 | 106 | | MaxLookupProbes = value.Budget.MaxLookupProbes; |
| | 62 | 107 | | MaxEndpointCandidates = value.Budget.MaxEndpointCandidates; |
| | 62 | 108 | | MaxExpandedNodes = value.Budget.MaxExpandedNodes; |
| | 62 | 109 | | MaxEvaluatedEdges = value.Budget.MaxEvaluatedEdges; |
| | 62 | 110 | | MaxConnectionLegs = value.Budget.MaxConnectionLegs; |
| | 62 | 111 | | MaxTransitionCandidates = value.Budget.MaxTransitionCandidates; |
| | 62 | 112 | | MaxTransitionPairs = value.Budget.MaxTransitionPairs; |
| | 62 | 113 | | MaxStagedLegAttempts = value.Budget.MaxStagedLegAttempts; |
| | 62 | 114 | | MaxTraceIntervals = value.Budget.MaxTraceIntervals; |
| | 62 | 115 | | MaxCoveredVoxelIntervals = value.Budget.MaxCoveredVoxelIntervals; |
| | 62 | 116 | | MaxSimplificationRays = value.Budget.MaxSimplificationRays; |
| | 62 | 117 | | AllowTransitions = value.AllowTransitions; |
| | 62 | 118 | | ExtraIntegrationCost = value.FlowField.ExtraIntegrationCost; |
| | 62 | 119 | | } |
| | | 120 | | |
| | | 121 | | private bool TryCreateQuery(out PathQuery? query) |
| | | 122 | | { |
| | 22 | 123 | | query = null; |
| | 22 | 124 | | if (SchemaVersion != CurrentSchemaVersion |
| | 22 | 125 | | || Algorithm is not PathAlgorithm.AStar and not PathAlgorithm.FlowField |
| | 22 | 126 | | || !TraversalTransitionDefinition.IsKnownMedium(StartMedium) |
| | 22 | 127 | | || TargetMedia == TraversalMedia.None |
| | 22 | 128 | | || (TargetMedia & ~NavigationCell.KnownMedia) != 0 |
| | 22 | 129 | | || string.IsNullOrWhiteSpace(AreaPolicyId) |
| | 22 | 130 | | || !Agent.TryCreate(out NavigationAgentProfile profile)) |
| | | 131 | | { |
| | 16 | 132 | | return false; |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | try |
| | | 136 | | { |
| | 6 | 137 | | query = new PathQuery( |
| | 6 | 138 | | new NavigationEndpoint( |
| | 6 | 139 | | StartPosition, |
| | 6 | 140 | | StartMapId, |
| | 6 | 141 | | StartResolution, |
| | 6 | 142 | | StartMaxResolutionDistance), |
| | 6 | 143 | | new NavigationEndpoint( |
| | 6 | 144 | | EndPosition, |
| | 6 | 145 | | EndMapId, |
| | 6 | 146 | | EndResolution, |
| | 6 | 147 | | EndMaxResolutionDistance), |
| | 6 | 148 | | profile, |
| | 6 | 149 | | new NavigationAreaPolicyKey(AreaPolicyId!, AreaPolicyRevision), |
| | 6 | 150 | | new TraversalIntent(StartMedium, TargetMedia), |
| | 6 | 151 | | Algorithm, |
| | 6 | 152 | | new NavigationWorkBudget( |
| | 6 | 153 | | MaxLookupProbes, |
| | 6 | 154 | | MaxEndpointCandidates, |
| | 6 | 155 | | MaxExpandedNodes, |
| | 6 | 156 | | MaxEvaluatedEdges, |
| | 6 | 157 | | MaxConnectionLegs, |
| | 6 | 158 | | MaxTransitionCandidates, |
| | 6 | 159 | | MaxTransitionPairs, |
| | 6 | 160 | | MaxStagedLegAttempts, |
| | 6 | 161 | | MaxTraceIntervals, |
| | 6 | 162 | | MaxCoveredVoxelIntervals, |
| | 6 | 163 | | MaxSimplificationRays), |
| | 6 | 164 | | AllowTransitions, |
| | 6 | 165 | | Algorithm == PathAlgorithm.FlowField |
| | 6 | 166 | | ? new FlowFieldQueryOptions(ExtraIntegrationCost) |
| | 6 | 167 | | : default); |
| | 4 | 168 | | return true; |
| | | 169 | | } |
| | 2 | 170 | | catch (ArgumentException) |
| | | 171 | | { |
| | 2 | 172 | | return false; |
| | | 173 | | } |
| | 6 | 174 | | } |
| | | 175 | | |
| | | 176 | | /// <inheritdoc /> |
| | | 177 | | public void RecordData(IChronicler chronicler) |
| | | 178 | | { |
| | 46 | 179 | | bool isLoading = chronicler.Mode == SerializationMode.Loading; |
| | 46 | 180 | | if (!isLoading) |
| | | 181 | | { |
| | 24 | 182 | | PathQuery? query = Query; |
| | 24 | 183 | | SwiftThrowHelper.ThrowIfTrue( |
| | 24 | 184 | | !query.HasValue, |
| | 24 | 185 | | message: "PathQueryRecord must contain a query before it can be serialized."); |
| | | 186 | | |
| | 22 | 187 | | Capture(query.Value); |
| | | 188 | | } |
| | | 189 | | |
| | 44 | 190 | | RecordValues.Look(chronicler, ref SchemaVersion, "SchemaVersion", 0); |
| | 44 | 191 | | RecordValues.Look(chronicler, ref StartPosition, "StartPosition", Vector3d.Zero); |
| | 44 | 192 | | RecordValues.Look(chronicler, ref StartMapId, "StartMapId", null); |
| | 44 | 193 | | RecordValues.Look(chronicler, ref StartResolution, "StartResolution", EndpointResolutionPolicy.Strict); |
| | 44 | 194 | | RecordValues.Look(chronicler, ref StartMaxResolutionDistance, "StartMaxResolutionDistance", Fixed64.Zero); |
| | 44 | 195 | | RecordValues.Look(chronicler, ref EndPosition, "EndPosition", Vector3d.Zero); |
| | 44 | 196 | | RecordValues.Look(chronicler, ref EndMapId, "EndMapId", null); |
| | 44 | 197 | | RecordValues.Look(chronicler, ref EndResolution, "EndResolution", EndpointResolutionPolicy.Strict); |
| | 44 | 198 | | RecordValues.Look(chronicler, ref EndMaxResolutionDistance, "EndMaxResolutionDistance", Fixed64.Zero); |
| | 44 | 199 | | RecordDeep.Look(chronicler, ref Agent, "Agent"); |
| | 44 | 200 | | RecordValues.Look(chronicler, ref AreaPolicyId, "AreaPolicyId", null); |
| | 44 | 201 | | RecordValues.Look(chronicler, ref AreaPolicyRevision, "AreaPolicyRevision", 0L); |
| | 44 | 202 | | RecordValues.Look(chronicler, ref StartMedium, "StartMedium", TraversalMedium.Unknown); |
| | 44 | 203 | | RecordValues.Look(chronicler, ref TargetMedia, "TargetMedia", TraversalMedia.None); |
| | 44 | 204 | | RecordValues.Look(chronicler, ref Algorithm, "Algorithm", PathAlgorithm.AStar); |
| | 44 | 205 | | RecordValues.Look(chronicler, ref MaxLookupProbes, "MaxLookupProbes", 0); |
| | 44 | 206 | | RecordValues.Look(chronicler, ref MaxEndpointCandidates, "MaxEndpointCandidates", 0); |
| | 44 | 207 | | RecordValues.Look(chronicler, ref MaxExpandedNodes, "MaxExpandedNodes", 0); |
| | 44 | 208 | | RecordValues.Look(chronicler, ref MaxEvaluatedEdges, "MaxEvaluatedEdges", 0); |
| | 44 | 209 | | RecordValues.Look(chronicler, ref MaxConnectionLegs, "MaxConnectionLegs", 0); |
| | 44 | 210 | | RecordValues.Look(chronicler, ref MaxTransitionCandidates, "MaxTransitionCandidates", 0); |
| | 44 | 211 | | RecordValues.Look(chronicler, ref MaxTransitionPairs, "MaxTransitionPairs", 0); |
| | 44 | 212 | | RecordValues.Look(chronicler, ref MaxStagedLegAttempts, "MaxStagedLegAttempts", 0); |
| | 44 | 213 | | RecordValues.Look(chronicler, ref MaxTraceIntervals, "MaxTraceIntervals", 0); |
| | 44 | 214 | | RecordValues.Look(chronicler, ref MaxCoveredVoxelIntervals, "MaxCoveredVoxelIntervals", 0); |
| | 44 | 215 | | RecordValues.Look(chronicler, ref MaxSimplificationRays, "MaxSimplificationRays", 0); |
| | 44 | 216 | | RecordValues.Look(chronicler, ref AllowTransitions, "AllowTransitions", false); |
| | 44 | 217 | | RecordValues.Look(chronicler, ref ExtraIntegrationCost, "ExtraIntegrationCost", Fixed64.Zero); |
| | | 218 | | |
| | 44 | 219 | | if (isLoading) |
| | | 220 | | { |
| | 22 | 221 | | SwiftThrowHelper.ThrowIfTrue( |
| | 22 | 222 | | !TryCreateQuery(out PathQuery? query) || !query.HasValue, |
| | 22 | 223 | | message: "Serialized PathQuery is missing, invalid, or unsupported."); |
| | | 224 | | |
| | 4 | 225 | | Query = query.Value; |
| | | 226 | | } |
| | 26 | 227 | | } |
| | | 228 | | } |