StarCraft II API是暴雪为星际2提供的编程接口,可用于:
脚本机器人
基于机器学习的机器人
录像分析
游戏辅助工具
星际2只提供Windows和Mac的客户端,但是暴雪为Linux提供了星际2 headless客户端(无原始游戏画面)。
星际2客户端
Windows和Mac平台可直接至暴雪官网下载安装星际2客户端。客户端体积较大(大约30G),如果网速慢的话,可以使用工具通过国服下载。
Linux平台需下载暴雪提供的headless客户端。
暴雪官方C++库
星际2API通过websocket通讯,使用为封装好的基于protobuf定义的协议。理论上,下载安装了平台对应的星际2客户端,就可以直接编写程序访问API协议,但是一般而言,使用暴雪官方的C++库或者 其他 语言 的第三方库比较方便。
一个简单的例子:
// `sc2_api.h`是创建机器人需要的常用头文件。
#include <sc2api/sc2_api.h>
#include <iostream>
using namespace sc2;
class Bot : public Agent {
public:
// 游戏开始时吼一声
virtual void OnGameStart() final {
std::cout << "Hello, World!" << std::endl;
}
// 每走一步就调用一次 OnStep 函数
virtual void OnStep() final {
// Observation 函数返回一个接口,
// 这个接口包含一个检查当前游戏状态的函数。
std::cout << Observation()->GetGameLoop() << std::endl;
}
};
int main(int argc, char* argv[]) {
// 加载设定
Coordinator coordinator;
coordinator.LoadSettings(argc, argv);
// 设定种族
Bot bot;
coordinator.SetParticipants({
CreateParticipant(Race::Terran, &bot),
CreateComputer(Race::Zerg)
});
// 运行游戏
coordinator.LaunchStarcraft();
coordinator.StartGame(sc2::kMapBelShirVestigeLE);
// 不断循环调用 coordinator.Update(),
// 使机器人不断前进。
while (coordinator.Update()) {
}
return 0;
}
更多例子见GitHub仓库.
暴雪官方C++库需要编译安装(除非你在Windows平台下使用Visual Studio 2017)。
首先,安装以下依赖:
Civetweb
Protobuf
(可选)SDL
然后,运行如下命令拉取源代码:
git clone --recursive https://github.com/Blizzard/s2client-api
cd s2client-api
mkdir build
cd build
具体的编译命令各平台不同。
Windows:
cmake ../ -G "Visual Studio 15 2017 Win64"
start s2client-api.sln
MacOS:
cmake ../ -G Xcode
open s2client-api.xcodeproj/
Linux:
cmake ../
make
如果你在Wndows平台上使用Visual Studio 2017,那可以直接下载编译好的二进制文件。
机器人网原创文章,未经授权禁止转载。详情见转载须知
本文来自机器人网,如若转载,请注明出处:https://www.jqr.com/news/008216