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
2 changes: 1 addition & 1 deletion compiler/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,7 @@ export interface Sim {
/** A line of chat, for the next frame: what chatted() finds. */
type(line: string, player?: Player): this;
moveMouse(x: number, y: number, player?: Player): this;
/** The player's screen to a place on the map, its top left corner in pixels: what local.camera reads. The mouse stays where it is on the map. */
/** The player's screen to a place on the map, its top left corner in pixels: what local.camera reads. The mouse stays where it is on the map. centerView() moves it too. */
moveCamera(x: number, y: number, player?: Player): this;
/** The player selects these units (up to twelve), as a drag would at their computer: what local.selected reads. */
selectUnits(units: readonly SimUnit[], player?: Player): this;
Expand Down
30 changes: 27 additions & 3 deletions compiler/simulateIr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1709,11 +1709,34 @@ export class ProgramSimulation {
for (const v of this.vision.values()) for (let i = 0; i < v.length; i++) if (v[i] === 2) v[i] = 1;
for (const u of this.world.game.units) if (u.alive) this.reveal(u);
}
/** The map's size in tiles: what `memory.map.width` and `memory.map.height` read, else 128 by 128. */
private mapTiles(): { width: number; height: number } {
return {
width: this.tableOf?.({ name: "memory.map.width", base: 0x57f1d4, stride: 0, index: 0, width: 2 }) || 128,
height: this.tableOf?.({ name: "memory.map.height", base: 0x57f1d6, stride: 0, index: 0, width: 2 }) || 128,
};
}

/**
* Center View: the player's 640 × 480 screen (Classic graphics) centered on the location, snapped down to 8 pixels and
* kept on the map. The mouse stays where it is on the screen.
*/
private centerCamera(player: number, location: number): void {
const { x, y } = this.game.centreOf(location);
const map = this.mapTiles();
const kept = (value: number, most: number) => Math.max(0, Math.min(value, most)) & ~7;
// Played in Classic graphics: Center View stops the screen 648 pixels short of the map's right edge and 488 short of its bottom.
const now = { x: kept(x - 320, map.width * 32 - 648), y: kept(y - 240, map.height * 32 - 488) };
const was = this.cameras.get(player) ?? { x: 0, y: 0 };
const mouse = this.mice.get(player);
if (mouse) this.mice.set(player, { x: mouse.x + now.x - was.x, y: mouse.y + now.y - was.y });
this.cameras.set(player, now);
}

/** What a player sees of the fog, as GameSim.fog gives it from the game: "#" now, "+" before, "." never. */
fog(player: number, area?: { x: number; y: number; width: number; height: number }): string[] {
const w = this.tableOf?.({ name: "memory.map.width", base: 0x57f1d4, stride: 0, index: 0, width: 2 }) || 128;
const h = this.tableOf?.({ name: "memory.map.height", base: 0x57f1d6, stride: 0, index: 0, width: 2 }) || 128;
const { x = 0, y = 0, width = w, height = h } = area ?? {};
const map = this.mapTiles();
const { x = 0, y = 0, width = map.width, height = map.height } = area ?? {};
const v = this.visionOf(player), rows: string[] = [];
for (let ty = y; ty < Math.min(y + height, FOG_SIZE); ty++) {
let row = "";
Expand Down Expand Up @@ -1765,6 +1788,7 @@ export class ProgramSimulation {
if (a.unitId === ResourceType.Gas || a.unitId === ResourceType.OreAndGas) stock[1] = set(stock[1]);
}
}
if (a.type === ActionType.CenterView) this.centerCamera(run.player, a.location);
switch (a.type) {
case ActionType.SetDeaths:
this.world.changeDeath(a.player, a.unitId, a.modifier, a.target);
Expand Down
6 changes: 5 additions & 1 deletion compiler/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ export interface TestWorld {
stackDepth?: number;
/** Unit types by their name in lower case, for a `{…:unit}` typed in chat. */
unitNames?: Record<string, number>;
/** The map's size in tiles: what `memory.map.width` and `memory.map.height` read, and where Center View stops the camera. */
mapSize?: { width: number; height: number };
}

export interface TestRunOptions {
Expand Down Expand Up @@ -377,7 +379,9 @@ export class TestSim implements TestWorldLike {
...(as !== undefined ? { player: as } : options.players ? {} : { player: ir[0]?.owner !== undefined && ir[0].owner < 12 ? ir[0].owner : undefined }),
});
const names = options.unitNames ?? {};
this.programs = new ProgramSimulation(ir, { world: this.world, strings, random, heapCells: options.heapCells, stackDepth: options.stackDepth, unitStats: (type) => stats[type], unitByName: (lower) => names[lower] });
this.programs = new ProgramSimulation(ir, { world: this.world, strings, random, heapCells: options.heapCells, stackDepth: options.stackDepth, unitStats: (type) => stats[type], unitByName: (lower) => names[lower],
table: (cell) => (cell.name === "memory.map.width" ? options.mapSize?.width : cell.name === "memory.map.height" ? options.mapSize?.height : undefined),
});
}

/** How many frames have run: the game's frame number. */
Expand Down
2 changes: 1 addition & 1 deletion service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ export class ScriptService {
});
const computers = scn.playerTypes.slice(0, 8).flatMap((type, slot) => (type === OWNER_COMPUTER ? [slot] : []));
const settings = this.settings();
return { units, locations, ...(players.length ? { players, forces, computers } : {}), unitStats, properties, heapCells: settings.heapCells, stackDepth: settings.stackDepth };
return { units, locations, ...(players.length ? { players, forces, computers } : {}), unitStats, properties, heapCells: settings.heapCells, stackDepth: settings.stackDepth, mapSize: { width: scn.width, height: scn.height } };
}

/** With the map's setting on, a failing test refuses the build: said as the first of them, and how many. */
Expand Down
25 changes: 25 additions & 0 deletions tests/testing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,28 @@ test("rolls", (sim) => { ${seed} sim.frames(5); expect(sim.program("dice").sum).
expect(got(run(script("sim.seed(9);")))).not.toBe(got(run(script(""))));
});
});

describe("the camera", () => {
it("moves on Center View, centered on the location and kept on the map, the mouse staying on the screen", () => {
const r = run(`import { test, expect, program, centerLocation, centerView, print, local, type Location } from "trigscript";
const VIEW = 1 as Location;
program(() => {
centerLocation(VIEW, 1024, 1024);
centerView(VIEW);
print(\`middle \${local.camera.x}, \${local.camera.y}; mouse \${local.mouse.x}, \${local.mouse.y}\`);
centerLocation(VIEW, 2048, 2048);
centerView(VIEW);
print(\`corner \${local.camera.x}, \${local.camera.y}\`);
centerLocation(VIEW, 0, 0);
centerView(VIEW);
print(\`origin \${local.camera.x}, \${local.camera.y}\`);
});
test("Center View", (sim) => {
sim.moveMouse(100, 50);
sim.frames(2);
});`, { world: { ...WORLD, mapSize: { width: 64, height: 64 } } });
expect(r.diagnostics.map((d) => d.message)).toEqual([]);
// Played on a 64 × 64 map in Classic graphics.
expect(r.tests!.results[0].printed).toEqual(["middle 704, 784; mouse 100, 50", "corner 1400, 1560", "origin 0, 0"]);
});
});