@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.
- package/.env.template +4 -4
- package/.github/workflows/npm-publish.yml +24 -0
- package/README.md +108 -97
- package/dist/bot/bot.js +105 -105
- package/dist/bot/bot.js.map +1 -1
- package/dist/bot/logic/awareness.js +136 -136
- package/dist/bot/logic/building/antiAirStaticDefence.js +42 -42
- package/dist/bot/logic/building/antiGroundStaticDefence.js +34 -30
- package/dist/bot/logic/building/antiGroundStaticDefence.js.map +1 -1
- package/dist/bot/logic/building/{ArtilleryUnit.js → artilleryUnit.js} +18 -18
- package/dist/bot/logic/building/basicAirUnit.js +19 -19
- package/dist/bot/logic/building/basicBuilding.js +26 -26
- package/dist/bot/logic/building/basicGroundUnit.js +19 -19
- package/dist/bot/logic/building/buildingRules.js +175 -174
- package/dist/bot/logic/building/buildingRules.js.map +1 -1
- package/dist/bot/logic/building/common.js +19 -18
- package/dist/bot/logic/building/common.js.map +1 -1
- package/dist/bot/logic/building/harvester.js +16 -16
- package/dist/bot/logic/building/powerPlant.js +20 -20
- package/dist/bot/logic/building/queueController.js +183 -183
- package/dist/bot/logic/building/resourceCollectionBuilding.js +36 -36
- package/dist/bot/logic/common/scout.js +126 -126
- package/dist/bot/logic/common/utils.js +95 -85
- package/dist/bot/logic/common/utils.js.map +1 -1
- package/dist/bot/logic/composition/alliedCompositions.js +12 -12
- package/dist/bot/logic/composition/common.js +1 -1
- package/dist/bot/logic/composition/sovietCompositions.js +12 -12
- package/dist/bot/logic/map/map.js +44 -44
- package/dist/bot/logic/map/sector.js +137 -137
- package/dist/bot/logic/mission/actionBatcher.js +91 -91
- package/dist/bot/logic/mission/mission.js +122 -122
- package/dist/bot/logic/mission/missionController.js +321 -321
- package/dist/bot/logic/mission/missionFactories.js +12 -12
- package/dist/bot/logic/mission/missions/attackMission.js +214 -214
- package/dist/bot/logic/mission/missions/defenceMission.js +82 -82
- package/dist/bot/logic/mission/missions/engineerMission.js +63 -63
- package/dist/bot/logic/mission/missions/expansionMission.js +60 -60
- package/dist/bot/logic/mission/missions/retreatMission.js +33 -33
- package/dist/bot/logic/mission/missions/scoutingMission.js +133 -133
- package/dist/bot/logic/mission/missions/squads/combatSquad.js +115 -115
- package/dist/bot/logic/mission/missions/squads/common.js +57 -57
- package/dist/bot/logic/mission/missions/squads/squad.js +1 -1
- package/dist/bot/logic/threat/threat.js +22 -22
- package/dist/bot/logic/threat/threatCalculator.js +73 -73
- package/dist/exampleBot.js +100 -112
- package/dist/exampleBot.js.map +1 -1
- package/package.json +32 -29
- package/src/bot/bot.ts +161 -161
- package/src/bot/logic/awareness.ts +245 -245
- package/src/bot/logic/building/antiAirStaticDefence.ts +64 -64
- package/src/bot/logic/building/antiGroundStaticDefence.ts +55 -51
- package/src/bot/logic/building/artilleryUnit.ts +39 -39
- package/src/bot/logic/building/basicAirUnit.ts +39 -39
- package/src/bot/logic/building/basicBuilding.ts +49 -49
- package/src/bot/logic/building/basicGroundUnit.ts +39 -39
- package/src/bot/logic/building/buildingRules.ts +250 -247
- package/src/bot/logic/building/common.ts +21 -23
- package/src/bot/logic/building/harvester.ts +31 -31
- package/src/bot/logic/building/powerPlant.ts +32 -32
- package/src/bot/logic/building/queueController.ts +297 -297
- package/src/bot/logic/building/resourceCollectionBuilding.ts +52 -52
- package/src/bot/logic/common/scout.ts +183 -183
- package/src/bot/logic/common/utils.ts +120 -112
- package/src/bot/logic/composition/alliedCompositions.ts +22 -22
- package/src/bot/logic/composition/common.ts +3 -3
- package/src/bot/logic/composition/sovietCompositions.ts +21 -21
- package/src/bot/logic/map/map.ts +66 -66
- package/src/bot/logic/map/sector.ts +174 -174
- package/src/bot/logic/mission/actionBatcher.ts +124 -124
- package/src/bot/logic/mission/mission.ts +232 -232
- package/src/bot/logic/mission/missionController.ts +413 -413
- package/src/bot/logic/mission/missionFactories.ts +51 -51
- package/src/bot/logic/mission/missions/attackMission.ts +336 -336
- package/src/bot/logic/mission/missions/defenceMission.ts +151 -151
- package/src/bot/logic/mission/missions/engineerMission.ts +113 -113
- package/src/bot/logic/mission/missions/expansionMission.ts +104 -104
- package/src/bot/logic/mission/missions/retreatMission.ts +54 -54
- package/src/bot/logic/mission/missions/scoutingMission.ts +186 -186
- package/src/bot/logic/mission/missions/squads/combatSquad.ts +160 -160
- package/src/bot/logic/mission/missions/squads/common.ts +63 -63
- package/src/bot/logic/mission/missions/squads/squad.ts +19 -19
- package/src/bot/logic/threat/threatCalculator.ts +100 -100
- package/src/exampleBot.ts +111 -124
- package/tsconfig.json +73 -73
- package/dist/bot/logic/building/building.js +0 -82
- package/dist/bot/logic/building/massedAntiGroundUnit.js +0 -20
- package/dist/bot/logic/building/queues.js +0 -19
- package/dist/bot/logic/knowledge.js +0 -1
- package/dist/bot/logic/mission/basicMission.js +0 -26
- package/dist/bot/logic/mission/behaviours/combatSquad.js +0 -124
- package/dist/bot/logic/mission/behaviours/combatSquad.js.map +0 -1
- package/dist/bot/logic/mission/behaviours/common.js +0 -56
- package/dist/bot/logic/mission/behaviours/common.js.map +0 -1
- package/dist/bot/logic/mission/behaviours/engineerSquad.js +0 -39
- package/dist/bot/logic/mission/behaviours/engineerSquad.js.map +0 -1
- package/dist/bot/logic/mission/behaviours/expansionSquad.js +0 -46
- package/dist/bot/logic/mission/behaviours/expansionSquad.js.map +0 -1
- package/dist/bot/logic/mission/behaviours/retreatSquad.js +0 -31
- package/dist/bot/logic/mission/behaviours/retreatSquad.js.map +0 -1
- package/dist/bot/logic/mission/behaviours/scoutingSquad.js +0 -94
- package/dist/bot/logic/mission/behaviours/scoutingSquad.js.map +0 -1
- package/dist/bot/logic/mission/expansionMission.js +0 -32
- package/dist/bot/logic/mission/missions/basicMission.js +0 -13
- package/dist/bot/logic/mission/missions/basicMission.js.map +0 -1
- package/dist/bot/logic/mission/missions/missionBehaviour.js +0 -2
- package/dist/bot/logic/mission/missions/missionBehaviour.js.map +0 -1
- package/dist/bot/logic/mission/missions/oneTimeMission.js +0 -27
- package/dist/bot/logic/mission/missions/oneTimeMission.js.map +0 -1
- package/dist/bot/logic/squad/behaviours/actionBatcher.js +0 -36
- package/dist/bot/logic/squad/behaviours/actionBatcher.js.map +0 -1
- package/dist/bot/logic/squad/behaviours/attackSquad.js +0 -82
- package/dist/bot/logic/squad/behaviours/combatSquad.js +0 -106
- package/dist/bot/logic/squad/behaviours/combatSquad.js.map +0 -1
- package/dist/bot/logic/squad/behaviours/common.js +0 -55
- package/dist/bot/logic/squad/behaviours/common.js.map +0 -1
- package/dist/bot/logic/squad/behaviours/defenceSquad.js +0 -48
- package/dist/bot/logic/squad/behaviours/engineerSquad.js +0 -38
- package/dist/bot/logic/squad/behaviours/engineerSquad.js.map +0 -1
- package/dist/bot/logic/squad/behaviours/expansionSquad.js +0 -45
- package/dist/bot/logic/squad/behaviours/expansionSquad.js.map +0 -1
- package/dist/bot/logic/squad/behaviours/retreatSquad.js +0 -31
- package/dist/bot/logic/squad/behaviours/retreatSquad.js.map +0 -1
- package/dist/bot/logic/squad/behaviours/scoutingSquad.js +0 -93
- package/dist/bot/logic/squad/behaviours/scoutingSquad.js.map +0 -1
- package/dist/bot/logic/squad/behaviours/squadExpansion.js +0 -31
- package/dist/bot/logic/squad/behaviours/squadScouters.js +0 -8
- package/dist/bot/logic/squad/squad.js +0 -126
- package/dist/bot/logic/squad/squad.js.map +0 -1
- package/dist/bot/logic/squad/squadBehaviour.js +0 -6
- package/dist/bot/logic/squad/squadBehaviour.js.map +0 -1
- package/dist/bot/logic/squad/squadBehaviours.js +0 -7
- package/dist/bot/logic/squad/squadBehaviours.js.map +0 -1
- package/dist/bot/logic/squad/squadController.js +0 -215
- package/dist/bot/logic/squad/squadController.js.map +0 -1
package/.env.template
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
SERVER_URL="wss://gserv-sea1.chronodivide.com"
|
|
2
|
-
CLIENT_URL="https://game.chronodivide.com/"
|
|
3
|
-
ONLINE_BOT_NAME="username_of_your_bot"
|
|
4
|
-
ONLINE_BOT_PASSWORD="password_of_your_bot"
|
|
1
|
+
SERVER_URL="wss://gserv-sea1.chronodivide.com"
|
|
2
|
+
CLIENT_URL="https://game.chronodivide.com/"
|
|
3
|
+
ONLINE_BOT_NAME="username_of_your_bot"
|
|
4
|
+
ONLINE_BOT_PASSWORD="password_of_your_bot"
|
|
5
5
|
PLAYER_NAME="username_of_human_account"
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
name: Publish Package
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags:
|
|
6
|
+
- 'v*'
|
|
7
|
+
|
|
8
|
+
permissions:
|
|
9
|
+
id-token: write # Required for OIDC
|
|
10
|
+
contents: read
|
|
11
|
+
|
|
12
|
+
jobs:
|
|
13
|
+
publish:
|
|
14
|
+
runs-on: ubuntu-latest
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v4
|
|
17
|
+
|
|
18
|
+
- uses: actions/setup-node@v4
|
|
19
|
+
with:
|
|
20
|
+
node-version: 24
|
|
21
|
+
registry-url: 'https://registry.npmjs.org'
|
|
22
|
+
- run: npm ci
|
|
23
|
+
- run: npm run build --if-present
|
|
24
|
+
- run: npm publish
|
package/README.md
CHANGED
|
@@ -1,97 +1,108 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
[
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
```
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
1
|
+
[](https://github.com/Supalosa/supalosa-chronodivide-bot/actions/workflows/npm-publish.yml)
|
|
2
|
+
|
|
3
|
+
# Supalosa's Chrono Divide Bot
|
|
4
|
+
|
|
5
|
+
[中文版文档](README-CN.md)
|
|
6
|
+
|
|
7
|
+
[Chrono Divide](https://chronodivide.com/) is a ground-up rebuild of Red Alert 2 in the browser. It is feature-complete and allows for online skirmish play against other players.
|
|
8
|
+
It also provides [an API to build bots](https://discord.com/channels/771701199812558848/842700851520339988), as there is no built-in AI yet.
|
|
9
|
+
|
|
10
|
+
This repository is one such implementation of a bot. The original template for the bot is available at [game-api-playground](https://github.com/chronodivide/game-api-playground/blob/master/README.md).
|
|
11
|
+
|
|
12
|
+
## Development State and Future plans
|
|
13
|
+
|
|
14
|
+
The developer of Chrono Divide has expressed interest in integrating this bot into the game directly. As a consequence, I am aiming to implement missing features to create a satisfactory AI opponent for humans.
|
|
15
|
+
Directionally, this means I am not looking to make this AI a perfect opponent with perfect compositions or micro, and instead hope that it can be a fun challenge for newer players.
|
|
16
|
+
|
|
17
|
+
See `TODO.md` for a granular list of structural changes and feature improvements that are planned for the bot.
|
|
18
|
+
|
|
19
|
+
Feel free to contribute to the repository, or even fork the repo and build your own version.
|
|
20
|
+
|
|
21
|
+
## Install instructions
|
|
22
|
+
|
|
23
|
+
Node 20 is required by the Chrono Divide API. However, publishing is done on Node 24 or above.
|
|
24
|
+
|
|
25
|
+
```sh
|
|
26
|
+
npm install
|
|
27
|
+
npm run build
|
|
28
|
+
npx cross-env MIX_DIR="C:\path_to_ra2_install_dir" npm start
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
This will create a replay (`.rpl`) file that can be [imported into the live game](https://game.chronodivide.com/).
|
|
32
|
+
|
|
33
|
+
You can modify `exampleBot.ts` to configure the match. You will most likely want to look at the line with `const mapName = "..."` to change the map, or the `const offlineSettings1v1` to change the bot countries.
|
|
34
|
+
|
|
35
|
+
## Playing against the bot
|
|
36
|
+
|
|
37
|
+
Currently, playing against this bot **is only possible for developers**, because it requires you to run this repository from source. Follow these steps to set up online play.
|
|
38
|
+
|
|
39
|
+
### Initial set up steps (one time only)
|
|
40
|
+
|
|
41
|
+
1. Create a Chronodivide account for your bot using the official client at [https://game.chronodivide.com].
|
|
42
|
+
2. If you don't already have one, create a Chronodivide account for yourself using the same link,
|
|
43
|
+
3. Copy `.env.template` to `.env`. The `.env` file is not checked into the repo.
|
|
44
|
+
4. Set the value of `ONLINE_BOT_NAME` to the username of the bot from step 1.
|
|
45
|
+
5. Set the value of `ONLINE_BOT_PASSWORD` to the password from step 1.
|
|
46
|
+
6. Set the value of `PLAYER_NAME` to the human's account name.
|
|
47
|
+
7. (Optional) Change `SERVER_URL` if you want to connect to another server. The Chronodivide accounts from step 1 and 2 need to be present on that server.
|
|
48
|
+
|
|
49
|
+
### Running the bot and connecting to the game
|
|
50
|
+
|
|
51
|
+
Start the bot with `ONLINE_MATCH=1`. For example:
|
|
52
|
+
|
|
53
|
+
```sh
|
|
54
|
+
ONLINE_MATCH=1 npx cross-env MIX_DIR="${GAMEPATH}" npm --node-options="${NODE_OPTIONS} --inspect" start
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
The bot will connect to the server and should return output like this:
|
|
58
|
+
|
|
59
|
+
```
|
|
60
|
+
You may use the following link(s) to join, after the game is created:
|
|
61
|
+
|
|
62
|
+
https://game.chronodivide.com/#/game/12345/supalosa
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
Press ENTER to create the game now...
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Navigate to the link, **log in using the human credentials first**, then hit ENTER in the terminal so the bot can create the game.
|
|
69
|
+
Do not hit ENTER too early, as there is a very narrow window for the human connect to the match.
|
|
70
|
+
|
|
71
|
+
## Debugging
|
|
72
|
+
|
|
73
|
+
To generate a replay with debugging enabled:
|
|
74
|
+
|
|
75
|
+
```sh
|
|
76
|
+
npx cross-env MIX_DIR="C:\path_to_ra2_install_dir" npm --node-options="${NODE_OPTIONS} --inspect" start
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
To log all actions generated by the bots:
|
|
80
|
+
|
|
81
|
+
```sh
|
|
82
|
+
DEBUG_LOGGING="action" npx cross-env MIX_DIR="${GAMEPATH}" npm --node-options="${NODE_OPTIONS} --inspect" start
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
We also take advantage of the in-game bot debug functionality provided by CD. These are basically bot-only actions that are saved in the replay, but you must enable the visualisations in the CD client before watching the replay, by typing the following into the dev console:.
|
|
86
|
+
|
|
87
|
+
```
|
|
88
|
+
r.debug_text = true;
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
This will debug the bot which has been configured with `setDebugMode(true)`, this is done in `exampleBot.ts`.
|
|
92
|
+
|
|
93
|
+
## Publishing
|
|
94
|
+
|
|
95
|
+
A Github Action has been set up on the [main repo](https://github.com/Supalosa/supalosa-chronodivide-bot). We use npmjs's [trusted publisher](https://docs.npmjs.com/trusted-publishers#supported-cicd-providers) setup to do this.
|
|
96
|
+
|
|
97
|
+
To publish a new version of the bot:
|
|
98
|
+
|
|
99
|
+
```
|
|
100
|
+
npm version v0.6.0 # major.minor.patch
|
|
101
|
+
git push origin tag v0.6.0
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Contributors
|
|
105
|
+
|
|
106
|
+
- use-strict: Making Chrono Divide
|
|
107
|
+
- Libi: Improvements to base structure placement performance
|
|
108
|
+
- Dogemoon: CN Documentation
|
package/dist/bot/bot.js
CHANGED
|
@@ -1,106 +1,106 @@
|
|
|
1
|
-
import { ApiEventType, Bot, ObjectType, FactoryType } from "@chronodivide/game-api";
|
|
2
|
-
import { determineMapBounds } from "./logic/map/map.js";
|
|
3
|
-
import { SectorCache } from "./logic/map/sector.js";
|
|
4
|
-
import { MissionController } from "./logic/mission/missionController.js";
|
|
5
|
-
import { QueueController } from "./logic/building/queueController.js";
|
|
6
|
-
import { MatchAwarenessImpl } from "./logic/awareness.js";
|
|
7
|
-
import { formatTimeDuration } from "./logic/common/utils.js";
|
|
8
|
-
const DEBUG_STATE_UPDATE_INTERVAL_SECONDS = 6;
|
|
9
|
-
// Number of ticks per second at the base speed.
|
|
10
|
-
const NATURAL_TICK_RATE = 15;
|
|
11
|
-
export class SupalosaBot extends Bot {
|
|
12
|
-
constructor(name, country, tryAllyWith = [], enableLogging = true) {
|
|
13
|
-
super(name, country);
|
|
14
|
-
this.tryAllyWith = tryAllyWith;
|
|
15
|
-
this.enableLogging = enableLogging;
|
|
16
|
-
this.tickOfLastAttackOrder = 0;
|
|
17
|
-
this.matchAwareness = null;
|
|
18
|
-
this.missionController = new MissionController((message, sayInGame) => this.logBotStatus(message, sayInGame));
|
|
19
|
-
this.queueController = new QueueController();
|
|
20
|
-
}
|
|
21
|
-
onGameStart(game) {
|
|
22
|
-
const gameRate = game.getTickRate();
|
|
23
|
-
const botApm = 300;
|
|
24
|
-
const botRate = botApm / 60;
|
|
25
|
-
this.tickRatio = Math.ceil(gameRate / botRate);
|
|
26
|
-
this.knownMapBounds = determineMapBounds(game.mapApi);
|
|
27
|
-
const myPlayer = game.getPlayerData(this.name);
|
|
28
|
-
this.matchAwareness = new MatchAwarenessImpl(null, new SectorCache(game.mapApi, this.knownMapBounds), myPlayer.startLocation, (message, sayInGame) => this.logBotStatus(message, sayInGame));
|
|
29
|
-
this.matchAwareness.onGameStart(game, myPlayer);
|
|
30
|
-
this.logBotStatus(`Map bounds: ${this.knownMapBounds.width}, ${this.knownMapBounds.height}`);
|
|
31
|
-
this.tryAllyWith.forEach((playerName) => this.actionsApi.toggleAlliance(playerName, true));
|
|
32
|
-
}
|
|
33
|
-
onGameTick(game) {
|
|
34
|
-
if (!this.matchAwareness) {
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
const threatCache = this.matchAwareness.getThreatCache();
|
|
38
|
-
if ((game.getCurrentTick() / NATURAL_TICK_RATE) % DEBUG_STATE_UPDATE_INTERVAL_SECONDS === 0) {
|
|
39
|
-
this.updateDebugState(game);
|
|
40
|
-
}
|
|
41
|
-
if (game.getCurrentTick() % this.tickRatio === 0) {
|
|
42
|
-
const myPlayer = game.getPlayerData(this.name);
|
|
43
|
-
this.matchAwareness.onAiUpdate(game, myPlayer);
|
|
44
|
-
// hacky resign condition
|
|
45
|
-
const armyUnits = game.getVisibleUnits(this.name, "self", (r) => r.isSelectableCombatant);
|
|
46
|
-
const mcvUnits = game.getVisibleUnits(this.name, "self", (r) => !!r.deploysInto && game.getGeneralRules().baseUnit.includes(r.name));
|
|
47
|
-
const productionBuildings = game.getVisibleUnits(this.name, "self", (r) => r.type == ObjectType.Building && r.factory != FactoryType.None);
|
|
48
|
-
if (armyUnits.length == 0 && productionBuildings.length == 0 && mcvUnits.length == 0) {
|
|
49
|
-
this.logBotStatus(`No army or production left, quitting.`);
|
|
50
|
-
this.actionsApi.quitGame();
|
|
51
|
-
}
|
|
52
|
-
// Mission logic every 3 ticks
|
|
53
|
-
if (this.gameApi.getCurrentTick() % 3 === 0) {
|
|
54
|
-
this.missionController.onAiUpdate(game, this.actionsApi, myPlayer, this.matchAwareness);
|
|
55
|
-
}
|
|
56
|
-
const unitTypeRequests = this.missionController.getRequestedUnitTypes();
|
|
57
|
-
// Build logic.
|
|
58
|
-
this.queueController.onAiUpdate(game, this.productionApi, this.actionsApi, myPlayer, threatCache, unitTypeRequests, (message) => this.logBotStatus(message));
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
getHumanTimestamp(game) {
|
|
62
|
-
return formatTimeDuration(game.getCurrentTick() / NATURAL_TICK_RATE);
|
|
63
|
-
}
|
|
64
|
-
logBotStatus(message, sayInGame = false) {
|
|
65
|
-
if (!this.enableLogging) {
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
this.logger.info(message);
|
|
69
|
-
if (sayInGame) {
|
|
70
|
-
const timestamp = this.getHumanTimestamp(this.gameApi);
|
|
71
|
-
this.actionsApi.sayAll(`${timestamp}: ${message}`);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
updateDebugState(game) {
|
|
75
|
-
if (!this.getDebugMode()) {
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
// Update the global debug text.
|
|
79
|
-
const myPlayer = game.getPlayerData(this.name);
|
|
80
|
-
const harvesters = game.getVisibleUnits(this.name, "self", (r) => r.harvester).length;
|
|
81
|
-
let globalDebugText = `Cash: ${myPlayer.credits} | Harvesters: ${harvesters}\n`;
|
|
82
|
-
globalDebugText += this.queueController.getGlobalDebugText(this.gameApi, this.productionApi);
|
|
83
|
-
globalDebugText += this.missionController.getGlobalDebugText(this.gameApi);
|
|
84
|
-
globalDebugText += this.matchAwareness?.getGlobalDebugText();
|
|
85
|
-
this.missionController.updateDebugText(this.actionsApi);
|
|
86
|
-
// Tag enemy units with IDs
|
|
87
|
-
game.getVisibleUnits(this.name, "enemy").forEach((unitId) => {
|
|
88
|
-
this.actionsApi.setUnitDebugText(unitId, unitId.toString());
|
|
89
|
-
});
|
|
90
|
-
this.actionsApi.setGlobalDebugText(globalDebugText);
|
|
91
|
-
}
|
|
92
|
-
onGameEvent(ev) {
|
|
93
|
-
switch (ev.type) {
|
|
94
|
-
case ApiEventType.ObjectDestroy: {
|
|
95
|
-
// Add to the stalemate detection.
|
|
96
|
-
if (ev.attackerInfo?.playerName == this.name) {
|
|
97
|
-
this.tickOfLastAttackOrder += (this.gameApi.getCurrentTick() - this.tickOfLastAttackOrder) / 2;
|
|
98
|
-
}
|
|
99
|
-
break;
|
|
100
|
-
}
|
|
101
|
-
default:
|
|
102
|
-
break;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}
|
|
1
|
+
import { ApiEventType, Bot, ObjectType, FactoryType } from "@chronodivide/game-api";
|
|
2
|
+
import { determineMapBounds } from "./logic/map/map.js";
|
|
3
|
+
import { SectorCache } from "./logic/map/sector.js";
|
|
4
|
+
import { MissionController } from "./logic/mission/missionController.js";
|
|
5
|
+
import { QueueController } from "./logic/building/queueController.js";
|
|
6
|
+
import { MatchAwarenessImpl } from "./logic/awareness.js";
|
|
7
|
+
import { formatTimeDuration } from "./logic/common/utils.js";
|
|
8
|
+
const DEBUG_STATE_UPDATE_INTERVAL_SECONDS = 6;
|
|
9
|
+
// Number of ticks per second at the base speed.
|
|
10
|
+
const NATURAL_TICK_RATE = 15;
|
|
11
|
+
export class SupalosaBot extends Bot {
|
|
12
|
+
constructor(name, country, tryAllyWith = [], enableLogging = true) {
|
|
13
|
+
super(name, country);
|
|
14
|
+
this.tryAllyWith = tryAllyWith;
|
|
15
|
+
this.enableLogging = enableLogging;
|
|
16
|
+
this.tickOfLastAttackOrder = 0;
|
|
17
|
+
this.matchAwareness = null;
|
|
18
|
+
this.missionController = new MissionController((message, sayInGame) => this.logBotStatus(message, sayInGame));
|
|
19
|
+
this.queueController = new QueueController();
|
|
20
|
+
}
|
|
21
|
+
onGameStart(game) {
|
|
22
|
+
const gameRate = game.getTickRate();
|
|
23
|
+
const botApm = 300;
|
|
24
|
+
const botRate = botApm / 60;
|
|
25
|
+
this.tickRatio = Math.ceil(gameRate / botRate);
|
|
26
|
+
this.knownMapBounds = determineMapBounds(game.mapApi);
|
|
27
|
+
const myPlayer = game.getPlayerData(this.name);
|
|
28
|
+
this.matchAwareness = new MatchAwarenessImpl(null, new SectorCache(game.mapApi, this.knownMapBounds), myPlayer.startLocation, (message, sayInGame) => this.logBotStatus(message, sayInGame));
|
|
29
|
+
this.matchAwareness.onGameStart(game, myPlayer);
|
|
30
|
+
this.logBotStatus(`Map bounds: ${this.knownMapBounds.width}, ${this.knownMapBounds.height}`);
|
|
31
|
+
this.tryAllyWith.forEach((playerName) => this.actionsApi.toggleAlliance(playerName, true));
|
|
32
|
+
}
|
|
33
|
+
onGameTick(game) {
|
|
34
|
+
if (!this.matchAwareness) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const threatCache = this.matchAwareness.getThreatCache();
|
|
38
|
+
if ((game.getCurrentTick() / NATURAL_TICK_RATE) % DEBUG_STATE_UPDATE_INTERVAL_SECONDS === 0) {
|
|
39
|
+
this.updateDebugState(game);
|
|
40
|
+
}
|
|
41
|
+
if (game.getCurrentTick() % this.tickRatio === 0) {
|
|
42
|
+
const myPlayer = game.getPlayerData(this.name);
|
|
43
|
+
this.matchAwareness.onAiUpdate(game, myPlayer);
|
|
44
|
+
// hacky resign condition
|
|
45
|
+
const armyUnits = game.getVisibleUnits(this.name, "self", (r) => r.isSelectableCombatant);
|
|
46
|
+
const mcvUnits = game.getVisibleUnits(this.name, "self", (r) => !!r.deploysInto && game.getGeneralRules().baseUnit.includes(r.name));
|
|
47
|
+
const productionBuildings = game.getVisibleUnits(this.name, "self", (r) => r.type == ObjectType.Building && r.factory != FactoryType.None);
|
|
48
|
+
if (armyUnits.length == 0 && productionBuildings.length == 0 && mcvUnits.length == 0) {
|
|
49
|
+
this.logBotStatus(`No army or production left, quitting.`);
|
|
50
|
+
this.actionsApi.quitGame();
|
|
51
|
+
}
|
|
52
|
+
// Mission logic every 3 ticks
|
|
53
|
+
if (this.gameApi.getCurrentTick() % 3 === 0) {
|
|
54
|
+
this.missionController.onAiUpdate(game, this.actionsApi, myPlayer, this.matchAwareness);
|
|
55
|
+
}
|
|
56
|
+
const unitTypeRequests = this.missionController.getRequestedUnitTypes();
|
|
57
|
+
// Build logic.
|
|
58
|
+
this.queueController.onAiUpdate(game, this.productionApi, this.actionsApi, myPlayer, threatCache, unitTypeRequests, (message) => this.logBotStatus(message));
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
getHumanTimestamp(game) {
|
|
62
|
+
return formatTimeDuration(game.getCurrentTick() / NATURAL_TICK_RATE);
|
|
63
|
+
}
|
|
64
|
+
logBotStatus(message, sayInGame = false) {
|
|
65
|
+
if (!this.enableLogging) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
this.logger.info(message);
|
|
69
|
+
if (sayInGame) {
|
|
70
|
+
const timestamp = this.getHumanTimestamp(this.gameApi);
|
|
71
|
+
this.actionsApi.sayAll(`${timestamp}: ${message}`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
updateDebugState(game) {
|
|
75
|
+
if (!this.getDebugMode()) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
// Update the global debug text.
|
|
79
|
+
const myPlayer = game.getPlayerData(this.name);
|
|
80
|
+
const harvesters = game.getVisibleUnits(this.name, "self", (r) => r.harvester).length;
|
|
81
|
+
let globalDebugText = `Cash: ${myPlayer.credits} | Harvesters: ${harvesters}\n`;
|
|
82
|
+
globalDebugText += this.queueController.getGlobalDebugText(this.gameApi, this.productionApi);
|
|
83
|
+
globalDebugText += this.missionController.getGlobalDebugText(this.gameApi);
|
|
84
|
+
globalDebugText += this.matchAwareness?.getGlobalDebugText();
|
|
85
|
+
this.missionController.updateDebugText(this.actionsApi);
|
|
86
|
+
// Tag enemy units with IDs
|
|
87
|
+
game.getVisibleUnits(this.name, "enemy").forEach((unitId) => {
|
|
88
|
+
this.actionsApi.setUnitDebugText(unitId, unitId.toString());
|
|
89
|
+
});
|
|
90
|
+
this.actionsApi.setGlobalDebugText(globalDebugText);
|
|
91
|
+
}
|
|
92
|
+
onGameEvent(ev) {
|
|
93
|
+
switch (ev.type) {
|
|
94
|
+
case ApiEventType.ObjectDestroy: {
|
|
95
|
+
// Add to the stalemate detection.
|
|
96
|
+
if (ev.attackerInfo?.playerName == this.name) {
|
|
97
|
+
this.tickOfLastAttackOrder += (this.gameApi.getCurrentTick() - this.tickOfLastAttackOrder) / 2;
|
|
98
|
+
}
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
default:
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
106
|
//# sourceMappingURL=bot.js.map
|
package/dist/bot/bot.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bot.js","sourceRoot":"","sources":["../../src/bot/bot.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,GAAG,EAAqB,UAAU,EAAE,WAAW,EAAQ,MAAM,wBAAwB,CAAC;AAE7G,OAAO,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,iBAAiB,EAAE,MAAM,sCAAsC,CAAC;AACzE,OAAO,EAAE,eAAe,EAAE,MAAM,qCAAqC,CAAC;AACtE,OAAO,EAAkB,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1E,OAAO,
|
|
1
|
+
{"version":3,"file":"bot.js","sourceRoot":"","sources":["../../src/bot/bot.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAA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