@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.
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,151 +1,151 @@
1
- import { ActionsApi, GameApi, PlayerData, UnitData, Vector2 } from "@chronodivide/game-api";
2
- import { MatchAwareness } from "../../awareness.js";
3
- import { MissionController } from "../missionController.js";
4
- import { Mission, MissionAction, grabCombatants, noop, releaseUnits, requestUnits } from "../mission.js";
5
- import { MissionFactory } from "../missionFactories.js";
6
- import { CombatSquad } from "./squads/combatSquad.js";
7
- import { DebugLogger, isOwnedByNeutral } from "../../common/utils.js";
8
- import { ActionBatcher } from "../actionBatcher.js";
9
-
10
- export const MAX_PRIORITY = 100;
11
- export const PRIORITY_INCREASE_PER_TICK_RATIO = 1.025;
12
-
13
- /**
14
- * A mission that tries to defend a certain area.
15
- */
16
- export class DefenceMission extends Mission<CombatSquad> {
17
- private squad: CombatSquad;
18
-
19
- constructor(
20
- uniqueName: string,
21
- private priority: number,
22
- rallyArea: Vector2,
23
- private defenceArea: Vector2,
24
- private radius: number,
25
- logger: DebugLogger,
26
- ) {
27
- super(uniqueName, logger);
28
- this.squad = new CombatSquad(rallyArea, defenceArea, radius);
29
- }
30
-
31
- _onAiUpdate(
32
- gameApi: GameApi,
33
- actionsApi: ActionsApi,
34
- playerData: PlayerData,
35
- matchAwareness: MatchAwareness,
36
- actionBatcher: ActionBatcher,
37
- ): MissionAction {
38
- // Dispatch missions.
39
- const foundTargets = matchAwareness
40
- .getHostilesNearPoint2d(this.defenceArea, this.radius)
41
- .map((unit) => gameApi.getUnitData(unit.unitId))
42
- .filter((unit) => !isOwnedByNeutral(unit)) as UnitData[];
43
-
44
- const update = this.squad.onAiUpdate(
45
- gameApi,
46
- actionsApi,
47
- actionBatcher,
48
- playerData,
49
- this,
50
- matchAwareness,
51
- this.logger,
52
- );
53
-
54
- if (update.type !== "noop") {
55
- return update;
56
- }
57
-
58
- if (foundTargets.length === 0) {
59
- this.priority = 0;
60
- if (this.getUnitIds().length > 0) {
61
- this.logger(`(Defence Mission ${this.getUniqueName()}): No targets found, releasing units.`);
62
- return releaseUnits(this.getUnitIds());
63
- } else {
64
- return noop();
65
- }
66
- } else {
67
- const targetUnit = foundTargets[0];
68
- this.logger(
69
- `(Defence Mission ${this.getUniqueName()}): Focused on target ${targetUnit?.name} (${
70
- foundTargets.length
71
- } found in area ${this.radius})`,
72
- );
73
- this.squad.setAttackArea(new Vector2(foundTargets[0].tile.rx, foundTargets[0].tile.ry));
74
- this.priority = MAX_PRIORITY; // Math.min(MAX_PRIORITY, this.priority * PRIORITY_INCREASE_PER_TICK_RATIO);
75
- return grabCombatants(playerData.startLocation, this.priority);
76
- }
77
- //return requestUnits(["E1", "E2", "FV", "HTK", "MTNK", "HTNK"], this.priority);
78
- }
79
-
80
- public getGlobalDebugText(): string | undefined {
81
- return this.squad.getGlobalDebugText() ?? "<none>";
82
- }
83
-
84
- public getPriority() {
85
- return this.priority;
86
- }
87
- }
88
-
89
- const DEFENCE_CHECK_TICKS = 30;
90
-
91
- // Starting radius around the player's base to trigger defense.
92
- const DEFENCE_STARTING_RADIUS = 10;
93
- // Every game tick, we increase the defendable area by this amount.
94
- const DEFENCE_RADIUS_INCREASE_PER_GAME_TICK = 0.001;
95
-
96
- export class DefenceMissionFactory implements MissionFactory {
97
- private lastDefenceCheckAt = 0;
98
-
99
- constructor() {}
100
-
101
- getName(): string {
102
- return "DefenceMissionFactory";
103
- }
104
-
105
- maybeCreateMissions(
106
- gameApi: GameApi,
107
- playerData: PlayerData,
108
- matchAwareness: MatchAwareness,
109
- missionController: MissionController,
110
- logger: DebugLogger,
111
- ): void {
112
- if (gameApi.getCurrentTick() < this.lastDefenceCheckAt + DEFENCE_CHECK_TICKS) {
113
- return;
114
- }
115
- this.lastDefenceCheckAt = gameApi.getCurrentTick();
116
-
117
- const defendableRadius =
118
- DEFENCE_STARTING_RADIUS + DEFENCE_RADIUS_INCREASE_PER_GAME_TICK * gameApi.getCurrentTick();
119
- const enemiesNearSpawn = matchAwareness
120
- .getHostilesNearPoint2d(playerData.startLocation, defendableRadius)
121
- .map((unit) => gameApi.getUnitData(unit.unitId))
122
- .filter((unit) => !isOwnedByNeutral(unit)) as UnitData[];
123
-
124
- if (enemiesNearSpawn.length > 0) {
125
- logger(
126
- `Starting defence mission, ${
127
- enemiesNearSpawn.length
128
- } found in radius ${defendableRadius} (tick ${gameApi.getCurrentTick()})`,
129
- );
130
- missionController.addMission(
131
- new DefenceMission(
132
- "globalDefence",
133
- 10,
134
- matchAwareness.getMainRallyPoint(),
135
- playerData.startLocation,
136
- defendableRadius * 1.2,
137
- logger,
138
- ),
139
- );
140
- }
141
- }
142
-
143
- onMissionFailed(
144
- gameApi: GameApi,
145
- playerData: PlayerData,
146
- matchAwareness: MatchAwareness,
147
- failedMission: Mission<any>,
148
- failureReason: undefined,
149
- missionController: MissionController,
150
- ): void {}
151
- }
1
+ import { ActionsApi, GameApi, PlayerData, UnitData, Vector2 } from "@chronodivide/game-api";
2
+ import { MatchAwareness } from "../../awareness.js";
3
+ import { MissionController } from "../missionController.js";
4
+ import { Mission, MissionAction, grabCombatants, noop, releaseUnits, requestUnits } from "../mission.js";
5
+ import { MissionFactory } from "../missionFactories.js";
6
+ import { CombatSquad } from "./squads/combatSquad.js";
7
+ import { DebugLogger, isOwnedByNeutral } from "../../common/utils.js";
8
+ import { ActionBatcher } from "../actionBatcher.js";
9
+
10
+ export const MAX_PRIORITY = 100;
11
+ export const PRIORITY_INCREASE_PER_TICK_RATIO = 1.025;
12
+
13
+ /**
14
+ * A mission that tries to defend a certain area.
15
+ */
16
+ export class DefenceMission extends Mission<CombatSquad> {
17
+ private squad: CombatSquad;
18
+
19
+ constructor(
20
+ uniqueName: string,
21
+ private priority: number,
22
+ rallyArea: Vector2,
23
+ private defenceArea: Vector2,
24
+ private radius: number,
25
+ logger: DebugLogger,
26
+ ) {
27
+ super(uniqueName, logger);
28
+ this.squad = new CombatSquad(rallyArea, defenceArea, radius);
29
+ }
30
+
31
+ _onAiUpdate(
32
+ gameApi: GameApi,
33
+ actionsApi: ActionsApi,
34
+ playerData: PlayerData,
35
+ matchAwareness: MatchAwareness,
36
+ actionBatcher: ActionBatcher,
37
+ ): MissionAction {
38
+ // Dispatch missions.
39
+ const foundTargets = matchAwareness
40
+ .getHostilesNearPoint2d(this.defenceArea, this.radius)
41
+ .map((unit) => gameApi.getUnitData(unit.unitId))
42
+ .filter((unit) => !isOwnedByNeutral(unit)) as UnitData[];
43
+
44
+ const update = this.squad.onAiUpdate(
45
+ gameApi,
46
+ actionsApi,
47
+ actionBatcher,
48
+ playerData,
49
+ this,
50
+ matchAwareness,
51
+ this.logger,
52
+ );
53
+
54
+ if (update.type !== "noop") {
55
+ return update;
56
+ }
57
+
58
+ if (foundTargets.length === 0) {
59
+ this.priority = 0;
60
+ if (this.getUnitIds().length > 0) {
61
+ this.logger(`(Defence Mission ${this.getUniqueName()}): No targets found, releasing units.`);
62
+ return releaseUnits(this.getUnitIds());
63
+ } else {
64
+ return noop();
65
+ }
66
+ } else {
67
+ const targetUnit = foundTargets[0];
68
+ this.logger(
69
+ `(Defence Mission ${this.getUniqueName()}): Focused on target ${targetUnit?.name} (${
70
+ foundTargets.length
71
+ } found in area ${this.radius})`,
72
+ );
73
+ this.squad.setAttackArea(new Vector2(foundTargets[0].tile.rx, foundTargets[0].tile.ry));
74
+ this.priority = MAX_PRIORITY; // Math.min(MAX_PRIORITY, this.priority * PRIORITY_INCREASE_PER_TICK_RATIO);
75
+ return grabCombatants(playerData.startLocation, this.priority);
76
+ }
77
+ //return requestUnits(["E1", "E2", "FV", "HTK", "MTNK", "HTNK"], this.priority);
78
+ }
79
+
80
+ public getGlobalDebugText(): string | undefined {
81
+ return this.squad.getGlobalDebugText() ?? "<none>";
82
+ }
83
+
84
+ public getPriority() {
85
+ return this.priority;
86
+ }
87
+ }
88
+
89
+ const DEFENCE_CHECK_TICKS = 30;
90
+
91
+ // Starting radius around the player's base to trigger defense.
92
+ const DEFENCE_STARTING_RADIUS = 10;
93
+ // Every game tick, we increase the defendable area by this amount.
94
+ const DEFENCE_RADIUS_INCREASE_PER_GAME_TICK = 0.001;
95
+
96
+ export class DefenceMissionFactory implements MissionFactory {
97
+ private lastDefenceCheckAt = 0;
98
+
99
+ constructor() {}
100
+
101
+ getName(): string {
102
+ return "DefenceMissionFactory";
103
+ }
104
+
105
+ maybeCreateMissions(
106
+ gameApi: GameApi,
107
+ playerData: PlayerData,
108
+ matchAwareness: MatchAwareness,
109
+ missionController: MissionController,
110
+ logger: DebugLogger,
111
+ ): void {
112
+ if (gameApi.getCurrentTick() < this.lastDefenceCheckAt + DEFENCE_CHECK_TICKS) {
113
+ return;
114
+ }
115
+ this.lastDefenceCheckAt = gameApi.getCurrentTick();
116
+
117
+ const defendableRadius =
118
+ DEFENCE_STARTING_RADIUS + DEFENCE_RADIUS_INCREASE_PER_GAME_TICK * gameApi.getCurrentTick();
119
+ const enemiesNearSpawn = matchAwareness
120
+ .getHostilesNearPoint2d(playerData.startLocation, defendableRadius)
121
+ .map((unit) => gameApi.getUnitData(unit.unitId))
122
+ .filter((unit) => !isOwnedByNeutral(unit)) as UnitData[];
123
+
124
+ if (enemiesNearSpawn.length > 0) {
125
+ logger(
126
+ `Starting defence mission, ${
127
+ enemiesNearSpawn.length
128
+ } found in radius ${defendableRadius} (tick ${gameApi.getCurrentTick()})`,
129
+ );
130
+ missionController.addMission(
131
+ new DefenceMission(
132
+ "globalDefence",
133
+ 10,
134
+ matchAwareness.getMainRallyPoint(),
135
+ playerData.startLocation,
136
+ defendableRadius * 1.2,
137
+ logger,
138
+ ),
139
+ );
140
+ }
141
+ }
142
+
143
+ onMissionFailed(
144
+ gameApi: GameApi,
145
+ playerData: PlayerData,
146
+ matchAwareness: MatchAwareness,
147
+ failedMission: Mission<any>,
148
+ failureReason: undefined,
149
+ missionController: MissionController,
150
+ ): void {}
151
+ }
@@ -1,113 +1,113 @@
1
- import { ActionsApi, GameApi, OrderType, PlayerData } from "@chronodivide/game-api";
2
- import { Mission, MissionAction, disbandMission, noop, requestUnits } from "../mission.js";
3
- import { MissionFactory } from "../missionFactories.js";
4
- import { MatchAwareness } from "../../awareness.js";
5
- import { MissionController } from "../missionController.js";
6
- import { DebugLogger } from "../../common/utils.js";
7
- import { ActionBatcher } from "../actionBatcher.js";
8
-
9
- const CAPTURE_COOLDOWN_TICKS = 30;
10
-
11
- /**
12
- * A mission that tries to send an engineer into a building (e.g. to capture tech building or repair bridge)
13
- */
14
- export class EngineerMission extends Mission {
15
- private hasAttemptedCaptureWith: {
16
- unitId: number;
17
- gameTick: number;
18
- } | null = null;
19
-
20
- constructor(
21
- uniqueName: string,
22
- private priority: number,
23
- private captureTargetId: number,
24
- logger: DebugLogger,
25
- ) {
26
- super(uniqueName, logger);
27
- }
28
-
29
- public _onAiUpdate(
30
- gameApi: GameApi,
31
- actionsApi: ActionsApi,
32
- playerData: PlayerData,
33
- matchAwareness: MatchAwareness,
34
- actionBatcher: ActionBatcher,
35
- ): MissionAction {
36
- const engineerTypes = ["ENGINEER", "SENGINEER"];
37
- const engineers = this.getUnitsOfTypes(gameApi, ...engineerTypes);
38
- if (engineers.length === 0) {
39
- // Perhaps we deployed already (or the unit was destroyed), end the mission.
40
- if (this.hasAttemptedCaptureWith !== null) {
41
- return disbandMission();
42
- }
43
- return requestUnits(engineerTypes, this.priority);
44
- } else if (
45
- !this.hasAttemptedCaptureWith ||
46
- gameApi.getCurrentTick() > this.hasAttemptedCaptureWith.gameTick + CAPTURE_COOLDOWN_TICKS
47
- ) {
48
- actionsApi.orderUnits(
49
- engineers.map((engineer) => engineer.id),
50
- OrderType.Capture,
51
- this.captureTargetId,
52
- );
53
- // Add a cooldown to deploy attempts.
54
- this.hasAttemptedCaptureWith = {
55
- unitId: engineers[0].id,
56
- gameTick: gameApi.getCurrentTick(),
57
- };
58
- }
59
- return noop();
60
- }
61
-
62
- public getGlobalDebugText(): string | undefined {
63
- return undefined;
64
- }
65
-
66
- public getPriority() {
67
- return this.priority;
68
- }
69
- }
70
-
71
- // Only try to capture tech buildings within this radius of the starting point.
72
- const MAX_TECH_CAPTURE_RADIUS = 50;
73
-
74
- const TECH_CHECK_INTERVAL_TICKS = 300;
75
-
76
- export class EngineerMissionFactory implements MissionFactory {
77
- private lastCheckAt = 0;
78
-
79
- getName(): string {
80
- return "EngineerMissionFactory";
81
- }
82
-
83
- maybeCreateMissions(
84
- gameApi: GameApi,
85
- playerData: PlayerData,
86
- matchAwareness: MatchAwareness,
87
- missionController: MissionController,
88
- logger: DebugLogger,
89
- ): void {
90
- if (!(gameApi.getCurrentTick() > this.lastCheckAt + TECH_CHECK_INTERVAL_TICKS)) {
91
- return;
92
- }
93
- this.lastCheckAt = gameApi.getCurrentTick();
94
- const eligibleTechBuildings = gameApi.getVisibleUnits(
95
- playerData.name,
96
- "hostile",
97
- (r) => r.capturable && r.produceCashAmount > 0,
98
- );
99
-
100
- eligibleTechBuildings.forEach((techBuildingId) => {
101
- missionController.addMission(new EngineerMission("capture-" + techBuildingId, 100, techBuildingId, logger));
102
- });
103
- }
104
-
105
- onMissionFailed(
106
- gameApi: GameApi,
107
- playerData: PlayerData,
108
- matchAwareness: MatchAwareness,
109
- failedMission: Mission<any>,
110
- failureReason: undefined,
111
- missionController: MissionController,
112
- ): void {}
113
- }
1
+ import { ActionsApi, GameApi, OrderType, PlayerData } from "@chronodivide/game-api";
2
+ import { Mission, MissionAction, disbandMission, noop, requestUnits } from "../mission.js";
3
+ import { MissionFactory } from "../missionFactories.js";
4
+ import { MatchAwareness } from "../../awareness.js";
5
+ import { MissionController } from "../missionController.js";
6
+ import { DebugLogger } from "../../common/utils.js";
7
+ import { ActionBatcher } from "../actionBatcher.js";
8
+
9
+ const CAPTURE_COOLDOWN_TICKS = 30;
10
+
11
+ /**
12
+ * A mission that tries to send an engineer into a building (e.g. to capture tech building or repair bridge)
13
+ */
14
+ export class EngineerMission extends Mission {
15
+ private hasAttemptedCaptureWith: {
16
+ unitId: number;
17
+ gameTick: number;
18
+ } | null = null;
19
+
20
+ constructor(
21
+ uniqueName: string,
22
+ private priority: number,
23
+ private captureTargetId: number,
24
+ logger: DebugLogger,
25
+ ) {
26
+ super(uniqueName, logger);
27
+ }
28
+
29
+ public _onAiUpdate(
30
+ gameApi: GameApi,
31
+ actionsApi: ActionsApi,
32
+ playerData: PlayerData,
33
+ matchAwareness: MatchAwareness,
34
+ actionBatcher: ActionBatcher,
35
+ ): MissionAction {
36
+ const engineerTypes = ["ENGINEER", "SENGINEER"];
37
+ const engineers = this.getUnitsOfTypes(gameApi, ...engineerTypes);
38
+ if (engineers.length === 0) {
39
+ // Perhaps we deployed already (or the unit was destroyed), end the mission.
40
+ if (this.hasAttemptedCaptureWith !== null) {
41
+ return disbandMission();
42
+ }
43
+ return requestUnits(engineerTypes, this.priority);
44
+ } else if (
45
+ !this.hasAttemptedCaptureWith ||
46
+ gameApi.getCurrentTick() > this.hasAttemptedCaptureWith.gameTick + CAPTURE_COOLDOWN_TICKS
47
+ ) {
48
+ actionsApi.orderUnits(
49
+ engineers.map((engineer) => engineer.id),
50
+ OrderType.Capture,
51
+ this.captureTargetId,
52
+ );
53
+ // Add a cooldown to deploy attempts.
54
+ this.hasAttemptedCaptureWith = {
55
+ unitId: engineers[0].id,
56
+ gameTick: gameApi.getCurrentTick(),
57
+ };
58
+ }
59
+ return noop();
60
+ }
61
+
62
+ public getGlobalDebugText(): string | undefined {
63
+ return undefined;
64
+ }
65
+
66
+ public getPriority() {
67
+ return this.priority;
68
+ }
69
+ }
70
+
71
+ // Only try to capture tech buildings within this radius of the starting point.
72
+ const MAX_TECH_CAPTURE_RADIUS = 50;
73
+
74
+ const TECH_CHECK_INTERVAL_TICKS = 300;
75
+
76
+ export class EngineerMissionFactory implements MissionFactory {
77
+ private lastCheckAt = 0;
78
+
79
+ getName(): string {
80
+ return "EngineerMissionFactory";
81
+ }
82
+
83
+ maybeCreateMissions(
84
+ gameApi: GameApi,
85
+ playerData: PlayerData,
86
+ matchAwareness: MatchAwareness,
87
+ missionController: MissionController,
88
+ logger: DebugLogger,
89
+ ): void {
90
+ if (!(gameApi.getCurrentTick() > this.lastCheckAt + TECH_CHECK_INTERVAL_TICKS)) {
91
+ return;
92
+ }
93
+ this.lastCheckAt = gameApi.getCurrentTick();
94
+ const eligibleTechBuildings = gameApi.getVisibleUnits(
95
+ playerData.name,
96
+ "hostile",
97
+ (r) => r.capturable && r.produceCashAmount > 0,
98
+ );
99
+
100
+ eligibleTechBuildings.forEach((techBuildingId) => {
101
+ missionController.addMission(new EngineerMission("capture-" + techBuildingId, 100, techBuildingId, logger));
102
+ });
103
+ }
104
+
105
+ onMissionFailed(
106
+ gameApi: GameApi,
107
+ playerData: PlayerData,
108
+ matchAwareness: MatchAwareness,
109
+ failedMission: Mission<any>,
110
+ failureReason: undefined,
111
+ missionController: MissionController,
112
+ ): void {}
113
+ }