From 076f21f9cababb74966fea7fb382b033883ba01a Mon Sep 17 00:00:00 2001 From: "Kacper Kostka (aider)" Date: Sat, 5 Apr 2025 14:54:17 +0200 Subject: [PATCH] feat: Enhance terrain generation with noisy sand and dynamic grass coverage --- js/world.js | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/js/world.js b/js/world.js index f1debd6..b85154c 100644 --- a/js/world.js +++ b/js/world.js @@ -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; + } + } + } } }