]> git.mxchange.org Git - flightgear.git/commitdiff
Automatic tower positioning
authorThorstenB <brehmt@gmail.com>
Mon, 18 Apr 2011 20:54:33 +0000 (22:54 +0200)
committerThorstenB <brehmt@gmail.com>
Mon, 18 Apr 2011 20:54:33 +0000 (22:54 +0200)
New "auto-position" switch to always use closest tower for tower-view.
Exposes new "/sim/airport/closest-airport-id" property.
Display closest airport on groundradar by default.

src/Environment/environment_mgr.cxx
src/Environment/environment_mgr.hxx
src/Instrumentation/groundradar.cxx
src/Main/fg_init.cxx

index 3182f1159d24f95fe7579eb5077e5ff9e12cda20..40ca18f2052af7e3a713a1450535cd584548b5f6 100644 (file)
@@ -42,6 +42,7 @@
 #include "precipitation_mgr.hxx"
 #include "ridge_lift.hxx"
 #include "terrainsampler.hxx"
+#include "Airports/simple.hxx"
 
 class SGSky;
 extern SGSky *thesky;
@@ -49,8 +50,11 @@ extern SGSky *thesky;
 FGEnvironmentMgr::FGEnvironmentMgr () :
   _environment(new FGEnvironment()),
   fgClouds(new FGClouds()),
+  _cloudLayersDirty(true),
   _altitudeNode(fgGetNode("/position/altitude-ft", true)),
-  _cloudLayersDirty(true)
+  _longitude_n(fgGetNode( "/position/longitude-deg", true )),
+  _latitude_n( fgGetNode( "/position/latitude-deg", true )),
+  _positionTimeToLive(0.0)
 {
   set_subsystem("controller", Environment::LayerInterpolateController::createInstance( fgGetNode("/environment/config", true ) ));
   set_subsystem("realwx", Environment::RealWxController::createInstance( fgGetNode("/environment/realwx", true ) ), 1.0 );
@@ -111,73 +115,73 @@ FGEnvironmentMgr::bind ()
   _tiedProperties.setRoot( fgGetNode( "/environment", true ) );
 
   _tiedProperties.Tie( "effective-visibility-m", thesky,
-       &SGSky::get_visibility );
+          &SGSky::get_visibility );
 
   _tiedProperties.Tie("rebuild-layers", fgClouds,
-      &FGClouds::get_update_event,
-      &FGClouds::set_update_event);
+          &FGClouds::get_update_event,
+          &FGClouds::set_update_event);
 
   _tiedProperties.Tie("turbulence/use-cloud-turbulence", &sgEnviro,
-      &SGEnviro::get_turbulence_enable_state,
-      &SGEnviro::set_turbulence_enable_state);
+          &SGEnviro::get_turbulence_enable_state,
+          &SGEnviro::set_turbulence_enable_state);
 
   for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
-    SGPropertyNode_ptr layerNode = fgGetNode("/environment/clouds",true)->getChild("layer", i, true );
+      SGPropertyNode_ptr layerNode = fgGetNode("/environment/clouds",true)->getChild("layer", i, true );
 
-    _tiedProperties.Tie( layerNode->getNode("span-m",true), this, i, 
-         &FGEnvironmentMgr::get_cloud_layer_span_m,
-         &FGEnvironmentMgr::set_cloud_layer_span_m);
+      _tiedProperties.Tie( layerNode->getNode("span-m",true), this, i, 
+              &FGEnvironmentMgr::get_cloud_layer_span_m,
+              &FGEnvironmentMgr::set_cloud_layer_span_m);
 
-    _tiedProperties.Tie( layerNode->getNode("elevation-ft",true), this, i, 
-         &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
-         &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
+      _tiedProperties.Tie( layerNode->getNode("elevation-ft",true), this, i, 
+              &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
+              &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
 
-    _tiedProperties.Tie( layerNode->getNode("thickness-ft",true), this, i, 
-         &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
-         &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
+      _tiedProperties.Tie( layerNode->getNode("thickness-ft",true), this, i, 
+              &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
+              &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
 
-    _tiedProperties.Tie( layerNode->getNode("transition-ft",true), this, i, 
-         &FGEnvironmentMgr::get_cloud_layer_transition_ft,
-         &FGEnvironmentMgr::set_cloud_layer_transition_ft);
+      _tiedProperties.Tie( layerNode->getNode("transition-ft",true), this, i, 
+              &FGEnvironmentMgr::get_cloud_layer_transition_ft,
+              &FGEnvironmentMgr::set_cloud_layer_transition_ft);
 
-    _tiedProperties.Tie( layerNode->getNode("coverage",true), this, i, 
-         &FGEnvironmentMgr::get_cloud_layer_coverage,
-         &FGEnvironmentMgr::set_cloud_layer_coverage);
+      _tiedProperties.Tie( layerNode->getNode("coverage",true), this, i, 
+              &FGEnvironmentMgr::get_cloud_layer_coverage,
+              &FGEnvironmentMgr::set_cloud_layer_coverage);
 
-    _tiedProperties.Tie( layerNode->getNode("coverage-type",true), this, i, 
-         &FGEnvironmentMgr::get_cloud_layer_coverage_type,
-         &FGEnvironmentMgr::set_cloud_layer_coverage_type);
+      _tiedProperties.Tie( layerNode->getNode("coverage-type",true), this, i, 
+              &FGEnvironmentMgr::get_cloud_layer_coverage_type,
+              &FGEnvironmentMgr::set_cloud_layer_coverage_type);
 
-    _tiedProperties.Tie( layerNode->getNode( "visibility-m",true), this, i,
-        &FGEnvironmentMgr::get_cloud_layer_visibility_m,
-        &FGEnvironmentMgr::set_cloud_layer_visibility_m);
+      _tiedProperties.Tie( layerNode->getNode( "visibility-m",true), this, i,
+              &FGEnvironmentMgr::get_cloud_layer_visibility_m,
+              &FGEnvironmentMgr::set_cloud_layer_visibility_m);
 
-    _tiedProperties.Tie( layerNode->getNode( "alpha",true), this, i,
-        &FGEnvironmentMgr::get_cloud_layer_maxalpha,
-        &FGEnvironmentMgr::set_cloud_layer_maxalpha);
+      _tiedProperties.Tie( layerNode->getNode( "alpha",true), this, i,
+              &FGEnvironmentMgr::get_cloud_layer_maxalpha,
+              &FGEnvironmentMgr::set_cloud_layer_maxalpha);
   }
 
   _tiedProperties.setRoot( fgGetNode("/sim/rendering", true ) );
 
   _tiedProperties.Tie( "clouds3d-enable", fgClouds,
-         &FGClouds::get_3dClouds,
-         &FGClouds::set_3dClouds);
+          &FGClouds::get_3dClouds,
+          &FGClouds::set_3dClouds);
 
   _tiedProperties.Tie( "clouds3d-density", thesky,
-         &SGSky::get_3dCloudDensity,
-         &SGSky::set_3dCloudDensity);
+          &SGSky::get_3dCloudDensity,
+          &SGSky::set_3dCloudDensity);
 
   _tiedProperties.Tie("clouds3d-vis-range", thesky,
-        &SGSky::get_3dCloudVisRange,
-        &SGSky::set_3dCloudVisRange);
+          &SGSky::get_3dCloudVisRange,
+          &SGSky::set_3dCloudVisRange);
   
   _tiedProperties.Tie("precipitation-enable", &sgEnviro,
-         &SGEnviro::get_precipitation_enable_state, 
-         &SGEnviro::set_precipitation_enable_state);
+          &SGEnviro::get_precipitation_enable_state, 
+          &SGEnviro::set_precipitation_enable_state);
 
   _tiedProperties.Tie("lightning-enable", &sgEnviro,
-      &SGEnviro::get_lightning_enable_state,
-      &SGEnviro::set_lightning_enable_state);
+          &SGEnviro::get_lightning_enable_state,
+          &SGEnviro::set_lightning_enable_state);
 
   sgEnviro.config(fgGetNode("/sim/rendering/precipitation"));
 }
@@ -198,11 +202,38 @@ FGEnvironmentMgr::update (double dt)
   _environment->set_elevation_ft( _altitudeNode->getDoubleValue() );
 
   simgear::Particles::setWindFrom( _environment->get_wind_from_heading_deg(),
-                                  _environment->get_wind_speed_kt() );
+                                   _environment->get_wind_speed_kt() );
   if( _cloudLayersDirty ) {
     _cloudLayersDirty = false;
     fgClouds->set_update_event( fgClouds->get_update_event()+1 );
   }
+
+  _positionTimeToLive -= dt;
+  if( _positionTimeToLive <= 0.0 )
+  {
+      // update closest airport information
+      _positionTimeToLive = 30.0;
+
+      SG_LOG(SG_ALL, SG_INFO, "FGEnvironmentMgr::update: updating closest airport");
+
+      SGGeod pos = SGGeod::fromDeg(_longitude_n->getDoubleValue(),
+                                   _latitude_n->getDoubleValue());
+
+      FGAirport * nearestAirport = FGAirport::findClosest(pos, 100.0);
+      if( nearestAirport == NULL )
+      {
+          SG_LOG(SG_ALL,SG_WARN,"FGEnvironmentMgr::update: No airport within 100NM range");
+      }
+      else
+      {
+          const string currentId = fgGetString("/sim/airport/closest-airport-id", ""); 
+          if (currentId != nearestAirport->ident())
+          {
+              fgSetString("/sim/airport/closest-airport-id",
+                      nearestAirport->ident().c_str());
+          }
+      }
+  }
 }
 
 FGEnvironment
index e6041f81361b55068066356addd88c67d715db93..67d929ec8e90300b46c44362f7b77a6531c3e117 100644 (file)
@@ -94,8 +94,11 @@ private:
 
   FGEnvironment * _environment;        // always the same, for now
   FGClouds *fgClouds;
-  SGPropertyNode_ptr _altitudeNode;
   bool _cloudLayersDirty;
+  SGPropertyNode_ptr _altitudeNode;
+  SGPropertyNode_ptr _longitude_n;
+  SGPropertyNode_ptr _latitude_n;
+  double _positionTimeToLive;
   simgear::TiedPropertyList _tiedProperties;
 };
 
index ffa2e1fc945881d6f4278c49b4aa22f425e3b573..c604cc43c224121088d3f1ef53125fd5adc85c6e 100644 (file)
@@ -47,7 +47,7 @@
 #include "groundradar.hxx"
 
 static const char* airport_source_node_name = "airport-id-source";
-static const char* default_airport_node_name = "/sim/tower/airport-id";
+static const char* default_airport_node_name = "/sim/airport/closest-airport-id";
 static const char* texture_node_name = "texture-name";
 static const char* default_texture_name = "Aircraft/Instruments/Textures/od_groundradar.rgb";
 static const char* range_source_node_name = "range-source";
index 9eb8ae7a96b576906e9bbae5b1e87e907acc3332..50329b39f48506467809f5d548a0face5ee1e64e 100644 (file)
@@ -793,14 +793,44 @@ static bool fgSetTowerPosFromAirportID( const string& id) {
 
 struct FGTowerLocationListener : SGPropertyChangeListener {
     void valueChanged(SGPropertyNode* node) {
-        const string id(node->getStringValue());
+        string id(node->getStringValue());
+        if (fgGetBool("/sim/tower/auto-position",true))
+        {
+            // enforce using closest airport when auto-positioning is enabled 
+            const char* closest_airport = fgGetString("/sim/airport/closest-airport-id", "");
+            if (closest_airport && (id != closest_airport))
+            {
+                id = closest_airport;
+                node->setStringValue(id);
+            }
+        }
         fgSetTowerPosFromAirportID(id);
     }
 };
 
+struct FGClosestTowerLocationListener : SGPropertyChangeListener
+{
+    void valueChanged(SGPropertyNode* )
+    {
+        // closest airport has changed
+        if (fgGetBool("/sim/tower/auto-position",true))
+        {
+            // update tower position
+            const char* id = fgGetString("/sim/airport/closest-airport-id", "");
+            if (id && *id!=0)
+                fgSetString("/sim/tower/airport-id", id);
+        }
+    }
+};
+
 void fgInitTowerLocationListener() {
     fgGetNode("/sim/tower/airport-id",  true)
         ->addChangeListener( new FGTowerLocationListener(), true );
+    FGClosestTowerLocationListener* ntcl = new FGClosestTowerLocationListener();
+    fgGetNode("/sim/airport/closest-airport-id", true)
+        ->addChangeListener(ntcl , true );
+    fgGetNode("/sim/tower/auto-position", true)
+           ->addChangeListener(ntcl, true );
 }
 
 static void fgApplyStartOffset(const SGGeod& aStartPos, double aHeading, double aTargetHeading = HUGE_VAL)
@@ -1163,6 +1193,7 @@ bool fgInitPosition() {
         // An airport + parking position is requested
         if ( fgSetPosFromAirportIDandParkpos( apt, parkpos ) ) {
             // set tower position
+            fgSetString("/sim/airport/closest-airport-id",  apt.c_str());
             fgSetString("/sim/tower/airport-id",  apt.c_str());
             set_pos = true;
         }
@@ -1173,7 +1204,8 @@ bool fgInitPosition() {
         if ( fgSetPosFromAirportIDandRwy( apt, rwy_no, rwy_req ) ) {
             // set tower position (a little off the heading for single
             // runway airports)
-           fgSetString("/sim/tower/airport-id",  apt.c_str());
+            fgSetString("/sim/airport/closest-airport-id",  apt.c_str());
+            fgSetString("/sim/tower/airport-id",  apt.c_str());
             set_pos = true;
         }
     }
@@ -1183,7 +1215,8 @@ bool fgInitPosition() {
         if ( fgSetPosFromAirportIDandHdg( apt, hdg ) ) {
             // set tower position (a little off the heading for single
             // runway airports)
-           fgSetString("/sim/tower/airport-id",  apt.c_str());
+            fgSetString("/sim/airport/closest-airport-id",  apt.c_str());
+            fgSetString("/sim/tower/airport-id",  apt.c_str());
             set_pos = true;
         }
     }