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., 675 Mass Ave, Cambridge, MA 02139, USA.
29 // For BC 5.01 this must be included before OpenGL includes.
30 #ifdef SG_MATH_EXCEPTION_CLASH
36 #include <string.h> // strcmp()
39 #if defined( unix ) || defined( __CYGWIN__ )
40 # include <unistd.h> // for gethostname()
43 // work around a stdc++ lib bug in some versions of linux, but doesn't
44 // seem to hurt to have this here for all versions of Linux.
46 # define _G_NO_EXTERN_TEMPLATES
49 #include <simgear/compiler.h>
53 #include <simgear/constants.h>
54 #include <simgear/debug/logstream.hxx>
55 #include <simgear/structure/exception.hxx>
56 #include <simgear/structure/event_mgr.hxx>
57 #include <simgear/math/point3d.hxx>
58 #include <simgear/math/polar3d.hxx>
59 #include <simgear/math/sg_geodesy.hxx>
60 #include <simgear/misc/sg_path.hxx>
61 #include <simgear/misc/interpolator.hxx>
62 #include <simgear/scene/material/matlib.hxx>
63 #ifdef FG_USE_CLOUDS_3D
64 # include <simgear/scene/sky/clouds3d/SkySceneLoader.hpp>
65 # include <simgear/scene/sky/clouds3d/SkyUtil.hpp>
66 # include <simgear/screen/texture.hxx>
68 #include <simgear/timing/sg_time.hxx>
69 #include <simgear/timing/lowleveltime.h>
71 #include <Aircraft/aircraft.hxx>
72 #include <FDM/UIUCModel/uiuc_aircraftdir.h>
73 #include <Airports/apt_loader.hxx>
74 #include <Airports/runways.hxx>
75 #include <Airports/simple.hxx>
76 #include <ATC/ATCdisplay.hxx>
77 #include <ATC/ATCmgr.hxx>
78 #include <ATC/AIMgr.hxx>
79 #include <Autopilot/auto_gui.hxx>
80 #include <Autopilot/route_mgr.hxx>
81 #include <Autopilot/xmlauto.hxx>
82 #include <Cockpit/cockpit.hxx>
83 #include <Cockpit/panel.hxx>
84 #include <Cockpit/panel_io.hxx>
86 #include <FDM/SP/ADA.hxx>
87 #include <FDM/SP/ACMS.hxx>
89 #include <FDM/Balloon.h>
90 #include <FDM/ExternalNet/ExternalNet.hxx>
91 #include <FDM/ExternalPipe/ExternalPipe.hxx>
92 #include <FDM/JSBSim/JSBSim.hxx>
93 #include <FDM/LaRCsim/LaRCsim.hxx>
94 #include <FDM/MagicCarpet.hxx>
95 #include <FDM/UFO.hxx>
96 #include <FDM/NullFDM.hxx>
97 #include <FDM/YASim/YASim.hxx>
98 #include <GUI/new_gui.hxx>
99 #include <Include/general.hxx>
100 #include <Input/input.hxx>
101 #include <Instrumentation/instrument_mgr.hxx>
102 #include <Model/acmodel.hxx>
103 #include <AIModel/submodel.hxx>
104 #include <AIModel/AIManager.hxx>
105 #include <Navaids/navdb.hxx>
106 #include <Navaids/navlist.hxx>
107 #include <Replay/replay.hxx>
108 #include <Scenery/scenery.hxx>
109 #include <Scenery/tilemgr.hxx>
110 #include <Scripting/NasalSys.hxx>
111 #include <Sound/fg_fx.hxx>
112 #include <Sound/beacon.hxx>
113 #include <Sound/morse.hxx>
114 #include <Systems/system_mgr.hxx>
115 #include <Time/light.hxx>
116 #include <Time/moonpos.hxx>
117 #include <Time/sunpos.hxx>
118 #include <Time/sunsolver.hxx>
119 #include <Time/tmp.hxx>
120 #include <Traffic/TrafficMgr.hxx>
123 #include <MultiPlayer/multiplaytxmgr.hxx>
124 #include <MultiPlayer/multiplayrxmgr.hxx>
127 #include <Environment/environment_mgr.hxx>
129 #include "fg_init.hxx"
131 #include "fg_commands.hxx"
132 #include "fg_props.hxx"
133 #include "options.hxx"
134 #include "globals.hxx"
135 #include "logger.hxx"
136 #include "viewmgr.hxx"
138 #if defined(FX) && defined(XMESA)
139 #include <GL/xmesa.h>
142 SG_USING_STD(string);
146 extern const char *default_root;
150 #ifdef FG_USE_CLOUDS_3D
151 SkySceneLoader *sgCloud3d;
155 // Scan the command line options for the specified option and return
157 static string fgScanForOption( const string& option, int argc, char **argv ) {
160 SG_LOG(SG_GENERAL, SG_INFO, "Scanning command line for: " << option );
162 int len = option.length();
165 SG_LOG( SG_GENERAL, SG_DEBUG, "argv[" << i << "] = " << argv[i] );
167 string arg = argv[i];
168 if ( arg.find( option ) == 0 ) {
169 return arg.substr( len );
179 // Scan the user config files for the specified option and return
181 static string fgScanForOption( const string& option, const string& path ) {
182 sg_gzifstream in( path );
183 if ( !in.is_open() ) {
187 SG_LOG( SG_GENERAL, SG_INFO, "Scanning " << path << " for: " << option );
189 int len = option.length();
193 while ( ! in.eof() ) {
196 while ( in.get(c) && c != '\0' ) {
201 #if defined( macintosh )
202 getline( in, line, '\r' );
204 getline( in, line, '\n' );
207 // catch extraneous (DOS) line ending character
208 if ( line[line.length() - 1] < 32 ) {
209 line = line.substr( 0, line.length()-1 );
212 if ( line.find( option ) == 0 ) {
213 return line.substr( len );
223 // Read in configuration (files and command line options) but only set
225 bool fgInitFGRoot ( int argc, char **argv ) {
229 // First parse command line options looking for --fg-root=, this
230 // will override anything specified in a config file
231 root = fgScanForOption( "--fg-root=", argc, argv);
233 #if defined( unix ) || defined( __CYGWIN__ )
234 // Next check home directory for .fgfsrc.hostname file
235 if ( root.empty() ) {
236 envp = ::getenv( "HOME" );
237 if ( envp != NULL ) {
238 SGPath config( envp );
239 config.append( ".fgfsrc" );
241 gethostname( name, 256 );
242 config.concat( "." );
243 config.concat( name );
244 root = fgScanForOption( "--fg-root=", config.str() );
249 // Next check home directory for .fgfsrc file
250 if ( root.empty() ) {
251 envp = ::getenv( "HOME" );
252 if ( envp != NULL ) {
253 SGPath config( envp );
254 config.append( ".fgfsrc" );
255 root = fgScanForOption( "--fg-root=", config.str() );
259 // Next check if fg-root is set as an env variable
260 if ( root.empty() ) {
261 envp = ::getenv( "FG_ROOT" );
262 if ( envp != NULL ) {
267 // Otherwise, default to a random compiled-in location if we can't
268 // find fg-root any other way.
269 if ( root.empty() ) {
270 #if defined( __CYGWIN__ )
271 root = "/FlightGear";
272 #elif defined( WIN32 )
273 root = "\\FlightGear";
274 #elif defined(OSX_BUNDLE)
275 /* the following code looks for the base package directly inside
276 the application bundle. This can be changed fairly easily by
277 fiddling with the code below. And yes, I know it's ugly and verbose.
279 CFBundleRef appBundle = CFBundleGetMainBundle();
280 CFURLRef appUrl = CFBundleCopyBundleURL(appBundle);
281 CFRelease(appBundle);
283 // look for a 'data' subdir directly inside the bundle : is there
284 // a better place? maybe in Resources? I don't know ...
285 CFURLRef dataDir = CFURLCreateCopyAppendingPathComponent(NULL, appUrl, CFSTR("data"), true);
287 // now convert down to a path, and the a c-string
288 CFStringRef path = CFURLCopyFileSystemPath(dataDir, kCFURLPOSIXPathStyle);
289 root = CFStringGetCStringPtr(path, CFStringGetSystemEncoding());
292 CFRelease(appBundle);
300 SG_LOG(SG_INPUT, SG_INFO, "fg_root = " << root );
301 globals->set_fg_root(root);
307 // Read in configuration (files and command line options) but only set
309 bool fgInitFGAircraft ( int argc, char **argv ) {
313 // First parse command line options looking for --aircraft=, this
314 // will override anything specified in a config file
315 aircraft = fgScanForOption( "--aircraft=", argc, argv );
317 #if defined( unix ) || defined( __CYGWIN__ )
318 // Next check home directory for .fgfsrc.hostname file
319 if ( aircraft.empty() ) {
320 envp = ::getenv( "HOME" );
321 if ( envp != NULL ) {
322 SGPath config( envp );
323 config.append( ".fgfsrc" );
325 gethostname( name, 256 );
326 config.concat( "." );
327 config.concat( name );
328 aircraft = fgScanForOption( "--aircraft=", config.str() );
333 // Next check home directory for .fgfsrc file
334 if ( aircraft.empty() ) {
335 envp = ::getenv( "HOME" );
336 if ( envp != NULL ) {
337 SGPath config( envp );
338 config.append( ".fgfsrc" );
339 aircraft = fgScanForOption( "--aircraft=", config.str() );
343 if ( aircraft.empty() ) {
344 // Check for $fg_root/system.fgfsrc
345 SGPath sysconf( globals->get_fg_root() );
346 sysconf.append( "system.fgfsrc" );
347 aircraft = fgScanForOption( "--aircraft=", sysconf.str() );
349 // if an aircraft was specified, set the property name
350 if ( !aircraft.empty() ) {
351 SG_LOG(SG_INPUT, SG_INFO, "aircraft = " << aircraft );
352 fgSetString("/sim/aircraft", aircraft.c_str() );
354 SG_LOG(SG_INPUT, SG_INFO, "No user specified aircraft, using default" );
361 // Return the current base package version
362 string fgBasePackageVersion() {
363 SGPath base_path( globals->get_fg_root() );
364 base_path.append("version");
366 sg_gzifstream in( base_path.str() );
367 if ( !in.is_open() ) {
368 SGPath old_path( globals->get_fg_root() );
369 old_path.append( "Thanks" );
370 sg_gzifstream old( old_path.str() );
371 if ( !old.is_open() ) {
374 return "[old version]";
385 // Initialize the localization
386 SGPropertyNode *fgInitLocale(const char *language) {
387 SGPropertyNode *c_node = NULL, *d_node = NULL;
388 SGPropertyNode *intl = fgGetNode("/sim/intl");
390 SG_LOG(SG_GENERAL, SG_INFO, "Selecting language: " << language );
392 // localization not defined
397 // Select the proper language from the list
399 vector<SGPropertyNode_ptr> locale = intl->getChildren("locale");
400 for (unsigned int i = 0; i < locale.size(); i++) {
402 vector<SGPropertyNode_ptr> lang = locale[i]->getChildren("lang");
403 for (unsigned int j = 0; j < lang.size(); j++) {
405 if (!strcmp(lang[j]->getStringValue(), language)) {
414 d_node = intl->getChild("locale");
418 // Check for localized font
419 SGPropertyNode *font_n = c_node->getNode("font", true);
420 if ( !strcmp(font_n->getStringValue(), "") )
421 font_n->setStringValue(d_node->getStringValue("font", "typewriter.txf"));
425 // Load the default strings
427 SGPath d_path( globals->get_fg_root() );
429 const char *d_path_str = d_node->getStringValue("strings");
431 SG_LOG(SG_GENERAL, SG_ALERT, "Incorrect path in configuration file.");
435 d_path.append(d_path_str);
436 SG_LOG(SG_GENERAL, SG_INFO, "Reading localized strings from "
439 SGPropertyNode *strings = c_node->getNode("strings");
441 readProperties(d_path.str(), strings);
442 } catch (const sg_exception &e) {
443 SG_LOG(SG_GENERAL, SG_ALERT, "Unable to read the localized strings");
448 // Load the language specific strings
450 if (c_node != d_node) {
451 SGPath c_path( globals->get_fg_root() );
453 const char *c_path_str = c_node->getStringValue("strings");
455 SG_LOG(SG_GENERAL, SG_ALERT, "Incorrect path in configuration file.");
459 c_path.append(c_path_str);
460 SG_LOG(SG_GENERAL, SG_INFO, "Reading localized strings from "
464 readProperties(c_path.str(), strings);
465 } catch (const sg_exception &e) {
466 SG_LOG(SG_GENERAL, SG_ALERT, "Unable to read the localized strings");
476 // Initialize the localization routines
477 bool fgDetectLanguage() {
478 char *language = ::getenv("LANG");
480 if (language == NULL) {
481 SG_LOG(SG_GENERAL, SG_INFO, "Unable to detect the language" );
485 SGPropertyNode *locale = fgInitLocale(language);
487 SG_LOG(SG_GENERAL, SG_ALERT,
488 "No internationalization settings specified in preferences.xml" );
493 globals->set_locale( locale );
498 // Attempt to locate and parse the various non-XML config files in order
499 // from least precidence to greatest precidence
501 do_options (int argc, char ** argv)
503 // Check for $fg_root/system.fgfsrc
504 SGPath config( globals->get_fg_root() );
505 config.append( "system.fgfsrc" );
506 fgParseOptions(config.str());
508 #if defined( unix ) || defined( __CYGWIN__ )
510 // Check for $fg_root/system.fgfsrc.hostname
511 gethostname( name, 256 );
512 config.concat( "." );
513 config.concat( name );
514 fgParseOptions(config.str());
517 // Check for ~/.fgfsrc
518 char* envp = ::getenv( "HOME" );
519 if ( envp != NULL ) {
521 config.append( ".fgfsrc" );
522 fgParseOptions(config.str());
525 #if defined( unix ) || defined( __CYGWIN__ )
526 // Check for ~/.fgfsrc.hostname
527 gethostname( name, 256 );
528 config.concat( "." );
529 config.concat( name );
530 fgParseOptions(config.str());
533 // Parse remaining command line options
534 // These will override anything specified in a config file
535 fgParseArgs(argc, argv);
539 static string fgFindAircraftPath( const SGPath &path, const string &aircraft ) {
541 ulDir *dirp = ulOpenDir(path.str().c_str());
543 cerr << "Unable to open aircraft directory." << endl;
547 while ((dire = ulReadDir(dirp)) != NULL) {
549 if ( strcmp("CVS", dire->d_name) && strcmp(".", dire->d_name)
550 && strcmp("..", dire->d_name) )
553 next.append(dire->d_name);
555 string result = fgFindAircraftPath( next, aircraft );
556 if ( ! result.empty() ) {
560 } else if ( !strcmp(dire->d_name, aircraft.c_str()) ) {
571 // Read in configuration (file and command line)
572 bool fgInitConfig ( int argc, char **argv ) {
574 // First, set some sane default values
577 // Read global preferences from $FG_ROOT/preferences.xml
578 SG_LOG(SG_INPUT, SG_INFO, "Reading global preferences");
579 fgLoadProps("preferences.xml", globals->get_props());
580 SG_LOG(SG_INPUT, SG_INFO, "Finished Reading global preferences");
582 // Detect the required language as early as possible
583 if ( !fgDetectLanguage() ) {
587 // Scan user config files and command line for a specified aircraft.
588 fgInitFGAircraft(argc, argv);
590 string aircraft = fgGetString( "/sim/aircraft", "" );
591 if ( aircraft.size() > 0 ) {
592 SGPath aircraft_search( globals->get_fg_root() );
593 aircraft_search.append( "Aircraft" );
595 string aircraft_set = aircraft + "-set.xml";
597 string result = fgFindAircraftPath( aircraft_search, aircraft_set );
598 if ( !result.empty() ) {
599 fgSetString( "/sim/aircraft-dir", result.c_str() );
600 SGPath full_name( result );
601 full_name.append( aircraft_set );
603 SG_LOG(SG_INPUT, SG_INFO, "Reading default aircraft: " << aircraft
604 << " from " << full_name.str());
606 readProperties( full_name.str(), globals->get_props() );
607 } catch ( const sg_exception &e ) {
608 string message = "Error reading default aircraft: ";
609 message += e.getFormattedMessage();
610 SG_LOG(SG_INPUT, SG_ALERT, message);
614 SG_LOG( SG_INPUT, SG_ALERT, "Cannot find specified aircraft: "
620 SG_LOG( SG_INPUT, SG_ALERT, "No default aircraft specified" );
623 // parse options after loading aircraft to ensure any user
624 // overrides of defaults are honored.
625 do_options(argc, argv);
631 // find basic airport location info from airport database
632 bool fgFindAirportID( const string& id, FGAirport *a ) {
635 SG_LOG( SG_GENERAL, SG_INFO, "Searching for airport code = " << id );
637 result = globals->get_airports()->search( id );
639 if ( result.getId().empty() ) {
640 SG_LOG( SG_GENERAL, SG_ALERT,
641 "Failed to find " << id << " in basic.dat.gz" );
650 SG_LOG( SG_GENERAL, SG_INFO,
651 "Position for " << id << " is ("
652 << a->getLongitude() << ", "
653 << a->getLatitude() << ")" );
659 // get airport elevation
660 static double fgGetAirportElev( const string& id ) {
664 SG_LOG( SG_GENERAL, SG_INFO,
665 "Finding elevation for airport: " << id );
667 if ( fgFindAirportID( id, &a ) ) {
668 return a.getElevation();
677 // This function is never used, but maybe useful in the future ???
680 // Preset lon/lat given an airport id
681 static bool fgSetPosFromAirportID( const string& id ) {
685 SG_LOG( SG_GENERAL, SG_INFO,
686 "Attempting to set starting position from airport code " << id );
688 if ( fgFindAirportID( id, &a ) ) {
690 fgSetDouble("/sim/presets/longitude-deg", a.longitude );
691 fgSetDouble("/sim/presets/latitude-deg", a.latitude );
693 // other code depends on the actual postition being set so set
695 fgSetDouble("/position/longitude-deg", a.longitude );
696 fgSetDouble("/position/latitude-deg", a.latitude );
698 SG_LOG( SG_GENERAL, SG_INFO,
699 "Position for " << id << " is (" << a.longitude
700 << ", " << a.latitude << ")" );
710 // Set current tower position lon/lat given an airport id
711 static bool fgSetTowerPosFromAirportID( const string& id, double hdg ) {
713 // tower height hard coded for now...
714 float towerheight=50.0f;
716 // make a little off the heading for 1 runway airports...
717 float fudge_lon = fabs(sin(hdg)) * .003f;
718 float fudge_lat = .003f - fudge_lon;
720 if ( fgFindAirportID( id, &a ) ) {
721 fgSetDouble("/sim/tower/longitude-deg", a.getLongitude() + fudge_lon);
722 fgSetDouble("/sim/tower/latitude-deg", a.getLatitude() + fudge_lat);
723 fgSetDouble("/sim/tower/altitude-ft", a.getElevation() + towerheight);
732 // Set current_options lon/lat given an airport id and heading (degrees)
733 static bool fgSetPosFromAirportIDandHdg( const string& id, double tgt_hdg ) {
737 // set initial position from runway and heading
739 SG_LOG( SG_GENERAL, SG_INFO,
740 "Attempting to set starting position from airport code "
741 << id << " heading " << tgt_hdg );
743 if ( ! globals->get_runways()->search( id, (int)tgt_hdg, &r ) ) {
744 SG_LOG( SG_GENERAL, SG_ALERT,
745 "Failed to find a good runway for " << id << '\n' );
752 double lat2, lon2, az2;
753 double heading = r._heading;
754 double azimuth = heading + 180.0;
755 while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
757 SG_LOG( SG_GENERAL, SG_INFO,
758 "runway = " << r._lon << ", " << r._lat
759 << " length = " << r._length * SG_FEET_TO_METER
760 << " heading = " << azimuth );
762 geo_direct_wgs_84 ( 0, r._lat, r._lon, azimuth,
763 r._length * SG_FEET_TO_METER * 0.5 - 5.0,
764 &lat2, &lon2, &az2 );
766 if ( fabs( fgGetDouble("/sim/presets/offset-distance") ) > SG_EPSILON ) {
768 double odist = fgGetDouble("/sim/presets/offset-distance");
769 odist *= SG_NM_TO_METER;
770 double oaz = azimuth;
771 if ( fabs(fgGetDouble("/sim/presets/offset-azimuth")) > SG_EPSILON ) {
772 oaz = fgGetDouble("/sim/presets/offset-azimuth") + 180;
775 while ( oaz >= 360.0 ) { oaz -= 360.0; }
776 geo_direct_wgs_84 ( 0, lat2, lon2, oaz, odist, &olat, &olon, &az2 );
782 fgSetDouble("/sim/presets/longitude-deg", lon2 );
783 fgSetDouble("/sim/presets/latitude-deg", lat2 );
784 fgSetDouble("/sim/presets/heading-deg", heading );
786 // other code depends on the actual values being set ...
787 fgSetDouble("/position/longitude-deg", lon2 );
788 fgSetDouble("/position/latitude-deg", lat2 );
789 fgSetDouble("/orientation/heading-deg", heading );
791 SG_LOG( SG_GENERAL, SG_INFO,
792 "Position for " << id << " is ("
794 << lat2 << ") new heading is "
801 // Set current_options lon/lat given an airport id and runway number
802 static bool fgSetPosFromAirportIDandRwy( const string& id, const string& rwy ) {
806 // set initial position from airport and runway number
808 SG_LOG( SG_GENERAL, SG_INFO,
809 "Attempting to set starting position for "
810 << id << ":" << rwy );
812 if ( ! globals->get_runways()->search( id, rwy, &r ) ) {
813 SG_LOG( SG_GENERAL, SG_ALERT,
814 "Failed to find runway " << rwy <<
815 " at airport " << id );
822 double lat2, lon2, az2;
823 double heading = r._heading;
824 double azimuth = heading + 180.0;
825 while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
827 SG_LOG( SG_GENERAL, SG_INFO,
828 "runway = " << r._lon << ", " << r._lat
829 << " length = " << r._length * SG_FEET_TO_METER
830 << " heading = " << azimuth );
832 geo_direct_wgs_84 ( 0, r._lat, r._lon,
834 r._length * SG_FEET_TO_METER * 0.5 - 5.0,
835 &lat2, &lon2, &az2 );
837 if ( fabs( fgGetDouble("/sim/presets/offset-distance") ) > SG_EPSILON )
840 double odist = fgGetDouble("/sim/presets/offset-distance");
841 odist *= SG_NM_TO_METER;
842 double oaz = azimuth;
843 if ( fabs(fgGetDouble("/sim/presets/offset-azimuth")) > SG_EPSILON )
845 oaz = fgGetDouble("/sim/presets/offset-azimuth") + 180;
846 heading = fgGetDouble("/sim/presets/heading-deg");
848 while ( oaz >= 360.0 ) { oaz -= 360.0; }
849 geo_direct_wgs_84 ( 0, lat2, lon2, oaz, odist, &olat, &olon, &az2 );
855 fgSetDouble("/sim/presets/longitude-deg", lon2 );
856 fgSetDouble("/sim/presets/latitude-deg", lat2 );
857 fgSetDouble("/sim/presets/heading-deg", heading );
859 // other code depends on the actual values being set ...
860 fgSetDouble("/position/longitude-deg", lon2 );
861 fgSetDouble("/position/latitude-deg", lat2 );
862 fgSetDouble("/orientation/heading-deg", heading );
864 SG_LOG( SG_GENERAL, SG_INFO,
865 "Position for " << id << " is ("
867 << lat2 << ") new heading is "
874 static void fgSetDistOrAltFromGlideSlope() {
875 // cout << "fgSetDistOrAltFromGlideSlope()" << endl;
876 string apt_id = fgGetString("/sim/presets/airport-id");
877 double gs = fgGetDouble("/sim/presets/glideslope-deg")
878 * SG_DEGREES_TO_RADIANS ;
879 double od = fgGetDouble("/sim/presets/offset-distance");
880 double alt = fgGetDouble("/sim/presets/altitude-ft");
882 double apt_elev = 0.0;
883 if ( ! apt_id.empty() ) {
884 apt_elev = fgGetAirportElev( apt_id );
885 if ( apt_elev < -9990.0 ) {
892 if( fabs(gs) > 0.01 && fabs(od) > 0.1 && alt < -9990 ) {
893 // set altitude from glideslope and offset-distance
894 od *= SG_NM_TO_METER * SG_METER_TO_FEET;
895 alt = fabs(od*tan(gs)) + apt_elev;
896 fgSetDouble("/sim/presets/altitude-ft", alt);
897 fgSetBool("/sim/presets/onground", false);
898 SG_LOG( SG_GENERAL, SG_INFO, "Calculated altitude as: "
900 } else if( fabs(gs) > 0.01 && alt > 0 && fabs(od) < 0.1) {
901 // set offset-distance from glideslope and altitude
902 od = (alt - apt_elev) / tan(gs);
903 od *= -1*SG_FEET_TO_METER * SG_METER_TO_NM;
904 fgSetDouble("/sim/presets/offset-distance", od);
905 fgSetBool("/sim/presets/onground", false);
906 SG_LOG( SG_GENERAL, SG_INFO, "Calculated offset distance as: "
908 } else if( fabs(gs) > 0.01 ) {
909 SG_LOG( SG_GENERAL, SG_ALERT,
910 "Glideslope given but not altitude or offset-distance." );
911 SG_LOG( SG_GENERAL, SG_ALERT, "Resetting glideslope to zero" );
912 fgSetDouble("/sim/presets/glideslope-deg", 0);
913 fgSetBool("/sim/presets/onground", true);
918 // Set current_options lon/lat given an airport id and heading (degrees)
919 static bool fgSetPosFromNAV( const string& id, const double& freq ) {
921 = globals->get_navlist()->findByIdentAndFreq( id.c_str(), freq );
923 // set initial position from runway and heading
925 SG_LOG( SG_GENERAL, SG_INFO, "Attempting to set starting position for "
926 << id << ":" << freq );
928 double lon = nav->get_lon();
929 double lat = nav->get_lat();
931 if ( fabs( fgGetDouble("/sim/presets/offset-distance") ) > SG_EPSILON )
933 double odist = fgGetDouble("/sim/presets/offset-distance")
935 double oaz = fabs(fgGetDouble("/sim/presets/offset-azimuth"))
937 while ( oaz >= 360.0 ) { oaz -= 360.0; }
938 double olat, olon, az2;
939 geo_direct_wgs_84 ( 0, lat, lon, oaz, odist, &olat, &olon, &az2 );
946 fgSetDouble("/sim/presets/longitude-deg", lon );
947 fgSetDouble("/sim/presets/latitude-deg", lat );
949 // other code depends on the actual values being set ...
950 fgSetDouble("/position/longitude-deg", lon );
951 fgSetDouble("/position/latitude-deg", lat );
952 fgSetDouble("/orientation/heading-deg",
953 fgGetDouble("/sim/presets/heading-deg") );
955 SG_LOG( SG_GENERAL, SG_INFO,
956 "Position for " << id << ":" << freq << " is ("
957 << lon << ", "<< lat << ")" );
961 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to locate NAV = "
962 << id << ":" << freq );
968 // Set current_options lon/lat given an airport id and heading (degrees)
969 static bool fgSetPosFromFix( const string& id ) {
972 // set initial position from runway and heading
973 if ( globals->get_fixlist()->query( id.c_str(), &fix ) ) {
974 SG_LOG( SG_GENERAL, SG_INFO, "Attempting to set starting position for "
977 double lon = fix.get_lon();
978 double lat = fix.get_lat();
980 if ( fabs( fgGetDouble("/sim/presets/offset-distance") ) > SG_EPSILON )
982 double odist = fgGetDouble("/sim/presets/offset-distance")
984 double oaz = fabs(fgGetDouble("/sim/presets/offset-azimuth"))
986 while ( oaz >= 360.0 ) { oaz -= 360.0; }
987 double olat, olon, az2;
988 geo_direct_wgs_84 ( 0, lat, lon, oaz, odist, &olat, &olon, &az2 );
995 fgSetDouble("/sim/presets/longitude-deg", lon );
996 fgSetDouble("/sim/presets/latitude-deg", lat );
998 // other code depends on the actual values being set ...
999 fgSetDouble("/position/longitude-deg", lon );
1000 fgSetDouble("/position/latitude-deg", lat );
1001 fgSetDouble("/orientation/heading-deg",
1002 fgGetDouble("/sim/presets/heading-deg") );
1004 SG_LOG( SG_GENERAL, SG_INFO,
1005 "Position for " << id << " is ("
1006 << lon << ", "<< lat << ")" );
1010 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to locate NAV = "
1016 static void parseWaypoints() {
1017 string_list *waypoints = globals->get_initial_waypoints();
1019 vector<string>::iterator i;
1020 for (i = waypoints->begin();
1021 i != waypoints->end();
1026 // Now were done using the way points we can deallocate the
1028 while (waypoints->begin() != waypoints->end()) {
1029 waypoints->pop_back();
1032 globals->set_initial_waypoints(0);
1041 * Initialize vor/ndb/ils/fix list management and query systems (as
1042 * well as simple airport db list)
1047 SG_LOG(SG_GENERAL, SG_INFO, "Loading Airport Database ...");
1049 SGPath aptdb( globals->get_fg_root() );
1050 aptdb.append( "Airports/apt.dat" );
1052 SGPath p_metar( globals->get_fg_root() );
1053 p_metar.append( "Airports/metar.dat" );
1055 FGAirportList *airports = new FGAirportList();
1056 globals->set_airports( airports );
1057 FGRunwayList *runways = new FGRunwayList();
1058 globals->set_runways( runways );
1060 fgAirportDBLoad( airports, runways, aptdb.str(), p_metar.str() );
1062 FGNavList *navlist = new FGNavList;
1063 FGNavList *loclist = new FGNavList;
1064 FGNavList *gslist = new FGNavList;
1065 FGNavList *dmelist = new FGNavList;
1066 FGNavList *mkrlist = new FGNavList;
1068 globals->set_navlist( navlist );
1069 globals->set_loclist( loclist );
1070 globals->set_gslist( gslist );
1071 globals->set_dmelist( dmelist );
1072 globals->set_mkrlist( mkrlist );
1074 if ( !fgNavDBInit(airports, navlist, loclist, gslist, dmelist, mkrlist) ) {
1075 SG_LOG( SG_GENERAL, SG_ALERT,
1076 "Problems loading one or more navigational database" );
1079 if ( fgGetBool("/sim/navdb/localizers/auto-align", true) ) {
1080 // align all the localizers with their corresponding runways
1081 // since data sources are good for cockpit navigation
1082 // purposes, but not always to the error tolerances needed to
1083 // exactly place these items.
1085 = fgGetDouble( "/sim/navdb/localizers/auto-align-threshold-deg",
1087 fgNavDBAlignLOCwithRunway( runways, loclist, threshold );
1090 SG_LOG(SG_GENERAL, SG_INFO, " Fixes");
1091 SGPath p_fix( globals->get_fg_root() );
1092 p_fix.append( "Navaids/fix.dat" );
1093 FGFixList *fixlist = new FGFixList;
1094 fixlist->init( p_fix );
1095 globals->set_fixlist( fixlist );
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 double hdg = fgGetDouble("/sim/presets/heading-deg");
1136 string vor = fgGetString("/sim/presets/vor-id");
1137 double vor_freq = fgGetDouble("/sim/presets/vor-freq");
1138 string ndb = fgGetString("/sim/presets/ndb-id");
1139 double ndb_freq = fgGetDouble("/sim/presets/ndb-freq");
1140 string fix = fgGetString("/sim/presets/fix");
1142 if ( !set_pos && !apt.empty() && !rwy_no.empty() ) {
1143 // An airport + runway is requested
1144 if ( fgSetPosFromAirportIDandRwy( apt, rwy_no ) ) {
1145 // set tower position (a little off the heading for single
1147 fgSetTowerPosFromAirportID( apt, hdg );
1152 if ( !set_pos && !apt.empty() ) {
1153 // An airport is requested (find runway closest to hdg)
1154 if ( fgSetPosFromAirportIDandHdg( apt, hdg ) ) {
1155 // set tower position (a little off the heading for single
1157 fgSetTowerPosFromAirportID( apt, hdg );
1162 if ( !set_pos && !vor.empty() ) {
1163 // a VOR is requested
1164 if ( fgSetPosFromNAV( vor, vor_freq ) ) {
1169 if ( !set_pos && !ndb.empty() ) {
1170 // an NDB is requested
1171 if ( fgSetPosFromNAV( ndb, ndb_freq ) ) {
1176 if ( !set_pos && !fix.empty() ) {
1177 // a Fix is requested
1178 if ( fgSetPosFromFix( fix ) ) {
1184 // No lon/lat specified, no airport specified, default to
1185 // middle of KSFO field.
1186 fgSetDouble("/sim/presets/longitude-deg", -122.374843);
1187 fgSetDouble("/sim/presets/latitude-deg", 37.619002);
1190 fgSetDouble( "/position/longitude-deg",
1191 fgGetDouble("/sim/presets/longitude-deg") );
1192 fgSetDouble( "/position/latitude-deg",
1193 fgGetDouble("/sim/presets/latitude-deg") );
1194 fgSetDouble( "/orientation/heading-deg",
1195 fgGetDouble("/sim/presets/heading-deg") );
1197 // determine if this should be an on-ground or in-air start
1198 if ( fabs(gs) > 0.01 || fabs(od) > 0.1 || alt > 0.1 ) {
1199 fgSetBool("/sim/presets/onground", false);
1201 fgSetBool("/sim/presets/onground", true);
1208 // General house keeping initializations
1209 bool fgInitGeneral() {
1212 #if defined(FX) && defined(XMESA)
1213 char *mesa_win_state;
1216 SG_LOG( SG_GENERAL, SG_INFO, "General Initialization" );
1217 SG_LOG( SG_GENERAL, SG_INFO, "======= ==============" );
1219 root = globals->get_fg_root();
1220 if ( ! root.length() ) {
1221 // No root path set? Then bail ...
1222 SG_LOG( SG_GENERAL, SG_ALERT,
1223 "Cannot continue without a path to the base package "
1224 << "being defined." );
1227 SG_LOG( SG_GENERAL, SG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
1229 #if defined(FX) && defined(XMESA)
1230 // initialize full screen flag
1231 globals->set_fullscreen(false);
1232 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
1233 // Test for the MESA_GLX_FX env variable
1234 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
1235 // test if we are fullscreen mesa/glide
1236 if ( (mesa_win_state[0] == 'f') ||
1237 (mesa_win_state[0] == 'F') ) {
1238 globals->set_fullscreen(true);
1248 // Initialize the flight model subsystem. This just creates the
1249 // object. The actual fdm initialization is delayed until we get a
1250 // proper scenery elevation hit. This is checked for in main.cxx
1254 if ( cur_fdm_state ) {
1255 delete cur_fdm_state;
1259 double dt = 1.0 / fgGetInt("/sim/model-hz");
1260 aircraft_dir = fgGetString("/sim/aircraft-dir");
1261 const string &model = fgGetString("/sim/flight-model");
1264 if ( model == "larcsim" ) {
1265 cur_fdm_state = new FGLaRCsim( dt );
1266 } else if ( model == "jsb" ) {
1267 cur_fdm_state = new FGJSBsim( dt );
1268 #ifdef ENABLE_SP_FMDS
1269 } else if ( model == "ada" ) {
1270 cur_fdm_state = new FGADA( dt );
1271 } else if ( model == "acms" ) {
1272 cur_fdm_state = new FGACMS( dt );
1274 } else if ( model == "balloon" ) {
1275 cur_fdm_state = new FGBalloonSim( dt );
1276 } else if ( model == "magic" ) {
1277 cur_fdm_state = new FGMagicCarpet( dt );
1278 } else if ( model == "ufo" ) {
1279 cur_fdm_state = new FGUFO( dt );
1280 } else if ( model == "external" ) {
1281 // external is a synonym for "--fdm=null" and is
1282 // maintained here for backwards compatibility
1283 cur_fdm_state = new FGNullFDM( dt );
1284 } else if ( model.find("network") == 0 ) {
1285 string host = "localhost";
1289 string net_options = model.substr(8);
1290 string::size_type begin, end;
1293 end = net_options.find( ",", begin );
1294 if ( end != string::npos ) {
1295 host = net_options.substr(begin, end - begin);
1299 end = net_options.find( ",", begin );
1300 if ( end != string::npos ) {
1301 port1 = atoi( net_options.substr(begin, end - begin).c_str() );
1305 end = net_options.find( ",", begin );
1306 if ( end != string::npos ) {
1307 port2 = atoi( net_options.substr(begin, end - begin).c_str() );
1311 end = net_options.find( ",", begin );
1312 if ( end != string::npos ) {
1313 port3 = atoi( net_options.substr(begin, end - begin).c_str() );
1316 cur_fdm_state = new FGExternalNet( dt, host, port1, port2, port3 );
1317 } else if ( model.find("pipe") == 0 ) {
1318 string pipe_path = model.substr(5);
1319 cur_fdm_state = new FGExternalPipe( dt, pipe_path );
1320 } else if ( model == "null" ) {
1321 cur_fdm_state = new FGNullFDM( dt );
1322 } else if ( model == "yasim" ) {
1323 cur_fdm_state = new YASim( dt );
1325 SG_LOG(SG_GENERAL, SG_ALERT,
1326 "Unrecognized flight model '" << model
1327 << "', cannot init flight dynamics model.");
1331 SG_LOG(SG_GENERAL, SG_ALERT, "FlightGear aborting\n\n");
1336 static void printMat(const sgVec4 *mat, char *name="")
1339 SG_LOG(SG_GENERAL, SG_BULK, name );
1340 for(i=0; i<4; i++) {
1341 SG_LOG(SG_GENERAL, SG_BULK, " " << mat[i][0] << " " << mat[i][1]
1342 << " " << mat[i][2] << " " << mat[i][3] );
1346 // Initialize view parameters
1348 // force update of model so that viewer can get some data...
1349 globals->get_aircraft_model()->update(0);
1350 // run update for current view so that data is current...
1351 globals->get_viewmgr()->update(0);
1353 printMat(globals->get_current_view()->get_VIEW(),"VIEW");
1354 printMat(globals->get_current_view()->get_UP(),"UP");
1355 // printMat(globals->get_current_view()->get_LOCAL(),"LOCAL");
1360 SGTime *fgInitTime() {
1362 static const SGPropertyNode *longitude
1363 = fgGetNode("/position/longitude-deg");
1364 static const SGPropertyNode *latitude
1365 = fgGetNode("/position/latitude-deg");
1366 static const SGPropertyNode *cur_time_override
1367 = fgGetNode("/sim/time/cur-time-override", true);
1369 SGPath zone( globals->get_fg_root() );
1370 zone.append( "Timezone" );
1371 SGTime *t = new SGTime( longitude->getDoubleValue()
1372 * SGD_DEGREES_TO_RADIANS,
1373 latitude->getDoubleValue()
1374 * SGD_DEGREES_TO_RADIANS,
1376 cur_time_override->getLongValue() );
1378 globals->set_warp_delta( 0 );
1380 t->update( 0.0, 0.0,
1381 cur_time_override->getLongValue(),
1382 globals->get_warp() );
1388 // set up a time offset (aka warp) if one is specified
1389 void fgInitTimeOffset() {
1390 static const SGPropertyNode *longitude
1391 = fgGetNode("/position/longitude-deg");
1392 static const SGPropertyNode *latitude
1393 = fgGetNode("/position/latitude-deg");
1394 static const SGPropertyNode *cur_time_override
1395 = fgGetNode("/sim/time/cur-time-override", true);
1397 // Handle potential user specified time offsets
1398 int orig_warp = globals->get_warp();
1399 SGTime *t = globals->get_time_params();
1400 time_t cur_time = t->get_cur_time();
1401 time_t currGMT = sgTimeGetGMT( gmtime(&cur_time) );
1402 time_t systemLocalTime = sgTimeGetGMT( localtime(&cur_time) );
1403 time_t aircraftLocalTime =
1404 sgTimeGetGMT( fgLocaltime(&cur_time, t->get_zonename() ) );
1406 // Okay, we now have several possible scenarios
1407 int offset = fgGetInt("/sim/startup/time-offset");
1408 const string &offset_type = fgGetString("/sim/startup/time-offset-type");
1411 if ( offset_type == "real" ) {
1413 } else if ( offset_type == "dawn" ) {
1414 warp = fgTimeSecondsUntilSunAngle( cur_time,
1415 longitude->getDoubleValue()
1416 * SGD_DEGREES_TO_RADIANS,
1417 latitude->getDoubleValue()
1418 * SGD_DEGREES_TO_RADIANS,
1420 } else if ( offset_type == "morning" ) {
1421 warp = fgTimeSecondsUntilSunAngle( cur_time,
1422 longitude->getDoubleValue()
1423 * SGD_DEGREES_TO_RADIANS,
1424 latitude->getDoubleValue()
1425 * SGD_DEGREES_TO_RADIANS,
1427 } else if ( offset_type == "noon" ) {
1428 warp = fgTimeSecondsUntilSunAngle( cur_time,
1429 longitude->getDoubleValue()
1430 * SGD_DEGREES_TO_RADIANS,
1431 latitude->getDoubleValue()
1432 * SGD_DEGREES_TO_RADIANS,
1434 } else if ( offset_type == "afternoon" ) {
1435 warp = fgTimeSecondsUntilSunAngle( cur_time,
1436 longitude->getDoubleValue()
1437 * SGD_DEGREES_TO_RADIANS,
1438 latitude->getDoubleValue()
1439 * SGD_DEGREES_TO_RADIANS,
1441 } else if ( offset_type == "dusk" ) {
1442 warp = fgTimeSecondsUntilSunAngle( cur_time,
1443 longitude->getDoubleValue()
1444 * SGD_DEGREES_TO_RADIANS,
1445 latitude->getDoubleValue()
1446 * SGD_DEGREES_TO_RADIANS,
1448 } else if ( offset_type == "evening" ) {
1449 warp = fgTimeSecondsUntilSunAngle( cur_time,
1450 longitude->getDoubleValue()
1451 * SGD_DEGREES_TO_RADIANS,
1452 latitude->getDoubleValue()
1453 * SGD_DEGREES_TO_RADIANS,
1455 } else if ( offset_type == "midnight" ) {
1456 warp = fgTimeSecondsUntilSunAngle( cur_time,
1457 longitude->getDoubleValue()
1458 * SGD_DEGREES_TO_RADIANS,
1459 latitude->getDoubleValue()
1460 * SGD_DEGREES_TO_RADIANS,
1462 } else if ( offset_type == "system-offset" ) {
1464 } else if ( offset_type == "gmt-offset" ) {
1465 warp = offset - (currGMT - systemLocalTime);
1466 } else if ( offset_type == "latitude-offset" ) {
1467 warp = offset - (aircraftLocalTime - systemLocalTime);
1468 } else if ( offset_type == "system" ) {
1469 warp = offset - cur_time;
1470 } else if ( offset_type == "gmt" ) {
1471 warp = offset - currGMT;
1472 } else if ( offset_type == "latitude" ) {
1473 warp = offset - (aircraftLocalTime - systemLocalTime) - cur_time;
1475 SG_LOG( SG_GENERAL, SG_ALERT,
1476 "FG_TIME::Unsupported offset type " << offset_type );
1479 globals->set_warp( orig_warp + warp );
1480 t->update( longitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
1481 latitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
1482 cur_time_override->getLongValue(),
1483 globals->get_warp() );
1485 SG_LOG( SG_GENERAL, SG_INFO, "After fgInitTimeOffset(): warp = "
1486 << globals->get_warp() );
1490 // This is the top level init routine which calls all the other
1491 // initialization routines. If you are adding a subsystem to flight
1492 // gear, its initialization call should located in this routine.
1493 // Returns non-zero if a problem encountered.
1494 bool fgInitSubsystems() {
1495 // static const SGPropertyNode *longitude
1496 // = fgGetNode("/sim/presets/longitude-deg");
1497 // static const SGPropertyNode *latitude
1498 // = fgGetNode("/sim/presets/latitude-deg");
1499 // static const SGPropertyNode *altitude
1500 // = fgGetNode("/sim/presets/altitude-ft");
1502 SG_LOG( SG_GENERAL, SG_INFO, "Initialize Subsystems");
1503 SG_LOG( SG_GENERAL, SG_INFO, "========== ==========");
1505 ////////////////////////////////////////////////////////////////////
1506 // Initialize the event manager subsystem.
1507 ////////////////////////////////////////////////////////////////////
1509 globals->get_event_mgr()->init();
1510 globals->get_event_mgr()->setFreezeProperty(fgGetNode("/sim/freeze/clock"));
1512 ////////////////////////////////////////////////////////////////////
1513 // Initialize the property interpolator subsystem
1514 ////////////////////////////////////////////////////////////////////
1515 globals->add_subsystem("interpolator", new SGInterpolator);
1518 ////////////////////////////////////////////////////////////////////
1519 // Add the FlightGear property utilities.
1520 ////////////////////////////////////////////////////////////////////
1521 globals->add_subsystem("properties", new FGProperties);
1523 ////////////////////////////////////////////////////////////////////
1524 // Initialize the material property subsystem.
1525 ////////////////////////////////////////////////////////////////////
1527 SGPath mpath( globals->get_fg_root() );
1528 mpath.append( "materials.xml" );
1529 if ( ! globals->get_matlib()->load(globals->get_fg_root(), mpath.str()) ) {
1530 SG_LOG( SG_GENERAL, SG_ALERT, "Error loading material lib!" );
1535 ////////////////////////////////////////////////////////////////////
1536 // Initialize the scenery management subsystem.
1537 ////////////////////////////////////////////////////////////////////
1539 if ( globals->get_tile_mgr()->init() ) {
1540 // Load the local scenery data
1541 double visibility_meters = fgGetDouble("/environment/visibility-m");
1543 globals->get_tile_mgr()->update( visibility_meters );
1545 SG_LOG( SG_GENERAL, SG_ALERT, "Error in Tile Manager initialization!" );
1549 // cause refresh of viewer scenery timestamps every 15 seconds...
1550 globals->get_event_mgr()->addTask( "FGTileMgr::refresh_view_timestamps()",
1551 globals->get_tile_mgr(),
1552 &FGTileMgr::refresh_view_timestamps,
1555 SG_LOG( SG_GENERAL, SG_DEBUG,
1556 "Current terrain elevation after tile mgr init " <<
1557 globals->get_scenery()->get_cur_elev() );
1560 ////////////////////////////////////////////////////////////////////
1561 // Initialize the flight model subsystem.
1562 ////////////////////////////////////////////////////////////////////
1566 // allocates structures so must happen before any of the flight
1567 // model or control parameters are set
1568 fgAircraftInit(); // In the future this might not be the case.
1571 ////////////////////////////////////////////////////////////////////
1572 // Initialize the XML Autopilot subsystem.
1573 ////////////////////////////////////////////////////////////////////
1575 globals->add_subsystem( "xml-autopilot", new FGXMLAutopilot );
1576 globals->add_subsystem( "route-manager", new FGRouteMgr );
1579 ////////////////////////////////////////////////////////////////////
1580 // Initialize the view manager subsystem.
1581 ////////////////////////////////////////////////////////////////////
1585 ////////////////////////////////////////////////////////////////////
1586 // Create and register the logger.
1587 ////////////////////////////////////////////////////////////////////
1589 globals->add_subsystem("logger", new FGLogger);
1591 ////////////////////////////////////////////////////////////////////
1592 // Create and register the XML GUI.
1593 ////////////////////////////////////////////////////////////////////
1595 globals->add_subsystem("gui", new NewGUI, SGSubsystemMgr::INIT);
1598 ////////////////////////////////////////////////////////////////////
1599 // Initialize the local time subsystem.
1600 ////////////////////////////////////////////////////////////////////
1602 // update the current timezone each 30 minutes
1603 globals->get_event_mgr()->addTask( "fgUpdateLocalTime()",
1604 &fgUpdateLocalTime, 30*60 );
1607 ////////////////////////////////////////////////////////////////////
1608 // Initialize the weather subsystem.
1609 ////////////////////////////////////////////////////////////////////
1611 // Initialize the weather modeling subsystem
1612 globals->add_subsystem("environment", new FGEnvironmentMgr);
1615 ////////////////////////////////////////////////////////////////////
1616 // Initialize the lighting subsystem.
1617 ////////////////////////////////////////////////////////////////////
1619 globals->add_subsystem("lighting", new FGLight);
1621 //////////////////////////////////////////////////////////////////////
1622 // Initialize the 2D cloud subsystem.
1623 ////////////////////////////////////////////////////////////////////
1624 fgGetBool("/sim/rendering/bump-mapping", false);
1626 #ifdef FG_USE_CLOUDS_3D
1627 ////////////////////////////////////////////////////////////////////
1628 // Initialize the 3D cloud subsystem.
1629 ////////////////////////////////////////////////////////////////////
1630 if ( fgGetBool("/sim/rendering/clouds3d") ) {
1631 static const SGPropertyNode *longitude
1632 = fgGetNode("/sim/presets/longitude-deg");
1633 static const SGPropertyNode *latitude
1634 = fgGetNode("/sim/presets/latitude-deg");
1635 // static const SGPropertyNode *altitude
1636 // = fgGetNode("/sim/presets/altitude-ft");
1638 SGPath cloud_path(globals->get_fg_root());
1640 cloud_path.append("Textures/Sky/scattered.rgba");
1641 SG_LOG(SG_GENERAL, SG_INFO, "Loading CLOUDS3d from: " << cloud_path.c_str());
1644 tx.read_rgba_texture(cloud_path.c_str());
1645 if ( !sgCloud3d->Load( tx.texture(), tx.width(),
1647 cloud_path.append("large.sky");
1648 if ( !sgCloud3d->Load( cloud_path.str(),
1650 latitude->getDoubleValue(),
1651 longitude->getDoubleValue()) )
1653 fgSetBool("/sim/rendering/clouds3d", false);
1654 SG_LOG(SG_GENERAL, SG_INFO, "CLOUDS3d FAILED: ");
1656 SG_LOG(SG_GENERAL, SG_INFO, "CLOUDS3d Loaded: ");
1660 #ifdef ENABLE_AUDIO_SUPPORT
1661 ////////////////////////////////////////////////////////////////////
1662 // Initialize the sound subsystem.
1663 ////////////////////////////////////////////////////////////////////
1665 init_volume = fgGetFloat("/sim/sound/volume");
1666 fgSetFloat("/sim/sound/volume", 0.0f);
1667 globals->set_soundmgr(new SGSoundMgr);
1668 globals->get_soundmgr()->init();
1669 globals->get_soundmgr()->bind();
1672 ////////////////////////////////////////////////////////////////////
1673 // Initialize the sound-effects subsystem.
1674 ////////////////////////////////////////////////////////////////////
1676 globals->add_subsystem("fx", new FGFX);
1680 ////////////////////////////////////////////////////////////////////
1681 // Initialise ATC display system
1682 ////////////////////////////////////////////////////////////////////
1684 SG_LOG(SG_GENERAL, SG_INFO, " ATC Display");
1685 globals->set_ATC_display(new FGATCDisplay);
1686 globals->get_ATC_display()->init();
1688 ////////////////////////////////////////////////////////////////////
1689 // Initialise the ATC Manager
1690 ////////////////////////////////////////////////////////////////////
1692 SG_LOG(SG_GENERAL, SG_INFO, " ATC Manager");
1693 globals->set_ATC_mgr(new FGATCMgr);
1694 globals->get_ATC_mgr()->init();
1696 ////////////////////////////////////////////////////////////////////
1697 // Initialise the AI Manager
1698 ////////////////////////////////////////////////////////////////////
1700 SG_LOG(SG_GENERAL, SG_INFO, " AI Manager");
1701 globals->set_AI_mgr(new FGAIMgr);
1702 globals->get_AI_mgr()->init();
1704 ////////////////////////////////////////////////////////////////////
1705 // Initialise the AI Model Manager
1706 ////////////////////////////////////////////////////////////////////
1707 SG_LOG(SG_GENERAL, SG_INFO, " AI Model Manager");
1708 globals->add_subsystem("ai_model", new FGAIManager);
1709 globals->add_subsystem("submodel_mgr", new FGSubmodelMgr);
1712 // It's probably a good idea to initialize the top level traffic manager
1713 // After the AI and ATC systems have been initialized properly.
1714 // AI Traffic manager
1715 globals->add_subsystem("Traffic Manager", new FGTrafficManager);
1716 FGTrafficManager *dispatcher =
1717 (FGTrafficManager*) globals->get_subsystem("Traffic Manager");
1718 SGPath path = globals->get_fg_root();
1719 path.append("/Traffic/fgtraffic.xml");
1722 //globals->get_subsystem("Traffic Manager")->init();
1724 globals->add_subsystem("instrumentation", new FGInstrumentMgr);
1725 globals->add_subsystem("systems", new FGSystemMgr);
1729 ////////////////////////////////////////////////////////////////////
1730 // Initialize the cockpit subsystem
1731 ////////////////////////////////////////////////////////////////////
1732 if( fgCockpitInit( ¤t_aircraft )) {
1733 // Cockpit initialized ok.
1735 SG_LOG( SG_GENERAL, SG_ALERT, "Error in Cockpit initialization!" );
1740 ////////////////////////////////////////////////////////////////////
1741 // Initialize the autopilot subsystem.
1742 ////////////////////////////////////////////////////////////////////
1744 // FIXME: these should go in the
1745 // GUI initialization code, not here.
1746 // fgAPAdjustInit();
1747 NewTgtAirportInit();
1751 ////////////////////////////////////////////////////////////////////
1752 // Initialize I/O subsystem.
1753 ////////////////////////////////////////////////////////////////////
1755 globals->get_io()->init();
1756 globals->get_io()->bind();
1759 ////////////////////////////////////////////////////////////////////
1760 // Add a new 2D panel.
1761 ////////////////////////////////////////////////////////////////////
1763 string panel_path = fgGetString("/sim/panel/path",
1764 "Panels/Default/default.xml");
1766 globals->set_current_panel( fgReadPanel(panel_path) );
1767 if (globals->get_current_panel() == 0) {
1768 SG_LOG( SG_INPUT, SG_ALERT,
1769 "Error reading new panel from " << panel_path );
1771 SG_LOG( SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path );
1772 globals->get_current_panel()->init();
1773 globals->get_current_panel()->bind();
1777 ////////////////////////////////////////////////////////////////////
1778 // Initialize the controls subsystem.
1779 ////////////////////////////////////////////////////////////////////
1781 globals->get_controls()->init();
1782 globals->get_controls()->bind();
1785 ////////////////////////////////////////////////////////////////////
1786 // Initialize the input subsystem.
1787 ////////////////////////////////////////////////////////////////////
1789 globals->add_subsystem("input", new FGInput);
1792 ////////////////////////////////////////////////////////////////////
1793 // Initialize the replay subsystem
1794 ////////////////////////////////////////////////////////////////////
1795 globals->add_subsystem("replay", new FGReplay);
1797 ////////////////////////////////////////////////////////////////////
1798 // Bind and initialize subsystems.
1799 ////////////////////////////////////////////////////////////////////
1801 globals->get_subsystem_mgr()->bind();
1802 globals->get_subsystem_mgr()->init();
1804 #ifdef FG_MPLAYER_AS
1805 ////////////////////////////////////////////////////////////////////
1806 // Initialize multiplayer subsystem
1807 ////////////////////////////////////////////////////////////////////
1809 globals->set_multiplayer_tx_mgr(new FGMultiplayTxMgr);
1810 globals->get_multiplayer_tx_mgr()->init();
1812 globals->set_multiplayer_rx_mgr(new FGMultiplayRxMgr);
1813 globals->get_multiplayer_rx_mgr()->init();
1816 ////////////////////////////////////////////////////////////////////////
1817 // Initialize the Nasal interpreter.
1818 // Do this last, so that the loaded scripts see initialized state
1819 ////////////////////////////////////////////////////////////////////////
1820 FGNasalSys* nasal = new FGNasalSys();
1821 globals->add_subsystem("nasal", nasal);
1824 ////////////////////////////////////////////////////////////////////
1825 // At this point we could try and parse the waypoint options
1826 ///////////////////////////////////////////////////////////////////
1830 ////////////////////////////////////////////////////////////////////////
1831 // End of subsystem initialization.
1832 ////////////////////////////////////////////////////////////////////
1834 SG_LOG( SG_GENERAL, SG_INFO, endl);
1836 // Save the initial state for future
1838 globals->saveInitialState();
1844 void fgReInitSubsystems()
1846 // static const SGPropertyNode *longitude
1847 // = fgGetNode("/sim/presets/longitude-deg");
1848 // static const SGPropertyNode *latitude
1849 // = fgGetNode("/sim/presets/latitude-deg");
1850 static const SGPropertyNode *altitude
1851 = fgGetNode("/sim/presets/altitude-ft");
1852 static const SGPropertyNode *master_freeze
1853 = fgGetNode("/sim/freeze/master");
1855 SG_LOG( SG_GENERAL, SG_INFO,
1856 "fgReInitSubsystems(): /position/altitude = "
1857 << altitude->getDoubleValue() );
1859 bool freeze = master_freeze->getBoolValue();
1861 fgSetBool("/sim/freeze/master", true);
1863 fgSetBool("/sim/crashed", false);
1865 // Initialize the FDM
1868 // allocates structures so must happen before any of the flight
1869 // model or control parameters are set
1870 fgAircraftInit(); // In the future this might not be the case.
1872 // reload offsets from config defaults
1873 globals->get_viewmgr()->reinit();
1877 globals->get_controls()->reset_all();
1879 fgUpdateLocalTime();
1881 // re-init to proper time of day setting
1885 fgSetBool("/sim/freeze/master", false);
1887 fgSetBool("/sim/sceneryloaded",false);