78 lines
3.3 KiB
TypeScript
78 lines
3.3 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// the golden fixture's hashes, shared by the tests that check generation against it: worldgen_golden_test.ts,
|
|
// worldgen_bundle_test.ts and worldgen_pool_test.ts. hashed by name, since numeric ids move whenever a block is added
|
|
import { AIR, ID_MASK } from "@/common/constants.ts";
|
|
import type { RawChunk } from "@/common/generation.ts";
|
|
import { AIR_ID } from "@/common/protocol.ts";
|
|
import { sha256 } from "@/common/utils.ts";
|
|
|
|
export const FIXTURE = new URL("./fixtures/worldgen_golden.json", import.meta.url);
|
|
export const SEEDS = ["golden-1", "golden-2", "golden-3"];
|
|
// 4 by 4 chunks around each of these, 64 a seed. far apart so they land in different terrain, and in squares so
|
|
// neighbors' spills are shared
|
|
export const CLUSTERS = [[0, 0], [40, -25], [-120, 85], [300, 300]];
|
|
|
|
export type Hashes = Record<string, Record<string, { raw: string; final: string }>>;
|
|
|
|
// every chunk the fixture has, as x, z
|
|
export function golden_chunks(): [number, number][] {
|
|
return CLUSTERS.flatMap(([x0, z0]) =>
|
|
[0, 1, 2, 3].flatMap((dx) => [0, 1, 2, 3].map((dz): [number, number] => [x0 + dx, z0 + dz]))
|
|
);
|
|
}
|
|
|
|
export function recorded(): Hashes {
|
|
return JSON.parse(Deno.readTextFileSync(FIXTURE));
|
|
}
|
|
|
|
// a block value's name and states, from numeric ids by id
|
|
export function namer(block_ids: Record<string, number>): (value: number) => string {
|
|
const names: string[] = [];
|
|
for (const [id, nid] of Object.entries(block_ids)) names[nid] = id;
|
|
names[AIR] = AIR_ID;
|
|
return (value) => `${names[value & ID_MASK]}[${value >>> 16}]`;
|
|
}
|
|
|
|
// what the fixture's "raw" is: the chunk's own blocks, its spills and its biomes
|
|
export function raw_hash(raw: RawChunk, name: (value: number) => string): Promise<string> {
|
|
const spills: (string | number)[] = [];
|
|
for (let i = 0; i < raw.spills.length; i += 4) {
|
|
spills.push(raw.spills[i], raw.spills[i + 1], raw.spills[i + 2], name(raw.spills[i + 3]));
|
|
}
|
|
return hash(blocks_text(raw.column.to_flat(), name), JSON.stringify(spills), raw.column.biomes.join(","));
|
|
}
|
|
|
|
// what the fixture's "final" is: the chunk with its neighbors' spills
|
|
export function final_hash(blocks: Uint32Array, name: (value: number) => string): Promise<string> {
|
|
return hash(blocks_text(blocks, name));
|
|
}
|
|
|
|
// the blocks as a palette of names in the order they first appear, then each block's place in it
|
|
function blocks_text(blocks: Uint32Array, name: (value: number) => string): (string | Uint8Array)[] {
|
|
const palette = new Map<number, number>();
|
|
const indices = new Uint16Array(blocks.length);
|
|
for (let i = 0; i < blocks.length; i++) {
|
|
let index = palette.get(blocks[i]);
|
|
if (index === undefined) {
|
|
index = palette.size;
|
|
palette.set(blocks[i], index);
|
|
}
|
|
indices[i] = index;
|
|
}
|
|
return [JSON.stringify([...palette.keys()].map(name)), new Uint8Array(indices.buffer)];
|
|
}
|
|
|
|
// the sha-256 of the parts one after another, strings each on their own line
|
|
function hash(...parts: (string | Uint8Array | (string | Uint8Array)[])[]) {
|
|
const bytes = parts.flat().map((part) => typeof part === "string" ? new TextEncoder().encode(part + "\n") : part);
|
|
const all = new Uint8Array(bytes.reduce((sum, part) => sum + part.length, 0));
|
|
let offset = 0;
|
|
for (const part of bytes) {
|
|
all.set(part, offset);
|
|
offset += part.length;
|
|
}
|
|
return sha256(all);
|
|
}
|