Files
bworld/tests/dangerous_business_test.ts
T
2026-11-21 14:21:02 -03:00

195 lines
8.4 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. the death block, and the coilhead
import { assert, assertAlmostEquals, assertEquals, assertNotEquals } from "@std/assert";
import { ItemStack } from "@/common/inventory.ts";
import { get_state_value, set_state_value } from "@/common/utils.ts";
import { create_entity } from "@/server/game/entity/entity_types.ts";
import { copy_dir, test_game, test_mob } 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";
const COILHEAD = "dangerous_business:coilhead";
const FACTORY_DOOR = "dangerous_business:factory_door";
// 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);
});
// a coilhead 6 blocks north of alice, right where she's looking
function spawn_coilhead(t: Awaited<ReturnType<typeof setup>>["t"]) {
const coilhead = create_entity(t.game.entities, COILHEAD)!;
coilhead.set_position(0.5, 100, -2.5);
t.game.entities.add_entity(coilhead);
return coilhead;
}
Deno.test("a coilhead can't move while a player looks at it, and comes for them once they look away", async () => {
const { t } = await setup();
const coilhead = spawn_coilhead(t);
for (let i = 0; i < 100; i++) t.game.tick();
assertAlmostEquals(coilhead.x, 0.5, 1e-6);
assertAlmostEquals(coilhead.z, -2.5, 1e-6);
// looking south, with it behind her
t.send(1, { type: "move", x: 0.5, y: 100, z: 3.5, yaw: Math.PI, pitch: 0 });
for (let i = 0; i < 100; i++) t.game.tick();
assert(coilhead.z > 0, `it only got to z ${coilhead.z}`);
});
Deno.test("a player can't stop a coilhead through a wall", async () => {
const { t } = await setup();
for (let x = -3; x <= 3; x++) {
for (let y = 100; y < 103; y++) t.game.set_block(x, y, 0, FLOOR);
}
const coilhead = spawn_coilhead(t);
for (let i = 0; i < 100; i++) t.game.tick();
// up against the wall, still on its way to her
assert(coilhead.z > -1.5, `it only got to z ${coilhead.z}`);
});
Deno.test("a mob's path goes through a closed factory door, and it waits there until the door opens", async () => {
const { t } = await setup();
const world = t.game.world;
const set_state = (x: number, y: number, z: number, name: string, value: number) =>
t.game.set_block_state(
x,
y,
z,
set_state_value(world.get_block_value(x, y, z), world.get_block_info(x, y, z)!, name, value)! >>> 16,
);
// a wall along z 0 with a closed factory door at x 0, facing so it's across the way
for (let x = -3; x <= 3; x++) {
for (let y = 100; y < 103; y++) t.game.set_block(x, y, 0, FLOOR);
}
for (const [y, half] of [[100, 0], [101, 1]]) {
t.game.set_block(0, y, 0, FACTORY_DOOR);
set_state(0, y, 0, "facing", 0);
set_state(0, y, 0, "half", half);
}
// thin enough to get past the door once it's open
const mob = test_mob(t.game, {}, 0.5, 100, -2.5);
assert(mob.navigation.move_to(0.5, 100, 2.5));
for (let i = 0; i < 60; i++) t.game.tick();
assertEquals(mob.navigation.blocked_by, { x: 0, y: 100, z: 0, id: FACTORY_DOOR });
assert(mob.z < 1);
for (const y of [100, 101]) set_state(0, y, 0, "open", 1);
for (let i = 0; i < 200 && mob.navigation.moving; i++) t.game.tick();
assert(!mob.navigation.moving);
assert(mob.z > 1.5, `it only got to z ${mob.z}`);
assertEquals(mob.navigation.blocked_by, undefined);
});