// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // fluids flowing on the server, like minecraft's water: out from a source, down past edges, and draining away import { assert, assertEquals } from "@std/assert"; import { type BlockRegistry, EverythingRegistry } from "@/common/everything_registry.ts"; import { get_state_value } from "@/common/utils.ts"; import { FLUID_DEFAULTS } from "@/common/everything_registry.ts"; import { FluidFlow } from "@/server/game/fluids.ts"; import { ItemStack } from "@/common/inventory.ts"; import { test_game } from "./helpers.ts"; type Game = Awaited>["game"]; // high above the test seed's ground, so only what the test builds is there const Y = 150; // a stone floor from -r to r around 0, 0 at y, with air above it function floor(game: Game, r: number, y = Y - 1) { for (let x = -r; x <= r; x++) { for (let z = -r; z <= r; z++) game.set_block(x, y, z, "bworld:stone"); } } function ticks(game: Game, count: number) { for (let i = 0; i < count; i++) game.tick(); } // the water's level there, undefined for no water function level(game: Game, x: number, y: number, z: number) { if (game.world.get_block_id(x, y, z) !== "bworld:water") return undefined; const water = EverythingRegistry.get("blocks", "bworld:water")!; return get_state_value(game.world.get_block_value(x, y, z), water, "level"); } Deno.test("water flows out from a source one block a step, lower each block, up to its range", async () => { const { game } = await test_game("mods"); floor(game, 10); game.set_block(0, Y, 0, "bworld:water"); // water's 5 ticks a step ticks(game, 5); assertEquals([1, 0, -1].map((x) => level(game, x, Y, 0)), [1, 0, 1]); assertEquals(level(game, 2, Y, 0), undefined); ticks(game, 60); for (let x = 0; x <= 7; x++) assertEquals(level(game, x, Y, 0), x, `at x ${x}`); // past its range of 7 there's none assertEquals(level(game, 8, Y, 0), undefined); // it spreads in a diamond, by how far it walked assertEquals(level(game, 3, Y, 3), 6); // and doesn't flow on its own after that assertEquals(game.fluids.pending, 0); }); // lava's level there, undefined for no lava function lava_level(game: Game, x: number, y: number, z: number) { if (game.world.get_block_id(x, y, z) !== "bworld:lava") return undefined; const lava = EverythingRegistry.get("blocks", "bworld:lava")!; return get_state_value(game.world.get_block_value(x, y, z), lava, "level"); } Deno.test("lava flows 3 blocks from a source, a step every 30 ticks, and two sources don't make another", async () => { const { game } = await test_game("mods"); floor(game, 10); game.set_block(0, Y, 0, "bworld:lava"); ticks(game, 29); assertEquals(lava_level(game, 1, Y, 0), undefined); ticks(game, 1); assertEquals(lava_level(game, 1, Y, 0), 1); ticks(game, 200); for (let x = 0; x <= 3; x++) assertEquals(lava_level(game, x, Y, 0), x, `at x ${x}`); assertEquals(lava_level(game, 4, Y, 0), undefined); // a spot between two sources stays flowing, unlike water game.set_block(0, Y, 0, "bworld:stone"); ticks(game, 200); game.set_block(5, Y, 5, "bworld:lava"); game.set_block(7, Y, 5, "bworld:lava"); ticks(game, 200); assertEquals(lava_level(game, 6, Y, 5), 1); assertEquals(EverythingRegistry.get("blocks", "bworld:lava")!.light_emission, 15); }); Deno.test("water falls off an edge and spreads again where it lands", async () => { const { game } = await test_game("mods"); // a ledge one block wide, and a floor 3 below game.set_block(0, Y - 1, 0, "bworld:stone"); floor(game, 10, Y - 4); game.set_block(0, Y, 0, "bworld:water"); ticks(game, 100); // off the ledge, then down the side as falling water assertEquals(level(game, 1, Y, 0), 1); for (let y = Y - 1; y >= Y - 3; y--) assertEquals(level(game, 1, y, 0), 1, `at y ${y}`); // over air it doesn't spread sideways assertEquals(level(game, 2, Y, 0), undefined); // where it lands it spreads like from a source assertEquals(level(game, 2, Y - 3, 0), 1); assertEquals(level(game, 4, Y - 3, 0), 3); }); Deno.test("taking the source away drains what flowed from it", async () => { const { game } = await test_game("mods"); floor(game, 10); game.set_block(0, Y, 0, "bworld:water"); ticks(game, 60); assertEquals(level(game, 5, Y, 0), 5); game.set_block(0, Y, 0, "cubi:air"); ticks(game, 200); for (let x = -8; x <= 8; x++) { for (let z = -8; z <= 8; z++) assertEquals(level(game, x, Y, z), undefined, `at ${x}, ${z}`); } assertEquals(game.fluids.pending, 0); }); Deno.test("two sources beside a spot on solid ground make it a source, like infinite water", async () => { const { game } = await test_game("mods"); floor(game, 10); // walls around a 3 long trench, open on top for (let x = -2; x <= 2; x++) { for (const z of [-1, 1]) game.set_block(x, Y, z, "bworld:stone"); } game.set_block(-2, Y, 0, "bworld:stone"); game.set_block(2, Y, 0, "bworld:stone"); game.set_block(-1, Y, 0, "bworld:water"); game.set_block(1, Y, 0, "bworld:water"); ticks(game, 20); assertEquals(level(game, 0, Y, 0), 0); // and stays one with the others gone game.set_block(-1, Y, 0, "cubi:air"); game.set_block(1, Y, 0, "cubi:air"); ticks(game, 20); assertEquals(level(game, 0, Y, 0), 0); }); Deno.test("water flows into a hole when a block next to it is broken", async () => { const { game } = await test_game("mods"); floor(game, 10); // a source boxed in by stone for (const [dx, dz] of [[1, 0], [-1, 0], [0, 1], [0, -1]]) game.set_block(dx, Y, dz, "bworld:stone"); game.set_block(0, Y, 0, "bworld:water"); ticks(game, 20); assertEquals(level(game, 2, Y, 0), undefined); game.destroy_block(1, Y, 0); ticks(game, 5); assertEquals(level(game, 1, Y, 0), 1); ticks(game, 5); assertEquals(level(game, 2, Y, 0), 2); }); Deno.test("water stops at chunks that aren't loaded instead of making them", () => { // a world of stone floor at y 0 where x is 0 or less, and nothing loaded past that const water = { id: "a:water", textures: "a:water", has_collision: false, fluid: FLUID_DEFAULTS, } as BlockRegistry; water.states = [{ name: "level", bits: 3, default: 0 }]; const blocks = new Map([["0,1,0", 1]]); const asked: number[] = []; const flow = new FluidFlow({ value_at(x, y, z) { asked.push(x); if (x > 0) return undefined; return y === 0 ? 2 : blocks.get(`${x},${y},${z}`) ?? 0; }, block: (nid) => nid === 1 ? water : nid === 2 ? { id: "a:stone", textures: "a:stone", has_collision: true } : undefined, nid: (id) => id === "a:water" ? 1 : 0, set_block(x, y, z, id, state) { assert(x <= 0, `set a block at x ${x}`); blocks.set(`${x},${y},${z}`, id === "a:water" ? 1 | (state << 16) : 0); flow.changed(x, y, z, 5); }, }); flow.changed(0, 1, 0, 0); flow.tick(5); // it flowed to -x, and not to +x assertEquals(blocks.get("-1,1,0"), 1 | (1 << 16)); assert(!blocks.has("1,1,0")); assert(asked.includes(1)); }); Deno.test("flowing water pushes things in it downstream, still water doesn't", async () => { // entities only move near a player const { game, join } = await test_game("mods"); join(1, "alice"); floor(game, 10); game.set_block(0, Y, 0, "bworld:water"); ticks(game, 60); // in the water flowing towards +x const item = game.spawn_item(3.5, Y, 0.5, new ItemStack("bworld:stone")); item.vx = item.vy = item.vz = 0; ticks(game, 20); assert(item.x > 4, `drifted to ${item.x}`); assert(Math.abs(item.z - 0.5) < 0.01, `and stayed in its row, at z ${item.z}`); // a pool of sources is still const { game: pool, join: join_pool } = await test_game("mods"); join_pool(1, "alice"); floor(pool, 10); for (let x = -2; x <= 2; x++) for (let z = -2; z <= 2; z++) pool.set_block(x, Y, z, "bworld:water"); const floating = pool.spawn_item(0.5, Y, 0.5, new ItemStack("bworld:stone")); floating.vx = floating.vy = floating.vz = 0; ticks(pool, 20); assertEquals([floating.x, floating.z], [0.5, 0.5]); });