@supalosa/chronodivide-bot 0.5.4 → 0.6.6-beta.1

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 (124) hide show
  1. package/.env.template +4 -4
  2. package/.github/workflows/npm-publish.yml +24 -0
  3. package/README.md +108 -103
  4. package/dist/bot/bot.js +105 -105
  5. package/dist/bot/logic/awareness.js +136 -136
  6. package/dist/bot/logic/building/antiAirStaticDefence.js +42 -42
  7. package/dist/bot/logic/building/antiGroundStaticDefence.js +34 -34
  8. package/dist/bot/logic/building/{ArtilleryUnit.js → artilleryUnit.js} +18 -18
  9. package/dist/bot/logic/building/basicAirUnit.js +19 -19
  10. package/dist/bot/logic/building/basicBuilding.js +26 -26
  11. package/dist/bot/logic/building/basicGroundUnit.js +19 -19
  12. package/dist/bot/logic/building/buildingRules.js +175 -175
  13. package/dist/bot/logic/building/common.js +19 -19
  14. package/dist/bot/logic/building/harvester.js +16 -16
  15. package/dist/bot/logic/building/powerPlant.js +20 -20
  16. package/dist/bot/logic/building/queueController.js +183 -183
  17. package/dist/bot/logic/building/resourceCollectionBuilding.js +36 -36
  18. package/dist/bot/logic/common/scout.js +126 -126
  19. package/dist/bot/logic/common/utils.js +95 -95
  20. package/dist/bot/logic/composition/alliedCompositions.js +12 -12
  21. package/dist/bot/logic/composition/common.js +1 -1
  22. package/dist/bot/logic/composition/sovietCompositions.js +12 -12
  23. package/dist/bot/logic/map/map.js +44 -44
  24. package/dist/bot/logic/map/sector.js +137 -137
  25. package/dist/bot/logic/mission/actionBatcher.js +91 -91
  26. package/dist/bot/logic/mission/mission.js +122 -122
  27. package/dist/bot/logic/mission/missionController.js +321 -321
  28. package/dist/bot/logic/mission/missionFactories.js +12 -12
  29. package/dist/bot/logic/mission/missions/attackMission.js +214 -214
  30. package/dist/bot/logic/mission/missions/defenceMission.js +82 -82
  31. package/dist/bot/logic/mission/missions/engineerMission.js +63 -63
  32. package/dist/bot/logic/mission/missions/expansionMission.js +60 -60
  33. package/dist/bot/logic/mission/missions/retreatMission.js +33 -33
  34. package/dist/bot/logic/mission/missions/scoutingMission.js +133 -133
  35. package/dist/bot/logic/mission/missions/squads/combatSquad.js +115 -115
  36. package/dist/bot/logic/mission/missions/squads/common.js +57 -57
  37. package/dist/bot/logic/mission/missions/squads/squad.js +1 -1
  38. package/dist/bot/logic/threat/threat.js +22 -22
  39. package/dist/bot/logic/threat/threatCalculator.js +73 -73
  40. package/dist/exampleBot.js +100 -100
  41. package/package.json +32 -29
  42. package/src/bot/bot.ts +161 -161
  43. package/src/bot/logic/awareness.ts +245 -245
  44. package/src/bot/logic/building/antiAirStaticDefence.ts +64 -64
  45. package/src/bot/logic/building/antiGroundStaticDefence.ts +55 -55
  46. package/src/bot/logic/building/artilleryUnit.ts +39 -39
  47. package/src/bot/logic/building/basicAirUnit.ts +39 -39
  48. package/src/bot/logic/building/basicBuilding.ts +49 -49
  49. package/src/bot/logic/building/basicGroundUnit.ts +39 -39
  50. package/src/bot/logic/building/buildingRules.ts +250 -250
  51. package/src/bot/logic/building/common.ts +21 -21
  52. package/src/bot/logic/building/harvester.ts +31 -31
  53. package/src/bot/logic/building/powerPlant.ts +32 -32
  54. package/src/bot/logic/building/queueController.ts +297 -297
  55. package/src/bot/logic/building/resourceCollectionBuilding.ts +52 -52
  56. package/src/bot/logic/common/scout.ts +183 -183
  57. package/src/bot/logic/common/utils.ts +120 -120
  58. package/src/bot/logic/composition/alliedCompositions.ts +22 -22
  59. package/src/bot/logic/composition/common.ts +3 -3
  60. package/src/bot/logic/composition/sovietCompositions.ts +21 -21
  61. package/src/bot/logic/map/map.ts +66 -66
  62. package/src/bot/logic/map/sector.ts +174 -174
  63. package/src/bot/logic/mission/actionBatcher.ts +124 -124
  64. package/src/bot/logic/mission/mission.ts +232 -232
  65. package/src/bot/logic/mission/missionController.ts +413 -413
  66. package/src/bot/logic/mission/missionFactories.ts +51 -51
  67. package/src/bot/logic/mission/missions/attackMission.ts +336 -336
  68. package/src/bot/logic/mission/missions/defenceMission.ts +151 -151
  69. package/src/bot/logic/mission/missions/engineerMission.ts +113 -113
  70. package/src/bot/logic/mission/missions/expansionMission.ts +104 -104
  71. package/src/bot/logic/mission/missions/retreatMission.ts +54 -54
  72. package/src/bot/logic/mission/missions/scoutingMission.ts +186 -186
  73. package/src/bot/logic/mission/missions/squads/combatSquad.ts +160 -160
  74. package/src/bot/logic/mission/missions/squads/common.ts +63 -63
  75. package/src/bot/logic/mission/missions/squads/squad.ts +19 -19
  76. package/src/bot/logic/threat/threat.ts +15 -15
  77. package/src/bot/logic/threat/threatCalculator.ts +100 -100
  78. package/src/exampleBot.ts +111 -111
  79. package/tsconfig.json +73 -73
  80. package/dist/bot/logic/awarenessImpl.js +0 -132
  81. package/dist/bot/logic/awarenessImpl.js.map +0 -1
  82. package/dist/bot/logic/building/building.js +0 -126
  83. package/dist/bot/logic/building/building.js.map +0 -1
  84. package/dist/bot/logic/mission/behaviours/combatSquad.js +0 -124
  85. package/dist/bot/logic/mission/behaviours/combatSquad.js.map +0 -1
  86. package/dist/bot/logic/mission/behaviours/common.js +0 -58
  87. package/dist/bot/logic/mission/behaviours/common.js.map +0 -1
  88. package/dist/bot/logic/mission/behaviours/engineerSquad.js +0 -39
  89. package/dist/bot/logic/mission/behaviours/engineerSquad.js.map +0 -1
  90. package/dist/bot/logic/mission/behaviours/expansionSquad.js +0 -46
  91. package/dist/bot/logic/mission/behaviours/expansionSquad.js.map +0 -1
  92. package/dist/bot/logic/mission/behaviours/retreatSquad.js +0 -31
  93. package/dist/bot/logic/mission/behaviours/retreatSquad.js.map +0 -1
  94. package/dist/bot/logic/mission/behaviours/scoutingSquad.js +0 -94
  95. package/dist/bot/logic/mission/behaviours/scoutingSquad.js.map +0 -1
  96. package/dist/bot/logic/mission/missions/basicMission.js +0 -13
  97. package/dist/bot/logic/mission/missions/basicMission.js.map +0 -1
  98. package/dist/bot/logic/mission/missions/missionBehaviour.js +0 -2
  99. package/dist/bot/logic/mission/missions/missionBehaviour.js.map +0 -1
  100. package/dist/bot/logic/mission/missions/oneTimeMission.js +0 -27
  101. package/dist/bot/logic/mission/missions/oneTimeMission.js.map +0 -1
  102. package/dist/bot/logic/squad/behaviours/attackSquad.js +0 -89
  103. package/dist/bot/logic/squad/behaviours/combatSquad.js +0 -102
  104. package/dist/bot/logic/squad/behaviours/combatSquad.js.map +0 -1
  105. package/dist/bot/logic/squad/behaviours/common.js +0 -40
  106. package/dist/bot/logic/squad/behaviours/common.js.map +0 -1
  107. package/dist/bot/logic/squad/behaviours/defenceSquad.js +0 -61
  108. package/dist/bot/logic/squad/behaviours/engineerSquad.js +0 -36
  109. package/dist/bot/logic/squad/behaviours/engineerSquad.js.map +0 -1
  110. package/dist/bot/logic/squad/behaviours/expansionSquad.js +0 -43
  111. package/dist/bot/logic/squad/behaviours/expansionSquad.js.map +0 -1
  112. package/dist/bot/logic/squad/behaviours/retreatSquad.js +0 -28
  113. package/dist/bot/logic/squad/behaviours/retreatSquad.js.map +0 -1
  114. package/dist/bot/logic/squad/behaviours/scoutingSquad.js +0 -86
  115. package/dist/bot/logic/squad/behaviours/scoutingSquad.js.map +0 -1
  116. package/dist/bot/logic/squad/squad.js +0 -126
  117. package/dist/bot/logic/squad/squad.js.map +0 -1
  118. package/dist/bot/logic/squad/squadBehaviour.js +0 -6
  119. package/dist/bot/logic/squad/squadBehaviour.js.map +0 -1
  120. package/dist/bot/logic/squad/squadBehaviours.js +0 -7
  121. package/dist/bot/logic/squad/squadBehaviours.js.map +0 -1
  122. package/dist/bot/logic/squad/squadController.js +0 -199
  123. package/dist/bot/logic/squad/squadController.js.map +0 -1
  124. /package/dist/bot/logic/building/{ArtilleryUnit.js.map → artilleryUnit.js.map} +0 -0
@@ -1,245 +1,245 @@
1
- import { GameApi, GameObjectData, ObjectType, PlayerData, UnitData, Vector2 } from "@chronodivide/game-api";
2
- import { SectorCache } from "./map/sector";
3
- import { GlobalThreat } from "./threat/threat";
4
- import { calculateGlobalThreat } from "./threat/threatCalculator.js";
5
- import { determineMapBounds, getDistanceBetweenPoints, getPointTowardsOtherPoint } from "./map/map.js";
6
- import { Circle, Quadtree } from "@timohausmann/quadtree-ts";
7
- import { ScoutingManager } from "./common/scout.js";
8
-
9
- export type UnitPositionQuery = { x: number; y: number; unitId: number };
10
-
11
- /**
12
- * The bot's understanding of the current state of the game.
13
- */
14
- export interface MatchAwareness {
15
- /**
16
- * Returns the threat cache for the AI.
17
- */
18
- getThreatCache(): GlobalThreat | null;
19
-
20
- /**
21
- * Returns the sector visibility cache.
22
- */
23
- getSectorCache(): SectorCache;
24
-
25
- /**
26
- * Returns the enemy unit IDs in a certain radius of a point.
27
- * Warning: this may return non-combatant hostiles, such as neutral units.
28
- */
29
- getHostilesNearPoint2d(point: Vector2, radius: number): UnitPositionQuery[];
30
-
31
- /**
32
- * Returns the enemy unit IDs in a certain radius of a point.
33
- * Warning: this may return non-combatant hostiles, such as neutral units.
34
- */
35
- getHostilesNearPoint(x: number, y: number, radius: number): UnitPositionQuery[];
36
-
37
- /**
38
- * Returns the main rally point for the AI, which updates every few ticks.
39
- */
40
- getMainRallyPoint(): Vector2;
41
-
42
- onGameStart(gameApi: GameApi, playerData: PlayerData): void;
43
-
44
- /**
45
- * Update the internal state of the Ai.
46
- * @param gameApi
47
- * @param playerData
48
- */
49
- onAiUpdate(gameApi: GameApi, playerData: PlayerData): void;
50
-
51
- /**
52
- * True if the AI should initiate an attack.
53
- */
54
- shouldAttack(): boolean;
55
-
56
- getScoutingManager(): ScoutingManager;
57
-
58
- getGlobalDebugText(): string | undefined;
59
- }
60
-
61
- const SECTORS_TO_UPDATE_PER_CYCLE = 8;
62
-
63
- const RALLY_POINT_UPDATE_INTERVAL_TICKS = 90;
64
-
65
- const THREAT_UPDATE_INTERVAL_TICKS = 30;
66
-
67
- type QTUnit = Circle<number>;
68
-
69
- const rebuildQuadtree = (quadtree: Quadtree<QTUnit>, units: GameObjectData[]) => {
70
- quadtree.clear();
71
- units.forEach((unit) => {
72
- quadtree.insert(new Circle<number>({ x: unit.tile.rx, y: unit.tile.ry, r: 1, data: unit.id }));
73
- });
74
- };
75
-
76
- export class MatchAwarenessImpl implements MatchAwareness {
77
- private _shouldAttack: boolean = false;
78
-
79
- private hostileQuadTree: Quadtree<QTUnit>;
80
- private scoutingManager: ScoutingManager;
81
-
82
- constructor(
83
- private threatCache: GlobalThreat | null,
84
- private sectorCache: SectorCache,
85
- private mainRallyPoint: Vector2,
86
- private logger: (message: string, sayInGame?: boolean) => void,
87
- ) {
88
- const { width, height } = sectorCache.getMapBounds();
89
- this.hostileQuadTree = new Quadtree({ width, height });
90
- this.scoutingManager = new ScoutingManager(logger);
91
- }
92
-
93
- getHostilesNearPoint2d(point: Vector2, radius: number): UnitPositionQuery[] {
94
- return this.getHostilesNearPoint(point.x, point.y, radius);
95
- }
96
-
97
- getHostilesNearPoint(searchX: number, searchY: number, radius: number): UnitPositionQuery[] {
98
- const intersections = this.hostileQuadTree.retrieve(new Circle({ x: searchX, y: searchY, r: radius }));
99
- return intersections
100
- .map(({ x, y, data: unitId }) => ({ x, y, unitId: unitId! }))
101
- .filter(({ x, y }) => new Vector2(x, y).distanceTo(new Vector2(searchX, searchY)) <= radius)
102
- .filter(({ unitId }) => !!unitId);
103
- }
104
-
105
- getThreatCache(): GlobalThreat | null {
106
- return this.threatCache;
107
- }
108
- getSectorCache(): SectorCache {
109
- return this.sectorCache;
110
- }
111
- getMainRallyPoint(): Vector2 {
112
- return this.mainRallyPoint;
113
- }
114
- getScoutingManager(): ScoutingManager {
115
- return this.scoutingManager;
116
- }
117
-
118
- shouldAttack(): boolean {
119
- return this._shouldAttack;
120
- }
121
-
122
- private checkShouldAttack(threatCache: GlobalThreat, threatFactor: number) {
123
- let scaledGroundPower = threatCache.totalAvailableAntiGroundFirepower * 1.1;
124
- let scaledGroundThreat =
125
- (threatFactor * threatCache.totalOffensiveLandThreat + threatCache.totalDefensiveThreat) * 1.1;
126
-
127
- let scaledAirPower = threatCache.totalAvailableAirPower * 1.1;
128
- let scaledAirThreat =
129
- (threatFactor * threatCache.totalOffensiveAntiAirThreat + threatCache.totalDefensiveThreat) * 1.1;
130
-
131
- return scaledGroundPower > scaledGroundThreat || scaledAirPower > scaledAirThreat;
132
- }
133
-
134
- private isHostileUnit(unit: UnitData | undefined, hostilePlayerNames: string[]): boolean {
135
- if (!unit) {
136
- return false;
137
- }
138
-
139
- return hostilePlayerNames.includes(unit.owner);
140
- }
141
-
142
- public onGameStart(gameApi: GameApi, playerData: PlayerData) {
143
- this.scoutingManager.onGameStart(gameApi, playerData, this.sectorCache);
144
- }
145
-
146
- onAiUpdate(game: GameApi, playerData: PlayerData): void {
147
- const sectorCache = this.sectorCache;
148
-
149
- sectorCache.updateSectors(game.getCurrentTick(), SECTORS_TO_UPDATE_PER_CYCLE, game.mapApi, playerData);
150
-
151
- this.scoutingManager.onAiUpdate(game, playerData, sectorCache);
152
-
153
- let updateRatio = sectorCache?.getSectorUpdateRatio(game.getCurrentTick() - game.getTickRate() * 60);
154
- if (updateRatio && updateRatio < 1.0) {
155
- this.logger(`${updateRatio * 100.0}% of sectors updated in last 60 seconds.`);
156
- }
157
-
158
- const hostilePlayerNames = game
159
- .getPlayers()
160
- .map((name) => game.getPlayerData(name))
161
- .filter(
162
- (other) =>
163
- other.name !== playerData.name &&
164
- other.isCombatant &&
165
- !game.areAlliedPlayers(playerData.name, other.name),
166
- )
167
- .map((other) => other.name);
168
-
169
- // Build the quadtree, if this is too slow we should consider doing this periodically.
170
- const hostileUnitIds = game.getVisibleUnits(playerData.name, "enemy");
171
- try {
172
- const hostileUnits = hostileUnitIds
173
- .map((id) => game.getGameObjectData(id))
174
- .filter(
175
- (gameObjectData: GameObjectData | undefined): gameObjectData is GameObjectData =>
176
- gameObjectData !== undefined,
177
- );
178
-
179
- rebuildQuadtree(this.hostileQuadTree, hostileUnits);
180
- } catch (err) {
181
- // Hack. Will be fixed soon.
182
- console.error(`caught error`, hostileUnitIds);
183
- }
184
-
185
- if (game.getCurrentTick() % THREAT_UPDATE_INTERVAL_TICKS == 0) {
186
- let visibility = sectorCache?.getOverallVisibility();
187
- if (visibility) {
188
- this.logger(`${Math.round(visibility * 1000.0) / 10}% of tiles visible. Calculating threat.`);
189
- // Update the global threat cache
190
- this.threatCache = calculateGlobalThreat(game, playerData, visibility);
191
-
192
- // As the game approaches 2 hours, be more willing to attack. (15 ticks per second)
193
- const gameLengthFactor = Math.max(0, 1.0 - game.getCurrentTick() / (15 * 7200.0));
194
- this.logger(`Game length multiplier: ${gameLengthFactor}`);
195
-
196
- if (!this._shouldAttack) {
197
- // If not attacking, make it harder to switch to attack mode by multiplying the opponent's threat.
198
- this._shouldAttack = this.checkShouldAttack(this.threatCache, 1.25 * gameLengthFactor);
199
- if (this._shouldAttack) {
200
- this.logger(`Globally switched to attack mode.`);
201
- }
202
- } else {
203
- // If currently attacking, make it harder to switch to defence mode my dampening the opponent's threat.
204
- this._shouldAttack = this.checkShouldAttack(this.threatCache, 0.75 * gameLengthFactor);
205
- if (!this._shouldAttack) {
206
- this.logger(`Globally switched to defence mode.`);
207
- }
208
- }
209
- }
210
- }
211
-
212
- // Update rally point every few ticks.
213
- if (game.getCurrentTick() % RALLY_POINT_UPDATE_INTERVAL_TICKS === 0) {
214
- const enemyPlayers = game
215
- .getPlayers()
216
- .filter((p) => p !== playerData.name && !game.areAlliedPlayers(playerData.name, p));
217
- const enemy = game.getPlayerData(enemyPlayers[0]);
218
- this.mainRallyPoint = getPointTowardsOtherPoint(
219
- game,
220
- playerData.startLocation,
221
- enemy.startLocation,
222
- 10,
223
- 10,
224
- 0,
225
- );
226
- }
227
- }
228
-
229
- public getGlobalDebugText(): string | undefined {
230
- if (!this.threatCache) {
231
- return undefined;
232
- }
233
- return (
234
- `Threat LAND: Them ${Math.round(this.threatCache.totalOffensiveLandThreat)}, us: ${Math.round(
235
- this.threatCache.totalAvailableAntiGroundFirepower,
236
- )}.\n` +
237
- `Threat DEFENSIVE: Them ${Math.round(this.threatCache.totalDefensiveThreat)}, us: ${Math.round(
238
- this.threatCache.totalDefensivePower,
239
- )}.\n` +
240
- `Threat AIR: Them ${Math.round(this.threatCache.totalOffensiveAirThreat)}, us: ${Math.round(
241
- this.threatCache.totalAvailableAntiAirFirepower,
242
- )}.`
243
- );
244
- }
245
- }
1
+ import { GameApi, GameObjectData, ObjectType, PlayerData, UnitData, Vector2 } from "@chronodivide/game-api";
2
+ import { SectorCache } from "./map/sector";
3
+ import { GlobalThreat } from "./threat/threat";
4
+ import { calculateGlobalThreat } from "./threat/threatCalculator.js";
5
+ import { determineMapBounds, getDistanceBetweenPoints, getPointTowardsOtherPoint } from "./map/map.js";
6
+ import { Circle, Quadtree } from "@timohausmann/quadtree-ts";
7
+ import { ScoutingManager } from "./common/scout.js";
8
+
9
+ export type UnitPositionQuery = { x: number; y: number; unitId: number };
10
+
11
+ /**
12
+ * The bot's understanding of the current state of the game.
13
+ */
14
+ export interface MatchAwareness {
15
+ /**
16
+ * Returns the threat cache for the AI.
17
+ */
18
+ getThreatCache(): GlobalThreat | null;
19
+
20
+ /**
21
+ * Returns the sector visibility cache.
22
+ */
23
+ getSectorCache(): SectorCache;
24
+
25
+ /**
26
+ * Returns the enemy unit IDs in a certain radius of a point.
27
+ * Warning: this may return non-combatant hostiles, such as neutral units.
28
+ */
29
+ getHostilesNearPoint2d(point: Vector2, radius: number): UnitPositionQuery[];
30
+
31
+ /**
32
+ * Returns the enemy unit IDs in a certain radius of a point.
33
+ * Warning: this may return non-combatant hostiles, such as neutral units.
34
+ */
35
+ getHostilesNearPoint(x: number, y: number, radius: number): UnitPositionQuery[];
36
+
37
+ /**
38
+ * Returns the main rally point for the AI, which updates every few ticks.
39
+ */
40
+ getMainRallyPoint(): Vector2;
41
+
42
+ onGameStart(gameApi: GameApi, playerData: PlayerData): void;
43
+
44
+ /**
45
+ * Update the internal state of the Ai.
46
+ * @param gameApi
47
+ * @param playerData
48
+ */
49
+ onAiUpdate(gameApi: GameApi, playerData: PlayerData): void;
50
+
51
+ /**
52
+ * True if the AI should initiate an attack.
53
+ */
54
+ shouldAttack(): boolean;
55
+
56
+ getScoutingManager(): ScoutingManager;
57
+
58
+ getGlobalDebugText(): string | undefined;
59
+ }
60
+
61
+ const SECTORS_TO_UPDATE_PER_CYCLE = 8;
62
+
63
+ const RALLY_POINT_UPDATE_INTERVAL_TICKS = 90;
64
+
65
+ const THREAT_UPDATE_INTERVAL_TICKS = 30;
66
+
67
+ type QTUnit = Circle<number>;
68
+
69
+ const rebuildQuadtree = (quadtree: Quadtree<QTUnit>, units: GameObjectData[]) => {
70
+ quadtree.clear();
71
+ units.forEach((unit) => {
72
+ quadtree.insert(new Circle<number>({ x: unit.tile.rx, y: unit.tile.ry, r: 1, data: unit.id }));
73
+ });
74
+ };
75
+
76
+ export class MatchAwarenessImpl implements MatchAwareness {
77
+ private _shouldAttack: boolean = false;
78
+
79
+ private hostileQuadTree: Quadtree<QTUnit>;
80
+ private scoutingManager: ScoutingManager;
81
+
82
+ constructor(
83
+ private threatCache: GlobalThreat | null,
84
+ private sectorCache: SectorCache,
85
+ private mainRallyPoint: Vector2,
86
+ private logger: (message: string, sayInGame?: boolean) => void,
87
+ ) {
88
+ const { width, height } = sectorCache.getMapBounds();
89
+ this.hostileQuadTree = new Quadtree({ width, height });
90
+ this.scoutingManager = new ScoutingManager(logger);
91
+ }
92
+
93
+ getHostilesNearPoint2d(point: Vector2, radius: number): UnitPositionQuery[] {
94
+ return this.getHostilesNearPoint(point.x, point.y, radius);
95
+ }
96
+
97
+ getHostilesNearPoint(searchX: number, searchY: number, radius: number): UnitPositionQuery[] {
98
+ const intersections = this.hostileQuadTree.retrieve(new Circle({ x: searchX, y: searchY, r: radius }));
99
+ return intersections
100
+ .map(({ x, y, data: unitId }) => ({ x, y, unitId: unitId! }))
101
+ .filter(({ x, y }) => new Vector2(x, y).distanceTo(new Vector2(searchX, searchY)) <= radius)
102
+ .filter(({ unitId }) => !!unitId);
103
+ }
104
+
105
+ getThreatCache(): GlobalThreat | null {
106
+ return this.threatCache;
107
+ }
108
+ getSectorCache(): SectorCache {
109
+ return this.sectorCache;
110
+ }
111
+ getMainRallyPoint(): Vector2 {
112
+ return this.mainRallyPoint;
113
+ }
114
+ getScoutingManager(): ScoutingManager {
115
+ return this.scoutingManager;
116
+ }
117
+
118
+ shouldAttack(): boolean {
119
+ return this._shouldAttack;
120
+ }
121
+
122
+ private checkShouldAttack(threatCache: GlobalThreat, threatFactor: number) {
123
+ let scaledGroundPower = threatCache.totalAvailableAntiGroundFirepower * 1.1;
124
+ let scaledGroundThreat =
125
+ (threatFactor * threatCache.totalOffensiveLandThreat + threatCache.totalDefensiveThreat) * 1.1;
126
+
127
+ let scaledAirPower = threatCache.totalAvailableAirPower * 1.1;
128
+ let scaledAirThreat =
129
+ (threatFactor * threatCache.totalOffensiveAntiAirThreat + threatCache.totalDefensiveThreat) * 1.1;
130
+
131
+ return scaledGroundPower > scaledGroundThreat || scaledAirPower > scaledAirThreat;
132
+ }
133
+
134
+ private isHostileUnit(unit: UnitData | undefined, hostilePlayerNames: string[]): boolean {
135
+ if (!unit) {
136
+ return false;
137
+ }
138
+
139
+ return hostilePlayerNames.includes(unit.owner);
140
+ }
141
+
142
+ public onGameStart(gameApi: GameApi, playerData: PlayerData) {
143
+ this.scoutingManager.onGameStart(gameApi, playerData, this.sectorCache);
144
+ }
145
+
146
+ onAiUpdate(game: GameApi, playerData: PlayerData): void {
147
+ const sectorCache = this.sectorCache;
148
+
149
+ sectorCache.updateSectors(game.getCurrentTick(), SECTORS_TO_UPDATE_PER_CYCLE, game.mapApi, playerData);
150
+
151
+ this.scoutingManager.onAiUpdate(game, playerData, sectorCache);
152
+
153
+ let updateRatio = sectorCache?.getSectorUpdateRatio(game.getCurrentTick() - game.getTickRate() * 60);
154
+ if (updateRatio && updateRatio < 1.0) {
155
+ this.logger(`${updateRatio * 100.0}% of sectors updated in last 60 seconds.`);
156
+ }
157
+
158
+ const hostilePlayerNames = game
159
+ .getPlayers()
160
+ .map((name) => game.getPlayerData(name))
161
+ .filter(
162
+ (other) =>
163
+ other.name !== playerData.name &&
164
+ other.isCombatant &&
165
+ !game.areAlliedPlayers(playerData.name, other.name),
166
+ )
167
+ .map((other) => other.name);
168
+
169
+ // Build the quadtree, if this is too slow we should consider doing this periodically.
170
+ const hostileUnitIds = game.getVisibleUnits(playerData.name, "enemy");
171
+ try {
172
+ const hostileUnits = hostileUnitIds
173
+ .map((id) => game.getGameObjectData(id))
174
+ .filter(
175
+ (gameObjectData: GameObjectData | undefined): gameObjectData is GameObjectData =>
176
+ gameObjectData !== undefined,
177
+ );
178
+
179
+ rebuildQuadtree(this.hostileQuadTree, hostileUnits);
180
+ } catch (err) {
181
+ // Hack. Will be fixed soon.
182
+ console.error(`caught error`, hostileUnitIds);
183
+ }
184
+
185
+ if (game.getCurrentTick() % THREAT_UPDATE_INTERVAL_TICKS == 0) {
186
+ let visibility = sectorCache?.getOverallVisibility();
187
+ if (visibility) {
188
+ this.logger(`${Math.round(visibility * 1000.0) / 10}% of tiles visible. Calculating threat.`);
189
+ // Update the global threat cache
190
+ this.threatCache = calculateGlobalThreat(game, playerData, visibility);
191
+
192
+ // As the game approaches 2 hours, be more willing to attack. (15 ticks per second)
193
+ const gameLengthFactor = Math.max(0, 1.0 - game.getCurrentTick() / (15 * 7200.0));
194
+ this.logger(`Game length multiplier: ${gameLengthFactor}`);
195
+
196
+ if (!this._shouldAttack) {
197
+ // If not attacking, make it harder to switch to attack mode by multiplying the opponent's threat.
198
+ this._shouldAttack = this.checkShouldAttack(this.threatCache, 1.25 * gameLengthFactor);
199
+ if (this._shouldAttack) {
200
+ this.logger(`Globally switched to attack mode.`);
201
+ }
202
+ } else {
203
+ // If currently attacking, make it harder to switch to defence mode my dampening the opponent's threat.
204
+ this._shouldAttack = this.checkShouldAttack(this.threatCache, 0.75 * gameLengthFactor);
205
+ if (!this._shouldAttack) {
206
+ this.logger(`Globally switched to defence mode.`);
207
+ }
208
+ }
209
+ }
210
+ }
211
+
212
+ // Update rally point every few ticks.
213
+ if (game.getCurrentTick() % RALLY_POINT_UPDATE_INTERVAL_TICKS === 0) {
214
+ const enemyPlayers = game
215
+ .getPlayers()
216
+ .filter((p) => p !== playerData.name && !game.areAlliedPlayers(playerData.name, p));
217
+ const enemy = game.getPlayerData(enemyPlayers[0]);
218
+ this.mainRallyPoint = getPointTowardsOtherPoint(
219
+ game,
220
+ playerData.startLocation,
221
+ enemy.startLocation,
222
+ 10,
223
+ 10,
224
+ 0,
225
+ );
226
+ }
227
+ }
228
+
229
+ public getGlobalDebugText(): string | undefined {
230
+ if (!this.threatCache) {
231
+ return undefined;
232
+ }
233
+ return (
234
+ `Threat LAND: Them ${Math.round(this.threatCache.totalOffensiveLandThreat)}, us: ${Math.round(
235
+ this.threatCache.totalAvailableAntiGroundFirepower,
236
+ )}.\n` +
237
+ `Threat DEFENSIVE: Them ${Math.round(this.threatCache.totalDefensiveThreat)}, us: ${Math.round(
238
+ this.threatCache.totalDefensivePower,
239
+ )}.\n` +
240
+ `Threat AIR: Them ${Math.round(this.threatCache.totalOffensiveAirThreat)}, us: ${Math.round(
241
+ this.threatCache.totalAvailableAntiAirFirepower,
242
+ )}.`
243
+ );
244
+ }
245
+ }
@@ -1,64 +1,64 @@
1
- import { GameApi, PlayerData, TechnoRules, Vector2 } from "@chronodivide/game-api";
2
- import { getPointTowardsOtherPoint } from "../map/map.js";
3
- import { GlobalThreat } from "../threat/threat.js";
4
- import { AiBuildingRules, getDefaultPlacementLocation, numBuildingsOwnedOfType } from "./buildingRules.js";
5
-
6
- export class AntiAirStaticDefence implements AiBuildingRules {
7
- constructor(
8
- private basePriority: number,
9
- private baseAmount: number,
10
- private airStrength: number,
11
- ) {}
12
-
13
- getPlacementLocation(
14
- game: GameApi,
15
- playerData: PlayerData,
16
- technoRules: TechnoRules,
17
- ): { rx: number; ry: number } | undefined {
18
- // Prefer front towards enemy.
19
- let startLocation = playerData.startLocation;
20
- let players = game.getPlayers();
21
- let enemyFacingLocationCandidates: Vector2[] = [];
22
- for (let i = 0; i < players.length; ++i) {
23
- let playerName = players[i];
24
- if (playerName == playerData.name) {
25
- continue;
26
- }
27
- let enemyPlayer = game.getPlayerData(playerName);
28
- enemyFacingLocationCandidates.push(
29
- getPointTowardsOtherPoint(game, startLocation, enemyPlayer.startLocation, 4, 16, 1.5),
30
- );
31
- }
32
- let selectedLocation =
33
- enemyFacingLocationCandidates[Math.floor(game.generateRandom() * enemyFacingLocationCandidates.length)];
34
- return getDefaultPlacementLocation(game, playerData, selectedLocation, technoRules, false, 0);
35
- }
36
-
37
- getPriority(
38
- game: GameApi,
39
- playerData: PlayerData,
40
- technoRules: TechnoRules,
41
- threatCache: GlobalThreat | null,
42
- ): number {
43
- if (threatCache) {
44
- let denominator = threatCache.totalAvailableAntiAirFirepower + this.airStrength;
45
- if (threatCache.totalOffensiveAirThreat > denominator * 1.1) {
46
- return this.basePriority * (threatCache.totalOffensiveAirThreat / Math.max(1, denominator));
47
- } else {
48
- return 0;
49
- }
50
- }
51
- const strengthPerCost = (this.airStrength / technoRules.cost) * 1000;
52
- const numOwned = numBuildingsOwnedOfType(game, playerData, technoRules);
53
- return this.basePriority * (1.0 - numOwned / this.baseAmount) * strengthPerCost;
54
- }
55
-
56
- getMaxCount(
57
- game: GameApi,
58
- playerData: PlayerData,
59
- technoRules: TechnoRules,
60
- threatCache: GlobalThreat | null,
61
- ): number | null {
62
- return null;
63
- }
64
- }
1
+ import { GameApi, PlayerData, TechnoRules, Vector2 } from "@chronodivide/game-api";
2
+ import { getPointTowardsOtherPoint } from "../map/map.js";
3
+ import { GlobalThreat } from "../threat/threat.js";
4
+ import { AiBuildingRules, getDefaultPlacementLocation, numBuildingsOwnedOfType } from "./buildingRules.js";
5
+
6
+ export class AntiAirStaticDefence implements AiBuildingRules {
7
+ constructor(
8
+ private basePriority: number,
9
+ private baseAmount: number,
10
+ private airStrength: number,
11
+ ) {}
12
+
13
+ getPlacementLocation(
14
+ game: GameApi,
15
+ playerData: PlayerData,
16
+ technoRules: TechnoRules,
17
+ ): { rx: number; ry: number } | undefined {
18
+ // Prefer front towards enemy.
19
+ let startLocation = playerData.startLocation;
20
+ let players = game.getPlayers();
21
+ let enemyFacingLocationCandidates: Vector2[] = [];
22
+ for (let i = 0; i < players.length; ++i) {
23
+ let playerName = players[i];
24
+ if (playerName == playerData.name) {
25
+ continue;
26
+ }
27
+ let enemyPlayer = game.getPlayerData(playerName);
28
+ enemyFacingLocationCandidates.push(
29
+ getPointTowardsOtherPoint(game, startLocation, enemyPlayer.startLocation, 4, 16, 1.5),
30
+ );
31
+ }
32
+ let selectedLocation =
33
+ enemyFacingLocationCandidates[Math.floor(game.generateRandom() * enemyFacingLocationCandidates.length)];
34
+ return getDefaultPlacementLocation(game, playerData, selectedLocation, technoRules, false, 0);
35
+ }
36
+
37
+ getPriority(
38
+ game: GameApi,
39
+ playerData: PlayerData,
40
+ technoRules: TechnoRules,
41
+ threatCache: GlobalThreat | null,
42
+ ): number {
43
+ if (threatCache) {
44
+ let denominator = threatCache.totalAvailableAntiAirFirepower + this.airStrength;
45
+ if (threatCache.totalOffensiveAirThreat > denominator * 1.1) {
46
+ return this.basePriority * (threatCache.totalOffensiveAirThreat / Math.max(1, denominator));
47
+ } else {
48
+ return 0;
49
+ }
50
+ }
51
+ const strengthPerCost = (this.airStrength / technoRules.cost) * 1000;
52
+ const numOwned = numBuildingsOwnedOfType(game, playerData, technoRules);
53
+ return this.basePriority * (1.0 - numOwned / this.baseAmount) * strengthPerCost;
54
+ }
55
+
56
+ getMaxCount(
57
+ game: GameApi,
58
+ playerData: PlayerData,
59
+ technoRules: TechnoRules,
60
+ threatCache: GlobalThreat | null,
61
+ ): number | null {
62
+ return null;
63
+ }
64
+ }