]> git.mxchange.org Git - flightgear.git/blobdiff - src/Main/positioninit.cxx
A new comm radio and atis implementation
[flightgear.git] / src / Main / positioninit.cxx
index c7c63b5336f586c7079d4448672a076ff7f12675..f3bac6f7ed6ae1da6ed455d21d7b5ba7f362cb7c 100644 (file)
 
 #include <Navaids/navlist.hxx>
 #include <Airports/runways.hxx>
-#include <Airports/simple.hxx>
+#include <Airports/airport.hxx>
 #include <Airports/dynamics.hxx>
 #include <AIModel/AIManager.hxx>
+#include <GUI/MessageBox.hxx>
+
 
 using std::endl;
+using std::string;
 
 namespace flightgear
 {
@@ -64,7 +67,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 +85,7 @@ struct FGTowerLocationListener : SGPropertyChangeListener {
   }
 };
 
-struct FGClosestTowerLocationListener : SGPropertyChangeListener
+class FGClosestTowerLocationListener : public SGPropertyChangeListener
 {
   void valueChanged(SGPropertyNode* )
   {
@@ -97,9 +101,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 +160,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;
 }
 
@@ -250,19 +271,29 @@ static bool fgSetPosFromAirportIDandRwy( const string& id, const string& rwy, bo
     SG_LOG( SG_GENERAL, SG_ALERT, "Failed to find airport:" << id);
     return false;
   }
-  
-  if (!apt->hasRunwayWithIdent(rwy)) {
-    SG_LOG( SG_GENERAL, rwy_req ? SG_ALERT : SG_INFO,
-           "Failed to find runway " << rwy <<
+
+  if (apt->hasRunwayWithIdent(rwy)) {
+      FGRunway* r(apt->getRunwayByIdent(rwy));
+      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());
+      return true;
+  } else if (apt->hasHelipadWithIdent(rwy)) {
+      FGHelipad* h(apt->getHelipadByIdent(rwy));
+      fgApplyStartOffset(h->geod(), h->headingDeg());
+      return true;
+  }
+
+  if (rwy_req) {
+      flightgear::modalMessageBox("Runway not available", "Runway/helipad "
+                                   + rwy + " not found at airport " + apt->getId()
+                                   + " - " + apt->getName() );
+  } else {
+    SG_LOG( SG_GENERAL, SG_INFO,
+           "Failed to find runway/helipad " << rwy <<
            " at airport " << id << ". Using default runway." );
-    return false;
   }
-  
-  FGRunway* r(apt->getRunwayByIdent(rwy));
-  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());
-  return true;
+  return false;
 }
 
 
@@ -316,7 +347,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,15 +584,24 @@ 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;
 }
   
 bool finalizeMetar()
 {
+  if (!fgGetBool("/environment/realwx/enabled")) {
+    return true;
+  }
+  
   double hdg = fgGetDouble( "/environment/metar/base-wind-dir-deg", 9999.0 );
-  string apt = fgGetString( "/sim/startup/options/airport" );
-  string rwy = fgGetString( "/sim/startup/options/runway" );
+  string apt = fgGetString("/sim/presets/airport-id");
+  string rwy = fgGetString("/sim/presets/runway");
   double strthdg = fgGetDouble( "/sim/startup/options/heading-deg", 9999.0 );
   string parkpos = fgGetString( "/sim/presets/parkpos" );
   bool onground = fgGetBool( "/sim/presets/onground", false );
@@ -571,17 +611,17 @@ bool finalizeMetar()
   
   if (needMetar) {
     // timeout so we don't spin forever if the network is down
-    if (global_finalizeTime.elapsedMSec() > fgGetInt("/sim/startup/metar-fetch-timeout-msec", 10000)) {
+    if (global_finalizeTime.elapsedMSec() > fgGetInt("/sim/startup/metar-fetch-timeout-msec", 6000)) {
       SG_LOG(SG_GENERAL, SG_WARN, "finalizePosition: timed out waiting for METAR fetch");
       return true;
     }
     
+    if (fgGetBool( "/environment/metar/failure" )) {
+      SG_LOG(SG_ENVIRONMENT, SG_INFO, "metar download failed, not waiting");
+      return true;
+    }
+
     if (!fgGetBool( "/environment/metar/valid" )) {
-      // bit hacky - run these two subsystems. We can't run the whole
-      // lot since some view things aren't initialised and hence FGLight
-      // crashes.
-      globals->get_subsystem("http")->update(0.0);
-      globals->get_subsystem("environment")->update(0.0);
       return false;
     }