Waterlogging

This commit is contained in:
2026-10-07 20:56:24 -03:00
parent 78117f65c9
commit 757ee74bc7
26 changed files with 459 additions and 73 deletions
+1 -1
View File
@@ -2,7 +2,7 @@
import { assert, assertAlmostEquals, assertEquals } from "@std/assert";
import { type Body, liquid_flow, type LiquidInCell, push_by_liquid } from "$/common/physics.ts";
const WATER = 1;
const WATER = "a:water";
// cells by "x,y,z", and solid blocks
function world(liquids: Record<string, number>, solid: string[] = []) {
+46 -1
View File
@@ -169,7 +169,8 @@ async function mesh(blocks: [number, number, number, string, number?][]) {
const worker = new Worker(new URL("../client/workers/chunk_worker.ts", import.meta.url).href, { type: "module" });
worker.postMessage({
type: "init",
blocks_registry: JSON.parse(JSON.stringify(registry)),
// with the registry's gaps, like the client sends it
blocks_registry: registry.map((block) => JSON.parse(JSON.stringify(block))),
models: {},
textures_info: { "engine:missing": { x: 0, y: 0, width: 16, height: 16 } },
image: { width: 1024, height: 1024 },
@@ -266,3 +267,47 @@ Deno.test("with a flowing texture of its own, a sloping top flows downhill and t
assert(plain.find((q) => q.face === 0)!.still);
assert(plain.some((q) => q.face !== 0 && q.uvs.some((n) => n > 8)));
});
Deno.test("a waterloggable block gets a waterlogged state after its own, and can't be a liquid", () => {
const json: BlockJson = {
id: "a:post",
textures: "a:post",
states: [{ name: "lit", bits: 1, default: 0 }],
waterloggable: "a:water",
};
const { block } = block_from_json(json);
assertEquals(block.states!.map((s) => s.name), ["lit", "waterlogged"]);
assertEquals(block_to_json(block, true), json);
assertEquals(validate_block(json), []);
assert(validate_block({ ...json, waterloggable: "water" }).some((p) => p.includes("waterloggable")));
assert(validate_block({ id: "a:w", textures: "a:w", liquid: {}, waterloggable: "a:water" }).length > 0);
assert(
validate_block({ ...json, states: [{ name: "waterlogged", bits: 1, default: 0 }] })
.some((p) => p.includes("of its own")),
);
// only ever placed waterlogged, which needs something to be waterlogged with
const only: BlockJson = { ...json, waterlogged_only: true };
assertEquals(block_to_json(block_from_json(only).block, true), only);
assertEquals(validate_block(only), []);
assert(validate_block({ id: "a:post", textures: "a:post", waterlogged_only: true }).length > 0);
});
Deno.test("the chunk worker draws the water in a waterlogged fence, joined to the water beside it", async () => {
await test_game("mods");
const fence = EverythingRegistry.get<BlockRegistry>("blocks", "bworld:oak_fence")!;
const waterlogged = set_state_value(0, fence, "waterlogged", 1)! >>> 16;
// a fence in water at x 1 with water at x 2, and a dry fence at x 5
const quads = await mesh([
[1, 10, 8, "bworld:oak_fence", waterlogged],
[2, 10, 8, "bworld:water", 0],
[5, 10, 8, "bworld:oak_fence", 0],
]);
const water_in = (x: number) =>
quads.filter((corners) => corners.every(([cx]) => cx >= x - 1e-6 && cx <= x + 1 + 1e-6));
// a full top, facing up and down, and no side between it and the water next to it
const tops = water_in(1).filter((corners) => corners.every(([, y]) => y === 11));
assertEquals(tops.length, 2);
assert(!water_in(1).some((corners) => corners.every(([cx]) => Math.abs(cx - 2) < 1e-6)));
assertEquals(water_in(5).length, 0);
});
+2 -1
View File
@@ -15,7 +15,8 @@ function setup(ceiling = Infinity, world?: { solid: Blocks; water: Blocks }) {
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,
liquid_at: (x: number, y: number, z: number) =>
world?.water(x, y, z) ? { id: "a:water", height: 1 } : undefined,
},
screen: undefined,
options: new Options(),
+100
View File
@@ -0,0 +1,100 @@
// waterlogging, like minecraft's: a fence holds a water source, which flows out of it, can be bucketed in and out, and
// is left behind when the fence is broken
import { assert, assertEquals } from "@std/assert";
import { type BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
import { get_state_value, set_state_value } from "$/common/utils.ts";
import { test_game } from "./helpers.ts";
// a player at 0.5 100 0.5 on stone, holding what they're given
async function setup(items: [string, number][] = []) {
const t = await test_game("mods");
t.join(1, "alice");
for (let x = -6; x <= 6; x++) {
for (let z = -6; z <= 6; z++) t.game.set_block(x, 99, z, "bworld:stone");
}
t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 });
for (const [id, count] of items) t.send(1, { type: "chat", text: `/give ${id} ${count}` });
return t;
}
type Game = Awaited<ReturnType<typeof setup>>["game"];
function waterlogged(game: Game, x: number, y: number, z: number) {
const fence = EverythingRegistry.get<BlockRegistry>("blocks", "bworld:oak_fence")!;
assertEquals(game.world.get_block_id(x, y, z), "bworld:oak_fence");
return get_state_value(game.world.get_block_value(x, y, z), fence, "waterlogged");
}
function ticks(game: Game, count: number) {
for (let i = 0; i < count; i++) game.tick();
}
Deno.test("a fence placed in a water source holds it, one placed in flowing water or air doesn't", async () => {
const t = await setup([["bworld:oak_fence", 3]]);
t.game.set_block(2, 100, 0, "bworld:water");
t.game.set_block(-2, 100, 0, "bworld:water", undefined, 3);
// water is replaceable, so clicking it places into it
t.send(1, { type: "use_block", x: 2, y: 100, z: 0, face: "top", hit: [0.5, 1, 0.5] });
assertEquals(waterlogged(t.game, 2, 100, 0), 1);
t.send(1, { type: "use_block", x: -2, y: 100, z: 0, face: "top", hit: [0.5, 1, 0.5] });
assertEquals(waterlogged(t.game, -2, 100, 0), 0);
t.send(1, { type: "use_block", x: 0, y: 99, z: 2, face: "top", hit: [0.5, 1, 0.5] });
assertEquals(waterlogged(t.game, 0, 100, 2), 0);
});
Deno.test("a water bucket fills a fence and a bucket empties it, the fence stays", async () => {
const t = await setup([["bworld:water_bucket", 1]]);
const held = () => t.game.players()[0].held_item?.type_id;
t.game.set_block(2, 100, 0, "bworld:oak_fence");
t.send(1, { type: "use_block", x: 2, y: 100, z: 0, face: "top", hit: [0.5, 1, 0.5] });
assertEquals(waterlogged(t.game, 2, 100, 0), 1);
assertEquals(held(), "bworld:bucket");
// nothing went on top of it
assertEquals(t.game.world.get_block_id(2, 101, 0), "bworld:air");
t.send(1, { type: "use_block", x: 2, y: 100, z: 0, face: "top", hit: [0.5, 1, 0.5] });
assertEquals(held(), "bworld:water_bucket");
assertEquals(waterlogged(t.game, 2, 100, 0), 0);
});
Deno.test("water flows out of a waterlogged fence, which never drains, and breaking it leaves a source", async () => {
const t = await setup();
t.game.set_block(2, 100, 0, "bworld:oak_fence");
const fence = EverythingRegistry.get<BlockRegistry>("blocks", "bworld:oak_fence")!;
const value = set_state_value(t.game.world.get_block_value(2, 100, 0), fence, "waterlogged", 1)!;
t.game.set_block_state(2, 100, 0, value >>> 16);
assertEquals(waterlogged(t.game, 2, 100, 0), 1);
assert(t.game.entities.is_liquid(2, 100, 0), "entities are in water in it");
ticks(t.game, 30);
assertEquals(t.game.world.get_block_id(3, 100, 0), "bworld:water");
assertEquals(waterlogged(t.game, 2, 100, 0), 1);
t.game.destroy_block(2, 100, 0);
assertEquals(t.game.world.get_block_id(2, 100, 0), "bworld:water");
assertEquals(t.game.world.get_block_value(2, 100, 0) >>> 16, 0);
});
Deno.test("rice is only planted in a water source, holds it, and leaves it behind when broken", async () => {
const t = await setup([["farmersdelight:rice", 1]]);
for (const x of [-2, 2, 4]) t.game.set_block(x, 99, 0, "bworld:dirt");
t.game.set_block(2, 100, 0, "bworld:water");
// not in flowing water, or out of water
t.game.set_block(-2, 100, 0, "bworld:water", undefined, 3);
t.send(1, { type: "use_block", x: -2, y: 100, z: 0, face: "top", hit: [0.5, 1, 0.5] });
assertEquals(t.game.world.get_block_id(-2, 100, 0), "bworld:water");
t.send(1, { type: "use_block", x: 4, y: 99, z: 0, face: "top", hit: [0.5, 1, 0.5] });
assertEquals(t.game.world.get_block_id(4, 100, 0), "bworld:air");
t.send(1, { type: "use_block", x: 2, y: 100, z: 0, face: "top", hit: [0.5, 1, 0.5] });
assertEquals(t.game.world.get_block_id(2, 100, 0), "farmersdelight:rice");
const rice = EverythingRegistry.get<BlockRegistry>("blocks", "farmersdelight:rice")!;
assertEquals(get_state_value(t.game.world.get_block_value(2, 100, 0), rice, "waterlogged"), 1);
assert(t.game.entities.is_liquid(2, 100, 0));
t.game.destroy_block(2, 100, 0);
ticks(t.game, 1);
assertEquals(t.game.world.get_block_id(2, 100, 0), "bworld:water");
assertEquals(t.game.world.get_block_value(2, 100, 0) >>> 16, 0);
});