From 291ec169325ad2e8d3c322560fc7054c4cecfe4e Mon Sep 17 00:00:00 2001 From: Jeany Date: Sat, 14 Nov 2026 19:19:24 -0600 Subject: [PATCH] trigscript/card and auto-restore's right-click toggle, back as they were merged This puts back what "trigscript/card and auto-restore's right-click toggle wait on a branch" took off main: the card library (CardButton, cardSlots, cardSetup), autoRestore()'s allowPlayersToTurnOffAutoCast, their ledger rows and their tests, on top of the renamed autoRestore() and the Medic check. What it waits on is in docs/v4-plan.md, under "Workers, spells and a unit's orders": play on Windows, on a 4:3 and an ultrawide display and on a map that is not square; one player's "off" holding with two people playing; the location reserved on import. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01VEdp4YG8zxHxfqFjpBsycX --- compiler/generated/stdlib.ts | 5 +- ledger/units/card-right-click.ts | 16 ++ ledger/units/spells-auto-restore-toggle.ts | 4 + stdlib/card.ts | 245 +++++++++++++++++++++ stdlib/spells.ts | 27 ++- tests/stdlib.test.ts | 83 ++++++- 6 files changed, 372 insertions(+), 8 deletions(-) create mode 100644 ledger/units/card-right-click.ts create mode 100644 ledger/units/spells-auto-restore-toggle.ts create mode 100644 stdlib/card.ts diff --git a/compiler/generated/stdlib.ts b/compiler/generated/stdlib.ts index 045a765..f9d5f7e 100644 --- a/compiler/generated/stdlib.ts +++ b/compiler/generated/stdlib.ts @@ -1,5 +1,6 @@ // Generated by scripts/embed-stdlib.mts from stdlib/*.ts and *.tsx — do not edit. export const STDLIB_SOURCES: Readonly> = { + "card": "/**\n * A right-click on a command card button: the standard library's \"trigscript/card\" (TrigScript 4). Single-player maps.\n *\n * import { CardButton, cardSlots } from \"trigscript/card\";\n *\n * program(async () => {\n * const restore = new CardButton(techs.Restoration, cardSlots.Restoration);\n * await restore.begin();\n * let on = true;\n * while (true) {\n * if (await restore.rightClicked()) {\n * on = !on;\n * print(on ? \"On\" : \"Off\", { to: CurrentPlayer });\n * }\n * …\n * await nextFrame();\n * }\n * }, { owner: AllPlayers });\n *\n * The game keeps no record of which card button a right-click landed on, and a right-click on a button does nothing in\n * the game. So the button tests where the mouse was when the right button went down against where the button is drawn,\n * while the card shows the buttons of the units that have the ability (a Medic's for Restoration). Make it inside a\n * program run for the human player, and call rightClicked() once on every pass of the program's loop.\n *\n * Where a button is drawn depends on the graphics. Classic graphics draws the game 640 pixels wide; Remastered's draws it\n * as wide as the display's shape (`local.screen.width`), and that width reads the same in both. The card sits against the\n * right edge, so the button needs to know which graphics are on. At the start it centers the view on the middle of the\n * map, reads where the camera stopped (half the drawn width left of the middle) and sends the view back; after that, a\n * click on the minimap and the camera going further right than Remastered's can keep it current when the player switches\n * graphics mid-game. Until it knows, a press on either layout's place for the button counts.\n *\n * Played in StarCraft: Remastered (2026-11-12), on a Mac in Classic and Remastered graphics:\n * - Center View glides: the camera takes a few frames to arrive. A second Center View sent during the glide was lost,\n * and moving a location and centering the view on it in one frame while the camera already sat there hung the game.\n * - A Center View after an await in the same branch, or in a task beside the program, misbehaved (the view locked);\n * each step here is its own pass of the caller's loop, and the only awaits are sync()s followed by nothing but sums.\n * - The card's buttons: columns 132, 86 and 40 pixels in from the drawn width's right edge, rows at 352, 396 and 440,\n * each about 31 by 30, in both graphics.\n * - A minimap click lands the camera half the drawn width, and one minimap pixel's worth more, left of the spot clicked.\n */\nimport { sync, clicked, centerLocation, centerView, reserveLocation, print, memory, local, techs, CurrentPlayer, type Tech } from \"trigscript\";\n\n/** The nine places on the command card, left to right and top to bottom, and each ability's own place. */\nexport const cardSlots = {\n topLeft: 0,\n topMiddle: 1,\n topRight: 2,\n middleLeft: 3,\n center: 4,\n middleRight: 5,\n bottomLeft: 6,\n bottomMiddle: 7,\n bottomRight: 8,\n Restoration: 7,\n};\n\nconst VIEW = reserveLocation(\"card: where the camera is sent to measure the screen\");\n\nlet afterCalibrationHandler: (() => void) | undefined = undefined;\nlet showMeasurements = false;\n\n/**\n * At the top of the script, before any CardButton: `afterCalibration` runs once the start's measurement is done, in\n * place of sending the view back — where an opening cinematic starts. `showMeasurements` prints each step of it, and each\n * right-click the button tests, as numbered lines.\n */\nexport function cardSetup(options: { afterCalibration?: () => void; showMeasurements?: boolean } = {}) {\n afterCalibrationHandler = options.afterCalibration;\n showMeasurements = options.showMeasurements === true;\n}\n\n/** One button of the command card, watched for a right-click. */\nexport class CardButton {\n /** The button set the card shows for the units that have the ability: a Medic's for Restoration. */\n buttonSet = 0xffff;\n /** How far in from the drawn width's right edge the button's column starts, and the top of its row. */\n fromRight = 132;\n top = 352;\n mapWidth = 0;\n mapHeight = 0;\n middleX = 0;\n middleY = 0;\n /** 0 the location is moved, 1 the view is sent, 2 waiting for it to settle, 3 read, 4 the location goes back, 5 the view goes back, 6 done. */\n phase = 6;\n passes = 0;\n startX = 0;\n startY = 0;\n settledX = 0;\n settledY = 0;\n /** Kept on this computer only: equal camera readings in a row while it settles, and the reading a pass ago. */\n steady = 0;\n lastX = 0;\n lastY = 0;\n /** Kept on this computer only: 0 not known, 1 Classic graphics, 2 Remastered's. */\n graphics = 0;\n pressed = 0;\n wasDown = 0;\n lastCameraX = 0;\n line = 0;\n\n constructor(tech: Tech, slot: number) {\n if (tech == techs.Restoration) this.buttonSet = 34;\n const column = slot % 3;\n if (column == 1) this.fromRight = 86;\n if (column == 2) this.fromRight = 40;\n if (slot >= 3) this.top = 396;\n if (slot >= 6) this.top = 440;\n }\n\n /** Before the program's loop: where the view starts, sent to every computer, so the view can be sent back there. */\n async begin() {\n this.mapWidth = memory.map.width * 32;\n this.mapHeight = memory.map.height * 32;\n this.middleX = (this.mapWidth - this.mapWidth % 64) / 2;\n this.middleY = (this.mapHeight - this.mapHeight % 64) / 2;\n const [x, y] = await sync(CurrentPlayer, local.camera.x, local.camera.y);\n this.startX = x;\n this.startY = y;\n this.lastCameraX = x;\n if (showMeasurements) {\n this.line += 1;\n print(`#${this.line} card: the view starts at ${x},${y}; the map is ${this.mapWidth} by ${this.mapHeight} pixels`);\n }\n // A map narrower or shorter than this would stop the camera before it is centered, and the reading would mean nothing.\n if (this.mapWidth >= 1024 && this.mapHeight >= 1024) this.phase = 0;\n else {\n if (showMeasurements) {\n this.line += 1;\n print(`#${this.line} card: the map is too small to measure the screen; both layouts count`);\n }\n const after = afterCalibrationHandler;\n if (after) after();\n }\n }\n\n /** On every pass of the program's loop: whether the player right-clicked the button since the last pass that said so. */\n async rightClicked(): Promise {\n const cameraX = local.camera.x;\n const cameraY = local.camera.y;\n if (this.phase == 0) {\n centerLocation(VIEW, this.middleX, this.middleY);\n this.phase = 1;\n } else if (this.phase == 1) {\n centerView(VIEW);\n this.phase = 2;\n this.passes = 0;\n this.steady = 0;\n this.lastX = cameraX;\n this.lastY = cameraY;\n } else if (this.phase == 2) {\n this.passes += 1;\n if (cameraX == this.lastX && cameraY == this.lastY) this.steady += 1;\n else this.steady = 0;\n this.lastX = cameraX;\n this.lastY = cameraY;\n if (this.passes >= 16) this.phase = 3;\n }\n if (this.phase == 3) {\n const [x, y] = await sync(CurrentPlayer, local.camera.x, local.camera.y);\n this.settledX = x;\n this.settledY = y;\n this.phase = 4;\n } else if (this.phase == 4) {\n if (this.steady >= 2 && this.settledX + 296 <= this.middleX && this.settledX + 344 >= this.middleX) this.graphics = 1;\n if (this.steady >= 2 && this.settledX + 352 <= this.middleX && this.settledX + 460 >= this.middleX) this.graphics = 2;\n if (showMeasurements) {\n this.line += 1;\n print(`#${this.line} card: centered on the middle, the camera settled at ${this.settledX},${this.settledY} (${this.steady} equal readings)`);\n this.line += 1;\n if (this.graphics == 1) print(`#${this.line} card: Classic graphics, the camera ${this.middleX - this.settledX} left of the middle`);\n else if (this.graphics == 2) print(`#${this.line} card: Remastered graphics, the camera ${this.middleX - this.settledX} left of the middle`);\n else print(`#${this.line} card: graphics not known; both layouts count`);\n }\n const after = afterCalibrationHandler;\n if (after) {\n after();\n this.phase = 6;\n } else if (this.settledX != this.startX || this.settledY != this.startY) {\n centerLocation(VIEW, this.startX + this.middleX - this.settledX, this.startY + this.middleY - this.settledY);\n this.phase = 5;\n } else this.phase = 6;\n } else if (this.phase == 5) {\n centerView(VIEW);\n this.phase = 6;\n if (showMeasurements) {\n this.line += 1;\n print(`#${this.line} card: the view is sent back`);\n }\n }\n\n const width = local.screen.width;\n const mouseX = local.mouse.x;\n const mouseY = local.mouse.y;\n const buttons = local.mouse.buttons;\n if (this.phase == 6) {\n const before = this.graphics;\n // A minimap click or drag (a square map's minimap is 128 pixels across, from 6,348): the camera jumps to the spot\n // under the cursor, half the drawn width and a minimap pixel left of it.\n const scale = this.mapWidth / 128;\n if (this.mapWidth == this.mapHeight && buttons % 4 >= 2 && mouseX >= 6 && mouseX <= 133 && mouseY >= 348 && mouseY <= 475 && (cameraX > this.lastCameraX + 96 || this.lastCameraX > cameraX + 96) && cameraX > 0) {\n const spot = (mouseX - 6) * scale;\n if (spot >= cameraX + scale + 296 && spot <= cameraX + scale + 344) this.graphics = 1;\n if (spot >= cameraX + scale + 352 && spot <= cameraX + scale + 460) this.graphics = 2;\n }\n // Remastered's camera stops a drawn width short of the map's right edge; only Classic's gets further.\n if (width > 648 && cameraX + width > this.mapWidth) this.graphics = 1;\n if (showMeasurements && this.graphics != before) {\n this.line += 1;\n if (this.graphics == 1) print(`#${this.line} card: now Classic graphics, the camera at ${cameraX},${cameraY}`);\n else print(`#${this.line} card: now Remastered graphics, the camera at ${cameraX},${cameraY}`);\n }\n }\n this.lastCameraX = cameraX;\n\n const down = buttons % 16 >= 8 ? 1 : 0;\n if (down == 1 && this.wasDown == 0) {\n const inRow = mouseY >= this.top && mouseY < this.top + 30;\n const inClassic = mouseX + this.fromRight >= 640 && mouseX + this.fromRight < 640 + 31;\n const inRemastered = mouseX + this.fromRight >= width && mouseX + this.fromRight < width + 31;\n let inSlot = false;\n if (this.graphics == 1) inSlot = inClassic;\n else if (this.graphics == 2) inSlot = inRemastered;\n else inSlot = inClassic || inRemastered;\n const shown = local.card.buttonSet;\n this.pressed = shown == this.buttonSet && inRow && inSlot ? 1 : 0;\n if (showMeasurements) {\n this.line += 1;\n const verdict = this.pressed == 1 ? \"on the button\" : \"not on the button\";\n if (shown == 34) print(`#${this.line} card: right button down at ${mouseX},${mouseY}, Medic buttons (34): ${verdict}`);\n else if (shown == 0) print(`#${this.line} card: right button down at ${mouseX},${mouseY}, Marine buttons (0): ${verdict}`);\n else if (shown == 244) print(`#${this.line} card: right button down at ${mouseX},${mouseY}, mixed group buttons (244): ${verdict}`);\n else if (shown == 229) print(`#${this.line} card: right button down at ${mouseX},${mouseY}, Cancel, a targeting cursor is up (229): ${verdict}`);\n else print(`#${this.line} card: right button down at ${mouseX},${mouseY}, button set (${shown}): ${verdict}`);\n }\n }\n this.wasDown = down;\n\n if (clicked(CurrentPlayer, \"right\")) {\n const onButton = await sync(CurrentPlayer, this.pressed);\n this.pressed = 0;\n return onButton == 1;\n }\n return false;\n }\n}\n", "cutscene": "/**\n * Scenes acted out in the game: the standard library's \"trigscript/cutscene\" (TrigScript 4).\n *\n * import { Camera, walk, line, play, SECOND } from \"trigscript/cutscene\";\n *\n * async function intro(raider: Unit) {\n * const cam = new Camera(raider.x, raider.y);\n * await line(units.JimRaynorMarine, \"Something's moving out there.\", 3 * SECOND);\n * const there = start(walk(raider, 1200, 640)); // the walk goes on while the camera follows\n * await cam.follow(raider, 2 * SECOND);\n * await there;\n * await cam.pan(1600, 400, 2 * SECOND);\n * await cam.shake(12);\n * }\n *\n * program(async () => {\n * const raider = first(unitsAt(locations.Base, { owner: P1, type: units.TerranMarine }));\n * if (!raider) return;\n * const skipped = await play(P1, start(intro(raider))); // Escape skips the rest of it\n * if (skipped) print(\"Skipped.\");\n * });\n *\n * Everything here is what a trigger can do, done a frame at a time: the camera is Center View on a location the library\n * keeps for itself, moved to where the camera should look; a unit walks by a Move order to another; a line is the\n * speaker's portrait talking and the words in the middle of the screen. Places are pixels (32 a tile), (0, 0) the map's\n * top left; time is frames of the game, 24 a second on Fastest (`SECOND`). The camera is the one of the player the\n * program runs as, as Center View's is.\n */\nimport { centerLocation, centerView, reserveLocation, talkingPortrait, print, sleep, frames, nextFrame, keyPressed, type Player, type Task, type Unit, type UnitType } from \"trigscript\";\nimport { easeInOutQuad, easeInQuad, easeOutQuad, easeOutBack } from \"trigscript/math\";\n\n/** Frames a second of game time on Fastest, the speed a map is played at. */\nexport const SECOND = 24;\n\nconst VIEW = reserveLocation(\"cutscene: where the camera looks\");\nconst GOAL = reserveLocation(\"cutscene: where a unit is sent\");\n\n/** How a glide speeds up and slows down: evenly, starting slow, ending slow, both, or ending past the mark and back. */\nexport type Ease = \"linear\" | \"in\" | \"out\" | \"inOut\" | \"back\";\n\n/** How far along a glide is at `t` (0 at its start, 1 at its end), as `ease` has it. */\nfunction eased(t: fixed, ease: Ease): fixed {\n if (ease === \"in\") return easeInQuad(t);\n if (ease === \"out\") return easeOutQuad(t);\n if (ease === \"inOut\") return easeInOutQuad(t);\n if (ease === \"back\") return easeOutBack(t);\n return t;\n}\n\n/**\n * The camera of the player the program runs as: where it looks, and how it gets somewhere else. It looks at its first\n * place as soon as it is made.\n */\nexport class Camera {\n /** The point the camera is centred on, in pixels. */\n x: number;\n y: number;\n\n constructor(x: number, y: number) {\n this.x = x;\n this.y = y;\n centerLocation(VIEW, x, y);\n centerView(VIEW);\n }\n\n /** Look at a point, now. */\n look(x: number, y: number) {\n this.x = x;\n this.y = y;\n centerLocation(VIEW, x, y);\n centerView(VIEW);\n }\n\n /**\n * Glide to a point over `time` frames, looking at each place on the way a frame at a time.\n * @param ease How it speeds up and slows down; \"inOut\" unless it says.\n */\n async pan(x: number, y: number, time: number, ease: Ease = \"inOut\") {\n const fromX = this.x;\n const fromY = this.y;\n let i = 1;\n while (i <= time) {\n const k = eased(fixed(i) / time, ease);\n this.look(fromX + Math.round((x - fromX) * k), fromY + Math.round((y - fromY) * k));\n await nextFrame();\n i++;\n }\n this.look(x, y);\n }\n\n /** Keep a unit in the middle of the screen for `time` frames, or until it is gone. */\n async follow(u: Unit, time: number) {\n let i = 0;\n while (i < time && u) {\n this.look(u.x, u.y);\n await nextFrame();\n i++;\n }\n }\n\n /** Shake for `time` frames, `size` pixels each way (8 unless it says), and settle where it was. */\n async shake(time: number, size: number = 8) {\n const x = this.x;\n const y = this.y;\n let i = 0;\n while (i < time) {\n const left = (i % 4) < 2;\n const up = (i % 2) === 0;\n centerLocation(VIEW, left ? x - size : x + size, up ? y - size : y + size);\n centerView(VIEW);\n await nextFrame();\n i++;\n }\n this.look(x, y);\n }\n}\n\n/**\n * Send a unit to a point by a Move order, and wait until it is there — within `near` pixels (48 unless it says) — or\n * `limit` frames have passed (30 seconds unless it says), or it is gone.\n * @returns Whether it got there.\n */\nexport async function walk(u: Unit, x: number, y: number, near: number = 48, limit: number = 30 * SECOND): Promise {\n return go(u, x, y, \"move\", near, limit);\n}\n\n/** The same with an Attack order: the unit fights what it meets on the way. */\nexport async function charge(u: Unit, x: number, y: number, near: number = 48, limit: number = 30 * SECOND): Promise {\n return go(u, x, y, \"attack\", near, limit);\n}\n\nasync function go(u: Unit, x: number, y: number, how: \"move\" | \"attack\", near: number, limit: number): Promise {\n centerLocation(GOAL, x, y);\n u.order(how, GOAL);\n let waited = 0;\n while (u && Math.abs(u.x - x) + Math.abs(u.y - y) > near && waited < limit) {\n await nextFrame();\n waited++;\n }\n return !!u && Math.abs(u.x - x) + Math.abs(u.y - y) <= near;\n}\n\n/**\n * Send every unit of a group to a point together, and wait until each is there, gone, or `limit` frames have passed.\n * @returns How many got there.\n */\nexport async function walkAll(group: Unit[], x: number, y: number, near: number = 64, limit: number = 30 * SECOND): Promise {\n centerLocation(GOAL, x, y);\n for (const u of group) if (u) u.order(\"move\", GOAL);\n let waited = 0;\n let there = 0;\n while (waited < limit) {\n there = 0;\n let moving = 0;\n for (const u of group) {\n if (!u) continue;\n if (Math.abs(u.x - x) + Math.abs(u.y - y) <= near) there++;\n else moving++;\n }\n if (moving === 0) break;\n await nextFrame();\n waited++;\n }\n return there;\n}\n\n/**\n * A line of the scene: the speaker's portrait talks and the words show in the middle of the screen, for `time` frames\n * (3 seconds unless it says), and then the scene goes on.\n */\nexport async function line(speaker: UnitType, text: string, time: number = 3 * SECOND) {\n talkingPortrait(speaker, Math.round((time * 1000) / SECOND));\n print(text, { position: \"center\" });\n await sleep(frames(time));\n}\n\n/**\n * Play a scene, started as a task (`start(intro())`), for a player who may skip it: when they press `key` (Escape unless\n * it says) the scene stops where it is. Put things where the scene would have left them when it gives true.\n * @returns Whether it was skipped.\n */\nexport async function play(player: Player, scene: Task, key: \"Escape\" | \"Space\" | \"Enter\" = \"Escape\"): Promise {\n while (scene.running) {\n if (keyPressed(player, key)) {\n scene.cancel();\n return true;\n }\n await nextFrame();\n }\n return false;\n}\n", "fog": "/**\n * Fog of war as a picture: the standard library's \"trigscript/fog\" (TrigScript 4).\n *\n * import { fog } from \"trigscript/fog\";\n *\n * const heart = fog.picture([\".##.##.\", \"#######\", \".#####.\", \"..###..\"]);\n *\n * program(async () => {\n * fog.reveal(P1); // the whole map lit once: from now on dark is fog, not black\n * while (true) {\n * fog.light(P1, 30, 20, 5); // a disc of size 5 around tile (30, 20), this frame only\n * fog.light(P1, 60, 40, heart); // a picture, its top left on tile (60, 40)\n * await nextFrame();\n * }\n * });\n *\n * The fog is a canvas that starts every frame dark: what a program lights this frame is what shows this frame, and\n * nothing else needs doing. There is no dark brush — the game's sight only ever reveals — so dark is whatever is not\n * lit; to draw dark shapes, light around them (a picture with `{ ink: \"dark\" }`). Dark is also the sharper ink: a lit\n * part is at least a brush wide (3 tiles across, 4 down), while a dark line one tile wide stays exactly that. Nothing\n * else needs doing — the library writes the game's vision countdown after every frame (memory.vision.countdown), so\n * the next frame starts from a wiped board. Watch it on the minimap. With Classic graphics (Options > Graphics) a tile\n * of the map is a pixel there, sharp; Remastered graphics blurs it a few tiles wide.\n *\n * What the board is made of decides how far apart lit and fog look. Remastered draws a tile on the minimap as the\n * average of its art and fog as 0.64 of lit, whatever the tile, so the brightest art is the best board there: plain\n * Ice snow (lit 130 against fog 82, measured 2026-10-19). Classic draws a tile as one colour of its own; Badlands\n * tile 23778 is the widest apart there, and dim and textured in Remastered. Snow played the better of the two.\n *\n * How it draws, played in StarCraft: Remastered (2026-10-17): a brush is a unit made on a tile and taken away in the\n * same trigger pass; the tiles its sight revealed stay lit through the frame. A brush's size is the unit's sight\n * range, so each size is a unit type of its own — the flying heroes below, which no ordinary map places — whose sight\n * is set every frame (Remastered takes a sight write a frame late, so one type cannot change size within a frame).\n * Coordinates are tiles, (0, 0) the map's top left. About 800 brushes a frame is the most the game makes: it keeps a\n * removed unit's place until the next frame.\n */\nimport { game, program, sleep, frames, stats, createUnit, removeUnit, centerLocation, reserveLocation, memory, cover, nextFrame, type Player, type UnitType, type Video } from \"trigscript\";\n\n/**\n * Each brush's shape, sizes 1 to 11: the tiles a flying unit of that sight range reveals, read out of the game.\n * \"@\" is the tile the brush is centred on. The game's air sight leaves the centre's row one short below, so every\n * shape reaches a row further up than down.\n */\nexport const SHAPES: readonly (readonly string[])[] = [\n // 1\n [\"###\", \"###\", \"#@#\", \"###\"],\n // 2\n [\".###.\", \"#####\", \"#####\", \"##@##\", \"#####\", \"#####\"],\n // 3\n [\"..###..\", \"#######\", \"#######\", \"#######\", \"###@###\", \"#######\", \"#######\", \"#######\"],\n // 4\n [\"..#####...\", \".#######..\", \"#########.\", \"##########\", \"##########\", \"####@#####\", \"##########\", \"##########\", \"#########.\", \".#######..\", \"......#...\"],\n // 5\n [\"...#####....\", \"..#######...\", \".#########..\", \"###########.\", \"############\", \"############\", \"#####@######\", \"############\", \"############\", \"###########.\", \".#########..\", \"..#######...\", \".......#....\"],\n // 6\n [\"....#####.....\", \"..#########...\", \".###########..\", \"#############.\", \"#############.\", \"##############\", \"##############\", \"######@#######\", \"##############\", \"##############\", \"#############.\", \"#############.\", \".###########..\", \"..#########...\", \"........#.....\"],\n // 7\n [\".....#####......\", \"...#########....\", \"..###########...\", \".#############..\", \"###############.\", \"###############.\", \"################\", \"################\", \"#######@########\", \"################\", \"################\", \"###############.\", \"###############.\", \".#############..\", \"..###########...\", \"...#########....\", \".........#......\"],\n // 8\n [\"......#####.......\", \"....#########.....\", \"..#############...\", \".###############..\", \".###############..\", \"#################.\", \"#################.\", \"##################\", \"##################\", \"########@#########\", \"##################\", \"##################\", \"#################.\", \"#################.\", \".###############..\", \".###############..\", \"..#############...\", \"....#########.....\", \"..........#.......\"],\n // 9\n [\"......#######.......\", \"....###########.....\", \"...#############....\", \"..###############...\", \".#################..\", \"###################.\", \"###################.\", \"####################\", \"####################\", \"####################\", \"#########@##########\", \"####################\", \"####################\", \"####################\", \"###################.\", \"###################.\", \".#################..\", \"..###############...\", \"...#############....\", \"....###########.....\", \"........#####.......\"],\n // 10\n [\".......#######........\", \".....###########......\", \"....#############.....\", \"..#################...\", \"..#################...\", \".###################..\", \"#####################.\", \"#####################.\", \"######################\", \"######################\", \"######################\", \"##########@###########\", \"######################\", \"######################\", \"######################\", \"#####################.\", \"#####################.\", \".###################..\", \"..#################...\", \"..#################...\", \"....#############.....\", \".....###########......\", \".........#####........\"],\n // 11\n [\"........#######.........\", \"......###########.......\", \"....###############.....\", \"...#################....\", \"..###################...\", \".#####################..\", \".#####################..\", \"#######################.\", \"#######################.\", \"########################\", \"########################\", \"########################\", \"###########@############\", \"########################\", \"########################\", \"########################\", \"#######################.\", \"#######################.\", \".#####################..\", \".#####################..\", \"..###################...\", \"...#################....\", \"....###############.....\", \"......###########.......\", \"..........#####.........\"],\n];\n\n/** Each size's unit, size 1 first: flying heroes without a sight upgrade, by the game's own ids (the same under a mod's names). A map that places one gets odd sight from it. */\nconst BRUSHES: readonly UnitType[] = [\n 55 /* Kukulza (Mutalisk) */, 56 /* Kukulza (Guardian) */, 57 /* Yggdrasill (Overlord) */, 22 /* Magellan (Science Vessel) */,\n 82 /* Gantrithor (Carrier) */, 28 /* Hyperion (Battlecruiser) */, 29 /* Norad II (Battlecruiser) */, 21 /* Tom Kazansky (Wraith) */,\n 86 /* Danimoth (Arbiter) */, 80 /* Mojo (Scout) */, 88 /* Artanis (Scout) */,\n] as UnitType[];\n\n/** Where each brush is made: a location the map does not use, taken when the script is built. */\nconst SPOT = reserveLocation(\"fog: where each brush is made\");\n\n/**\n * One disc: the tiles a brush of `size` covers (SHAPES), centred on tile (x, y). What light() does with a size. A brush\n * is a unit made where SPOT is and taken away again at once; each branch makes its own size's, so that the unit type\n * of the action is known when the script is built (one given by the program costs writing it into the action).\n */\nconst disc = game((player: Player, x: number, y: number, size: number) => {\n discPx(player, x * 32 + 16, y * 32 + 16, size);\n});\n\n/**\n * disc() in pixels, (px, py) the centre of the tile: what a caller with many brushes uses, its products by 32 worked\n * out once and the brushes' offsets added (a product by 32 of a number the game holds is a test a bit, about 0.4 µs).\n */\nconst discPx = game((player: Player, px: number, py: number, size: number) => {\n centerLocation(SPOT, px, py, 32, 32);\n switch (size) {\n case 0: case 1: createUnit(player, BRUSHES[0], 1, SPOT); removeUnit(player, BRUSHES[0]); break;\n case 2: createUnit(player, BRUSHES[1], 1, SPOT); removeUnit(player, BRUSHES[1]); break;\n case 3: createUnit(player, BRUSHES[2], 1, SPOT); removeUnit(player, BRUSHES[2]); break;\n case 4: createUnit(player, BRUSHES[3], 1, SPOT); removeUnit(player, BRUSHES[3]); break;\n case 5: createUnit(player, BRUSHES[4], 1, SPOT); removeUnit(player, BRUSHES[4]); break;\n case 6: createUnit(player, BRUSHES[5], 1, SPOT); removeUnit(player, BRUSHES[5]); break;\n case 7: createUnit(player, BRUSHES[6], 1, SPOT); removeUnit(player, BRUSHES[6]); break;\n case 8: createUnit(player, BRUSHES[7], 1, SPOT); removeUnit(player, BRUSHES[7]); break;\n case 9: createUnit(player, BRUSHES[8], 1, SPOT); removeUnit(player, BRUSHES[8]); break;\n case 10: createUnit(player, BRUSHES[9], 1, SPOT); removeUnit(player, BRUSHES[9]); break;\n default: createUnit(player, BRUSHES[10], 1, SPOT); removeUnit(player, BRUSHES[10]);\n }\n});\n\n/**\n * Light the whole map once, so that from now on a tile left dark shows as fog (grey) rather than as never seen\n * (black) — every picture then has the same two tones. Takes one frame.\n * @param player Whose fog: P1 … P8, known when the script is built.\n */\nexport const reveal = game((player: Player) => {\n const width = memory.map.width;\n const height = memory.map.height;\n // Size 11 is 23 tiles wide: a brush every 14 tiles leaves no gap, and one more past the end, kept inside, reaches the edge.\n for (let y = 2; y < height + 12; y += 14) {\n for (let x = 2; x < width + 12; x += 14) disc(player, Math.min(x, width - 3), Math.min(y, height - 3), 11);\n }\n});\n\n/**\n * Light a rectangle of tiles for this frame, worked out while the game runs: its top left on tile (x, y), `width`\n * tiles across and `height` down. Every one of the five may change from frame to frame — the walls of a raycaster, a\n * bar of a chart. It is exact from 3 × 4 up (the smallest brush); a thinner one is lit a brush wide, centred.\n *\n * fog.rect(P1, 10, 20, 3, wall); // a column 3 tiles wide, `wall` tall\n *\n * @param player Whose fog: P1 … P8, known when the script is built.\n * @param x The column of its left edge.\n * @param y The row of its top edge.\n * @param width How many tiles across.\n * @param height How many tiles down.\n */\nexport const rect = game((player: Player, x: number, y: number, width: number, height: number) => {\n if (width < 1 || height < 1) return;\n // Size 1 is 3 tiles across and 4 down, its centre the middle column and the third row: a grid of them, the last\n // column and row moved back to end on the edge (overlapping the one before), covers the rectangle exactly.\n const right = Math.max(x + width - 2, x + 1);\n const bottom = Math.max(y + height - 2, y + 2);\n // (The brush made here, not through disc(): its unit type is then known when the script is built, which makes the\n // action a constant — a size from the program costs a branch and a written field a brush.)\n let cy = y + 2;\n while (true) {\n let cx = x + 1;\n while (true) {\n centerLocation(SPOT, cx * 32 + 16, cy * 32 + 16, 32, 32);\n createUnit(player, BRUSHES[0], 1, SPOT);\n removeUnit(player, BRUSHES[0]);\n if (cx >= right) break;\n cx = Math.min(cx + 3, right);\n }\n if (cy >= bottom) break;\n cy = Math.min(cy + 4, bottom);\n }\n});\n\n/** A picture for the fog, made when the script is built: its size in tiles and the brushes that draw it. */\nexport interface Picture {\n readonly width: number;\n readonly height: number;\n readonly brushes: readonly { readonly x: number; readonly y: number; readonly size: number }[];\n}\n\n/**\n * A picture from rows of text, worked out when the script is built: `#` is the ink, anything else is not.\n *\n * fog.picture([\"..##..\", \".####.\", \"######\"]) // light ink: the # tiles are lit\n * fog.picture([\"..##..\", \".####.\", \"######\"], { ink: \"dark\" }) // dark ink: the # tiles stay dark, the rest is lit\n *\n * With light ink a lit part thinner than a brush (3 tiles across, 4 down) is drawn a tile or two thicker. An image's\n * halftone (`fog.picture(photo.halftone(6))`) is drawn with the brushes it was made of, one a cell. With dark\n * ink every tile of the picture's rectangle that is not `#` is lit and the `#` tiles keep their shape exactly,\n * however thin — the ink for line art and silhouettes.\n */\nexport function picture(rows: readonly string[], options: { readonly ink?: \"light\" | \"dark\" } = {}): Picture {\n const height = rows.length;\n const width = Math.max(0, ...rows.map((r) => r.length));\n // An image's halftone (image.halftone(6)) comes with the brushes that drew it: a brush a cell, as they are.\n const drawn = (rows as { readonly brushes?: readonly { readonly x: number; readonly y: number; readonly shape: number }[] }).brushes;\n if (drawn && options.ink !== \"dark\") return Object.freeze({ width, height, brushes: Object.freeze(drawn.map((b) => ({ x: b.x, y: b.y, size: b.shape + 1 }))) });\n // Dark ink: the picture's rectangle lit but for the # tiles, so what is covered is the rest.\n const ink = options.ink === \"dark\" ? Array.from({ length: height }, (_, y) => Array.from({ length: width }, (_, x) => (rows[y][x] === \"#\" ? \".\" : \"#\")).join(\"\")) : rows;\n const brushes = cover(ink, SHAPES).map((b) => ({ x: b.x, y: b.y, size: b.shape + 1 }));\n return Object.freeze({ width, height, brushes: Object.freeze(brushes) });\n}\n\n/**\n * A video's frames as brushes, worked out when the script is built: a word a brush (x | y << 8 | size << 16), the\n * frames one after another, and where each one starts — the tables the game reads a frame from. Once a build.\n */\ninterface Film { readonly words: readonly number[]; readonly starts: readonly number[] }\nconst films = new Map();\nfunction film(video: Video): Film {\n const had = films.get(video);\n if (had) return had;\n const words: number[] = [], starts: number[] = [0];\n for (const brushes of cover(video, SHAPES)) {\n for (const b of brushes) words.push(b.x | (b.y << 8) | ((b.shape + 1) << 16));\n starts.push(words.length);\n }\n const made = { words, starts };\n films.set(video, made);\n return made;\n}\n\n/**\n * Light the fog for this frame: a disc, a picture or a frame of a video. Tiles count from the map's top left, (0, 0).\n *\n * fog.light(P1, 30, 20, 5); // a disc of size 5 centred on tile (30, 20)\n * fog.light(P1, 60, 40, heart); // a picture (fog.picture), its top left on tile (60, 40)\n * fog.light(P1, 0, 0, badApple, f); // frame f of a video the script imports, its top left on tile (0, 0)\n *\n * @param player Whose fog: P1 … P8, known when the script is built.\n * @param x The tile's column: a disc's centre, a picture's left edge.\n * @param y The tile's row: a disc's centre, a picture's top edge.\n * @param shape A size, 1 (3 tiles wide) to 11 (23 wide) — it may change while the game runs — a picture, or a video.\n * @param frame Of a video, which frame, from 0: it may change while the game runs. Past the end, nothing is lit.\n */\nexport const light = game((player: Player, x: number, y: number, shape: number | Picture | Video, frame: number = 0) => {\n if (typeof shape === \"number\") disc(player, x, y, shape);\n else {\n // The picture's corner in pixels once; each brush is its offset from there (a constant of a picture).\n const px = x * 32 + 16, py = y * 32 + 16;\n if (\"brushes\" in shape) for (const b of shape.brushes) discPx(player, px + b.x * 32, py + b.y * 32, b.size);\n else {\n const { words, starts } = film(shape);\n const end = starts[frame + 1];\n for (let i = starts[frame]; i < end; i++) {\n const w = words[i];\n // (A row's offset in pixels straight from its byte: the same as ((w >> 8) & 255) * 32, and the shift only has\n // the byte's eight bits to look at.)\n discPx(player, px + (w & 255) * 32, py + ((w & 0xff00) >> 3), w >> 16);\n }\n }\n }\n});\n\n/**\n * Play a video in the fog, a frame of it every frame of the game, from its first to its last; resolves when it ends.\n * Inside an async program: `await fog.play(P1, badApple)`. On Fastest a game frame is 42 ms, which is what Import\n * Video makes a video's frames by default; the game runs a little slower when a frame is heavy, and so does the video.\n *\n * @param player Whose fog: P1 … P8, known when the script is built.\n * @param video A video the script imports.\n * @param x The column of its left edge, 0 by default.\n * @param y The row of its top edge, 0 by default.\n */\nexport const play = game(async (player: Player, video: Video, x: number = 0, y: number = 0) => {\n // (A while: a for over a length known when the script is built would be unrolled, a copy of the body a frame.)\n let f = 0;\n while (f < video.length) {\n light(player, x, y, video, f);\n await nextFrame();\n f++;\n }\n});\n\n/**\n * How fog.columns() lays its columns out, known when the script is built: `count` columns side by side, each `width`\n * tiles across (3, a brush's width, by default) and up to `height` tiles tall, centred on the middle row. `ink`:\n * \"light\" lights each column (the walls of a raycaster bright, on fog), \"dark\" lights all but the columns (dark walls\n * between a lit ceiling and floor); dark is cheaper when columns fill most of the board, light when they are short.\n */\nexport interface ColumnLayout {\n readonly count: number;\n readonly height: number;\n readonly width?: number;\n readonly ink?: \"light\" | \"dark\";\n /**\n * How the walls are told apart, with fog.columns()'s `faces`. \"outline\", the default: every wall drawn as its edges —\n * its top and bottom, a brush deep, and a column lit whole wherever one face meets another (on the nearer of the\n * two) — so that blocks read as boxes, the look of the raycasters of monochrome screens; with dark ink, the edges are\n * lit lines through dark walls. A few brushes a column. Chosen over the rest when played (2026-10-17).\n *\n * The others shade the faces whose number is odd (a raycaster's other side) and leave the rest whole: \"hollow\" —\n * dark, lit only along its top and bottom edges; \"ribbed\" — hollow and lit across the middle of each half; \"panelled\"\n * — hollow and lit across its quarters; \"line\" / \"lines\" — dark lines a tile thick across it at its middle or its\n * quarters (the finest, and two dozen brushes a column near the eye: for a view with room to spare).\n */\n readonly shading?: \"outline\" | \"lines\" | \"line\" | \"hollow\" | \"ribbed\" | \"panelled\";\n}\n\n/**\n * What fog.columns() reads, worked out once a layout when the script is built: for each level of the drawing that is\n * wider than a column (all the columns, a group of them, the last group) and each depth from 0 to half the height, the\n * brushes that cover a rectangle that wide and that deep exactly — the encoder's covers (cover()), a few large brushes\n * in the middle and small ones at the edges. A brush is a word, dx | dy << 8 | size << 16 from the rectangle's top\n * left; `starts` says where each (level, depth) begins.\n */\ninterface ColumnPlan { readonly words: readonly number[]; readonly starts: readonly number[]; readonly group: number; readonly half: number }\nconst columnPlans = new Map();\nconst COLUMN_GROUP = 6;\nfunction columnPlan(layout: ColumnLayout): ColumnPlan {\n const width = layout.width ?? 3, half = layout.height >> 1, group = Math.min(COLUMN_GROUP, layout.count);\n const key = `${layout.count}:${width}:${half}`;\n const had = columnPlans.get(key);\n if (had) return had;\n const words: number[] = [], starts: number[] = [];\n for (const columns of [layout.count, group, layout.count % group || group]) {\n for (let depth = 0; depth <= half; depth++) {\n starts.push(words.length);\n if (depth === 0) continue;\n // A rectangle shallower than a brush is drawn a brush deep, the rest of it on what the level before lit.\n const rows = Array.from({ length: Math.max(depth, 4) }, () => \"#\".repeat(columns * width));\n for (const b of cover(rows, SHAPES)) words.push(b.x | (b.y << 8) | ((b.shape + 1) << 16));\n }\n }\n starts.push(words.length);\n const made = { words, starts, group, half };\n columnPlans.set(key, made);\n return made;\n}\n\n/**\n * One rectangle of fog.columns() wider than a column: the cover at `at` (a level and a depth) drawn twice, the half\n * above the middle and the half below, its top rows `top` and `bottom`, from column `x`.\n */\nconst columnRect = game((player: Player, words: readonly number[], starts: readonly number[], at: number, x: number, top: number, bottom: number) => {\n let i = starts[at];\n const end = starts[at + 1];\n // In pixels once; each brush is its offset (a few bits, a few tests) from there.\n const px = x * 32 + 16, pt = top * 32 + 16, pb = bottom * 32 + 16;\n while (i < end) {\n const w = words[i];\n const dx = (w & 255) * 32, dy = (w & 0xff00) >> 3, size = w >> 16;\n discPx(player, px + dx, pt + dy, size);\n discPx(player, px + dx, pb + dy, size);\n i++;\n }\n});\n\n/**\n * One column's part of fog.columns(), in pixels: brushes (3 × 4) a column of 4 rows apart and the last moved back to\n * end on the edge, twice — from `pt` and from `pb`, the centres of the first brush of each — down a column `k32`\n * pixels deep (32 a row; under 4 rows, a brush deep), centred on `px`. What the smallest brush covers exactly, worked out\n * here rather than read: it is the most common rectangle by far. In pixels because the caller has them, stepped: a\n * product by 32 of a number the game holds is a test a bit, about 0.4 µs, where a step is an add.\n */\nconst columnBarPx = game((player: Player, px: number, pt: number, pb: number, k32: number) => {\n // How far down the last brush is from the first.\n const end = Math.max(k32, 128) - 128;\n let r = 0;\n while (true) {\n centerLocation(SPOT, px, pt, 32, 32);\n createUnit(player, BRUSHES[0], 1, SPOT);\n removeUnit(player, BRUSHES[0]);\n centerLocation(SPOT, px, pb, 32, 32);\n createUnit(player, BRUSHES[0], 1, SPOT);\n removeUnit(player, BRUSHES[0]);\n if (r >= end) break;\n const step = Math.min(end - r, 128);\n r += step;\n pt += step;\n pb += step;\n }\n});\n\n/** columnBarPx() in tiles: `k` rows from row `top` down and from row `bottom` down, centred on column `x`. */\nconst columnBar = game((player: Player, x: number, top: number, bottom: number, k: number) => {\n columnBarPx(player, x * 32 + 16, top * 32 + 80, bottom * 32 + 80, k * 32);\n});\n\n/**\n * A shaded column of fog.columns() (ColumnLayout's `shading`): `a` is half its height, `lines` how many lines across\n * each half — 0 hollow, 1 at its middle, 3 at its quarters. A line is a dark row through a lit column (light ink), or\n * a lit band a brush deep through a dark one; a column too short for its lines is drawn plain.\n */\nconst columnShaded = game((player: Player, cx: number, mid: number, a: number, dark: boolean, lines: number) => {\n if (lines <= 0) {\n if (dark) { if (a >= 6) columnBar(player, cx, mid - 4, mid, 4); }\n else if (a < 8) columnBar(player, cx, mid - a, mid, a);\n else {\n // Hollow: the edges. Ribbed (-1) and panelled (-3): bands across each half too, centred on its middle or\n // quarters, where there is room for them between the edges.\n columnBar(player, cx, mid - a, mid + a - 4, 4);\n const ribs = -lines;\n if (ribs > 0 && a >= 6 * (ribs + 1)) {\n let k = 1;\n while (k <= ribs) {\n const at = (a * k) / (ribs + 1);\n columnBar(player, cx, mid - at - 2, mid + at - 2, 4);\n k++;\n }\n }\n }\n } else if (a < 5 * (lines + 1)) {\n if (!dark) columnBar(player, cx, mid - a, mid, a);\n } else {\n // From the middle out: lit up to each line, the line's row dark (light ink); or a lit band on each line (dark ink).\n let from = 0, k = 1;\n while (k <= lines + 1) {\n const to = k > lines ? a : (a * k) / (lines + 1);\n if (dark) { if (k <= lines) columnBar(player, cx, mid - to - 2, mid + to - 2, 4); }\n else columnBar(player, cx, mid - to, mid + from, to - from);\n from = to + 1;\n k++;\n }\n }\n});\n\n/**\n * Light columns for this frame — the walls of a raycaster, a bar chart — each as tall as the program says, centred on\n * the layout's middle row. The covers are worked out when the script is built (see ColumnLayout), so a frame costs a\n * brush for each part of the picture that differs from its neighbours, not one for every few tiles: a wall that fills\n * the board is a few large brushes.\n *\n * const VIEW = { count: 42, height: 96, ink: \"dark\" } as const;\n * fog.columns(P1, 1, 0, VIEW, heights); // heights: one a column, in tiles, the program's\n * fog.columns(P1, 1, 0, VIEW, heights, faces); // faces: which face of which wall each column shows\n *\n * @param player Whose fog: P1 … P8, known when the script is built.\n * @param x The column of the layout's left edge.\n * @param y The row of its top edge.\n * @param layout How the columns are laid out: known when the script is built.\n * @param heights Each column's height in tiles (0 to the layout's height), an array of the program's.\n * @param faces Which face of which wall each column shows, a number the same for every column of one face — in a\n * raycaster, \\`(mapY * width + mapX) * 2 + side\\` — an array of the program's as long as `heights`: what the\n * layout's `shading` tells walls apart by. Optional: without it, every column is lit whole (a bar chart).\n */\nexport const columns = game((player: Player, x: number, y: number, layout: ColumnLayout, heights: readonly number[], faces?: readonly number[]) => {\n const { words, starts, half, group } = columnPlan(layout);\n const count = layout.count, width = layout.width ?? 3, dark = layout.ink === \"dark\";\n // (Dark lines through a lit column count up; lit bands through a dark one, ribbed and panelled, down.)\n // (Without faces there is nothing to tell apart, and every column is whole.)\n const outline = faces !== undefined && (layout.shading === undefined || layout.shading === \"outline\");\n const lines = layout.shading === \"lines\" ? 3 : layout.shading === \"line\" ? 1 : layout.shading === \"ribbed\" ? -1 : layout.shading === \"hollow\" ? 0 : -3;\n const span = half + 1;\n // Each column's depth from where its lit part starts: the middle row for light ink, the top and bottom for dark.\n // A shaded column of light ink is drawn on its own, after the rest: to the rest it is as if it were not there — and\n // so is every column of a light outline, which is all edges.\n const depth: number[] = new Array(count).fill(0);\n // Half of each column's height, at most half the layout's: worked out once. (Clamped before the shift, the same\n // number: the shift then has the few bits the clamp leaves, where a height from the program could have any.)\n const halves: number[] = new Array(count).fill(0);\n // And in pixels (32 a row), for the bars, which place their brushes in pixels: a small number times 32 is a few tests.\n const halves32: number[] = new Array(count).fill(0);\n let least = half, most = 0;\n // (A light outline is all edges: every depth is 0, nothing to work out.)\n const flat = !dark && outline;\n if (flat) least = 0;\n let i = 0;\n while (i < count) {\n const a = Math.min(Math.max(heights[i], 0), 2 * half + 1) >> 1;\n halves[i] = a;\n halves32[i] = a * 32;\n if (!flat) {\n let d = dark ? half - a : a;\n if (faces !== undefined && !dark && !outline && (faces[i] & 1) != 0) d = 0;\n depth[i] = d;\n if (d < least) least = d;\n if (d > most) most = d;\n }\n i++;\n }\n // A rectangle from depth p, k deep, starts on the rows above and below the middle (light) or below the top and above\n // the bottom (dark); one shallower than 4 is drawn 4 deep, reaching back over what is lit already.\n const mid = y + half;\n // Every column, from depth 0 to the least.\n if (least > 0) {\n const k = Math.max(least, 4);\n if (dark) columnRect(player, words, starts, least, x, y + least - k, y + 2 * half - least);\n else columnRect(player, words, starts, least, x, mid - least, mid + least - k);\n }\n // Each group of columns, from the least to the group's least; then each column, from there to its own. (Nothing,\n // when every column's depth is the least: a light outline's always.)\n let g = most > least ? 0 : count;\n while (g < count) {\n const last = Math.min(g + group, count);\n let gl = half;\n let j = g;\n while (j < last) { if (depth[j] < gl) gl = depth[j]; j++; }\n const level = last - g === group ? 1 : 2;\n if (gl > least) {\n const k = gl - least, kk = Math.max(k, 4);\n if (dark) columnRect(player, words, starts, level * span + k, x + g * width, y + gl - kk, y + 2 * half - gl);\n else columnRect(player, words, starts, level * span + k, x + g * width, mid - gl, mid + gl - kk);\n }\n j = g;\n while (j < last) {\n const k = depth[j] - gl;\n if (k > 0) {\n const kk = Math.max(k, 4), cx = x + j * width + 1;\n if (dark) columnBar(player, cx, y + depth[j] - kk, y + 2 * half - depth[j], k);\n else columnBar(player, cx, mid - depth[j], mid + depth[j] - kk, k);\n }\n j++;\n }\n g = last;\n }\n if (outline) {\n // Where one face meets another, the nearer (taller) of the two columns is an edge.\n const edge: number[] = new Array(count).fill(0);\n let face = faces[0], tall = heights[0];\n i = 1;\n while (i < count) {\n const f = faces[i], h = heights[i];\n if (f != face) { if (h >= tall) edge[i] = 1; else edge[i - 1] = 1; }\n face = f;\n tall = h;\n i++;\n }\n // The bars in pixels: a column's centre stepped across, the middle row's worked out once.\n const mid80 = mid * 32 + 80;\n let px = (x + 1) * 32 + 16;\n i = 0;\n while (i < count) {\n const a = halves[i], a32 = halves32[i];\n if (dark) { if (edge[i] != 0 && a >= 2) columnBarPx(player, px, mid80 - a32, mid80, a32); }\n else if (a > 0) {\n if (edge[i] != 0 || a < 8) columnBarPx(player, px, mid80 - a32, mid80, a32);\n else columnBarPx(player, px, mid80 - a32, mid80 + a32 - 128, 128);\n }\n px += width * 32;\n i++;\n }\n } else if (faces !== undefined) {\n i = 0;\n while (i < count) {\n if ((faces[i] & 1) != 0) columnShaded(player, x + i * width + 1, mid, halves[i], dark, lines);\n i++;\n }\n }\n});\n\n/**\n * Keep what was lit this frame on the board for `n` frames, and wait them out: \\`await fog.show(2)\\` in place of\n * \\`await nextFrame()\\` draws every other frame at half the cost. StarCraft: Remastered redraws the fog on the minimap\n * every other frame (measured in two played recordings, 2026-10-17), so a picture drawn every other frame looks the\n * same there as one drawn every frame. The board is wiped at the start of the frame after them, ready for the next.\n *\n * @param n How many frames, 1 to 24, known when the script is built.\n */\nexport const show = game(async (n: number) => {\n memory.vision.countdown = n;\n await sleep(frames(n));\n});\n\n/** Everything above as one name: import { fog } from \"trigscript/fog\"; fog.light(P1, x, y, 3). */\nexport const fog = { light, rect, columns, show, play, reveal, picture, SHAPES };\n\n/** The longest fog.show() keeps a picture: the countdown the game sets itself after a wipe is longer than this. */\nconst SHOW_MOST = 24;\n\n// The canvas: every brush's sight set, every frame, and the board wiped on the next — for as long as the game runs —\n// unless a program asked for the picture to stay (fog.show), when the countdown is that program's to run down.\nprogram(() => {\n while (true) {\n for (let s = 1; s <= 11; s++) stats(BRUSHES[s - 1]).sight = s;\n const left = memory.vision.countdown;\n if (left == 0 || left > SHOW_MOST) memory.vision.countdown = 1;\n sleep(frames(1));\n }\n});\n", "math": "/**\n * Numbers for motion and play: the standard library's \"trigscript/math\" (TrigScript 4).\n *\n * import { Vec, lerp, easeOutQuad, distance, angleTo, approach, wrap } from \"trigscript/math\";\n *\n * const t = fixed(frame) / 48; // 0 to 1 over two seconds\n * const x = lerp(fromX, toX, easeOutQuad(t)); // eased from one place to another\n * const v = Vec.fromAngle(angleTo(x, y, tx, ty), fixed(4));\n * pos = pos.add(v);\n *\n * Fractions are the language's fixed numbers (16.16: four decimal places or so, from about −32 768 to 32 767): a\n * number becomes one when a fraction is stored into it, or written `fixed(…)`. Angles are radians, as JavaScript's are.\n * Whole numbers stay whole where they can — `distanceSq`, `manhattan`, `wrap`, `sign` — and cost less.\n */\n\n/** π, and a whole turn. */\nexport const PI = Math.PI;\nexport const TAU = Math.PI * 2;\n\n/** From a to b by t: a at 0, b at 1, between them in between (t outside 0 … 1 goes past). */\nexport function lerp(a: fixed, b: fixed, t: fixed): fixed {\n return a + (b - a) * t;\n}\n\n/** Where v is from a to b, as lerp's t: 0 at a, 1 at b. */\nexport function inverseLerp(a: fixed, b: fixed, v: fixed): fixed {\n return b === a ? fixed(0) : (v - a) / (b - a);\n}\n\n/** v from the range a1 … b1 into a2 … b2. */\nexport function remap(v: fixed, a1: fixed, b1: fixed, a2: fixed, b2: fixed): fixed {\n return lerp(a2, b2, inverseLerp(a1, b1, v));\n}\n\n/** t kept inside 0 … 1. */\nexport function clamp01(t: fixed): fixed {\n return t < 0 ? fixed(0) : t > 1 ? fixed(1) : t;\n}\n\n/** From `current` towards `target` by at most `step`, never past it: a value that follows another at a speed. */\nexport function approach(current: number, target: number, step: number): number {\n if (current < target) return current + step > target ? target : current + step;\n return current - step < target ? target : current - step;\n}\n\n/** n into 0 … size − 1, going round: wrap(-1, 10) is 9, wrap(12, 10) is 2. */\nexport function wrap(n: number, size: number): number {\n return ((n % size) + size) % size;\n}\n\n/** −1, 0 or 1, by whether n is below, at or above zero. */\nexport function sign(n: number): number {\n return n > 0 ? 1 : n < 0 ? -1 : 0;\n}\n\n/** The distance from one point to another, as the crow flies. */\nexport function distance(x1: number, y1: number, x2: number, y2: number): fixed {\n return Math.hypot(x2 - x1, y2 - y1);\n}\n\n/** The distance squared: whole, and cheaper — compare it with a range squared. */\nexport function distanceSq(x1: number, y1: number, x2: number, y2: number): number {\n return (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);\n}\n\n/** The distance along the axes, |dx| + |dy|: what the game's own \"nearest\" goes by. */\nexport function manhattan(x1: number, y1: number, x2: number, y2: number): number {\n return Math.abs(x2 - x1) + Math.abs(y2 - y1);\n}\n\n/** The angle from one point to another, in radians from −π to π: 0 is to the right (east), π / 2 down (south, y grows down). */\nexport function angleTo(x1: number, y1: number, x2: number, y2: number): fixed {\n return Math.atan2(y2 - y1, x2 - x1);\n}\n\n/** Degrees as radians. */\nexport function radians(degrees: fixed): fixed {\n return (degrees * PI) / 180;\n}\n\n/** Radians as degrees. */\nexport function degrees(radians: fixed): fixed {\n return (radians * 180) / PI;\n}\n\n/* Easing: t from 0 to 1, how far along a motion is at that point of its time — slow then fast, fast then slow, … */\n\n/** Slow at the start. */\nexport function easeInQuad(t: fixed): fixed {\n return t * t;\n}\n\n/** Slow at the end. */\nexport function easeOutQuad(t: fixed): fixed {\n return t * (2 - t);\n}\n\n/** Slow at both ends. */\nexport function easeInOutQuad(t: fixed): fixed {\n return t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t;\n}\n\n/** Slower at the start than easeInQuad. */\nexport function easeInCubic(t: fixed): fixed {\n return t * t * t;\n}\n\n/** Slower at the end than easeOutQuad. */\nexport function easeOutCubic(t: fixed): fixed {\n const u = t - 1;\n return u * u * u + 1;\n}\n\n/** Goes a little past the end and comes back. */\nexport function easeOutBack(t: fixed): fixed {\n const u = t - 1;\n return 1 + 2.70158 * u * u * u + 1.70158 * u * u;\n}\n\n/** Slow at both ends, smoothly (Hermite: 3t² − 2t³). */\nexport function smoothstep(t: fixed): fixed {\n return t * t * (3 - 2 * t);\n}\n\n/**\n * A point, or a direction and a length: two fixed numbers. Each operation gives a new Vec.\n *\n * let pos = new Vec(fixed(x), fixed(y));\n * const toward = new Vec(fixed(tx), fixed(ty)).sub(pos).normalized().scale(fixed(3));\n * pos = pos.add(toward);\n */\nexport class Vec {\n x: fixed;\n y: fixed;\n\n constructor(x: fixed, y: fixed) {\n this.x = x;\n this.y = y;\n }\n\n /** A direction `length` long at `angle` radians (0 to the right, π / 2 down). */\n static fromAngle(angle: fixed, length: fixed): Vec {\n return new Vec(Math.cos(angle) * length, Math.sin(angle) * length);\n }\n\n add(o: Vec): Vec {\n return new Vec(this.x + o.x, this.y + o.y);\n }\n\n sub(o: Vec): Vec {\n return new Vec(this.x - o.x, this.y - o.y);\n }\n\n scale(k: fixed): Vec {\n return new Vec(this.x * k, this.y * k);\n }\n\n dot(o: Vec): fixed {\n return this.x * o.x + this.y * o.y;\n }\n\n /** How long it is. */\n length(): fixed {\n return Math.sqrt(this.x * this.x + this.y * this.y);\n }\n\n /** The same direction, 1 long (or none, for none). */\n normalized(): Vec {\n const l = this.length();\n const k: fixed = l === 0 ? fixed(0) : 1 / l;\n return new Vec(this.x * k, this.y * k);\n }\n\n /** Its angle, in radians from −π to π. */\n angle(): fixed {\n return Math.atan2(this.y, this.x);\n }\n\n /** Turned by `angle` radians (clockwise on the map, where y grows down). */\n rotate(angle: fixed): Vec {\n const c = Math.cos(angle);\n const s = Math.sin(angle);\n return new Vec(this.x * c - this.y * s, this.x * s + this.y * c);\n }\n\n /** How far it is from another point. */\n distanceTo(o: Vec): fixed {\n return this.sub(o).length();\n }\n\n /** From this towards another by t (0 here, 1 there). */\n lerp(o: Vec, t: fixed): Vec {\n return new Vec(this.x + (o.x - this.x) * t, this.y + (o.y - this.y) * t);\n }\n}\n", @@ -7,9 +8,9 @@ export const STDLIB_SOURCES: Readonly> = { "random": "/**\n * Chance: the standard library's \"trigscript/random\" (TrigScript 4).\n *\n * import { chance, range, pick, shuffle, weighted, Random } from \"trigscript/random\";\n *\n * if (chance(25)) dropLoot(); // one time in four\n * const lane = range(1, 3); // 1, 2 or 3\n * const boss = pick(BOSSES); // any one of a list\n * shuffle(order); // an array of the program, in place\n * const tier = weighted([70, 25, 5]); // 0 most of the time, 2 rarely\n *\n * const rng = new Random(seed); // the same numbers every time for the same seed\n * const x = rng.range(0, 63);\n *\n * The functions use the game's own random numbers, which every player's game draws alike, so nothing goes out of step\n * in a game of several players. `Random` is a generator of your own (xorshift), seeded: the same seed gives the same\n * numbers, game after game — a level made from a seed, a save code that holds one.\n */\nimport { random } from \"trigscript\";\n\n/** True `percent` times in a hundred (25: one time in four). */\nexport function chance(percent: number): boolean {\n return random(100) < percent;\n}\n\n/** A whole number from `min` to `max`, both included. */\nexport function range(min: number, max: number): number {\n return min + random(max - min + 1);\n}\n\n/** The total of `count` dice of `sides` sides each (3, 6: from 3 to 18). */\nexport function dice(count: number, sides: number): number {\n let total = 0;\n let i = 0;\n while (i < count) { total += 1 + random(sides); i++; }\n return total;\n}\n\n/** One of a list, any one alike; a list the script has, or an array of the program (the first's 0 when it is empty). */\nexport function pick(items: readonly T[]): T {\n return items[random(items.length)];\n}\n\n/** The place of one of the weights, each as likely as its share of their total: [70, 25, 5] gives 0 most of the time. */\nexport function weighted(weights: readonly number[]): number {\n let total = 0;\n for (const w of weights) total += w;\n let roll = random(total);\n let i = 0;\n while (i < weights.length) {\n if (roll < weights[i]) return i;\n roll -= weights[i];\n i++;\n }\n return weights.length - 1;\n}\n\n/** An array of the program in a random order, in place (Fisher–Yates: every order alike). */\nexport function shuffle(items: T[]) {\n let i = items.length - 1;\n while (i > 0) {\n const j = random(i + 1);\n const held = items[i];\n items[i] = items[j];\n items[j] = held;\n i--;\n }\n}\n\n/**\n * A generator of random numbers of your own, seeded: the same seed gives the same numbers, in the game and when the\n * script is tested. Make it in the program (`const rng = new Random(1234)`).\n */\nexport class Random {\n state: number;\n\n /** Seeded with any whole number (0 is taken as 1). */\n constructor(seed: number) {\n this.state = seed === 0 ? 1 : seed;\n }\n\n /** The next number, from 0 to 2 147 483 647. */\n next(): number {\n let x = this.state;\n x ^= x << 13;\n x ^= x >>> 17;\n x ^= x << 5;\n this.state = x;\n return x & 0x7fffffff;\n }\n\n /** A whole number from 0 to n − 1. */\n int(n: number): number {\n return this.next() % n;\n }\n\n /** A whole number from `min` to `max`, both included. */\n range(min: number, max: number): number {\n return min + this.int(max - min + 1);\n }\n\n /** True `percent` times in a hundred. */\n chance(percent: number): boolean {\n return this.int(100) < percent;\n }\n\n /** One of a list, any one alike. */\n pick(items: readonly T[]): T {\n return items[this.int(items.length)];\n }\n\n /** An array of the program in a random order, in place. */\n shuffle(items: T[]) {\n let i = items.length - 1;\n while (i > 0) {\n const j = this.int(i + 1);\n const held = items[i];\n items[i] = items[j];\n items[j] = held;\n i--;\n }\n }\n}\n", "screen": "/**\n * Pixel screens: the standard library's \"trigscript/screen\" (TrigScript 4).\n *\n * import { Screen, ScreenView, sprite } from \"trigscript/screen\";\n *\n * const SHIP = sprite([\".1.\", \"111\", \"1.1\"]);\n *\n * program(async () => {\n * const screen = new Screen(4, 4, 24, 16); // 24 × 16 pixels, its corner on tile (4, 4)\n * let x = 10;\n * while (true) {\n * screen.clear();\n * screen.rect(0, 15, 24, 1, 5); // the ground, in colour 5\n * screen.blit(SHIP, x, 12);\n * screen.number(score, 1, 1, 2); // digits 3 × 5 pixels, in colour 2\n * screen.present(); // on the map, and so on the minimap (screen.reveal(P1) once first)\n * await nextFrame();\n * }\n * });\n *\n * A screen is a grid of pixels, each 0 (empty) or a colour from 1 to 7; drawing changes the grid, and `present()`\n * shows it: a pixel is a unit on the map, owned by the player whose colour it is, so the minimap shows the picture —\n * neighbouring pixels of one colour join into a solid block there. Only the pixels that changed since the last\n * `present()` are made or taken away, and only the pixels drawn since are looked at, so a still picture costs nothing to\n * show. Draw through the methods (`set`, `clear`, `rect`, …): a pixel written into `pixels` directly is not seen. The same grid can be shown in a\n * player's text lines too, as squares (`ScreenView` in a `ui.lines` view): one game, two screens, as the Tetris probe has it.\n *\n * A player sees the pixels only where they have vision: `screen.reveal(P1)` once gives them Map Revealers over it.\n *\n * Two looks (the constructor's last argument): \"dots\", Overlords, which fly — the minimap draws each as a dot with a gap\n * round it, the screen may be over Space; and \"solid\", Pylons, which the minimap draws as their whole two-tile\n * footprint, so pixels side by side are one block — on buildable ground (probes/lib/screen-map.mts --ground).\n *\n * What the map needs, found playing the unit-pixel probe in StarCraft: Remastered (2026-10-16): the owners of the\n * pixels (P3, P4, P5, P1, P6, P7, P8 below; their colours are theirs on the map, so give them the colours of TEXT_COLORS) must not\n * be computers — a computer moves its own units — and Remastered will not start a game without a computer, so one slot\n * (P2 here) is a computer that owns no pixel. A pixel is an Overlord, which sits on any ground and on nothing else; keep\n * the screen's tiles clear of buildings. A Pylon needs buildable ground, and none of the map's own units in the way. About 80 pixels made and taken away in a frame did not slow the game.\n */\nimport { centerLocation, createUnit, removeUnitAt, reserveLocation, textColors, P1, P3, P4, P5, P6, P7, P8, type Player, type UnitType } from \"trigscript\";\n\n/**\n * Whose units show colours 1 to 7 on the map, in the order of TEXT_COLORS: give each player that colour in the map\n * (cyan, yellow, purple, green, red, blue, orange). Green is P1's — your own units are green on the minimap anyway.\n * Not computers.\n */\nexport const OWNERS: readonly Player[] = [P3, P4, P5, P1, P6, P7, P8];\n/** The same colours in the text lines (`ScreenView`), 0 an empty square: cyan, yellow, purple, green, red, blue, orange. */\nexport const TEXT_COLORS: readonly number[] = [0, textColors.cyan, textColors.yellow, textColors.purple, textColors.green, textColors.red, textColors.blue, textColors.orange];\n\n/** Where each pixel's unit is made: a location the map does not use, taken when the script is built. */\nconst SPOT = reserveLocation(\"screen: where each pixel is made\");\n\n/** A picture known when the script is built, to `blit` onto a screen: its size, and its pixels a row at a time (0 is see-through). */\nexport interface Sprite { readonly width: number; readonly height: number; readonly cells: readonly number[] }\n\n/**\n * A sprite from rows of text, when the script is built: \"1\" … \"7\" a pixel of that colour, anything else see-through.\n *\n * const SHIP = sprite([\".1.\", \"111\", \"1.1\"]);\n */\nexport function sprite(rows: readonly string[]): Sprite {\n const width = Math.max(0, ...rows.map((r) => r.length));\n const cells: number[] = [];\n for (const row of rows) for (let x = 0; x < width; x++) { const c = Number(row[x]); cells.push(c >= 1 && c <= 7 ? c : 0); }\n return { width, height: rows.length, cells };\n}\n\n/** The digits 0 to 9, three pixels wide and five high, for `number()`: each fifteen pixels, a row at a time. */\nconst DIGITS: readonly number[] = [\n \"111101101101111\", \"010110010010111\", \"111001111100111\", \"111001111001111\", \"101101111001001\",\n \"111100111001111\", \"111100111101111\", \"111001001010010\", \"111101111101111\", \"111101111001111\",\n].flatMap((d) => d.split(\"\").map(Number));\n\n/** The most pixels a screen has: 32 × 32 at two tiles a pixel covers a 64 × 64 map, 64 × 32 half of a 128 × 128 one. */\nexport const MAX_PIXELS = 2048;\n\nexport class Screen {\n /** The picture being drawn: a colour a pixel, a row at a time (the first width × height of them). */\n pixels: number[] = new Array(MAX_PIXELS).fill(0);\n /** What the map shows now: the picture as it was at the last `present()`. */\n shown: number[] = new Array(MAX_PIXELS).fill(0);\n /** The pixels drawn a colour they were not since the last `present()`, in order: each one's place and its x and y. */\n drawnAt: number[] = new Array(MAX_PIXELS).fill(0);\n drawnX: number[] = new Array(MAX_PIXELS).fill(0);\n drawnY: number[] = new Array(MAX_PIXELS).fill(0);\n drawn = 0;\n /** More drawn than the list holds: `present()` looks at every pixel instead. */\n overflow = false;\n x: number;\n y: number;\n width: number;\n height: number;\n cell: number;\n /** The unit a pixel is: an Overlord (\"dots\"), or a Pylon (\"solid\"). */\n unit = 0;\n\n /**\n * A screen `width` × `height` pixels (MAX_PIXELS at most), its top left corner on tile (x, y) of the map, a pixel two\n * tiles across.\n * @param look \"dots\" (the default): a pixel is an Overlord, which the minimap draws as a dot, a gap round it — it flies,\n * so the screen may be over anything, Space too. \"solid\": a pixel is a Pylon, which the minimap draws as its whole\n * footprint, two tiles square, so pixels side by side are one block — it needs buildable ground under the screen,\n * and gives its owner Protoss supply. (Overlords a tile apart do not fit: the game will not place most of them.)\n */\n constructor(x: number, y: number, width: number, height: number, look: \"dots\" | \"solid\" = \"dots\") {\n this.x = x;\n this.y = y;\n this.width = width;\n this.height = height;\n this.cell = 64;\n this.unit = (look === \"solid\" ? 156 : 42) as UnitType; // Protoss Pylon, Zerg Overlord: the game's own ids, the same under a mod's names\n }\n\n /** Every pixel the colour (0, empty, unless it says). */\n clear(color: number = 0) {\n let i = 0;\n let y = 0;\n while (y < this.height) {\n let x = 0;\n while (x < this.width) { this.put(i, x, y, color); i++; x++; }\n y++;\n }\n }\n\n /** One pixel; nothing happens outside the screen. */\n set(x: number, y: number, color: number) {\n if (x >= 0 && y >= 0 && x < this.width && y < this.height) this.put(y * this.width + x, x, y, color);\n }\n\n /** A pixel written, and listed for `present()` when its colour changed. */\n private put(i: number, x: number, y: number, color: number) {\n if (this.pixels[i] === color) return;\n this.pixels[i] = color;\n if (this.drawn < MAX_PIXELS) {\n this.drawnAt[this.drawn] = i;\n this.drawnX[this.drawn] = x;\n this.drawnY[this.drawn] = y;\n this.drawn++;\n } else this.overflow = true;\n }\n\n /** The colour of a pixel; 0 outside the screen. */\n get(x: number, y: number): number {\n if (x >= 0 && y >= 0 && x < this.width && y < this.height) return this.pixels[y * this.width + x];\n return 0;\n }\n\n /** A filled rectangle, its top left at (x, y); what is outside the screen is left out. */\n rect(x: number, y: number, width: number, height: number, color: number) {\n let j = 0;\n while (j < height) {\n let i = 0;\n while (i < width) { this.set(x + i, y + j, color); i++; }\n j++;\n }\n }\n\n /** A line from (x0, y0) to (x1, y1), both ends drawn. */\n line(x0: number, y0: number, x1: number, y1: number, color: number) {\n const dx = Math.abs(x1 - x0);\n const dy = -Math.abs(y1 - y0);\n const sx = x0 < x1 ? 1 : -1;\n const sy = y0 < y1 ? 1 : -1;\n let err = dx + dy;\n let x = x0;\n let y = y0;\n while (true) {\n this.set(x, y, color);\n if (x === x1 && y === y1) break;\n const e2 = 2 * err;\n if (e2 >= dy) { err += dy; x += sx; }\n if (e2 <= dx) { err += dx; y += sy; }\n }\n }\n\n /** A sprite with its top left at (x, y): its see-through pixels leave what is under them. */\n blit(s: Sprite, x: number, y: number) {\n let k = 0;\n let j = 0;\n while (j < s.height) {\n let i = 0;\n while (i < s.width) {\n const c = s.cells[k];\n if (c !== 0) this.set(x + i, y + j, c);\n k++;\n i++;\n }\n j++;\n }\n }\n\n /** A whole number's digits, 3 × 5 pixels each with a pixel between, its top left at (x, y): a score on the minimap. */\n number(n: number, x: number, y: number, color: number) {\n let rest = n < 0 ? -n : n;\n let count = 1;\n let probe = rest;\n while (probe >= 10) { probe = Math.floor(probe / 10); count++; }\n let place = x + (count - 1) * 4;\n while (true) {\n const d = rest % 10;\n let k = 0;\n while (k < 15) {\n if (DIGITS[d * 15 + k] === 1) this.set(place + (k % 3), y + Math.floor(k / 3), color);\n k++;\n }\n rest = Math.floor(rest / 10);\n place -= 4;\n if (rest === 0) break;\n }\n }\n\n /**\n * Let a player see the screen: Map Revealers of theirs over it, for the rest of the game. Without them its pixels,\n * owned by other players, are hidden by the fog of war — on the map and on the minimap. Once, before the first\n * `present()`.\n * @param player Who sees it: P1 … P8, known when the script is built.\n */\n reveal(player: Player) {\n // A Map Revealer sees ten tiles round; one every six tiles leaves no gap.\n const right = this.x * 32 + this.width * this.cell;\n const bottom = this.y * 32 + this.height * this.cell;\n let py = this.y * 32 + 96;\n while (py < bottom + 96) {\n let px = this.x * 32 + 96;\n while (px < right + 96) {\n centerLocation(SPOT, px, py, 32, 32);\n createUnit(player, 101 as UnitType, 1, SPOT); // Map Revealer\n px += 192;\n }\n py += 192;\n }\n }\n\n /**\n * The picture on the map: each pixel that changed since the last time taken away and made again in its colour. Only\n * the pixels drawn since are looked at (one drawn and drawn back is passed over), unless more were drawn than the list\n * holds.\n */\n present() {\n if (this.overflow) {\n let i = 0;\n let y = 0;\n while (y < this.height) {\n let x = 0;\n while (x < this.width) { this.show(i, x, y); i++; x++; }\n y++;\n }\n this.overflow = false;\n } else {\n for (let k = 0; k < this.drawn; k++) this.show(this.drawnAt[k], this.drawnX[k], this.drawnY[k]);\n }\n this.drawn = 0;\n }\n\n /** One pixel on the map as it is in the picture, when it is not already. */\n private show(i: number, x: number, y: number) {\n const now = this.pixels[i];\n const was = this.shown[i];\n if (now === was) return;\n const half = this.cell >> 1;\n centerLocation(SPOT, this.x * 32 + x * this.cell + half, this.y * 32 + y * this.cell + half, 32, 32);\n OWNERS.forEach((owner, k) => { if (was === k + 1) removeUnitAt(owner, this.unit, 1, SPOT); });\n OWNERS.forEach((owner, k) => { if (now === k + 1) createUnit(owner, this.unit, 1, SPOT); });\n this.shown[i] = now;\n }\n}\n\n/**\n * A screen in the text lines, a square a pixel — filled in its colour, or empty — a line a row. For a `ui.lines` view:\n * ``, the screen's size known when the script is built. Eleven lines\n * show at once, sixty squares across.\n */\nexport function ScreenView({ screen, width, height }: { screen: Screen; width: number; height: number }) {\n return <>{UP_TO_60.map((y) => y < height && {UP_TO_60.map((x) => x < width && )})};\n}\n\n/** 0 … 59: the places a view's rows and columns are counted over, the ones past its size left out when the script is built. */\nconst UP_TO_60: readonly number[] = Array.from({ length: 60 }, (_, i) => i);\n", "signals": "/**\n * Events of your own: the standard library's \"trigscript/signals\" (TrigScript 4).\n *\n * import { Signal } from \"trigscript/signals\";\n *\n * const waveCleared = new Signal();\n *\n * program(async () => {\n * waveCleared.on((wave) => { setResources(P1, \"add\", 100 * wave, \"ore\"); }); // braces: a handler gives back no action\n * waveCleared.once((wave) => print(`First wave down!`));\n * const id = waveCleared.on((wave) => print(`Wave ${wave} cleared`));\n * …\n * waveCleared.emit(3); // every handler, in the order they were added\n * waveCleared.off(id);\n * });\n *\n * A signal lets one part of a map say that something happened without knowing who listens: the shop, the score and the\n * music each add a handler, and the wave code only emits. What it carries is one value of the type it is made with —\n * a number, a boolean, a text or a unit (`new Signal()`). Its handlers are functions kept as values: they may use\n * what the block they were written in has, and they run to their end, one after another, within the frame.\n *\n * Make a signal inside the program that uses it (`const s = new Signal()` at its top): a signal is variables\n * of a program, as anything a program writes is.\n */\n\nexport class Signal {\n /** The handlers, by the number `on` gave; one taken off is undefined, and its place is given to the next one added. */\n handlers: (((value: T) => void) | undefined)[] = [];\n /** Which handlers go once they have run once. */\n onceOnly: boolean[] = [];\n\n /**\n * Add a handler: it runs at every `emit`, handed the value.\n * @returns Its number, for `off`.\n */\n on(handler: (value: T) => void): number {\n return this.add(handler, false);\n }\n\n /** Add a handler that runs at the next `emit` only. @returns Its number, for `off` before then. */\n once(handler: (value: T) => void): number {\n return this.add(handler, true);\n }\n\n /** Take a handler off, by the number `on` or `once` gave. */\n off(id: number) {\n if (id >= 0 && id < this.handlers.length) this.handlers[id] = undefined;\n }\n\n /** Run every handler, in the order they were added, each handed `value`. */\n emit(value: T) {\n let i = 0;\n while (i < this.handlers.length) {\n const h = this.handlers[i];\n if (h) {\n if (this.onceOnly[i]) this.handlers[i] = undefined;\n h(value);\n }\n i++;\n }\n }\n\n /** How many handlers it has. */\n count(): number {\n let n = 0;\n for (const h of this.handlers) if (h) n++;\n return n;\n }\n\n /** Take every handler off. */\n clear() {\n let i = 0;\n while (i < this.handlers.length) { this.handlers[i] = undefined; i++; }\n }\n\n private add(handler: (value: T) => void, once: boolean): number {\n // A place one was taken off from, or a new one at the end.\n let i = 0;\n while (i < this.handlers.length) {\n if (!this.handlers[i]) {\n this.handlers[i] = handler;\n this.onceOnly[i] = once;\n return i;\n }\n i++;\n }\n this.handlers.push(handler);\n this.onceOnly.push(once);\n return this.handlers.length - 1;\n }\n}\n", - "spells": "/**\n * Spells that cast themselves: the standard library's \"trigscript/spells\" (TrigScript 4).\n *\n * import { autoRestore } from \"trigscript/spells\";\n *\n * autoRestore(); // at the top of the script, once\n * autoRestore({ switch: \"Medics trained\", keepEnergy: 25 }); // or tied to a switch, keeping 25 energy\n *\n * With auto-restore on, a human player's Medic that is idle, or healing on its own from idle, casts Restoration on a\n * unit of its owner's, or of a player its owner has allied, that is within the spell's range and carries Lockdown,\n * Irradiate, Plague, Ensnare, Maelstrom, Acid Spores or Optical Flare. It is a real cast: the owner must have researched\n * Restoration, the Medic must have its energy (and `keepEnergy` more), and the cast shows and costs as a player's\n * does. The nearest Medic goes to the nearest afflicted unit, one Medic to a unit; one that is moving, attack-moving,\n * holding, patrolling or casting for the player is never used, nor one that could not cast for the player: in stasis,\n * under Maelstrom, or loaded in a transport or a Bunker. A Medic never walks to reach a unit. Parasite\n * alone, a unit in stasis or invincible, a hallucination (Restoration kills it) and the Medic itself are left alone.\n * Computer players' Medics are left to the computer, which casts Restoration itself.\n *\n * Played in StarCraft: Remastered (2026-11-11): the order written to the Medic (CastRestoration at the unit) is cast,\n * mid-heal too, and the Medic heals on afterwards. How it tells a Medic healing from idle from one the player sent,\n * from `map trace` (2026-11-11): either way the order is MedicHeal, but an attack-move or a patrol waits behind it in\n * the Medic's queue (`queuedOrders` 1 or 2), and the queue of one healing from idle holds nothing the game counts.\n */\nimport { program, nextFrame, isHuman, switchIs, switches, allUnits, stats, units, orders, techs, weapons, P1, P2, P3, P4, P5, P6, P7, P8, type Player, type Unit } from \"trigscript\";\n\nfunction ownerIsHuman(medic: Unit): boolean {\n const owner = medic.owner;\n if (owner == P1) return isHuman(P1);\n if (owner == P2) return isHuman(P2);\n if (owner == P3) return isHuman(P3);\n if (owner == P4) return isHuman(P4);\n if (owner == P5) return isHuman(P5);\n if (owner == P6) return isHuman(P6);\n if (owner == P7) return isHuman(P7);\n if (owner == P8) return isHuman(P8);\n return false;\n}\n\nfunction ownerHasRestoration(medic: Unit): boolean {\n const owner = medic.owner;\n if (owner == P1) return stats(P1).researched[techs.Restoration];\n if (owner == P2) return stats(P2).researched[techs.Restoration];\n if (owner == P3) return stats(P3).researched[techs.Restoration];\n if (owner == P4) return stats(P4).researched[techs.Restoration];\n if (owner == P5) return stats(P5).researched[techs.Restoration];\n if (owner == P6) return stats(P6).researched[techs.Restoration];\n if (owner == P7) return stats(P7).researched[techs.Restoration];\n if (owner == P8) return stats(P8).researched[techs.Restoration];\n return false;\n}\n\nfunction playerHasAllied(player: Player, target: Unit): boolean {\n const owner = target.owner;\n if (owner == P1) return stats(player).allied[P1];\n if (owner == P2) return stats(player).allied[P2];\n if (owner == P3) return stats(player).allied[P3];\n if (owner == P4) return stats(player).allied[P4];\n if (owner == P5) return stats(player).allied[P5];\n if (owner == P6) return stats(player).allied[P6];\n if (owner == P7) return stats(player).allied[P7];\n if (owner == P8) return stats(player).allied[P8];\n return false;\n}\n\nfunction isFriendly(medic: Unit, target: Unit): boolean {\n const owner = medic.owner;\n if (target.owner == owner) return true;\n if (owner == P1) return playerHasAllied(P1, target);\n if (owner == P2) return playerHasAllied(P2, target);\n if (owner == P3) return playerHasAllied(P3, target);\n if (owner == P4) return playerHasAllied(P4, target);\n if (owner == P5) return playerHasAllied(P5, target);\n if (owner == P6) return playerHasAllied(P6, target);\n if (owner == P7) return playerHasAllied(P7, target);\n if (owner == P8) return playerHasAllied(P8, target);\n return false;\n}\n\nfunction isIdleOrHealingFromIdle(medic: Unit): boolean {\n const order = medic.orderId;\n if (order == orders.Medic) return true;\n return (order == orders.MedicHeal || order == orders.MedicHealToIdle) && medic.queuedOrders == 0;\n}\n\n/** Whether the Medic could cast for its player now: not in stasis or under Maelstrom, and not loaded in a transport or a Bunker. */\nfunction canCast(medic: Unit): boolean {\n return medic.stasis == 0 && medic.maelstrom == 0 && !medic.inTransport;\n}\n\nfunction hasRestorableEffect(target: Unit): boolean {\n return target.lockdown > 0 || target.irradiate > 0 || target.plague > 0 || target.ensnare > 0 || target.maelstrom > 0 || target.acidSpores > 0 || target.opticalFlare;\n}\n\nfunction includes(list: Unit[], unit: Unit): boolean {\n for (const each of list) if (each == unit) return true;\n return false;\n}\n\nlet programBuilt = false;\n\n/**\n * Turn auto-restore on: every human player's idle or self-healing Medic casts Restoration on the nearest afflicted\n * unit of its owner's or an ally's within range. `switch`: a switch of the map, by its name;\n * auto-restore runs only while it is set. `keepEnergy`: the energy a Medic keeps after the cast (default 0). Called at\n * the top of a script: it makes a program, and programs are made when the map is built, so a call inside a program is\n * an error. A call after the first adds nothing.\n */\nexport function autoRestore(options: { switch?: keyof typeof switches; keepEnergy?: number } = {}) {\n if (programBuilt) return;\n programBuilt = true;\n const keepEnergy = options.keepEnergy ?? 0;\n program(async () => {\n const sentMedics: Unit[] = [];\n const sentTargets: Unit[] = [];\n while (true) {\n const range = stats(weapons.Restoration).range;\n for (let i = sentMedics.length - 1; i >= 0; i--) {\n const medic = sentMedics[i], target = sentTargets[i];\n if (medic.orderId == orders.CastRestoration && medic.orderTarget == target) {\n // (State 2 is the cast under way, its energy spent: let it land.)\n if (medic.orderState == 2 || medic.distanceTo(target) <= range) continue;\n medic.stop();\n }\n sentMedics.splice(i, 1);\n sentTargets.splice(i, 1);\n }\n if (options.switch !== undefined && !switchIs(switches[options.switch], \"set\")) {\n await nextFrame();\n continue;\n }\n const energy = stats(techs.Restoration).energy + keepEnergy;\n const medics: Unit[] = [];\n const castAt: Unit[] = [];\n for (const medic of allUnits({ type: units.TerranMedic })) {\n if (medic.orderId == orders.CastRestoration) {\n const target = medic.orderTarget;\n if (target) castAt.push(target);\n } else if (isIdleOrHealingFromIdle(medic) && canCast(medic) && medic.energy >= energy && ownerIsHuman(medic) && ownerHasRestoration(medic)) {\n medics.push(medic);\n }\n }\n if (medics.length > 0) {\n const pairMedics: Unit[] = [];\n const pairTargets: Unit[] = [];\n const pairDistances: number[] = [];\n for (const target of allUnits({ type: units.Men })) {\n if (!hasRestorableEffect(target) || target.stasis > 0 || target.hallucinated || target.invincible || includes(castAt, target)) continue;\n for (const medic of medics) {\n if (medic == target || !isFriendly(medic, target)) continue;\n const distance = medic.distanceTo(target);\n if (distance > range) continue;\n pairMedics.push(medic);\n pairTargets.push(target);\n pairDistances.push(distance);\n }\n }\n while (pairMedics.length > 0) {\n let nearest = 0;\n for (let i = 1; i < pairDistances.length; i++) if (pairDistances[i] < pairDistances[nearest]) nearest = i;\n const medic = pairMedics[nearest], target = pairTargets[nearest];\n medic.cast(techs.Restoration, target);\n sentMedics.push(medic);\n sentTargets.push(target);\n for (let i = pairMedics.length - 1; i >= 0; i--) {\n if (pairMedics[i] == medic || pairTargets[i] == target) {\n pairMedics.splice(i, 1);\n pairTargets.splice(i, 1);\n pairDistances.splice(i, 1);\n }\n }\n }\n }\n await nextFrame();\n }\n });\n}\n", + "spells": "/**\n * Spells that cast themselves: the standard library's \"trigscript/spells\" (TrigScript 4).\n *\n * import { autoRestore } from \"trigscript/spells\";\n *\n * autoRestore(); // at the top of the script, once\n * autoRestore({ switch: \"Medics trained\", keepEnergy: 25 }); // or tied to a switch, keeping 25 energy\n * autoRestore({ allowPlayersToTurnOffAutoCast: true }); // a single-player map: right-click Restore to turn it off\n *\n * With auto-restore on, a human player's Medic that is idle, or healing on its own from idle, casts Restoration on a\n * unit of its owner's, or of a player its owner has allied, that is within the spell's range and carries Lockdown,\n * Irradiate, Plague, Ensnare, Maelstrom, Acid Spores or Optical Flare. It is a real cast: the owner must have researched\n * Restoration, the Medic must have its energy (and `keepEnergy` more), and the cast shows and costs as a player's\n * does. The nearest Medic goes to the nearest afflicted unit, one Medic to a unit; one that is moving, attack-moving,\n * holding, patrolling or casting for the player is never used, nor one that could not cast for the player: in stasis,\n * under Maelstrom, or loaded in a transport or a Bunker. A Medic never walks to reach a unit. Parasite\n * alone, a unit in stasis or invincible, a hallucination (Restoration kills it) and the Medic itself are left alone.\n * Computer players' Medics are left to the computer, which casts Restoration itself.\n *\n * With `allowPlayersToTurnOffAutoCast`, for single-player maps, the player turns auto-restore off and on again by\n * right-clicking the Restore button on a Medic's command card (\"trigscript/card\"), and is told \"Auto-restore off\" or\n * \"Auto-restore on\". It starts on. While the map's switch is cleared, the right-click does nothing and the setting is kept.\n *\n * Played in StarCraft: Remastered (2026-11-11): the order written to the Medic (CastRestoration at the unit) is cast,\n * mid-heal too, and the Medic heals on afterwards. How it tells a Medic healing from idle from one the player sent,\n * from `map trace` (2026-11-11): either way the order is MedicHeal, but an attack-move or a patrol waits behind it in\n * the Medic's queue (`queuedOrders` 1 or 2), and the queue of one healing from idle holds nothing the game counts.\n */\nimport { program, nextFrame, isHuman, switchIs, switches, allUnits, stats, units, orders, techs, weapons, print, AllPlayers, CurrentPlayer, P1, P2, P3, P4, P5, P6, P7, P8, type Player, type Unit } from \"trigscript\";\nimport { CardButton, cardSlots } from \"trigscript/card\";\n\nfunction ownerIsHuman(medic: Unit): boolean {\n const owner = medic.owner;\n if (owner == P1) return isHuman(P1);\n if (owner == P2) return isHuman(P2);\n if (owner == P3) return isHuman(P3);\n if (owner == P4) return isHuman(P4);\n if (owner == P5) return isHuman(P5);\n if (owner == P6) return isHuman(P6);\n if (owner == P7) return isHuman(P7);\n if (owner == P8) return isHuman(P8);\n return false;\n}\n\nfunction ownerHasRestoration(medic: Unit): boolean {\n const owner = medic.owner;\n if (owner == P1) return stats(P1).researched[techs.Restoration];\n if (owner == P2) return stats(P2).researched[techs.Restoration];\n if (owner == P3) return stats(P3).researched[techs.Restoration];\n if (owner == P4) return stats(P4).researched[techs.Restoration];\n if (owner == P5) return stats(P5).researched[techs.Restoration];\n if (owner == P6) return stats(P6).researched[techs.Restoration];\n if (owner == P7) return stats(P7).researched[techs.Restoration];\n if (owner == P8) return stats(P8).researched[techs.Restoration];\n return false;\n}\n\nfunction playerHasAllied(player: Player, target: Unit): boolean {\n const owner = target.owner;\n if (owner == P1) return stats(player).allied[P1];\n if (owner == P2) return stats(player).allied[P2];\n if (owner == P3) return stats(player).allied[P3];\n if (owner == P4) return stats(player).allied[P4];\n if (owner == P5) return stats(player).allied[P5];\n if (owner == P6) return stats(player).allied[P6];\n if (owner == P7) return stats(player).allied[P7];\n if (owner == P8) return stats(player).allied[P8];\n return false;\n}\n\nfunction isFriendly(medic: Unit, target: Unit): boolean {\n const owner = medic.owner;\n if (target.owner == owner) return true;\n if (owner == P1) return playerHasAllied(P1, target);\n if (owner == P2) return playerHasAllied(P2, target);\n if (owner == P3) return playerHasAllied(P3, target);\n if (owner == P4) return playerHasAllied(P4, target);\n if (owner == P5) return playerHasAllied(P5, target);\n if (owner == P6) return playerHasAllied(P6, target);\n if (owner == P7) return playerHasAllied(P7, target);\n if (owner == P8) return playerHasAllied(P8, target);\n return false;\n}\n\nfunction isIdleOrHealingFromIdle(medic: Unit): boolean {\n const order = medic.orderId;\n if (order == orders.Medic) return true;\n return (order == orders.MedicHeal || order == orders.MedicHealToIdle) && medic.queuedOrders == 0;\n}\n\n/** Whether the Medic could cast for its player now: not in stasis or under Maelstrom, and not loaded in a transport or a Bunker. */\nfunction canCast(medic: Unit): boolean {\n return medic.stasis == 0 && medic.maelstrom == 0 && !medic.inTransport;\n}\n\nfunction hasRestorableEffect(target: Unit): boolean {\n return target.lockdown > 0 || target.irradiate > 0 || target.plague > 0 || target.ensnare > 0 || target.maelstrom > 0 || target.acidSpores > 0 || target.opticalFlare;\n}\n\nfunction includes(list: Unit[], unit: Unit): boolean {\n for (const each of list) if (each == unit) return true;\n return false;\n}\n\nlet programBuilt = false;\n\n/**\n * Turn auto-restore on: every human player's idle or self-healing Medic casts Restoration on the nearest afflicted\n * unit of its owner's or an ally's within range. `switch`: a switch of the map, by its name;\n * auto-restore runs only while it is set. `keepEnergy`: the energy a Medic keeps after the cast (default 0).\n * `allowPlayersToTurnOffAutoCast`: a single-player map's player turns it off and on by right-clicking Restore. Called at\n * the top of a script: it makes a program, and programs are made when the map is built, so a call inside a program is\n * an error. A call after the first adds nothing.\n */\nexport function autoRestore(options: { switch?: keyof typeof switches; keepEnergy?: number; allowPlayersToTurnOffAutoCast?: boolean } = {}) {\n if (programBuilt) return;\n programBuilt = true;\n const keepEnergy = options.keepEnergy ?? 0;\n const toggled = options.allowPlayersToTurnOffAutoCast === true;\n program(async () => {\n // Run for every player, so the card's watcher and its view moves are the human's: the others' copies end here.\n if (toggled && !isHuman(CurrentPlayer)) return;\n let autoRestoreOn = true;\n const restoreButton = new CardButton(techs.Restoration, cardSlots.Restoration);\n if (toggled) await restoreButton.begin();\n const sentMedics: Unit[] = [];\n const sentTargets: Unit[] = [];\n while (true) {\n if (toggled && (await restoreButton.rightClicked()) && (options.switch === undefined || switchIs(switches[options.switch], \"set\"))) {\n autoRestoreOn = !autoRestoreOn;\n print(autoRestoreOn ? \"Auto-restore on\" : \"Auto-restore off\", { to: CurrentPlayer });\n }\n const range = stats(weapons.Restoration).range;\n for (let i = sentMedics.length - 1; i >= 0; i--) {\n const medic = sentMedics[i], target = sentTargets[i];\n if (medic.orderId == orders.CastRestoration && medic.orderTarget == target) {\n // (State 2 is the cast under way, its energy spent: let it land.)\n if (medic.orderState == 2 || medic.distanceTo(target) <= range) continue;\n medic.stop();\n }\n sentMedics.splice(i, 1);\n sentTargets.splice(i, 1);\n }\n if ((options.switch !== undefined && !switchIs(switches[options.switch], \"set\")) || !autoRestoreOn) {\n await nextFrame();\n continue;\n }\n const energy = stats(techs.Restoration).energy + keepEnergy;\n const medics: Unit[] = [];\n const castAt: Unit[] = [];\n for (const medic of allUnits({ type: units.TerranMedic })) {\n if (medic.orderId == orders.CastRestoration) {\n const target = medic.orderTarget;\n if (target) castAt.push(target);\n } else if (isIdleOrHealingFromIdle(medic) && canCast(medic) && medic.energy >= energy && ownerIsHuman(medic) && ownerHasRestoration(medic)) {\n medics.push(medic);\n }\n }\n if (medics.length > 0) {\n const pairMedics: Unit[] = [];\n const pairTargets: Unit[] = [];\n const pairDistances: number[] = [];\n for (const target of allUnits({ type: units.Men })) {\n if (!hasRestorableEffect(target) || target.stasis > 0 || target.hallucinated || target.invincible || includes(castAt, target)) continue;\n for (const medic of medics) {\n if (medic == target || !isFriendly(medic, target)) continue;\n const distance = medic.distanceTo(target);\n if (distance > range) continue;\n pairMedics.push(medic);\n pairTargets.push(target);\n pairDistances.push(distance);\n }\n }\n while (pairMedics.length > 0) {\n let nearest = 0;\n for (let i = 1; i < pairDistances.length; i++) if (pairDistances[i] < pairDistances[nearest]) nearest = i;\n const medic = pairMedics[nearest], target = pairTargets[nearest];\n medic.cast(techs.Restoration, target);\n sentMedics.push(medic);\n sentTargets.push(target);\n for (let i = pairMedics.length - 1; i >= 0; i--) {\n if (pairMedics[i] == medic || pairTargets[i] == target) {\n pairMedics.splice(i, 1);\n pairTargets.splice(i, 1);\n pairDistances.splice(i, 1);\n }\n }\n }\n }\n await nextFrame();\n }\n }, toggled ? { owner: AllPlayers } : {});\n}\n", "timers": "/**\n * Time in a program: the standard library's \"trigscript/timers\" (TrigScript 4).\n *\n * import { every, after, Cooldown, Countdown, SECOND } from \"trigscript/timers\";\n *\n * program(async () => {\n * const spawner = every(10 * SECOND, (n) => spawnWave(n)); // every ten seconds, the wave's number handed over\n * after(3 * SECOND, () => print(\"Go!\"));\n * const shot = new Cooldown(12);\n * while (true) {\n * if (keyPressed(P1, \"Space\") && shot.use()) fire();\n * if (boss.hp <= 0) spawner.cancel();\n * await nextFrame();\n * }\n * });\n *\n * for await (const b of over(bullets, { per: 50 })) b.x += b.dx; // fifty a frame, on to the next frame after each fifty\n *\n * Time is counted in frames of the game, 24 a second on Fastest (`SECOND`). `every` and `after` run their function in a\n * task beside the program, so the program goes on at once; the Task they give back stops it (`cancel()`) and says\n * whether it still runs (`running`). Their function is kept for the task, with what it uses of the block it was written\n * in — written in a loop, each turn's is its own, as JavaScript has it — and it runs to its end within the frame (it\n * does not wait). Sixteen of one `every` or `after` written once can run at a time. `Cooldown`, `Countdown` and\n * `Stopwatch` read the game's frame counter: no task, nothing to stop.\n */\nimport { game, start, sleep, frames, nextFrame, memory, type Task } from \"trigscript\";\n\n/** Frames a second of game time on Fastest, the speed a map is played at. */\nexport const SECOND = 24;\n\n/** The frames of game time since the game began. */\nexport function now(): number {\n return memory.game.frame;\n}\n\nconst repeating = game(async (period: number, fn: (count: number) => void, times: number) => {\n let count = 0;\n while (times <= 0 || count < times) {\n await sleep(frames(period < 1 ? 1 : period));\n count++;\n fn(count);\n }\n});\n\nconst later = game(async (delay: number, fn: () => void) => {\n await sleep(frames(delay < 1 ? 1 : delay));\n fn();\n});\n\n/**\n * Run `fn` every `period` frames, from `period` frames from now: forever, or `times` times. It is handed how many times\n * it has run, 1 the first time.\n * @param period Frames between one run and the next (24 a second).\n * @param fn What to run; it runs to its end within the frame.\n * @param times How many times, or 0 (the default) for as long as the game lasts.\n * @returns The task: `cancel()` stops it.\n */\nexport const every = game((period: number, fn: (count: number) => void, times: number = 0): Task => start(repeating(period, fn, times)));\n\n/**\n * Run `fn` once, `delay` frames from now.\n * @param delay Frames from now (24 a second).\n * @param fn What to run; it runs to its end within the frame.\n * @returns The task: `cancel()` before then and it never runs.\n */\nexport const after = game((delay: number, fn: () => void): Task => start(later(delay, fn)));\n\n/**\n * The elements of `list`, `per` of them a frame: a loop spread over frames, for work that need not be done in one —\n * a thousand bullets moved, a map's cells looked at. What a frame costs stays what `per` turns cost.\n *\n * for await (const b of over(bullets, { per: 50 })) b.x += b.dx;\n *\n * It reads the list's length as it goes, as `for…of` does: what is pushed meanwhile is reached too, and an element\n * removed before it is reached moves the rest up, so one is passed over, as with an index. Each element is the list's\n * own: what is written to it stays. The loop goes on to the next frame only between elements, never after the last.\n * @param list The array to walk.\n * @param options `per`: how many elements a frame (1 at least).\n */\nexport async function* over(list: T[], options: { per: number }) {\n const per = options.per < 1 ? 1 : options.per;\n let n = 0;\n for (let i = 0; i < list.length; i++) {\n yield list[i];\n n++;\n if (n >= per && i + 1 < list.length) {\n n = 0;\n await nextFrame();\n }\n }\n}\n\n/**\n * 0, 1, … `count` − 1, `per` of them a frame: `over` for a count rather than an array.\n *\n * for await (const y of spread(96, { per: 8 })) drawRow(y); // twelve frames for 96 rows\n * @param count How many numbers, from 0.\n * @param options `per`: how many a frame (1 at least).\n */\nexport async function* spread(count: number, options: { per: number }) {\n const per = options.per < 1 ? 1 : options.per;\n let n = 0;\n for (let i = 0; i < count; i++) {\n yield i;\n n++;\n if (n >= per && i + 1 < count) {\n n = 0;\n await nextFrame();\n }\n }\n}\n\n/**\n * Something that can be done once every so many frames: a weapon's reload, a spell's cooldown.\n *\n * const blink = new Cooldown(5 * SECOND);\n * if (keyPressed(P1, \"B\") && blink.use()) teleport();\n */\nexport class Cooldown {\n period: number;\n until = 0;\n\n /** Ready at once, then once every `period` frames. */\n constructor(period: number) {\n this.period = period;\n }\n\n /** Whether it is ready now. */\n ready(): boolean {\n return now() >= this.until;\n }\n\n /** If it is ready, use it — it is ready again `period` frames from now — and say true; else false. */\n use(): boolean {\n if (now() < this.until) return false;\n this.until = now() + this.period;\n return true;\n }\n\n /** Frames until it is ready again: 0 when it is. */\n left(): number {\n return this.until > now() ? this.until - now() : 0;\n }\n\n /** Ready again at once. */\n reset() {\n this.until = 0;\n }\n}\n\n/**\n * A length of time that runs down while the game runs: a round's clock, a bomb's fuse.\n *\n * const round = new Countdown(90 * SECOND);\n * ui.lines(P1, () => {clock(round.left())});\n * await until(() => round.done());\n */\nexport class Countdown {\n end = 0;\n\n /** Running, done `length` frames from now (0: done already). */\n constructor(length: number = 0) {\n this.end = now() + length;\n }\n\n /** Start again, done `length` frames from now. */\n restart(length: number) {\n this.end = now() + length;\n }\n\n /** Frames left: 0 once it is done. */\n left(): number {\n return this.end > now() ? this.end - now() : 0;\n }\n\n /** Whether it has run out. */\n done(): boolean {\n return now() >= this.end;\n }\n\n /** More time, or less (a number below zero). */\n add(frames: number) {\n this.end += frames;\n }\n}\n\n/** How long since it was made, or last reset: a lap's time, a survival score. */\nexport class Stopwatch {\n from = 0;\n\n constructor() {\n this.from = now();\n }\n\n /** Frames since it started. */\n elapsed(): number {\n return now() - this.from;\n }\n\n /** From 0 again. */\n reset() {\n this.from = now();\n }\n}\n", "ui": "/**\n * A kit for the game's text lines: the standard library's \"trigscript/ui\" (TrigScript 4).\n *\n * import { Meter, Panel, MenuList, DialogBox, choose, say, clock } from \"trigscript/ui\";\n *\n * const ITEMS = [\"Marine (50)\", \"Medic (75)\", \"Leave\"];\n * const shop = { selected: 0, open: false };\n * const talk = { speaker: \"\", text: \"\", open: false };\n *\n * program(async () => {\n * ui.lines(P1, () => (\n * <>\n * \n * \n * \n * {shop.open && }\n * \n * \n * ));\n * await say(P1, talk, \"Raynor\", \"Hold the bridge until dawn.\");\n * const pick = await choose(P1, ITEMS.length, shop);\n * });\n *\n * The text lines are one player's: a view is shown to that player only, rebuilt when what it reads changes (a function\n * it calls included) and shown again once a second as it was, so a menu, a dialog and a meter cost nothing while nothing\n * changes. Give a player one `ui.lines` and\n * put everything in it, as the example does: the components are pieces of one view, and the state they show is a\n * record of yours (`shop`, `talk`) that the functions here change.\n *\n * What the text lines can show, played in StarCraft: Remastered (the UI spike, 2026-10-16): eleven lines; colour codes\n * take no width; ■ □ ● are one width; after grey on a line every later colour code is ignored, so grey goes last.\n */\nimport { keyPressed, nextFrame, sleep, frames, game, textColors, type Player, type TextColor } from \"trigscript\";\n\n/** Frames a second of game time on Fastest, the speed a map is played at. */\nconst PER_SECOND = 24;\n\n/** \"1:05\": frames of game time as minutes and seconds (24 frames a second, on Fastest). */\nexport function clock(frames: number): string {\n const seconds = Math.floor(frames / PER_SECOND);\n return `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, \"0\")}`;\n}\n\n/** \"12,345\": a whole number with its thousands marked. */\nexport function thousands(n: number): string {\n let rest = n < 0 ? -n : n;\n let out = \"\";\n while (rest >= 1000) {\n out = `,${String(rest % 1000).padStart(3, \"0\")}${out}`;\n rest = Math.floor(rest / 1000);\n }\n return `${n < 0 ? \"-\" : \"\"}${rest}${out}`;\n}\n\n/**\n * A labelled bar on a line of its own — \"Base ■■■■■■□□□□ 900/1500\" — green above half, yellow above a quarter, red below.\n * @param label What the bar is of.\n * @param value How much there is.\n * @param max How much there can be.\n * @param width How many squares the bar is (10 unless it says).\n */\nexport function Meter({ label, value, max, width }: { label: string; value: number; max: number; width?: number }) {\n return (\n \n {label} max ? textColors.green : value * 4 > max ? textColors.yellow : textColors.red} /> {value}/{max}\n \n );\n}\n\n/**\n * A title over what is inside it: \"-- Wave 3 --\" in its colour (yellow unless it says), then the lines inside.\n * @param title The title.\n * @param color The title's colour.\n */\nexport function Panel({ title, children, color }: { title: string; children?: Children; color?: TextColor }) {\n return (\n <>\n -- {title} --\n {children}\n \n );\n}\n\n/**\n * A menu's lines: each item on a line of its own, the one at `selected` marked \"> \" and in its colour (cyan unless it\n * says). `items` is a list the script has, or an array of texts of the program.\n */\nexport function MenuList({ items, selected, color }: { items: readonly string[]; selected: number; color?: TextColor }) {\n return (\n <>\n {items.map((item, i) => (\n \n {i === selected ? \"> \" : \" \"}\n {item}\n \n ))}\n \n );\n}\n\n/**\n * Let a player choose one of `count` things: the menu opens (`menu.open`), W and S (or Up and Down) move\n * `menu.selected` (round from the last to the first), and Space chooses — the place chosen, once it is. (Not Enter:\n * in a game of several players Enter opens the chat.) Show it with `MenuList`, in the\n * player's `ui.lines`: `{shop.open && }`.\n * @param player Who chooses: P1 … P8, known when the script is built.\n * @param count How many things there are to choose from.\n * @param menu The record a view shows: which is marked, and whether the menu is open.\n */\nexport const choose = game(async (player: Player, count: number, menu: { selected: number; open: boolean }): Promise => {\n menu.open = true;\n while (true) {\n if (keyPressed(player, \"W\") || keyPressed(player, \"Up\")) menu.selected = (menu.selected + count - 1) % count;\n if (keyPressed(player, \"S\") || keyPressed(player, \"Down\")) menu.selected = (menu.selected + 1) % count;\n if (keyPressed(player, \"Space\")) break;\n await nextFrame();\n }\n menu.open = false;\n return menu.selected;\n});\n\n/**\n * A text put into `into.text` a character at a time, one every `every` frames (2 unless it says): a view that shows\n * `into.text` shows it being typed. Done when the whole text is there.\n */\nexport const typewriter = game(async (into: { text: string }, text: string, every: number = 2) => {\n let n = 0;\n while (n < text.length) {\n n++;\n into.text = text.slice(0, n);\n await sleep(frames(every));\n }\n});\n\n/**\n * Someone says something to a player: the dialog opens (`box.open`), the speaker's name and the words typed in, and it\n * closes when the player presses Space. Show it with `DialogBox`.\n * @param player Who reads it: P1 … P8, known when the script is built.\n * @param box The record a view shows.\n * @param speaker Who speaks.\n * @param text What they say.\n */\nexport const say = game(async (player: Player, box: { speaker: string; text: string; open: boolean }, speaker: string, text: string) => {\n box.open = true;\n box.speaker = speaker;\n box.text = \"\";\n await typewriter(box, text);\n while (!keyPressed(player, \"Space\")) await nextFrame();\n box.open = false;\n});\n\n/** A dialog's lines while it is open: the speaker in yellow, the words, and what to press (Space). Nothing while it is closed. */\nexport function DialogBox({ box }: { box: { speaker: string; text: string; open: boolean } }) {\n return box.open ? (\n <>\n {box.speaker}\n {box.text}\n (Space)\n \n ) : null;\n}\n\n/** A line at the end of a log that keeps the last `keep` (6 unless it says), the oldest taken off the front. */\nexport const log = game((lines: string[], text: string, keep: number = 6) => {\n lines.push(text);\n while (lines.length > keep) lines.shift();\n});\n\n/** A log's lines, oldest first, each in its colour (white unless it says). */\nexport function LogLines({ lines, color }: { lines: readonly string[]; color?: TextColor }) {\n return <>{lines.map((text) => {text})};\n}\n", "workers": "/**\n * Workers that go to work: the standard library's \"trigscript/workers\" (TrigScript 4).\n *\n * import { autoMining } from \"trigscript/workers\";\n *\n * autoMining(); // at the top of the script, once: on for the whole game\n *\n * With auto-mining on, a human player's new SCV, Probe or Drone whose producer's rally point is on a mineral patch\n * starts mining that patch, instead of walking there and standing. One rallied to the player's own finished gas\n * building (Refinery, Extractor, Assimilator) starts gathering gas from it. Anywhere else — the ground, a bare geyser,\n * a gas building still being built or another player's, a patch mined out — it walks to the rally point as it always\n * does. It acts once, as the worker comes out: an order the player gives afterwards stands. Computer players' workers\n * and those a trigger makes (Create Unit) are left alone, and a busy patch or gas building is the game's to sort out,\n * as when the player right-clicks it.\n *\n * Played in StarCraft: Remastered (2026-11-07 and 2026-11-08): SCVs, Probes and Drones rallied to minerals and to gas\n * all went to work. How it sees a new worker, from `map trace` (2026-11-07): an SCV comes out of training with its\n * order changing from Nothing, and a Drone from ZergBirth, on the frame the rally is applied (a Probe was not traced;\n * in play it went to work as an SCV does). A rally on a patch gives the worker Move to the patch's exact centre with no target; one on a building gives\n * Follow with the building as the target. `gather()` then gives the order a right-click would: MoveToMinerals or\n * MoveToGas.\n */\nimport { program, on, isHuman, allUnits, units, orders, P1, P2, P3, P4, P5, P6, P7, P8, type Unit } from \"trigscript\";\n\nfunction ownerIsHuman(worker: Unit): boolean {\n const owner = worker.owner;\n if (owner == P1) return isHuman(P1);\n if (owner == P2) return isHuman(P2);\n if (owner == P3) return isHuman(P3);\n if (owner == P4) return isHuman(P4);\n if (owner == P5) return isHuman(P5);\n if (owner == P6) return isHuman(P6);\n if (owner == P7) return isHuman(P7);\n if (owner == P8) return isHuman(P8);\n return false;\n}\n\nfunction gatherFromRally(worker: Unit) {\n if (!ownerIsHuman(worker)) return;\n if (worker.orderId == orders.Follow) {\n const gasBuilding = worker.orderTarget;\n if (gasBuilding) if (gasBuilding.owner == worker.owner && gasBuilding.buildTime == 0) worker.gather(gasBuilding);\n return;\n }\n if (worker.orderId != orders.Move || worker.hasTarget) return;\n const x = worker.orderTargetX, y = worker.orderTargetY;\n for (const patch of allUnits({ type: units.MineralFieldType1 })) if (patch.x == x && patch.y == y) { worker.gather(patch); return; }\n for (const patch of allUnits({ type: units.MineralFieldType2 })) if (patch.x == x && patch.y == y) { worker.gather(patch); return; }\n for (const patch of allUnits({ type: units.MineralFieldType3 })) if (patch.x == x && patch.y == y) { worker.gather(patch); return; }\n}\n\nlet programBuilt = false;\n\n/**\n * Turn auto-mining on for the whole game: every human player's new SCV, Probe or Drone rallied to a mineral patch or\n * the player's own finished gas building starts gathering from it. Called at the top of a script: it makes a program,\n * and programs are made when the map is built, so a call inside a program is an error. A call after the first adds\n * nothing.\n */\nexport function autoMining() {\n if (programBuilt) return;\n programBuilt = true;\n program(async () => {\n on.order((worker, order, was) => {\n if (worker.type == units.ZergDrone) {\n if (was == orders.ZergBirth) gatherFromRally(worker);\n } else if (was == orders.Nothing) {\n if (worker.type == units.TerranSCV || worker.type == units.ProtossProbe) gatherFromRally(worker);\n }\n });\n });\n}\n" }; -export const STDLIB_EXTENSIONS: Readonly> = {"cutscene":".ts","fog":".ts","math":".ts","music":".ts","random":".ts","screen":".tsx","signals":".ts","spells":".ts","timers":".ts","ui":".tsx","workers":".ts"}; +export const STDLIB_EXTENSIONS: Readonly> = {"card":".ts","cutscene":".ts","fog":".ts","math":".ts","music":".ts","random":".ts","screen":".tsx","signals":".ts","spells":".ts","timers":".ts","ui":".tsx","workers":".ts"}; diff --git a/ledger/units/card-right-click.ts b/ledger/units/card-right-click.ts new file mode 100644 index 0000000..616dbee --- /dev/null +++ b/ledger/units/card-right-click.ts @@ -0,0 +1,16 @@ +// ledger: step 10, takes +// trigscript/card: a right-click on a command card button, in a single-player map: `new CardButton(techs.Restoration, cardSlots.Restoration)`, `await button.begin()` before the program's loop and `await button.rightClicked()` on each pass; `cardSetup()` at the top of a script. +import { CardButton, cardSlots, cardSetup } from "trigscript/card"; +cardSetup({ afterCalibration: () => print("Measured"), showMeasurements: true }); +program(async () => { + const button = new CardButton(techs.Restoration, cardSlots.Restoration); + await button.begin(); + let clicks = 0; + while (true) { + if (await button.rightClicked()) { + clicks += 1; + print(`${clicks}`, { to: CurrentPlayer }); + } + await nextFrame(); + } +}, { owner: AllPlayers }); diff --git a/ledger/units/spells-auto-restore-toggle.ts b/ledger/units/spells-auto-restore-toggle.ts new file mode 100644 index 0000000..1351bc4 --- /dev/null +++ b/ledger/units/spells-auto-restore-toggle.ts @@ -0,0 +1,4 @@ +// ledger: step 10, takes +// trigscript/spells: in a single-player map, the player turns auto-restore off and on by right-clicking Restore: `autoRestore({ allowPlayersToTurnOffAutoCast: true })`. +import { autoRestore } from "trigscript/spells"; +autoRestore({ allowPlayersToTurnOffAutoCast: true }); diff --git a/stdlib/card.ts b/stdlib/card.ts new file mode 100644 index 0000000..755b5fe --- /dev/null +++ b/stdlib/card.ts @@ -0,0 +1,245 @@ +/** + * A right-click on a command card button: the standard library's "trigscript/card" (TrigScript 4). Single-player maps. + * + * import { CardButton, cardSlots } from "trigscript/card"; + * + * program(async () => { + * const restore = new CardButton(techs.Restoration, cardSlots.Restoration); + * await restore.begin(); + * let on = true; + * while (true) { + * if (await restore.rightClicked()) { + * on = !on; + * print(on ? "On" : "Off", { to: CurrentPlayer }); + * } + * … + * await nextFrame(); + * } + * }, { owner: AllPlayers }); + * + * The game keeps no record of which card button a right-click landed on, and a right-click on a button does nothing in + * the game. So the button tests where the mouse was when the right button went down against where the button is drawn, + * while the card shows the buttons of the units that have the ability (a Medic's for Restoration). Make it inside a + * program run for the human player, and call rightClicked() once on every pass of the program's loop. + * + * Where a button is drawn depends on the graphics. Classic graphics draws the game 640 pixels wide; Remastered's draws it + * as wide as the display's shape (`local.screen.width`), and that width reads the same in both. The card sits against the + * right edge, so the button needs to know which graphics are on. At the start it centers the view on the middle of the + * map, reads where the camera stopped (half the drawn width left of the middle) and sends the view back; after that, a + * click on the minimap and the camera going further right than Remastered's can keep it current when the player switches + * graphics mid-game. Until it knows, a press on either layout's place for the button counts. + * + * Played in StarCraft: Remastered (2026-11-12), on a Mac in Classic and Remastered graphics: + * - Center View glides: the camera takes a few frames to arrive. A second Center View sent during the glide was lost, + * and moving a location and centering the view on it in one frame while the camera already sat there hung the game. + * - A Center View after an await in the same branch, or in a task beside the program, misbehaved (the view locked); + * each step here is its own pass of the caller's loop, and the only awaits are sync()s followed by nothing but sums. + * - The card's buttons: columns 132, 86 and 40 pixels in from the drawn width's right edge, rows at 352, 396 and 440, + * each about 31 by 30, in both graphics. + * - A minimap click lands the camera half the drawn width, and one minimap pixel's worth more, left of the spot clicked. + */ +import { sync, clicked, centerLocation, centerView, reserveLocation, print, memory, local, techs, CurrentPlayer, type Tech } from "trigscript"; + +/** The nine places on the command card, left to right and top to bottom, and each ability's own place. */ +export const cardSlots = { + topLeft: 0, + topMiddle: 1, + topRight: 2, + middleLeft: 3, + center: 4, + middleRight: 5, + bottomLeft: 6, + bottomMiddle: 7, + bottomRight: 8, + Restoration: 7, +}; + +const VIEW = reserveLocation("card: where the camera is sent to measure the screen"); + +let afterCalibrationHandler: (() => void) | undefined = undefined; +let showMeasurements = false; + +/** + * At the top of the script, before any CardButton: `afterCalibration` runs once the start's measurement is done, in + * place of sending the view back — where an opening cinematic starts. `showMeasurements` prints each step of it, and each + * right-click the button tests, as numbered lines. + */ +export function cardSetup(options: { afterCalibration?: () => void; showMeasurements?: boolean } = {}) { + afterCalibrationHandler = options.afterCalibration; + showMeasurements = options.showMeasurements === true; +} + +/** One button of the command card, watched for a right-click. */ +export class CardButton { + /** The button set the card shows for the units that have the ability: a Medic's for Restoration. */ + buttonSet = 0xffff; + /** How far in from the drawn width's right edge the button's column starts, and the top of its row. */ + fromRight = 132; + top = 352; + mapWidth = 0; + mapHeight = 0; + middleX = 0; + middleY = 0; + /** 0 the location is moved, 1 the view is sent, 2 waiting for it to settle, 3 read, 4 the location goes back, 5 the view goes back, 6 done. */ + phase = 6; + passes = 0; + startX = 0; + startY = 0; + settledX = 0; + settledY = 0; + /** Kept on this computer only: equal camera readings in a row while it settles, and the reading a pass ago. */ + steady = 0; + lastX = 0; + lastY = 0; + /** Kept on this computer only: 0 not known, 1 Classic graphics, 2 Remastered's. */ + graphics = 0; + pressed = 0; + wasDown = 0; + lastCameraX = 0; + line = 0; + + constructor(tech: Tech, slot: number) { + if (tech == techs.Restoration) this.buttonSet = 34; + const column = slot % 3; + if (column == 1) this.fromRight = 86; + if (column == 2) this.fromRight = 40; + if (slot >= 3) this.top = 396; + if (slot >= 6) this.top = 440; + } + + /** Before the program's loop: where the view starts, sent to every computer, so the view can be sent back there. */ + async begin() { + this.mapWidth = memory.map.width * 32; + this.mapHeight = memory.map.height * 32; + this.middleX = (this.mapWidth - this.mapWidth % 64) / 2; + this.middleY = (this.mapHeight - this.mapHeight % 64) / 2; + const [x, y] = await sync(CurrentPlayer, local.camera.x, local.camera.y); + this.startX = x; + this.startY = y; + this.lastCameraX = x; + if (showMeasurements) { + this.line += 1; + print(`#${this.line} card: the view starts at ${x},${y}; the map is ${this.mapWidth} by ${this.mapHeight} pixels`); + } + // A map narrower or shorter than this would stop the camera before it is centered, and the reading would mean nothing. + if (this.mapWidth >= 1024 && this.mapHeight >= 1024) this.phase = 0; + else { + if (showMeasurements) { + this.line += 1; + print(`#${this.line} card: the map is too small to measure the screen; both layouts count`); + } + const after = afterCalibrationHandler; + if (after) after(); + } + } + + /** On every pass of the program's loop: whether the player right-clicked the button since the last pass that said so. */ + async rightClicked(): Promise { + const cameraX = local.camera.x; + const cameraY = local.camera.y; + if (this.phase == 0) { + centerLocation(VIEW, this.middleX, this.middleY); + this.phase = 1; + } else if (this.phase == 1) { + centerView(VIEW); + this.phase = 2; + this.passes = 0; + this.steady = 0; + this.lastX = cameraX; + this.lastY = cameraY; + } else if (this.phase == 2) { + this.passes += 1; + if (cameraX == this.lastX && cameraY == this.lastY) this.steady += 1; + else this.steady = 0; + this.lastX = cameraX; + this.lastY = cameraY; + if (this.passes >= 16) this.phase = 3; + } + if (this.phase == 3) { + const [x, y] = await sync(CurrentPlayer, local.camera.x, local.camera.y); + this.settledX = x; + this.settledY = y; + this.phase = 4; + } else if (this.phase == 4) { + if (this.steady >= 2 && this.settledX + 296 <= this.middleX && this.settledX + 344 >= this.middleX) this.graphics = 1; + if (this.steady >= 2 && this.settledX + 352 <= this.middleX && this.settledX + 460 >= this.middleX) this.graphics = 2; + if (showMeasurements) { + this.line += 1; + print(`#${this.line} card: centered on the middle, the camera settled at ${this.settledX},${this.settledY} (${this.steady} equal readings)`); + this.line += 1; + if (this.graphics == 1) print(`#${this.line} card: Classic graphics, the camera ${this.middleX - this.settledX} left of the middle`); + else if (this.graphics == 2) print(`#${this.line} card: Remastered graphics, the camera ${this.middleX - this.settledX} left of the middle`); + else print(`#${this.line} card: graphics not known; both layouts count`); + } + const after = afterCalibrationHandler; + if (after) { + after(); + this.phase = 6; + } else if (this.settledX != this.startX || this.settledY != this.startY) { + centerLocation(VIEW, this.startX + this.middleX - this.settledX, this.startY + this.middleY - this.settledY); + this.phase = 5; + } else this.phase = 6; + } else if (this.phase == 5) { + centerView(VIEW); + this.phase = 6; + if (showMeasurements) { + this.line += 1; + print(`#${this.line} card: the view is sent back`); + } + } + + const width = local.screen.width; + const mouseX = local.mouse.x; + const mouseY = local.mouse.y; + const buttons = local.mouse.buttons; + if (this.phase == 6) { + const before = this.graphics; + // A minimap click or drag (a square map's minimap is 128 pixels across, from 6,348): the camera jumps to the spot + // under the cursor, half the drawn width and a minimap pixel left of it. + const scale = this.mapWidth / 128; + if (this.mapWidth == this.mapHeight && buttons % 4 >= 2 && mouseX >= 6 && mouseX <= 133 && mouseY >= 348 && mouseY <= 475 && (cameraX > this.lastCameraX + 96 || this.lastCameraX > cameraX + 96) && cameraX > 0) { + const spot = (mouseX - 6) * scale; + if (spot >= cameraX + scale + 296 && spot <= cameraX + scale + 344) this.graphics = 1; + if (spot >= cameraX + scale + 352 && spot <= cameraX + scale + 460) this.graphics = 2; + } + // Remastered's camera stops a drawn width short of the map's right edge; only Classic's gets further. + if (width > 648 && cameraX + width > this.mapWidth) this.graphics = 1; + if (showMeasurements && this.graphics != before) { + this.line += 1; + if (this.graphics == 1) print(`#${this.line} card: now Classic graphics, the camera at ${cameraX},${cameraY}`); + else print(`#${this.line} card: now Remastered graphics, the camera at ${cameraX},${cameraY}`); + } + } + this.lastCameraX = cameraX; + + const down = buttons % 16 >= 8 ? 1 : 0; + if (down == 1 && this.wasDown == 0) { + const inRow = mouseY >= this.top && mouseY < this.top + 30; + const inClassic = mouseX + this.fromRight >= 640 && mouseX + this.fromRight < 640 + 31; + const inRemastered = mouseX + this.fromRight >= width && mouseX + this.fromRight < width + 31; + let inSlot = false; + if (this.graphics == 1) inSlot = inClassic; + else if (this.graphics == 2) inSlot = inRemastered; + else inSlot = inClassic || inRemastered; + const shown = local.card.buttonSet; + this.pressed = shown == this.buttonSet && inRow && inSlot ? 1 : 0; + if (showMeasurements) { + this.line += 1; + const verdict = this.pressed == 1 ? "on the button" : "not on the button"; + if (shown == 34) print(`#${this.line} card: right button down at ${mouseX},${mouseY}, Medic buttons (34): ${verdict}`); + else if (shown == 0) print(`#${this.line} card: right button down at ${mouseX},${mouseY}, Marine buttons (0): ${verdict}`); + else if (shown == 244) print(`#${this.line} card: right button down at ${mouseX},${mouseY}, mixed group buttons (244): ${verdict}`); + else if (shown == 229) print(`#${this.line} card: right button down at ${mouseX},${mouseY}, Cancel, a targeting cursor is up (229): ${verdict}`); + else print(`#${this.line} card: right button down at ${mouseX},${mouseY}, button set (${shown}): ${verdict}`); + } + } + this.wasDown = down; + + if (clicked(CurrentPlayer, "right")) { + const onButton = await sync(CurrentPlayer, this.pressed); + this.pressed = 0; + return onButton == 1; + } + return false; + } +} diff --git a/stdlib/spells.ts b/stdlib/spells.ts index 2a9218d..03acf08 100644 --- a/stdlib/spells.ts +++ b/stdlib/spells.ts @@ -5,6 +5,7 @@ * * autoRestore(); // at the top of the script, once * autoRestore({ switch: "Medics trained", keepEnergy: 25 }); // or tied to a switch, keeping 25 energy + * autoRestore({ allowPlayersToTurnOffAutoCast: true }); // a single-player map: right-click Restore to turn it off * * With auto-restore on, a human player's Medic that is idle, or healing on its own from idle, casts Restoration on a * unit of its owner's, or of a player its owner has allied, that is within the spell's range and carries Lockdown, @@ -16,12 +17,17 @@ * alone, a unit in stasis or invincible, a hallucination (Restoration kills it) and the Medic itself are left alone. * Computer players' Medics are left to the computer, which casts Restoration itself. * + * With `allowPlayersToTurnOffAutoCast`, for single-player maps, the player turns auto-restore off and on again by + * right-clicking the Restore button on a Medic's command card ("trigscript/card"), and is told "Auto-restore off" or + * "Auto-restore on". It starts on. While the map's switch is cleared, the right-click does nothing and the setting is kept. + * * Played in StarCraft: Remastered (2026-11-11): the order written to the Medic (CastRestoration at the unit) is cast, * mid-heal too, and the Medic heals on afterwards. How it tells a Medic healing from idle from one the player sent, * from `map trace` (2026-11-11): either way the order is MedicHeal, but an attack-move or a patrol waits behind it in * the Medic's queue (`queuedOrders` 1 or 2), and the queue of one healing from idle holds nothing the game counts. */ -import { program, nextFrame, isHuman, switchIs, switches, allUnits, stats, units, orders, techs, weapons, P1, P2, P3, P4, P5, P6, P7, P8, type Player, type Unit } from "trigscript"; +import { program, nextFrame, isHuman, switchIs, switches, allUnits, stats, units, orders, techs, weapons, print, AllPlayers, CurrentPlayer, P1, P2, P3, P4, P5, P6, P7, P8, type Player, type Unit } from "trigscript"; +import { CardButton, cardSlots } from "trigscript/card"; function ownerIsHuman(medic: Unit): boolean { const owner = medic.owner; @@ -101,18 +107,29 @@ let programBuilt = false; /** * Turn auto-restore on: every human player's idle or self-healing Medic casts Restoration on the nearest afflicted * unit of its owner's or an ally's within range. `switch`: a switch of the map, by its name; - * auto-restore runs only while it is set. `keepEnergy`: the energy a Medic keeps after the cast (default 0). Called at + * auto-restore runs only while it is set. `keepEnergy`: the energy a Medic keeps after the cast (default 0). + * `allowPlayersToTurnOffAutoCast`: a single-player map's player turns it off and on by right-clicking Restore. Called at * the top of a script: it makes a program, and programs are made when the map is built, so a call inside a program is * an error. A call after the first adds nothing. */ -export function autoRestore(options: { switch?: keyof typeof switches; keepEnergy?: number } = {}) { +export function autoRestore(options: { switch?: keyof typeof switches; keepEnergy?: number; allowPlayersToTurnOffAutoCast?: boolean } = {}) { if (programBuilt) return; programBuilt = true; const keepEnergy = options.keepEnergy ?? 0; + const toggled = options.allowPlayersToTurnOffAutoCast === true; program(async () => { + // Run for every player, so the card's watcher and its view moves are the human's: the others' copies end here. + if (toggled && !isHuman(CurrentPlayer)) return; + let autoRestoreOn = true; + const restoreButton = new CardButton(techs.Restoration, cardSlots.Restoration); + if (toggled) await restoreButton.begin(); const sentMedics: Unit[] = []; const sentTargets: Unit[] = []; while (true) { + if (toggled && (await restoreButton.rightClicked()) && (options.switch === undefined || switchIs(switches[options.switch], "set"))) { + autoRestoreOn = !autoRestoreOn; + print(autoRestoreOn ? "Auto-restore on" : "Auto-restore off", { to: CurrentPlayer }); + } const range = stats(weapons.Restoration).range; for (let i = sentMedics.length - 1; i >= 0; i--) { const medic = sentMedics[i], target = sentTargets[i]; @@ -124,7 +141,7 @@ export function autoRestore(options: { switch?: keyof typeof switches; keepEnerg sentMedics.splice(i, 1); sentTargets.splice(i, 1); } - if (options.switch !== undefined && !switchIs(switches[options.switch], "set")) { + if ((options.switch !== undefined && !switchIs(switches[options.switch], "set")) || !autoRestoreOn) { await nextFrame(); continue; } @@ -172,5 +189,5 @@ export function autoRestore(options: { switch?: keyof typeof switches; keepEnerg } await nextFrame(); } - }); + }, toggled ? { owner: AllPlayers } : {}); } diff --git a/tests/stdlib.test.ts b/tests/stdlib.test.ts index ca731d1..1830e00 100644 --- a/tests/stdlib.test.ts +++ b/tests/stdlib.test.ts @@ -2,7 +2,7 @@ * The standard library (TrigScript 4): modules of TrigScript shipped with the compiler and imported as * "trigscript/" — fog first. What joins a script's files and when, the location a library reserves, and what the * fog's functions compile to: a picture's brushes, read back out of the IR, light the picture's tiles. Workers' - * autoMining() and spells' autoRestore(). + * autoMining(), spells' autoRestore(), and card's right-click on a button. */ import { readdirSync, readFileSync } from "node:fs"; import { join } from "node:path"; @@ -381,3 +381,84 @@ describe("trigscript/spells", () => { expect(r.ir.map((p) => p.at?.file)).toEqual(["__trigscript__/spells.ts"]); }); }); + +/** A script's own tests, run in the simulator: the outcome of each by its name. */ +const tested = (main: string): Record => { + const r = compileScript(ts, { "main.ts": main }, NAMES, { lib: LIB, tests: { world: { locations: { 1: { left: 100, top: 100, right: 200, bottom: 200 } }, unitStats: {} } } }); + expect(r.diagnostics.map((d) => `${d.file}:${d.line}: ${d.message}`)).toEqual([]); + return Object.fromEntries((r.tests?.results ?? []).map((t) => [t.id.replace(/^.*?::/, ""), t.status === "failed" ? `failed: ${t.message}` : t.status])); +}; +/** Restore's button in Classic graphics, on the map with the camera at its corner: the middle of the bottom row. */ +const RESTORE = "sim.moveMouse(569, 455)"; + +describe("trigscript/card", () => { + const PROGRAM = `program(async () => { + const button = new CardButton(techs.Restoration, cardSlots.Restoration); + await button.begin(); + let clicks = 0; + while (true) { + if (await button.rightClicked()) { clicks += 1; print(\`click \${clicks}\`, { to: CurrentPlayer }); } + await nextFrame(); + } +}, { owner: AllPlayers });`; + + it("says yes to a right-click on the slot while the card shows the ability's units, and to nothing else", () => { + expect(tested(`import { test, expect, program, print, nextFrame, AllPlayers, CurrentPlayer, P1, units, techs, type Location } from "trigscript"; +import { CardButton, cardSlots } from "trigscript/card"; +${PROGRAM} +test("clicks", (sim) => { + const medics = sim.place(P1, units.TerranMedic, 1 as Location); + const marines = sim.place(P1, units.TerranMarine, 1 as Location); + sim.frames(5).selectUnits(medics); + ${RESTORE}.click("right").frames(10); + sim.moveMouse(615, 455).click("right").frames(10); + sim.selectUnits(marines); + ${RESTORE}.click("right").frames(10); + sim.selectUnits(medics); + ${RESTORE}.click("left").frames(10); + ${RESTORE}.click("right").frames(10); + expect(sim.printed(P1).join("|")).toBe("click 1|click 2"); +});`)).toEqual({ clicks: "passed" }); + }); + + it("cardSetup: afterCalibration runs once the start's measurement is done, and showMeasurements numbers its lines", () => { + expect(tested(`import { test, expect, program, print, nextFrame, AllPlayers, CurrentPlayer, P1, techs } from "trigscript"; +import { CardButton, cardSlots, cardSetup } from "trigscript/card"; +cardSetup({ afterCalibration: () => print("measured"), showMeasurements: true }); +${PROGRAM} +test("after", (sim) => { + sim.frames(60); + expect(sim.printed(P1).join("|")).toContain("#1 card: the view starts at 0,0"); + expect(sim.printed(P1).join("|")).toContain("measured"); +});`)).toEqual({ after: "passed" }); + }); +}); + +describe("trigscript/spells: allowPlayersToTurnOffAutoCast", () => { + const SCRIPT = (options: string, body: string) => `import { test, expect, P1, units, switches, type Location } from "trigscript"; +import { autoRestore } from "trigscript/spells"; +autoRestore(${options}); +test("toggle", (sim) => { + const medics = sim.place(P1, units.TerranMedic, 1 as Location); + sim.frames(5).selectUnits(medics); +${body} +});`; + + it("a right-click on Restore turns auto-restore off, and another on again, told to the player", () => { + expect(tested(SCRIPT("{ allowPlayersToTurnOffAutoCast: true }", ` ${RESTORE}.click("right").frames(10); + ${RESTORE}.click("right").frames(10); + expect(sim.printed(P1).join("|")).toBe("Auto-restore off|Auto-restore on");`))).toEqual({ toggle: "passed" }); + }); + + it("while the map's switch is cleared, the right-click is ignored and says nothing", () => { + expect(tested(SCRIPT(`{ switch: "Switch3", allowPlayersToTurnOffAutoCast: true }`, ` ${RESTORE}.click("right").frames(10); + expect(sim.printed(P1)).toEqual([]);`))).toEqual({ toggle: "passed" }); + }); + + it("without the option, a right-click on Restore does nothing, and the program is Player 1's as before", () => { + expect(tested(SCRIPT("{}", ` ${RESTORE}.click("right").frames(10); + expect(sim.printed(P1)).toEqual([]);`))).toEqual({ toggle: "passed" }); + const plain = JSON.stringify(ok(`import { autoRestore } from "trigscript/spells";\nautoRestore();`).ir[0]); + expect(plain).not.toContain(String(0x68c14c)); + }); +});