From 4bedf0007337ff4a65f7fd942acd1d3bb7b16d7f Mon Sep 17 00:00:00 2001 From: Matt Hunt Date: Wed, 11 Nov 2026 17:52:32 -0500 Subject: [PATCH] stats(P).researched and .upgrades read Brood War's own rows for its ids The game keeps Brood War's techs (ids 24-43) and upgrades (ids 46-60) in tables of their own, 20 and 15 a player wide. stats() had one table for each, 24 and 46 wide, so P1's Restoration was P2's Stim Packs, and P12's ran past the table. A Brood War key now lands in its row of techResearchedBW (0x58F140) or currentUpgradeLevelBW (0x58F32C), the places BWAPI's BWGame.h gives. The simulator keys a cell by its row's base too, so a Brood War id and the original id at the same place in the row stay apart, and placeOfAddress names the Brood War rows. Fixes #91. Co-Authored-By: Claude Opus 5.5 --- compiler/eudError.ts | 4 ++++ compiler/runtime.ts | 6 ++++-- compiler/simulateIr.ts | 2 +- compiler/tables.ts | 11 +++++++---- tests/eud-error.test.ts | 1 + tests/ownGame.test.ts | 12 ++++++++++++ tests/units.test.ts | 5 +++++ 7 files changed, 34 insertions(+), 7 deletions(-) diff --git a/compiler/eudError.ts b/compiler/eudError.ts index e42a7568..f37db7fd 100644 --- a/compiler/eudError.ts +++ b/compiler/eudError.ts @@ -108,6 +108,10 @@ export function placeOfAddress(address: number, list?: Allowlist): string { const i = Math.floor(off / f.stride); if (i < (f.keyed ? 12 : TABLE_SIZE_OF[kind])) return `stats(): the ${kind} table's ${f.name}, entry ${i}`; } + for (const f of fields) { + const rows = f.keyed?.broodWar, off = rows ? address - rows.base : -1; + if (rows && off >= 0 && off < 12 * rows.stride) return `stats(): the ${kind} table's ${f.name}, entry ${Math.floor(off / rows.stride)}`; + } } const named = list?.entries.find((e) => !/^0x[0-9a-f]+$/i.test(e.name) && covers(e, address)); return named ? named.name : "no table TrigScript knows"; diff --git a/compiler/runtime.ts b/compiler/runtime.ts index 664a560a..2cec4eb7 100644 --- a/compiler/runtime.ts +++ b/compiler/runtime.ts @@ -665,9 +665,11 @@ export function createRuntime(names: ScriptNames, collector: Collector, options: /* ── The game's tables ── */ const tableValue = (kind: TableKind, field: TableField, index: number, ident: string, key?: number): TableValue => { + const broodWar = field.keyed?.broodWar; + const row = broodWar && key !== undefined && key >= broodWar.from ? { base: broodWar.base, stride: broodWar.stride, key: key - broodWar.from } : { base: field.base, stride: field.stride, key }; const cell: TableCell = { - name: `${kind}.${field.name}`, base: field.base, stride: field.stride, index, width: field.width, - ...(key !== undefined ? { key } : {}), ...(field.bit !== undefined ? { bit: field.bit } : {}), ...(field.scale ? { scale: field.scale } : {}), + name: `${kind}.${field.name}`, base: row.base, stride: row.stride, index, width: field.width, + ...(row.key !== undefined ? { key: row.key } : {}), ...(field.bit !== undefined ? { bit: field.bit } : {}), ...(field.scale ? { scale: field.scale } : {}), ...(kind === "player" ? { player: true } : {}), ...(field.special ? { special: field.special } : {}), }; const fail = (): never => { throw new ScriptError(`${ident} is a value the game holds: it has no value when the script is built. Inside program(), assign to it, assign it to a let, or use it in the program's own arithmetic and comparisons.`); }; diff --git a/compiler/simulateIr.ts b/compiler/simulateIr.ts index 509a8cbd..8d7d159e 100644 --- a/compiler/simulateIr.ts +++ b/compiler/simulateIr.ts @@ -1660,7 +1660,7 @@ export class ProgramSimulation { centre(location: number, x: number, y: number, size?: { width: number; height: number }): void { this.game.centre(location, x, y, size); } private cellKey(c: TableCell): string { - return `${c.name}:${c.player ? this.slotOf(c.index) : c.index}${c.key !== undefined ? `:${c.key}` : ""}`; + return `${c.name}:${c.player ? this.slotOf(c.index) : c.index}${c.key !== undefined ? `:${c.base.toString(16)}:${c.key}` : ""}`; } /** A cell of the game's tables, in the script's units: what a program wrote, else the caller's answer, else 0. */ diff --git a/compiler/tables.ts b/compiler/tables.ts index f9b1b2d9..f90a0ffb 100644 --- a/compiler/tables.ts +++ b/compiler/tables.ts @@ -38,10 +38,13 @@ export interface TableField { special?: "speed" | "color" | "name"; /** The TypeScript type of the property, when it is not `number` / `boolean`. */ type?: string; - /** A second index: `stats(P1).upgrades[upgrades.U238Shells]`. The cell is `base + index × stride + key`. */ - keyed?: { kind: TableKind; type: string }; + /** A second index: `stats(P1).upgrades[upgrades.U238Shells]`. The cell is `base + index × stride + key`, from `broodWar`'s base and stride for its keys. */ + keyed?: { kind: TableKind; type: string; broodWar?: BroodWarRows }; } +/** Brood War's techs and upgrades are rows of their own table: a key from `from` up is `key - from` in it. */ +interface BroodWarRows { from: number; base: number; stride: number } + const UNIT_FLAGS = 0x664080; const flag = (name: string, bit: number, doc: string): TableField => ({ name, doc, base: UNIT_FLAGS, stride: 4, width: "bit", bit, boolean: true }); @@ -94,8 +97,8 @@ export const TABLE_FIELDS: Record = { ], player: [ { name: "color", doc: "The colour the player's units and minimap dots are drawn in: one of colors.*, or \"teal\". Takes effect at once.", base: 0x581d76, stride: 1, width: 1, writeOnly: true, special: "color", type: "PlayerColor | ColorName" }, - { name: "upgrades", doc: "The player's level of each upgrade: stats(P1).upgrades[upgrades.TerranInfantryWeapons] = 3.", base: 0x58d2b0, stride: 46, width: 1, keyed: { kind: "upgrade", type: "Upgrade" } }, - { name: "researched", doc: "Whether the player has each technology: stats(P1).researched[techs.Lockdown] = true.", base: 0x58cf44, stride: 24, width: 1, boolean: true, keyed: { kind: "tech", type: "Tech" } }, + { name: "upgrades", doc: "The player's level of each upgrade: stats(P1).upgrades[upgrades.TerranInfantryWeapons] = 3.", base: 0x58d2b0, stride: 46, width: 1, keyed: { kind: "upgrade", type: "Upgrade", broodWar: { from: 46, base: 0x58f32c, stride: 15 } } }, + { name: "researched", doc: "Whether the player has each technology: stats(P1).researched[techs.Lockdown] = true.", base: 0x58cf44, stride: 24, width: 1, boolean: true, keyed: { kind: "tech", type: "Tech", broodWar: { from: 24, base: 0x58f140, stride: 20 } } }, ], }; diff --git a/tests/eud-error.test.ts b/tests/eud-error.test.ts index 1b45ca77..f1082e6c 100644 --- a/tests/eud-error.test.ts +++ b/tests/eud-error.test.ts @@ -47,6 +47,7 @@ describe("where an address lies", () => { expect(placeOfAddress(0x640b60 + 218 * 2 + 5)).toBe("text lines, line 3 (+5)"); expect(placeOfAddress(0x6509b0)).toBe("current player"); expect(placeOfAddress(0x663888 + 2 * 5)).toBe("stats(): the unit table's minerals, entry 5"); + expect(placeOfAddress(0x58f140 + 20 * 2 + 3)).toBe("stats(): the player table's researched, entry 2"); const list: Allowlist = { version: 1, entries: [ { name: "0x700000", base: 0x700000, width: 16, read: "allowed", write: "allowed", evidence: [], from: "dump" }, { name: "the game's clock", base: 0x700000, width: 4, read: "allowed", write: "ends", evidence: ["probe 9"] }, diff --git a/tests/ownGame.test.ts b/tests/ownGame.test.ts index 81dd09c4..99546618 100644 --- a/tests/ownGame.test.ts +++ b/tests/ownGame.test.ts @@ -167,6 +167,18 @@ describe("a map of our own, the game's tables", () => { expect(w.target).toBe(3 * (w.location / 0xff)); }); + it("writes Brood War's techs and upgrades into Brood War's own rows", () => { + const address = (statement: string) => { + const [w] = actions(own(statement).graph); + return (0x58a364 + 4 * w.player + [0xff, 0xff00, 0xff0000, 0xff000000].indexOf(w.location)) >>> 0; + }; + expect(address(`stats(P2).researched[techs.StimPacks] = true;`)).toBe(0x58cf44 + 24); + expect(address(`stats(P1).researched[techs.Restoration] = true;`)).toBe(0x58f140); + expect(address(`stats(P12).researched[techs.Restoration] = true;`)).toBe(0x58f140 + 20 * 11); + expect(address(`stats(P1).upgrades[upgrades.ProtossGroundArmor] = 1;`)).toBe(0x58d2b0 + 5); + expect(address(`stats(P3).upgrades[upgrades.CaduceusReactor] = 1;`)).toBe(0x58f32c + 15 * 2 + 5); + }); + it("writes a flag as its bit, and reads a scaled field divided by its scale", () => { const [w] = actions(own(`stats(units.TerranMarine).detector = true;`).graph); expect(w.location.toString(2).replace(/0/g, "")).toBe("1"); diff --git a/tests/units.test.ts b/tests/units.test.ts index 1908be4b..75015ab8 100644 --- a/tests/units.test.ts +++ b/tests/units.test.ts @@ -201,6 +201,11 @@ describe("stats(): the game's tables", () => { expect([sim.value("cost"), sim.value("time"), sim.value("cloaked"), sim.value("other"), sim.value("level"), sim.value("has")]).toEqual([30, 1, true, 50, 3, true]); expect(sim.tables.get("unit.buildTime:0")).toBe(23); }); + it("a Brood War tech is a cell of its own, apart from the original tech at its place in the row", () => { + const sim = run(`stats(P2).researched[techs.StimPacks] = true; let restoration = stats(P1).researched[techs.Restoration]; let reactor = stats(P1).upgrades[upgrades.CaduceusReactor]; + stats(P1).researched[techs.Restoration] = true; let after = stats(P1).researched[techs.Restoration]; let stim = stats(P1).researched[techs.StimPacks];`); + expect([sim.value("restoration"), sim.value("reactor"), sim.value("after"), sim.value("stim")]).toEqual([false, 0, true, false]); + }); it("a cell holds what its width allows", () => { const sim = run(`let big = 300; stats(units.TerranMarine).armor = big; stats(units.ZergZergling).name = "Dog"; stats(CurrentPlayer).color = colors.green;`); expect(sim.tables.get("unit.armor:0")).toBe(255);