]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/server/mapgen.c
9f51fa9b35dd7508fdab12c2d85e6ae2e48cfbc9
[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 + 32;
36
37                         f64 factor;
38                         Biome biome = get_biome(pos_horizontal, &factor);
39                         BiomeDef *biome_def = &biomes[biome];
40
41                         if (biome_def->block_data_size > 0 && ! block_data[biome])
42                                 block_data[biome] = malloc(biome_def->block_data_size);
43
44                         if (biome_def->preprocess_block && ! preprocessed_block[biome]) {
45                                 biome_def->preprocess_block(block, changed_blocks, block_data[biome]);
46                                 preprocessed_block[biome] = true;
47                         }
48
49                         char row_data[biome_def->row_data_size];
50
51                         if (biome_def->preprocess_row)
52                                 biome_def->preprocess_row(pos_horizontal, height, factor, row_data, block_data[biome]);
53
54                         height = biome_def->height(pos_horizontal, factor, height, row_data, block_data[biome]);
55
56                         for (u8 y = 0; y < MAPBLOCK_SIZE; y++) {
57                                 v3s32 pos = {pos_horizontal.x, block_node_pos.y + y, pos_horizontal.y};
58
59                                 f64 humidity = get_humidity(pos);
60                                 f64 temperature = get_temperature(pos);
61
62                                 s32 diff = pos.y - height;
63
64                                 Node node = biome_def->generate(pos, diff, humidity, temperature, factor, block, changed_blocks, row_data, block_data[biome]);
65
66                                 if (biome_def->snow && diff <= 1 && temperature < 0.0 && node == NODE_AIR)
67                                         node = NODE_SNOW;
68
69                                 pthread_mutex_lock(&block->mtx);
70                                 if (extra->mgs_buffer[x][y][z] <= MGS_TERRAIN) {
71                                         block->data[x][y][z] = map_node_create(node);
72                                         extra->mgs_buffer[x][y][z] = MGS_TERRAIN;
73                                 }
74                                 pthread_mutex_unlock(&block->mtx);
75                         }
76                 }
77         }
78
79         for (Biome i = 0; i < BIOME_COUNT; i++) {
80                 if (block_data[i])
81                         free(block_data[i]);
82         }
83 }