Generation wow

This commit is contained in:
2026-11-17 22:37:20 -03:00
parent 38716ba8c3
commit 00a679b053
3 changed files with 349 additions and 34 deletions
+33 -34
View File
@@ -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;
}