screepers/cppreeps

Name: cppreeps

Owner: screepers

Description: WASM C++ Screeps API and utilities pack (beta, proof-of-concepts)

Created: 2017-10-03 13:16:36.0

Updated: 2018-02-16 03:09:40.0

Pushed: 2017-10-16 10:18:34.0

Homepage:

Size: 25

Language: C++

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

CPPREEPS

Example of C++ API, bindings and utilities for Screeps game. Native C++ source code can be compiled to WebAssembly (WASM) format using Emscripten compiler.

WebAssembly (or wasm) is a low-level bytecode format for in-browser client-side scripting, evolved from JavaScript. Its initial aim is to support compilation from C and C++, though other source languages such as Rust are also supported… WIKI

Emscripten is an Open Source LLVM to JavaScript compiler. Using Emscripten you can: compile C and C++ code into JavaScript, compile any other code that can be translated into LLVM bitcode into JavaScript, compile the C/C++ runtimes of other languages into JavaScript, and then run code in those other languages in an indirect way (this has been done for Python and Lua)!.. DOCS

WASM modules (.wasm files) can be relatively easily loaded within .js code. After that, native C++ functions can be called from JS, example:

t wasm_loader = require("wasm_loader");
t module = wasm_loader("js_filename", "wasm_filename");
 * * */
result = module.native_function(arg1, arg2);
some_sinus = module.sin(9001);
packed_mem = module.lzw_encode(RawMemory.get());
path = module.find_path(creep1, creep2);
Getting started (under construction)
  1. Download and install emsdk (Emscripten SDK, compiler toolchain): guide.

    NOTE: the simplest way to get started with WASM is to use portable emsdk package: it easy to install and work with, just follow installation guide very carefully.

    This step briefly (Linux/iOS syntax):

    ownload emsdk-portable.tar.gz && Unpack archive
    emsdk update
    emsdk install latest
    emsdk activate latest
    
  2. Prepare building environment:

    • Linux/iOS: $ source ./emsdk_env.sh

    • Windows: > emsdk_env.bat

    • OR by configuring own building system (see official example).

      NOTE: configuring own building system (cmake + make etc.) is the most painful way to get started with Emscripten, so we really need help here to complete this guide =)

  3. Build project using em++ to WASM module (pair of .wasm and .js files):

    • Linux/iOS: $ ./create.sh (see script file example)
    • Windows: > create.bat (see script file example)
    • OR using your own building system (make etc.)
  4. Push generated files from /dist folder to Screeps/PTR (using grunt, gulp, or whatever), see Screeps docs: 1, 2.

    NOTE: WASM is an experimental feature, and for now it only available on PTR.

    NOTE: for now binary GUI uploading isn't implemented yet.

See src/loop.cpp, src/main.js for WASM usage examples.

Current repo can be cloned by: git clone --recursive git://***.git

Submodules updating can be performed by: git submodule update --init --recursive


Utility pack:

LZW-based codec (submodule) Build Status

Header-only library lib/lzw/lzw.hpp contains implementation of original LZW compression/decompression algorithms. It's a submodule, see my source repo for details.

Header file include/lzw.hpp contains wrapper around lzw_codec<ASCII_128_common, UTF16_pack> codec functions and EMSCRIPTEN_BINDINGS(lzw){...} block exporting them to WASM module making them easy to use. Their native signatures:

:wstring lzw_encode(std::wstring in);
:wstring lzw_decode(std::wstring in);

…and example of JS usage (see sources for other examples):

t src = "Ololo, some string!";
t enc = mod.lzw_encode(src);
t dec = mod.lzw_decode(enc);
ssert(src == dec);
Native performance test (1 KIB string):
encode = 94 us/KIB, decode = 94 us/KIB
ratio = 0.0527344 (enc/src) str="AAAAAAAAAAAAAAAA..."
encode = 117 us/KIB, decode = 20 us/KIB
ratio = 0.538086 (enc/src) str="{"key1":42, "key2":"val1"..." (~JSON)
encode = 151 us/KIB, decode = 16 us/KIB
ratio = 1.25977 (enc/src) str="v21ny6E5624VjTk8..." (random)
JS performance tests, lzw_xxcode(RawMemory.get()), ~600 KIB:
encode = 167.497 CPU, 244.144 CPU/MIB
       => ratio = 0.229, len = 597102
decode = 185.010 CPU, 1413.525 CPU/MIB
In-game usage result:
ry view:

KB
:   {\"stats\":{\"profiler.findInRange\":0.06027972683740784,..."

KB
:  **???????????????????????????????????...

Thanks @ags131, @primus, @tedivm for their examples and experience =)


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.