257 lines
11 KiB
TypeScript
257 lines
11 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// the render benchmark's level, made on the client from a fixed seed so every run draws the same thing whatever world
|
|
// the player is in: hills with caves, trees and plants, a lake with glass in it, and flat ground for the scenes with
|
|
// entities, visuals and changing blocks. it's made of what the loaded mods' worldgen data makes the ground of, and other
|
|
// blocks picked by what they are (a cutout plant, a translucent cube), not by their ids
|
|
import { Alea } from "@paulaboks/rng";
|
|
import { AIR, CHUNK_AREA, CHUNK_HEIGHT, CHUNK_SIZE } from "@/common/constants.ts";
|
|
import { type BlockRegistry, EverythingRegistry } from "@/common/everything_registry.ts";
|
|
import { DEFAULT_MODEL } from "@/common/block_models.ts";
|
|
import { OctaveNoise2D, OctaveNoise3D, smoothstep } from "@/common/worldgen/noise.ts";
|
|
import { can_stand_on, default_block_value } from "@/common/utils.ts";
|
|
import type { Biome } from "@/common/worldgen/biomes.ts";
|
|
import type { NoiseSettingsJson } from "@/common/worldgen/data.ts";
|
|
|
|
const SEED = "benchmark";
|
|
// chunks go from -LEVEL_RADIUS to LEVEL_RADIUS - 1 each way. the outermost ring is only there so the ones inside it
|
|
// can be meshed, see ClientLevel.can_mesh
|
|
export const LEVEL_RADIUS = 13;
|
|
export const SEA_LEVEL = 62;
|
|
|
|
// the lake, and flat ground for the scenes. the terrain eases into each over BLEND blocks
|
|
export const ZONES = {
|
|
lake: { x: -96, z: -96, radius: 44, height: 46 },
|
|
arena: { x: 96, z: -96, radius: 26, height: 72 },
|
|
visuals: { x: -96, z: 96, radius: 22, height: 72 },
|
|
updates: { x: 96, z: 96, radius: 18, height: 72 },
|
|
};
|
|
const BLEND = 24;
|
|
// what the block update scene changes, a wall facing south
|
|
export const UPDATE_WALL = {
|
|
x: ZONES.updates.x - 8,
|
|
y: ZONES.updates.height + 1,
|
|
z: ZONES.updates.z,
|
|
width: 16,
|
|
height: 8,
|
|
};
|
|
// the field of blocks drawn with code, this many on a side
|
|
const VISUALS_SIZE = 20;
|
|
// pillars standing in the lake
|
|
const GLASS_PILLARS = 6;
|
|
|
|
// of the ground's top blocks
|
|
const PLANT_CHANCE = 0.12;
|
|
const LIGHT_CHANCE = 0.003;
|
|
// a tree grows in each TREE_CELL square with TREE_CHANCE
|
|
const TREE_CELL = 8;
|
|
const TREE_CHANCE = 0.5;
|
|
// how far leaves reach from the trunk
|
|
const LEAVES_REACH = 2;
|
|
// caves are where their noise is above this
|
|
const CAVE_THRESHOLD = 0.42;
|
|
|
|
// what the level is made of, as values with their default states. what the loaded mods have no block for is left out
|
|
export interface BenchBlocks {
|
|
// the level's biome, for its colors
|
|
biome: string;
|
|
stone: number;
|
|
// the ground's cover, see SurfaceJson in common/worldgen/biomes.ts
|
|
top: number;
|
|
filler: number;
|
|
filler_depth: number;
|
|
underwater: number;
|
|
log: number;
|
|
leaves?: number;
|
|
plants: number[];
|
|
glass?: number;
|
|
fluid?: number;
|
|
light?: number;
|
|
// blocks drawn with code, see client/rendering/visuals.ts
|
|
visuals: number[];
|
|
}
|
|
|
|
// visual_blocks are the numeric ids of the blocks drawn with code. the same mods give the same blocks
|
|
export function bench_blocks(visual_blocks: ReadonlySet<number>): BenchBlocks {
|
|
const all = EverythingRegistry.get_registry<BlockRegistry>("blocks")
|
|
.map((block, nid) => ({ block, nid, value: default_block_value(nid, block) }))
|
|
.filter(({ block, nid }) => block && nid !== AIR)
|
|
.sort((a, b) => a.block.id.localeCompare(b.block.id));
|
|
type Entry = typeof all[number];
|
|
const layer = ({ block }: Entry) => block.render_layer ?? "solid";
|
|
const cube = ({ block }: Entry) => (block.model ?? DEFAULT_MODEL) === DEFAULT_MODEL && !block.parts;
|
|
// one look that nothing changes
|
|
const plain = ({ block, nid }: Entry) =>
|
|
!block.states?.length && !block.variants && !block.fluid && !block.light_emission && !visual_blocks.has(nid);
|
|
const tinted = ({ block }: Entry, color: string) => block.tint?.color === color;
|
|
const by_id = (id: string | undefined) => all.find((b) => b.block.id === id);
|
|
|
|
const solid = all.filter((b) => plain(b) && cube(b) && layer(b) === "solid" && b.block.has_collision);
|
|
if (solid.length === 0) throw new Error("The benchmark needs a solid block to build its level with");
|
|
// what the world generator fills the ground with, and a biome with trees for its cover
|
|
const by_key = ([a]: [string, unknown], [b]: [string, unknown]) => a.localeCompare(b);
|
|
const settings = EverythingRegistry.entries<NoiseSettingsJson>("noise_settings").sort(by_key)[0]?.[1];
|
|
const biomes = EverythingRegistry.entries<Biome>("biomes").sort(by_key).map(([, biome]) => biome);
|
|
const biome = biomes.find((biome) => biome.trees) ?? biomes[0];
|
|
const stone = by_id(settings?.default_block) ?? solid[0];
|
|
const top = by_id(biome?.surface.top) ?? solid.find((b) => tinted(b, "grass")) ?? stone;
|
|
const filler = by_id(biome?.surface.filler) ?? stone;
|
|
const ground = [stone, top, filler];
|
|
const leaves = all.filter((b) => plain(b) && cube(b) && layer(b) === "cutout");
|
|
// what says it grows on the ground's top, with its default states
|
|
const plants = all.filter((b) =>
|
|
b.block.place_on && can_stand_on(b.block, top.block.id) && layer(b) === "cutout" && !cube(b) &&
|
|
!b.block.has_collision && !visual_blocks.has(b.nid)
|
|
);
|
|
// water rather than lava
|
|
const fluids = all.filter((b) => b.block.fluid && !b.block.light_emission);
|
|
return {
|
|
biome: biome?.id ?? "",
|
|
stone: stone.value,
|
|
top: top.value,
|
|
filler: filler.value,
|
|
filler_depth: biome?.surface.filler_depth ?? 3,
|
|
underwater: (by_id(biome?.surface.underwater_top) ?? filler).value,
|
|
log: (solid.find((b) => !ground.includes(b)) ?? stone).value,
|
|
leaves: (leaves.find((b) => tinted(b, "foliage")) ?? leaves[0])?.value,
|
|
plants: plants.map((b) => b.value),
|
|
glass: all.find((b) => plain(b) && cube(b) && layer(b) === "translucent")?.value,
|
|
fluid: (by_id(settings?.fluids[0]?.block) ?? fluids.find((b) => layer(b) === "translucent") ?? fluids[0])
|
|
?.value,
|
|
light: all.find((b) => (b.block.light_emission ?? 0) > 0 && !b.block.fluid)?.value,
|
|
visuals: all.filter((b) => visual_blocks.has(b.nid)).map((b) => b.value),
|
|
};
|
|
}
|
|
|
|
// the level's terrain, a chunk at a time. a chunk always comes out the same
|
|
export class BenchTerrain {
|
|
#blocks: BenchBlocks;
|
|
#hills = new OctaveNoise2D(SEED, "hills", 160, [1, 0.5, 0.25, 0.125]);
|
|
#mountains = new OctaveNoise2D(SEED, "mountains", 320, [1, 0.5, 0.25]);
|
|
#caves = new OctaveNoise3D(SEED, "caves", 40, [1, 0.5], 0.75);
|
|
|
|
constructor(blocks: BenchBlocks) {
|
|
this.#blocks = blocks;
|
|
}
|
|
|
|
// the y of the top block
|
|
height(x: number, z: number): number {
|
|
const mountains = Math.max(0, this.#mountains.sample(x, z));
|
|
let height = 68 + this.#hills.sample(x, z) * 16 + mountains * mountains * 140;
|
|
for (const zone of Object.values(ZONES)) {
|
|
const t = smoothstep(zone.radius, zone.radius + BLEND, Math.hypot(x - zone.x, z - zone.z));
|
|
height = zone.height + (height - zone.height) * t;
|
|
}
|
|
return Math.min(CHUNK_HEIGHT - 32, Math.round(height));
|
|
}
|
|
|
|
chunk(chunk_x: number, chunk_z: number): { blocks: Uint32Array; biomes: string[] } {
|
|
const b = this.#blocks;
|
|
const blocks = new Uint32Array(CHUNK_AREA * CHUNK_HEIGHT);
|
|
const random = new Alea(`${SEED}_${chunk_x}_${chunk_z}`);
|
|
const x0 = chunk_x * CHUNK_SIZE;
|
|
const z0 = chunk_z * CHUNK_SIZE;
|
|
// in world coordinates, only what's in this chunk
|
|
const set = (x: number, y: number, z: number, value: number, only_air = false) => {
|
|
const lx = x - x0;
|
|
const lz = z - z0;
|
|
if (lx < 0 || lx >= CHUNK_SIZE || lz < 0 || lz >= CHUNK_SIZE || y < 0 || y >= CHUNK_HEIGHT) return;
|
|
const index = y * CHUNK_AREA + lz * CHUNK_SIZE + lx;
|
|
if (!only_air || blocks[index] === AIR) blocks[index] = value;
|
|
};
|
|
|
|
for (let lz = 0; lz < CHUNK_SIZE; lz++) {
|
|
for (let lx = 0; lx < CHUNK_SIZE; lx++) {
|
|
const x = x0 + lx;
|
|
const z = z0 + lz;
|
|
const height = this.height(x, z);
|
|
const shore = height <= SEA_LEVEL + 1;
|
|
const cover = height <= SEA_LEVEL ? b.underwater : b.top;
|
|
const column = lz * CHUNK_SIZE + lx;
|
|
for (let y = 0; y <= height; y++) {
|
|
let value = y === height ? cover : y >= height - b.filler_depth ? b.filler : b.stone;
|
|
if (y > 4 && y < height - 4 && this.#caves.sample(x, y, z) > CAVE_THRESHOLD) value = AIR;
|
|
blocks[y * CHUNK_AREA + column] = value;
|
|
}
|
|
for (let y = height + 1; b.fluid !== undefined && y <= SEA_LEVEL; y++) {
|
|
blocks[y * CHUNK_AREA + column] = b.fluid;
|
|
}
|
|
if (shore || this.#in_zone(x, z, 4)) continue;
|
|
const above = (height + 1) * CHUNK_AREA + column;
|
|
const roll = random.next();
|
|
if (b.light !== undefined && roll < LIGHT_CHANCE) {
|
|
blocks[above] = b.light;
|
|
} else if (b.plants.length > 0 && roll < PLANT_CHANCE) {
|
|
blocks[above] = b.plants[Math.floor(random.next() * b.plants.length)];
|
|
}
|
|
}
|
|
}
|
|
this.#trees(chunk_x, chunk_z, set);
|
|
this.#features(set);
|
|
return { blocks, biomes: new Array(CHUNK_AREA).fill(b.biome) };
|
|
}
|
|
|
|
#in_zone(x: number, z: number, margin: number) {
|
|
return Object.values(ZONES).some((zone) => Math.hypot(x - zone.x, z - zone.z) < zone.radius + margin);
|
|
}
|
|
|
|
// every tree with blocks in this chunk, some grow from the chunks beside it
|
|
#trees(chunk_x: number, chunk_z: number, set: (x: number, y: number, z: number, v: number, air?: boolean) => void) {
|
|
const b = this.#blocks;
|
|
const cells = (chunk: number) => [
|
|
Math.floor((chunk * CHUNK_SIZE - LEAVES_REACH) / TREE_CELL),
|
|
Math.floor((chunk * CHUNK_SIZE + CHUNK_SIZE - 1 + LEAVES_REACH) / TREE_CELL),
|
|
];
|
|
const [first_x, last_x] = cells(chunk_x);
|
|
const [first_z, last_z] = cells(chunk_z);
|
|
for (let cell_z = first_z; cell_z <= last_z; cell_z++) {
|
|
for (let cell_x = first_x; cell_x <= last_x; cell_x++) {
|
|
const random = new Alea(`${SEED}_tree_${cell_x}_${cell_z}`);
|
|
if (random.next() > TREE_CHANCE) continue;
|
|
// away from the cell's edges, so leaves don't run into the next cell's tree
|
|
const x = cell_x * TREE_CELL + LEAVES_REACH +
|
|
Math.floor(random.next() * (TREE_CELL - LEAVES_REACH * 2));
|
|
const z = cell_z * TREE_CELL + LEAVES_REACH +
|
|
Math.floor(random.next() * (TREE_CELL - LEAVES_REACH * 2));
|
|
const trunk = 4 + Math.floor(random.next() * 3);
|
|
const ground = this.height(x, z);
|
|
if (ground <= SEA_LEVEL + 1 || this.#in_zone(x, z, 6)) continue;
|
|
for (let dy = 0; dy < trunk; dy++) set(x, ground + 1 + dy, z, b.log);
|
|
if (b.leaves === undefined) continue;
|
|
for (let dy = trunk - 2; dy <= trunk + 1; dy++) {
|
|
const reach = dy < trunk ? LEAVES_REACH : 1;
|
|
for (let dz = -reach; dz <= reach; dz++) {
|
|
for (let dx = -reach; dx <= reach; dx++) {
|
|
// rounded off at the corners
|
|
if (Math.abs(dx) === reach && Math.abs(dz) === reach) continue;
|
|
set(x + dx, ground + 1 + dy, z + dz, b.leaves, true);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// glass standing in the lake, and the field of visuals
|
|
#features(set: (x: number, y: number, z: number, value: number) => void) {
|
|
const b = this.#blocks;
|
|
const { lake, visuals } = ZONES;
|
|
for (let i = 0; b.glass !== undefined && i < GLASS_PILLARS; i++) {
|
|
const angle = (i / GLASS_PILLARS) * Math.PI * 2;
|
|
const x = lake.x + Math.round(Math.cos(angle) * lake.radius / 2);
|
|
const z = lake.z + Math.round(Math.sin(angle) * lake.radius / 2);
|
|
for (let y = lake.height + 1; y <= SEA_LEVEL + 12; y++) {
|
|
for (let dz = -1; dz <= 1; dz++) {
|
|
for (let dx = -1; dx <= 1; dx++) set(x + dx, y, z + dz, b.glass);
|
|
}
|
|
}
|
|
}
|
|
for (let i = 0; b.visuals.length > 0 && i < VISUALS_SIZE * VISUALS_SIZE; i++) {
|
|
const x = visuals.x - VISUALS_SIZE / 2 + (i % VISUALS_SIZE);
|
|
const z = visuals.z - VISUALS_SIZE / 2 + Math.floor(i / VISUALS_SIZE);
|
|
set(x, visuals.height + 1, z, b.visuals[i % b.visuals.length]);
|
|
}
|
|
}
|
|
}
|