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
@@ -0,0 +1,19 @@
{
"format_version": 1,
"block": {
"id": "dangerous_business:vent_spot",
"textures": "cubi:missing",
"model": "dangerous_business:empty",
"item_model": "cubi:cube",
"render_layer": "cutout",
"shape": [[0, 0, 0, 16, 16, 16]],
"collision": false,
"states": [
{
"name": "facing",
"bits": 2,
"default": 0
}
]
}
}
+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
+15 -1
View File
@@ -2,7 +2,7 @@ import type { ClientContext, Color } from "cubi/client";
import type { ServerContext } from "cubi/server";
import { DOOR, FACILITY_DOOR } from "./shared.ts";
import { Alea } from "@paulaboks/rng";
import { MAIN_TILE, type PlacedTile, plan_interior, tile_type, TILES } from "./interior.ts";
import { MAIN_TILE, type Marker, type PlacedTile, plan_interior, tile_type, TILES } from "./interior.ts";
export { shared } from "./shared.ts";
@@ -129,6 +129,8 @@ function register_commands(ctx: ServerContext) {
}
let interior_tiles: PlacedTile[] = [];
// the vents placed in the interior, for monsters to come out of
let interior_vents: Marker[] = [];
function clear_map(ctx: ServerContext) {
// theres no need to clear the surface but we do need to clear each tile
@@ -147,6 +149,9 @@ const INTERIOR_ORIGIN = { x: 0, y: 60, z: 0 };
const DOOR_CHANCE = 0.8;
const INTERIOR_DOOR = "dangerous_business:factory_door";
const DOORWAY_WALL = "dangerous_business:sheet_wall";
// how likely a vent spot is to get a vent, otherwise it's left empty
const VENT_CHANCE = 0.65;
const VENT = "dangerous_business:vent";
interface InteriorGenerationConfig {
size_multiplier: number;
@@ -173,5 +178,14 @@ function generate_interior(ctx: ServerContext, config: InteriorGenerationConfig)
ctx.world.set_block(x, y, z, "cubi:air");
}
}
interior_vents = [];
for (const { x, y, z, facing } of plan.vent_spots) {
if (rng.next() < VENT_CHANCE) {
ctx.world.set_block(x, y, z, VENT, { facing });
interior_vents.push({ x, y, z, facing });
} else {
ctx.world.set_block(x, y, z, "cubi:air");
}
}
interior_tiles = plan.tiles;
}