@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,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
+ }