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;
}
+28 -13
View File
@@ -1,19 +1,19 @@
// 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
// planning dangerous business's facility interior: rooms fitted together at their doorways, with spots for vents in
// them. the rooms are made up here so the tests don't depend on the mod's own
import { assert, assertEquals } from "@std/assert";
import { Alea } from "@paulaboks/rng";
import {
type Doorway,
type InteriorPlan,
type Marker,
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[] = [
const cross: Marker[] = [
{ x: 4, y: 1, z: 0, facing: 2 },
{ x: 8, y: 1, z: 4, facing: 3 },
{ x: 4, y: 1, z: 8, facing: 0 },
@@ -21,22 +21,30 @@ const cross: Doorway[] = [
];
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 },
{ id: "test:main", size: [9, 6, 9], doorways: cross, vent_spots: [], weight: 0 },
{ id: "test:hallway", size: [9, 6, 9], doorways: cross, vent_spots: [], 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 }],
vent_spots: [{ x: 3, y: 5, z: 2, facing: 1 }],
weight: 5,
},
{ id: "test:once", size: [9, 4, 9], doorways: [{ x: 4, y: 1, z: 0, facing: 2 }], weight: 5, one_time: true },
{
id: "test:once",
size: [9, 4, 9],
doorways: [{ x: 4, y: 1, z: 0, facing: 2 }],
vent_spots: [],
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
// where each placed tile is and its markers 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)!;
@@ -48,11 +56,11 @@ function placed(plan: InteriorPlan) {
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 };
const in_world = (marker: Marker) => {
const [x, z] = turn(marker.x, marker.z);
return { x, y: position.y + marker.y, z, facing: (marker.facing + rotation / 90) % 4 };
};
return { tile, min, max, doorways: type.doorways.map(in_world), vent_spots: type.vent_spots.map(in_world) };
});
}
@@ -106,3 +114,10 @@ Deno.test("a one time room is in an interior once at most", () => {
Deno.test("the same seed plans the same interior", () => {
assertEquals(plan("same"), plan("same"));
});
Deno.test("vent spots are in the plan where their rooms put them, turned with them", () => {
const result = plan("vents");
const spots = placed(result).flatMap((tile) => tile.vent_spots);
assert(spots.length > 0);
assertEquals(result.vent_spots, spots);
});