From d8f64aef88fa2ae09ab5b3985596fad166b3c6a3 Mon Sep 17 00:00:00 2001 From: Stafford Williams Date: Thu, 28 Mar 2024 01:20:46 +1100 Subject: [PATCH] command ship also shuttles --- src/features/actors/shuttle.ts | 20 +++++++++++++++++--- src/features/status/startup.ts | 12 ++++++------ 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/features/actors/shuttle.ts b/src/features/actors/shuttle.ts index 79e3058..625b0fe 100644 --- a/src/features/actors/shuttle.ts +++ b/src/features/actors/shuttle.ts @@ -14,18 +14,30 @@ export const shuttleActorFactory = ( miningLocation: IWaypoint, ships: ShipEntity[], sell: TradeSymbol[], -) => - decisionMaker(shuttle, agent, act, async (ship: ShipEntity, agent: AgentEntity) => { +) => decisionMaker(shuttle, agent, act, shuttleLogicFactory(act, markets, miningLocation, ships, sell)) + +export const shuttleLogicFactory = + ( + act: Awaited>, + markets: WaypointEntity[], + miningLocation: IWaypoint, + ships: ShipEntity[], + sell: TradeSymbol[], + ) => + async (ship: ShipEntity, agent: AgentEntity) => { await act.refuelShip(ship) await act.jettisonUnwanted(ship, sell) const currentAction = ship.action?.type if (!currentAction) { await act.updateShipAction(ship, ShipActionType.FILL) + return } else if (currentAction === ShipActionType.FILL) { if (ship.cargo.units === ship.cargo.capacity) { await act.updateShipAction(ship, ShipActionType.SELL) + return } else if (ship.nav.waypointSymbol !== miningLocation.symbol) { await act.navigateShip(ship, miningLocation, markets) + return } else { const capacity = ship.cargo.capacity - ship.cargo.units const dronesAtMiningLocation = ships.filter( @@ -40,12 +52,14 @@ export const shuttleActorFactory = ( } else if (currentAction === ShipActionType.SELL) { if (ship.cargo.units === 0) { await act.updateShipAction(ship, ShipActionType.FILL) + return } else if (ship.cargo.inventory.find((p) => p.symbol === agent.contractGood.tradeSymbol)?.units) { await act.deliverGoods(ship) + return } else { await act.sellGoods(markets, ship, [agent.contractGood.tradeSymbol as TradeSymbol]) return } } else throw new Error(`Unknown action: ${currentAction}`) await act.wait(1000 * 10) - }) + } diff --git a/src/features/status/startup.ts b/src/features/status/startup.ts index 2d1ffc6..06a991f 100644 --- a/src/features/status/startup.ts +++ b/src/features/status/startup.ts @@ -5,7 +5,7 @@ import { updateShips } from '../../db/updateShips' import { invariant } from '../../invariant' import { log } from '../../logging/configure-logging' import { miningDroneActorFactory } from '../actors/mining-drone' -import { shuttleActorFactory } from '../actors/shuttle' +import { shuttleActorFactory, shuttleLogicFactory } from '../actors/shuttle' import { ShipEntity } from '../ship/ship.entity' import { getActor } from './actions/getActor' import { getAgent } from './actions/getAgent' @@ -39,8 +39,11 @@ export async function startup() { }, } = await api.systems.getSystemWaypoints(commandShip.nav.systemSymbol, undefined, 20, 'ENGINEERED_ASTEROID') - const miningDronesToPurchase = 5 + const miningDronesToPurchase = 15 const shuttlesToPurchase = 1 + const keep: TradeSymbol[] = ['IRON_ORE', 'COPPER_ORE', 'ALUMINUM_ORE'] + + const shuttleLogic = shuttleLogicFactory(act, markets, engineeredAsteroid, ships, keep) await decisionMaker(commandShip, agent, act, async (ship: ShipEntity) => { if (!agent.contract || agent.contract.fulfilled) { @@ -53,8 +56,6 @@ export async function startup() { return } - const keep: TradeSymbol[] = ['IRON_ORE', 'COPPER_ORE', 'ALUMINUM_ORE'] - const miningDrones = ships.filter((s) => s.frame.symbol === 'FRAME_DRONE') log.info('agent', `There are ${miningDrones.length} mining drones`) // TODO: don't hardcode the price @@ -84,8 +85,7 @@ export async function startup() { }) } - log.info('ship', `${ship.label} has nothing to do, will idle 5 minutes`) - await act.wait(1000 * 60 * 5) + await shuttleLogic(commandShip, agent) }) }