| | | 1 | | using SwiftCollections; |
| | | 2 | | using System; |
| | | 3 | | |
| | | 4 | | namespace Trailblazer.Pathing; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Flattens staged hybrid route steps into a single waypoint stream for cached hybrid guides. |
| | | 8 | | /// </summary> |
| | | 9 | | internal static class HybridWaypointFlattener |
| | | 10 | | { |
| | | 11 | | public static bool TryBuild( |
| | | 12 | | HybridRoutePlan routePlan, |
| | | 13 | | out AStarWaypoint[]? flattenedWaypoints, |
| | | 14 | | out string[] chartKeys) |
| | | 15 | | { |
| | 27 | 16 | | flattenedWaypoints = null; |
| | 27 | 17 | | chartKeys = Array.Empty<string>(); |
| | 27 | 18 | | if (routePlan == null) |
| | 1 | 19 | | return false; |
| | | 20 | | |
| | 26 | 21 | | SwiftList<AStarWaypoint> waypoints = new(); |
| | 26 | 22 | | SwiftList<IGuide> borrowedGuides = new(); |
| | 26 | 23 | | SwiftList<string> utilizedCharts = new(); |
| | 26 | 24 | | SwiftHashSet<string> utilizedChartSet = new(); |
| | 26 | 25 | | int pathCostOffset = 0; |
| | | 26 | | |
| | | 27 | | try |
| | | 28 | | { |
| | 238 | 29 | | for (int i = 0; i < routePlan.Steps.Length; i++) |
| | | 30 | | { |
| | 95 | 31 | | HybridRouteStep step = routePlan.Steps[i]; |
| | 95 | 32 | | switch (step.Kind) |
| | | 33 | | { |
| | | 34 | | case HybridRouteStepKind.Waypoint: |
| | 77 | 35 | | pathCostOffset += step.AdditionalCost; |
| | 77 | 36 | | AppendWaypoint( |
| | 77 | 37 | | waypoints, |
| | 77 | 38 | | new AStarWaypoint |
| | 77 | 39 | | { |
| | 77 | 40 | | Position = step.WaypointPosition, |
| | 77 | 41 | | PathCost = pathCostOffset |
| | 77 | 42 | | }); |
| | 77 | 43 | | break; |
| | | 44 | | |
| | | 45 | | case HybridRouteStepKind.PathSegment: |
| | 18 | 46 | | if (!TryAppendSegmentWaypoints( |
| | 18 | 47 | | step, |
| | 18 | 48 | | waypoints, |
| | 18 | 49 | | borrowedGuides, |
| | 18 | 50 | | utilizedCharts, |
| | 18 | 51 | | utilizedChartSet, |
| | 18 | 52 | | ref pathCostOffset)) |
| | | 53 | | { |
| | 2 | 54 | | return false; |
| | | 55 | | } |
| | | 56 | | break; |
| | | 57 | | } |
| | | 58 | | } |
| | | 59 | | |
| | 24 | 60 | | if (waypoints.Count == 0) |
| | 1 | 61 | | return false; |
| | | 62 | | |
| | 23 | 63 | | flattenedWaypoints = waypoints.ToArray(); |
| | 226 | 64 | | for (int i = 0; i < flattenedWaypoints.Length; i++) |
| | 90 | 65 | | flattenedWaypoints[i].IsGoal = false; |
| | | 66 | | |
| | 23 | 67 | | flattenedWaypoints[^1].IsGoal = true; |
| | 23 | 68 | | chartKeys = utilizedCharts.ToArray(); |
| | 23 | 69 | | return true; |
| | | 70 | | } |
| | | 71 | | finally |
| | | 72 | | { |
| | 76 | 73 | | for (int i = 0; i < borrowedGuides.Count; i++) |
| | 12 | 74 | | ReturnBorrowedGuide(borrowedGuides[i]); |
| | 26 | 75 | | } |
| | 26 | 76 | | } |
| | | 77 | | |
| | | 78 | | private static bool TryAppendSegmentWaypoints( |
| | | 79 | | HybridRouteStep step, |
| | | 80 | | SwiftList<AStarWaypoint> destination, |
| | | 81 | | SwiftList<IGuide> borrowedGuides, |
| | | 82 | | SwiftList<string> utilizedCharts, |
| | | 83 | | SwiftHashSet<string> utilizedChartSet, |
| | | 84 | | ref int pathCostOffset) |
| | | 85 | | { |
| | 18 | 86 | | AddChartKeys(utilizedCharts, utilizedChartSet, step.SegmentChartKeys); |
| | | 87 | | |
| | 18 | 88 | | switch (step.SegmentRequest) |
| | | 89 | | { |
| | | 90 | | case AStarPathRequest aStarRequest: |
| | 5 | 91 | | AStarSurveyResult aStarResult = aStarRequest.Context.Pathing.State.GuideState.AStarSurveyor.FindPath(aSt |
| | 5 | 92 | | if (!aStarResult.HasPath) |
| | 1 | 93 | | return false; |
| | | 94 | | |
| | 4 | 95 | | AddChartKeys(utilizedCharts, utilizedChartSet, aStarResult.ChartsUtilized); |
| | 4 | 96 | | AppendWaypoints(destination, aStarResult.Waypoints, ref pathCostOffset); |
| | 4 | 97 | | return true; |
| | | 98 | | |
| | | 99 | | case VolumePathRequest volumeRequest: |
| | 12 | 100 | | if (!volumeRequest.Context.Guides.RequestGuide(volumeRequest, out VolumeGuide? volumeGuide) |
| | 12 | 101 | | || volumeGuide == null) |
| | 0 | 102 | | return false; |
| | | 103 | | |
| | 12 | 104 | | borrowedGuides.Add(volumeGuide); |
| | 12 | 105 | | if (volumeGuide.TrailMap != null) |
| | 12 | 106 | | AddChartKeys(utilizedCharts, utilizedChartSet, volumeGuide.TrailMap.ChartsUtilized); |
| | | 107 | | |
| | 12 | 108 | | AppendWaypoints(destination, volumeGuide.ActiveWaypoints, ref pathCostOffset); |
| | 12 | 109 | | return true; |
| | | 110 | | |
| | | 111 | | default: |
| | 1 | 112 | | return false; |
| | | 113 | | } |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | private static void ReturnBorrowedGuide(IGuide guide) |
| | | 117 | | { |
| | 12 | 118 | | TrailblazerWorldContext? context = guide switch |
| | 12 | 119 | | { |
| | 0 | 120 | | AStarGuide aStarGuide => aStarGuide.TrailMap.Context, |
| | 0 | 121 | | FlowFieldGuide flowFieldGuide => flowFieldGuide.FlowMap?.Context, |
| | 12 | 122 | | VolumeGuide volumeGuide => volumeGuide.TrailMap?.Context, |
| | 0 | 123 | | _ => null |
| | 12 | 124 | | }; |
| | | 125 | | |
| | 12 | 126 | | if (context == null) |
| | 0 | 127 | | throw new InvalidOperationException("Borrowed guide is missing its TrailblazerWorldContext."); |
| | | 128 | | |
| | 12 | 129 | | context.Guides.ReturnGuide(guide); |
| | 12 | 130 | | } |
| | | 131 | | |
| | | 132 | | private static void AppendWaypoints( |
| | | 133 | | SwiftList<AStarWaypoint> destination, |
| | | 134 | | AStarWaypoint[] waypoints, |
| | | 135 | | ref int pathCostOffset) |
| | | 136 | | { |
| | 16 | 137 | | if (waypoints == null) |
| | 0 | 138 | | return; |
| | | 139 | | |
| | 122 | 140 | | for (int i = 0; i < waypoints.Length; i++) |
| | | 141 | | { |
| | 45 | 142 | | AStarWaypoint waypoint = waypoints[i]; |
| | 45 | 143 | | waypoint.PathCost += pathCostOffset; |
| | 45 | 144 | | AppendWaypoint(destination, waypoint); |
| | | 145 | | } |
| | | 146 | | |
| | 16 | 147 | | if (destination.Count > 0) |
| | 16 | 148 | | pathCostOffset = destination[destination.Count - 1].PathCost; |
| | 16 | 149 | | } |
| | | 150 | | |
| | | 151 | | private static void AddChartKeys( |
| | | 152 | | SwiftList<string> destination, |
| | | 153 | | SwiftHashSet<string> utilizedChartSet, |
| | | 154 | | string[] charts) |
| | | 155 | | { |
| | 34 | 156 | | if (charts == null) |
| | 0 | 157 | | return; |
| | | 158 | | |
| | 214 | 159 | | for (int i = 0; i < charts.Length; i++) |
| | | 160 | | { |
| | 73 | 161 | | string chart = charts[i]; |
| | 73 | 162 | | if (string.IsNullOrEmpty(chart) || !utilizedChartSet.Add(chart)) |
| | | 163 | | continue; |
| | | 164 | | |
| | 38 | 165 | | destination.Add(chart); |
| | | 166 | | } |
| | 34 | 167 | | } |
| | | 168 | | |
| | | 169 | | private static void AppendWaypoint( |
| | | 170 | | SwiftList<AStarWaypoint> destination, |
| | | 171 | | AStarWaypoint waypoint) |
| | | 172 | | { |
| | 122 | 173 | | if (destination.Count > 0) |
| | | 174 | | { |
| | 99 | 175 | | AStarWaypoint last = destination[destination.Count - 1]; |
| | 99 | 176 | | if (last.GlobalIndex.HasValue |
| | 99 | 177 | | && waypoint.GlobalIndex.HasValue |
| | 99 | 178 | | && last.GlobalIndex.Value.Equals(waypoint.GlobalIndex.Value)) |
| | | 179 | | { |
| | 1 | 180 | | return; |
| | | 181 | | } |
| | | 182 | | |
| | 98 | 183 | | if (last.Position == waypoint.Position) |
| | 31 | 184 | | return; |
| | | 185 | | } |
| | | 186 | | |
| | 90 | 187 | | waypoint.IsGoal = false; |
| | 90 | 188 | | destination.Add(waypoint); |
| | 90 | 189 | | } |
| | | 190 | | } |