From b1627bf0d7e6992d93530474081265c663b7431b Mon Sep 17 00:00:00 2001
From: Julien Dessaux <julien.dessaux@adyxax.org>
Date: Tue, 21 May 2024 23:50:13 +0200
Subject: [PATCH] [node] implement automation next step: sending the startup
 probe to a shipyard that selles probes

---
 nodejs/automation/agent.ts | 31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)

diff --git a/nodejs/automation/agent.ts b/nodejs/automation/agent.ts
index 429c83f..9ee0ea8 100644
--- a/nodejs/automation/agent.ts
+++ b/nodejs/automation/agent.ts
@@ -17,6 +17,7 @@ let state = 0;
 enum states {
 	start_running_contracts_with_the_command_ship = 0,
 	visit_all_shipyards,
+	send_the_starting_probe_to_a_shipyard_that_sells_probes,
 }
 
 export async function run(): Promise<void> {
@@ -30,13 +31,17 @@ export async function run(): Promise<void> {
 			const ships = getShips();
 			switch(state) {
 				case states.start_running_contracts_with_the_command_ship:
-					//await autoContracting.run(ships[0]);
+					// TODO await autoContracting.run(ships[0]);
 					state++;
 					continue;
 				case states.visit_all_shipyards:
 					await visit_all_shipyards(ships[1]);
 					state++;
 					continue;
+				case states.send_the_starting_probe_to_a_shipyard_that_sells_probes:
+					await send_the_starting_probe_to_a_shipyard_that_sells_probes(ships[1]);
+					state++;
+					continue;
 				default:
 					debugLog('No more agent processor states implemented, exiting!')
 					return;
@@ -48,6 +53,30 @@ export async function run(): Promise<void> {
 	}
 }
 
+async function send_the_starting_probe_to_a_shipyard_that_sells_probes(probe: Ship) {
+	const probeWaypoint = await waypoint(probe.nav.waypointSymbol);
+	const myShipyard = await shipyard(probeWaypoint);
+	if (myShipyard.shipTypes.some(t => t.type === 'SHIP_PROBE')) return;
+	// our starting probe is not at a shipyard that sells probes, let's move
+	const shipyardWaypoints = await trait(probe.nav.systemSymbol, 'SHIPYARD');
+	let candidates: Array<{price: number, waypoint: Waypoint}> = [];
+	for (const w of shipyardWaypoints) {
+		const shipyardData = await shipyard(w);
+		const probeData = shipyardData.ships.filter(t => t.type === 'SHIP_PROBE');
+		if (probeData.length === 0) continue;
+		candidates.push({price: probeData[0].purchasePrice, waypoint: w });
+	};
+	candidates.sort(function(a, b) {
+		if (a.price < b.price) {
+			return -1;
+		} else if (a.price > b.price) {
+			return 1;
+		}
+		return 0;
+	});
+	await probe.navigate(candidates[0].waypoint);
+}
+
 async function visit_all_shipyards(probe: Ship) {
 	const probeWaypoint = await waypoint(probe.nav.waypointSymbol);
 	const shipyardWaypoints = await trait(probe.nav.systemSymbol, 'SHIPYARD');