Files
bworld/tests/waterlog_test.ts
2026-11-04 21:51:26 -03:00

104 lines
4.9 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// 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), "cubi: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_fluid(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([["bworld: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), "cubi: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), "bworld:rice");
const rice = EverythingRegistry.get<BlockRegistry>("blocks", "bworld:rice")!;
assertEquals(get_state_value(t.game.world.get_block_value(2, 100, 0), rice, "waterlogged"), 1);
assert(t.game.entities.is_fluid(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);
});