Mapgen: Remove calculateNoise from most mapgens
authorkwolekr <kwolekr@minetest.net>
Fri, 20 May 2016 07:37:31 +0000 (03:37 -0400)
committerkwolekr <kwolekr@minetest.net>
Sat, 28 May 2016 03:23:58 +0000 (23:23 -0400)
This commit moves noise calculation to the functions where the noise is
actually required, increasing the separation of concerns and level of
interdependency for each mapgen method.  Valleys Mapgen is left unmodified.

src/mapgen.cpp
src/mapgen_flat.cpp
src/mapgen_flat.h
src/mapgen_fractal.cpp
src/mapgen_fractal.h
src/mapgen_v5.cpp
src/mapgen_v5.h
src/mapgen_v7.cpp
src/mapgen_v7.h
src/mapgen_valleys.cpp

index 7e959533db0c6053a74372229121081542636bdf..009a81f0c8f87a7083ed485463f20b3c8e477e9f 100644 (file)
@@ -386,6 +386,8 @@ MgStoneType MapgenBasic::generateBiomes()
        u32 index = 0;
        MgStoneType stone_type = MGSTONE_STONE;
 
+       noise_filler_depth->perlinMap2D(node_min.X, node_min.Z);
+
        for (s16 z = node_min.Z; z <= node_max.Z; z++)
        for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
                Biome *biome = NULL;
index a541a54f3e22f9c1d874b52dfd55ccb1a1462f32..ded859f650950e14470866e2fa2f21474b6bf94c 100644 (file)
@@ -227,9 +227,6 @@ void MapgenFlat::makeChunk(BlockMakeData *data)
 
        blockseed = getBlockSeed2(full_node_min, seed);
 
-       // Make some noise
-       calculateNoise();
-
        // Generate base terrain, mountains, and ridges with initial heightmaps
        s16 stone_surface_max_y = generateTerrain();
 
@@ -312,24 +309,6 @@ void MapgenFlat::makeChunk(BlockMakeData *data)
 }
 
 
-void MapgenFlat::calculateNoise()
-{
-       //TimeTaker t("calculateNoise", NULL, PRECISION_MICRO);
-       s16 x = node_min.X;
-       s16 z = node_min.Z;
-
-       if ((spflags & MGFLAT_LAKES) || (spflags & MGFLAT_HILLS))
-               noise_terrain->perlinMap2D(x, z);
-
-       // Cave noises are calculated in generateCaves()
-       // only if solid terrain is present in mapchunk
-
-       noise_filler_depth->perlinMap2D(x, z);
-
-       //printf("calculateNoise: %dus\n", t.stop());
-}
-
-
 s16 MapgenFlat::generateTerrain()
 {
        MapNode n_air(CONTENT_AIR);
@@ -340,13 +319,14 @@ s16 MapgenFlat::generateTerrain()
        s16 stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT;
        u32 ni2d = 0;
 
+       bool use_noise = (spflags & MGFLAT_LAKES) || (spflags & MGFLAT_HILLS);
+       if (use_noise)
+               noise_terrain->perlinMap2D(node_min.X, node_min.Z);
+
        for (s16 z = node_min.Z; z <= node_max.Z; z++)
        for (s16 x = node_min.X; x <= node_max.X; x++, ni2d++) {
                s16 stone_level = ground_level;
-               float n_terrain = 0.0f;
-
-               if ((spflags & MGFLAT_LAKES) || (spflags & MGFLAT_HILLS))
-                       n_terrain = noise_terrain->result[ni2d];
+               float n_terrain = use_noise ? noise_terrain->result[ni2d] : 0.0f;
 
                if ((spflags & MGFLAT_LAKES) && n_terrain < lake_threshold) {
                        s16 depress = (lake_threshold - n_terrain) * lake_steepness;
index 2b98c1f31a703b97efd2dc5da2324fe0eb90ebad..39da6e0257255229a2a921228a45ecb268017b92 100644 (file)
@@ -78,7 +78,6 @@ public:
 
        virtual void makeChunk(BlockMakeData *data);
        int getSpawnLevelAtPoint(v2s16 p);
-       void calculateNoise();
        s16 generateTerrain();
 };
 
index dca10d25308b5f4aeeee2850f1869b6319025d98..f54de6275ee2c991f41b5080901bcf796f275574 100644 (file)
@@ -243,9 +243,6 @@ void MapgenFractal::makeChunk(BlockMakeData *data)
 
        blockseed = getBlockSeed2(full_node_min, seed);
 
-       // Make some noise
-       calculateNoise();
-
        // Generate base terrain, mountains, and ridges with initial heightmaps
        s16 stone_surface_max_y = generateTerrain();
 
@@ -328,23 +325,6 @@ void MapgenFractal::makeChunk(BlockMakeData *data)
 }
 
 
-void MapgenFractal::calculateNoise()
-{
-       //TimeTaker t("calculateNoise", NULL, PRECISION_MICRO);
-       s16 x = node_min.X;
-       s16 z = node_min.Z;
-
-       noise_seabed->perlinMap2D(x, z);
-
-       // Cave noises are calculated in generateCaves()
-       // only if solid terrain is present in mapchunk
-
-       noise_filler_depth->perlinMap2D(x, z);
-
-       //printf("calculateNoise: %dus\n", t.stop());
-}
-
-
 bool MapgenFractal::getFractalAtPoint(s16 x, s16 y, s16 z)
 {
        float cx, cy, cz, cw, ox, oy, oz, ow;
@@ -474,6 +454,8 @@ s16 MapgenFractal::generateTerrain()
        s16 stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT;
        u32 index2d = 0;
 
+       noise_seabed->perlinMap2D(node_min.X, node_min.Z);
+
        for (s16 z = node_min.Z; z <= node_max.Z; z++) {
                for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) {
                        u32 vi = vm->m_area.index(node_min.X, y, z);
index 176885d46bfe309a3d7a3aedcba2fd0b0c043a17..e30550405119039801d92b9993e239eb08e01d45 100644 (file)
@@ -88,7 +88,6 @@ public:
 
        virtual void makeChunk(BlockMakeData *data);
        int getSpawnLevelAtPoint(v2s16 p);
-       void calculateNoise();
        bool getFractalAtPoint(s16 x, s16 y, s16 z);
        s16 generateTerrain();
 };
index 0c063dc6f9f6eaa5f3a286543f3c24e1011b6740..85df34353a0b10891299a91d1533b8d73d23547e 100644 (file)
@@ -225,9 +225,6 @@ void MapgenV5::makeChunk(BlockMakeData *data)
        // Create a block-specific seed
        blockseed = getBlockSeed2(full_node_min, seed);
 
-       // Make some noise
-       calculateNoise();
-
        // Generate base terrain
        s16 stone_surface_max_y = generateBaseTerrain();
 
@@ -312,26 +309,6 @@ void MapgenV5::makeChunk(BlockMakeData *data)
 }
 
 
-void MapgenV5::calculateNoise()
-{
-       //TimeTaker t("calculateNoise", NULL, PRECISION_MICRO);
-       s16 x = node_min.X;
-       s16 y = node_min.Y - 1;
-       s16 z = node_min.Z;
-
-       noise_factor->perlinMap2D(x, z);
-       noise_height->perlinMap2D(x, z);
-       noise_ground->perlinMap3D(x, y, z);
-
-       // Cave noises are calculated in generateCaves()
-       // only if solid terrain is present in mapchunk
-
-       noise_filler_depth->perlinMap2D(x, z);
-
-       //printf("calculateNoise: %dus\n", t.stop());
-}
-
-
 //bool is_cave(u32 index) {
 //     double d1 = contour(noise_cave1->result[index]);
 //     double d2 = contour(noise_cave2->result[index]);
@@ -355,6 +332,10 @@ int MapgenV5::generateBaseTerrain()
        u32 index2d = 0;
        int stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT;
 
+       noise_factor->perlinMap2D(node_min.X, node_min.Z);
+       noise_height->perlinMap2D(node_min.X, node_min.Z);
+       noise_ground->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z);
+
        for (s16 z=node_min.Z; z<=node_max.Z; z++) {
                for (s16 y=node_min.Y - 1; y<=node_max.Y + 1; y++) {
                        u32 vi = vm->m_area.index(node_min.X, y, z);
index 99836b23e0bad597119b6b82412a5a10614cfc12..dd5ca0a317b9bae7a1a4b2ad98f9f25b365d82b5 100644 (file)
@@ -69,7 +69,6 @@ public:
 
        virtual void makeChunk(BlockMakeData *data);
        int getSpawnLevelAtPoint(v2s16 p);
-       void calculateNoise();
        int generateBaseTerrain();
 };
 
index ee2c5f45f56658c2a70e2dfc3f8e59ba9142e430..5c5f13b8801bdc2bb48497f03af60501c5dd4b39 100644 (file)
@@ -252,9 +252,6 @@ void MapgenV7::makeChunk(BlockMakeData *data)
 
        blockseed = getBlockSeed2(full_node_min, seed);
 
-       // Make some noise
-       calculateNoise();
-
        // Generate terrain and ridges with initial heightmaps
        s16 stone_surface_max_y = generateTerrain();
 
@@ -340,37 +337,6 @@ void MapgenV7::makeChunk(BlockMakeData *data)
 }
 
 
-void MapgenV7::calculateNoise()
-{
-       //TimeTaker t("calculateNoise", NULL, PRECISION_MICRO);
-       s16 x = node_min.X;
-       s16 y = node_min.Y - 1;
-       s16 z = node_min.Z;
-
-       noise_terrain_persist->perlinMap2D(x, z);
-       float *persistmap = noise_terrain_persist->result;
-
-       noise_terrain_base->perlinMap2D(x, z, persistmap);
-       noise_terrain_alt->perlinMap2D(x, z, persistmap);
-       noise_height_select->perlinMap2D(x, z);
-
-       if (spflags & MGV7_MOUNTAINS) {
-               noise_mountain->perlinMap3D(x, y, z);
-               noise_mount_height->perlinMap2D(x, z);
-       }
-
-       if ((spflags & MGV7_RIDGES) && node_max.Y >= water_level) {
-               noise_ridge->perlinMap3D(x, y, z);
-               noise_ridge_uwater->perlinMap2D(x, z);
-       }
-
-       // Cave noises are calculated in generateCaves()
-       // only if solid terrain is present in mapchunk
-
-       //printf("calculateNoise: %dus\n", t.stop());
-}
-
-
 float MapgenV7::baseTerrainLevelAtPoint(s16 x, s16 z)
 {
        float hselect = NoisePerlin2D(&noise_height_select->np, x, z, seed);
@@ -430,10 +396,23 @@ int MapgenV7::generateTerrain()
        MapNode n_stone(c_stone);
        MapNode n_water(c_water_source);
 
+       //// Calculate noise for terrain generation
+       noise_terrain_persist->perlinMap2D(node_min.X, node_min.Z);
+       float *persistmap = noise_terrain_persist->result;
+
+       noise_terrain_base->perlinMap2D(node_min.X, node_min.Z, persistmap);
+       noise_terrain_alt->perlinMap2D(node_min.X, node_min.Z, persistmap);
+       noise_height_select->perlinMap2D(node_min.X, node_min.Z);
+
+       if (spflags & MGV7_MOUNTAINS) {
+               noise_mountain->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z);
+               noise_mount_height->perlinMap2D(node_min.X, node_min.Z);
+       }
+
+       //// Place nodes
        v3s16 em = vm->m_area.getExtent();
        s16 stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT;
        u32 index2d = 0;
-       bool mountain_flag = spflags & MGV7_MOUNTAINS;
 
        for (s16 z = node_min.Z; z <= node_max.Z; z++)
        for (s16 x = node_min.X; x <= node_max.X; x++, index2d++) {
@@ -450,7 +429,7 @@ int MapgenV7::generateTerrain()
                        if (vm->m_data[vi].getContent() == CONTENT_IGNORE) {
                                if (y <= surface_y) {
                                        vm->m_data[vi] = n_stone;  // Base terrain
-                               } else if (mountain_flag &&
+                               } else if ((spflags & MGV7_MOUNTAINS) &&
                                                getMountainTerrainFromMap(index3d, index2d, y)) {
                                        vm->m_data[vi] = n_stone;  // Mountain terrain
                                        if (y > stone_surface_max_y)
@@ -475,6 +454,9 @@ void MapgenV7::generateRidgeTerrain()
        if (node_max.Y < water_level - 16)
                return;
 
+       noise_ridge->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z);
+       noise_ridge_uwater->perlinMap2D(node_min.X, node_min.Z);
+
        MapNode n_water(c_water_source);
        MapNode n_air(CONTENT_AIR);
        u32 index = 0;
index eb89d59782d688138c061715c2fb959ed4944618..723f1217f9abee9224f7b0c49fc2ddb5af235640 100644 (file)
@@ -87,9 +87,6 @@ public:
        float baseTerrainLevelFromMap(int index);
        bool getMountainTerrainAtPoint(s16 x, s16 y, s16 z);
        bool getMountainTerrainFromMap(int idx_xyz, int idx_xz, s16 y);
-
-       void calculateNoise();
-
        int generateTerrain();
        void generateRidgeTerrain();
 };
index 699554ecd6a594f0539a54938ad0c589b7c9d56b..3a4f984f9879e38ac015c8cd322d641d534a3fca 100644 (file)
@@ -388,7 +388,6 @@ void MapgenValleys::calculateNoise()
 
        //TimeTaker tcn("actualNoise");
 
-       noise_filler_depth->perlinMap2D(x, z);
        noise_inter_valley_slope->perlinMap2D(x, z);
        noise_rivers->perlinMap2D(x, z);
        noise_terrain_height->perlinMap2D(x, z);