]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/server/mapgen.c
22e54e09ffebd1f9f9f9397a749b7dfc1005f037
[dragonblocks_alpha.git] / src / server / mapgen.c
1 #include <math.h>
2 #include <stdlib.h>
3 #include "environment.h"
4 #include "perlin.h"
5 #include "server/biomes.h"
6 #include "server/mapgen.h"
7 #include "server/server_map.h"
8 #include "util.h"
9
10 void mapgen_set_node(v3s32 pos, MapNode node, MapgenStage mgs, List *changed_blocks)
11 {
12         MapgenSetNodeArg arg = {
13                 .mgs = mgs,
14                 .changed_blocks = changed_blocks,
15         };
16         map_set_node(server_map.map, pos, node, true, &arg);
17 }
18
19 // generate a block (does not manage block state or threading)
20 void mapgen_generate_block(MapBlock *block, List *changed_blocks)
21 {
22         MapBlockExtraData *extra = block->extra;
23
24         v3s32 block_node_pos = {block->pos.x * MAPBLOCK_SIZE, block->pos.y * MAPBLOCK_SIZE, block->pos.z * MAPBLOCK_SIZE};
25
26         char *block_data[BIOME_COUNT] = {NULL};
27         bool preprocessed_block[BIOME_COUNT];
28
29         for (u8 x = 0; x < MAPBLOCK_SIZE; x++) {
30                 s32 pos_x = block_node_pos.x + x;
31
32                 for (u8 z = 0; z < MAPBLOCK_SIZE; z++) {
33                         v2s32 pos_horizontal = {pos_x, block_node_pos.z + z};
34
35                         s32 height = (pnoise2d(U32(pos_horizontal.x) / 32.0, U32(pos_horizontal.y) / 32.0, 0.45, 5, seed + SO_HEIGHT) * 16.0)
36                                 * (pnoise2d(U32(pos_horizontal.x) / 256.0, U32(pos_horizontal.y) / 256.0, 0.45, 5, seed + SO_HILLYNESS) * 0.5 + 0.5)
37                                 + 32;
38
39                         f64 factor;
40                         Biome biome = get_biome(pos_horizontal, &factor);
41                         BiomeDef *biome_def = &biomes[biome];
42
43                         if (biome_def->block_data_size > 0 && ! block_data[biome])
44                                 block_data[biome] = malloc(biome_def->block_data_size);
45
46                         if (biome_def->preprocess_block && ! preprocessed_block[biome]) {
47                                 biome_def->preprocess_block(block, changed_blocks, block_data[biome]);
48                                 preprocessed_block[biome] = true;
49                         }
50
51                         char row_data[biome_def->row_data_size];
52
53                         if (biome_def->preprocess_row)
54                                 biome_def->preprocess_row(pos_horizontal, height, factor, row_data, block_data[biome]);
55
56                         height = biome_def->height(pos_horizontal, factor, height, row_data, block_data[biome]);
57
58                         for (u8 y = 0; y < MAPBLOCK_SIZE; y++) {
59                                 v3s32 pos = {pos_horizontal.x, block_node_pos.y + y, pos_horizontal.y};
60
61                                 f64 humidity = get_humidity(pos);
62                                 f64 temperature = get_temperature(pos);
63
64                                 s32 diff = pos.y - height;
65
66                                 Node node = biome_def->generate(pos, diff, humidity, temperature, factor, block, changed_blocks, row_data, block_data[biome]);
67
68                                 if (biome_def->snow && diff <= 1 && temperature < 0.0 && node == NODE_AIR)
69                                         node = NODE_SNOW;
70
71                                 pthread_mutex_lock(&block->mtx);
72                                 if (extra->mgs_buffer[x][y][z] <= MGS_TERRAIN) {
73                                         block->data[x][y][z] = map_node_create(node);
74                                         extra->mgs_buffer[x][y][z] = MGS_TERRAIN;
75                                 }
76                                 pthread_mutex_unlock(&block->mtx);
77                         }
78                 }
79         }
80
81         for (Biome i = 0; i < BIOME_COUNT; i++) {
82                 if (block_data[i])
83                         free(block_data[i]);
84         }
85 }