Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions compiler/eudError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
6 changes: 4 additions & 2 deletions compiler/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`); };
Expand Down
2 changes: 1 addition & 1 deletion compiler/simulateIr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
11 changes: 7 additions & 4 deletions compiler/tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });

Expand Down Expand Up @@ -94,8 +97,8 @@ export const TABLE_FIELDS: Record<TableKind, TableField[]> = {
],
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 } } },
],
};

Expand Down
1 change: 1 addition & 0 deletions tests/eud-error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"] },
Expand Down
12 changes: 12 additions & 0 deletions tests/ownGame.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
5 changes: 5 additions & 0 deletions tests/units.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down