63 lines
3.1 KiB
TypeScript
63 lines
3.1 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// which way liquid flows and how it pushes what's in it, like minecraft's FlowingFluid.getFlow
|
|
import { assert, assertAlmostEquals, assertEquals } from "@std/assert";
|
|
import { type Body, liquid_flow, type LiquidInCell, push_by_liquid } from "$/common/physics.ts";
|
|
|
|
const WATER = "a:water";
|
|
|
|
// cells by "x,y,z", and solid blocks
|
|
function world(liquids: Record<string, number>, solid: string[] = []) {
|
|
const liquid_at = (x: number, y: number, z: number): LiquidInCell | undefined => {
|
|
const height = liquids[`${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 { liquid_at, is_solid };
|
|
}
|
|
|
|
const rounded = (v: number[]) => v.map((n) => Math.round(n * 1000) / 1000 + 0);
|
|
|
|
Deno.test("liquid 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(liquid_flow(0, 0, 0, stream.liquid_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(liquid_flow(0, 0, 0, pool.liquid_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(liquid_flow(0, 0, 0, world({ "0,0,0": 1 }).liquid_at, () => false)), [0, 0, 0]);
|
|
const edge = world({ "0,0,0": 0.5, "0,-1,1": 1 });
|
|
assertEquals(rounded(liquid_flow(0, 0, 0, edge.liquid_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(liquid_flow(0, 0, 0, fall.liquid_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(liquid_flow(0, 0, 0, deep.liquid_at, deep.is_solid)), [0, 0, 0]);
|
|
});
|
|
|
|
Deno.test("a body in flowing liquid 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_liquid(item, stream.liquid_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_liquid(player, half.liquid_at, half.is_solid, true);
|
|
const other = { ...wide };
|
|
push_by_liquid(other, half.liquid_at, half.is_solid, false);
|
|
assert(player.vx > 0 && player.vx < other.vx, `${player.vx} < ${other.vx}`);
|
|
|
|
// out of the liquid, nothing
|
|
const dry = { ...body(), y: 3 };
|
|
push_by_liquid(dry, stream.liquid_at, stream.is_solid, false);
|
|
assertEquals([dry.vx, dry.vy, dry.vz], [0, 0, 0]);
|
|
});
|