Skip to content

Commit

Permalink
Fixed ship routes reporting wrong jump distances.
Browse files Browse the repository at this point in the history
Resolves EDCD#2591
Bring route tracking inside NavWaypointCollection so that each collection can independently track route progress.
  • Loading branch information
Tkael authored and bcthund committed Jun 26, 2024
1 parent 00dd708 commit b886044
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 106 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 14 additions & 15 deletions DataDefinitions/NavWaypointCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,27 @@ public int? RouteFuelTotal
private decimal? currentZ;

[JsonConstructor]
public NavWaypointCollection()
public NavWaypointCollection (IEnumerable<NavWaypoint> 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<NavWaypoint> 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;
}

Expand Down
118 changes: 57 additions & 61 deletions NavigationMonitor/NavigationMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions NavigationService/QueryResolvers/MissionQueryResolvers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
{
Expand All @@ -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 );
Expand Down
55 changes: 27 additions & 28 deletions NavigationService/QueryResolvers/RouteQueryResolvers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,48 +13,47 @@ 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<long>() );
}
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<long> () );
return null;
}
}

[UsedImplicitly]
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 ()
{
Expand All @@ -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 );

/// <summary> 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 </summary>
/// <returns> The star system result from the repeated query </returns>
private static RouteDetailsEvent RefreshLastNavigationQuery ()
private static RouteDetailsEvent RefreshLastNavigationQuery ( [ NotNull ] StarSystem currentSystem )
{
var config = ConfigService.Instance.navigationMonitorConfiguration;
if ( !( config?.plottedRouteList?.GuidanceEnabled ?? false ) )
Expand All @@ -92,7 +91,7 @@ private static RouteDetailsEvent RefreshLastNavigationQuery ()
var missionsList = ConfigService.Instance.missionMonitorConfiguration?.missions?.ToList() ?? new List<Mission>();
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;
Expand All @@ -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
Expand Down

0 comments on commit b886044

Please sign in to comment.