152 lines
5.8 KiB
TypeScript
152 lines
5.8 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// planning dangerous business's facility interior: rooms fitted together at their doorways, with spots for vents in
|
|
// them. the rooms are made up here so the tests don't depend on the mod's own
|
|
import { assert, assertEquals } from "@std/assert";
|
|
import { Alea } from "@paulaboks/rng";
|
|
import {
|
|
type InteriorPlan,
|
|
type Marker,
|
|
plan_interior,
|
|
type TileType,
|
|
} from "../mods/dangerous_business/scripts/interior.ts";
|
|
|
|
// a doorway in the middle of each wall of a 9 by 9 room, facing into it
|
|
const cross: Marker[] = [
|
|
{ x: 4, y: 1, z: 0, facing: 2 },
|
|
{ x: 8, y: 1, z: 4, facing: 3 },
|
|
{ x: 4, y: 1, z: 8, facing: 0 },
|
|
{ x: 0, y: 1, z: 4, facing: 1 },
|
|
];
|
|
|
|
const TYPES: TileType[] = [
|
|
{ id: "test:main", size: [9, 6, 9], doorways: cross, vent_spots: [], weight: 0 },
|
|
{ id: "test:hallway", size: [9, 6, 9], doorways: cross, vent_spots: [], weight: 10 },
|
|
// two cells long, with doorways at one end and along one side
|
|
{
|
|
id: "test:long",
|
|
size: [17, 6, 9],
|
|
doorways: [{ x: 0, y: 1, z: 4, facing: 1 }, { x: 12, y: 1, z: 0, facing: 2 }],
|
|
vent_spots: [{ x: 3, y: 5, z: 2, facing: 1 }],
|
|
weight: 5,
|
|
},
|
|
{
|
|
id: "test:once",
|
|
size: [9, 4, 9],
|
|
doorways: [{ x: 4, y: 1, z: 0, facing: 2 }],
|
|
vent_spots: [],
|
|
weight: 5,
|
|
one_time: true,
|
|
},
|
|
];
|
|
|
|
const plan = (seed: string, rooms = 30) =>
|
|
plan_interior(new Alea(seed), TYPES, { main: "test:main", origin: { x: 100, y: 50, z: -20 }, rooms });
|
|
|
|
// where each placed tile is and its markers in the world, turned like a placed schematic
|
|
function placed(plan: InteriorPlan) {
|
|
return plan.tiles.map(({ tile, position, rotation }) => {
|
|
const type = TYPES.find((type) => type.id === tile)!;
|
|
const turn = (x: number, z: number) => {
|
|
for (let i = 0; i < rotation / 90; i++) [x, z] = [-z, x];
|
|
return [position.x + x, position.z + z];
|
|
};
|
|
const [ax, az] = turn(0, 0);
|
|
const [bx, bz] = turn(type.size[0] - 1, type.size[2] - 1);
|
|
const min = [Math.min(ax, bx), position.y, Math.min(az, bz)];
|
|
const max = [Math.max(ax, bx), position.y + type.size[1] - 1, Math.max(az, bz)];
|
|
const in_world = (marker: Marker) => {
|
|
const [x, z] = turn(marker.x, marker.z);
|
|
return { x, y: position.y + marker.y, z, facing: (marker.facing + rotation / 90) % 4 };
|
|
};
|
|
return { tile, min, max, doorways: type.doorways.map(in_world), vent_spots: type.vent_spots.map(in_world) };
|
|
});
|
|
}
|
|
|
|
Deno.test("an interior has as many rooms as it's asked for when there's room, starting from main", () => {
|
|
const { tiles } = plan("rooms");
|
|
assertEquals(tiles.length, 30);
|
|
const { tile, position, rotation } = tiles[0];
|
|
assertEquals({ tile, position, rotation }, { tile: "test:main", position: { x: 100, y: 50, z: -20 }, rotation: 0 });
|
|
assertEquals(tiles.filter((tile) => tile.tile === "test:main").length, 1);
|
|
});
|
|
|
|
Deno.test("rooms only share their outer walls", () => {
|
|
for (const seed of ["a", "b", "c", "d", "e"]) {
|
|
const result = plan(seed);
|
|
const tiles = placed(result);
|
|
for (let i = 0; i < tiles.length; i++) {
|
|
// the box the plan keeps is the one its schematic covers
|
|
assertEquals(result.tiles[i].box, { min: tiles[i].min, max: tiles[i].max });
|
|
for (let j = i + 1; j < tiles.length; j++) {
|
|
const [a, b] = [tiles[i], tiles[j]];
|
|
const inside = [0, 1, 2].every((axis) => a.min[axis] < b.max[axis] && b.min[axis] < a.max[axis]);
|
|
assert(!inside, `seed ${seed}: ${a.tile} and ${b.tile} overlap`);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
Deno.test("each doorway is in the plan once, connected when two rooms meet there facing each other", () => {
|
|
for (const seed of ["a", "b", "c"]) {
|
|
const result = plan(seed);
|
|
const rooms_at = new Map<string, number[]>();
|
|
for (const { doorways } of placed(result)) {
|
|
for (const { x, y, z, facing } of doorways) {
|
|
const key = `${x},${y},${z}`;
|
|
rooms_at.set(key, [...rooms_at.get(key) ?? [], facing]);
|
|
}
|
|
}
|
|
assertEquals(result.doorways.length, rooms_at.size);
|
|
for (const { x, y, z, connected } of result.doorways) {
|
|
const facings = rooms_at.get(`${x},${y},${z}`)!;
|
|
const meet = facings.length === 2 && (facings[0] + 2) % 4 === facings[1];
|
|
assertEquals(connected, meet, `seed ${seed}: doorway at ${x} ${y} ${z}`);
|
|
}
|
|
// every room past main came in through a doorway
|
|
assert(result.doorways.filter((doorway) => doorway.connected).length >= result.tiles.length - 1);
|
|
}
|
|
});
|
|
|
|
Deno.test("a one time room is in an interior once at most", () => {
|
|
for (const seed of ["a", "b", "c", "d", "e"]) {
|
|
assert(plan(seed).tiles.filter((tile) => tile.tile === "test:once").length <= 1);
|
|
}
|
|
});
|
|
|
|
Deno.test("the same seed plans the same interior", () => {
|
|
assertEquals(plan("same"), plan("same"));
|
|
});
|
|
|
|
Deno.test("vent spots are in the plan where their rooms put them, turned with them", () => {
|
|
const result = plan("vents");
|
|
const spots = placed(result).flatMap((tile) => tile.vent_spots);
|
|
assert(spots.length > 0);
|
|
assertEquals(result.vent_spots, spots);
|
|
});
|
|
|
|
Deno.test("a room with entries is only attached to another by them", () => {
|
|
const types: TileType[] = [
|
|
{ id: "test:main", size: [9, 6, 9], doorways: cross, vent_spots: [], weight: 0 },
|
|
{ id: "test:hallway", size: [9, 6, 9], doorways: cross, entries: [cross[0]], vent_spots: [], weight: 1 },
|
|
];
|
|
for (const seed of ["a", "b", "c"]) {
|
|
const result = plan_interior(new Alea(seed), types, {
|
|
main: "test:main",
|
|
origin: { x: 0, y: 0, z: 0 },
|
|
rooms: 20,
|
|
});
|
|
const connected = new Set(result.doorways.filter((d) => d.connected).map(({ x, y, z }) => `${x},${y},${z}`));
|
|
for (const { tile, position, rotation } of result.tiles.slice(1)) {
|
|
// the entry, the north doorway, where it is once the hallway is turned
|
|
let [x, z] = [cross[0].x, cross[0].z];
|
|
for (let i = 0; i < rotation / 90; i++) [x, z] = [-z, x];
|
|
assert(
|
|
connected.has(`${position.x + x},${position.y + cross[0].y},${position.z + z}`),
|
|
`${tile} at ${seed}`,
|
|
);
|
|
}
|
|
}
|
|
});
|