2023-02-07 22:13:50 -05:00
|
|
|
|
/*
|
|
|
|
|
|
Copyright 2023 New Vector Ltd
|
|
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
|
limitations under the License.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
import TinyQueue from "tinyqueue";
|
2023-06-27 12:19:06 -04:00
|
|
|
|
import { RectReadOnly } from "react-use-measure";
|
|
|
|
|
|
import { FC, memo, ReactNode } from "react";
|
2023-07-06 00:43:17 -04:00
|
|
|
|
import { zip } from "lodash";
|
2023-02-13 21:57:57 -05:00
|
|
|
|
|
2023-06-09 17:22:34 -04:00
|
|
|
|
import { TileDescriptor } from "./VideoGrid";
|
2023-06-27 12:19:06 -04:00
|
|
|
|
import { Slot } from "./NewVideoGrid";
|
|
|
|
|
|
import { Layout } from "./Layout";
|
2023-06-17 15:21:38 -04:00
|
|
|
|
import { count, findLastIndex } from "../array-utils";
|
2023-06-27 12:19:06 -04:00
|
|
|
|
import styles from "./BigGrid.module.css";
|
2023-02-07 22:13:50 -05:00
|
|
|
|
|
2023-02-07 23:27:49 -05:00
|
|
|
|
/**
|
|
|
|
|
|
* A 1×1 cell in a grid which belongs to a tile.
|
|
|
|
|
|
*/
|
2023-06-27 12:19:06 -04:00
|
|
|
|
interface Cell {
|
2023-02-07 22:13:50 -05:00
|
|
|
|
/**
|
2023-02-07 23:27:49 -05:00
|
|
|
|
* The item displayed on the tile.
|
2023-02-07 22:13:50 -05:00
|
|
|
|
*/
|
2023-06-27 12:19:06 -04:00
|
|
|
|
readonly item: TileDescriptor<unknown>;
|
2023-02-07 22:13:50 -05:00
|
|
|
|
/**
|
2023-02-07 23:27:49 -05:00
|
|
|
|
* Whether this cell is the origin (top left corner) of the tile.
|
2023-02-07 22:13:50 -05:00
|
|
|
|
*/
|
2023-06-27 12:19:06 -04:00
|
|
|
|
readonly origin: boolean;
|
2023-02-07 22:13:50 -05:00
|
|
|
|
/**
|
2023-02-07 23:27:49 -05:00
|
|
|
|
* The width, in columns, of the tile.
|
2023-02-07 22:13:50 -05:00
|
|
|
|
*/
|
2023-06-27 12:19:06 -04:00
|
|
|
|
readonly columns: number;
|
2023-02-07 22:13:50 -05:00
|
|
|
|
/**
|
2023-02-07 23:27:49 -05:00
|
|
|
|
* The height, in rows, of the tile.
|
2023-02-07 22:13:50 -05:00
|
|
|
|
*/
|
2023-06-27 12:19:06 -04:00
|
|
|
|
readonly rows: number;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-06 00:43:17 -04:00
|
|
|
|
export interface Grid {
|
|
|
|
|
|
columns: number;
|
2023-06-27 12:19:06 -04:00
|
|
|
|
/**
|
|
|
|
|
|
* The cells of the grid, in left-to-right top-to-bottom order.
|
|
|
|
|
|
*/
|
2023-07-06 00:43:17 -04:00
|
|
|
|
cells: Cell[];
|
2023-02-07 22:13:50 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-06 00:43:17 -04:00
|
|
|
|
interface SparseGrid {
|
2023-02-07 22:13:50 -05:00
|
|
|
|
columns: number;
|
2023-02-07 23:27:49 -05:00
|
|
|
|
/**
|
|
|
|
|
|
* The cells of the grid, in left-to-right top-to-bottom order.
|
2023-07-06 00:43:17 -04:00
|
|
|
|
* undefined = a gap in the grid.
|
2023-02-07 23:27:49 -05:00
|
|
|
|
*/
|
2023-02-07 22:13:50 -05:00
|
|
|
|
cells: (Cell | undefined)[];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-02-13 12:17:09 -05:00
|
|
|
|
/**
|
|
|
|
|
|
* Gets the paths that tiles should travel along in the grid to reach a
|
|
|
|
|
|
* particular destination.
|
|
|
|
|
|
* @param g The grid.
|
2023-07-06 00:43:17 -04:00
|
|
|
|
* @param dest The destination index.
|
|
|
|
|
|
* @param avoid A predicate defining the cells that paths should avoid going
|
|
|
|
|
|
* through.
|
2023-02-13 12:17:09 -05:00
|
|
|
|
* @returns An array in which each cell holds the index of the next cell to move
|
2023-07-06 00:43:17 -04:00
|
|
|
|
* to to reach the destination, or null if it is the destination or otherwise
|
|
|
|
|
|
* immovable.
|
2023-02-13 12:17:09 -05:00
|
|
|
|
*/
|
2023-07-06 00:43:17 -04:00
|
|
|
|
export function getPaths(
|
|
|
|
|
|
g: SparseGrid,
|
|
|
|
|
|
dest: number,
|
|
|
|
|
|
avoid: (cell: number) => boolean = () => false
|
|
|
|
|
|
): (number | null)[] {
|
2023-02-13 12:17:09 -05:00
|
|
|
|
const destRow = row(dest, g);
|
|
|
|
|
|
const destColumn = column(dest, g);
|
2023-02-07 22:13:50 -05:00
|
|
|
|
|
2023-02-13 12:17:09 -05:00
|
|
|
|
// This is Dijkstra's algorithm
|
|
|
|
|
|
|
|
|
|
|
|
const distances = new Array<number>(dest + 1).fill(Infinity);
|
|
|
|
|
|
distances[dest] = 0;
|
2023-07-06 00:43:17 -04:00
|
|
|
|
const edges = new Array<number | null>(dest).fill(null);
|
2023-02-13 12:17:09 -05:00
|
|
|
|
edges[dest] = null;
|
|
|
|
|
|
const heap = new TinyQueue([dest], (i) => distances[i]);
|
2023-02-07 22:13:50 -05:00
|
|
|
|
|
2023-07-06 00:43:17 -04:00
|
|
|
|
const visit = (curr: number, via: number, distanceVia: number) => {
|
2023-02-07 22:13:50 -05:00
|
|
|
|
if (distanceVia < distances[curr]) {
|
|
|
|
|
|
distances[curr] = distanceVia;
|
|
|
|
|
|
edges[curr] = via;
|
|
|
|
|
|
heap.push(curr);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
while (heap.length > 0) {
|
|
|
|
|
|
const via = heap.pop()!;
|
2023-07-06 00:43:17 -04:00
|
|
|
|
|
|
|
|
|
|
if (!avoid(via)) {
|
|
|
|
|
|
const viaRow = row(via, g);
|
|
|
|
|
|
const viaColumn = column(via, g);
|
|
|
|
|
|
const viaCell = g.cells[via];
|
|
|
|
|
|
const viaLargeTile = viaCell !== undefined && !is1By1(viaCell);
|
|
|
|
|
|
// Since it looks nicer to have paths go around large tiles, we impose an
|
|
|
|
|
|
// increased cost for moving through them
|
|
|
|
|
|
const distanceVia = distances[via] + (viaLargeTile ? 8 : 1);
|
|
|
|
|
|
|
|
|
|
|
|
// Visit each neighbor
|
|
|
|
|
|
if (viaRow > 0) visit(via - g.columns, via, distanceVia);
|
|
|
|
|
|
if (viaColumn > 0) visit(via - 1, via, distanceVia);
|
|
|
|
|
|
if (viaColumn < (viaRow === destRow ? destColumn : g.columns - 1))
|
|
|
|
|
|
visit(via + 1, via, distanceVia);
|
|
|
|
|
|
if (
|
|
|
|
|
|
viaRow < destRow - 1 ||
|
|
|
|
|
|
(viaRow === destRow - 1 && viaColumn <= destColumn)
|
|
|
|
|
|
)
|
|
|
|
|
|
visit(via + g.columns, via, distanceVia);
|
|
|
|
|
|
}
|
2023-02-07 22:13:50 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-02-13 12:17:09 -05:00
|
|
|
|
// The heap is empty, so we've generated all paths
|
2023-07-06 00:43:17 -04:00
|
|
|
|
return edges;
|
2023-02-07 22:13:50 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-06 00:43:17 -04:00
|
|
|
|
const is1By1 = (c: Cell) => c.columns === 1 && c.rows === 1;
|
2023-02-07 22:13:50 -05:00
|
|
|
|
|
2023-07-06 00:43:17 -04:00
|
|
|
|
const findLast1By1Index = (g: SparseGrid): number | null =>
|
|
|
|
|
|
findLastIndex(g.cells, (c) => c !== undefined && is1By1(c));
|
|
|
|
|
|
|
|
|
|
|
|
export function row(index: number, g: SparseGrid): number {
|
2023-02-07 22:13:50 -05:00
|
|
|
|
return Math.floor(index / g.columns);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-06 00:43:17 -04:00
|
|
|
|
export function column(index: number, g: SparseGrid): number {
|
2023-02-07 22:13:50 -05:00
|
|
|
|
return ((index % g.columns) + g.columns) % g.columns;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-27 12:19:06 -04:00
|
|
|
|
function inArea(
|
|
|
|
|
|
index: number,
|
|
|
|
|
|
start: number,
|
|
|
|
|
|
end: number,
|
2023-07-06 00:43:17 -04:00
|
|
|
|
g: SparseGrid
|
2023-06-27 12:19:06 -04:00
|
|
|
|
): boolean {
|
2023-02-07 22:13:50 -05:00
|
|
|
|
const indexColumn = column(index, g);
|
|
|
|
|
|
const indexRow = row(index, g);
|
|
|
|
|
|
return (
|
|
|
|
|
|
indexRow >= row(start, g) &&
|
|
|
|
|
|
indexRow <= row(end, g) &&
|
|
|
|
|
|
indexColumn >= column(start, g) &&
|
|
|
|
|
|
indexColumn <= column(end, g)
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function* cellsInArea(
|
|
|
|
|
|
start: number,
|
|
|
|
|
|
end: number,
|
2023-07-06 00:43:17 -04:00
|
|
|
|
g: SparseGrid
|
2023-02-07 22:13:50 -05:00
|
|
|
|
): Generator<number, void, unknown> {
|
|
|
|
|
|
const startColumn = column(start, g);
|
|
|
|
|
|
const endColumn = column(end, g);
|
|
|
|
|
|
for (
|
|
|
|
|
|
let i = start;
|
|
|
|
|
|
i <= end;
|
|
|
|
|
|
i =
|
|
|
|
|
|
column(i, g) === endColumn
|
|
|
|
|
|
? i + g.columns + startColumn - endColumn
|
|
|
|
|
|
: i + 1
|
|
|
|
|
|
)
|
|
|
|
|
|
yield i;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-06 00:43:17 -04:00
|
|
|
|
export function forEachCellInArea<G extends Grid | SparseGrid>(
|
2023-02-07 22:13:50 -05:00
|
|
|
|
start: number,
|
|
|
|
|
|
end: number,
|
2023-07-06 00:43:17 -04:00
|
|
|
|
g: G,
|
|
|
|
|
|
fn: (c: G["cells"][0], i: number) => void
|
2023-02-07 22:13:50 -05:00
|
|
|
|
): void {
|
|
|
|
|
|
for (const i of cellsInArea(start, end, g)) fn(g.cells[i], i);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-06 00:43:17 -04:00
|
|
|
|
function allCellsInArea<G extends Grid | SparseGrid>(
|
2023-02-07 22:13:50 -05:00
|
|
|
|
start: number,
|
|
|
|
|
|
end: number,
|
2023-07-06 00:43:17 -04:00
|
|
|
|
g: G,
|
|
|
|
|
|
fn: (c: G["cells"][0], i: number) => boolean
|
2023-02-07 22:13:50 -05:00
|
|
|
|
): boolean {
|
|
|
|
|
|
for (const i of cellsInArea(start, end, g)) {
|
|
|
|
|
|
if (!fn(g.cells[i], i)) return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-06 00:43:17 -04:00
|
|
|
|
/**
|
|
|
|
|
|
* Counts the number of cells in the area that satsify the given predicate.
|
|
|
|
|
|
*/
|
|
|
|
|
|
function countCellsInArea<G extends Grid | SparseGrid>(
|
|
|
|
|
|
start: number,
|
|
|
|
|
|
end: number,
|
|
|
|
|
|
g: G,
|
|
|
|
|
|
predicate: (c: G["cells"][0], i: number) => boolean
|
|
|
|
|
|
): number {
|
|
|
|
|
|
let count = 0;
|
|
|
|
|
|
for (const i of cellsInArea(start, end, g)) {
|
|
|
|
|
|
if (predicate(g.cells[i], i)) count++;
|
|
|
|
|
|
}
|
|
|
|
|
|
return count;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-02-07 22:13:50 -05:00
|
|
|
|
const areaEnd = (
|
|
|
|
|
|
start: number,
|
|
|
|
|
|
columns: number,
|
|
|
|
|
|
rows: number,
|
2023-07-06 00:43:17 -04:00
|
|
|
|
g: SparseGrid
|
2023-02-07 22:13:50 -05:00
|
|
|
|
): number => start + columns - 1 + g.columns * (rows - 1);
|
|
|
|
|
|
|
2023-07-06 00:43:17 -04:00
|
|
|
|
const cloneGrid = <G extends Grid | SparseGrid>(g: G): G => ({
|
2023-06-27 12:19:06 -04:00
|
|
|
|
...g,
|
|
|
|
|
|
cells: [...g.cells],
|
|
|
|
|
|
});
|
2023-06-17 17:28:54 -04:00
|
|
|
|
|
2023-02-07 22:13:50 -05:00
|
|
|
|
/**
|
|
|
|
|
|
* Gets the index of the next gap in the grid that should be backfilled by 1×1
|
|
|
|
|
|
* tiles.
|
|
|
|
|
|
*/
|
2023-07-06 00:43:17 -04:00
|
|
|
|
function getNextGap(
|
|
|
|
|
|
g: SparseGrid,
|
|
|
|
|
|
ignoreGap: (cell: number) => boolean
|
|
|
|
|
|
): number | null {
|
2023-02-07 22:13:50 -05:00
|
|
|
|
const last1By1Index = findLast1By1Index(g);
|
|
|
|
|
|
if (last1By1Index === null) return null;
|
|
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < last1By1Index; i++) {
|
|
|
|
|
|
// To make the backfilling process look natural when there are multiple
|
|
|
|
|
|
// gaps, we actually scan each row from right to left
|
2023-07-06 00:43:17 -04:00
|
|
|
|
const j = i; /*
|
2023-02-07 22:13:50 -05:00
|
|
|
|
(row(i, g) === row(last1By1Index, g)
|
|
|
|
|
|
? last1By1Index
|
|
|
|
|
|
: (row(i, g) + 1) * g.columns) -
|
|
|
|
|
|
1 -
|
2023-07-06 00:43:17 -04:00
|
|
|
|
column(i, g);*/
|
2023-02-07 22:13:50 -05:00
|
|
|
|
|
2023-07-06 00:43:17 -04:00
|
|
|
|
if (!ignoreGap(j) && g.cells[j] === undefined) return j;
|
2023-02-07 22:13:50 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-17 15:21:38 -04:00
|
|
|
|
/**
|
|
|
|
|
|
* Moves the tile at index "from" over to index "to", displacing other tiles
|
|
|
|
|
|
* along the way.
|
|
|
|
|
|
* Precondition: the destination area must consist of only 1×1 tiles.
|
|
|
|
|
|
*/
|
2023-07-06 00:43:17 -04:00
|
|
|
|
function moveTileUnchecked(g: SparseGrid, from: number, to: number) {
|
2023-06-17 15:21:38 -04:00
|
|
|
|
const tile = g.cells[from]!;
|
|
|
|
|
|
const fromEnd = areaEnd(from, tile.columns, tile.rows, g);
|
|
|
|
|
|
const toEnd = areaEnd(to, tile.columns, tile.rows, g);
|
|
|
|
|
|
|
|
|
|
|
|
const displacedTiles: Cell[] = [];
|
|
|
|
|
|
forEachCellInArea(to, toEnd, g, (c, i) => {
|
2023-07-06 00:43:17 -04:00
|
|
|
|
if (c !== undefined && !inArea(i, from, fromEnd, g))
|
|
|
|
|
|
displacedTiles.push(c!);
|
2023-06-17 15:21:38 -04:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const movingCells: Cell[] = [];
|
|
|
|
|
|
forEachCellInArea(from, fromEnd, g, (c, i) => {
|
|
|
|
|
|
movingCells.push(c!);
|
|
|
|
|
|
g.cells[i] = undefined;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
forEachCellInArea(
|
|
|
|
|
|
to,
|
|
|
|
|
|
toEnd,
|
|
|
|
|
|
g,
|
|
|
|
|
|
(_c, i) => (g.cells[i] = movingCells.shift())
|
|
|
|
|
|
);
|
|
|
|
|
|
forEachCellInArea(
|
|
|
|
|
|
from,
|
|
|
|
|
|
fromEnd,
|
|
|
|
|
|
g,
|
|
|
|
|
|
(_c, i) => (g.cells[i] ??= displacedTiles.shift())
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-17 17:28:54 -04:00
|
|
|
|
/**
|
|
|
|
|
|
* Moves the tile at index "from" over to index "to", if there is space.
|
|
|
|
|
|
*/
|
2023-07-06 00:43:17 -04:00
|
|
|
|
export function moveTile<G extends Grid | SparseGrid>(
|
|
|
|
|
|
g: G,
|
2023-06-27 12:19:06 -04:00
|
|
|
|
from: number,
|
|
|
|
|
|
to: number
|
2023-07-06 00:43:17 -04:00
|
|
|
|
): G {
|
2023-06-17 17:28:54 -04:00
|
|
|
|
const tile = g.cells[from]!;
|
|
|
|
|
|
|
|
|
|
|
|
if (
|
2023-06-27 12:19:06 -04:00
|
|
|
|
to !== from && // Skip the operation if nothing would move
|
2023-06-18 00:28:08 -04:00
|
|
|
|
to >= 0 &&
|
2023-06-17 17:28:54 -04:00
|
|
|
|
to < g.cells.length &&
|
|
|
|
|
|
column(to, g) <= g.columns - tile.columns
|
|
|
|
|
|
) {
|
|
|
|
|
|
const fromEnd = areaEnd(from, tile.columns, tile.rows, g);
|
|
|
|
|
|
const toEnd = areaEnd(to, tile.columns, tile.rows, g);
|
|
|
|
|
|
|
|
|
|
|
|
// The contents of a given cell are 'displaceable' if it's empty, holds a
|
|
|
|
|
|
// 1×1 tile, or is part of the original tile we're trying to reposition
|
|
|
|
|
|
const displaceable = (c: Cell | undefined, i: number): boolean =>
|
2023-07-06 00:43:17 -04:00
|
|
|
|
c === undefined || is1By1(c) || inArea(i, from, fromEnd, g);
|
2023-06-17 17:28:54 -04:00
|
|
|
|
|
|
|
|
|
|
if (allCellsInArea(to, toEnd, g, displaceable)) {
|
|
|
|
|
|
// The target space is free; move
|
|
|
|
|
|
const gClone = cloneGrid(g);
|
2023-06-27 12:19:06 -04:00
|
|
|
|
moveTileUnchecked(gClone, from, to);
|
2023-06-17 17:28:54 -04:00
|
|
|
|
return gClone;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// The target space isn't free; don't move
|
|
|
|
|
|
return g;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-17 15:21:38 -04:00
|
|
|
|
/**
|
2023-07-06 00:43:17 -04:00
|
|
|
|
* Attempts to push a tile upwards by a number of rows, displacing 1×1 tiles.
|
|
|
|
|
|
* @returns The number of rows the tile was successfully pushed (may be less
|
|
|
|
|
|
* than requested if there are obstacles blocking movement).
|
2023-06-17 15:21:38 -04:00
|
|
|
|
*/
|
2023-07-06 00:43:17 -04:00
|
|
|
|
function pushTileUp(
|
|
|
|
|
|
g: SparseGrid,
|
|
|
|
|
|
from: number,
|
|
|
|
|
|
rows: number,
|
|
|
|
|
|
avoid: (cell: number) => boolean = () => false
|
|
|
|
|
|
): number {
|
2023-06-17 15:21:38 -04:00
|
|
|
|
const tile = g.cells[from]!;
|
|
|
|
|
|
|
2023-07-06 00:43:17 -04:00
|
|
|
|
for (let tryRows = rows; tryRows > 0; tryRows--) {
|
|
|
|
|
|
const to = from - tryRows * g.columns;
|
|
|
|
|
|
const toEnd = areaEnd(to, tile.columns, tile.rows, g);
|
2023-06-17 15:21:38 -04:00
|
|
|
|
|
2023-07-06 00:43:17 -04:00
|
|
|
|
const cellsAboveAreDisplacable =
|
|
|
|
|
|
from - g.columns >= 0 &&
|
|
|
|
|
|
allCellsInArea(
|
|
|
|
|
|
to,
|
|
|
|
|
|
Math.min(from - g.columns + tile.columns - 1, toEnd),
|
|
|
|
|
|
g,
|
|
|
|
|
|
(c, i) => (c === undefined || is1By1(c)) && !avoid(i)
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
if (cellsAboveAreDisplacable) {
|
|
|
|
|
|
moveTileUnchecked(g, from, to);
|
|
|
|
|
|
return tryRows;
|
|
|
|
|
|
}
|
2023-06-17 15:21:38 -04:00
|
|
|
|
}
|
2023-07-06 00:43:17 -04:00
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function trimTrailingGaps(g: SparseGrid) {
|
|
|
|
|
|
// Shrink the array to remove trailing gaps
|
|
|
|
|
|
const newLength = (findLastIndex(g.cells, (c) => c !== undefined) ?? -1) + 1;
|
|
|
|
|
|
if (newLength !== g.cells.length) g.cells = g.cells.slice(0, newLength);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Determines whether the given area is sufficiently clear of obstacles for
|
|
|
|
|
|
* vacateArea to work.
|
|
|
|
|
|
*/
|
|
|
|
|
|
function canVacateArea(g: SparseGrid, start: number, end: number): boolean {
|
|
|
|
|
|
const newCellCount = countCellsInArea(start, end, g, (c) => c !== undefined);
|
|
|
|
|
|
const newFullRows = Math.floor(newCellCount / g.columns);
|
|
|
|
|
|
return allCellsInArea(
|
|
|
|
|
|
start,
|
|
|
|
|
|
end - newFullRows * g.columns,
|
|
|
|
|
|
g,
|
|
|
|
|
|
(c) => c === undefined || is1By1(c)
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Clears away all the tiles in a given area by pushing them elsewhere.
|
|
|
|
|
|
* Precondition: the area must first be checked with canVacateArea, and the only
|
|
|
|
|
|
* gaps in the given grid must lie either within the area being cleared, or
|
|
|
|
|
|
* after the last 1×1 tile.
|
|
|
|
|
|
*/
|
|
|
|
|
|
function vacateArea(g: SparseGrid, start: number, end: number): SparseGrid {
|
|
|
|
|
|
const newCellCount = countCellsInArea(
|
|
|
|
|
|
start,
|
|
|
|
|
|
end,
|
|
|
|
|
|
g,
|
|
|
|
|
|
(c, i) => c !== undefined || i >= g.cells.length
|
|
|
|
|
|
);
|
|
|
|
|
|
const newFullRows = Math.floor(newCellCount / g.columns);
|
|
|
|
|
|
const endRow = row(end, g);
|
|
|
|
|
|
|
|
|
|
|
|
// To avoid subverting users' expectations, this operation should be the exact
|
|
|
|
|
|
// inverse of fillGaps. We do this by reverse-engineering a grid G with the
|
|
|
|
|
|
// area cleared out and structured such that fillGaps(G) = g.
|
|
|
|
|
|
|
|
|
|
|
|
// A grid that will have the same structure as the final result, but be filled
|
|
|
|
|
|
// with fake data
|
|
|
|
|
|
const outputStructure: SparseGrid = {
|
|
|
|
|
|
columns: g.columns,
|
|
|
|
|
|
cells: new Array(g.cells.length + newCellCount),
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// The first step in populating outputStructure is to copy over all the large
|
|
|
|
|
|
// tiles, pushing those tiles downwards that fillGaps would push upwards
|
|
|
|
|
|
g.cells.forEach((cell, fromStart) => {
|
|
|
|
|
|
if (cell?.origin && !is1By1(cell)) {
|
|
|
|
|
|
const fromEnd = areaEnd(fromStart, cell.columns, cell.rows, g);
|
|
|
|
|
|
const offset =
|
|
|
|
|
|
row(fromStart, g) + newFullRows > endRow ? newFullRows * g.columns : 0;
|
|
|
|
|
|
forEachCellInArea(fromStart, fromEnd, g, (c, i) => {
|
|
|
|
|
|
outputStructure.cells[i + offset] = c;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Then, we need to fill it in with the same number of 1×1 tiles as appear in
|
|
|
|
|
|
// the input
|
|
|
|
|
|
const oneByOneTileCount = count(g.cells, (c) => c !== undefined && is1By1(c));
|
|
|
|
|
|
let oneByOneTilesDistributed = 0;
|
|
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < outputStructure.cells.length; i++) {
|
|
|
|
|
|
if (outputStructure.cells[i] === undefined) {
|
|
|
|
|
|
if (inArea(i, start, end, g)) {
|
|
|
|
|
|
// Leave the requested area clear
|
|
|
|
|
|
outputStructure.cells[i] = undefined;
|
|
|
|
|
|
} else if (oneByOneTilesDistributed < oneByOneTileCount) {
|
|
|
|
|
|
outputStructure.cells[i] = {
|
|
|
|
|
|
// Fake data because we only care about the grid's structure
|
|
|
|
|
|
item: {} as unknown as TileDescriptor<unknown>,
|
|
|
|
|
|
origin: true,
|
|
|
|
|
|
columns: 1,
|
|
|
|
|
|
rows: 1,
|
|
|
|
|
|
};
|
|
|
|
|
|
oneByOneTilesDistributed++;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Lastly, handle the edge case where there were gaps in the input after the
|
|
|
|
|
|
// last 1×1 tile by resizing the cells array to delete these gaps
|
|
|
|
|
|
trimTrailingGaps(outputStructure);
|
|
|
|
|
|
|
|
|
|
|
|
// outputStructure is now fully populated, and so running fillGaps on it
|
|
|
|
|
|
// should produce a grid with the same structure as the input
|
|
|
|
|
|
const inputStructure = fillGaps(
|
|
|
|
|
|
outputStructure,
|
|
|
|
|
|
false,
|
|
|
|
|
|
(i) => inArea(i, start, end, g) && g.cells[i] === undefined
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// We exploit the fact that g and inputStructure have the same structure to
|
|
|
|
|
|
// create a mapping between cells in the structure grids and cells in g
|
|
|
|
|
|
const structureMapping = new Map(zip(inputStructure.cells, g.cells));
|
|
|
|
|
|
|
|
|
|
|
|
// And finally, we can use that mapping to swap the fake data in
|
|
|
|
|
|
// outputStructure with the real thing
|
|
|
|
|
|
return {
|
|
|
|
|
|
columns: g.columns,
|
|
|
|
|
|
cells: outputStructure.cells.map((placeholder) =>
|
|
|
|
|
|
structureMapping.get(placeholder)
|
|
|
|
|
|
),
|
|
|
|
|
|
};
|
2023-06-17 15:21:38 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-02-13 12:17:09 -05:00
|
|
|
|
/**
|
|
|
|
|
|
* Backfill any gaps in the grid.
|
|
|
|
|
|
*/
|
2023-07-06 00:43:17 -04:00
|
|
|
|
export function fillGaps(
|
|
|
|
|
|
g: SparseGrid,
|
|
|
|
|
|
packLargeTiles?: true,
|
|
|
|
|
|
ignoreGap?: () => false
|
|
|
|
|
|
): Grid;
|
|
|
|
|
|
export function fillGaps(
|
|
|
|
|
|
g: SparseGrid,
|
|
|
|
|
|
packLargeTiles?: boolean,
|
|
|
|
|
|
ignoreGap?: (cell: number) => boolean
|
|
|
|
|
|
): SparseGrid;
|
|
|
|
|
|
export function fillGaps(
|
|
|
|
|
|
g: SparseGrid,
|
|
|
|
|
|
packLargeTiles = true,
|
|
|
|
|
|
ignoreGap: (cell: number) => boolean = () => false
|
|
|
|
|
|
): SparseGrid {
|
|
|
|
|
|
const lastGap = findLastIndex(
|
|
|
|
|
|
g.cells,
|
|
|
|
|
|
(c, i) => c === undefined && !ignoreGap(i)
|
|
|
|
|
|
);
|
|
|
|
|
|
if (lastGap === null) return g; // There are no gaps to fill
|
|
|
|
|
|
const lastGapRow = row(lastGap, g);
|
|
|
|
|
|
|
|
|
|
|
|
const result = cloneGrid(g);
|
|
|
|
|
|
|
|
|
|
|
|
// This will be the size of the grid after we're done here (assuming we're
|
|
|
|
|
|
// allowed to pack the large tiles into the rest of the grid as necessary)
|
|
|
|
|
|
let idealLength = count(
|
|
|
|
|
|
result.cells,
|
|
|
|
|
|
(c, i) => c !== undefined || ignoreGap(i)
|
|
|
|
|
|
);
|
|
|
|
|
|
const fullRowsRemoved = Math.floor(
|
|
|
|
|
|
(g.cells.length - idealLength) / g.columns
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// Step 1: Push all large tiles below the last gap upwards, so that they move
|
|
|
|
|
|
// roughly the same distance that we're expecting 1×1 tiles to move
|
|
|
|
|
|
if (fullRowsRemoved > 0) {
|
|
|
|
|
|
for (
|
|
|
|
|
|
let i = (lastGapRow + 1) * result.columns;
|
|
|
|
|
|
i < result.cells.length;
|
|
|
|
|
|
i++
|
|
|
|
|
|
) {
|
|
|
|
|
|
const cell = result.cells[i];
|
|
|
|
|
|
if (cell?.origin && !is1By1(cell))
|
|
|
|
|
|
pushTileUp(result, i, fullRowsRemoved, ignoreGap);
|
2023-06-17 15:21:38 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-06 00:43:17 -04:00
|
|
|
|
// Step 2: Deal with any large tiles that are still hanging off the bottom
|
|
|
|
|
|
if (packLargeTiles) {
|
|
|
|
|
|
for (let i = result.cells.length - 1; i >= idealLength; i--) {
|
|
|
|
|
|
const cell = result.cells[i];
|
|
|
|
|
|
if (cell !== undefined && !is1By1(cell)) {
|
|
|
|
|
|
// First, try to just push it upwards a bit more
|
|
|
|
|
|
const originIndex =
|
|
|
|
|
|
i - (cell.columns - 1) - result.columns * (cell.rows - 1);
|
|
|
|
|
|
const pushed = pushTileUp(result, originIndex, 1, ignoreGap) === 1;
|
|
|
|
|
|
|
|
|
|
|
|
// If that failed, collapse the tile to 1×1 so it can be dealt with in
|
|
|
|
|
|
// step 3
|
|
|
|
|
|
if (!pushed) {
|
|
|
|
|
|
const collapsedTile: Cell = {
|
|
|
|
|
|
item: cell.item,
|
|
|
|
|
|
origin: true,
|
|
|
|
|
|
columns: 1,
|
|
|
|
|
|
rows: 1,
|
|
|
|
|
|
};
|
|
|
|
|
|
forEachCellInArea(originIndex, i, result, (_c, j) => {
|
|
|
|
|
|
result.cells[j] = undefined;
|
|
|
|
|
|
});
|
|
|
|
|
|
result.cells[i] = collapsedTile;
|
|
|
|
|
|
// Collapsing the tile makes the final grid size smaller
|
|
|
|
|
|
idealLength -= cell.columns * cell.rows - 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Step 3: Fill all remaining gaps with 1×1 tiles
|
|
|
|
|
|
let gap = getNextGap(result, ignoreGap);
|
2023-02-07 22:13:50 -05:00
|
|
|
|
|
|
|
|
|
|
if (gap !== null) {
|
2023-07-06 00:43:17 -04:00
|
|
|
|
const pathsToEnd = getPaths(result, findLast1By1Index(result)!, ignoreGap);
|
2023-02-07 22:13:50 -05:00
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
|
let filled = false;
|
|
|
|
|
|
let to = gap;
|
2023-02-13 12:17:09 -05:00
|
|
|
|
let from = pathsToEnd[gap];
|
2023-02-07 22:13:50 -05:00
|
|
|
|
|
|
|
|
|
|
// First, attempt to fill the gap by moving 1×1 tiles backwards from the
|
|
|
|
|
|
// end of the grid along a set path
|
2023-02-13 12:17:09 -05:00
|
|
|
|
while (from !== null) {
|
2023-07-06 00:43:17 -04:00
|
|
|
|
const toCell = result.cells[to] as Cell | undefined;
|
|
|
|
|
|
const fromCell = result.cells[from] as Cell | undefined;
|
2023-02-07 22:13:50 -05:00
|
|
|
|
|
2023-02-13 12:17:09 -05:00
|
|
|
|
// Skip over slots that are already full
|
2023-02-07 22:13:50 -05:00
|
|
|
|
if (toCell !== undefined) {
|
2023-02-13 12:17:09 -05:00
|
|
|
|
to = pathsToEnd[to]!;
|
2023-02-07 22:13:50 -05:00
|
|
|
|
// Skip over large tiles. Also, we might run into gaps along the path
|
|
|
|
|
|
// created during the filling of previous gaps. Skip over those too;
|
|
|
|
|
|
// they'll be picked up on the next iteration of the outer loop.
|
2023-07-06 00:43:17 -04:00
|
|
|
|
} else if (fromCell === undefined || !is1By1(fromCell)) {
|
2023-02-13 12:17:09 -05:00
|
|
|
|
from = pathsToEnd[from];
|
2023-02-07 22:13:50 -05:00
|
|
|
|
} else {
|
|
|
|
|
|
result.cells[to] = result.cells[from];
|
|
|
|
|
|
result.cells[from] = undefined;
|
|
|
|
|
|
filled = true;
|
2023-02-13 12:17:09 -05:00
|
|
|
|
to = pathsToEnd[to]!;
|
|
|
|
|
|
from = pathsToEnd[from];
|
2023-02-07 22:13:50 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// In case the path approach failed, fall back to taking the very last 1×1
|
|
|
|
|
|
// tile, and just dropping it into place
|
|
|
|
|
|
if (!filled) {
|
|
|
|
|
|
const last1By1Index = findLast1By1Index(result)!;
|
|
|
|
|
|
result.cells[gap] = result.cells[last1By1Index];
|
|
|
|
|
|
result.cells[last1By1Index] = undefined;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-06 00:43:17 -04:00
|
|
|
|
gap = getNextGap(result, ignoreGap);
|
2023-02-07 22:13:50 -05:00
|
|
|
|
} while (gap !== null);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-06 00:43:17 -04:00
|
|
|
|
trimTrailingGaps(result);
|
2023-02-07 22:13:50 -05:00
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-06 00:43:17 -04:00
|
|
|
|
// TODO: replace all usages of this function with vacateArea, as this results in
|
|
|
|
|
|
// somewhat unpredictable movement
|
|
|
|
|
|
function createRows(g: SparseGrid, count: number, atRow: number): SparseGrid {
|
2023-06-18 00:47:37 -04:00
|
|
|
|
const result = {
|
|
|
|
|
|
columns: g.columns,
|
|
|
|
|
|
cells: new Array(g.cells.length + g.columns * count),
|
2023-02-07 23:27:49 -05:00
|
|
|
|
};
|
2023-06-18 00:47:37 -04:00
|
|
|
|
const offsetAfterNewRows = g.columns * count;
|
|
|
|
|
|
|
|
|
|
|
|
// Copy tiles from the original grid to the new one, with the new rows
|
|
|
|
|
|
// inserted at the target location
|
|
|
|
|
|
g.cells.forEach((c, from) => {
|
|
|
|
|
|
if (c?.origin) {
|
|
|
|
|
|
const offset = row(from, g) >= atRow ? offsetAfterNewRows : 0;
|
|
|
|
|
|
forEachCellInArea(
|
|
|
|
|
|
from,
|
|
|
|
|
|
areaEnd(from, c.columns, c.rows, g),
|
|
|
|
|
|
g,
|
|
|
|
|
|
(c, i) => {
|
|
|
|
|
|
result.cells[i + offset] = c;
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-07-06 00:43:17 -04:00
|
|
|
|
* Adds a set of new items into the grid.
|
2023-06-18 00:47:37 -04:00
|
|
|
|
*/
|
2023-06-27 12:19:06 -04:00
|
|
|
|
export function addItems(
|
|
|
|
|
|
items: TileDescriptor<unknown>[],
|
2023-07-06 00:43:17 -04:00
|
|
|
|
g: SparseGrid
|
|
|
|
|
|
): SparseGrid {
|
|
|
|
|
|
let result: SparseGrid = cloneGrid(g);
|
2023-06-18 00:47:37 -04:00
|
|
|
|
|
|
|
|
|
|
for (const item of items) {
|
|
|
|
|
|
const cell = {
|
|
|
|
|
|
item,
|
|
|
|
|
|
origin: true,
|
|
|
|
|
|
columns: 1,
|
|
|
|
|
|
rows: 1,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let placeAt: number;
|
|
|
|
|
|
|
|
|
|
|
|
if (item.placeNear === undefined) {
|
|
|
|
|
|
// This item has no special placement requests, so let's put it
|
|
|
|
|
|
// uneventfully at the end of the grid
|
|
|
|
|
|
placeAt = result.cells.length;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// This item wants to be placed near another; let's put it on a row
|
|
|
|
|
|
// directly below the related tile
|
|
|
|
|
|
const placeNear = result.cells.findIndex(
|
|
|
|
|
|
(c) => c?.item.id === item.placeNear
|
|
|
|
|
|
);
|
|
|
|
|
|
if (placeNear === -1) {
|
|
|
|
|
|
// Can't find the related tile, so let's give up and place it at the end
|
|
|
|
|
|
placeAt = result.cells.length;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
const placeNearCell = result.cells[placeNear]!;
|
|
|
|
|
|
const placeNearEnd = areaEnd(
|
|
|
|
|
|
placeNear,
|
|
|
|
|
|
placeNearCell.columns,
|
|
|
|
|
|
placeNearCell.rows,
|
|
|
|
|
|
result
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
result = createRows(result, 1, row(placeNearEnd, result) + 1);
|
|
|
|
|
|
placeAt =
|
|
|
|
|
|
placeNear +
|
|
|
|
|
|
Math.floor(placeNearCell.columns / 2) +
|
|
|
|
|
|
result.columns * placeNearCell.rows;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
result.cells[placeAt] = cell;
|
|
|
|
|
|
|
|
|
|
|
|
if (item.largeBaseSize) {
|
|
|
|
|
|
// Cycle the tile size once to set up the tile with its larger base size
|
|
|
|
|
|
// This also fills any gaps in the grid, hence no extra call to fillGaps
|
2023-06-27 12:19:06 -04:00
|
|
|
|
result = cycleTileSize(result, item);
|
2023-06-18 00:47:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
2023-02-07 23:27:49 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-06 00:43:17 -04:00
|
|
|
|
const largeTileDimensions = (g: SparseGrid): [number, number] => [
|
2023-06-17 21:33:54 -04:00
|
|
|
|
Math.min(3, Math.max(2, g.columns - 1)),
|
|
|
|
|
|
2,
|
|
|
|
|
|
];
|
|
|
|
|
|
|
2023-07-06 00:43:17 -04:00
|
|
|
|
const extraLargeTileDimensions = (g: SparseGrid): [number, number] =>
|
2023-06-18 00:47:37 -04:00
|
|
|
|
g.columns > 3 ? [4, 3] : [g.columns, 2];
|
|
|
|
|
|
|
2023-07-06 00:43:17 -04:00
|
|
|
|
export function cycleTileSize<G extends Grid | SparseGrid>(
|
|
|
|
|
|
g: G,
|
2023-06-27 12:19:06 -04:00
|
|
|
|
tile: TileDescriptor<unknown>
|
2023-07-06 00:43:17 -04:00
|
|
|
|
): G {
|
2023-06-27 12:19:06 -04:00
|
|
|
|
const from = g.cells.findIndex((c) => c?.item === tile);
|
2023-02-07 22:13:50 -05:00
|
|
|
|
if (from === -1) return g; // Tile removed, no change
|
2023-06-18 00:47:37 -04:00
|
|
|
|
const fromCell = g.cells[from]!;
|
|
|
|
|
|
const fromWidth = fromCell.columns;
|
|
|
|
|
|
const fromHeight = fromCell.rows;
|
2023-02-07 22:13:50 -05:00
|
|
|
|
|
2023-06-18 00:47:37 -04:00
|
|
|
|
const [baseDimensions, enlargedDimensions] = fromCell.item.largeBaseSize
|
|
|
|
|
|
? [largeTileDimensions(g), extraLargeTileDimensions(g)]
|
|
|
|
|
|
: [[1, 1], largeTileDimensions(g)];
|
|
|
|
|
|
// The target dimensions, which toggle between the base and enlarged sizes
|
2023-02-07 22:13:50 -05:00
|
|
|
|
const [toWidth, toHeight] =
|
2023-06-18 00:47:37 -04:00
|
|
|
|
fromWidth === baseDimensions[0] && fromHeight === baseDimensions[1]
|
|
|
|
|
|
? enlargedDimensions
|
|
|
|
|
|
: baseDimensions;
|
2023-02-13 12:17:09 -05:00
|
|
|
|
|
2023-07-06 00:43:17 -04:00
|
|
|
|
return setTileSize(g, from, toWidth, toHeight);
|
|
|
|
|
|
}
|
2023-02-07 22:13:50 -05:00
|
|
|
|
|
2023-07-06 00:43:17 -04:00
|
|
|
|
/**
|
|
|
|
|
|
* Finds the cell nearest to 'nearestTo' that satsifies the given predicate.
|
|
|
|
|
|
* @param shouldScan A predicate constraining the bounds of the search.
|
|
|
|
|
|
*/
|
|
|
|
|
|
function findNearestCell<G extends Grid | SparseGrid>(
|
|
|
|
|
|
g: G,
|
|
|
|
|
|
nearestTo: number,
|
|
|
|
|
|
shouldScan: (index: number) => boolean,
|
|
|
|
|
|
predicate: (cell: G["cells"][0], index: number) => boolean
|
|
|
|
|
|
): number | null {
|
|
|
|
|
|
const scanLocations = new Set([nearestTo]);
|
|
|
|
|
|
|
|
|
|
|
|
for (const scanLocation of scanLocations) {
|
|
|
|
|
|
if (shouldScan(scanLocation)) {
|
|
|
|
|
|
if (predicate(g.cells[scanLocation], scanLocation)) return scanLocation;
|
|
|
|
|
|
|
|
|
|
|
|
// Scan outwards in all directions
|
|
|
|
|
|
const scanColumn = column(scanLocation, g);
|
|
|
|
|
|
const scanRow = row(scanLocation, g);
|
|
|
|
|
|
if (scanColumn > 0) scanLocations.add(scanLocation - 1);
|
|
|
|
|
|
if (scanColumn < g.columns - 1) scanLocations.add(scanLocation + 1);
|
|
|
|
|
|
if (scanRow > 0) scanLocations.add(scanLocation - g.columns);
|
|
|
|
|
|
scanLocations.add(scanLocation + g.columns);
|
2023-02-07 22:13:50 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-06 00:43:17 -04:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2023-02-07 22:13:50 -05:00
|
|
|
|
|
2023-07-06 00:43:17 -04:00
|
|
|
|
/**
|
|
|
|
|
|
* Changes the size of a tile, rearranging the grid to make space.
|
|
|
|
|
|
* @param tileId The ID of the tile to modify.
|
|
|
|
|
|
* @param g The grid.
|
|
|
|
|
|
* @returns The updated grid.
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function setTileSize<G extends Grid | SparseGrid>(
|
|
|
|
|
|
g: G,
|
|
|
|
|
|
from: number,
|
|
|
|
|
|
toWidth: number,
|
|
|
|
|
|
toHeight: number
|
|
|
|
|
|
): G {
|
|
|
|
|
|
const fromCell = g.cells[from]!;
|
|
|
|
|
|
const fromWidth = fromCell.columns;
|
|
|
|
|
|
const fromHeight = fromCell.rows;
|
|
|
|
|
|
const fromEnd = areaEnd(from, fromWidth, fromHeight, g);
|
|
|
|
|
|
const newGridSize =
|
|
|
|
|
|
g.cells.length + toWidth * toHeight - fromWidth * fromHeight;
|
2023-06-18 00:47:37 -04:00
|
|
|
|
|
2023-07-06 00:43:17 -04:00
|
|
|
|
const toColumn = Math.max(
|
|
|
|
|
|
0,
|
|
|
|
|
|
Math.min(
|
|
|
|
|
|
g.columns - toWidth,
|
|
|
|
|
|
column(from, g) + Math.trunc((fromWidth - toWidth) / 2)
|
|
|
|
|
|
)
|
2023-06-18 00:47:37 -04:00
|
|
|
|
);
|
2023-07-06 00:43:17 -04:00
|
|
|
|
const toRow = Math.max(
|
|
|
|
|
|
0,
|
|
|
|
|
|
row(from, g) + Math.trunc((fromHeight - toHeight) / 2)
|
|
|
|
|
|
);
|
|
|
|
|
|
const targetDest = toColumn + toRow * g.columns;
|
2023-02-07 22:13:50 -05:00
|
|
|
|
|
2023-07-06 00:43:17 -04:00
|
|
|
|
const gridWithoutTile = cloneGrid(g);
|
|
|
|
|
|
forEachCellInArea(from, fromEnd, gridWithoutTile, (_c, i) => {
|
|
|
|
|
|
gridWithoutTile.cells[i] = undefined;
|
2023-02-07 22:13:50 -05:00
|
|
|
|
});
|
|
|
|
|
|
|
2023-07-06 00:43:17 -04:00
|
|
|
|
const placeTile = (to: number, toEnd: number, grid: Grid | SparseGrid) => {
|
|
|
|
|
|
forEachCellInArea(to, toEnd, grid, (_c, i) => {
|
|
|
|
|
|
grid.cells[i] = {
|
|
|
|
|
|
item: fromCell.item,
|
|
|
|
|
|
origin: i === to,
|
|
|
|
|
|
columns: toWidth,
|
|
|
|
|
|
rows: toHeight,
|
|
|
|
|
|
};
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (toWidth <= fromWidth && toHeight <= fromHeight) {
|
|
|
|
|
|
// The tile is shrinking, which can always happen in-place
|
|
|
|
|
|
const to = targetDest;
|
|
|
|
|
|
const toEnd = areaEnd(to, toWidth, toHeight, g);
|
|
|
|
|
|
|
|
|
|
|
|
const result: SparseGrid = gridWithoutTile;
|
|
|
|
|
|
placeTile(to, toEnd, result);
|
|
|
|
|
|
return fillGaps(result, true, (i) => inArea(i, to, toEnd, g)) as G;
|
|
|
|
|
|
} else if (toWidth >= fromWidth && toHeight >= fromHeight) {
|
|
|
|
|
|
// The tile is growing, which might be able to happen in-place
|
|
|
|
|
|
const to = findNearestCell(
|
|
|
|
|
|
gridWithoutTile,
|
|
|
|
|
|
targetDest,
|
|
|
|
|
|
(i) => {
|
|
|
|
|
|
const end = areaEnd(i, toWidth, toHeight, g);
|
|
|
|
|
|
return (
|
|
|
|
|
|
column(i, g) + toWidth - 1 < g.columns &&
|
|
|
|
|
|
inArea(from, i, end, g) &&
|
|
|
|
|
|
inArea(fromEnd, i, end, g)
|
|
|
|
|
|
);
|
|
|
|
|
|
},
|
|
|
|
|
|
(_c, i) => {
|
|
|
|
|
|
const end = areaEnd(i, toWidth, toHeight, g);
|
|
|
|
|
|
return end < newGridSize && canVacateArea(gridWithoutTile, i, end);
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
if (to !== null) {
|
|
|
|
|
|
const toEnd = areaEnd(to, toWidth, toHeight, g);
|
|
|
|
|
|
const result = vacateArea(gridWithoutTile, to, toEnd);
|
|
|
|
|
|
|
|
|
|
|
|
placeTile(to, toEnd, result);
|
|
|
|
|
|
return result as G;
|
|
|
|
|
|
}
|
2023-02-07 22:13:50 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-06 00:43:17 -04:00
|
|
|
|
// Catch-all path for when the tile is neither strictly shrinking nor
|
|
|
|
|
|
// growing, or when there's not enough space for it to grow in-place
|
|
|
|
|
|
|
|
|
|
|
|
const packedGridWithoutTile = fillGaps(gridWithoutTile, false);
|
|
|
|
|
|
|
|
|
|
|
|
const to = findNearestCell(
|
|
|
|
|
|
packedGridWithoutTile,
|
|
|
|
|
|
targetDest,
|
|
|
|
|
|
(i) => i < newGridSize && column(i, g) + toWidth - 1 < g.columns,
|
|
|
|
|
|
(_c, i) => {
|
|
|
|
|
|
const end = areaEnd(i, toWidth, toHeight, g);
|
|
|
|
|
|
return end < newGridSize && canVacateArea(packedGridWithoutTile, i, end);
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
if (to === null) return g; // There's no space anywhere; give up
|
|
|
|
|
|
|
|
|
|
|
|
const toEnd = areaEnd(to, toWidth, toHeight, g);
|
|
|
|
|
|
const result = vacateArea(packedGridWithoutTile, to, toEnd);
|
|
|
|
|
|
|
|
|
|
|
|
placeTile(to, toEnd, result);
|
|
|
|
|
|
return result as G;
|
2023-02-07 22:13:50 -05:00
|
|
|
|
}
|
2023-06-17 21:33:54 -04:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Resizes the grid to a new column width.
|
|
|
|
|
|
*/
|
2023-07-06 00:43:17 -04:00
|
|
|
|
export function resize(g: Grid, columns: number): Grid {
|
|
|
|
|
|
const result: SparseGrid = { columns, cells: [] };
|
2023-06-17 21:33:54 -04:00
|
|
|
|
const [largeColumns, largeRows] = largeTileDimensions(result);
|
|
|
|
|
|
|
|
|
|
|
|
// Copy each tile from the old grid to the resized one in the same order
|
|
|
|
|
|
|
|
|
|
|
|
// The next index in the result grid to copy a tile to
|
|
|
|
|
|
let next = 0;
|
|
|
|
|
|
|
|
|
|
|
|
for (const cell of g.cells) {
|
2023-07-06 00:43:17 -04:00
|
|
|
|
if (cell.origin) {
|
2023-06-27 12:19:06 -04:00
|
|
|
|
// TODO make aware of extra large tiles
|
2023-07-06 00:43:17 -04:00
|
|
|
|
const [nextColumns, nextRows] = is1By1(cell)
|
|
|
|
|
|
? [1, 1]
|
|
|
|
|
|
: [largeColumns, largeRows];
|
2023-06-17 21:33:54 -04:00
|
|
|
|
|
|
|
|
|
|
// If there isn't enough space left on this row, jump to the next row
|
|
|
|
|
|
if (columns - column(next, result) < nextColumns)
|
|
|
|
|
|
next = columns * (Math.floor(next / columns) + 1);
|
|
|
|
|
|
const nextEnd = areaEnd(next, nextColumns, nextRows, result);
|
|
|
|
|
|
|
|
|
|
|
|
// Expand the cells array as necessary
|
|
|
|
|
|
if (result.cells.length <= nextEnd)
|
|
|
|
|
|
result.cells.push(...new Array(nextEnd + 1 - result.cells.length));
|
|
|
|
|
|
|
|
|
|
|
|
// Copy the tile into place
|
|
|
|
|
|
forEachCellInArea(next, nextEnd, result, (_c, i) => {
|
|
|
|
|
|
result.cells[i] = {
|
|
|
|
|
|
item: cell.item,
|
|
|
|
|
|
origin: i === next,
|
|
|
|
|
|
columns: nextColumns,
|
|
|
|
|
|
rows: nextRows,
|
|
|
|
|
|
};
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
next = nextEnd + 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return fillGaps(result);
|
|
|
|
|
|
}
|
2023-06-18 11:32:21 -04:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Promotes speakers to the first page of the grid.
|
|
|
|
|
|
*/
|
2023-07-06 00:43:17 -04:00
|
|
|
|
export function promoteSpeakers(g: SparseGrid) {
|
2023-06-18 11:32:21 -04:00
|
|
|
|
// This is all a bit of a hack right now, because we don't know if the designs
|
|
|
|
|
|
// will stick with this approach in the long run
|
|
|
|
|
|
// We assume that 4 rows are probably about 1 page
|
|
|
|
|
|
const firstPageEnd = g.columns * 4;
|
|
|
|
|
|
|
|
|
|
|
|
for (let from = firstPageEnd; from < g.cells.length; from++) {
|
|
|
|
|
|
const fromCell = g.cells[from];
|
|
|
|
|
|
// Don't bother trying to promote enlarged tiles
|
2023-07-06 00:43:17 -04:00
|
|
|
|
if (fromCell?.item.isSpeaker && is1By1(fromCell)) {
|
2023-06-18 11:32:21 -04:00
|
|
|
|
// Promote this tile by making 10 attempts to place it on the first page
|
|
|
|
|
|
for (let j = 0; j < 10; j++) {
|
|
|
|
|
|
const to = Math.floor(Math.random() * firstPageEnd);
|
|
|
|
|
|
const toCell = g.cells[to];
|
2023-07-06 00:43:17 -04:00
|
|
|
|
if (toCell === undefined || is1By1(toCell)) {
|
2023-06-27 12:19:06 -04:00
|
|
|
|
moveTileUnchecked(g, from, to);
|
2023-06-18 11:45:01 -04:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
2023-06-18 11:32:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-06-27 12:19:06 -04:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* The algorithm for updating a grid with a new set of tiles.
|
|
|
|
|
|
*/
|
2023-07-06 00:43:17 -04:00
|
|
|
|
function updateTiles(g: Grid, tiles: TileDescriptor<unknown>[]): Grid {
|
2023-06-27 12:19:06 -04:00
|
|
|
|
// Step 1: Update tiles that still exist, and remove tiles that have left
|
|
|
|
|
|
// the grid
|
|
|
|
|
|
const itemsById = new Map(tiles.map((i) => [i.id, i]));
|
2023-07-06 00:43:17 -04:00
|
|
|
|
const grid1: SparseGrid = {
|
2023-06-27 12:19:06 -04:00
|
|
|
|
...g,
|
|
|
|
|
|
cells: g.cells.map((c) => {
|
|
|
|
|
|
if (c === undefined) return undefined;
|
|
|
|
|
|
const item = itemsById.get(c.item.id);
|
|
|
|
|
|
return item === undefined ? undefined : { ...c, item };
|
|
|
|
|
|
}),
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Step 2: Add new tiles
|
|
|
|
|
|
const existingItemIds = new Set(
|
|
|
|
|
|
grid1.cells.filter((c) => c !== undefined).map((c) => c!.item.id)
|
|
|
|
|
|
);
|
|
|
|
|
|
const newItems = tiles.filter((i) => !existingItemIds.has(i.id));
|
|
|
|
|
|
const grid2 = addItems(newItems, grid1);
|
|
|
|
|
|
|
|
|
|
|
|
// Step 3: Promote speakers to the top
|
|
|
|
|
|
promoteSpeakers(grid2);
|
|
|
|
|
|
|
|
|
|
|
|
return fillGaps(grid2);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-06 00:43:17 -04:00
|
|
|
|
function updateBounds(g: Grid, bounds: RectReadOnly): Grid {
|
2023-06-27 12:19:06 -04:00
|
|
|
|
const columns = Math.max(2, Math.floor(bounds.width * 0.0045));
|
|
|
|
|
|
return columns === g.columns ? g : resize(g, columns);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-06 00:43:17 -04:00
|
|
|
|
const Slots: FC<{ s: Grid }> = memo(({ s: g }) => {
|
2023-06-27 12:19:06 -04:00
|
|
|
|
const areas = new Array<(number | null)[]>(
|
|
|
|
|
|
Math.ceil(g.cells.length / g.columns)
|
|
|
|
|
|
);
|
|
|
|
|
|
for (let i = 0; i < areas.length; i++)
|
|
|
|
|
|
areas[i] = new Array<number | null>(g.columns).fill(null);
|
|
|
|
|
|
|
|
|
|
|
|
let slotCount = 0;
|
|
|
|
|
|
for (let i = 0; i < g.cells.length; i++) {
|
|
|
|
|
|
const cell = g.cells[i];
|
2023-07-06 00:43:17 -04:00
|
|
|
|
if (cell.origin) {
|
2023-06-27 12:19:06 -04:00
|
|
|
|
const slotEnd = i + cell.columns - 1 + g.columns * (cell.rows - 1);
|
|
|
|
|
|
forEachCellInArea(
|
|
|
|
|
|
i,
|
|
|
|
|
|
slotEnd,
|
|
|
|
|
|
g,
|
|
|
|
|
|
(_c, j) => (areas[row(j, g)][column(j, g)] = slotCount)
|
|
|
|
|
|
);
|
|
|
|
|
|
slotCount++;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const style = {
|
|
|
|
|
|
gridTemplateAreas: areas
|
|
|
|
|
|
.map(
|
|
|
|
|
|
(row) =>
|
|
|
|
|
|
`'${row
|
|
|
|
|
|
.map((slotId) => (slotId === null ? "." : `s${slotId}`))
|
|
|
|
|
|
.join(" ")}'`
|
|
|
|
|
|
)
|
|
|
|
|
|
.join(" "),
|
|
|
|
|
|
gridTemplateColumns: `repeat(${g.columns}, 1fr)`,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const slots = new Array<ReactNode>(slotCount);
|
|
|
|
|
|
for (let i = 0; i < slotCount; i++)
|
|
|
|
|
|
slots[i] = <Slot key={i} style={{ gridArea: `s${i}` }} />;
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div className={styles.bigGrid} style={style}>
|
|
|
|
|
|
{slots}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Given a tile and numbers in the range [0, 1) describing a position within the
|
|
|
|
|
|
* tile, this returns the index of the specific cell in which that position
|
|
|
|
|
|
* lies.
|
|
|
|
|
|
*/
|
|
|
|
|
|
function positionOnTileToCell(
|
2023-07-06 00:43:17 -04:00
|
|
|
|
g: SparseGrid,
|
2023-06-27 12:19:06 -04:00
|
|
|
|
tileOriginIndex: number,
|
|
|
|
|
|
xPositionOnTile: number,
|
|
|
|
|
|
yPositionOnTile: number
|
|
|
|
|
|
): number {
|
|
|
|
|
|
const tileOrigin = g.cells[tileOriginIndex]!;
|
|
|
|
|
|
const columnOnTile = Math.floor(xPositionOnTile * tileOrigin.columns);
|
|
|
|
|
|
const rowOnTile = Math.floor(yPositionOnTile * tileOrigin.rows);
|
|
|
|
|
|
return tileOriginIndex + columnOnTile + g.columns * rowOnTile;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function dragTile(
|
2023-07-06 00:43:17 -04:00
|
|
|
|
g: Grid,
|
2023-06-27 12:19:06 -04:00
|
|
|
|
from: TileDescriptor<unknown>,
|
|
|
|
|
|
to: TileDescriptor<unknown>,
|
|
|
|
|
|
xPositionOnFrom: number,
|
|
|
|
|
|
yPositionOnFrom: number,
|
|
|
|
|
|
xPositionOnTo: number,
|
|
|
|
|
|
yPositionOnTo: number
|
2023-07-06 00:43:17 -04:00
|
|
|
|
): Grid {
|
|
|
|
|
|
const fromOrigin = g.cells.findIndex((c) => c.item === from);
|
|
|
|
|
|
const toOrigin = g.cells.findIndex((c) => c.item === to);
|
2023-06-27 12:19:06 -04:00
|
|
|
|
const fromCell = positionOnTileToCell(
|
|
|
|
|
|
g,
|
|
|
|
|
|
fromOrigin,
|
|
|
|
|
|
xPositionOnFrom,
|
|
|
|
|
|
yPositionOnFrom
|
|
|
|
|
|
);
|
|
|
|
|
|
const toCell = positionOnTileToCell(
|
|
|
|
|
|
g,
|
|
|
|
|
|
toOrigin,
|
|
|
|
|
|
xPositionOnTo,
|
|
|
|
|
|
yPositionOnTo
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
return moveTile(g, fromOrigin, fromOrigin + toCell - fromCell);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-06 00:43:17 -04:00
|
|
|
|
export const BigGrid: Layout<Grid> = {
|
2023-06-27 12:19:06 -04:00
|
|
|
|
emptyState: { columns: 4, cells: [] },
|
|
|
|
|
|
updateTiles,
|
|
|
|
|
|
updateBounds,
|
2023-07-06 00:43:17 -04:00
|
|
|
|
getTiles: <T,>(g) => g.cells.filter((c) => c.origin).map((c) => c!.item as T),
|
2023-06-27 12:19:06 -04:00
|
|
|
|
canDragTile: () => true,
|
|
|
|
|
|
dragTile,
|
|
|
|
|
|
toggleFocus: cycleTileSize,
|
|
|
|
|
|
Slots,
|
|
|
|
|
|
rememberState: false,
|
|
|
|
|
|
};
|