91 lines
3.8 KiB
TypeScript
91 lines
3.8 KiB
TypeScript
// world generation gives the same world block for block, release after release: a save only keeps what players
|
|
// changed, so any change to what generation makes changes every existing world (docs/modding_plan.md, rule 2).
|
|
// this generates chunks for a few seeds and compares them with tests/fixtures/worldgen_golden.json, see GENERATION.md.
|
|
// after a change that's meant to change generation, record it again:
|
|
// deno test -A tests/worldgen_golden_test.ts -- --update
|
|
import { assertEquals } from "@std/assert";
|
|
import { AIR, ID_MASK } from "$/common/constants.ts";
|
|
import { generate_raw_chunk } from "$/common/generation.ts";
|
|
import { AIR_ID } from "$/common/protocol.ts";
|
|
import { test_game } from "./helpers.ts";
|
|
|
|
const FIXTURE = new URL("./fixtures/worldgen_golden.json", import.meta.url);
|
|
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
|
|
const CLUSTERS = [[0, 0], [40, -25], [-120, 85], [300, 300]];
|
|
|
|
type Hashes = Record<string, Record<string, { raw: string; final: string }>>;
|
|
|
|
Deno.test("world generation makes the same chunks as recorded", async () => {
|
|
const hashes: Hashes = {};
|
|
for (const seed of SEEDS) {
|
|
const { game } = await test_game("mods", undefined, seed);
|
|
const world = game.world;
|
|
// by name, since numeric ids move whenever a block is added
|
|
const names: string[] = [];
|
|
for (const [id, nid] of Object.entries(world.block_ids)) names[nid] = id;
|
|
names[AIR] = AIR_ID;
|
|
const name = (value: number) => `${names[value & ID_MASK]}[${value >>> 16}]`;
|
|
|
|
hashes[seed] = {};
|
|
for (const [x0, z0] of CLUSTERS) {
|
|
for (let dx = 0; dx < 4; dx++) {
|
|
for (let dz = 0; dz < 4; dz++) {
|
|
const [x, z] = [x0 + dx, z0 + dz];
|
|
const raw = generate_raw_chunk(x, z, seed, world.block_ids, world.worldgen, world.default_values);
|
|
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]));
|
|
}
|
|
hashes[seed][`${x},${z}`] = {
|
|
raw: await sha256(blocks_text(raw.blocks, name), JSON.stringify(spills), raw.biomes.join(",")),
|
|
final: await sha256(blocks_text(world.generated_blocks(x, z), name)),
|
|
};
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (Deno.args.includes("--update")) {
|
|
Deno.writeTextFileSync(FIXTURE, JSON.stringify(hashes, null, "\t") + "\n");
|
|
return;
|
|
}
|
|
const recorded: Hashes = JSON.parse(Deno.readTextFileSync(FIXTURE));
|
|
for (const seed of SEEDS) {
|
|
for (const [chunk, hash] of Object.entries(hashes[seed])) {
|
|
assertEquals(hash, recorded[seed]?.[chunk], `seed ${seed}, chunk ${chunk} isn't what was recorded`);
|
|
}
|
|
}
|
|
});
|
|
|
|
// 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)];
|
|
}
|
|
|
|
async function sha256(...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;
|
|
}
|
|
let hex = "";
|
|
for (const byte of new Uint8Array(await crypto.subtle.digest("SHA-256", all))) {
|
|
hex += byte.toString(16).padStart(2, "0");
|
|
}
|
|
return hex;
|
|
}
|