npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@node-sc2/core

v0.9.0-beta.1

Published

A lightweight node.js framework to facilitate writing agents (or 'bots') for Starcraft II in JavaScript.

Downloads

3

Readme

node-sc2

node-sc2 is a lightweight node.js framework to facilitate fast development of agents (or "bots") for Starcraft II in JavaScript.

Motivation

There are a few existing libraries in the node.js ecosystem to work with the C++ API (sc2client-api), but node-sc2 is a pure javascript implementation of sc2client-proto, with the goal of being ergonomic across a variety of environments without the need for additional build tools. Under the hood it uses @node-sc2/proto as the transport layer.

Getting started

npm install --save @node-sc2/core

You must also have any maps you expect to load available in the standard location (/Maps relative to your SC2 directory). Official map downloads can be found here. For a more up-to-date pack of maps, join the SC2 AI discord (https://discord.gg/Emm5Ztz) and type !maps in #general.

Hello World

The 'hello world' of sc2 bots seems to be a worker rush, so... here we go:

// main.js
const { createAgent, createEngine, createPlayer } = require('@node-sc2/core');
const { Difficulty, Race } = require('@node-sc2/core/constants/enums');

const bot = createAgent({
    async onGameStart({ resources }) {
        const { units, actions, map } = resources.get();

        const workers = units.getWorkers();
        return actions.attackMove(workers, map.getEnemyMain().townhallPosition);
    }
});

const engine = createEngine();

engine.connect().then(() => {
    return engine.runGame('Blueshift LE', [
        createPlayer({ race: Race.RANDOM }, bot),
        createPlayer({ race: Race.RANDOM, difficulty: Difficulty.MEDIUM }),
    ]);
});

Now you can run it with node main.js. Wasn't that easy? Now this isn't going to win you any awards... but it might win you a few games against the built-in AI.

NOTE: The first time you run the bot, it will take up to 15 seconds to launch the SC2 client. After that, the default behavior is to keep the client running, so starting a new game will take only a moment. Also, feel free to manage the client yourself, as node-sc2 will just use the existing instance listening on the selected port.

Overview and Tutorial

An overview of the library and its usage is available by clicking here. The overview is the recommended place to get started. If you want to skip it and go straight to a tutorial of a bot that can consistently win against the built-in Elite AI, click here.

Feature Layer and Rendered Interfaces

Although most of the supported features of node-sc2 are based on the raw data interface, there is initial support for both the Feature Layer and the Rendered interfaces. Importantly, you must set your NODE_ENV to production if you don't want the rendered interface to crawl (unless you need long stack trace support). For example, on windows that would be: set NODE_ENV=production. The feature layer interface is pretty slow either way (on windows, linux should be significantly better). Currently there are no abstractions but you can get access the exposed data as such:

/** @type {SC2APIProtocol.SpatialCameraSetup} */
const camera = {
    // you can experiment with various resolutions
    resolution: {
        x: 640,
        y: 480,
    },
    // you can also leave this off if you don't want the minimap to render
    minimapResolution: {
        x: 128,
        y: 128,
    }
};

// the 'interface' prop of your agent blueprint is of SC2APIProtocol.InterfaceOptions
const bot = createAgent({
    settings: {
        race: Race.PROTOSS,
    },
    interface: {
        raw: true,
        score: true, // optional, score data
        render: camera, // turns on the rendered interface
        featureLayer: camera, // turns on the feature layer interface
    },
    async onStep(world) {
        const { frame } = world.resources.get();
        console.log(frame.getRender(), frame.getFeatureLayer());
    }
});

The shape of the data is of types SC2APIProtocol.ObservationRander and SC2APIProtocol.ObservationFeatureLayer. More can be read about it in the blizzard proto definitions.

Notice of Active Development

The goal of @node-sc2/core is to use semver. As long as @node-sc2/core is pre v1.0.0, the library is under very active development. It may be missing obvious features, even ones that are simple to implement. It may expose APIs that are fundimentally broken. It may even break backwards compatibility (although we're going to try really hard not to without major version bumps). The goal is to get to v1.0.0 rapidly and remove this notice.

Contributing

Any contributions are appreciated. That includes detailed issue reports (with repro if possible), comments, concerns, API design/suggestions, and PRs. I will try to work together with everyone as much as feasible to create the best user experience possible.

Ladder and Tournament Submission

Currently the following method is available as a stop-gap to be able to submit your node-sc2-based bot to a tournament or ladder (such as one using software like Sc2LadderServer).

First install pkg, which is what we're going to use to package up your bot: npm install --save-dev pkg

Then add the bin directive, and this npm script to your package.json under scripts:

"bin": "main.js", // change main.js to your entrypoint file name, if different
...
"scripts": {
    "build": "pkg ./ --target win-x64 --out-path ./dist",
}

Finally, run the script: npm run build. Your compiled bot will be in dist/your-bot-name.exe and will be compatible with the CLI commands needed to run it using the ladder manager or similar software.

In the future, this will be built into node-sc2 and be a cli command. It is on the major timeline and should be ready by v1.0.0.

Features and Roadmap

This readme will be updated with a link to a Trello board shortly, outlining feature development roadmap progress. On top of that, github issues can be used to discuss features and bugs.

Environmental Variables

Various settings can be adjusted through env vars in your shell. In a windows command shell, this is done with the set command, eg: set DEBUG=sc2:debug:*.

Speed Control

Default is 4 frames per step. This can be adjusted with the STEP_COUNT env var... for example, STEP_COUNT=8 for faster simulations.

Debugging

node-sc2 makes use of the debug library. Run your agent with DEBUG=sc2:debug:* for additional helpful output, or DEBUG=sc2:* for way too much output that's probably not too helpful :) For extra fun, run your script with node --inspect, open a chrome instance, navigate to chrome://inspect and click on "Open dedicated DevTools for Node". Enjoy the full debugging experience (including cpu and memory profiling, pausing, breakpoints, etc).

Getting Help

First I would encourage you to read through all available documentation. Beyond this readme, three other user documents are available:

Beyond that, there are also two documents aimed towards those wanting to help develop the core library, or just understanding more about how it works:

Beyond the documentation, Starcraft 2 AI has a very active community, available through this discord invitation link: https://discord.gg/Emm5Ztz - This library specifically can be discussed in the #javascript channel. Come say hi!

Changelog

The changelog has been moved to a dedicated document: changelog.md.