Files
bworld/server/game/liquids.ts
T
2026-10-07 20:56:24 -03:00

168 lines
6.7 KiB
TypeScript

// liquids flowing, like minecraft's FlowingFluid: a change next to a liquid gets the cells around it looked at again
// after the liquid's ticks, and each works out what it should be from its neighbors (see next_level). that changes
// more cells, and so it spreads one block a step, and drains away the same way once its source is gone
import { AIR, CHUNK_HEIGHT, ID_MASK } from "$/common/constants.ts";
import type { BlockRegistry } from "$/common/everything_registry.ts";
import { AIR_ID } from "$/common/protocol.ts";
import { get_state_value, liquid_block, set_state_value } from "$/common/utils.ts";
// what the flow needs of the world
export interface LiquidWorld {
// undefined where the chunk isn't loaded, so liquid stops at the edge of the world instead of making new chunks
value_at(x: number, y: number, z: number): number | undefined;
// by numeric id
block(nid: number): BlockRegistry | undefined;
nid(id: string): number;
// changes a block, with its components and neighbors told like any other change
set_block(x: number, y: number, z: number, id: string, state: number): void;
}
// the most cells looked at in one tick, the rest wait for the next
const MAX_PER_TICK = 4096;
const HORIZONTAL = [[1, 0], [-1, 0], [0, 1], [0, -1]];
const NEIGHBORS = [[0, 0, 0], [0, 1, 0], [0, -1, 0], [1, 0, 0], [-1, 0, 0], [0, 0, 1], [0, 0, -1]];
// a liquid in a cell: which, and its level
interface Liquid {
nid: number;
block: BlockRegistry;
level: number;
}
const key = (x: number, y: number, z: number) => `${x},${y},${z}`;
export class LiquidFlow {
#world: LiquidWorld;
// cells to look at, by key, with the tick they're due
#due = new Map<string, { x: number; y: number; z: number; tick: number }>();
constructor(world: LiquidWorld) {
this.#world = world;
}
// how many cells are waiting, for tests
get pending() {
return this.#due.size;
}
// a block changed at x y z: if there's liquid there or beside it, it and its neighbors are looked at again after
// that liquid's ticks
changed(x: number, y: number, z: number, now: number) {
let ticks = Infinity;
for (const [dx, dy, dz] of NEIGHBORS) {
const liquid = this.#liquid(x + dx, y + dy, z + dz);
if (liquid) ticks = Math.min(ticks, liquid.block.liquid!.ticks);
}
if (ticks === Infinity) return;
for (const [dx, dy, dz] of NEIGHBORS) {
const k = key(x + dx, y + dy, z + dz);
const due = this.#due.get(k);
if (due && due.tick <= now + ticks) continue;
this.#due.set(k, { x: x + dx, y: y + dy, z: z + dz, tick: now + ticks });
}
}
// looks at the cells due by now. changing them schedules their neighbors through changed
tick(now: number) {
const ready: { x: number; y: number; z: number }[] = [];
for (const [k, due] of this.#due) {
if (due.tick > now) continue;
this.#due.delete(k);
ready.push(due);
if (ready.length >= MAX_PER_TICK) break;
}
for (const { x, y, z } of ready) this.#update(x, y, z);
}
#update(x: number, y: number, z: number) {
const value = this.#world.value_at(x, y, z);
if (value === undefined) return;
const nid = value & ID_MASK;
const here = this.#liquid(x, y, z);
if (here?.level === 0) return;
// only air and the liquid's own flowing cells are flowed into
if (!here && nid !== AIR) return;
// the liquid that would be here: what's here, or one from above or beside
const candidates = here ? [here] : this.#liquids_around(x, y, z);
for (const liquid of candidates) {
const level = this.next_level(liquid, x, y, z);
if (level === undefined) continue;
if (!here || here.level !== level) this.#set(liquid.block, x, y, z, level);
return;
}
if (here) this.#world.set_block(x, y, z, AIR_ID, 0);
}
// what level the liquid would be at x y z, from the cells around it, or undefined for none there
next_level(liquid: Liquid, x: number, y: number, z: number): number | undefined {
const props = liquid.block.liquid!;
const same = (cx: number, cy: number, cz: number) => {
const other = this.#liquid(cx, cy, cz);
return other?.nid === liquid.nid ? other : undefined;
};
// two sources beside it over something solid or a source make another, like minecraft's infinite water
if (props.renewable) {
const sources = HORIZONTAL.filter(([dx, dz]) => same(x + dx, y, z + dz)?.level === 0).length;
const below = this.#world.value_at(x, y - 1, z);
const below_block = below === undefined ? undefined : this.#world.block(below & ID_MASK);
if (sources >= 2 && (below_block?.has_collision || same(x, y - 1, z)?.level === 0)) return 0;
}
// falling, with more of it above. it's drawn full whatever its level, see liquid_mesh.ts
if (same(x, y + 1, z)) return 1;
let lowest = Infinity;
for (const [dx, dz] of HORIZONTAL) {
const nx = x + dx;
const nz = z + dz;
const neighbor = same(nx, y, nz);
if (!neighbor || !this.#spreads_sideways(neighbor, nx, y, nz)) continue;
// falling liquid spreads like a source once it lands
const level = same(nx, y + 1, nz) ? 0 : neighbor.level;
lowest = Math.min(lowest, level);
}
const level = lowest + 1;
return level <= props.range ? level : undefined;
}
// sources spread sideways, and flowing liquid that can't fall any further. over air or more of itself it falls
// instead, like minecraft's isWaterHole
#spreads_sideways(liquid: Liquid, x: number, y: number, z: number) {
if (liquid.level === 0) return true;
const below = this.#world.value_at(x, y - 1, z);
if (below === undefined) return false;
const nid = below & ID_MASK;
return nid !== AIR && nid !== liquid.nid;
}
// the liquids above and beside x y z that could flow into it, above first
#liquids_around(x: number, y: number, z: number): Liquid[] {
const found: Liquid[] = [];
for (const [dx, dy, dz] of [[0, 1, 0], ...HORIZONTAL.map(([dx, dz]) => [dx, 0, dz])]) {
const liquid = this.#liquid(x + dx, y + dy, z + dz);
if (liquid && !found.some((other) => other.nid === liquid.nid)) found.push(liquid);
}
return found;
}
#liquid(x: number, y: number, z: number): Liquid | undefined {
if (y < 0 || y >= CHUNK_HEIGHT) return undefined;
const value = this.#world.value_at(x, y, z);
if (value === undefined) return undefined;
const nid = value & ID_MASK;
const block = this.#world.block(nid);
if (block?.liquid) return { nid, block, level: get_state_value(value, block, "level")! };
// a waterlogged block is a source of its liquid that never flows away, like minecraft's
const liquid = liquid_block(value, block);
return liquid && { nid: this.#world.nid(liquid.id), block: liquid, level: 0 };
}
#set(block: BlockRegistry, x: number, y: number, z: number, level: number) {
const value = set_state_value(this.#world.nid(block.id), block, "level", level)!;
this.#world.set_block(x, y, z, block.id, value >>> 16);
}
}