Skip to content
Source
Sign inSign up
samwillis/one-million-boxes
Open History to see the last commit

tiles.ts

120 lines4.1 KB

1import { boxCoordsToId, coordsToEdgeId, edgeIdToCoords, isValidEdgeId } from "./edge-math.js"
2import { GRID_WIDTH, GRID_HEIGHT } from "./game-config.js"
3​
4export const TILE_SIZE = 8
5export const TILE_COLUMNS = GRID_WIDTH / TILE_SIZE
6export const TILE_ROWS = GRID_HEIGHT / TILE_SIZE
7export const TILE_COUNT = TILE_COLUMNS * TILE_ROWS
8​
9export interface BoardTile {
10 tile_id: number
11 revision: number
12 edges: Uint8Array
13 boxes: Uint8Array
15​
16export function getOwner(bytes: Uint8Array, index: number): number {
17 const bit = index * 3
18 return ((bytes[bit >> 3] | ((bytes[(bit >> 3) + 1] ?? 0) << 8)) >> (bit & 7)) & 7
20​
21export function setOwner(bytes: Uint8Array, index: number, owner: number): void {
22 if (
23 !Number.isInteger(index) ||
24 index < 0 ||
25 index * 3 + 3 > bytes.length * 8 ||
26 !Number.isInteger(owner) ||
27 owner < 0 ||
28 owner > 4
29 )
30 throw new Error("Invalid packed owner")
31 const bit = index * 3,
32 byte = bit >> 3,
33 shift = bit & 7
34 const value = ((bytes[byte] | ((bytes[byte + 1] ?? 0) << 8)) & ~(7 << shift)) | (owner << shift)
35 bytes[byte] = value & 255
36 if (shift > 5) bytes[byte + 1] = value >> 8
38​
39export function tileOrigin(tileId: number): { x: number; y: number } {
40 if (!Number.isInteger(tileId) || tileId < 0 || tileId >= TILE_COUNT)
41 throw new Error("Invalid tile ID")
42 return {
43 x: (tileId % TILE_COLUMNS) * TILE_SIZE,
44 y: Math.floor(tileId / TILE_COLUMNS) * TILE_SIZE,
45 }
47​
48export function boxLocation(x: number, y: number): { tileId: number; index: number } {
49 return {
50 tileId: Math.floor(y / TILE_SIZE) * TILE_COLUMNS + Math.floor(x / TILE_SIZE),
51 index: (y % TILE_SIZE) * TILE_SIZE + (x % TILE_SIZE),
52 }
54​
55export function edgeLocation(edgeId: number): { tileId: number; index: number } {
56 if (!isValidEdgeId(edgeId)) throw new Error("Invalid edge ID")
57 const { x, y, horizontal } = edgeIdToCoords(edgeId)
58 const tileX = Math.floor(Math.min(x, GRID_WIDTH - 1) / TILE_SIZE)
59 const tileY = Math.floor(Math.min(y, GRID_HEIGHT - 1) / TILE_SIZE)
60 const tileId = tileY * TILE_COLUMNS + tileX
61 if (horizontal && y === GRID_HEIGHT) return { tileId, index: 128 + (x % TILE_SIZE) }
62 if (!horizontal && x === GRID_WIDTH)
63 return { tileId, index: 128 + (tileY === TILE_ROWS - 1 ? TILE_SIZE : 0) + (y % TILE_SIZE) }
64 return { tileId, index: (horizontal ? 0 : 64) + (y % TILE_SIZE) * TILE_SIZE + (x % TILE_SIZE) }
66​
67export function emptyTile(tileId: number): BoardTile {
68 const { x, y } = tileOrigin(tileId)
69 const edgeCount =
70 128 + (x + TILE_SIZE === GRID_WIDTH ? 8 : 0) + (y + TILE_SIZE === GRID_HEIGHT ? 8 : 0)
71 return {
72 tile_id: tileId,
73 revision: 0,
74 edges: new Uint8Array((edgeCount * 3) / 8),
75 boxes: new Uint8Array(24),
76 }
78​
79export function visitTileEdges(
80 tileId: number,
81 visit: (edgeId: number, index: number) => void,
82): void {
83 const { x, y } = tileOrigin(tileId)
84 for (let row = 0; row < TILE_SIZE; row++)
85 for (let col = 0; col < TILE_SIZE; col++) {
86 const index = row * TILE_SIZE + col
87 visit(coordsToEdgeId(x + col, y + row, true), index)
88 visit(coordsToEdgeId(x + col, y + row, false), 64 + index)
89 }
90 let index = 128
91 if (y + TILE_SIZE === GRID_HEIGHT)
92 for (let col = 0; col < TILE_SIZE; col++)
93 visit(coordsToEdgeId(x + col, GRID_HEIGHT, true), index++)
94 if (x + TILE_SIZE === GRID_WIDTH)
95 for (let row = 0; row < TILE_SIZE; row++)
96 visit(coordsToEdgeId(GRID_WIDTH, y + row, false), index++)
98​
99export function visitTileBoxes(
100 tileId: number,
101 visit: (boxId: number, index: number) => void,
102): void {
103 const { x, y } = tileOrigin(tileId)
104 for (let row = 0; row < TILE_SIZE; row++)
105 for (let col = 0; col < TILE_SIZE; col++)
106 visit(boxCoordsToId(x + col, y + row), row * TILE_SIZE + col)
108​
109export function validateTile(tile: BoardTile): void {
110 const expected = emptyTile(tile.tile_id)
111 if (
112 !Number.isSafeInteger(tile.revision) ||
113 tile.revision < 0 ||
114 !(tile.edges instanceof Uint8Array) ||
115 !(tile.boxes instanceof Uint8Array) ||
116 tile.edges.length !== expected.edges.length ||
117 tile.boxes.length !== 24
118 )
119 throw new Error("Invalid board tile")