scmJS docs

A first bot

A bot in JavaScript is one ES module. The game calls it every few frames with what its player can see, and it answers with orders.

There are two ways to write one. The short way is an onFrame(game) function: units are objects with their orders on them, counts include what is on its way, and a building you ask for finds its own money, builder and site. Start there. The interface underneath is create(init) and onFrame(view, api): a plain view of the game and a list of orders, for a bot that wants to keep its own books. The short way is written on it. In the types your editor shows, a bot written the short way is an EasyBot, and one written on the interface underneath is a BotModule.

Download starter.mjs (from the scmbot repository's examples), or save the code below under that name. It is Starter, one of the play page's own bots: it plays all three races, and against the game's own computer it wins more games than it loses.

/** Each race's first fighting building and the unit it makes. The game's names are forgiving: "Marine" will do. */
const ARMY = {
  Terran: { building: "Barracks", unit: "Marine" },
  Protoss: { building: "Gateway", unit: "Zealot" },
  Zerg: { building: "Spawning_Pool", unit: "Zergling" },
};

/** How many fighters go out together. */
const WAVE = 12;

/** @type {import("@scm-js/bot").EasyBot} */
export default {
  name: "Starter",
  version: "2",

  onFrame(game) {
    const { building, unit } = ARMY[game.race.name];
    const zerg = game.race.name === "Zerg";

    // 1. The economy. Idle workers go to the minerals, a supply building (or overlord) goes up before supply runs
    // out, and workers are trained up to 18. `want` counts what is already on its way, so it is safe every frame.
    game.mine();
    game.keepSupply();
    game.want(game.race.worker, 18);

    // 2. Production: two of the fighting building (a zerg needs one pool; its units come from larvae).
    game.want(building, zerg ? 1 : 2);

    // 3. Growth. A new base before the minerals at ours run out — for zerg also whenever money piles up, since each
    // hatchery brings more larvae. For the others, piling money means one more production building, up to six.
    const mineralsLeft = game.ownBases.reduce((sum, base) => sum + game.mineralsLeft(base), 0);
    const ready = game.done(building).length > 0;
    if (game.planned(game.race.hall) === 0 && (mineralsLeft < 4000 || (zerg && ready && game.minerals > 400))) game.expand();
    if (!zerg && ready && game.minerals > 400 && game.planned(building) === 0 && game.count(building) < 6) game.build(building);

    // 4. The army: as many fighters as there is money for.
    game.want(unit, 200);

    // 5. Attack. A dozen at home leave together for the enemy's base; once out, a fighter with nothing left to do
    // goes on to the next thing of theirs it knows of (`enemyBase` searches the map when it knows of nothing).
    const target = game.enemyBase();
    const atHome = game.army.filter((u) => !u.memory.out);
    if (atHome.length >= WAVE && target) {
      for (const u of atHome) u.memory.out = true;
      atHome.attack(target);
      game.log(`a wave of ${atHome.length} goes to ${Math.round(target.x)},${Math.round(target.y)}`);
    }
    game.army.filter((u) => u.memory.out).idle.attack(target);
  },
};

To play against it, open scmjs.dev/play, go to Options, and under Community bots choose Add a bot from a file… and pick starter.mjs. The page keeps the file and lists it there. The line above it, "Nothing kept yet", is about the files BWAPI bots write between games:

The play page's Options, with starter.mjs added under Community bots

Back on the setup screen, choose starter (your file) in a computer row, pick its race, and start the game; the picture under playing against a bot shows that row. The bot is named after its file, so it sits in the list beside the page's own copy, Starter (JavaScript).

To watch the bot instead of fighting it, turn Fog of war off in Options, which shows the whole map, and scroll to its base:

A terran base on the play page: workers on the minerals beside a command centre, three barracks, marines setting off, and a chat line reading "starter: a wave of 12 goes to 3456,3056"

Starter as terran, four minutes into a game: its first twelve marines leave for the other base. The line at the left is the bot's own, from the game.log call in step 5.

With the command line and your own copy of StarCraft installed, this plays it against the game's computer on a map of yours:

scmjs bot run starter.mjs "(2)Jungle Duel.scx" --race terran

Each line of onFrame asks for something, and what is asked for first is paid for first, so the order of the lines is the bot's order of priorities: workers before buildings, buildings before the army. game.want(type, n) counts what is already on its way (in training, in an egg, under construction, or asked for and not yet placed), so the same line runs every frame without ordering anything twice. game.want("Barracks", 2) is the whole of what it takes to get two barracks: the short way keeps their price aside, picks a worker, asks the game for a site and sends the worker there, and tries another site or another worker when the game turns the first one away. u.memory is an object that stays with a unit for the whole game; Starter marks there which units have left home. game.enemyBase() is the nearest enemy building the bot has seen, and before it has seen one, a start location it has not yet looked at, so a wave sent there goes looking.

ARMY names each race's first fighting building and the unit it makes. The comment above export default is optional: it tells your editor what the module is, so that game. offers its fields and methods as you type (types in your editor). The page and the command line ignore it.

Making it your own#

A change is usually a line or two. This gives a terran Starter a refinery, an academy, the upgrade that lengthens a marine's range, and a medic for every five marines. Put it before step 4, so that it is paid for before the army:

if (game.race.name === "Terran" && game.done("Barracks").length > 0) {
  game.want("Refinery", 1);
  game.want("Academy", 1);
  game.upgrade("U_238_Shells", { save: true });
  game.want("Medic", Math.floor(game.count("Marine") / 5));
}

game.mine() puts three workers on the refinery once it is finished. Nothing here waits for the academy to stand before the upgrade and the medics are asked for: upgrade orders nothing until a free academy is there, and want trains a medic only once the tech tree allows it. { save: true } keeps the upgrade's price aside for the rest of the frame when the academy is free and only the money is missing, so the marines trained after it do not spend every mineral as it comes in. Whether a change helps is found out by playing: change a line, play a ladder against the game's computer, and keep what wins (how good the sample bots are).