]> git.lizzy.rs Git - dragonblocks_alpha.git/blobdiff - src/server/mapgen.c
Use thread pool for map generation
[dragonblocks_alpha.git] / src / server / mapgen.c
index b9c7e97c3122099c01636da1534e29184ce810fc..767c19ec767b0703915fdd9537438cfce258b682 100644 (file)
@@ -1,11 +1,12 @@
-#include <stdio.h>
 #include <math.h>
 #include <stdlib.h>
+#include <stdio.h>
 #include "environment.h"
 #include "perlin.h"
 #include "server/biomes.h"
 #include "server/mapgen.h"
 #include "server/server_map.h"
+#include "server/trees.h"
 #include "util.h"
 
 void mapgen_set_node(v3s32 pos, MapNode node, MapgenStage mgs, List *changed_blocks)
@@ -14,20 +15,19 @@ void mapgen_set_node(v3s32 pos, MapNode node, MapgenStage mgs, List *changed_blo
                .mgs = mgs,
                .changed_blocks = changed_blocks,
        };
+
        map_set_node(server_map.map, pos, node, true, &arg);
 }
 
 // generate a block (does not manage block state or threading)
 void mapgen_generate_block(MapBlock *block, List *changed_blocks)
 {
-       printf("Generating block at (%d, %d, %d)\n", block->pos.x, block->pos.y, block->pos.z);
-
        MapBlockExtraData *extra = block->extra;
 
        v3s32 block_node_pos = {block->pos.x * MAPBLOCK_SIZE, block->pos.y * MAPBLOCK_SIZE, block->pos.z * MAPBLOCK_SIZE};
 
        char *block_data[BIOME_COUNT] = {NULL};
-       bool preprocessed_block[BIOME_COUNT];
+       bool preprocessed_block[BIOME_COUNT] = {false};
 
        for (u8 x = 0; x < MAPBLOCK_SIZE; x++) {
                s32 pos_x = block_node_pos.x + x;
@@ -35,7 +35,9 @@ void mapgen_generate_block(MapBlock *block, List *changed_blocks)
                for (u8 z = 0; z < MAPBLOCK_SIZE; z++) {
                        v2s32 pos_horizontal = {pos_x, block_node_pos.z + z};
 
-                       s32 height = pnoise2d(U32(pos_horizontal.x) / 32.0, U32(pos_horizontal.y) / 32.0, 0.45, 5, seed + SO_HEIGHT) * 16.0 + 32;
+                       s32 default_height = (pnoise2d(U32(pos_horizontal.x) / 32.0, U32(pos_horizontal.y) / 32.0, 0.45, 5, seed + SO_HEIGHT) * 16.0)
+                               * (pnoise2d(U32(pos_horizontal.x) / 256.0, U32(pos_horizontal.y) / 256.0, 0.45, 5, seed + SO_HILLYNESS) * 0.5 + 0.5)
+                               + 32.0;
 
                        f64 factor;
                        Biome biome = get_biome(pos_horizontal, &factor);
@@ -52,27 +54,40 @@ void mapgen_generate_block(MapBlock *block, List *changed_blocks)
                        char row_data[biome_def->row_data_size];
 
                        if (biome_def->preprocess_row)
-                               biome_def->preprocess_row(pos_horizontal, height, factor, row_data, block_data[biome]);
+                               biome_def->preprocess_row(pos_horizontal, factor, row_data, block_data[biome]);
 
-                       height = biome_def->height(pos_horizontal, factor, height, row_data, block_data[biome]);
+                       s32 height = biome_def->height(pos_horizontal, factor, default_height, row_data, block_data[biome]);
 
                        for (u8 y = 0; y < MAPBLOCK_SIZE; y++) {
                                v3s32 pos = {pos_horizontal.x, block_node_pos.y + y, pos_horizontal.y};
 
-                               f64 wetness = get_wetness(pos);
+                               f64 humidity = get_humidity(pos);
                                f64 temperature = get_temperature(pos);
 
                                s32 diff = pos.y - height;
 
-                               Node node = biome_def->generate(pos, diff, wetness, temperature, factor, block, changed_blocks, row_data, block_data[biome]);
+                               Node node = biome_def->generate(pos, diff, humidity, temperature, factor, block, changed_blocks, row_data, block_data[biome]);
 
                                if (biome_def->snow && diff <= 1 && temperature < 0.0 && node == NODE_AIR)
                                        node = NODE_SNOW;
 
+                               if (diff == 1) {
+                                       for (int i = 0; i < NUM_TREES; i++) {
+                                               TreeDef *def = &tree_definitions[i];
+
+                                               if (def->condition(pos, humidity, temperature, biome, factor, block, row_data, block_data)
+                                                       && noise2d(pos.x, pos.z, 0, seed + def->offset) * 0.5 + 0.5 < def->probability
+                                                       && smooth2d(U32(pos.x) / def->spread, U32(pos.z) / def->spread, 0, seed + def->area_offset) * 0.5 + 0.5 < def->area_probability) {
+                                                       def->generate(pos, changed_blocks);
+                                                       break;
+                                               }
+                                       }
+                               }
+
                                pthread_mutex_lock(&block->mtx);
-                               if (extra->mgs_buffer[x][y][z] <= MGS_TERRAIN) {
-                                       block->data[x][y][z] = map_node_create(node);
-                                       extra->mgs_buffer[x][y][z] = MGS_TERRAIN;
+                               if (extra->mgsb.raw.nodes[x][y][z] <= MGS_TERRAIN) {
+                                       block->data[x][y][z] = map_node_create(node, (Blob) {0, NULL});
+                                       extra->mgsb.raw.nodes[x][y][z] = MGS_TERRAIN;
                                }
                                pthread_mutex_unlock(&block->mtx);
                        }