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.
This commit is contained in:
parent
c8f625b12c
commit
16e4b9f60d
4 changed files with 62 additions and 48 deletions
|
|
@ -139,20 +139,25 @@ function PlacementPhase({
|
||||||
for (const fob of match.you.fleet) {
|
for (const fob of match.you.fleet) {
|
||||||
const definition = FOB_FLEET.find((d) => d.id === fob.id);
|
const definition = FOB_FLEET.find((d) => d.id === fob.id);
|
||||||
if (!definition || fob.cells.length === 0) continue;
|
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);
|
const turns = turnsForShape(definition.shape);
|
||||||
let orientation: FobRotation = 0;
|
let placement: FobPlacement | null = null;
|
||||||
for (let t = 0; t < turns; t += 1) {
|
// Recover the (anchor, rotation) that reproduces the stored cells; the
|
||||||
const cells = cellsForFob(definition, origin, t as FobRotation, BOARD);
|
// anchor can be any piece cell (line start, square corner, L elbow).
|
||||||
if (cells && sameCellSet(cells, fob.cells)) {
|
for (const anchor of fob.cells) {
|
||||||
orientation = t as FobRotation;
|
for (let t = 0; t < turns; t += 1) {
|
||||||
break;
|
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;
|
return initial;
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -62,20 +62,13 @@ export function createVersusState(
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Base cells of the 3-cell L within its 2x2 box, at rotation 0. */
|
/** Rotate an offset vector by `turns` quarter-turns clockwise (screen coords). */
|
||||||
const L_BASE: readonly GridPoint[] = [
|
function rotateVector(vector: GridPoint, turns: number): GridPoint {
|
||||||
{ x: 0, y: 0 },
|
let x = vector.x;
|
||||||
{ x: 0, y: 1 },
|
let y = vector.y;
|
||||||
{ 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;
|
|
||||||
const normalized = ((turns % 4) + 4) % 4;
|
const normalized = ((turns % 4) + 4) % 4;
|
||||||
for (let t = 0; t < normalized; t += 1) {
|
for (let t = 0; t < normalized; t += 1) {
|
||||||
const nextX = box - 1 - y;
|
const nextX = -y;
|
||||||
const nextY = x;
|
const nextY = x;
|
||||||
x = nextX;
|
x = nextX;
|
||||||
y = nextY;
|
y = nextY;
|
||||||
|
|
@ -99,7 +92,14 @@ function relativeCells(
|
||||||
return cells;
|
return cells;
|
||||||
}
|
}
|
||||||
if (shape === "l") {
|
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 horizontal = orientation % 2 === 0;
|
||||||
const cells: GridPoint[] = [];
|
const cells: GridPoint[] = [];
|
||||||
|
|
@ -111,7 +111,9 @@ function relativeCells(
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cells a placed emplacement covers, or null when it runs off the board. The
|
* 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(
|
export function cellsForPlacement(
|
||||||
origin: GridPoint,
|
origin: GridPoint,
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ import { computeVersusStatsDelta, readVersusStats, xpForVersus } from "@/lib/min
|
||||||
function fleetAt(x: number, y: number): FobPlacement[] {
|
function fleetAt(x: number, y: number): FobPlacement[] {
|
||||||
return [
|
return [
|
||||||
{ assetId: "command-post", origin: { x, y }, orientation: 0 },
|
{ 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: "radar-site", origin: { x, y: y + 4 }, orientation: 0 },
|
||||||
{ assetId: "fob", origin: { x, y: y + 5 }, orientation: 0 },
|
{ assetId: "fob", origin: { x, y: y + 5 }, orientation: 0 },
|
||||||
];
|
];
|
||||||
|
|
|
||||||
|
|
@ -16,13 +16,14 @@ import {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A deterministic, non-overlapping fleet layout anchored at (x, y):
|
* 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 2x2 Command Post (rows y..y+1), an L Ammo Depot with its elbow at
|
||||||
* a Radar Site line (row y+4) and a 1-cell FOB (row y+5).
|
* (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[] {
|
function fleetAt(x: number, y: number): FobPlacement[] {
|
||||||
return [
|
return [
|
||||||
{ assetId: "command-post", origin: { x, y }, orientation: 0 },
|
{ 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: "radar-site", origin: { x, y: y + 4 }, orientation: 0 },
|
||||||
{ assetId: "fob", origin: { x, y: y + 5 }, 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();
|
expect(cellsForPlacement({ x: 9, y: 0 }, 0, 4, 10, "square")).toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("rotates the 3-cell L through four distinct orientations", () => {
|
it("rotates the 3-cell L around its fixed elbow anchor", () => {
|
||||||
expect(cellsForPlacement({ x: 0, y: 0 }, 0, 3, 10, "l")).toEqual([
|
const anchor = { x: 2, y: 2 };
|
||||||
{ x: 0, y: 0 },
|
expect(cellsForPlacement(anchor, 0, 3, 10, "l")).toEqual([
|
||||||
{ x: 0, y: 1 },
|
{ x: 2, y: 2 },
|
||||||
{ x: 1, y: 1 },
|
{ x: 2, y: 1 },
|
||||||
|
{ x: 3, y: 2 },
|
||||||
]);
|
]);
|
||||||
expect(cellsForPlacement({ x: 0, y: 0 }, 1, 3, 10, "l")).toEqual([
|
expect(cellsForPlacement(anchor, 1, 3, 10, "l")).toEqual([
|
||||||
{ x: 1, y: 0 },
|
{ x: 2, y: 2 },
|
||||||
{ x: 0, y: 0 },
|
{ x: 3, y: 2 },
|
||||||
{ x: 0, y: 1 },
|
{ x: 2, y: 3 },
|
||||||
]);
|
]);
|
||||||
expect(cellsForPlacement({ x: 0, y: 0 }, 2, 3, 10, "l")).toEqual([
|
expect(cellsForPlacement(anchor, 2, 3, 10, "l")).toEqual([
|
||||||
{ x: 1, y: 1 },
|
{ x: 2, y: 2 },
|
||||||
{ x: 1, y: 0 },
|
{ x: 2, y: 3 },
|
||||||
{ x: 0, y: 0 },
|
{ x: 1, y: 2 },
|
||||||
]);
|
]);
|
||||||
expect(cellsForPlacement({ x: 0, y: 0 }, 3, 3, 10, "l")).toEqual([
|
expect(cellsForPlacement(anchor, 3, 3, 10, "l")).toEqual([
|
||||||
{ x: 0, y: 1 },
|
{ x: 2, y: 2 },
|
||||||
{ x: 1, y: 1 },
|
{ x: 1, y: 2 },
|
||||||
{ x: 1, y: 0 },
|
{ 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", () => {
|
it("rejects missing, overlapping, and unknown emplacements", () => {
|
||||||
|
|
@ -91,7 +98,7 @@ describe("call for fire versus: fleet placement", () => {
|
||||||
|
|
||||||
const overlapping: FobPlacement[] = [
|
const overlapping: FobPlacement[] = [
|
||||||
{ assetId: "command-post", origin: { x: 0, y: 0 }, orientation: 0 },
|
{ 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: "radar-site", origin: { x: 0, y: 4 }, orientation: 0 },
|
||||||
{ assetId: "fob", origin: { x: 0, y: 5 }, orientation: 0 },
|
{ assetId: "fob", origin: { x: 0, y: 5 }, orientation: 0 },
|
||||||
];
|
];
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue