@supalosa/chronodivide-bot 0.5.3 → 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.
Files changed (134) hide show
  1. package/.env.template +4 -4
  2. package/.github/workflows/npm-publish.yml +24 -0
  3. package/README.md +108 -97
  4. package/dist/bot/bot.js +105 -105
  5. package/dist/bot/bot.js.map +1 -1
  6. package/dist/bot/logic/awareness.js +136 -136
  7. package/dist/bot/logic/building/antiAirStaticDefence.js +42 -42
  8. package/dist/bot/logic/building/antiGroundStaticDefence.js +34 -30
  9. package/dist/bot/logic/building/antiGroundStaticDefence.js.map +1 -1
  10. package/dist/bot/logic/building/{ArtilleryUnit.js → artilleryUnit.js} +18 -18
  11. package/dist/bot/logic/building/basicAirUnit.js +19 -19
  12. package/dist/bot/logic/building/basicBuilding.js +26 -26
  13. package/dist/bot/logic/building/basicGroundUnit.js +19 -19
  14. package/dist/bot/logic/building/buildingRules.js +175 -174
  15. package/dist/bot/logic/building/buildingRules.js.map +1 -1
  16. package/dist/bot/logic/building/common.js +19 -18
  17. package/dist/bot/logic/building/common.js.map +1 -1
  18. package/dist/bot/logic/building/harvester.js +16 -16
  19. package/dist/bot/logic/building/powerPlant.js +20 -20
  20. package/dist/bot/logic/building/queueController.js +183 -183
  21. package/dist/bot/logic/building/resourceCollectionBuilding.js +36 -36
  22. package/dist/bot/logic/common/scout.js +126 -126
  23. package/dist/bot/logic/common/utils.js +95 -85
  24. package/dist/bot/logic/common/utils.js.map +1 -1
  25. package/dist/bot/logic/composition/alliedCompositions.js +12 -12
  26. package/dist/bot/logic/composition/common.js +1 -1
  27. package/dist/bot/logic/composition/sovietCompositions.js +12 -12
  28. package/dist/bot/logic/map/map.js +44 -44
  29. package/dist/bot/logic/map/sector.js +137 -137
  30. package/dist/bot/logic/mission/actionBatcher.js +91 -91
  31. package/dist/bot/logic/mission/mission.js +122 -122
  32. package/dist/bot/logic/mission/missionController.js +321 -321
  33. package/dist/bot/logic/mission/missionFactories.js +12 -12
  34. package/dist/bot/logic/mission/missions/attackMission.js +214 -214
  35. package/dist/bot/logic/mission/missions/defenceMission.js +82 -82
  36. package/dist/bot/logic/mission/missions/engineerMission.js +63 -63
  37. package/dist/bot/logic/mission/missions/expansionMission.js +60 -60
  38. package/dist/bot/logic/mission/missions/retreatMission.js +33 -33
  39. package/dist/bot/logic/mission/missions/scoutingMission.js +133 -133
  40. package/dist/bot/logic/mission/missions/squads/combatSquad.js +115 -115
  41. package/dist/bot/logic/mission/missions/squads/common.js +57 -57
  42. package/dist/bot/logic/mission/missions/squads/squad.js +1 -1
  43. package/dist/bot/logic/threat/threat.js +22 -22
  44. package/dist/bot/logic/threat/threatCalculator.js +73 -73
  45. package/dist/exampleBot.js +100 -112
  46. package/dist/exampleBot.js.map +1 -1
  47. package/package.json +32 -29
  48. package/src/bot/bot.ts +161 -161
  49. package/src/bot/logic/awareness.ts +245 -245
  50. package/src/bot/logic/building/antiAirStaticDefence.ts +64 -64
  51. package/src/bot/logic/building/antiGroundStaticDefence.ts +55 -51
  52. package/src/bot/logic/building/artilleryUnit.ts +39 -39
  53. package/src/bot/logic/building/basicAirUnit.ts +39 -39
  54. package/src/bot/logic/building/basicBuilding.ts +49 -49
  55. package/src/bot/logic/building/basicGroundUnit.ts +39 -39
  56. package/src/bot/logic/building/buildingRules.ts +250 -247
  57. package/src/bot/logic/building/common.ts +21 -23
  58. package/src/bot/logic/building/harvester.ts +31 -31
  59. package/src/bot/logic/building/powerPlant.ts +32 -32
  60. package/src/bot/logic/building/queueController.ts +297 -297
  61. package/src/bot/logic/building/resourceCollectionBuilding.ts +52 -52
  62. package/src/bot/logic/common/scout.ts +183 -183
  63. package/src/bot/logic/common/utils.ts +120 -112
  64. package/src/bot/logic/composition/alliedCompositions.ts +22 -22
  65. package/src/bot/logic/composition/common.ts +3 -3
  66. package/src/bot/logic/composition/sovietCompositions.ts +21 -21
  67. package/src/bot/logic/map/map.ts +66 -66
  68. package/src/bot/logic/map/sector.ts +174 -174
  69. package/src/bot/logic/mission/actionBatcher.ts +124 -124
  70. package/src/bot/logic/mission/mission.ts +232 -232
  71. package/src/bot/logic/mission/missionController.ts +413 -413
  72. package/src/bot/logic/mission/missionFactories.ts +51 -51
  73. package/src/bot/logic/mission/missions/attackMission.ts +336 -336
  74. package/src/bot/logic/mission/missions/defenceMission.ts +151 -151
  75. package/src/bot/logic/mission/missions/engineerMission.ts +113 -113
  76. package/src/bot/logic/mission/missions/expansionMission.ts +104 -104
  77. package/src/bot/logic/mission/missions/retreatMission.ts +54 -54
  78. package/src/bot/logic/mission/missions/scoutingMission.ts +186 -186
  79. package/src/bot/logic/mission/missions/squads/combatSquad.ts +160 -160
  80. package/src/bot/logic/mission/missions/squads/common.ts +63 -63
  81. package/src/bot/logic/mission/missions/squads/squad.ts +19 -19
  82. package/src/bot/logic/threat/threatCalculator.ts +100 -100
  83. package/src/exampleBot.ts +111 -124
  84. package/tsconfig.json +73 -73
  85. package/dist/bot/logic/building/building.js +0 -82
  86. package/dist/bot/logic/building/massedAntiGroundUnit.js +0 -20
  87. package/dist/bot/logic/building/queues.js +0 -19
  88. package/dist/bot/logic/knowledge.js +0 -1
  89. package/dist/bot/logic/mission/basicMission.js +0 -26
  90. package/dist/bot/logic/mission/behaviours/combatSquad.js +0 -124
  91. package/dist/bot/logic/mission/behaviours/combatSquad.js.map +0 -1
  92. package/dist/bot/logic/mission/behaviours/common.js +0 -56
  93. package/dist/bot/logic/mission/behaviours/common.js.map +0 -1
  94. package/dist/bot/logic/mission/behaviours/engineerSquad.js +0 -39
  95. package/dist/bot/logic/mission/behaviours/engineerSquad.js.map +0 -1
  96. package/dist/bot/logic/mission/behaviours/expansionSquad.js +0 -46
  97. package/dist/bot/logic/mission/behaviours/expansionSquad.js.map +0 -1
  98. package/dist/bot/logic/mission/behaviours/retreatSquad.js +0 -31
  99. package/dist/bot/logic/mission/behaviours/retreatSquad.js.map +0 -1
  100. package/dist/bot/logic/mission/behaviours/scoutingSquad.js +0 -94
  101. package/dist/bot/logic/mission/behaviours/scoutingSquad.js.map +0 -1
  102. package/dist/bot/logic/mission/expansionMission.js +0 -32
  103. package/dist/bot/logic/mission/missions/basicMission.js +0 -13
  104. package/dist/bot/logic/mission/missions/basicMission.js.map +0 -1
  105. package/dist/bot/logic/mission/missions/missionBehaviour.js +0 -2
  106. package/dist/bot/logic/mission/missions/missionBehaviour.js.map +0 -1
  107. package/dist/bot/logic/mission/missions/oneTimeMission.js +0 -27
  108. package/dist/bot/logic/mission/missions/oneTimeMission.js.map +0 -1
  109. package/dist/bot/logic/squad/behaviours/actionBatcher.js +0 -36
  110. package/dist/bot/logic/squad/behaviours/actionBatcher.js.map +0 -1
  111. package/dist/bot/logic/squad/behaviours/attackSquad.js +0 -82
  112. package/dist/bot/logic/squad/behaviours/combatSquad.js +0 -106
  113. package/dist/bot/logic/squad/behaviours/combatSquad.js.map +0 -1
  114. package/dist/bot/logic/squad/behaviours/common.js +0 -55
  115. package/dist/bot/logic/squad/behaviours/common.js.map +0 -1
  116. package/dist/bot/logic/squad/behaviours/defenceSquad.js +0 -48
  117. package/dist/bot/logic/squad/behaviours/engineerSquad.js +0 -38
  118. package/dist/bot/logic/squad/behaviours/engineerSquad.js.map +0 -1
  119. package/dist/bot/logic/squad/behaviours/expansionSquad.js +0 -45
  120. package/dist/bot/logic/squad/behaviours/expansionSquad.js.map +0 -1
  121. package/dist/bot/logic/squad/behaviours/retreatSquad.js +0 -31
  122. package/dist/bot/logic/squad/behaviours/retreatSquad.js.map +0 -1
  123. package/dist/bot/logic/squad/behaviours/scoutingSquad.js +0 -93
  124. package/dist/bot/logic/squad/behaviours/scoutingSquad.js.map +0 -1
  125. package/dist/bot/logic/squad/behaviours/squadExpansion.js +0 -31
  126. package/dist/bot/logic/squad/behaviours/squadScouters.js +0 -8
  127. package/dist/bot/logic/squad/squad.js +0 -126
  128. package/dist/bot/logic/squad/squad.js.map +0 -1
  129. package/dist/bot/logic/squad/squadBehaviour.js +0 -6
  130. package/dist/bot/logic/squad/squadBehaviour.js.map +0 -1
  131. package/dist/bot/logic/squad/squadBehaviours.js +0 -7
  132. package/dist/bot/logic/squad/squadBehaviours.js.map +0 -1
  133. package/dist/bot/logic/squad/squadController.js +0 -215
  134. package/dist/bot/logic/squad/squadController.js.map +0 -1
@@ -1,13 +1,13 @@
1
- import { ExpansionMissionFactory } from "./missions/expansionMission.js";
2
- import { ScoutingMissionFactory } from "./missions/scoutingMission.js";
3
- import { AttackMissionFactory } from "./missions/attackMission.js";
4
- import { DefenceMissionFactory } from "./missions/defenceMission.js";
5
- import { EngineerMissionFactory } from "./missions/engineerMission.js";
6
- export const createMissionFactories = () => [
7
- new ExpansionMissionFactory(),
8
- new ScoutingMissionFactory(),
9
- new AttackMissionFactory(),
10
- new DefenceMissionFactory(),
11
- new EngineerMissionFactory(),
12
- ];
1
+ import { ExpansionMissionFactory } from "./missions/expansionMission.js";
2
+ import { ScoutingMissionFactory } from "./missions/scoutingMission.js";
3
+ import { AttackMissionFactory } from "./missions/attackMission.js";
4
+ import { DefenceMissionFactory } from "./missions/defenceMission.js";
5
+ import { EngineerMissionFactory } from "./missions/engineerMission.js";
6
+ export const createMissionFactories = () => [
7
+ new ExpansionMissionFactory(),
8
+ new ScoutingMissionFactory(),
9
+ new AttackMissionFactory(),
10
+ new DefenceMissionFactory(),
11
+ new EngineerMissionFactory(),
12
+ ];
13
13
  //# sourceMappingURL=missionFactories.js.map
@@ -1,215 +1,215 @@
1
- import { ObjectType, SideType, Vector2 } from "@chronodivide/game-api";
2
- import { CombatSquad } from "./squads/combatSquad.js";
3
- import { Mission, disbandMission, noop, requestUnits } from "../mission.js";
4
- import { RetreatMission } from "./retreatMission.js";
5
- import { countBy, isOwnedByNeutral, maxBy } from "../../common/utils.js";
6
- import { getSovietComposition } from "../../composition/sovietCompositions.js";
7
- import { getAlliedCompositions } from "../../composition/alliedCompositions.js";
8
- import { manageMoveMicro } from "./squads/common.js";
9
- export var AttackFailReason;
10
- (function (AttackFailReason) {
11
- AttackFailReason[AttackFailReason["NoTargets"] = 0] = "NoTargets";
12
- AttackFailReason[AttackFailReason["DefenceTooStrong"] = 1] = "DefenceTooStrong";
13
- })(AttackFailReason || (AttackFailReason = {}));
14
- var AttackMissionState;
15
- (function (AttackMissionState) {
16
- AttackMissionState[AttackMissionState["Preparing"] = 0] = "Preparing";
17
- AttackMissionState[AttackMissionState["Attacking"] = 1] = "Attacking";
18
- AttackMissionState[AttackMissionState["Retreating"] = 2] = "Retreating";
19
- })(AttackMissionState || (AttackMissionState = {}));
20
- const NO_TARGET_RETARGET_TICKS = 450;
21
- const NO_TARGET_IDLE_TIMEOUT_TICKS = 900;
22
- function calculateTargetComposition(gameApi, playerData, matchAwareness) {
23
- if (!playerData.country) {
24
- throw new Error(`player ${playerData.name} has no country`);
25
- }
26
- else if (playerData.country.side === SideType.Nod) {
27
- return getSovietComposition(gameApi, playerData, matchAwareness);
28
- }
29
- else {
30
- return getAlliedCompositions(gameApi, playerData, matchAwareness);
31
- }
32
- }
33
- const ATTACK_MISSION_PRIORITY_RAMP = 1.01;
34
- const ATTACK_MISSION_MAX_PRIORITY = 50;
35
- /**
36
- * A mission that tries to attack a certain area.
37
- */
38
- export class AttackMission extends Mission {
39
- constructor(uniqueName, priority, rallyArea, attackArea, radius, composition, logger) {
40
- super(uniqueName, logger);
41
- this.priority = priority;
42
- this.attackArea = attackArea;
43
- this.radius = radius;
44
- this.composition = composition;
45
- this.lastTargetSeenAt = 0;
46
- this.hasPickedNewTarget = false;
47
- this.state = AttackMissionState.Preparing;
48
- this.squad = new CombatSquad(rallyArea, attackArea, radius);
49
- }
50
- _onAiUpdate(gameApi, actionsApi, playerData, matchAwareness, actionBatcher) {
51
- switch (this.state) {
52
- case AttackMissionState.Preparing:
53
- return this.handlePreparingState(gameApi, actionsApi, playerData, matchAwareness, actionBatcher);
54
- case AttackMissionState.Attacking:
55
- return this.handleAttackingState(gameApi, actionsApi, playerData, matchAwareness, actionBatcher);
56
- case AttackMissionState.Retreating:
57
- return this.handleRetreatingState(gameApi, actionsApi, playerData, matchAwareness, actionBatcher);
58
- }
59
- }
60
- handlePreparingState(gameApi, actionsApi, playerData, matchAwareness, actionBatcher) {
61
- const currentComposition = countBy(this.getUnits(gameApi), (unit) => unit.name);
62
- const missingUnits = Object.entries(this.composition).filter(([unitType, targetAmount]) => {
63
- return !currentComposition[unitType] || currentComposition[unitType] < targetAmount;
64
- });
65
- if (missingUnits.length > 0) {
66
- this.priority = Math.min(this.priority * ATTACK_MISSION_PRIORITY_RAMP, ATTACK_MISSION_MAX_PRIORITY);
67
- return requestUnits(missingUnits.map(([unitName]) => unitName), this.priority);
68
- }
69
- else {
70
- this.priority = ATTACK_MISSION_INITIAL_PRIORITY;
71
- this.state = AttackMissionState.Attacking;
72
- return noop();
73
- }
74
- }
75
- handleAttackingState(gameApi, actionsApi, playerData, matchAwareness, actionBatcher) {
76
- if (this.getUnitIds().length === 0) {
77
- // TODO: disband directly (we no longer retreat when losing)
78
- this.state = AttackMissionState.Retreating;
79
- return noop();
80
- }
81
- const foundTargets = matchAwareness
82
- .getHostilesNearPoint2d(this.attackArea, this.radius)
83
- .map((unit) => gameApi.getUnitData(unit.unitId))
84
- .filter((unit) => !isOwnedByNeutral(unit));
85
- const update = this.squad.onAiUpdate(gameApi, actionsApi, actionBatcher, playerData, this, matchAwareness, this.logger);
86
- if (update.type !== "noop") {
87
- return update;
88
- }
89
- if (foundTargets.length > 0) {
90
- this.lastTargetSeenAt = gameApi.getCurrentTick();
91
- this.hasPickedNewTarget = false;
92
- }
93
- else if (gameApi.getCurrentTick() > this.lastTargetSeenAt + NO_TARGET_IDLE_TIMEOUT_TICKS) {
94
- return disbandMission(AttackFailReason.NoTargets);
95
- }
96
- else if (!this.hasPickedNewTarget &&
97
- gameApi.getCurrentTick() > this.lastTargetSeenAt + NO_TARGET_RETARGET_TICKS) {
98
- const newTarget = generateTarget(gameApi, playerData, matchAwareness);
99
- if (newTarget) {
100
- this.squad.setAttackArea(newTarget);
101
- this.hasPickedNewTarget = true;
102
- }
103
- }
104
- return noop();
105
- }
106
- handleRetreatingState(gameApi, actionsApi, playerData, matchAwareness, actionBatcher) {
107
- this.getUnits(gameApi).forEach((unitId) => {
108
- actionBatcher.push(manageMoveMicro(unitId, matchAwareness.getMainRallyPoint()));
109
- });
110
- return disbandMission();
111
- }
112
- getGlobalDebugText() {
113
- return this.squad.getGlobalDebugText() ?? "<none>";
114
- }
115
- getState() {
116
- return this.state;
117
- }
118
- // This mission can give up its units while preparing.
119
- isUnitsLocked() {
120
- return this.state !== AttackMissionState.Preparing;
121
- }
122
- getPriority() {
123
- return this.priority;
124
- }
125
- }
126
- // Calculates the weight for initiating an attack on the position of a unit or building.
127
- // This is separate from unit micro; the squad will be ordered to attack in the vicinity of the point.
128
- const getTargetWeight = (unitData, tryFocusHarvester) => {
129
- if (tryFocusHarvester && unitData.rules.harvester) {
130
- return 100000;
131
- }
132
- else if (unitData.type === ObjectType.Building) {
133
- return unitData.maxHitPoints * 10;
134
- }
135
- else {
136
- return unitData.maxHitPoints;
137
- }
138
- };
139
- function generateTarget(gameApi, playerData, matchAwareness, includeBaseLocations = false) {
140
- // Randomly decide between harvester and base.
141
- try {
142
- const tryFocusHarvester = gameApi.generateRandomInt(0, 1) === 0;
143
- const enemyUnits = gameApi
144
- .getVisibleUnits(playerData.name, "enemy")
145
- .map((unitId) => gameApi.getUnitData(unitId))
146
- .filter((u) => !!u && gameApi.getPlayerData(u.owner).isCombatant);
147
- const maxUnit = maxBy(enemyUnits, (u) => getTargetWeight(u, tryFocusHarvester));
148
- if (maxUnit) {
149
- return new Vector2(maxUnit.tile.rx, maxUnit.tile.ry);
150
- }
151
- if (includeBaseLocations) {
152
- const mapApi = gameApi.mapApi;
153
- const enemyPlayers = gameApi
154
- .getPlayers()
155
- .map(gameApi.getPlayerData)
156
- .filter((otherPlayer) => !gameApi.areAlliedPlayers(playerData.name, otherPlayer.name));
157
- const unexploredEnemyLocations = enemyPlayers.filter((otherPlayer) => {
158
- const tile = mapApi.getTile(otherPlayer.startLocation.x, otherPlayer.startLocation.y);
159
- if (!tile) {
160
- return false;
161
- }
162
- return !mapApi.isVisibleTile(tile, playerData.name);
163
- });
164
- if (unexploredEnemyLocations.length > 0) {
165
- const idx = gameApi.generateRandomInt(0, unexploredEnemyLocations.length - 1);
166
- return unexploredEnemyLocations[idx].startLocation;
167
- }
168
- }
169
- }
170
- catch (err) {
171
- // There's a crash here when accessing a building that got destroyed. Will catch and ignore or now.
172
- return null;
173
- }
174
- return null;
175
- }
176
- // Number of ticks between attacking visible targets.
177
- const VISIBLE_TARGET_ATTACK_COOLDOWN_TICKS = 120;
178
- // Number of ticks between attacking "bases" (enemy starting locations).
179
- const BASE_ATTACK_COOLDOWN_TICKS = 1800;
180
- const ATTACK_MISSION_INITIAL_PRIORITY = 1;
181
- export class AttackMissionFactory {
182
- constructor(lastAttackAt = -VISIBLE_TARGET_ATTACK_COOLDOWN_TICKS) {
183
- this.lastAttackAt = lastAttackAt;
184
- }
185
- getName() {
186
- return "AttackMissionFactory";
187
- }
188
- maybeCreateMissions(gameApi, playerData, matchAwareness, missionController, logger) {
189
- if (gameApi.getCurrentTick() < this.lastAttackAt + VISIBLE_TARGET_ATTACK_COOLDOWN_TICKS) {
190
- return;
191
- }
192
- // can only have one attack 'preparing' at once.
193
- if (missionController
194
- .getMissions()
195
- .some((mission) => mission instanceof AttackMission && mission.getState() === AttackMissionState.Preparing)) {
196
- return;
197
- }
198
- const attackRadius = 10;
199
- const includeEnemyBases = gameApi.getCurrentTick() > this.lastAttackAt + BASE_ATTACK_COOLDOWN_TICKS;
200
- const attackArea = generateTarget(gameApi, playerData, matchAwareness, includeEnemyBases);
201
- if (!attackArea) {
202
- return;
203
- }
204
- const squadName = "attack_" + gameApi.getCurrentTick();
205
- const composition = calculateTargetComposition(gameApi, playerData, matchAwareness);
206
- const tryAttack = missionController.addMission(new AttackMission(squadName, ATTACK_MISSION_INITIAL_PRIORITY, matchAwareness.getMainRallyPoint(), attackArea, attackRadius, composition, logger).then((unitIds, reason) => {
207
- missionController.addMission(new RetreatMission("retreat-from-" + squadName + gameApi.getCurrentTick(), matchAwareness.getMainRallyPoint(), unitIds, logger));
208
- }));
209
- if (tryAttack) {
210
- this.lastAttackAt = gameApi.getCurrentTick();
211
- }
212
- }
213
- onMissionFailed(gameApi, playerData, matchAwareness, failedMission, failureReason, missionController) { }
214
- }
1
+ import { ObjectType, SideType, Vector2 } from "@chronodivide/game-api";
2
+ import { CombatSquad } from "./squads/combatSquad.js";
3
+ import { Mission, disbandMission, noop, requestUnits } from "../mission.js";
4
+ import { RetreatMission } from "./retreatMission.js";
5
+ import { countBy, isOwnedByNeutral, maxBy } from "../../common/utils.js";
6
+ import { getSovietComposition } from "../../composition/sovietCompositions.js";
7
+ import { getAlliedCompositions } from "../../composition/alliedCompositions.js";
8
+ import { manageMoveMicro } from "./squads/common.js";
9
+ export var AttackFailReason;
10
+ (function (AttackFailReason) {
11
+ AttackFailReason[AttackFailReason["NoTargets"] = 0] = "NoTargets";
12
+ AttackFailReason[AttackFailReason["DefenceTooStrong"] = 1] = "DefenceTooStrong";
13
+ })(AttackFailReason || (AttackFailReason = {}));
14
+ var AttackMissionState;
15
+ (function (AttackMissionState) {
16
+ AttackMissionState[AttackMissionState["Preparing"] = 0] = "Preparing";
17
+ AttackMissionState[AttackMissionState["Attacking"] = 1] = "Attacking";
18
+ AttackMissionState[AttackMissionState["Retreating"] = 2] = "Retreating";
19
+ })(AttackMissionState || (AttackMissionState = {}));
20
+ const NO_TARGET_RETARGET_TICKS = 450;
21
+ const NO_TARGET_IDLE_TIMEOUT_TICKS = 900;
22
+ function calculateTargetComposition(gameApi, playerData, matchAwareness) {
23
+ if (!playerData.country) {
24
+ throw new Error(`player ${playerData.name} has no country`);
25
+ }
26
+ else if (playerData.country.side === SideType.Nod) {
27
+ return getSovietComposition(gameApi, playerData, matchAwareness);
28
+ }
29
+ else {
30
+ return getAlliedCompositions(gameApi, playerData, matchAwareness);
31
+ }
32
+ }
33
+ const ATTACK_MISSION_PRIORITY_RAMP = 1.01;
34
+ const ATTACK_MISSION_MAX_PRIORITY = 50;
35
+ /**
36
+ * A mission that tries to attack a certain area.
37
+ */
38
+ export class AttackMission extends Mission {
39
+ constructor(uniqueName, priority, rallyArea, attackArea, radius, composition, logger) {
40
+ super(uniqueName, logger);
41
+ this.priority = priority;
42
+ this.attackArea = attackArea;
43
+ this.radius = radius;
44
+ this.composition = composition;
45
+ this.lastTargetSeenAt = 0;
46
+ this.hasPickedNewTarget = false;
47
+ this.state = AttackMissionState.Preparing;
48
+ this.squad = new CombatSquad(rallyArea, attackArea, radius);
49
+ }
50
+ _onAiUpdate(gameApi, actionsApi, playerData, matchAwareness, actionBatcher) {
51
+ switch (this.state) {
52
+ case AttackMissionState.Preparing:
53
+ return this.handlePreparingState(gameApi, actionsApi, playerData, matchAwareness, actionBatcher);
54
+ case AttackMissionState.Attacking:
55
+ return this.handleAttackingState(gameApi, actionsApi, playerData, matchAwareness, actionBatcher);
56
+ case AttackMissionState.Retreating:
57
+ return this.handleRetreatingState(gameApi, actionsApi, playerData, matchAwareness, actionBatcher);
58
+ }
59
+ }
60
+ handlePreparingState(gameApi, actionsApi, playerData, matchAwareness, actionBatcher) {
61
+ const currentComposition = countBy(this.getUnits(gameApi), (unit) => unit.name);
62
+ const missingUnits = Object.entries(this.composition).filter(([unitType, targetAmount]) => {
63
+ return !currentComposition[unitType] || currentComposition[unitType] < targetAmount;
64
+ });
65
+ if (missingUnits.length > 0) {
66
+ this.priority = Math.min(this.priority * ATTACK_MISSION_PRIORITY_RAMP, ATTACK_MISSION_MAX_PRIORITY);
67
+ return requestUnits(missingUnits.map(([unitName]) => unitName), this.priority);
68
+ }
69
+ else {
70
+ this.priority = ATTACK_MISSION_INITIAL_PRIORITY;
71
+ this.state = AttackMissionState.Attacking;
72
+ return noop();
73
+ }
74
+ }
75
+ handleAttackingState(gameApi, actionsApi, playerData, matchAwareness, actionBatcher) {
76
+ if (this.getUnitIds().length === 0) {
77
+ // TODO: disband directly (we no longer retreat when losing)
78
+ this.state = AttackMissionState.Retreating;
79
+ return noop();
80
+ }
81
+ const foundTargets = matchAwareness
82
+ .getHostilesNearPoint2d(this.attackArea, this.radius)
83
+ .map((unit) => gameApi.getUnitData(unit.unitId))
84
+ .filter((unit) => !isOwnedByNeutral(unit));
85
+ const update = this.squad.onAiUpdate(gameApi, actionsApi, actionBatcher, playerData, this, matchAwareness, this.logger);
86
+ if (update.type !== "noop") {
87
+ return update;
88
+ }
89
+ if (foundTargets.length > 0) {
90
+ this.lastTargetSeenAt = gameApi.getCurrentTick();
91
+ this.hasPickedNewTarget = false;
92
+ }
93
+ else if (gameApi.getCurrentTick() > this.lastTargetSeenAt + NO_TARGET_IDLE_TIMEOUT_TICKS) {
94
+ return disbandMission(AttackFailReason.NoTargets);
95
+ }
96
+ else if (!this.hasPickedNewTarget &&
97
+ gameApi.getCurrentTick() > this.lastTargetSeenAt + NO_TARGET_RETARGET_TICKS) {
98
+ const newTarget = generateTarget(gameApi, playerData, matchAwareness);
99
+ if (newTarget) {
100
+ this.squad.setAttackArea(newTarget);
101
+ this.hasPickedNewTarget = true;
102
+ }
103
+ }
104
+ return noop();
105
+ }
106
+ handleRetreatingState(gameApi, actionsApi, playerData, matchAwareness, actionBatcher) {
107
+ this.getUnits(gameApi).forEach((unitId) => {
108
+ actionBatcher.push(manageMoveMicro(unitId, matchAwareness.getMainRallyPoint()));
109
+ });
110
+ return disbandMission();
111
+ }
112
+ getGlobalDebugText() {
113
+ return this.squad.getGlobalDebugText() ?? "<none>";
114
+ }
115
+ getState() {
116
+ return this.state;
117
+ }
118
+ // This mission can give up its units while preparing.
119
+ isUnitsLocked() {
120
+ return this.state !== AttackMissionState.Preparing;
121
+ }
122
+ getPriority() {
123
+ return this.priority;
124
+ }
125
+ }
126
+ // Calculates the weight for initiating an attack on the position of a unit or building.
127
+ // This is separate from unit micro; the squad will be ordered to attack in the vicinity of the point.
128
+ const getTargetWeight = (unitData, tryFocusHarvester) => {
129
+ if (tryFocusHarvester && unitData.rules.harvester) {
130
+ return 100000;
131
+ }
132
+ else if (unitData.type === ObjectType.Building) {
133
+ return unitData.maxHitPoints * 10;
134
+ }
135
+ else {
136
+ return unitData.maxHitPoints;
137
+ }
138
+ };
139
+ function generateTarget(gameApi, playerData, matchAwareness, includeBaseLocations = false) {
140
+ // Randomly decide between harvester and base.
141
+ try {
142
+ const tryFocusHarvester = gameApi.generateRandomInt(0, 1) === 0;
143
+ const enemyUnits = gameApi
144
+ .getVisibleUnits(playerData.name, "enemy")
145
+ .map((unitId) => gameApi.getUnitData(unitId))
146
+ .filter((u) => !!u && gameApi.getPlayerData(u.owner).isCombatant);
147
+ const maxUnit = maxBy(enemyUnits, (u) => getTargetWeight(u, tryFocusHarvester));
148
+ if (maxUnit) {
149
+ return new Vector2(maxUnit.tile.rx, maxUnit.tile.ry);
150
+ }
151
+ if (includeBaseLocations) {
152
+ const mapApi = gameApi.mapApi;
153
+ const enemyPlayers = gameApi
154
+ .getPlayers()
155
+ .map(gameApi.getPlayerData)
156
+ .filter((otherPlayer) => !gameApi.areAlliedPlayers(playerData.name, otherPlayer.name));
157
+ const unexploredEnemyLocations = enemyPlayers.filter((otherPlayer) => {
158
+ const tile = mapApi.getTile(otherPlayer.startLocation.x, otherPlayer.startLocation.y);
159
+ if (!tile) {
160
+ return false;
161
+ }
162
+ return !mapApi.isVisibleTile(tile, playerData.name);
163
+ });
164
+ if (unexploredEnemyLocations.length > 0) {
165
+ const idx = gameApi.generateRandomInt(0, unexploredEnemyLocations.length - 1);
166
+ return unexploredEnemyLocations[idx].startLocation;
167
+ }
168
+ }
169
+ }
170
+ catch (err) {
171
+ // There's a crash here when accessing a building that got destroyed. Will catch and ignore or now.
172
+ return null;
173
+ }
174
+ return null;
175
+ }
176
+ // Number of ticks between attacking visible targets.
177
+ const VISIBLE_TARGET_ATTACK_COOLDOWN_TICKS = 120;
178
+ // Number of ticks between attacking "bases" (enemy starting locations).
179
+ const BASE_ATTACK_COOLDOWN_TICKS = 1800;
180
+ const ATTACK_MISSION_INITIAL_PRIORITY = 1;
181
+ export class AttackMissionFactory {
182
+ constructor(lastAttackAt = -VISIBLE_TARGET_ATTACK_COOLDOWN_TICKS) {
183
+ this.lastAttackAt = lastAttackAt;
184
+ }
185
+ getName() {
186
+ return "AttackMissionFactory";
187
+ }
188
+ maybeCreateMissions(gameApi, playerData, matchAwareness, missionController, logger) {
189
+ if (gameApi.getCurrentTick() < this.lastAttackAt + VISIBLE_TARGET_ATTACK_COOLDOWN_TICKS) {
190
+ return;
191
+ }
192
+ // can only have one attack 'preparing' at once.
193
+ if (missionController
194
+ .getMissions()
195
+ .some((mission) => mission instanceof AttackMission && mission.getState() === AttackMissionState.Preparing)) {
196
+ return;
197
+ }
198
+ const attackRadius = 10;
199
+ const includeEnemyBases = gameApi.getCurrentTick() > this.lastAttackAt + BASE_ATTACK_COOLDOWN_TICKS;
200
+ const attackArea = generateTarget(gameApi, playerData, matchAwareness, includeEnemyBases);
201
+ if (!attackArea) {
202
+ return;
203
+ }
204
+ const squadName = "attack_" + gameApi.getCurrentTick();
205
+ const composition = calculateTargetComposition(gameApi, playerData, matchAwareness);
206
+ const tryAttack = missionController.addMission(new AttackMission(squadName, ATTACK_MISSION_INITIAL_PRIORITY, matchAwareness.getMainRallyPoint(), attackArea, attackRadius, composition, logger).then((unitIds, reason) => {
207
+ missionController.addMission(new RetreatMission("retreat-from-" + squadName + gameApi.getCurrentTick(), matchAwareness.getMainRallyPoint(), unitIds, logger));
208
+ }));
209
+ if (tryAttack) {
210
+ this.lastAttackAt = gameApi.getCurrentTick();
211
+ }
212
+ }
213
+ onMissionFailed(gameApi, playerData, matchAwareness, failedMission, failureReason, missionController) { }
214
+ }
215
215
  //# sourceMappingURL=attackMission.js.map
@@ -1,83 +1,83 @@
1
- import { Vector2 } from "@chronodivide/game-api";
2
- import { Mission, grabCombatants, noop, releaseUnits } from "../mission.js";
3
- import { CombatSquad } from "./squads/combatSquad.js";
4
- import { isOwnedByNeutral } from "../../common/utils.js";
5
- export const MAX_PRIORITY = 100;
6
- export const PRIORITY_INCREASE_PER_TICK_RATIO = 1.025;
7
- /**
8
- * A mission that tries to defend a certain area.
9
- */
10
- export class DefenceMission extends Mission {
11
- constructor(uniqueName, priority, rallyArea, defenceArea, radius, logger) {
12
- super(uniqueName, logger);
13
- this.priority = priority;
14
- this.defenceArea = defenceArea;
15
- this.radius = radius;
16
- this.squad = new CombatSquad(rallyArea, defenceArea, radius);
17
- }
18
- _onAiUpdate(gameApi, actionsApi, playerData, matchAwareness, actionBatcher) {
19
- // Dispatch missions.
20
- const foundTargets = matchAwareness
21
- .getHostilesNearPoint2d(this.defenceArea, this.radius)
22
- .map((unit) => gameApi.getUnitData(unit.unitId))
23
- .filter((unit) => !isOwnedByNeutral(unit));
24
- const update = this.squad.onAiUpdate(gameApi, actionsApi, actionBatcher, playerData, this, matchAwareness, this.logger);
25
- if (update.type !== "noop") {
26
- return update;
27
- }
28
- if (foundTargets.length === 0) {
29
- this.priority = 0;
30
- if (this.getUnitIds().length > 0) {
31
- this.logger(`(Defence Mission ${this.getUniqueName()}): No targets found, releasing units.`);
32
- return releaseUnits(this.getUnitIds());
33
- }
34
- else {
35
- return noop();
36
- }
37
- }
38
- else {
39
- const targetUnit = foundTargets[0];
40
- this.logger(`(Defence Mission ${this.getUniqueName()}): Focused on target ${targetUnit?.name} (${foundTargets.length} found in area ${this.radius})`);
41
- this.squad.setAttackArea(new Vector2(foundTargets[0].tile.rx, foundTargets[0].tile.ry));
42
- this.priority = MAX_PRIORITY; // Math.min(MAX_PRIORITY, this.priority * PRIORITY_INCREASE_PER_TICK_RATIO);
43
- return grabCombatants(playerData.startLocation, this.priority);
44
- }
45
- //return requestUnits(["E1", "E2", "FV", "HTK", "MTNK", "HTNK"], this.priority);
46
- }
47
- getGlobalDebugText() {
48
- return this.squad.getGlobalDebugText() ?? "<none>";
49
- }
50
- getPriority() {
51
- return this.priority;
52
- }
53
- }
54
- const DEFENCE_CHECK_TICKS = 30;
55
- // Starting radius around the player's base to trigger defense.
56
- const DEFENCE_STARTING_RADIUS = 10;
57
- // Every game tick, we increase the defendable area by this amount.
58
- const DEFENCE_RADIUS_INCREASE_PER_GAME_TICK = 0.001;
59
- export class DefenceMissionFactory {
60
- constructor() {
61
- this.lastDefenceCheckAt = 0;
62
- }
63
- getName() {
64
- return "DefenceMissionFactory";
65
- }
66
- maybeCreateMissions(gameApi, playerData, matchAwareness, missionController, logger) {
67
- if (gameApi.getCurrentTick() < this.lastDefenceCheckAt + DEFENCE_CHECK_TICKS) {
68
- return;
69
- }
70
- this.lastDefenceCheckAt = gameApi.getCurrentTick();
71
- const defendableRadius = DEFENCE_STARTING_RADIUS + DEFENCE_RADIUS_INCREASE_PER_GAME_TICK * gameApi.getCurrentTick();
72
- const enemiesNearSpawn = matchAwareness
73
- .getHostilesNearPoint2d(playerData.startLocation, defendableRadius)
74
- .map((unit) => gameApi.getUnitData(unit.unitId))
75
- .filter((unit) => !isOwnedByNeutral(unit));
76
- if (enemiesNearSpawn.length > 0) {
77
- logger(`Starting defence mission, ${enemiesNearSpawn.length} found in radius ${defendableRadius} (tick ${gameApi.getCurrentTick()})`);
78
- missionController.addMission(new DefenceMission("globalDefence", 10, matchAwareness.getMainRallyPoint(), playerData.startLocation, defendableRadius * 1.2, logger));
79
- }
80
- }
81
- onMissionFailed(gameApi, playerData, matchAwareness, failedMission, failureReason, missionController) { }
82
- }
1
+ import { Vector2 } from "@chronodivide/game-api";
2
+ import { Mission, grabCombatants, noop, releaseUnits } from "../mission.js";
3
+ import { CombatSquad } from "./squads/combatSquad.js";
4
+ import { isOwnedByNeutral } from "../../common/utils.js";
5
+ export const MAX_PRIORITY = 100;
6
+ export const PRIORITY_INCREASE_PER_TICK_RATIO = 1.025;
7
+ /**
8
+ * A mission that tries to defend a certain area.
9
+ */
10
+ export class DefenceMission extends Mission {
11
+ constructor(uniqueName, priority, rallyArea, defenceArea, radius, logger) {
12
+ super(uniqueName, logger);
13
+ this.priority = priority;
14
+ this.defenceArea = defenceArea;
15
+ this.radius = radius;
16
+ this.squad = new CombatSquad(rallyArea, defenceArea, radius);
17
+ }
18
+ _onAiUpdate(gameApi, actionsApi, playerData, matchAwareness, actionBatcher) {
19
+ // Dispatch missions.
20
+ const foundTargets = matchAwareness
21
+ .getHostilesNearPoint2d(this.defenceArea, this.radius)
22
+ .map((unit) => gameApi.getUnitData(unit.unitId))
23
+ .filter((unit) => !isOwnedByNeutral(unit));
24
+ const update = this.squad.onAiUpdate(gameApi, actionsApi, actionBatcher, playerData, this, matchAwareness, this.logger);
25
+ if (update.type !== "noop") {
26
+ return update;
27
+ }
28
+ if (foundTargets.length === 0) {
29
+ this.priority = 0;
30
+ if (this.getUnitIds().length > 0) {
31
+ this.logger(`(Defence Mission ${this.getUniqueName()}): No targets found, releasing units.`);
32
+ return releaseUnits(this.getUnitIds());
33
+ }
34
+ else {
35
+ return noop();
36
+ }
37
+ }
38
+ else {
39
+ const targetUnit = foundTargets[0];
40
+ this.logger(`(Defence Mission ${this.getUniqueName()}): Focused on target ${targetUnit?.name} (${foundTargets.length} found in area ${this.radius})`);
41
+ this.squad.setAttackArea(new Vector2(foundTargets[0].tile.rx, foundTargets[0].tile.ry));
42
+ this.priority = MAX_PRIORITY; // Math.min(MAX_PRIORITY, this.priority * PRIORITY_INCREASE_PER_TICK_RATIO);
43
+ return grabCombatants(playerData.startLocation, this.priority);
44
+ }
45
+ //return requestUnits(["E1", "E2", "FV", "HTK", "MTNK", "HTNK"], this.priority);
46
+ }
47
+ getGlobalDebugText() {
48
+ return this.squad.getGlobalDebugText() ?? "<none>";
49
+ }
50
+ getPriority() {
51
+ return this.priority;
52
+ }
53
+ }
54
+ const DEFENCE_CHECK_TICKS = 30;
55
+ // Starting radius around the player's base to trigger defense.
56
+ const DEFENCE_STARTING_RADIUS = 10;
57
+ // Every game tick, we increase the defendable area by this amount.
58
+ const DEFENCE_RADIUS_INCREASE_PER_GAME_TICK = 0.001;
59
+ export class DefenceMissionFactory {
60
+ constructor() {
61
+ this.lastDefenceCheckAt = 0;
62
+ }
63
+ getName() {
64
+ return "DefenceMissionFactory";
65
+ }
66
+ maybeCreateMissions(gameApi, playerData, matchAwareness, missionController, logger) {
67
+ if (gameApi.getCurrentTick() < this.lastDefenceCheckAt + DEFENCE_CHECK_TICKS) {
68
+ return;
69
+ }
70
+ this.lastDefenceCheckAt = gameApi.getCurrentTick();
71
+ const defendableRadius = DEFENCE_STARTING_RADIUS + DEFENCE_RADIUS_INCREASE_PER_GAME_TICK * gameApi.getCurrentTick();
72
+ const enemiesNearSpawn = matchAwareness
73
+ .getHostilesNearPoint2d(playerData.startLocation, defendableRadius)
74
+ .map((unit) => gameApi.getUnitData(unit.unitId))
75
+ .filter((unit) => !isOwnedByNeutral(unit));
76
+ if (enemiesNearSpawn.length > 0) {
77
+ logger(`Starting defence mission, ${enemiesNearSpawn.length} found in radius ${defendableRadius} (tick ${gameApi.getCurrentTick()})`);
78
+ missionController.addMission(new DefenceMission("globalDefence", 10, matchAwareness.getMainRallyPoint(), playerData.startLocation, defendableRadius * 1.2, logger));
79
+ }
80
+ }
81
+ onMissionFailed(gameApi, playerData, matchAwareness, failedMission, failureReason, missionController) { }
82
+ }
83
83
  //# sourceMappingURL=defenceMission.js.map