2 #include "server/biomes.h"
3 #include "server/server_terrain.h"
4 #include "server/terrain_gen.h"
6 Biome get_biome(v2s32 pos, f64 *factor)
8 for (Biome i = 0; i < COUNT_BIOME; i++) {
9 BiomeDef *def = &biomes[i];
10 f64 f = def->probability == 1.0 ? 1.0
11 : (smooth2d(U32(pos.x) / def->threshold, U32(pos.y) / def->threshold, 0, seed + def->offset) * 0.5 - 0.5 + def->probability) / def->probability;
25 static s32 height_mountain(BiomeArgsHeight *args)
27 return pow((args->height + 96) * pow(((smooth2d(U32(args->pos.x) / 48.0, U32(args->pos.y) / 48.0, 0, seed + SO_MOUNTAIN_HEIGHT) + 1.0) * 256.0 + 128.0), args->factor), 1.0 / (args->factor + 1.0)) - 96;
30 static NodeType generate_mountain(BiomeArgsGenerate *args)
32 return args->diff <= 0 ? NODE_STONE : NODE_AIR;
45 static f64 ocean_level_start[COUNT_OCEAN] = {
61 s32 vulcano_crater_top;
62 NodeType vulcano_stone;
65 static const f64 vulcano_radius = 256.0;
66 static const f64 vulcano_chunk_offset = vulcano_radius * 2.0 / CHUNK_SIZE;
68 static OceanLevel get_ocean_level(f64 factor)
70 if (factor >= ocean_level_start[OCEAN_DEEP])
72 else if (factor >= ocean_level_start[OCEAN_MAIN])
74 else if (factor >= ocean_level_start[OCEAN_BEACH])
80 static f64 get_ocean_level_factor(f64 factor, OceanLevel level)
83 start = ocean_level_start[level];
84 end = ++level == COUNT_OCEAN ? 1.0 : ocean_level_start[level];
86 return (factor - start) / (end - start);
89 static bool is_vulcano(v2s32 pos)
93 return noise2d(pos.x, pos.y, 0, seed + SO_VULCANO) > 0.0
94 && get_biome((v2s32) {pos.x * CHUNK_SIZE, pos.y * CHUNK_SIZE}, &factor) == BIOME_OCEAN
95 && get_ocean_level(factor) == OCEAN_DEEP;
98 static bool find_near_vulcano(v2s32 pos, v2s32 *result)
100 f64 x = pos.x / vulcano_chunk_offset;
101 f64 z = pos.y / vulcano_chunk_offset;
111 for (s32 ix = lx; ix <= hx; ix++) {
112 for (s32 iz = lz; iz <= hz; iz++) {
113 v2s32 vulcano_pos = {ix * 32, iz * 32};
115 if (is_vulcano(vulcano_pos)) {
116 *result = vulcano_pos;
125 static f64 distance(v2s32 a, v2s32 b)
127 return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2));
130 static s32 calculate_ocean_floor(f64 factor, s32 height)
132 switch (get_ocean_level(factor)) {
134 return f64_mix(height + 1, 0, pow(get_ocean_level_factor(factor, OCEAN_EDGE), 0.8));
140 return f64_mix(0, -10, pow(get_ocean_level_factor(factor, OCEAN_MAIN), 0.5));
143 return f64_mix(-10, -50, pow(get_ocean_level_factor(factor, OCEAN_DEEP), 0.5));
152 static void chunk_ocean(BiomeArgsChunk *args)
154 OceanChunkData *chunk_data = args->chunk_data;
157 if ((chunk_data->has_vulcano = find_near_vulcano((v2s32) {args->chunk->pos.x, args->chunk->pos.z}, &vulcano_pos)))
158 chunk_data->vulcano_pos = (v2s32) {vulcano_pos.x * CHUNK_SIZE, vulcano_pos.y * CHUNK_SIZE};
161 static void row_ocean(BiomeArgsRow *args)
163 OceanChunkData *chunk_data = args->chunk_data;
164 OceanRowData *row_data = args->row_data;
166 row_data->vulcano = false;
168 if (chunk_data->has_vulcano) {
169 f64 dist = distance(args->pos, chunk_data->vulcano_pos);
171 if (dist < vulcano_radius) {
172 f64 crater_factor = pow(asin(1.0 - dist / vulcano_radius), 2.0);
173 f64 vulcano_height = (pnoise2d(U32(args->pos.x) / 100.0, U32(args->pos.y) / 100.0, 0.2, 2, seed + SO_VULCANO_HEIGHT) * 0.5 + 0.5) * 128.0 * crater_factor + 1.0 - 30.0;
174 bool is_crater = vulcano_height > 0;
177 vulcano_height = f64_min(vulcano_height + 5.0, 0.0);
179 if (vulcano_height < 0)
180 vulcano_height *= 2.0;
182 row_data->vulcano = true;
183 row_data->vulcano_crater = is_crater;
184 row_data->vulcano_height = floor(vulcano_height + 0.5);
185 row_data->vulcano_crater_top = 50 + floor((pnoise2d(U32(args->pos.x) / 3.0, U32(args->pos.y) / 3.0, 0.0, 1, seed + SO_VULCANO_CRATER_TOP) * 0.5 + 0.5) * 3.0 + 0.5);
186 row_data->vulcano_stone = is_crater
187 ? ((pnoise2d(U32(args->pos.x) / 16.0, U32(args->pos.y) / 16.0, 0.85, 3, seed + SO_VULCANO_STONE) * 0.5 + 0.5) * crater_factor > 0.4
195 static s32 height_ocean(BiomeArgsHeight *args)
197 OceanRowData *row_data = args->row_data;
199 s32 ocean_floor = calculate_ocean_floor(args->factor, args->height);
200 return row_data->vulcano ? f64_max(ocean_floor, row_data->vulcano_height) : ocean_floor;
203 NodeType ocean_get_node_at(v3s32 pos, s32 diff, void *_row_data)
205 OceanRowData *row_data = _row_data;
207 if (row_data->vulcano && row_data->vulcano_crater) {
209 return pos.y <= 45 ? NODE_LAVA : NODE_AIR;
211 return pos.y <= row_data->vulcano_crater_top ? row_data->vulcano_stone : NODE_AIR;
226 static NodeType generate_ocean(BiomeArgsGenerate *args)
228 return ocean_get_node_at(args->pos, args->diff, args->row_data);
233 static bool boulder_touching_ground(v3s32 pos, s32 diff)
235 for (s32 dir = diff > 0 ? -1 : +1; dir > 0 ? diff <= 0 : diff >= 0; pos.y += dir, diff += dir) {
236 if (smooth3d(U32(pos.x) / 12.0, U32(pos.y) / 6.0, U32(pos.z) / 12.0, 0, seed + SO_BOULDER) < 0.8)
243 static s32 height_hills(BiomeArgsHeight *args)
248 static NodeType generate_hills(BiomeArgsGenerate *args)
250 if (boulder_touching_ground(args->pos, args->diff))
253 if (args->diff <= -5)
255 else if (args->diff <= -1)
257 else if (args->diff <= 0)
263 BiomeDef biomes[COUNT_BIOME] = {
266 .offset = SO_MOUNTAIN,
269 .height = &height_mountain,
270 .generate = &generate_mountain,
271 .chunk_data_size = 0,
281 .height = &height_ocean,
282 .generate = &generate_ocean,
283 .chunk_data_size = sizeof(OceanChunkData),
284 .chunk = &chunk_ocean,
285 .row_data_size = sizeof(OceanRowData),
293 .height = &height_hills,
294 .generate = &generate_hills,
295 .chunk_data_size = 0,