This commit is contained in:
2026-11-17 23:05:38 -03:00
parent 00a679b053
commit c0c182490e
4 changed files with 100 additions and 32 deletions
+38 -18
View File
@@ -4,10 +4,12 @@ import type { Position, Schematic } from "cubi/server";
// placed, so a plan that comes out too small can be tried again without touching the world
export const DOORWAY = "dangerous_business:doorway";
export const VENT_SPOT = "dangerous_business:vent_spot";
// a doorway, the bottom of a 1 by 2 opening in a tile's outer wall. tiles share their outer walls, so two tiles meeting
// at a doorway have it in the same place. facing points into the tile: 0 north, 1 east, 2 south, 3 west
export interface Doorway {
// 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;
@@ -24,11 +26,15 @@ export interface TileRules {
neighbors?: Record<string, number>;
}
// a schematic with doorway blocks in it, from its lowest corner before it's turned
// a schematic with doorway blocks in it
export interface TileType extends TileRules {
id: string;
size: [number, number, number];
doorways: Doorway[];
// 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[];
// where a vent can go, facing the way the vent would
vent_spots: Marker[];
}
export interface PlacedTile {
@@ -40,13 +46,14 @@ export interface PlacedTile {
// 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 Doorway {
export interface PlannedDoorway extends Marker {
connected: boolean;
}
export interface InteriorPlan {
tiles: PlacedTile[];
doorways: PlannedDoorway[];
vent_spots: Marker[];
}
export interface InteriorPlanConfig {
@@ -69,10 +76,17 @@ export const TILES: Record<string, TileRules> = {
const PLAN_ATTEMPTS = 10;
export function tile_type(schematic: Schematic, rules: TileRules): TileType {
const doorways = schematic.blocks
.filter((block) => schematic.palette[block.i].id === DOORWAY)
.map(({ i, p: [x, y, z] }) => ({ x, y, z, facing: schematic.palette[i].s?.facing ?? 0 }));
return { ...rules, id: schematic.id!, size: schematic.size, doorways };
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 }));
return {
...rules,
id: schematic.id!,
size: schematic.size,
doorways: markers(DOORWAY),
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
@@ -98,22 +112,28 @@ function try_plan(rng: { next(): number }, types: TileType[], config: InteriorPl
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: Doorway; from: TileType }>();
const open = new Map<string, { doorway: Marker; from: TileType }>();
const used = new Set<string>();
const add = (type: TileType, position: Position, quarters: number) => {
tiles.push({ tile: type.id, position, rotation: quarters * 90 as PlacedTile["rotation"] });
boxes.push(tile_box(type, position, quarters));
if (type.one_time) used.add(type.id);
for (const doorway of type.doorways) {
const [x, z] = turned(doorway.x, doorway.z, quarters);
const placed = {
// 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 + doorway.y,
y: position.y + marker.y,
z: position.z + z,
facing: (doorway.facing + quarters) % 4,
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) {
@@ -130,7 +150,7 @@ function try_plan(rng: { next(): number }, types: TileType[], config: InteriorPl
};
// a tile that fits on the other side of the doorway, attached by any of its doorways. false when none does
const attach = (doorway: Doorway, from: TileType) => {
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;
@@ -165,7 +185,7 @@ function try_plan(rng: { next(): number }, types: TileType[], config: InteriorPl
}
}
for (const { doorway } of open.values()) doorways.push({ ...doorway, connected: false });
return { tiles, doorways };
return { tiles, doorways, vent_spots };
}
// clockwise seen from above, like a placed schematic turns, see SchematicPlacement