From 0b96238226994d82e4ae232925a938379cc28001 Mon Sep 17 00:00:00 2001 From: "Kacper Kostka (aider)" Date: Sat, 5 Apr 2025 15:05:51 +0200 Subject: [PATCH] feat: Implement minimum 5-pixel spacing for tree seed generation --- js/world.js | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/js/world.js b/js/world.js index d990f2c..2f648ad 100644 --- a/js/world.js +++ b/js/world.js @@ -177,7 +177,21 @@ function generateSpecialChunk(chunkData, chunkX, playerChunkX) { // Randomly spawn tree seeds on grass (reduced frequency) if (random() < 0.03) { // Reduced from 8% to 3% chance for a tree seed on grass const seedY = floorY - noiseHeight - 1; // Position above the grass - if (seedY >= 0 && chunkData[(floorY - noiseHeight) * CHUNK_SIZE + x] === GRASS) { + + // Check if there are any existing tree seeds within 5 pixels + let hasSeedNearby = false; + for (let checkY = Math.max(0, seedY - 5); checkY <= Math.min(CHUNK_SIZE - 1, seedY + 5); checkY++) { + for (let checkX = Math.max(0, x - 5); checkX <= Math.min(CHUNK_SIZE - 1, x + 5); checkX++) { + if (chunkData[checkY * CHUNK_SIZE + checkX] === TREE_SEED) { + hasSeedNearby = true; + break; + } + } + if (hasSeedNearby) break; + } + + // Only place the seed if there are no other seeds nearby + if (!hasSeedNearby && seedY >= 0 && chunkData[(floorY - noiseHeight) * CHUNK_SIZE + x] === GRASS) { chunkData[seedY * CHUNK_SIZE + x] = TREE_SEED; // Add metadata for the tree seed const seedMetadata = { @@ -262,7 +276,9 @@ function generateSpecialChunk(chunkData, chunkX, playerChunkX) { } } - // Add additional tree seeds scattered throughout the terrain (with increased spacing and reduced chance) + // Add additional tree seeds scattered throughout the terrain with minimum spacing + const treePositions = []; // Track positions of placed tree seeds + for (let x = 0; x < CHUNK_SIZE; x += 15 + Math.floor(random() * 20)) { // Increased spacing from 5-15 to 15-35 const height = heightMap[x]; const surfaceY = floorY - height; @@ -272,8 +288,21 @@ function generateSpecialChunk(chunkData, chunkX, playerChunkX) { // Reduced from 25% to 10% chance for a tree seed at each valid position if (random() < 0.1) { const seedY = surfaceY - 1; // Position above the grass - if (seedY >= 0) { + + // Check if this position is at least 5 pixels away from any existing tree seed + let tooClose = false; + for (const pos of treePositions) { + const distance = Math.abs(x - pos.x) + Math.abs(seedY - pos.y); // Manhattan distance + if (distance < 5) { + tooClose = true; + break; + } + } + + // Only place the seed if it's not too close to another seed + if (!tooClose && seedY >= 0) { chunkData[seedY * CHUNK_SIZE + x] = TREE_SEED; + treePositions.push({ x, y: seedY }); } } }