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
|
#include "Generator.hpp"
#include "../Math/Noise.hpp"
namespace MC::World {
Chunk Generator::generate(int64_t chunk_x, int64_t chunk_y) {
Chunk chunk(chunk_x, chunk_y);
auto ocean_weights = ocean_weights_pass(chunk_x, chunk_y);
auto biome_weights = biome_weights_pass(chunk_x, chunk_y, ocean_weights);
auto heights = height_pass(chunk_x, chunk_y, biome_weights);
auto biomes = flat_biome_pass(biome_weights);
float extent = 60.0f;
float base = 4.0f;
for (int x = 0; x < Chunk::Width; x++) {
for (int z = 0; z < Chunk::Width; z++) {
uint32_t height = std::round(heights(x, z) * extent + base);
auto biome = biomes(x, z);
BlockType top_block{};
BlockType fill_block{};
switch (biome) {
case BiomeType::Forest:
case BiomeType::Plains:
top_block = BlockType::Grass;
fill_block = BlockType::Dirt;
break;
case BiomeType::Desert:
top_block = BlockType::Sand;
fill_block = BlockType::Sand;
break;
case BiomeType::Ocean:
top_block = BlockType::Water;
fill_block = BlockType::Dirt;
break;
}
for (int y = 0; y < Chunk::Height; y++) {
BlockType type = BlockType::Air;
if (y < (int32_t)height - 10) {
type = BlockType::Stone;
} else if (y < height) {
type = fill_block;
} else if (y == height) {
type = top_block;
}
chunk.set(x, y, z, type);
}
}
}
return chunk;
}
Matrix<Chunk::Width, Chunk::Width> Generator::ocean_weights_pass(int64_t chunk_x, int64_t chunk_y) {
auto ocean_offset = 1125.0f;
auto ocean_scale = 0.05f;
Matrix<Chunk::Width, Chunk::Width> ocean_weights{};
for (int x = 0; x < Chunk::Width; x++) {
for (int y = 0; y < Chunk::Width; y++) {
float ocean_weight = Math::noise2d({
(chunk_x * Chunk::Width + x) * ocean_scale + ocean_offset,
(chunk_y * Chunk::Width + y) * ocean_scale + ocean_offset
});
ocean_weights(x, y) = ocean_weight;
}
}
return ocean_weights;
}
Matrix<Chunk::Width, Chunk::Width, Vector<BiomeType::Size>> Generator::biome_weights_pass(
int64_t chunk_x,
int64_t chunk_y,
Matrix<Chunk::Width, Chunk::Width, float> ocean_weights
) {
auto ocean_threshold = 0.4f;
std::vector<float> biome_offsets = {110.0f, 2450.0f, 5042.0f};
auto biome_scale = 0.15f;
Matrix<Chunk::Width, Chunk::Width, Vector<BiomeType::Size>> biome_weights{};
for (int x = 0; x < Chunk::Width; x++) {
for (int y = 0; y < Chunk::Width; y++) {
Vector<BiomeType::Size> weights{};
for (auto biome : MC::World::BiomeType::all_ground()) {
float biome_noise = Math::noise2d({
(chunk_x * Chunk::Width + x) * biome_scale + biome_offsets[biome],
(chunk_y * Chunk::Width + y) * biome_scale + biome_offsets[biome]
});
weights[biome] = biome_noise;
}
bool ocean_weight = ocean_weights(x, y) < ocean_threshold;
weights[MC::World::BiomeType::Ocean] = ocean_weight;
biome_weights(x,y) = weights;
}
}
return biome_weights;
}
Matrix<Chunk::Width, Chunk::Width> Generator::height_pass(
int64_t chunk_x,
int64_t chunk_y,
Matrix<Chunk::Width, Chunk::Width, Vector<BiomeType::Size>> biome_weights
) {
auto height_offset = 6050.0f;
auto height_scale = 0.7f;
Matrix<Chunk::Width, Chunk::Width> heights{};
for (int x = 0; x < Chunk::Width; x++) {
for (int y = 0; y < Chunk::Width; y++) {
auto weights = biome_weights(x, y);
auto total_weight = 0.0f;
for (auto biome : MC::World::BiomeType::all()) {
total_weight += weights[biome];
}
std::pair<float, float> total_effect{};
for (auto biome : MC::World::BiomeType::all()) {
auto weight = weights[biome];
std::pair<float, float> effect{};
switch (biome) {
case MC::World::BiomeType::Forest:
effect = {0.5f, 0.45f};
break;
case MC::World::BiomeType::Plains:
effect = {0.4f, 0.4f};
break;
case MC::World::BiomeType::Desert:
effect = {0.6f, 0.35f};
break;
case MC::World::BiomeType::Ocean:
effect = {0.0f, 0.0f};
break;
}
total_effect = {
total_effect.first + effect.first * (weight / total_weight),
total_effect.second + effect.second * (weight / total_weight),
};
}
float height = Math::noise2d({
(chunk_x * Chunk::Width + x) * height_scale + height_offset,
(chunk_y * Chunk::Width + y) * height_scale + height_offset
}) * total_effect.first + total_effect.second;
heights(x, y) = height;
}
}
return heights;
}
Matrix<Chunk::Width, Chunk::Width, BiomeType> Generator::flat_biome_pass(
Matrix<Chunk::Width, Chunk::Width, Vector<BiomeType::Size>> biome_weights
) {
Matrix<Chunk::Width, Chunk::Width, BiomeType> biomes{};
for (int x = 0; x < Chunk::Width; x++) {
for (int y = 0; y < Chunk::Width; y++) {
auto weights = biome_weights(x, y);
std::pair<MC::World::BiomeType, float> max_biome{MC::World::BiomeType::Plains, 0.0f};
for (auto biome : MC::World::BiomeType::all()) {
auto weight = weights[biome];
if (weight > max_biome.second) {
max_biome = {biome, weight};
}
}
biomes(x, y) = max_biome.first;
}
}
return biomes;
}
}
|