Skip to content

Commit bee36be

Browse files
authored
Merge pull request #42 from evillollive/copilot/improve-room-generation
Carve corridors between room perimeters so generated rooms keep 4 walls + a door
2 parents a0857d0 + a4a37cf commit bee36be

1 file changed

Lines changed: 131 additions & 13 deletions

File tree

‎src/utils/generators/roomsCorridors.ts‎

Lines changed: 131 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,113 @@ function carveRoom(grid: TypeGrid, r: Room): void {
4848
}
4949
}
5050

51-
function carveCorridor(
52-
grid: TypeGrid,
51+
/**
52+
* Enumerate the cells of an L-shaped corridor between (ax, ay) and (bx, by).
53+
* `horizontalFirst` controls which leg comes first.
54+
*/
55+
function corridorCells(
5356
ax: number,
5457
ay: number,
5558
bx: number,
5659
by: number,
5760
horizontalFirst: boolean
58-
): void {
61+
): { x: number; y: number }[] {
62+
const out: { x: number; y: number }[] = [];
5963
if (horizontalFirst) {
60-
for (let x = Math.min(ax, bx); x <= Math.max(ax, bx); x++) setCell(grid, x, ay, 'floor');
61-
for (let y = Math.min(ay, by); y <= Math.max(ay, by); y++) setCell(grid, bx, y, 'floor');
64+
for (let x = Math.min(ax, bx); x <= Math.max(ax, bx); x++) out.push({ x, y: ay });
65+
for (let y = Math.min(ay, by); y <= Math.max(ay, by); y++) out.push({ x: bx, y });
6266
} else {
63-
for (let y = Math.min(ay, by); y <= Math.max(ay, by); y++) setCell(grid, ax, y, 'floor');
64-
for (let x = Math.min(ax, bx); x <= Math.max(ax, bx); x++) setCell(grid, x, by, 'floor');
67+
for (let y = Math.min(ay, by); y <= Math.max(ay, by); y++) out.push({ x: ax, y });
68+
for (let x = Math.min(ax, bx); x <= Math.max(ax, bx); x++) out.push({ x, y: by });
69+
}
70+
return out;
71+
}
72+
73+
/**
74+
* Count how many cells of a corridor path land inside the *interior* of any
75+
* room not in `skip`. Used to prefer L-bends that don't slice through
76+
* unrelated rooms (which is what produced the open, half-walled rooms in
77+
* the previous algorithm).
78+
*/
79+
function countRoomCrossings(
80+
cells: { x: number; y: number }[],
81+
rooms: Room[],
82+
skip: Set<number>
83+
): number {
84+
let n = 0;
85+
for (const c of cells) {
86+
for (let i = 0; i < rooms.length; i++) {
87+
if (skip.has(i)) continue;
88+
const r = rooms[i];
89+
if (c.x >= r.x && c.x < r.x + r.w && c.y >= r.y && c.y < r.y + r.h) {
90+
n++;
91+
break;
92+
}
93+
}
6594
}
95+
return n;
96+
}
97+
98+
/**
99+
* Pick a single doorway on `room` facing `target`. Returns:
100+
* - `door`: the wall cell on `room`'s perimeter that should become a door
101+
* tile (lies between the room's interior and the corridor).
102+
* - `exit`: the floor cell one step further out, where the corridor
103+
* actually starts. Carving from `exit` (rather than the room center) is
104+
* what keeps the room's four walls intact.
105+
*
106+
* The side is chosen by the dominant axis between room centers, with the
107+
* other three sides as fallbacks if the preferred exit would fall outside
108+
* the map bounds.
109+
*/
110+
function pickExit(
111+
room: Room,
112+
target: Room,
113+
rng: Rng,
114+
width: number,
115+
height: number
116+
): {
117+
exit: { x: number; y: number };
118+
door: { x: number; y: number; type: 'door-h' | 'door-v' };
119+
} {
120+
const tc = roomCenter(target);
121+
const rc = roomCenter(room);
122+
const dx = tc.x - rc.x;
123+
const dy = tc.y - rc.y;
124+
type Side = 'e' | 'w' | 's' | 'n';
125+
const order: Side[] = Math.abs(dx) >= Math.abs(dy)
126+
? [dx >= 0 ? 'e' : 'w', dy >= 0 ? 's' : 'n', dx >= 0 ? 'w' : 'e', dy >= 0 ? 'n' : 's']
127+
: [dy >= 0 ? 's' : 'n', dx >= 0 ? 'e' : 'w', dy >= 0 ? 'n' : 's', dx >= 0 ? 'w' : 'e'];
128+
129+
for (const side of order) {
130+
let doorX: number, doorY: number, exitX: number, exitY: number;
131+
let type: 'door-h' | 'door-v';
132+
if (side === 'e' || side === 'w') {
133+
// Door sits on the east or west wall; traversal is east-west → door-v.
134+
doorX = side === 'e' ? room.x + room.w : room.x - 1;
135+
exitX = side === 'e' ? doorX + 1 : doorX - 1;
136+
const ey = rng.int(room.y, room.y + room.h - 1);
137+
doorY = ey;
138+
exitY = ey;
139+
type = 'door-v';
140+
} else {
141+
// Door on the north or south wall; traversal is north-south → door-h.
142+
doorY = side === 's' ? room.y + room.h : room.y - 1;
143+
exitY = side === 's' ? doorY + 1 : doorY - 1;
144+
const ex = rng.int(room.x, room.x + room.w - 1);
145+
doorX = ex;
146+
exitX = ex;
147+
type = 'door-h';
148+
}
149+
if (exitX >= 1 && exitX < width - 1 && exitY >= 1 && exitY < height - 1) {
150+
return { exit: { x: exitX, y: exitY }, door: { x: doorX, y: doorY, type } };
151+
}
152+
}
153+
154+
// Fallback (only triggers for rooms wedged against the map border): aim
155+
// straight at the room center so connectivity is at least preserved.
156+
const c = roomCenter(room);
157+
return { exit: c, door: { x: c.x, y: c.y, type: 'door-v' } };
66158
}
67159

68160
/**
@@ -257,16 +349,42 @@ export function generateRoomsCorridors(ctx: GenerateContext): GeneratedMap {
257349

258350
for (const r of rooms) carveRoom(grid, r);
259351

260-
// Connect each room to the previous one with an L-shaped corridor. This
261-
// guarantees the dungeon is fully connected (the connectivity graph is a
262-
// path, so BFS from any room reaches every other room).
352+
// Connect each room to the previous one with an L-shaped corridor. To
353+
// keep rooms looking like real rooms (four intact walls + a door),
354+
// corridors are routed between *perimeter doorway* cells of each room
355+
// rather than between room centers — that way the carve never tears
356+
// through a room's interior. We also try both L-bends and pick the one
357+
// that crosses the fewest other rooms' interiors, so corridors mostly
358+
// run along the outside of rooms instead of straight through them.
359+
// The connectivity graph remains a path (room i ↔ room i+1), so the
360+
// dungeon is still fully connected.
361+
const doorsToPlace: { x: number; y: number; type: 'door-h' | 'door-v' }[] = [];
263362
for (let i = 1; i < rooms.length; i++) {
264-
const a = roomCenter(rooms[i - 1]);
265-
const b = roomCenter(rooms[i]);
266-
carveCorridor(grid, a.x, a.y, b.x, b.y, rng.chance());
363+
const a = pickExit(rooms[i - 1], rooms[i], rng, width, height);
364+
const b = pickExit(rooms[i], rooms[i - 1], rng, width, height);
365+
const skip = new Set([i - 1, i]);
366+
const opt1 = corridorCells(a.exit.x, a.exit.y, b.exit.x, b.exit.y, true);
367+
const opt2 = corridorCells(a.exit.x, a.exit.y, b.exit.x, b.exit.y, false);
368+
const c1 = countRoomCrossings(opt1, rooms, skip);
369+
const c2 = countRoomCrossings(opt2, rooms, skip);
370+
// Tie-break randomly so seeds with no crossings on either bend stay varied.
371+
let cells: { x: number; y: number }[];
372+
if (c1 < c2) cells = opt1;
373+
else if (c2 < c1) cells = opt2;
374+
else cells = rng.chance() ? opt1 : opt2;
375+
for (const c of cells) setCell(grid, c.x, c.y, 'floor');
376+
doorsToPlace.push(a.door, b.door);
267377
}
268378

269379
outlineWalls(grid);
380+
// Stamp the explicit per-room doors first. Each door cell sits in a
381+
// freshly-outlined wall between the room's interior and the corridor
382+
// we just carved, so converting it to a door tile both visualizes the
383+
// entrance and makes the room reachable for the BFS / floodfills below.
384+
// `placeDoors` skips non-`wall` cells, so these stay put.
385+
for (const d of doorsToPlace) {
386+
if (getCell(grid, d.x, d.y) === 'wall') setCell(grid, d.x, d.y, d.type);
387+
}
270388
placeDoors(grid);
271389
// Apply the "Doors" slider: thin doors after they've been geometrically
272390
// placed. Defaults to keeping all of them so legacy seeds reproduce.

0 commit comments

Comments
 (0)