@supalosa/chronodivide-bot 0.3.1 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (149) hide show
  1. package/.env.template +5 -0
  2. package/README.md +57 -39
  3. package/dist/bot/bot.js +27 -37
  4. package/dist/bot/bot.js.map +1 -1
  5. package/dist/bot/logic/awareness.js +13 -8
  6. package/dist/bot/logic/awareness.js.map +1 -1
  7. package/dist/bot/logic/awarenessImpl.js +132 -0
  8. package/dist/bot/logic/awarenessImpl.js.map +1 -0
  9. package/dist/bot/logic/building/ArtilleryUnit.js +2 -29
  10. package/dist/bot/logic/building/ArtilleryUnit.js.map +1 -0
  11. package/dist/bot/logic/building/antiAirStaticDefence.js +43 -0
  12. package/dist/bot/logic/building/antiAirStaticDefence.js.map +1 -0
  13. package/dist/bot/logic/building/antiGroundStaticDefence.js +8 -5
  14. package/dist/bot/logic/building/antiGroundStaticDefence.js.map +1 -1
  15. package/dist/bot/logic/building/basicAirUnit.js +2 -23
  16. package/dist/bot/logic/building/basicAirUnit.js.map +1 -1
  17. package/dist/bot/logic/building/basicBuilding.js +3 -2
  18. package/dist/bot/logic/building/basicBuilding.js.map +1 -1
  19. package/dist/bot/logic/building/basicGroundUnit.js +2 -43
  20. package/dist/bot/logic/building/basicGroundUnit.js.map +1 -1
  21. package/dist/bot/logic/building/building.js +55 -11
  22. package/dist/bot/logic/building/building.js.map +1 -0
  23. package/dist/bot/logic/building/buildingRules.js +62 -50
  24. package/dist/bot/logic/building/buildingRules.js.map +1 -1
  25. package/dist/bot/logic/building/common.js +19 -0
  26. package/dist/bot/logic/building/common.js.map +1 -0
  27. package/dist/bot/logic/building/harvester.js +2 -1
  28. package/dist/bot/logic/building/harvester.js.map +1 -1
  29. package/dist/bot/logic/building/queueController.js +73 -41
  30. package/dist/bot/logic/building/queueController.js.map +1 -1
  31. package/dist/bot/logic/common/utils.js +35 -0
  32. package/dist/bot/logic/common/utils.js.map +1 -1
  33. package/dist/bot/logic/composition/alliedCompositions.js +13 -0
  34. package/dist/bot/logic/composition/alliedCompositions.js.map +1 -0
  35. package/dist/bot/logic/composition/common.js +2 -0
  36. package/dist/bot/logic/composition/common.js.map +1 -0
  37. package/dist/bot/logic/composition/sovietCompositions.js +13 -0
  38. package/dist/bot/logic/composition/sovietCompositions.js.map +1 -0
  39. package/dist/bot/logic/mission/actionBatcher.js +92 -0
  40. package/dist/bot/logic/mission/actionBatcher.js.map +1 -0
  41. package/dist/bot/logic/mission/behaviours/combatSquad.js +124 -0
  42. package/dist/bot/logic/mission/behaviours/combatSquad.js.map +1 -0
  43. package/dist/bot/logic/mission/behaviours/common.js +58 -0
  44. package/dist/bot/logic/mission/behaviours/common.js.map +1 -0
  45. package/dist/bot/logic/mission/behaviours/engineerSquad.js +39 -0
  46. package/dist/bot/logic/mission/behaviours/engineerSquad.js.map +1 -0
  47. package/dist/bot/logic/mission/behaviours/expansionSquad.js +46 -0
  48. package/dist/bot/logic/mission/behaviours/expansionSquad.js.map +1 -0
  49. package/dist/bot/logic/mission/behaviours/retreatSquad.js +31 -0
  50. package/dist/bot/logic/mission/behaviours/retreatSquad.js.map +1 -0
  51. package/{src/bot/logic/squad/behaviours/scoutingSquad.ts → dist/bot/logic/mission/behaviours/scoutingSquad.js} +29 -47
  52. package/dist/bot/logic/mission/behaviours/scoutingSquad.js.map +1 -0
  53. package/dist/bot/logic/mission/mission.js +91 -19
  54. package/dist/bot/logic/mission/mission.js.map +1 -1
  55. package/dist/bot/logic/mission/missionController.js +262 -21
  56. package/dist/bot/logic/mission/missionController.js.map +1 -1
  57. package/dist/bot/logic/mission/missions/attackMission.js +159 -52
  58. package/dist/bot/logic/mission/missions/attackMission.js.map +1 -1
  59. package/dist/bot/logic/mission/missions/basicMission.js +13 -0
  60. package/dist/bot/logic/mission/missions/basicMission.js.map +1 -0
  61. package/dist/bot/logic/mission/missions/defenceMission.js +43 -28
  62. package/dist/bot/logic/mission/missions/defenceMission.js.map +1 -1
  63. package/dist/bot/logic/mission/missions/engineerMission.js +37 -7
  64. package/dist/bot/logic/mission/missions/engineerMission.js.map +1 -1
  65. package/dist/bot/logic/mission/missions/expansionMission.js +42 -6
  66. package/dist/bot/logic/mission/missions/expansionMission.js.map +1 -1
  67. package/dist/bot/logic/mission/missions/missionBehaviour.js +2 -0
  68. package/dist/bot/logic/mission/missions/missionBehaviour.js.map +1 -0
  69. package/dist/bot/logic/mission/missions/retreatMission.js +31 -5
  70. package/dist/bot/logic/mission/missions/retreatMission.js.map +1 -1
  71. package/dist/bot/logic/mission/missions/scoutingMission.js +103 -6
  72. package/dist/bot/logic/mission/missions/scoutingMission.js.map +1 -1
  73. package/dist/bot/logic/mission/missions/squads/combatSquad.js +116 -0
  74. package/dist/bot/logic/mission/missions/squads/combatSquad.js.map +1 -0
  75. package/dist/bot/logic/mission/missions/squads/common.js +58 -0
  76. package/dist/bot/logic/mission/missions/squads/common.js.map +1 -0
  77. package/dist/bot/logic/mission/missions/squads/squad.js +2 -0
  78. package/dist/bot/logic/mission/missions/squads/squad.js.map +1 -0
  79. package/dist/bot/logic/squad/behaviours/attackSquad.js +63 -56
  80. package/dist/bot/logic/squad/behaviours/combatSquad.js +19 -18
  81. package/dist/bot/logic/squad/behaviours/combatSquad.js.map +1 -1
  82. package/dist/bot/logic/squad/behaviours/common.js +2 -19
  83. package/dist/bot/logic/squad/behaviours/common.js.map +1 -1
  84. package/dist/bot/logic/squad/behaviours/defenceSquad.js +15 -2
  85. package/dist/bot/logic/squad/behaviours/retreatSquad.js.map +1 -1
  86. package/dist/bot/logic/squad/behaviours/scoutingSquad.js +17 -21
  87. package/dist/bot/logic/squad/behaviours/scoutingSquad.js.map +1 -1
  88. package/dist/bot/logic/squad/squad.js +8 -5
  89. package/dist/bot/logic/squad/squad.js.map +1 -1
  90. package/dist/bot/logic/squad/squadBehaviour.js.map +1 -1
  91. package/dist/bot/logic/squad/squadController.js +3 -2
  92. package/dist/bot/logic/squad/squadController.js.map +1 -1
  93. package/dist/bot/logic/threat/threatCalculator.js +5 -5
  94. package/dist/bot/logic/threat/threatCalculator.js.map +1 -1
  95. package/dist/exampleBot.js +53 -16
  96. package/dist/exampleBot.js.map +1 -1
  97. package/package.json +5 -4
  98. package/src/bot/bot.ts +38 -53
  99. package/src/bot/logic/awareness.ts +34 -22
  100. package/src/bot/logic/building/antiAirStaticDefence.ts +64 -0
  101. package/src/bot/logic/building/antiGroundStaticDefence.ts +7 -20
  102. package/src/bot/logic/building/artilleryUnit.ts +2 -28
  103. package/src/bot/logic/building/basicAirUnit.ts +2 -33
  104. package/src/bot/logic/building/basicBuilding.ts +8 -6
  105. package/src/bot/logic/building/basicGroundUnit.ts +2 -46
  106. package/src/bot/logic/building/buildingRules.ts +73 -57
  107. package/src/bot/logic/building/common.ts +23 -0
  108. package/src/bot/logic/building/harvester.ts +2 -1
  109. package/src/bot/logic/building/queueController.ts +105 -42
  110. package/src/bot/logic/common/utils.ts +47 -0
  111. package/src/bot/logic/composition/alliedCompositions.ts +22 -0
  112. package/src/bot/logic/composition/common.ts +3 -0
  113. package/src/bot/logic/composition/sovietCompositions.ts +21 -0
  114. package/src/bot/logic/mission/actionBatcher.ts +124 -0
  115. package/src/bot/logic/mission/mission.ts +186 -37
  116. package/src/bot/logic/mission/missionController.ts +340 -31
  117. package/src/bot/logic/mission/missionFactories.ts +3 -3
  118. package/src/bot/logic/mission/missions/attackMission.ts +234 -56
  119. package/src/bot/logic/mission/missions/defenceMission.ts +72 -45
  120. package/src/bot/logic/mission/missions/engineerMission.ts +67 -15
  121. package/src/bot/logic/mission/missions/expansionMission.ts +67 -14
  122. package/src/bot/logic/mission/missions/retreatMission.ts +50 -6
  123. package/src/bot/logic/mission/missions/scoutingMission.ts +138 -14
  124. package/src/bot/logic/mission/missions/squads/combatSquad.ts +160 -0
  125. package/src/bot/logic/{squad/behaviours → mission/missions/squads}/common.ts +14 -20
  126. package/src/bot/logic/mission/missions/squads/squad.ts +19 -0
  127. package/src/bot/logic/threat/threat.ts +15 -15
  128. package/src/bot/logic/threat/threatCalculator.ts +10 -10
  129. package/src/exampleBot.ts +59 -19
  130. package/.prettierrc +0 -5
  131. package/TODO.md +0 -18
  132. package/dist/bot/logic/building/artilleryUnit.js.map +0 -1
  133. package/dist/bot/logic/building/massedAntiGroundUnit.js +0 -20
  134. package/dist/bot/logic/building/queues.js +0 -19
  135. package/dist/bot/logic/knowledge.js +0 -1
  136. package/dist/bot/logic/mission/basicMission.js +0 -26
  137. package/dist/bot/logic/mission/expansionMission.js +0 -32
  138. package/dist/bot/logic/squad/behaviours/squadExpansion.js +0 -31
  139. package/dist/bot/logic/squad/behaviours/squadScouters.js +0 -8
  140. package/rules.ini +0 -23126
  141. package/src/bot/logic/mission/missions/oneTimeMission.ts +0 -33
  142. package/src/bot/logic/squad/behaviours/combatSquad.ts +0 -127
  143. package/src/bot/logic/squad/behaviours/engineerSquad.ts +0 -53
  144. package/src/bot/logic/squad/behaviours/expansionSquad.ts +0 -59
  145. package/src/bot/logic/squad/behaviours/retreatSquad.ts +0 -44
  146. package/src/bot/logic/squad/squad.ts +0 -159
  147. package/src/bot/logic/squad/squadBehaviour.ts +0 -62
  148. package/src/bot/logic/squad/squadBehaviours.ts +0 -8
  149. package/src/bot/logic/squad/squadController.ts +0 -254
@@ -1,20 +1,74 @@
1
- import { GameApi, PlayerData } from "@chronodivide/game-api";
2
- import { GlobalThreat } from "../../threat/threat.js";
3
- import { Mission } from "../mission.js";
4
- import { ExpansionSquad } from "../../squad/behaviours/expansionSquad.js";
1
+ import { ActionsApi, GameApi, OrderType, PlayerData } from "@chronodivide/game-api";
2
+ import { Mission, MissionAction, disbandMission, noop, requestSpecificUnits, requestUnits } from "../mission.js";
5
3
  import { MissionFactory } from "../missionFactories.js";
6
- import { OneTimeMission } from "./oneTimeMission.js";
7
4
  import { MatchAwareness } from "../../awareness.js";
8
5
  import { MissionController } from "../missionController.js";
9
6
  import { DebugLogger } from "../../common/utils.js";
7
+ import { ActionBatcher } from "../actionBatcher.js";
8
+
9
+ const DEPLOY_COOLDOWN_TICKS = 30;
10
10
 
11
11
  /**
12
12
  * A mission that tries to create an MCV (if it doesn't exist) and deploy it somewhere it can be deployed.
13
13
  */
14
- export class ExpansionMission extends OneTimeMission {
15
- constructor(uniqueName: string, priority: number, selectedMcv: number | null,
16
- logger: DebugLogger) {
17
- super(uniqueName, priority, () => new ExpansionSquad(selectedMcv), logger);
14
+ export class ExpansionMission extends Mission {
15
+ private hasAttemptedDeployWith: {
16
+ unitId: number;
17
+ gameTick: number;
18
+ } | null = null;
19
+
20
+ constructor(
21
+ uniqueName: string,
22
+ private priority: number,
23
+ private selectedMcv: number | null,
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 mcvTypes = ["AMCV", "SMCV"];
37
+ const mcvs = this.getUnitsOfTypes(gameApi, ...mcvTypes);
38
+ if (mcvs.length === 0) {
39
+ // Perhaps we deployed already (or the unit was destroyed), end the mission.
40
+ if (this.hasAttemptedDeployWith !== null) {
41
+ return disbandMission();
42
+ }
43
+ // We need an mcv!
44
+ if (this.selectedMcv) {
45
+ return requestSpecificUnits([this.selectedMcv], this.priority);
46
+ } else {
47
+ return requestUnits(mcvTypes, this.priority);
48
+ }
49
+ } else if (
50
+ !this.hasAttemptedDeployWith ||
51
+ gameApi.getCurrentTick() > this.hasAttemptedDeployWith.gameTick + DEPLOY_COOLDOWN_TICKS
52
+ ) {
53
+ actionsApi.orderUnits(
54
+ mcvs.map((mcv) => mcv.id),
55
+ OrderType.DeploySelected,
56
+ );
57
+ // Add a cooldown to deploy attempts.
58
+ this.hasAttemptedDeployWith = {
59
+ unitId: mcvs[0].id,
60
+ gameTick: gameApi.getCurrentTick(),
61
+ };
62
+ }
63
+ return noop();
64
+ }
65
+
66
+ public getGlobalDebugText(): string | undefined {
67
+ return `Expand with MCV ${this.selectedMcv}`;
68
+ }
69
+
70
+ public getPriority() {
71
+ return this.priority;
18
72
  }
19
73
  }
20
74
 
@@ -28,11 +82,11 @@ export class ExpansionMissionFactory implements MissionFactory {
28
82
  playerData: PlayerData,
29
83
  matchAwareness: MatchAwareness,
30
84
  missionController: MissionController,
31
- logger: DebugLogger
85
+ logger: DebugLogger,
32
86
  ): void {
33
87
  // At this point, only expand if we have a loose MCV.
34
88
  const mcvs = gameApi.getVisibleUnits(playerData.name, "self", (r) =>
35
- gameApi.getGeneralRules().baseUnit.includes(r.name)
89
+ gameApi.getGeneralRules().baseUnit.includes(r.name),
36
90
  );
37
91
  mcvs.forEach((mcv) => {
38
92
  missionController.addMission(new ExpansionMission("expand-with-" + mcv, 100, mcv, logger));
@@ -43,9 +97,8 @@ export class ExpansionMissionFactory implements MissionFactory {
43
97
  gameApi: GameApi,
44
98
  playerData: PlayerData,
45
99
  matchAwareness: MatchAwareness,
46
- failedMission: Mission,
100
+ failedMission: Mission<any>,
47
101
  failureReason: undefined,
48
102
  missionController: MissionController,
49
- ): void {
50
- }
103
+ ): void {}
51
104
  }
@@ -1,10 +1,54 @@
1
- import { OneTimeMission } from "./oneTimeMission.js";
2
- import { RetreatSquad } from "../../squad/behaviours/retreatSquad.js";
3
1
  import { DebugLogger } from "../../common/utils.js";
4
- import { Vector2 } from "@chronodivide/game-api";
2
+ import { ActionsApi, GameApi, OrderType, PlayerData, Vector2 } from "@chronodivide/game-api";
3
+ import { Mission, MissionAction, disbandMission, requestSpecificUnits } from "../mission.js";
4
+ import { ActionBatcher } from "../actionBatcher.js";
5
+ import { MatchAwareness } from "../../awareness.js";
5
6
 
6
- export class RetreatMission extends OneTimeMission {
7
- constructor(uniqueName: string, priority: number, retreatToPoint: Vector2, unitIds: number[], logger: DebugLogger) {
8
- super(uniqueName, priority, () => new RetreatSquad(unitIds, retreatToPoint), logger);
7
+ export class RetreatMission extends Mission {
8
+ private createdAt: number | null = null;
9
+
10
+ constructor(
11
+ uniqueName: string,
12
+ private retreatToPoint: Vector2,
13
+ private withUnitIds: number[],
14
+ logger: DebugLogger,
15
+ ) {
16
+ super(uniqueName, logger);
17
+ }
18
+
19
+ public _onAiUpdate(
20
+ gameApi: GameApi,
21
+ actionsApi: ActionsApi,
22
+ playerData: PlayerData,
23
+ matchAwareness: MatchAwareness,
24
+ actionBatcher: ActionBatcher,
25
+ ): MissionAction {
26
+ if (!this.createdAt) {
27
+ this.createdAt = gameApi.getCurrentTick();
28
+ }
29
+ if (this.getUnitIds().length > 0) {
30
+ // Only send the order once we have managed to claim some units.
31
+ actionsApi.orderUnits(
32
+ this.getUnitIds(),
33
+ OrderType.AttackMove,
34
+ this.retreatToPoint.x,
35
+ this.retreatToPoint.y,
36
+ );
37
+ return disbandMission();
38
+ }
39
+ if (this.createdAt && gameApi.getCurrentTick() > this.createdAt + 240) {
40
+ // Disband automatically after 240 ticks in case we couldn't actually claim any units.
41
+ return disbandMission();
42
+ } else {
43
+ return requestSpecificUnits(this.withUnitIds, 1000);
44
+ }
45
+ }
46
+
47
+ public getGlobalDebugText(): string | undefined {
48
+ return `retreat with ${this.withUnitIds.length} units`;
49
+ }
50
+
51
+ public getPriority() {
52
+ return 100;
9
53
  }
10
54
  }
@@ -1,21 +1,138 @@
1
- import { GameApi, PlayerData } from "@chronodivide/game-api";
2
- import { ScoutingSquad } from "../../squad/behaviours/scoutingSquad.js";
1
+ import { ActionsApi, GameApi, OrderType, PlayerData, Vector2 } from "@chronodivide/game-api";
3
2
  import { MissionFactory } from "../missionFactories.js";
4
- import { OneTimeMission } from "./oneTimeMission.js";
5
3
  import { MatchAwareness } from "../../awareness.js";
6
- import { Mission } from "../mission.js";
4
+ import { Mission, MissionAction, disbandMission, noop, requestUnits } from "../mission.js";
7
5
  import { AttackMission } from "./attackMission.js";
8
6
  import { MissionController } from "../missionController.js";
9
- import { getUnseenStartingLocations } from "../../common/scout.js";
10
7
  import { DebugLogger } from "../../common/utils.js";
8
+ import { ActionBatcher } from "../actionBatcher.js";
9
+ import { getDistanceBetweenTileAndPoint } from "../../map/map.js";
10
+ import { PrioritisedScoutTarget } from "../../common/scout.js";
11
+
12
+ const SCOUT_MOVE_COOLDOWN_TICKS = 30;
13
+
14
+ // Max units to spend on a particular scout target.
15
+ const MAX_ATTEMPTS_PER_TARGET = 5;
16
+
17
+ // Maximum ticks to spend trying to scout a target *without making progress towards it*.
18
+ // Every time a unit gets closer to the target, the timer refreshes.
19
+ const MAX_TICKS_PER_TARGET = 600;
11
20
 
12
21
  /**
13
22
  * A mission that tries to scout around the map with a cheap, fast unit (usually attack dogs)
14
23
  */
15
- export class ScoutingMission extends OneTimeMission {
16
- constructor(uniqueName: string, priority: number,
17
- logger: DebugLogger) {
18
- super(uniqueName, priority, () => new ScoutingSquad(), logger);
24
+ export class ScoutingMission extends Mission {
25
+ private scoutTarget: Vector2 | null = null;
26
+ private attemptsOnCurrentTarget: number = 0;
27
+ private scoutTargetRefreshedAt: number = 0;
28
+ private lastMoveCommandTick: number = 0;
29
+ private scoutTargetIsPermanent: boolean = false;
30
+
31
+ // Minimum distance from a scout to the target.
32
+ private scoutMinDistance?: number;
33
+
34
+ private hadUnit: boolean = false;
35
+
36
+ constructor(
37
+ uniqueName: string,
38
+ private priority: number,
39
+ logger: DebugLogger,
40
+ ) {
41
+ super(uniqueName, logger);
42
+ }
43
+
44
+ public _onAiUpdate(
45
+ gameApi: GameApi,
46
+ actionsApi: ActionsApi,
47
+ playerData: PlayerData,
48
+ matchAwareness: MatchAwareness,
49
+ actionBatcher: ActionBatcher,
50
+ ): MissionAction {
51
+ const scoutNames = ["ADOG", "DOG", "E1", "E2", "FV", "HTK"];
52
+ const scouts = this.getUnitsOfTypes(gameApi, ...scoutNames);
53
+
54
+ if ((matchAwareness.getSectorCache().getOverallVisibility() || 0) > 0.9) {
55
+ return disbandMission();
56
+ }
57
+
58
+ if (scouts.length === 0) {
59
+ // Count the number of times the scout dies trying to uncover the current scoutTarget.
60
+ if (this.scoutTarget && this.hadUnit) {
61
+ this.attemptsOnCurrentTarget++;
62
+ this.hadUnit = false;
63
+ }
64
+ return requestUnits(scoutNames, this.priority);
65
+ } else if (this.scoutTarget) {
66
+ this.hadUnit = true;
67
+ if (!this.scoutTargetIsPermanent) {
68
+ if (this.attemptsOnCurrentTarget > MAX_ATTEMPTS_PER_TARGET) {
69
+ this.logger(
70
+ `Scout target ${this.scoutTarget.x},${this.scoutTarget.y} took too many attempts, moving to next`,
71
+ );
72
+ this.setScoutTarget(null, 0);
73
+ return noop();
74
+ }
75
+ if (gameApi.getCurrentTick() > this.scoutTargetRefreshedAt + MAX_TICKS_PER_TARGET) {
76
+ this.logger(
77
+ `Scout target ${this.scoutTarget.x},${this.scoutTarget.y} took too long, moving to next`,
78
+ );
79
+ this.setScoutTarget(null, 0);
80
+ return noop();
81
+ }
82
+ }
83
+ const targetTile = gameApi.mapApi.getTile(this.scoutTarget.x, this.scoutTarget.y);
84
+ if (!targetTile) {
85
+ throw new Error(`target tile ${this.scoutTarget.x},${this.scoutTarget.y} does not exist`);
86
+ }
87
+ if (gameApi.getCurrentTick() > this.lastMoveCommandTick + SCOUT_MOVE_COOLDOWN_TICKS) {
88
+ this.lastMoveCommandTick = gameApi.getCurrentTick();
89
+ scouts.forEach((unit) => {
90
+ if (this.scoutTarget) {
91
+ actionsApi.orderUnits([unit.id], OrderType.AttackMove, this.scoutTarget.x, this.scoutTarget.y);
92
+ }
93
+ });
94
+ // Check that a scout is actually moving closer to the target.
95
+ const distances = scouts.map((unit) => getDistanceBetweenTileAndPoint(unit.tile, this.scoutTarget!));
96
+ const newMinDistance = Math.min(...distances);
97
+ if (!this.scoutMinDistance || newMinDistance < this.scoutMinDistance) {
98
+ this.logger(
99
+ `Scout timeout refreshed because unit moved closer to point (${newMinDistance} < ${this.scoutMinDistance})`,
100
+ );
101
+ this.scoutTargetRefreshedAt = gameApi.getCurrentTick();
102
+ this.scoutMinDistance = newMinDistance;
103
+ }
104
+ }
105
+ if (gameApi.mapApi.isVisibleTile(targetTile, playerData.name)) {
106
+ this.logger(
107
+ `Scout target ${this.scoutTarget.x},${this.scoutTarget.y} successfully scouted, moving to next`,
108
+ );
109
+ this.setScoutTarget(null, gameApi.getCurrentTick());
110
+ }
111
+ } else {
112
+ const nextScoutTarget = matchAwareness.getScoutingManager().getNewScoutTarget();
113
+ if (!nextScoutTarget) {
114
+ this.logger(`No more scouting targets available, disbanding.`);
115
+ return disbandMission();
116
+ }
117
+ this.setScoutTarget(nextScoutTarget, gameApi.getCurrentTick());
118
+ }
119
+ return noop();
120
+ }
121
+
122
+ setScoutTarget(target: PrioritisedScoutTarget | null, currentTick: number) {
123
+ this.attemptsOnCurrentTarget = 0;
124
+ this.scoutTargetRefreshedAt = currentTick;
125
+ this.scoutTarget = target?.asVector2() ?? null;
126
+ this.scoutMinDistance = undefined;
127
+ this.scoutTargetIsPermanent = target?.isPermanent ?? false;
128
+ }
129
+
130
+ public getGlobalDebugText(): string | undefined {
131
+ return "scouting";
132
+ }
133
+
134
+ public getPriority() {
135
+ return this.priority;
19
136
  }
20
137
  }
21
138
 
@@ -33,7 +150,7 @@ export class ScoutingMissionFactory implements MissionFactory {
33
150
  playerData: PlayerData,
34
151
  matchAwareness: MatchAwareness,
35
152
  missionController: MissionController,
36
- logger: DebugLogger
153
+ logger: DebugLogger,
37
154
  ): void {
38
155
  if (gameApi.getCurrentTick() < this.lastScoutAt + SCOUT_COOLDOWN_TICKS) {
39
156
  return;
@@ -41,7 +158,7 @@ export class ScoutingMissionFactory implements MissionFactory {
41
158
  if (!matchAwareness.getScoutingManager().hasScoutTargets()) {
42
159
  return;
43
160
  }
44
- if (!missionController.addMission(new ScoutingMission("globalScout", 100, logger))) {
161
+ if (!missionController.addMission(new ScoutingMission("globalScout", 10, logger))) {
45
162
  this.lastScoutAt = gameApi.getCurrentTick();
46
163
  }
47
164
  }
@@ -50,13 +167,20 @@ export class ScoutingMissionFactory implements MissionFactory {
50
167
  gameApi: GameApi,
51
168
  playerData: PlayerData,
52
169
  matchAwareness: MatchAwareness,
53
- failedMission: Mission,
170
+ failedMission: Mission<any>,
54
171
  failureReason: undefined,
55
172
  missionController: MissionController,
56
- logger: DebugLogger
173
+ logger: DebugLogger,
57
174
  ): void {
175
+ if (gameApi.getCurrentTick() < this.lastScoutAt + SCOUT_COOLDOWN_TICKS) {
176
+ return;
177
+ }
178
+ if (!matchAwareness.getScoutingManager().hasScoutTargets()) {
179
+ return;
180
+ }
58
181
  if (failedMission instanceof AttackMission) {
59
- missionController.addMission(new ScoutingMission("globalScout", 100, logger));
182
+ missionController.addMission(new ScoutingMission("globalScout", 10, logger));
183
+ this.lastScoutAt = gameApi.getCurrentTick();
60
184
  }
61
185
  }
62
186
  }
@@ -0,0 +1,160 @@
1
+ import {
2
+ ActionsApi,
3
+ AttackState,
4
+ GameApi,
5
+ GameMath,
6
+ MovementZone,
7
+ PlayerData,
8
+ UnitData,
9
+ Vector2,
10
+ } from "@chronodivide/game-api";
11
+ import { MatchAwareness } from "../../../awareness.js";
12
+ import { getAttackWeight, manageAttackMicro, manageMoveMicro } from "./common.js";
13
+ import { DebugLogger, isOwnedByNeutral, maxBy, minBy } from "../../../common/utils.js";
14
+ import { ActionBatcher, BatchableAction } from "../../actionBatcher.js";
15
+ import { Squad } from "./squad.js";
16
+ import { Mission, MissionAction, grabCombatants, noop } from "../../mission.js";
17
+
18
+ const TARGET_UPDATE_INTERVAL_TICKS = 10;
19
+
20
+ // Units must be in a certain radius of the center of mass before attacking.
21
+ // This scales for number of units in the squad though.
22
+ const MIN_GATHER_RADIUS = 5;
23
+
24
+ // If the radius expands beyond this amount then we should switch back to gathering mode.
25
+ const MAX_GATHER_RADIUS = 15;
26
+
27
+ const GATHER_RATIO = 10;
28
+
29
+ const ATTACK_SCAN_AREA = 15;
30
+
31
+ enum SquadState {
32
+ Gathering,
33
+ Attacking,
34
+ }
35
+
36
+ export class CombatSquad implements Squad {
37
+ private lastCommand: number | null = null;
38
+ private state = SquadState.Gathering;
39
+
40
+ private debugLastTarget: string | undefined;
41
+
42
+ private lastOrderGiven: { [unitId: number]: BatchableAction } = {};
43
+
44
+ /**
45
+ *
46
+ * @param rallyArea the initial location to grab combatants
47
+ * @param targetArea
48
+ * @param radius
49
+ */
50
+ constructor(
51
+ private rallyArea: Vector2,
52
+ private targetArea: Vector2,
53
+ private radius: number,
54
+ ) {}
55
+
56
+ public getGlobalDebugText(): string | undefined {
57
+ return this.debugLastTarget ?? "<none>";
58
+ }
59
+
60
+ public setAttackArea(targetArea: Vector2) {
61
+ this.targetArea = targetArea;
62
+ }
63
+
64
+ public onAiUpdate(
65
+ gameApi: GameApi,
66
+ actionsApi: ActionsApi,
67
+ actionBatcher: ActionBatcher,
68
+ playerData: PlayerData,
69
+ mission: Mission<any>,
70
+ matchAwareness: MatchAwareness,
71
+ logger: DebugLogger,
72
+ ): MissionAction {
73
+ if (
74
+ mission.getUnitIds().length > 0 &&
75
+ (!this.lastCommand || gameApi.getCurrentTick() > this.lastCommand + TARGET_UPDATE_INTERVAL_TICKS)
76
+ ) {
77
+ this.lastCommand = gameApi.getCurrentTick();
78
+ const centerOfMass = mission.getCenterOfMass();
79
+ const maxDistance = mission.getMaxDistanceToCenterOfMass();
80
+ const units = mission.getUnitsMatching(gameApi, (r) => r.rules.isSelectableCombatant);
81
+
82
+ // Only use ground units for center of mass.
83
+ const groundUnits = mission.getUnitsMatching(
84
+ gameApi,
85
+ (r) =>
86
+ r.rules.isSelectableCombatant &&
87
+ (r.rules.movementZone === MovementZone.Infantry ||
88
+ r.rules.movementZone === MovementZone.Normal ||
89
+ r.rules.movementZone === MovementZone.InfantryDestroyer),
90
+ );
91
+
92
+ if (this.state === SquadState.Gathering) {
93
+ const requiredGatherRadius = GameMath.sqrt(groundUnits.length) * GATHER_RATIO + MIN_GATHER_RADIUS;
94
+ if (
95
+ centerOfMass &&
96
+ maxDistance &&
97
+ gameApi.mapApi.getTile(centerOfMass.x, centerOfMass.y) !== undefined &&
98
+ maxDistance > requiredGatherRadius
99
+ ) {
100
+ units.forEach((unit) => {
101
+ this.submitActionIfNew(actionBatcher, manageMoveMicro(unit, centerOfMass));
102
+ });
103
+ } else {
104
+ logger(`CombatSquad ${mission.getUniqueName()} switching back to attack mode (${maxDistance})`);
105
+ this.state = SquadState.Attacking;
106
+ }
107
+ } else {
108
+ const targetPoint = this.targetArea || playerData.startLocation;
109
+ const requiredGatherRadius = GameMath.sqrt(groundUnits.length) * GATHER_RATIO + MAX_GATHER_RADIUS;
110
+ if (
111
+ centerOfMass &&
112
+ maxDistance &&
113
+ gameApi.mapApi.getTile(centerOfMass.x, centerOfMass.y) !== undefined &&
114
+ maxDistance > requiredGatherRadius
115
+ ) {
116
+ // Switch back to gather mode
117
+ logger(`CombatSquad ${mission.getUniqueName()} switching back to gather (${maxDistance})`);
118
+ this.state = SquadState.Gathering;
119
+ return noop();
120
+ }
121
+ // The unit with the shortest range chooses the target. Otherwise, a base range of 5 is chosen.
122
+ const getRangeForUnit = (unit: UnitData) =>
123
+ unit.primaryWeapon?.maxRange ?? unit.secondaryWeapon?.maxRange ?? 5;
124
+ const attackLeader = minBy(units, getRangeForUnit);
125
+ if (!attackLeader) {
126
+ return noop();
127
+ }
128
+ // Find units within double the range of the leader.
129
+ const nearbyHostiles = matchAwareness
130
+ .getHostilesNearPoint(attackLeader.tile.rx, attackLeader.tile.ry, ATTACK_SCAN_AREA)
131
+ .map(({ unitId }) => gameApi.getUnitData(unitId))
132
+ .filter((unit) => !isOwnedByNeutral(unit)) as UnitData[];
133
+
134
+ for (const unit of units) {
135
+ const bestUnit = maxBy(nearbyHostiles, (target) => getAttackWeight(unit, target));
136
+ if (bestUnit) {
137
+ this.submitActionIfNew(actionBatcher, manageAttackMicro(unit, bestUnit));
138
+ this.debugLastTarget = `Unit ${bestUnit.id.toString()}`;
139
+ } else {
140
+ this.submitActionIfNew(actionBatcher, manageMoveMicro(unit, targetPoint));
141
+ this.debugLastTarget = `@${targetPoint.x},${targetPoint.y}`;
142
+ }
143
+ }
144
+ }
145
+ }
146
+ return noop();
147
+ }
148
+
149
+ /**
150
+ * Sends an action to the acitonBatcher if and only if the action is different from the last action we submitted to it.
151
+ * Prevents spamming redundant orders, which affects performance and can also ccause the unit to sit around doing nothing.
152
+ */
153
+ private submitActionIfNew(actionBatcher: ActionBatcher, action: BatchableAction) {
154
+ const lastAction = this.lastOrderGiven[action.unitId];
155
+ if (!lastAction || !lastAction.isSameAs(action)) {
156
+ actionBatcher.push(action);
157
+ this.lastOrderGiven[action.unitId] = action;
158
+ }
159
+ }
160
+ }
@@ -1,38 +1,32 @@
1
- import {
2
- ActionsApi,
3
- AttackState,
4
- ObjectType,
5
- OrderType,
6
- StanceType,
7
- UnitData,
8
- Vector2,
9
- ZoneType,
10
- } from "@chronodivide/game-api";
11
- import { getDistanceBetweenPoints, getDistanceBetweenUnits } from "../../map/map.js";
1
+ import { AttackState, ObjectType, OrderType, StanceType, UnitData, Vector2, ZoneType } from "@chronodivide/game-api";
2
+ import { getDistanceBetweenPoints, getDistanceBetweenUnits } from "../../../map/map.js";
3
+ import { BatchableAction } from "../../actionBatcher.js";
4
+
5
+ const NONCE_GI_DEPLOY = 0;
6
+ const NONCE_GI_UNDEPLOY = 1;
12
7
 
13
8
  // Micro methods
14
- export function manageMoveMicro(actionsApi: ActionsApi, attacker: UnitData, attackPoint: Vector2) {
9
+ export function manageMoveMicro(attacker: UnitData, attackPoint: Vector2): BatchableAction {
15
10
  if (attacker.name === "E1") {
16
11
  const isDeployed = attacker.stance === StanceType.Deployed;
17
12
  if (isDeployed) {
18
- actionsApi.orderUnits([attacker.id], OrderType.DeploySelected);
13
+ return BatchableAction.noTarget(attacker.id, OrderType.DeploySelected, NONCE_GI_UNDEPLOY);
19
14
  }
20
15
  }
21
- actionsApi.orderUnits([attacker.id], OrderType.Move, attackPoint.x, attackPoint.y);
16
+
17
+ return BatchableAction.toPoint(attacker.id, OrderType.AttackMove, attackPoint);
22
18
  }
23
19
 
24
- export function manageAttackMicro(actionsApi: ActionsApi, attacker: UnitData, target: UnitData) {
20
+ export function manageAttackMicro(attacker: UnitData, target: UnitData): BatchableAction {
25
21
  const distance = getDistanceBetweenUnits(attacker, target);
26
22
  if (attacker.name === "E1") {
27
23
  // Para (deployed weapon) range is 5.
28
24
  const deployedWeaponRange = attacker.secondaryWeapon?.maxRange || 5;
29
25
  const isDeployed = attacker.stance === StanceType.Deployed;
30
26
  if (!isDeployed && (distance <= deployedWeaponRange || attacker.attackState === AttackState.JustFired)) {
31
- actionsApi.orderUnits([attacker.id], OrderType.DeploySelected);
32
- return;
27
+ return BatchableAction.noTarget(attacker.id, OrderType.DeploySelected, NONCE_GI_DEPLOY);
33
28
  } else if (isDeployed && distance > deployedWeaponRange) {
34
- actionsApi.orderUnits([attacker.id], OrderType.DeploySelected);
35
- return;
29
+ return BatchableAction.noTarget(attacker.id, OrderType.DeploySelected, NONCE_GI_UNDEPLOY);
36
30
  }
37
31
  }
38
32
  let targetData = target;
@@ -44,7 +38,7 @@ export function manageAttackMicro(actionsApi: ActionsApi, attacker: UnitData, ta
44
38
  // Special case for mirage tank/spy as otherwise they just sit next to it.
45
39
  orderType = OrderType.Attack;
46
40
  }
47
- actionsApi.orderUnits([attacker.id], orderType, target.id);
41
+ return BatchableAction.toTargetId(attacker.id, orderType, target.id);
48
42
  }
49
43
 
50
44
  /**
@@ -0,0 +1,19 @@
1
+ import { ActionsApi, GameApi, PlayerData } from "@chronodivide/game-api";
2
+ import { ActionBatcher } from "../../actionBatcher";
3
+ import { Mission, MissionAction } from "../../mission";
4
+ import { MatchAwareness } from "../../../awareness";
5
+ import { DebugLogger } from "../../../common/utils";
6
+
7
+ export interface Squad {
8
+ onAiUpdate(
9
+ gameApi: GameApi,
10
+ actionsApi: ActionsApi,
11
+ actionBatcher: ActionBatcher,
12
+ playerData: PlayerData,
13
+ mission: Mission<any>,
14
+ matchAwareness: MatchAwareness,
15
+ logger: DebugLogger,
16
+ ): MissionAction;
17
+
18
+ getGlobalDebugText(): string | undefined;
19
+ }
@@ -1,15 +1,15 @@
1
- // A periodically-refreshed cache of known threats to a bot so we can use it in decision making.
2
-
3
- export class GlobalThreat {
4
- constructor(
5
- public certainty: number, // 0.0 - 1.0 based on approximate visibility around the map.
6
- public totalOffensiveLandThreat: number, // a number that approximates how much land-based firepower our opponents have.
7
- public totalOffensiveAirThreat: number, // a number that approximates how much airborne firepower our opponents have.
8
- public totalOffensiveAntiAirThreat: number, // a number that approximates how much anti-air firepower our opponents have.
9
- public totalDefensiveThreat: number, // a number that approximates how much defensive power our opponents have.
10
- public totalDefensivePower: number, // a number that approximates how much defensive power we have.
11
- public totalAvailableAntiGroundFirepower: number, // how much anti-ground power we have
12
- public totalAvailableAntiAirFirepower: number, // how much anti-air power we have
13
- public totalAvailableAirPower: number, // how much firepower we have in air units
14
- ) {}
15
- }
1
+ // A periodically-refreshed cache of known threats to a bot so we can use it in decision making.
2
+
3
+ export class GlobalThreat {
4
+ constructor(
5
+ public certainty: number, // 0.0 - 1.0 based on approximate visibility around the map.
6
+ public totalOffensiveLandThreat: number, // a number that approximates how much land-based firepower our opponents have.
7
+ public totalOffensiveAirThreat: number, // a number that approximates how much airborne firepower our opponents have.
8
+ public totalOffensiveAntiAirThreat: number, // a number that approximates how much anti-air firepower our opponents have.
9
+ public totalDefensiveThreat: number, // a number that approximates how much defensive power our opponents have.
10
+ public totalDefensivePower: number, // a number that approximates how much defensive power we have.
11
+ public totalAvailableAntiGroundFirepower: number, // how much anti-ground power we have
12
+ public totalAvailableAntiAirFirepower: number, // how much anti-air power we have
13
+ public totalAvailableAirPower: number, // how much firepower we have in air units
14
+ ) {}
15
+ }