1
0
Fork 0

feat(minigames): make the Ammo Depot an L-shaped block with four-way rotation

The Ammo Depot is now a 3-cell L (a 2x2 box minus one corner) instead of a
2-cell line. Emplacement orientation becomes quarter-turns (0-3) so the L
can turn through all four; lines still use 0/1, the square ignores it.

The editor seeds an emplacement's rotation by matching its stored cells
against each orientation, the rotate toggle shows "Rotate 90°" for the L,
and the engine/rotation tests cover the four L orientations.
This commit is contained in:
Jason Fraley 2026-10-11 22:36:51 -04:00
parent 8afbf00805
commit c8f625b12c
5 changed files with 171 additions and 60 deletions

View file

@ -15,8 +15,9 @@ import { gridDesignation, METERS_PER_CELL, type GridPoint } from "@/lib/minigame
import {
FOB_FLEET,
VERSUS_WAITING_USER_ID,
type FobOrientation,
type FobPlacement,
type FobRotation,
type FobShape,
} from "@/lib/minigames/call-for-fire-versus/constants";
import { cellsForFob } from "@/lib/minigames/call-for-fire-versus/engine";
import type { VersusFobView, VersusShotView } from "@/lib/minigames/call-for-fire-versus/types";
@ -61,6 +62,25 @@ function fobStyle(id: string) {
);
}
/** Distinct orientations a shape supports (the square has one, the L has four). */
function turnsForShape(shape: FobShape): number {
if (shape === "square") return 1;
if (shape === "l") return 4;
return 2;
}
/** Toggle-button label for the selected emplacement's current orientation. */
function rotationLabel(shape: FobShape, orientation: FobRotation): string {
if (shape === "square") return "Fixed";
if (shape === "l") return "Rotate 90°";
return orientation % 2 === 0 ? "Horizontal" : "Vertical";
}
function sameCellSet(a: readonly GridPoint[], b: readonly GridPoint[]): boolean {
if (a.length !== b.length) return false;
return a.every((cell) => b.some((other) => other.x === cell.x && other.y === cell.y));
}
function key(point: GridPoint): string {
return `${point.x},${point.y}`;
}
@ -85,7 +105,9 @@ function randomFleet(): Record<string, FobPlacement> {
const occupied = new Set<string>();
for (const definition of FOB_FLEET) {
for (let attempt = 0; attempt < 800; attempt += 1) {
const orientation: FobOrientation = Math.random() < 0.5 ? "h" : "v";
const orientation = Math.floor(
Math.random() * turnsForShape(definition.shape),
) as FobRotation;
const origin = {
x: Math.floor(Math.random() * BOARD),
y: Math.floor(Math.random() * BOARD),
@ -115,16 +137,27 @@ function PlacementPhase({
const [placements, setPlacements] = useState<Record<string, FobPlacement>>(() => {
const initial: Record<string, FobPlacement> = {};
for (const fob of match.you.fleet) {
const origin = fob.cells[0];
if (!origin) continue;
const orientation: FobOrientation =
fob.size > 1 && fob.cells[1] && fob.cells[1].x !== origin.x ? "h" : "v";
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;
}
}
initial[fob.id] = { assetId: fob.id, origin, orientation };
}
return initial;
});
const [selectedId, setSelectedId] = useState<string>(FOB_FLEET[0].id);
const [orientation, setOrientation] = useState<FobOrientation>("h");
const [orientation, setOrientation] = useState<FobRotation>(0);
const [error, setError] = useState<string | null>(null);
const [busy, setBusy] = useState(false);
@ -204,8 +237,10 @@ function PlacementPhase({
(assetId: string) => {
const definition = FOB_FLEET.find((f) => f.id === assetId);
const placement = placements[assetId];
if (!definition || !placement || definition.shape === "square") return;
const nextOrientation: FobOrientation = placement.orientation === "h" ? "v" : "h";
if (!definition || !placement) return;
const turns = turnsForShape(definition.shape);
if (turns <= 1) return; // squares do not rotate
const nextOrientation = ((placement.orientation + 1) % turns) as FobRotation;
const cells = cellsForFob(definition, placement.origin, nextOrientation, BOARD);
if (!cells) {
setError(`${definition.label} does not fit rotated there.`);
@ -263,8 +298,9 @@ function PlacementPhase({
rotateAsset(selectedId);
return;
}
setOrientation((value) => (value === "h" ? "v" : "h"));
}, [rotateAsset, selectedId, selectedIsSquare, selectedPlacement]);
const turns = turnsForShape(selectedDefinition.shape);
setOrientation((value) => ((value + 1) % turns) as FobRotation);
}, [rotateAsset, selectedDefinition.shape, selectedId, selectedIsSquare, selectedPlacement]);
return (
<Card>
@ -350,7 +386,7 @@ function PlacementPhase({
title={selectedIsSquare ? "The Command Post is a fixed 2x2 block." : undefined}
onClick={toggleOrientation}
>
<RotateCcw /> {effectiveOrientation === "h" ? "Horizontal" : "Vertical"}
<RotateCcw /> {rotationLabel(selectedDefinition.shape, effectiveOrientation)}
</Button>
</div>
@ -390,8 +426,9 @@ function PlacementPhase({
<li>Select an emplacement above, then click a cell to anchor it there.</li>
<li>Click a placed emplacement to pick it up, then click a new cell to move it.</li>
<li>
Double-click a placed emplacement, or press the Horizontal/Vertical toggle, to rotate
it in place. The Command Post is a fixed 2x2 block and does not rotate.
Double-click a placed emplacement, or press the rotate toggle, to rotate it in place.
The Command Post is a fixed 2x2 block and does not rotate; the Ammo Depot is an L and
turns through four orientations.
</li>
<li>The toggle also sets the orientation used for your next placement.</li>
<li>Emplacements cannot overlap, and your opponent never sees your layout.</li>

View file

@ -26,34 +26,38 @@ export const VERSUS_MAX_NAME_LENGTH = 40;
/** Sentinel userId for the empty seat in a waiting match (never a real user). */
export const VERSUS_WAITING_USER_ID = 0;
export type FobShape = "line" | "square";
export type FobShape = "line" | "square" | "l";
export interface FobDefinition {
readonly id: string;
readonly label: string;
/** Occupied cells when placed. */
readonly size: number;
/** "line" lays the cells straight (orientation applies); "square" is an NxN block. */
/** "line" is straight, "square" is an NxN block, "l" is a 3-cell corner. */
readonly shape: FobShape;
}
/**
* The emplacement fleet each player hides. A shorter list keeps a duel brisk;
* total occupied cells are 9 of 100, so rounds matter without dragging.
* The emplacement fleet each player hides. Total occupied cells are 10 of 100,
* so rounds matter without dragging.
*/
export const FOB_FLEET: readonly FobDefinition[] = [
{ id: "command-post", label: "Command Post", size: 4, shape: "square" },
{ id: "ammo-depot", label: "Ammo Depot", size: 2, shape: "line" },
{ id: "ammo-depot", label: "Ammo Depot", size: 3, shape: "l" },
{ id: "radar-site", label: "Radar Site", size: 2, shape: "line" },
{ id: "fob", label: "Forward Operating Base", size: 1, shape: "line" },
];
export type FobOrientation = "h" | "v";
/**
* Quarter-turns clockwise. Lines use 0 (horizontal) and 1 (vertical); the
* square ignores it; the L uses all four.
*/
export type FobRotation = 0 | 1 | 2 | 3;
export interface FobPlacement {
readonly assetId: string;
readonly origin: GridPoint;
readonly orientation: FobOrientation;
readonly orientation: FobRotation;
}
/** Scoring: emplacements destroyed dominate; cells struck break ties. */

View file

@ -14,8 +14,8 @@ import {
VERSUS_SCORE_PER_CELL,
VERSUS_SCORE_PER_FOB,
type FobDefinition,
type FobOrientation,
type FobPlacement,
type FobRotation,
type FobShape,
} from "./constants";
import type {
@ -62,33 +62,70 @@ export function createVersusState(
};
}
/** Cells a straight-line or square emplacement covers, or null when it runs off the board. */
export function cellsForPlacement(
origin: GridPoint,
orientation: FobOrientation,
/** 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;
const normalized = ((turns % 4) + 4) % 4;
for (let t = 0; t < normalized; t += 1) {
const nextX = box - 1 - y;
const nextY = x;
x = nextX;
y = nextY;
}
return { x, y };
}
/** Relative cells for a shape at a given orientation. */
function relativeCells(
shape: FobShape,
orientation: FobRotation,
size: number,
boardSize: number,
shape: FobShape = "line",
): GridPoint[] | null {
if (shape === "square") {
const side = Math.round(Math.sqrt(size));
if (side * side !== size) return null;
const cells: GridPoint[] = [];
for (let dy = 0; dy < side; dy += 1) {
for (let dx = 0; dx < side; dx += 1) {
const x = origin.x + dx;
const y = origin.y + dy;
if (x < 0 || y < 0 || x >= boardSize || y >= boardSize) return null;
cells.push({ x, y });
}
for (let dx = 0; dx < side; dx += 1) cells.push({ x: dx, y: dy });
}
return cells;
}
if (shape === "l") {
return L_BASE.map((cell) => rotateQuarterTurn(cell, orientation, 2));
}
const horizontal = orientation % 2 === 0;
const cells: GridPoint[] = [];
for (let i = 0; i < size; i += 1) {
const x = orientation === "h" ? origin.x + i : origin.x;
const y = orientation === "v" ? origin.y + i : origin.y;
cells.push(horizontal ? { x: i, y: 0 } : { x: 0, y: i });
}
return cells;
}
/**
* Cells a placed emplacement covers, or null when it runs off the board. The
* shape drives the footprint; orientation is quarter-turns clockwise.
*/
export function cellsForPlacement(
origin: GridPoint,
orientation: FobRotation,
size: number,
boardSize: number,
shape: FobShape = "line",
): GridPoint[] | null {
const relative = relativeCells(shape, orientation, size);
if (!relative) return null;
const cells: GridPoint[] = [];
for (const cell of relative) {
const x = origin.x + cell.x;
const y = origin.y + cell.y;
if (x < 0 || y < 0 || x >= boardSize || y >= boardSize) return null;
cells.push({ x, y });
}
@ -99,7 +136,7 @@ export function cellsForPlacement(
export function cellsForFob(
definition: FobDefinition,
origin: GridPoint,
orientation: FobOrientation,
orientation: FobRotation,
boardSize: number,
): GridPoint[] | null {
return cellsForPlacement(origin, orientation, definition.size, boardSize, definition.shape);

View file

@ -15,10 +15,10 @@ import { computeVersusStatsDelta, readVersusStats, xpForVersus } from "@/lib/min
function fleetAt(x: number, y: number): FobPlacement[] {
return [
{ assetId: "command-post", origin: { x, y }, orientation: "h" },
{ assetId: "ammo-depot", origin: { x, y: y + 2 }, orientation: "h" },
{ assetId: "radar-site", origin: { x, y: y + 3 }, orientation: "h" },
{ assetId: "fob", origin: { x, y: y + 4 }, orientation: "h" },
{ assetId: "command-post", origin: { x, y }, orientation: 0 },
{ assetId: "ammo-depot", origin: { x, y: y + 2 }, orientation: 0 },
{ assetId: "radar-site", origin: { x, y: y + 4 }, orientation: 0 },
{ assetId: "fob", origin: { x, y: y + 5 }, orientation: 0 },
];
}

View file

@ -14,13 +14,17 @@ import {
type VersusState,
} from "@/lib/minigames/call-for-fire-versus";
/** 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 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: "h" },
{ assetId: "ammo-depot", origin: { x, y: y + 2 }, orientation: "h" },
{ assetId: "radar-site", origin: { x, y: y + 3 }, orientation: "h" },
{ assetId: "fob", origin: { x, y: y + 4 }, orientation: "h" },
{ assetId: "command-post", origin: { x, y }, orientation: 0 },
{ assetId: "ammo-depot", origin: { x, y: y + 2 }, orientation: 0 },
{ assetId: "radar-site", origin: { x, y: y + 4 }, orientation: 0 },
{ assetId: "fob", origin: { x, y: y + 5 }, orientation: 0 },
];
}
@ -38,8 +42,8 @@ function expectState(result: { state: VersusState } | { error: string }): Versus
describe("call for fire versus: fleet placement", () => {
it("lays out straight-line emplacements and rejects off-board ones", () => {
expect(cellsForPlacement({ x: 8, y: 0 }, "h", 3, 10)).toBeNull();
expect(cellsForPlacement({ x: 0, y: 0 }, "v", 3, 10)).toEqual([
expect(cellsForPlacement({ x: 8, y: 0 }, 0, 3, 10)).toBeNull();
expect(cellsForPlacement({ x: 0, y: 0 }, 1, 3, 10)).toEqual([
{ x: 0, y: 0 },
{ x: 0, y: 1 },
{ x: 0, y: 2 },
@ -53,23 +57,50 @@ describe("call for fire versus: fleet placement", () => {
{ x: 0, y: 1 },
{ x: 1, y: 1 },
];
expect(cellsForPlacement({ x: 0, y: 0 }, "h", 4, 10, "square")).toEqual(square);
expect(cellsForPlacement({ x: 0, y: 0 }, "v", 4, 10, "square")).toEqual(square);
expect(cellsForPlacement({ x: 9, y: 0 }, "h", 4, 10, "square")).toBeNull();
expect(cellsForPlacement({ x: 0, y: 0 }, 0, 4, 10, "square")).toEqual(square);
expect(cellsForPlacement({ x: 0, y: 0 }, 1, 4, 10, "square")).toEqual(square);
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 },
]);
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({ x: 0, y: 0 }, 2, 3, 10, "l")).toEqual([
{ x: 1, y: 1 },
{ x: 1, y: 0 },
{ x: 0, y: 0 },
]);
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({ x: 9, y: 0 }, 0, 3, 10, "l")).toBeNull();
});
it("rejects missing, overlapping, and unknown emplacements", () => {
expect(buildFleet([fleetAt(0, 0)[0]], 10)).toEqual({ error: "Missing emplacement: Ammo Depot." });
const overlapping: FobPlacement[] = [
{ assetId: "command-post", origin: { x: 0, y: 0 }, orientation: "h" },
{ assetId: "ammo-depot", origin: { x: 1, y: 0 }, orientation: "h" },
{ assetId: "radar-site", origin: { x: 0, y: 3 }, orientation: "h" },
{ assetId: "fob", origin: { x: 0, y: 4 }, orientation: "h" },
{ assetId: "command-post", origin: { x: 0, y: 0 }, orientation: 0 },
{ assetId: "ammo-depot", origin: { x: 1, y: 0 }, orientation: 0 },
{ assetId: "radar-site", origin: { x: 0, y: 4 }, orientation: 0 },
{ assetId: "fob", origin: { x: 0, y: 5 }, orientation: 0 },
];
expect(buildFleet(overlapping, 10)).toEqual({ error: "Emplacements cannot overlap." });
const unknown = [...fleetAt(0, 0), { assetId: "nuke", origin: { x: 9, y: 9 }, orientation: "h" as const }];
const unknown: FobPlacement[] = [
...fleetAt(0, 0),
{ assetId: "nuke", origin: { x: 9, y: 9 }, orientation: 0 },
];
expect("error" in buildFleet(unknown, 10)).toBe(true);
});
@ -77,7 +108,7 @@ describe("call for fire versus: fleet placement", () => {
let state = createVersusState({ userId: 1, name: "A" }, { userId: 2, name: "B" });
state = expectState(placeFleet(state, 0, fleetAt(0, 0)));
expect(state.phase).toBe("placing");
state = expectState(placeFleet(state, 1, fleetAt(0, 5)));
state = expectState(placeFleet(state, 1, fleetAt(0, 4)));
expect(state.phase).toBe("battle");
expect(state.turn).toBe(0);
expect(state.startedAt).not.toBeNull();
@ -134,6 +165,8 @@ describe("call for fire versus: firing", () => {
{ i: 1, input: { kind: "correction", rangeM: 0, deflectionM: 0 } },
{ i: 0, input: { kind: "correction", rangeM: -50, deflectionM: -50 } },
{ i: 1, input: { kind: "correction", rangeM: 0, deflectionM: 0 } },
{ i: 0, input: { kind: "correction", rangeM: -50, deflectionM: 0 } },
{ i: 1, input: { kind: "correction", rangeM: 0, deflectionM: 0 } },
{ i: 0, input: { kind: "correction", rangeM: 0, deflectionM: 50 } },
{ i: 1, input: { kind: "correction", rangeM: 0, deflectionM: 0 } },
{ i: 0, input: { kind: "correction", rangeM: -50, deflectionM: -50 } },
@ -165,8 +198,8 @@ describe("call for fire versus: views", () => {
const state = startedState();
const view = buildVersusView(state, 0);
const serialized = JSON.stringify(view);
// Player 1's fleet sits at (0,0)..(0,3); player 0 has fired nothing, so
// none of those cells may appear in player 0's view.
// Player 1's fleet includes (0,0) and (1,0); player 0 has fired nothing,
// so none of those cells may appear in player 0's view.
expect(serialized).not.toContain('{"x":0,"y":0}');
expect(serialized).not.toContain('{"x":1,"y":0}');
expect(serialized).not.toContain('"fobsDestroyed":4');