262 lines
9.6 KiB
TypeScript
262 lines
9.6 KiB
TypeScript
import type { Position, Schematic } from "cubi/server";
|
|
|
|
// the facility's interior: rooms (tiles) fitted together at their doorways. it's planned first, only as boxes, then
|
|
// placed, so a plan that comes out too small can be tried again without touching the world
|
|
|
|
export const DOORWAY = "dangerous_business:doorway";
|
|
// where the surface is placed, high above the interior. anyone higher than it is outside the facility
|
|
export const SURFACE_Y = 150;
|
|
export const VENT_SPOT = "dangerous_business:vent_spot";
|
|
|
|
// a marker block in a tile, a doorway or a vent spot, with its facing state: 0 north, 1 east, 2 south, 3 west. in a
|
|
// tile type it's from the schematic's lowest corner before it's turned, in a plan it's in the world, turned with its
|
|
// tile
|
|
export interface Marker {
|
|
x: number;
|
|
y: number;
|
|
z: number;
|
|
facing: number;
|
|
}
|
|
|
|
// how a tile is picked, see TILES
|
|
export interface TileRules {
|
|
// how likely it is to be picked for an open doorway, against the others. 0 for one that's never picked, like main
|
|
weight: number;
|
|
// used at most once in an interior
|
|
one_time?: boolean;
|
|
// extra weight for these tiles, by id, when picking one for this tile's doorways
|
|
neighbors?: Record<string, number>;
|
|
}
|
|
|
|
// a schematic with doorway blocks in it
|
|
export interface TileType extends TileRules {
|
|
id: string;
|
|
size: [number, number, number];
|
|
// each the bottom of a 1 by 2 opening in its outer wall, facing into the tile. tiles share their outer walls, so
|
|
// two tiles meeting at a doorway have it in the same place
|
|
doorways: Marker[];
|
|
// the doorways it can be attached to another tile by, all of them when left out. a tile that starts with a row of
|
|
// structure void, like lethal3's, is only entered there: that row is the wall of the tile it's entered from
|
|
entries?: Marker[];
|
|
// where a vent can go, facing the way the vent would
|
|
vent_spots: Marker[];
|
|
}
|
|
|
|
export function in_facility(position: Position) {
|
|
return position.y <= SURFACE_Y;
|
|
}
|
|
|
|
export interface PlacedTile {
|
|
tile: string;
|
|
// where the schematic's lowest corner goes, before it's turned around it
|
|
position: Position;
|
|
rotation: 0 | 90 | 180 | 270;
|
|
// the blocks it takes up once it's turned, its outer walls too
|
|
box: Box;
|
|
}
|
|
|
|
// a doorway in the world, once. connected when it has a tile on both sides, otherwise it leads nowhere and gets
|
|
// walled off. facing points into one of its tiles
|
|
export interface PlannedDoorway extends Marker {
|
|
connected: boolean;
|
|
}
|
|
|
|
export interface InteriorPlan {
|
|
tiles: PlacedTile[];
|
|
doorways: PlannedDoorway[];
|
|
vent_spots: Marker[];
|
|
}
|
|
|
|
export interface InteriorPlanConfig {
|
|
// the tile it starts from, unturned, with its lowest corner at origin
|
|
main: string;
|
|
origin: Position;
|
|
// how many tiles it tries to have, main too
|
|
rooms: number;
|
|
}
|
|
|
|
export const MAIN_TILE = "dangerous_business:main";
|
|
|
|
// every tile the interior can have
|
|
export const TILES: Record<string, TileRules> = {
|
|
[MAIN_TILE]: { weight: 0 },
|
|
"dangerous_business:2x1catwalk": { weight: 20 },
|
|
...looks("hallway", 25),
|
|
...looks("hallway_t", 15),
|
|
...looks("hallway_lt", 15),
|
|
...looks("hallway_rt", 15),
|
|
...looks("hallway_x", 15),
|
|
...looks("hallway_big_x", 3),
|
|
"dangerous_business:hallway_maze": { weight: 1 },
|
|
};
|
|
|
|
// a tile with three looks, name, name2 and name3, each as likely, together as likely as weight
|
|
function looks(name: string, weight: number): Record<string, TileRules> {
|
|
const rules = { weight: weight / 3 };
|
|
return Object.fromEntries(["", "2", "3"].map((look) => [`dangerous_business:${name}${look}`, rules]));
|
|
}
|
|
|
|
// how many times a plan that comes out with fewer rooms than it should is tried again
|
|
const PLAN_ATTEMPTS = 10;
|
|
|
|
export function tile_type(schematic: Schematic, rules: TileRules): TileType {
|
|
const markers = (id: string) =>
|
|
schematic.blocks
|
|
.filter((block) => schematic.palette[block.i].id === id)
|
|
.map(({ i, p: [x, y, z] }) => ({ x, y, z, facing: schematic.palette[i].s?.facing ?? 0 }));
|
|
const doorways = markers(DOORWAY);
|
|
// a doorway with nothing beside it along the wall is in the row of structure void
|
|
const blocks = new Set(schematic.blocks.map((block) => block.p.join()));
|
|
const entries = doorways.filter(({ x, y, z, facing }) => {
|
|
const [step_x, step_z] = facing % 2 === 0 ? [1, 0] : [0, 1];
|
|
return !blocks.has([x - step_x, y, z - step_z].join()) && !blocks.has([x + step_x, y, z + step_z].join());
|
|
});
|
|
return {
|
|
...rules,
|
|
id: schematic.id!,
|
|
size: schematic.size,
|
|
doorways,
|
|
entries: entries.length ? entries : undefined,
|
|
vent_spots: markers(VENT_SPOT),
|
|
};
|
|
}
|
|
|
|
// the plan with as many rooms as config asks for, or the biggest one it found when none had that many
|
|
export function plan_interior(rng: { next(): number }, types: TileType[], config: InteriorPlanConfig): InteriorPlan {
|
|
let best: InteriorPlan | undefined;
|
|
for (let attempt = 0; attempt < PLAN_ATTEMPTS; attempt++) {
|
|
const plan = try_plan(rng, types, config);
|
|
if (plan.tiles.length >= config.rooms) return plan;
|
|
if (!best || plan.tiles.length > best.tiles.length) best = plan;
|
|
}
|
|
return best!;
|
|
}
|
|
|
|
// the blocks a placed tile takes up, both corners in it
|
|
export interface Box {
|
|
min: [number, number, number];
|
|
max: [number, number, number];
|
|
}
|
|
|
|
function try_plan(rng: { next(): number }, types: TileType[], config: InteriorPlanConfig): InteriorPlan {
|
|
const main = types.find((type) => type.id === config.main);
|
|
if (!main) throw new Error(`there's no tile ${config.main} to start from`);
|
|
const tiles: PlacedTile[] = [];
|
|
const boxes: Box[] = [];
|
|
const doorways: PlannedDoorway[] = [];
|
|
const vent_spots: Marker[] = [];
|
|
// doorways with a tile on one side only, by where they are, with the tile they lead out of
|
|
const open = new Map<string, { doorway: Marker; from: TileType }>();
|
|
const used = new Set<string>();
|
|
|
|
const add = (type: TileType, position: Position, quarters: number) => {
|
|
const box = tile_box(type, position, quarters);
|
|
tiles.push({ tile: type.id, position, rotation: quarters * 90 as PlacedTile["rotation"], box });
|
|
boxes.push(box);
|
|
if (type.one_time) used.add(type.id);
|
|
// a marker of this tile where it is in the world
|
|
const placed_marker = (marker: Marker) => {
|
|
const [x, z] = turned(marker.x, marker.z, quarters);
|
|
return {
|
|
x: position.x + x,
|
|
y: position.y + marker.y,
|
|
z: position.z + z,
|
|
facing: (marker.facing + quarters) % 4,
|
|
};
|
|
};
|
|
vent_spots.push(...type.vent_spots.map(placed_marker));
|
|
for (const doorway of type.doorways) {
|
|
const placed = placed_marker(doorway);
|
|
const key = `${placed.x},${placed.y},${placed.z}`;
|
|
const other = open.get(key);
|
|
if (!other) {
|
|
open.set(key, { doorway: placed, from: type });
|
|
} else if (other.doorway.facing === (placed.facing + 2) % 4) {
|
|
open.delete(key);
|
|
doorways.push({ ...other.doorway, connected: true });
|
|
} else {
|
|
// two tiles with a doorway in the same place that don't face each other, they can't lead anywhere
|
|
open.delete(key);
|
|
doorways.push({ ...other.doorway, connected: false });
|
|
}
|
|
}
|
|
};
|
|
|
|
// a tile that fits on the other side of the doorway, attached by any of its doorways. false when none does
|
|
const attach = (doorway: Marker, from: TileType) => {
|
|
const pool = new Map<TileType, number>();
|
|
for (const type of types) {
|
|
if (type.id === config.main || used.has(type.id)) continue;
|
|
const weight = type.weight + (from.neighbors?.[type.id] ?? 0);
|
|
if (weight > 0) pool.set(type, weight);
|
|
}
|
|
while (pool.size > 0) {
|
|
const type = pick(rng, pool);
|
|
pool.delete(type);
|
|
for (const own of shuffled(rng, type.entries ?? type.doorways)) {
|
|
// turned to face away from the tile it's attached to
|
|
const quarters = (doorway.facing + 2 - own.facing + 4) % 4;
|
|
const [x, z] = turned(own.x, own.z, quarters);
|
|
const position = { x: doorway.x - x, y: doorway.y - own.y, z: doorway.z - z };
|
|
const box = tile_box(type, position, quarters);
|
|
if (boxes.some((other) => overlaps(box, other))) continue;
|
|
add(type, position, quarters);
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
};
|
|
|
|
add(main, config.origin, 0);
|
|
while (tiles.length < config.rooms && open.size > 0) {
|
|
const keys = [...open.keys()];
|
|
const key = keys[Math.floor(rng.next() * keys.length)];
|
|
const { doorway, from } = open.get(key)!;
|
|
if (!attach(doorway, from)) {
|
|
open.delete(key);
|
|
doorways.push({ ...doorway, connected: false });
|
|
}
|
|
}
|
|
for (const { doorway } of open.values()) doorways.push({ ...doorway, connected: false });
|
|
return { tiles, doorways, vent_spots };
|
|
}
|
|
|
|
// clockwise seen from above, like a placed schematic turns, see SchematicPlacement
|
|
function turned(x: number, z: number, quarters: number): [number, number] {
|
|
for (let i = 0; i < quarters; i++) [x, z] = [-z, x];
|
|
return [x, z];
|
|
}
|
|
|
|
function tile_box(type: TileType, position: Position, quarters: number): Box {
|
|
const [x, z] = turned(type.size[0] - 1, type.size[2] - 1, quarters);
|
|
return {
|
|
min: [position.x + Math.min(0, x), position.y, position.z + Math.min(0, z)],
|
|
max: [position.x + Math.max(0, x), position.y + type.size[1] - 1, position.z + Math.max(0, z)],
|
|
};
|
|
}
|
|
|
|
// tiles share their outer walls, so two of them only overlap when they share more than one layer of blocks
|
|
function overlaps(a: Box, b: Box): boolean {
|
|
return [0, 1, 2].every((axis) => a.min[axis] < b.max[axis] && b.min[axis] < a.max[axis]);
|
|
}
|
|
|
|
function pick<T>(rng: { next(): number }, weights: Map<T, number>): T {
|
|
let total = 0;
|
|
for (const weight of weights.values()) total += weight;
|
|
let roll = rng.next() * total;
|
|
for (const [value, weight] of weights) {
|
|
roll -= weight;
|
|
if (roll < 0) return value;
|
|
}
|
|
return [...weights.keys()].at(-1)!;
|
|
}
|
|
|
|
function shuffled<T>(rng: { next(): number }, values: T[]): T[] {
|
|
const copy = [...values];
|
|
for (let i = copy.length - 1; i > 0; i--) {
|
|
const j = Math.floor(rng.next() * (i + 1));
|
|
[copy[i], copy[j]] = [copy[j], copy[i]];
|
|
}
|
|
return copy;
|
|
}
|