From 16e4b9f60d9e3a4c98303069b84db4c5a404c46d Mon Sep 17 00:00:00 2001 From: Z8MB1E Date: Sun, 11 Oct 2026 22:46:36 -0400 Subject: [PATCH] feat(minigames): rotate the L ammo depot around its fixed elbow anchor The L now pivots on its elbow (corner) cell instead of being re-laid-out in its bounding box, so rotating keeps that one cell in place instead of shifting the whole piece. Orientation 0 is the L look with the elbow at the bottom-left and arms up and to the right. Cells are the anchor plus two arm vectors rotated in quarter-turns, so the anchor is always occupied. The editor recovers a stored emplacement's (anchor, rotation) by matching its cells against every candidate, and the engine test asserts the anchor cell is present in all four orientations. --- .../call-for-fire-versus/VersusMatch.tsx | 27 ++++++---- .../minigames/call-for-fire-versus/engine.ts | 30 ++++++----- .../call-for-fire-versus-registry.int.spec.ts | 2 +- tests/int/call-for-fire-versus.int.spec.ts | 51 +++++++++++-------- 4 files changed, 62 insertions(+), 48 deletions(-) diff --git a/src/components/frontend/minigames/call-for-fire-versus/VersusMatch.tsx b/src/components/frontend/minigames/call-for-fire-versus/VersusMatch.tsx index d0839aae..2c11d0b5 100644 --- a/src/components/frontend/minigames/call-for-fire-versus/VersusMatch.tsx +++ b/src/components/frontend/minigames/call-for-fire-versus/VersusMatch.tsx @@ -139,20 +139,25 @@ function PlacementPhase({ for (const fob of match.you.fleet) { const definition = FOB_FLEET.find((d) => d.id === fob.id); if (!definition || fob.cells.length === 0) continue; - const origin = { - x: Math.min(...fob.cells.map((cell) => cell.x)), - y: Math.min(...fob.cells.map((cell) => cell.y)), - }; const turns = turnsForShape(definition.shape); - let orientation: FobRotation = 0; - for (let t = 0; t < turns; t += 1) { - const cells = cellsForFob(definition, origin, t as FobRotation, BOARD); - if (cells && sameCellSet(cells, fob.cells)) { - orientation = t as FobRotation; - break; + let placement: FobPlacement | null = null; + // Recover the (anchor, rotation) that reproduces the stored cells; the + // anchor can be any piece cell (line start, square corner, L elbow). + for (const anchor of fob.cells) { + for (let t = 0; t < turns; t += 1) { + const cells = cellsForFob(definition, anchor, t as FobRotation, BOARD); + if (cells && sameCellSet(cells, fob.cells)) { + placement = { assetId: fob.id, origin: anchor, orientation: t as FobRotation }; + break; + } } + if (placement) break; } - initial[fob.id] = { assetId: fob.id, origin, orientation }; + initial[fob.id] = placement ?? { + assetId: fob.id, + origin: fob.cells[0], + orientation: 0, + }; } return initial; }); diff --git a/src/lib/minigames/call-for-fire-versus/engine.ts b/src/lib/minigames/call-for-fire-versus/engine.ts index 14945df6..8c64969a 100644 --- a/src/lib/minigames/call-for-fire-versus/engine.ts +++ b/src/lib/minigames/call-for-fire-versus/engine.ts @@ -62,20 +62,13 @@ export function createVersusState( }; } -/** Base cells of the 3-cell L within its 2x2 box, at rotation 0. */ -const L_BASE: readonly GridPoint[] = [ - { x: 0, y: 0 }, - { x: 0, y: 1 }, - { x: 1, y: 1 }, -]; - -/** Rotate a cell by `turns` quarter-turns clockwise within a square box. */ -function rotateQuarterTurn(cell: GridPoint, turns: number, box: number): GridPoint { - let x = cell.x; - let y = cell.y; +/** Rotate an offset vector by `turns` quarter-turns clockwise (screen coords). */ +function rotateVector(vector: GridPoint, turns: number): GridPoint { + let x = vector.x; + let y = vector.y; const normalized = ((turns % 4) + 4) % 4; for (let t = 0; t < normalized; t += 1) { - const nextX = box - 1 - y; + const nextX = -y; const nextY = x; x = nextX; y = nextY; @@ -99,7 +92,14 @@ function relativeCells( return cells; } if (shape === "l") { - return L_BASE.map((cell) => rotateQuarterTurn(cell, orientation, 2)); + // Anchored on the elbow (the corner cell), so rotating keeps that cell + // fixed on the board and the two arms swing around it. Orientation 0 looks + // like an L with the elbow at its bottom-left, arms up and to the right. + return [ + { x: 0, y: 0 }, + rotateVector({ x: 0, y: -1 }, orientation), + rotateVector({ x: 1, y: 0 }, orientation), + ]; } const horizontal = orientation % 2 === 0; const cells: GridPoint[] = []; @@ -111,7 +111,9 @@ function relativeCells( /** * Cells a placed emplacement covers, or null when it runs off the board. The - * shape drives the footprint; orientation is quarter-turns clockwise. + * shape drives the footprint and orientation is quarter-turns clockwise. + * `origin` is the anchor kept fixed while rotating: a line's first cell, the + * square's top-left, or the L's elbow. For the L, `origin` is the elbow cell. */ export function cellsForPlacement( origin: GridPoint, diff --git a/tests/int/call-for-fire-versus-registry.int.spec.ts b/tests/int/call-for-fire-versus-registry.int.spec.ts index 86aa9f40..cfc6445c 100644 --- a/tests/int/call-for-fire-versus-registry.int.spec.ts +++ b/tests/int/call-for-fire-versus-registry.int.spec.ts @@ -16,7 +16,7 @@ import { computeVersusStatsDelta, readVersusStats, xpForVersus } from "@/lib/min function fleetAt(x: number, y: number): FobPlacement[] { return [ { assetId: "command-post", origin: { x, y }, orientation: 0 }, - { assetId: "ammo-depot", origin: { x, y: y + 2 }, orientation: 0 }, + { assetId: "ammo-depot", origin: { x, y: y + 3 }, orientation: 0 }, { assetId: "radar-site", origin: { x, y: y + 4 }, orientation: 0 }, { assetId: "fob", origin: { x, y: y + 5 }, orientation: 0 }, ]; diff --git a/tests/int/call-for-fire-versus.int.spec.ts b/tests/int/call-for-fire-versus.int.spec.ts index dcbb4884..8d65db48 100644 --- a/tests/int/call-for-fire-versus.int.spec.ts +++ b/tests/int/call-for-fire-versus.int.spec.ts @@ -16,13 +16,14 @@ import { /** * A deterministic, non-overlapping fleet layout anchored at (x, y): - * a 2x2 Command Post (rows y..y+1), an L Ammo Depot (box rows y+2..y+3), - * a Radar Site line (row y+4) and a 1-cell FOB (row y+5). + * a 2x2 Command Post (rows y..y+1), an L Ammo Depot with its elbow at + * (x, y+3) (rows y+2..y+3), a Radar Site line (row y+4) and a 1-cell FOB + * (row y+5). */ function fleetAt(x: number, y: number): FobPlacement[] { return [ { assetId: "command-post", origin: { x, y }, orientation: 0 }, - { assetId: "ammo-depot", origin: { x, y: y + 2 }, orientation: 0 }, + { assetId: "ammo-depot", origin: { x, y: y + 3 }, orientation: 0 }, { assetId: "radar-site", origin: { x, y: y + 4 }, orientation: 0 }, { assetId: "fob", origin: { x, y: y + 5 }, orientation: 0 }, ]; @@ -62,28 +63,34 @@ describe("call for fire versus: fleet placement", () => { expect(cellsForPlacement({ x: 9, y: 0 }, 0, 4, 10, "square")).toBeNull(); }); - it("rotates the 3-cell L through four distinct orientations", () => { - expect(cellsForPlacement({ x: 0, y: 0 }, 0, 3, 10, "l")).toEqual([ - { x: 0, y: 0 }, - { x: 0, y: 1 }, - { x: 1, y: 1 }, + it("rotates the 3-cell L around its fixed elbow anchor", () => { + const anchor = { x: 2, y: 2 }; + expect(cellsForPlacement(anchor, 0, 3, 10, "l")).toEqual([ + { x: 2, y: 2 }, + { x: 2, y: 1 }, + { x: 3, y: 2 }, ]); - expect(cellsForPlacement({ x: 0, y: 0 }, 1, 3, 10, "l")).toEqual([ - { x: 1, y: 0 }, - { x: 0, y: 0 }, - { x: 0, y: 1 }, + expect(cellsForPlacement(anchor, 1, 3, 10, "l")).toEqual([ + { x: 2, y: 2 }, + { x: 3, y: 2 }, + { x: 2, y: 3 }, ]); - expect(cellsForPlacement({ x: 0, y: 0 }, 2, 3, 10, "l")).toEqual([ - { x: 1, y: 1 }, - { x: 1, y: 0 }, - { x: 0, y: 0 }, + expect(cellsForPlacement(anchor, 2, 3, 10, "l")).toEqual([ + { x: 2, y: 2 }, + { x: 2, y: 3 }, + { x: 1, y: 2 }, ]); - expect(cellsForPlacement({ x: 0, y: 0 }, 3, 3, 10, "l")).toEqual([ - { x: 0, y: 1 }, - { x: 1, y: 1 }, - { x: 1, y: 0 }, + expect(cellsForPlacement(anchor, 3, 3, 10, "l")).toEqual([ + { x: 2, y: 2 }, + { x: 1, y: 2 }, + { x: 2, y: 1 }, ]); - expect(cellsForPlacement({ x: 9, y: 0 }, 0, 3, 10, "l")).toBeNull(); + // The elbow anchor stays put through every rotation. + for (const turn of [0, 1, 2, 3] as const) { + expect(cellsForPlacement(anchor, turn, 3, 10, "l")).toContainEqual(anchor); + } + // Orientation 0 with the elbow on the top edge runs the up-arm off board. + expect(cellsForPlacement({ x: 0, y: 0 }, 0, 3, 10, "l")).toBeNull(); }); it("rejects missing, overlapping, and unknown emplacements", () => { @@ -91,7 +98,7 @@ describe("call for fire versus: fleet placement", () => { const overlapping: FobPlacement[] = [ { assetId: "command-post", origin: { x: 0, y: 0 }, orientation: 0 }, - { assetId: "ammo-depot", origin: { x: 1, y: 0 }, orientation: 0 }, + { assetId: "ammo-depot", origin: { x: 1, y: 1 }, orientation: 0 }, { assetId: "radar-site", origin: { x: 0, y: 4 }, orientation: 0 }, { assetId: "fob", origin: { x: 0, y: 5 }, orientation: 0 }, ];