Skip to content

Commit

Permalink
refactor: Use local BehaviorState
Browse files Browse the repository at this point in the history
BehaviorState was originally introduced as side-loaded per-ship data
but we added passing it to behavior functions a while back.  This
change makes us use the local version of state in almost all cases.

Added BehaviorState.isComplete, but are not using it yet, it seemed
the most risky of the possible changes.
  • Loading branch information
eseidel committed Aug 20, 2023
1 parent 913cbbf commit 5fb434a
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 97 deletions.
8 changes: 8 additions & 0 deletions packages/cli/lib/behavior/advance.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ Future<DateTime?> advanceShipBehavior(
final navResult = await continueNavigationIfNeeded(
api,
ship,
state,
caches.ships,
caches.systems,
centralCommand,
Expand All @@ -94,6 +95,13 @@ Future<DateTime?> advanceShipBehavior(
ship,
getNow: getNow,
);
if (state.isComplete) {
// If the behavior is complete, clear it.
centralCommand.completeBehavior(ship.shipSymbol);
} else {
// Otherwise update the behavior state.
centralCommand.setBehavior(ship.shipSymbol, state);
}
return waitUntil;
} on JobException catch (error) {
centralCommand.disableBehaviorForShip(
Expand Down
1 change: 1 addition & 0 deletions packages/cli/lib/behavior/buy_ship.dart
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ Future<DateTime?> advanceBuyShip(
return beingNewRouteAndLog(
api,
ship,
state,
caches.ships,
caches.systems,
caches.routePlanner,
Expand Down
72 changes: 0 additions & 72 deletions packages/cli/lib/behavior/central_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -383,22 +383,6 @@ class CentralCommand {
return _behaviorCache.deleteBehavior(shipSymbol);
}

/// Returns any mount that's been queued for adding to this ship.
ShipMountSymbolEnum? getMountToAdd(ShipSymbol shipSymbol) {
final state = _behaviorCache.getBehavior(shipSymbol);
return state?.mountToAdd;
}

/// Saves the given mount to be added to the ship.
void claimMount(ShipSymbol shipSymbol, ShipMountSymbolEnum mountSymbol) {
final state = _behaviorCache.getBehavior(shipSymbol);
if (state == null) {
logger.warn('No state for $shipSymbol');
return;
}
state.mountToAdd = mountSymbol;
}

/// Returns the delivery ship bringing the mounts.
Ship? getDeliveryShip(ShipSymbol shipSymbol, TradeSymbol item) {
final deliveryShip = _shipCache.ships.first;
Expand Down Expand Up @@ -455,62 +439,6 @@ class CentralCommand {
return available.difference(claimed);
}

/// Sets the deliver state for the given ship.
void setBuyJob(Ship ship, BuyJob buyJob) {
final shipSymbol = ship.shipSymbol;
final state = _behaviorCache.getBehavior(shipSymbol);
if (state == null) {
shipWarn(ship, 'No state for $shipSymbol');
return;
}
state.buyJob = buyJob;
}

/// Returns the DeliverState for the given ship.
BuyJob? getBuyJob(Ship ship) {
final shipSymbol = ship.shipSymbol;
final state = _behaviorCache.getBehavior(shipSymbol);
return state?.buyJob;
}

/// Sets the deliver state for the given ship.
void setDeliverJob(Ship ship, DeliverJob deliverJob) {
final shipSymbol = ship.shipSymbol;
final state = _behaviorCache.getBehavior(shipSymbol);
if (state == null) {
shipWarn(ship, 'No state for $shipSymbol');
return;
}
state.deliverJob = deliverJob;
}

/// Returns the DeliverState for the given ship.
DeliverJob? getDeliverJob(Ship ship) {
final shipSymbol = ship.shipSymbol;
final state = _behaviorCache.getBehavior(shipSymbol);
return state?.deliverJob;
}

/// Set the [RoutePlan] for the ship.
void setRoutePlan(Ship ship, RoutePlan routePlan) {
final state = _behaviorCache.getBehavior(ship.shipSymbol)!
..routePlan = routePlan;
_behaviorCache.setBehavior(ship.shipSymbol, state);
}

/// Get the current [RoutePlan] for the given ship.
RoutePlan? currentRoutePlan(Ship ship) {
final state = _behaviorCache.getBehavior(ship.shipSymbol);
return state?.routePlan;
}

/// The [ship] has reached its destination.
void reachedEndOfRoutePlan(Ship ship) {
final state = _behaviorCache.getBehavior(ship.shipSymbol)!
..routePlan = null;
_behaviorCache.setBehavior(ship.shipSymbol, state);
}

/// Record the given [transactions] for the current deal for [ship].
void recordDealTransactions(
Ship ship,
Expand Down
5 changes: 3 additions & 2 deletions packages/cli/lib/behavior/change_mounts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Future<DateTime?> advanceChangeMounts(
Ship ship, {
DateTime Function() getNow = defaultGetNow,
}) async {
final toMount = centralCommand.getMountToAdd(ship.shipSymbol);
final toMount = state.mountToAdd;

// Re-validate every loop in case resuming from error.
final template = assertNotNull(
Expand Down Expand Up @@ -170,12 +170,13 @@ Future<DateTime?> advanceChangeMounts(
const Duration(minutes: 10),
);
shipInfo(ship, 'Claiming mount: $toClaim.');
centralCommand.claimMount(ship.shipSymbol, toClaim);
state.mountToAdd = toClaim;

// Go to the shipyard.
final waitUntil = beingNewRouteAndLog(
api,
ship,
state,
caches.ships,
caches.systems,
caches.routePlanner,
Expand Down
7 changes: 5 additions & 2 deletions packages/cli/lib/behavior/deliver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ Future<JobResult> doBuyJob(
centralCommand,
caches,
ship,
state,
maybeMarket,
buyJob.tradeSymbol,
);
Expand All @@ -199,6 +200,7 @@ Future<JobResult> doBuyJob(
final waitUntil = await beingNewRouteAndLog(
api,
ship,
state,
caches.ships,
caches.systems,
caches.routePlanner,
Expand Down Expand Up @@ -309,6 +311,7 @@ Future<JobResult> doDeliverJob(
final waitUntil = await beingNewRouteAndLog(
api,
ship,
state,
caches.ships,
caches.systems,
caches.routePlanner,
Expand Down Expand Up @@ -346,7 +349,7 @@ Future<JobResult> doInitJob(
const Duration(minutes: 20),
);

centralCommand.setBuyJob(ship, buyJob);
state.buyJob = buyJob;

final hqSystem = caches.agent.headquartersSymbol.systemSymbol;
final hqWaypoints = await caches.waypoints.waypointsInSystem(hqSystem);
Expand All @@ -356,7 +359,7 @@ Future<JobResult> doInitJob(
tradeSymbol: buyJob.tradeSymbol,
waypointSymbol: shipyard.waypointSymbol,
);
centralCommand.setDeliverJob(ship, deliverJob);
state.deliverJob = deliverJob;
return JobResult.complete();
}

Expand Down
4 changes: 4 additions & 0 deletions packages/cli/lib/behavior/explorer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ Future<DateTime?> routeForEmergencyFuelingIfNeeded(
CentralCommand centralCommand,
Waypoint waypoint,
Ship ship,
BehaviorState state,
) async {
if (ship.fuelPercentage > 0.4) {
return null;
Expand Down Expand Up @@ -250,6 +251,7 @@ Future<DateTime?> routeForEmergencyFuelingIfNeeded(
final waitUntil = await beingNewRouteAndLog(
api,
ship,
state,
caches.ships,
caches.systems,
caches.routePlanner,
Expand Down Expand Up @@ -314,6 +316,7 @@ Future<DateTime?> advanceExplorer(
centralCommand,
waypoint,
ship,
state,
);
if (refuelWaitTime != null) {
return refuelWaitTime;
Expand Down Expand Up @@ -350,6 +353,7 @@ Future<DateTime?> advanceExplorer(
return beingNewRouteAndLog(
api,
ship,
state,
caches.ships,
caches.systems,
caches.routePlanner,
Expand Down
6 changes: 5 additions & 1 deletion packages/cli/lib/behavior/miner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ class MineJob {
// - Navigate to mine
// - Survey if needed
// - Mine
// - Handle cargo (navigate to market, sell, jettison, etc)
// - Navigate to market
// - Sell

/// Apply the miner behavior to the ship.
Future<DateTime?> advanceMiner(
Expand Down Expand Up @@ -272,6 +272,7 @@ Future<DateTime?> advanceMiner(
return beingNewRouteAndLog(
api,
ship,
state,
caches.ships,
caches.systems,
caches.routePlanner,
Expand All @@ -289,6 +290,7 @@ Future<DateTime?> advanceMiner(
return beingNewRouteAndLog(
api,
ship,
state,
caches.ships,
caches.systems,
caches.routePlanner,
Expand Down Expand Up @@ -324,6 +326,8 @@ Future<DateTime?> advanceMiner(
if (ship.isCommand) {
centralCommand.completeBehavior(ship.shipSymbol);
}
// We wait the full cooldown because our next action will be either
// surveying or mining, both of which require the reactor cooldown.
return response.cooldown.expiration;
}

Expand Down
1 change: 1 addition & 0 deletions packages/cli/lib/behavior/surveyor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Future<DateTime?> advanceSurveyor(
return beingNewRouteAndLog(
api,
ship,
state,
caches.ships,
caches.systems,
caches.routePlanner,
Expand Down
Loading

0 comments on commit 5fb434a

Please sign in to comment.