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 #include <simgear/timing/sg_time.hxx>
64 #include <simgear/timing/lowleveltime.h>
66 #include <Aircraft/aircraft.hxx>
67 #include <Airports/apt_loader.hxx>
68 #include <Airports/runways.hxx>
69 #include <Airports/simple.hxx>
70 #include <AIModel/AIManager.hxx>
71 #include <ATC/ATCdisplay.hxx>
72 #include <ATC/ATCmgr.hxx>
73 #include <ATC/AIMgr.hxx>
74 #include <Autopilot/auto_gui.hxx>
75 #include <Autopilot/route_mgr.hxx>
76 #include <Autopilot/xmlauto.hxx>
77 #include <Cockpit/cockpit.hxx>
78 #include <Cockpit/panel.hxx>
79 #include <Cockpit/panel_io.hxx>
81 #include <FDM/SP/ADA.hxx>
82 #include <FDM/SP/ACMS.hxx>
84 #include <FDM/Balloon.h>
85 #include <FDM/ExternalNet/ExternalNet.hxx>
86 #include <FDM/ExternalPipe/ExternalPipe.hxx>
87 #include <FDM/JSBSim/JSBSim.hxx>
88 #include <FDM/LaRCsim/LaRCsim.hxx>
89 #include <FDM/MagicCarpet.hxx>
90 #include <FDM/UFO.hxx>
91 #include <FDM/NullFDM.hxx>
92 #include <FDM/YASim/YASim.hxx>
93 #include <GUI/new_gui.hxx>
94 #include <Include/general.hxx>
95 #include <Input/input.hxx>
96 #include <Instrumentation/instrument_mgr.hxx>
97 #include <Model/acmodel.hxx>
98 #include <AIModel/submodel.hxx>
99 #include <AIModel/AIManager.hxx>
100 #include <Navaids/navdb.hxx>
101 #include <Navaids/navlist.hxx>
102 #include <Replay/replay.hxx>
103 #include <Scenery/scenery.hxx>
104 #include <Scenery/tilemgr.hxx>
105 #include <Scripting/NasalSys.hxx>
106 #include <Sound/fg_fx.hxx>
107 #include <Sound/beacon.hxx>
108 #include <Sound/morse.hxx>
109 #include <Systems/system_mgr.hxx>
110 #include <Time/light.hxx>
111 #include <Time/sunpos.hxx>
112 #include <Time/sunsolver.hxx>
113 #include <Time/tmp.hxx>
114 #include <Traffic/TrafficMgr.hxx>
117 #include <MultiPlayer/multiplaytxmgr.hxx>
118 #include <MultiPlayer/multiplayrxmgr.hxx>
121 #include <Environment/environment_mgr.hxx>
123 #include "fg_init.hxx"
125 #include "fg_commands.hxx"
126 #include "fg_props.hxx"
127 #include "options.hxx"
128 #include "globals.hxx"
129 #include "logger.hxx"
130 #include "viewmgr.hxx"
132 #if defined(FX) && defined(XMESA)
133 #include <GL/xmesa.h>
136 SG_USING_STD(string);
140 extern const char *default_root;
144 // Scan the command line options for the specified option and return
146 static string fgScanForOption( const string& option, int argc, char **argv ) {
149 SG_LOG(SG_GENERAL, SG_INFO, "Scanning command line for: " << option );
151 int len = option.length();
154 SG_LOG( SG_GENERAL, SG_DEBUG, "argv[" << i << "] = " << argv[i] );
156 string arg = argv[i];
157 if ( arg.find( option ) == 0 ) {
158 return arg.substr( len );
168 // Scan the user config files for the specified option and return
170 static string fgScanForOption( const string& option, const string& path ) {
171 sg_gzifstream in( path );
172 if ( !in.is_open() ) {
176 SG_LOG( SG_GENERAL, SG_INFO, "Scanning " << path << " for: " << option );
178 int len = option.length();
182 while ( ! in.eof() ) {
185 while ( in.get(c) && c != '\0' ) {
190 #if defined( macintosh )
191 getline( in, line, '\r' );
193 getline( in, line, '\n' );
196 // catch extraneous (DOS) line ending character
197 if ( line[line.length() - 1] < 32 ) {
198 line = line.substr( 0, line.length()-1 );
201 if ( line.find( option ) == 0 ) {
202 return line.substr( len );
212 // Read in configuration (files and command line options) but only set
214 bool fgInitFGRoot ( int argc, char **argv ) {
218 // First parse command line options looking for --fg-root=, this
219 // will override anything specified in a config file
220 root = fgScanForOption( "--fg-root=", argc, argv);
222 #if defined( unix ) || defined( __CYGWIN__ )
223 // Next check home directory for .fgfsrc.hostname file
224 if ( root.empty() ) {
225 envp = ::getenv( "HOME" );
226 if ( envp != NULL ) {
227 SGPath config( envp );
228 config.append( ".fgfsrc" );
230 gethostname( name, 256 );
231 config.concat( "." );
232 config.concat( name );
233 root = fgScanForOption( "--fg-root=", config.str() );
238 // Next check home directory for .fgfsrc file
239 if ( root.empty() ) {
240 envp = ::getenv( "HOME" );
241 if ( envp != NULL ) {
242 SGPath config( envp );
243 config.append( ".fgfsrc" );
244 root = fgScanForOption( "--fg-root=", config.str() );
248 // Next check if fg-root is set as an env variable
249 if ( root.empty() ) {
250 envp = ::getenv( "FG_ROOT" );
251 if ( envp != NULL ) {
256 // Otherwise, default to a random compiled-in location if we can't
257 // find fg-root any other way.
258 if ( root.empty() ) {
259 #if defined( __CYGWIN__ )
260 root = "/FlightGear";
261 #elif defined( WIN32 )
262 root = "\\FlightGear";
263 #elif defined(OSX_BUNDLE)
264 /* the following code looks for the base package directly inside
265 the application bundle. This can be changed fairly easily by
266 fiddling with the code below. And yes, I know it's ugly and verbose.
268 CFBundleRef appBundle = CFBundleGetMainBundle();
269 CFURLRef appUrl = CFBundleCopyBundleURL(appBundle);
270 CFRelease(appBundle);
272 // look for a 'data' subdir directly inside the bundle : is there
273 // a better place? maybe in Resources? I don't know ...
274 CFURLRef dataDir = CFURLCreateCopyAppendingPathComponent(NULL, appUrl, CFSTR("data"), true);
276 // now convert down to a path, and the a c-string
277 CFStringRef path = CFURLCopyFileSystemPath(dataDir, kCFURLPOSIXPathStyle);
278 root = CFStringGetCStringPtr(path, CFStringGetSystemEncoding());
281 CFRelease(appBundle);
289 SG_LOG(SG_INPUT, SG_INFO, "fg_root = " << root );
290 globals->set_fg_root(root);
296 // Read in configuration (files and command line options) but only set
298 bool fgInitFGAircraft ( int argc, char **argv ) {
302 // First parse command line options looking for --aircraft=, this
303 // will override anything specified in a config file
304 aircraft = fgScanForOption( "--aircraft=", argc, argv );
306 #if defined( unix ) || defined( __CYGWIN__ )
307 // Next check home directory for .fgfsrc.hostname file
308 if ( aircraft.empty() ) {
309 envp = ::getenv( "HOME" );
310 if ( envp != NULL ) {
311 SGPath config( envp );
312 config.append( ".fgfsrc" );
314 gethostname( name, 256 );
315 config.concat( "." );
316 config.concat( name );
317 aircraft = fgScanForOption( "--aircraft=", config.str() );
322 // Next check home directory for .fgfsrc file
323 if ( aircraft.empty() ) {
324 envp = ::getenv( "HOME" );
325 if ( envp != NULL ) {
326 SGPath config( envp );
327 config.append( ".fgfsrc" );
328 aircraft = fgScanForOption( "--aircraft=", config.str() );
332 if ( aircraft.empty() ) {
333 // Check for $fg_root/system.fgfsrc
334 SGPath sysconf( globals->get_fg_root() );
335 sysconf.append( "system.fgfsrc" );
336 aircraft = fgScanForOption( "--aircraft=", sysconf.str() );
338 // if an aircraft was specified, set the property name
339 if ( !aircraft.empty() ) {
340 SG_LOG(SG_INPUT, SG_INFO, "aircraft = " << aircraft );
341 fgSetString("/sim/aircraft", aircraft.c_str() );
343 SG_LOG(SG_INPUT, SG_INFO, "No user specified aircraft, using default" );
350 // Return the current base package version
351 string fgBasePackageVersion() {
352 SGPath base_path( globals->get_fg_root() );
353 base_path.append("version");
355 sg_gzifstream in( base_path.str() );
356 if ( !in.is_open() ) {
357 SGPath old_path( globals->get_fg_root() );
358 old_path.append( "Thanks" );
359 sg_gzifstream old( old_path.str() );
360 if ( !old.is_open() ) {
363 return "[old version]";
374 // Initialize the localization
375 SGPropertyNode *fgInitLocale(const char *language) {
376 SGPropertyNode *c_node = NULL, *d_node = NULL;
377 SGPropertyNode *intl = fgGetNode("/sim/intl");
379 SG_LOG(SG_GENERAL, SG_INFO, "Selecting language: " << language );
381 // localization not defined
386 // Select the proper language from the list
388 vector<SGPropertyNode_ptr> locale = intl->getChildren("locale");
389 for (unsigned int i = 0; i < locale.size(); i++) {
391 vector<SGPropertyNode_ptr> lang = locale[i]->getChildren("lang");
392 for (unsigned int j = 0; j < lang.size(); j++) {
394 if (!strcmp(lang[j]->getStringValue(), language)) {
403 d_node = intl->getChild("locale");
407 // Check for localized font
408 SGPropertyNode *font_n = c_node->getNode("font", true);
409 if ( !strcmp(font_n->getStringValue(), "") )
410 font_n->setStringValue(d_node->getStringValue("font", "typewriter.txf"));
414 // Load the default strings
416 SGPath d_path( globals->get_fg_root() );
418 const char *d_path_str = d_node->getStringValue("strings");
420 SG_LOG(SG_GENERAL, SG_ALERT, "Incorrect path in configuration file.");
424 d_path.append(d_path_str);
425 SG_LOG(SG_GENERAL, SG_INFO, "Reading localized strings from "
428 SGPropertyNode *strings = c_node->getNode("strings");
430 readProperties(d_path.str(), strings);
431 } catch (const sg_exception &e) {
432 SG_LOG(SG_GENERAL, SG_ALERT, "Unable to read the localized strings");
437 // Load the language specific strings
439 if (c_node != d_node) {
440 SGPath c_path( globals->get_fg_root() );
442 const char *c_path_str = c_node->getStringValue("strings");
444 SG_LOG(SG_GENERAL, SG_ALERT, "Incorrect path in configuration file.");
448 c_path.append(c_path_str);
449 SG_LOG(SG_GENERAL, SG_INFO, "Reading localized strings from "
453 readProperties(c_path.str(), strings);
454 } catch (const sg_exception &e) {
455 SG_LOG(SG_GENERAL, SG_ALERT, "Unable to read the localized strings");
465 // Initialize the localization routines
466 bool fgDetectLanguage() {
467 char *language = ::getenv("LANG");
469 if (language == NULL) {
470 SG_LOG(SG_GENERAL, SG_INFO, "Unable to detect the language" );
474 SGPropertyNode *locale = fgInitLocale(language);
476 SG_LOG(SG_GENERAL, SG_ALERT,
477 "No internationalization settings specified in preferences.xml" );
482 globals->set_locale( locale );
487 // Attempt to locate and parse the various non-XML config files in order
488 // from least precidence to greatest precidence
490 do_options (int argc, char ** argv)
492 // Check for $fg_root/system.fgfsrc
493 SGPath config( globals->get_fg_root() );
494 config.append( "system.fgfsrc" );
495 fgParseOptions(config.str());
497 #if defined( unix ) || defined( __CYGWIN__ )
499 // Check for $fg_root/system.fgfsrc.hostname
500 gethostname( name, 256 );
501 config.concat( "." );
502 config.concat( name );
503 fgParseOptions(config.str());
506 // Check for ~/.fgfsrc
507 char* envp = ::getenv( "HOME" );
508 if ( envp != NULL ) {
510 config.append( ".fgfsrc" );
511 fgParseOptions(config.str());
514 #if defined( unix ) || defined( __CYGWIN__ )
515 // Check for ~/.fgfsrc.hostname
516 gethostname( name, 256 );
517 config.concat( "." );
518 config.concat( name );
519 fgParseOptions(config.str());
522 // Parse remaining command line options
523 // These will override anything specified in a config file
524 fgParseArgs(argc, argv);
528 static string fgFindAircraftPath( const SGPath &path, const string &aircraft ) {
530 ulDir *dirp = ulOpenDir(path.str().c_str());
532 cerr << "Unable to open aircraft directory '" << path.str() << '\'' << endl;
536 while ((dire = ulReadDir(dirp)) != NULL) {
538 if ( strcmp("CVS", dire->d_name) && strcmp(".", dire->d_name)
539 && strcmp("..", dire->d_name) )
542 next.append(dire->d_name);
544 string result = fgFindAircraftPath( next, aircraft );
545 if ( ! result.empty() ) {
549 } else if ( !strcmp(dire->d_name, aircraft.c_str()) ) {
560 // Read in configuration (file and command line)
561 bool fgInitConfig ( int argc, char **argv ) {
563 // First, set some sane default values
566 // Read global preferences from $FG_ROOT/preferences.xml
567 SG_LOG(SG_INPUT, SG_INFO, "Reading global preferences");
568 fgLoadProps("preferences.xml", globals->get_props());
569 SG_LOG(SG_INPUT, SG_INFO, "Finished Reading global preferences");
571 // Detect the required language as early as possible
572 if ( !fgDetectLanguage() ) {
576 // Scan user config files and command line for a specified aircraft.
577 fgInitFGAircraft(argc, argv);
579 string aircraft = fgGetString( "/sim/aircraft", "" );
580 if ( aircraft.size() > 0 ) {
581 SGPath aircraft_search( globals->get_fg_root() );
582 aircraft_search.append( "Aircraft" );
584 string aircraft_set = aircraft + "-set.xml";
586 string result = fgFindAircraftPath( aircraft_search, aircraft_set );
587 if ( !result.empty() ) {
588 fgSetString( "/sim/aircraft-dir", result.c_str() );
589 SGPath full_name( result );
590 full_name.append( aircraft_set );
592 SG_LOG(SG_INPUT, SG_INFO, "Reading default aircraft: " << aircraft
593 << " from " << full_name.str());
595 readProperties( full_name.str(), globals->get_props() );
596 } catch ( const sg_exception &e ) {
597 string message = "Error reading default aircraft: ";
598 message += e.getFormattedMessage();
599 SG_LOG(SG_INPUT, SG_ALERT, message);
603 SG_LOG( SG_INPUT, SG_ALERT, "Cannot find specified aircraft: "
609 SG_LOG( SG_INPUT, SG_ALERT, "No default aircraft specified" );
612 // parse options after loading aircraft to ensure any user
613 // overrides of defaults are honored.
614 do_options(argc, argv);
620 // find basic airport location info from airport database
621 bool fgFindAirportID( const string& id, FGAirport *a ) {
622 const FGAirport* result;
624 SG_LOG( SG_GENERAL, SG_INFO, "Searching for airport code = " << id );
626 result = globals->get_airports()->search( id );
628 if ( result == NULL ) {
629 SG_LOG( SG_GENERAL, SG_ALERT,
630 "Failed to find " << id << " in apt.dat.gz" );
639 SG_LOG( SG_GENERAL, SG_INFO,
640 "Position for " << id << " is ("
641 << a->getLongitude() << ", "
642 << a->getLatitude() << ")" );
648 // get airport elevation
649 static double fgGetAirportElev( const string& id ) {
653 SG_LOG( SG_GENERAL, SG_INFO,
654 "Finding elevation for airport: " << id );
656 if ( fgFindAirportID( id, &a ) ) {
657 return a.getElevation();
666 // This function is never used, but maybe useful in the future ???
669 // Preset lon/lat given an airport id
670 static bool fgSetPosFromAirportID( const string& id ) {
674 SG_LOG( SG_GENERAL, SG_INFO,
675 "Attempting to set starting position from airport code " << id );
677 if ( fgFindAirportID( id, &a ) ) {
679 fgSetDouble("/sim/presets/longitude-deg", a.longitude );
680 fgSetDouble("/sim/presets/latitude-deg", a.latitude );
682 // other code depends on the actual postition being set so set
684 fgSetDouble("/position/longitude-deg", a.longitude );
685 fgSetDouble("/position/latitude-deg", a.latitude );
687 SG_LOG( SG_GENERAL, SG_INFO,
688 "Position for " << id << " is (" << a.longitude
689 << ", " << a.latitude << ")" );
699 // Set current tower position lon/lat given an airport id
700 static bool fgSetTowerPosFromAirportID( const string& id, double hdg ) {
702 // tower height hard coded for now...
703 float towerheight=50.0f;
705 // make a little off the heading for 1 runway airports...
706 float fudge_lon = fabs(sin(hdg)) * .003f;
707 float fudge_lat = .003f - fudge_lon;
709 if ( fgFindAirportID( id, &a ) ) {
710 fgSetDouble("/sim/tower/longitude-deg", a.getLongitude() + fudge_lon);
711 fgSetDouble("/sim/tower/latitude-deg", a.getLatitude() + fudge_lat);
712 fgSetDouble("/sim/tower/altitude-ft", a.getElevation() + towerheight);
721 // Set current_options lon/lat given an airport id and heading (degrees)
722 static bool fgSetPosFromAirportIDandHdg( const string& id, double tgt_hdg ) {
726 // set initial position from runway and heading
728 SG_LOG( SG_GENERAL, SG_INFO,
729 "Attempting to set starting position from airport code "
730 << id << " heading " << tgt_hdg );
732 if ( ! globals->get_runways()->search( id, (int)tgt_hdg, &r ) ) {
733 SG_LOG( SG_GENERAL, SG_ALERT,
734 "Failed to find a good runway for " << id << '\n' );
741 double lat2, lon2, az2;
742 double heading = r._heading;
743 double azimuth = heading + 180.0;
744 while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
746 SG_LOG( SG_GENERAL, SG_INFO,
747 "runway = " << r._lon << ", " << r._lat
748 << " length = " << r._length * SG_FEET_TO_METER
749 << " heading = " << azimuth );
751 geo_direct_wgs_84 ( 0, r._lat, r._lon, azimuth,
752 r._length * SG_FEET_TO_METER * 0.5 - 5.0,
753 &lat2, &lon2, &az2 );
755 if ( fabs( fgGetDouble("/sim/presets/offset-distance") ) > SG_EPSILON ) {
757 double odist = fgGetDouble("/sim/presets/offset-distance");
758 odist *= SG_NM_TO_METER;
759 double oaz = azimuth;
760 if ( fabs(fgGetDouble("/sim/presets/offset-azimuth")) > SG_EPSILON ) {
761 oaz = fgGetDouble("/sim/presets/offset-azimuth") + 180;
764 while ( oaz >= 360.0 ) { oaz -= 360.0; }
765 geo_direct_wgs_84 ( 0, lat2, lon2, oaz, odist, &olat, &olon, &az2 );
771 fgSetDouble("/sim/presets/longitude-deg", lon2 );
772 fgSetDouble("/sim/presets/latitude-deg", lat2 );
773 fgSetDouble("/sim/presets/heading-deg", heading );
775 // other code depends on the actual values being set ...
776 fgSetDouble("/position/longitude-deg", lon2 );
777 fgSetDouble("/position/latitude-deg", lat2 );
778 fgSetDouble("/orientation/heading-deg", heading );
780 SG_LOG( SG_GENERAL, SG_INFO,
781 "Position for " << id << " is ("
783 << lat2 << ") new heading is "
790 // Set current_options lon/lat given an airport id and runway number
791 static bool fgSetPosFromAirportIDandRwy( const string& id, const string& rwy ) {
795 // set initial position from airport and runway number
797 SG_LOG( SG_GENERAL, SG_INFO,
798 "Attempting to set starting position for "
799 << id << ":" << rwy );
801 if ( ! globals->get_runways()->search( id, rwy, &r ) ) {
802 SG_LOG( SG_GENERAL, SG_ALERT,
803 "Failed to find runway " << rwy <<
804 " at airport " << id );
811 double lat2, lon2, az2;
812 double heading = r._heading;
813 double azimuth = heading + 180.0;
814 while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
816 SG_LOG( SG_GENERAL, SG_INFO,
817 "runway = " << r._lon << ", " << r._lat
818 << " length = " << r._length * SG_FEET_TO_METER
819 << " heading = " << azimuth );
821 geo_direct_wgs_84 ( 0, r._lat, r._lon,
823 r._length * SG_FEET_TO_METER * 0.5 - 5.0,
824 &lat2, &lon2, &az2 );
826 if ( fabs( fgGetDouble("/sim/presets/offset-distance") ) > SG_EPSILON )
829 double odist = fgGetDouble("/sim/presets/offset-distance");
830 odist *= SG_NM_TO_METER;
831 double oaz = azimuth;
832 if ( fabs(fgGetDouble("/sim/presets/offset-azimuth")) > SG_EPSILON )
834 oaz = fgGetDouble("/sim/presets/offset-azimuth") + 180;
835 heading = fgGetDouble("/sim/presets/heading-deg");
837 while ( oaz >= 360.0 ) { oaz -= 360.0; }
838 geo_direct_wgs_84 ( 0, lat2, lon2, oaz, odist, &olat, &olon, &az2 );
844 fgSetDouble("/sim/presets/longitude-deg", lon2 );
845 fgSetDouble("/sim/presets/latitude-deg", lat2 );
846 fgSetDouble("/sim/presets/heading-deg", heading );
848 // other code depends on the actual values being set ...
849 fgSetDouble("/position/longitude-deg", lon2 );
850 fgSetDouble("/position/latitude-deg", lat2 );
851 fgSetDouble("/orientation/heading-deg", heading );
853 SG_LOG( SG_GENERAL, SG_INFO,
854 "Position for " << id << " is ("
856 << lat2 << ") new heading is "
863 static void fgSetDistOrAltFromGlideSlope() {
864 // cout << "fgSetDistOrAltFromGlideSlope()" << endl;
865 string apt_id = fgGetString("/sim/presets/airport-id");
866 double gs = fgGetDouble("/sim/presets/glideslope-deg")
867 * SG_DEGREES_TO_RADIANS ;
868 double od = fgGetDouble("/sim/presets/offset-distance");
869 double alt = fgGetDouble("/sim/presets/altitude-ft");
871 double apt_elev = 0.0;
872 if ( ! apt_id.empty() ) {
873 apt_elev = fgGetAirportElev( apt_id );
874 if ( apt_elev < -9990.0 ) {
881 if( fabs(gs) > 0.01 && fabs(od) > 0.1 && alt < -9990 ) {
882 // set altitude from glideslope and offset-distance
883 od *= SG_NM_TO_METER * SG_METER_TO_FEET;
884 alt = fabs(od*tan(gs)) + apt_elev;
885 fgSetDouble("/sim/presets/altitude-ft", alt);
886 fgSetBool("/sim/presets/onground", false);
887 SG_LOG( SG_GENERAL, SG_INFO, "Calculated altitude as: "
889 } else if( fabs(gs) > 0.01 && alt > 0 && fabs(od) < 0.1) {
890 // set offset-distance from glideslope and altitude
891 od = (alt - apt_elev) / tan(gs);
892 od *= -1*SG_FEET_TO_METER * SG_METER_TO_NM;
893 fgSetDouble("/sim/presets/offset-distance", od);
894 fgSetBool("/sim/presets/onground", false);
895 SG_LOG( SG_GENERAL, SG_INFO, "Calculated offset distance as: "
897 } else if( fabs(gs) > 0.01 ) {
898 SG_LOG( SG_GENERAL, SG_ALERT,
899 "Glideslope given but not altitude or offset-distance." );
900 SG_LOG( SG_GENERAL, SG_ALERT, "Resetting glideslope to zero" );
901 fgSetDouble("/sim/presets/glideslope-deg", 0);
902 fgSetBool("/sim/presets/onground", true);
907 // Set current_options lon/lat given an airport id and heading (degrees)
908 static bool fgSetPosFromNAV( const string& id, const double& freq ) {
910 = globals->get_navlist()->findByIdentAndFreq( id.c_str(), freq );
912 // set initial position from runway and heading
914 SG_LOG( SG_GENERAL, SG_INFO, "Attempting to set starting position for "
915 << id << ":" << freq );
917 double lon = nav->get_lon();
918 double lat = nav->get_lat();
920 if ( fabs( fgGetDouble("/sim/presets/offset-distance") ) > SG_EPSILON )
922 double odist = fgGetDouble("/sim/presets/offset-distance")
924 double oaz = fabs(fgGetDouble("/sim/presets/offset-azimuth"))
926 while ( oaz >= 360.0 ) { oaz -= 360.0; }
927 double olat, olon, az2;
928 geo_direct_wgs_84 ( 0, lat, lon, oaz, odist, &olat, &olon, &az2 );
935 fgSetDouble("/sim/presets/longitude-deg", lon );
936 fgSetDouble("/sim/presets/latitude-deg", lat );
938 // other code depends on the actual values being set ...
939 fgSetDouble("/position/longitude-deg", lon );
940 fgSetDouble("/position/latitude-deg", lat );
941 fgSetDouble("/orientation/heading-deg",
942 fgGetDouble("/sim/presets/heading-deg") );
944 SG_LOG( SG_GENERAL, SG_INFO,
945 "Position for " << id << ":" << freq << " is ("
946 << lon << ", "<< lat << ")" );
950 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to locate NAV = "
951 << id << ":" << freq );
956 // Set current_options lon/lat given an aircraft carrier id
957 static bool fgSetPosFromCarrier( const string& carrier, const string& posid ) {
959 // set initial position from runway and heading
963 if (FGAIManager::getStartPosition(carrier, posid, geodPos, heading, uvw)) {
964 double lon = geodPos.lon() * SGD_RADIANS_TO_DEGREES;
965 double lat = geodPos.lat() * SGD_RADIANS_TO_DEGREES;
966 double alt = geodPos.elev() * SG_METER_TO_FEET;
968 SG_LOG( SG_GENERAL, SG_INFO, "Attempting to set starting position for "
969 << carrier << " at lat = " << lat << ", lon = " << lon
970 << ", alt = " << alt << ", heading = " << heading);
972 fgSetDouble("/sim/presets/longitude-deg", lon);
973 fgSetDouble("/sim/presets/latitude-deg", lat);
974 fgSetDouble("/sim/presets/altitude-ft", alt);
975 fgSetDouble("/sim/presets/heading-deg", heading);
976 fgSetDouble("/position/longitude-deg", lon);
977 fgSetDouble("/position/latitude-deg", lat);
978 fgSetDouble("/position/altitude-ft", alt);
979 fgSetDouble("/orientation/heading-deg", heading);
981 fgSetString("/sim/presets/speed-set", "UVW");
982 fgSetDouble("/velocities/uBody-fps", uvw[0]);
983 fgSetDouble("/velocities/vBody-fps", uvw[1]);
984 fgSetDouble("/velocities/wBody-fps", uvw[2]);
985 fgSetDouble("/sim/presets/uBody-fps", uvw[0]);
986 fgSetDouble("/sim/presets/vBody-fps", uvw[1]);
987 fgSetDouble("/sim/presets/wBody-fps", uvw[2]);
989 fgSetBool("/sim/presets/onground", true);
993 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to locate aircraft carrier = "
999 // Set current_options lon/lat given an airport id and heading (degrees)
1000 static bool fgSetPosFromFix( const string& id ) {
1003 // set initial position from runway and heading
1004 if ( globals->get_fixlist()->query( id.c_str(), &fix ) ) {
1005 SG_LOG( SG_GENERAL, SG_INFO, "Attempting to set starting position for "
1008 double lon = fix.get_lon();
1009 double lat = fix.get_lat();
1011 if ( fabs( fgGetDouble("/sim/presets/offset-distance") ) > SG_EPSILON )
1013 double odist = fgGetDouble("/sim/presets/offset-distance")
1015 double oaz = fabs(fgGetDouble("/sim/presets/offset-azimuth"))
1017 while ( oaz >= 360.0 ) { oaz -= 360.0; }
1018 double olat, olon, az2;
1019 geo_direct_wgs_84 ( 0, lat, lon, oaz, odist, &olat, &olon, &az2 );
1026 fgSetDouble("/sim/presets/longitude-deg", lon );
1027 fgSetDouble("/sim/presets/latitude-deg", lat );
1029 // other code depends on the actual values being set ...
1030 fgSetDouble("/position/longitude-deg", lon );
1031 fgSetDouble("/position/latitude-deg", lat );
1032 fgSetDouble("/orientation/heading-deg",
1033 fgGetDouble("/sim/presets/heading-deg") );
1035 SG_LOG( SG_GENERAL, SG_INFO,
1036 "Position for " << id << " is ("
1037 << lon << ", "<< lat << ")" );
1041 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to locate NAV = "
1047 static void parseWaypoints() {
1048 string_list *waypoints = globals->get_initial_waypoints();
1050 vector<string>::iterator i;
1051 for (i = waypoints->begin();
1052 i != waypoints->end();
1057 // Now were done using the way points we can deallocate the
1059 while (waypoints->begin() != waypoints->end()) {
1060 waypoints->pop_back();
1063 globals->set_initial_waypoints(0);
1072 * Initialize vor/ndb/ils/fix list management and query systems (as
1073 * well as simple airport db list)
1078 SG_LOG(SG_GENERAL, SG_INFO, "Loading Airport Database ...");
1080 SGPath aptdb( globals->get_fg_root() );
1081 aptdb.append( "Airports/apt.dat" );
1083 SGPath p_metar( globals->get_fg_root() );
1084 p_metar.append( "Airports/metar.dat" );
1086 FGAirportList *airports = new FGAirportList();
1087 globals->set_airports( airports );
1088 FGRunwayList *runways = new FGRunwayList();
1089 globals->set_runways( runways );
1091 fgAirportDBLoad( airports, runways, aptdb.str(), p_metar.str() );
1093 FGNavList *navlist = new FGNavList;
1094 FGNavList *loclist = new FGNavList;
1095 FGNavList *gslist = new FGNavList;
1096 FGNavList *dmelist = new FGNavList;
1097 FGNavList *mkrlist = new FGNavList;
1098 FGNavList *tacanlist = new FGNavList;
1099 FGNavList *carrierlist = new FGNavList;
1100 FGTACANList *channellist = new FGTACANList;
1102 globals->set_navlist( navlist );
1103 globals->set_loclist( loclist );
1104 globals->set_gslist( gslist );
1105 globals->set_dmelist( dmelist );
1106 globals->set_mkrlist( mkrlist );
1107 globals->set_tacanlist( tacanlist );
1108 globals->set_carrierlist( carrierlist );
1109 globals->set_channellist( channellist );
1111 if ( !fgNavDBInit(airports, navlist, loclist, gslist, dmelist, mkrlist, tacanlist, carrierlist, channellist) ) {
1112 SG_LOG( SG_GENERAL, SG_ALERT,
1113 "Problems loading one or more navigational database" );
1116 if ( fgGetBool("/sim/navdb/localizers/auto-align", true) ) {
1117 // align all the localizers with their corresponding runways
1118 // since data sources are good for cockpit navigation
1119 // purposes, but not always to the error tolerances needed to
1120 // exactly place these items.
1122 = fgGetDouble( "/sim/navdb/localizers/auto-align-threshold-deg",
1124 fgNavDBAlignLOCwithRunway( runways, loclist, threshold );
1127 SG_LOG(SG_GENERAL, SG_INFO, " Fixes");
1128 SGPath p_fix( globals->get_fg_root() );
1129 p_fix.append( "Navaids/fix.dat" );
1130 FGFixList *fixlist = new FGFixList;
1131 fixlist->init( p_fix );
1132 globals->set_fixlist( fixlist );
1138 // Set the initial position based on presets (or defaults)
1139 bool fgInitPosition() {
1140 // cout << "fgInitPosition()" << endl;
1141 double gs = fgGetDouble("/sim/presets/glideslope-deg")
1142 * SG_DEGREES_TO_RADIANS ;
1143 double od = fgGetDouble("/sim/presets/offset-distance");
1144 double alt = fgGetDouble("/sim/presets/altitude-ft");
1146 bool set_pos = false;
1148 // If glideslope is specified, then calculate offset-distance or
1149 // altitude relative to glide slope if either of those was not
1151 if ( fabs( gs ) > 0.01 ) {
1152 fgSetDistOrAltFromGlideSlope();
1156 // If we have an explicit, in-range lon/lat, don't change it, just use it.
1157 // If not, check for an airport-id and use that.
1158 // If not, default to the middle of the KSFO field.
1159 // The default values for lon/lat are deliberately out of range
1160 // so that the airport-id can take effect; valid lon/lat will
1161 // override airport-id, however.
1162 double lon_deg = fgGetDouble("/sim/presets/longitude-deg");
1163 double lat_deg = fgGetDouble("/sim/presets/latitude-deg");
1164 if ( lon_deg >= -180.0 && lon_deg <= 180.0
1165 && lat_deg >= -90.0 && lat_deg <= 90.0 )
1170 string apt = fgGetString("/sim/presets/airport-id");
1171 string rwy_no = fgGetString("/sim/presets/runway");
1172 double hdg = fgGetDouble("/sim/presets/heading-deg");
1173 string vor = fgGetString("/sim/presets/vor-id");
1174 double vor_freq = fgGetDouble("/sim/presets/vor-freq");
1175 string ndb = fgGetString("/sim/presets/ndb-id");
1176 double ndb_freq = fgGetDouble("/sim/presets/ndb-freq");
1177 string carrier = fgGetString("/sim/presets/carrier");
1178 string parkpos = fgGetString("/sim/presets/parkpos");
1179 string fix = fgGetString("/sim/presets/fix");
1181 if ( !set_pos && !apt.empty() && !rwy_no.empty() ) {
1182 // An airport + runway is requested
1183 if ( fgSetPosFromAirportIDandRwy( apt, rwy_no ) ) {
1184 // set tower position (a little off the heading for single
1186 fgSetTowerPosFromAirportID( apt, hdg );
1191 if ( !set_pos && !apt.empty() ) {
1192 // An airport is requested (find runway closest to hdg)
1193 if ( fgSetPosFromAirportIDandHdg( apt, hdg ) ) {
1194 // set tower position (a little off the heading for single
1196 fgSetTowerPosFromAirportID( apt, hdg );
1201 if ( !set_pos && !vor.empty() ) {
1202 // a VOR is requested
1203 if ( fgSetPosFromNAV( vor, vor_freq ) ) {
1208 if ( !set_pos && !ndb.empty() ) {
1209 // an NDB is requested
1210 if ( fgSetPosFromNAV( ndb, ndb_freq ) ) {
1215 if ( !set_pos && !carrier.empty() ) {
1216 // an aircraft carrier is requested
1217 if ( fgSetPosFromCarrier( carrier, parkpos ) ) {
1222 if ( !set_pos && !fix.empty() ) {
1223 // a Fix is requested
1224 if ( fgSetPosFromFix( fix ) ) {
1230 // No lon/lat specified, no airport specified, default to
1231 // middle of KSFO field.
1232 fgSetDouble("/sim/presets/longitude-deg", -122.374843);
1233 fgSetDouble("/sim/presets/latitude-deg", 37.619002);
1236 fgSetDouble( "/position/longitude-deg",
1237 fgGetDouble("/sim/presets/longitude-deg") );
1238 fgSetDouble( "/position/latitude-deg",
1239 fgGetDouble("/sim/presets/latitude-deg") );
1240 fgSetDouble( "/orientation/heading-deg",
1241 fgGetDouble("/sim/presets/heading-deg") );
1243 // determine if this should be an on-ground or in-air start
1244 if ((fabs(gs) > 0.01 || fabs(od) > 0.1 || alt > 0.1) && carrier.empty()) {
1245 fgSetBool("/sim/presets/onground", false);
1247 fgSetBool("/sim/presets/onground", true);
1254 // General house keeping initializations
1255 bool fgInitGeneral() {
1258 #if defined(FX) && defined(XMESA)
1259 char *mesa_win_state;
1262 SG_LOG( SG_GENERAL, SG_INFO, "General Initialization" );
1263 SG_LOG( SG_GENERAL, SG_INFO, "======= ==============" );
1265 root = globals->get_fg_root();
1266 if ( ! root.length() ) {
1267 // No root path set? Then bail ...
1268 SG_LOG( SG_GENERAL, SG_ALERT,
1269 "Cannot continue without a path to the base package "
1270 << "being defined." );
1273 SG_LOG( SG_GENERAL, SG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
1275 #if defined(FX) && defined(XMESA)
1276 // initialize full screen flag
1277 globals->set_fullscreen(false);
1278 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
1279 // Test for the MESA_GLX_FX env variable
1280 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
1281 // test if we are fullscreen mesa/glide
1282 if ( (mesa_win_state[0] == 'f') ||
1283 (mesa_win_state[0] == 'F') ) {
1284 globals->set_fullscreen(true);
1294 // Initialize the flight model subsystem. This just creates the
1295 // object. The actual fdm initialization is delayed until we get a
1296 // proper scenery elevation hit. This is checked for in main.cxx
1300 if ( cur_fdm_state ) {
1301 delete cur_fdm_state;
1305 double dt = 1.0 / fgGetInt("/sim/model-hz");
1306 string model = fgGetString("/sim/flight-model");
1309 if ( model == "larcsim" ) {
1310 cur_fdm_state = new FGLaRCsim( dt );
1311 } else if ( model == "jsb" ) {
1312 cur_fdm_state = new FGJSBsim( dt );
1313 #ifdef ENABLE_SP_FMDS
1314 } else if ( model == "ada" ) {
1315 cur_fdm_state = new FGADA( dt );
1316 } else if ( model == "acms" ) {
1317 cur_fdm_state = new FGACMS( dt );
1319 } else if ( model == "balloon" ) {
1320 cur_fdm_state = new FGBalloonSim( dt );
1321 } else if ( model == "magic" ) {
1322 cur_fdm_state = new FGMagicCarpet( dt );
1323 } else if ( model == "ufo" ) {
1324 cur_fdm_state = new FGUFO( dt );
1325 } else if ( model == "external" ) {
1326 // external is a synonym for "--fdm=null" and is
1327 // maintained here for backwards compatibility
1328 cur_fdm_state = new FGNullFDM( dt );
1329 } else if ( model.find("network") == 0 ) {
1330 string host = "localhost";
1334 string net_options = model.substr(8);
1335 string::size_type begin, end;
1338 end = net_options.find( ",", begin );
1339 if ( end != string::npos ) {
1340 host = net_options.substr(begin, end - begin);
1344 end = net_options.find( ",", begin );
1345 if ( end != string::npos ) {
1346 port1 = atoi( net_options.substr(begin, end - begin).c_str() );
1350 end = net_options.find( ",", begin );
1351 if ( end != string::npos ) {
1352 port2 = atoi( net_options.substr(begin, end - begin).c_str() );
1356 end = net_options.find( ",", begin );
1357 if ( end != string::npos ) {
1358 port3 = atoi( net_options.substr(begin, end - begin).c_str() );
1361 cur_fdm_state = new FGExternalNet( dt, host, port1, port2, port3 );
1362 } else if ( model.find("pipe") == 0 ) {
1363 // /* old */ string pipe_path = model.substr(5);
1364 // /* old */ cur_fdm_state = new FGExternalPipe( dt, pipe_path );
1365 string pipe_path = "";
1366 string pipe_protocol = "";
1367 string pipe_options = model.substr(5);
1368 string::size_type begin, end;
1371 end = pipe_options.find( ",", begin );
1372 if ( end != string::npos ) {
1373 pipe_path = pipe_options.substr(begin, end - begin);
1376 // protocol (last option)
1377 pipe_protocol = pipe_options.substr(begin);
1378 cur_fdm_state = new FGExternalPipe( dt, pipe_path, pipe_protocol );
1379 } else if ( model == "null" ) {
1380 cur_fdm_state = new FGNullFDM( dt );
1381 } else if ( model == "yasim" ) {
1382 cur_fdm_state = new YASim( dt );
1384 SG_LOG(SG_GENERAL, SG_ALERT,
1385 "Unrecognized flight model '" << model
1386 << "', cannot init flight dynamics model.");
1390 SG_LOG(SG_GENERAL, SG_ALERT, "FlightGear aborting\n\n");
1395 static void printMat(const sgVec4 *mat, char *name="")
1398 SG_LOG(SG_GENERAL, SG_BULK, name );
1399 for(i=0; i<4; i++) {
1400 SG_LOG(SG_GENERAL, SG_BULK, " " << mat[i][0] << " " << mat[i][1]
1401 << " " << mat[i][2] << " " << mat[i][3] );
1405 // Initialize view parameters
1407 // force update of model so that viewer can get some data...
1408 globals->get_aircraft_model()->update(0);
1409 // run update for current view so that data is current...
1410 globals->get_viewmgr()->update(0);
1412 printMat(globals->get_current_view()->get_VIEW(),"VIEW");
1413 printMat(globals->get_current_view()->get_UP(),"UP");
1414 // printMat(globals->get_current_view()->get_LOCAL(),"LOCAL");
1419 SGTime *fgInitTime() {
1421 static const SGPropertyNode *longitude
1422 = fgGetNode("/position/longitude-deg");
1423 static const SGPropertyNode *latitude
1424 = fgGetNode("/position/latitude-deg");
1425 static const SGPropertyNode *cur_time_override
1426 = fgGetNode("/sim/time/cur-time-override", true);
1428 SGPath zone( globals->get_fg_root() );
1429 zone.append( "Timezone" );
1430 SGTime *t = new SGTime( longitude->getDoubleValue()
1431 * SGD_DEGREES_TO_RADIANS,
1432 latitude->getDoubleValue()
1433 * SGD_DEGREES_TO_RADIANS,
1435 cur_time_override->getLongValue() );
1437 globals->set_warp_delta( 0 );
1439 t->update( 0.0, 0.0,
1440 cur_time_override->getLongValue(),
1441 globals->get_warp() );
1447 // set up a time offset (aka warp) if one is specified
1448 void fgInitTimeOffset() {
1449 static const SGPropertyNode *longitude
1450 = fgGetNode("/position/longitude-deg");
1451 static const SGPropertyNode *latitude
1452 = fgGetNode("/position/latitude-deg");
1453 static const SGPropertyNode *cur_time_override
1454 = fgGetNode("/sim/time/cur-time-override", true);
1456 // Handle potential user specified time offsets
1457 int orig_warp = globals->get_warp();
1458 SGTime *t = globals->get_time_params();
1459 time_t cur_time = t->get_cur_time();
1460 time_t currGMT = sgTimeGetGMT( gmtime(&cur_time) );
1461 time_t systemLocalTime = sgTimeGetGMT( localtime(&cur_time) );
1462 time_t aircraftLocalTime =
1463 sgTimeGetGMT( fgLocaltime(&cur_time, t->get_zonename() ) );
1465 // Okay, we now have several possible scenarios
1466 int offset = fgGetInt("/sim/startup/time-offset");
1467 string offset_type = fgGetString("/sim/startup/time-offset-type");
1470 if ( offset_type == "real" ) {
1472 } else if ( offset_type == "dawn" ) {
1473 warp = fgTimeSecondsUntilSunAngle( cur_time,
1474 longitude->getDoubleValue()
1475 * SGD_DEGREES_TO_RADIANS,
1476 latitude->getDoubleValue()
1477 * SGD_DEGREES_TO_RADIANS,
1479 } else if ( offset_type == "morning" ) {
1480 warp = fgTimeSecondsUntilSunAngle( cur_time,
1481 longitude->getDoubleValue()
1482 * SGD_DEGREES_TO_RADIANS,
1483 latitude->getDoubleValue()
1484 * SGD_DEGREES_TO_RADIANS,
1486 } else if ( offset_type == "noon" ) {
1487 warp = fgTimeSecondsUntilSunAngle( cur_time,
1488 longitude->getDoubleValue()
1489 * SGD_DEGREES_TO_RADIANS,
1490 latitude->getDoubleValue()
1491 * SGD_DEGREES_TO_RADIANS,
1493 } else if ( offset_type == "afternoon" ) {
1494 warp = fgTimeSecondsUntilSunAngle( cur_time,
1495 longitude->getDoubleValue()
1496 * SGD_DEGREES_TO_RADIANS,
1497 latitude->getDoubleValue()
1498 * SGD_DEGREES_TO_RADIANS,
1500 } else if ( offset_type == "dusk" ) {
1501 warp = fgTimeSecondsUntilSunAngle( cur_time,
1502 longitude->getDoubleValue()
1503 * SGD_DEGREES_TO_RADIANS,
1504 latitude->getDoubleValue()
1505 * SGD_DEGREES_TO_RADIANS,
1507 } else if ( offset_type == "evening" ) {
1508 warp = fgTimeSecondsUntilSunAngle( cur_time,
1509 longitude->getDoubleValue()
1510 * SGD_DEGREES_TO_RADIANS,
1511 latitude->getDoubleValue()
1512 * SGD_DEGREES_TO_RADIANS,
1514 } else if ( offset_type == "midnight" ) {
1515 warp = fgTimeSecondsUntilSunAngle( cur_time,
1516 longitude->getDoubleValue()
1517 * SGD_DEGREES_TO_RADIANS,
1518 latitude->getDoubleValue()
1519 * SGD_DEGREES_TO_RADIANS,
1521 } else if ( offset_type == "system-offset" ) {
1523 } else if ( offset_type == "gmt-offset" ) {
1524 warp = offset - (currGMT - systemLocalTime);
1525 } else if ( offset_type == "latitude-offset" ) {
1526 warp = offset - (aircraftLocalTime - systemLocalTime);
1527 } else if ( offset_type == "system" ) {
1528 warp = offset - cur_time;
1529 } else if ( offset_type == "gmt" ) {
1530 warp = offset - currGMT;
1531 } else if ( offset_type == "latitude" ) {
1532 warp = offset - (aircraftLocalTime - systemLocalTime) - cur_time;
1534 SG_LOG( SG_GENERAL, SG_ALERT,
1535 "FG_TIME::Unsupported offset type " << offset_type );
1538 globals->set_warp( orig_warp + warp );
1539 t->update( longitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
1540 latitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
1541 cur_time_override->getLongValue(),
1542 globals->get_warp() );
1544 SG_LOG( SG_GENERAL, SG_INFO, "After fgInitTimeOffset(): warp = "
1545 << globals->get_warp() );
1549 // This is the top level init routine which calls all the other
1550 // initialization routines. If you are adding a subsystem to flight
1551 // gear, its initialization call should located in this routine.
1552 // Returns non-zero if a problem encountered.
1553 bool fgInitSubsystems() {
1554 // static const SGPropertyNode *longitude
1555 // = fgGetNode("/sim/presets/longitude-deg");
1556 // static const SGPropertyNode *latitude
1557 // = fgGetNode("/sim/presets/latitude-deg");
1558 // static const SGPropertyNode *altitude
1559 // = fgGetNode("/sim/presets/altitude-ft");
1561 SG_LOG( SG_GENERAL, SG_INFO, "Initialize Subsystems");
1562 SG_LOG( SG_GENERAL, SG_INFO, "========== ==========");
1564 ////////////////////////////////////////////////////////////////////
1565 // Initialize the event manager subsystem.
1566 ////////////////////////////////////////////////////////////////////
1568 globals->get_event_mgr()->init();
1569 globals->get_event_mgr()->setFreezeProperty(fgGetNode("/sim/freeze/clock"));
1571 ////////////////////////////////////////////////////////////////////
1572 // Initialize the property interpolator subsystem
1573 ////////////////////////////////////////////////////////////////////
1574 globals->add_subsystem("interpolator", new SGInterpolator);
1577 ////////////////////////////////////////////////////////////////////
1578 // Add the FlightGear property utilities.
1579 ////////////////////////////////////////////////////////////////////
1580 globals->add_subsystem("properties", new FGProperties);
1582 ////////////////////////////////////////////////////////////////////
1583 // Initialize the material property subsystem.
1584 ////////////////////////////////////////////////////////////////////
1586 SGPath mpath( globals->get_fg_root() );
1587 mpath.append( "materials.xml" );
1588 if ( ! globals->get_matlib()->load(globals->get_fg_root(), mpath.str()) ) {
1589 SG_LOG( SG_GENERAL, SG_ALERT, "Error loading material lib!" );
1594 ////////////////////////////////////////////////////////////////////
1595 // Initialize the scenery management subsystem.
1596 ////////////////////////////////////////////////////////////////////
1598 if ( globals->get_tile_mgr()->init() ) {
1599 // Load the local scenery data
1600 double visibility_meters = fgGetDouble("/environment/visibility-m");
1602 globals->get_tile_mgr()->update( visibility_meters );
1604 SG_LOG( SG_GENERAL, SG_ALERT, "Error in Tile Manager initialization!" );
1608 // cause refresh of viewer scenery timestamps every 15 seconds...
1609 globals->get_event_mgr()->addTask( "FGTileMgr::refresh_view_timestamps()",
1610 globals->get_tile_mgr(),
1611 &FGTileMgr::refresh_view_timestamps,
1615 ////////////////////////////////////////////////////////////////////
1616 // Initialize the flight model subsystem.
1617 ////////////////////////////////////////////////////////////////////
1621 // allocates structures so must happen before any of the flight
1622 // model or control parameters are set
1623 fgAircraftInit(); // In the future this might not be the case.
1626 ////////////////////////////////////////////////////////////////////
1627 // Initialize the XML Autopilot subsystem.
1628 ////////////////////////////////////////////////////////////////////
1630 globals->add_subsystem( "xml-autopilot", new FGXMLAutopilot );
1631 globals->add_subsystem( "route-manager", new FGRouteMgr );
1634 ////////////////////////////////////////////////////////////////////
1635 // Initialize the view manager subsystem.
1636 ////////////////////////////////////////////////////////////////////
1640 ////////////////////////////////////////////////////////////////////
1641 // Create and register the logger.
1642 ////////////////////////////////////////////////////////////////////
1644 globals->add_subsystem("logger", new FGLogger);
1646 ////////////////////////////////////////////////////////////////////
1647 // Create and register the XML GUI.
1648 ////////////////////////////////////////////////////////////////////
1650 globals->add_subsystem("gui", new NewGUI, SGSubsystemMgr::INIT);
1653 ////////////////////////////////////////////////////////////////////
1654 // Initialize the local time subsystem.
1655 ////////////////////////////////////////////////////////////////////
1657 // update the current timezone each 30 minutes
1658 globals->get_event_mgr()->addTask( "fgUpdateLocalTime()",
1659 &fgUpdateLocalTime, 30*60 );
1660 fgInitTimeOffset(); // the environment subsystem needs this
1663 ////////////////////////////////////////////////////////////////////
1664 // Initialize the weather subsystem.
1665 ////////////////////////////////////////////////////////////////////
1667 // Initialize the weather modeling subsystem
1668 globals->add_subsystem("environment", new FGEnvironmentMgr);
1671 ////////////////////////////////////////////////////////////////////
1672 // Initialize the lighting subsystem.
1673 ////////////////////////////////////////////////////////////////////
1675 globals->add_subsystem("lighting", new FGLight);
1677 //////////////////////////////////////////////////////////////////////
1678 // Initialize the 2D cloud subsystem.
1679 ////////////////////////////////////////////////////////////////////
1680 fgGetBool("/sim/rendering/bump-mapping", false);
1682 #ifdef ENABLE_AUDIO_SUPPORT
1683 ////////////////////////////////////////////////////////////////////
1684 // Initialize the sound subsystem.
1685 ////////////////////////////////////////////////////////////////////
1687 init_volume = fgGetFloat("/sim/sound/volume");
1688 fgSetFloat("/sim/sound/volume", 0.0f);
1689 globals->set_soundmgr(new SGSoundMgr);
1690 globals->get_soundmgr()->init();
1691 globals->get_soundmgr()->bind();
1694 ////////////////////////////////////////////////////////////////////
1695 // Initialize the sound-effects subsystem.
1696 ////////////////////////////////////////////////////////////////////
1698 globals->add_subsystem("fx", new FGFX);
1702 ////////////////////////////////////////////////////////////////////
1703 // Initialise ATC display system
1704 ////////////////////////////////////////////////////////////////////
1706 SG_LOG(SG_GENERAL, SG_INFO, " ATC Display");
1707 globals->set_ATC_display(new FGATCDisplay);
1708 globals->get_ATC_display()->init();
1710 ////////////////////////////////////////////////////////////////////
1711 // Initialise the ATC Manager
1712 ////////////////////////////////////////////////////////////////////
1714 SG_LOG(SG_GENERAL, SG_INFO, " ATC Manager");
1715 globals->set_ATC_mgr(new FGATCMgr);
1716 globals->get_ATC_mgr()->init();
1718 ////////////////////////////////////////////////////////////////////
1719 // Initialise the AI Manager
1720 ////////////////////////////////////////////////////////////////////
1722 SG_LOG(SG_GENERAL, SG_INFO, " AI Manager");
1723 globals->set_AI_mgr(new FGAIMgr);
1724 globals->get_AI_mgr()->init();
1726 ////////////////////////////////////////////////////////////////////
1727 // Initialise the AI Model Manager
1728 ////////////////////////////////////////////////////////////////////
1729 SG_LOG(SG_GENERAL, SG_INFO, " AI Model Manager");
1730 globals->add_subsystem("ai_model", new FGAIManager);
1731 globals->add_subsystem("submodel_mgr", new FGSubmodelMgr);
1734 // It's probably a good idea to initialize the top level traffic manager
1735 // After the AI and ATC systems have been initialized properly.
1736 // AI Traffic manager
1737 globals->add_subsystem("Traffic Manager", new FGTrafficManager);
1738 FGTrafficManager *dispatcher =
1739 (FGTrafficManager*) globals->get_subsystem("Traffic Manager");
1740 SGPath path = globals->get_fg_root();
1741 path.append("/Traffic/fgtraffic.xml");
1744 //globals->get_subsystem("Traffic Manager")->init();
1746 globals->add_subsystem("instrumentation", new FGInstrumentMgr);
1747 globals->add_subsystem("systems", new FGSystemMgr);
1751 ////////////////////////////////////////////////////////////////////
1752 // Initialize the cockpit subsystem
1753 ////////////////////////////////////////////////////////////////////
1754 if( fgCockpitInit( ¤t_aircraft )) {
1755 // Cockpit initialized ok.
1757 SG_LOG( SG_GENERAL, SG_ALERT, "Error in Cockpit initialization!" );
1762 ////////////////////////////////////////////////////////////////////
1763 // Initialize the autopilot subsystem.
1764 ////////////////////////////////////////////////////////////////////
1766 // FIXME: these should go in the
1767 // GUI initialization code, not here.
1768 // fgAPAdjustInit();
1769 NewTgtAirportInit();
1773 ////////////////////////////////////////////////////////////////////
1774 // Initialize I/O subsystem.
1775 ////////////////////////////////////////////////////////////////////
1777 globals->get_io()->init();
1778 globals->get_io()->bind();
1781 ////////////////////////////////////////////////////////////////////
1782 // Add a new 2D panel.
1783 ////////////////////////////////////////////////////////////////////
1785 string panel_path = fgGetString("/sim/panel/path",
1786 "Panels/Default/default.xml");
1788 globals->set_current_panel( fgReadPanel(panel_path) );
1789 if (globals->get_current_panel() == 0) {
1790 SG_LOG( SG_INPUT, SG_ALERT,
1791 "Error reading new panel from " << panel_path );
1793 SG_LOG( SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path );
1794 globals->get_current_panel()->init();
1795 globals->get_current_panel()->bind();
1799 ////////////////////////////////////////////////////////////////////
1800 // Initialize the controls subsystem.
1801 ////////////////////////////////////////////////////////////////////
1803 globals->get_controls()->init();
1804 globals->get_controls()->bind();
1807 ////////////////////////////////////////////////////////////////////
1808 // Initialize the input subsystem.
1809 ////////////////////////////////////////////////////////////////////
1811 globals->add_subsystem("input", new FGInput);
1814 ////////////////////////////////////////////////////////////////////
1815 // Initialize the replay subsystem
1816 ////////////////////////////////////////////////////////////////////
1817 globals->add_subsystem("replay", new FGReplay);
1819 ////////////////////////////////////////////////////////////////////
1820 // Bind and initialize subsystems.
1821 ////////////////////////////////////////////////////////////////////
1823 globals->get_subsystem_mgr()->bind();
1824 globals->get_subsystem_mgr()->init();
1826 #ifdef FG_MPLAYER_AS
1827 ////////////////////////////////////////////////////////////////////
1828 // Initialize multiplayer subsystem
1829 ////////////////////////////////////////////////////////////////////
1831 globals->set_multiplayer_tx_mgr(new FGMultiplayTxMgr);
1832 globals->get_multiplayer_tx_mgr()->init();
1834 globals->set_multiplayer_rx_mgr(new FGMultiplayRxMgr);
1835 globals->get_multiplayer_rx_mgr()->init();
1838 ////////////////////////////////////////////////////////////////////////
1839 // Initialize the Nasal interpreter.
1840 // Do this last, so that the loaded scripts see initialized state
1841 ////////////////////////////////////////////////////////////////////////
1842 FGNasalSys* nasal = new FGNasalSys();
1843 globals->add_subsystem("nasal", nasal);
1846 ////////////////////////////////////////////////////////////////////
1847 // At this point we could try and parse the waypoint options
1848 ///////////////////////////////////////////////////////////////////
1851 // initialize methods that depend on other subsystems.
1852 globals->get_subsystem_mgr()->postinit();
1854 ////////////////////////////////////////////////////////////////////////
1855 // End of subsystem initialization.
1856 ////////////////////////////////////////////////////////////////////
1858 SG_LOG( SG_GENERAL, SG_INFO, endl);
1860 // Save the initial state for future
1862 globals->saveInitialState();
1868 void fgReInitSubsystems()
1870 // static const SGPropertyNode *longitude
1871 // = fgGetNode("/sim/presets/longitude-deg");
1872 // static const SGPropertyNode *latitude
1873 // = fgGetNode("/sim/presets/latitude-deg");
1874 static const SGPropertyNode *altitude
1875 = fgGetNode("/sim/presets/altitude-ft");
1876 static const SGPropertyNode *master_freeze
1877 = fgGetNode("/sim/freeze/master");
1879 SG_LOG( SG_GENERAL, SG_INFO,
1880 "fgReInitSubsystems(): /position/altitude = "
1881 << altitude->getDoubleValue() );
1883 bool freeze = master_freeze->getBoolValue();
1885 fgSetBool("/sim/freeze/master", true);
1887 fgSetBool("/sim/crashed", false);
1889 // Force reupdating the positions of the ai 3d models. They are used for
1890 // initializing ground level for the FDM.
1891 globals->get_subsystem("ai_model")->reinit();
1893 // Initialize the FDM
1896 // allocates structures so must happen before any of the flight
1897 // model or control parameters are set
1898 fgAircraftInit(); // In the future this might not be the case.
1900 // reload offsets from config defaults
1901 globals->get_viewmgr()->reinit();
1905 globals->get_controls()->reset_all();
1907 fgUpdateLocalTime();
1909 // re-init to proper time of day setting
1913 fgSetBool("/sim/freeze/master", false);
1915 fgSetBool("/sim/sceneryloaded",false);