@supalosa/chronodivide-bot 0.5.4 → 0.6.4
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/.env.template +4 -4
- package/.github/workflows/npm-publish.yml +24 -0
- package/README.md +108 -103
- package/dist/bot/bot.js +105 -105
- package/dist/bot/logic/awareness.js +136 -136
- package/dist/bot/logic/building/antiAirStaticDefence.js +42 -42
- package/dist/bot/logic/building/antiGroundStaticDefence.js +34 -34
- package/dist/bot/logic/building/{ArtilleryUnit.js → artilleryUnit.js} +18 -18
- package/dist/bot/logic/building/basicAirUnit.js +19 -19
- package/dist/bot/logic/building/basicBuilding.js +26 -26
- package/dist/bot/logic/building/basicGroundUnit.js +19 -19
- package/dist/bot/logic/building/buildingRules.js +175 -175
- package/dist/bot/logic/building/common.js +19 -19
- package/dist/bot/logic/building/harvester.js +16 -16
- package/dist/bot/logic/building/powerPlant.js +20 -20
- package/dist/bot/logic/building/queueController.js +183 -183
- package/dist/bot/logic/building/resourceCollectionBuilding.js +36 -36
- package/dist/bot/logic/common/scout.js +126 -126
- package/dist/bot/logic/common/utils.js +95 -95
- package/dist/bot/logic/composition/alliedCompositions.js +12 -12
- package/dist/bot/logic/composition/common.js +1 -1
- package/dist/bot/logic/composition/sovietCompositions.js +12 -12
- package/dist/bot/logic/map/map.js +44 -44
- package/dist/bot/logic/map/sector.js +137 -137
- package/dist/bot/logic/mission/actionBatcher.js +91 -91
- package/dist/bot/logic/mission/mission.js +122 -122
- package/dist/bot/logic/mission/missionController.js +321 -321
- package/dist/bot/logic/mission/missionFactories.js +12 -12
- package/dist/bot/logic/mission/missions/attackMission.js +214 -214
- package/dist/bot/logic/mission/missions/defenceMission.js +82 -82
- package/dist/bot/logic/mission/missions/engineerMission.js +63 -63
- package/dist/bot/logic/mission/missions/expansionMission.js +60 -60
- package/dist/bot/logic/mission/missions/retreatMission.js +33 -33
- package/dist/bot/logic/mission/missions/scoutingMission.js +133 -133
- package/dist/bot/logic/mission/missions/squads/combatSquad.js +115 -115
- package/dist/bot/logic/mission/missions/squads/common.js +57 -57
- package/dist/bot/logic/mission/missions/squads/squad.js +1 -1
- package/dist/bot/logic/threat/threat.js +22 -22
- package/dist/bot/logic/threat/threatCalculator.js +73 -73
- package/dist/exampleBot.js +100 -100
- package/package.json +32 -29
- package/src/bot/bot.ts +161 -161
- package/src/bot/logic/awareness.ts +245 -245
- package/src/bot/logic/building/antiAirStaticDefence.ts +64 -64
- package/src/bot/logic/building/antiGroundStaticDefence.ts +55 -55
- package/src/bot/logic/building/artilleryUnit.ts +39 -39
- package/src/bot/logic/building/basicAirUnit.ts +39 -39
- package/src/bot/logic/building/basicBuilding.ts +49 -49
- package/src/bot/logic/building/basicGroundUnit.ts +39 -39
- package/src/bot/logic/building/buildingRules.ts +250 -250
- package/src/bot/logic/building/common.ts +21 -21
- package/src/bot/logic/building/harvester.ts +31 -31
- package/src/bot/logic/building/powerPlant.ts +32 -32
- package/src/bot/logic/building/queueController.ts +297 -297
- package/src/bot/logic/building/resourceCollectionBuilding.ts +52 -52
- package/src/bot/logic/common/scout.ts +183 -183
- package/src/bot/logic/common/utils.ts +120 -120
- package/src/bot/logic/composition/alliedCompositions.ts +22 -22
- package/src/bot/logic/composition/common.ts +3 -3
- package/src/bot/logic/composition/sovietCompositions.ts +21 -21
- package/src/bot/logic/map/map.ts +66 -66
- package/src/bot/logic/map/sector.ts +174 -174
- package/src/bot/logic/mission/actionBatcher.ts +124 -124
- package/src/bot/logic/mission/mission.ts +232 -232
- package/src/bot/logic/mission/missionController.ts +413 -413
- package/src/bot/logic/mission/missionFactories.ts +51 -51
- package/src/bot/logic/mission/missions/attackMission.ts +336 -336
- package/src/bot/logic/mission/missions/defenceMission.ts +151 -151
- package/src/bot/logic/mission/missions/engineerMission.ts +113 -113
- package/src/bot/logic/mission/missions/expansionMission.ts +104 -104
- package/src/bot/logic/mission/missions/retreatMission.ts +54 -54
- package/src/bot/logic/mission/missions/scoutingMission.ts +186 -186
- package/src/bot/logic/mission/missions/squads/combatSquad.ts +160 -160
- package/src/bot/logic/mission/missions/squads/common.ts +63 -63
- package/src/bot/logic/mission/missions/squads/squad.ts +19 -19
- package/src/bot/logic/threat/threat.ts +15 -15
- package/src/bot/logic/threat/threatCalculator.ts +100 -100
- package/src/exampleBot.ts +111 -111
- package/tsconfig.json +73 -73
- package/dist/bot/logic/awarenessImpl.js +0 -132
- package/dist/bot/logic/awarenessImpl.js.map +0 -1
- package/dist/bot/logic/building/building.js +0 -126
- package/dist/bot/logic/building/building.js.map +0 -1
- package/dist/bot/logic/mission/behaviours/combatSquad.js +0 -124
- package/dist/bot/logic/mission/behaviours/combatSquad.js.map +0 -1
- package/dist/bot/logic/mission/behaviours/common.js +0 -58
- package/dist/bot/logic/mission/behaviours/common.js.map +0 -1
- package/dist/bot/logic/mission/behaviours/engineerSquad.js +0 -39
- package/dist/bot/logic/mission/behaviours/engineerSquad.js.map +0 -1
- package/dist/bot/logic/mission/behaviours/expansionSquad.js +0 -46
- package/dist/bot/logic/mission/behaviours/expansionSquad.js.map +0 -1
- package/dist/bot/logic/mission/behaviours/retreatSquad.js +0 -31
- package/dist/bot/logic/mission/behaviours/retreatSquad.js.map +0 -1
- package/dist/bot/logic/mission/behaviours/scoutingSquad.js +0 -94
- package/dist/bot/logic/mission/behaviours/scoutingSquad.js.map +0 -1
- package/dist/bot/logic/mission/missions/basicMission.js +0 -13
- package/dist/bot/logic/mission/missions/basicMission.js.map +0 -1
- package/dist/bot/logic/mission/missions/missionBehaviour.js +0 -2
- package/dist/bot/logic/mission/missions/missionBehaviour.js.map +0 -1
- package/dist/bot/logic/mission/missions/oneTimeMission.js +0 -27
- package/dist/bot/logic/mission/missions/oneTimeMission.js.map +0 -1
- package/dist/bot/logic/squad/behaviours/attackSquad.js +0 -89
- package/dist/bot/logic/squad/behaviours/combatSquad.js +0 -102
- package/dist/bot/logic/squad/behaviours/combatSquad.js.map +0 -1
- package/dist/bot/logic/squad/behaviours/common.js +0 -40
- package/dist/bot/logic/squad/behaviours/common.js.map +0 -1
- package/dist/bot/logic/squad/behaviours/defenceSquad.js +0 -61
- package/dist/bot/logic/squad/behaviours/engineerSquad.js +0 -36
- package/dist/bot/logic/squad/behaviours/engineerSquad.js.map +0 -1
- package/dist/bot/logic/squad/behaviours/expansionSquad.js +0 -43
- package/dist/bot/logic/squad/behaviours/expansionSquad.js.map +0 -1
- package/dist/bot/logic/squad/behaviours/retreatSquad.js +0 -28
- package/dist/bot/logic/squad/behaviours/retreatSquad.js.map +0 -1
- package/dist/bot/logic/squad/behaviours/scoutingSquad.js +0 -86
- package/dist/bot/logic/squad/behaviours/scoutingSquad.js.map +0 -1
- package/dist/bot/logic/squad/squad.js +0 -126
- package/dist/bot/logic/squad/squad.js.map +0 -1
- package/dist/bot/logic/squad/squadBehaviour.js +0 -6
- package/dist/bot/logic/squad/squadBehaviour.js.map +0 -1
- package/dist/bot/logic/squad/squadBehaviours.js +0 -7
- package/dist/bot/logic/squad/squadBehaviours.js.map +0 -1
- package/dist/bot/logic/squad/squadController.js +0 -199
- package/dist/bot/logic/squad/squadController.js.map +0 -1
- /package/dist/bot/logic/building/{ArtilleryUnit.js.map → artilleryUnit.js.map} +0 -0
|
@@ -1,336 +1,336 @@
|
|
|
1
|
-
import { ActionsApi, GameApi, ObjectType, PlayerData, SideType, UnitData, Vector2 } from "@chronodivide/game-api";
|
|
2
|
-
import { CombatSquad } from "./squads/combatSquad.js";
|
|
3
|
-
import { Mission, MissionAction, disbandMission, noop, requestUnits } from "../mission.js";
|
|
4
|
-
import { MissionFactory } from "../missionFactories.js";
|
|
5
|
-
import { MatchAwareness } from "../../awareness.js";
|
|
6
|
-
import { MissionController } from "../missionController.js";
|
|
7
|
-
import { RetreatMission } from "./retreatMission.js";
|
|
8
|
-
import { DebugLogger, countBy, isOwnedByNeutral, maxBy } from "../../common/utils.js";
|
|
9
|
-
import { ActionBatcher } from "../actionBatcher.js";
|
|
10
|
-
import { getSovietComposition } from "../../composition/sovietCompositions.js";
|
|
11
|
-
import { getAlliedCompositions } from "../../composition/alliedCompositions.js";
|
|
12
|
-
import { UnitComposition } from "../../composition/common.js";
|
|
13
|
-
import { manageMoveMicro } from "./squads/common.js";
|
|
14
|
-
|
|
15
|
-
export enum AttackFailReason {
|
|
16
|
-
NoTargets = 0,
|
|
17
|
-
DefenceTooStrong = 1,
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
enum AttackMissionState {
|
|
21
|
-
Preparing = 0,
|
|
22
|
-
Attacking = 1,
|
|
23
|
-
Retreating = 2,
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const NO_TARGET_RETARGET_TICKS = 450;
|
|
27
|
-
const NO_TARGET_IDLE_TIMEOUT_TICKS = 900;
|
|
28
|
-
|
|
29
|
-
function calculateTargetComposition(
|
|
30
|
-
gameApi: GameApi,
|
|
31
|
-
playerData: PlayerData,
|
|
32
|
-
matchAwareness: MatchAwareness,
|
|
33
|
-
): UnitComposition {
|
|
34
|
-
if (!playerData.country) {
|
|
35
|
-
throw new Error(`player ${playerData.name} has no country`);
|
|
36
|
-
} else if (playerData.country.side === SideType.Nod) {
|
|
37
|
-
return getSovietComposition(gameApi, playerData, matchAwareness);
|
|
38
|
-
} else {
|
|
39
|
-
return getAlliedCompositions(gameApi, playerData, matchAwareness);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const ATTACK_MISSION_PRIORITY_RAMP = 1.01;
|
|
44
|
-
const ATTACK_MISSION_MAX_PRIORITY = 50;
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* A mission that tries to attack a certain area.
|
|
48
|
-
*/
|
|
49
|
-
export class AttackMission extends Mission<AttackFailReason> {
|
|
50
|
-
private squad: CombatSquad;
|
|
51
|
-
|
|
52
|
-
private lastTargetSeenAt = 0;
|
|
53
|
-
private hasPickedNewTarget: boolean = false;
|
|
54
|
-
|
|
55
|
-
private state: AttackMissionState = AttackMissionState.Preparing;
|
|
56
|
-
|
|
57
|
-
constructor(
|
|
58
|
-
uniqueName: string,
|
|
59
|
-
private priority: number,
|
|
60
|
-
rallyArea: Vector2,
|
|
61
|
-
private attackArea: Vector2,
|
|
62
|
-
private radius: number,
|
|
63
|
-
private composition: UnitComposition,
|
|
64
|
-
logger: DebugLogger,
|
|
65
|
-
) {
|
|
66
|
-
super(uniqueName, logger);
|
|
67
|
-
this.squad = new CombatSquad(rallyArea, attackArea, radius);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
_onAiUpdate(
|
|
71
|
-
gameApi: GameApi,
|
|
72
|
-
actionsApi: ActionsApi,
|
|
73
|
-
playerData: PlayerData,
|
|
74
|
-
matchAwareness: MatchAwareness,
|
|
75
|
-
actionBatcher: ActionBatcher,
|
|
76
|
-
): MissionAction {
|
|
77
|
-
switch (this.state) {
|
|
78
|
-
case AttackMissionState.Preparing:
|
|
79
|
-
return this.handlePreparingState(gameApi, actionsApi, playerData, matchAwareness, actionBatcher);
|
|
80
|
-
case AttackMissionState.Attacking:
|
|
81
|
-
return this.handleAttackingState(gameApi, actionsApi, playerData, matchAwareness, actionBatcher);
|
|
82
|
-
case AttackMissionState.Retreating:
|
|
83
|
-
return this.handleRetreatingState(gameApi, actionsApi, playerData, matchAwareness, actionBatcher);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
private handlePreparingState(
|
|
88
|
-
gameApi: GameApi,
|
|
89
|
-
actionsApi: ActionsApi,
|
|
90
|
-
playerData: PlayerData,
|
|
91
|
-
matchAwareness: MatchAwareness,
|
|
92
|
-
actionBatcher: ActionBatcher,
|
|
93
|
-
) {
|
|
94
|
-
const currentComposition: UnitComposition = countBy(this.getUnits(gameApi), (unit) => unit.name);
|
|
95
|
-
|
|
96
|
-
const missingUnits = Object.entries(this.composition).filter(([unitType, targetAmount]) => {
|
|
97
|
-
return !currentComposition[unitType] || currentComposition[unitType] < targetAmount;
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
if (missingUnits.length > 0) {
|
|
101
|
-
this.priority = Math.min(this.priority * ATTACK_MISSION_PRIORITY_RAMP, ATTACK_MISSION_MAX_PRIORITY);
|
|
102
|
-
return requestUnits(
|
|
103
|
-
missingUnits.map(([unitName]) => unitName),
|
|
104
|
-
this.priority,
|
|
105
|
-
);
|
|
106
|
-
} else {
|
|
107
|
-
this.priority = ATTACK_MISSION_INITIAL_PRIORITY;
|
|
108
|
-
this.state = AttackMissionState.Attacking;
|
|
109
|
-
return noop();
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
private handleAttackingState(
|
|
114
|
-
gameApi: GameApi,
|
|
115
|
-
actionsApi: ActionsApi,
|
|
116
|
-
playerData: PlayerData,
|
|
117
|
-
matchAwareness: MatchAwareness,
|
|
118
|
-
actionBatcher: ActionBatcher,
|
|
119
|
-
) {
|
|
120
|
-
if (this.getUnitIds().length === 0) {
|
|
121
|
-
// TODO: disband directly (we no longer retreat when losing)
|
|
122
|
-
this.state = AttackMissionState.Retreating;
|
|
123
|
-
return noop();
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
const foundTargets = matchAwareness
|
|
127
|
-
.getHostilesNearPoint2d(this.attackArea, this.radius)
|
|
128
|
-
.map((unit) => gameApi.getUnitData(unit.unitId))
|
|
129
|
-
.filter((unit) => !isOwnedByNeutral(unit)) as UnitData[];
|
|
130
|
-
|
|
131
|
-
const update = this.squad.onAiUpdate(
|
|
132
|
-
gameApi,
|
|
133
|
-
actionsApi,
|
|
134
|
-
actionBatcher,
|
|
135
|
-
playerData,
|
|
136
|
-
this,
|
|
137
|
-
matchAwareness,
|
|
138
|
-
this.logger,
|
|
139
|
-
);
|
|
140
|
-
|
|
141
|
-
if (update.type !== "noop") {
|
|
142
|
-
return update;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
if (foundTargets.length > 0) {
|
|
146
|
-
this.lastTargetSeenAt = gameApi.getCurrentTick();
|
|
147
|
-
this.hasPickedNewTarget = false;
|
|
148
|
-
} else if (gameApi.getCurrentTick() > this.lastTargetSeenAt + NO_TARGET_IDLE_TIMEOUT_TICKS) {
|
|
149
|
-
return disbandMission(AttackFailReason.NoTargets);
|
|
150
|
-
} else if (
|
|
151
|
-
!this.hasPickedNewTarget &&
|
|
152
|
-
gameApi.getCurrentTick() > this.lastTargetSeenAt + NO_TARGET_RETARGET_TICKS
|
|
153
|
-
) {
|
|
154
|
-
const newTarget = generateTarget(gameApi, playerData, matchAwareness);
|
|
155
|
-
if (newTarget) {
|
|
156
|
-
this.squad.setAttackArea(newTarget);
|
|
157
|
-
this.hasPickedNewTarget = true;
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
return noop();
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
private handleRetreatingState(
|
|
165
|
-
gameApi: GameApi,
|
|
166
|
-
actionsApi: ActionsApi,
|
|
167
|
-
playerData: PlayerData,
|
|
168
|
-
matchAwareness: MatchAwareness,
|
|
169
|
-
actionBatcher: ActionBatcher,
|
|
170
|
-
) {
|
|
171
|
-
this.getUnits(gameApi).forEach((unitId) => {
|
|
172
|
-
actionBatcher.push(manageMoveMicro(unitId, matchAwareness.getMainRallyPoint()));
|
|
173
|
-
});
|
|
174
|
-
return disbandMission();
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
public getGlobalDebugText(): string | undefined {
|
|
178
|
-
return this.squad.getGlobalDebugText() ?? "<none>";
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
public getState() {
|
|
182
|
-
return this.state;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
// This mission can give up its units while preparing.
|
|
186
|
-
public isUnitsLocked(): boolean {
|
|
187
|
-
return this.state !== AttackMissionState.Preparing;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
public getPriority() {
|
|
191
|
-
return this.priority;
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
// Calculates the weight for initiating an attack on the position of a unit or building.
|
|
196
|
-
// This is separate from unit micro; the squad will be ordered to attack in the vicinity of the point.
|
|
197
|
-
const getTargetWeight: (unitData: UnitData, tryFocusHarvester: boolean) => number = (unitData, tryFocusHarvester) => {
|
|
198
|
-
if (tryFocusHarvester && unitData.rules.harvester) {
|
|
199
|
-
return 100000;
|
|
200
|
-
} else if (unitData.type === ObjectType.Building) {
|
|
201
|
-
return unitData.maxHitPoints * 10;
|
|
202
|
-
} else {
|
|
203
|
-
return unitData.maxHitPoints;
|
|
204
|
-
}
|
|
205
|
-
};
|
|
206
|
-
|
|
207
|
-
function generateTarget(
|
|
208
|
-
gameApi: GameApi,
|
|
209
|
-
playerData: PlayerData,
|
|
210
|
-
matchAwareness: MatchAwareness,
|
|
211
|
-
includeBaseLocations: boolean = false,
|
|
212
|
-
): Vector2 | null {
|
|
213
|
-
// Randomly decide between harvester and base.
|
|
214
|
-
try {
|
|
215
|
-
const tryFocusHarvester = gameApi.generateRandomInt(0, 1) === 0;
|
|
216
|
-
const enemyUnits = gameApi
|
|
217
|
-
.getVisibleUnits(playerData.name, "enemy")
|
|
218
|
-
.map((unitId) => gameApi.getUnitData(unitId))
|
|
219
|
-
.filter((u) => !!u && gameApi.getPlayerData(u.owner).isCombatant) as UnitData[];
|
|
220
|
-
|
|
221
|
-
const maxUnit = maxBy(enemyUnits, (u) => getTargetWeight(u, tryFocusHarvester));
|
|
222
|
-
if (maxUnit) {
|
|
223
|
-
return new Vector2(maxUnit.tile.rx, maxUnit.tile.ry);
|
|
224
|
-
}
|
|
225
|
-
if (includeBaseLocations) {
|
|
226
|
-
const mapApi = gameApi.mapApi;
|
|
227
|
-
const enemyPlayers = gameApi
|
|
228
|
-
.getPlayers()
|
|
229
|
-
.map(gameApi.getPlayerData)
|
|
230
|
-
.filter((otherPlayer) => !gameApi.areAlliedPlayers(playerData.name, otherPlayer.name));
|
|
231
|
-
|
|
232
|
-
const unexploredEnemyLocations = enemyPlayers.filter((otherPlayer) => {
|
|
233
|
-
const tile = mapApi.getTile(otherPlayer.startLocation.x, otherPlayer.startLocation.y);
|
|
234
|
-
if (!tile) {
|
|
235
|
-
return false;
|
|
236
|
-
}
|
|
237
|
-
return !mapApi.isVisibleTile(tile, playerData.name);
|
|
238
|
-
});
|
|
239
|
-
if (unexploredEnemyLocations.length > 0) {
|
|
240
|
-
const idx = gameApi.generateRandomInt(0, unexploredEnemyLocations.length - 1);
|
|
241
|
-
return unexploredEnemyLocations[idx].startLocation;
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
} catch (err) {
|
|
245
|
-
// There's a crash here when accessing a building that got destroyed. Will catch and ignore or now.
|
|
246
|
-
return null;
|
|
247
|
-
}
|
|
248
|
-
return null;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
// Number of ticks between attacking visible targets.
|
|
252
|
-
const VISIBLE_TARGET_ATTACK_COOLDOWN_TICKS = 120;
|
|
253
|
-
|
|
254
|
-
// Number of ticks between attacking "bases" (enemy starting locations).
|
|
255
|
-
const BASE_ATTACK_COOLDOWN_TICKS = 1800;
|
|
256
|
-
|
|
257
|
-
const ATTACK_MISSION_INITIAL_PRIORITY = 1;
|
|
258
|
-
|
|
259
|
-
export class AttackMissionFactory implements MissionFactory {
|
|
260
|
-
constructor(private lastAttackAt: number = -VISIBLE_TARGET_ATTACK_COOLDOWN_TICKS) {}
|
|
261
|
-
|
|
262
|
-
getName(): string {
|
|
263
|
-
return "AttackMissionFactory";
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
maybeCreateMissions(
|
|
267
|
-
gameApi: GameApi,
|
|
268
|
-
playerData: PlayerData,
|
|
269
|
-
matchAwareness: MatchAwareness,
|
|
270
|
-
missionController: MissionController,
|
|
271
|
-
logger: DebugLogger,
|
|
272
|
-
): void {
|
|
273
|
-
if (gameApi.getCurrentTick() < this.lastAttackAt + VISIBLE_TARGET_ATTACK_COOLDOWN_TICKS) {
|
|
274
|
-
return;
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
// can only have one attack 'preparing' at once.
|
|
278
|
-
if (
|
|
279
|
-
missionController
|
|
280
|
-
.getMissions()
|
|
281
|
-
.some(
|
|
282
|
-
(mission): mission is AttackMission =>
|
|
283
|
-
mission instanceof AttackMission && mission.getState() === AttackMissionState.Preparing,
|
|
284
|
-
)
|
|
285
|
-
) {
|
|
286
|
-
return;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
const attackRadius = 10;
|
|
290
|
-
|
|
291
|
-
const includeEnemyBases = gameApi.getCurrentTick() > this.lastAttackAt + BASE_ATTACK_COOLDOWN_TICKS;
|
|
292
|
-
|
|
293
|
-
const attackArea = generateTarget(gameApi, playerData, matchAwareness, includeEnemyBases);
|
|
294
|
-
|
|
295
|
-
if (!attackArea) {
|
|
296
|
-
return;
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
const squadName = "attack_" + gameApi.getCurrentTick();
|
|
300
|
-
|
|
301
|
-
const composition: UnitComposition = calculateTargetComposition(gameApi, playerData, matchAwareness);
|
|
302
|
-
|
|
303
|
-
const tryAttack = missionController.addMission(
|
|
304
|
-
new AttackMission(
|
|
305
|
-
squadName,
|
|
306
|
-
ATTACK_MISSION_INITIAL_PRIORITY,
|
|
307
|
-
matchAwareness.getMainRallyPoint(),
|
|
308
|
-
attackArea,
|
|
309
|
-
attackRadius,
|
|
310
|
-
composition,
|
|
311
|
-
logger,
|
|
312
|
-
).then((unitIds, reason) => {
|
|
313
|
-
missionController.addMission(
|
|
314
|
-
new RetreatMission(
|
|
315
|
-
"retreat-from-" + squadName + gameApi.getCurrentTick(),
|
|
316
|
-
matchAwareness.getMainRallyPoint(),
|
|
317
|
-
unitIds,
|
|
318
|
-
logger,
|
|
319
|
-
),
|
|
320
|
-
);
|
|
321
|
-
}),
|
|
322
|
-
);
|
|
323
|
-
if (tryAttack) {
|
|
324
|
-
this.lastAttackAt = gameApi.getCurrentTick();
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
onMissionFailed(
|
|
329
|
-
gameApi: GameApi,
|
|
330
|
-
playerData: PlayerData,
|
|
331
|
-
matchAwareness: MatchAwareness,
|
|
332
|
-
failedMission: Mission<any>,
|
|
333
|
-
failureReason: any,
|
|
334
|
-
missionController: MissionController,
|
|
335
|
-
): void {}
|
|
336
|
-
}
|
|
1
|
+
import { ActionsApi, GameApi, ObjectType, PlayerData, SideType, UnitData, Vector2 } from "@chronodivide/game-api";
|
|
2
|
+
import { CombatSquad } from "./squads/combatSquad.js";
|
|
3
|
+
import { Mission, MissionAction, disbandMission, noop, requestUnits } from "../mission.js";
|
|
4
|
+
import { MissionFactory } from "../missionFactories.js";
|
|
5
|
+
import { MatchAwareness } from "../../awareness.js";
|
|
6
|
+
import { MissionController } from "../missionController.js";
|
|
7
|
+
import { RetreatMission } from "./retreatMission.js";
|
|
8
|
+
import { DebugLogger, countBy, isOwnedByNeutral, maxBy } from "../../common/utils.js";
|
|
9
|
+
import { ActionBatcher } from "../actionBatcher.js";
|
|
10
|
+
import { getSovietComposition } from "../../composition/sovietCompositions.js";
|
|
11
|
+
import { getAlliedCompositions } from "../../composition/alliedCompositions.js";
|
|
12
|
+
import { UnitComposition } from "../../composition/common.js";
|
|
13
|
+
import { manageMoveMicro } from "./squads/common.js";
|
|
14
|
+
|
|
15
|
+
export enum AttackFailReason {
|
|
16
|
+
NoTargets = 0,
|
|
17
|
+
DefenceTooStrong = 1,
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
enum AttackMissionState {
|
|
21
|
+
Preparing = 0,
|
|
22
|
+
Attacking = 1,
|
|
23
|
+
Retreating = 2,
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const NO_TARGET_RETARGET_TICKS = 450;
|
|
27
|
+
const NO_TARGET_IDLE_TIMEOUT_TICKS = 900;
|
|
28
|
+
|
|
29
|
+
function calculateTargetComposition(
|
|
30
|
+
gameApi: GameApi,
|
|
31
|
+
playerData: PlayerData,
|
|
32
|
+
matchAwareness: MatchAwareness,
|
|
33
|
+
): UnitComposition {
|
|
34
|
+
if (!playerData.country) {
|
|
35
|
+
throw new Error(`player ${playerData.name} has no country`);
|
|
36
|
+
} else if (playerData.country.side === SideType.Nod) {
|
|
37
|
+
return getSovietComposition(gameApi, playerData, matchAwareness);
|
|
38
|
+
} else {
|
|
39
|
+
return getAlliedCompositions(gameApi, playerData, matchAwareness);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const ATTACK_MISSION_PRIORITY_RAMP = 1.01;
|
|
44
|
+
const ATTACK_MISSION_MAX_PRIORITY = 50;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* A mission that tries to attack a certain area.
|
|
48
|
+
*/
|
|
49
|
+
export class AttackMission extends Mission<AttackFailReason> {
|
|
50
|
+
private squad: CombatSquad;
|
|
51
|
+
|
|
52
|
+
private lastTargetSeenAt = 0;
|
|
53
|
+
private hasPickedNewTarget: boolean = false;
|
|
54
|
+
|
|
55
|
+
private state: AttackMissionState = AttackMissionState.Preparing;
|
|
56
|
+
|
|
57
|
+
constructor(
|
|
58
|
+
uniqueName: string,
|
|
59
|
+
private priority: number,
|
|
60
|
+
rallyArea: Vector2,
|
|
61
|
+
private attackArea: Vector2,
|
|
62
|
+
private radius: number,
|
|
63
|
+
private composition: UnitComposition,
|
|
64
|
+
logger: DebugLogger,
|
|
65
|
+
) {
|
|
66
|
+
super(uniqueName, logger);
|
|
67
|
+
this.squad = new CombatSquad(rallyArea, attackArea, radius);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
_onAiUpdate(
|
|
71
|
+
gameApi: GameApi,
|
|
72
|
+
actionsApi: ActionsApi,
|
|
73
|
+
playerData: PlayerData,
|
|
74
|
+
matchAwareness: MatchAwareness,
|
|
75
|
+
actionBatcher: ActionBatcher,
|
|
76
|
+
): MissionAction {
|
|
77
|
+
switch (this.state) {
|
|
78
|
+
case AttackMissionState.Preparing:
|
|
79
|
+
return this.handlePreparingState(gameApi, actionsApi, playerData, matchAwareness, actionBatcher);
|
|
80
|
+
case AttackMissionState.Attacking:
|
|
81
|
+
return this.handleAttackingState(gameApi, actionsApi, playerData, matchAwareness, actionBatcher);
|
|
82
|
+
case AttackMissionState.Retreating:
|
|
83
|
+
return this.handleRetreatingState(gameApi, actionsApi, playerData, matchAwareness, actionBatcher);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
private handlePreparingState(
|
|
88
|
+
gameApi: GameApi,
|
|
89
|
+
actionsApi: ActionsApi,
|
|
90
|
+
playerData: PlayerData,
|
|
91
|
+
matchAwareness: MatchAwareness,
|
|
92
|
+
actionBatcher: ActionBatcher,
|
|
93
|
+
) {
|
|
94
|
+
const currentComposition: UnitComposition = countBy(this.getUnits(gameApi), (unit) => unit.name);
|
|
95
|
+
|
|
96
|
+
const missingUnits = Object.entries(this.composition).filter(([unitType, targetAmount]) => {
|
|
97
|
+
return !currentComposition[unitType] || currentComposition[unitType] < targetAmount;
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
if (missingUnits.length > 0) {
|
|
101
|
+
this.priority = Math.min(this.priority * ATTACK_MISSION_PRIORITY_RAMP, ATTACK_MISSION_MAX_PRIORITY);
|
|
102
|
+
return requestUnits(
|
|
103
|
+
missingUnits.map(([unitName]) => unitName),
|
|
104
|
+
this.priority,
|
|
105
|
+
);
|
|
106
|
+
} else {
|
|
107
|
+
this.priority = ATTACK_MISSION_INITIAL_PRIORITY;
|
|
108
|
+
this.state = AttackMissionState.Attacking;
|
|
109
|
+
return noop();
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
private handleAttackingState(
|
|
114
|
+
gameApi: GameApi,
|
|
115
|
+
actionsApi: ActionsApi,
|
|
116
|
+
playerData: PlayerData,
|
|
117
|
+
matchAwareness: MatchAwareness,
|
|
118
|
+
actionBatcher: ActionBatcher,
|
|
119
|
+
) {
|
|
120
|
+
if (this.getUnitIds().length === 0) {
|
|
121
|
+
// TODO: disband directly (we no longer retreat when losing)
|
|
122
|
+
this.state = AttackMissionState.Retreating;
|
|
123
|
+
return noop();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const foundTargets = matchAwareness
|
|
127
|
+
.getHostilesNearPoint2d(this.attackArea, this.radius)
|
|
128
|
+
.map((unit) => gameApi.getUnitData(unit.unitId))
|
|
129
|
+
.filter((unit) => !isOwnedByNeutral(unit)) as UnitData[];
|
|
130
|
+
|
|
131
|
+
const update = this.squad.onAiUpdate(
|
|
132
|
+
gameApi,
|
|
133
|
+
actionsApi,
|
|
134
|
+
actionBatcher,
|
|
135
|
+
playerData,
|
|
136
|
+
this,
|
|
137
|
+
matchAwareness,
|
|
138
|
+
this.logger,
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
if (update.type !== "noop") {
|
|
142
|
+
return update;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (foundTargets.length > 0) {
|
|
146
|
+
this.lastTargetSeenAt = gameApi.getCurrentTick();
|
|
147
|
+
this.hasPickedNewTarget = false;
|
|
148
|
+
} else if (gameApi.getCurrentTick() > this.lastTargetSeenAt + NO_TARGET_IDLE_TIMEOUT_TICKS) {
|
|
149
|
+
return disbandMission(AttackFailReason.NoTargets);
|
|
150
|
+
} else if (
|
|
151
|
+
!this.hasPickedNewTarget &&
|
|
152
|
+
gameApi.getCurrentTick() > this.lastTargetSeenAt + NO_TARGET_RETARGET_TICKS
|
|
153
|
+
) {
|
|
154
|
+
const newTarget = generateTarget(gameApi, playerData, matchAwareness);
|
|
155
|
+
if (newTarget) {
|
|
156
|
+
this.squad.setAttackArea(newTarget);
|
|
157
|
+
this.hasPickedNewTarget = true;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return noop();
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
private handleRetreatingState(
|
|
165
|
+
gameApi: GameApi,
|
|
166
|
+
actionsApi: ActionsApi,
|
|
167
|
+
playerData: PlayerData,
|
|
168
|
+
matchAwareness: MatchAwareness,
|
|
169
|
+
actionBatcher: ActionBatcher,
|
|
170
|
+
) {
|
|
171
|
+
this.getUnits(gameApi).forEach((unitId) => {
|
|
172
|
+
actionBatcher.push(manageMoveMicro(unitId, matchAwareness.getMainRallyPoint()));
|
|
173
|
+
});
|
|
174
|
+
return disbandMission();
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
public getGlobalDebugText(): string | undefined {
|
|
178
|
+
return this.squad.getGlobalDebugText() ?? "<none>";
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
public getState() {
|
|
182
|
+
return this.state;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// This mission can give up its units while preparing.
|
|
186
|
+
public isUnitsLocked(): boolean {
|
|
187
|
+
return this.state !== AttackMissionState.Preparing;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
public getPriority() {
|
|
191
|
+
return this.priority;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Calculates the weight for initiating an attack on the position of a unit or building.
|
|
196
|
+
// This is separate from unit micro; the squad will be ordered to attack in the vicinity of the point.
|
|
197
|
+
const getTargetWeight: (unitData: UnitData, tryFocusHarvester: boolean) => number = (unitData, tryFocusHarvester) => {
|
|
198
|
+
if (tryFocusHarvester && unitData.rules.harvester) {
|
|
199
|
+
return 100000;
|
|
200
|
+
} else if (unitData.type === ObjectType.Building) {
|
|
201
|
+
return unitData.maxHitPoints * 10;
|
|
202
|
+
} else {
|
|
203
|
+
return unitData.maxHitPoints;
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
function generateTarget(
|
|
208
|
+
gameApi: GameApi,
|
|
209
|
+
playerData: PlayerData,
|
|
210
|
+
matchAwareness: MatchAwareness,
|
|
211
|
+
includeBaseLocations: boolean = false,
|
|
212
|
+
): Vector2 | null {
|
|
213
|
+
// Randomly decide between harvester and base.
|
|
214
|
+
try {
|
|
215
|
+
const tryFocusHarvester = gameApi.generateRandomInt(0, 1) === 0;
|
|
216
|
+
const enemyUnits = gameApi
|
|
217
|
+
.getVisibleUnits(playerData.name, "enemy")
|
|
218
|
+
.map((unitId) => gameApi.getUnitData(unitId))
|
|
219
|
+
.filter((u) => !!u && gameApi.getPlayerData(u.owner).isCombatant) as UnitData[];
|
|
220
|
+
|
|
221
|
+
const maxUnit = maxBy(enemyUnits, (u) => getTargetWeight(u, tryFocusHarvester));
|
|
222
|
+
if (maxUnit) {
|
|
223
|
+
return new Vector2(maxUnit.tile.rx, maxUnit.tile.ry);
|
|
224
|
+
}
|
|
225
|
+
if (includeBaseLocations) {
|
|
226
|
+
const mapApi = gameApi.mapApi;
|
|
227
|
+
const enemyPlayers = gameApi
|
|
228
|
+
.getPlayers()
|
|
229
|
+
.map(gameApi.getPlayerData)
|
|
230
|
+
.filter((otherPlayer) => !gameApi.areAlliedPlayers(playerData.name, otherPlayer.name));
|
|
231
|
+
|
|
232
|
+
const unexploredEnemyLocations = enemyPlayers.filter((otherPlayer) => {
|
|
233
|
+
const tile = mapApi.getTile(otherPlayer.startLocation.x, otherPlayer.startLocation.y);
|
|
234
|
+
if (!tile) {
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
return !mapApi.isVisibleTile(tile, playerData.name);
|
|
238
|
+
});
|
|
239
|
+
if (unexploredEnemyLocations.length > 0) {
|
|
240
|
+
const idx = gameApi.generateRandomInt(0, unexploredEnemyLocations.length - 1);
|
|
241
|
+
return unexploredEnemyLocations[idx].startLocation;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
} catch (err) {
|
|
245
|
+
// There's a crash here when accessing a building that got destroyed. Will catch and ignore or now.
|
|
246
|
+
return null;
|
|
247
|
+
}
|
|
248
|
+
return null;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// Number of ticks between attacking visible targets.
|
|
252
|
+
const VISIBLE_TARGET_ATTACK_COOLDOWN_TICKS = 120;
|
|
253
|
+
|
|
254
|
+
// Number of ticks between attacking "bases" (enemy starting locations).
|
|
255
|
+
const BASE_ATTACK_COOLDOWN_TICKS = 1800;
|
|
256
|
+
|
|
257
|
+
const ATTACK_MISSION_INITIAL_PRIORITY = 1;
|
|
258
|
+
|
|
259
|
+
export class AttackMissionFactory implements MissionFactory {
|
|
260
|
+
constructor(private lastAttackAt: number = -VISIBLE_TARGET_ATTACK_COOLDOWN_TICKS) {}
|
|
261
|
+
|
|
262
|
+
getName(): string {
|
|
263
|
+
return "AttackMissionFactory";
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
maybeCreateMissions(
|
|
267
|
+
gameApi: GameApi,
|
|
268
|
+
playerData: PlayerData,
|
|
269
|
+
matchAwareness: MatchAwareness,
|
|
270
|
+
missionController: MissionController,
|
|
271
|
+
logger: DebugLogger,
|
|
272
|
+
): void {
|
|
273
|
+
if (gameApi.getCurrentTick() < this.lastAttackAt + VISIBLE_TARGET_ATTACK_COOLDOWN_TICKS) {
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// can only have one attack 'preparing' at once.
|
|
278
|
+
if (
|
|
279
|
+
missionController
|
|
280
|
+
.getMissions()
|
|
281
|
+
.some(
|
|
282
|
+
(mission): mission is AttackMission =>
|
|
283
|
+
mission instanceof AttackMission && mission.getState() === AttackMissionState.Preparing,
|
|
284
|
+
)
|
|
285
|
+
) {
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const attackRadius = 10;
|
|
290
|
+
|
|
291
|
+
const includeEnemyBases = gameApi.getCurrentTick() > this.lastAttackAt + BASE_ATTACK_COOLDOWN_TICKS;
|
|
292
|
+
|
|
293
|
+
const attackArea = generateTarget(gameApi, playerData, matchAwareness, includeEnemyBases);
|
|
294
|
+
|
|
295
|
+
if (!attackArea) {
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
const squadName = "attack_" + gameApi.getCurrentTick();
|
|
300
|
+
|
|
301
|
+
const composition: UnitComposition = calculateTargetComposition(gameApi, playerData, matchAwareness);
|
|
302
|
+
|
|
303
|
+
const tryAttack = missionController.addMission(
|
|
304
|
+
new AttackMission(
|
|
305
|
+
squadName,
|
|
306
|
+
ATTACK_MISSION_INITIAL_PRIORITY,
|
|
307
|
+
matchAwareness.getMainRallyPoint(),
|
|
308
|
+
attackArea,
|
|
309
|
+
attackRadius,
|
|
310
|
+
composition,
|
|
311
|
+
logger,
|
|
312
|
+
).then((unitIds, reason) => {
|
|
313
|
+
missionController.addMission(
|
|
314
|
+
new RetreatMission(
|
|
315
|
+
"retreat-from-" + squadName + gameApi.getCurrentTick(),
|
|
316
|
+
matchAwareness.getMainRallyPoint(),
|
|
317
|
+
unitIds,
|
|
318
|
+
logger,
|
|
319
|
+
),
|
|
320
|
+
);
|
|
321
|
+
}),
|
|
322
|
+
);
|
|
323
|
+
if (tryAttack) {
|
|
324
|
+
this.lastAttackAt = gameApi.getCurrentTick();
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
onMissionFailed(
|
|
329
|
+
gameApi: GameApi,
|
|
330
|
+
playerData: PlayerData,
|
|
331
|
+
matchAwareness: MatchAwareness,
|
|
332
|
+
failedMission: Mission<any>,
|
|
333
|
+
failureReason: any,
|
|
334
|
+
missionController: MissionController,
|
|
335
|
+
): void {}
|
|
336
|
+
}
|