@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,174 +1,174 @@
1
- // A sector is a uniform-sized segment of the map.
2
-
3
- import { MapApi, PlayerData, Size, Tile, Vector2 } from "@chronodivide/game-api";
4
- import { calculateAreaVisibility } from "./map.js";
5
-
6
- export const SECTOR_SIZE = 8;
7
-
8
- export class Sector {
9
- // How many times we've attempted to enter the sector.
10
- private sectorExploreAttempts: number;
11
- private sectorLastExploredAt: number | undefined;
12
-
13
- constructor(
14
- public sectorStartPoint: Vector2,
15
- public sectorStartTile: Tile | undefined,
16
- public sectorVisibilityPct: number | undefined,
17
- public sectorVisibilityLastCheckTick: number | undefined,
18
- ) {
19
- this.sectorExploreAttempts = 0;
20
- }
21
-
22
- public onExploreAttempted(currentTick: number) {
23
- this.sectorExploreAttempts++;
24
- this.sectorLastExploredAt = currentTick;
25
- }
26
-
27
- // Whether we should attempt to explore this sector, given the cooldown and limit of attempts.
28
- public shouldAttemptExploration(currentTick: number, cooldown: number, limit: number) {
29
- if (limit >= this.sectorExploreAttempts) {
30
- return false;
31
- }
32
-
33
- if (this.sectorLastExploredAt && currentTick < this.sectorLastExploredAt + cooldown) {
34
- return false;
35
- }
36
-
37
- return true;
38
- }
39
- }
40
-
41
- export class SectorCache {
42
- private sectors: Sector[][] = [];
43
- private mapBounds: Size;
44
- private sectorsX: number;
45
- private sectorsY: number;
46
- private lastUpdatedSectorX: number | undefined;
47
- private lastUpdatedSectorY: number | undefined;
48
-
49
- constructor(mapApi: MapApi, mapBounds: Size) {
50
- this.mapBounds = mapBounds;
51
- this.sectorsX = Math.ceil(mapBounds.width / SECTOR_SIZE);
52
- this.sectorsY = Math.ceil(mapBounds.height / SECTOR_SIZE);
53
- this.sectors = new Array(this.sectorsX);
54
- for (let xx = 0; xx < this.sectorsX; ++xx) {
55
- this.sectors[xx] = new Array(this.sectorsY);
56
- for (let yy = 0; yy < this.sectorsY; ++yy) {
57
- const tileX = xx * SECTOR_SIZE;
58
- const tileY = yy * SECTOR_SIZE;
59
- this.sectors[xx][yy] = new Sector(
60
- new Vector2(tileX, tileY),
61
- mapApi.getTile(tileX, tileY),
62
- undefined,
63
- undefined,
64
- );
65
- }
66
- }
67
- }
68
-
69
- public getMapBounds(): Size {
70
- return this.mapBounds;
71
- }
72
-
73
- public updateSectors(currentGameTick: number, maxSectorsToUpdate: number, mapApi: MapApi, playerData: PlayerData) {
74
- let nextSectorX = this.lastUpdatedSectorX ? this.lastUpdatedSectorX + 1 : 0;
75
- let nextSectorY = this.lastUpdatedSectorY ? this.lastUpdatedSectorY : 0;
76
- let updatedThisCycle = 0;
77
-
78
- while (updatedThisCycle < maxSectorsToUpdate) {
79
- if (nextSectorX >= this.sectorsX) {
80
- nextSectorX = 0;
81
- ++nextSectorY;
82
- }
83
- if (nextSectorY >= this.sectorsY) {
84
- nextSectorY = 0;
85
- nextSectorX = 0;
86
- }
87
- let sector: Sector | undefined = this.getSector(nextSectorX, nextSectorY);
88
- if (sector) {
89
- sector.sectorVisibilityLastCheckTick = currentGameTick;
90
- let sp = sector.sectorStartPoint;
91
- let ep = new Vector2(sp.x + SECTOR_SIZE, sp.y + SECTOR_SIZE);
92
- let visibility = calculateAreaVisibility(mapApi, playerData, sp, ep);
93
- if (visibility.validTiles > 0) {
94
- sector.sectorVisibilityPct = visibility.visibleTiles / visibility.validTiles;
95
- } else {
96
- sector.sectorVisibilityPct = undefined;
97
- }
98
- }
99
- this.lastUpdatedSectorX = nextSectorX;
100
- this.lastUpdatedSectorY = nextSectorY;
101
- ++nextSectorX;
102
- ++updatedThisCycle;
103
- }
104
- }
105
-
106
- // Return % of sectors that are updated.
107
- public getSectorUpdateRatio(sectorsUpdatedSinceGameTick: number): number {
108
- let updated = 0,
109
- total = 0;
110
- for (let xx = 0; xx < this.sectorsX; ++xx) {
111
- for (let yy = 0; yy < this.sectorsY; ++yy) {
112
- let sector: Sector = this.sectors[xx][yy];
113
- if (
114
- sector &&
115
- sector.sectorVisibilityLastCheckTick &&
116
- sector.sectorVisibilityLastCheckTick >= sectorsUpdatedSinceGameTick
117
- ) {
118
- ++updated;
119
- }
120
- ++total;
121
- }
122
- }
123
- return updated / total;
124
- }
125
-
126
- /**
127
- * Return the ratio (0-1) of tiles that are visible. Returns undefined if we haven't scanned the whole map yet.
128
- */
129
- public getOverallVisibility(): number | undefined {
130
- let visible = 0,
131
- total = 0;
132
- for (let xx = 0; xx < this.sectorsX; ++xx) {
133
- for (let yy = 0; yy < this.sectorsY; ++yy) {
134
- let sector: Sector = this.sectors[xx][yy];
135
-
136
- // Undefined visibility.
137
- if (sector.sectorVisibilityPct != undefined) {
138
- visible += sector.sectorVisibilityPct;
139
- total += 1.0;
140
- }
141
- }
142
- }
143
- return visible / total;
144
- }
145
-
146
- public getSector(sectorX: number, sectorY: number): Sector | undefined {
147
- if (sectorX < 0 || sectorX >= this.sectorsX || sectorY < 0 || sectorY >= this.sectorsY) {
148
- return undefined;
149
- }
150
- return this.sectors[sectorX][sectorY];
151
- }
152
-
153
- public getSectorBounds(): Size {
154
- return { width: this.sectorsX, height: this.sectorsY };
155
- }
156
-
157
- public getSectorCoordinatesForWorldPosition(x: number, y: number) {
158
- if (x < 0 || x >= this.mapBounds.width || y < 0 || y >= this.mapBounds.height) {
159
- return undefined;
160
- }
161
- return {
162
- sectorX: Math.floor(x / SECTOR_SIZE),
163
- sectorY: Math.floor(y / SECTOR_SIZE),
164
- };
165
- }
166
-
167
- public getSectorForWorldPosition(x: number, y: number): Sector | undefined {
168
- const sectorCoordinates = this.getSectorCoordinatesForWorldPosition(x, y);
169
- if (!sectorCoordinates) {
170
- return undefined;
171
- }
172
- return this.sectors[Math.floor(x / SECTOR_SIZE)][Math.floor(y / SECTOR_SIZE)];
173
- }
174
- }
1
+ // A sector is a uniform-sized segment of the map.
2
+
3
+ import { MapApi, PlayerData, Size, Tile, Vector2 } from "@chronodivide/game-api";
4
+ import { calculateAreaVisibility } from "./map.js";
5
+
6
+ export const SECTOR_SIZE = 8;
7
+
8
+ export class Sector {
9
+ // How many times we've attempted to enter the sector.
10
+ private sectorExploreAttempts: number;
11
+ private sectorLastExploredAt: number | undefined;
12
+
13
+ constructor(
14
+ public sectorStartPoint: Vector2,
15
+ public sectorStartTile: Tile | undefined,
16
+ public sectorVisibilityPct: number | undefined,
17
+ public sectorVisibilityLastCheckTick: number | undefined,
18
+ ) {
19
+ this.sectorExploreAttempts = 0;
20
+ }
21
+
22
+ public onExploreAttempted(currentTick: number) {
23
+ this.sectorExploreAttempts++;
24
+ this.sectorLastExploredAt = currentTick;
25
+ }
26
+
27
+ // Whether we should attempt to explore this sector, given the cooldown and limit of attempts.
28
+ public shouldAttemptExploration(currentTick: number, cooldown: number, limit: number) {
29
+ if (limit >= this.sectorExploreAttempts) {
30
+ return false;
31
+ }
32
+
33
+ if (this.sectorLastExploredAt && currentTick < this.sectorLastExploredAt + cooldown) {
34
+ return false;
35
+ }
36
+
37
+ return true;
38
+ }
39
+ }
40
+
41
+ export class SectorCache {
42
+ private sectors: Sector[][] = [];
43
+ private mapBounds: Size;
44
+ private sectorsX: number;
45
+ private sectorsY: number;
46
+ private lastUpdatedSectorX: number | undefined;
47
+ private lastUpdatedSectorY: number | undefined;
48
+
49
+ constructor(mapApi: MapApi, mapBounds: Size) {
50
+ this.mapBounds = mapBounds;
51
+ this.sectorsX = Math.ceil(mapBounds.width / SECTOR_SIZE);
52
+ this.sectorsY = Math.ceil(mapBounds.height / SECTOR_SIZE);
53
+ this.sectors = new Array(this.sectorsX);
54
+ for (let xx = 0; xx < this.sectorsX; ++xx) {
55
+ this.sectors[xx] = new Array(this.sectorsY);
56
+ for (let yy = 0; yy < this.sectorsY; ++yy) {
57
+ const tileX = xx * SECTOR_SIZE;
58
+ const tileY = yy * SECTOR_SIZE;
59
+ this.sectors[xx][yy] = new Sector(
60
+ new Vector2(tileX, tileY),
61
+ mapApi.getTile(tileX, tileY),
62
+ undefined,
63
+ undefined,
64
+ );
65
+ }
66
+ }
67
+ }
68
+
69
+ public getMapBounds(): Size {
70
+ return this.mapBounds;
71
+ }
72
+
73
+ public updateSectors(currentGameTick: number, maxSectorsToUpdate: number, mapApi: MapApi, playerData: PlayerData) {
74
+ let nextSectorX = this.lastUpdatedSectorX ? this.lastUpdatedSectorX + 1 : 0;
75
+ let nextSectorY = this.lastUpdatedSectorY ? this.lastUpdatedSectorY : 0;
76
+ let updatedThisCycle = 0;
77
+
78
+ while (updatedThisCycle < maxSectorsToUpdate) {
79
+ if (nextSectorX >= this.sectorsX) {
80
+ nextSectorX = 0;
81
+ ++nextSectorY;
82
+ }
83
+ if (nextSectorY >= this.sectorsY) {
84
+ nextSectorY = 0;
85
+ nextSectorX = 0;
86
+ }
87
+ let sector: Sector | undefined = this.getSector(nextSectorX, nextSectorY);
88
+ if (sector) {
89
+ sector.sectorVisibilityLastCheckTick = currentGameTick;
90
+ let sp = sector.sectorStartPoint;
91
+ let ep = new Vector2(sp.x + SECTOR_SIZE, sp.y + SECTOR_SIZE);
92
+ let visibility = calculateAreaVisibility(mapApi, playerData, sp, ep);
93
+ if (visibility.validTiles > 0) {
94
+ sector.sectorVisibilityPct = visibility.visibleTiles / visibility.validTiles;
95
+ } else {
96
+ sector.sectorVisibilityPct = undefined;
97
+ }
98
+ }
99
+ this.lastUpdatedSectorX = nextSectorX;
100
+ this.lastUpdatedSectorY = nextSectorY;
101
+ ++nextSectorX;
102
+ ++updatedThisCycle;
103
+ }
104
+ }
105
+
106
+ // Return % of sectors that are updated.
107
+ public getSectorUpdateRatio(sectorsUpdatedSinceGameTick: number): number {
108
+ let updated = 0,
109
+ total = 0;
110
+ for (let xx = 0; xx < this.sectorsX; ++xx) {
111
+ for (let yy = 0; yy < this.sectorsY; ++yy) {
112
+ let sector: Sector = this.sectors[xx][yy];
113
+ if (
114
+ sector &&
115
+ sector.sectorVisibilityLastCheckTick &&
116
+ sector.sectorVisibilityLastCheckTick >= sectorsUpdatedSinceGameTick
117
+ ) {
118
+ ++updated;
119
+ }
120
+ ++total;
121
+ }
122
+ }
123
+ return updated / total;
124
+ }
125
+
126
+ /**
127
+ * Return the ratio (0-1) of tiles that are visible. Returns undefined if we haven't scanned the whole map yet.
128
+ */
129
+ public getOverallVisibility(): number | undefined {
130
+ let visible = 0,
131
+ total = 0;
132
+ for (let xx = 0; xx < this.sectorsX; ++xx) {
133
+ for (let yy = 0; yy < this.sectorsY; ++yy) {
134
+ let sector: Sector = this.sectors[xx][yy];
135
+
136
+ // Undefined visibility.
137
+ if (sector.sectorVisibilityPct != undefined) {
138
+ visible += sector.sectorVisibilityPct;
139
+ total += 1.0;
140
+ }
141
+ }
142
+ }
143
+ return visible / total;
144
+ }
145
+
146
+ public getSector(sectorX: number, sectorY: number): Sector | undefined {
147
+ if (sectorX < 0 || sectorX >= this.sectorsX || sectorY < 0 || sectorY >= this.sectorsY) {
148
+ return undefined;
149
+ }
150
+ return this.sectors[sectorX][sectorY];
151
+ }
152
+
153
+ public getSectorBounds(): Size {
154
+ return { width: this.sectorsX, height: this.sectorsY };
155
+ }
156
+
157
+ public getSectorCoordinatesForWorldPosition(x: number, y: number) {
158
+ if (x < 0 || x >= this.mapBounds.width || y < 0 || y >= this.mapBounds.height) {
159
+ return undefined;
160
+ }
161
+ return {
162
+ sectorX: Math.floor(x / SECTOR_SIZE),
163
+ sectorY: Math.floor(y / SECTOR_SIZE),
164
+ };
165
+ }
166
+
167
+ public getSectorForWorldPosition(x: number, y: number): Sector | undefined {
168
+ const sectorCoordinates = this.getSectorCoordinatesForWorldPosition(x, y);
169
+ if (!sectorCoordinates) {
170
+ return undefined;
171
+ }
172
+ return this.sectors[Math.floor(x / SECTOR_SIZE)][Math.floor(y / SECTOR_SIZE)];
173
+ }
174
+ }
@@ -1,124 +1,124 @@
1
- // Used to group related actions together to minimise actionApi calls. For example, if multiple units
2
-
3
- import { ActionsApi, OrderType, Vector2 } from "@chronodivide/game-api";
4
- import { groupBy } from "../common/utils.js";
5
-
6
- // are ordered to move to the same location, all of them will be ordered to move in a single action.
7
- export class BatchableAction {
8
- private constructor(
9
- private _unitId: number,
10
- private _orderType: OrderType,
11
- private _point?: Vector2,
12
- private _targetId?: number,
13
- // If you don't want this action to be swallowed by dedupe, provide a unique nonce
14
- private _nonce: number = 0,
15
- ) {}
16
-
17
- static noTarget(unitId: number, orderType: OrderType, nonce: number = 0) {
18
- return new BatchableAction(unitId, orderType, undefined, undefined, nonce);
19
- }
20
-
21
- static toPoint(unitId: number, orderType: OrderType, point: Vector2, nonce: number = 0) {
22
- return new BatchableAction(unitId, orderType, point, undefined);
23
- }
24
-
25
- static toTargetId(unitId: number, orderType: OrderType, targetId: number, nonce: number = 0) {
26
- return new BatchableAction(unitId, orderType, undefined, targetId, nonce);
27
- }
28
-
29
- public get unitId() {
30
- return this._unitId;
31
- }
32
-
33
- public get orderType() {
34
- return this._orderType;
35
- }
36
-
37
- public get point() {
38
- return this._point;
39
- }
40
-
41
- public get targetId() {
42
- return this._targetId;
43
- }
44
-
45
- public isSameAs(other: BatchableAction) {
46
- if (this._unitId !== other._unitId) {
47
- return false;
48
- }
49
- if (this._orderType !== other._orderType) {
50
- return false;
51
- }
52
- if (this._point !== other._point) {
53
- return false;
54
- }
55
- if (this._targetId !== other._targetId) {
56
- return false;
57
- }
58
- if (this._nonce !== other._nonce) {
59
- return false;
60
- }
61
- return true;
62
- }
63
- }
64
-
65
- export class ActionBatcher {
66
- private actions: BatchableAction[];
67
-
68
- constructor() {
69
- this.actions = [];
70
- }
71
-
72
- push(action: BatchableAction) {
73
- this.actions.push(action);
74
- }
75
-
76
- resolve(actionsApi: ActionsApi) {
77
- const groupedCommands = groupBy(this.actions, (action) => action.orderType.valueOf().toString());
78
- const vectorToStr = (v: Vector2) => v.x + "," + v.y;
79
- const strToVector = (str: string) => {
80
- const [x, y] = str.split(",");
81
- return new Vector2(parseInt(x), parseInt(y));
82
- };
83
-
84
- // Group by command type.
85
- Object.entries(groupedCommands).forEach(([commandValue, commands]) => {
86
- // i hate this
87
- const commandType: OrderType = parseInt(commandValue) as OrderType;
88
- // Group by command target ID.
89
- const byTarget = groupBy(
90
- commands.filter((command) => !!command.targetId),
91
- (command) => command.targetId?.toString()!,
92
- );
93
- Object.entries(byTarget).forEach(([targetId, unitCommands]) => {
94
- actionsApi.orderUnits(
95
- unitCommands.map((command) => command.unitId),
96
- commandType,
97
- parseInt(targetId),
98
- );
99
- });
100
- // Group by position (the vector is encoded as a string of the form "x,y")
101
- const byPosition = groupBy(
102
- commands.filter((command) => !!command.point),
103
- (command) => vectorToStr(command.point!),
104
- );
105
- Object.entries(byPosition).forEach(([point, unitCommands]) => {
106
- const vector = strToVector(point);
107
- actionsApi.orderUnits(
108
- unitCommands.map((command) => command.unitId),
109
- commandType,
110
- vector.x,
111
- vector.y,
112
- );
113
- });
114
- // Actions with no targets
115
- const noTargets = commands.filter((command) => !command.targetId && !command.point);
116
- if (noTargets.length > 0) {
117
- actionsApi.orderUnits(
118
- noTargets.map((action) => action.unitId),
119
- commandType,
120
- );
121
- }
122
- });
123
- }
124
- }
1
+ // Used to group related actions together to minimise actionApi calls. For example, if multiple units
2
+
3
+ import { ActionsApi, OrderType, Vector2 } from "@chronodivide/game-api";
4
+ import { groupBy } from "../common/utils.js";
5
+
6
+ // are ordered to move to the same location, all of them will be ordered to move in a single action.
7
+ export class BatchableAction {
8
+ private constructor(
9
+ private _unitId: number,
10
+ private _orderType: OrderType,
11
+ private _point?: Vector2,
12
+ private _targetId?: number,
13
+ // If you don't want this action to be swallowed by dedupe, provide a unique nonce
14
+ private _nonce: number = 0,
15
+ ) {}
16
+
17
+ static noTarget(unitId: number, orderType: OrderType, nonce: number = 0) {
18
+ return new BatchableAction(unitId, orderType, undefined, undefined, nonce);
19
+ }
20
+
21
+ static toPoint(unitId: number, orderType: OrderType, point: Vector2, nonce: number = 0) {
22
+ return new BatchableAction(unitId, orderType, point, undefined);
23
+ }
24
+
25
+ static toTargetId(unitId: number, orderType: OrderType, targetId: number, nonce: number = 0) {
26
+ return new BatchableAction(unitId, orderType, undefined, targetId, nonce);
27
+ }
28
+
29
+ public get unitId() {
30
+ return this._unitId;
31
+ }
32
+
33
+ public get orderType() {
34
+ return this._orderType;
35
+ }
36
+
37
+ public get point() {
38
+ return this._point;
39
+ }
40
+
41
+ public get targetId() {
42
+ return this._targetId;
43
+ }
44
+
45
+ public isSameAs(other: BatchableAction) {
46
+ if (this._unitId !== other._unitId) {
47
+ return false;
48
+ }
49
+ if (this._orderType !== other._orderType) {
50
+ return false;
51
+ }
52
+ if (this._point !== other._point) {
53
+ return false;
54
+ }
55
+ if (this._targetId !== other._targetId) {
56
+ return false;
57
+ }
58
+ if (this._nonce !== other._nonce) {
59
+ return false;
60
+ }
61
+ return true;
62
+ }
63
+ }
64
+
65
+ export class ActionBatcher {
66
+ private actions: BatchableAction[];
67
+
68
+ constructor() {
69
+ this.actions = [];
70
+ }
71
+
72
+ push(action: BatchableAction) {
73
+ this.actions.push(action);
74
+ }
75
+
76
+ resolve(actionsApi: ActionsApi) {
77
+ const groupedCommands = groupBy(this.actions, (action) => action.orderType.valueOf().toString());
78
+ const vectorToStr = (v: Vector2) => v.x + "," + v.y;
79
+ const strToVector = (str: string) => {
80
+ const [x, y] = str.split(",");
81
+ return new Vector2(parseInt(x), parseInt(y));
82
+ };
83
+
84
+ // Group by command type.
85
+ Object.entries(groupedCommands).forEach(([commandValue, commands]) => {
86
+ // i hate this
87
+ const commandType: OrderType = parseInt(commandValue) as OrderType;
88
+ // Group by command target ID.
89
+ const byTarget = groupBy(
90
+ commands.filter((command) => !!command.targetId),
91
+ (command) => command.targetId?.toString()!,
92
+ );
93
+ Object.entries(byTarget).forEach(([targetId, unitCommands]) => {
94
+ actionsApi.orderUnits(
95
+ unitCommands.map((command) => command.unitId),
96
+ commandType,
97
+ parseInt(targetId),
98
+ );
99
+ });
100
+ // Group by position (the vector is encoded as a string of the form "x,y")
101
+ const byPosition = groupBy(
102
+ commands.filter((command) => !!command.point),
103
+ (command) => vectorToStr(command.point!),
104
+ );
105
+ Object.entries(byPosition).forEach(([point, unitCommands]) => {
106
+ const vector = strToVector(point);
107
+ actionsApi.orderUnits(
108
+ unitCommands.map((command) => command.unitId),
109
+ commandType,
110
+ vector.x,
111
+ vector.y,
112
+ );
113
+ });
114
+ // Actions with no targets
115
+ const noTargets = commands.filter((command) => !command.targetId && !command.point);
116
+ if (noTargets.length > 0) {
117
+ actionsApi.orderUnits(
118
+ noTargets.map((action) => action.unitId),
119
+ commandType,
120
+ );
121
+ }
122
+ });
123
+ }
124
+ }