From b8860440d2e89eff68f21327b798e7692c163fae Mon Sep 17 00:00:00 2001 From: T'kael Date: Sun, 17 Mar 2024 00:48:40 -0700 Subject: [PATCH] Fixed ship routes reporting wrong jump distances. Resolves #2591 Bring route tracking inside NavWaypointCollection so that each collection can independently track route progress. --- ChangeLog.md | 1 + DataDefinitions/NavWaypointCollection.cs | 29 +++-- NavigationMonitor/NavigationMonitor.cs | 118 +++++++++--------- .../QueryResolvers/MissionQueryResolvers.cs | 3 +- .../QueryResolvers/RouteQueryResolvers.cs | 55 ++++---- 5 files changed, 100 insertions(+), 106 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index f9dbc2dcc6..842dc99871 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -7,6 +7,7 @@ Full details of the variables available for each noted event, and VoiceAttack in * EDDI will no longer report your environment as "Supercruise" right after a Thargoid hyperdiction. (#2597) * Navigation Monitor * Improved route guidance updates. + * Fixed ship routes reporting wrong jump distances. (#2591) * Speech Responder * Functions * `Play()` now supports relative file system paths. (#2581) diff --git a/DataDefinitions/NavWaypointCollection.cs b/DataDefinitions/NavWaypointCollection.cs index 7b81472510..1f01afa450 100644 --- a/DataDefinitions/NavWaypointCollection.cs +++ b/DataDefinitions/NavWaypointCollection.cs @@ -61,28 +61,27 @@ public int? RouteFuelTotal private decimal? currentZ; [JsonConstructor] - public NavWaypointCollection() + public NavWaypointCollection (IEnumerable items = null, bool fillVisitedGaps = false) { - Waypoints.CollectionChanged += NavWaypointList_CollectionChanged; - } + if ( items != null ) + { + foreach ( var item in items ) + { + Waypoints.Add( item ); + } + CalculateFuelUsed(); + CalculateRouteDistances(); + } - public NavWaypointCollection(bool fillVisitedGaps = false) - { FillVisitedGaps = fillVisitedGaps; Waypoints.CollectionChanged += NavWaypointList_CollectionChanged; } - public NavWaypointCollection(IEnumerable items, bool fillVisitedGaps = false) + public NavWaypointCollection ( decimal x, decimal y, decimal z ) { - foreach (var item in items) - { - Waypoints.Add(item); - } - - CalculateFuelUsed(); - CalculateRouteDistances(); - - FillVisitedGaps = fillVisitedGaps; + currentX = x; + currentY = y; + currentZ = z; Waypoints.CollectionChanged += NavWaypointList_CollectionChanged; } diff --git a/NavigationMonitor/NavigationMonitor.cs b/NavigationMonitor/NavigationMonitor.cs index 64131ad13c..18fa78523a 100644 --- a/NavigationMonitor/NavigationMonitor.cs +++ b/NavigationMonitor/NavigationMonitor.cs @@ -143,66 +143,62 @@ public void HandleProfile(JObject profile) public void PreHandle(Event @event) { - if (@event.timestamp >= updateDat) + // Handle the events that we care about + if (@event is CarrierJumpRequestEvent carrierJumpRequestEvent) { - // Handle the events that we care about - - if (@event is CarrierJumpRequestEvent carrierJumpRequestEvent) - { - handleCarrierJumpRequestEvent(carrierJumpRequestEvent); - } - else if (@event is CarrierJumpCancelledEvent carrierJumpCancelledEvent) - { - handleCarrierJumpCancelledEvent(carrierJumpCancelledEvent); - } - else if (@event is CarrierJumpedEvent carrierJumpedEvent) - { - handleCarrierJumpedEvent(carrierJumpedEvent); - } - else if (@event is CarrierJumpEngagedEvent carrierJumpEngagedEvent) - { - handleCarrierJumpEngagedEvent(carrierJumpEngagedEvent); - } - else if (@event is CarrierPurchasedEvent carrierPurchasedEvent) - { - handleCarrierPurchasedEvent(carrierPurchasedEvent); - } - else if (@event is CarrierStatsEvent carrierStatsEvent) - { - handleCarrierStatsEvent(carrierStatsEvent); - } - else if (@event is CommodityPurchasedEvent commodityPurchasedEvent) - { - handleCommodityPurchasedEvent(commodityPurchasedEvent); - } - else if (@event is CommoditySoldEvent commoditySoldEvent) - { - handleCommoditySoldEvent(commoditySoldEvent); - } - else if (@event is DockedEvent dockedEvent) - { - handleDockedEvent(dockedEvent); - } - else if (@event is JumpedEvent jumpedEvent) - { - handleJumpedEvent(jumpedEvent); - } - else if (@event is LocationEvent locationEvent) - { - handleLocationEvent(locationEvent); - } - else if (@event is NavRouteEvent navRouteEvent) - { - handleNavRouteEvent(navRouteEvent); - } - else if (@event is RouteDetailsEvent routeDetailsEvent) - { - handleRouteDetailsEvent(routeDetailsEvent); - } - else if (@event is FSDTargetEvent fsdTargetEvent) - { - handleFSDTargetEvent(fsdTargetEvent); - } + handleCarrierJumpRequestEvent(carrierJumpRequestEvent); + } + else if (@event is CarrierJumpCancelledEvent carrierJumpCancelledEvent) + { + handleCarrierJumpCancelledEvent(carrierJumpCancelledEvent); + } + else if (@event is CarrierJumpedEvent carrierJumpedEvent) + { + handleCarrierJumpedEvent(carrierJumpedEvent); + } + else if (@event is CarrierJumpEngagedEvent carrierJumpEngagedEvent) + { + handleCarrierJumpEngagedEvent(carrierJumpEngagedEvent); + } + else if (@event is CarrierPurchasedEvent carrierPurchasedEvent) + { + handleCarrierPurchasedEvent(carrierPurchasedEvent); + } + else if (@event is CarrierStatsEvent carrierStatsEvent) + { + handleCarrierStatsEvent(carrierStatsEvent); + } + else if (@event is CommodityPurchasedEvent commodityPurchasedEvent) + { + handleCommodityPurchasedEvent(commodityPurchasedEvent); + } + else if (@event is CommoditySoldEvent commoditySoldEvent) + { + handleCommoditySoldEvent(commoditySoldEvent); + } + else if (@event is DockedEvent dockedEvent) + { + handleDockedEvent(dockedEvent); + } + else if (@event is JumpedEvent jumpedEvent) + { + handleJumpedEvent(jumpedEvent); + } + else if (@event is LocationEvent locationEvent) + { + handleLocationEvent(locationEvent); + } + else if (@event is NavRouteEvent navRouteEvent) + { + handleNavRouteEvent(navRouteEvent); + } + else if (@event is RouteDetailsEvent routeDetailsEvent) + { + handleRouteDetailsEvent(routeDetailsEvent); + } + else if (@event is FSDTargetEvent fsdTargetEvent) + { + handleFSDTargetEvent(fsdTargetEvent); } } @@ -635,11 +631,11 @@ private void ReadNavConfig() GetBookmarkExtras(Bookmarks); // Restore our in-game routing - NavRoute = navConfig.navRouteList ?? new NavWaypointCollection(true); + NavRoute = navConfig.navRouteList ?? new NavWaypointCollection(null, true); // Restore our plotted routes + CarrierPlottedRoute = navConfig.carrierPlottedRoute ?? new NavWaypointCollection(null, true); PlottedRoute = navConfig.plottedRouteList ?? new NavWaypointCollection(); - CarrierPlottedRoute = navConfig.carrierPlottedRoute ?? new NavWaypointCollection(true); // Misc updateDat = navConfig.updatedat; diff --git a/NavigationService/QueryResolvers/MissionQueryResolvers.cs b/NavigationService/QueryResolvers/MissionQueryResolvers.cs index f981be02a4..baab6dc7b4 100644 --- a/NavigationService/QueryResolvers/MissionQueryResolvers.cs +++ b/NavigationService/QueryResolvers/MissionQueryResolvers.cs @@ -360,8 +360,6 @@ private RouteDetailsEvent GetRepetiveNearestNeighborMissionRoute ( [ NotNull ] S var homeSystemWaypoint = homeStarSystem is null ? null : new NavWaypoint( homeStarSystem ); if ( CalculateRepetiveNearestNeighbor ( navWaypoints, missions, out var sortedRoute, homeSystemWaypoint ) ) { - var searchSystem = sortedRoute.FirstOrDefault ( w => !w.visited )?.systemName; - // Prepend our current system to the route if it is not already present if ( sortedRoute.FirstOrDefault ()?.systemAddress != currentSystem.systemAddress ) { @@ -370,6 +368,7 @@ private RouteDetailsEvent GetRepetiveNearestNeighborMissionRoute ( [ NotNull ] S var navRouteList = new NavWaypointCollection ( sortedRoute ); navRouteList.UpdateLocationData( currentSystem.systemAddress, currentSystem.x, currentSystem.y, currentSystem.z ); + var searchSystem = navRouteList.Waypoints.FirstOrDefault ( w => !w.visited )?.systemName; var routeCount = navRouteList.Waypoints.Count; Logging.Debug ( "Calculated Route Selected = " + string.Join ( ", ", sortedRoute.Select ( w => w.systemName ) ) + ", Total Distance = " + navRouteList.RouteDistance ); diff --git a/NavigationService/QueryResolvers/RouteQueryResolvers.cs b/NavigationService/QueryResolvers/RouteQueryResolvers.cs index 168282e756..f253195501 100644 --- a/NavigationService/QueryResolvers/RouteQueryResolvers.cs +++ b/NavigationService/QueryResolvers/RouteQueryResolvers.cs @@ -13,40 +13,39 @@ namespace EddiNavigationService.QueryResolvers internal class SetQueryResolver : IQueryResolver { public QueryType Type => QueryType.set; - public RouteDetailsEvent Resolve ( Query query ) => SetRoute ( query.StringArg0, query.StringArg1 ); + public RouteDetailsEvent Resolve ( Query query, StarSystem startSystem ) => SetRoute ( startSystem, query.StringArg0, query.StringArg1 ); - private static RouteDetailsEvent SetRoute ( string system, string station = null ) + private static RouteDetailsEvent SetRoute ( StarSystem startSystem, string system, string station = null ) { - NavWaypointCollection navRouteList; - NavWaypoint firstUnvisitedWaypoint; - // Use our saved route if a named system is not provided + // Disregard commands to set a route to the current star system. + if ( startSystem.systemname == system ) { return null; } + if ( string.IsNullOrEmpty ( system ) ) { - navRouteList = ConfigService.Instance.navigationMonitorConfiguration.plottedRouteList ?? new NavWaypointCollection (); - firstUnvisitedWaypoint = navRouteList.Waypoints.FirstOrDefault ( w => !w.visited ); + // Use our saved route if a named system is not provided + var navRouteList = ConfigService.Instance.navigationMonitorConfiguration.plottedRouteList ?? new NavWaypointCollection (); + navRouteList.UpdateLocationData( startSystem.systemAddress, startSystem.x, startSystem.y, startSystem.z ); + var firstUnvisitedWaypoint = navRouteList.Waypoints.FirstOrDefault ( w => !w.visited ); if ( firstUnvisitedWaypoint != null ) { return new RouteDetailsEvent ( DateTime.UtcNow, QueryType.set.ToString (), firstUnvisitedWaypoint.systemName, firstUnvisitedWaypoint.stationName, navRouteList, navRouteList.Waypoints.Count, firstUnvisitedWaypoint.missionids ); } } - - // Disregard commands to set a route to the current star system. - var curr = EDDI.Instance?.CurrentStarSystem; - if ( curr?.systemname == system ) - { return null; } - - // Set a course to a named system (and optionally station) - var neutronRoute = NavigationService.Instance.NavQuery(QueryType.neutron, system); - if ( neutronRoute == null || neutronRoute.Route.Waypoints.Count <= 1 ) - { return null; } - - navRouteList = neutronRoute.Route; - foreach ( var wp in navRouteList.Waypoints ) + else { - wp.missionids = NavigationService.GetSystemMissionIds ( wp.systemName ); + // Set a course to a named system (and optionally station) + var neutronRoute = NavigationService.Instance.NavQuery(QueryType.neutron, system); + if ( neutronRoute == null || neutronRoute.Route.Waypoints.Count == 1 ) { return null; } + var navRouteList = neutronRoute.Route; + navRouteList.UpdateLocationData( startSystem.systemAddress, startSystem.x, startSystem.y, startSystem.z ); + foreach ( var wp in navRouteList.Waypoints ) + { + wp.missionids = NavigationService.GetSystemMissionIds( wp.systemName ); + } + var firstUnvisitedWaypoint = navRouteList.Waypoints.FirstOrDefault( w => !w.visited ); + return new RouteDetailsEvent( DateTime.UtcNow, QueryType.set.ToString(), firstUnvisitedWaypoint?.systemName, firstUnvisitedWaypoint?.systemName == system ? station : null, navRouteList, navRouteList.Waypoints.Count, firstUnvisitedWaypoint?.missionids ?? new List() ); } - firstUnvisitedWaypoint = navRouteList.Waypoints.FirstOrDefault ( w => !w.visited ); - return new RouteDetailsEvent ( DateTime.UtcNow, QueryType.set.ToString (), firstUnvisitedWaypoint?.systemName, firstUnvisitedWaypoint?.systemName == system ? station : null, navRouteList, navRouteList.Waypoints.Count, firstUnvisitedWaypoint?.missionids ?? new List () ); + return null; } } @@ -54,7 +53,7 @@ private static RouteDetailsEvent SetRoute ( string system, string station = null internal class CancelQueryResolver : IQueryResolver { public QueryType Type => QueryType.cancel; - public RouteDetailsEvent Resolve ( Query query ) => CancelRoute (); + public RouteDetailsEvent Resolve ( Query query, StarSystem startSystem ) => CancelRoute (); private static RouteDetailsEvent CancelRoute () { @@ -77,11 +76,11 @@ private static RouteDetailsEvent CancelRoute () public class UpdateQueryResolver : IQueryResolver { public QueryType Type => QueryType.update; - public RouteDetailsEvent Resolve ( Query query ) => RefreshLastNavigationQuery (); + public RouteDetailsEvent Resolve ( Query query, StarSystem currentSystem ) => RefreshLastNavigationQuery ( currentSystem ); /// Repeat the last mission query and return an updated result if different from the prior result, either relative to your current location or to a named system /// The star system result from the repeated query - private static RouteDetailsEvent RefreshLastNavigationQuery () + private static RouteDetailsEvent RefreshLastNavigationQuery ( [ NotNull ] StarSystem currentSystem ) { var config = ConfigService.Instance.navigationMonitorConfiguration; if ( !( config?.plottedRouteList?.GuidanceEnabled ?? false ) ) @@ -92,7 +91,7 @@ private static RouteDetailsEvent RefreshLastNavigationQuery () var missionsList = ConfigService.Instance.missionMonitorConfiguration?.missions?.ToList() ?? new List(); if ( missionsList .Where ( m => m != null && m.statusDef == MissionStatus.Active ) - .Any ( m => m.destinationsystem == EDDI.Instance.CurrentStarSystem?.systemname ) ) + .Any ( m => m.destinationsystem == currentSystem.systemname ) ) { // We still have active missions at the current location return null; @@ -107,7 +106,7 @@ private static RouteDetailsEvent RefreshLastNavigationQuery () } var currentWaypoint = currentPlottedRoute?.Waypoints.FirstOrDefault( w => - w.systemAddress == EDDI.Instance.CurrentStarSystem?.systemAddress ); + w.systemAddress == currentSystem.systemAddress ); if ( currentWaypoint != null ) { // We're visiting a waypoint on the plotted route and need to update using the next unvisited waypoint