1个稳定版本

1.0.0 2021年2月4日

#2567 in Rust模式


rust-sc2 中使用

自定义许可

3KB

rust-sc2

StarCraft II API的Rust实现

该库旨在简单易用,同时速度快且功能丰富。然而,它提供了高、低层次抽象。这个库受到python-sc2库的启发,因此人们可能会觉得切换到rust-sc2很容易。它最初创建是因为其他Rust库老旧、功能不完整且层次低。

欢迎在这些Discord服务器的 #rust 频道中提问

入门指南

安装Rust >= 1.42.0

警告:在rustc 1.45.0 - 1.46.0中编译会出错,您将得到以下错误

thread 'rustc' has overflowed its stack
error: could not compile `rust-sc2`.

在Cargo.toml中添加依赖项

[dependencies]
rust-sc2 = { git = "https://github.com/UltraMachine/rust-sc2" }

少于30行的最简单的竞技性机器人

use rust_sc2::prelude::*;

#[bot]
#[derive(Default)]
struct WorkerRush;
impl Player for WorkerRush {
    fn get_player_settings(&self) -> PlayerSettings {
        PlayerSettings::new(Race::Protoss, None)
    }
    fn on_start(&mut self) -> SC2Result<()> {
        for worker in self.units.my.workers.iter() {
            worker.attack(Target::Pos(self.enemy_start), false);
        }
        Ok(())
    }
}

fn main() -> SC2Result<()> {
    let mut bot = WorkerRush::default();
    run_vs_computer(
        &mut bot,
        Computer::new(Race::Random, Difficulty::Medium, None),
        "EternalEmpireLE",
        Default::default(),
    )
}

更多高级示例请参阅 examples 文件夹。

可选功能

  • "rayon" - 启用并行性并使所有类型线程安全
  • "serde" - 为ids、Race、GameResult等添加SerializeDeserialize实现

逐步制作机器人

首先,导入rust-sc2库

use rust_sc2::prelude::*;

创建你的机器人结构体(可以是Unit或C-like)

#[bot]
struct MyBot;
#[bot]
struct MyBot {
    /* fields here */
}

然后为你的机器人实现Player特质

// You mustn't call any of these methods by hands, they're for API only
impl Player for MyBot {
    // Must be implemented
    fn get_player_settings(&self) -> PlayerSettings {
        // Race can be Terran, Zerg, Protoss or Random
        // If bot name is None, it'll be shown as "foo(whatever)" in game
        PlayerSettings::new(Race::Random, Some("Bot Name"))
    }

    // Methods below aren't necessary to implement (Empty by default)

    // Called once on first step
    fn on_start(&mut self) -> SC2Result<()> {
        /* your awesome code here */
    }

    // Called on every game step
    fn on_step(&mut self, iteration: usize) -> SC2Result<()> {
        /* your awesome code here */
    }

    // Called once on last step
    // "result" says if your bot won or lost game
    fn on_end(&self, result: GameResult) -> SC2Result<()> {
        /* your awesome code here */
    }

    // Called on different events, see more in `examples/events.rs`
    fn on_event(&mut self, event: Event) -> SC2Result<()> {
        /* your awesome code here */
    }
}

你也可以添加一个构造它的方法

impl MyBot {
    // It's necessary to have #[bot_new] here
    #[bot_new]
    fn new() -> Self {
        Self {
            /* initializing fields */
        }
    }
}

如果你的机器人实现了Default,你可以简单地调用MyBot::default(),但如果你想要更多的初始化控制

impl MyBot {
    // You don't need #[bot_new] here, because of "..Default::default()"
    fn new() -> Self {
        Self {
            /* initializing fields */
            ..Default::default()
        }
    }
}

其余的就是运行它

fn main() -> SC2Result<()> {
    let mut bot = MyBot::new();
    run_vs_computer(
        &mut bot,
        Computer::new(
            Race::Random,
            Difficulty::VeryEasy,
            None,              // AI Build (random here)
        ),
        "EternalEmpireLE", // Map name
        LaunchOptions::default(),
    )
}

依赖项

~3.5–4.5MB
~89K SLoC