1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
import * as autoContracting from './contracting.ts';
import { debugLog, send, sleep } from '../lib/api.ts';
import { getAgent } from '../lib/agent.ts';
import { getShips, purchaseShip, Ship } from '../lib/ships.ts';
import { market, shipyard, trait, waypoint } from '../lib/systems.ts';
import { Waypoint } from '../lib/types.ts';
import {
are_we_done_visiting_all_markets,
distance,
is_there_a_ship_at_this_waypoint,
sortByDistanceFrom,
} from '../lib/utils.ts';
let running = false;
let state = 0;
enum states {
start_running_contracts_with_the_command_ship = 0,
visit_all_shipyards,
visit_all_markets,
send_probes_to_all_shipyards,
}
export async function run(): Promise<void> {
if (running) {
throw 'refusing to start a second agent processor';
}
running = true;
state = 0;
try {
while(true) {
const ships = getShips();
switch(state) {
case states.start_running_contracts_with_the_command_ship:
autoContracting.run(ships[0]);
state++;
continue;
case states.visit_all_shipyards:
await visit_all_shipyards(ships[1]);
state++;
continue;
case states.visit_all_markets:
await visit_all_markets();
state++;
continue;
case states.send_probes_to_all_shipyards:
await send_probes_to_all_shipyards();
state++;
continue;
default:
debugLog('No more agent processor states implemented, exiting!')
return;
}
}
} catch (e) {
running = false;
throw e;
}
}
async function send_probes_to_all_shipyards(): Promise<void> {
outer: while(true) {
const shipyardWaypoints = await trait(getShips()[0].nav.systemSymbol, 'SHIPYARD');
let candidates: Array<Waypoint> = [];
for (const w of shipyardWaypoints) {
if (is_there_a_ship_at_this_waypoint(w)) continue;
candidates.push(w);
}
if (candidates.length === 0) return;
// if we do not have enough probes, we buy some
if (candidates.length - 1 >= getShips().length - 2) {
const probe = await purchaseShip('SHIP_PROBE');
const probeWaypoint = await waypoint(probe.nav.waypointSymbol);
await probe.navigate(candidates[0]);
continue outer;
}
// otherwise we find the closest ones from a shipyard
const probes = getShips().slice(2);
let probesWaypoints: Array<Waypoint> = [];
for (const p of probes) {
probesWaypoints.push(await waypoint(p.nav.waypointSymbol));
}
const next = sortByDistanceFrom(candidates[0], probesWaypoints)[0].data;
await probes.filter(p => p.nav.waypointSymbol === next.symbol)[0].navigate(candidates[0]);
}
}
async function visit_all_markets(): Promise<void> {
if (await are_we_done_visiting_all_markets()) return;
// send all our probes except the starting one to map the system's markets
for (const probe of getShips().slice(2)) {
if (probe.registration.role !== 'SATELLITE') continue;
visit_next_markets(probe);
await sleep(10000); // we do not send them all at once so they can chose different destinations
}
// buy more probes to speed up the process
while (true) {
if (await are_we_done_visiting_all_markets()) return;
while (getAgent().credits < 250000) {
await sleep(60000);
}
const probe = await purchaseShip('SHIP_PROBE');
visit_next_markets(probe);
await sleep(10000);
}
}
async function visit_next_markets(probe: Ship): Promise<void> {
while (true) {
const probeWaypoint = await waypoint(probe.nav.waypointSymbol);
const marketplaceWaypoints = await trait(probe.nav.systemSymbol, 'MARKETPLACE');
let candidates: Array<Waypoint> = [];
for (const w of marketplaceWaypoints) {
const marketplaceData = await market(w);
if (marketplaceData.tradeGoods) continue;
if (is_there_a_ship_at_this_waypoint(w)) continue;
candidates.push(w);
}
if (candidates.length === 0) {
return;
}
const next = sortByDistanceFrom(probeWaypoint, candidates)[0].data;
await probe.navigate(next);
await market(next);
}
}
async function visit_all_shipyards(probe: Ship): Promise<void> {
const probeWaypoint = await waypoint(probe.nav.waypointSymbol);
const shipyardWaypoints = await trait(probe.nav.systemSymbol, 'SHIPYARD');
let candidates: Array<Waypoint> = [];
for (const w of shipyardWaypoints) {
const shipyardData = await shipyard(w);
if (shipyardData.ships) continue;
candidates.push(w);
}
const nexts = sortByDistanceFrom(probeWaypoint, candidates).map(o => o.data);
for (const next of nexts) {
await probe.navigate(next);
await market(next);
await shipyard(next);
}
}
|