]> git.mxchange.org Git - flightgear.git/blobdiff - src/Main/positioninit.cxx
Crash-fix: mat-lib is now reference-counted.
[flightgear.git] / src / Main / positioninit.cxx
index eb35799679e013dc0a09cf7a90642fe982603245..cd5130db6abbd10f10c8d9a80275c13fa5dcb1b8 100644 (file)
@@ -37,6 +37,7 @@
 #include <AIModel/AIManager.hxx>
 
 using std::endl;
+using std::string;
 
 namespace flightgear
 {
@@ -64,7 +65,8 @@ static bool fgSetTowerPosFromAirportID( const string& id) {
   
 }
 
-struct FGTowerLocationListener : SGPropertyChangeListener {
+class FGTowerLocationListener : public SGPropertyChangeListener {
+    
   void valueChanged(SGPropertyNode* node) {
     string id(node->getStringValue());
     if (fgGetBool("/sim/tower/auto-position",true))
@@ -81,7 +83,7 @@ struct FGTowerLocationListener : SGPropertyChangeListener {
   }
 };
 
-struct FGClosestTowerLocationListener : SGPropertyChangeListener
+class FGClosestTowerLocationListener : public SGPropertyChangeListener
 {
   void valueChanged(SGPropertyNode* )
   {
@@ -97,9 +99,14 @@ struct FGClosestTowerLocationListener : SGPropertyChangeListener
 };
 
 void initTowerLocationListener() {
+    
+  SGPropertyChangeListener* tll = new FGTowerLocationListener();
+  globals->addListenerToCleanup(tll);
   fgGetNode("/sim/tower/airport-id",  true)
-  ->addChangeListener( new FGTowerLocationListener(), true );
+  ->addChangeListener( tll, true );
+    
   FGClosestTowerLocationListener* ntcl = new FGClosestTowerLocationListener();
+  globals->addListenerToCleanup(ntcl);
   fgGetNode("/sim/airport/closest-airport-id", true)
   ->addChangeListener(ntcl , true );
   fgGetNode("/sim/tower/auto-position", true)
@@ -151,11 +158,23 @@ static bool setPosFromAirportIDandHdg( const string& id, double tgt_hdg ) {
   
   const FGAirport* apt = fgFindAirportID(id);
   if (!apt) return false;
-  FGRunway* r = apt->findBestRunwayForHeading(tgt_hdg);
-  fgSetString("/sim/atc/runway", r->ident().c_str());
   
-  SGGeod startPos = r->pointOnCenterline(fgGetDouble("/sim/airport/runways/start-offset-m", 5.0));
-  fgApplyStartOffset(startPos, r->headingDeg(), tgt_hdg);
+  SGGeod startPos;
+  double heading = tgt_hdg;
+  if (apt->type() == FGPositioned::HELIPORT) {
+    if (apt->numHelipads() > 0) {
+      startPos = apt->getHelipadByIndex(0)->geod();
+    } else {
+      startPos = apt->geod();
+    }
+  } else {
+    FGRunway* r = apt->findBestRunwayForHeading(tgt_hdg);
+    fgSetString("/sim/atc/runway", r->ident().c_str());
+    startPos = r->pointOnCenterline(fgGetDouble("/sim/airport/runways/start-offset-m", 5.0));
+    heading = r->headingDeg();
+  }
+
+  fgApplyStartOffset(startPos, heading, tgt_hdg);
   return true;
 }
 
@@ -316,7 +335,7 @@ static bool fgSetPosFromNAV( const string& id, const double& freq, FGPositioned:
   FGNavList::TypeFilter filter(type);
   const nav_list_type navlist = FGNavList::findByIdentAndFreq( id.c_str(), freq, &filter );
   
-  if (navlist.size() == 0 ) {
+  if (navlist.empty()) {
     SG_LOG( SG_GENERAL, SG_ALERT, "Failed to locate NAV = "
            << id << ":" << freq );
     return false;
@@ -553,6 +572,11 @@ bool initPosition()
   }
   
   fgSetBool("/sim/position-finalized", false);
+
+// Initialize the longitude, latitude and altitude to the initial position
+    fgSetDouble("/position/altitude-ft", fgGetDouble("/sim/presets/altitude-ft"));
+    fgSetDouble("/position/longitude-deg", fgGetDouble("/sim/presets/longitude-deg"));
+    fgSetDouble("/position/latitude-deg", fgGetDouble("/sim/presets/latitude-deg"));
     
   return true;
 }