diff --git a/compiler/declarations.ts b/compiler/declarations.ts index 5c4c051..ba29003 100644 --- a/compiler/declarations.ts +++ b/compiler/declarations.ts @@ -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; diff --git a/compiler/simulateIr.ts b/compiler/simulateIr.ts index 509a8cb..ec71953 100644 --- a/compiler/simulateIr.ts +++ b/compiler/simulateIr.ts @@ -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 = ""; @@ -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); diff --git a/compiler/testing.ts b/compiler/testing.ts index 91ece3c..434bbb0 100644 --- a/compiler/testing.ts +++ b/compiler/testing.ts @@ -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; + /** 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 { @@ -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. */ diff --git a/service.ts b/service.ts index b5eae38..a608934 100644 --- a/service.ts +++ b/service.ts @@ -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. */ diff --git a/tests/testing.test.ts b/tests/testing.test.ts index 75dc2b1..f9b3ba5 100644 --- a/tests/testing.test.ts +++ b/tests/testing.test.ts @@ -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"]); + }); +});