From d34a695bb81e14262b3c4ffa5b7547464e1b20dd Mon Sep 17 00:00:00 2001 From: "Kacper Kostka (aider)" Date: Sat, 5 Apr 2025 15:21:34 +0200 Subject: [PATCH] feat: Prevent tree seeds from spawning near water --- js/world.js | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/js/world.js b/js/world.js index 72ebd4a..f927865 100644 --- a/js/world.js +++ b/js/world.js @@ -211,10 +211,19 @@ function generateSpecialChunk(chunkData, chunkX, playerChunkX) { if (hasSeedNearby) break; } - // Only place the seed if there are no other seeds nearby + // Check if there's water below or nearby + let hasWaterBelow = false; + for (let checkY = floorY - noiseHeight + 1; checkY < Math.min(CHUNK_SIZE, floorY - noiseHeight + 5); checkY++) { + if (chunkData[checkY * CHUNK_SIZE + x] === WATER) { + hasWaterBelow = true; + break; + } + } + + // Only place the seed if there are no other seeds nearby and no water below // Place seed 3 pixels above the surface instead of just 1 const elevatedSeedY = floorY - noiseHeight - 3; // 3 pixels above the grass - if (!hasSeedNearby && elevatedSeedY >= 0 && chunkData[(floorY - noiseHeight) * CHUNK_SIZE + x] === GRASS) { + if (!hasSeedNearby && !hasWaterBelow && elevatedSeedY >= 0 && chunkData[(floorY - noiseHeight) * CHUNK_SIZE + x] === GRASS) { chunkData[elevatedSeedY * CHUNK_SIZE + x] = TREE_SEED; // Add metadata for the tree seed const seedMetadata = { @@ -322,8 +331,17 @@ function generateSpecialChunk(chunkData, chunkX, playerChunkX) { } } - // Only place the seed if it's not too close to another seed - if (!tooClose && seedY >= 0) { + // Check if there's water below or nearby + let hasWaterBelow = false; + for (let checkY = surfaceY + 1; checkY < Math.min(CHUNK_SIZE, surfaceY + 5); checkY++) { + if (chunkData[checkY * CHUNK_SIZE + x] === WATER) { + hasWaterBelow = true; + break; + } + } + + // Only place the seed if it's not too close to another seed and no water below + if (!tooClose && !hasWaterBelow && seedY >= 0) { chunkData[seedY * CHUNK_SIZE + x] = TREE_SEED; treePositions.push({ x, y: seedY }); }