Files
bworld/tests/dangerous_business_test.ts
2026-11-22 13:55:30 -03:00

506 lines
21 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 type { Mob } from "@/server/game/entity/mob.ts";
import { copy_dir, errors_during, ListOutput, load_as_player, test_game, test_mob, VOLUMES } from "./helpers.ts";
import { animation_for } from "@/client/rendering/entity_animators.ts";
import { sounds } from "@/client/sounds.ts";
import { set_mods_client } from "@/client/mods.ts";
import type { Client } from "@/client/client.ts";
import type { EntityData } from "@/common/protocol.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 JESTER = "dangerous_business:jester";
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 connecting block that joins solid blocks joins the full side of another block too", async () => {
const { t, hold, state } = await setup();
t.send(1, { type: "chat", text: "/give dangerous_business:iron_bars" });
hold(4);
t.game.set_block(1, 100, 0, FLOOR);
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, "east"), 1);
assertEquals(state(0, 100, 0, "west"), 0);
t.game.set_block(1, 100, 0, "cubi:air");
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) as Mob;
for (let i = 0; i < 100; i++) t.game.tick();
assertAlmostEquals(coilhead.x, 0.5, 1e-6);
assertAlmostEquals(coilhead.z, -2.5, 1e-6);
// what its animator reads
assertEquals(coilhead.client_data.frozen, true);
// 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}`);
assertEquals(coilhead.client_data.frozen, undefined);
});
// the sounds conn was sent since last asked, as [sound, where]: the entity it follows, or the point it's at
const sounds_sent = (t: Awaited<ReturnType<typeof setup>>["t"], conn: number): [string, unknown][] =>
t.take(conn).filter((m) => m.type === "sound").map((m) => [m.sound, m.entity ?? m.at]);
Deno.test("a coilhead springs once when a player's look freezes it", async () => {
const { t } = await setup();
// looking south, away from it
t.send(1, { type: "move", x: 0.5, y: 100, z: 3.5, yaw: Math.PI, pitch: 0 });
const coilhead = spawn_coilhead(t);
for (let i = 0; i < 5; i++) t.game.tick();
assertEquals(sounds_sent(t, 1), []);
t.send(1, { type: "move", x: 0.5, y: 100, z: 3.5, yaw: 0, pitch: 0 });
for (let i = 0; i < 40; i++) t.game.tick();
assertEquals(sounds_sent(t, 1), [
["dangerous_business:coilhead/stop", coilhead.id],
["dangerous_business:coilhead/seen", coilhead.id],
]);
});
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}`);
});
// a wall along z 0 with a closed factory door at x 0, facing so it's across the way. open says whether both its halves
// are open, open_door opens them
function door_in_wall(
t: Awaited<ReturnType<typeof setup>>["t"],
state: (x: number, y: number, z: number, name: string) => number | undefined,
) {
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,
);
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);
}
return {
open: () => state(0, 100, 0, "open") === 1 && state(0, 101, 0, "open") === 1,
open_door: () => [100, 101].forEach((y) => set_state(0, y, 0, "open", 1)),
};
}
Deno.test("a mob's path goes through a closed factory door, and it waits there until the door opens", async () => {
const { t, state } = await setup();
const { open_door } = door_in_wall(t, state);
// 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);
open_door();
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);
});
Deno.test("a coilhead going after a player opens a closed door in its way after standing at it a while", async () => {
const { t, state } = await setup();
const { open } = door_in_wall(t, state);
// alice looks away from the door, so nothing freezes it
t.send(1, { type: "move", x: 0.5, y: 100, z: 3.5, yaw: Math.PI, pitch: 0 });
const coilhead = spawn_coilhead(t) as Mob;
let blocked_at: number | undefined;
let tick = 0;
for (; tick < 300 && !open(); tick++) {
t.game.tick();
if (blocked_at === undefined && coilhead.navigation.blocked_by) blocked_at = tick;
}
assert(open(), "it never opened the door");
assert(blocked_at !== undefined && tick - blocked_at >= 40, `it opened it at ${tick}, blocked from ${blocked_at}`);
});
Deno.test("a coilhead seen while it waits at a closed door stops waiting, and doesn't open it", async () => {
const { t, state } = await setup();
const { open } = door_in_wall(t, state);
// a window 2 wide at head height beside the door, too low to get through
for (const x of [1, 2]) t.game.set_block(x, 101, 0, "cubi:air");
// alice beside the window, looking away
t.send(1, { type: "move", x: 3.5, y: 100, z: 1.5, yaw: -Math.PI / 2, pitch: 0 });
const coilhead = spawn_coilhead(t) as Mob;
for (let i = 0; i < 300 && !coilhead.navigation.blocked_by; i++) t.game.tick();
assert(coilhead.navigation.blocked_by, "it never got to the door");
for (let i = 0; i < 10; i++) t.game.tick();
// she turns to look at it through the window, before it's waited long enough to open the door
const errors = await errors_during(() => {
t.send(1, {
type: "move",
x: 3.5,
y: 100,
z: 1.5,
yaw: Math.atan2(3.5 - coilhead.x, 1.5 - coilhead.z),
pitch: 0,
});
for (let i = 0; i < 60; i++) t.game.tick();
});
assertEquals(errors, []);
assert(!open(), "it opened the door while it was seen");
assertEquals(coilhead.navigation.blocked_by, undefined);
});
Deno.test("a factory door sounds as it opens and closes, for everyone but the player whose click their client heard", async () => {
const { t, state } = await setup();
const { open } = door_in_wall(t, state);
t.join(2, "bob");
t.send(2, { type: "move", x: 1.5, y: 100, z: 2.5, yaw: 0, pitch: 0 });
t.game.tick();
t.take(1);
t.take(2);
const click = () => t.send(1, { type: "use_block", x: 0, y: 100, z: 0, face: "south", hit: [0.5, 0.5, 1] });
click();
assert(open());
assertEquals(sounds_sent(t, 1), []);
assertEquals(sounds_sent(t, 2), [["dangerous_business:door/open", [0.5, 101, 0.5]]]);
click();
assertEquals(sounds_sent(t, 2), [["dangerous_business:door/close", [0.5, 101, 0.5]]]);
});
// a jester 6 blocks north of alice, in a state from its client data like one that was saved
function spawn_jester(t: Awaited<ReturnType<typeof setup>>["t"], state?: number) {
const jester = create_entity(t.game.entities, JESTER)! as Mob;
jester.set_position(0.5, 100, -2.5);
if (state !== undefined) jester.client_data = { state };
t.game.entities.add_entity(jester);
return jester;
}
Deno.test("a jester follows a player it sees without hurting them, then winds its crank standing still", async () => {
const { t } = await setup();
const alice = t.game.players()[0];
const jester = spawn_jester(t);
for (let i = 0; i < 100; i++) t.game.tick();
assert(jester.z > -1, `it stayed at z ${jester.z}, away from her`);
assertEquals(alice.health, alice.max_health);
for (let i = 0; i < 2000 && jester.client_data.state !== 2; i++) t.game.tick();
assertEquals(jester.client_data.state, 2);
const at = [jester.x, jester.z];
t.send(1, { type: "move", x: -2.5, y: 100, z: 3.5, yaw: 0, pitch: 0 });
for (let i = 0; i < 100; i++) t.game.tick();
assertEquals([jester.x, jester.z], at);
});
Deno.test("a popped jester runs down the nearest player faster and faster, and boxes up once they're out", async () => {
const { t } = await setup();
const alice = t.game.players()[0];
const jester = spawn_jester(t, 3);
const speed = () => t.game.mods.mob(jester).attribute("cubi:movement_speed");
const walking = speed();
for (let i = 0; i < 200 && alice.health === alice.max_health; i++) t.game.tick();
assert(alice.health < alice.max_health, "it never got to her");
assert(speed() > walking);
const chasing = speed();
for (let i = 0; i < 300; i++) t.game.tick();
assert(speed() > chasing, "it didn't get any faster");
// up on the surface, out of the facility
alice.set_position(0.5, 160, 3.5);
t.game.tick();
assertEquals(jester.client_data.state, 1);
assertEquals(speed(), walking);
});
Deno.test("a jester saved while it winds goes on from where it got to", async () => {
const { t } = await setup();
const jester = spawn_jester(t, 2);
// 39.5 seconds in
jester.client_data = { state: 2, since: t.game.world.time - 790 };
for (let i = 0; i < 5; i++) t.game.tick();
assertEquals(jester.client_data.state, 2);
for (let i = 0; i < 15; i++) t.game.tick();
assertEquals(jester.client_data.state, 3);
});
Deno.test("a jester's crank clicks once a second while it winds, and it pops once", async () => {
const { t } = await setup();
const jester = spawn_jester(t, 2);
for (let i = 0; i < 100; i++) t.game.tick();
const cranking = sounds_sent(t, 1);
assertEquals(cranking.length, 5);
assertEquals(new Set(cranking.map(([sound]) => sound)), new Set(["dangerous_business:jester/turn_crank"]));
for (let i = 0; i < 1000 && jester.client_data.state !== 3; i++) t.game.tick();
assertEquals(sounds_sent(t, 1).filter(([sound]) => sound !== "dangerous_business:jester/turn_crank"), [
["dangerous_business:jester/pop", jester.id],
]);
});
// last, since it loads the mod the way a player gets it, in place of the server's
Deno.test("a coilhead's head springs back and forth when it freezes, and stays still when it stands", async () => {
const dir = Deno.makeTempDirSync({ prefix: "dangerous_business_client_" });
copy_dir("mods/dangerous_business", `${dir}/dangerous_business`);
await load_as_player(dir);
const coilhead = (frozen: boolean) => ({
id: "e1",
type: COILHEAD,
data: frozen ? { client_data: { frozen: true } } : {},
age: 0,
yaw: 0,
head_yaw: 0,
pitch: 0,
walk_animation: () => ({ position: 0, speed: 0 }),
attack_animation: () => 0,
});
const head = (frozen: boolean, time: number) => animation_for(coilhead(frozen))!.pose(time)!.get("head")?.rotation;
assertNotEquals(head(true, 5), [0, 0, 0]);
assertEquals(head(false, 5), [0, 0, 0]);
// it's over in under a minute
assertEquals(head(true, 1200), head(false, 1200));
Deno.removeSync(dir, { recursive: true });
});
Deno.test("a jester keeps its tendrils in the box until it pops, and turns its crank while it winds", async () => {
const dir = Deno.makeTempDirSync({ prefix: "dangerous_business_client_" });
copy_dir("mods/dangerous_business", `${dir}/dangerous_business`);
await load_as_player(dir);
const pose = (state: number | undefined, time: number) => {
const jester = {
id: "e1",
type: JESTER,
data: state === undefined ? {} : { client_data: { state } },
age: 0,
yaw: 0,
head_yaw: 0,
pitch: 0,
walk_animation: () => ({ position: 0, speed: 0 }),
attack_animation: () => 0,
};
return animation_for(jester)!.pose(time)!;
};
assertEquals(pose(undefined, 10).get("tendrils")!.scale, [0, 0, 0]);
assertNotEquals(pose(3, 100).get("tendrils")!.scale, [0, 0, 0]);
assertNotEquals(pose(2, 5).get("crank")!.rotation, pose(2, 15).get("crank")!.rotation);
Deno.removeSync(dir, { recursive: true });
});
Deno.test("a jester's music box and scream play from as far as it's got, for a player who comes upon it partway", async () => {
const dir = Deno.makeTempDirSync({ prefix: "dangerous_business_client_" });
copy_dir("mods/dangerous_business", `${dir}/dangerous_business`);
await load_as_player(dir);
// the world's time on this client, in ticks
const level = { time: 10_000 };
set_mods_client({ level } as unknown as Client);
const output = new ListOutput();
sounds.set_output(output);
const jester = {
id: "e1",
type: JESTER,
// winding for 30 seconds already
data: { client_data: { state: 2, since: 10_000 - 600 } } as EntityData,
age: 0,
yaw: 0,
head_yaw: 0,
pitch: 0,
walk_animation: () => ({ position: 0, speed: 0 }),
attack_animation: () => 0,
};
const frame = () =>
sounds.frame(
{ x: 0, y: 0, z: 0, yaw: 0, pitch: 0 },
(id) => id === "e1" ? { x: 0, y: 0, z: 3 } : undefined,
VOLUMES,
);
const set_state = (state: number) => {
const before = jester.data;
jester.data = { client_data: { state, since: level.time } };
animation.data_changed(before);
frame();
};
const animation = animation_for(jester)!;
frame();
const [theme] = output.playing;
assertEquals(theme.file.id, "dangerous_business:jester/theme");
assert(theme.settings.loop);
// and a moment of the real clock since it was played
assertAlmostEquals(theme.settings.offset, 30, 0.1);
level.time += 200;
set_state(3);
assert(theme.stopped);
const [scream] = output.playing;
assertEquals(scream.file.id, "dangerous_business:jester/scream");
assert(scream.settings.loop);
assertAlmostEquals(scream.settings.offset, 0, 0.1);
set_state(1);
assertEquals(output.playing, []);
sounds.stop_all();
set_mods_client(undefined as unknown as Client);
Deno.removeSync(dir, { recursive: true });
});
Deno.test("dropped scrap lies around as scrap, picked up by right clicking it into a free hotbar slot", async () => {
const { t } = await setup();
const alice = t.game.players()[0];
const inventory = alice.inventory;
const scrap = t.game.spawn_item(0.5, 100, 2.5, new ItemStack("dangerous_business:air_horn"));
assertEquals(scrap.type, "dangerous_business:scrap");
// with every hotbar slot taken it stays where it is
for (let slot = 0; slot < 9; slot++) inventory.set_item(slot, new ItemStack(FLOOR));
t.send(1, { type: "interact_entity", id: scrap.id });
assert(!scrap.removed);
inventory.set_item(5, undefined);
t.send(1, { type: "interact_entity", id: scrap.id });
assert(scrap.removed);
assertEquals(inventory.get_item(5)?.type_id, "dangerous_business:air_horn");
});