From a31f401378481f903d99bd390bcc471da32cc671 Mon Sep 17 00:00:00 2001 From: "Kacper Kostka (aider)" Date: Sat, 5 Apr 2025 14:53:15 +0200 Subject: [PATCH] feat: Enhance terrain generation with dynamic chunk spreading and seeded randomness --- js/world.js | 62 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 9 deletions(-) diff --git a/js/world.js b/js/world.js index 9ae476f..f1debd6 100644 --- a/js/world.js +++ b/js/world.js @@ -41,9 +41,12 @@ function getOrCreateChunk(chunkX, chunkY) { } } - // Special generation for the first chunk (0,0) - if (chunkX === 0 && chunkY === 0) { - generateFirstChunk(chunkData); + // Get the current player chunk position + const playerChunkX = Math.floor(worldOffsetX / CHUNK_SIZE); + + // Special generation for chunks within 3 chunks of the player's position + if (chunkY === 0 && Math.abs(chunkX - playerChunkX) <= 3) { + generateSpecialChunk(chunkData, chunkX, playerChunkX); } chunks.set(key, chunkData); @@ -52,17 +55,21 @@ function getOrCreateChunk(chunkX, chunkY) { return chunks.get(key); } -// Generate special terrain for the first chunk -function generateFirstChunk(chunkData) { +// Generate special terrain for chunks near the player +function generateSpecialChunk(chunkData, chunkX, playerChunkX) { // 1. Create a base layer of sand above the floor const floorY = CHUNK_SIZE - 1; const baseHeight = 10; // Base height of sand + // Use the chunk position as part of the seed for consistent generation + const seed = chunkX * 10000; + const random = createSeededRandom(seed); + // Create two random hill points - const hill1X = Math.floor(CHUNK_SIZE * 0.3); - const hill2X = Math.floor(CHUNK_SIZE * 0.7); - const hill1Height = baseHeight + Math.floor(Math.random() * 10) + 5; // 5-15 blocks higher - const hill2Height = baseHeight + Math.floor(Math.random() * 10) + 5; + const hill1X = Math.floor(CHUNK_SIZE * (0.2 + random() * 0.2)); + const hill2X = Math.floor(CHUNK_SIZE * (0.6 + random() * 0.2)); + const hill1Height = baseHeight + Math.floor(random() * 10) + 5; // 5-15 blocks higher + const hill2Height = baseHeight + Math.floor(random() * 10) + 5; // Generate height map for sand const heightMap = new Array(CHUNK_SIZE).fill(0); @@ -79,6 +86,14 @@ function generateFirstChunk(chunkData) { // Take the maximum height contribution heightMap[x] = Math.floor(baseHeight + Math.max(h1, h2)); + + // Add some variation based on distance from player's chunk + const distanceFromPlayer = Math.abs(chunkX - playerChunkX); + if (distanceFromPlayer > 0) { + // Make terrain more extreme as we move away from player + const factor = 1 + (distanceFromPlayer * 0.2); + heightMap[x] = Math.floor(heightMap[x] * factor); + } } // Find the lowest points for water @@ -112,6 +127,35 @@ function generateFirstChunk(chunkData) { } } } + + // Add some random elements based on the chunk position + if (random() < 0.3) { + // Add a small tree or plant cluster + const plantX = Math.floor(random() * CHUNK_SIZE); + const plantY = floorY - heightMap[plantX] - 1; + + if (plantY > 0 && chunkData[plantY * CHUNK_SIZE + plantX] === GRASS) { + // Add a small tree + for (let i = 0; i < 3; i++) { + if (plantY - i > 0) { + chunkData[(plantY - i) * CHUNK_SIZE + plantX] = WOOD; + } + } + + // Add some leaves + for (let dy = -2; dy <= 0; dy++) { + for (let dx = -2; dx <= 2; dx++) { + const leafX = plantX + dx; + const leafY = plantY - 3 + dy; + + if (leafX >= 0 && leafX < CHUNK_SIZE && leafY >= 0 && + Math.abs(dx) + Math.abs(dy) < 3) { + chunkData[leafY * CHUNK_SIZE + leafX] = LEAF; + } + } + } + } + } } function getChunkCoordinates(worldX, worldY) {