+
+
+// modify the current visibility based on cloud layers, thickness,
+// transition range, and simulated "puffs".
+void SGSky::modify_vis( float alt, float time_factor ) {
+ float effvis = visibility;
+
+ for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
+ float asl = cloud_layers[i]->get_asl();
+ float thickness = cloud_layers[i]->get_thickness();
+ float transition = cloud_layers[i]->get_transition();
+
+ double ratio = 1.0;
+
+ if ( alt < asl - transition ) {
+ // below cloud layer
+ ratio = 1.0;
+ } else if ( alt < asl ) {
+ // in lower transition
+ ratio = (asl - alt) / transition;
+ } else if ( alt < asl + thickness ) {
+ // in cloud layer
+ ratio = 0.0;
+ } else if ( alt < asl + thickness + transition ) {
+ // in upper transition
+ ratio = (alt - (asl + thickness)) / transition;
+ } else {
+ // above cloud layer
+ ratio = 1.0;
+ }
+
+ // accumulate effects from multiple cloud layers
+ effvis *= ratio;
+
+ if ( ratio < 1.0 ) {
+ if ( ! in_puff ) {
+ // calc chance of entering cloud puff
+ double rnd = fg_random();
+ double chance = rnd * rnd * rnd;
+ if ( chance > 0.95 /* * (diff - 25) / 50.0 */ ) {
+ in_puff = true;
+ puff_length = fg_random() * 2.0; // up to 2 seconds
+ puff_progression = 0.0;
+ }
+ }
+
+ if ( in_puff ) {
+ // modify actual_visibility based on puff envelope
+
+ if ( puff_progression <= ramp_up ) {
+ double x = 0.5 * SGD_PI * puff_progression / ramp_up;
+ double factor = 1.0 - sin( x );
+ // cout << "ramp up = " << puff_progression
+ // << " factor = " << factor << endl;
+ effvis = effvis * factor;
+ } else if ( puff_progression >= ramp_up + puff_length ) {
+ double x = 0.5 * SGD_PI *
+ (puff_progression - (ramp_up + puff_length)) /
+ ramp_down;
+ double factor = sin( x );
+ // cout << "ramp down = "
+ // << puff_progression - (ramp_up + puff_length)
+ // << " factor = " << factor << endl;
+ effvis = effvis * factor;
+ } else {
+ effvis = 0.0;
+ }
+
+ /* cout << "len = " << puff_length
+ << " x = " << x
+ << " factor = " << factor
+ << " actual_visibility = " << actual_visibility
+ << endl; */
+
+ // time_factor = ( global_multi_loop *
+ // current_options.get_speed_up() ) /
+ // (double)current_options.get_model_hz();
+
+ puff_progression += time_factor;
+ // cout << "time factor = " << time_factor << endl;
+
+ /* cout << "gml = " << global_multi_loop
+ << " speed up = " << current_options.get_speed_up()
+ << " hz = " << current_options.get_model_hz() << endl;
+ */
+
+ if ( puff_progression > puff_length + ramp_up + ramp_down) {
+ in_puff = false;
+ }
+ }
+
+ // never let visibility drop below 25 meters
+ if ( effvis <= 25.0 ) {
+ effvis = 25.0;
+ }
+ }
+ } // for
+
+ effective_visibility = effvis;
+}
+