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