@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.
- package/README.md +71 -46
- package/dist/bot/bot.js +27 -183
- package/dist/bot/logic/awareness.js +122 -0
- package/dist/bot/logic/building/basicGroundUnit.js +8 -6
- package/dist/bot/logic/building/building.js +6 -3
- package/dist/bot/logic/building/harvester.js +1 -1
- package/dist/bot/logic/building/queueController.js +4 -21
- package/dist/bot/logic/common/scout.js +10 -0
- package/dist/bot/logic/knowledge.js +1 -0
- package/dist/bot/logic/map/map.js +6 -0
- package/dist/bot/logic/map/sector.js +6 -1
- package/dist/bot/logic/mission/basicMission.js +1 -5
- package/dist/bot/logic/mission/expansionMission.js +22 -4
- package/dist/bot/logic/mission/mission.js +49 -2
- package/dist/bot/logic/mission/missionController.js +67 -34
- package/dist/bot/logic/mission/missionFactories.js +10 -0
- package/dist/bot/logic/mission/missions/attackMission.js +109 -0
- package/dist/bot/logic/mission/missions/defenceMission.js +62 -0
- package/dist/bot/logic/mission/missions/expansionMission.js +24 -0
- package/dist/bot/logic/mission/missions/oneTimeMission.js +26 -0
- package/dist/bot/logic/mission/missions/retreatMission.js +7 -0
- package/dist/bot/logic/mission/missions/scoutingMission.js +38 -0
- package/dist/bot/logic/squad/behaviours/attackSquad.js +82 -0
- package/dist/bot/logic/squad/behaviours/combatSquad.js +99 -0
- package/dist/bot/logic/squad/behaviours/common.js +37 -0
- package/dist/bot/logic/squad/behaviours/defenceSquad.js +48 -0
- package/dist/bot/logic/squad/behaviours/expansionSquad.js +42 -0
- package/dist/bot/logic/squad/behaviours/retreatSquad.js +32 -0
- package/dist/bot/logic/squad/behaviours/scoutingSquad.js +38 -0
- package/dist/bot/logic/squad/behaviours/squadExpansion.js +26 -13
- package/dist/bot/logic/squad/squad.js +68 -15
- package/dist/bot/logic/squad/squadBehaviour.js +5 -5
- package/dist/bot/logic/squad/squadBehaviours.js +6 -0
- package/dist/bot/logic/squad/squadController.js +106 -15
- package/dist/exampleBot.js +22 -7
- package/package.json +29 -24
- package/src/bot/bot.ts +178 -378
- package/src/bot/logic/awareness.ts +220 -0
- package/src/bot/logic/building/ArtilleryUnit.ts +2 -2
- package/src/bot/logic/building/antiGroundStaticDefence.ts +2 -2
- package/src/bot/logic/building/basicAirUnit.ts +2 -2
- package/src/bot/logic/building/basicBuilding.ts +2 -2
- package/src/bot/logic/building/basicGroundUnit.ts +83 -78
- package/src/bot/logic/building/building.ts +125 -120
- package/src/bot/logic/building/harvester.ts +27 -27
- package/src/bot/logic/building/powerPlant.ts +1 -1
- package/src/bot/logic/building/queueController.ts +17 -38
- package/src/bot/logic/building/resourceCollectionBuilding.ts +1 -1
- package/src/bot/logic/common/scout.ts +12 -0
- package/src/bot/logic/map/map.ts +11 -3
- package/src/bot/logic/map/sector.ts +136 -130
- package/src/bot/logic/mission/mission.ts +83 -47
- package/src/bot/logic/mission/missionController.ts +103 -51
- package/src/bot/logic/mission/missionFactories.ts +46 -0
- package/src/bot/logic/mission/missions/attackMission.ts +152 -0
- package/src/bot/logic/mission/missions/defenceMission.ts +104 -0
- package/src/bot/logic/mission/missions/expansionMission.ts +49 -0
- package/src/bot/logic/mission/missions/oneTimeMission.ts +32 -0
- package/src/bot/logic/mission/missions/retreatMission.ts +9 -0
- package/src/bot/logic/mission/missions/scoutingMission.ts +59 -0
- package/src/bot/logic/squad/behaviours/combatSquad.ts +125 -0
- package/src/bot/logic/squad/behaviours/common.ts +37 -0
- package/src/bot/logic/squad/behaviours/expansionSquad.ts +59 -0
- package/src/bot/logic/squad/behaviours/retreatSquad.ts +46 -0
- package/src/bot/logic/squad/behaviours/scoutingSquad.ts +56 -0
- package/src/bot/logic/squad/squad.ts +163 -97
- package/src/bot/logic/squad/squadBehaviour.ts +61 -43
- package/src/bot/logic/squad/squadBehaviours.ts +8 -0
- package/src/bot/logic/squad/squadController.ts +190 -66
- package/src/exampleBot.ts +19 -4
- package/tsconfig.json +1 -1
- package/src/bot/logic/mission/basicMission.ts +0 -42
- package/src/bot/logic/mission/expansionMission.ts +0 -25
- 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
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
if (
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
let
|
|
66
|
-
|
|
67
|
-
sector.
|
|
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
|
-
|
|
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 {
|
|
4
|
-
import {
|
|
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
|
-
|
|
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,
|
|
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
|
-
}
|
|
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
|
+
];
|