feat: Enhance terrain generation with noisy sand and dynamic grass coverage

This commit is contained in:
Kacper Kostka (aider) 2025-04-05 14:54:17 +02:00
parent a31f401378
commit 076f21f9ca

View File

@ -99,16 +99,49 @@ function generateSpecialChunk(chunkData, chunkX, playerChunkX) {
// Find the lowest points for water
let minHeight = Math.min(...heightMap);
// Place sand according to the height map
// Place sand according to the height map with noise
for (let x = 0; x < CHUNK_SIZE; x++) {
const height = heightMap[x];
for (let y = floorY - height; y < floorY; y++) {
// Add some noise to the height
const noiseHeight = height + Math.floor(random() * 3) - 1;
for (let y = floorY - noiseHeight; y < floorY; y++) {
chunkData[y * CHUNK_SIZE + x] = SAND;
}
// 3. Add grass on top of the hills
if (height > baseHeight + 3) { // Only on higher parts
chunkData[(floorY - height) * CHUNK_SIZE + x] = GRASS;
// 3. Add grass with more coverage and noise
// Add grass on top of the sand with probability based on height
const grassProbability = (height - baseHeight) / (hill1Height - baseHeight);
if (random() < grassProbability * 0.8 + 0.2) { // Minimum 20% 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) {
// 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) {
// 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;
for (let d = 1; d <= patchDepth; d++) {
if (floorY - noiseHeight + d < floorY) {
chunkData[(floorY - noiseHeight + d) * CHUNK_SIZE + x] = GRASS;
}
}
}
}
}