Skip to content

Commit

Permalink
Mapgen: Remove calculateNoise from most mapgens
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kwolekr committed May 28, 2016
1 parent c596804 commit 0df5c01
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 111 deletions.
2 changes: 2 additions & 0 deletions src/mapgen.cpp
Expand Up @@ -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;
Expand Down
30 changes: 5 additions & 25 deletions src/mapgen_flat.cpp
Expand Up @@ -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();

Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/mapgen_flat.h
Expand Up @@ -78,7 +78,6 @@ class MapgenFlat : public MapgenBasic {

virtual void makeChunk(BlockMakeData *data);
int getSpawnLevelAtPoint(v2s16 p);
void calculateNoise();
s16 generateTerrain();
};

Expand Down
22 changes: 2 additions & 20 deletions src/mapgen_fractal.cpp
Expand Up @@ -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();

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion src/mapgen_fractal.h
Expand Up @@ -88,7 +88,6 @@ class MapgenFractal : public MapgenBasic {

virtual void makeChunk(BlockMakeData *data);
int getSpawnLevelAtPoint(v2s16 p);
void calculateNoise();
bool getFractalAtPoint(s16 x, s16 y, s16 z);
s16 generateTerrain();
};
Expand Down
27 changes: 4 additions & 23 deletions src/mapgen_v5.cpp
Expand Up @@ -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();

Expand Down Expand Up @@ -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]);
Expand All @@ -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);
Expand Down
1 change: 0 additions & 1 deletion src/mapgen_v5.h
Expand Up @@ -69,7 +69,6 @@ class MapgenV5 : public MapgenBasic {

virtual void makeChunk(BlockMakeData *data);
int getSpawnLevelAtPoint(v2s16 p);
void calculateNoise();
int generateBaseTerrain();
};

Expand Down
54 changes: 18 additions & 36 deletions src/mapgen_v7.cpp
Expand Up @@ -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();

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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++) {
Expand All @@ -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)
Expand All @@ -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;
Expand Down
3 changes: 0 additions & 3 deletions src/mapgen_v7.h
Expand Up @@ -87,9 +87,6 @@ class MapgenV7 : public MapgenBasic {
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();
};
Expand Down
1 change: 0 additions & 1 deletion src/mapgen_valleys.cpp
Expand Up @@ -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);
Expand Down

0 comments on commit 0df5c01

Please sign in to comment.