]> git.mxchange.org Git - flightgear.git/blobdiff - src/Instrumentation/dme.cxx
Fix initialization order
[flightgear.git] / src / Instrumentation / dme.cxx
index bcea965d83b596c766f49a772f4d50406e6f428b..33ea69279d18b949484a93576a9325951f7ba267 100644 (file)
@@ -11,6 +11,7 @@
 #include <simgear/sg_inlines.h>
 #include <simgear/math/sg_geodesy.hxx>
 #include <simgear/math/sg_random.h>
+#include <simgear/sound/sample_group.hxx>
 
 #include <Main/fg_props.hxx>
 #include <Navaids/navlist.hxx>
@@ -40,6 +41,36 @@ adjust_range (double transmitter_elevation_ft, double aircraft_altitude_ft,
     return range_nm + (range_nm * rand * rand);
 }
 
+namespace {
+  
+  class DMEFilter : public FGNavList::TypeFilter
+  {
+  public:
+    DMEFilter() :
+      TypeFilter(FGPositioned::DME),
+      _locEnabled(fgGetBool("/sim/realism/dme-fallback-to-loc", true))
+    {
+      if (_locEnabled) {
+        _mintype = FGPositioned::ILS;
+      }
+    }
+    
+    virtual bool pass(FGPositioned* pos) const
+    {
+      switch (pos->type()) {
+      case FGPositioned::DME: return true;
+      case FGPositioned::ILS:
+      case FGPositioned::LOC: return _locEnabled;
+      default: return false;
+      }
+    }
+    
+  private:
+    const bool _locEnabled;
+  };
+  
+} // of anonymous namespace
+
 
 DME::DME ( SGPropertyNode *node )
     : _last_distance_nm(0),
@@ -60,7 +91,7 @@ DME::~DME ()
 void
 DME::init ()
 {
-    string branch;
+    std::string branch;
     branch = "/instrumentation/" + _name;
 
     SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
@@ -88,6 +119,14 @@ DME::init ()
     if( NULL == _audioIdent ) 
         _audioIdent = new DMEAudioIdent( temp.str() );
     _audioIdent->init();
+
+    reinit();
+}
+
+void
+DME::reinit ()
+{
+    _time_before_search_sec = 0;
 }
 
 void
@@ -99,7 +138,7 @@ DME::update (double delta_time_sec)
                                 // Figure out the source
     const char * source = _source_node->getStringValue();
     if (source[0] == '\0') {
-        string branch;
+        std::string branch;
         branch = "/instrumentation/" + _name + "/frequencies/selected-mhz";
         _source_node->setStringValue(branch.c_str());
         source = _source_node->getStringValue();
@@ -119,17 +158,9 @@ DME::update (double delta_time_sec)
     if (_time_before_search_sec < 0) {
         _time_before_search_sec = 1.0;
 
-        if( fgGetBool( "/sim/realism/dme-fallback-to-loc", true ) ) {
-            if( NULL == (_navrecord = globals->get_loclist()->findByFreq( frequency_mhz,
-                globals->get_aircraft_position())) ) {
-
-                _navrecord = globals->get_dmelist()->findByFreq( frequency_mhz,
-                    globals->get_aircraft_position());
-            }
-        } else {
-            _navrecord = globals->get_dmelist()->findByFreq( frequency_mhz,
-                globals->get_aircraft_position());
-        }
+      SGGeod pos(globals->get_aircraft_position());
+      DMEFilter filter;
+      _navrecord = FGNavList::findByFreq(frequency_mhz, pos, &filter);
     }
 
     // If it's off, don't bother.