]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/fdm_shell.cxx
Fix native protocol crashes.
[flightgear.git] / src / FDM / fdm_shell.cxx
index 705776b1fc532094dd27f5d3a3e205f1c3c1a81f..21a3b79df7e0824168cb6d2e228e8051dcf9292d 100644 (file)
@@ -35,7 +35,7 @@
 #include <Scenery/scenery.hxx>
 
 // all the FDMs, since we are the factory method
-#if ENABLE_SP_FDM
+#ifdef ENABLE_SP_FDM
 #include <FDM/SP/ADA.hxx>
 #include <FDM/SP/ACMS.hxx>
 #include <FDM/SP/MagicCarpet.hxx>
 #endif
 #include <FDM/ExternalNet/ExternalNet.hxx>
 #include <FDM/ExternalPipe/ExternalPipe.hxx>
+
+#ifdef ENABLE_JSBSIM
 #include <FDM/JSBSim/JSBSim.hxx>
+#endif
+
+#ifdef ENABLE_LARCSIM
 #include <FDM/LaRCsim/LaRCsim.hxx>
+#endif
+
 #include <FDM/UFO.hxx>
 #include <FDM/NullFDM.hxx>
-#include <FDM/YASim/YASim.hxx>
 
-/*
- * Evil global variable required by Network/FGNative,
- * see that class for more information
- */
-FGInterface* evil_global_fdm_state = NULL;
+#ifdef ENABLE_YASIM
+#include <FDM/YASim/YASim.hxx>
+#endif
 
 FDMShell::FDMShell() :
+  _tankProperties( fgGetNode("/consumables/fuel", true) ),
   _impl(NULL),
   _dataLogging(false)
 {
@@ -70,6 +75,17 @@ void FDMShell::init()
 {
   _props = globals->get_props();
   fgSetBool("/sim/fdm-initialized", false);
+
+  _wind_north       = _props->getNode("environment/wind-from-north-fps",    true);
+  _wind_east        = _props->getNode("environment/wind-from-east-fps",     true);
+  _wind_down        = _props->getNode("environment/wind-from-down-fps",     true);
+  _control_fdm_atmo = _props->getNode("environment/params/control-fdm-atmosphere", true);
+  _temp_degc        = _props->getNode("environment/temperature-degc",       true);
+  _pressure_inhg    = _props->getNode("environment/pressure-inhg",          true);
+  _density_slugft   = _props->getNode("environment/density-slugft3",        true);
+  _data_logging     = _props->getNode("/sim/temp/fdm-data-logging",         true);
+  _replay_master    = _props->getNode("/sim/freeze/replay-state",           true);
+
   createImplementation();
 }
 
@@ -77,7 +93,6 @@ void FDMShell::reinit()
 {
   if (_impl) {
     fgSetBool("/sim/fdm-initialized", false);
-    evil_global_fdm_state = NULL;
     _impl->unbind();
     delete _impl;
     _impl = NULL;
@@ -88,11 +103,11 @@ void FDMShell::reinit()
 
 void FDMShell::bind()
 {
+  _tankProperties.bind();
   if (_impl && _impl->get_inited()) {
     if (_impl->get_bound()) {
       throw sg_exception("FDMShell::bind of bound FGInterface impl");
     }
-    
     _impl->bind();
   }
 }
@@ -100,6 +115,7 @@ void FDMShell::bind()
 void FDMShell::unbind()
 {
   if( _impl ) _impl->unbind();
+  _tankProperties.unbind();
 }
 
 void FDMShell::update(double dt)
@@ -113,7 +129,7 @@ void FDMShell::update(double dt)
     double lon = fgGetDouble("/sim/presets/longitude-deg");
     double lat = fgGetDouble("/sim/presets/latitude-deg");
         
-    double range = 100.0; // in meters
+    double range = 1000.0; // in meters
     SGGeod geod = SGGeod::fromDeg(lon, lat);
     if (globals->get_scenery()->scenery_available(geod, range)) {
         SG_LOG(SG_FLIGHT, SG_INFO, "Scenery loaded, will init FDM");
@@ -123,7 +139,6 @@ void FDMShell::update(double dt)
         }
         _impl->bind();
         
-        evil_global_fdm_state = _impl;
         fgSetBool("/sim/fdm-initialized", true);
         fgSetBool("/sim/signals/fdm-initialized", true);
     }
@@ -135,46 +150,47 @@ void FDMShell::update(double dt)
 
 // pull environmental data in, since the FDMs are lazy
   _impl->set_Velocities_Local_Airmass(
-      _props->getDoubleValue("environment/wind-from-north-fps", 0.0),
-                       _props->getDoubleValue("environment/wind-from-east-fps", 0.0),
-                       _props->getDoubleValue("environment/wind-from-down-fps", 0.0));
+          _wind_north->getDoubleValue(),
+          _wind_east->getDoubleValue(),
+          _wind_down->getDoubleValue());
 
-  if (_props->getBoolValue("environment/params/control-fdm-atmosphere")) {
+  if (_control_fdm_atmo->getBoolValue()) {
     // convert from Rankine to Celsius
-    double tempDegC = _props->getDoubleValue("environment/temperature-degc");
+    double tempDegC = _temp_degc->getDoubleValue();
     _impl->set_Static_temperature((9.0/5.0) * (tempDegC + 273.15));
     
     // convert from inHG to PSF
-    double pressureInHg = _props->getDoubleValue("environment/pressure-inhg");
+    double pressureInHg = _pressure_inhg->getDoubleValue();
     _impl->set_Static_pressure(pressureInHg * 70.726566);
     // keep in slugs/ft^3
-    _impl->set_Density(_props->getDoubleValue("environment/density-slugft3"));
+    _impl->set_Density(_density_slugft->getDoubleValue());
   }
 
-  bool doLog = _props->getBoolValue("/sim/temp/fdm-data-logging", false);
+  bool doLog = _data_logging->getBoolValue();
   if (doLog != _dataLogging) {
     _dataLogging = doLog;
     _impl->ToggleDataLogging(doLog);
   }
 
-// FIXME - replay manager should handle most of this           
-  int replayState = fgGetInt("/sim/freeze/replay-state", 0);
-  if (replayState == 0) {
-    _impl->update(dt); // normal code path
-  } else if (replayState == 1) {
-    // should be inside FGReplay!
-    SGPropertyNode* replay_time = fgGetNode("/sim/replay/time", true);
-    FGReplay *r = (FGReplay *)(globals->get_subsystem( "replay" ));
-    r->replay( replay_time->getDoubleValue() );
-    replay_time->setDoubleValue( replay_time->getDoubleValue()
-                                             + ( dt
-                                                 * fgGetInt("/sim/speed-up") ) );
-  
-  } else if (replayState == 2) {
-    // paused replay, no-op
-  } else {
-    throw sg_range_exception("unknown FGReplay state");
-  }  
+  switch(_replay_master->getIntValue())
+  {
+      case 0:
+          // normal FDM operation
+          _impl->update(dt);
+          break;
+      case 3:
+          // resume FDM operation at current replay position
+          _impl->reinit();
+          break;
+      default:
+          // replay is active
+          break;
+  }
+}
+
+FGInterface* FDMShell::getInterface() const
+{
+    return _impl;
 }
 
 void FDMShell::createImplementation()
@@ -184,12 +200,83 @@ void FDMShell::createImplementation()
   double dt = 1.0 / fgGetInt("/sim/model-hz");
   string model = fgGetString("/sim/flight-model");
 
-    if ( model == "larcsim" ) {
+  bool fdmUnavailable = false;
+
+  if ( model == "ufo" ) {
+    _impl = new FGUFO( dt );
+  } else if ( model == "external" ) {
+    // external is a synonym for "--fdm=null" and is
+    // maintained here for backwards compatibility
+    _impl = new FGNullFDM( dt );
+  } else if ( model.find("network") == 0 ) {
+    string host = "localhost";
+    int port1 = 5501;
+    int port2 = 5502;
+    int port3 = 5503;
+    string net_options = model.substr(8);
+    string::size_type begin, end;
+    begin = 0;
+    // host
+    end = net_options.find( ",", begin );
+    if ( end != string::npos ) {
+      host = net_options.substr(begin, end - begin);
+      begin = end + 1;
+    }
+    // port1
+    end = net_options.find( ",", begin );
+    if ( end != string::npos ) {
+      port1 = atoi( net_options.substr(begin, end - begin).c_str() );
+      begin = end + 1;
+    }
+    // port2
+    end = net_options.find( ",", begin );
+    if ( end != string::npos ) {
+      port2 = atoi( net_options.substr(begin, end - begin).c_str() );
+      begin = end + 1;
+    }
+    // port3
+    end = net_options.find( ",", begin );
+    if ( end != string::npos ) {
+      port3 = atoi( net_options.substr(begin, end - begin).c_str() );
+      begin = end + 1;
+    }
+    _impl = new FGExternalNet( dt, host, port1, port2, port3 );
+  } else if ( model.find("pipe") == 0 ) {
+    // /* old */ string pipe_path = model.substr(5);
+    // /* old */ _impl = new FGExternalPipe( dt, pipe_path );
+    string pipe_path = "";
+    string pipe_protocol = "";
+    string pipe_options = model.substr(5);
+    string::size_type begin, end;
+    begin = 0;
+    // pipe file path
+    end = pipe_options.find( ",", begin );
+    if ( end != string::npos ) {
+      pipe_path = pipe_options.substr(begin, end - begin);
+      begin = end + 1;
+    }
+    // protocol (last option)
+    pipe_protocol = pipe_options.substr(begin);
+    _impl = new FGExternalPipe( dt, pipe_path, pipe_protocol );
+  } else if ( model == "null" ) {
+    _impl = new FGNullFDM( dt );
+  }
+    else if ( model == "larcsim" ) {
+#ifdef ENABLE_LARCSIM
         _impl = new FGLaRCsim( dt );
-    } else if ( model == "jsb" ) {
+#else
+        fdmUnavailable = true;
+#endif
+    }
+    else if ( model == "jsb" ) {
+#ifdef ENABLE_JSBSIM
         _impl = new FGJSBsim( dt );
-#if ENABLE_SP_FDM
-    } else if ( model == "ada" ) {
+#else
+        fdmUnavailable = true;
+#endif
+    }
+#ifdef ENABLE_SP_FDM
+    else if ( model == "ada" ) {
         _impl = new FGADA( dt );
     } else if ( model == "acms" ) {
         _impl = new FGACMS( dt );
@@ -197,71 +284,29 @@ void FDMShell::createImplementation()
         _impl = new FGBalloonSim( dt );
     } else if ( model == "magic" ) {
         _impl = new FGMagicCarpet( dt );
+    }
+#else
+    else if (( model == "ada" )||(model == "acms")||( model == "balloon" )||( model == "magic" ))
+    {
+        fdmUnavailable = true;
+    }
 #endif
-    } else if ( model == "ufo" ) {
-        _impl = new FGUFO( dt );
-    } else if ( model == "external" ) {
-        // external is a synonym for "--fdm=null" and is
-        // maintained here for backwards compatibility
-        _impl = new FGNullFDM( dt );
-    } else if ( model.find("network") == 0 ) {
-        string host = "localhost";
-        int port1 = 5501;
-        int port2 = 5502;
-        int port3 = 5503;
-        string net_options = model.substr(8);
-        string::size_type begin, end;
-        begin = 0;
-        // host
-        end = net_options.find( ",", begin );
-        if ( end != string::npos ) {
-            host = net_options.substr(begin, end - begin);
-            begin = end + 1;
-        }
-        // port1
-        end = net_options.find( ",", begin );
-        if ( end != string::npos ) {
-            port1 = atoi( net_options.substr(begin, end - begin).c_str() );
-            begin = end + 1;
-        }
-        // port2
-        end = net_options.find( ",", begin );
-        if ( end != string::npos ) {
-            port2 = atoi( net_options.substr(begin, end - begin).c_str() );
-            begin = end + 1;
-        }
-        // port3
-        end = net_options.find( ",", begin );
-        if ( end != string::npos ) {
-            port3 = atoi( net_options.substr(begin, end - begin).c_str() );
-            begin = end + 1;
-        }
-        _impl = new FGExternalNet( dt, host, port1, port2, port3 );
-    } else if ( model.find("pipe") == 0 ) {
-        // /* old */ string pipe_path = model.substr(5);
-        // /* old */ _impl = new FGExternalPipe( dt, pipe_path );
-        string pipe_path = "";
-        string pipe_protocol = "";
-        string pipe_options = model.substr(5);
-        string::size_type begin, end;
-        begin = 0;
-        // pipe file path
-        end = pipe_options.find( ",", begin );
-        if ( end != string::npos ) {
-            pipe_path = pipe_options.substr(begin, end - begin);
-            begin = end + 1;
-        }
-        // protocol (last option)
-        pipe_protocol = pipe_options.substr(begin);
-        _impl = new FGExternalPipe( dt, pipe_path, pipe_protocol );
-    } else if ( model == "null" ) {
-        _impl = new FGNullFDM( dt );
-    } else if ( model == "yasim" ) {
+    else if ( model == "yasim" ) {
+#ifdef ENABLE_YASIM
         _impl = new YASim( dt );
+#else
+        fdmUnavailable = true;
+#endif
     } else {
         throw sg_exception(string("Unrecognized flight model '") + model
                + "', cannot init flight dynamics model.");
     }
 
+    if (fdmUnavailable)
+    {
+        // FDM type is known, but its support was disabled at compile-time.
+        throw sg_exception(string("Support for flight model '") + model
+                + ("' is not available with this binary (deprecated/disabled).\n"
+                   "If you still need it, please rebuild FlightGear and enable its support."));
+    }
 }
-