Playing with asm.js for AI in a cross platform game

While building troikagame.com I wanted to find ways to save myself a ton of work. The game is available both as Apps (iOS and Android) as well as a web application so people can play on other platforms easily. So we used the Ionic framework. Since it’s all web based, the core of the game is the same on the web as it is in the apps. The game is also multiplayer (via websockets) so that the different platforms can play against each other.

One of the challenges is that there is a computer player that you can play against in single player and multiplayer. Single player AI runs locally on the device/web page and multiplayer AI runs on a server. I didn’t want to write the code twice so I really wanted a common language between the two.

Weirdly, it turns out that the common language is C++. The code runs in a python module on the server and is compiled to asm.js for single player via emscripten. All of the code and scripts are available at https://github.com/smellslikedonkey/troikajs_public

To actually call the module from an es7 application, I use this

1
2
3
4
5
6
7
8
9
10
// In angular.json include the asm.js file
"scripts": [
    "pathtofile/aicpp.js"
]

// Near the top of the file
declare var Module:any;  // Expose module

// Call the module
Module.ai_get_pass_cards(...parameters...);

Check out the final results at www.troikagame.com