BAR Headless
What is this?
This is a very basic writeup of what I discovered on how to run a headless game between AI’s and how to run a headless replay. The information here is not 100% accurate; it’s just what worked for me at the time. Take it with a grain of salt!
Running a Headless AI Game
When running a headless game between two AI’s I wanted to accomplish the following:
- Be able to choose the map, AI’s, settings, etc. of the game that would be run
- Have the game run quickly and with as few resources as possible
- Generate a replay from which I could watch the game and use for further projects
Choosing the game
This was accomplished by simply setting up a game using the normal BAR UI.
- Open the game as you normally would
- Go to the Skirmish menu
- Choose
Spectate
above theStart
button (this will remove you from the game)
- Choose the AI(s), map, teams, and settings that you wish
- Start the game
- Exit the game once it is loaded (there is no need to wait for this game to play out, you are only getting the settings)
Run a headless game quickly
Now if you go to your BAR data directory (probably either C:\Program Files\Beyond-All-Reason\data
or %LocalAppData%\Programs\Beyond-All-Reason\data
) there should be file called _script.txt
.
Example _script.txt:
- This file contains all the settings from the game you just set up. In that file replace
|
|
with
|
|
so that the game will run as fast as possible (otherwise it will run in real time, which is pointless if you are not watching or playing the game).
- Go to the engine folder under the BAR data folder and find the most recent engine
Example (in this case it would be 105.1.1-2511-g747f18b bar
):
- Open a command prompt and go to that engine directory using
cd
cd "C:\Program Files\Beyond-All-Reason\data\engine\105.1.1-2511-g747f18b bar"
- Run a headless game using the
_script.txt
file you generated
.\spring-headless.exe --write-dir <full path to the BAR/data directory> _script.txt
Example: .\spring-headless.exe --write-dir "C:\Program Files\Beyond-All-Reason\data" _script.txt
Getting the replay from our headless game
Thankfully BAR automatically generates a replay of our game in the demo
folder under the main BAR data folder. This means we can watch it using BAR’s built-in replay viewer or use it in whatever projects we would normally use a replay.
Running a Headless Replay
Here we want to be able to run any replay headlessly in order to generate statistics or other data from the replay that may not be output when the game is initially run. We need to
- Get the map
- Get a replay
- Get the game version
- Get the engine
- Inject a widget (to export data)
- Run the replay headlessly
Getting the Stuff
Replays can be downloaded easily from the BAR website, but there is also a replay API that allows you to download the replay programmatically. This API gives responses in JSON format, so you can easily code a project to pull these replays.
To get what you need:
- Download any needed maps (preferably all available maps) from the BAR map pack (NOTE: there is an API for this, but I haven’t quite figured that out yet)
- Get the result from https://api.bar-rts.com/replays?page={page}&limit={length}&preset=duel&hasBots=false&endedNormally=true (this address can be edited as you wish for the kind of replay you want)
- Select the
id
of the replay you want and get the JSON response from https://api.bar-rts.com/replays/{id}. This will give you thefileName
,engineVersion
, andgameVersion
. - Download the replay from https://storage.uk.cloud.ovh.net/v1/AUTH_10286efc0d334efd917d476d7183232e/BAR/demos/{fileName}
- Move that replay file into the BAR
demos
directory (located under the BARdata
directory) - Open a command prompt and go to the BAR bin directory (<BAR Install directory>/bin) using
cd
Example: cd C:\Program Files\Beyond-All-Reason\bin
- Run the
pr-downloader
to download the game version by running:.\pr-downloader.exe --filesystem-writepath <BAR data folder> --download-game <gameVersion>
Example: .\pr-downloader.exe --filesystem-writepath "..\data" --download-game "Beyond All Reason test-26380-9194139"
- Download the
engineVersion
from Github, REMOVING THEBAR105
FROM THE VERSION, using this URL:https://github.com/beyond-all-reason/spring/releases/download/spring_bar_%7BBAR105%7D{engineVersion}/spring_bar_.BAR105.{engineVersion}_windows-64-minimal-portable.7z
Example: https://github.com/beyond-all-reason/spring/releases/download/spring_bar_%7BBAR105%7D105.1.1-2511-g747f18b/spring_bar_.BAR105.105.1.1-2511-g747f18b_windows-64-minimal-portable.7z
- Extract and move this engine folder into the
engine
directory (keep it in its own, named folder).
Injecting a Widget
In order to extract data, we need to inject a widget into the engine that will run as the replay runs. This is a little touch-and-go, and is the one part of this process that I’m pretty unsure of. What I have gotten to work is by spoofing another widget’s widget:GetInfo
function. If this doesn’t work, try spoofing a different widget? It’s worked before!
Take the following widget file and drop it into <BAR Engine>\LuaUI\Widgets
folder (create the folder if you need to) and name it whatever you wish.
Example: C:\Program Files\Beyond-All-Reason\data\engine\105.1.1-2511-g747f18b bar\LuaUI\Widgets\headless_stat_generator.lua
This widget file will speed up the replay when running in headless mode, and will output statistic and unit information into the BAR data
directory as CSV’s.
Widget File:
|
|
Running the Replay
In order to run the replay we need to create a _script.txt
file that points to the replay, and then run that replay headlessly.
- Create a
_script.txt
file in the BARdata
folder with contents like the following:
|
|
Example:
|
|
- Open a command prompt and go to that engine directory using
cd
cd "C:\Program Files\Beyond-All-Reason\data\engine\105.1.1-2511-g747f18b bar"
- Run a headless game using the
_script.txt
file you generated
.\spring-headless.exe --write-dir <full path to the BAR/data directory> _script.txt
Example: .\spring-headless.exe --write-dir "C:\Program Files\Beyond-All-Reason\data" _script.txt