// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // fluids flowing, like minecraft's FlowingFluid: a change next to a fluid gets the cells around it looked at again // after the fluid'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 { fluid_block, get_state_value, set_state_value } from "$/common/utils.ts"; // what the flow needs of the world export interface FluidWorld { // undefined where the chunk isn't loaded, so fluid 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 fluid in a cell: which, and its level interface Fluid { nid: number; block: BlockRegistry; level: number; } const key = (x: number, y: number, z: number) => `${x},${y},${z}`; export class FluidFlow { #world: FluidWorld; // cells to look at, by key, with the tick they're due #due = new Map(); constructor(world: FluidWorld) { 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 fluid there or beside it, it and its neighbors are looked at again after // that fluid's ticks changed(x: number, y: number, z: number, now: number) { let ticks = Infinity; for (const [dx, dy, dz] of NEIGHBORS) { const fluid = this.#fluid(x + dx, y + dy, z + dz); if (fluid) ticks = Math.min(ticks, fluid.block.fluid!.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.#fluid(x, y, z); if (here?.level === 0) return; // only air and the fluid's own flowing cells are flowed into if (!here && nid !== AIR) return; // the fluid that would be here: what's here, or one from above or beside const candidates = here ? [here] : this.#fluids_around(x, y, z); for (const fluid of candidates) { const level = this.next_level(fluid, x, y, z); if (level === undefined) continue; if (!here || here.level !== level) this.#set(fluid.block, x, y, z, level); return; } if (here) this.#world.set_block(x, y, z, AIR_ID, 0); } // what level the fluid would be at x y z, from the cells around it, or undefined for none there next_level(fluid: Fluid, x: number, y: number, z: number): number | undefined { const props = fluid.block.fluid!; const same = (cx: number, cy: number, cz: number) => { const other = this.#fluid(cx, cy, cz); return other?.nid === fluid.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 fluid_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 fluid 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 fluid that can't fall any further. over air or more of itself it falls // instead, like minecraft's isWaterHole #spreads_sideways(fluid: Fluid, x: number, y: number, z: number) { if (fluid.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 !== fluid.nid; } // the fluids above and beside x y z that could flow into it, above first #fluids_around(x: number, y: number, z: number): Fluid[] { const found: Fluid[] = []; for (const [dx, dy, dz] of [[0, 1, 0], ...HORIZONTAL.map(([dx, dz]) => [dx, 0, dz])]) { const fluid = this.#fluid(x + dx, y + dy, z + dz); if (fluid && !found.some((other) => other.nid === fluid.nid)) found.push(fluid); } return found; } #fluid(x: number, y: number, z: number): Fluid | 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?.fluid) return { nid, block, level: get_state_value(value, block, "level")! }; // a waterlogged block is a source of its fluid that never flows away, like minecraft's const fluid = fluid_block(value, block); return fluid && { nid: this.#world.nid(fluid.id), block: fluid, 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); } }