-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworldgen.cpp
68 lines (64 loc) · 1.65 KB
/
worldgen.cpp
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
#include "worldgen.h"
WorldGenerator::WorldGenerator(void)
{
m_seed = 0;
}
WorldGenerator::WorldGenerator(unsigned int seed)
{
m_seed = seed;
if (m_seed == 0)
{
long current_time = time(NULL);
srand(current_time);
logw("\nGenerator created with random seed : " + std::to_string(current_time));
}
else
{
srand(m_seed);
logw("\nGenerator created with set seed : " + std::to_string(seed));
}
for (int i = 0; i < WORLD_STRETCHING_NB_PARAMETERS; i++)
m_perlin_noises[i] = PerlinNoise(rand());
m_population_pns[0] = PerlinNoise(rand());
srand(time(NULL));
}
GenResult WorldGenerator::generate(int int_x, int int_y)
{
double x;
double y;
double noise;
double value = 0;
for (int i = 0; i < WORLD_STRETCHING_NB_PARAMETERS; i++)
{
x = (double)int_x / WORLD_STRETCHING_VALUES[i];
y = (double)int_y / WORLD_STRETCHING_VALUES[i];
noise = m_perlin_noises[i].noise(x, y, 1);
value += noise * pow(noise + 0.5, WORLD_STRETCHING_EXTREMES[i]);
}
value /= WORLD_STRETCHING_NB_PARAMETERS;
GenResult result;
result.value = value;
if (value < 0.28)
result.square = GenResultBlock::WATER;
else if (value < 0.295)
result.square = GenResultBlock::SAND;
else if (value < 0.41)
{
x = (double)int_x / 2;
y = (double)int_y / 2;
noise = m_population_pns[0].noise(x, y, 1);
if (noise < 0.35)
result.square = GenResultBlock::GRASS_AND_STICKS;
else if (noise < 0.75)
result.square = GenResultBlock::GRASS;
else
result.square = GenResultBlock::TREE;
}
else if (value < 0.5)
result.square = GenResultBlock::GRASS;
else if (value < 0.51)
result.square = GenResultBlock::STONE;
else
result.square = GenResultBlock::MOUNTAIN;
return result;
}