Voila une toute première version d'une implémentation du Perlin Noise ( aucune bibliothèque utilisée )
Fait en une soirée avec Perplexity AI alors que je ne connaissais rien au Perlin Noise.
Encore pas mal de chose à améliorer, mais je suis content de ce début

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
using System;
 
public class PerlinNoise
{
    private static int[] permutation = { 
                    151, 160, 137,  91,  90,  15, 131,  13, 201,  95,  96,  53, 194, 233,   7, 225,
                    140,  36, 103,  30,  69, 142,   8,  99,  37, 240,  21,  10,  23, 190,   6, 148,
                    247, 120, 234,  75,   0,  26, 197,  62,  94, 252, 219, 203, 117,  35,  11,  32,
                    57, 177,  33,  88, 237, 149,  56,  87, 174,  20, 125, 136, 171, 168,  68, 175,
                    74, 165,  71, 134, 139,  48,  27, 166,  77, 146, 158, 231,  83, 111, 229, 122,
                    60, 211, 133, 230, 220, 105,  92,  41,  55,  46, 245,  40, 244, 102, 143,  54,
                    65,  25,  63, 161,   1, 216,  80,  73, 209,  76, 132, 187, 208,  89,  18, 169,
                    200, 196, 135, 130, 116, 188, 159,  86, 164, 100, 109, 198, 173, 186,   3,  64,
                    52, 217, 226, 250, 124, 123,   5, 202,  38, 147, 118, 126, 255,  82,  85, 212,
                    207, 206,  59, 227,  47,  16,  58,  17, 182, 189,  28,  42, 223, 183, 170, 213,
                    119, 248, 152,   2,  44, 154, 163,  70, 221, 153, 101, 155, 167,  43, 172,   9,
                    129,  22,  39, 253,  19,  98, 108, 110,  79, 113, 224, 232, 178, 185, 112, 104,
                    218, 246,  97, 228, 251,  34, 242, 193, 238, 210, 144,  12, 191, 179, 162, 241,
                    81,  51, 145, 235, 249,  14, 239, 107,  49, 192, 214,  31, 181, 199, 106, 157,
                    184,  84, 204, 176, 115, 121,  50,  45, 127,   4, 150, 254, 138, 236, 205,  93,
                    222, 114,  67,  29,  24,  72, 243, 141, 128, 195,  78,  66, 215,  61, 156, 180 };
 
 
    private static int[] p;
 
    static PerlinNoise()
    {
        p = new int[512];
        for (int i = 0; i < 512; i++)
        {
            p[i] = permutation[i % 256];
        }
    }
 
    public static double Noise(double x, double y)
    {
        int X = (int)Math.Floor(x) & 255;
        int Y = (int)Math.Floor(y) & 255;
 
        x -= Math.Floor(x);
        y -= Math.Floor(y);
 
        double u = Fade(x);
        double v = Fade(y);
 
        int A = p[X] + Y;
        int B = p[X + 1] + Y;
 
        return Lerp(v,
            Lerp(u,
                Grad(p[A], x , y),
                Grad(p[B], x - 1 , y)),
            Lerp(u,
                Grad(p[A + 1], x , y - 1),
                Grad(p[B + 1], x - 1 , y - 1)));
    }
 
    private static double Fade(double t)
    {
        return t * t * t * (t * (t * 6 - 15) + 10);
    }
 
    private static double Lerp(double t , double a , double b)
    {
        return a + t * (b - a);
    }
 
    private static double Grad(int hash , double x , double y)
    {
        int h = hash & 15;
        double u = h < 8 ? x : y;
        double v = h < 4 ? y : h == 12 || h == 14 ? x : 0;
        return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v);
    }
 
    public static double OctaveNoise(double x, double y)
    {
        // https://libnoise.sourceforge.net/tutorials/tutorial4.html
 
        int octaves = 6; // Nombre d'octaves pour combiner les bruits de Perlin
        double frequency = 1;
        double lacunarity = 2.0; // Facteur d'augmentation de la fréquence entre les octaves
        double persistence = 0.25; // Facteur de diminution de l'amplitude entre les octaves
 
        double total = 0;
        double amplitude = 1;
        double maxValue = 0; // Used for normalizing result to [0,1]
 
        for (int i = 0; i < octaves; i++)
        {
            total += Noise(x * frequency , y * frequency) * amplitude;
            maxValue += amplitude;
            amplitude *= persistence;
            frequency *= lacunarity;
        }
 
        return total / maxValue;
    }
}
 
 
class Program
{
    static void Main(string[] args)
    {
        int width = 100; // Remplacez par la largeur souhaitée de votre carte
        int height = 50; // Remplacez par la hauteur souhaitée de votre carte
        double[,] elevation = new double[height, width];
 
        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                double nx = (double)x / width - 0.5;
                double ny = (double)y / height - 0.5;
                elevation[y, x] = PerlinNoise.OctaveNoise(nx, ny);
                //Console.Write(elevation[y, x]+" ");
            }
        }
 
        //AnalyzeElevation(elevation);
        DisplayMap(elevation);
 
        // Maintenant, le tableau "elevation" contient les valeurs d'élévation générées à l'aide du bruit de Perlin avec prise en compte des octaves
        // Vous pouvez utiliser ces valeurs pour générer votre terrain ou votre carte selon les besoins
    }
/*    
static void AnalyzeElevation(double[,] elevation)
{
    double min = double.MaxValue;
    double max = double.MinValue;
 
    // Trouver les valeurs min et max
    for (int y = 0; y < elevation.GetLength(0); y++)
    {
        for (int x = 0; x < elevation.GetLength(1); x++)
        {
            if (elevation[y, x] < min) min = elevation[y, x];
            if (elevation[y, x] > max) max = elevation[y, x];
        }
    }
 
    // Initialiser les compteurs pour chaque intervalle
    int intervals = (int)((max - min) / 0.01) + 1;
    int[] counters = new int[intervals];
 
    // Compter les valeurs dans chaque intervalle
    for (int y = 0; y < elevation.GetLength(0); y++)
    {
        for (int x = 0; x < elevation.GetLength(1); x++)
        {
            int index = (int)((elevation[y, x] - min) / 0.01);
            counters[index]++;
        }
    }
 
    // Afficher les résultats
    for (int i = 0; i < intervals; i++)
    {
        double intervalStart = min + i * 0.01;
        double intervalEnd = intervalStart + 0.01;
        Console.WriteLine($"Interval [{intervalStart:F2}, {intervalEnd:F2}): {counters[i]}");
    }
}*/    
 
static void DisplayMap(double[,] map)
{
    int darkBlueCount = 0;
    int blueCount = 0;
    int yellowCount = 0;
    int greenCount = 0;
    int darkGreenCount = 0;
    int darkYellowCount = 0;
    int darkRedCount = 0;
    int whiteCount = 0;
 
    for (int y = 0; y < map.GetLength(0); y++)
    {
        for (int x = 0; x < map.GetLength(1); x++)
        {
            double value = map[y, x];
            if (value < -0.1)
            {
                Console.BackgroundColor = ConsoleColor.Cyan;
                Console.Write(" ");
                Console.ResetColor();
                darkBlueCount++;
            }
            else if (value < -0.03)
            {
                Console.BackgroundColor = ConsoleColor.Blue;
                Console.Write(" ");
                Console.ResetColor();
                blueCount++;
            }
            else if (value < 0)
            {
                Console.BackgroundColor = ConsoleColor.Yellow;
                Console.Write(" ");
                Console.ResetColor();
                yellowCount++;
            }
            else if (value < 0.03)
            {
                Console.BackgroundColor = ConsoleColor.Green;
                Console.Write(" ");
                Console.ResetColor();
                greenCount++;
            }
            else if (value < 0.09)
            {
                Console.BackgroundColor = ConsoleColor.Green;
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write("O");
                Console.ResetColor();
                darkGreenCount++;
            }
            else if (value < 0.17)
            {
                Console.BackgroundColor = ConsoleColor.Red;
                Console.Write(" ");
                Console.ResetColor();
                darkYellowCount++;
            }
            else if (value < 0.22)
            {
                Console.BackgroundColor = ConsoleColor.Magenta;
                Console.Write(" ");
                Console.ResetColor();
                darkRedCount++;
            }
            else
            {
                Console.BackgroundColor = ConsoleColor.White;
                Console.Write(" ");
                Console.ResetColor();
                whiteCount++;
            }
        }
        Console.ResetColor();
        Console.WriteLine();
    }
 
    // Afficher les compteurs
    Console.WriteLine("DarkBlue Count: " + darkBlueCount);
    Console.WriteLine("Blue Count: " + blueCount);
    Console.WriteLine("Yellow Count: " + yellowCount);
    Console.WriteLine("Green Count: " + greenCount);
    Console.WriteLine("DarkGreen Count: " + darkGreenCount);
    Console.WriteLine("DarkYellow Count: " + darkYellowCount);
    Console.WriteLine("DarkRed Count: " + darkRedCount);
    Console.WriteLine("DarkWhiteCount Count: " + whiteCount);
}
 
}
https://www.redblobgames.com/maps/terrain-from-noise/
https://gamedev.stackexchange.com/qu...noise-to-tiles
https://longwelwind.net/blog/perlin-noise/ ( Elevation + Humidity )
https://youtu.be/eiM_AStmeGE?si=Ha_xoG7ZpDX9byWc
https://youtu.be/cos4xkWbTRI?si=OINaJW2r86sz-bfr
https://youtu.be/wN-p7CG3LyU?si=pXFecqeR_y24O7Rw