Skip to content

Commit

Permalink
Mgv5/v7/flat/fractal: Move tunnel noise calculation into generateCaves
Browse files Browse the repository at this point in the history
Tunnel 3D noises are only calculated when solid terrain is present
in mapchunk, avoiding large amounts of unnecessary calculations
Change 'int' to 's16' in calculateNoise
Change 'i' to 'vi' for voxelmanip indexes for consistency
Keep 'u32 index3d' local to a smaller part of tunnel code
Mgv7: Don't call CaveV7 if no solid terrain in mapchunk
Give 'open' bool a more descriptive name
  • Loading branch information
paramat committed Feb 2, 2016
1 parent 80c7612 commit 0a8af88
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 166 deletions.
88 changes: 44 additions & 44 deletions src/mapgen_flat.cpp
Expand Up @@ -323,20 +323,16 @@ void MapgenFlat::makeChunk(BlockMakeData *data)
void MapgenFlat::calculateNoise()
{
//TimeTaker t("calculateNoise", NULL, PRECISION_MICRO);
int x = node_min.X;
int y = node_min.Y - 1;
int z = node_min.Z;
s16 x = node_min.X;
s16 z = node_min.Z;

if ((spflags & MGFLAT_LAKES) || (spflags & MGFLAT_HILLS))
noise_terrain->perlinMap2D(x, z);

noise_filler_depth->perlinMap2D(x, z);

if (flags & MG_CAVES) {
noise_cave1->perlinMap3D(x, y, z);
noise_cave2->perlinMap3D(x, y, z);
}
// Cave noises are calculated in generateCaves()
// only if solid terrain is present in mapchunk

noise_filler_depth->perlinMap2D(x, z);
noise_heat->perlinMap2D(x, z);
noise_humidity->perlinMap2D(x, z);
noise_heat_blend->perlinMap2D(x, z);
Expand Down Expand Up @@ -550,41 +546,45 @@ void MapgenFlat::dustTopNodes()

void MapgenFlat::generateCaves(s16 max_stone_y)
{
if (max_stone_y >= node_min.Y) {
v3s16 em = vm->m_area.getExtent();
u32 index2d = 0;
u32 index3d;

for (s16 z = node_min.Z; z <= node_max.Z; z++)
for (s16 x = node_min.X; x <= node_max.X; x++, index2d++) {
bool open = false; // Is column open to overground
u32 vi = vm->m_area.index(x, node_max.Y + 1, z);
index3d = (z - node_min.Z) * zstride + (csize.Y + 1) * ystride +
(x - node_min.X);
// Biome of column
Biome *biome = (Biome *)bmgr->getRaw(biomemap[index2d]);

for (s16 y = node_max.Y + 1; y >= node_min.Y - 1;
y--, index3d -= ystride, vm->m_area.add_y(em, vi, -1)) {
content_t c = vm->m_data[vi].getContent();
if (c == CONTENT_AIR || c == biome->c_water_top ||
c == biome->c_water) {
open = true;
continue;
}
// Ground
float d1 = contour(noise_cave1->result[index3d]);
float d2 = contour(noise_cave2->result[index3d]);
if (d1 * d2 > 0.3f && ndef->get(c).is_ground_content) {
// In tunnel and ground content, excavate
vm->m_data[vi] = MapNode(CONTENT_AIR);
} else if (open && (c == biome->c_filler || c == biome->c_stone)) {
// Tunnel entrance floor
vm->m_data[vi] = MapNode(biome->c_top);
open = false;
} else {
open = false;
}
if (max_stone_y < node_min.Y)
return;

noise_cave1->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z);
noise_cave2->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z);

v3s16 em = vm->m_area.getExtent();
u32 index2d = 0;

for (s16 z = node_min.Z; z <= node_max.Z; z++)
for (s16 x = node_min.X; x <= node_max.X; x++, index2d++) {
bool column_is_open = false; // Is column open to overground
u32 vi = vm->m_area.index(x, node_max.Y + 1, z);
u32 index3d = (z - node_min.Z) * zstride + (csize.Y + 1) * ystride +
(x - node_min.X);
// Biome of column
Biome *biome = (Biome *)bmgr->getRaw(biomemap[index2d]);

for (s16 y = node_max.Y + 1; y >= node_min.Y - 1;
y--, index3d -= ystride, vm->m_area.add_y(em, vi, -1)) {
content_t c = vm->m_data[vi].getContent();
if (c == CONTENT_AIR || c == biome->c_water_top ||
c == biome->c_water) {
column_is_open = true;
continue;
}
// Ground
float d1 = contour(noise_cave1->result[index3d]);
float d2 = contour(noise_cave2->result[index3d]);
if (d1 * d2 > 0.3f && ndef->get(c).is_ground_content) {
// In tunnel and ground content, excavate
vm->m_data[vi] = MapNode(CONTENT_AIR);
} else if (column_is_open &&
(c == biome->c_filler || c == biome->c_stone)) {
// Tunnel entrance floor
vm->m_data[vi] = MapNode(biome->c_top);
column_is_open = false;
} else {
column_is_open = false;
}
}
}
Expand Down
87 changes: 44 additions & 43 deletions src/mapgen_fractal.cpp
Expand Up @@ -339,18 +339,15 @@ void MapgenFractal::makeChunk(BlockMakeData *data)
void MapgenFractal::calculateNoise()
{
//TimeTaker t("calculateNoise", NULL, PRECISION_MICRO);
int x = node_min.X;
int y = node_min.Y - 1;
int z = node_min.Z;
s16 x = node_min.X;
s16 z = node_min.Z;

noise_seabed->perlinMap2D(x, z);
noise_filler_depth->perlinMap2D(x, z);

if (flags & MG_CAVES) {
noise_cave1->perlinMap3D(x, y, z);
noise_cave2->perlinMap3D(x, y, z);
}
// Cave noises are calculated in generateCaves()
// only if solid terrain is present in mapchunk

noise_filler_depth->perlinMap2D(x, z);
noise_heat->perlinMap2D(x, z);
noise_humidity->perlinMap2D(x, z);
noise_heat_blend->perlinMap2D(x, z);
Expand Down Expand Up @@ -673,41 +670,45 @@ void MapgenFractal::dustTopNodes()

void MapgenFractal::generateCaves(s16 max_stone_y)
{
if (max_stone_y >= node_min.Y) {
v3s16 em = vm->m_area.getExtent();
u32 index2d = 0;
u32 index3d;

for (s16 z = node_min.Z; z <= node_max.Z; z++)
for (s16 x = node_min.X; x <= node_max.X; x++, index2d++) {
bool open = false; // Is column open to overground
u32 vi = vm->m_area.index(x, node_max.Y + 1, z);
index3d = (z - node_min.Z) * zstride + (csize.Y + 1) * ystride +
(x - node_min.X);
// Biome of column
Biome *biome = (Biome *)bmgr->getRaw(biomemap[index2d]);

for (s16 y = node_max.Y + 1; y >= node_min.Y - 1;
y--, index3d -= ystride, vm->m_area.add_y(em, vi, -1)) {
content_t c = vm->m_data[vi].getContent();
if (c == CONTENT_AIR || c == biome->c_water_top ||
c == biome->c_water) {
open = true;
continue;
}
// Ground
float d1 = contour(noise_cave1->result[index3d]);
float d2 = contour(noise_cave2->result[index3d]);
if (d1 * d2 > 0.3f && ndef->get(c).is_ground_content) {
// In tunnel and ground content, excavate
vm->m_data[vi] = MapNode(CONTENT_AIR);
} else if (open && (c == biome->c_filler || c == biome->c_stone)) {
// Tunnel entrance floor
vm->m_data[vi] = MapNode(biome->c_top);
open = false;
} else {
open = false;
}
if (max_stone_y < node_min.Y)
return;

noise_cave1->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z);
noise_cave2->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z);

v3s16 em = vm->m_area.getExtent();
u32 index2d = 0;

for (s16 z = node_min.Z; z <= node_max.Z; z++)
for (s16 x = node_min.X; x <= node_max.X; x++, index2d++) {
bool column_is_open = false; // Is column open to overground
u32 vi = vm->m_area.index(x, node_max.Y + 1, z);
u32 index3d = (z - node_min.Z) * zstride + (csize.Y + 1) * ystride +
(x - node_min.X);
// Biome of column
Biome *biome = (Biome *)bmgr->getRaw(biomemap[index2d]);

for (s16 y = node_max.Y + 1; y >= node_min.Y - 1;
y--, index3d -= ystride, vm->m_area.add_y(em, vi, -1)) {
content_t c = vm->m_data[vi].getContent();
if (c == CONTENT_AIR || c == biome->c_water_top ||
c == biome->c_water) {
column_is_open = true;
continue;
}
// Ground
float d1 = contour(noise_cave1->result[index3d]);
float d2 = contour(noise_cave2->result[index3d]);
if (d1 * d2 > 0.3f && ndef->get(c).is_ground_content) {
// In tunnel and ground content, excavate
vm->m_data[vi] = MapNode(CONTENT_AIR);
} else if (column_is_open &&
(c == biome->c_filler || c == biome->c_stone)) {
// Tunnel entrance floor
vm->m_data[vi] = MapNode(biome->c_top);
column_is_open = false;
} else {
column_is_open = false;
}
}
}
Expand Down
60 changes: 31 additions & 29 deletions src/mapgen_v5.cpp
Expand Up @@ -322,18 +322,16 @@ void MapgenV5::makeChunk(BlockMakeData *data)
void MapgenV5::calculateNoise()
{
//TimeTaker t("calculateNoise", NULL, PRECISION_MICRO);
int x = node_min.X;
int y = node_min.Y - 1;
int z = node_min.Z;
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);

if (flags & MG_CAVES) {
noise_cave1->perlinMap3D(x, y, z);
noise_cave2->perlinMap3D(x, y, z);
}
// Cave noises are calculated in generateCaves()
// only if solid terrain is present in mapchunk

noise_filler_depth->perlinMap2D(x, z);
noise_heat->perlinMap2D(x, z);
Expand Down Expand Up @@ -377,9 +375,9 @@ int MapgenV5::generateBaseTerrain()

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 i = vm->m_area.index(node_min.X, y, z);
for (s16 x=node_min.X; x<=node_max.X; x++, i++, index++, index2d++) {
if (vm->m_data[i].getContent() != CONTENT_IGNORE)
u32 vi = vm->m_area.index(node_min.X, y, z);
for (s16 x=node_min.X; x<=node_max.X; x++, vi++, index++, index2d++) {
if (vm->m_data[vi].getContent() != CONTENT_IGNORE)
continue;

float f = 0.55 + noise_factor->result[index2d];
Expand All @@ -391,11 +389,11 @@ int MapgenV5::generateBaseTerrain()

if (noise_ground->result[index] * f < y - h) {
if (y <= water_level)
vm->m_data[i] = MapNode(c_water_source);
vm->m_data[vi] = MapNode(c_water_source);
else
vm->m_data[i] = MapNode(CONTENT_AIR);
vm->m_data[vi] = MapNode(CONTENT_AIR);
} else {
vm->m_data[i] = MapNode(c_stone);
vm->m_data[vi] = MapNode(c_stone);
if (y > stone_surface_max_y)
stone_surface_max_y = y;
}
Expand Down Expand Up @@ -508,22 +506,26 @@ MgStoneType MapgenV5::generateBiomes(float *heat_map, float *humidity_map)

void MapgenV5::generateCaves(int max_stone_y)
{
if (max_stone_y >= node_min.Y) {
u32 index = 0;

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 i = vm->m_area.index(node_min.X, y, z);
for (s16 x = node_min.X; x <= node_max.X; x++, i++, index++) {
float d1 = contour(noise_cave1->result[index]);
float d2 = contour(noise_cave2->result[index]);
if (d1 * d2 > 0.125f) {
content_t c = vm->m_data[i].getContent();
if (!ndef->get(c).is_ground_content || c == CONTENT_AIR)
continue;

vm->m_data[i] = MapNode(CONTENT_AIR);
}
if (max_stone_y < node_min.Y)
return;

noise_cave1->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z);
noise_cave2->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z);

u32 index = 0;

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);
for (s16 x = node_min.X; x <= node_max.X; x++, vi++, index++) {
float d1 = contour(noise_cave1->result[index]);
float d2 = contour(noise_cave2->result[index]);
if (d1 * d2 > 0.125f) {
content_t c = vm->m_data[vi].getContent();
if (!ndef->get(c).is_ground_content || c == CONTENT_AIR)
continue;

vm->m_data[vi] = MapNode(CONTENT_AIR);
}
}
}
Expand Down

0 comments on commit 0a8af88

Please sign in to comment.