diff --git a/src/mapgen_flat.cpp b/src/mapgen_flat.cpp
index d654a772e25fa5334fee1c553c52d0147af06649..7a5302df0b42825b003ab2dce988265216b4f044 100644
--- a/src/mapgen_flat.cpp
+++ b/src/mapgen_flat.cpp
@@ -565,6 +565,7 @@ void MapgenFlat::generateCaves(s16 max_stone_y)
 	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
+		bool is_tunnel = false;  // Is tunnel or tunnel floor
 		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);
@@ -591,13 +592,16 @@ void MapgenFlat::generateCaves(s16 max_stone_y)
 			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 &&
+				is_tunnel = true;
+			} else if (is_tunnel && 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;
+				is_tunnel = false;
 			} else {
 				column_is_open = false;
+				is_tunnel = false;
 			}
 		}
 	}
diff --git a/src/mapgen_fractal.cpp b/src/mapgen_fractal.cpp
index cdea9c09af854e0e4ba79ed186db1e982c8bcb59..0430f92ff6981d58b98a55d5cad3cf0a9c9bcdde 100644
--- a/src/mapgen_fractal.cpp
+++ b/src/mapgen_fractal.cpp
@@ -693,6 +693,7 @@ void MapgenFractal::generateCaves(s16 max_stone_y)
 	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
+		bool is_tunnel = false;  // Is tunnel or tunnel floor
 		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);
@@ -719,13 +720,16 @@ void MapgenFractal::generateCaves(s16 max_stone_y)
 			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 &&
+				is_tunnel = true;
+			} else if (is_tunnel && 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;
+				is_tunnel = false;
 			} else {
 				column_is_open = false;
+				is_tunnel = false;
 			}
 		}
 	}
diff --git a/src/mapgen_v7.cpp b/src/mapgen_v7.cpp
index 9e3e3f1a8699e60776e1106a1d223b3357fb9ea1..db13e3ce6eadecd247cbe01fbf2378032a7c2e02 100644
--- a/src/mapgen_v7.cpp
+++ b/src/mapgen_v7.cpp
@@ -875,6 +875,7 @@ void MapgenV7::generateCaves(s16 max_stone_y)
 	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
+		bool is_tunnel = false;  // Is tunnel or tunnel floor
 		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);
@@ -901,13 +902,16 @@ void MapgenV7::generateCaves(s16 max_stone_y)
 			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 &&
+				is_tunnel = true;
+			} else if (is_tunnel && 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;
+				is_tunnel = false;
 			} else {
 				column_is_open = false;
+				is_tunnel = false;
 			}
 		}
 	}