]> git.mxchange.org Git - flightgear.git/blobdiff - src/Environment/environment_ctrl.cxx
fix a pointer reference.
[flightgear.git] / src / Environment / environment_ctrl.cxx
index 8e3f908e785ea5f3fae3ebc8bb1279a1fd4a857f..5b9cfcfb99744b5e0a72843dbb252f57ab1518d8 100644 (file)
@@ -141,9 +141,9 @@ FGInterpolateEnvironmentCtrl::read_table (const SGPropertyNode * node, vector<bu
 {
        double last_altitude_ft = 0.0;
        double sort_required = false;
-       int i;
+       size_t i;
 
-       for (i = 0; i < node->nChildren(); i++) {
+       for (i = 0; i < (size_t)node->nChildren(); i++) {
                const SGPropertyNode * child = node->getChild(i);
                if ( strcmp(child->getName(), "entry") == 0
                 && child->getStringValue("elevation-ft", "")[0] != '\0'
@@ -170,9 +170,11 @@ FGInterpolateEnvironmentCtrl::read_table (const SGPropertyNode * node, vector<bu
                }
        }
        // remove leftover buckets
-       vector<bucket*>::iterator it = table.begin() + i;
-       while( it != table.end() )
-               table.erase( it );
+       while( table.size() > i ) {
+               bucket * b = *(table.end() - 1);
+               delete b;
+               table.pop_back();
+       }
 
        if( sort_required )
                sort(table.begin(), table.end(), bucket::lessThan);
@@ -262,8 +264,7 @@ FGInterpolateEnvironmentCtrl::bucket::lessThan(bucket *a, bucket *b)
 ////////////////////////////////////////////////////////////////////////
 
 FGMetarCtrl::FGMetarCtrl( SGSubsystem * environmentCtrl )
-       : _environmentCtrl(environmentCtrl),
-       station_elevation_ft(0.0),
+       :
        metar_valid(false),
        setup_winds_aloft(true),
        wind_interpolation_required(true),
@@ -275,7 +276,8 @@ FGMetarCtrl::FGMetarCtrl( SGSubsystem * environmentCtrl )
        MaxCloudAltitudeChangeFtSec( 20.0 ),
        MaxCloudThicknessChangeFtSec( 50.0 ),
        MaxCloudInterpolationHeightFt( 5000.0 ),
-       MaxCloudInterpolationDeltaFt( 4000.0 )
+       MaxCloudInterpolationDeltaFt( 4000.0 ),
+       _environmentCtrl(environmentCtrl)
 {
        windModulator = new FGBasicWindModulator();
 
@@ -298,6 +300,7 @@ FGMetarCtrl::FGMetarCtrl( SGSubsystem * environmentCtrl )
        hail_n = metar_base_n->getNode("hail-norm", true );
        snow_n = metar_base_n->getNode("snow-norm", true );
        snow_cover_n = metar_base_n->getNode("snow-cover", true );
+       magnetic_variation_n = fgGetNode( "/environment/magnetic-variation-deg", true );
        ground_elevation_n = fgGetNode( "/position/ground-elev-m", true );
        longitude_n = fgGetNode( "/position/longitude-deg", true );
        latitude_n = fgGetNode( "/position/latitude-deg", true );
@@ -388,11 +391,9 @@ static void setupWindBranch( string branchName, double dir, double speed, double
        }
 }
 
-static void setupWind( bool setup_boundary, bool setup_aloft, double dir, double speed, double gust )
+static void setupWind( bool setup_aloft, double dir, double speed, double gust )
 {
-       if( setup_boundary )
-               setupWindBranch( "boundary", dir, speed, gust );
-
+       setupWindBranch( "boundary", dir, speed, gust );
        if( setup_aloft )
                setupWindBranch( "aloft", dir, speed, gust );
 }
@@ -445,10 +446,10 @@ FGMetarCtrl::update(double dt)
        bool layer_rebuild_required = false;
 
        if (first_update) {
-               double dir = base_wind_dir_n->getDoubleValue();
+               double dir = base_wind_dir_n->getDoubleValue()+magnetic_variation_n->getDoubleValue();
                double speed = base_wind_speed_n->getDoubleValue();
                double gust = gust_wind_speed_n->getDoubleValue();
-               setupWind(true, setup_winds_aloft, dir, speed, gust);
+               setupWind(setup_winds_aloft, dir, speed, gust);
 
                double metarvis = min_visibility_n->getDoubleValue();
                fgDefaultWeatherValue("visibility-m", metarvis);
@@ -486,7 +487,7 @@ FGMetarCtrl::update(double dt)
                        // Pick up the METAR wind values and convert them into a vector.
                        double metar[2];
                        double metar_speed = base_wind_speed_n->getDoubleValue();
-                       double metar_heading = base_wind_dir_n->getDoubleValue();
+                       double metar_heading = base_wind_dir_n->getDoubleValue()+magnetic_variation_n->getDoubleValue();
 
                        metar[0] = metar_speed * sin(metar_heading * SG_DEGREES_TO_RADIANS );
                        metar[1] = metar_speed * cos(metar_heading * SG_DEGREES_TO_RADIANS);
@@ -518,10 +519,7 @@ FGMetarCtrl::update(double dt)
                                current[1] = interpolate_val(current[1], metar[1], maxdy);
 
                                // Now convert back to polar coordinates.
-                               if ((current[0] == 0.0) && (current[1] == 0.0)) {
-                                       // Special case where there is no wind (otherwise atan2 barfs)
-                                       speed = 0.0;
-                               } else {
+                               if ((fabs(current[0]) > 0.1) || (fabs(current[1]) > 0.1)) {
                                        // Some real wind to convert back from. Work out the speed
                                        // and direction value in degrees.
                                        speed = sqrt((current[0] * current[0]) + (current[1] * current[1]));
@@ -532,9 +530,12 @@ FGMetarCtrl::update(double dt)
                                                dir_from += 360.0;
 
                                        SG_LOG( SG_GENERAL, SG_DEBUG, "Wind : " << dir_from << "@" << speed);
+                               } else {
+                                       // Special case where there is no wind (otherwise atan2 barfs)
+                                       speed = 0.0;
                                }
                                double gust = gust_wind_speed_n->getDoubleValue();
-                               setupWind(true, setup_winds_aloft, dir_from, speed, gust);
+                               setupWind(setup_winds_aloft, dir_from, speed, gust);
                                reinit_required = true;
                        } else { 
                                wind_interpolation_required = false;
@@ -543,16 +544,20 @@ FGMetarCtrl::update(double dt)
                        // interpolation of wind vector is finished, apply wind
                        // variations and gusts for the boundary layer only
 
+
+                       bool wind_modulated = false;
+
                        // start with the main wind direction
-                       double wind_dir = base_wind_dir_n->getDoubleValue();
-                       double min = convert_to_180(base_wind_range_from_n->getDoubleValue());
-                       double max = convert_to_180(base_wind_range_to_n->getDoubleValue());
+                       double wind_dir = base_wind_dir_n->getDoubleValue()+magnetic_variation_n->getDoubleValue();
+                       double min = convert_to_180(base_wind_range_from_n->getDoubleValue()+magnetic_variation_n->getDoubleValue());
+                       double max = convert_to_180(base_wind_range_to_n->getDoubleValue()+magnetic_variation_n->getDoubleValue());
                        if( max > min ) {
                                // if variable winds configured, modulate the wind direction
                                double f = windModulator->get_direction_offset_norm();
                                wind_dir = min+(max-min)*f;
                                double old = convert_to_180(boundary_wind_from_heading_n->getDoubleValue());
                                wind_dir = convert_to_360(fgGetLowPass(old, wind_dir, dt ));
+                               wind_modulated = true;
                        }
                        
                        // start with main wind speed
@@ -563,9 +568,12 @@ FGMetarCtrl::update(double dt)
                                double f = windModulator->get_magnitude_factor_norm();
                                wind_speed = wind_speed+(max-wind_speed)*f;
                                wind_speed = fgGetLowPass(boundary_wind_speed_n->getDoubleValue(), wind_speed, dt );
+                               wind_modulated = true;
+                       }
+                       if( wind_modulated ) {
+                               setupWind(false, wind_dir, wind_speed, max);
+                               reinit_required = true;
                        }
-                       setupWind(true, false, wind_dir, wind_speed, max);
-                       reinit_required = true;
                }
 
                // Now handle the visibility. We convert both visibility values
@@ -658,9 +666,11 @@ FGMetarCtrl::update(double dt)
                        }
                }
        }
-
-       set_temp_at_altitude(temperature_n->getDoubleValue(), station_elevation_ft);
-       set_dewpoint_at_altitude(dewpoint_n->getDoubleValue(), station_elevation_ft);
+       {
+               double station_elevation_ft = station_elevation_n->getDoubleValue();
+               set_temp_at_altitude(temperature_n->getDoubleValue(), station_elevation_ft);
+               set_dewpoint_at_altitude(dewpoint_n->getDoubleValue(), station_elevation_ft);
+       }
        //TODO: check if temperature/dewpoint have changed. This requires reinit.
 
        // Force an update of the 3D clouds
@@ -815,7 +825,7 @@ void MetarThread::run()
                        break;
                }
 
-                       metar_fetcher->fetch( airport_id );
+               metar_fetcher->fetch( airport_id );
        }
 }
 #endif
@@ -828,7 +838,8 @@ FGMetarFetcher::FGMetarFetcher() :
        search_timer(0.0),
        error_timer(0.0),
        _stale_count(0),
-       _error_count(0)
+       _error_count(0),
+       enabled(false)
 {
        longitude_n = fgGetNode( "/position/longitude-deg", true );
        latitude_n  = fgGetNode( "/position/latitude-deg", true );
@@ -866,6 +877,22 @@ void FGMetarFetcher::init ()
        _stale_count = 0;
        _error_count = 0;
        current_airport_id.clear();
+       /* Torsten Dreyer:
+          hack to stop startup.nas complaining if metar arrives after nasal-dir-initialized
+          is fired. Immediately fetch and wait for the METAR before continuing. This gets the
+          /environment/metar/xxx properties filled before nasal-dir is initialized.
+          Maybe the runway selection should happen here to make startup.nas obsolete?
+       */
+       const char * startup_airport = fgGetString("/sim/startup/options/airport");
+       if( *startup_airport ) {
+               FGAirport * a = FGAirport::getByIdent( startup_airport );
+               if( a ) {
+                       SGGeod pos = SGGeod::fromDeg(a->getLongitude(), a->getLatitude());
+                       a = FGAirport::findClosest(pos, 10000.0, &airportWithMetarFilter);
+                       current_airport_id = a->getId();
+                       fetch( current_airport_id );
+               }
+       }
 }
 
 void FGMetarFetcher::reinit ()
@@ -893,8 +920,19 @@ void FGMetarFetcher::update (double delta_time_sec)
                _error_count = 0;
        }
 
-       if( enable_n->getBoolValue() == false ) 
+       if( enable_n->getBoolValue() == false ) {
+               enabled = false;
                return;
+       }
+
+       // we were just enabled, reset all timers to 
+       // trigger immediate metar fetch
+       if( !enabled ) {
+               search_timer = 0.0;
+               fetch_timer = 0.0;
+               error_timer = error_timer_sec;
+               enabled = true;
+       }
 
        FGAirport * a = NULL;
 
@@ -925,6 +963,9 @@ void FGMetarFetcher::update (double delta_time_sec)
 
 void FGMetarFetcher::fetch( const string & id )
 {
+       if( enable_n->getBoolValue() == false ) 
+               return;
+
        SGSharedPtr<FGMetar> result = NULL;
 
        // fetch current metar data
@@ -946,11 +987,16 @@ void FGMetarFetcher::fetch( const string & id )
                        }
                } else {
                        _stale_count = 0;
-                       }
+               }
 
        } catch (const sg_io_exception& e) {
                SG_LOG( SG_GENERAL, SG_WARN, "Error fetching live weather data: " << e.getFormattedMessage().c_str() );
                result = NULL;
+               // remove METAR flag from the airport
+               FGAirport * a = FGAirport::findByIdent( id );
+               if( a ) a->setMetar( false );
+               // immediately schedule a new search
+               search_timer = 0.0;
        }
 
        // write the metar to the property node, the rest is done by the methods tied to this property