@supalosa/chronodivide-bot 0.1.0 → 0.2.0
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.
- package/README.md +71 -46
- package/dist/bot/bot.js +27 -183
- package/dist/bot/logic/awareness.js +122 -0
- package/dist/bot/logic/building/basicGroundUnit.js +8 -6
- package/dist/bot/logic/building/building.js +6 -3
- package/dist/bot/logic/building/harvester.js +1 -1
- package/dist/bot/logic/building/queueController.js +4 -21
- package/dist/bot/logic/common/scout.js +10 -0
- package/dist/bot/logic/knowledge.js +1 -0
- package/dist/bot/logic/map/map.js +6 -0
- package/dist/bot/logic/map/sector.js +6 -1
- package/dist/bot/logic/mission/basicMission.js +1 -5
- package/dist/bot/logic/mission/expansionMission.js +22 -4
- package/dist/bot/logic/mission/mission.js +49 -2
- package/dist/bot/logic/mission/missionController.js +67 -34
- package/dist/bot/logic/mission/missionFactories.js +10 -0
- package/dist/bot/logic/mission/missions/attackMission.js +109 -0
- package/dist/bot/logic/mission/missions/defenceMission.js +62 -0
- package/dist/bot/logic/mission/missions/expansionMission.js +24 -0
- package/dist/bot/logic/mission/missions/oneTimeMission.js +26 -0
- package/dist/bot/logic/mission/missions/retreatMission.js +7 -0
- package/dist/bot/logic/mission/missions/scoutingMission.js +38 -0
- package/dist/bot/logic/squad/behaviours/attackSquad.js +82 -0
- package/dist/bot/logic/squad/behaviours/combatSquad.js +99 -0
- package/dist/bot/logic/squad/behaviours/common.js +37 -0
- package/dist/bot/logic/squad/behaviours/defenceSquad.js +48 -0
- package/dist/bot/logic/squad/behaviours/expansionSquad.js +42 -0
- package/dist/bot/logic/squad/behaviours/retreatSquad.js +32 -0
- package/dist/bot/logic/squad/behaviours/scoutingSquad.js +38 -0
- package/dist/bot/logic/squad/behaviours/squadExpansion.js +26 -13
- package/dist/bot/logic/squad/squad.js +68 -15
- package/dist/bot/logic/squad/squadBehaviour.js +5 -5
- package/dist/bot/logic/squad/squadBehaviours.js +6 -0
- package/dist/bot/logic/squad/squadController.js +106 -15
- package/dist/exampleBot.js +22 -7
- package/package.json +29 -24
- package/src/bot/bot.ts +178 -378
- package/src/bot/logic/awareness.ts +220 -0
- package/src/bot/logic/building/ArtilleryUnit.ts +2 -2
- package/src/bot/logic/building/antiGroundStaticDefence.ts +2 -2
- package/src/bot/logic/building/basicAirUnit.ts +2 -2
- package/src/bot/logic/building/basicBuilding.ts +2 -2
- package/src/bot/logic/building/basicGroundUnit.ts +83 -78
- package/src/bot/logic/building/building.ts +125 -120
- package/src/bot/logic/building/harvester.ts +27 -27
- package/src/bot/logic/building/powerPlant.ts +1 -1
- package/src/bot/logic/building/queueController.ts +17 -38
- package/src/bot/logic/building/resourceCollectionBuilding.ts +1 -1
- package/src/bot/logic/common/scout.ts +12 -0
- package/src/bot/logic/map/map.ts +11 -3
- package/src/bot/logic/map/sector.ts +136 -130
- package/src/bot/logic/mission/mission.ts +83 -47
- package/src/bot/logic/mission/missionController.ts +103 -51
- package/src/bot/logic/mission/missionFactories.ts +46 -0
- package/src/bot/logic/mission/missions/attackMission.ts +152 -0
- package/src/bot/logic/mission/missions/defenceMission.ts +104 -0
- package/src/bot/logic/mission/missions/expansionMission.ts +49 -0
- package/src/bot/logic/mission/missions/oneTimeMission.ts +32 -0
- package/src/bot/logic/mission/missions/retreatMission.ts +9 -0
- package/src/bot/logic/mission/missions/scoutingMission.ts +59 -0
- package/src/bot/logic/squad/behaviours/combatSquad.ts +125 -0
- package/src/bot/logic/squad/behaviours/common.ts +37 -0
- package/src/bot/logic/squad/behaviours/expansionSquad.ts +59 -0
- package/src/bot/logic/squad/behaviours/retreatSquad.ts +46 -0
- package/src/bot/logic/squad/behaviours/scoutingSquad.ts +56 -0
- package/src/bot/logic/squad/squad.ts +163 -97
- package/src/bot/logic/squad/squadBehaviour.ts +61 -43
- package/src/bot/logic/squad/squadBehaviours.ts +8 -0
- package/src/bot/logic/squad/squadController.ts +190 -66
- package/src/exampleBot.ts +19 -4
- package/tsconfig.json +1 -1
- package/src/bot/logic/mission/basicMission.ts +0 -42
- package/src/bot/logic/mission/expansionMission.ts +0 -25
- package/src/bot/logic/squad/behaviours/squadExpansion.ts +0 -33
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { GameApi, ObjectType, PlayerData, Point2D, UnitData } from "@chronodivide/game-api";
|
|
2
|
+
import { SectorCache } from "./map/sector";
|
|
3
|
+
import { GlobalThreat } from "./threat/threat";
|
|
4
|
+
import { calculateGlobalThreat } from "../logic/threat/threatCalculator.js";
|
|
5
|
+
import { determineMapBounds, getDistanceBetweenPoints, getPointTowardsOtherPoint } from "../logic/map/map.js";
|
|
6
|
+
import { Circle, Quadtree, Rectangle } from "@timohausmann/quadtree-ts";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The bot's understanding of the current state of the game.
|
|
10
|
+
*/
|
|
11
|
+
export interface MatchAwareness {
|
|
12
|
+
/**
|
|
13
|
+
* Returns the threat cache for the AI.
|
|
14
|
+
*/
|
|
15
|
+
getThreatCache(): GlobalThreat | null;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Returns the sector visibility cache.
|
|
19
|
+
*/
|
|
20
|
+
getSectorCache(): SectorCache;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Returns the enemy unit IDs in a certain radius of a point
|
|
24
|
+
*/
|
|
25
|
+
getHostilesNearPoint2d(point: Point2D, radius: number): UnitPositionQuery[];
|
|
26
|
+
|
|
27
|
+
getHostilesNearPoint(x: number, y: number, radius: number): UnitPositionQuery[];
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Returns the main rally point for the AI, which updates every few ticks.
|
|
31
|
+
*/
|
|
32
|
+
getMainRallyPoint(): Point2D;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Update the internal state of the Ai.
|
|
36
|
+
* @param gameApi
|
|
37
|
+
* @param playerData
|
|
38
|
+
*/
|
|
39
|
+
onAiUpdate(gameApi: GameApi, playerData: PlayerData): void;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* True if the AI should initiate an attack.
|
|
43
|
+
*/
|
|
44
|
+
shouldAttack(): boolean;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const SECTORS_TO_UPDATE_PER_CYCLE = 8;
|
|
48
|
+
|
|
49
|
+
const RALLY_POINT_UPDATE_INTERVAL_TICKS = 60;
|
|
50
|
+
|
|
51
|
+
const THREAT_UPDATE_INTERVAL_TICKS = 30;
|
|
52
|
+
|
|
53
|
+
type QTUnit = Circle<number>;
|
|
54
|
+
export type UnitPositionQuery = { x: number; y: number; unitId: number };
|
|
55
|
+
|
|
56
|
+
const rebuildQuadtree = (quadtree: Quadtree<QTUnit>, units: UnitData[]) => {
|
|
57
|
+
quadtree.clear();
|
|
58
|
+
units.forEach((unit) => {
|
|
59
|
+
quadtree.insert(new Circle<number>({ x: unit.tile.rx, y: unit.tile.ry, r: 1, data: unit.id }));
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export class MatchAwarenessImpl implements MatchAwareness {
|
|
64
|
+
private _shouldAttack: boolean = false;
|
|
65
|
+
|
|
66
|
+
private hostileQuadTree: Quadtree<QTUnit>;
|
|
67
|
+
|
|
68
|
+
constructor(
|
|
69
|
+
private threatCache: GlobalThreat | null,
|
|
70
|
+
private sectorCache: SectorCache,
|
|
71
|
+
private mainRallyPoint: Point2D,
|
|
72
|
+
private logger: (message: string) => void,
|
|
73
|
+
) {
|
|
74
|
+
const { x: width, y: height } = sectorCache.getMapBounds();
|
|
75
|
+
this.hostileQuadTree = new Quadtree({ width, height });
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
getHostilesNearPoint2d(point: Point2D, radius: number): UnitPositionQuery[] {
|
|
79
|
+
return this.getHostilesNearPoint(point.x, point.y, radius);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
getHostilesNearPoint(searchX: number, searchY: number, radius: number): UnitPositionQuery[] {
|
|
83
|
+
const intersections = this.hostileQuadTree.retrieve(new Circle({ x: searchX, y: searchY, r: radius }));
|
|
84
|
+
return intersections
|
|
85
|
+
.map(({ x, y, data: unitId }) => ({ x, y, unitId: unitId! }))
|
|
86
|
+
.filter(({ x, y }) => getDistanceBetweenPoints({ x, y }, { x: searchX, y: searchY }) <= radius)
|
|
87
|
+
.filter(({ unitId }) => !!unitId);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
getThreatCache(): GlobalThreat | null {
|
|
91
|
+
return this.threatCache;
|
|
92
|
+
}
|
|
93
|
+
getSectorCache(): SectorCache {
|
|
94
|
+
return this.sectorCache;
|
|
95
|
+
}
|
|
96
|
+
getMainRallyPoint(): Point2D {
|
|
97
|
+
return this.mainRallyPoint;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
shouldAttack(): boolean {
|
|
101
|
+
return this._shouldAttack;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
private checkShouldAttack(threatCache: GlobalThreat, threatFactor: number) {
|
|
105
|
+
let scaledGroundPower = Math.pow(threatCache.totalAvailableAntiGroundFirepower, 1.025);
|
|
106
|
+
let scaledGroundThreat =
|
|
107
|
+
(threatFactor * threatCache.totalOffensiveLandThreat + threatCache.totalDefensiveThreat) * 1.1;
|
|
108
|
+
|
|
109
|
+
let scaledAirPower = Math.pow(threatCache.totalAvailableAirPower, 1.025);
|
|
110
|
+
let scaledAirThreat =
|
|
111
|
+
(threatFactor * threatCache.totalOffensiveAntiAirThreat + threatCache.totalDefensiveThreat) * 1.1;
|
|
112
|
+
|
|
113
|
+
return scaledGroundPower > scaledGroundThreat || scaledAirPower > scaledAirThreat;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
private isHostileUnit(unit: UnitData | undefined, hostilePlayerNames: string[]): boolean {
|
|
117
|
+
if (!unit) {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return hostilePlayerNames.includes(unit.owner);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
onAiUpdate(game: GameApi, playerData: PlayerData): void {
|
|
125
|
+
const sectorCache = this.sectorCache;
|
|
126
|
+
|
|
127
|
+
sectorCache.updateSectors(game.getCurrentTick(), SECTORS_TO_UPDATE_PER_CYCLE, game.mapApi, playerData);
|
|
128
|
+
|
|
129
|
+
let updateRatio = sectorCache?.getSectorUpdateRatio(game.getCurrentTick() - game.getTickRate() * 60);
|
|
130
|
+
if (updateRatio && updateRatio < 1.0) {
|
|
131
|
+
this.logger(`${updateRatio * 100.0}% of sectors updated in last 60 seconds.`);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const hostilePlayerNames = game
|
|
135
|
+
.getPlayers()
|
|
136
|
+
.map((name) => game.getPlayerData(name))
|
|
137
|
+
.filter(
|
|
138
|
+
(other) =>
|
|
139
|
+
other.name !== playerData.name &&
|
|
140
|
+
other.isCombatant &&
|
|
141
|
+
!game.areAlliedPlayers(playerData.name, other.name),
|
|
142
|
+
)
|
|
143
|
+
.map((other) => other.name);
|
|
144
|
+
|
|
145
|
+
// Build the quadtree, if this is too slow we should consider doing this periodically.
|
|
146
|
+
const hostileUnitIds = game.getVisibleUnits(
|
|
147
|
+
playerData.name,
|
|
148
|
+
"hostile",
|
|
149
|
+
(r) => r.isSelectableCombatant || r.type === ObjectType.Building,
|
|
150
|
+
);
|
|
151
|
+
try {
|
|
152
|
+
const hostileUnits = hostileUnitIds
|
|
153
|
+
.map((id) => game.getUnitData(id))
|
|
154
|
+
.filter((unit) => this.isHostileUnit(unit, hostilePlayerNames)) as UnitData[];
|
|
155
|
+
|
|
156
|
+
rebuildQuadtree(this.hostileQuadTree, hostileUnits);
|
|
157
|
+
} catch (err) {
|
|
158
|
+
// Hack. Will be fixed soon.
|
|
159
|
+
console.error(`caught error`, hostileUnitIds);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (game.getCurrentTick() % THREAT_UPDATE_INTERVAL_TICKS == 0) {
|
|
163
|
+
let visibility = sectorCache?.getOverallVisibility();
|
|
164
|
+
if (visibility) {
|
|
165
|
+
this.logger(`${Math.round(visibility * 1000.0) / 10}% of tiles visible. Calculating threat.`);
|
|
166
|
+
// Update the global threat cache
|
|
167
|
+
this.threatCache = calculateGlobalThreat(game, playerData, visibility);
|
|
168
|
+
this.logger(
|
|
169
|
+
`Threat LAND: Them ${Math.round(this.threatCache.totalOffensiveLandThreat)}, us: ${Math.round(
|
|
170
|
+
this.threatCache.totalAvailableAntiGroundFirepower,
|
|
171
|
+
)}.`,
|
|
172
|
+
);
|
|
173
|
+
this.logger(
|
|
174
|
+
`Threat DEFENSIVE: Them ${Math.round(this.threatCache.totalDefensiveThreat)}, us: ${Math.round(
|
|
175
|
+
this.threatCache.totalDefensivePower,
|
|
176
|
+
)}.`,
|
|
177
|
+
);
|
|
178
|
+
this.logger(
|
|
179
|
+
`Threat AIR: Them ${Math.round(this.threatCache.totalOffensiveAirThreat)}, us: ${Math.round(
|
|
180
|
+
this.threatCache.totalAvailableAntiAirFirepower,
|
|
181
|
+
)}.`,
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
// As the game approaches 2 hours, be more willing to attack. (15 ticks per second)
|
|
185
|
+
const gameLengthFactor = Math.max(0, 1.0 - game.getCurrentTick() / (15 * 7200.0));
|
|
186
|
+
this.logger(`Game length multiplier: ${gameLengthFactor}`);
|
|
187
|
+
|
|
188
|
+
if (!this._shouldAttack) {
|
|
189
|
+
// If not attacking, make it harder to switch to attack mode by multiplying the opponent's threat.
|
|
190
|
+
this._shouldAttack = this.checkShouldAttack(this.threatCache, 1.25 * gameLengthFactor);
|
|
191
|
+
if (this._shouldAttack) {
|
|
192
|
+
this.logger(`Globally switched to attack mode.`);
|
|
193
|
+
}
|
|
194
|
+
} else {
|
|
195
|
+
// If currently attacking, make it harder to switch to defence mode my dampening the opponent's threat.
|
|
196
|
+
this._shouldAttack = this.checkShouldAttack(this.threatCache, 0.75 * gameLengthFactor);
|
|
197
|
+
if (!this._shouldAttack) {
|
|
198
|
+
this.logger(`Globally switched to defence mode.`);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Update rally point every few ticks.
|
|
205
|
+
if (game.getCurrentTick() % RALLY_POINT_UPDATE_INTERVAL_TICKS === 0) {
|
|
206
|
+
const enemyPlayers = game
|
|
207
|
+
.getPlayers()
|
|
208
|
+
.filter((p) => p !== playerData.name && !game.areAlliedPlayers(playerData.name, p));
|
|
209
|
+
const enemy = game.getPlayerData(enemyPlayers[0]);
|
|
210
|
+
this.mainRallyPoint = getPointTowardsOtherPoint(
|
|
211
|
+
game,
|
|
212
|
+
playerData.startLocation,
|
|
213
|
+
enemy.startLocation,
|
|
214
|
+
10,
|
|
215
|
+
10,
|
|
216
|
+
0,
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
@@ -17,7 +17,7 @@ export class ArtilleryUnit implements AiBuildingRules {
|
|
|
17
17
|
game: GameApi,
|
|
18
18
|
playerData: PlayerData,
|
|
19
19
|
technoRules: TechnoRules,
|
|
20
|
-
threatCache: GlobalThreat |
|
|
20
|
+
threatCache: GlobalThreat | null
|
|
21
21
|
): number {
|
|
22
22
|
// If the enemy's defensive power is increasing we will start to build these.
|
|
23
23
|
if (threatCache) {
|
|
@@ -36,7 +36,7 @@ export class ArtilleryUnit implements AiBuildingRules {
|
|
|
36
36
|
game: GameApi,
|
|
37
37
|
playerData: PlayerData,
|
|
38
38
|
technoRules: TechnoRules,
|
|
39
|
-
threatCache: GlobalThreat |
|
|
39
|
+
threatCache: GlobalThreat | null
|
|
40
40
|
): number | null {
|
|
41
41
|
return null;
|
|
42
42
|
}
|
|
@@ -34,7 +34,7 @@ export class AntiGroundStaticDefence implements AiBuildingRules {
|
|
|
34
34
|
game: GameApi,
|
|
35
35
|
playerData: PlayerData,
|
|
36
36
|
technoRules: TechnoRules,
|
|
37
|
-
threatCache: GlobalThreat |
|
|
37
|
+
threatCache: GlobalThreat | null
|
|
38
38
|
): number {
|
|
39
39
|
// If the enemy's ground power is increasing we should try to keep up.
|
|
40
40
|
if (threatCache) {
|
|
@@ -53,7 +53,7 @@ export class AntiGroundStaticDefence implements AiBuildingRules {
|
|
|
53
53
|
game: GameApi,
|
|
54
54
|
playerData: PlayerData,
|
|
55
55
|
technoRules: TechnoRules,
|
|
56
|
-
threatCache: GlobalThreat |
|
|
56
|
+
threatCache: GlobalThreat | null
|
|
57
57
|
): number | null {
|
|
58
58
|
return null;
|
|
59
59
|
}
|
|
@@ -22,7 +22,7 @@ export class BasicAirUnit implements AiBuildingRules {
|
|
|
22
22
|
game: GameApi,
|
|
23
23
|
playerData: PlayerData,
|
|
24
24
|
technoRules: TechnoRules,
|
|
25
|
-
threatCache: GlobalThreat |
|
|
25
|
+
threatCache: GlobalThreat | null
|
|
26
26
|
): number {
|
|
27
27
|
// If the enemy's anti-air power is low we might build more.
|
|
28
28
|
if (threatCache) {
|
|
@@ -61,7 +61,7 @@ export class BasicAirUnit implements AiBuildingRules {
|
|
|
61
61
|
game: GameApi,
|
|
62
62
|
playerData: PlayerData,
|
|
63
63
|
technoRules: TechnoRules,
|
|
64
|
-
threatCache: GlobalThreat |
|
|
64
|
+
threatCache: GlobalThreat | null
|
|
65
65
|
): number | null {
|
|
66
66
|
return null;
|
|
67
67
|
}
|
|
@@ -21,7 +21,7 @@ export class BasicBuilding implements AiBuildingRules {
|
|
|
21
21
|
game: GameApi,
|
|
22
22
|
playerData: PlayerData,
|
|
23
23
|
technoRules: TechnoRules,
|
|
24
|
-
threatCache: GlobalThreat |
|
|
24
|
+
threatCache: GlobalThreat | null
|
|
25
25
|
): number {
|
|
26
26
|
const numOwned = numBuildingsOwnedOfType(game, playerData, technoRules);
|
|
27
27
|
const calcMaxCount = this.getMaxCount(game, playerData, technoRules, threatCache);
|
|
@@ -40,7 +40,7 @@ export class BasicBuilding implements AiBuildingRules {
|
|
|
40
40
|
game: GameApi,
|
|
41
41
|
playerData: PlayerData,
|
|
42
42
|
technoRules: TechnoRules,
|
|
43
|
-
threatCache: GlobalThreat |
|
|
43
|
+
threatCache: GlobalThreat | null
|
|
44
44
|
): number | null {
|
|
45
45
|
return this.maxNeeded;
|
|
46
46
|
}
|
|
@@ -1,78 +1,83 @@
|
|
|
1
|
-
import { GameApi, PlayerData, TechnoRules } from "@chronodivide/game-api";
|
|
2
|
-
import { GlobalThreat } from "../threat/threat.js";
|
|
3
|
-
import { AiBuildingRules, getDefaultPlacementLocation, numBuildingsOwnedOfType } from "./building.js";
|
|
4
|
-
|
|
5
|
-
export class BasicGroundUnit implements AiBuildingRules {
|
|
6
|
-
constructor(
|
|
7
|
-
protected basePriority: number,
|
|
8
|
-
protected baseAmount: number,
|
|
9
|
-
protected antiGroundPower: number = 1, // boolean for now, but will eventually be used in weighting.
|
|
10
|
-
protected antiAirPower: number = 0
|
|
11
|
-
) {}
|
|
12
|
-
|
|
13
|
-
getPlacementLocation(
|
|
14
|
-
game: GameApi,
|
|
15
|
-
playerData: PlayerData,
|
|
16
|
-
technoRules: TechnoRules
|
|
17
|
-
): { rx: number; ry: number } | undefined {
|
|
18
|
-
return undefined;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
getPriority(
|
|
22
|
-
game: GameApi,
|
|
23
|
-
playerData: PlayerData,
|
|
24
|
-
technoRules: TechnoRules,
|
|
25
|
-
threatCache: GlobalThreat |
|
|
26
|
-
): number {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
this.
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
this.
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
priority +=
|
|
56
|
-
this.antiAirPower *
|
|
57
|
-
this.basePriority *
|
|
58
|
-
Math.
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
1
|
+
import { GameApi, PlayerData, TechnoRules } from "@chronodivide/game-api";
|
|
2
|
+
import { GlobalThreat } from "../threat/threat.js";
|
|
3
|
+
import { AiBuildingRules, getDefaultPlacementLocation, numBuildingsOwnedOfType } from "./building.js";
|
|
4
|
+
|
|
5
|
+
export class BasicGroundUnit implements AiBuildingRules {
|
|
6
|
+
constructor(
|
|
7
|
+
protected basePriority: number,
|
|
8
|
+
protected baseAmount: number,
|
|
9
|
+
protected antiGroundPower: number = 1, // boolean for now, but will eventually be used in weighting.
|
|
10
|
+
protected antiAirPower: number = 0,
|
|
11
|
+
) {}
|
|
12
|
+
|
|
13
|
+
getPlacementLocation(
|
|
14
|
+
game: GameApi,
|
|
15
|
+
playerData: PlayerData,
|
|
16
|
+
technoRules: TechnoRules,
|
|
17
|
+
): { rx: number; ry: number } | undefined {
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
getPriority(
|
|
22
|
+
game: GameApi,
|
|
23
|
+
playerData: PlayerData,
|
|
24
|
+
technoRules: TechnoRules,
|
|
25
|
+
threatCache: GlobalThreat | null,
|
|
26
|
+
): number {
|
|
27
|
+
const numOwned = numBuildingsOwnedOfType(game, playerData, technoRules);
|
|
28
|
+
if (threatCache) {
|
|
29
|
+
let priority = this.basePriority;
|
|
30
|
+
if (this.antiGroundPower > 0) {
|
|
31
|
+
// If the enemy's power is increasing we should try to keep up.
|
|
32
|
+
if (threatCache.totalOffensiveLandThreat > threatCache.totalAvailableAntiGroundFirepower) {
|
|
33
|
+
priority +=
|
|
34
|
+
this.antiGroundPower *
|
|
35
|
+
this.basePriority *
|
|
36
|
+
(threatCache.totalOffensiveLandThreat /
|
|
37
|
+
Math.max(1, threatCache.totalAvailableAntiGroundFirepower));
|
|
38
|
+
} else {
|
|
39
|
+
// But also, if our power dwarfs the enemy, keep pressing the advantage.
|
|
40
|
+
priority +=
|
|
41
|
+
(this.antiGroundPower *
|
|
42
|
+
this.basePriority *
|
|
43
|
+
Math.sqrt(
|
|
44
|
+
threatCache.totalAvailableAntiGroundFirepower /
|
|
45
|
+
Math.max(
|
|
46
|
+
1,
|
|
47
|
+
threatCache.totalOffensiveLandThreat + threatCache.totalDefensiveThreat,
|
|
48
|
+
),
|
|
49
|
+
)) /
|
|
50
|
+
(numOwned + 1);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (this.antiAirPower > 0) {
|
|
54
|
+
if (threatCache.totalOffensiveAirThreat > threatCache.totalAvailableAntiAirFirepower) {
|
|
55
|
+
priority +=
|
|
56
|
+
this.antiAirPower *
|
|
57
|
+
this.basePriority *
|
|
58
|
+
(threatCache.totalOffensiveAirThreat / Math.max(1, threatCache.totalAvailableAntiAirFirepower));
|
|
59
|
+
} else {
|
|
60
|
+
priority +=
|
|
61
|
+
(this.antiAirPower *
|
|
62
|
+
this.basePriority *
|
|
63
|
+
Math.sqrt(
|
|
64
|
+
threatCache.totalAvailableAntiAirFirepower /
|
|
65
|
+
Math.max(1, threatCache.totalOffensiveAirThreat + threatCache.totalDefensiveThreat),
|
|
66
|
+
)) /
|
|
67
|
+
(numOwned + 1);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return priority;
|
|
71
|
+
}
|
|
72
|
+
return this.basePriority * (1.0 - numOwned / this.baseAmount);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
getMaxCount(
|
|
76
|
+
game: GameApi,
|
|
77
|
+
playerData: PlayerData,
|
|
78
|
+
technoRules: TechnoRules,
|
|
79
|
+
threatCache: GlobalThreat | null,
|
|
80
|
+
): number | null {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
}
|