1 // fg_init.cxx -- Flight Gear top level initialization routines
3 // Written by Curtis Olson, started August 1997.
5 // Copyright (C) 1997 Curtis L. Olson - http://www.flightgear.org/~curt
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 // For BC 5.01 this must be included before OpenGL includes.
29 #ifdef SG_MATH_EXCEPTION_CLASH
35 #include <string.h> // strcmp()
37 #if defined( unix ) || defined( __CYGWIN__ )
38 # include <unistd.h> // for gethostname()
41 // work around a stdc++ lib bug in some versions of linux, but doesn't
42 // seem to hurt to have this here for all versions of Linux.
44 # define _G_NO_EXTERN_TEMPLATES
47 #include <simgear/compiler.h>
51 #include <simgear/constants.h>
52 #include <simgear/debug/logstream.hxx>
53 #include <simgear/structure/exception.hxx>
54 #include <simgear/structure/event_mgr.hxx>
55 #include <simgear/math/point3d.hxx>
56 #include <simgear/math/polar3d.hxx>
57 #include <simgear/math/sg_geodesy.hxx>
58 #include <simgear/misc/sg_path.hxx>
59 #include <simgear/misc/interpolator.hxx>
60 #include <simgear/scene/material/matlib.hxx>
61 #include <simgear/timing/sg_time.hxx>
62 #include <simgear/timing/lowleveltime.h>
64 #include <Aircraft/aircraft.hxx>
65 #include <Aircraft/replay.hxx>
66 #include <Airports/apt_loader.hxx>
67 #include <Airports/runways.hxx>
68 #include <Airports/simple.hxx>
69 #include <AIModel/AIManager.hxx>
70 #include <ATC/ATCdisplay.hxx>
71 #include <ATC/ATCmgr.hxx>
72 #include <ATC/AIMgr.hxx>
73 #include <Autopilot/route_mgr.hxx>
74 #include <Autopilot/xmlauto.hxx>
75 #include <Cockpit/cockpit.hxx>
76 #include <Cockpit/panel.hxx>
77 #include <Cockpit/panel_io.hxx>
79 #include <FDM/SP/ADA.hxx>
80 #include <FDM/SP/ACMS.hxx>
82 #include <FDM/Balloon.h>
83 #include <FDM/ExternalNet/ExternalNet.hxx>
84 #include <FDM/ExternalPipe/ExternalPipe.hxx>
85 #include <FDM/JSBSim/JSBSim.hxx>
86 #include <FDM/LaRCsim/LaRCsim.hxx>
87 #include <FDM/MagicCarpet.hxx>
88 #include <FDM/UFO.hxx>
89 #include <FDM/NullFDM.hxx>
90 #include <FDM/YASim/YASim.hxx>
91 #include <GUI/new_gui.hxx>
92 #include <Include/general.hxx>
93 #include <Input/input.hxx>
94 #include <Instrumentation/instrument_mgr.hxx>
95 #include <Model/acmodel.hxx>
96 #include <AIModel/submodel.hxx>
97 #include <AIModel/AIManager.hxx>
98 #include <Navaids/navdb.hxx>
99 #include <Navaids/navlist.hxx>
100 #include <Scenery/scenery.hxx>
101 #include <Scenery/tilemgr.hxx>
102 #include <Scripting/NasalSys.hxx>
103 #include <Sound/fg_fx.hxx>
104 #include <Sound/beacon.hxx>
105 #include <Sound/morse.hxx>
106 #include <Sound/voice.hxx>
107 #include <Systems/system_mgr.hxx>
108 #include <Time/light.hxx>
109 #include <Time/sunsolver.hxx>
110 #include <Time/tmp.hxx>
111 #include <Traffic/TrafficMgr.hxx>
112 #include <MultiPlayer/multiplaymgr.hxx>
114 #include <Environment/environment_mgr.hxx>
116 #include "fg_init.hxx"
118 #include "fg_commands.hxx"
119 #include "fg_props.hxx"
120 #include "options.hxx"
121 #include "globals.hxx"
122 #include "logger.hxx"
123 #include "viewmgr.hxx"
126 #if defined(FX) && defined(XMESA)
127 #include <GL/xmesa.h>
130 SG_USING_STD(string);
134 extern const char *default_root;
138 // Scan the command line options for the specified option and return
140 static string fgScanForOption( const string& option, int argc, char **argv ) {
143 if (hostname == NULL)
146 gethostname(_hostname, 256);
147 hostname = strdup(_hostname);
148 free_hostname = true;
151 SG_LOG(SG_GENERAL, SG_INFO, "Scanning command line for: " << option );
153 int len = option.length();
156 SG_LOG( SG_GENERAL, SG_DEBUG, "argv[" << i << "] = " << argv[i] );
158 string arg = argv[i];
159 if ( arg.find( option ) == 0 ) {
160 return arg.substr( len );
170 // Scan the user config files for the specified option and return
172 static string fgScanForOption( const string& option, const string& path ) {
173 sg_gzifstream in( path );
174 if ( !in.is_open() ) {
178 SG_LOG( SG_GENERAL, SG_INFO, "Scanning " << path << " for: " << option );
180 int len = option.length();
184 while ( ! in.eof() ) {
187 while ( in.get(c) && c != '\0' ) {
192 #if defined( macintosh )
193 getline( in, line, '\r' );
195 getline( in, line, '\n' );
198 // catch extraneous (DOS) line ending character
199 if ( line[line.length() - 1] < 32 ) {
200 line = line.substr( 0, line.length()-1 );
203 if ( line.find( option ) == 0 ) {
204 return line.substr( len );
213 // Scan the user config files for the specified option and return
215 static string fgScanForOption( const string& option ) {
218 #if defined( unix ) || defined( __CYGWIN__ )
219 // Next check home directory for .fgfsrc.hostname file
221 if ( homedir != NULL ) {
222 SGPath config( homedir );
223 config.append( ".fgfsrc" );
224 config.concat( "." );
225 config.concat( hostname );
226 arg = fgScanForOption( option, config.str() );
231 // Next check home directory for .fgfsrc file
233 if ( homedir != NULL ) {
234 SGPath config( homedir );
235 config.append( ".fgfsrc" );
236 arg = fgScanForOption( option, config.str() );
241 // Check for $fg_root/system.fgfsrc
242 SGPath config( globals->get_fg_root() );
243 config.append( "system.fgfsrc" );
244 arg = fgScanForOption( option, config.str() );
251 // Read in configuration (files and command line options) but only set
253 bool fgInitFGRoot ( int argc, char **argv ) {
256 // First parse command line options looking for --fg-root=, this
257 // will override anything specified in a config file
258 root = fgScanForOption( "--fg-root=", argc, argv);
260 // Check in one of the user configuration files.
262 root = fgScanForOption( "--fg-root=" );
264 // Next check if fg-root is set as an env variable
265 if ( root.empty() ) {
266 char *envp = ::getenv( "FG_ROOT" );
267 if ( envp != NULL ) {
272 // Otherwise, default to a random compiled-in location if we can't
273 // find fg-root any other way.
274 if ( root.empty() ) {
275 #if defined( __CYGWIN__ )
276 root = "/FlightGear";
277 #elif defined( WIN32 )
278 root = "\\FlightGear";
279 #elif defined(OSX_BUNDLE)
280 /* the following code looks for the base package directly inside
281 the application bundle. This can be changed fairly easily by
282 fiddling with the code below. And yes, I know it's ugly and verbose.
284 CFBundleRef appBundle = CFBundleGetMainBundle();
285 CFURLRef appUrl = CFBundleCopyBundleURL(appBundle);
286 CFRelease(appBundle);
288 // look for a 'data' subdir directly inside the bundle : is there
289 // a better place? maybe in Resources? I don't know ...
290 CFURLRef dataDir = CFURLCreateCopyAppendingPathComponent(NULL, appUrl, CFSTR("data"), true);
292 // now convert down to a path, and the a c-string
293 CFStringRef path = CFURLCopyFileSystemPath(dataDir, kCFURLPOSIXPathStyle);
294 root = CFStringGetCStringPtr(path, CFStringGetSystemEncoding());
297 CFRelease(appBundle);
305 SG_LOG(SG_INPUT, SG_INFO, "fg_root = " << root );
306 globals->set_fg_root(root);
312 // Read in configuration (files and command line options) but only set
314 bool fgInitFGAircraft ( int argc, char **argv ) {
317 // First parse command line options looking for --aircraft=, this
318 // will override anything specified in a config file
319 aircraft = fgScanForOption( "--aircraft=", argc, argv );
321 // Check in one of the user configuration files.
322 if ( aircraft.empty() )
323 aircraft = fgScanForOption( "--aircraft=" );
325 // if an aircraft was specified, set the property name
326 if ( !aircraft.empty() ) {
327 SG_LOG(SG_INPUT, SG_INFO, "aircraft = " << aircraft );
328 fgSetString("/sim/aircraft", aircraft.c_str() );
330 SG_LOG(SG_INPUT, SG_INFO, "No user specified aircraft, using default" );
337 // Return the current base package version
338 string fgBasePackageVersion() {
339 SGPath base_path( globals->get_fg_root() );
340 base_path.append("version");
342 sg_gzifstream in( base_path.str() );
343 if ( !in.is_open() ) {
344 SGPath old_path( globals->get_fg_root() );
345 old_path.append( "Thanks" );
346 sg_gzifstream old( old_path.str() );
347 if ( !old.is_open() ) {
350 return "[old version]";
361 // Initialize the localization
362 SGPropertyNode *fgInitLocale(const char *language) {
363 SGPropertyNode *c_node = NULL, *d_node = NULL;
364 SGPropertyNode *intl = fgGetNode("/sim/intl");
366 SG_LOG(SG_GENERAL, SG_INFO, "Selecting language: " << language );
368 // localization not defined
373 // Select the proper language from the list
375 vector<SGPropertyNode_ptr> locale = intl->getChildren("locale");
376 for (unsigned int i = 0; i < locale.size(); i++) {
378 vector<SGPropertyNode_ptr> lang = locale[i]->getChildren("lang");
379 for (unsigned int j = 0; j < lang.size(); j++) {
381 if (!strcmp(lang[j]->getStringValue(), language)) {
390 d_node = intl->getChild("locale");
394 // Check for localized font
395 SGPropertyNode *font_n = c_node->getNode("font", true);
396 if ( !strcmp(font_n->getStringValue(), "") )
397 font_n->setStringValue(d_node->getStringValue("font", "typewriter.txf"));
401 // Load the default strings
403 SGPath d_path( globals->get_fg_root() );
405 const char *d_path_str = d_node->getStringValue("strings");
407 SG_LOG(SG_GENERAL, SG_ALERT, "Incorrect path in configuration file.");
411 d_path.append(d_path_str);
412 SG_LOG(SG_GENERAL, SG_INFO, "Reading localized strings from "
415 SGPropertyNode *strings = c_node->getNode("strings");
417 readProperties(d_path.str(), strings);
418 } catch (const sg_exception &e) {
419 SG_LOG(SG_GENERAL, SG_ALERT, "Unable to read the localized strings");
424 // Load the language specific strings
426 if (c_node != d_node) {
427 SGPath c_path( globals->get_fg_root() );
429 const char *c_path_str = c_node->getStringValue("strings");
431 SG_LOG(SG_GENERAL, SG_ALERT, "Incorrect path in configuration file.");
435 c_path.append(c_path_str);
436 SG_LOG(SG_GENERAL, SG_INFO, "Reading localized strings from "
440 readProperties(c_path.str(), strings);
441 } catch (const sg_exception &e) {
442 SG_LOG(SG_GENERAL, SG_ALERT, "Unable to read the localized strings");
452 // Initialize the localization routines
453 bool fgDetectLanguage() {
454 char *language = ::getenv("LANG");
456 if (language == NULL) {
457 SG_LOG(SG_GENERAL, SG_INFO, "Unable to detect the language" );
461 SGPropertyNode *locale = fgInitLocale(language);
463 SG_LOG(SG_GENERAL, SG_ALERT,
464 "No internationalization settings specified in preferences.xml" );
469 globals->set_locale( locale );
474 // Attempt to locate and parse the various non-XML config files in order
475 // from least precidence to greatest precidence
477 do_options (int argc, char ** argv)
479 // Check for $fg_root/system.fgfsrc
480 SGPath config( globals->get_fg_root() );
481 config.append( "system.fgfsrc" );
482 fgParseOptions(config.str());
484 #if defined( unix ) || defined( __CYGWIN__ )
485 config.concat( "." );
486 config.concat( hostname );
487 fgParseOptions(config.str());
490 // Check for ~/.fgfsrc
491 if ( homedir != NULL ) {
492 config.set( homedir );
493 config.append( ".fgfsrc" );
494 fgParseOptions(config.str());
497 #if defined( unix ) || defined( __CYGWIN__ )
498 // Check for ~/.fgfsrc.hostname
499 config.concat( "." );
500 config.concat( hostname );
501 fgParseOptions(config.str());
504 // Parse remaining command line options
505 // These will override anything specified in a config file
506 fgParseArgs(argc, argv);
510 static string fgFindAircraftPath( const SGPath &path, const string &aircraft ) {
512 ulDir *dirp = ulOpenDir(path.str().c_str());
514 cerr << "Unable to open aircraft directory '" << path.str() << '\'' << endl;
519 while ((dire = ulReadDir(dirp)) != NULL) {
521 if ( strcmp("CVS", dire->d_name) && strcmp(".", dire->d_name)
522 && strcmp("..", dire->d_name) && strcmp("AI", dire->d_name))
525 next.append(dire->d_name);
527 result = fgFindAircraftPath( next, aircraft );
528 if ( ! result.empty() ) {
532 } else if ( !strcmp(dire->d_name, aircraft.c_str()) ) {
543 // Read in configuration (file and command line)
544 bool fgInitConfig ( int argc, char **argv ) {
546 // First, set some sane default values
549 // Read global preferences from $FG_ROOT/preferences.xml
550 SG_LOG(SG_INPUT, SG_INFO, "Reading global preferences");
551 fgLoadProps("preferences.xml", globals->get_props());
552 SG_LOG(SG_INPUT, SG_INFO, "Finished Reading global preferences");
554 // Detect the required language as early as possible
555 if ( !fgDetectLanguage() ) {
559 // Scan user config files and command line for a specified aircraft.
560 fgInitFGAircraft(argc, argv);
562 string aircraft = fgGetString( "/sim/aircraft", "" );
563 if ( aircraft.size() > 0 ) {
564 SGPath aircraft_search( globals->get_fg_root() );
565 aircraft_search.append( "Aircraft" );
567 string aircraft_set = aircraft + "-set.xml";
569 string result = fgFindAircraftPath( aircraft_search, aircraft_set );
570 if ( !result.empty() ) {
571 fgSetString( "/sim/aircraft-dir", result.c_str() );
572 SGPath full_name( result );
573 full_name.append( aircraft_set );
575 SG_LOG(SG_INPUT, SG_INFO, "Reading default aircraft: " << aircraft
576 << " from " << full_name.str());
578 readProperties( full_name.str(), globals->get_props() );
579 } catch ( const sg_exception &e ) {
580 string message = "Error reading default aircraft: ";
581 message += e.getFormattedMessage();
582 SG_LOG(SG_INPUT, SG_ALERT, message);
586 SG_LOG( SG_INPUT, SG_ALERT, "Cannot find specified aircraft: "
592 SG_LOG( SG_INPUT, SG_ALERT, "No default aircraft specified" );
596 char *envp = ::getenv( "APPDATA" );
598 SGPath config( envp );
599 config.append( "flightgear.org" );
601 if ( homedir != NULL ) {
602 SGPath config( homedir );
603 config.append( ".fgfs" );
605 fgSetString("/sim/fg-home", config.c_str());
606 config.append( "autosave.xml" );
607 SG_LOG(SG_INPUT, SG_INFO, "Reading user settings from autosave.xml");
609 readProperties(config.str(), globals->get_props(), SGPropertyNode::USERARCHIVE);
611 SG_LOG(SG_INPUT, SG_DEBUG, "First time reading user settings");
613 SG_LOG(SG_INPUT, SG_DEBUG, "Finished Reading user settings");
616 // parse options after loading aircraft to ensure any user
617 // overrides of defaults are honored.
618 do_options(argc, argv);
629 // This function is never used, but maybe useful in the future ???
632 // Preset lon/lat given an airport id
633 static bool fgSetPosFromAirportID( const string& id ) {
637 SG_LOG( SG_GENERAL, SG_INFO,
638 "Attempting to set starting position from airport code " << id );
640 if ( fgFindAirportID( id, &a ) ) {
642 fgSetDouble("/sim/presets/longitude-deg", a->longitude );
643 fgSetDouble("/sim/presets/latitude-deg", a->latitude );
645 // other code depends on the actual postition being set so set
647 fgSetDouble("/position/longitude-deg", a->longitude );
648 fgSetDouble("/position/latitude-deg", a->latitude );
650 SG_LOG( SG_GENERAL, SG_INFO,
651 "Position for " << id << " is (" << a->longitude
652 << ", " << a->latitude << ")" );
662 // Set current tower position lon/lat given an airport id
663 static bool fgSetTowerPosFromAirportID( const string& id, double hdg ) {
665 // tower height hard coded for now...
666 float towerheight=50.0f;
668 // make a little off the heading for 1 runway airports...
669 float fudge_lon = fabs(sin(hdg)) * .003f;
670 float fudge_lat = .003f - fudge_lon;
672 const FGAirport *a = fgFindAirportID( id);
674 fgSetDouble("/sim/tower/longitude-deg", a->getLongitude() + fudge_lon);
675 fgSetDouble("/sim/tower/latitude-deg", a->getLatitude() + fudge_lat);
676 fgSetDouble("/sim/tower/altitude-ft", a->getElevation() + towerheight);
684 struct FGTowerLocationListener : SGPropertyChangeListener {
685 void valueChanged(SGPropertyNode* node) {
686 const double hdg = fgGetDouble( "/orientation/heading-deg", 0);
687 const string id(node->getStringValue());
688 fgSetTowerPosFromAirportID(id, hdg);
692 static void fgInitTowerLocationListener() {
693 fgGetNode("/sim/tower/airport-id", true)
694 ->addChangeListener( new FGTowerLocationListener() );
697 // Set current_options lon/lat given an airport id and heading (degrees)
698 static bool fgSetPosFromAirportIDandHdg( const string& id, double tgt_hdg ) {
702 // set initial position from runway and heading
704 SG_LOG( SG_GENERAL, SG_INFO,
705 "Attempting to set starting position from airport code "
706 << id << " heading " << tgt_hdg );
708 if ( ! globals->get_runways()->search( id, (int)tgt_hdg, &r ) ) {
709 SG_LOG( SG_GENERAL, SG_ALERT,
710 "Failed to find a good runway for " << id << '\n' );
717 double lat2, lon2, az2;
718 double heading = r._heading;
719 double azimuth = heading + 180.0;
720 while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
722 SG_LOG( SG_GENERAL, SG_INFO,
723 "runway = " << r._lon << ", " << r._lat
724 << " length = " << r._length * SG_FEET_TO_METER
725 << " heading = " << azimuth );
727 geo_direct_wgs_84 ( 0, r._lat, r._lon, azimuth,
728 r._length * SG_FEET_TO_METER * 0.5 - 5.0,
729 &lat2, &lon2, &az2 );
731 if ( fabs( fgGetDouble("/sim/presets/offset-distance") ) > SG_EPSILON ) {
733 double odist = fgGetDouble("/sim/presets/offset-distance");
734 odist *= SG_NM_TO_METER;
735 double oaz = azimuth;
736 if ( fabs(fgGetDouble("/sim/presets/offset-azimuth")) > SG_EPSILON ) {
737 oaz = fgGetDouble("/sim/presets/offset-azimuth") + 180;
740 while ( oaz >= 360.0 ) { oaz -= 360.0; }
741 geo_direct_wgs_84 ( 0, lat2, lon2, oaz, odist, &olat, &olon, &az2 );
747 fgSetDouble("/sim/presets/longitude-deg", lon2 );
748 fgSetDouble("/sim/presets/latitude-deg", lat2 );
749 fgSetDouble("/sim/presets/heading-deg", heading );
751 // other code depends on the actual values being set ...
752 fgSetDouble("/position/longitude-deg", lon2 );
753 fgSetDouble("/position/latitude-deg", lat2 );
754 fgSetDouble("/orientation/heading-deg", heading );
756 SG_LOG( SG_GENERAL, SG_INFO,
757 "Position for " << id << " is ("
759 << lat2 << ") new heading is "
766 // Set current_options lon/lat given an airport id and runway number
767 static bool fgSetPosFromAirportIDandRwy( const string& id, const string& rwy, bool rwy_req ) {
771 // set initial position from airport and runway number
773 SG_LOG( SG_GENERAL, SG_INFO,
774 "Attempting to set starting position for "
775 << id << ":" << rwy );
777 if ( ! globals->get_runways()->search( id, rwy, &r ) ) {
778 SG_LOG( SG_GENERAL, rwy_req ? SG_ALERT : SG_INFO,
779 "Failed to find runway " << rwy <<
780 " at airport " << id << ". Using default runway." );
787 double lat2, lon2, az2;
788 double heading = r._heading;
789 double azimuth = heading + 180.0;
790 while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
792 SG_LOG( SG_GENERAL, SG_INFO,
793 "runway = " << r._lon << ", " << r._lat
794 << " length = " << r._length * SG_FEET_TO_METER
795 << " heading = " << azimuth );
797 geo_direct_wgs_84 ( 0, r._lat, r._lon,
799 r._length * SG_FEET_TO_METER * 0.5 - 5.0,
800 &lat2, &lon2, &az2 );
802 if ( fabs( fgGetDouble("/sim/presets/offset-distance") ) > SG_EPSILON )
805 double odist = fgGetDouble("/sim/presets/offset-distance");
806 odist *= SG_NM_TO_METER;
807 double oaz = azimuth;
808 if ( fabs(fgGetDouble("/sim/presets/offset-azimuth")) > SG_EPSILON )
810 oaz = fgGetDouble("/sim/presets/offset-azimuth") + 180;
811 heading = fgGetDouble("/sim/presets/heading-deg");
813 while ( oaz >= 360.0 ) { oaz -= 360.0; }
814 geo_direct_wgs_84 ( 0, lat2, lon2, oaz, odist, &olat, &olon, &az2 );
820 fgSetDouble("/sim/presets/longitude-deg", lon2 );
821 fgSetDouble("/sim/presets/latitude-deg", lat2 );
822 fgSetDouble("/sim/presets/heading-deg", heading );
824 // other code depends on the actual values being set ...
825 fgSetDouble("/position/longitude-deg", lon2 );
826 fgSetDouble("/position/latitude-deg", lat2 );
827 fgSetDouble("/orientation/heading-deg", heading );
829 SG_LOG( SG_GENERAL, SG_INFO,
830 "Position for " << id << " is ("
832 << lat2 << ") new heading is "
839 static void fgSetDistOrAltFromGlideSlope() {
840 // cout << "fgSetDistOrAltFromGlideSlope()" << endl;
841 string apt_id = fgGetString("/sim/presets/airport-id");
842 double gs = fgGetDouble("/sim/presets/glideslope-deg")
843 * SG_DEGREES_TO_RADIANS ;
844 double od = fgGetDouble("/sim/presets/offset-distance");
845 double alt = fgGetDouble("/sim/presets/altitude-ft");
847 double apt_elev = 0.0;
848 if ( ! apt_id.empty() ) {
849 apt_elev = fgGetAirportElev( apt_id );
850 if ( apt_elev < -9990.0 ) {
857 if( fabs(gs) > 0.01 && fabs(od) > 0.1 && alt < -9990 ) {
858 // set altitude from glideslope and offset-distance
859 od *= SG_NM_TO_METER * SG_METER_TO_FEET;
860 alt = fabs(od*tan(gs)) + apt_elev;
861 fgSetDouble("/sim/presets/altitude-ft", alt);
862 fgSetBool("/sim/presets/onground", false);
863 SG_LOG( SG_GENERAL, SG_INFO, "Calculated altitude as: "
865 } else if( fabs(gs) > 0.01 && alt > 0 && fabs(od) < 0.1) {
866 // set offset-distance from glideslope and altitude
867 od = (alt - apt_elev) / tan(gs);
868 od *= -1*SG_FEET_TO_METER * SG_METER_TO_NM;
869 fgSetDouble("/sim/presets/offset-distance", od);
870 fgSetBool("/sim/presets/onground", false);
871 SG_LOG( SG_GENERAL, SG_INFO, "Calculated offset distance as: "
873 } else if( fabs(gs) > 0.01 ) {
874 SG_LOG( SG_GENERAL, SG_ALERT,
875 "Glideslope given but not altitude or offset-distance." );
876 SG_LOG( SG_GENERAL, SG_ALERT, "Resetting glideslope to zero" );
877 fgSetDouble("/sim/presets/glideslope-deg", 0);
878 fgSetBool("/sim/presets/onground", true);
883 // Set current_options lon/lat given an airport id and heading (degrees)
884 static bool fgSetPosFromNAV( const string& id, const double& freq ) {
886 = globals->get_navlist()->findByIdentAndFreq( id.c_str(), freq );
888 // set initial position from runway and heading
890 SG_LOG( SG_GENERAL, SG_INFO, "Attempting to set starting position for "
891 << id << ":" << freq );
893 double lon = nav->get_lon();
894 double lat = nav->get_lat();
896 if ( fabs( fgGetDouble("/sim/presets/offset-distance") ) > SG_EPSILON )
898 double odist = fgGetDouble("/sim/presets/offset-distance")
900 double oaz = fabs(fgGetDouble("/sim/presets/offset-azimuth"))
902 while ( oaz >= 360.0 ) { oaz -= 360.0; }
903 double olat, olon, az2;
904 geo_direct_wgs_84 ( 0, lat, lon, oaz, odist, &olat, &olon, &az2 );
911 fgSetDouble("/sim/presets/longitude-deg", lon );
912 fgSetDouble("/sim/presets/latitude-deg", lat );
914 // other code depends on the actual values being set ...
915 fgSetDouble("/position/longitude-deg", lon );
916 fgSetDouble("/position/latitude-deg", lat );
917 fgSetDouble("/orientation/heading-deg",
918 fgGetDouble("/sim/presets/heading-deg") );
920 SG_LOG( SG_GENERAL, SG_INFO,
921 "Position for " << id << ":" << freq << " is ("
922 << lon << ", "<< lat << ")" );
926 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to locate NAV = "
927 << id << ":" << freq );
932 // Set current_options lon/lat given an aircraft carrier id
933 static bool fgSetPosFromCarrier( const string& carrier, const string& posid ) {
935 // set initial position from runway and heading
939 if (FGAIManager::getStartPosition(carrier, posid, geodPos, heading, uvw)) {
940 double lon = geodPos.getLongitudeDeg();
941 double lat = geodPos.getLatitudeDeg();
942 double alt = geodPos.getElevationFt();
944 SG_LOG( SG_GENERAL, SG_INFO, "Attempting to set starting position for "
945 << carrier << " at lat = " << lat << ", lon = " << lon
946 << ", alt = " << alt << ", heading = " << heading);
948 fgSetDouble("/sim/presets/longitude-deg", lon);
949 fgSetDouble("/sim/presets/latitude-deg", lat);
950 fgSetDouble("/sim/presets/altitude-ft", alt);
951 fgSetDouble("/sim/presets/heading-deg", heading);
952 fgSetDouble("/position/longitude-deg", lon);
953 fgSetDouble("/position/latitude-deg", lat);
954 fgSetDouble("/position/altitude-ft", alt);
955 fgSetDouble("/orientation/heading-deg", heading);
957 fgSetString("/sim/presets/speed-set", "UVW");
958 fgSetDouble("/velocities/uBody-fps", uvw(0));
959 fgSetDouble("/velocities/vBody-fps", uvw(1));
960 fgSetDouble("/velocities/wBody-fps", uvw(2));
961 fgSetDouble("/sim/presets/uBody-fps", uvw(0));
962 fgSetDouble("/sim/presets/vBody-fps", uvw(1));
963 fgSetDouble("/sim/presets/wBody-fps", uvw(2));
965 fgSetBool("/sim/presets/onground", true);
969 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to locate aircraft carrier = "
975 // Set current_options lon/lat given an airport id and heading (degrees)
976 static bool fgSetPosFromFix( const string& id ) {
979 // set initial position from runway and heading
980 if ( globals->get_fixlist()->query( id.c_str(), &fix ) ) {
981 SG_LOG( SG_GENERAL, SG_INFO, "Attempting to set starting position for "
984 double lon = fix.get_lon();
985 double lat = fix.get_lat();
987 if ( fabs( fgGetDouble("/sim/presets/offset-distance") ) > SG_EPSILON )
989 double odist = fgGetDouble("/sim/presets/offset-distance")
991 double oaz = fabs(fgGetDouble("/sim/presets/offset-azimuth"))
993 while ( oaz >= 360.0 ) { oaz -= 360.0; }
994 double olat, olon, az2;
995 geo_direct_wgs_84 ( 0, lat, lon, oaz, odist, &olat, &olon, &az2 );
1002 fgSetDouble("/sim/presets/longitude-deg", lon );
1003 fgSetDouble("/sim/presets/latitude-deg", lat );
1005 // other code depends on the actual values being set ...
1006 fgSetDouble("/position/longitude-deg", lon );
1007 fgSetDouble("/position/latitude-deg", lat );
1008 fgSetDouble("/orientation/heading-deg",
1009 fgGetDouble("/sim/presets/heading-deg") );
1011 SG_LOG( SG_GENERAL, SG_INFO,
1012 "Position for " << id << " is ("
1013 << lon << ", "<< lat << ")" );
1017 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to locate NAV = "
1025 * Initialize vor/ndb/ils/fix list management and query systems (as
1026 * well as simple airport db list)
1031 SG_LOG(SG_GENERAL, SG_INFO, "Loading Airport Database ...");
1033 SGPath aptdb( globals->get_fg_root() );
1034 aptdb.append( "Airports/apt.dat" );
1036 SGPath p_metar( globals->get_fg_root() );
1037 p_metar.append( "Airports/metar.dat" );
1039 FGAirportList *airports = new FGAirportList();
1040 globals->set_airports( airports );
1041 FGRunwayList *runways = new FGRunwayList();
1042 globals->set_runways( runways );
1044 fgAirportDBLoad( airports, runways, aptdb.str(), p_metar.str() );
1046 FGNavList *navlist = new FGNavList;
1047 FGNavList *loclist = new FGNavList;
1048 FGNavList *gslist = new FGNavList;
1049 FGNavList *dmelist = new FGNavList;
1050 FGNavList *mkrlist = new FGNavList;
1051 FGNavList *tacanlist = new FGNavList;
1052 FGNavList *carrierlist = new FGNavList;
1053 FGTACANList *channellist = new FGTACANList;
1055 globals->set_navlist( navlist );
1056 globals->set_loclist( loclist );
1057 globals->set_gslist( gslist );
1058 globals->set_dmelist( dmelist );
1059 globals->set_mkrlist( mkrlist );
1060 globals->set_tacanlist( tacanlist );
1061 globals->set_carrierlist( carrierlist );
1062 globals->set_channellist( channellist );
1064 if ( !fgNavDBInit(airports, navlist, loclist, gslist, dmelist, mkrlist, tacanlist, carrierlist, channellist) ) {
1065 SG_LOG( SG_GENERAL, SG_ALERT,
1066 "Problems loading one or more navigational database" );
1069 if ( fgGetBool("/sim/navdb/localizers/auto-align", true) ) {
1070 // align all the localizers with their corresponding runways
1071 // since data sources are good for cockpit navigation
1072 // purposes, but not always to the error tolerances needed to
1073 // exactly place these items.
1075 = fgGetDouble( "/sim/navdb/localizers/auto-align-threshold-deg",
1077 fgNavDBAlignLOCwithRunway( runways, loclist, threshold );
1080 SG_LOG(SG_GENERAL, SG_INFO, " Fixes");
1081 SGPath p_fix( globals->get_fg_root() );
1082 p_fix.append( "Navaids/fix.dat" );
1083 FGFixList *fixlist = new FGFixList;
1084 fixlist->init( p_fix );
1085 globals->set_fixlist( fixlist );
1087 SG_LOG(SG_GENERAL, SG_INFO, " Airways");
1088 SGPath p_awy( globals->get_fg_root() );
1089 p_awy.append( "Navaids/awy.dat" );
1090 FGAirwayNetwork *awyNet = new FGAirwayNetwork;
1091 //cerr << "Loading Airways" << endl;
1092 awyNet->load (p_awy );
1094 //cerr << "initializing airways" << endl;
1095 globals->set_airwaynet( awyNet );
1101 // Set the initial position based on presets (or defaults)
1102 bool fgInitPosition() {
1103 // cout << "fgInitPosition()" << endl;
1104 double gs = fgGetDouble("/sim/presets/glideslope-deg")
1105 * SG_DEGREES_TO_RADIANS ;
1106 double od = fgGetDouble("/sim/presets/offset-distance");
1107 double alt = fgGetDouble("/sim/presets/altitude-ft");
1109 bool set_pos = false;
1111 // If glideslope is specified, then calculate offset-distance or
1112 // altitude relative to glide slope if either of those was not
1114 if ( fabs( gs ) > 0.01 ) {
1115 fgSetDistOrAltFromGlideSlope();
1119 // If we have an explicit, in-range lon/lat, don't change it, just use it.
1120 // If not, check for an airport-id and use that.
1121 // If not, default to the middle of the KSFO field.
1122 // The default values for lon/lat are deliberately out of range
1123 // so that the airport-id can take effect; valid lon/lat will
1124 // override airport-id, however.
1125 double lon_deg = fgGetDouble("/sim/presets/longitude-deg");
1126 double lat_deg = fgGetDouble("/sim/presets/latitude-deg");
1127 if ( lon_deg >= -180.0 && lon_deg <= 180.0
1128 && lat_deg >= -90.0 && lat_deg <= 90.0 )
1133 string apt = fgGetString("/sim/presets/airport-id");
1134 string rwy_no = fgGetString("/sim/presets/runway");
1135 bool rwy_req = fgGetBool("/sim/presets/runway-requested");
1136 double hdg = fgGetDouble("/sim/presets/heading-deg");
1137 string vor = fgGetString("/sim/presets/vor-id");
1138 double vor_freq = fgGetDouble("/sim/presets/vor-freq");
1139 string ndb = fgGetString("/sim/presets/ndb-id");
1140 double ndb_freq = fgGetDouble("/sim/presets/ndb-freq");
1141 string carrier = fgGetString("/sim/presets/carrier");
1142 string parkpos = fgGetString("/sim/presets/parkpos");
1143 string fix = fgGetString("/sim/presets/fix");
1145 fgSetDouble( "/orientation/heading-deg", hdg );
1146 fgInitTowerLocationListener();
1148 if ( !set_pos && !apt.empty() && !rwy_no.empty() ) {
1149 // An airport + runway is requested
1150 if ( fgSetPosFromAirportIDandRwy( apt, rwy_no, rwy_req ) ) {
1151 // set tower position (a little off the heading for single
1153 fgSetString("/sim/tower/airport-id", apt.c_str());
1158 if ( !set_pos && !apt.empty() ) {
1159 // An airport is requested (find runway closest to hdg)
1160 if ( fgSetPosFromAirportIDandHdg( apt, hdg ) ) {
1161 // set tower position (a little off the heading for single
1163 fgSetString("/sim/tower/airport-id", apt.c_str());
1168 if ( !set_pos && !vor.empty() ) {
1169 // a VOR is requested
1170 if ( fgSetPosFromNAV( vor, vor_freq ) ) {
1175 if ( !set_pos && !ndb.empty() ) {
1176 // an NDB is requested
1177 if ( fgSetPosFromNAV( ndb, ndb_freq ) ) {
1182 if ( !set_pos && !carrier.empty() ) {
1183 // an aircraft carrier is requested
1184 if ( fgSetPosFromCarrier( carrier, parkpos ) ) {
1189 if ( !set_pos && !fix.empty() ) {
1190 // a Fix is requested
1191 if ( fgSetPosFromFix( fix ) ) {
1197 // No lon/lat specified, no airport specified, default to
1198 // middle of KSFO field.
1199 fgSetDouble("/sim/presets/longitude-deg", -122.374843);
1200 fgSetDouble("/sim/presets/latitude-deg", 37.619002);
1203 fgSetDouble( "/position/longitude-deg",
1204 fgGetDouble("/sim/presets/longitude-deg") );
1205 fgSetDouble( "/position/latitude-deg",
1206 fgGetDouble("/sim/presets/latitude-deg") );
1208 // determine if this should be an on-ground or in-air start
1209 if ((fabs(gs) > 0.01 || fabs(od) > 0.1 || alt > 0.1) && carrier.empty()) {
1210 fgSetBool("/sim/presets/onground", false);
1212 fgSetBool("/sim/presets/onground", true);
1219 // General house keeping initializations
1220 bool fgInitGeneral() {
1223 #if defined(FX) && defined(XMESA)
1224 char *mesa_win_state;
1227 SG_LOG( SG_GENERAL, SG_INFO, "General Initialization" );
1228 SG_LOG( SG_GENERAL, SG_INFO, "======= ==============" );
1230 root = globals->get_fg_root();
1231 if ( ! root.length() ) {
1232 // No root path set? Then bail ...
1233 SG_LOG( SG_GENERAL, SG_ALERT,
1234 "Cannot continue without a path to the base package "
1235 << "being defined." );
1238 SG_LOG( SG_GENERAL, SG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
1240 #if defined(FX) && defined(XMESA)
1241 // initialize full screen flag
1242 globals->set_fullscreen(false);
1243 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
1244 // Test for the MESA_GLX_FX env variable
1245 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
1246 // test if we are fullscreen mesa/glide
1247 if ( (mesa_win_state[0] == 'f') ||
1248 (mesa_win_state[0] == 'F') ) {
1249 globals->set_fullscreen(true);
1259 // Initialize the flight model subsystem. This just creates the
1260 // object. The actual fdm initialization is delayed until we get a
1261 // proper scenery elevation hit. This is checked for in main.cxx
1265 if ( cur_fdm_state ) {
1266 delete cur_fdm_state;
1270 double dt = 1.0 / fgGetInt("/sim/model-hz");
1271 string model = fgGetString("/sim/flight-model");
1274 if ( model == "larcsim" ) {
1275 cur_fdm_state = new FGLaRCsim( dt );
1276 } else if ( model == "jsb" ) {
1277 cur_fdm_state = new FGJSBsim( dt );
1279 } else if ( model == "ada" ) {
1280 cur_fdm_state = new FGADA( dt );
1281 } else if ( model == "acms" ) {
1282 cur_fdm_state = new FGACMS( dt );
1284 } else if ( model == "balloon" ) {
1285 cur_fdm_state = new FGBalloonSim( dt );
1286 } else if ( model == "magic" ) {
1287 cur_fdm_state = new FGMagicCarpet( dt );
1288 } else if ( model == "ufo" ) {
1289 cur_fdm_state = new FGUFO( dt );
1290 } else if ( model == "external" ) {
1291 // external is a synonym for "--fdm=null" and is
1292 // maintained here for backwards compatibility
1293 cur_fdm_state = new FGNullFDM( dt );
1294 } else if ( model.find("network") == 0 ) {
1295 string host = "localhost";
1299 string net_options = model.substr(8);
1300 string::size_type begin, end;
1303 end = net_options.find( ",", begin );
1304 if ( end != string::npos ) {
1305 host = net_options.substr(begin, end - begin);
1309 end = net_options.find( ",", begin );
1310 if ( end != string::npos ) {
1311 port1 = atoi( net_options.substr(begin, end - begin).c_str() );
1315 end = net_options.find( ",", begin );
1316 if ( end != string::npos ) {
1317 port2 = atoi( net_options.substr(begin, end - begin).c_str() );
1321 end = net_options.find( ",", begin );
1322 if ( end != string::npos ) {
1323 port3 = atoi( net_options.substr(begin, end - begin).c_str() );
1326 cur_fdm_state = new FGExternalNet( dt, host, port1, port2, port3 );
1327 } else if ( model.find("pipe") == 0 ) {
1328 // /* old */ string pipe_path = model.substr(5);
1329 // /* old */ cur_fdm_state = new FGExternalPipe( dt, pipe_path );
1330 string pipe_path = "";
1331 string pipe_protocol = "";
1332 string pipe_options = model.substr(5);
1333 string::size_type begin, end;
1336 end = pipe_options.find( ",", begin );
1337 if ( end != string::npos ) {
1338 pipe_path = pipe_options.substr(begin, end - begin);
1341 // protocol (last option)
1342 pipe_protocol = pipe_options.substr(begin);
1343 cur_fdm_state = new FGExternalPipe( dt, pipe_path, pipe_protocol );
1344 } else if ( model == "null" ) {
1345 cur_fdm_state = new FGNullFDM( dt );
1346 } else if ( model == "yasim" ) {
1347 cur_fdm_state = new YASim( dt );
1349 SG_LOG(SG_GENERAL, SG_ALERT,
1350 "Unrecognized flight model '" << model
1351 << "', cannot init flight dynamics model.");
1355 SG_LOG(SG_GENERAL, SG_ALERT, "FlightGear aborting\n\n");
1360 static void printMat(const sgVec4 *mat, char *name="")
1363 SG_LOG(SG_GENERAL, SG_BULK, name );
1364 for(i=0; i<4; i++) {
1365 SG_LOG(SG_GENERAL, SG_BULK, " " << mat[i][0] << " " << mat[i][1]
1366 << " " << mat[i][2] << " " << mat[i][3] );
1370 // Initialize view parameters
1372 // force update of model so that viewer can get some data...
1373 globals->get_aircraft_model()->update(0);
1374 // run update for current view so that data is current...
1375 globals->get_viewmgr()->update(0);
1377 printMat(globals->get_current_view()->get_VIEW(),"VIEW");
1378 printMat(globals->get_current_view()->get_UP(),"UP");
1379 // printMat(globals->get_current_view()->get_LOCAL(),"LOCAL");
1384 SGTime *fgInitTime() {
1386 static const SGPropertyNode *longitude
1387 = fgGetNode("/position/longitude-deg");
1388 static const SGPropertyNode *latitude
1389 = fgGetNode("/position/latitude-deg");
1390 static const SGPropertyNode *cur_time_override
1391 = fgGetNode("/sim/time/cur-time-override", true);
1393 SGPath zone( globals->get_fg_root() );
1394 zone.append( "Timezone" );
1395 SGTime *t = new SGTime( longitude->getDoubleValue()
1396 * SGD_DEGREES_TO_RADIANS,
1397 latitude->getDoubleValue()
1398 * SGD_DEGREES_TO_RADIANS,
1400 cur_time_override->getLongValue() );
1402 globals->set_warp_delta( 0 );
1404 t->update( 0.0, 0.0,
1405 cur_time_override->getLongValue(),
1406 globals->get_warp() );
1412 // set up a time offset (aka warp) if one is specified
1413 void fgInitTimeOffset() {
1414 static const SGPropertyNode *longitude
1415 = fgGetNode("/position/longitude-deg");
1416 static const SGPropertyNode *latitude
1417 = fgGetNode("/position/latitude-deg");
1418 static const SGPropertyNode *cur_time_override
1419 = fgGetNode("/sim/time/cur-time-override", true);
1421 // Handle potential user specified time offsets
1422 int orig_warp = globals->get_warp();
1423 SGTime *t = globals->get_time_params();
1424 time_t cur_time = t->get_cur_time();
1425 time_t currGMT = sgTimeGetGMT( gmtime(&cur_time) );
1426 time_t systemLocalTime = sgTimeGetGMT( localtime(&cur_time) );
1427 time_t aircraftLocalTime =
1428 sgTimeGetGMT( fgLocaltime(&cur_time, t->get_zonename() ) );
1430 // Okay, we now have several possible scenarios
1431 int offset = fgGetInt("/sim/startup/time-offset");
1432 string offset_type = fgGetString("/sim/startup/time-offset-type");
1435 if ( offset_type == "real" ) {
1437 } else if ( offset_type == "dawn" ) {
1438 warp = fgTimeSecondsUntilSunAngle( cur_time,
1439 longitude->getDoubleValue()
1440 * SGD_DEGREES_TO_RADIANS,
1441 latitude->getDoubleValue()
1442 * SGD_DEGREES_TO_RADIANS,
1444 } else if ( offset_type == "morning" ) {
1445 warp = fgTimeSecondsUntilSunAngle( cur_time,
1446 longitude->getDoubleValue()
1447 * SGD_DEGREES_TO_RADIANS,
1448 latitude->getDoubleValue()
1449 * SGD_DEGREES_TO_RADIANS,
1451 } else if ( offset_type == "noon" ) {
1452 warp = fgTimeSecondsUntilSunAngle( cur_time,
1453 longitude->getDoubleValue()
1454 * SGD_DEGREES_TO_RADIANS,
1455 latitude->getDoubleValue()
1456 * SGD_DEGREES_TO_RADIANS,
1458 } else if ( offset_type == "afternoon" ) {
1459 warp = fgTimeSecondsUntilSunAngle( cur_time,
1460 longitude->getDoubleValue()
1461 * SGD_DEGREES_TO_RADIANS,
1462 latitude->getDoubleValue()
1463 * SGD_DEGREES_TO_RADIANS,
1465 } else if ( offset_type == "dusk" ) {
1466 warp = fgTimeSecondsUntilSunAngle( cur_time,
1467 longitude->getDoubleValue()
1468 * SGD_DEGREES_TO_RADIANS,
1469 latitude->getDoubleValue()
1470 * SGD_DEGREES_TO_RADIANS,
1472 } else if ( offset_type == "evening" ) {
1473 warp = fgTimeSecondsUntilSunAngle( cur_time,
1474 longitude->getDoubleValue()
1475 * SGD_DEGREES_TO_RADIANS,
1476 latitude->getDoubleValue()
1477 * SGD_DEGREES_TO_RADIANS,
1479 } else if ( offset_type == "midnight" ) {
1480 warp = fgTimeSecondsUntilSunAngle( cur_time,
1481 longitude->getDoubleValue()
1482 * SGD_DEGREES_TO_RADIANS,
1483 latitude->getDoubleValue()
1484 * SGD_DEGREES_TO_RADIANS,
1486 } else if ( offset_type == "system-offset" ) {
1489 } else if ( offset_type == "gmt-offset" ) {
1490 warp = offset - (currGMT - systemLocalTime);
1492 } else if ( offset_type == "latitude-offset" ) {
1493 warp = offset - (aircraftLocalTime - systemLocalTime);
1495 } else if ( offset_type == "system" ) {
1496 warp = offset - (systemLocalTime - currGMT) - cur_time;
1497 } else if ( offset_type == "gmt" ) {
1498 warp = offset - cur_time;
1499 } else if ( offset_type == "latitude" ) {
1500 warp = offset - (aircraftLocalTime - currGMT)- cur_time;
1502 SG_LOG( SG_GENERAL, SG_ALERT,
1503 "FG_TIME::Unsupported offset type " << offset_type );
1506 globals->set_warp( orig_warp + warp );
1507 t->update( longitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
1508 latitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
1509 cur_time_override->getLongValue(),
1510 globals->get_warp() );
1512 SG_LOG( SG_GENERAL, SG_INFO, "After fgInitTimeOffset(): warp = "
1513 << globals->get_warp() );
1517 // This is the top level init routine which calls all the other
1518 // initialization routines. If you are adding a subsystem to flight
1519 // gear, its initialization call should located in this routine.
1520 // Returns non-zero if a problem encountered.
1521 bool fgInitSubsystems() {
1522 // static const SGPropertyNode *longitude
1523 // = fgGetNode("/sim/presets/longitude-deg");
1524 // static const SGPropertyNode *latitude
1525 // = fgGetNode("/sim/presets/latitude-deg");
1526 // static const SGPropertyNode *altitude
1527 // = fgGetNode("/sim/presets/altitude-ft");
1528 SG_LOG( SG_GENERAL, SG_INFO, "Initialize Subsystems");
1529 SG_LOG( SG_GENERAL, SG_INFO, "========== ==========");
1531 ////////////////////////////////////////////////////////////////////
1532 // Initialize the event manager subsystem.
1533 ////////////////////////////////////////////////////////////////////
1535 globals->get_event_mgr()->init();
1536 globals->get_event_mgr()->setRealtimeProperty(fgGetNode("/sim/time/delta-realtime-sec", true));
1538 ////////////////////////////////////////////////////////////////////
1539 // Initialize the property interpolator subsystem
1540 ////////////////////////////////////////////////////////////////////
1541 globals->add_subsystem("interpolator", new SGInterpolator);
1544 ////////////////////////////////////////////////////////////////////
1545 // Add the FlightGear property utilities.
1546 ////////////////////////////////////////////////////////////////////
1547 globals->add_subsystem("properties", new FGProperties);
1549 ////////////////////////////////////////////////////////////////////
1550 // Initialize the material property subsystem.
1551 ////////////////////////////////////////////////////////////////////
1553 SGPath mpath( globals->get_fg_root() );
1554 mpath.append( "materials.xml" );
1555 string season = fgGetString("/sim/startup/season", "summer");
1556 if ( ! globals->get_matlib()->load(globals->get_fg_root(), mpath.str(), season.c_str()) ) {
1557 SG_LOG( SG_GENERAL, SG_ALERT, "Error loading material lib!" );
1562 ////////////////////////////////////////////////////////////////////
1563 // Initialize the scenery management subsystem.
1564 ////////////////////////////////////////////////////////////////////
1566 if ( globals->get_tile_mgr()->init() ) {
1567 // Load the local scenery data
1568 double visibility_meters = fgGetDouble("/environment/visibility-m");
1570 globals->get_tile_mgr()->update( visibility_meters );
1572 SG_LOG( SG_GENERAL, SG_ALERT, "Error in Tile Manager initialization!" );
1576 // cause refresh of viewer scenery timestamps every 15 seconds...
1577 globals->get_event_mgr()->addTask( "FGTileMgr::refresh_view_timestamps()",
1578 globals->get_tile_mgr(),
1579 &FGTileMgr::refresh_view_timestamps,
1583 ////////////////////////////////////////////////////////////////////
1584 // Initialize the flight model subsystem.
1585 ////////////////////////////////////////////////////////////////////
1589 // allocates structures so must happen before any of the flight
1590 // model or control parameters are set
1591 fgAircraftInit(); // In the future this might not be the case.
1593 ////////////////////////////////////////////////////////////////////
1594 // Initialize the aircraft systems and instrumentation (before the
1596 ////////////////////////////////////////////////////////////////////
1598 globals->add_subsystem("instrumentation", new FGInstrumentMgr);
1599 globals->add_subsystem("systems", new FGSystemMgr);
1601 ////////////////////////////////////////////////////////////////////
1602 // Initialize the XML Autopilot subsystem.
1603 ////////////////////////////////////////////////////////////////////
1605 globals->add_subsystem( "xml-autopilot", new FGXMLAutopilot );
1606 globals->add_subsystem( "route-manager", new FGRouteMgr );
1609 ////////////////////////////////////////////////////////////////////
1610 // Initialize the view manager subsystem.
1611 ////////////////////////////////////////////////////////////////////
1615 ////////////////////////////////////////////////////////////////////
1616 // Create and register the logger.
1617 ////////////////////////////////////////////////////////////////////
1619 globals->add_subsystem("logger", new FGLogger);
1621 ////////////////////////////////////////////////////////////////////
1622 // Create and register the XML GUI.
1623 ////////////////////////////////////////////////////////////////////
1625 globals->add_subsystem("gui", new NewGUI, SGSubsystemMgr::INIT);
1628 ////////////////////////////////////////////////////////////////////
1629 // Initialize the local time subsystem.
1630 ////////////////////////////////////////////////////////////////////
1632 // update the current timezone each 30 minutes
1633 globals->get_event_mgr()->addTask( "fgUpdateLocalTime()",
1634 &fgUpdateLocalTime, 30*60 );
1635 fgInitTimeOffset(); // the environment subsystem needs this
1638 ////////////////////////////////////////////////////////////////////
1639 // Initialize the weather subsystem.
1640 ////////////////////////////////////////////////////////////////////
1642 // Initialize the weather modeling subsystem
1643 globals->add_subsystem("environment", new FGEnvironmentMgr);
1646 ////////////////////////////////////////////////////////////////////
1647 // Initialize the lighting subsystem.
1648 ////////////////////////////////////////////////////////////////////
1650 globals->add_subsystem("lighting", new FGLight);
1652 //////////////////////////////////////////////////////////////////////
1653 // Initialize the 2D cloud subsystem.
1654 ////////////////////////////////////////////////////////////////////
1655 fgGetBool("/sim/rendering/bump-mapping", false);
1657 #ifdef ENABLE_AUDIO_SUPPORT
1658 ////////////////////////////////////////////////////////////////////
1659 // Initialize the sound subsystem.
1660 ////////////////////////////////////////////////////////////////////
1662 init_volume = fgGetFloat("/sim/sound/volume");
1663 fgSetFloat("/sim/sound/volume", 0.0f);
1664 globals->set_soundmgr(new SGSoundMgr);
1665 globals->get_soundmgr()->init();
1666 globals->get_soundmgr()->bind();
1669 ////////////////////////////////////////////////////////////////////
1670 // Initialize the sound-effects subsystem.
1671 ////////////////////////////////////////////////////////////////////
1673 globals->add_subsystem("fx", new FGFX);
1674 globals->add_subsystem("voice", new FGVoiceMgr);
1677 ////////////////////////////////////////////////////////////////////
1678 // Initialise ATC display system
1679 ////////////////////////////////////////////////////////////////////
1681 SG_LOG(SG_GENERAL, SG_INFO, " ATC Display");
1682 globals->set_ATC_display(new FGATCDisplay);
1683 globals->get_ATC_display()->init();
1685 ////////////////////////////////////////////////////////////////////
1686 // Initialise the ATC Manager
1687 ////////////////////////////////////////////////////////////////////
1689 SG_LOG(SG_GENERAL, SG_INFO, " ATC Manager");
1690 globals->set_ATC_mgr(new FGATCMgr);
1691 globals->get_ATC_mgr()->init();
1693 ////////////////////////////////////////////////////////////////////
1694 // Initialise the AI Manager
1695 ////////////////////////////////////////////////////////////////////
1697 SG_LOG(SG_GENERAL, SG_INFO, " AI Manager");
1698 globals->set_AI_mgr(new FGAIMgr);
1699 globals->get_AI_mgr()->init();
1701 ////////////////////////////////////////////////////////////////////
1702 // Initialise the AI Model Manager
1703 ////////////////////////////////////////////////////////////////////
1704 SG_LOG(SG_GENERAL, SG_INFO, " AI Model Manager");
1705 globals->add_subsystem("ai_model", new FGAIManager);
1706 globals->add_subsystem("submodel_mgr", new FGSubmodelMgr);
1709 // It's probably a good idea to initialize the top level traffic manager
1710 // After the AI and ATC systems have been initialized properly.
1711 // AI Traffic manager
1712 globals->add_subsystem("Traffic Manager", new FGTrafficManager);
1713 FGTrafficManager *dispatcher =
1714 (FGTrafficManager*) globals->get_subsystem("Traffic Manager");
1715 SGPath path = globals->get_fg_root();
1716 path.append("/Traffic/fgtraffic.xml");
1719 //globals->get_subsystem("Traffic Manager")->init();
1722 ////////////////////////////////////////////////////////////////////
1723 // Initialize the cockpit subsystem
1724 ////////////////////////////////////////////////////////////////////
1725 if( fgCockpitInit( ¤t_aircraft )) {
1726 // Cockpit initialized ok.
1728 SG_LOG( SG_GENERAL, SG_ALERT, "Error in Cockpit initialization!" );
1733 ////////////////////////////////////////////////////////////////////
1734 // Initialize I/O subsystem.
1735 ////////////////////////////////////////////////////////////////////
1737 globals->get_io()->init();
1738 globals->get_io()->bind();
1741 ////////////////////////////////////////////////////////////////////
1742 // Add a new 2D panel.
1743 ////////////////////////////////////////////////////////////////////
1745 string panel_path = fgGetString("/sim/panel/path",
1746 "Panels/Default/default.xml");
1748 globals->set_current_panel( fgReadPanel(panel_path) );
1749 if (globals->get_current_panel() == 0) {
1750 SG_LOG( SG_INPUT, SG_ALERT,
1751 "Error reading new panel from " << panel_path );
1753 SG_LOG( SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path );
1754 globals->get_current_panel()->init();
1755 globals->get_current_panel()->bind();
1759 ////////////////////////////////////////////////////////////////////
1760 // Initialize the controls subsystem.
1761 ////////////////////////////////////////////////////////////////////
1763 globals->get_controls()->init();
1764 globals->get_controls()->bind();
1767 ////////////////////////////////////////////////////////////////////
1768 // Initialize the input subsystem.
1769 ////////////////////////////////////////////////////////////////////
1771 globals->add_subsystem("input", new FGInput);
1774 ////////////////////////////////////////////////////////////////////
1775 // Initialize the replay subsystem
1776 ////////////////////////////////////////////////////////////////////
1777 globals->add_subsystem("replay", new FGReplay);
1779 ////////////////////////////////////////////////////////////////////
1780 // Bind and initialize subsystems.
1781 ////////////////////////////////////////////////////////////////////
1783 globals->get_subsystem_mgr()->bind();
1784 globals->get_subsystem_mgr()->init();
1786 ////////////////////////////////////////////////////////////////////
1787 // Initialize multiplayer subsystem
1788 ////////////////////////////////////////////////////////////////////
1790 globals->set_multiplayer_mgr(new FGMultiplayMgr);
1791 globals->get_multiplayer_mgr()->init();
1793 ////////////////////////////////////////////////////////////////////////
1794 // Initialize the Nasal interpreter.
1795 // Do this last, so that the loaded scripts see initialized state
1796 ////////////////////////////////////////////////////////////////////////
1797 FGNasalSys* nasal = new FGNasalSys();
1798 globals->add_subsystem("nasal", nasal, SGSubsystemMgr::INIT);
1801 // initialize methods that depend on other subsystems.
1802 globals->get_subsystem_mgr()->postinit();
1804 ////////////////////////////////////////////////////////////////////////
1805 // End of subsystem initialization.
1806 ////////////////////////////////////////////////////////////////////
1808 SG_LOG( SG_GENERAL, SG_INFO, endl);
1810 // Save the initial state for future
1812 globals->saveInitialState();
1818 void fgReInitSubsystems()
1820 // static const SGPropertyNode *longitude
1821 // = fgGetNode("/sim/presets/longitude-deg");
1822 // static const SGPropertyNode *latitude
1823 // = fgGetNode("/sim/presets/latitude-deg");
1824 static const SGPropertyNode *altitude
1825 = fgGetNode("/sim/presets/altitude-ft");
1826 static const SGPropertyNode *master_freeze
1827 = fgGetNode("/sim/freeze/master");
1829 SG_LOG( SG_GENERAL, SG_INFO,
1830 "fgReInitSubsystems(): /position/altitude = "
1831 << altitude->getDoubleValue() );
1833 bool freeze = master_freeze->getBoolValue();
1835 fgSetBool("/sim/freeze/master", true);
1837 fgSetBool("/sim/crashed", false);
1839 // Force reupdating the positions of the ai 3d models. They are used for
1840 // initializing ground level for the FDM.
1841 globals->get_subsystem("ai_model")->reinit();
1843 // Initialize the FDM
1846 // allocates structures so must happen before any of the flight
1847 // model or control parameters are set
1848 fgAircraftInit(); // In the future this might not be the case.
1850 // reload offsets from config defaults
1851 globals->get_viewmgr()->reinit();
1855 globals->get_controls()->reset_all();
1857 fgUpdateLocalTime();
1859 // re-init to proper time of day setting
1863 fgSetBool("/sim/freeze/master", false);
1865 fgSetBool("/sim/sceneryloaded",false);