Water phyisics
This commit is contained in:
@@ -116,15 +116,34 @@ Deno.test("q throws one of an item the way the player looks, ctrl throws the who
|
||||
assertEquals(game.item_entities().length, 2);
|
||||
});
|
||||
|
||||
Deno.test("water has no collision, items sink through it to the floor", async () => {
|
||||
Deno.test("items fall into water, float back up and bob at its surface", async () => {
|
||||
const { game, join } = await test_game("mods");
|
||||
join(1, "alice");
|
||||
build_floor(game);
|
||||
for (let y = 100; y <= 102; y++) {
|
||||
game.set_block(6, y, 0, "bworld:water");
|
||||
// a pool 3 deep in a stone basin, so the water stays still
|
||||
for (let x = 3; x <= 9; x++) {
|
||||
for (let z = -3; z <= 3; z++) {
|
||||
for (let y = 99; y <= 103; y++) {
|
||||
const inside = x >= 4 && x <= 8 && z >= -2 && z <= 2 && y >= 100;
|
||||
game.set_block(x, y, z, inside ? (y <= 102 ? "bworld:water" : "bworld:air") : "bworld:stone");
|
||||
}
|
||||
}
|
||||
}
|
||||
const item = game.spawn_item(6.5, 104, 0.5, new ItemStack("bworld:dirt"));
|
||||
for (let i = 0; i < 40; i++) game.tick();
|
||||
assert(item.on_ground);
|
||||
assertEquals(item.y, 100, "on the stone under the water");
|
||||
const item = game.spawn_item(6.5, 104.5, 0.5, new ItemStack("bworld:dirt"));
|
||||
let lowest = Infinity;
|
||||
for (let i = 0; i < 40; i++) {
|
||||
game.tick();
|
||||
lowest = Math.min(lowest, item.y);
|
||||
}
|
||||
assert(lowest < 102, `it went in, down to ${lowest}`);
|
||||
|
||||
for (let i = 0; i < 400; i++) game.tick();
|
||||
const heights: number[] = [];
|
||||
for (let i = 0; i < 200; i++) {
|
||||
game.tick();
|
||||
heights.push(item.y);
|
||||
}
|
||||
// about a tenth of it out of the water at the top, never far under
|
||||
assert(Math.max(...heights) > 102.85 && Math.max(...heights) < 103, `up to ${Math.max(...heights)}`);
|
||||
assert(Math.min(...heights) > 102.4, `down to ${Math.min(...heights)}`);
|
||||
assertEquals([item.x, item.z], [6.5, 0.5]);
|
||||
});
|
||||
|
||||
@@ -4,6 +4,7 @@ import { type BlockRegistry, EverythingRegistry } from "$/common/everything_regi
|
||||
import { get_state_value } from "$/common/utils.ts";
|
||||
import { LIQUID_DEFAULTS } from "$/common/everything_registry.ts";
|
||||
import { LiquidFlow } from "$/server/game/liquids.ts";
|
||||
import { ItemStack } from "$/common/inventory.ts";
|
||||
import { test_game } from "./helpers.ts";
|
||||
|
||||
type Game = Awaited<ReturnType<typeof test_game>>["game"];
|
||||
@@ -152,3 +153,29 @@ Deno.test("water stops at chunks that aren't loaded instead of making them", ()
|
||||
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]);
|
||||
});
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
// 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 = 1;
|
||||
|
||||
// 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]);
|
||||
});
|
||||
@@ -14,6 +14,8 @@ function setup(ceiling = Infinity, world?: { solid: Blocks; water: Blocks }) {
|
||||
level: {
|
||||
collision_shape: world?.solid ?? ((_x: number, y: number) => y < 0 || y >= ceiling),
|
||||
is_liquid: world?.water ?? (() => false),
|
||||
// its water is all still sources
|
||||
liquid_at: (x: number, y: number, z: number) => world?.water(x, y, z) ? { id: 1, height: 1 } : undefined,
|
||||
},
|
||||
screen: undefined,
|
||||
options: new Options(),
|
||||
|
||||
Reference in New Issue
Block a user