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/auto_gui.hxx>
74 #include <Autopilot/route_mgr.hxx>
75 #include <Autopilot/xmlauto.hxx>
76 #include <Cockpit/cockpit.hxx>
77 #include <Cockpit/panel.hxx>
78 #include <Cockpit/panel_io.hxx>
80 #include <FDM/SP/ADA.hxx>
81 #include <FDM/SP/ACMS.hxx>
83 #include <FDM/Balloon.h>
84 #include <FDM/ExternalNet/ExternalNet.hxx>
85 #include <FDM/ExternalPipe/ExternalPipe.hxx>
86 #include <FDM/JSBSim/JSBSim.hxx>
87 #include <FDM/LaRCsim/LaRCsim.hxx>
88 #include <FDM/MagicCarpet.hxx>
89 #include <FDM/UFO.hxx>
90 #include <FDM/NullFDM.hxx>
91 #include <FDM/YASim/YASim.hxx>
92 #include <GUI/new_gui.hxx>
93 #include <Include/general.hxx>
94 #include <Input/input.hxx>
95 #include <Instrumentation/instrument_mgr.hxx>
96 #include <Model/acmodel.hxx>
97 #include <AIModel/submodel.hxx>
98 #include <AIModel/AIManager.hxx>
99 #include <Navaids/navdb.hxx>
100 #include <Navaids/navlist.hxx>
101 #include <Scenery/scenery.hxx>
102 #include <Scenery/tilemgr.hxx>
103 #include <Scripting/NasalSys.hxx>
104 #include <Sound/fg_fx.hxx>
105 #include <Sound/beacon.hxx>
106 #include <Sound/morse.hxx>
107 #include <Sound/voice.hxx>
108 #include <Systems/system_mgr.hxx>
109 #include <Time/light.hxx>
110 #include <Time/sunsolver.hxx>
111 #include <Time/tmp.hxx>
112 #include <Traffic/TrafficMgr.hxx>
113 #include <MultiPlayer/multiplaymgr.hxx>
115 #include <Environment/environment_mgr.hxx>
117 #include "fg_init.hxx"
119 #include "fg_commands.hxx"
120 #include "fg_props.hxx"
121 #include "options.hxx"
122 #include "globals.hxx"
123 #include "logger.hxx"
124 #include "viewmgr.hxx"
127 #if defined(FX) && defined(XMESA)
128 #include <GL/xmesa.h>
131 SG_USING_STD(string);
135 extern const char *default_root;
139 // Scan the command line options for the specified option and return
141 static string fgScanForOption( const string& option, int argc, char **argv ) {
144 if (hostname == NULL)
147 gethostname(_hostname, 256);
148 hostname = strdup(_hostname);
149 free_hostname = true;
152 SG_LOG(SG_GENERAL, SG_INFO, "Scanning command line for: " << option );
154 int len = option.length();
157 SG_LOG( SG_GENERAL, SG_DEBUG, "argv[" << i << "] = " << argv[i] );
159 string arg = argv[i];
160 if ( arg.find( option ) == 0 ) {
161 return arg.substr( len );
171 // Scan the user config files for the specified option and return
173 static string fgScanForOption( const string& option, const string& path ) {
174 sg_gzifstream in( path );
175 if ( !in.is_open() ) {
179 SG_LOG( SG_GENERAL, SG_INFO, "Scanning " << path << " for: " << option );
181 int len = option.length();
185 while ( ! in.eof() ) {
188 while ( in.get(c) && c != '\0' ) {
193 #if defined( macintosh )
194 getline( in, line, '\r' );
196 getline( in, line, '\n' );
199 // catch extraneous (DOS) line ending character
200 if ( line[line.length() - 1] < 32 ) {
201 line = line.substr( 0, line.length()-1 );
204 if ( line.find( option ) == 0 ) {
205 return line.substr( len );
214 // Scan the user config files for the specified option and return
216 static string fgScanForOption( const string& option ) {
219 #if defined( unix ) || defined( __CYGWIN__ )
220 // Next check home directory for .fgfsrc.hostname file
222 if ( homedir != NULL ) {
223 SGPath config( homedir );
224 config.append( ".fgfsrc" );
225 config.concat( "." );
226 config.concat( hostname );
227 arg = fgScanForOption( option, config.str() );
232 // Next check home directory for .fgfsrc file
234 if ( homedir != NULL ) {
235 SGPath config( homedir );
236 config.append( ".fgfsrc" );
237 arg = fgScanForOption( option, config.str() );
242 // Check for $fg_root/system.fgfsrc
243 SGPath config( globals->get_fg_root() );
244 config.append( "system.fgfsrc" );
245 arg = fgScanForOption( option, config.str() );
252 // Read in configuration (files and command line options) but only set
254 bool fgInitFGRoot ( int argc, char **argv ) {
257 // First parse command line options looking for --fg-root=, this
258 // will override anything specified in a config file
259 root = fgScanForOption( "--fg-root=", argc, argv);
261 // Check in one of the user configuration files.
263 root = fgScanForOption( "--fg-root=" );
265 // Next check if fg-root is set as an env variable
266 if ( root.empty() ) {
267 char *envp = ::getenv( "FG_ROOT" );
268 if ( envp != NULL ) {
273 // Otherwise, default to a random compiled-in location if we can't
274 // find fg-root any other way.
275 if ( root.empty() ) {
276 #if defined( __CYGWIN__ )
277 root = "/FlightGear";
278 #elif defined( WIN32 )
279 root = "\\FlightGear";
280 #elif defined(OSX_BUNDLE)
281 /* the following code looks for the base package directly inside
282 the application bundle. This can be changed fairly easily by
283 fiddling with the code below. And yes, I know it's ugly and verbose.
285 CFBundleRef appBundle = CFBundleGetMainBundle();
286 CFURLRef appUrl = CFBundleCopyBundleURL(appBundle);
287 CFRelease(appBundle);
289 // look for a 'data' subdir directly inside the bundle : is there
290 // a better place? maybe in Resources? I don't know ...
291 CFURLRef dataDir = CFURLCreateCopyAppendingPathComponent(NULL, appUrl, CFSTR("data"), true);
293 // now convert down to a path, and the a c-string
294 CFStringRef path = CFURLCopyFileSystemPath(dataDir, kCFURLPOSIXPathStyle);
295 root = CFStringGetCStringPtr(path, CFStringGetSystemEncoding());
298 CFRelease(appBundle);
306 SG_LOG(SG_INPUT, SG_INFO, "fg_root = " << root );
307 globals->set_fg_root(root);
313 // Read in configuration (files and command line options) but only set
315 bool fgInitFGAircraft ( int argc, char **argv ) {
318 // First parse command line options looking for --aircraft=, this
319 // will override anything specified in a config file
320 aircraft = fgScanForOption( "--aircraft=", argc, argv );
322 // Check in one of the user configuration files.
323 if ( aircraft.empty() )
324 aircraft = fgScanForOption( "--aircraft=" );
326 // if an aircraft was specified, set the property name
327 if ( !aircraft.empty() ) {
328 SG_LOG(SG_INPUT, SG_INFO, "aircraft = " << aircraft );
329 fgSetString("/sim/aircraft", aircraft.c_str() );
331 SG_LOG(SG_INPUT, SG_INFO, "No user specified aircraft, using default" );
338 // Return the current base package version
339 string fgBasePackageVersion() {
340 SGPath base_path( globals->get_fg_root() );
341 base_path.append("version");
343 sg_gzifstream in( base_path.str() );
344 if ( !in.is_open() ) {
345 SGPath old_path( globals->get_fg_root() );
346 old_path.append( "Thanks" );
347 sg_gzifstream old( old_path.str() );
348 if ( !old.is_open() ) {
351 return "[old version]";
362 // Initialize the localization
363 SGPropertyNode *fgInitLocale(const char *language) {
364 SGPropertyNode *c_node = NULL, *d_node = NULL;
365 SGPropertyNode *intl = fgGetNode("/sim/intl");
367 SG_LOG(SG_GENERAL, SG_INFO, "Selecting language: " << language );
369 // localization not defined
374 // Select the proper language from the list
376 vector<SGPropertyNode_ptr> locale = intl->getChildren("locale");
377 for (unsigned int i = 0; i < locale.size(); i++) {
379 vector<SGPropertyNode_ptr> lang = locale[i]->getChildren("lang");
380 for (unsigned int j = 0; j < lang.size(); j++) {
382 if (!strcmp(lang[j]->getStringValue(), language)) {
391 d_node = intl->getChild("locale");
395 // Check for localized font
396 SGPropertyNode *font_n = c_node->getNode("font", true);
397 if ( !strcmp(font_n->getStringValue(), "") )
398 font_n->setStringValue(d_node->getStringValue("font", "typewriter.txf"));
402 // Load the default strings
404 SGPath d_path( globals->get_fg_root() );
406 const char *d_path_str = d_node->getStringValue("strings");
408 SG_LOG(SG_GENERAL, SG_ALERT, "Incorrect path in configuration file.");
412 d_path.append(d_path_str);
413 SG_LOG(SG_GENERAL, SG_INFO, "Reading localized strings from "
416 SGPropertyNode *strings = c_node->getNode("strings");
418 readProperties(d_path.str(), strings);
419 } catch (const sg_exception &e) {
420 SG_LOG(SG_GENERAL, SG_ALERT, "Unable to read the localized strings");
425 // Load the language specific strings
427 if (c_node != d_node) {
428 SGPath c_path( globals->get_fg_root() );
430 const char *c_path_str = c_node->getStringValue("strings");
432 SG_LOG(SG_GENERAL, SG_ALERT, "Incorrect path in configuration file.");
436 c_path.append(c_path_str);
437 SG_LOG(SG_GENERAL, SG_INFO, "Reading localized strings from "
441 readProperties(c_path.str(), strings);
442 } catch (const sg_exception &e) {
443 SG_LOG(SG_GENERAL, SG_ALERT, "Unable to read the localized strings");
453 // Initialize the localization routines
454 bool fgDetectLanguage() {
455 char *language = ::getenv("LANG");
457 if (language == NULL) {
458 SG_LOG(SG_GENERAL, SG_INFO, "Unable to detect the language" );
462 SGPropertyNode *locale = fgInitLocale(language);
464 SG_LOG(SG_GENERAL, SG_ALERT,
465 "No internationalization settings specified in preferences.xml" );
470 globals->set_locale( locale );
475 // Attempt to locate and parse the various non-XML config files in order
476 // from least precidence to greatest precidence
478 do_options (int argc, char ** argv)
480 // Check for $fg_root/system.fgfsrc
481 SGPath config( globals->get_fg_root() );
482 config.append( "system.fgfsrc" );
483 fgParseOptions(config.str());
485 #if defined( unix ) || defined( __CYGWIN__ )
486 config.concat( "." );
487 config.concat( hostname );
488 fgParseOptions(config.str());
491 // Check for ~/.fgfsrc
492 if ( homedir != NULL ) {
493 config.set( homedir );
494 config.append( ".fgfsrc" );
495 fgParseOptions(config.str());
498 #if defined( unix ) || defined( __CYGWIN__ )
499 // Check for ~/.fgfsrc.hostname
500 config.concat( "." );
501 config.concat( hostname );
502 fgParseOptions(config.str());
505 // Parse remaining command line options
506 // These will override anything specified in a config file
507 fgParseArgs(argc, argv);
511 static string fgFindAircraftPath( const SGPath &path, const string &aircraft ) {
513 ulDir *dirp = ulOpenDir(path.str().c_str());
515 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 string 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 fgLoadProps(config.str().c_str(), globals->get_props(), false,
610 SGPropertyNode::USERARCHIVE);
612 SG_LOG(SG_INPUT, SG_BULK, "First time reading user settings");
614 SG_LOG(SG_INPUT, SG_BULK, "Finished Reading user settings");
617 // parse options after loading aircraft to ensure any user
618 // overrides of defaults are honored.
619 do_options(argc, argv);
630 // This function is never used, but maybe useful in the future ???
633 // Preset lon/lat given an airport id
634 static bool fgSetPosFromAirportID( const string& id ) {
638 SG_LOG( SG_GENERAL, SG_INFO,
639 "Attempting to set starting position from airport code " << id );
641 if ( fgFindAirportID( id, &a ) ) {
643 fgSetDouble("/sim/presets/longitude-deg", a->longitude );
644 fgSetDouble("/sim/presets/latitude-deg", a->latitude );
646 // other code depends on the actual postition being set so set
648 fgSetDouble("/position/longitude-deg", a->longitude );
649 fgSetDouble("/position/latitude-deg", a->latitude );
651 SG_LOG( SG_GENERAL, SG_INFO,
652 "Position for " << id << " is (" << a->longitude
653 << ", " << a->latitude << ")" );
663 // Set current tower position lon/lat given an airport id
664 static bool fgSetTowerPosFromAirportID( const string& id, double hdg ) {
666 // tower height hard coded for now...
667 float towerheight=50.0f;
669 // make a little off the heading for 1 runway airports...
670 float fudge_lon = fabs(sin(hdg)) * .003f;
671 float fudge_lat = .003f - fudge_lon;
673 const FGAirport *a = fgFindAirportID( id);
675 fgSetDouble("/sim/tower/longitude-deg", a->getLongitude() + fudge_lon);
676 fgSetDouble("/sim/tower/latitude-deg", a->getLatitude() + fudge_lat);
677 fgSetDouble("/sim/tower/altitude-ft", a->getElevation() + towerheight);
685 struct FGTowerLocationListener : SGPropertyChangeListener {
686 void valueChanged(SGPropertyNode* node) {
687 const double hdg = fgGetDouble( "/orientation/heading-deg", 0);
688 const string id(node->getStringValue());
689 fgSetTowerPosFromAirportID(id, hdg);
693 static void fgInitTowerLocationListener() {
694 fgGetNode("/sim/tower/airport-id", true)
695 ->addChangeListener( new FGTowerLocationListener() );
698 // Set current_options lon/lat given an airport id and heading (degrees)
699 static bool fgSetPosFromAirportIDandHdg( const string& id, double tgt_hdg ) {
703 // set initial position from runway and heading
705 SG_LOG( SG_GENERAL, SG_INFO,
706 "Attempting to set starting position from airport code "
707 << id << " heading " << tgt_hdg );
709 if ( ! globals->get_runways()->search( id, (int)tgt_hdg, &r ) ) {
710 SG_LOG( SG_GENERAL, SG_ALERT,
711 "Failed to find a good runway for " << id << '\n' );
718 double lat2, lon2, az2;
719 double heading = r._heading;
720 double azimuth = heading + 180.0;
721 while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
723 SG_LOG( SG_GENERAL, SG_INFO,
724 "runway = " << r._lon << ", " << r._lat
725 << " length = " << r._length * SG_FEET_TO_METER
726 << " heading = " << azimuth );
728 geo_direct_wgs_84 ( 0, r._lat, r._lon, azimuth,
729 r._length * SG_FEET_TO_METER * 0.5 - 5.0,
730 &lat2, &lon2, &az2 );
732 if ( fabs( fgGetDouble("/sim/presets/offset-distance") ) > SG_EPSILON ) {
734 double odist = fgGetDouble("/sim/presets/offset-distance");
735 odist *= SG_NM_TO_METER;
736 double oaz = azimuth;
737 if ( fabs(fgGetDouble("/sim/presets/offset-azimuth")) > SG_EPSILON ) {
738 oaz = fgGetDouble("/sim/presets/offset-azimuth") + 180;
741 while ( oaz >= 360.0 ) { oaz -= 360.0; }
742 geo_direct_wgs_84 ( 0, lat2, lon2, oaz, odist, &olat, &olon, &az2 );
748 fgSetDouble("/sim/presets/longitude-deg", lon2 );
749 fgSetDouble("/sim/presets/latitude-deg", lat2 );
750 fgSetDouble("/sim/presets/heading-deg", heading );
752 // other code depends on the actual values being set ...
753 fgSetDouble("/position/longitude-deg", lon2 );
754 fgSetDouble("/position/latitude-deg", lat2 );
755 fgSetDouble("/orientation/heading-deg", heading );
757 SG_LOG( SG_GENERAL, SG_INFO,
758 "Position for " << id << " is ("
760 << lat2 << ") new heading is "
767 // Set current_options lon/lat given an airport id and runway number
768 static bool fgSetPosFromAirportIDandRwy( const string& id, const string& rwy ) {
772 // set initial position from airport and runway number
774 SG_LOG( SG_GENERAL, SG_INFO,
775 "Attempting to set starting position for "
776 << id << ":" << rwy );
778 if ( ! globals->get_runways()->search( id, rwy, &r ) ) {
779 SG_LOG( SG_GENERAL, SG_ALERT,
780 "Failed to find runway " << rwy <<
781 " at airport " << id );
788 double lat2, lon2, az2;
789 double heading = r._heading;
790 double azimuth = heading + 180.0;
791 while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
793 SG_LOG( SG_GENERAL, SG_INFO,
794 "runway = " << r._lon << ", " << r._lat
795 << " length = " << r._length * SG_FEET_TO_METER
796 << " heading = " << azimuth );
798 geo_direct_wgs_84 ( 0, r._lat, r._lon,
800 r._length * SG_FEET_TO_METER * 0.5 - 5.0,
801 &lat2, &lon2, &az2 );
803 if ( fabs( fgGetDouble("/sim/presets/offset-distance") ) > SG_EPSILON )
806 double odist = fgGetDouble("/sim/presets/offset-distance");
807 odist *= SG_NM_TO_METER;
808 double oaz = azimuth;
809 if ( fabs(fgGetDouble("/sim/presets/offset-azimuth")) > SG_EPSILON )
811 oaz = fgGetDouble("/sim/presets/offset-azimuth") + 180;
812 heading = fgGetDouble("/sim/presets/heading-deg");
814 while ( oaz >= 360.0 ) { oaz -= 360.0; }
815 geo_direct_wgs_84 ( 0, lat2, lon2, oaz, odist, &olat, &olon, &az2 );
821 fgSetDouble("/sim/presets/longitude-deg", lon2 );
822 fgSetDouble("/sim/presets/latitude-deg", lat2 );
823 fgSetDouble("/sim/presets/heading-deg", heading );
825 // other code depends on the actual values being set ...
826 fgSetDouble("/position/longitude-deg", lon2 );
827 fgSetDouble("/position/latitude-deg", lat2 );
828 fgSetDouble("/orientation/heading-deg", heading );
830 SG_LOG( SG_GENERAL, SG_INFO,
831 "Position for " << id << " is ("
833 << lat2 << ") new heading is "
840 static void fgSetDistOrAltFromGlideSlope() {
841 // cout << "fgSetDistOrAltFromGlideSlope()" << endl;
842 string apt_id = fgGetString("/sim/presets/airport-id");
843 double gs = fgGetDouble("/sim/presets/glideslope-deg")
844 * SG_DEGREES_TO_RADIANS ;
845 double od = fgGetDouble("/sim/presets/offset-distance");
846 double alt = fgGetDouble("/sim/presets/altitude-ft");
848 double apt_elev = 0.0;
849 if ( ! apt_id.empty() ) {
850 apt_elev = fgGetAirportElev( apt_id );
851 if ( apt_elev < -9990.0 ) {
858 if( fabs(gs) > 0.01 && fabs(od) > 0.1 && alt < -9990 ) {
859 // set altitude from glideslope and offset-distance
860 od *= SG_NM_TO_METER * SG_METER_TO_FEET;
861 alt = fabs(od*tan(gs)) + apt_elev;
862 fgSetDouble("/sim/presets/altitude-ft", alt);
863 fgSetBool("/sim/presets/onground", false);
864 SG_LOG( SG_GENERAL, SG_INFO, "Calculated altitude as: "
866 } else if( fabs(gs) > 0.01 && alt > 0 && fabs(od) < 0.1) {
867 // set offset-distance from glideslope and altitude
868 od = (alt - apt_elev) / tan(gs);
869 od *= -1*SG_FEET_TO_METER * SG_METER_TO_NM;
870 fgSetDouble("/sim/presets/offset-distance", od);
871 fgSetBool("/sim/presets/onground", false);
872 SG_LOG( SG_GENERAL, SG_INFO, "Calculated offset distance as: "
874 } else if( fabs(gs) > 0.01 ) {
875 SG_LOG( SG_GENERAL, SG_ALERT,
876 "Glideslope given but not altitude or offset-distance." );
877 SG_LOG( SG_GENERAL, SG_ALERT, "Resetting glideslope to zero" );
878 fgSetDouble("/sim/presets/glideslope-deg", 0);
879 fgSetBool("/sim/presets/onground", true);
884 // Set current_options lon/lat given an airport id and heading (degrees)
885 static bool fgSetPosFromNAV( const string& id, const double& freq ) {
887 = globals->get_navlist()->findByIdentAndFreq( id.c_str(), freq );
889 // set initial position from runway and heading
891 SG_LOG( SG_GENERAL, SG_INFO, "Attempting to set starting position for "
892 << id << ":" << freq );
894 double lon = nav->get_lon();
895 double lat = nav->get_lat();
897 if ( fabs( fgGetDouble("/sim/presets/offset-distance") ) > SG_EPSILON )
899 double odist = fgGetDouble("/sim/presets/offset-distance")
901 double oaz = fabs(fgGetDouble("/sim/presets/offset-azimuth"))
903 while ( oaz >= 360.0 ) { oaz -= 360.0; }
904 double olat, olon, az2;
905 geo_direct_wgs_84 ( 0, lat, lon, oaz, odist, &olat, &olon, &az2 );
912 fgSetDouble("/sim/presets/longitude-deg", lon );
913 fgSetDouble("/sim/presets/latitude-deg", lat );
915 // other code depends on the actual values being set ...
916 fgSetDouble("/position/longitude-deg", lon );
917 fgSetDouble("/position/latitude-deg", lat );
918 fgSetDouble("/orientation/heading-deg",
919 fgGetDouble("/sim/presets/heading-deg") );
921 SG_LOG( SG_GENERAL, SG_INFO,
922 "Position for " << id << ":" << freq << " is ("
923 << lon << ", "<< lat << ")" );
927 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to locate NAV = "
928 << id << ":" << freq );
933 // Set current_options lon/lat given an aircraft carrier id
934 static bool fgSetPosFromCarrier( const string& carrier, const string& posid ) {
936 // set initial position from runway and heading
940 if (FGAIManager::getStartPosition(carrier, posid, geodPos, heading, uvw)) {
941 double lon = geodPos.getLongitudeDeg();
942 double lat = geodPos.getLatitudeDeg();
943 double alt = geodPos.getElevationFt();
945 SG_LOG( SG_GENERAL, SG_INFO, "Attempting to set starting position for "
946 << carrier << " at lat = " << lat << ", lon = " << lon
947 << ", alt = " << alt << ", heading = " << heading);
949 fgSetDouble("/sim/presets/longitude-deg", lon);
950 fgSetDouble("/sim/presets/latitude-deg", lat);
951 fgSetDouble("/sim/presets/altitude-ft", alt);
952 fgSetDouble("/sim/presets/heading-deg", heading);
953 fgSetDouble("/position/longitude-deg", lon);
954 fgSetDouble("/position/latitude-deg", lat);
955 fgSetDouble("/position/altitude-ft", alt);
956 fgSetDouble("/orientation/heading-deg", heading);
958 fgSetString("/sim/presets/speed-set", "UVW");
959 fgSetDouble("/velocities/uBody-fps", uvw(0));
960 fgSetDouble("/velocities/vBody-fps", uvw(1));
961 fgSetDouble("/velocities/wBody-fps", uvw(2));
962 fgSetDouble("/sim/presets/uBody-fps", uvw(0));
963 fgSetDouble("/sim/presets/vBody-fps", uvw(1));
964 fgSetDouble("/sim/presets/wBody-fps", uvw(2));
966 fgSetBool("/sim/presets/onground", true);
970 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to locate aircraft carrier = "
976 // Set current_options lon/lat given an airport id and heading (degrees)
977 static bool fgSetPosFromFix( const string& id ) {
980 // set initial position from runway and heading
981 if ( globals->get_fixlist()->query( id.c_str(), &fix ) ) {
982 SG_LOG( SG_GENERAL, SG_INFO, "Attempting to set starting position for "
985 double lon = fix.get_lon();
986 double lat = fix.get_lat();
988 if ( fabs( fgGetDouble("/sim/presets/offset-distance") ) > SG_EPSILON )
990 double odist = fgGetDouble("/sim/presets/offset-distance")
992 double oaz = fabs(fgGetDouble("/sim/presets/offset-azimuth"))
994 while ( oaz >= 360.0 ) { oaz -= 360.0; }
995 double olat, olon, az2;
996 geo_direct_wgs_84 ( 0, lat, lon, oaz, odist, &olat, &olon, &az2 );
1003 fgSetDouble("/sim/presets/longitude-deg", lon );
1004 fgSetDouble("/sim/presets/latitude-deg", lat );
1006 // other code depends on the actual values being set ...
1007 fgSetDouble("/position/longitude-deg", lon );
1008 fgSetDouble("/position/latitude-deg", lat );
1009 fgSetDouble("/orientation/heading-deg",
1010 fgGetDouble("/sim/presets/heading-deg") );
1012 SG_LOG( SG_GENERAL, SG_INFO,
1013 "Position for " << id << " is ("
1014 << lon << ", "<< lat << ")" );
1018 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to locate NAV = "
1024 static void parseWaypoints() {
1025 string_list *waypoints = globals->get_initial_waypoints();
1027 vector<string>::iterator i;
1028 for (i = waypoints->begin();
1029 i != waypoints->end();
1034 // Now were done using the way points we can deallocate the
1036 while (waypoints->begin() != waypoints->end()) {
1037 waypoints->pop_back();
1040 globals->set_initial_waypoints(0);
1049 * Initialize vor/ndb/ils/fix list management and query systems (as
1050 * well as simple airport db list)
1055 SG_LOG(SG_GENERAL, SG_INFO, "Loading Airport Database ...");
1057 SGPath aptdb( globals->get_fg_root() );
1058 aptdb.append( "Airports/apt.dat" );
1060 SGPath p_metar( globals->get_fg_root() );
1061 p_metar.append( "Airports/metar.dat" );
1063 FGAirportList *airports = new FGAirportList();
1064 globals->set_airports( airports );
1065 FGRunwayList *runways = new FGRunwayList();
1066 globals->set_runways( runways );
1068 fgAirportDBLoad( airports, runways, aptdb.str(), p_metar.str() );
1070 FGNavList *navlist = new FGNavList;
1071 FGNavList *loclist = new FGNavList;
1072 FGNavList *gslist = new FGNavList;
1073 FGNavList *dmelist = new FGNavList;
1074 FGNavList *mkrlist = new FGNavList;
1075 FGNavList *tacanlist = new FGNavList;
1076 FGNavList *carrierlist = new FGNavList;
1077 FGTACANList *channellist = new FGTACANList;
1079 globals->set_navlist( navlist );
1080 globals->set_loclist( loclist );
1081 globals->set_gslist( gslist );
1082 globals->set_dmelist( dmelist );
1083 globals->set_mkrlist( mkrlist );
1084 globals->set_tacanlist( tacanlist );
1085 globals->set_carrierlist( carrierlist );
1086 globals->set_channellist( channellist );
1088 if ( !fgNavDBInit(airports, navlist, loclist, gslist, dmelist, mkrlist, tacanlist, carrierlist, channellist) ) {
1089 SG_LOG( SG_GENERAL, SG_ALERT,
1090 "Problems loading one or more navigational database" );
1093 if ( fgGetBool("/sim/navdb/localizers/auto-align", true) ) {
1094 // align all the localizers with their corresponding runways
1095 // since data sources are good for cockpit navigation
1096 // purposes, but not always to the error tolerances needed to
1097 // exactly place these items.
1099 = fgGetDouble( "/sim/navdb/localizers/auto-align-threshold-deg",
1101 fgNavDBAlignLOCwithRunway( runways, loclist, threshold );
1104 SG_LOG(SG_GENERAL, SG_INFO, " Fixes");
1105 SGPath p_fix( globals->get_fg_root() );
1106 p_fix.append( "Navaids/fix.dat" );
1107 FGFixList *fixlist = new FGFixList;
1108 fixlist->init( p_fix );
1109 globals->set_fixlist( fixlist );
1115 // Set the initial position based on presets (or defaults)
1116 bool fgInitPosition() {
1117 // cout << "fgInitPosition()" << endl;
1118 double gs = fgGetDouble("/sim/presets/glideslope-deg")
1119 * SG_DEGREES_TO_RADIANS ;
1120 double od = fgGetDouble("/sim/presets/offset-distance");
1121 double alt = fgGetDouble("/sim/presets/altitude-ft");
1123 bool set_pos = false;
1125 // If glideslope is specified, then calculate offset-distance or
1126 // altitude relative to glide slope if either of those was not
1128 if ( fabs( gs ) > 0.01 ) {
1129 fgSetDistOrAltFromGlideSlope();
1133 // If we have an explicit, in-range lon/lat, don't change it, just use it.
1134 // If not, check for an airport-id and use that.
1135 // If not, default to the middle of the KSFO field.
1136 // The default values for lon/lat are deliberately out of range
1137 // so that the airport-id can take effect; valid lon/lat will
1138 // override airport-id, however.
1139 double lon_deg = fgGetDouble("/sim/presets/longitude-deg");
1140 double lat_deg = fgGetDouble("/sim/presets/latitude-deg");
1141 if ( lon_deg >= -180.0 && lon_deg <= 180.0
1142 && lat_deg >= -90.0 && lat_deg <= 90.0 )
1147 string apt = fgGetString("/sim/presets/airport-id");
1148 string rwy_no = fgGetString("/sim/presets/runway");
1149 double hdg = fgGetDouble("/sim/presets/heading-deg");
1150 string vor = fgGetString("/sim/presets/vor-id");
1151 double vor_freq = fgGetDouble("/sim/presets/vor-freq");
1152 string ndb = fgGetString("/sim/presets/ndb-id");
1153 double ndb_freq = fgGetDouble("/sim/presets/ndb-freq");
1154 string carrier = fgGetString("/sim/presets/carrier");
1155 string parkpos = fgGetString("/sim/presets/parkpos");
1156 string fix = fgGetString("/sim/presets/fix");
1158 fgSetDouble( "/orientation/heading-deg", hdg );
1159 fgInitTowerLocationListener();
1161 if ( !set_pos && !apt.empty() && !rwy_no.empty() ) {
1162 // An airport + runway is requested
1163 if ( fgSetPosFromAirportIDandRwy( apt, rwy_no ) ) {
1164 // set tower position (a little off the heading for single
1166 fgSetString("/sim/tower/airport-id", apt.c_str());
1171 if ( !set_pos && !apt.empty() ) {
1172 // An airport is requested (find runway closest to hdg)
1173 if ( fgSetPosFromAirportIDandHdg( apt, hdg ) ) {
1174 // set tower position (a little off the heading for single
1176 fgSetString("/sim/tower/airport-id", apt.c_str());
1181 if ( !set_pos && !vor.empty() ) {
1182 // a VOR is requested
1183 if ( fgSetPosFromNAV( vor, vor_freq ) ) {
1188 if ( !set_pos && !ndb.empty() ) {
1189 // an NDB is requested
1190 if ( fgSetPosFromNAV( ndb, ndb_freq ) ) {
1195 if ( !set_pos && !carrier.empty() ) {
1196 // an aircraft carrier is requested
1197 if ( fgSetPosFromCarrier( carrier, parkpos ) ) {
1202 if ( !set_pos && !fix.empty() ) {
1203 // a Fix is requested
1204 if ( fgSetPosFromFix( fix ) ) {
1210 // No lon/lat specified, no airport specified, default to
1211 // middle of KSFO field.
1212 fgSetDouble("/sim/presets/longitude-deg", -122.374843);
1213 fgSetDouble("/sim/presets/latitude-deg", 37.619002);
1216 fgSetDouble( "/position/longitude-deg",
1217 fgGetDouble("/sim/presets/longitude-deg") );
1218 fgSetDouble( "/position/latitude-deg",
1219 fgGetDouble("/sim/presets/latitude-deg") );
1221 // determine if this should be an on-ground or in-air start
1222 if ((fabs(gs) > 0.01 || fabs(od) > 0.1 || alt > 0.1) && carrier.empty()) {
1223 fgSetBool("/sim/presets/onground", false);
1225 fgSetBool("/sim/presets/onground", true);
1232 // General house keeping initializations
1233 bool fgInitGeneral() {
1236 #if defined(FX) && defined(XMESA)
1237 char *mesa_win_state;
1240 SG_LOG( SG_GENERAL, SG_INFO, "General Initialization" );
1241 SG_LOG( SG_GENERAL, SG_INFO, "======= ==============" );
1243 root = globals->get_fg_root();
1244 if ( ! root.length() ) {
1245 // No root path set? Then bail ...
1246 SG_LOG( SG_GENERAL, SG_ALERT,
1247 "Cannot continue without a path to the base package "
1248 << "being defined." );
1251 SG_LOG( SG_GENERAL, SG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
1253 #if defined(FX) && defined(XMESA)
1254 // initialize full screen flag
1255 globals->set_fullscreen(false);
1256 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
1257 // Test for the MESA_GLX_FX env variable
1258 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
1259 // test if we are fullscreen mesa/glide
1260 if ( (mesa_win_state[0] == 'f') ||
1261 (mesa_win_state[0] == 'F') ) {
1262 globals->set_fullscreen(true);
1272 // Initialize the flight model subsystem. This just creates the
1273 // object. The actual fdm initialization is delayed until we get a
1274 // proper scenery elevation hit. This is checked for in main.cxx
1278 if ( cur_fdm_state ) {
1279 delete cur_fdm_state;
1283 double dt = 1.0 / fgGetInt("/sim/model-hz");
1284 string model = fgGetString("/sim/flight-model");
1287 if ( model == "larcsim" ) {
1288 cur_fdm_state = new FGLaRCsim( dt );
1289 } else if ( model == "jsb" ) {
1290 cur_fdm_state = new FGJSBsim( dt );
1292 } else if ( model == "ada" ) {
1293 cur_fdm_state = new FGADA( dt );
1294 } else if ( model == "acms" ) {
1295 cur_fdm_state = new FGACMS( dt );
1297 } else if ( model == "balloon" ) {
1298 cur_fdm_state = new FGBalloonSim( dt );
1299 } else if ( model == "magic" ) {
1300 cur_fdm_state = new FGMagicCarpet( dt );
1301 } else if ( model == "ufo" ) {
1302 cur_fdm_state = new FGUFO( dt );
1303 } else if ( model == "external" ) {
1304 // external is a synonym for "--fdm=null" and is
1305 // maintained here for backwards compatibility
1306 cur_fdm_state = new FGNullFDM( dt );
1307 } else if ( model.find("network") == 0 ) {
1308 string host = "localhost";
1312 string net_options = model.substr(8);
1313 string::size_type begin, end;
1316 end = net_options.find( ",", begin );
1317 if ( end != string::npos ) {
1318 host = net_options.substr(begin, end - begin);
1322 end = net_options.find( ",", begin );
1323 if ( end != string::npos ) {
1324 port1 = atoi( net_options.substr(begin, end - begin).c_str() );
1328 end = net_options.find( ",", begin );
1329 if ( end != string::npos ) {
1330 port2 = atoi( net_options.substr(begin, end - begin).c_str() );
1334 end = net_options.find( ",", begin );
1335 if ( end != string::npos ) {
1336 port3 = atoi( net_options.substr(begin, end - begin).c_str() );
1339 cur_fdm_state = new FGExternalNet( dt, host, port1, port2, port3 );
1340 } else if ( model.find("pipe") == 0 ) {
1341 // /* old */ string pipe_path = model.substr(5);
1342 // /* old */ cur_fdm_state = new FGExternalPipe( dt, pipe_path );
1343 string pipe_path = "";
1344 string pipe_protocol = "";
1345 string pipe_options = model.substr(5);
1346 string::size_type begin, end;
1349 end = pipe_options.find( ",", begin );
1350 if ( end != string::npos ) {
1351 pipe_path = pipe_options.substr(begin, end - begin);
1354 // protocol (last option)
1355 pipe_protocol = pipe_options.substr(begin);
1356 cur_fdm_state = new FGExternalPipe( dt, pipe_path, pipe_protocol );
1357 } else if ( model == "null" ) {
1358 cur_fdm_state = new FGNullFDM( dt );
1359 } else if ( model == "yasim" ) {
1360 cur_fdm_state = new YASim( dt );
1362 SG_LOG(SG_GENERAL, SG_ALERT,
1363 "Unrecognized flight model '" << model
1364 << "', cannot init flight dynamics model.");
1368 SG_LOG(SG_GENERAL, SG_ALERT, "FlightGear aborting\n\n");
1373 static void printMat(const sgVec4 *mat, char *name="")
1376 SG_LOG(SG_GENERAL, SG_BULK, name );
1377 for(i=0; i<4; i++) {
1378 SG_LOG(SG_GENERAL, SG_BULK, " " << mat[i][0] << " " << mat[i][1]
1379 << " " << mat[i][2] << " " << mat[i][3] );
1383 // Initialize view parameters
1385 // force update of model so that viewer can get some data...
1386 globals->get_aircraft_model()->update(0);
1387 // run update for current view so that data is current...
1388 globals->get_viewmgr()->update(0);
1390 printMat(globals->get_current_view()->get_VIEW(),"VIEW");
1391 printMat(globals->get_current_view()->get_UP(),"UP");
1392 // printMat(globals->get_current_view()->get_LOCAL(),"LOCAL");
1397 SGTime *fgInitTime() {
1399 static const SGPropertyNode *longitude
1400 = fgGetNode("/position/longitude-deg");
1401 static const SGPropertyNode *latitude
1402 = fgGetNode("/position/latitude-deg");
1403 static const SGPropertyNode *cur_time_override
1404 = fgGetNode("/sim/time/cur-time-override", true);
1406 SGPath zone( globals->get_fg_root() );
1407 zone.append( "Timezone" );
1408 SGTime *t = new SGTime( longitude->getDoubleValue()
1409 * SGD_DEGREES_TO_RADIANS,
1410 latitude->getDoubleValue()
1411 * SGD_DEGREES_TO_RADIANS,
1413 cur_time_override->getLongValue() );
1415 globals->set_warp_delta( 0 );
1417 t->update( 0.0, 0.0,
1418 cur_time_override->getLongValue(),
1419 globals->get_warp() );
1425 // set up a time offset (aka warp) if one is specified
1426 void fgInitTimeOffset() {
1427 static const SGPropertyNode *longitude
1428 = fgGetNode("/position/longitude-deg");
1429 static const SGPropertyNode *latitude
1430 = fgGetNode("/position/latitude-deg");
1431 static const SGPropertyNode *cur_time_override
1432 = fgGetNode("/sim/time/cur-time-override", true);
1434 // Handle potential user specified time offsets
1435 int orig_warp = globals->get_warp();
1436 SGTime *t = globals->get_time_params();
1437 time_t cur_time = t->get_cur_time();
1438 time_t currGMT = sgTimeGetGMT( gmtime(&cur_time) );
1439 time_t systemLocalTime = sgTimeGetGMT( localtime(&cur_time) );
1440 time_t aircraftLocalTime =
1441 sgTimeGetGMT( fgLocaltime(&cur_time, t->get_zonename() ) );
1443 // Okay, we now have several possible scenarios
1444 int offset = fgGetInt("/sim/startup/time-offset");
1445 string offset_type = fgGetString("/sim/startup/time-offset-type");
1448 if ( offset_type == "real" ) {
1450 } else if ( offset_type == "dawn" ) {
1451 warp = fgTimeSecondsUntilSunAngle( cur_time,
1452 longitude->getDoubleValue()
1453 * SGD_DEGREES_TO_RADIANS,
1454 latitude->getDoubleValue()
1455 * SGD_DEGREES_TO_RADIANS,
1457 } else if ( offset_type == "morning" ) {
1458 warp = fgTimeSecondsUntilSunAngle( cur_time,
1459 longitude->getDoubleValue()
1460 * SGD_DEGREES_TO_RADIANS,
1461 latitude->getDoubleValue()
1462 * SGD_DEGREES_TO_RADIANS,
1464 } else if ( offset_type == "noon" ) {
1465 warp = fgTimeSecondsUntilSunAngle( cur_time,
1466 longitude->getDoubleValue()
1467 * SGD_DEGREES_TO_RADIANS,
1468 latitude->getDoubleValue()
1469 * SGD_DEGREES_TO_RADIANS,
1471 } else if ( offset_type == "afternoon" ) {
1472 warp = fgTimeSecondsUntilSunAngle( cur_time,
1473 longitude->getDoubleValue()
1474 * SGD_DEGREES_TO_RADIANS,
1475 latitude->getDoubleValue()
1476 * SGD_DEGREES_TO_RADIANS,
1478 } else if ( offset_type == "dusk" ) {
1479 warp = fgTimeSecondsUntilSunAngle( cur_time,
1480 longitude->getDoubleValue()
1481 * SGD_DEGREES_TO_RADIANS,
1482 latitude->getDoubleValue()
1483 * SGD_DEGREES_TO_RADIANS,
1485 } else if ( offset_type == "evening" ) {
1486 warp = fgTimeSecondsUntilSunAngle( cur_time,
1487 longitude->getDoubleValue()
1488 * SGD_DEGREES_TO_RADIANS,
1489 latitude->getDoubleValue()
1490 * SGD_DEGREES_TO_RADIANS,
1492 } else if ( offset_type == "midnight" ) {
1493 warp = fgTimeSecondsUntilSunAngle( cur_time,
1494 longitude->getDoubleValue()
1495 * SGD_DEGREES_TO_RADIANS,
1496 latitude->getDoubleValue()
1497 * SGD_DEGREES_TO_RADIANS,
1499 } else if ( offset_type == "system-offset" ) {
1501 } else if ( offset_type == "gmt-offset" ) {
1502 warp = offset - (currGMT - systemLocalTime);
1503 } else if ( offset_type == "latitude-offset" ) {
1504 warp = offset - (aircraftLocalTime - systemLocalTime);
1505 } else if ( offset_type == "system" ) {
1506 warp = offset - cur_time;
1507 } else if ( offset_type == "gmt" ) {
1508 warp = offset - currGMT;
1509 } else if ( offset_type == "latitude" ) {
1510 warp = offset - (aircraftLocalTime - systemLocalTime) - cur_time;
1512 SG_LOG( SG_GENERAL, SG_ALERT,
1513 "FG_TIME::Unsupported offset type " << offset_type );
1516 globals->set_warp( orig_warp + warp );
1517 t->update( longitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
1518 latitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
1519 cur_time_override->getLongValue(),
1520 globals->get_warp() );
1522 SG_LOG( SG_GENERAL, SG_INFO, "After fgInitTimeOffset(): warp = "
1523 << globals->get_warp() );
1527 // This is the top level init routine which calls all the other
1528 // initialization routines. If you are adding a subsystem to flight
1529 // gear, its initialization call should located in this routine.
1530 // Returns non-zero if a problem encountered.
1531 bool fgInitSubsystems() {
1532 // static const SGPropertyNode *longitude
1533 // = fgGetNode("/sim/presets/longitude-deg");
1534 // static const SGPropertyNode *latitude
1535 // = fgGetNode("/sim/presets/latitude-deg");
1536 // static const SGPropertyNode *altitude
1537 // = fgGetNode("/sim/presets/altitude-ft");
1538 SG_LOG( SG_GENERAL, SG_INFO, "Initialize Subsystems");
1539 SG_LOG( SG_GENERAL, SG_INFO, "========== ==========");
1541 ////////////////////////////////////////////////////////////////////
1542 // Initialize the event manager subsystem.
1543 ////////////////////////////////////////////////////////////////////
1545 globals->get_event_mgr()->init();
1546 globals->get_event_mgr()->setRealtimeProperty(fgGetNode("/sim/time/delta-realtime-sec", true));
1548 ////////////////////////////////////////////////////////////////////
1549 // Initialize the property interpolator subsystem
1550 ////////////////////////////////////////////////////////////////////
1551 globals->add_subsystem("interpolator", new SGInterpolator);
1554 ////////////////////////////////////////////////////////////////////
1555 // Add the FlightGear property utilities.
1556 ////////////////////////////////////////////////////////////////////
1557 globals->add_subsystem("properties", new FGProperties);
1559 ////////////////////////////////////////////////////////////////////
1560 // Initialize the material property subsystem.
1561 ////////////////////////////////////////////////////////////////////
1563 SGPath mpath( globals->get_fg_root() );
1564 mpath.append( "materials.xml" );
1565 string season = fgGetString("/sim/startup/season", "summer");
1566 if ( ! globals->get_matlib()->load(globals->get_fg_root(), mpath.str(), season.c_str()) ) {
1567 SG_LOG( SG_GENERAL, SG_ALERT, "Error loading material lib!" );
1572 ////////////////////////////////////////////////////////////////////
1573 // Initialize the scenery management subsystem.
1574 ////////////////////////////////////////////////////////////////////
1576 if ( globals->get_tile_mgr()->init() ) {
1577 // Load the local scenery data
1578 double visibility_meters = fgGetDouble("/environment/visibility-m");
1580 globals->get_tile_mgr()->update( visibility_meters );
1582 SG_LOG( SG_GENERAL, SG_ALERT, "Error in Tile Manager initialization!" );
1586 // cause refresh of viewer scenery timestamps every 15 seconds...
1587 globals->get_event_mgr()->addTask( "FGTileMgr::refresh_view_timestamps()",
1588 globals->get_tile_mgr(),
1589 &FGTileMgr::refresh_view_timestamps,
1593 ////////////////////////////////////////////////////////////////////
1594 // Initialize the flight model subsystem.
1595 ////////////////////////////////////////////////////////////////////
1599 // allocates structures so must happen before any of the flight
1600 // model or control parameters are set
1601 fgAircraftInit(); // In the future this might not be the case.
1604 ////////////////////////////////////////////////////////////////////
1605 // Initialize the XML Autopilot subsystem.
1606 ////////////////////////////////////////////////////////////////////
1608 globals->add_subsystem( "xml-autopilot", new FGXMLAutopilot );
1609 globals->add_subsystem( "route-manager", new FGRouteMgr );
1612 ////////////////////////////////////////////////////////////////////
1613 // Initialize the view manager subsystem.
1614 ////////////////////////////////////////////////////////////////////
1618 ////////////////////////////////////////////////////////////////////
1619 // Create and register the logger.
1620 ////////////////////////////////////////////////////////////////////
1622 globals->add_subsystem("logger", new FGLogger);
1624 ////////////////////////////////////////////////////////////////////
1625 // Create and register the XML GUI.
1626 ////////////////////////////////////////////////////////////////////
1628 globals->add_subsystem("gui", new NewGUI, SGSubsystemMgr::INIT);
1631 ////////////////////////////////////////////////////////////////////
1632 // Initialize the local time subsystem.
1633 ////////////////////////////////////////////////////////////////////
1635 // update the current timezone each 30 minutes
1636 globals->get_event_mgr()->addTask( "fgUpdateLocalTime()",
1637 &fgUpdateLocalTime, 30*60 );
1638 fgInitTimeOffset(); // the environment subsystem needs this
1641 ////////////////////////////////////////////////////////////////////
1642 // Initialize the weather subsystem.
1643 ////////////////////////////////////////////////////////////////////
1645 // Initialize the weather modeling subsystem
1646 globals->add_subsystem("environment", new FGEnvironmentMgr);
1649 ////////////////////////////////////////////////////////////////////
1650 // Initialize the lighting subsystem.
1651 ////////////////////////////////////////////////////////////////////
1653 globals->add_subsystem("lighting", new FGLight);
1655 //////////////////////////////////////////////////////////////////////
1656 // Initialize the 2D cloud subsystem.
1657 ////////////////////////////////////////////////////////////////////
1658 fgGetBool("/sim/rendering/bump-mapping", false);
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);
1677 globals->add_subsystem("voice", new FGVoiceMgr);
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 ////////////////////////////////////////////////////////////////////
1805 // Initialize multiplayer subsystem
1806 ////////////////////////////////////////////////////////////////////
1808 globals->set_multiplayer_mgr(new FGMultiplayMgr);
1809 globals->get_multiplayer_mgr()->init();
1811 ////////////////////////////////////////////////////////////////////////
1812 // Initialize the Nasal interpreter.
1813 // Do this last, so that the loaded scripts see initialized state
1814 ////////////////////////////////////////////////////////////////////////
1815 FGNasalSys* nasal = new FGNasalSys();
1816 globals->add_subsystem("nasal", nasal);
1819 ////////////////////////////////////////////////////////////////////
1820 // At this point we could try and parse the waypoint options
1821 ///////////////////////////////////////////////////////////////////
1824 // initialize methods that depend on other subsystems.
1825 globals->get_subsystem_mgr()->postinit();
1827 ////////////////////////////////////////////////////////////////////////
1828 // End of subsystem initialization.
1829 ////////////////////////////////////////////////////////////////////
1831 SG_LOG( SG_GENERAL, SG_INFO, endl);
1833 // Save the initial state for future
1835 globals->saveInitialState();
1841 void fgReInitSubsystems()
1843 // static const SGPropertyNode *longitude
1844 // = fgGetNode("/sim/presets/longitude-deg");
1845 // static const SGPropertyNode *latitude
1846 // = fgGetNode("/sim/presets/latitude-deg");
1847 static const SGPropertyNode *altitude
1848 = fgGetNode("/sim/presets/altitude-ft");
1849 static const SGPropertyNode *master_freeze
1850 = fgGetNode("/sim/freeze/master");
1852 SG_LOG( SG_GENERAL, SG_INFO,
1853 "fgReInitSubsystems(): /position/altitude = "
1854 << altitude->getDoubleValue() );
1856 bool freeze = master_freeze->getBoolValue();
1858 fgSetBool("/sim/freeze/master", true);
1860 fgSetBool("/sim/crashed", false);
1862 // Force reupdating the positions of the ai 3d models. They are used for
1863 // initializing ground level for the FDM.
1864 globals->get_subsystem("ai_model")->reinit();
1866 // Initialize the FDM
1869 // allocates structures so must happen before any of the flight
1870 // model or control parameters are set
1871 fgAircraftInit(); // In the future this might not be the case.
1873 // reload offsets from config defaults
1874 globals->get_viewmgr()->reinit();
1878 globals->get_controls()->reset_all();
1880 fgUpdateLocalTime();
1882 // re-init to proper time of day setting
1886 fgSetBool("/sim/freeze/master", false);
1888 fgSetBool("/sim/sceneryloaded",false);