feat: Enhance terrain generation with more grass and water variety

This commit is contained in:
Kacper Kostka (aider) 2025-04-05 14:57:01 +02:00
parent 076f21f9ca
commit 7806b39a51

View File

@ -103,55 +103,71 @@ function generateSpecialChunk(chunkData, chunkX, playerChunkX) {
for (let x = 0; x < CHUNK_SIZE; x++) {
const height = heightMap[x];
// Add some noise to the height
const noiseHeight = height + Math.floor(random() * 3) - 1;
// Add more noise to the height
const noiseHeight = height + Math.floor(random() * 5) - 2;
for (let y = floorY - noiseHeight; y < floorY; y++) {
chunkData[y * CHUNK_SIZE + x] = SAND;
}
// 3. Add grass with more coverage and noise
// Add grass on top of the sand with probability based on height
// 3. Add grass with significantly more coverage and noise
// Increase grass probability for more coverage
const grassProbability = (height - baseHeight) / (hill1Height - baseHeight);
if (random() < grassProbability * 0.8 + 0.2) { // Minimum 20% chance, up to 100%
if (random() < grassProbability * 0.7 + 0.3) { // Minimum 30% chance, up to 100%
// Add grass on top
chunkData[(floorY - noiseHeight) * CHUNK_SIZE + x] = GRASS;
// Sometimes add patches of grass on the sides
if (random() < 0.3) {
// More frequently add patches of grass on the sides
if (random() < 0.5) { // Increased from 0.3
// Add grass to the left if possible
if (x > 0 && chunkData[(floorY - noiseHeight) * CHUNK_SIZE + (x-1)] === SAND) {
chunkData[(floorY - noiseHeight) * CHUNK_SIZE + (x-1)] = GRASS;
}
}
if (random() < 0.3) {
if (random() < 0.5) { // Increased from 0.3
// Add grass to the right if possible
if (x < CHUNK_SIZE-1 && chunkData[(floorY - noiseHeight) * CHUNK_SIZE + (x+1)] === SAND) {
chunkData[(floorY - noiseHeight) * CHUNK_SIZE + (x+1)] = GRASS;
}
}
// Sometimes add grass patches below the top
if (random() < 0.15 && noiseHeight > 2) {
const patchDepth = Math.floor(random() * 3) + 1;
// More frequently add grass patches below the top
if (random() < 0.3 && noiseHeight > 2) { // Increased from 0.15
const patchDepth = Math.floor(random() * 4) + 1; // Increased max depth
for (let d = 1; d <= patchDepth; d++) {
if (floorY - noiseHeight + d < floorY) {
chunkData[(floorY - noiseHeight + d) * CHUNK_SIZE + x] = GRASS;
}
}
}
// Sometimes add grass clusters
if (random() < 0.2) {
// Add a small cluster of grass
for (let dy = -1; dy <= 1; dy++) {
for (let dx = -1; dx <= 1; dx++) {
const nx = x + dx;
const ny = floorY - noiseHeight + dy;
if (nx >= 0 && nx < CHUNK_SIZE && ny >= 0 && ny < CHUNK_SIZE &&
chunkData[ny * CHUNK_SIZE + nx] === SAND) {
chunkData[ny * CHUNK_SIZE + nx] = GRASS;
}
}
}
}
}
}
// 2. Add water in the lowest areas
// 2. Add water in more areas with greater depth
for (let x = 0; x < CHUNK_SIZE; x++) {
const height = heightMap[x];
// Add water where the height is close to the minimum
if (height <= minHeight + 2) {
// Add a few layers of water
const waterDepth = 3;
// Add water where the height is close to the minimum (increased threshold)
if (height <= minHeight + 4) { // Increased from +2 to +4
// Add more layers of water
const waterDepth = 5; // Increased from 3 to 5
for (let d = 0; d < waterDepth; d++) {
const y = floorY - height - d - 1;
if (y >= 0) {
@ -159,6 +175,32 @@ function generateSpecialChunk(chunkData, chunkX, playerChunkX) {
}
}
}
// Sometimes add small water pools in random depressions
if (random() < 0.1 && height <= minHeight + 8 && height > minHeight + 4) {
// Add a small pool of water
const poolDepth = Math.floor(random() * 2) + 1;
for (let d = 0; d < poolDepth; d++) {
const y = floorY - height - d - 1;
if (y >= 0) {
chunkData[y * CHUNK_SIZE + x] = WATER;
}
}
}
}
// Add some connected water channels between pools
for (let x = 1; x < CHUNK_SIZE - 1; x++) {
// Check if there's water to the left and right but not at this position
const y = floorY - heightMap[x] - 1;
const leftHasWater = x > 0 && chunkData[y * CHUNK_SIZE + (x-1)] === WATER;
const rightHasWater = x < CHUNK_SIZE-1 && chunkData[y * CHUNK_SIZE + (x+1)] === WATER;
if (leftHasWater && rightHasWater && chunkData[y * CHUNK_SIZE + x] !== WATER) {
if (random() < 0.7) { // 70% chance to connect water bodies
chunkData[y * CHUNK_SIZE + x] = WATER;
}
}
}
// Add some random elements based on the chunk position