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"; // 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 { 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; } // a schematic with doorway blocks in it, from its lowest corner before it's turned export interface TileType extends TileRules { id: string; size: [number, number, number]; doorways: Doorway[]; } 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; } // 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 { connected: boolean; } export interface InteriorPlan { tiles: PlacedTile[]; doorways: PlannedDoorway[]; } 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 = { [MAIN_TILE]: { weight: 0 }, "dangerous_business:2x1catwalk": { weight: 20 }, }; // 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 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 }; } // 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 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[] = []; // doorways with a tile on one side only, by where they are, with the tile they lead out of const open = new Map(); const used = new Set(); 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 = { x: position.x + x, y: position.y + doorway.y, z: position.z + z, facing: (doorway.facing + quarters) % 4, }; 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: Doorway, from: TileType) => { const pool = new Map(); 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.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 }; } // 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(rng: { next(): number }, weights: Map): 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(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; }