Files
bworld/tests/dangerous_business_test.ts
T
2026-11-17 08:20:29 -03:00

126 lines
5.7 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// dangerous business's placement components: slabs and other blocks that go in the top or bottom half, blocks that
// join the same block beside them, blocks that pick some of their looks when placed, and pipes that stand up or lie
// down. and the death block
import { assert, assertEquals, assertNotEquals } from "@std/assert";
import { ItemStack } from "@/common/inventory.ts";
import { get_state_value } from "@/common/utils.ts";
import { copy_dir, test_game } from "./helpers.ts";
const HALF = "dangerous_business:concrete_wall_slab";
const CONNECTS = "dangerous_business:floor_grate";
const RANDOM_LOOK = "dangerous_business:water_puddle";
const FLOOR = "dangerous_business:concrete_floor";
const DEATH = "dangerous_business:death";
const PIPE = "dangerous_business:double_steel_pipes";
// alice on a concrete floor at y 100, south of 0 100 0 and looking north, holding each of the blocks. it's a game of
// its own, which tests playing mods/ leave out
async function setup() {
const dir = Deno.makeTempDirSync({ prefix: "dangerous_business_" });
copy_dir("mods/dangerous_business", `${dir}/dangerous_business`);
const t = await test_game(dir);
t.join(1, "alice");
for (let x = -3; x <= 3; x++) {
for (let z = -3; z <= 3; z++) {
t.game.set_block(x, 99, z, FLOOR);
for (let y = 100; y < 103; y++) t.game.set_block(x, y, z, "cubi:air");
}
}
t.send(1, { type: "move", x: 0.5, y: 100, z: 3.5, yaw: 0, pitch: 0 });
for (const id of [HALF, CONNECTS, RANDOM_LOOK, PIPE]) t.send(1, { type: "chat", text: `/give ${id} 64` });
const hold = (slot: number) => t.send(1, { type: "select_slot", slot });
const state = (x: number, y: number, z: number, name: string) =>
get_state_value(t.game.world.get_block_value(x, y, z), t.game.world.get_block_info(x, y, z)!, name);
return { t, hold, state };
}
Deno.test("a half block goes in the top half against the underside of a block or the upper half of a side", async () => {
const { t, hold, state } = await setup();
hold(0);
t.send(1, { type: "use_block", x: 0, y: 99, z: 0, face: "top", hit: [0.5, 1, 0.5] });
assertEquals(state(0, 100, 0, "half"), 0);
t.game.set_block(0, 102, -1, FLOOR);
t.send(1, { type: "use_block", x: 0, y: 102, z: -1, face: "bottom", hit: [0.5, 0, 0.5] });
assertEquals(state(0, 101, -1, "half"), 1);
t.send(1, { type: "use_block", x: 0, y: 102, z: -1, face: "south", hit: [0.5, 0.8, 1] });
assertEquals(state(0, 102, 0, "half"), 1);
t.send(1, { type: "use_block", x: 0, y: 102, z: -1, face: "west", hit: [0, 0.2, 0.5] });
assertEquals(state(-1, 102, -1, "half"), 0);
});
Deno.test("a pipe stands up on top of a block and lies along the way it was placed against a side", async () => {
const { t, hold, state } = await setup();
hold(3);
t.send(1, { type: "use_block", x: 0, y: 99, z: 0, face: "top", hit: [0.5, 1, 0.5] });
assertEquals(state(0, 100, 0, "horizontal"), 0);
t.game.set_block(0, 101, -2, FLOOR);
t.send(1, { type: "use_block", x: 0, y: 101, z: -2, face: "east", hit: [1, 0.5, 0.5] });
assertEquals(state(1, 101, -2, "horizontal"), 1);
assertEquals(state(1, 101, -2, "facing"), 1);
t.send(1, { type: "use_block", x: 0, y: 101, z: -2, face: "south", hit: [0.5, 0.5, 1] });
assertEquals(state(0, 101, -1, "horizontal"), 1);
assertEquals(state(0, 101, -1, "facing"), 2);
});
Deno.test("a connecting block joins the same block beside it, and parts when it's gone", async () => {
const { t, hold, state } = await setup();
hold(1);
t.send(1, { type: "use_block", x: 0, y: 99, z: 0, face: "top", hit: [0.5, 1, 0.5] });
t.send(1, { type: "use_block", x: 1, y: 99, z: 0, face: "top", hit: [0.5, 1, 0.5] });
// the one placed second joins the first as it's placed, and the first joins it when it's told
assertEquals(state(1, 100, 0, "west"), 1);
assertEquals(state(0, 100, 0, "east"), 1);
assertEquals(state(0, 100, 0, "north"), 0);
t.game.set_block(1, 100, 0, FLOOR);
assertEquals(state(0, 100, 0, "east"), 0);
});
Deno.test("a block with a random look picks the same one wherever it's placed again", async () => {
const { t, hold, state } = await setup();
hold(2);
const looks = () => {
const out = [];
for (let x = -3; x <= 3; x++) {
t.send(1, { type: "use_block", x, y: 99, z: 0, face: "top", hit: [0.5, 1, 0.5] });
out.push(`${state(x, 100, 0, "look")} ${state(x, 100, 0, "facing")}`);
}
return out;
};
const first = looks();
for (let x = -3; x <= 3; x++) t.game.set_block(x, 100, 0, "cubi:air");
assertEquals(looks(), first);
// the puddle has three looks, and seven of them don't all come out the same
for (const look of first) assertEquals(Number(look.split(" ")[0]) < 3, true);
assertEquals(new Set(first).size > 1, true);
});
Deno.test("the death block kills a player who walks into it, but not one just beside it", async () => {
const { t } = await setup();
t.game.set_block(0, 101, 1, DEATH);
// touching its side isn't being in it
t.send(1, { type: "move", x: 0.5, y: 100, z: 2.3, yaw: 0, pitch: 0 });
assert(!t.take(1).some((m) => m.type === "chat" && m.text === "alice died"));
// its head goes into it
t.send(1, { type: "move", x: 0.5, y: 100, z: 1.9, yaw: 0, pitch: 0 });
assert(t.take(1).some((m) => m.type === "chat" && m.text === "alice died"));
const player = t.game.players().find((p) => p.name === "alice")!;
assertEquals(player.health, player.max_health);
assertNotEquals([player.x, player.y, player.z], [0.5, 100, 1.9]);
});
Deno.test("the death block takes away an item falling into it", async () => {
const { t } = await setup();
t.game.set_block(2, 100, 0, DEATH);
const item = t.game.spawn_item(2.5, 101.5, 0.5, new ItemStack(FLOOR));
for (let i = 0; i < 40 && !item.removed; i++) t.game.tick();
assert(item.removed);
});