StarCraft Rust Bot

     

StarCraft 2 logo

I finished my StarCraft Rust Bot! It’s now good enough to beat the hardest AI that ships with the game. All in only 152 lines. You can see the original source code here. Even though the code itself is brief, the journey to creating and testing it was not.

Originally the bot was going to be built using BWAPI (Brood War API). Then I found out that BWAPI was written in C++. Rust has a foreign function interface (FFI) binding generation library, but the C++ headers are so badly mangled that most of the code that is generated is useless. So I did a little digging and came across BWAPI-C, a Brood War API written in C. That was a little better, but it was still annoying and error-prone to have to generate the FFI myself. I did a little more research and found someone else had already done the work for me and created BWAPI-RS, a Brood War API written in Rust. Unfortunately that project had very little in the way of documentation. The README specifically mentions “As you’ve probably noticed, the project is in it’s very early days of development. Actually, we have no idea how it would work. The future will tell.” The fact that it hasn’t been updated in over five years was hardly encouraging.

Finally, I stumbled upon the Rust-SC2 repository. Unlike the other repositories, this one was in active development, had great documentation, and a lively community with its own Discord server. After trying Manjaro (ruled out because most of the Linux dependencies for the project had to be recompiled from Debian to Arch) and Ubuntu (ruled out because of driver incompatibilities with my PC), I finally settled on Windows. While the Windows setup documentation from the Rust-SC2 repo wasn’t as comprehensive as it was for Linux, it was enough to get me started. Running on Windows also meant that I didn’t need to bother with Wine (Wine Is Not an Emulator). Once I had StarCraft 2 installed and rust setup through rustup I was finally able to start iterating on bot.

The bot itself was pretty simple to write once I became acquainted with the methods and properties available through the Rust-SC2 API. Its a Terran bot that builds a supply depot and then 4 barracks as soon as its initial supply of SCVs (Space Construction Vehicles) has harvested a sufficient supply of minerals to do so. It then starts building marines, with an SCV being redirected from resource duty to build a supply depot every time the available supply gets below 3. Once 20 marines have been built they’re all ordered to move to the enemy’s starting location. From there they attack any enemies that they find, and then move on to the enemy’s expansion locations to perform a final cleanup.

Admittedly, the marine blob is a pretty cheesy strategy, but it did allow me to put the fundamentals that I had learned from the Rust Programming Language book the previous month into practice. I’d like to eventually expand my bot to be able to play as Zerg or Protoss as well, but for the purposes of my New Year’s Resolutions, I’m considering this one complete!

comments powered by Disqus