mirror of
https://github.com/kolbytn/mindcraft.git
synced 2025-08-04 14:25:43 +02:00
basic setup for sequential generation
This commit is contained in:
parent
ff3da96cf6
commit
13bd808eee
1 changed files with 178 additions and 22 deletions
|
@ -1,5 +1,5 @@
|
||||||
|
|
||||||
/** Build the foundation of the house
|
/** Main big funciton: Builds the foundation of the house. breaks down into smaller functions like generating the rooms, generating embellishments, etc...
|
||||||
*
|
*
|
||||||
* @param position coordinate that specify where the house should be built. come in as [x,y,z]
|
* @param position coordinate that specify where the house should be built. come in as [x,y,z]
|
||||||
* @param windows an int that can be 0,1,2 for increasing frequencies of windows
|
* @param windows an int that can be 0,1,2 for increasing frequencies of windows
|
||||||
|
@ -15,8 +15,8 @@ function buildHouse(position, windows, doors){
|
||||||
|
|
||||||
|
|
||||||
// slice up the space ensuring each compartment has at least 4x4x2 space.
|
// slice up the space ensuring each compartment has at least 4x4x2 space.
|
||||||
const resultMatrix = generateAbstractHouse(width, length, height, 3);
|
const resultMatrix = generateAbstractRooms(width, length, height, 3);
|
||||||
console.log(resultMatrix)
|
printMatrix(resultMatrix)
|
||||||
|
|
||||||
|
|
||||||
// todo: then, internally do things like windows / stairs / doors/ etc...
|
// todo: then, internally do things like windows / stairs / doors/ etc...
|
||||||
|
@ -28,10 +28,10 @@ function buildHouse(position, windows, doors){
|
||||||
* @param m - length (x-axis)
|
* @param m - length (x-axis)
|
||||||
* @param n - width (y-axis)
|
* @param n - width (y-axis)
|
||||||
* @param p - depth (z-axis, how many layers)
|
* @param p - depth (z-axis, how many layers)
|
||||||
* @param compartmentCount
|
* @param rooms
|
||||||
* @returns {any[][][]}
|
* @returns {any[][][]}
|
||||||
*/
|
*/
|
||||||
function generateAbstractHouse(m, n, p, compartmentCount = 5) {
|
function generateAbstractRooms(m, n, p, rooms = 5) {
|
||||||
const matrix = Array.from({ length: p }, () =>
|
const matrix = Array.from({ length: p }, () =>
|
||||||
Array.from({ length: m }, () =>
|
Array.from({ length: m }, () =>
|
||||||
Array(n).fill('air')
|
Array(n).fill('air')
|
||||||
|
@ -56,7 +56,7 @@ function generateAbstractHouse(m, n, p, compartmentCount = 5) {
|
||||||
const usedSpaces = new Set();
|
const usedSpaces = new Set();
|
||||||
|
|
||||||
// Loop that places rooms
|
// Loop that places rooms
|
||||||
for (let roomCount = 0; roomCount < compartmentCount; roomCount++) {
|
for (let roomCount = 0; roomCount < rooms; roomCount++) {
|
||||||
const length = Math.max(4, Math.floor(Math.random() * 6) + 4);
|
const length = Math.max(4, Math.floor(Math.random() * 6) + 4);
|
||||||
const width = Math.max(4, Math.floor(Math.random() * 6) + 4);
|
const width = Math.max(4, Math.floor(Math.random() * 6) + 4);
|
||||||
const depth = Math.max(3, Math.floor(Math.random() * 6) + 4);
|
const depth = Math.max(3, Math.floor(Math.random() * 6) + 4);
|
||||||
|
@ -123,16 +123,181 @@ function generateAbstractHouse(m, n, p, compartmentCount = 5) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//todo: new sequetial funciton
|
|
||||||
/**
|
/**
|
||||||
*
|
* Systematically builds the houses by placing them next to the already existing rooms. Still uses randomness.
|
||||||
* @param matrix
|
* @param m Width of the 3D space
|
||||||
|
* @param n Height of the 3D space
|
||||||
|
* @param p Depth of the 3D space
|
||||||
|
* @param rooms Number of rooms to generate
|
||||||
*/
|
*/
|
||||||
function randomSequentialHouse(m,n,p,rooms){
|
function generateSequentialRooms(m, n, p, rooms) {
|
||||||
// place 1 room randomly on the ground floor
|
// build 3d space
|
||||||
//while the total number of rooms has not been reached
|
const matrix = Array.from({ length: p }, () =>
|
||||||
|
Array.from({ length: m }, () =>
|
||||||
|
Array(n).fill('air')
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Mark entire outer border with 'stone'
|
||||||
|
for (let z = 0; z < p; z++) {
|
||||||
|
for (let x = 0; x < m; x++) {
|
||||||
|
for (let y = 0; y < n; y++) {
|
||||||
|
if (
|
||||||
|
z === 0 || z === p - 1 || // Top and bottom faces
|
||||||
|
x === 0 || x === m - 1 || // Front and back faces
|
||||||
|
y === 0 || y === n - 1 // Left and right faces
|
||||||
|
) {
|
||||||
|
matrix[z][x][y] = 'stone';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const usedSpaces = new Set();
|
||||||
|
let placedRooms = 0;
|
||||||
|
let lastRoom = null;
|
||||||
|
|
||||||
|
const placementDirections = ['above', 'left', 'right', 'forward', 'backward'];
|
||||||
|
|
||||||
|
while (placedRooms < rooms) {
|
||||||
|
const newLength = Math.max(4, Math.floor(Math.random() * 6) + 4);
|
||||||
|
const newWidth = Math.max(4, Math.floor(Math.random() * 6) + 4);
|
||||||
|
const newDepth = Math.max(3, Math.floor(Math.random() * 6) + 4);
|
||||||
|
|
||||||
|
let roomPlaced = false;
|
||||||
|
|
||||||
|
for (let attempt = 0; attempt < 150; attempt++) {
|
||||||
|
let newX, newY, newZ;
|
||||||
|
|
||||||
|
if (placedRooms === 0) {
|
||||||
|
newX = Math.floor(Math.random() * (m - newLength - 1)) + 1;
|
||||||
|
newY = Math.floor(Math.random() * (n - newWidth - 1)) + 1;
|
||||||
|
newZ = 1; // Ground floor
|
||||||
|
} else {
|
||||||
|
const direction = placementDirections[Math.floor(Math.random() * placementDirections.length)];
|
||||||
|
|
||||||
|
switch (direction) {
|
||||||
|
case 'above':
|
||||||
|
newX = lastRoom.x;
|
||||||
|
newY = lastRoom.y;
|
||||||
|
newZ = lastRoom.z + lastRoom.depth;
|
||||||
|
break;
|
||||||
|
case 'left':
|
||||||
|
newX = lastRoom.x - newLength;
|
||||||
|
newY = lastRoom.y;
|
||||||
|
newZ = lastRoom.z;
|
||||||
|
break;
|
||||||
|
case 'right':
|
||||||
|
newX = lastRoom.x + lastRoom.length;
|
||||||
|
newY = lastRoom.y;
|
||||||
|
newZ = lastRoom.z;
|
||||||
|
break;
|
||||||
|
case 'forward':
|
||||||
|
newX = lastRoom.x;
|
||||||
|
newY = lastRoom.y + lastRoom.width;
|
||||||
|
newZ = lastRoom.z;
|
||||||
|
break;
|
||||||
|
case 'backward':
|
||||||
|
newX = lastRoom.x;
|
||||||
|
newY = lastRoom.y - newWidth;
|
||||||
|
newZ = lastRoom.z;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (newX > 0 && newX + newLength < m &&
|
||||||
|
newY > 0 && newY + newWidth < n &&
|
||||||
|
newZ > 0 && newZ + newDepth < p) {
|
||||||
|
|
||||||
|
// Check space availability
|
||||||
|
const spaceAvailable = !Array.from({ length: newDepth }).some((_, di) =>
|
||||||
|
Array.from({ length: newLength }).some((_, dj) =>
|
||||||
|
Array.from({ length: newWidth }).some((_, dk) =>
|
||||||
|
usedSpaces.has(`${newX + dj},${newY + dk},${newZ + di}`)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (spaceAvailable) {
|
||||||
|
// Place room and mark spaces
|
||||||
|
for (let di = 0; di < newDepth; di++) {
|
||||||
|
for (let dj = 0; dj < newLength; dj++) {
|
||||||
|
for (let dk = 0; dk < newWidth; dk++) {
|
||||||
|
const spaceKey = `${newX + dj},${newY + dk},${newZ + di}`;
|
||||||
|
usedSpaces.add(spaceKey);
|
||||||
|
|
||||||
|
// Mark only the outer edges of the room
|
||||||
|
if (di === 0 || di === newDepth - 1 ||
|
||||||
|
dj === 0 || dj === newLength - 1 ||
|
||||||
|
dk === 0 || dk === newWidth - 1) {
|
||||||
|
matrix[newZ + di][newX + dj][newY + dk] = 'stone';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lastRoom = { x: newX, y: newY, z: newZ, length: newLength, width: newWidth, depth: newDepth };
|
||||||
|
placedRooms++;
|
||||||
|
roomPlaced = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!roomPlaced) {
|
||||||
|
console.warn(`Could not place room ${placedRooms + 1}`);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`Placed rooms: ${placedRooms}`);
|
||||||
|
return matrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Helper function to place a room in the matrix
|
||||||
|
function placeRoom(matrix, x, y, z, length, width, depth) {
|
||||||
|
for (let dx = x; dx < x + length; dx++) {
|
||||||
|
for (let dy = y; dy < y + width; dy++) {
|
||||||
|
for (let dz = z; dz < z + depth; dz++) {
|
||||||
|
// Mark borders as stone
|
||||||
|
if (dx === x || dx === x + length - 1 ||
|
||||||
|
dy === y || dy === y + width - 1 ||
|
||||||
|
dz === z || dz === z + depth - 1) {
|
||||||
|
matrix[dz][dx][dy] = 'stone';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for room overlap (excluding border)
|
||||||
|
function roomOverlaps(matrix, x, y, z, length, width, depth) {
|
||||||
|
for (let di = 1; di < depth - 1; di++) {
|
||||||
|
for (let dj = 1; dj < length - 1; dj++) {
|
||||||
|
for (let dk = 1; dk < width - 1; dk++) {
|
||||||
|
// Check internal spaces for overlap
|
||||||
|
if (matrix[z + di][x + dj][y + dk] === 'stone') {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fisher-Yates shuffle to randomize placement directions
|
||||||
|
function shuffleArray(array) {
|
||||||
|
for (let i = array.length - 1; i > 0; i--) {
|
||||||
|
const j = Math.floor(Math.random() * (i + 1));
|
||||||
|
[array[i], array[j]] = [array[j], array[i]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* todo: Given a matrix, turn it into a blueprint
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
function printMatrix(matrix) {
|
function printMatrix(matrix) {
|
||||||
|
@ -147,16 +312,7 @@ function printMatrix(matrix) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const resultMatrix = generateAbstractHouse(10, 20, 6, 6);
|
const resultMatrix = generateSequentialRooms(10, 20, 20, 6);
|
||||||
printMatrix(resultMatrix)
|
printMatrix(resultMatrix)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* todo: Given a matrix, turn it into a blueprint
|
|
||||||
*/
|
|
Loading…
Add table
Reference in a new issue