// SPDX-License-Identifier: AGPL-3.0-only // Ported from Folia (https://github.com/chthollyphile/folia-major, v0.8.2), src/components/wall/blockTemplates.ts // and the block geometry of src/components/wall/layout.ts. See ../README.md. // // The poster wall is made of 12x8 blocks of cells, each covered exactly by twelve cards in one of four // templates, mirrored four ways. Opening a card gives it a 6x6 gear; the other eleven take smaller gears from a // precomputed table (blockReflows.ts), so the block stays exactly covered and nothing outside it moves. // // Changed from Folia: the wall here is finite and stands in the page's flow (blocks side by side, then down), // where Folia's repeats without end under a camera. A finite wall has a last block that may not be full: its // cards close over the slots that have no card (closeOverBareSlots), so the wall has no holes. import { BLOCK_REFLOWS } from './blockReflows'; export type BlockSlot = { x: number; y: number; cols: number; rows: number }; export const BLOCK_COLS = 12; export const BLOCK_ROWS = 8; export const SLOTS_PER_BLOCK = 12; /** Folia's world units: a cell and the gap between cards. Everything on the wall is sized in these and scaled. */ export const CELL_SIZE = 128; export const GAP = 8; export const PITCH = CELL_SIZE + GAP; /** Below this width a page shows the wall as a plain grid: twelve columns would make thumbnails of the covers. */ export const NARROW_WIDTH = 700; /** The side of an open card, in world units. */ export const OPEN_SIZE = 6 * PITCH - GAP; /** * How far the world is scaled to stand in a page: as wide as the page lets it be, but never so large that the open * card outgrows the window, and never larger than Folia's own units. It reads nothing but its arguments, so a page * can hand its source to a script that runs before the wall's own has loaded (see wall.ts on a wall the page placed). */ export const fitWallScale = (available: number, planWidth: number, viewportHeight: number, openSize: number): number => ( Math.min(1, available / planWidth, Math.max(0.4, (viewportHeight - 120) / openSize)) ); const TEMPLATES: BlockSlot[][] = [ [ { x: 0, y: 0, cols: 3, rows: 2 }, { x: 3, y: 0, cols: 6, rows: 4 }, { x: 9, y: 0, cols: 3, rows: 2 }, { x: 0, y: 2, cols: 3, rows: 2 }, { x: 9, y: 2, cols: 3, rows: 4 }, { x: 0, y: 4, cols: 4, rows: 4 }, { x: 4, y: 4, cols: 2, rows: 2 }, { x: 6, y: 4, cols: 3, rows: 2 }, { x: 4, y: 6, cols: 2, rows: 2 }, { x: 6, y: 6, cols: 2, rows: 2 }, { x: 8, y: 6, cols: 2, rows: 2 }, { x: 10, y: 6, cols: 2, rows: 2 }, ], [ { x: 0, y: 0, cols: 2, rows: 3 }, { x: 2, y: 0, cols: 2, rows: 3 }, { x: 4, y: 0, cols: 2, rows: 3 }, { x: 6, y: 0, cols: 6, rows: 4 }, { x: 0, y: 3, cols: 6, rows: 2 }, { x: 6, y: 4, cols: 2, rows: 4 }, { x: 8, y: 4, cols: 2, rows: 4 }, { x: 10, y: 4, cols: 2, rows: 2 }, { x: 0, y: 5, cols: 2, rows: 3 }, { x: 2, y: 5, cols: 2, rows: 3 }, { x: 4, y: 5, cols: 2, rows: 3 }, { x: 10, y: 6, cols: 2, rows: 2 }, ], [ { x: 0, y: 0, cols: 6, rows: 4 }, { x: 6, y: 0, cols: 6, rows: 3 }, { x: 6, y: 3, cols: 4, rows: 3 }, { x: 10, y: 3, cols: 2, rows: 2 }, { x: 0, y: 4, cols: 4, rows: 2 }, { x: 4, y: 4, cols: 2, rows: 2 }, { x: 10, y: 5, cols: 2, rows: 3 }, { x: 0, y: 6, cols: 2, rows: 2 }, { x: 2, y: 6, cols: 2, rows: 2 }, { x: 4, y: 6, cols: 2, rows: 2 }, { x: 6, y: 6, cols: 2, rows: 2 }, { x: 8, y: 6, cols: 2, rows: 2 }, ], [ { x: 0, y: 0, cols: 2, rows: 2 }, { x: 2, y: 0, cols: 2, rows: 2 }, { x: 4, y: 0, cols: 4, rows: 3 }, { x: 8, y: 0, cols: 2, rows: 4 }, { x: 10, y: 0, cols: 2, rows: 4 }, { x: 0, y: 2, cols: 2, rows: 3 }, { x: 2, y: 2, cols: 2, rows: 2 }, { x: 4, y: 3, cols: 4, rows: 3 }, { x: 2, y: 4, cols: 2, rows: 2 }, { x: 8, y: 4, cols: 4, rows: 4 }, { x: 0, y: 5, cols: 2, rows: 3 }, { x: 2, y: 6, cols: 6, rows: 2 }, ], ]; const flipX = (slots: BlockSlot[]): BlockSlot[] => slots.map(slot => ({ ...slot, x: BLOCK_COLS - slot.x - slot.cols })); const flipY = (slots: BlockSlot[]): BlockSlot[] => slots.map(slot => ({ ...slot, y: BLOCK_ROWS - slot.y - slot.rows })); // Reflections preserve slot order, so a card keeps its index in every orientation. const REFLECTIONS: Array<(slots: BlockSlot[]) => BlockSlot[]> = [ slots => slots, flipX, flipY, slots => flipX(flipY(slots)), ]; const ORIENTED_TEMPLATES: BlockSlot[][] = TEMPLATES.flatMap(template => REFLECTIONS.map(reflect => reflect(template))); const ORIENTED_REFLOWS: BlockSlot[][][] = BLOCK_REFLOWS.flatMap(perSlot => REFLECTIONS.map(reflect => perSlot.map(reflect))); // Integer hash; picks the reflection so the field never settles into a visible period. const mixBlockCoords = (blockColumn: number, blockRow: number): number => { let value = Math.imul(blockColumn, 0x9e3779b1) ^ Math.imul(blockRow, 0x85ebca6b); value = Math.imul(value ^ (value >>> 15), 0x2c1b3c6d); value ^= value >>> 12; return value >>> 0; }; // The template steps linearly (+1 across, +2 down) so neighbouring blocks never share one; the reflection // comes from a hash, which keeps the field from reading as a grid. const getBlockOrientation = (blockColumn: number, blockRow: number): number => { const count = TEMPLATES.length; const template = ((blockColumn + blockRow * 2) % count + count) % count; return template * REFLECTIONS.length + mixBlockCoords(blockColumn, blockRow) % REFLECTIONS.length; }; export const getBlockTemplate = (blockColumn: number, blockRow: number): BlockSlot[] => ( ORIENTED_TEMPLATES[getBlockOrientation(blockColumn, blockRow)] ); /** Where every card of a block sits while `expandedSlot` is open. */ export const getBlockReflow = (blockColumn: number, blockRow: number, expandedSlot: number): BlockSlot[] | null => { if (expandedSlot < 0 || expandedSlot >= SLOTS_PER_BLOCK) return null; return ORIENTED_REFLOWS[getBlockOrientation(blockColumn, blockRow)][expandedSlot]; }; export type WallRect = { x: number; y: number; width: number; height: number }; /** Two slots that share a whole edge, as one; null when they do not. */ const joinSlots = (a: BlockSlot, b: BlockSlot): BlockSlot | null => { if (a.x === b.x && a.cols === b.cols && (a.y + a.rows === b.y || b.y + b.rows === a.y)) return { x: a.x, y: Math.min(a.y, b.y), cols: a.cols, rows: a.rows + b.rows }; if (a.y === b.y && a.rows === b.rows && (a.x + a.cols === b.x || b.x + b.cols === a.x)) return { x: Math.min(a.x, b.x), y: a.y, cols: a.cols + b.cols, rows: a.rows }; return null; }; /** A card that has grown is still a card of this wall: at most six cells either way, at most three times as long as wide. */ const isCardShape = (slot: BlockSlot | null): slot is BlockSlot => !!slot && slot.cols <= 6 && slot.rows <= 6 && slot.cols <= slot.rows * 3 && slot.rows <= slot.cols * 3; const halveSlot = (slot: BlockSlot): [BlockSlot, BlockSlot][] => { const halves: [BlockSlot, BlockSlot][] = []; const left = Math.floor(slot.cols / 2); const top = Math.floor(slot.rows / 2); if (slot.cols >= 4) halves.push([{ ...slot, cols: left }, { ...slot, x: slot.x + left, cols: slot.cols - left }]); if (slot.rows >= 4) halves.push([{ ...slot, rows: top }, { ...slot, y: slot.y + top, rows: slot.rows - top }]); return halves; }; /** * A block with fewer than twelve cards: the slots past its last card would be holes in the wall, so the cards * beside them take them, the smallest card first. A bare slot goes to a card it shares a whole edge with; where * none does, two bare slots are joined or one is cut in two and tried again. The open card (`open`, or -1) keeps * its gear. What no card can take stays bare. Not in Folia, whose wall is endless and has no last block. */ const closeOverBareSlots = (slots: BlockSlot[], filled: number, open: number): BlockSlot[] => { const cards = slots.slice(0, filled).map((slot) => ({ ...slot })); const bare = slots.slice(filled).map((slot) => ({ ...slot })); const taker = (part: BlockSlot) => cards.some((card, index) => index !== open && isCardShape(joinSlots(card, part))); for (let pass = 0; pass < 64 && bare.length; pass += 1) { let grown = -1; let taken = -1; let area = Infinity; for (let index = 0; index < cards.length; index += 1) { const card = cards[index]; if (index === open || card.cols * card.rows >= area) continue; const slot = bare.findIndex((empty) => isCardShape(joinSlots(card, empty))); if (slot < 0) continue; grown = index; taken = slot; area = card.cols * card.rows; } if (grown >= 0) { cards[grown] = joinSlots(cards[grown], bare[taken])!; bare.splice(taken, 1); continue; } let changed = false; for (let i = 0; i < bare.length && !changed; i += 1) { for (let j = i + 1; j < bare.length && !changed; j += 1) { const union = joinSlots(bare[i], bare[j]); if (!union || !taker(union)) continue; bare[i] = union; bare.splice(j, 1); changed = true; } } for (let i = 0; i < bare.length && !changed; i += 1) { const halves = halveSlot(bare[i]).find(([first, second]) => taker(first) || taker(second)); if (!halves) continue; bare.splice(i, 1, ...halves); changed = true; } if (!changed) break; } return cards; }; /** * The cards of a finite wall, in world units: `count` entries fill blocks set `blocksPerRow` across, in order; * the entry at `expanded` (or none, with -1) has its 6x6 gear and its block is re-geared around it. In the last * block the cards close over the slots that have no card, and the wall ends with its lowest card. */ export const layoutFiniteWall = (count: number, blocksPerRow: number, expanded: number): { rects: WallRect[]; width: number; height: number } => { const blocks = Math.max(1, Math.ceil(count / SLOTS_PER_BLOCK)); const across = Math.max(1, Math.min(blocksPerRow, blocks)); const rects: WallRect[] = []; const expandedBlock = expanded >= 0 ? Math.floor(expanded / SLOTS_PER_BLOCK) : -1; for (let block = 0; block * SLOTS_PER_BLOCK < count; block += 1) { const column = block % across; const row = Math.floor(block / across); const filled = Math.min(SLOTS_PER_BLOCK, count - block * SLOTS_PER_BLOCK); const open = block === expandedBlock ? expanded % SLOTS_PER_BLOCK : -1; const geared = (open >= 0 ? getBlockReflow(column, row, open) : null) ?? getBlockTemplate(column, row); const slots = filled < SLOTS_PER_BLOCK ? closeOverBareSlots(geared, filled, open) : geared; for (let index = 0; index < filled; index += 1) { const slot = slots[index]; rects.push({ x: (column * BLOCK_COLS + slot.x) * PITCH, y: (row * BLOCK_ROWS + slot.y) * PITCH, width: slot.cols * PITCH - GAP, height: slot.rows * PITCH - GAP, }); } } const height = rects.reduce((bottom, rect) => Math.max(bottom, rect.y + rect.height), 0); return { rects, width: across * BLOCK_COLS * PITCH - GAP, height }; };