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/runways.hxx>
74 #include <Airports/simple.hxx>
75 #include <ATC/ATCdisplay.hxx>
76 #include <ATC/ATCmgr.hxx>
77 #include <ATC/AIMgr.hxx>
78 #include <Autopilot/auto_gui.hxx>
79 #include <Autopilot/route_mgr.hxx>
80 #include <Autopilot/xmlauto.hxx>
81 #include <Cockpit/cockpit.hxx>
82 #include <Cockpit/panel.hxx>
83 #include <Cockpit/panel_io.hxx>
85 #include <FDM/SP/ADA.hxx>
86 #include <FDM/SP/ACMS.hxx>
88 #include <FDM/Balloon.h>
89 #include <FDM/ExternalNet/ExternalNet.hxx>
90 #include <FDM/ExternalPipe/ExternalPipe.hxx>
91 #include <FDM/JSBSim/JSBSim.hxx>
92 #include <FDM/LaRCsim/LaRCsim.hxx>
93 #include <FDM/MagicCarpet.hxx>
94 #include <FDM/UFO.hxx>
95 #include <FDM/NullFDM.hxx>
96 #include <FDM/YASim/YASim.hxx>
97 #include <GUI/new_gui.hxx>
98 #include <Include/general.hxx>
99 #include <Input/input.hxx>
100 #include <Instrumentation/instrument_mgr.hxx>
101 #include <Model/acmodel.hxx>
102 #include <AIModel/submodel.hxx>
103 #include <AIModel/AIManager.hxx>
104 #include <Navaids/navdb.hxx>
105 #include <Navaids/navlist.hxx>
106 #include <Replay/replay.hxx>
107 #include <Scenery/scenery.hxx>
108 #include <Scenery/tilemgr.hxx>
109 #include <Scripting/NasalSys.hxx>
110 #include <Sound/fg_fx.hxx>
111 #include <Sound/beacon.hxx>
112 #include <Sound/morse.hxx>
113 #include <Systems/system_mgr.hxx>
114 #include <Time/light.hxx>
115 #include <Time/moonpos.hxx>
116 #include <Time/sunpos.hxx>
117 #include <Time/sunsolver.hxx>
118 #include <Time/tmp.hxx>
119 #include <Traffic/TrafficMgr.hxx>
122 #include <MultiPlayer/multiplaytxmgr.hxx>
123 #include <MultiPlayer/multiplayrxmgr.hxx>
126 #include <Environment/environment_mgr.hxx>
128 #include "fg_init.hxx"
130 #include "fg_commands.hxx"
131 #include "fg_props.hxx"
132 #include "options.hxx"
133 #include "globals.hxx"
134 #include "logger.hxx"
135 #include "viewmgr.hxx"
137 #if defined(FX) && defined(XMESA)
138 #include <GL/xmesa.h>
141 SG_USING_STD(string);
146 extern const char *default_root;
149 #ifdef FG_USE_CLOUDS_3D
150 SkySceneLoader *sgCloud3d;
154 // Scan the command line options for the specified option and return
156 static string fgScanForOption( const string& option, int argc, char **argv ) {
159 SG_LOG(SG_GENERAL, SG_INFO, "Scanning command line for: " << option );
161 int len = option.length();
164 SG_LOG( SG_GENERAL, SG_DEBUG, "argv[" << i << "] = " << argv[i] );
166 string arg = argv[i];
167 if ( arg.find( option ) == 0 ) {
168 return arg.substr( len );
178 // Scan the user config files for the specified option and return
180 static string fgScanForOption( const string& option, const string& path ) {
181 sg_gzifstream in( path );
182 if ( !in.is_open() ) {
186 SG_LOG( SG_GENERAL, SG_INFO, "Scanning " << path << " for: " << option );
188 int len = option.length();
192 while ( ! in.eof() ) {
195 while ( in.get(c) && c != '\0' ) {
200 #if defined( macintosh )
201 getline( in, line, '\r' );
203 getline( in, line, '\n' );
206 // catch extraneous (DOS) line ending character
207 if ( line[line.length() - 1] < 32 ) {
208 line = line.substr( 0, line.length()-1 );
211 if ( line.find( option ) == 0 ) {
212 return line.substr( len );
222 // Read in configuration (files and command line options) but only set
224 bool fgInitFGRoot ( int argc, char **argv ) {
228 // First parse command line options looking for --fg-root=, this
229 // will override anything specified in a config file
230 root = fgScanForOption( "--fg-root=", argc, argv);
232 #if defined( unix ) || defined( __CYGWIN__ )
233 // Next check home directory for .fgfsrc.hostname file
234 if ( root.empty() ) {
235 envp = ::getenv( "HOME" );
236 if ( envp != NULL ) {
237 SGPath config( envp );
238 config.append( ".fgfsrc" );
240 gethostname( name, 256 );
241 config.concat( "." );
242 config.concat( name );
243 root = fgScanForOption( "--fg-root=", config.str() );
248 // Next check home directory for .fgfsrc file
249 if ( root.empty() ) {
250 envp = ::getenv( "HOME" );
251 if ( envp != NULL ) {
252 SGPath config( envp );
253 config.append( ".fgfsrc" );
254 root = fgScanForOption( "--fg-root=", config.str() );
258 // Next check if fg-root is set as an env variable
259 if ( root.empty() ) {
260 envp = ::getenv( "FG_ROOT" );
261 if ( envp != NULL ) {
266 // Otherwise, default to a random compiled-in location if we can't
267 // find fg-root any other way.
268 if ( root.empty() ) {
269 #if defined( __CYGWIN__ )
270 root = "/FlightGear";
271 #elif defined( WIN32 )
272 root = "\\FlightGear";
273 #elif defined(OSX_BUNDLE)
274 /* the following code looks for the base package directly inside
275 the application bundle. This can be changed fairly easily by
276 fiddling with the code below. And yes, I know it's ugly and verbose.
278 CFBundleRef appBundle = CFBundleGetMainBundle();
279 CFURLRef appUrl = CFBundleCopyBundleURL(appBundle);
280 CFRelease(appBundle);
282 // look for a 'data' subdir directly inside the bundle : is there
283 // a better place? maybe in Resources? I don't know ...
284 CFURLRef dataDir = CFURLCreateCopyAppendingPathComponent(NULL, appUrl, CFSTR("data"), true);
286 // now convert down to a path, and the a c-string
287 CFStringRef path = CFURLCopyFileSystemPath(dataDir, kCFURLPOSIXPathStyle);
288 root = CFStringGetCStringPtr(path, CFStringGetSystemEncoding());
291 CFRelease(appBundle);
299 SG_LOG(SG_INPUT, SG_INFO, "fg_root = " << root );
300 globals->set_fg_root(root);
306 // Read in configuration (files and command line options) but only set
308 bool fgInitFGAircraft ( int argc, char **argv ) {
312 // First parse command line options looking for --aircraft=, this
313 // will override anything specified in a config file
314 aircraft = fgScanForOption( "--aircraft=", argc, argv );
316 #if defined( unix ) || defined( __CYGWIN__ )
317 // Next check home directory for .fgfsrc.hostname file
318 if ( aircraft.empty() ) {
319 envp = ::getenv( "HOME" );
320 if ( envp != NULL ) {
321 SGPath config( envp );
322 config.append( ".fgfsrc" );
324 gethostname( name, 256 );
325 config.concat( "." );
326 config.concat( name );
327 aircraft = fgScanForOption( "--aircraft=", config.str() );
332 // Next check home directory for .fgfsrc file
333 if ( aircraft.empty() ) {
334 envp = ::getenv( "HOME" );
335 if ( envp != NULL ) {
336 SGPath config( envp );
337 config.append( ".fgfsrc" );
338 aircraft = fgScanForOption( "--aircraft=", config.str() );
342 // if an aircraft was specified, set the property name
343 if ( !aircraft.empty() ) {
344 SG_LOG(SG_INPUT, SG_INFO, "aircraft = " << aircraft );
345 fgSetString("/sim/aircraft", aircraft.c_str() );
347 SG_LOG(SG_INPUT, SG_INFO, "No user specified aircraft, using default" );
354 // Return the current base package version
355 string fgBasePackageVersion() {
356 SGPath base_path( globals->get_fg_root() );
357 base_path.append("version");
359 sg_gzifstream in( base_path.str() );
360 if ( !in.is_open() ) {
361 SGPath old_path( globals->get_fg_root() );
362 old_path.append( "Thanks" );
363 sg_gzifstream old( old_path.str() );
364 if ( !old.is_open() ) {
367 return "[old version]";
378 // Initialize the localization
379 SGPropertyNode *fgInitLocale(const char *language) {
380 SGPropertyNode *c_node = NULL, *d_node = NULL;
381 SGPropertyNode *intl = fgGetNode("/sim/intl");
383 SG_LOG(SG_GENERAL, SG_INFO, "Selecting language: " << language );
385 // localization not defined
390 // Select the proper language from the list
392 vector<SGPropertyNode_ptr> locale = intl->getChildren("locale");
393 for (unsigned int i = 0; i < locale.size(); i++) {
395 vector<SGPropertyNode_ptr> lang = locale[i]->getChildren("lang");
396 for (unsigned int j = 0; j < lang.size(); j++) {
398 if (!strcmp(lang[j]->getStringValue(), language)) {
407 d_node = intl->getChild("locale");
411 // Check for localized font
412 SGPropertyNode *font_n = c_node->getNode("font", true);
413 if ( !strcmp(font_n->getStringValue(), "") )
414 font_n->setStringValue(d_node->getStringValue("font", "typewriter.txf"));
418 // Load the default strings
420 SGPath d_path( globals->get_fg_root() );
422 const char *d_path_str = d_node->getStringValue("strings");
424 SG_LOG(SG_GENERAL, SG_ALERT, "Incorrect path in configuration file.");
428 d_path.append(d_path_str);
429 SG_LOG(SG_GENERAL, SG_INFO, "Reading localized strings from "
432 SGPropertyNode *strings = c_node->getNode("strings");
434 readProperties(d_path.str(), strings);
435 } catch (const sg_exception &e) {
436 SG_LOG(SG_GENERAL, SG_ALERT, "Unable to read the localized strings");
441 // Load the language specific strings
443 if (c_node != d_node) {
444 SGPath c_path( globals->get_fg_root() );
446 const char *c_path_str = c_node->getStringValue("strings");
448 SG_LOG(SG_GENERAL, SG_ALERT, "Incorrect path in configuration file.");
452 c_path.append(c_path_str);
453 SG_LOG(SG_GENERAL, SG_INFO, "Reading localized strings from "
457 readProperties(c_path.str(), strings);
458 } catch (const sg_exception &e) {
459 SG_LOG(SG_GENERAL, SG_ALERT, "Unable to read the localized strings");
469 // Initialize the localization routines
470 bool fgDetectLanguage() {
471 char *language = ::getenv("LANG");
473 if (language == NULL) {
474 SG_LOG(SG_GENERAL, SG_INFO, "Unable to detect the language" );
478 SGPropertyNode *locale = fgInitLocale(language);
480 SG_LOG(SG_GENERAL, SG_ALERT,
481 "No internationalization settings specified in preferences.xml" );
486 globals->set_locale( locale );
491 // Attempt to locate and parse the various non-XML config files in order
492 // from least precidence to greatest precidence
494 do_options (int argc, char ** argv)
496 // Check for $fg_root/system.fgfsrc
497 SGPath config( globals->get_fg_root() );
498 config.append( "system.fgfsrc" );
499 fgParseOptions(config.str());
501 #if defined( unix ) || defined( __CYGWIN__ )
503 // Check for $fg_root/system.fgfsrc.hostname
504 gethostname( name, 256 );
505 config.concat( "." );
506 config.concat( name );
507 fgParseOptions(config.str());
510 // Check for ~/.fgfsrc
511 char* envp = ::getenv( "HOME" );
512 if ( envp != NULL ) {
514 config.append( ".fgfsrc" );
515 fgParseOptions(config.str());
518 #if defined( unix ) || defined( __CYGWIN__ )
519 // Check for ~/.fgfsrc.hostname
520 gethostname( name, 256 );
521 config.concat( "." );
522 config.concat( name );
523 fgParseOptions(config.str());
526 // Parse remaining command line options
527 // These will override anything specified in a config file
528 fgParseArgs(argc, argv);
532 static string fgFindAircraftPath( const SGPath &path, const string &aircraft ) {
534 ulDir *dirp = ulOpenDir(path.str().c_str());
536 cerr << "Unable to open aircraft directory." << endl;
540 while ((dire = ulReadDir(dirp)) != NULL) {
542 if ( strcmp("CVS", dire->d_name) && strcmp(".", dire->d_name)
543 && strcmp("..", dire->d_name) )
546 next.append(dire->d_name);
548 string result = fgFindAircraftPath( next, aircraft );
549 if ( ! result.empty() ) {
553 } else if ( !strcmp(dire->d_name, aircraft.c_str()) ) {
564 // Read in configuration (file and command line)
565 bool fgInitConfig ( int argc, char **argv ) {
567 // First, set some sane default values
570 // Read global preferences from $FG_ROOT/preferences.xml
571 SG_LOG(SG_INPUT, SG_INFO, "Reading global preferences");
572 fgLoadProps("preferences.xml", globals->get_props());
573 SG_LOG(SG_INPUT, SG_INFO, "Finished Reading global preferences");
575 // Detect the required language as early as possible
576 if ( !fgDetectLanguage() ) {
580 // Scan user config files and command line for a specified aircraft.
581 fgInitFGAircraft(argc, argv);
583 string aircraft = fgGetString( "/sim/aircraft", "" );
584 if ( aircraft.size() > 0 ) {
585 SGPath aircraft_search( globals->get_fg_root() );
586 aircraft_search.append( "Aircraft" );
588 string aircraft_set = aircraft + "-set.xml";
590 string result = fgFindAircraftPath( aircraft_search, aircraft_set );
591 if ( !result.empty() ) {
592 fgSetString( "/sim/aircraft-dir", result.c_str() );
593 SGPath full_name( result );
594 full_name.append( aircraft_set );
596 SG_LOG(SG_INPUT, SG_INFO, "Reading default aircraft: " << aircraft
597 << " from " << full_name.str());
599 readProperties( full_name.str(), globals->get_props() );
600 } catch ( const sg_exception &e ) {
601 string message = "Error reading default aircraft: ";
602 message += e.getFormattedMessage();
603 SG_LOG(SG_INPUT, SG_ALERT, message);
607 SG_LOG( SG_INPUT, SG_ALERT, "Cannot find specified aircraft: "
613 SG_LOG( SG_INPUT, SG_ALERT, "No default aircraft specified" );
616 // parse options after loading aircraft to ensure any user
617 // overrides of defaults are honored.
618 do_options(argc, argv);
624 // find basic airport location info from airport database
625 bool fgFindAirportID( const string& id, FGAirport *a ) {
628 SG_LOG( SG_GENERAL, SG_INFO, "Searching for airport code = " << id );
630 result = globals->get_airports()->search( id );
632 if ( result.id.empty() ) {
633 SG_LOG( SG_GENERAL, SG_ALERT,
634 "Failed to find " << id << " in basic.dat.gz" );
643 SG_LOG( SG_GENERAL, SG_INFO,
644 "Position for " << id << " is ("
645 << a->longitude << ", "
646 << a->latitude << ")" );
652 // get airport elevation
653 static double fgGetAirportElev( const string& id ) {
657 SG_LOG( SG_GENERAL, SG_INFO,
658 "Finding elevation for airport: " << id );
660 if ( fgFindAirportID( id, &a ) ) {
670 // This function is never used, but maybe useful in the future ???
673 // Preset lon/lat given an airport id
674 static bool fgSetPosFromAirportID( const string& id ) {
678 SG_LOG( SG_GENERAL, SG_INFO,
679 "Attempting to set starting position from airport code " << id );
681 if ( fgFindAirportID( id, &a ) ) {
683 fgSetDouble("/sim/presets/longitude-deg", a.longitude );
684 fgSetDouble("/sim/presets/latitude-deg", a.latitude );
686 // other code depends on the actual postition being set so set
688 fgSetDouble("/position/longitude-deg", a.longitude );
689 fgSetDouble("/position/latitude-deg", a.latitude );
691 SG_LOG( SG_GENERAL, SG_INFO,
692 "Position for " << id << " is (" << a.longitude
693 << ", " << a.latitude << ")" );
703 // Set current tower position lon/lat given an airport id
704 static bool fgSetTowerPosFromAirportID( const string& id, double hdg ) {
706 // tower height hard coded for now...
707 float towerheight=50.0f;
709 // make a little off the heading for 1 runway airports...
710 float fudge_lon = fabs(sin(hdg)) * .003f;
711 float fudge_lat = .003f - fudge_lon;
713 if ( fgFindAirportID( id, &a ) ) {
714 fgSetDouble("/sim/tower/longitude-deg", a.longitude + fudge_lon);
715 fgSetDouble("/sim/tower/latitude-deg", a.latitude + fudge_lat);
716 fgSetDouble("/sim/tower/altitude-ft", a.elevation + towerheight);
725 // Set current_options lon/lat given an airport id and heading (degrees)
726 static bool fgSetPosFromAirportIDandHdg( const string& id, double tgt_hdg ) {
730 // set initial position from runway and heading
732 SG_LOG( SG_GENERAL, SG_INFO,
733 "Attempting to set starting position from airport code "
734 << id << " heading " << tgt_hdg );
736 if ( ! globals->get_runways()->search( id, (int)tgt_hdg, &r ) ) {
737 SG_LOG( SG_GENERAL, SG_ALERT,
738 "Failed to find a good runway for " << id << '\n' );
745 double lat2, lon2, az2;
746 double heading = r.heading;
747 double azimuth = heading + 180.0;
748 while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
750 SG_LOG( SG_GENERAL, SG_INFO,
751 "runway = " << r.lon << ", " << r.lat
752 << " length = " << r.length * SG_FEET_TO_METER
753 << " heading = " << azimuth );
755 geo_direct_wgs_84 ( 0, r.lat, r.lon, azimuth,
756 r.length * SG_FEET_TO_METER * 0.5 - 5.0,
757 &lat2, &lon2, &az2 );
759 if ( fabs( fgGetDouble("/sim/presets/offset-distance") ) > SG_EPSILON ) {
761 double odist = fgGetDouble("/sim/presets/offset-distance");
762 odist *= SG_NM_TO_METER;
763 double oaz = azimuth;
764 if ( fabs(fgGetDouble("/sim/presets/offset-azimuth")) > SG_EPSILON ) {
765 oaz = fgGetDouble("/sim/presets/offset-azimuth") + 180;
768 while ( oaz >= 360.0 ) { oaz -= 360.0; }
769 geo_direct_wgs_84 ( 0, lat2, lon2, oaz, odist, &olat, &olon, &az2 );
775 fgSetDouble("/sim/presets/longitude-deg", lon2 );
776 fgSetDouble("/sim/presets/latitude-deg", lat2 );
777 fgSetDouble("/sim/presets/heading-deg", heading );
779 // other code depends on the actual values being set ...
780 fgSetDouble("/position/longitude-deg", lon2 );
781 fgSetDouble("/position/latitude-deg", lat2 );
782 fgSetDouble("/orientation/heading-deg", heading );
784 SG_LOG( SG_GENERAL, SG_INFO,
785 "Position for " << id << " is ("
787 << lat2 << ") new heading is "
794 // Set current_options lon/lat given an airport id and runway number
795 static bool fgSetPosFromAirportIDandRwy( const string& id, const string& rwy ) {
799 // set initial position from airport and runway number
801 SG_LOG( SG_GENERAL, SG_INFO,
802 "Attempting to set starting position for "
803 << id << ":" << rwy );
805 if ( ! globals->get_runways()->search( id, rwy, &r ) ) {
806 SG_LOG( SG_GENERAL, SG_ALERT,
807 "Failed to find runway " << rwy <<
808 " at airport " << id );
815 double lat2, lon2, az2;
816 double heading = r.heading;
817 double azimuth = heading + 180.0;
818 while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
820 SG_LOG( SG_GENERAL, SG_INFO,
821 "runway = " << r.lon << ", " << r.lat
822 << " length = " << r.length * SG_FEET_TO_METER
823 << " heading = " << azimuth );
825 geo_direct_wgs_84 ( 0, r.lat, r.lon,
827 r.length * SG_FEET_TO_METER * 0.5 - 5.0,
828 &lat2, &lon2, &az2 );
830 if ( fabs( fgGetDouble("/sim/presets/offset-distance") ) > SG_EPSILON )
833 double odist = fgGetDouble("/sim/presets/offset-distance");
834 odist *= SG_NM_TO_METER;
835 double oaz = azimuth;
836 if ( fabs(fgGetDouble("/sim/presets/offset-azimuth")) > SG_EPSILON )
838 oaz = fgGetDouble("/sim/presets/offset-azimuth") + 180;
839 heading = fgGetDouble("/sim/presets/heading-deg");
841 while ( oaz >= 360.0 ) { oaz -= 360.0; }
842 geo_direct_wgs_84 ( 0, lat2, lon2, oaz, odist, &olat, &olon, &az2 );
848 fgSetDouble("/sim/presets/longitude-deg", lon2 );
849 fgSetDouble("/sim/presets/latitude-deg", lat2 );
850 fgSetDouble("/sim/presets/heading-deg", heading );
852 // other code depends on the actual values being set ...
853 fgSetDouble("/position/longitude-deg", lon2 );
854 fgSetDouble("/position/latitude-deg", lat2 );
855 fgSetDouble("/orientation/heading-deg", heading );
857 SG_LOG( SG_GENERAL, SG_INFO,
858 "Position for " << id << " is ("
860 << lat2 << ") new heading is "
867 static void fgSetDistOrAltFromGlideSlope() {
868 // cout << "fgSetDistOrAltFromGlideSlope()" << endl;
869 string apt_id = fgGetString("/sim/presets/airport-id");
870 double gs = fgGetDouble("/sim/presets/glideslope-deg")
871 * SG_DEGREES_TO_RADIANS ;
872 double od = fgGetDouble("/sim/presets/offset-distance");
873 double alt = fgGetDouble("/sim/presets/altitude-ft");
875 double apt_elev = 0.0;
876 if ( ! apt_id.empty() ) {
877 apt_elev = fgGetAirportElev( apt_id );
878 if ( apt_elev < -9990.0 ) {
885 if( fabs(gs) > 0.01 && fabs(od) > 0.1 && alt < -9990 ) {
886 // set altitude from glideslope and offset-distance
887 od *= SG_NM_TO_METER * SG_METER_TO_FEET;
888 alt = fabs(od*tan(gs)) + apt_elev;
889 fgSetDouble("/sim/presets/altitude-ft", alt);
890 fgSetBool("/sim/presets/onground", false);
891 SG_LOG( SG_GENERAL, SG_INFO, "Calculated altitude as: "
893 } else if( fabs(gs) > 0.01 && alt > 0 && fabs(od) < 0.1) {
894 // set offset-distance from glideslope and altitude
895 od = (alt - apt_elev) / tan(gs);
896 od *= -1*SG_FEET_TO_METER * SG_METER_TO_NM;
897 fgSetDouble("/sim/presets/offset-distance", od);
898 fgSetBool("/sim/presets/onground", false);
899 SG_LOG( SG_GENERAL, SG_INFO, "Calculated offset distance as: "
901 } else if( fabs(gs) > 0.01 ) {
902 SG_LOG( SG_GENERAL, SG_ALERT,
903 "Glideslope given but not altitude or offset-distance." );
904 SG_LOG( SG_GENERAL, SG_ALERT, "Resetting glideslope to zero" );
905 fgSetDouble("/sim/presets/glideslope-deg", 0);
906 fgSetBool("/sim/presets/onground", true);
911 // Set current_options lon/lat given an airport id and heading (degrees)
912 static bool fgSetPosFromNAV( const string& id, const double& freq ) {
914 = globals->get_navlist()->findByIdentAndFreq( id.c_str(), freq );
916 // set initial position from runway and heading
918 SG_LOG( SG_GENERAL, SG_INFO, "Attempting to set starting position for "
919 << id << ":" << freq );
921 double lon = nav->get_lon();
922 double lat = nav->get_lat();
924 if ( fabs( fgGetDouble("/sim/presets/offset-distance") ) > SG_EPSILON )
926 double odist = fgGetDouble("/sim/presets/offset-distance")
928 double oaz = fabs(fgGetDouble("/sim/presets/offset-azimuth"))
930 while ( oaz >= 360.0 ) { oaz -= 360.0; }
931 double olat, olon, az2;
932 geo_direct_wgs_84 ( 0, lat, lon, oaz, odist, &olat, &olon, &az2 );
939 fgSetDouble("/sim/presets/longitude-deg", lon );
940 fgSetDouble("/sim/presets/latitude-deg", lat );
942 // other code depends on the actual values being set ...
943 fgSetDouble("/position/longitude-deg", lon );
944 fgSetDouble("/position/latitude-deg", lat );
945 fgSetDouble("/orientation/heading-deg",
946 fgGetDouble("/sim/presets/heading-deg") );
948 SG_LOG( SG_GENERAL, SG_INFO,
949 "Position for " << id << ":" << freq << " is ("
950 << lon << ", "<< lat << ")" );
954 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to locate NAV = "
955 << id << ":" << freq );
961 // Set current_options lon/lat given an airport id and heading (degrees)
962 static bool fgSetPosFromFix( const string& id ) {
965 // set initial position from runway and heading
966 if ( globals->get_fixlist()->query( id.c_str(), &fix ) ) {
967 SG_LOG( SG_GENERAL, SG_INFO, "Attempting to set starting position for "
970 double lon = fix.get_lon();
971 double lat = fix.get_lat();
973 if ( fabs( fgGetDouble("/sim/presets/offset-distance") ) > SG_EPSILON )
975 double odist = fgGetDouble("/sim/presets/offset-distance")
977 double oaz = fabs(fgGetDouble("/sim/presets/offset-azimuth"))
979 while ( oaz >= 360.0 ) { oaz -= 360.0; }
980 double olat, olon, az2;
981 geo_direct_wgs_84 ( 0, lat, lon, oaz, odist, &olat, &olon, &az2 );
988 fgSetDouble("/sim/presets/longitude-deg", lon );
989 fgSetDouble("/sim/presets/latitude-deg", lat );
991 // other code depends on the actual values being set ...
992 fgSetDouble("/position/longitude-deg", lon );
993 fgSetDouble("/position/latitude-deg", lat );
994 fgSetDouble("/orientation/heading-deg",
995 fgGetDouble("/sim/presets/heading-deg") );
997 SG_LOG( SG_GENERAL, SG_INFO,
998 "Position for " << id << " is ("
999 << lon << ", "<< lat << ")" );
1003 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to locate NAV = "
1009 static void parseWaypoints() {
1010 string_list *waypoints = globals->get_initial_waypoints();
1012 vector<string>::iterator i;
1013 for (i = waypoints->begin();
1014 i != waypoints->end();
1019 // Now were done using the way points we can deallocate the
1021 while (waypoints->begin() != waypoints->end()) {
1022 waypoints->pop_back();
1025 globals->set_initial_waypoints(0);
1034 * Initialize vor/ndb/ils/fix list management and query systems (as
1035 * well as simple airport db list)
1040 SG_LOG(SG_GENERAL, SG_INFO, "Loading Simple Airport List");
1041 SGPath p_simple( globals->get_fg_root() );
1042 p_simple.append( "Airports/basic.dat" );
1043 SGPath p_metar( globals->get_fg_root() );
1044 p_metar.append( "Airports/metar.dat" );
1045 FGAirportList *airports = new FGAirportList(p_simple.str(), p_metar.str());
1046 globals->set_airports( airports );
1048 SG_LOG(SG_GENERAL, SG_INFO, "Loading Runway List");
1049 SGPath p_runway( globals->get_fg_root() );
1050 p_runway.append( "Airports/runways.dat" );
1051 FGRunwayList *runways = new FGRunwayList( p_runway.str() );
1052 globals->set_runways( runways );
1054 FGNavList *navlist = new FGNavList;
1055 FGNavList *loclist = new FGNavList;
1056 FGNavList *gslist = new FGNavList;
1057 FGNavList *dmelist = new FGNavList;
1058 FGNavList *mkrlist = new FGNavList;
1060 globals->set_navlist( navlist );
1061 globals->set_loclist( loclist );
1062 globals->set_gslist( gslist );
1063 globals->set_dmelist( dmelist );
1064 globals->set_mkrlist( mkrlist );
1066 if ( !fgNavDBInit(airports, navlist, loclist, gslist, dmelist, mkrlist) ) {
1067 SG_LOG( SG_GENERAL, SG_ALERT,
1068 "Problems loading one or more navigational database" );
1071 if ( fgGetBool("/sim/navdb/localizers/auto-align", true) ) {
1072 // align all the localizers with their corresponding runways
1073 // since data sources are good for cockpit navigation
1074 // purposes, but not always to the error tolerances needed to
1075 // exactly place these items.
1077 = fgGetDouble( "/sim/navdb/localizers/auto-align-threshold-deg",
1079 fgNavDBAlignLOCwithRunway( runways, loclist, threshold );
1082 SG_LOG(SG_GENERAL, SG_INFO, " Fixes");
1083 SGPath p_fix( globals->get_fg_root() );
1084 p_fix.append( "Navaids/fix.dat" );
1085 FGFixList *fixlist = new FGFixList;
1086 fixlist->init( p_fix );
1087 globals->set_fixlist( fixlist );
1093 // Set the initial position based on presets (or defaults)
1094 bool fgInitPosition() {
1095 // cout << "fgInitPosition()" << endl;
1096 double gs = fgGetDouble("/sim/presets/glideslope-deg")
1097 * SG_DEGREES_TO_RADIANS ;
1098 double od = fgGetDouble("/sim/presets/offset-distance");
1099 double alt = fgGetDouble("/sim/presets/altitude-ft");
1101 bool set_pos = false;
1103 // If glideslope is specified, then calculate offset-distance or
1104 // altitude relative to glide slope if either of those was not
1106 if ( fabs( gs ) > 0.01 ) {
1107 fgSetDistOrAltFromGlideSlope();
1111 // If we have an explicit, in-range lon/lat, don't change it, just use it.
1112 // If not, check for an airport-id and use that.
1113 // If not, default to the middle of the KSFO field.
1114 // The default values for lon/lat are deliberately out of range
1115 // so that the airport-id can take effect; valid lon/lat will
1116 // override airport-id, however.
1117 double lon_deg = fgGetDouble("/sim/presets/longitude-deg");
1118 double lat_deg = fgGetDouble("/sim/presets/latitude-deg");
1119 if ( lon_deg >= -180.0 && lon_deg <= 180.0
1120 && lat_deg >= -90.0 && lat_deg <= 90.0 )
1125 string apt = fgGetString("/sim/presets/airport-id");
1126 string rwy_no = fgGetString("/sim/presets/runway");
1127 double hdg = fgGetDouble("/sim/presets/heading-deg");
1128 string vor = fgGetString("/sim/presets/vor-id");
1129 double vor_freq = fgGetDouble("/sim/presets/vor-freq");
1130 string ndb = fgGetString("/sim/presets/ndb-id");
1131 double ndb_freq = fgGetDouble("/sim/presets/ndb-freq");
1132 string fix = fgGetString("/sim/presets/fix");
1134 if ( !set_pos && !apt.empty() && !rwy_no.empty() ) {
1135 // An airport + runway is requested
1136 if ( fgSetPosFromAirportIDandRwy( apt, rwy_no ) ) {
1137 // set tower position (a little off the heading for single
1139 fgSetTowerPosFromAirportID( apt, hdg );
1144 if ( !set_pos && !apt.empty() ) {
1145 // An airport is requested (find runway closest to hdg)
1146 if ( fgSetPosFromAirportIDandHdg( apt, hdg ) ) {
1147 // set tower position (a little off the heading for single
1149 fgSetTowerPosFromAirportID( apt, hdg );
1154 if ( !set_pos && !vor.empty() ) {
1155 // a VOR is requested
1156 if ( fgSetPosFromNAV( vor, vor_freq ) ) {
1161 if ( !set_pos && !ndb.empty() ) {
1162 // an NDB is requested
1163 if ( fgSetPosFromNAV( ndb, ndb_freq ) ) {
1168 if ( !set_pos && !fix.empty() ) {
1169 // a Fix is requested
1170 if ( fgSetPosFromFix( fix ) ) {
1176 // No lon/lat specified, no airport specified, default to
1177 // middle of KSFO field.
1178 fgSetDouble("/sim/presets/longitude-deg", -122.374843);
1179 fgSetDouble("/sim/presets/latitude-deg", 37.619002);
1182 fgSetDouble( "/position/longitude-deg",
1183 fgGetDouble("/sim/presets/longitude-deg") );
1184 fgSetDouble( "/position/latitude-deg",
1185 fgGetDouble("/sim/presets/latitude-deg") );
1186 fgSetDouble( "/orientation/heading-deg",
1187 fgGetDouble("/sim/presets/heading-deg") );
1189 // determine if this should be an on-ground or in-air start
1190 if ( fabs(gs) > 0.01 || fabs(od) > 0.1 || alt > 0.1 ) {
1191 fgSetBool("/sim/presets/onground", false);
1193 fgSetBool("/sim/presets/onground", true);
1200 // General house keeping initializations
1201 bool fgInitGeneral() {
1204 #if defined(FX) && defined(XMESA)
1205 char *mesa_win_state;
1208 SG_LOG( SG_GENERAL, SG_INFO, "General Initialization" );
1209 SG_LOG( SG_GENERAL, SG_INFO, "======= ==============" );
1211 root = globals->get_fg_root();
1212 if ( ! root.length() ) {
1213 // No root path set? Then bail ...
1214 SG_LOG( SG_GENERAL, SG_ALERT,
1215 "Cannot continue without a path to the base package "
1216 << "being defined." );
1219 SG_LOG( SG_GENERAL, SG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
1221 #if defined(FX) && defined(XMESA)
1222 // initialize full screen flag
1223 globals->set_fullscreen(false);
1224 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
1225 // Test for the MESA_GLX_FX env variable
1226 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
1227 // test if we are fullscreen mesa/glide
1228 if ( (mesa_win_state[0] == 'f') ||
1229 (mesa_win_state[0] == 'F') ) {
1230 globals->set_fullscreen(true);
1240 // Initialize the flight model subsystem. This just creates the
1241 // object. The actual fdm initialization is delayed until we get a
1242 // proper scenery elevation hit. This is checked for in main.cxx
1246 if ( cur_fdm_state ) {
1247 delete cur_fdm_state;
1251 double dt = 1.0 / fgGetInt("/sim/model-hz");
1252 aircraft_dir = fgGetString("/sim/aircraft-dir");
1253 const string &model = fgGetString("/sim/flight-model");
1256 if ( model == "larcsim" ) {
1257 cur_fdm_state = new FGLaRCsim( dt );
1258 } else if ( model == "jsb" ) {
1259 cur_fdm_state = new FGJSBsim( dt );
1260 #ifdef ENABLE_SP_FMDS
1261 } else if ( model == "ada" ) {
1262 cur_fdm_state = new FGADA( dt );
1263 } else if ( model == "acms" ) {
1264 cur_fdm_state = new FGACMS( dt );
1266 } else if ( model == "balloon" ) {
1267 cur_fdm_state = new FGBalloonSim( dt );
1268 } else if ( model == "magic" ) {
1269 cur_fdm_state = new FGMagicCarpet( dt );
1270 } else if ( model == "ufo" ) {
1271 cur_fdm_state = new FGUFO( dt );
1272 } else if ( model == "external" ) {
1273 // external is a synonym for "--fdm=null" and is
1274 // maintained here for backwards compatibility
1275 cur_fdm_state = new FGNullFDM( dt );
1276 } else if ( model.find("network") == 0 ) {
1277 string host = "localhost";
1281 string net_options = model.substr(8);
1282 string::size_type begin, end;
1285 end = net_options.find( ",", begin );
1286 if ( end != string::npos ) {
1287 host = net_options.substr(begin, end - begin);
1291 end = net_options.find( ",", begin );
1292 if ( end != string::npos ) {
1293 port1 = atoi( net_options.substr(begin, end - begin).c_str() );
1297 end = net_options.find( ",", begin );
1298 if ( end != string::npos ) {
1299 port2 = atoi( net_options.substr(begin, end - begin).c_str() );
1303 end = net_options.find( ",", begin );
1304 if ( end != string::npos ) {
1305 port3 = atoi( net_options.substr(begin, end - begin).c_str() );
1308 cur_fdm_state = new FGExternalNet( dt, host, port1, port2, port3 );
1309 } else if ( model.find("pipe") == 0 ) {
1310 string pipe_path = model.substr(5);
1311 cur_fdm_state = new FGExternalPipe( dt, pipe_path );
1312 } else if ( model == "null" ) {
1313 cur_fdm_state = new FGNullFDM( dt );
1314 } else if ( model == "yasim" ) {
1315 cur_fdm_state = new YASim( dt );
1317 SG_LOG(SG_GENERAL, SG_ALERT,
1318 "Unrecognized flight model '" << model
1319 << "', cannot init flight dynamics model.");
1323 SG_LOG(SG_GENERAL, SG_ALERT, "FlightGear aborting\n\n");
1328 static void printMat(const sgVec4 *mat, char *name="")
1331 SG_LOG(SG_GENERAL, SG_BULK, name );
1332 for(i=0; i<4; i++) {
1333 SG_LOG(SG_GENERAL, SG_BULK, " " << mat[i][0] << " " << mat[i][1]
1334 << " " << mat[i][2] << " " << mat[i][3] );
1338 // Initialize view parameters
1340 // force update of model so that viewer can get some data...
1341 globals->get_aircraft_model()->update(0);
1342 // run update for current view so that data is current...
1343 globals->get_viewmgr()->update(0);
1345 printMat(globals->get_current_view()->get_VIEW(),"VIEW");
1346 printMat(globals->get_current_view()->get_UP(),"UP");
1347 // printMat(globals->get_current_view()->get_LOCAL(),"LOCAL");
1352 SGTime *fgInitTime() {
1354 static const SGPropertyNode *longitude
1355 = fgGetNode("/position/longitude-deg");
1356 static const SGPropertyNode *latitude
1357 = fgGetNode("/position/latitude-deg");
1358 static const SGPropertyNode *cur_time_override
1359 = fgGetNode("/sim/time/cur-time-override", true);
1361 SGPath zone( globals->get_fg_root() );
1362 zone.append( "Timezone" );
1363 SGTime *t = new SGTime( longitude->getDoubleValue()
1364 * SGD_DEGREES_TO_RADIANS,
1365 latitude->getDoubleValue()
1366 * SGD_DEGREES_TO_RADIANS,
1368 cur_time_override->getLongValue() );
1370 globals->set_warp_delta( 0 );
1372 t->update( 0.0, 0.0,
1373 cur_time_override->getLongValue(),
1374 globals->get_warp() );
1380 // set up a time offset (aka warp) if one is specified
1381 void fgInitTimeOffset() {
1382 static const SGPropertyNode *longitude
1383 = fgGetNode("/position/longitude-deg");
1384 static const SGPropertyNode *latitude
1385 = fgGetNode("/position/latitude-deg");
1386 static const SGPropertyNode *cur_time_override
1387 = fgGetNode("/sim/time/cur-time-override", true);
1389 // Handle potential user specified time offsets
1390 int orig_warp = globals->get_warp();
1391 SGTime *t = globals->get_time_params();
1392 time_t cur_time = t->get_cur_time();
1393 time_t currGMT = sgTimeGetGMT( gmtime(&cur_time) );
1394 time_t systemLocalTime = sgTimeGetGMT( localtime(&cur_time) );
1395 time_t aircraftLocalTime =
1396 sgTimeGetGMT( fgLocaltime(&cur_time, t->get_zonename() ) );
1398 // Okay, we now have several possible scenarios
1399 int offset = fgGetInt("/sim/startup/time-offset");
1400 const string &offset_type = fgGetString("/sim/startup/time-offset-type");
1403 if ( offset_type == "real" ) {
1405 } else if ( offset_type == "dawn" ) {
1406 warp = fgTimeSecondsUntilSunAngle( cur_time,
1407 longitude->getDoubleValue()
1408 * SGD_DEGREES_TO_RADIANS,
1409 latitude->getDoubleValue()
1410 * SGD_DEGREES_TO_RADIANS,
1412 } else if ( offset_type == "morning" ) {
1413 warp = fgTimeSecondsUntilSunAngle( cur_time,
1414 longitude->getDoubleValue()
1415 * SGD_DEGREES_TO_RADIANS,
1416 latitude->getDoubleValue()
1417 * SGD_DEGREES_TO_RADIANS,
1419 } else if ( offset_type == "noon" ) {
1420 warp = fgTimeSecondsUntilSunAngle( cur_time,
1421 longitude->getDoubleValue()
1422 * SGD_DEGREES_TO_RADIANS,
1423 latitude->getDoubleValue()
1424 * SGD_DEGREES_TO_RADIANS,
1426 } else if ( offset_type == "afternoon" ) {
1427 warp = fgTimeSecondsUntilSunAngle( cur_time,
1428 longitude->getDoubleValue()
1429 * SGD_DEGREES_TO_RADIANS,
1430 latitude->getDoubleValue()
1431 * SGD_DEGREES_TO_RADIANS,
1433 } else if ( offset_type == "dusk" ) {
1434 warp = fgTimeSecondsUntilSunAngle( cur_time,
1435 longitude->getDoubleValue()
1436 * SGD_DEGREES_TO_RADIANS,
1437 latitude->getDoubleValue()
1438 * SGD_DEGREES_TO_RADIANS,
1440 } else if ( offset_type == "evening" ) {
1441 warp = fgTimeSecondsUntilSunAngle( cur_time,
1442 longitude->getDoubleValue()
1443 * SGD_DEGREES_TO_RADIANS,
1444 latitude->getDoubleValue()
1445 * SGD_DEGREES_TO_RADIANS,
1447 } else if ( offset_type == "midnight" ) {
1448 warp = fgTimeSecondsUntilSunAngle( cur_time,
1449 longitude->getDoubleValue()
1450 * SGD_DEGREES_TO_RADIANS,
1451 latitude->getDoubleValue()
1452 * SGD_DEGREES_TO_RADIANS,
1454 } else if ( offset_type == "system-offset" ) {
1456 } else if ( offset_type == "gmt-offset" ) {
1457 warp = offset - (currGMT - systemLocalTime);
1458 } else if ( offset_type == "latitude-offset" ) {
1459 warp = offset - (aircraftLocalTime - systemLocalTime);
1460 } else if ( offset_type == "system" ) {
1461 warp = offset - cur_time;
1462 } else if ( offset_type == "gmt" ) {
1463 warp = offset - currGMT;
1464 } else if ( offset_type == "latitude" ) {
1465 warp = offset - (aircraftLocalTime - systemLocalTime) - cur_time;
1467 SG_LOG( SG_GENERAL, SG_ALERT,
1468 "FG_TIME::Unsupported offset type " << offset_type );
1471 globals->set_warp( orig_warp + warp );
1472 t->update( longitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
1473 latitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
1474 cur_time_override->getLongValue(),
1475 globals->get_warp() );
1477 SG_LOG( SG_GENERAL, SG_INFO, "After fgInitTimeOffset(): warp = "
1478 << globals->get_warp() );
1482 // This is the top level init routine which calls all the other
1483 // initialization routines. If you are adding a subsystem to flight
1484 // gear, its initialization call should located in this routine.
1485 // Returns non-zero if a problem encountered.
1486 bool fgInitSubsystems() {
1487 // static const SGPropertyNode *longitude
1488 // = fgGetNode("/sim/presets/longitude-deg");
1489 // static const SGPropertyNode *latitude
1490 // = fgGetNode("/sim/presets/latitude-deg");
1491 // static const SGPropertyNode *altitude
1492 // = fgGetNode("/sim/presets/altitude-ft");
1494 SG_LOG( SG_GENERAL, SG_INFO, "Initialize Subsystems");
1495 SG_LOG( SG_GENERAL, SG_INFO, "========== ==========");
1497 ////////////////////////////////////////////////////////////////////
1498 // Initialize the event manager subsystem.
1499 ////////////////////////////////////////////////////////////////////
1501 globals->get_event_mgr()->init();
1502 globals->get_event_mgr()->setFreezeProperty(fgGetNode("/sim/freeze/clock"));
1504 ////////////////////////////////////////////////////////////////////
1505 // Initialize the property interpolator subsystem
1506 ////////////////////////////////////////////////////////////////////
1507 globals->add_subsystem("interpolator", new SGInterpolator);
1510 ////////////////////////////////////////////////////////////////////
1511 // Add the FlightGear property utilities.
1512 ////////////////////////////////////////////////////////////////////
1513 globals->add_subsystem("properties", new FGProperties);
1515 ////////////////////////////////////////////////////////////////////
1516 // Initialize the material property subsystem.
1517 ////////////////////////////////////////////////////////////////////
1519 SGPath mpath( globals->get_fg_root() );
1520 mpath.append( "materials.xml" );
1521 if ( ! globals->get_matlib()->load(globals->get_fg_root(), mpath.str()) ) {
1522 SG_LOG( SG_GENERAL, SG_ALERT, "Error loading material lib!" );
1527 ////////////////////////////////////////////////////////////////////
1528 // Initialize the scenery management subsystem.
1529 ////////////////////////////////////////////////////////////////////
1531 if ( globals->get_tile_mgr()->init() ) {
1532 // Load the local scenery data
1533 double visibility_meters = fgGetDouble("/environment/visibility-m");
1535 globals->get_tile_mgr()->update( visibility_meters );
1537 SG_LOG( SG_GENERAL, SG_ALERT, "Error in Tile Manager initialization!" );
1541 // cause refresh of viewer scenery timestamps every 15 seconds...
1542 globals->get_event_mgr()->addTask( "FGTileMgr::refresh_view_timestamps()",
1543 globals->get_tile_mgr(),
1544 &FGTileMgr::refresh_view_timestamps,
1547 SG_LOG( SG_GENERAL, SG_DEBUG,
1548 "Current terrain elevation after tile mgr init " <<
1549 globals->get_scenery()->get_cur_elev() );
1552 ////////////////////////////////////////////////////////////////////
1553 // Initialize the flight model subsystem.
1554 ////////////////////////////////////////////////////////////////////
1558 // allocates structures so must happen before any of the flight
1559 // model or control parameters are set
1560 fgAircraftInit(); // In the future this might not be the case.
1563 ////////////////////////////////////////////////////////////////////
1564 // Initialize the XML Autopilot subsystem.
1565 ////////////////////////////////////////////////////////////////////
1567 globals->add_subsystem( "xml-autopilot", new FGXMLAutopilot );
1568 globals->add_subsystem( "route-manager", new FGRouteMgr );
1571 ////////////////////////////////////////////////////////////////////
1572 // Initialize the view manager subsystem.
1573 ////////////////////////////////////////////////////////////////////
1577 ////////////////////////////////////////////////////////////////////
1578 // Create and register the logger.
1579 ////////////////////////////////////////////////////////////////////
1581 globals->add_subsystem("logger", new FGLogger);
1583 ////////////////////////////////////////////////////////////////////
1584 // Create and register the XML GUI.
1585 ////////////////////////////////////////////////////////////////////
1587 globals->add_subsystem("gui", new NewGUI, SGSubsystemMgr::INIT);
1590 ////////////////////////////////////////////////////////////////////
1591 // Initialize the local time subsystem.
1592 ////////////////////////////////////////////////////////////////////
1594 // update the current timezone each 30 minutes
1595 globals->get_event_mgr()->addTask( "fgUpdateLocalTime()",
1596 &fgUpdateLocalTime, 30*60 );
1599 ////////////////////////////////////////////////////////////////////
1600 // Initialize the weather subsystem.
1601 ////////////////////////////////////////////////////////////////////
1603 // Initialize the weather modeling subsystem
1604 globals->add_subsystem("environment", new FGEnvironmentMgr);
1607 ////////////////////////////////////////////////////////////////////
1608 // Initialize the lighting subsystem.
1609 ////////////////////////////////////////////////////////////////////
1611 globals->add_subsystem("lighting", new FGLight);
1613 //////////////////////////////////////////////////////////////////////
1614 // Initialize the 2D cloud subsystem.
1615 ////////////////////////////////////////////////////////////////////
1616 fgGetBool("/sim/rendering/bump-mapping", false);
1618 #ifdef FG_USE_CLOUDS_3D
1619 ////////////////////////////////////////////////////////////////////
1620 // Initialize the 3D cloud subsystem.
1621 ////////////////////////////////////////////////////////////////////
1622 if ( fgGetBool("/sim/rendering/clouds3d") ) {
1623 static const SGPropertyNode *longitude
1624 = fgGetNode("/sim/presets/longitude-deg");
1625 static const SGPropertyNode *latitude
1626 = fgGetNode("/sim/presets/latitude-deg");
1627 // static const SGPropertyNode *altitude
1628 // = fgGetNode("/sim/presets/altitude-ft");
1630 SGPath cloud_path(globals->get_fg_root());
1632 cloud_path.append("Textures/Sky/scattered.rgba");
1633 SG_LOG(SG_GENERAL, SG_INFO, "Loading CLOUDS3d from: " << cloud_path.c_str());
1636 tx.read_rgba_texture(cloud_path.c_str());
1637 if ( !sgCloud3d->Load( tx.texture(), tx.width(),
1639 cloud_path.append("large.sky");
1640 if ( !sgCloud3d->Load( cloud_path.str(),
1642 latitude->getDoubleValue(),
1643 longitude->getDoubleValue()) )
1645 fgSetBool("/sim/rendering/clouds3d", false);
1646 SG_LOG(SG_GENERAL, SG_INFO, "CLOUDS3d FAILED: ");
1648 SG_LOG(SG_GENERAL, SG_INFO, "CLOUDS3d Loaded: ");
1652 #ifdef ENABLE_AUDIO_SUPPORT
1653 ////////////////////////////////////////////////////////////////////
1654 // Initialize the sound subsystem.
1655 ////////////////////////////////////////////////////////////////////
1657 globals->set_soundmgr(new SGSoundMgr);
1658 globals->get_soundmgr()->init();
1659 globals->get_soundmgr()->bind();
1662 ////////////////////////////////////////////////////////////////////
1663 // Initialize the sound-effects subsystem.
1664 ////////////////////////////////////////////////////////////////////
1666 globals->add_subsystem("fx", new FGFX);
1670 ////////////////////////////////////////////////////////////////////
1671 // Initialise ATC display system
1672 ////////////////////////////////////////////////////////////////////
1674 SG_LOG(SG_GENERAL, SG_INFO, " ATC Display");
1675 globals->set_ATC_display(new FGATCDisplay);
1676 globals->get_ATC_display()->init();
1678 ////////////////////////////////////////////////////////////////////
1679 // Initialise the ATC Manager
1680 ////////////////////////////////////////////////////////////////////
1682 SG_LOG(SG_GENERAL, SG_INFO, " ATC Manager");
1683 globals->set_ATC_mgr(new FGATCMgr);
1684 globals->get_ATC_mgr()->init();
1686 ////////////////////////////////////////////////////////////////////
1687 // Initialise the AI Manager
1688 ////////////////////////////////////////////////////////////////////
1690 SG_LOG(SG_GENERAL, SG_INFO, " AI Manager");
1691 globals->set_AI_mgr(new FGAIMgr);
1692 globals->get_AI_mgr()->init();
1694 ////////////////////////////////////////////////////////////////////
1695 // Initialise the AI Model Manager
1696 ////////////////////////////////////////////////////////////////////
1697 SG_LOG(SG_GENERAL, SG_INFO, " AI Model Manager");
1698 globals->add_subsystem("ai_model", new FGAIManager);
1699 globals->add_subsystem("submodel_mgr", new FGSubmodelMgr);
1702 // It's probably a good idea to initialize the top level traffic manager
1703 // After the AI and ATC systems have been initialized properly.
1704 // AI Traffic manager
1705 globals->add_subsystem("Traffic Manager", new FGTrafficManager);
1706 FGTrafficManager *dispatcher =
1707 (FGTrafficManager*) globals->get_subsystem("Traffic Manager");
1709 readXML(string(globals->get_fg_root() + string("/Traffic/fgtraffic.xml")),
1711 globals->get_subsystem("Traffic Manager")->init();
1713 globals->add_subsystem("instrumentation", new FGInstrumentMgr);
1714 globals->add_subsystem("systems", new FGSystemMgr);
1718 ////////////////////////////////////////////////////////////////////
1719 // Initialize the cockpit subsystem
1720 ////////////////////////////////////////////////////////////////////
1721 if( fgCockpitInit( ¤t_aircraft )) {
1722 // Cockpit initialized ok.
1724 SG_LOG( SG_GENERAL, SG_ALERT, "Error in Cockpit initialization!" );
1729 ////////////////////////////////////////////////////////////////////
1730 // Initialize the autopilot subsystem.
1731 ////////////////////////////////////////////////////////////////////
1733 // FIXME: these should go in the
1734 // GUI initialization code, not here.
1735 // fgAPAdjustInit();
1736 NewTgtAirportInit();
1740 ////////////////////////////////////////////////////////////////////
1741 // Initialize I/O subsystem.
1742 ////////////////////////////////////////////////////////////////////
1744 globals->get_io()->init();
1745 globals->get_io()->bind();
1748 ////////////////////////////////////////////////////////////////////
1749 // Add a new 2D panel.
1750 ////////////////////////////////////////////////////////////////////
1752 string panel_path = fgGetString("/sim/panel/path",
1753 "Panels/Default/default.xml");
1755 globals->set_current_panel( fgReadPanel(panel_path) );
1756 if (globals->get_current_panel() == 0) {
1757 SG_LOG( SG_INPUT, SG_ALERT,
1758 "Error reading new panel from " << panel_path );
1760 SG_LOG( SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path );
1761 globals->get_current_panel()->init();
1762 globals->get_current_panel()->bind();
1766 ////////////////////////////////////////////////////////////////////
1767 // Initialize the controls subsystem.
1768 ////////////////////////////////////////////////////////////////////
1770 globals->get_controls()->init();
1771 globals->get_controls()->bind();
1774 ////////////////////////////////////////////////////////////////////
1775 // Initialize the input subsystem.
1776 ////////////////////////////////////////////////////////////////////
1778 globals->add_subsystem("input", new FGInput);
1781 ////////////////////////////////////////////////////////////////////
1782 // Initialize the replay subsystem
1783 ////////////////////////////////////////////////////////////////////
1784 globals->add_subsystem("replay", new FGReplay);
1786 ////////////////////////////////////////////////////////////////////
1787 // Bind and initialize subsystems.
1788 ////////////////////////////////////////////////////////////////////
1790 globals->get_subsystem_mgr()->bind();
1791 globals->get_subsystem_mgr()->init();
1793 #ifdef FG_MPLAYER_AS
1794 ////////////////////////////////////////////////////////////////////
1795 // Initialize multiplayer subsystem
1796 ////////////////////////////////////////////////////////////////////
1798 globals->set_multiplayer_tx_mgr(new FGMultiplayTxMgr);
1799 globals->get_multiplayer_tx_mgr()->init();
1801 globals->set_multiplayer_rx_mgr(new FGMultiplayRxMgr);
1802 globals->get_multiplayer_rx_mgr()->init();
1805 ////////////////////////////////////////////////////////////////////////
1806 // Initialize the Nasal interpreter.
1807 // Do this last, so that the loaded scripts see initialized state
1808 ////////////////////////////////////////////////////////////////////////
1809 FGNasalSys* nasal = new FGNasalSys();
1810 globals->add_subsystem("nasal", nasal);
1813 ////////////////////////////////////////////////////////////////////
1814 // At this point we could try and parse the waypoint options
1815 ///////////////////////////////////////////////////////////////////
1819 ////////////////////////////////////////////////////////////////////////
1820 // End of subsystem initialization.
1821 ////////////////////////////////////////////////////////////////////
1823 SG_LOG( SG_GENERAL, SG_INFO, endl);
1825 // Save the initial state for future
1827 globals->saveInitialState();
1833 void fgReInitSubsystems()
1835 // static const SGPropertyNode *longitude
1836 // = fgGetNode("/sim/presets/longitude-deg");
1837 // static const SGPropertyNode *latitude
1838 // = fgGetNode("/sim/presets/latitude-deg");
1839 static const SGPropertyNode *altitude
1840 = fgGetNode("/sim/presets/altitude-ft");
1841 static const SGPropertyNode *master_freeze
1842 = fgGetNode("/sim/freeze/master");
1844 SG_LOG( SG_GENERAL, SG_INFO,
1845 "fgReInitSubsystems(): /position/altitude = "
1846 << altitude->getDoubleValue() );
1848 bool freeze = master_freeze->getBoolValue();
1850 fgSetBool("/sim/freeze/master", true);
1852 fgSetBool("/sim/crashed", false);
1853 fgSetBool("/sim/initialised", false);
1855 // Initialize the FDM
1858 // allocates structures so must happen before any of the flight
1859 // model or control parameters are set
1860 fgAircraftInit(); // In the future this might not be the case.
1862 // reload offsets from config defaults
1863 globals->get_viewmgr()->reinit();
1867 globals->get_controls()->reset_all();
1869 fgUpdateLocalTime();
1871 // re-init to proper time of day setting
1875 fgSetBool("/sim/freeze/master", false);
1877 fgSetBool("/sim/initialised", true);
1878 fgSetBool("/sim/sceneryloaded",false);