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()
38 #if defined( unix ) || defined( __CYGWIN__ )
39 # include <unistd.h> // for gethostname()
42 // work around a stdc++ lib bug in some versions of linux, but doesn't
43 // seem to hurt to have this here for all versions of Linux.
45 # define _G_NO_EXTERN_TEMPLATES
48 #include <simgear/compiler.h>
52 #include <simgear/constants.h>
53 #include <simgear/debug/logstream.hxx>
54 #include <simgear/structure/exception.hxx>
55 #include <simgear/structure/event_mgr.hxx>
56 #include <simgear/math/point3d.hxx>
57 #include <simgear/math/polar3d.hxx>
58 #include <simgear/math/sg_geodesy.hxx>
59 #include <simgear/misc/sg_path.hxx>
60 #include <simgear/misc/interpolator.hxx>
61 #include <simgear/scene/material/matlib.hxx>
62 #include <simgear/timing/sg_time.hxx>
63 #include <simgear/timing/lowleveltime.h>
65 #include <Aircraft/aircraft.hxx>
66 #include <Aircraft/replay.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 <Scenery/scenery.hxx>
103 #include <Scenery/tilemgr.hxx>
104 #include <Scripting/NasalSys.hxx>
105 #include <Sound/fg_fx.hxx>
106 #include <Sound/beacon.hxx>
107 #include <Sound/morse.hxx>
108 #include <Sound/voice.hxx>
109 #include <Systems/system_mgr.hxx>
110 #include <Time/light.hxx>
111 #include <Time/sunsolver.hxx>
112 #include <Time/tmp.hxx>
113 #include <Traffic/TrafficMgr.hxx>
116 #include <MultiPlayer/multiplaymgr.hxx>
119 #include <Environment/environment_mgr.hxx>
121 #include "fg_init.hxx"
123 #include "fg_commands.hxx"
124 #include "fg_props.hxx"
125 #include "options.hxx"
126 #include "globals.hxx"
127 #include "logger.hxx"
128 #include "viewmgr.hxx"
131 #if defined(FX) && defined(XMESA)
132 #include <GL/xmesa.h>
135 SG_USING_STD(string);
139 extern const char *default_root;
143 // Scan the command line options for the specified option and return
145 static string fgScanForOption( const string& option, int argc, char **argv ) {
148 if (hostname == NULL)
151 gethostname(_hostname, 256);
152 hostname = strdup(_hostname);
153 free_hostname = true;
156 SG_LOG(SG_GENERAL, SG_INFO, "Scanning command line for: " << option );
158 int len = option.length();
161 SG_LOG( SG_GENERAL, SG_DEBUG, "argv[" << i << "] = " << argv[i] );
163 string arg = argv[i];
164 if ( arg.find( option ) == 0 ) {
165 return arg.substr( len );
175 // Scan the user config files for the specified option and return
177 static string fgScanForOption( const string& option, const string& path ) {
178 sg_gzifstream in( path );
179 if ( !in.is_open() ) {
183 SG_LOG( SG_GENERAL, SG_INFO, "Scanning " << path << " for: " << option );
185 int len = option.length();
189 while ( ! in.eof() ) {
192 while ( in.get(c) && c != '\0' ) {
197 #if defined( macintosh )
198 getline( in, line, '\r' );
200 getline( in, line, '\n' );
203 // catch extraneous (DOS) line ending character
204 if ( line[line.length() - 1] < 32 ) {
205 line = line.substr( 0, line.length()-1 );
208 if ( line.find( option ) == 0 ) {
209 return line.substr( len );
218 // Scan the user config files for the specified option and return
220 static string fgScanForOption( const string& option ) {
223 #if defined( unix ) || defined( __CYGWIN__ )
224 // Next check home directory for .fgfsrc.hostname file
226 if ( homedir != NULL ) {
227 SGPath config( homedir );
228 config.append( ".fgfsrc" );
229 config.concat( "." );
230 config.concat( hostname );
231 arg = fgScanForOption( option, config.str() );
236 // Next check home directory for .fgfsrc file
238 if ( homedir != NULL ) {
239 SGPath config( homedir );
240 config.append( ".fgfsrc" );
241 arg = fgScanForOption( option, config.str() );
246 // Check for $fg_root/system.fgfsrc
247 SGPath config( globals->get_fg_root() );
248 config.append( "system.fgfsrc" );
249 arg = fgScanForOption( option, config.str() );
256 // Read in configuration (files and command line options) but only set
258 bool fgInitFGRoot ( int argc, char **argv ) {
261 // First parse command line options looking for --fg-root=, this
262 // will override anything specified in a config file
263 root = fgScanForOption( "--fg-root=", argc, argv);
265 // Check in one of the user configuration files.
267 root = fgScanForOption( "--fg-root=" );
269 // Next check if fg-root is set as an env variable
270 if ( root.empty() ) {
271 char *envp = ::getenv( "FG_ROOT" );
272 if ( envp != NULL ) {
277 // Otherwise, default to a random compiled-in location if we can't
278 // find fg-root any other way.
279 if ( root.empty() ) {
280 #if defined( __CYGWIN__ )
281 root = "/FlightGear";
282 #elif defined( WIN32 )
283 root = "\\FlightGear";
284 #elif defined(OSX_BUNDLE)
285 /* the following code looks for the base package directly inside
286 the application bundle. This can be changed fairly easily by
287 fiddling with the code below. And yes, I know it's ugly and verbose.
289 CFBundleRef appBundle = CFBundleGetMainBundle();
290 CFURLRef appUrl = CFBundleCopyBundleURL(appBundle);
291 CFRelease(appBundle);
293 // look for a 'data' subdir directly inside the bundle : is there
294 // a better place? maybe in Resources? I don't know ...
295 CFURLRef dataDir = CFURLCreateCopyAppendingPathComponent(NULL, appUrl, CFSTR("data"), true);
297 // now convert down to a path, and the a c-string
298 CFStringRef path = CFURLCopyFileSystemPath(dataDir, kCFURLPOSIXPathStyle);
299 root = CFStringGetCStringPtr(path, CFStringGetSystemEncoding());
302 CFRelease(appBundle);
310 SG_LOG(SG_INPUT, SG_INFO, "fg_root = " << root );
311 globals->set_fg_root(root);
317 // Read in configuration (files and command line options) but only set
319 bool fgInitFGAircraft ( int argc, char **argv ) {
322 // First parse command line options looking for --aircraft=, this
323 // will override anything specified in a config file
324 aircraft = fgScanForOption( "--aircraft=", argc, argv );
326 // Check in one of the user configuration files.
327 if ( aircraft.empty() )
328 aircraft = fgScanForOption( "--aircraft=" );
330 // if an aircraft was specified, set the property name
331 if ( !aircraft.empty() ) {
332 SG_LOG(SG_INPUT, SG_INFO, "aircraft = " << aircraft );
333 fgSetString("/sim/aircraft", aircraft.c_str() );
335 SG_LOG(SG_INPUT, SG_INFO, "No user specified aircraft, using default" );
342 // Return the current base package version
343 string fgBasePackageVersion() {
344 SGPath base_path( globals->get_fg_root() );
345 base_path.append("version");
347 sg_gzifstream in( base_path.str() );
348 if ( !in.is_open() ) {
349 SGPath old_path( globals->get_fg_root() );
350 old_path.append( "Thanks" );
351 sg_gzifstream old( old_path.str() );
352 if ( !old.is_open() ) {
355 return "[old version]";
366 // Initialize the localization
367 SGPropertyNode *fgInitLocale(const char *language) {
368 SGPropertyNode *c_node = NULL, *d_node = NULL;
369 SGPropertyNode *intl = fgGetNode("/sim/intl");
371 SG_LOG(SG_GENERAL, SG_INFO, "Selecting language: " << language );
373 // localization not defined
378 // Select the proper language from the list
380 vector<SGPropertyNode_ptr> locale = intl->getChildren("locale");
381 for (unsigned int i = 0; i < locale.size(); i++) {
383 vector<SGPropertyNode_ptr> lang = locale[i]->getChildren("lang");
384 for (unsigned int j = 0; j < lang.size(); j++) {
386 if (!strcmp(lang[j]->getStringValue(), language)) {
395 d_node = intl->getChild("locale");
399 // Check for localized font
400 SGPropertyNode *font_n = c_node->getNode("font", true);
401 if ( !strcmp(font_n->getStringValue(), "") )
402 font_n->setStringValue(d_node->getStringValue("font", "typewriter.txf"));
406 // Load the default strings
408 SGPath d_path( globals->get_fg_root() );
410 const char *d_path_str = d_node->getStringValue("strings");
412 SG_LOG(SG_GENERAL, SG_ALERT, "Incorrect path in configuration file.");
416 d_path.append(d_path_str);
417 SG_LOG(SG_GENERAL, SG_INFO, "Reading localized strings from "
420 SGPropertyNode *strings = c_node->getNode("strings");
422 readProperties(d_path.str(), strings);
423 } catch (const sg_exception &e) {
424 SG_LOG(SG_GENERAL, SG_ALERT, "Unable to read the localized strings");
429 // Load the language specific strings
431 if (c_node != d_node) {
432 SGPath c_path( globals->get_fg_root() );
434 const char *c_path_str = c_node->getStringValue("strings");
436 SG_LOG(SG_GENERAL, SG_ALERT, "Incorrect path in configuration file.");
440 c_path.append(c_path_str);
441 SG_LOG(SG_GENERAL, SG_INFO, "Reading localized strings from "
445 readProperties(c_path.str(), strings);
446 } catch (const sg_exception &e) {
447 SG_LOG(SG_GENERAL, SG_ALERT, "Unable to read the localized strings");
457 // Initialize the localization routines
458 bool fgDetectLanguage() {
459 char *language = ::getenv("LANG");
461 if (language == NULL) {
462 SG_LOG(SG_GENERAL, SG_INFO, "Unable to detect the language" );
466 SGPropertyNode *locale = fgInitLocale(language);
468 SG_LOG(SG_GENERAL, SG_ALERT,
469 "No internationalization settings specified in preferences.xml" );
474 globals->set_locale( locale );
479 // Attempt to locate and parse the various non-XML config files in order
480 // from least precidence to greatest precidence
482 do_options (int argc, char ** argv)
484 // Check for $fg_root/system.fgfsrc
485 SGPath config( globals->get_fg_root() );
486 config.append( "system.fgfsrc" );
487 fgParseOptions(config.str());
489 #if defined( unix ) || defined( __CYGWIN__ )
490 config.concat( "." );
491 config.concat( hostname );
492 fgParseOptions(config.str());
495 // Check for ~/.fgfsrc
496 if ( homedir != NULL ) {
497 config.set( homedir );
498 config.append( ".fgfsrc" );
499 fgParseOptions(config.str());
502 #if defined( unix ) || defined( __CYGWIN__ )
503 // Check for ~/.fgfsrc.hostname
504 config.concat( "." );
505 config.concat( hostname );
506 fgParseOptions(config.str());
509 // Parse remaining command line options
510 // These will override anything specified in a config file
511 fgParseArgs(argc, argv);
515 static string fgFindAircraftPath( const SGPath &path, const string &aircraft ) {
517 ulDir *dirp = ulOpenDir(path.str().c_str());
519 cerr << "Unable to open aircraft directory '" << path.str() << '\'' << endl;
523 while ((dire = ulReadDir(dirp)) != NULL) {
525 if ( strcmp("CVS", dire->d_name) && strcmp(".", dire->d_name)
526 && strcmp("..", dire->d_name) && strcmp("AI", dire->d_name))
529 next.append(dire->d_name);
531 string result = fgFindAircraftPath( next, aircraft );
532 if ( ! result.empty() ) {
536 } else if ( !strcmp(dire->d_name, aircraft.c_str()) ) {
547 // Read in configuration (file and command line)
548 bool fgInitConfig ( int argc, char **argv ) {
550 // First, set some sane default values
553 // Read global preferences from $FG_ROOT/preferences.xml
554 SG_LOG(SG_INPUT, SG_INFO, "Reading global preferences");
555 fgLoadProps("preferences.xml", globals->get_props());
556 SG_LOG(SG_INPUT, SG_INFO, "Finished Reading global preferences");
558 // Detect the required language as early as possible
559 if ( !fgDetectLanguage() ) {
563 // Scan user config files and command line for a specified aircraft.
564 fgInitFGAircraft(argc, argv);
566 string aircraft = fgGetString( "/sim/aircraft", "" );
567 if ( aircraft.size() > 0 ) {
568 SGPath aircraft_search( globals->get_fg_root() );
569 aircraft_search.append( "Aircraft" );
571 string aircraft_set = aircraft + "-set.xml";
573 string result = fgFindAircraftPath( aircraft_search, aircraft_set );
574 if ( !result.empty() ) {
575 fgSetString( "/sim/aircraft-dir", result.c_str() );
576 SGPath full_name( result );
577 full_name.append( aircraft_set );
579 SG_LOG(SG_INPUT, SG_INFO, "Reading default aircraft: " << aircraft
580 << " from " << full_name.str());
582 readProperties( full_name.str(), globals->get_props() );
583 } catch ( const sg_exception &e ) {
584 string message = "Error reading default aircraft: ";
585 message += e.getFormattedMessage();
586 SG_LOG(SG_INPUT, SG_ALERT, message);
590 SG_LOG( SG_INPUT, SG_ALERT, "Cannot find specified aircraft: "
596 SG_LOG( SG_INPUT, SG_ALERT, "No default aircraft specified" );
600 char *envp = ::getenv( "APPDATA" );
602 SGPath config( envp );
603 config.append( "flightgear.org" );
605 if ( homedir != NULL ) {
606 SGPath config( homedir );
607 config.append( ".fgfs" );
609 fgSetString("/sim/fg-home", config.c_str());
610 config.append( "autosave.xml" );
611 SG_LOG(SG_INPUT, SG_INFO, "Reading user settings from autosave.xml");
613 fgLoadProps(config.str().c_str(), globals->get_props(), false,
614 SGPropertyNode::USERARCHIVE);
616 SG_LOG(SG_INPUT, SG_BULK, "First time reading user settings");
618 SG_LOG(SG_INPUT, SG_BULK, "Finished Reading user settings");
621 // parse options after loading aircraft to ensure any user
622 // overrides of defaults are honored.
623 do_options(argc, argv);
634 // This function is never used, but maybe useful in the future ???
637 // Preset lon/lat given an airport id
638 static bool fgSetPosFromAirportID( const string& id ) {
642 SG_LOG( SG_GENERAL, SG_INFO,
643 "Attempting to set starting position from airport code " << id );
645 if ( fgFindAirportID( id, &a ) ) {
647 fgSetDouble("/sim/presets/longitude-deg", a->longitude );
648 fgSetDouble("/sim/presets/latitude-deg", a->latitude );
650 // other code depends on the actual postition being set so set
652 fgSetDouble("/position/longitude-deg", a->longitude );
653 fgSetDouble("/position/latitude-deg", a->latitude );
655 SG_LOG( SG_GENERAL, SG_INFO,
656 "Position for " << id << " is (" << a->longitude
657 << ", " << a->latitude << ")" );
667 // Set current tower position lon/lat given an airport id
668 static bool fgSetTowerPosFromAirportID( const string& id, double hdg ) {
670 // tower height hard coded for now...
671 float towerheight=50.0f;
673 // make a little off the heading for 1 runway airports...
674 float fudge_lon = fabs(sin(hdg)) * .003f;
675 float fudge_lat = .003f - fudge_lon;
677 const FGAirport *a = fgFindAirportID( id);
679 fgSetDouble("/sim/tower/longitude-deg", a->getLongitude() + fudge_lon);
680 fgSetDouble("/sim/tower/latitude-deg", a->getLatitude() + fudge_lat);
681 fgSetDouble("/sim/tower/altitude-ft", a->getElevation() + towerheight);
689 struct FGTowerLocationListener : SGPropertyChangeListener {
690 void valueChanged(SGPropertyNode* node) {
691 const double hdg = fgGetDouble( "/orientation/heading-deg", 0);
692 const string id(node->getStringValue());
693 fgSetTowerPosFromAirportID(id, hdg);
697 static void fgInitTowerLocationListener() {
698 fgGetNode("/sim/tower/airport-id", true)
699 ->addChangeListener( new FGTowerLocationListener() );
702 // Set current_options lon/lat given an airport id and heading (degrees)
703 static bool fgSetPosFromAirportIDandHdg( const string& id, double tgt_hdg ) {
707 // set initial position from runway and heading
709 SG_LOG( SG_GENERAL, SG_INFO,
710 "Attempting to set starting position from airport code "
711 << id << " heading " << tgt_hdg );
713 if ( ! globals->get_runways()->search( id, (int)tgt_hdg, &r ) ) {
714 SG_LOG( SG_GENERAL, SG_ALERT,
715 "Failed to find a good runway for " << id << '\n' );
722 double lat2, lon2, az2;
723 double heading = r._heading;
724 double azimuth = heading + 180.0;
725 while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
727 SG_LOG( SG_GENERAL, SG_INFO,
728 "runway = " << r._lon << ", " << r._lat
729 << " length = " << r._length * SG_FEET_TO_METER
730 << " heading = " << azimuth );
732 geo_direct_wgs_84 ( 0, r._lat, r._lon, azimuth,
733 r._length * SG_FEET_TO_METER * 0.5 - 5.0,
734 &lat2, &lon2, &az2 );
736 if ( fabs( fgGetDouble("/sim/presets/offset-distance") ) > SG_EPSILON ) {
738 double odist = fgGetDouble("/sim/presets/offset-distance");
739 odist *= SG_NM_TO_METER;
740 double oaz = azimuth;
741 if ( fabs(fgGetDouble("/sim/presets/offset-azimuth")) > SG_EPSILON ) {
742 oaz = fgGetDouble("/sim/presets/offset-azimuth") + 180;
745 while ( oaz >= 360.0 ) { oaz -= 360.0; }
746 geo_direct_wgs_84 ( 0, lat2, lon2, oaz, odist, &olat, &olon, &az2 );
752 fgSetDouble("/sim/presets/longitude-deg", lon2 );
753 fgSetDouble("/sim/presets/latitude-deg", lat2 );
754 fgSetDouble("/sim/presets/heading-deg", heading );
756 // other code depends on the actual values being set ...
757 fgSetDouble("/position/longitude-deg", lon2 );
758 fgSetDouble("/position/latitude-deg", lat2 );
759 fgSetDouble("/orientation/heading-deg", heading );
761 SG_LOG( SG_GENERAL, SG_INFO,
762 "Position for " << id << " is ("
764 << lat2 << ") new heading is "
771 // Set current_options lon/lat given an airport id and runway number
772 static bool fgSetPosFromAirportIDandRwy( const string& id, const string& rwy ) {
776 // set initial position from airport and runway number
778 SG_LOG( SG_GENERAL, SG_INFO,
779 "Attempting to set starting position for "
780 << id << ":" << rwy );
782 if ( ! globals->get_runways()->search( id, rwy, &r ) ) {
783 SG_LOG( SG_GENERAL, SG_ALERT,
784 "Failed to find runway " << rwy <<
785 " at airport " << id );
792 double lat2, lon2, az2;
793 double heading = r._heading;
794 double azimuth = heading + 180.0;
795 while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
797 SG_LOG( SG_GENERAL, SG_INFO,
798 "runway = " << r._lon << ", " << r._lat
799 << " length = " << r._length * SG_FEET_TO_METER
800 << " heading = " << azimuth );
802 geo_direct_wgs_84 ( 0, r._lat, r._lon,
804 r._length * SG_FEET_TO_METER * 0.5 - 5.0,
805 &lat2, &lon2, &az2 );
807 if ( fabs( fgGetDouble("/sim/presets/offset-distance") ) > SG_EPSILON )
810 double odist = fgGetDouble("/sim/presets/offset-distance");
811 odist *= SG_NM_TO_METER;
812 double oaz = azimuth;
813 if ( fabs(fgGetDouble("/sim/presets/offset-azimuth")) > SG_EPSILON )
815 oaz = fgGetDouble("/sim/presets/offset-azimuth") + 180;
816 heading = fgGetDouble("/sim/presets/heading-deg");
818 while ( oaz >= 360.0 ) { oaz -= 360.0; }
819 geo_direct_wgs_84 ( 0, lat2, lon2, oaz, odist, &olat, &olon, &az2 );
825 fgSetDouble("/sim/presets/longitude-deg", lon2 );
826 fgSetDouble("/sim/presets/latitude-deg", lat2 );
827 fgSetDouble("/sim/presets/heading-deg", heading );
829 // other code depends on the actual values being set ...
830 fgSetDouble("/position/longitude-deg", lon2 );
831 fgSetDouble("/position/latitude-deg", lat2 );
832 fgSetDouble("/orientation/heading-deg", heading );
834 SG_LOG( SG_GENERAL, SG_INFO,
835 "Position for " << id << " is ("
837 << lat2 << ") new heading is "
844 static void fgSetDistOrAltFromGlideSlope() {
845 // cout << "fgSetDistOrAltFromGlideSlope()" << endl;
846 string apt_id = fgGetString("/sim/presets/airport-id");
847 double gs = fgGetDouble("/sim/presets/glideslope-deg")
848 * SG_DEGREES_TO_RADIANS ;
849 double od = fgGetDouble("/sim/presets/offset-distance");
850 double alt = fgGetDouble("/sim/presets/altitude-ft");
852 double apt_elev = 0.0;
853 if ( ! apt_id.empty() ) {
854 apt_elev = fgGetAirportElev( apt_id );
855 if ( apt_elev < -9990.0 ) {
862 if( fabs(gs) > 0.01 && fabs(od) > 0.1 && alt < -9990 ) {
863 // set altitude from glideslope and offset-distance
864 od *= SG_NM_TO_METER * SG_METER_TO_FEET;
865 alt = fabs(od*tan(gs)) + apt_elev;
866 fgSetDouble("/sim/presets/altitude-ft", alt);
867 fgSetBool("/sim/presets/onground", false);
868 SG_LOG( SG_GENERAL, SG_INFO, "Calculated altitude as: "
870 } else if( fabs(gs) > 0.01 && alt > 0 && fabs(od) < 0.1) {
871 // set offset-distance from glideslope and altitude
872 od = (alt - apt_elev) / tan(gs);
873 od *= -1*SG_FEET_TO_METER * SG_METER_TO_NM;
874 fgSetDouble("/sim/presets/offset-distance", od);
875 fgSetBool("/sim/presets/onground", false);
876 SG_LOG( SG_GENERAL, SG_INFO, "Calculated offset distance as: "
878 } else if( fabs(gs) > 0.01 ) {
879 SG_LOG( SG_GENERAL, SG_ALERT,
880 "Glideslope given but not altitude or offset-distance." );
881 SG_LOG( SG_GENERAL, SG_ALERT, "Resetting glideslope to zero" );
882 fgSetDouble("/sim/presets/glideslope-deg", 0);
883 fgSetBool("/sim/presets/onground", true);
888 // Set current_options lon/lat given an airport id and heading (degrees)
889 static bool fgSetPosFromNAV( const string& id, const double& freq ) {
891 = globals->get_navlist()->findByIdentAndFreq( id.c_str(), freq );
893 // set initial position from runway and heading
895 SG_LOG( SG_GENERAL, SG_INFO, "Attempting to set starting position for "
896 << id << ":" << freq );
898 double lon = nav->get_lon();
899 double lat = nav->get_lat();
901 if ( fabs( fgGetDouble("/sim/presets/offset-distance") ) > SG_EPSILON )
903 double odist = fgGetDouble("/sim/presets/offset-distance")
905 double oaz = fabs(fgGetDouble("/sim/presets/offset-azimuth"))
907 while ( oaz >= 360.0 ) { oaz -= 360.0; }
908 double olat, olon, az2;
909 geo_direct_wgs_84 ( 0, lat, lon, oaz, odist, &olat, &olon, &az2 );
916 fgSetDouble("/sim/presets/longitude-deg", lon );
917 fgSetDouble("/sim/presets/latitude-deg", lat );
919 // other code depends on the actual values being set ...
920 fgSetDouble("/position/longitude-deg", lon );
921 fgSetDouble("/position/latitude-deg", lat );
922 fgSetDouble("/orientation/heading-deg",
923 fgGetDouble("/sim/presets/heading-deg") );
925 SG_LOG( SG_GENERAL, SG_INFO,
926 "Position for " << id << ":" << freq << " is ("
927 << lon << ", "<< lat << ")" );
931 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to locate NAV = "
932 << id << ":" << freq );
937 // Set current_options lon/lat given an aircraft carrier id
938 static bool fgSetPosFromCarrier( const string& carrier, const string& posid ) {
940 // set initial position from runway and heading
944 if (FGAIManager::getStartPosition(carrier, posid, geodPos, heading, uvw)) {
945 double lon = geodPos.lon() * SGD_RADIANS_TO_DEGREES;
946 double lat = geodPos.lat() * SGD_RADIANS_TO_DEGREES;
947 double alt = geodPos.elev() * SG_METER_TO_FEET;
949 SG_LOG( SG_GENERAL, SG_INFO, "Attempting to set starting position for "
950 << carrier << " at lat = " << lat << ", lon = " << lon
951 << ", alt = " << alt << ", heading = " << heading);
953 fgSetDouble("/sim/presets/longitude-deg", lon);
954 fgSetDouble("/sim/presets/latitude-deg", lat);
955 fgSetDouble("/sim/presets/altitude-ft", alt);
956 fgSetDouble("/sim/presets/heading-deg", heading);
957 fgSetDouble("/position/longitude-deg", lon);
958 fgSetDouble("/position/latitude-deg", lat);
959 fgSetDouble("/position/altitude-ft", alt);
960 fgSetDouble("/orientation/heading-deg", heading);
962 fgSetString("/sim/presets/speed-set", "UVW");
963 fgSetDouble("/velocities/uBody-fps", uvw[0]);
964 fgSetDouble("/velocities/vBody-fps", uvw[1]);
965 fgSetDouble("/velocities/wBody-fps", uvw[2]);
966 fgSetDouble("/sim/presets/uBody-fps", uvw[0]);
967 fgSetDouble("/sim/presets/vBody-fps", uvw[1]);
968 fgSetDouble("/sim/presets/wBody-fps", uvw[2]);
970 fgSetBool("/sim/presets/onground", true);
974 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to locate aircraft carrier = "
980 // Set current_options lon/lat given an airport id and heading (degrees)
981 static bool fgSetPosFromFix( const string& id ) {
984 // set initial position from runway and heading
985 if ( globals->get_fixlist()->query( id.c_str(), &fix ) ) {
986 SG_LOG( SG_GENERAL, SG_INFO, "Attempting to set starting position for "
989 double lon = fix.get_lon();
990 double lat = fix.get_lat();
992 if ( fabs( fgGetDouble("/sim/presets/offset-distance") ) > SG_EPSILON )
994 double odist = fgGetDouble("/sim/presets/offset-distance")
996 double oaz = fabs(fgGetDouble("/sim/presets/offset-azimuth"))
998 while ( oaz >= 360.0 ) { oaz -= 360.0; }
999 double olat, olon, az2;
1000 geo_direct_wgs_84 ( 0, lat, lon, oaz, odist, &olat, &olon, &az2 );
1007 fgSetDouble("/sim/presets/longitude-deg", lon );
1008 fgSetDouble("/sim/presets/latitude-deg", lat );
1010 // other code depends on the actual values being set ...
1011 fgSetDouble("/position/longitude-deg", lon );
1012 fgSetDouble("/position/latitude-deg", lat );
1013 fgSetDouble("/orientation/heading-deg",
1014 fgGetDouble("/sim/presets/heading-deg") );
1016 SG_LOG( SG_GENERAL, SG_INFO,
1017 "Position for " << id << " is ("
1018 << lon << ", "<< lat << ")" );
1022 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to locate NAV = "
1028 static void parseWaypoints() {
1029 string_list *waypoints = globals->get_initial_waypoints();
1031 vector<string>::iterator i;
1032 for (i = waypoints->begin();
1033 i != waypoints->end();
1038 // Now were done using the way points we can deallocate the
1040 while (waypoints->begin() != waypoints->end()) {
1041 waypoints->pop_back();
1044 globals->set_initial_waypoints(0);
1053 * Initialize vor/ndb/ils/fix list management and query systems (as
1054 * well as simple airport db list)
1059 SG_LOG(SG_GENERAL, SG_INFO, "Loading Airport Database ...");
1061 SGPath aptdb( globals->get_fg_root() );
1062 aptdb.append( "Airports/apt.dat" );
1064 SGPath p_metar( globals->get_fg_root() );
1065 p_metar.append( "Airports/metar.dat" );
1067 FGAirportList *airports = new FGAirportList();
1068 globals->set_airports( airports );
1069 FGRunwayList *runways = new FGRunwayList();
1070 globals->set_runways( runways );
1072 fgAirportDBLoad( airports, runways, aptdb.str(), p_metar.str() );
1074 FGNavList *navlist = new FGNavList;
1075 FGNavList *loclist = new FGNavList;
1076 FGNavList *gslist = new FGNavList;
1077 FGNavList *dmelist = new FGNavList;
1078 FGNavList *mkrlist = new FGNavList;
1079 FGNavList *tacanlist = new FGNavList;
1080 FGNavList *carrierlist = new FGNavList;
1081 FGTACANList *channellist = new FGTACANList;
1083 globals->set_navlist( navlist );
1084 globals->set_loclist( loclist );
1085 globals->set_gslist( gslist );
1086 globals->set_dmelist( dmelist );
1087 globals->set_mkrlist( mkrlist );
1088 globals->set_tacanlist( tacanlist );
1089 globals->set_carrierlist( carrierlist );
1090 globals->set_channellist( channellist );
1092 if ( !fgNavDBInit(airports, navlist, loclist, gslist, dmelist, mkrlist, tacanlist, carrierlist, channellist) ) {
1093 SG_LOG( SG_GENERAL, SG_ALERT,
1094 "Problems loading one or more navigational database" );
1097 if ( fgGetBool("/sim/navdb/localizers/auto-align", true) ) {
1098 // align all the localizers with their corresponding runways
1099 // since data sources are good for cockpit navigation
1100 // purposes, but not always to the error tolerances needed to
1101 // exactly place these items.
1103 = fgGetDouble( "/sim/navdb/localizers/auto-align-threshold-deg",
1105 fgNavDBAlignLOCwithRunway( runways, loclist, threshold );
1108 SG_LOG(SG_GENERAL, SG_INFO, " Fixes");
1109 SGPath p_fix( globals->get_fg_root() );
1110 p_fix.append( "Navaids/fix.dat" );
1111 FGFixList *fixlist = new FGFixList;
1112 fixlist->init( p_fix );
1113 globals->set_fixlist( fixlist );
1119 // Set the initial position based on presets (or defaults)
1120 bool fgInitPosition() {
1121 // cout << "fgInitPosition()" << endl;
1122 double gs = fgGetDouble("/sim/presets/glideslope-deg")
1123 * SG_DEGREES_TO_RADIANS ;
1124 double od = fgGetDouble("/sim/presets/offset-distance");
1125 double alt = fgGetDouble("/sim/presets/altitude-ft");
1127 bool set_pos = false;
1129 // If glideslope is specified, then calculate offset-distance or
1130 // altitude relative to glide slope if either of those was not
1132 if ( fabs( gs ) > 0.01 ) {
1133 fgSetDistOrAltFromGlideSlope();
1137 // If we have an explicit, in-range lon/lat, don't change it, just use it.
1138 // If not, check for an airport-id and use that.
1139 // If not, default to the middle of the KSFO field.
1140 // The default values for lon/lat are deliberately out of range
1141 // so that the airport-id can take effect; valid lon/lat will
1142 // override airport-id, however.
1143 double lon_deg = fgGetDouble("/sim/presets/longitude-deg");
1144 double lat_deg = fgGetDouble("/sim/presets/latitude-deg");
1145 if ( lon_deg >= -180.0 && lon_deg <= 180.0
1146 && lat_deg >= -90.0 && lat_deg <= 90.0 )
1151 string apt = fgGetString("/sim/presets/airport-id");
1152 string rwy_no = fgGetString("/sim/presets/runway");
1153 double hdg = fgGetDouble("/sim/presets/heading-deg");
1154 string vor = fgGetString("/sim/presets/vor-id");
1155 double vor_freq = fgGetDouble("/sim/presets/vor-freq");
1156 string ndb = fgGetString("/sim/presets/ndb-id");
1157 double ndb_freq = fgGetDouble("/sim/presets/ndb-freq");
1158 string carrier = fgGetString("/sim/presets/carrier");
1159 string parkpos = fgGetString("/sim/presets/parkpos");
1160 string fix = fgGetString("/sim/presets/fix");
1162 fgSetDouble( "/orientation/heading-deg", hdg );
1163 fgInitTowerLocationListener();
1165 if ( !set_pos && !apt.empty() && !rwy_no.empty() ) {
1166 // An airport + runway is requested
1167 if ( fgSetPosFromAirportIDandRwy( apt, rwy_no ) ) {
1168 // set tower position (a little off the heading for single
1170 fgSetString("/sim/tower/airport-id", apt.c_str());
1175 if ( !set_pos && !apt.empty() ) {
1176 // An airport is requested (find runway closest to hdg)
1177 if ( fgSetPosFromAirportIDandHdg( apt, hdg ) ) {
1178 // set tower position (a little off the heading for single
1180 fgSetString("/sim/tower/airport-id", apt.c_str());
1185 if ( !set_pos && !vor.empty() ) {
1186 // a VOR is requested
1187 if ( fgSetPosFromNAV( vor, vor_freq ) ) {
1192 if ( !set_pos && !ndb.empty() ) {
1193 // an NDB is requested
1194 if ( fgSetPosFromNAV( ndb, ndb_freq ) ) {
1199 if ( !set_pos && !carrier.empty() ) {
1200 // an aircraft carrier is requested
1201 if ( fgSetPosFromCarrier( carrier, parkpos ) ) {
1206 if ( !set_pos && !fix.empty() ) {
1207 // a Fix is requested
1208 if ( fgSetPosFromFix( fix ) ) {
1214 // No lon/lat specified, no airport specified, default to
1215 // middle of KSFO field.
1216 fgSetDouble("/sim/presets/longitude-deg", -122.374843);
1217 fgSetDouble("/sim/presets/latitude-deg", 37.619002);
1220 fgSetDouble( "/position/longitude-deg",
1221 fgGetDouble("/sim/presets/longitude-deg") );
1222 fgSetDouble( "/position/latitude-deg",
1223 fgGetDouble("/sim/presets/latitude-deg") );
1225 // determine if this should be an on-ground or in-air start
1226 if ((fabs(gs) > 0.01 || fabs(od) > 0.1 || alt > 0.1) && carrier.empty()) {
1227 fgSetBool("/sim/presets/onground", false);
1229 fgSetBool("/sim/presets/onground", true);
1236 // General house keeping initializations
1237 bool fgInitGeneral() {
1240 #if defined(FX) && defined(XMESA)
1241 char *mesa_win_state;
1244 SG_LOG( SG_GENERAL, SG_INFO, "General Initialization" );
1245 SG_LOG( SG_GENERAL, SG_INFO, "======= ==============" );
1247 root = globals->get_fg_root();
1248 if ( ! root.length() ) {
1249 // No root path set? Then bail ...
1250 SG_LOG( SG_GENERAL, SG_ALERT,
1251 "Cannot continue without a path to the base package "
1252 << "being defined." );
1255 SG_LOG( SG_GENERAL, SG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
1257 #if defined(FX) && defined(XMESA)
1258 // initialize full screen flag
1259 globals->set_fullscreen(false);
1260 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
1261 // Test for the MESA_GLX_FX env variable
1262 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
1263 // test if we are fullscreen mesa/glide
1264 if ( (mesa_win_state[0] == 'f') ||
1265 (mesa_win_state[0] == 'F') ) {
1266 globals->set_fullscreen(true);
1276 // Initialize the flight model subsystem. This just creates the
1277 // object. The actual fdm initialization is delayed until we get a
1278 // proper scenery elevation hit. This is checked for in main.cxx
1282 if ( cur_fdm_state ) {
1283 delete cur_fdm_state;
1287 double dt = 1.0 / fgGetInt("/sim/model-hz");
1288 string model = fgGetString("/sim/flight-model");
1291 if ( model == "larcsim" ) {
1292 cur_fdm_state = new FGLaRCsim( dt );
1293 } else if ( model == "jsb" ) {
1294 cur_fdm_state = new FGJSBsim( dt );
1296 } else if ( model == "ada" ) {
1297 cur_fdm_state = new FGADA( dt );
1298 } else if ( model == "acms" ) {
1299 cur_fdm_state = new FGACMS( dt );
1301 } else if ( model == "balloon" ) {
1302 cur_fdm_state = new FGBalloonSim( dt );
1303 } else if ( model == "magic" ) {
1304 cur_fdm_state = new FGMagicCarpet( dt );
1305 } else if ( model == "ufo" ) {
1306 cur_fdm_state = new FGUFO( dt );
1307 } else if ( model == "external" ) {
1308 // external is a synonym for "--fdm=null" and is
1309 // maintained here for backwards compatibility
1310 cur_fdm_state = new FGNullFDM( dt );
1311 } else if ( model.find("network") == 0 ) {
1312 string host = "localhost";
1316 string net_options = model.substr(8);
1317 string::size_type begin, end;
1320 end = net_options.find( ",", begin );
1321 if ( end != string::npos ) {
1322 host = net_options.substr(begin, end - begin);
1326 end = net_options.find( ",", begin );
1327 if ( end != string::npos ) {
1328 port1 = atoi( net_options.substr(begin, end - begin).c_str() );
1332 end = net_options.find( ",", begin );
1333 if ( end != string::npos ) {
1334 port2 = atoi( net_options.substr(begin, end - begin).c_str() );
1338 end = net_options.find( ",", begin );
1339 if ( end != string::npos ) {
1340 port3 = atoi( net_options.substr(begin, end - begin).c_str() );
1343 cur_fdm_state = new FGExternalNet( dt, host, port1, port2, port3 );
1344 } else if ( model.find("pipe") == 0 ) {
1345 // /* old */ string pipe_path = model.substr(5);
1346 // /* old */ cur_fdm_state = new FGExternalPipe( dt, pipe_path );
1347 string pipe_path = "";
1348 string pipe_protocol = "";
1349 string pipe_options = model.substr(5);
1350 string::size_type begin, end;
1353 end = pipe_options.find( ",", begin );
1354 if ( end != string::npos ) {
1355 pipe_path = pipe_options.substr(begin, end - begin);
1358 // protocol (last option)
1359 pipe_protocol = pipe_options.substr(begin);
1360 cur_fdm_state = new FGExternalPipe( dt, pipe_path, pipe_protocol );
1361 } else if ( model == "null" ) {
1362 cur_fdm_state = new FGNullFDM( dt );
1363 } else if ( model == "yasim" ) {
1364 cur_fdm_state = new YASim( dt );
1366 SG_LOG(SG_GENERAL, SG_ALERT,
1367 "Unrecognized flight model '" << model
1368 << "', cannot init flight dynamics model.");
1372 SG_LOG(SG_GENERAL, SG_ALERT, "FlightGear aborting\n\n");
1377 static void printMat(const sgVec4 *mat, char *name="")
1380 SG_LOG(SG_GENERAL, SG_BULK, name );
1381 for(i=0; i<4; i++) {
1382 SG_LOG(SG_GENERAL, SG_BULK, " " << mat[i][0] << " " << mat[i][1]
1383 << " " << mat[i][2] << " " << mat[i][3] );
1387 // Initialize view parameters
1389 // force update of model so that viewer can get some data...
1390 globals->get_aircraft_model()->update(0);
1391 // run update for current view so that data is current...
1392 globals->get_viewmgr()->update(0);
1394 printMat(globals->get_current_view()->get_VIEW(),"VIEW");
1395 printMat(globals->get_current_view()->get_UP(),"UP");
1396 // printMat(globals->get_current_view()->get_LOCAL(),"LOCAL");
1401 SGTime *fgInitTime() {
1403 static const SGPropertyNode *longitude
1404 = fgGetNode("/position/longitude-deg");
1405 static const SGPropertyNode *latitude
1406 = fgGetNode("/position/latitude-deg");
1407 static const SGPropertyNode *cur_time_override
1408 = fgGetNode("/sim/time/cur-time-override", true);
1410 SGPath zone( globals->get_fg_root() );
1411 zone.append( "Timezone" );
1412 SGTime *t = new SGTime( longitude->getDoubleValue()
1413 * SGD_DEGREES_TO_RADIANS,
1414 latitude->getDoubleValue()
1415 * SGD_DEGREES_TO_RADIANS,
1417 cur_time_override->getLongValue() );
1419 globals->set_warp_delta( 0 );
1421 t->update( 0.0, 0.0,
1422 cur_time_override->getLongValue(),
1423 globals->get_warp() );
1429 // set up a time offset (aka warp) if one is specified
1430 void fgInitTimeOffset() {
1431 static const SGPropertyNode *longitude
1432 = fgGetNode("/position/longitude-deg");
1433 static const SGPropertyNode *latitude
1434 = fgGetNode("/position/latitude-deg");
1435 static const SGPropertyNode *cur_time_override
1436 = fgGetNode("/sim/time/cur-time-override", true);
1438 // Handle potential user specified time offsets
1439 int orig_warp = globals->get_warp();
1440 SGTime *t = globals->get_time_params();
1441 time_t cur_time = t->get_cur_time();
1442 time_t currGMT = sgTimeGetGMT( gmtime(&cur_time) );
1443 time_t systemLocalTime = sgTimeGetGMT( localtime(&cur_time) );
1444 time_t aircraftLocalTime =
1445 sgTimeGetGMT( fgLocaltime(&cur_time, t->get_zonename() ) );
1447 // Okay, we now have several possible scenarios
1448 int offset = fgGetInt("/sim/startup/time-offset");
1449 string offset_type = fgGetString("/sim/startup/time-offset-type");
1452 if ( offset_type == "real" ) {
1454 } else if ( offset_type == "dawn" ) {
1455 warp = fgTimeSecondsUntilSunAngle( cur_time,
1456 longitude->getDoubleValue()
1457 * SGD_DEGREES_TO_RADIANS,
1458 latitude->getDoubleValue()
1459 * SGD_DEGREES_TO_RADIANS,
1461 } else if ( offset_type == "morning" ) {
1462 warp = fgTimeSecondsUntilSunAngle( cur_time,
1463 longitude->getDoubleValue()
1464 * SGD_DEGREES_TO_RADIANS,
1465 latitude->getDoubleValue()
1466 * SGD_DEGREES_TO_RADIANS,
1468 } else if ( offset_type == "noon" ) {
1469 warp = fgTimeSecondsUntilSunAngle( cur_time,
1470 longitude->getDoubleValue()
1471 * SGD_DEGREES_TO_RADIANS,
1472 latitude->getDoubleValue()
1473 * SGD_DEGREES_TO_RADIANS,
1475 } else if ( offset_type == "afternoon" ) {
1476 warp = fgTimeSecondsUntilSunAngle( cur_time,
1477 longitude->getDoubleValue()
1478 * SGD_DEGREES_TO_RADIANS,
1479 latitude->getDoubleValue()
1480 * SGD_DEGREES_TO_RADIANS,
1482 } else if ( offset_type == "dusk" ) {
1483 warp = fgTimeSecondsUntilSunAngle( cur_time,
1484 longitude->getDoubleValue()
1485 * SGD_DEGREES_TO_RADIANS,
1486 latitude->getDoubleValue()
1487 * SGD_DEGREES_TO_RADIANS,
1489 } else if ( offset_type == "evening" ) {
1490 warp = fgTimeSecondsUntilSunAngle( cur_time,
1491 longitude->getDoubleValue()
1492 * SGD_DEGREES_TO_RADIANS,
1493 latitude->getDoubleValue()
1494 * SGD_DEGREES_TO_RADIANS,
1496 } else if ( offset_type == "midnight" ) {
1497 warp = fgTimeSecondsUntilSunAngle( cur_time,
1498 longitude->getDoubleValue()
1499 * SGD_DEGREES_TO_RADIANS,
1500 latitude->getDoubleValue()
1501 * SGD_DEGREES_TO_RADIANS,
1503 } else if ( offset_type == "system-offset" ) {
1505 } else if ( offset_type == "gmt-offset" ) {
1506 warp = offset - (currGMT - systemLocalTime);
1507 } else if ( offset_type == "latitude-offset" ) {
1508 warp = offset - (aircraftLocalTime - systemLocalTime);
1509 } else if ( offset_type == "system" ) {
1510 warp = offset - cur_time;
1511 } else if ( offset_type == "gmt" ) {
1512 warp = offset - currGMT;
1513 } else if ( offset_type == "latitude" ) {
1514 warp = offset - (aircraftLocalTime - systemLocalTime) - cur_time;
1516 SG_LOG( SG_GENERAL, SG_ALERT,
1517 "FG_TIME::Unsupported offset type " << offset_type );
1520 globals->set_warp( orig_warp + warp );
1521 t->update( longitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
1522 latitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
1523 cur_time_override->getLongValue(),
1524 globals->get_warp() );
1526 SG_LOG( SG_GENERAL, SG_INFO, "After fgInitTimeOffset(): warp = "
1527 << globals->get_warp() );
1531 // This is the top level init routine which calls all the other
1532 // initialization routines. If you are adding a subsystem to flight
1533 // gear, its initialization call should located in this routine.
1534 // Returns non-zero if a problem encountered.
1535 bool fgInitSubsystems() {
1536 // static const SGPropertyNode *longitude
1537 // = fgGetNode("/sim/presets/longitude-deg");
1538 // static const SGPropertyNode *latitude
1539 // = fgGetNode("/sim/presets/latitude-deg");
1540 // static const SGPropertyNode *altitude
1541 // = fgGetNode("/sim/presets/altitude-ft");
1542 SG_LOG( SG_GENERAL, SG_INFO, "Initialize Subsystems");
1543 SG_LOG( SG_GENERAL, SG_INFO, "========== ==========");
1545 ////////////////////////////////////////////////////////////////////
1546 // Initialize the event manager subsystem.
1547 ////////////////////////////////////////////////////////////////////
1549 globals->get_event_mgr()->init();
1550 globals->get_event_mgr()->setRealtimeProperty(fgGetNode("/sim/time/delta-realtime-sec", true));
1552 ////////////////////////////////////////////////////////////////////
1553 // Initialize the property interpolator subsystem
1554 ////////////////////////////////////////////////////////////////////
1555 globals->add_subsystem("interpolator", new SGInterpolator);
1558 ////////////////////////////////////////////////////////////////////
1559 // Add the FlightGear property utilities.
1560 ////////////////////////////////////////////////////////////////////
1561 globals->add_subsystem("properties", new FGProperties);
1563 ////////////////////////////////////////////////////////////////////
1564 // Initialize the material property subsystem.
1565 ////////////////////////////////////////////////////////////////////
1567 SGPath mpath( globals->get_fg_root() );
1568 mpath.append( "materials.xml" );
1569 string season = fgGetString("/sim/startup/season", "summer");
1570 if ( ! globals->get_matlib()->load(globals->get_fg_root(), mpath.str(), season.c_str()) ) {
1571 SG_LOG( SG_GENERAL, SG_ALERT, "Error loading material lib!" );
1576 ////////////////////////////////////////////////////////////////////
1577 // Initialize the scenery management subsystem.
1578 ////////////////////////////////////////////////////////////////////
1580 if ( globals->get_tile_mgr()->init() ) {
1581 // Load the local scenery data
1582 double visibility_meters = fgGetDouble("/environment/visibility-m");
1584 globals->get_tile_mgr()->update( visibility_meters );
1586 SG_LOG( SG_GENERAL, SG_ALERT, "Error in Tile Manager initialization!" );
1590 // cause refresh of viewer scenery timestamps every 15 seconds...
1591 globals->get_event_mgr()->addTask( "FGTileMgr::refresh_view_timestamps()",
1592 globals->get_tile_mgr(),
1593 &FGTileMgr::refresh_view_timestamps,
1597 ////////////////////////////////////////////////////////////////////
1598 // Initialize the flight model subsystem.
1599 ////////////////////////////////////////////////////////////////////
1603 // allocates structures so must happen before any of the flight
1604 // model or control parameters are set
1605 fgAircraftInit(); // In the future this might not be the case.
1608 ////////////////////////////////////////////////////////////////////
1609 // Initialize the XML Autopilot subsystem.
1610 ////////////////////////////////////////////////////////////////////
1612 globals->add_subsystem( "xml-autopilot", new FGXMLAutopilot );
1613 globals->add_subsystem( "route-manager", new FGRouteMgr );
1616 ////////////////////////////////////////////////////////////////////
1617 // Initialize the view manager subsystem.
1618 ////////////////////////////////////////////////////////////////////
1622 ////////////////////////////////////////////////////////////////////
1623 // Create and register the logger.
1624 ////////////////////////////////////////////////////////////////////
1626 globals->add_subsystem("logger", new FGLogger);
1628 ////////////////////////////////////////////////////////////////////
1629 // Create and register the XML GUI.
1630 ////////////////////////////////////////////////////////////////////
1632 globals->add_subsystem("gui", new NewGUI, SGSubsystemMgr::INIT);
1635 ////////////////////////////////////////////////////////////////////
1636 // Initialize the local time subsystem.
1637 ////////////////////////////////////////////////////////////////////
1639 // update the current timezone each 30 minutes
1640 globals->get_event_mgr()->addTask( "fgUpdateLocalTime()",
1641 &fgUpdateLocalTime, 30*60 );
1642 fgInitTimeOffset(); // the environment subsystem needs this
1645 ////////////////////////////////////////////////////////////////////
1646 // Initialize the weather subsystem.
1647 ////////////////////////////////////////////////////////////////////
1649 // Initialize the weather modeling subsystem
1650 globals->add_subsystem("environment", new FGEnvironmentMgr);
1653 ////////////////////////////////////////////////////////////////////
1654 // Initialize the lighting subsystem.
1655 ////////////////////////////////////////////////////////////////////
1657 globals->add_subsystem("lighting", new FGLight);
1659 //////////////////////////////////////////////////////////////////////
1660 // Initialize the 2D cloud subsystem.
1661 ////////////////////////////////////////////////////////////////////
1662 fgGetBool("/sim/rendering/bump-mapping", false);
1664 #ifdef ENABLE_AUDIO_SUPPORT
1665 ////////////////////////////////////////////////////////////////////
1666 // Initialize the sound subsystem.
1667 ////////////////////////////////////////////////////////////////////
1669 init_volume = fgGetFloat("/sim/sound/volume");
1670 fgSetFloat("/sim/sound/volume", 0.0f);
1671 globals->set_soundmgr(new SGSoundMgr);
1672 globals->get_soundmgr()->init();
1673 globals->get_soundmgr()->bind();
1676 ////////////////////////////////////////////////////////////////////
1677 // Initialize the sound-effects subsystem.
1678 ////////////////////////////////////////////////////////////////////
1680 globals->add_subsystem("fx", new FGFX);
1681 globals->add_subsystem("voice", new FGVoiceMgr);
1684 ////////////////////////////////////////////////////////////////////
1685 // Initialise ATC display system
1686 ////////////////////////////////////////////////////////////////////
1688 SG_LOG(SG_GENERAL, SG_INFO, " ATC Display");
1689 globals->set_ATC_display(new FGATCDisplay);
1690 globals->get_ATC_display()->init();
1692 ////////////////////////////////////////////////////////////////////
1693 // Initialise the ATC Manager
1694 ////////////////////////////////////////////////////////////////////
1696 SG_LOG(SG_GENERAL, SG_INFO, " ATC Manager");
1697 globals->set_ATC_mgr(new FGATCMgr);
1698 globals->get_ATC_mgr()->init();
1700 ////////////////////////////////////////////////////////////////////
1701 // Initialise the AI Manager
1702 ////////////////////////////////////////////////////////////////////
1704 SG_LOG(SG_GENERAL, SG_INFO, " AI Manager");
1705 globals->set_AI_mgr(new FGAIMgr);
1706 globals->get_AI_mgr()->init();
1708 ////////////////////////////////////////////////////////////////////
1709 // Initialise the AI Model Manager
1710 ////////////////////////////////////////////////////////////////////
1711 SG_LOG(SG_GENERAL, SG_INFO, " AI Model Manager");
1712 globals->add_subsystem("ai_model", new FGAIManager);
1713 globals->add_subsystem("submodel_mgr", new FGSubmodelMgr);
1716 // It's probably a good idea to initialize the top level traffic manager
1717 // After the AI and ATC systems have been initialized properly.
1718 // AI Traffic manager
1719 globals->add_subsystem("Traffic Manager", new FGTrafficManager);
1720 FGTrafficManager *dispatcher =
1721 (FGTrafficManager*) globals->get_subsystem("Traffic Manager");
1722 SGPath path = globals->get_fg_root();
1723 path.append("/Traffic/fgtraffic.xml");
1726 //globals->get_subsystem("Traffic Manager")->init();
1728 globals->add_subsystem("instrumentation", new FGInstrumentMgr);
1729 globals->add_subsystem("systems", new FGSystemMgr);
1733 ////////////////////////////////////////////////////////////////////
1734 // Initialize the cockpit subsystem
1735 ////////////////////////////////////////////////////////////////////
1736 if( fgCockpitInit( ¤t_aircraft )) {
1737 // Cockpit initialized ok.
1739 SG_LOG( SG_GENERAL, SG_ALERT, "Error in Cockpit initialization!" );
1744 ////////////////////////////////////////////////////////////////////
1745 // Initialize the autopilot subsystem.
1746 ////////////////////////////////////////////////////////////////////
1748 // FIXME: these should go in the
1749 // GUI initialization code, not here.
1750 // fgAPAdjustInit();
1751 NewTgtAirportInit();
1755 ////////////////////////////////////////////////////////////////////
1756 // Initialize I/O subsystem.
1757 ////////////////////////////////////////////////////////////////////
1759 globals->get_io()->init();
1760 globals->get_io()->bind();
1763 ////////////////////////////////////////////////////////////////////
1764 // Add a new 2D panel.
1765 ////////////////////////////////////////////////////////////////////
1767 string panel_path = fgGetString("/sim/panel/path",
1768 "Panels/Default/default.xml");
1770 globals->set_current_panel( fgReadPanel(panel_path) );
1771 if (globals->get_current_panel() == 0) {
1772 SG_LOG( SG_INPUT, SG_ALERT,
1773 "Error reading new panel from " << panel_path );
1775 SG_LOG( SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path );
1776 globals->get_current_panel()->init();
1777 globals->get_current_panel()->bind();
1781 ////////////////////////////////////////////////////////////////////
1782 // Initialize the controls subsystem.
1783 ////////////////////////////////////////////////////////////////////
1785 globals->get_controls()->init();
1786 globals->get_controls()->bind();
1789 ////////////////////////////////////////////////////////////////////
1790 // Initialize the input subsystem.
1791 ////////////////////////////////////////////////////////////////////
1793 globals->add_subsystem("input", new FGInput);
1796 ////////////////////////////////////////////////////////////////////
1797 // Initialize the replay subsystem
1798 ////////////////////////////////////////////////////////////////////
1799 globals->add_subsystem("replay", new FGReplay);
1801 ////////////////////////////////////////////////////////////////////
1802 // Bind and initialize subsystems.
1803 ////////////////////////////////////////////////////////////////////
1805 globals->get_subsystem_mgr()->bind();
1806 globals->get_subsystem_mgr()->init();
1808 #ifdef FG_MPLAYER_AS
1809 ////////////////////////////////////////////////////////////////////
1810 // Initialize multiplayer subsystem
1811 ////////////////////////////////////////////////////////////////////
1813 globals->set_multiplayer_mgr(new FGMultiplayMgr);
1814 globals->get_multiplayer_mgr()->init();
1817 ////////////////////////////////////////////////////////////////////////
1818 // Initialize the Nasal interpreter.
1819 // Do this last, so that the loaded scripts see initialized state
1820 ////////////////////////////////////////////////////////////////////////
1821 FGNasalSys* nasal = new FGNasalSys();
1822 globals->add_subsystem("nasal", nasal);
1825 ////////////////////////////////////////////////////////////////////
1826 // At this point we could try and parse the waypoint options
1827 ///////////////////////////////////////////////////////////////////
1830 // initialize methods that depend on other subsystems.
1831 globals->get_subsystem_mgr()->postinit();
1833 ////////////////////////////////////////////////////////////////////////
1834 // End of subsystem initialization.
1835 ////////////////////////////////////////////////////////////////////
1837 SG_LOG( SG_GENERAL, SG_INFO, endl);
1839 // Save the initial state for future
1841 globals->saveInitialState();
1847 void fgReInitSubsystems()
1849 // static const SGPropertyNode *longitude
1850 // = fgGetNode("/sim/presets/longitude-deg");
1851 // static const SGPropertyNode *latitude
1852 // = fgGetNode("/sim/presets/latitude-deg");
1853 static const SGPropertyNode *altitude
1854 = fgGetNode("/sim/presets/altitude-ft");
1855 static const SGPropertyNode *master_freeze
1856 = fgGetNode("/sim/freeze/master");
1858 SG_LOG( SG_GENERAL, SG_INFO,
1859 "fgReInitSubsystems(): /position/altitude = "
1860 << altitude->getDoubleValue() );
1862 bool freeze = master_freeze->getBoolValue();
1864 fgSetBool("/sim/freeze/master", true);
1866 fgSetBool("/sim/crashed", false);
1868 // Force reupdating the positions of the ai 3d models. They are used for
1869 // initializing ground level for the FDM.
1870 globals->get_subsystem("ai_model")->reinit();
1872 // Initialize the FDM
1875 // allocates structures so must happen before any of the flight
1876 // model or control parameters are set
1877 fgAircraftInit(); // In the future this might not be the case.
1879 // reload offsets from config defaults
1880 globals->get_viewmgr()->reinit();
1884 globals->get_controls()->reset_all();
1886 fgUpdateLocalTime();
1888 // re-init to proper time of day setting
1892 fgSetBool("/sim/freeze/master", false);
1894 fgSetBool("/sim/sceneryloaded",false);