Skip to content

Commit

Permalink
fix: make nearest shipyards fancier
Browse files Browse the repository at this point in the history
  • Loading branch information
eseidel committed Mar 24, 2024
1 parent 525d22c commit dff0d38
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
3 changes: 1 addition & 2 deletions packages/cli/lib/behavior/mount_from_buy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ Future<MountRequest?> mountRequestForShip(
const mountCost = 100000;
final creditsNeeded = buyCost + mountCost;

final shipyard =
await nearestShipyard(routePlanner, shipyards, ship.waypointSymbol);
final shipyard = await nearestShipyard(routePlanner, shipyards, ship);
if (shipyard == null) {
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/lib/behavior/scrap.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Future<JobResult> doScrapJob(
await nearestShipyard(
caches.routePlanner,
shipyardListings,
ship.waypointSymbol,
ship,
),
'No shipyard found.',
const Duration(minutes: 5),
Expand Down
24 changes: 20 additions & 4 deletions packages/cli/lib/nav/route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -619,15 +619,31 @@ String describeRoutePlan(RoutePlan plan) {
Future<ShipyardListing?> nearestShipyard(
RoutePlanner routePlanner,
ShipyardListingSnapshot shipyards,
WaypointSymbol start,
Ship ship,
) async {
final start = ship.waypointSymbol;
final listings = shipyards.listingsInSystem(start.system);

// If not in this system. Should list all shipyardListings.
// Filter by ones which are reachable (e.g. if this ship can warp).
// Pick the one with the shortest route.
final systemConnectivity = routePlanner.systemConnectivity;

// TODO(eseidel): Sort by distance.
// TODO(eseidel): Consider reachable systems not just this one.
return listings.firstOrNull;
final reachableShipyards = listings.where((listing) {
return systemConnectivity.existsJumpPathBetween(
start.system,
listing.waypointSymbol.system,
);
});
// Sort by system distance.
final listing = minBy(reachableShipyards, (listing) {
return routePlanner
.planRoute(
ship.shipSpec,
start: start,
end: listing.waypointSymbol,
)!
.duration;
});
return listing;
}

0 comments on commit dff0d38

Please sign in to comment.