// liquids 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 { LIQUID_DEFAULTS } from "$/common/everything_registry.ts"; import { LiquidFlow } from "$/server/game/liquids.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.liquids.pending, 0); }); 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, "bworld: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.liquids.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, "bworld:air"); game.set_block(1, Y, 0, "bworld: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, liquid: LIQUID_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 LiquidFlow({ 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)); });