scmJS docs

The short way

A module whose default export has onFrame and no create is written the short way. Its fields:

Field What it is
name The bot's name. The command line puts it before the bot's log lines; the page lists a bot added from a file under the file's name
version, author Optional
frameInterval Frames between two calls of onFrame: 8 unless set, a third of a second at the game's normal speed (24 frames a second)
onFrame(game) Called every frameInterval frames. It may be async; while it runs, no other frame is delivered
onStart(game) Optional: once, before the first onFrame
onEnd(result, game) Optional: the game is over for this player. result.result is "victory", "defeat" or "ended" (stopped without a winner)
onUnitComplete(unit, game) Optional: one of the bot's units is finished
onUnitDestroy(unit, game) Optional: one of the bot's units died, or turned into something the game counts as a new unit
onEnemySeen(enemy, game) Optional: another player's unit came into sight for the first time

Orders given during a call are collected and go into the game when the call returns. The game does not wait for a bot on the page: a frame that comes due while the bot is still thinking about the last one is skipped, and the next call sees the game as it is then.

Names#

A unit type can be named the way BWAPI names it or more loosely: "Terran_Marine", "Marine", "marine" and "Siege Tank" all work, the bot's own race first. Techs and upgrades go by BWAPI's names too, as loosely: "Stim_Packs", "Protoss_Ground_Weapons". A name the game does not know throws, so a typo stops the bot with an error that names it. A type of another race ("Barracks" for a protoss) is said once in the log, and nothing is ordered. Anywhere a type is taken, its number works too.

game.type(name) turns a name into its number, and game.info(type) is the type's entry in the game's tables: mineralPrice, gasPrice, supplyRequired, maxHitPoints, groundWeapon and the rest, listed under what a bot is told once.

Units#

A unit is an object that lasts the whole game: the same object for the same unit, updated with every frame. It has the fields of the view (x, y, hitPoints, shields, energy, isIdle, order and the rest, under the view), and these besides:

On a unit What it is
alive False once the unit is known to be gone
memory An object of your own, kept with the unit
info, name The type's table entry; the type's name ("Terran_Marine")
tileX, tileY The tile it stands on (a tile is 32 pixels)
isBuilding, isWorker From its type
is(...types) Whether it is of one of these types
distanceTo(point) In pixels, to a point or another unit

The bot's own units carry their orders as methods: attack, move, patrol, gather, returnCargo, stop, hold, train, build, buildAddon, morph, research, startUpgrade, useTech, repair, heal, load, unloadAll, setRally, siege, unsiege, burrow, unburrow, stim, cloak, decloak, lift, land and cancel. Each returns the unit, so they chain. attack takes a unit, a unit's number or a point: marine.attack(enemy) attacks that unit, marine.attack({ x, y }) attack-moves there. A point is in pixels from the map's top-left corner.

An order is given as it is, and the game's own checks apply, with one exception. A fresh order makes a unit drop the shot or the heal it was in the middle of, so a bot that repeated attack every frame would have an army that never fires. For that reason attack, move and patrol are not sent again to a unit already busy with the same order (the same target, or a point within a tile and a half of the same point).

Other players' units are objects too, as last seen: game.enemies holds those in sight now, and game.enemyMemory every one seen and not known to be gone, with visible false for those out of sight and lastSeen the frame they were last seen.

Lists of units#

game.units, game.army, game.enemies and the other lists are arrays, so filter, map, length and for … of work as usual, and filter keeps a list a list. They add:

On a list What it is
of(...types) Those of these types: game.units.of("Marine", "Medic")
done, idle The finished ones; those standing with nothing to do
near(point, radius) Those within radius pixels of a point
nearest(point), farthest(point) One unit, or undefined for an empty list
byDistance(point), take(n) Nearest first; the first n
center, first, ids The middle of the group (null when empty); the first unit; their numbers

and the orders for a group: attack, move, patrol, rightClick, gather, returnCargo, stop, hold, siege, unsiege, burrow, unburrow, stim, load and setRally, sent to all of the bot's own units in the list as one order. attack and move with null do nothing, so game.army.attack(game.enemyBase()) is safe before the enemy is found.

The game#

On game What it is
race The bot's race: name ("Terran", "Protoss", "Zerg"), and the types of its worker, hall (town hall), supply provider and gas building
frame The game's frame counter: 24 frames is a second at normal speed
home The bot's start location: x, y in pixels, tileX, tileY of its town hall
bases Every base on the map, nearest home first: its mineral fields, its geysers, and the tile a town hall goes on (hall)
ownBases, freeBases() Bases with a finished town hall of the bot's; bases where no one's town hall is known
mineralsLeft(base) What a base's fields hold, as last seen
units, workers, army, buildings, larvae The bot's units: all of them; finished workers not building; finished fighting units (not workers, overlords, larvae or eggs); buildings; larvae not yet given an order
enemies, enemyMemory, neutral Other players' units in sight now; every enemy unit seen and not known to be gone; mineral fields, geysers and critters in sight
minerals, gas Money not yet spent this frame and not kept for a building asked for
supply used, total and free, in the game's own numbers (a zergling is ½), less this frame's orders
count(type) Units of a type, finished and on their way
done(type), coming(type) The finished ones; how many are on their way
planned(type) Buildings asked for and not yet placed
want(type, n) Have n, counting what is on its way: trains, morphs or builds what is missing as far as money, supply, producers and the tech tree allow. Call it every frame
train(type, { save }) One more now, if a producer is free: a larva for a zerg unit, the building for an add-on or a Lair. Returns whether it was ordered
make(type) One more, whatever it takes: as train, and for a building, as build
build(type, near?) Ask for one building, near a point or on a tile given as { tileX, tileY }. Returns the job: its state is waiting, asking, sited, walking, started or failed, with reason when it failed
expand() A town hall at the nearest free base. Returns the job, or null when no base is free
research(tech, { save }), upgrade(upgrade, { save }) At a free building of the right kind (a Lair or Hive does what a Hatchery does). Return whether it was ordered
has(tech), level(upgrade) Whether a tech is researched; the level an upgrade has reached
researching(tech), upgrading(upgrade) Whether it is under way
canAfford(type), canMake(type) The price and supply are there; the tech tree allows it
save(type) Keep a type's price aside for the rest of this frame
mine({ gas }) Idle workers to the least busy field of their nearest base, and gas workers (3 unless set) on each finished gas building; any more go back to the minerals
keepSupply({ margin }) One supply building or overlord more whenever the supply left, counting those on their way, falls below margin
freeGeyser() A geyser at one of the bot's bases with no gas building on it or asked for, or null
enemyBase() The nearest enemy building known; else the nearest start of a seated enemy not yet seen empty; else the base out of sight longest
unit(id) One of the bot's units by its number
log(text) A line of text: printed by the command line, and shown on the page as a chat line from the bot
memory An object of your own, kept for the whole game
init, view, api The interface underneath, for anything the short way does not cover

What the short way takes care of#

The game has rules that every new bot runs into. The short way handles these, so a bot written on it does not have to.

What is asked for first is paid for first. A building asked for with want keeps its price from the point where that line runs, each frame: the lines before it come first, the lines after it wait. A building asked for once, with build or expand, keeps its price from the start of every frame, and so does one whose site has been found. A building whose want line stops running is dropped after ten seconds. An order given this frame keeps its price, its supply and its producer until the game shows it, so a building is not told twice to train while the first order is on its way.

A builder is picked and watched. The nearest worker with free hands (not carrying minerals, not on gas) is sent. One the game turns away is replaced and another site is asked for; one that stands still or takes too long is called off and rested. After eight sites, or three workers called off, the building is given up, with a line in the log that says why.

The game places no building on ground nobody has seen. A builder sent to a site far off or out of sight walks there first, and the building is ordered, and its price kept, once the builder is near and the site in sight. A long walk does not hold the money up.

No worker is sent to die. A site with enemy fighters on it is left for a few seconds while another is looked for, and that does not count against the building. A base where an expansion failed is left alone for a minute.

A town hall goes where the game allows, three tiles clear of every mineral field and geyser, and of those spots, the one nearest them all.

Types in your editor#

The types of both ways are published as the npm package @scm-js/bot. Nothing in it runs: it gives your editor completion and catches a misspelt method before you play. In a folder of your own:

npm install --save-dev @scm-js/bot

In a JavaScript file, name the type in a comment on the default export, as Starter does:

/** @type {import("@scm-js/bot").EasyBot} */
export default {
  name: "Miner",
  onFrame(game) {
    game.mine();
    game.want(game.race.worker, 24);
  },
};

Visual Studio Code reads that comment with no setup. Add // @ts-check as the file's first line to have mistakes underlined as well. In TypeScript, import the types with import type, which is removed before the bot runs:

import type { EasyBot, Game } from "@scm-js/bot";

const bot: EasyBot = {
  name: "Miner",
  onFrame(game: Game) {
    game.mine();
    game.want(game.race.worker, 24);
  },
};
export default bot;

The command line runs a .ts file as it is on Node.js 22.18 or newer. The page takes .js and .mjs files only, so compile a TypeScript bot to JavaScript before you add it there. A bot the page plays cannot import other files at run time, since the page keeps the one file.

The package's major version is the interface's (BOT_API, now 1). The interface only grows: a field or a call that exists keeps its meaning.

scmJS 0.18.0 · Generated from the repository. The source is under the MIT license, which does not cover what ATTRIBUTION.md lists. StarCraft and Brood War are trademarks of Blizzard Entertainment; this project ships none of their data.