]> git.mxchange.org Git - flightgear.git/blobdiff - src/Main/fg_init.cxx
Added FGScript.{cpp,h}
[flightgear.git] / src / Main / fg_init.cxx
index d8cea2a5f0746b38e3ccebd8bab4239c682cbb32..15c406282ca047295811be64ff1a2a6d39f87596 100644 (file)
@@ -1,4 +1,4 @@
-<// fg_init.cxx -- Flight Gear top level initialization routines
+// fg_init.cxx -- Flight Gear top level initialization routines
 //
 // Written by Curtis Olson, started August 1997.
 //
@@ -52,6 +52,7 @@
 #endif
 
 #include <simgear/compiler.h>
+#include <simgear/misc/exception.hxx>
 
 #include STL_STRING
 
@@ -67,6 +68,7 @@
 #include <FDM/UIUCModel/uiuc_aircraftdir.h>
 #include <Airports/runways.hxx>
 #include <Airports/simple.hxx>
+#include <ATC/ATCdisplay.hxx>
 #include <Autopilot/auto_gui.hxx>
 #include <Autopilot/newauto.hxx>
 #include <Cockpit/cockpit.hxx>
@@ -79,6 +81,8 @@
 #include <FDM/JSBSim.hxx>
 #include <FDM/LaRCsim.hxx>
 #include <FDM/MagicCarpet.hxx>
+#include <FDM/NullFDM.hxx>
+#include <FDM/YASim/YASim.hxx>
 #include <Include/general.hxx>
 #include <Input/input.hxx>
 // #include <Joystick/joystick.hxx>
@@ -89,6 +93,8 @@
 #include <Navaids/navlist.hxx>
 #include <Scenery/scenery.hxx>
 #include <Scenery/tilemgr.hxx>
+#include <Sound/fg_fx.hxx>
+#include <Sound/soundmgr.hxx>
 #include <Time/event.hxx>
 #include <Time/light.hxx>
 #include <Time/sunpos.hxx>
 #include "fg_props.hxx"
 #include "options.hxx"
 #include "globals.hxx"
+#include "viewmgr.hxx"
 
 #if defined(FX) && defined(XMESA)
 #include <GL/xmesa.h>
@@ -181,6 +188,30 @@ bool fgInitFGRoot ( int argc, char **argv ) {
 }
 
 
+// Return the current base package version
+string fgBasePackageVersion() {
+    SGPath base_path( globals->get_fg_root() );
+    base_path.append("version");
+
+    sg_gzifstream in( base_path.str() );
+    if ( !in.is_open() ) {
+       SGPath old_path( globals->get_fg_root() );
+       old_path.append( "Thanks" );
+       sg_gzifstream old( old_path.str() );
+       if ( !old.is_open() ) {
+           return "[none]";
+       } else {
+           return "[old version]";
+       }
+    }
+
+    string version;
+    in >> version;
+
+    return version;
+}
+
+
 // Read in configuration (file and command line)
 bool fgInitConfig ( int argc, char **argv ) {
 
@@ -191,11 +222,35 @@ bool fgInitConfig ( int argc, char **argv ) {
     SGPath props_path(globals->get_fg_root());
     props_path.append("preferences.xml");
     SG_LOG(SG_INPUT, SG_INFO, "Reading global preferences");
-    if (!readProperties(props_path.str(), globals->get_props())) {
-      SG_LOG(SG_INPUT, SG_ALERT, "Failed to read global preferences from "
-            << props_path.str());
+    try {
+      readProperties(props_path.str(), globals->get_props());
+    } catch (const sg_exception &e) {
+      string message = "Error reading global preferences: ";
+      message += e.getFormattedMessage();
+      SG_LOG(SG_INPUT, SG_ALERT, message);
+      exit(2);
+    }
+    SG_LOG(SG_INPUT, SG_INFO, "Finished Reading global preferences");
+
+    // Read the default aircraft config file.
+    string aircraft = fgGetString("/sim/aircraft", "");
+    if (aircraft.size() > 0) {
+      SGPath aircraft_path(globals->get_fg_root());
+      aircraft_path.append("Aircraft");
+      aircraft_path.append(aircraft);
+      aircraft_path.concat("-set.xml");
+      SG_LOG(SG_INPUT, SG_INFO, "Reading default aircraft: " << aircraft
+            << " from " << props_path.str());
+      try {
+       readProperties(aircraft_path.str(), globals->get_props());
+      } catch (const sg_exception &e) {
+       string message = "Error reading default aircraft: ";
+       message += e.getFormattedMessage();
+       SG_LOG(SG_INPUT, SG_ALERT, message);
+       exit(2);
+      }
     } else {
-      SG_LOG(SG_INPUT, SG_INFO, "Finished Reading global preferences");
+      SG_LOG(SG_INPUT, SG_ALERT, "No default aircraft specified");
     }
 
     // Attempt to locate and parse the various config files in order
@@ -276,8 +331,8 @@ bool fgSetPosFromAirportID( const string& id ) {
            "Attempting to set starting position from airport code " << id );
 
     if ( fgFindAirportID( id, &a ) ) {
-       fgSetDouble("/position/longitude",  a.longitude );
-       fgSetDouble("/position/latitude",  a.latitude );
+       fgSetDouble("/position/longitude-deg",  a.longitude );
+       fgSetDouble("/position/latitude-deg",  a.latitude );
        SG_LOG( SG_GENERAL, SG_INFO,
                "Position for " << id << " is ("
                << a.longitude << ", "
@@ -397,9 +452,9 @@ bool fgSetPosFromAirportIDandHdg( const string& id, double tgt_hdg ) {
        lat2=olat;
        lon2=olon;
     }
-    fgSetDouble("/position/longitude",  lon2 );
-    fgSetDouble("/position/latitude",  lat2 );
-    fgSetDouble("/orientation/heading", heading );
+    fgSetDouble("/position/longitude-deg",  lon2 );
+    fgSetDouble("/position/latitude-deg",  lat2 );
+    fgSetDouble("/orientation/heading-deg", heading );
 
     SG_LOG( SG_GENERAL, SG_INFO,
            "Position for " << id << " is ("
@@ -426,7 +481,7 @@ bool fgInitGeneral( void ) {
     if ( ! root.length() ) {
        // No root path set? Then bail ...
        SG_LOG( SG_GENERAL, SG_ALERT,
-               "Cannot continue without environment variable FG_ROOT"
+               "Cannot continue without a path to the base package "
                << "being defined." );
        exit(-1);
     }
@@ -456,6 +511,13 @@ bool fgInitGeneral( void ) {
 // gear, its initialization call should located in this routine.
 // Returns non-zero if a problem encountered.
 bool fgInitSubsystems( void ) {
+    static const SGPropertyNode *longitude
+       = fgGetNode("/position/longitude-deg");
+    static const SGPropertyNode *latitude
+       = fgGetNode("/position/latitude-deg");
+    static const SGPropertyNode *altitude
+       = fgGetNode("/position/altitude-ft");
+
     fgLIGHT *l = &cur_light_params;
 
     SG_LOG( SG_GENERAL, SG_INFO, "Initialize Subsystems");
@@ -479,17 +541,13 @@ bool fgInitSubsystems( void ) {
     // Initialize the scenery management subsystem.
     ////////////////////////////////////////////////////////////////////
 
-    if ( fgSceneryInit() ) {
-       // Material lib initialized ok.
-    } else {
-       SG_LOG( SG_GENERAL, SG_ALERT, "Error in Scenery initialization!" );
-       exit(-1);
-    }
+    scenery.init();
+    scenery.bind();
 
     if ( global_tile_mgr.init() ) {
        // Load the local scenery data
-       global_tile_mgr.update( fgGetDouble("/position/longitude"),
-                               fgGetDouble("/position/latitude") );
+       global_tile_mgr.update( fgGetDouble("/position/longitude-deg"),
+                               fgGetDouble("/position/latitude-deg") );
     } else {
        SG_LOG( SG_GENERAL, SG_ALERT, "Error in Tile Manager initialization!" );
        exit(-1);
@@ -497,7 +555,7 @@ bool fgInitSubsystems( void ) {
 
     SG_LOG( SG_GENERAL, SG_DEBUG,
            "Current terrain elevation after tile mgr init " <<
-           scenery.cur_elev );
+           scenery.get_cur_elev() );
 
 
     ////////////////////////////////////////////////////////////////////
@@ -509,26 +567,38 @@ bool fgInitSubsystems( void ) {
 
     aircraft_dir = fgGetString("/sim/aircraft-dir");
     const string &model = fgGetString("/sim/flight-model");
-    if (model == "larcsim") {
-       cur_fdm_state = new FGLaRCsim( dt );
-    } else if (model == "jsb") {
-       cur_fdm_state = new FGJSBsim( dt );
-    } else if (model == "ada") {
-       cur_fdm_state = new FGADA( dt );
-    } else if (model == "balloon") {
-       cur_fdm_state = new FGBalloonSim( dt );
-    } else if (model == "magic") {
-       cur_fdm_state = new FGMagicCarpet( dt );
-    } else if (model == "external") {
-       cur_fdm_state = new FGExternal( dt );
-    } else {
-       SG_LOG(SG_GENERAL, SG_ALERT,
-              "Unrecognized flight model '" << model
-              << ", can't init aircraft");
+    try {
+       if (model == "larcsim") {
+           cur_fdm_state = new FGLaRCsim( dt );
+       } else if (model == "jsb") {
+           cur_fdm_state = new FGJSBsim( dt );
+       } else if (model == "ada") {
+           cur_fdm_state = new FGADA( dt );
+       } else if (model == "balloon") {
+           cur_fdm_state = new FGBalloonSim( dt );
+       } else if (model == "magic") {
+           cur_fdm_state = new FGMagicCarpet( dt );
+       } else if (model == "external") {
+           cur_fdm_state = new FGExternal( dt );
+       } else if (model == "null") {
+           cur_fdm_state = new FGNullFDM( dt );
+       } else if (model == "yasim") {
+           cur_fdm_state = new YASim( dt );
+       } else {
+           SG_LOG(SG_GENERAL, SG_ALERT,
+                  "Unrecognized flight model '" << model
+                  << "', cannot init flight dynamics model.");
+           exit(-1);
+       }
+    } catch ( ... ) {
+       SG_LOG(SG_GENERAL, SG_ALERT, "FlightGear aborting\n\n");
        exit(-1);
     }
-    cur_fdm_state->init();
-    cur_fdm_state->bind();
+
+    // Actual fdm initialization is delayed until we get a proper
+    // scenery elevation hit.  This is checked for in main.cxx
+    // cur_fdm_state->init();
+    // cur_fdm_state->bind();
     
     // allocates structures so must happen before any of the flight
     // model or control parameters are set
@@ -552,23 +622,25 @@ bool fgInitSubsystems( void ) {
     // Initialize the view manager subsystem.
     ////////////////////////////////////////////////////////////////////
 
+#if 0  /* As this wrongly does an integer division and gets x and y the wrong way around, I guess it's not needed.  JAF */
     // Initialize win_ratio parameters
     for ( int i = 0; i < globals->get_viewmgr()->size(); ++i ) {
        globals->get_viewmgr()->get_view(i)->
            set_win_ratio( fgGetInt("/sim/startup/xsize") /
                           fgGetInt("/sim/startup/ysize") );
     }
+#endif
 
     // Initialize pilot view
     FGViewerRPH *pilot_view =
        (FGViewerRPH *)globals->get_viewmgr()->get_view( 0 );
 
-    pilot_view->set_geod_view_pos( cur_fdm_state->get_Longitude(), 
-                                  cur_fdm_state->get_Lat_geocentric()
-                                  cur_fdm_state->get_Altitude() *
-                                  SG_FEET_TO_METER );
-    pilot_view->set_sea_level_radius( cur_fdm_state->get_Sea_level_radius() *
-                                     SG_FEET_TO_METER ); 
+    pilot_view->set_geod_view_pos( longitude->getDoubleValue()
+                                    * SGD_DEGREES_TO_RADIANS
+                                  latitude->getDoubleValue()
+                                    * SGD_DEGREES_TO_RADIANS,
+                                  altitude->getDoubleValue()
+                                    * SG_FEET_TO_METER );
     pilot_view->set_rph( cur_fdm_state->get_Phi(),
                         cur_fdm_state->get_Theta(),
                         cur_fdm_state->get_Psi() );
@@ -625,19 +697,35 @@ bool fgInitSubsystems( void ) {
     sgSetVec3( position, current_aircraft.fdm_state->get_Latitude(),
               current_aircraft.fdm_state->get_Longitude(),
               current_aircraft.fdm_state->get_Altitude() * SG_FEET_TO_METER );
-    FGLocalWeatherDatabase::theFGLocalWeatherDatabase = 
+    double init_vis = fgGetDouble("/environment/visibility-m");
+
+    FGLocalWeatherDatabase::DatabaseWorkingType working_type;
+
+    if (fgGetString("/environment/weather/working-type") == "internet")
+    {
+      working_type = FGLocalWeatherDatabase::use_internet;
+    } else {
+      working_type = FGLocalWeatherDatabase::default_mode;
+    }
+    
+    if ( init_vis > 0 ) {
+      FGLocalWeatherDatabase::theFGLocalWeatherDatabase = 
+       new FGLocalWeatherDatabase( position,
+                                   globals->get_fg_root(),
+                                    working_type,
+                                    init_vis );
+    } else {
+      FGLocalWeatherDatabase::theFGLocalWeatherDatabase = 
        new FGLocalWeatherDatabase( position,
-                                   globals->get_fg_root() );
+                                   globals->get_fg_root(),
+                                    working_type );
+    }
+
     // cout << theFGLocalWeatherDatabase << endl;
     // cout << "visibility = " 
     //      << theFGLocalWeatherDatabase->getWeatherVisibility() << endl;
 
     WeatherDatabase = FGLocalWeatherDatabase::theFGLocalWeatherDatabase;
-    
-    double init_vis = fgGetDouble("/environment/visibility");
-    if ( init_vis > 0 ) {
-       WeatherDatabase->setWeatherVisibility( init_vis );
-    }
 
     // register the periodic update of the weather
     global_events.Register( "weather update", fgUpdateWeatherDatabase,
@@ -672,6 +760,25 @@ bool fgInitSubsystems( void ) {
     p_fix.append( "Navaids/default.fix" );
     current_fixlist->init( p_fix );
 
+    ////////////////////////////////////////////////////////////////////
+    // Initialize ATC list management and query systems
+    ////////////////////////////////////////////////////////////////////
+
+    //DCL
+    SG_LOG(SG_GENERAL, SG_INFO, "  ATIS");
+    current_atislist = new FGATISList;
+    SGPath p_atis( globals->get_fg_root() );
+    p_atis.append( "ATC/default.atis" );
+    current_atislist->init( p_atis );
+
+    ////////////////////////////////////////////////////////////////////
+    // Initialise ATC display system
+    ////////////////////////////////////////////////////////////////////
+
+    //DCL
+    SG_LOG(SG_GENERAL, SG_INFO, "  ATC Display");
+    current_atcdisplay = new FGATCDisplay;
+    current_atcdisplay->init();   
 
     ////////////////////////////////////////////////////////////////////
     // Initialize the built-in commands.
@@ -679,6 +786,23 @@ bool fgInitSubsystems( void ) {
     fgInitCommands();
 
 
+    ////////////////////////////////////////////////////////////////////
+    // Initialize the sound subsystem.
+    ////////////////////////////////////////////////////////////////////
+
+    globals->set_soundmgr(new FGSoundMgr);
+    globals->get_soundmgr()->init();
+    globals->get_soundmgr()->bind();
+
+
+    ////////////////////////////////////////////////////////////////////
+    // Initialize the sound-effects subsystem.
+    ////////////////////////////////////////////////////////////////////
+    globals->set_fx(new FGFX);
+    globals->get_fx()->init();
+    globals->get_fx()->bind();
+
+
     ////////////////////////////////////////////////////////////////////
     // Initialize the radio stack subsystem.
     ////////////////////////////////////////////////////////////////////
@@ -751,15 +875,15 @@ bool fgInitSubsystems( void ) {
     // Initialize the default (kludged) properties.
     ////////////////////////////////////////////////////////////////////
 
-    fgInitProps ();
+    fgInitProps();
 
 
     ////////////////////////////////////////////////////////////////////
     // Initialize the controls subsystem.
     ////////////////////////////////////////////////////////////////////
 
-    controls.init();
-    controls.bind();
+    globals->get_controls()->init();
+    globals->get_controls()->bind();
 
 
     ////////////////////////////////////////////////////////////////////
@@ -786,26 +910,65 @@ bool fgInitSubsystems( void ) {
 
 void fgReInitSubsystems( void )
 {
+    static const SGPropertyNode *longitude
+       = fgGetNode("/position/longitude-deg");
+    static const SGPropertyNode *latitude
+       = fgGetNode("/position/latitude-deg");
+    static const SGPropertyNode *altitude
+       = fgGetNode("/position/altitude-ft");
+
     SG_LOG( SG_GENERAL, SG_INFO,
-           "/position/altitude = " << fgGetDouble("/position/altitude") );
+           "/position/altitude = " << altitude->getDoubleValue() );
 
     bool freeze = globals->get_freeze();
-    if( !freeze )
+    if( !freeze ) {
         globals->set_freeze( true );
+    }
     
     // Initialize the Scenery Management subsystem
-    if ( ! fgSceneryInit() ) {
-       SG_LOG( SG_GENERAL, SG_ALERT, "Error in Scenery initialization!" );
-       exit(-1);
-    }
+    scenery.init();
 
-    if( global_tile_mgr.init() ) {
+    // if( global_tile_mgr.init() ) {
        // Load the local scenery data
-       global_tile_mgr.update( fgGetDouble("/position/longitude"),
-                               fgGetDouble("/position/latitude") );
-    } else {
-       SG_LOG( SG_GENERAL, SG_ALERT, "Error in Tile Manager initialization!" );
-               exit(-1);
+       global_tile_mgr.update( longitude->getDoubleValue(),
+                               latitude->getDoubleValue() );
+    // } else {
+       // SG_LOG( SG_GENERAL, SG_ALERT, "Error in Tile Manager initialization!" );
+               // exit(-1);
+    // }
+
+    // Delete then Initialize the flight model subsystem.
+    delete cur_fdm_state;
+
+    double dt = 1.0 / fgGetInt("/sim/model-hz");
+    aircraft_dir = fgGetString("/sim/aircraft-dir");
+    const string &model = fgGetString("/sim/flight-model");
+    try {
+       if (model == "larcsim") {
+           cur_fdm_state = new FGLaRCsim( dt );
+       } else if (model == "jsb") {
+           cur_fdm_state = new FGJSBsim( dt );
+       } else if (model == "ada") {
+           cur_fdm_state = new FGADA( dt );
+       } else if (model == "balloon") {
+           cur_fdm_state = new FGBalloonSim( dt );
+       } else if (model == "magic") {
+           cur_fdm_state = new FGMagicCarpet( dt );
+       } else if (model == "external") {
+           cur_fdm_state = new FGExternal( dt );
+       } else if (model == "null") {
+           cur_fdm_state = new FGNullFDM( dt );
+       } else if (model == "yasim") {
+           cur_fdm_state = new YASim( dt );
+       } else {
+           SG_LOG(SG_GENERAL, SG_ALERT,
+                  "Unrecognized flight model '" << model
+                  << "', cannot init flight dynamics model.");
+           exit(-1);
+       }
+    } catch ( ... ) {
+       SG_LOG(SG_GENERAL, SG_ALERT, "FlightGear aborting\n\n");
+       exit(-1);
     }
 
     // Initialize view parameters
@@ -815,12 +978,12 @@ void fgReInitSubsystems( void )
     pilot_view->set_view_offset( 0.0 );
     pilot_view->set_goal_view_offset( 0.0 );
 
-    pilot_view->set_geod_view_pos( cur_fdm_state->get_Longitude(), 
-                                  cur_fdm_state->get_Lat_geocentric()
-                                  cur_fdm_state->get_Altitude() *
-                                  SG_FEET_TO_METER );
-    pilot_view->set_sea_level_radius( cur_fdm_state->get_Sea_level_radius() *
-                                     SG_FEET_TO_METER ); 
+    pilot_view->set_geod_view_pos( longitude->getDoubleValue()
+                                    * SGD_DEGREES_TO_RADIANS
+                                  latitude->getDoubleValue()
+                                    * SGD_DEGREES_TO_RADIANS, 
+                                  cur_fdm_state->get_Altitude()
+                                    * SG_FEET_TO_METER );
     pilot_view->set_rph( cur_fdm_state->get_Phi(),
                         cur_fdm_state->get_Theta(),
                         cur_fdm_state->get_Psi() );
@@ -831,9 +994,7 @@ void fgReInitSubsystems( void )
     SG_LOG( SG_GENERAL, SG_DEBUG, "  abs_view_pos = "
            << globals->get_current_view()->get_abs_view_pos());
 
-    cur_fdm_state->init();
-
-    controls.reset_all();
+    globals->get_controls()->reset_all();
     current_autopilot->reset();
 
     fgUpdateSunPos();