scmJS docs

Bots for mods

On the play page and under scmjs bot run --mod, a bot plays a mod as it plays the game. Its view and its tables are the mod's, read from the game when the match starts: the mod's prices, its tech tree, its types past the game's 228. It is told which mod it is in game.init.mod ({ name, hash }), and every unit type is called by the mod's own name, made an identifier. In Cosmonarchy BW the terran worker is Terran_Mason, so game.want("Terran_Mason", 14), or game.want("Mason", 14), trains Masons. BWAPI's names for the game's types stay as second names for the same types (Terran_SCV is the Mason there too), so a bot written for the game's own units still runs. The names are the ones scmjs mod header writes, by the rules the mods guide gives; scmjs mod header "<mod>" --js writes them as a module, for your editor's completion. Weapons, upgrades and techs keep BWAPI's names while the game runs.

This bot is made for Cosmonarchy BW. Its Masons mine, the Ministry trains more of them, a Mason puts up a Stockade (and more of them, up to four, while minerals pile up), the Stockades train Mavericks, and every six Mavericks attack:

const WORKERS = 14, WAVE = 6, STOCKADES = 4;

export default {
  name: "Cosmonarchy Example (JavaScript)",

  onStart(game) {
    const mod = game.init.mod;
    if (mod?.name !== "Cosmonarchy BW") game.log(`made for Cosmonarchy BW, playing ${mod ? mod.name : "the game's own units"}: the names below may mean nothing here`);
    if (game.race.name !== "Terran") game.log("I play Cosmonarchy's terrans only: my workers will mine, nothing more");
  },

  onFrame(game) {
    game.mine();
    if (game.race.name !== "Terran") return;

    game.want("Terran_Mason", WORKERS);

    const stockades = game.count("Terran_Stockade");
    const more = stockades === 0 || (stockades < STOCKADES && game.minerals >= 2 * game.info("Terran_Stockade").mineralPrice);
    if (more && game.planned("Terran_Stockade") === 0) game.build("Terran_Stockade");

    game.want("Terran_Maverick", 200);

    const target = game.enemyBase();
    const atHome = game.army.of("Terran_Maverick").filter((u) => !u.memory.out);
    if (atHome.length >= WAVE && target) {
      for (const u of atHome) u.memory.out = true;
      atHome.attack(target);
    }
    game.army.filter((u) => u.memory.out).idle.attack(target);
  },

  onEnd(result, game) {
    game.log(result.result === "victory" ? "gg: a win for Cosmonarchy BW" : "gg");
  },
};

On the play page, choose the mod under Mods, then add the file as a bot. From a terminal, scmjs bot run cosmonarchy.mjs "(4)Exodus 1.0.scx" --mod "Cosmonarchy BW" plays it with the mod's plugins (a bot on a mod). The mods guide has more on bots made for a mod.