@supalosa/chronodivide-bot 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/README.md +71 -46
  2. package/dist/bot/bot.js +27 -183
  3. package/dist/bot/logic/awareness.js +122 -0
  4. package/dist/bot/logic/building/basicGroundUnit.js +8 -6
  5. package/dist/bot/logic/building/building.js +6 -3
  6. package/dist/bot/logic/building/harvester.js +1 -1
  7. package/dist/bot/logic/building/queueController.js +4 -21
  8. package/dist/bot/logic/common/scout.js +10 -0
  9. package/dist/bot/logic/knowledge.js +1 -0
  10. package/dist/bot/logic/map/map.js +6 -0
  11. package/dist/bot/logic/map/sector.js +6 -1
  12. package/dist/bot/logic/mission/basicMission.js +1 -5
  13. package/dist/bot/logic/mission/expansionMission.js +22 -4
  14. package/dist/bot/logic/mission/mission.js +49 -2
  15. package/dist/bot/logic/mission/missionController.js +67 -34
  16. package/dist/bot/logic/mission/missionFactories.js +10 -0
  17. package/dist/bot/logic/mission/missions/attackMission.js +109 -0
  18. package/dist/bot/logic/mission/missions/defenceMission.js +62 -0
  19. package/dist/bot/logic/mission/missions/expansionMission.js +24 -0
  20. package/dist/bot/logic/mission/missions/oneTimeMission.js +26 -0
  21. package/dist/bot/logic/mission/missions/retreatMission.js +7 -0
  22. package/dist/bot/logic/mission/missions/scoutingMission.js +38 -0
  23. package/dist/bot/logic/squad/behaviours/attackSquad.js +82 -0
  24. package/dist/bot/logic/squad/behaviours/combatSquad.js +99 -0
  25. package/dist/bot/logic/squad/behaviours/common.js +37 -0
  26. package/dist/bot/logic/squad/behaviours/defenceSquad.js +48 -0
  27. package/dist/bot/logic/squad/behaviours/expansionSquad.js +42 -0
  28. package/dist/bot/logic/squad/behaviours/retreatSquad.js +32 -0
  29. package/dist/bot/logic/squad/behaviours/scoutingSquad.js +38 -0
  30. package/dist/bot/logic/squad/behaviours/squadExpansion.js +26 -13
  31. package/dist/bot/logic/squad/squad.js +68 -15
  32. package/dist/bot/logic/squad/squadBehaviour.js +5 -5
  33. package/dist/bot/logic/squad/squadBehaviours.js +6 -0
  34. package/dist/bot/logic/squad/squadController.js +106 -15
  35. package/dist/exampleBot.js +22 -7
  36. package/package.json +29 -24
  37. package/src/bot/bot.ts +178 -378
  38. package/src/bot/logic/awareness.ts +220 -0
  39. package/src/bot/logic/building/ArtilleryUnit.ts +2 -2
  40. package/src/bot/logic/building/antiGroundStaticDefence.ts +2 -2
  41. package/src/bot/logic/building/basicAirUnit.ts +2 -2
  42. package/src/bot/logic/building/basicBuilding.ts +2 -2
  43. package/src/bot/logic/building/basicGroundUnit.ts +83 -78
  44. package/src/bot/logic/building/building.ts +125 -120
  45. package/src/bot/logic/building/harvester.ts +27 -27
  46. package/src/bot/logic/building/powerPlant.ts +1 -1
  47. package/src/bot/logic/building/queueController.ts +17 -38
  48. package/src/bot/logic/building/resourceCollectionBuilding.ts +1 -1
  49. package/src/bot/logic/common/scout.ts +12 -0
  50. package/src/bot/logic/map/map.ts +11 -3
  51. package/src/bot/logic/map/sector.ts +136 -130
  52. package/src/bot/logic/mission/mission.ts +83 -47
  53. package/src/bot/logic/mission/missionController.ts +103 -51
  54. package/src/bot/logic/mission/missionFactories.ts +46 -0
  55. package/src/bot/logic/mission/missions/attackMission.ts +152 -0
  56. package/src/bot/logic/mission/missions/defenceMission.ts +104 -0
  57. package/src/bot/logic/mission/missions/expansionMission.ts +49 -0
  58. package/src/bot/logic/mission/missions/oneTimeMission.ts +32 -0
  59. package/src/bot/logic/mission/missions/retreatMission.ts +9 -0
  60. package/src/bot/logic/mission/missions/scoutingMission.ts +59 -0
  61. package/src/bot/logic/squad/behaviours/combatSquad.ts +125 -0
  62. package/src/bot/logic/squad/behaviours/common.ts +37 -0
  63. package/src/bot/logic/squad/behaviours/expansionSquad.ts +59 -0
  64. package/src/bot/logic/squad/behaviours/retreatSquad.ts +46 -0
  65. package/src/bot/logic/squad/behaviours/scoutingSquad.ts +56 -0
  66. package/src/bot/logic/squad/squad.ts +163 -97
  67. package/src/bot/logic/squad/squadBehaviour.ts +61 -43
  68. package/src/bot/logic/squad/squadBehaviours.ts +8 -0
  69. package/src/bot/logic/squad/squadController.ts +190 -66
  70. package/src/exampleBot.ts +19 -4
  71. package/tsconfig.json +1 -1
  72. package/src/bot/logic/mission/basicMission.ts +0 -42
  73. package/src/bot/logic/mission/expansionMission.ts +0 -25
  74. package/src/bot/logic/squad/behaviours/squadExpansion.ts +0 -33
@@ -1,130 +1,136 @@
1
- // A sector is a uniform-sized segment of the map.
2
-
3
- import { MapApi, PlayerData, Point2D, Tile } from "@chronodivide/game-api";
4
- import { calculateAreaVisibility } from "./map.js";
5
-
6
- export const SECTOR_SIZE = 8;
7
-
8
- export class Sector {
9
- constructor(
10
- public sectorStartPoint: Point2D,
11
- public sectorStartTile: Tile | undefined,
12
- public sectorVisibilityPct: number | undefined,
13
- public sectorVisibilityLastCheckTick: number | undefined,
14
- ) {}
15
- }
16
-
17
- export class SectorCache {
18
- private sectors: Sector[][] = [];
19
- private mapBounds: Point2D;
20
- private sectorsX: number;
21
- private sectorsY: number;
22
- private lastUpdatedSectorX: number | undefined;
23
- private lastUpdatedSectorY: number | undefined;
24
-
25
- constructor(mapApi: MapApi, mapBounds: Point2D) {
26
- this.mapBounds = mapBounds;
27
- this.sectorsX = Math.ceil(mapBounds.x / SECTOR_SIZE);
28
- this.sectorsY = Math.ceil(mapBounds.y / SECTOR_SIZE);
29
- this.sectors = new Array(this.sectorsX);
30
- for (let xx = 0; xx < this.sectorsX; ++xx) {
31
- this.sectors[xx] = new Array(this.sectorsY);
32
- for (let yy = 0; yy < this.sectorsY; ++yy) {
33
- this.sectors[xx][yy] = new Sector(
34
- { x: xx * SECTOR_SIZE, y: yy * SECTOR_SIZE },
35
- mapApi.getTile(xx * SECTOR_SIZE, yy * SECTOR_SIZE),
36
- undefined,
37
- undefined,
38
- );
39
- }
40
- }
41
- }
42
-
43
- public updateSectors(currentGameTick: number, maxSectorsToUpdate: number, mapApi: MapApi, playerData: PlayerData) {
44
- let nextSectorX = this.lastUpdatedSectorX ? this.lastUpdatedSectorX + 1 : 0;
45
- let nextSectorY = this.lastUpdatedSectorY ? this.lastUpdatedSectorY : 0;
46
- let updatedThisCycle = 0;
47
-
48
- while (updatedThisCycle < maxSectorsToUpdate) {
49
- if (nextSectorX >= this.sectorsX) {
50
- nextSectorX = 0;
51
- ++nextSectorY;
52
- }
53
- if (nextSectorY >= this.sectorsY) {
54
- nextSectorY = 0;
55
- nextSectorX = 0;
56
- }
57
- let sector: Sector | undefined = this.getSector(nextSectorX, nextSectorY);
58
- if (sector) {
59
- sector.sectorVisibilityLastCheckTick = currentGameTick;
60
- let sp = sector.sectorStartPoint;
61
- let ep = {
62
- x: sector.sectorStartPoint.x + SECTOR_SIZE,
63
- y: sector.sectorStartPoint.y + SECTOR_SIZE,
64
- };
65
- let visibility = calculateAreaVisibility(mapApi, playerData, sp, ep);
66
- if (visibility.validTiles > 0) {
67
- sector.sectorVisibilityPct = visibility.visibleTiles / visibility.validTiles;
68
- } else {
69
- sector.sectorVisibilityPct = undefined;
70
- }
71
- }
72
- this.lastUpdatedSectorX = nextSectorX;
73
- this.lastUpdatedSectorY = nextSectorY;
74
- ++nextSectorX;
75
- ++updatedThisCycle;
76
- }
77
- }
78
-
79
- // Return % of sectors that are updated.
80
- public getSectorUpdateRatio(sectorsUpdatedSinceGameTick: number): number {
81
- let updated = 0,
82
- total = 0;
83
- for (let xx = 0; xx < this.sectorsX; ++xx) {
84
- for (let yy = 0; yy < this.sectorsY; ++yy) {
85
- let sector: Sector = this.sectors[xx][yy];
86
- if (
87
- sector &&
88
- sector.sectorVisibilityLastCheckTick &&
89
- sector.sectorVisibilityLastCheckTick >= sectorsUpdatedSinceGameTick
90
- ) {
91
- ++updated;
92
- }
93
- ++total;
94
- }
95
- }
96
- return updated / total;
97
- }
98
-
99
- // Return % of tiles that are visible. Returns undefined if we haven't scanned the whole map yet.
100
- public getOverallVisibility(): number | undefined {
101
- let visible = 0,
102
- total = 0;
103
- for (let xx = 0; xx < this.sectorsX; ++xx) {
104
- for (let yy = 0; yy < this.sectorsY; ++yy) {
105
- let sector: Sector = this.sectors[xx][yy];
106
-
107
- // Undefined visibility.
108
- if (sector.sectorVisibilityPct != undefined) {
109
- visible += sector.sectorVisibilityPct;
110
- total += 1.0;
111
- }
112
- }
113
- }
114
- return visible / total;
115
- }
116
-
117
- public getSector(sectorX: number, sectorY: number): Sector | undefined {
118
- if (sectorX < 0 || sectorX >= this.sectorsX || sectorY < 0 || sectorY >= this.sectorsY) {
119
- return undefined;
120
- }
121
- return this.sectors[sectorX][sectorY];
122
- }
123
-
124
- public getSectorForWorldPosition(x: number, y: number): Sector | undefined {
125
- if (x < 0 || x >= this.mapBounds.x || y < 0 || y >= this.mapBounds.y) {
126
- return undefined;
127
- }
128
- return this.sectors[Math.floor(x / SECTOR_SIZE)][Math.floor(y / SECTOR_SIZE)];
129
- }
130
- }
1
+ // A sector is a uniform-sized segment of the map.
2
+
3
+ import { MapApi, PlayerData, Point2D, Tile } from "@chronodivide/game-api";
4
+ import { calculateAreaVisibility } from "./map.js";
5
+
6
+ export const SECTOR_SIZE = 8;
7
+
8
+ export class Sector {
9
+ constructor(
10
+ public sectorStartPoint: Point2D,
11
+ public sectorStartTile: Tile | undefined,
12
+ public sectorVisibilityPct: number | undefined,
13
+ public sectorVisibilityLastCheckTick: number | undefined
14
+ ) {}
15
+ }
16
+
17
+ export class SectorCache {
18
+ private sectors: Sector[][] = [];
19
+ private mapBounds: Point2D;
20
+ private sectorsX: number;
21
+ private sectorsY: number;
22
+ private lastUpdatedSectorX: number | undefined;
23
+ private lastUpdatedSectorY: number | undefined;
24
+
25
+ constructor(mapApi: MapApi, mapBounds: Point2D) {
26
+ this.mapBounds = mapBounds;
27
+ this.sectorsX = Math.ceil(mapBounds.x / SECTOR_SIZE);
28
+ this.sectorsY = Math.ceil(mapBounds.y / SECTOR_SIZE);
29
+ this.sectors = new Array(this.sectorsX);
30
+ for (let xx = 0; xx < this.sectorsX; ++xx) {
31
+ this.sectors[xx] = new Array(this.sectorsY);
32
+ for (let yy = 0; yy < this.sectorsY; ++yy) {
33
+ this.sectors[xx][yy] = new Sector(
34
+ { x: xx * SECTOR_SIZE, y: yy * SECTOR_SIZE },
35
+ mapApi.getTile(xx * SECTOR_SIZE, yy * SECTOR_SIZE),
36
+ undefined,
37
+ undefined
38
+ );
39
+ }
40
+ }
41
+ }
42
+
43
+ public getMapBounds(): Point2D {
44
+ return this.mapBounds;
45
+ }
46
+
47
+ public updateSectors(currentGameTick: number, maxSectorsToUpdate: number, mapApi: MapApi, playerData: PlayerData) {
48
+ let nextSectorX = this.lastUpdatedSectorX ? this.lastUpdatedSectorX + 1 : 0;
49
+ let nextSectorY = this.lastUpdatedSectorY ? this.lastUpdatedSectorY : 0;
50
+ let updatedThisCycle = 0;
51
+
52
+ while (updatedThisCycle < maxSectorsToUpdate) {
53
+ if (nextSectorX >= this.sectorsX) {
54
+ nextSectorX = 0;
55
+ ++nextSectorY;
56
+ }
57
+ if (nextSectorY >= this.sectorsY) {
58
+ nextSectorY = 0;
59
+ nextSectorX = 0;
60
+ }
61
+ let sector: Sector | undefined = this.getSector(nextSectorX, nextSectorY);
62
+ if (sector) {
63
+ sector.sectorVisibilityLastCheckTick = currentGameTick;
64
+ let sp = sector.sectorStartPoint;
65
+ let ep = {
66
+ x: sector.sectorStartPoint.x + SECTOR_SIZE,
67
+ y: sector.sectorStartPoint.y + SECTOR_SIZE,
68
+ };
69
+ let visibility = calculateAreaVisibility(mapApi, playerData, sp, ep);
70
+ if (visibility.validTiles > 0) {
71
+ sector.sectorVisibilityPct = visibility.visibleTiles / visibility.validTiles;
72
+ } else {
73
+ sector.sectorVisibilityPct = undefined;
74
+ }
75
+ }
76
+ this.lastUpdatedSectorX = nextSectorX;
77
+ this.lastUpdatedSectorY = nextSectorY;
78
+ ++nextSectorX;
79
+ ++updatedThisCycle;
80
+ }
81
+ }
82
+
83
+ // Return % of sectors that are updated.
84
+ public getSectorUpdateRatio(sectorsUpdatedSinceGameTick: number): number {
85
+ let updated = 0,
86
+ total = 0;
87
+ for (let xx = 0; xx < this.sectorsX; ++xx) {
88
+ for (let yy = 0; yy < this.sectorsY; ++yy) {
89
+ let sector: Sector = this.sectors[xx][yy];
90
+ if (
91
+ sector &&
92
+ sector.sectorVisibilityLastCheckTick &&
93
+ sector.sectorVisibilityLastCheckTick >= sectorsUpdatedSinceGameTick
94
+ ) {
95
+ ++updated;
96
+ }
97
+ ++total;
98
+ }
99
+ }
100
+ return updated / total;
101
+ }
102
+
103
+ /**
104
+ * Return the ratio (0-1) of tiles that are visible. Returns undefined if we haven't scanned the whole map yet.
105
+ */
106
+ public getOverallVisibility(): number | undefined {
107
+ let visible = 0,
108
+ total = 0;
109
+ for (let xx = 0; xx < this.sectorsX; ++xx) {
110
+ for (let yy = 0; yy < this.sectorsY; ++yy) {
111
+ let sector: Sector = this.sectors[xx][yy];
112
+
113
+ // Undefined visibility.
114
+ if (sector.sectorVisibilityPct != undefined) {
115
+ visible += sector.sectorVisibilityPct;
116
+ total += 1.0;
117
+ }
118
+ }
119
+ }
120
+ return visible / total;
121
+ }
122
+
123
+ public getSector(sectorX: number, sectorY: number): Sector | undefined {
124
+ if (sectorX < 0 || sectorX >= this.sectorsX || sectorY < 0 || sectorY >= this.sectorsY) {
125
+ return undefined;
126
+ }
127
+ return this.sectors[sectorX][sectorY];
128
+ }
129
+
130
+ public getSectorForWorldPosition(x: number, y: number): Sector | undefined {
131
+ if (x < 0 || x >= this.mapBounds.x || y < 0 || y >= this.mapBounds.y) {
132
+ return undefined;
133
+ }
134
+ return this.sectors[Math.floor(x / SECTOR_SIZE)][Math.floor(y / SECTOR_SIZE)];
135
+ }
136
+ }
@@ -1,47 +1,83 @@
1
- import { GameApi, PlayerData } from "@chronodivide/game-api";
2
- import { Squad } from "../squad/squad.js";
3
- import { SquadBehaviour } from "../squad/squadBehaviour.js";
4
- import { GlobalThreat } from "../threat/threat.js";
5
- import { ExpansionMissionFactory } from "./expansionMission.js";
6
-
7
- // AI starts Missions based on heuristics, which have one or more squads.
8
- // Missions can create squads (but squads will disband themselves).
9
- export interface Mission {
10
- onAiUpdate(gameApi: GameApi, playerData: PlayerData, threatData: GlobalThreat | undefined): MissionAction;
11
-
12
- isActive(): boolean;
13
-
14
- removeSquad(squad: Squad): void;
15
-
16
- addSquad(squad: Squad): void;
17
-
18
- getSquads(): Squad[];
19
-
20
- getUniqueName(): string;
21
- }
22
-
23
- export type MissionActionNoop = {
24
- type: "noop";
25
- };
26
- export type MissionActionCreateSquad = {
27
- type: "createSquad";
28
- name: string;
29
- behaviour: SquadBehaviour;
30
- };
31
- export type MissionActionDisband = {
32
- type: "disband";
33
- };
34
-
35
- export type MissionAction = MissionActionNoop | MissionActionCreateSquad | MissionActionDisband;
36
-
37
- export interface MissionFactory {
38
- // Potentially return a new mission.
39
- maybeCreateMission(
40
- gameApi: GameApi,
41
- playerData: PlayerData,
42
- threatData: GlobalThreat,
43
- existingMissions: Mission[],
44
- ): Mission | undefined;
45
- }
46
-
47
- export const missionFactories = [new ExpansionMissionFactory()];
1
+ import { GameApi, PlayerData } from "@chronodivide/game-api";
2
+ import { Squad } from "../squad/squad.js";
3
+ import { GlobalThreat } from "../threat/threat.js";
4
+ import { MatchAwareness } from "../awareness.js";
5
+
6
+ // AI starts Missions based on heuristics, which have one or more squads.
7
+ // Missions can create squads (but squads will disband themselves).
8
+ export abstract class Mission<FailureReasons = undefined> {
9
+ private squad: Squad | null = null;
10
+ private active = true;
11
+
12
+ private onFinish: (reason: FailureReasons, squad: Squad | null) => void = () => {};
13
+
14
+ constructor(private uniqueName: string, private priority: number = 1) {}
15
+
16
+ abstract onAiUpdate(gameApi: GameApi, playerData: PlayerData, matchAwareness: MatchAwareness): MissionAction;
17
+
18
+ isActive(): boolean {
19
+ return this.active;
20
+ }
21
+
22
+ protected setSquad(squad: Squad): MissionActionRegisterSquad {
23
+ this.squad = squad;
24
+ return registerSquad(squad);
25
+ }
26
+
27
+ getSquad(): Squad | null {
28
+ return this.squad;
29
+ }
30
+
31
+ removeSquad() {
32
+ // The squad was removed from this mission.
33
+ this.squad = null;
34
+ }
35
+
36
+ getUniqueName(): string {
37
+ return this.uniqueName;
38
+ }
39
+
40
+ // Don't call this from the mission itself
41
+ endMission(reason: FailureReasons): void {
42
+ this.onFinish(reason, this.squad);
43
+ this.squad = null;
44
+ this.active = false;
45
+ }
46
+
47
+ /**
48
+ * Declare a callback that is executed when the mission is disbanded for whatever reason.
49
+ */
50
+ then(onFinish: (reason: FailureReasons, squad: Squad | null) => void): Mission<FailureReasons> {
51
+ this.onFinish = onFinish;
52
+ return this;
53
+ }
54
+ }
55
+
56
+ export type MissionActionNoop = {
57
+ type: "noop";
58
+ };
59
+
60
+ export type MissionActionRegisterSquad = {
61
+ type: "registerSquad";
62
+ squad: Squad;
63
+ };
64
+
65
+ export type MissionActionDisband = {
66
+ type: "disband";
67
+ reason: any | null;
68
+ };
69
+
70
+ export const noop = () =>
71
+ ({
72
+ type: "noop",
73
+ } as MissionActionNoop);
74
+
75
+ export const registerSquad = (squad: Squad) =>
76
+ ({
77
+ type: "registerSquad",
78
+ squad,
79
+ } as MissionActionRegisterSquad);
80
+
81
+ export const disbandMission = (reason?: any) => ({ type: "disband", reason } as MissionActionDisband);
82
+
83
+ export type MissionAction = MissionActionNoop | MissionActionRegisterSquad | MissionActionDisband;
@@ -1,51 +1,103 @@
1
- // Meta-controller for forming and controlling squads.
2
-
3
- import { GameApi, PlayerData } from "@chronodivide/game-api";
4
- import { GlobalThreat } from "../threat/threat.js";
5
- import { Mission, MissionAction, MissionActionDisband, missionFactories } from "./mission.js";
6
-
7
- export class MissionController {
8
- constructor(private missions: Mission[] = []) {}
9
-
10
- public onAiUpdate(gameApi: GameApi, playerData: PlayerData, threatData: GlobalThreat | undefined) {
11
- // Remove disbanded missions.
12
- this.missions = this.missions.filter((missions) => missions.isActive());
13
-
14
- let missionActions = this.missions.map((mission) => {
15
- return {
16
- mission,
17
- action: mission.onAiUpdate(gameApi, playerData, threatData),
18
- };
19
- });
20
- // Handle disbands and merges.
21
- const isDisband = (a: MissionAction): a is MissionActionDisband => a.type == "disband";
22
- let disbandedMissions: Set<string> = new Set();
23
- missionActions
24
- .filter((a) => isDisband(a.action))
25
- .forEach((a) => {
26
- a.mission.getSquads().forEach((squad) => {
27
- squad.setMission(undefined);
28
- });
29
- disbandedMissions.add(a.mission.getUniqueName());
30
- });
31
- // remove disbanded and merged squads.
32
- this.missions.filter((missions) => !disbandedMissions.has(missions.getUniqueName()));
33
-
34
- // Create missions.
35
- let newMissions: Mission[];
36
- let missionNames: Set<String> = new Set();
37
- this.missions.forEach((mission) => missionNames.add(mission.getUniqueName()));
38
- missionFactories.forEach((missionFactory) => {
39
- let maybeMission = missionFactory.maybeCreateMission(gameApi, playerData, threatData, this.missions);
40
- if (maybeMission) {
41
- if (missionNames.has(maybeMission.getUniqueName())) {
42
- //console.log(`Rejecting new mission ${maybeMission.getUniqueName()} as another mission exists.`);
43
- } else {
44
- console.log(`Starting new mission ${maybeMission.getUniqueName()}.`);
45
- this.missions.push(maybeMission);
46
- missionNames.add(maybeMission.getUniqueName());
47
- }
48
- }
49
- });
50
- }
51
- }
1
+ // Meta-controller for forming and controlling squads.
2
+
3
+ import { GameApi, PlayerData } from "@chronodivide/game-api";
4
+ import { GlobalThreat } from "../threat/threat.js";
5
+ import { Mission, MissionAction, MissionActionDisband, MissionActionRegisterSquad } from "./mission.js";
6
+ import { SquadController } from "../squad/squadController.js";
7
+ import { MatchAwareness } from "../awareness.js";
8
+ import { MissionFactory, createMissionFactories } from "./missionFactories.js";
9
+
10
+ export class MissionController {
11
+ private missionFactories: MissionFactory[];
12
+ private missions: Mission<any>[] = [];
13
+
14
+ private forceDisbandedMissions: string[] = [];
15
+
16
+ constructor(private logger: (message: string) => void) {
17
+ this.missionFactories = createMissionFactories();
18
+ }
19
+
20
+ public onAiUpdate(
21
+ gameApi: GameApi,
22
+ playerData: PlayerData,
23
+ matchAwareness: MatchAwareness,
24
+ squadController: SquadController,
25
+ ) {
26
+ // Remove inactive missions.
27
+ this.missions = this.missions.filter((missions) => missions.isActive());
28
+
29
+ // Poll missions for requested actions.
30
+ const missionActions = this.missions.map((mission) => ({
31
+ mission,
32
+ action: mission.onAiUpdate(gameApi, playerData, matchAwareness),
33
+ }));
34
+
35
+ // Handle disbands and merges.
36
+ const isDisband = (a: MissionAction) => a.type == "disband";
37
+ const disbandedMissions: Map<string, any> = new Map();
38
+ const disbandedMissionsArray: { mission: Mission; reason: any }[] = [];
39
+ this.forceDisbandedMissions.forEach((name) => disbandedMissions.set(name, null));
40
+ this.forceDisbandedMissions = [];
41
+ missionActions
42
+ .filter((a) => isDisband(a.action))
43
+ .forEach((a) => {
44
+ disbandedMissions.set(a.mission.getUniqueName(), (a.action as MissionActionDisband).reason);
45
+ });
46
+
47
+ // Remove disbanded and merged squads.
48
+ this.missions
49
+ .filter((missions) => disbandedMissions.has(missions.getUniqueName()))
50
+ .forEach((disbandedMission) => {
51
+ this.logger(`mission disbanded: ${disbandedMission.getUniqueName()}`);
52
+ const reason = disbandedMissions.get(disbandedMission.getUniqueName());
53
+ disbandedMissionsArray.push({ mission: disbandedMission, reason });
54
+ disbandedMission.getSquad()?.setMission(null);
55
+ disbandedMission.endMission(disbandedMissions.get(disbandedMission.getUniqueName()));
56
+ });
57
+ this.missions = this.missions.filter((missions) => !disbandedMissions.has(missions.getUniqueName()));
58
+
59
+ // Register new squads
60
+ const isNewSquad = (a: MissionAction) => a.type == "registerSquad";
61
+ missionActions
62
+ .filter((a) => isNewSquad(a.action))
63
+ .forEach((a) => {
64
+ const action = a.action as MissionActionRegisterSquad;
65
+ squadController.registerSquad(action.squad);
66
+ this.logger(`registered a squad: ${action.squad.getName()}`);
67
+ });
68
+
69
+ // Create dynamic missions.
70
+ this.missionFactories.forEach((missionFactory) => {
71
+ missionFactory.maybeCreateMissions(gameApi, playerData, matchAwareness, this);
72
+ disbandedMissionsArray.forEach(({ reason, mission }) => {
73
+ missionFactory.onMissionFailed(gameApi, playerData, matchAwareness, mission, reason, this);
74
+ });
75
+ });
76
+ }
77
+
78
+ /**
79
+ * Attempts to add a mission to the active set.
80
+ * @param mission
81
+ * @returns The mission if it was accepted, or null if it was not.
82
+ */
83
+ public addMission<T>(mission: Mission<T | any>): Mission<T> | null {
84
+ if (this.missions.some((m) => m.getUniqueName() === mission.getUniqueName())) {
85
+ // reject non-unique mission names
86
+ return null;
87
+ }
88
+ this.logger(`Added mission: ${mission.getUniqueName()}`);
89
+ this.missions.push(mission);
90
+ return mission;
91
+ }
92
+
93
+ /**
94
+ * Disband the provided mission on the next possible opportunity.
95
+ */
96
+ public disbandMission(missionName: string) {
97
+ this.forceDisbandedMissions.push(missionName);
98
+ }
99
+
100
+ public logDebugOutput() {
101
+ this.logger(`Missions (${this.missions.length}): ${this.missions.map((m) => m.getUniqueName()).join(", ")}`);
102
+ }
103
+ }
@@ -0,0 +1,46 @@
1
+ import { GameApi, PlayerData } from "@chronodivide/game-api";
2
+ import { ExpansionMissionFactory } from "./missions/expansionMission.js";
3
+ import { Mission } from "./mission.js";
4
+ import { MatchAwareness } from "../awareness.js";
5
+ import { ScoutingMissionFactory } from "./missions/scoutingMission.js";
6
+ import { AttackMissionFactory } from "./missions/attackMission.js";
7
+ import { MissionController } from "./missionController.js";
8
+ import { DefenceMissionFactory } from "./missions/defenceMission.js";
9
+
10
+ export interface MissionFactory {
11
+ getName(): string;
12
+
13
+ /**
14
+ * Queries the factory for new missions to be spawned.
15
+ *
16
+ * @param gameApi
17
+ * @param playerData
18
+ * @param matchAwareness
19
+ * @param missionController
20
+ */
21
+ maybeCreateMissions(
22
+ gameApi: GameApi,
23
+ playerData: PlayerData,
24
+ matchAwareness: MatchAwareness,
25
+ missionController: MissionController,
26
+ ): void;
27
+
28
+ /**
29
+ * Called when any mission fails - can be used to trigger another mission in response.
30
+ */
31
+ onMissionFailed(
32
+ gameApi: GameApi,
33
+ playerData: PlayerData,
34
+ matchAwareness: MatchAwareness,
35
+ failedMission: Mission,
36
+ failureReason: any,
37
+ missionController: MissionController,
38
+ ): void;
39
+ }
40
+
41
+ export const createMissionFactories = () => [
42
+ new ExpansionMissionFactory(),
43
+ new ScoutingMissionFactory(),
44
+ new AttackMissionFactory(),
45
+ new DefenceMissionFactory(),
46
+ ];