// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // which way fluid flows and how it pushes what's in it, like minecraft's FlowingFluid.getFlow import { assert, assertAlmostEquals, assertEquals } from "@std/assert"; import { type Body, fluid_flow, type FluidInCell, push_by_fluid } from "@/common/physics.ts"; const WATER = "a:water"; // cells by "x,y,z", and solid blocks function world(fluids: Record, solid: string[] = []) { const fluid_at = (x: number, y: number, z: number): FluidInCell | undefined => { const height = fluids[`${x},${y},${z}`]; return height === undefined ? undefined : { id: WATER, height }; }; const is_solid = (x: number, y: number, z: number) => solid.includes(`${x},${y},${z}`); return { fluid_at, is_solid }; } const rounded = (v: number[]) => v.map((n) => Math.round(n * 1000) / 1000 + 0); Deno.test("fluid flows towards lower cells of it and over edges, not into walls or air beside it", () => { // a source with lower water to +x const stream = world({ "0,0,0": 1, "1,0,0": 0.8 }); assertEquals(rounded(fluid_flow(0, 0, 0, stream.fluid_at, stream.is_solid)), [1, 0, 0]); // level, nothing const pool = world({ "0,0,0": 1, "1,0,0": 1, "-1,0,0": 1 }); assertEquals(rounded(fluid_flow(0, 0, 0, pool.fluid_at, pool.is_solid)), [0, 0, 0]); // air beside it with nothing below doesn't pull, air with more of it below does assertEquals(rounded(fluid_flow(0, 0, 0, world({ "0,0,0": 1 }).fluid_at, () => false)), [0, 0, 0]); const edge = world({ "0,0,0": 0.5, "0,-1,1": 1 }); assertEquals(rounded(fluid_flow(0, 0, 0, edge.fluid_at, edge.is_solid)), [0, 0, 1]); // falling down beside a wall, down const fall = world({ "0,0,0": 0.9, "0,1,0": 1 }, ["1,0,0"]); assertEquals(rounded(fluid_flow(0, 0, 0, fall.fluid_at, fall.is_solid)), [0, -1, 0]); // a source under more water beside a wall is still, like the bottom of a pool const deep = world({ "0,0,0": 1, "0,1,0": 1 }, ["1,0,0"]); assertEquals(rounded(fluid_flow(0, 0, 0, deep.fluid_at, deep.is_solid)), [0, 0, 0]); }); Deno.test("a body in flowing fluid is pushed along it, a player only by the average", () => { const stream = world({ "0,0,0": 1, "1,0,0": 0.8 }); const body = (): Body => ({ x: 0.5, y: 0, z: 0.5, vx: 0, vy: 0, vz: 0, width: 0.25, height: 0.25 }); const item = body(); push_by_fluid(item, stream.fluid_at, stream.is_solid, false); assertAlmostEquals(item.vx, 0.28); assertEquals([item.vy, item.vz], [0, 0]); // half in a still cell, half in the flowing one const wide: Body = { ...body(), x: 0, width: 0.5 }; const half = world({ "-1,0,0": 1, "0,0,0": 1, "1,0,0": 0.8 }); const player = { ...wide }; push_by_fluid(player, half.fluid_at, half.is_solid, true); const other = { ...wide }; push_by_fluid(other, half.fluid_at, half.is_solid, false); assert(player.vx > 0 && player.vx < other.vx, `${player.vx} < ${other.vx}`); // out of the fluid, nothing const dry = { ...body(), y: 3 }; push_by_fluid(dry, stream.fluid_at, stream.is_solid, false); assertEquals([dry.vx, dry.vy, dry.vz], [0, 0, 0]); });