no rooms in rooms and sharing borders is okay

This commit is contained in:
mmaheshwari2 2025-01-28 10:45:46 -08:00
parent b72d3afc00
commit 6fda1675a4

View file

@ -156,7 +156,7 @@ function generateSequentialRooms(m, n, p, rooms) {
let placedRooms = 0; let placedRooms = 0;
let lastRoom = null; let lastRoom = null;
// Direction probabilities (e.g., 'above': 20%, 'left': 20%, etc.) // Direction probabilities (e.g., 'above': 40%, 'left': 15%, etc.)
const directionChances = [ const directionChances = [
{ direction: 'above', chance: 0.4 }, { direction: 'above', chance: 0.4 },
{ direction: 'left', chance: 0.15 }, { direction: 'left', chance: 0.15 },
@ -177,8 +177,27 @@ function generateSequentialRooms(m, n, p, rooms) {
return directionChances[0].direction; // Fallback to the first direction return directionChances[0].direction; // Fallback to the first direction
} }
while (placedRooms < rooms) { // Ensures no rooms in rooms
function isSpaceValid(newX, newY, newZ, newLength, newWidth, newDepth) {
for (let di = 0; di < newDepth; di++) {
for (let dj = 0; dj < newLength; dj++) {
for (let dk = 0; dk < newWidth; dk++) {
// Check if space is already occupied inside the bounds
const x = newX + dj -1;
const y = newY + dk-1;
const z = newZ + di+1;
if (matrix[z][x][y] !== 'air') {
return false;
}
}
}
}
return true;
}
// Places rooms until we can't, or we place all
// attempts random configurations of rooms in random directions.
while (placedRooms < rooms) {
let roomPlaced = false; let roomPlaced = false;
for (let attempt = 0; attempt < 150; attempt++) { for (let attempt = 0; attempt < 150; attempt++) {
@ -187,6 +206,7 @@ function generateSequentialRooms(m, n, p, rooms) {
const newDepth = Math.max(3, Math.floor(Math.random() * 6) + 4); const newDepth = Math.max(3, Math.floor(Math.random() * 6) + 4);
let newX, newY, newZ; let newX, newY, newZ;
// First room has to start on the ground floor
if (placedRooms === 0) { if (placedRooms === 0) {
// First room placement // First room placement
newX = Math.floor(Math.random() * (m - newLength - 1)) + 1; newX = Math.floor(Math.random() * (m - newLength - 1)) + 1;
@ -203,33 +223,35 @@ function generateSequentialRooms(m, n, p, rooms) {
newZ = lastRoom.z + lastRoom.depth ; newZ = lastRoom.z + lastRoom.depth ;
break; break;
case 'left': case 'left':
newX = lastRoom.x - newLength; newX = lastRoom.x - newLength + 1;
newY = lastRoom.y; newY = lastRoom.y;
newZ = lastRoom.z; newZ = lastRoom.z;
break; break;
case 'right': case 'right':
newX = lastRoom.x + lastRoom.length; newX = lastRoom.x + lastRoom.length - 1;
newY = lastRoom.y; newY = lastRoom.y;
newZ = lastRoom.z; newZ = lastRoom.z;
break; break;
case 'forward': case 'forward':
newX = lastRoom.x; newX = lastRoom.x;
newY = lastRoom.y + lastRoom.width; newY = lastRoom.y + lastRoom.width - 1;
newZ = lastRoom.z; newZ = lastRoom.z;
break; break;
case 'backward': case 'backward':
newX = lastRoom.x; newX = lastRoom.x;
newY = lastRoom.y - newWidth; newY = lastRoom.y - newWidth + 1;
newZ = lastRoom.z; newZ = lastRoom.z;
break; break;
} }
} }
// check if valid config, and build room
if (newX > 0 && newX + newLength < m && if (newX > 0 && newX + newLength < m &&
newY > 0 && newY + newWidth < n && newY > 0 && newY + newWidth < n &&
newZ > 0 && newZ + newDepth < p) { newZ > 0 && newZ + newDepth < p &&
isSpaceValid(newX, newY, newZ, newLength, newWidth, newDepth)) {
// Place room and mark spaces (allow overlapping) // Place room and mark spaces (allow shared borders)
for (let di = 0; di < newDepth; di++) { for (let di = 0; di < newDepth; di++) {
for (let dj = 0; dj < newLength; dj++) { for (let dj = 0; dj < newLength; dj++) {
for (let dk = 0; dk < newWidth; dk++) { for (let dk = 0; dk < newWidth; dk++) {
@ -263,6 +285,7 @@ function generateSequentialRooms(m, n, p, rooms) {
} }
/** /**
* todo: Given a matrix, turn it into a blueprint * todo: Given a matrix, turn it into a blueprint
*/ */