From d43c291c837d1aa6a653ba0d455fe3ff2708579e Mon Sep 17 00:00:00 2001
From: miepzerino <o.skotnik@gmail.com>
Date: Wed, 02 Apr 2025 08:41:13 +0000
Subject: [PATCH] #38 added minClusterSize to ores

---
 Assets/Scripts/GenerateTileMap.cs            |   47 ++++++++++++++++++++++++++++++++++++++++++++++-
 Assets/TileSets/Palettes/DrillIronOre.prefab |    2 +-
 2 files changed, 47 insertions(+), 2 deletions(-)

diff --git a/Assets/Scripts/GenerateTileMap.cs b/Assets/Scripts/GenerateTileMap.cs
index 46b27b8..01742a2 100644
--- a/Assets/Scripts/GenerateTileMap.cs
+++ b/Assets/Scripts/GenerateTileMap.cs
@@ -131,7 +131,13 @@
                             Vector3Int tileSpawnCoord = new Vector3Int(x, y);
                             if (!destroyedTiles.Contains(tileSpawnCoord) && tilemap.HasTile(tileSpawnCoord))
                             {
-                                tilemap.SetTile(tileSpawnCoord, generateable.tile);
+                                // Check potential cluster size before placing
+                                int clusterSize = CountPotentialClusterSize(x, y, generateable.weight, generateable.clusterWeight);
+                                if (clusterSize >= generateable.minClusterSize)
+                                {
+                                    tilemap.SetTile(tileSpawnCoord, generateable.tile);
+                                }
+                                //tilemap.SetTile(tileSpawnCoord, generateable.tile);
                             }
                         }
 
@@ -167,4 +173,43 @@
         yield return null;
         finishedCallback();
     }
+    private int CountPotentialClusterSize(int startX, int startY, int weight, int clusterWeight)
+    {
+        int size = 0;
+        Queue<Vector2Int> toCheck = new Queue<Vector2Int>();
+        HashSet<Vector2Int> checked_positions = new HashSet<Vector2Int>();
+
+        toCheck.Enqueue(new Vector2Int(startX, startY));
+        checked_positions.Add(new Vector2Int(startX, startY));
+
+        while (toCheck.Count > 0)
+        {
+            Vector2Int current = toCheck.Dequeue();
+            size++;
+
+            // Check all 8 neighboring tiles
+            for (int dx = -1; dx <= 1; dx++)
+            {
+                for (int dy = -1; dy <= 1; dy++)
+                {
+                    if (dx == 0 && dy == 0) continue;
+
+                    Vector2Int neighbor = new Vector2Int(current.x + dx, current.y + dy);
+                    if (checked_positions.Contains(neighbor)) continue;
+
+                    float xPerlin = ((float)neighbor.x / maxWidth) * clusterWeight + offsetX;
+                    float yPerlin = ((float)neighbor.y / maxHeight) * clusterWeight + offsetY;
+                    float perlinNoise = Mathf.PerlinNoise(xPerlin, yPerlin);
+
+                    if (perlinNoise <= (1f / (float)weight))
+                    {
+                        toCheck.Enqueue(neighbor);
+                        checked_positions.Add(neighbor);
+                    }
+                }
+            }
+        }
+
+        return size;
+    }
 }
diff --git a/Assets/TileSets/Palettes/DrillIronOre.prefab b/Assets/TileSets/Palettes/DrillIronOre.prefab
index ef4d998..c94273e 100644
--- a/Assets/TileSets/Palettes/DrillIronOre.prefab
+++ b/Assets/TileSets/Palettes/DrillIronOre.prefab
@@ -77,7 +77,7 @@
   isGenerateable: 1
   weight: 7
   clusterWeight: 40
-  minClusterSize: 4
+  minClusterSize: 8
   tile: {fileID: 11400000, guid: bbc5a3964d8331546a4dbc880f8ae9fc, type: 2}
   maxSpawnHeight: 250
   minSpawnHeight: 150

--
Gitblit v1.9.3