]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/fdm_shell.cxx
commradio: improvements for atis speech
[flightgear.git] / src / FDM / fdm_shell.cxx
index faa37d788520db2368f12916fc8a78ff7ccd151c..3629b22abb9964261b513e19a639f64db103a48f 100644 (file)
@@ -26,6 +26,7 @@
 
 #include <cassert>
 #include <simgear/structure/exception.hxx>
+#include <simgear/props/props_io.hxx>
 
 #include <FDM/fdm_shell.hxx>
 #include <FDM/flight.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>
 
+#ifdef ENABLE_YASIM
+#include <FDM/YASim/YASim.hxx>
+#endif
 
-/*
- * Evil global variable required by Network/FGNative,
- * see that class for more information
- */
-FGInterface* evil_global_fdm_state = NULL;
+using std::string;
 
 FDMShell::FDMShell() :
   _tankProperties( fgGetNode("/consumables/fuel", true) ),
-  _impl(NULL),
   _dataLogging(false)
 {
 }
 
 FDMShell::~FDMShell()
 {
-  delete _impl;
+    SG_LOG(SG_GENERAL, SG_INFO, "destorying FDM shell");
 }
 
 void FDMShell::init()
@@ -81,20 +86,54 @@ void FDMShell::init()
   _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();
 }
 
+void FDMShell::postinit()
+{
+    _initialFdmProperties = new SGPropertyNode;
+    
+    if (!copyProperties(_props->getNode("fdm", true),
+                                     _initialFdmProperties))
+    {
+        SG_LOG(SG_FLIGHT, SG_ALERT, "Failed to save initial FDM property state");
+    }
+}
+
+void FDMShell::shutdown()
+{
+    if (_impl) {
+        fgSetBool("/sim/fdm-initialized", false);
+        _impl->unbind();
+        _impl.clear();
+    }
+    
+    _props.clear();
+    _wind_north.clear();
+    _wind_east.clear();
+    _wind_down.clear();
+    _control_fdm_atmo.clear();
+    _temp_degc.clear();
+    _pressure_inhg.clear();
+    _density_slugft .clear();
+    _data_logging.clear();
+    _replay_master.clear();
+}
+
 void FDMShell::reinit()
 {
-  if (_impl) {
-    fgSetBool("/sim/fdm-initialized", false);
-    evil_global_fdm_state = NULL;
-    _impl->unbind();
-    delete _impl;
-    _impl = NULL;
-  }
+  shutdown();
   
+    if ( copyProperties(_initialFdmProperties, fgGetNode("/fdm", true)) ) {
+        SG_LOG( SG_FLIGHT, SG_INFO, "Preserved state restored successfully" );
+    } else {
+        SG_LOG( SG_FLIGHT, SG_WARN,
+               "FDM: Some errors restoring preserved state" );
+    }
+
+    
   init();
 }
 
@@ -136,7 +175,6 @@ void FDMShell::update(double dt)
         }
         _impl->bind();
         
-        evil_global_fdm_state = _impl;
         fgSetBool("/sim/fdm-initialized", true);
         fgSetBool("/sim/signals/fdm-initialized", true);
     }
@@ -170,8 +208,25 @@ void FDMShell::update(double dt)
     _impl->ToggleDataLogging(doLog);
   }
 
-  if (!_impl->is_suspended())
-      _impl->update(dt);
+  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()
@@ -181,12 +236,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 );
+#else
+        fdmUnavailable = true;
+#endif
+    }
 #ifdef ENABLE_SP_FDM
-    else if ( model == "ada" ) {
+    else if ( model == "ada" ) {
         _impl = new FGADA( dt );
     } else if ( model == "acms" ) {
         _impl = new FGACMS( dt );
@@ -194,81 +320,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.");
     }
 
-}
-
-/*
- * Return FDM subsystem.
- */
-
-SGSubsystem* FDMShell::getFDM()
-{
-    /* FIXME we could drop/replace this method, when _impl was a added
-     * to the global subsystem manager - like other proper subsystems... */
-    return _impl;
+    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."));
+    }
 }