mirror of
https://github.com/kolbytn/mindcraft.git
synced 2025-09-04 21:33:03 +02:00
properly solved overlapping rooms
This commit is contained in:
parent
87ecbc18ea
commit
ad9ecd72e1
1 changed files with 28 additions and 17 deletions
|
@ -158,7 +158,7 @@ function generateSequentialRooms(m, n, p, rooms) {
|
||||||
|
|
||||||
// Direction probabilities (e.g., 'above': 40%, 'left': 15%, etc.)
|
// Direction probabilities (e.g., 'above': 40%, 'left': 15%, etc.)
|
||||||
const directionChances = [
|
const directionChances = [
|
||||||
{direction: 'above', chance: 0.4},
|
{direction: 'above', chance: 0.15},
|
||||||
{direction: 'left', chance: 0.15},
|
{direction: 'left', chance: 0.15},
|
||||||
{direction: 'right', chance: 0.15},
|
{direction: 'right', chance: 0.15},
|
||||||
{direction: 'forward', chance: 0.15},
|
{direction: 'forward', chance: 0.15},
|
||||||
|
@ -174,10 +174,11 @@ function generateSequentialRooms(m, n, p, rooms) {
|
||||||
cumulative += chance;
|
cumulative += chance;
|
||||||
if (rand <= cumulative) return direction;
|
if (rand <= cumulative) return direction;
|
||||||
}
|
}
|
||||||
return directionChances[0].direction; // Fallback to the first direction
|
return directionChances[1].direction; // Fallback to the first direction
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensures no rooms overlap except at edges
|
// Ensures no rooms overlap except at edges
|
||||||
|
// Ensures no rooms overlap except at edges
|
||||||
function isSpaceValid(newX, newY, newZ, newLength, newWidth, newDepth) {
|
function isSpaceValid(newX, newY, newZ, newLength, newWidth, newDepth) {
|
||||||
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++) {
|
||||||
|
@ -186,10 +187,10 @@ function generateSequentialRooms(m, n, p, rooms) {
|
||||||
const y = newY + dk;
|
const y = newY + dk;
|
||||||
const z = newZ + di;
|
const z = newZ + di;
|
||||||
|
|
||||||
// Skip checking the matrix borders since we want to share them
|
// Skip checking the outermost borders of the new room (these can overlap with stone)
|
||||||
if (x === 0 || x === m - 1 ||
|
if (dj === 0 || dj === newLength - 1 ||
|
||||||
y === 0 || y === n - 1 ||
|
dk === 0 || dk === newWidth - 1 ||
|
||||||
z === 0 || z === p - 1) {
|
di === 0 || di === newDepth - 1) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -227,14 +228,19 @@ function generateSequentialRooms(m, n, p, rooms) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// For non-border spaces, build room walls as normal
|
// For non-border spaces, check if this is a floor that should be shared
|
||||||
if (di === 0 || di === newDepth - 1 ||
|
if (di === 0 && matrix[z-1][x][y] === 'stone') {
|
||||||
|
// Skip creating floor if there's a ceiling below
|
||||||
|
matrix[z][x][y] = 'air';
|
||||||
|
} else if (di === 0 || di === newDepth - 1 ||
|
||||||
dj === 0 || dj === newLength - 1 ||
|
dj === 0 || dj === newLength - 1 ||
|
||||||
dk === 0 || dk === newWidth - 1) {
|
dk === 0 || dk === newWidth - 1) {
|
||||||
matrix[z][x][y] = 'stone';
|
matrix[z][x][y] = 'stone';
|
||||||
} else {
|
} else {
|
||||||
matrix[z][x][y] = 'air';
|
matrix[z][x][y] = 'air';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -242,7 +248,6 @@ function generateSequentialRooms(m, n, p, rooms) {
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
function addDoor(matrix, x, y, z) {
|
function addDoor(matrix, x, y, z) {
|
||||||
matrix[z][x][y] = 'door';
|
matrix[z][x][y] = 'door';
|
||||||
}
|
}
|
||||||
|
@ -257,11 +262,12 @@ function generateSequentialRooms(m, n, p, rooms) {
|
||||||
let roomPlaced = false;
|
let roomPlaced = false;
|
||||||
|
|
||||||
for (let attempt = 0; attempt < 150; attempt++) {
|
for (let attempt = 0; attempt < 150; attempt++) {
|
||||||
const newLength = Math.max(4, Math.floor(Math.random() * 6) + 4);
|
const newLength = Math.max(6, Math.floor(Math.random() * 6) + 4);
|
||||||
const newWidth = Math.max(4, Math.floor(Math.random() * 6) + 4);
|
const newWidth = Math.max(6, Math.floor(Math.random() * 6) + 4);
|
||||||
const newDepth = Math.max(3, Math.floor(Math.random() * 3) + 2);
|
const newDepth = Math.max(5, Math.floor(Math.random() * 5) + 2);
|
||||||
let newX, newY, newZ;
|
let newX, newY, newZ;
|
||||||
|
|
||||||
|
// first room is special
|
||||||
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;
|
||||||
|
@ -278,16 +284,16 @@ function generateSequentialRooms(m, n, p, rooms) {
|
||||||
// Todo: add doors to room on all sides
|
// Todo: add doors to room on all sides
|
||||||
|
|
||||||
break;
|
break;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
const direction = getRandomDirection();
|
const direction = getRandomDirection();
|
||||||
let doorPlaced = false;
|
|
||||||
|
|
||||||
switch (direction) {
|
switch (direction) {
|
||||||
case 'above':
|
case 'above':
|
||||||
// todo: the ceiling / floor are not the same when they should be
|
// todo: the ceiling / floor are not the same when they should be
|
||||||
newX = lastRoom.x;
|
newX = lastRoom.x;
|
||||||
newY = lastRoom.y;
|
newY = lastRoom.y;
|
||||||
newZ = lastRoom.z + lastRoom.depth;
|
newZ = lastRoom.z + lastRoom.depth - 1;
|
||||||
if (validateAndBuildBorder(matrix, newX, newY, newZ, newLength, newWidth, newDepth, m, n, p)) {
|
if (validateAndBuildBorder(matrix, newX, newY, newZ, newLength, newWidth, newDepth, m, n, p)) {
|
||||||
addStairs(matrix, lastRoom.x + Math.floor(lastRoom.length / 2),
|
addStairs(matrix, lastRoom.x + Math.floor(lastRoom.length / 2),
|
||||||
lastRoom.y + Math.floor(lastRoom.width / 2),
|
lastRoom.y + Math.floor(lastRoom.width / 2),
|
||||||
|
@ -357,6 +363,10 @@ function generateSequentialRooms(m, n, p, rooms) {
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (roomPlaced) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -366,6 +376,7 @@ function generateSequentialRooms(m, n, p, rooms) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return matrix
|
return matrix
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -396,7 +407,7 @@ function printMatrix(matrix) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const resultMatrix = generateSequentialRooms(10, 20, 20, 6);
|
const resultMatrix = generateSequentialRooms(20, 20, 20, 6);
|
||||||
printMatrix(resultMatrix)
|
printMatrix(resultMatrix)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue