Generation wow
This commit is contained in:
@@ -0,0 +1,208 @@
|
||||
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<string, number>;
|
||||
}
|
||||
|
||||
// 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<string, TileRules> = {
|
||||
[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<string, { doorway: Doorway; 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 = {
|
||||
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<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.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<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;
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
import type { ClientContext, Color } from "cubi/client";
|
||||
import type { Position, ServerContext } from "cubi/server";
|
||||
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";
|
||||
|
||||
export { shared } from "./shared.ts";
|
||||
|
||||
@@ -83,11 +84,6 @@ function register_door(ctx: ServerContext) {
|
||||
ctx.world.set_state(x, y + 1, z, state, ctx.world.get_state(x, y, z, state) ?? 0);
|
||||
}
|
||||
ctx.world.set_state(x, y + 1, z, "half", 1);
|
||||
// it took its open state from the lower half, but it can be the half that's powered
|
||||
if (ctx.world.get_state(x, y + 1, z, "powered")) {
|
||||
ctx.world.set_state(x, y, z, "open", 1);
|
||||
ctx.world.set_state(x, y + 1, z, "open", 1);
|
||||
}
|
||||
});
|
||||
|
||||
// the halves being taken away, so the other half's on_break doesn't take the first away again
|
||||
@@ -106,18 +102,6 @@ function register_door(ctx: ServerContext) {
|
||||
for (const half of halves) breaking.delete(half);
|
||||
}
|
||||
},
|
||||
on_state_change(block, _params, before) {
|
||||
const { x, y, z } = block;
|
||||
const powered = ctx.world.get_state(x, y, z, "powered");
|
||||
if (powered === before.powered) return;
|
||||
const other = ctx.world.get_state(x, y, z, "half") ? y - 1 : y + 1;
|
||||
const paired = ctx.world.get_block(x, other, z) === block.id;
|
||||
const other_powered = paired && ctx.world.get_state(x, other, z, "powered") === 1;
|
||||
const open = powered === 1 || other_powered;
|
||||
if (open === (before.powered === 1 || other_powered)) return;
|
||||
ctx.world.set_state(x, y, z, "open", open ? 1 : 0);
|
||||
if (paired) ctx.world.set_state(x, other, z, "open", open ? 1 : 0);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -138,18 +122,13 @@ function register_commands(ctx: ServerContext) {
|
||||
.executes((context) => {
|
||||
clear_map(ctx);
|
||||
place_surface(ctx);
|
||||
generate_interior(ctx, { size_multiplier: 1 });
|
||||
context.source.send_message("A new day starts");
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
interface Tile {
|
||||
tile: string;
|
||||
position: Position;
|
||||
rotation: number;
|
||||
}
|
||||
|
||||
let interior_tiles: Tile[] = [];
|
||||
let interior_tiles: PlacedTile[] = [];
|
||||
|
||||
function clear_map(ctx: ServerContext) {
|
||||
// theres no need to clear the surface but we do need to clear each tile
|
||||
@@ -162,17 +141,37 @@ function place_surface(ctx: ServerContext) {
|
||||
);
|
||||
}
|
||||
|
||||
interface InteriorGenerationConfig {
|
||||
size_multipler: number;
|
||||
}
|
||||
// where main's lowest corner goes, under the surface
|
||||
const INTERIOR_ORIGIN = { x: 0, y: 60, z: 0 };
|
||||
// how likely a doorway between two rooms is to get a door, otherwise it's left open
|
||||
const DOOR_CHANCE = 0.8;
|
||||
const INTERIOR_DOOR = "dangerous_business:factory_door";
|
||||
const DOORWAY_WALL = "dangerous_business:sheet_wall";
|
||||
|
||||
function plan_interior(rng: Alea, config: InteriorGenerationConfig): Tile[] {
|
||||
const tiles: Tile[] = [];
|
||||
return tiles;
|
||||
interface InteriorGenerationConfig {
|
||||
size_multiplier: number;
|
||||
}
|
||||
|
||||
function generate_interior(ctx: ServerContext, config: InteriorGenerationConfig) {
|
||||
interior_tiles = plan_interior(new Alea(), config);
|
||||
// generate main
|
||||
// generate other rooms
|
||||
const seed = String(Date.now());
|
||||
ctx.log(`interior seed ${seed}`);
|
||||
const rng = new Alea(seed);
|
||||
const types = Object.entries(TILES).map(([id, rules]) => tile_type(ctx.schematics.get(id)!, rules));
|
||||
const rooms = Math.floor((21 + rng.next() * 9) * config.size_multiplier);
|
||||
const plan = plan_interior(rng, types, { main: MAIN_TILE, origin: INTERIOR_ORIGIN, rooms });
|
||||
for (const { tile, position, rotation } of plan.tiles) {
|
||||
ctx.schematics.place(tile, { position, rotation });
|
||||
}
|
||||
for (const { x, y, z, facing, connected } of plan.doorways) {
|
||||
if (!connected) {
|
||||
ctx.world.set_block(x, y, z, DOORWAY_WALL);
|
||||
ctx.world.set_block(x, y + 1, z, DOORWAY_WALL);
|
||||
} else if (rng.next() < DOOR_CHANCE) {
|
||||
ctx.world.set_block(x, y, z, INTERIOR_DOOR, { facing, half: 0 });
|
||||
ctx.world.set_block(x, y + 1, z, INTERIOR_DOOR, { facing, half: 1 });
|
||||
} else {
|
||||
ctx.world.set_block(x, y, z, "cubi:air");
|
||||
}
|
||||
}
|
||||
interior_tiles = plan.tiles;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
// 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
|
||||
import { assert, assertEquals } from "@std/assert";
|
||||
import { Alea } from "@paulaboks/rng";
|
||||
import {
|
||||
type Doorway,
|
||||
type InteriorPlan,
|
||||
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[] = [
|
||||
{ x: 4, y: 1, z: 0, facing: 2 },
|
||||
{ x: 8, y: 1, z: 4, facing: 3 },
|
||||
{ x: 4, y: 1, z: 8, facing: 0 },
|
||||
{ x: 0, y: 1, z: 4, facing: 1 },
|
||||
];
|
||||
|
||||
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 },
|
||||
// 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 }],
|
||||
weight: 5,
|
||||
},
|
||||
{ id: "test:once", size: [9, 4, 9], doorways: [{ x: 4, y: 1, z: 0, facing: 2 }], 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
|
||||
function placed(plan: InteriorPlan) {
|
||||
return plan.tiles.map(({ tile, position, rotation }) => {
|
||||
const type = TYPES.find((type) => type.id === tile)!;
|
||||
const turn = (x: number, z: number) => {
|
||||
for (let i = 0; i < rotation / 90; i++) [x, z] = [-z, x];
|
||||
return [position.x + x, position.z + z];
|
||||
};
|
||||
const [ax, az] = turn(0, 0);
|
||||
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 };
|
||||
});
|
||||
}
|
||||
|
||||
Deno.test("an interior has as many rooms as it's asked for when there's room, starting from main", () => {
|
||||
const { tiles } = plan("rooms");
|
||||
assertEquals(tiles.length, 30);
|
||||
assertEquals(tiles[0], { tile: "test:main", position: { x: 100, y: 50, z: -20 }, rotation: 0 });
|
||||
assertEquals(tiles.filter((tile) => tile.tile === "test:main").length, 1);
|
||||
});
|
||||
|
||||
Deno.test("rooms only share their outer walls", () => {
|
||||
for (const seed of ["a", "b", "c", "d", "e"]) {
|
||||
const tiles = placed(plan(seed));
|
||||
for (let i = 0; i < tiles.length; i++) {
|
||||
for (let j = i + 1; j < tiles.length; j++) {
|
||||
const [a, b] = [tiles[i], tiles[j]];
|
||||
const inside = [0, 1, 2].every((axis) => a.min[axis] < b.max[axis] && b.min[axis] < a.max[axis]);
|
||||
assert(!inside, `seed ${seed}: ${a.tile} and ${b.tile} overlap`);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("each doorway is in the plan once, connected when two rooms meet there facing each other", () => {
|
||||
for (const seed of ["a", "b", "c"]) {
|
||||
const result = plan(seed);
|
||||
const rooms_at = new Map<string, number[]>();
|
||||
for (const { doorways } of placed(result)) {
|
||||
for (const { x, y, z, facing } of doorways) {
|
||||
const key = `${x},${y},${z}`;
|
||||
rooms_at.set(key, [...rooms_at.get(key) ?? [], facing]);
|
||||
}
|
||||
}
|
||||
assertEquals(result.doorways.length, rooms_at.size);
|
||||
for (const { x, y, z, connected } of result.doorways) {
|
||||
const facings = rooms_at.get(`${x},${y},${z}`)!;
|
||||
const meet = facings.length === 2 && (facings[0] + 2) % 4 === facings[1];
|
||||
assertEquals(connected, meet, `seed ${seed}: doorway at ${x} ${y} ${z}`);
|
||||
}
|
||||
// every room past main came in through a doorway
|
||||
assert(result.doorways.filter((doorway) => doorway.connected).length >= result.tiles.length - 1);
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("a one time room is in an interior once at most", () => {
|
||||
for (const seed of ["a", "b", "c", "d", "e"]) {
|
||||
assert(plan(seed).tiles.filter((tile) => tile.tile === "test:once").length <= 1);
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("the same seed plans the same interior", () => {
|
||||
assertEquals(plan("same"), plan("same"));
|
||||
});
|
||||
Reference in New Issue
Block a user