Generation wow
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
// 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, made up here so the
|
||||
// tests don't depend on the mod's own rooms
|
||||
import { assert, assertEquals } from "@std/assert";
|
||||
import { Alea } from "@paulaboks/rng";
|
||||
import {
|
||||
type Doorway,
|
||||
type InteriorPlan,
|
||||
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: Doorway[] = [
|
||||
{ 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, weight: 0 },
|
||||
{ id: "test:hallway", size: [9, 6, 9], doorways: cross, 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 }],
|
||||
weight: 5,
|
||||
},
|
||||
{ id: "test:once", size: [9, 4, 9], doorways: [{ x: 4, y: 1, z: 0, facing: 2 }], 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 doorways 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 doorways = type.doorways.map((doorway) => {
|
||||
const [x, z] = turn(doorway.x, doorway.z);
|
||||
return { x, y: position.y + doorway.y, z, facing: (doorway.facing + rotation / 90) % 4 };
|
||||
});
|
||||
return { tile, min, max, doorways };
|
||||
});
|
||||
}
|
||||
|
||||
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);
|
||||
assertEquals(tiles[0], { 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 tiles = placed(plan(seed));
|
||||
for (let i = 0; i < tiles.length; i++) {
|
||||
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"));
|
||||
});
|
||||
Reference in New Issue
Block a user