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 - curt@infoplane.com
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 // For BC 5.01 this must be included before OpenGL includes.
30 #ifdef SG_MATH_EXCEPTION_CLASH
36 #include <string.h> // strcmp()
39 #if defined( unix ) || defined( __CYGWIN__ )
40 # include <unistd.h> // for gethostname()
43 // work around a stdc++ lib bug in some versions of linux, but doesn't
44 // seem to hurt to have this here for all versions of Linux.
46 # define _G_NO_EXTERN_TEMPLATES
49 #include <simgear/compiler.h>
53 #include <simgear/constants.h>
54 #include <simgear/debug/logstream.hxx>
55 #include <simgear/structure/exception.hxx>
56 #include <simgear/structure/event_mgr.hxx>
57 #include <simgear/math/point3d.hxx>
58 #include <simgear/math/polar3d.hxx>
59 #include <simgear/math/sg_geodesy.hxx>
60 #include <simgear/misc/sg_path.hxx>
61 #include <simgear/misc/interpolator.hxx>
62 #include <simgear/scene/material/matlib.hxx>
63 #ifdef FG_USE_CLOUDS_3D
64 # include <simgear/scene/sky/clouds3d/SkySceneLoader.hpp>
65 # include <simgear/scene/sky/clouds3d/SkyUtil.hpp>
66 # include <simgear/screen/texture.hxx>
68 #include <simgear/timing/sg_time.hxx>
69 #include <simgear/timing/lowleveltime.h>
71 #include <Aircraft/aircraft.hxx>
72 #include <FDM/UIUCModel/uiuc_aircraftdir.h>
73 #include <Airports/runways.hxx>
74 #include <Airports/simple.hxx>
75 #include <ATC/ATCdisplay.hxx>
76 #include <ATC/ATCmgr.hxx>
77 #include <ATC/AIMgr.hxx>
78 #include <Autopilot/auto_gui.hxx>
79 #include <Autopilot/route_mgr.hxx>
80 #include <Autopilot/xmlauto.hxx>
81 #include <Cockpit/cockpit.hxx>
82 #include <Cockpit/radiostack.hxx>
83 #include <Cockpit/panel.hxx>
84 #include <Cockpit/panel_io.hxx>
85 #include <FDM/ADA.hxx>
86 #include <FDM/Balloon.h>
87 #include <FDM/ExternalNet/ExternalNet.hxx>
88 #include <FDM/ExternalPipe/ExternalPipe.hxx>
89 #include <FDM/JSBSim/JSBSim.hxx>
90 #include <FDM/LaRCsim/LaRCsim.hxx>
91 #include <FDM/MagicCarpet.hxx>
92 #include <FDM/UFO.hxx>
93 #include <FDM/NullFDM.hxx>
94 #include <FDM/YASim/YASim.hxx>
95 #include <GUI/new_gui.hxx>
96 #include <Include/general.hxx>
97 #include <Input/input.hxx>
98 #include <Instrumentation/instrument_mgr.hxx>
99 #include <Model/acmodel.hxx>
100 #include <AIModel/AIManager.hxx>
101 #include <Navaids/fixlist.hxx>
102 #include <Navaids/ilslist.hxx>
103 #include <Navaids/mkrbeacons.hxx>
104 #include <Navaids/navlist.hxx>
105 #include <Replay/replay.hxx>
106 #include <Scenery/scenery.hxx>
107 #include <Scenery/tilemgr.hxx>
108 #include <Scripting/NasalSys.hxx>
109 #include <Sound/fg_fx.hxx>
110 #include <Systems/system_mgr.hxx>
111 #include <Time/light.hxx>
112 #include <Time/moonpos.hxx>
113 #include <Time/sunpos.hxx>
114 #include <Time/sunsolver.hxx>
115 #include <Time/tmp.hxx>
118 #include <MultiPlayer/multiplaytxmgr.hxx>
119 #include <MultiPlayer/multiplayrxmgr.hxx>
123 # include <WeatherCM/FGLocalWeatherDatabase.h>
125 # include <Environment/environment_mgr.hxx>
128 #include "fg_init.hxx"
130 #include "fg_commands.hxx"
131 #include "fg_props.hxx"
132 #include "options.hxx"
133 #include "globals.hxx"
134 #include "logger.hxx"
135 #include "viewmgr.hxx"
137 #if defined(FX) && defined(XMESA)
138 #include <GL/xmesa.h>
141 SG_USING_STD(string);
146 extern const char *default_root;
148 #ifdef FG_USE_CLOUDS_3D
149 SkySceneLoader *sgCloud3d;
153 // Scan the command line options for the specified option and return
155 static string fgScanForOption( const string& option, int argc, char **argv ) {
158 SG_LOG(SG_GENERAL, SG_INFO, "Scanning command line for: " << option );
160 int len = option.length();
163 SG_LOG( SG_GENERAL, SG_DEBUG, "argv[" << i << "] = " << argv[i] );
165 string arg = argv[i];
166 if ( arg.find( option ) == 0 ) {
167 return arg.substr( len );
177 // Scan the user config files for the specified option and return
179 static string fgScanForOption( const string& option, const string& path ) {
180 sg_gzifstream in( path );
181 if ( !in.is_open() ) {
185 SG_LOG( SG_GENERAL, SG_INFO, "Scanning " << path << " for: " << option );
187 int len = option.length();
191 while ( ! in.eof() ) {
194 while ( in.get(c) && c != '\0' ) {
199 #if defined( macintosh )
200 getline( in, line, '\r' );
202 getline( in, line, '\n' );
205 // catch extraneous (DOS) line ending character
206 if ( line[line.length() - 1] < 32 ) {
207 line = line.substr( 0, line.length()-1 );
210 if ( line.find( option ) == 0 ) {
211 return line.substr( len );
221 // Read in configuration (files and command line options) but only set
223 bool fgInitFGRoot ( int argc, char **argv ) {
227 // First parse command line options looking for --fg-root=, this
228 // will override anything specified in a config file
229 root = fgScanForOption( "--fg-root=", argc, argv);
231 #if defined( unix ) || defined( __CYGWIN__ )
232 // Next check home directory for .fgfsrc.hostname file
233 if ( root.empty() ) {
234 envp = ::getenv( "HOME" );
235 if ( envp != NULL ) {
236 SGPath config( envp );
237 config.append( ".fgfsrc" );
239 gethostname( name, 256 );
240 config.concat( "." );
241 config.concat( name );
242 root = fgScanForOption( "--fg-root=", config.str() );
247 // Next check home directory for .fgfsrc file
248 if ( root.empty() ) {
249 envp = ::getenv( "HOME" );
250 if ( envp != NULL ) {
251 SGPath config( envp );
252 config.append( ".fgfsrc" );
253 root = fgScanForOption( "--fg-root=", config.str() );
257 // Next check if fg-root is set as an env variable
258 if ( root.empty() ) {
259 envp = ::getenv( "FG_ROOT" );
260 if ( envp != NULL ) {
265 // Otherwise, default to a random compiled-in location if we can't
266 // find fg-root any other way.
267 if ( root.empty() ) {
268 #if defined( __CYGWIN__ )
269 root = "/FlightGear";
270 #elif defined( WIN32 )
271 root = "\\FlightGear";
272 #elif defined(OSX_BUNDLE)
273 /* the following code looks for the base package directly inside
274 the application bundle. This can be changed fairly easily by
275 fiddling with the code below. And yes, I know it's ugly and verbose.
277 CFBundleRef appBundle = CFBundleGetMainBundle();
278 CFURLRef appUrl = CFBundleCopyBundleURL(appBundle);
279 CFRelease(appBundle);
281 // look for a 'data' subdir directly inside the bundle : is there
282 // a better place? maybe in Resources? I don't know ...
283 CFURLRef dataDir = CFURLCreateCopyAppendingPathComponent(NULL, appUrl, CFSTR("data"), true);
285 // now convert down to a path, and the a c-string
286 CFStringRef path = CFURLCopyFileSystemPath(dataDir, kCFURLPOSIXPathStyle);
287 root = CFStringGetCStringPtr(path, CFStringGetSystemEncoding());
290 CFRelease(appBundle);
298 SG_LOG(SG_INPUT, SG_INFO, "fg_root = " << root );
299 globals->set_fg_root(root);
305 // Read in configuration (files and command line options) but only set
307 bool fgInitFGAircraft ( int argc, char **argv ) {
311 // First parse command line options looking for --aircraft=, this
312 // will override anything specified in a config file
313 aircraft = fgScanForOption( "--aircraft=", argc, argv );
315 #if defined( unix ) || defined( __CYGWIN__ )
316 // Next check home directory for .fgfsrc.hostname file
317 if ( aircraft.empty() ) {
318 envp = ::getenv( "HOME" );
319 if ( envp != NULL ) {
320 SGPath config( envp );
321 config.append( ".fgfsrc" );
323 gethostname( name, 256 );
324 config.concat( "." );
325 config.concat( name );
326 aircraft = fgScanForOption( "--aircraft=", config.str() );
331 // Next check home directory for .fgfsrc file
332 if ( aircraft.empty() ) {
333 envp = ::getenv( "HOME" );
334 if ( envp != NULL ) {
335 SGPath config( envp );
336 config.append( ".fgfsrc" );
337 aircraft = fgScanForOption( "--aircraft=", config.str() );
341 // if an aircraft was specified, set the property name
342 if ( !aircraft.empty() ) {
343 SG_LOG(SG_INPUT, SG_INFO, "aircraft = " << aircraft );
344 fgSetString("/sim/aircraft", aircraft.c_str() );
346 SG_LOG(SG_INPUT, SG_INFO, "No user specified aircraft, using default" );
353 // Return the current base package version
354 string fgBasePackageVersion() {
355 SGPath base_path( globals->get_fg_root() );
356 base_path.append("version");
358 sg_gzifstream in( base_path.str() );
359 if ( !in.is_open() ) {
360 SGPath old_path( globals->get_fg_root() );
361 old_path.append( "Thanks" );
362 sg_gzifstream old( old_path.str() );
363 if ( !old.is_open() ) {
366 return "[old version]";
377 // Initialize the localization
378 SGPropertyNode *fgInitLocale(const char *language) {
379 SGPropertyNode *c_node = NULL, *d_node = NULL;
380 SGPropertyNode *intl = fgGetNode("/sim/intl");
382 SG_LOG(SG_GENERAL, SG_INFO, "Selecting language: " << language );
384 // localization not defined
389 // Select the proper language from the list
391 vector<SGPropertyNode_ptr> locale = intl->getChildren("locale");
392 for (unsigned int i = 0; i < locale.size(); i++) {
394 vector<SGPropertyNode_ptr> lang = locale[i]->getChildren("lang");
395 for (unsigned int j = 0; j < lang.size(); j++) {
397 if (!strcmp(lang[j]->getStringValue(), language)) {
406 d_node = intl->getChild("locale");
410 // Check for localized font
411 SGPropertyNode *font_n = c_node->getNode("font", true);
412 if ( !strcmp(font_n->getStringValue(), "") )
413 font_n->setStringValue(d_node->getStringValue("font", "typewriter.txf"));
417 // Load the default strings
419 SGPath d_path( globals->get_fg_root() );
421 const char *d_path_str = d_node->getStringValue("strings");
423 SG_LOG(SG_GENERAL, SG_ALERT, "Incorrect path in configuration file.");
427 d_path.append(d_path_str);
428 SG_LOG(SG_GENERAL, SG_INFO, "Reading localized strings from "
431 SGPropertyNode *strings = c_node->getNode("strings");
433 readProperties(d_path.str(), strings);
434 } catch (const sg_exception &e) {
435 SG_LOG(SG_GENERAL, SG_ALERT, "Unable to read the localized strings");
440 // Load the language specific strings
442 if (c_node != d_node) {
443 SGPath c_path( globals->get_fg_root() );
445 const char *c_path_str = c_node->getStringValue("strings");
447 SG_LOG(SG_GENERAL, SG_ALERT, "Incorrect path in configuration file.");
451 c_path.append(c_path_str);
452 SG_LOG(SG_GENERAL, SG_INFO, "Reading localized strings from "
456 readProperties(c_path.str(), strings);
457 } catch (const sg_exception &e) {
458 SG_LOG(SG_GENERAL, SG_ALERT, "Unable to read the localized strings");
468 // Initialize the localization routines
469 bool fgDetectLanguage() {
470 char *language = ::getenv("LANG");
472 if (language == NULL) {
473 SG_LOG(SG_GENERAL, SG_INFO, "Unable to detect the language" );
477 SGPropertyNode *locale = fgInitLocale(language);
479 SG_LOG(SG_GENERAL, SG_ALERT,
480 "No internationalization settings specified in preferences.xml" );
485 globals->set_locale( locale );
490 // Attempt to locate and parse the various non-XML config files in order
491 // from least precidence to greatest precidence
493 do_options (int argc, char ** argv)
495 // Check for $fg_root/system.fgfsrc
496 SGPath config( globals->get_fg_root() );
497 config.append( "system.fgfsrc" );
498 fgParseOptions(config.str());
500 #if defined( unix ) || defined( __CYGWIN__ )
502 // Check for $fg_root/system.fgfsrc.hostname
503 gethostname( name, 256 );
504 config.concat( "." );
505 config.concat( name );
506 fgParseOptions(config.str());
509 // Check for ~/.fgfsrc
510 char* envp = ::getenv( "HOME" );
511 if ( envp != NULL ) {
513 config.append( ".fgfsrc" );
514 fgParseOptions(config.str());
517 #if defined( unix ) || defined( __CYGWIN__ )
518 // Check for ~/.fgfsrc.hostname
519 gethostname( name, 256 );
520 config.concat( "." );
521 config.concat( name );
522 fgParseOptions(config.str());
525 // Parse remaining command line options
526 // These will override anything specified in a config file
527 fgParseArgs(argc, argv);
531 static string fgFindAircraftPath( const SGPath &path, const string &aircraft ) {
533 ulDir *dirp = ulOpenDir(path.str().c_str());
535 cerr << "Unable to open aircraft directory." << endl;
539 while ((dire = ulReadDir(dirp)) != NULL) {
541 if ( strcmp("CVS", dire->d_name) && strcmp(".", dire->d_name)
542 && strcmp("..", dire->d_name) )
545 next.append(dire->d_name);
547 string result = fgFindAircraftPath( next, aircraft );
548 if ( ! result.empty() ) {
552 } else if ( !strcmp(dire->d_name, aircraft.c_str()) ) {
563 // Read in configuration (file and command line)
564 bool fgInitConfig ( int argc, char **argv ) {
566 // First, set some sane default values
569 // Read global preferences from $FG_ROOT/preferences.xml
570 SG_LOG(SG_INPUT, SG_INFO, "Reading global preferences");
571 fgLoadProps("preferences.xml", globals->get_props());
572 SG_LOG(SG_INPUT, SG_INFO, "Finished Reading global preferences");
574 // Detect the required language as early as possible
575 if ( !fgDetectLanguage() ) {
579 // Scan user config files and command line for a specified aircraft.
580 fgInitFGAircraft(argc, argv);
582 string aircraft = fgGetString( "/sim/aircraft", "" );
583 if ( aircraft.size() > 0 ) {
584 SGPath aircraft_search( globals->get_fg_root() );
585 aircraft_search.append( "Aircraft" );
587 string aircraft_set = aircraft + "-set.xml";
589 string result = fgFindAircraftPath( aircraft_search, aircraft_set );
590 if ( !result.empty() ) {
591 fgSetString( "/sim/aircraft-dir", result.c_str() );
592 SGPath full_name( result );
593 full_name.append( aircraft_set );
595 SG_LOG(SG_INPUT, SG_INFO, "Reading default aircraft: " << aircraft
596 << " from " << full_name.str());
598 readProperties( full_name.str(), globals->get_props() );
599 } catch ( const sg_exception &e ) {
600 string message = "Error reading default aircraft: ";
601 message += e.getFormattedMessage();
602 SG_LOG(SG_INPUT, SG_ALERT, message);
606 SG_LOG( SG_INPUT, SG_ALERT, "Cannot find specified aircraft: "
612 SG_LOG( SG_INPUT, SG_ALERT, "No default aircraft specified" );
615 // parse options after loading aircraft to ensure any user
616 // overrides of defaults are honored.
617 do_options(argc, argv);
623 // find basic airport location info from airport database
624 bool fgFindAirportID( const string& id, FGAirport *a ) {
627 SG_LOG( SG_GENERAL, SG_INFO, "Searching for airport code = " << id );
629 result = globals->get_airports()->search( id );
631 if ( result.id.empty() ) {
632 SG_LOG( SG_GENERAL, SG_ALERT,
633 "Failed to find " << id << " in basic.dat.gz" );
642 SG_LOG( SG_GENERAL, SG_INFO,
643 "Position for " << id << " is ("
644 << a->longitude << ", "
645 << a->latitude << ")" );
651 // get airport elevation
652 static double fgGetAirportElev( const string& id ) {
656 SG_LOG( SG_GENERAL, SG_INFO,
657 "Finding elevation for airport: " << id );
659 if ( fgFindAirportID( id, &a ) ) {
667 // Preset lon/lat given an airport id
668 static bool fgSetPosFromAirportID( const string& id ) {
672 SG_LOG( SG_GENERAL, SG_INFO,
673 "Attempting to set starting position from airport code " << id );
675 if ( fgFindAirportID( id, &a ) ) {
677 fgSetDouble("/sim/presets/longitude-deg", a.longitude );
678 fgSetDouble("/sim/presets/latitude-deg", a.latitude );
680 // other code depends on the actual postition being set so set
682 fgSetDouble("/position/longitude-deg", a.longitude );
683 fgSetDouble("/position/latitude-deg", a.latitude );
685 SG_LOG( SG_GENERAL, SG_INFO,
686 "Position for " << id << " is (" << a.longitude
687 << ", " << a.latitude << ")" );
696 // Set current tower position lon/lat given an airport id
697 static bool fgSetTowerPosFromAirportID( const string& id, double hdg ) {
699 // tower height hard coded for now...
700 float towerheight=50.0f;
702 // make a little off the heading for 1 runway airports...
703 float fudge_lon = fabs(sin(hdg)) * .003f;
704 float fudge_lat = .003f - fudge_lon;
706 if ( fgFindAirportID( id, &a ) ) {
707 fgSetDouble("/sim/tower/longitude-deg", a.longitude + fudge_lon);
708 fgSetDouble("/sim/tower/latitude-deg", a.latitude + fudge_lat);
709 fgSetDouble("/sim/tower/altitude-ft", a.elevation + towerheight);
718 // Set current_options lon/lat given an airport id and heading (degrees)
719 static bool fgSetPosFromAirportIDandHdg( const string& id, double tgt_hdg ) {
723 // set initial position from runway and heading
725 SG_LOG( SG_GENERAL, SG_INFO,
726 "Attempting to set starting position from airport code "
727 << id << " heading " << tgt_hdg );
729 if ( ! globals->get_runways()->search( id, (int)tgt_hdg, &r ) ) {
730 SG_LOG( SG_GENERAL, SG_ALERT,
731 "Failed to find a good runway for " << id << '\n' );
738 double lat2, lon2, az2;
739 double heading = r.heading;
740 double azimuth = heading + 180.0;
741 while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
743 SG_LOG( SG_GENERAL, SG_INFO,
744 "runway = " << r.lon << ", " << r.lat
745 << " length = " << r.length * SG_FEET_TO_METER
746 << " heading = " << azimuth );
748 geo_direct_wgs_84 ( 0, r.lat, r.lon, azimuth,
749 r.length * SG_FEET_TO_METER * 0.5 - 5.0,
750 &lat2, &lon2, &az2 );
752 if ( fabs( fgGetDouble("/sim/presets/offset-distance") ) > SG_EPSILON ) {
754 double odist = fgGetDouble("/sim/presets/offset-distance");
755 odist *= SG_NM_TO_METER;
756 double oaz = azimuth;
757 if ( fabs(fgGetDouble("/sim/presets/offset-azimuth")) > SG_EPSILON ) {
758 oaz = fgGetDouble("/sim/presets/offset-azimuth") + 180;
761 while ( oaz >= 360.0 ) { oaz -= 360.0; }
762 geo_direct_wgs_84 ( 0, lat2, lon2, oaz, odist, &olat, &olon, &az2 );
768 fgSetDouble("/sim/presets/longitude-deg", lon2 );
769 fgSetDouble("/sim/presets/latitude-deg", lat2 );
770 fgSetDouble("/sim/presets/heading-deg", heading );
772 // other code depends on the actual values being set ...
773 fgSetDouble("/position/longitude-deg", lon2 );
774 fgSetDouble("/position/latitude-deg", lat2 );
775 fgSetDouble("/orientation/heading-deg", heading );
777 SG_LOG( SG_GENERAL, SG_INFO,
778 "Position for " << id << " is ("
780 << lat2 << ") new heading is "
787 // Set current_options lon/lat given an airport id and runway number
788 static bool fgSetPosFromAirportIDandRwy( const string& id, const string& rwy ) {
792 // set initial position from airport and runway number
794 SG_LOG( SG_GENERAL, SG_INFO,
795 "Attempting to set starting position for "
796 << id << ":" << rwy );
798 if ( ! globals->get_runways()->search( id, rwy, &r ) ) {
799 SG_LOG( SG_GENERAL, SG_ALERT,
800 "Failed to find runway " << rwy <<
801 " at airport " << id );
808 double lat2, lon2, az2;
809 double heading = r.heading;
810 double azimuth = heading + 180.0;
811 while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
813 SG_LOG( SG_GENERAL, SG_INFO,
814 "runway = " << r.lon << ", " << r.lat
815 << " length = " << r.length * SG_FEET_TO_METER
816 << " heading = " << azimuth );
818 geo_direct_wgs_84 ( 0, r.lat, r.lon,
820 r.length * SG_FEET_TO_METER * 0.5 - 5.0,
821 &lat2, &lon2, &az2 );
823 if ( fabs( fgGetDouble("/sim/presets/offset-distance") ) > SG_EPSILON )
826 double odist = fgGetDouble("/sim/presets/offset-distance");
827 odist *= SG_NM_TO_METER;
828 double oaz = azimuth;
829 if ( fabs(fgGetDouble("/sim/presets/offset-azimuth")) > SG_EPSILON )
831 oaz = fgGetDouble("/sim/presets/offset-azimuth") + 180;
832 heading = fgGetDouble("/sim/presets/heading-deg");
834 while ( oaz >= 360.0 ) { oaz -= 360.0; }
835 geo_direct_wgs_84 ( 0, lat2, lon2, oaz, odist, &olat, &olon, &az2 );
841 fgSetDouble("/sim/presets/longitude-deg", lon2 );
842 fgSetDouble("/sim/presets/latitude-deg", lat2 );
843 fgSetDouble("/sim/presets/heading-deg", heading );
845 // other code depends on the actual values being set ...
846 fgSetDouble("/position/longitude-deg", lon2 );
847 fgSetDouble("/position/latitude-deg", lat2 );
848 fgSetDouble("/orientation/heading-deg", heading );
850 SG_LOG( SG_GENERAL, SG_INFO,
851 "Position for " << id << " is ("
853 << lat2 << ") new heading is "
860 static void fgSetDistOrAltFromGlideSlope() {
861 // cout << "fgSetDistOrAltFromGlideSlope()" << endl;
862 string apt_id = fgGetString("/sim/presets/airport-id");
863 double gs = fgGetDouble("/sim/presets/glideslope-deg")
864 * SG_DEGREES_TO_RADIANS ;
865 double od = fgGetDouble("/sim/presets/offset-distance");
866 double alt = fgGetDouble("/sim/presets/altitude-ft");
868 double apt_elev = 0.0;
869 if ( ! apt_id.empty() ) {
870 apt_elev = fgGetAirportElev( apt_id );
871 if ( apt_elev < -9990.0 ) {
878 if( fabs(gs) > 0.01 && fabs(od) > 0.1 && alt < -9990 ) {
879 // set altitude from glideslope and offset-distance
880 od *= SG_NM_TO_METER * SG_METER_TO_FEET;
881 alt = fabs(od*tan(gs)) + apt_elev;
882 fgSetDouble("/sim/presets/altitude-ft", alt);
883 fgSetBool("/sim/presets/onground", false);
884 SG_LOG( SG_GENERAL, SG_INFO, "Calculated altitude as: "
886 } else if( fabs(gs) > 0.01 && alt > 0 && fabs(od) < 0.1) {
887 // set offset-distance from glideslope and altitude
888 od = (alt - apt_elev) / tan(gs);
889 od *= -1*SG_FEET_TO_METER * SG_METER_TO_NM;
890 fgSetDouble("/sim/presets/offset-distance", od);
891 fgSetBool("/sim/presets/onground", false);
892 SG_LOG( SG_GENERAL, SG_INFO, "Calculated offset distance as: "
894 } else if( fabs(gs) > 0.01 ) {
895 SG_LOG( SG_GENERAL, SG_ALERT,
896 "Glideslope given but not altitude or offset-distance." );
897 SG_LOG( SG_GENERAL, SG_ALERT, "Resetting glideslope to zero" );
898 fgSetDouble("/sim/presets/glideslope-deg", 0);
899 fgSetBool("/sim/presets/onground", true);
904 // Set current_options lon/lat given an airport id and heading (degrees)
905 static bool fgSetPosFromNAV( const string& id, const double& freq ) {
906 FGNav *nav = current_navlist->findByIdentAndFreq( id.c_str(), freq );
908 // set initial position from runway and heading
910 SG_LOG( SG_GENERAL, SG_INFO, "Attempting to set starting position for "
911 << id << ":" << freq );
913 double lon = nav->get_lon();
914 double lat = nav->get_lat();
916 if ( fabs( fgGetDouble("/sim/presets/offset-distance") ) > SG_EPSILON )
918 double odist = fgGetDouble("/sim/presets/offset-distance")
920 double oaz = fabs(fgGetDouble("/sim/presets/offset-azimuth"))
922 while ( oaz >= 360.0 ) { oaz -= 360.0; }
923 double olat, olon, az2;
924 geo_direct_wgs_84 ( 0, lat, lon, oaz, odist, &olat, &olon, &az2 );
931 fgSetDouble("/sim/presets/longitude-deg", lon );
932 fgSetDouble("/sim/presets/latitude-deg", lat );
934 // other code depends on the actual values being set ...
935 fgSetDouble("/position/longitude-deg", lon );
936 fgSetDouble("/position/latitude-deg", lat );
937 fgSetDouble("/orientation/heading-deg",
938 fgGetDouble("/sim/presets/heading-deg") );
940 SG_LOG( SG_GENERAL, SG_INFO,
941 "Position for " << id << ":" << freq << " is ("
942 << lon << ", "<< lat << ")" );
946 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to locate NAV = "
947 << id << ":" << freq );
953 // Set current_options lon/lat given an airport id and heading (degrees)
954 static bool fgSetPosFromFix( const string& id ) {
957 // set initial position from runway and heading
958 if ( current_fixlist->query( id.c_str(), &fix ) ) {
959 SG_LOG( SG_GENERAL, SG_INFO, "Attempting to set starting position for "
962 double lon = fix.get_lon();
963 double lat = fix.get_lat();
965 if ( fabs( fgGetDouble("/sim/presets/offset-distance") ) > SG_EPSILON )
967 double odist = fgGetDouble("/sim/presets/offset-distance")
969 double oaz = fabs(fgGetDouble("/sim/presets/offset-azimuth"))
971 while ( oaz >= 360.0 ) { oaz -= 360.0; }
972 double olat, olon, az2;
973 geo_direct_wgs_84 ( 0, lat, lon, oaz, odist, &olat, &olon, &az2 );
980 fgSetDouble("/sim/presets/longitude-deg", lon );
981 fgSetDouble("/sim/presets/latitude-deg", lat );
983 // other code depends on the actual values being set ...
984 fgSetDouble("/position/longitude-deg", lon );
985 fgSetDouble("/position/latitude-deg", lat );
986 fgSetDouble("/orientation/heading-deg",
987 fgGetDouble("/sim/presets/heading-deg") );
989 SG_LOG( SG_GENERAL, SG_INFO,
990 "Position for " << id << " is ("
991 << lon << ", "<< lat << ")" );
995 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to locate NAV = "
1003 * Initialize vor/ndb/ils/fix list management and query systems (as
1004 * well as simple airport db list)
1009 SG_LOG(SG_GENERAL, SG_INFO, "Loading Simple Airport List");
1010 SGPath p_simple( globals->get_fg_root() );
1011 p_simple.append( "Airports/basic.dat" );
1012 SGPath p_metar( globals->get_fg_root() );
1013 p_metar.append( "Airports/metar.dat" );
1014 FGAirportList *airports = new FGAirportList(p_simple.str(), p_metar.str());
1015 globals->set_airports( airports );
1017 SG_LOG(SG_GENERAL, SG_INFO, "Loading Runway List");
1018 SGPath p_runway( globals->get_fg_root() );
1019 p_runway.append( "Airports/runways.dat" );
1020 FGRunwayList *runways = new FGRunwayList( p_runway.str() );
1021 globals->set_runways( runways );
1023 SG_LOG(SG_GENERAL, SG_INFO, "Loading Navaids");
1025 SG_LOG(SG_GENERAL, SG_INFO, " VOR/NDB");
1026 current_navlist = new FGNavList;
1027 SGPath p_nav( globals->get_fg_root() );
1028 p_nav.append( "Navaids/default.nav" );
1029 current_navlist->init( p_nav );
1031 SG_LOG(SG_GENERAL, SG_INFO, " ILS and Marker Beacons");
1032 current_beacons = new FGMarkerBeacons;
1033 current_beacons->init();
1034 current_ilslist = new FGILSList;
1035 SGPath p_ils( globals->get_fg_root() );
1036 p_ils.append( "Navaids/default.ils" );
1037 current_ilslist->init( p_ils );
1039 SG_LOG(SG_GENERAL, SG_INFO, " Fixes");
1040 current_fixlist = new FGFixList;
1041 SGPath p_fix( globals->get_fg_root() );
1042 p_fix.append( "Navaids/default.fix" );
1043 current_fixlist->init( p_fix );
1049 // Set the initial position based on presets (or defaults)
1050 bool fgInitPosition() {
1051 // cout << "fgInitPosition()" << endl;
1052 double gs = fgGetDouble("/sim/presets/glideslope-deg")
1053 * SG_DEGREES_TO_RADIANS ;
1054 double od = fgGetDouble("/sim/presets/offset-distance");
1055 double alt = fgGetDouble("/sim/presets/altitude-ft");
1057 bool set_pos = false;
1059 // If glideslope is specified, then calculate offset-distance or
1060 // altitude relative to glide slope if either of those was not
1062 if ( fabs( gs ) > 0.01 ) {
1063 fgSetDistOrAltFromGlideSlope();
1067 // If we have an explicit, in-range lon/lat, don't change it, just use it.
1068 // If not, check for an airport-id and use that.
1069 // If not, default to the middle of the KSFO field.
1070 // The default values for lon/lat are deliberately out of range
1071 // so that the airport-id can take effect; valid lon/lat will
1072 // override airport-id, however.
1073 double lon_deg = fgGetDouble("/sim/presets/longitude-deg");
1074 double lat_deg = fgGetDouble("/sim/presets/latitude-deg");
1075 if ( lon_deg >= -180.0 && lon_deg <= 180.0
1076 && lat_deg >= -90.0 && lat_deg <= 90.0 )
1081 string apt = fgGetString("/sim/presets/airport-id");
1082 string rwy_no = fgGetString("/sim/presets/runway");
1083 double hdg = fgGetDouble("/sim/presets/heading-deg");
1084 string vor = fgGetString("/sim/presets/vor-id");
1085 double vor_freq = fgGetDouble("/sim/presets/vor-freq");
1086 string ndb = fgGetString("/sim/presets/ndb-id");
1087 double ndb_freq = fgGetDouble("/sim/presets/ndb-freq");
1088 string fix = fgGetString("/sim/presets/fix");
1090 if ( !set_pos && !apt.empty() && !rwy_no.empty() ) {
1091 // An airport + runway is requested
1092 if ( fgSetPosFromAirportIDandRwy( apt, rwy_no ) ) {
1093 // set tower position (a little off the heading for single
1095 fgSetTowerPosFromAirportID( apt, hdg );
1100 if ( !set_pos && !apt.empty() ) {
1101 // An airport is requested (find runway closest to hdg)
1102 if ( fgSetPosFromAirportIDandHdg( apt, hdg ) ) {
1103 // set tower position (a little off the heading for single
1105 fgSetTowerPosFromAirportID( apt, hdg );
1110 if ( !set_pos && !vor.empty() ) {
1111 // a VOR is requested
1112 if ( fgSetPosFromNAV( vor, vor_freq ) ) {
1117 if ( !set_pos && !ndb.empty() ) {
1118 // an NDB is requested
1119 if ( fgSetPosFromNAV( ndb, ndb_freq ) ) {
1124 if ( !set_pos && !fix.empty() ) {
1125 // a Fix is requested
1126 if ( fgSetPosFromFix( fix ) ) {
1132 // No lon/lat specified, no airport specified, default to
1133 // middle of KSFO field.
1134 fgSetDouble("/sim/presets/longitude-deg", -122.374843);
1135 fgSetDouble("/sim/presets/latitude-deg", 37.619002);
1138 fgSetDouble( "/position/longitude-deg",
1139 fgGetDouble("/sim/presets/longitude-deg") );
1140 fgSetDouble( "/position/latitude-deg",
1141 fgGetDouble("/sim/presets/latitude-deg") );
1142 fgSetDouble( "/orientation/heading-deg",
1143 fgGetDouble("/sim/presets/heading-deg") );
1145 // determine if this should be an on-ground or in-air start
1146 if ( fabs(gs) > 0.01 || fabs(od) > 0.1 || alt > 0.1 ) {
1147 fgSetBool("/sim/presets/onground", false);
1149 fgSetBool("/sim/presets/onground", true);
1156 // General house keeping initializations
1157 bool fgInitGeneral() {
1160 #if defined(FX) && defined(XMESA)
1161 char *mesa_win_state;
1164 SG_LOG( SG_GENERAL, SG_INFO, "General Initialization" );
1165 SG_LOG( SG_GENERAL, SG_INFO, "======= ==============" );
1167 root = globals->get_fg_root();
1168 if ( ! root.length() ) {
1169 // No root path set? Then bail ...
1170 SG_LOG( SG_GENERAL, SG_ALERT,
1171 "Cannot continue without a path to the base package "
1172 << "being defined." );
1175 SG_LOG( SG_GENERAL, SG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
1177 #if defined(FX) && defined(XMESA)
1178 // initialize full screen flag
1179 globals->set_fullscreen(false);
1180 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
1181 // Test for the MESA_GLX_FX env variable
1182 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
1183 // test if we are fullscreen mesa/glide
1184 if ( (mesa_win_state[0] == 'f') ||
1185 (mesa_win_state[0] == 'F') ) {
1186 globals->set_fullscreen(true);
1196 // Initialize the flight model subsystem. This just creates the
1197 // object. The actual fdm initialization is delayed until we get a
1198 // proper scenery elevation hit. This is checked for in main.cxx
1202 if ( cur_fdm_state ) {
1203 delete cur_fdm_state;
1207 double dt = 1.0 / fgGetInt("/sim/model-hz");
1208 aircraft_dir = fgGetString("/sim/aircraft-dir");
1209 const string &model = fgGetString("/sim/flight-model");
1212 if ( model == "larcsim" ) {
1213 cur_fdm_state = new FGLaRCsim( dt );
1214 } else if ( model == "jsb" ) {
1215 cur_fdm_state = new FGJSBsim( dt );
1216 } else if ( model == "ada" ) {
1217 cur_fdm_state = new FGADA( dt );
1218 } else if ( model == "balloon" ) {
1219 cur_fdm_state = new FGBalloonSim( dt );
1220 } else if ( model == "magic" ) {
1221 cur_fdm_state = new FGMagicCarpet( dt );
1222 } else if ( model == "ufo" ) {
1223 cur_fdm_state = new FGUFO( dt );
1224 } else if ( model == "external" ) {
1225 // external is a synonym for "--fdm=null" and is
1226 // maintained here for backwards compatibility
1227 cur_fdm_state = new FGNullFDM( dt );
1228 } else if ( model.find("network") == 0 ) {
1229 string host = "localhost";
1233 string net_options = model.substr(8);
1234 string::size_type begin, end;
1237 end = net_options.find( ",", begin );
1238 if ( end != string::npos ) {
1239 host = net_options.substr(begin, end - begin);
1243 end = net_options.find( ",", begin );
1244 if ( end != string::npos ) {
1245 port1 = atoi( net_options.substr(begin, end - begin).c_str() );
1249 end = net_options.find( ",", begin );
1250 if ( end != string::npos ) {
1251 port2 = atoi( net_options.substr(begin, end - begin).c_str() );
1255 end = net_options.find( ",", begin );
1256 if ( end != string::npos ) {
1257 port3 = atoi( net_options.substr(begin, end - begin).c_str() );
1260 cur_fdm_state = new FGExternalNet( dt, host, port1, port2, port3 );
1261 } else if ( model.find("pipe") == 0 ) {
1262 string pipe_path = model.substr(5);
1263 cur_fdm_state = new FGExternalPipe( dt, pipe_path );
1264 } else if ( model == "null" ) {
1265 cur_fdm_state = new FGNullFDM( dt );
1266 } else if ( model == "yasim" ) {
1267 cur_fdm_state = new YASim( dt );
1269 SG_LOG(SG_GENERAL, SG_ALERT,
1270 "Unrecognized flight model '" << model
1271 << "', cannot init flight dynamics model.");
1275 SG_LOG(SG_GENERAL, SG_ALERT, "FlightGear aborting\n\n");
1280 static void printMat(const sgVec4 *mat, char *name="")
1283 SG_LOG(SG_GENERAL, SG_BULK, name );
1284 for(i=0; i<4; i++) {
1285 SG_LOG(SG_GENERAL, SG_BULK, " " << mat[i][0] << " " << mat[i][1]
1286 << " " << mat[i][2] << " " << mat[i][3] );
1290 // Initialize view parameters
1292 // force update of model so that viewer can get some data...
1293 globals->get_aircraft_model()->update(0);
1294 // run update for current view so that data is current...
1295 globals->get_viewmgr()->update(0);
1297 printMat(globals->get_current_view()->get_VIEW(),"VIEW");
1298 printMat(globals->get_current_view()->get_UP(),"UP");
1299 // printMat(globals->get_current_view()->get_LOCAL(),"LOCAL");
1304 SGTime *fgInitTime() {
1306 static const SGPropertyNode *longitude
1307 = fgGetNode("/position/longitude-deg");
1308 static const SGPropertyNode *latitude
1309 = fgGetNode("/position/latitude-deg");
1310 static const SGPropertyNode *cur_time_override
1311 = fgGetNode("/sim/time/cur-time-override", true);
1313 SGPath zone( globals->get_fg_root() );
1314 zone.append( "Timezone" );
1315 SGTime *t = new SGTime( longitude->getDoubleValue()
1316 * SGD_DEGREES_TO_RADIANS,
1317 latitude->getDoubleValue()
1318 * SGD_DEGREES_TO_RADIANS,
1320 cur_time_override->getLongValue() );
1322 globals->set_warp_delta( 0 );
1324 t->update( 0.0, 0.0,
1325 cur_time_override->getLongValue(),
1326 globals->get_warp() );
1332 // set up a time offset (aka warp) if one is specified
1333 void fgInitTimeOffset() {
1334 static const SGPropertyNode *longitude
1335 = fgGetNode("/position/longitude-deg");
1336 static const SGPropertyNode *latitude
1337 = fgGetNode("/position/latitude-deg");
1338 static const SGPropertyNode *cur_time_override
1339 = fgGetNode("/sim/time/cur-time-override", true);
1341 // Handle potential user specified time offsets
1342 int orig_warp = globals->get_warp();
1343 SGTime *t = globals->get_time_params();
1344 time_t cur_time = t->get_cur_time();
1345 time_t currGMT = sgTimeGetGMT( gmtime(&cur_time) );
1346 time_t systemLocalTime = sgTimeGetGMT( localtime(&cur_time) );
1347 time_t aircraftLocalTime =
1348 sgTimeGetGMT( fgLocaltime(&cur_time, t->get_zonename() ) );
1350 // Okay, we now have several possible scenarios
1351 int offset = fgGetInt("/sim/startup/time-offset");
1352 const string &offset_type = fgGetString("/sim/startup/time-offset-type");
1355 if ( offset_type == "real" ) {
1357 } else if ( offset_type == "dawn" ) {
1358 warp = fgTimeSecondsUntilSunAngle( cur_time,
1359 longitude->getDoubleValue()
1360 * SGD_DEGREES_TO_RADIANS,
1361 latitude->getDoubleValue()
1362 * SGD_DEGREES_TO_RADIANS,
1364 } else if ( offset_type == "morning" ) {
1365 warp = fgTimeSecondsUntilSunAngle( cur_time,
1366 longitude->getDoubleValue()
1367 * SGD_DEGREES_TO_RADIANS,
1368 latitude->getDoubleValue()
1369 * SGD_DEGREES_TO_RADIANS,
1371 } else if ( offset_type == "noon" ) {
1372 warp = fgTimeSecondsUntilSunAngle( cur_time,
1373 longitude->getDoubleValue()
1374 * SGD_DEGREES_TO_RADIANS,
1375 latitude->getDoubleValue()
1376 * SGD_DEGREES_TO_RADIANS,
1378 } else if ( offset_type == "afternoon" ) {
1379 warp = fgTimeSecondsUntilSunAngle( cur_time,
1380 longitude->getDoubleValue()
1381 * SGD_DEGREES_TO_RADIANS,
1382 latitude->getDoubleValue()
1383 * SGD_DEGREES_TO_RADIANS,
1385 } else if ( offset_type == "dusk" ) {
1386 warp = fgTimeSecondsUntilSunAngle( cur_time,
1387 longitude->getDoubleValue()
1388 * SGD_DEGREES_TO_RADIANS,
1389 latitude->getDoubleValue()
1390 * SGD_DEGREES_TO_RADIANS,
1392 } else if ( offset_type == "evening" ) {
1393 warp = fgTimeSecondsUntilSunAngle( cur_time,
1394 longitude->getDoubleValue()
1395 * SGD_DEGREES_TO_RADIANS,
1396 latitude->getDoubleValue()
1397 * SGD_DEGREES_TO_RADIANS,
1399 } else if ( offset_type == "midnight" ) {
1400 warp = fgTimeSecondsUntilSunAngle( cur_time,
1401 longitude->getDoubleValue()
1402 * SGD_DEGREES_TO_RADIANS,
1403 latitude->getDoubleValue()
1404 * SGD_DEGREES_TO_RADIANS,
1406 } else if ( offset_type == "system-offset" ) {
1408 } else if ( offset_type == "gmt-offset" ) {
1409 warp = offset - (currGMT - systemLocalTime);
1410 } else if ( offset_type == "latitude-offset" ) {
1411 warp = offset - (aircraftLocalTime - systemLocalTime);
1412 } else if ( offset_type == "system" ) {
1413 warp = offset - cur_time;
1414 } else if ( offset_type == "gmt" ) {
1415 warp = offset - currGMT;
1416 } else if ( offset_type == "latitude" ) {
1417 warp = offset - (aircraftLocalTime - systemLocalTime) - cur_time;
1419 SG_LOG( SG_GENERAL, SG_ALERT,
1420 "FG_TIME::Unsupported offset type " << offset_type );
1423 globals->set_warp( orig_warp + warp );
1424 t->update( longitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
1425 latitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
1426 cur_time_override->getLongValue(),
1427 globals->get_warp() );
1429 SG_LOG( SG_GENERAL, SG_INFO, "After fgInitTimeOffset(): warp = "
1430 << globals->get_warp() );
1433 // This is the top level init routine which calls all the other
1434 // initialization routines. If you are adding a subsystem to flight
1435 // gear, its initialization call should located in this routine.
1436 // Returns non-zero if a problem encountered.
1437 bool fgInitSubsystems() {
1438 // static const SGPropertyNode *longitude
1439 // = fgGetNode("/sim/presets/longitude-deg");
1440 // static const SGPropertyNode *latitude
1441 // = fgGetNode("/sim/presets/latitude-deg");
1442 // static const SGPropertyNode *altitude
1443 // = fgGetNode("/sim/presets/altitude-ft");
1445 FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
1447 SG_LOG( SG_GENERAL, SG_INFO, "Initialize Subsystems");
1448 SG_LOG( SG_GENERAL, SG_INFO, "========== ==========");
1450 ////////////////////////////////////////////////////////////////////
1451 // Initialize the event manager subsystem.
1452 ////////////////////////////////////////////////////////////////////
1454 globals->get_event_mgr()->init();
1455 globals->get_event_mgr()->setFreezeProperty(fgGetNode("/sim/freeze/clock"));
1457 ////////////////////////////////////////////////////////////////////
1458 // Initialize the property interpolator subsystem
1459 ////////////////////////////////////////////////////////////////////
1460 globals->add_subsystem("interpolator", new SGInterpolator);
1463 ////////////////////////////////////////////////////////////////////
1464 // Add the FlightGear property utilities.
1465 ////////////////////////////////////////////////////////////////////
1466 globals->add_subsystem("properties", new FGProperties);
1468 ////////////////////////////////////////////////////////////////////
1469 // Initialize the material property subsystem.
1470 ////////////////////////////////////////////////////////////////////
1472 SGPath mpath( globals->get_fg_root() );
1473 mpath.append( "materials.xml" );
1474 if ( ! globals->get_matlib()->load(globals->get_fg_root(), mpath.str()) ) {
1475 SG_LOG( SG_GENERAL, SG_ALERT, "Error loading material lib!" );
1480 ////////////////////////////////////////////////////////////////////
1481 // Initialize the scenery management subsystem.
1482 ////////////////////////////////////////////////////////////////////
1484 if ( globals->get_tile_mgr()->init() ) {
1485 // Load the local scenery data
1486 double visibility_meters = fgGetDouble("/environment/visibility-m");
1488 globals->get_tile_mgr()->update( visibility_meters );
1490 SG_LOG( SG_GENERAL, SG_ALERT, "Error in Tile Manager initialization!" );
1494 // cause refresh of viewer scenery timestamps every 15 seconds...
1495 globals->get_event_mgr()->addTask( "FGTileMgr::refresh_view_timestamps()",
1496 globals->get_tile_mgr(),
1497 &FGTileMgr::refresh_view_timestamps,
1500 SG_LOG( SG_GENERAL, SG_DEBUG,
1501 "Current terrain elevation after tile mgr init " <<
1502 globals->get_scenery()->get_cur_elev() );
1505 ////////////////////////////////////////////////////////////////////
1506 // Initialize the flight model subsystem.
1507 ////////////////////////////////////////////////////////////////////
1511 // allocates structures so must happen before any of the flight
1512 // model or control parameters are set
1513 fgAircraftInit(); // In the future this might not be the case.
1516 ////////////////////////////////////////////////////////////////////
1517 // Initialize the XML Autopilot subsystem.
1518 ////////////////////////////////////////////////////////////////////
1520 globals->add_subsystem( "xml-autopilot", new FGXMLAutopilot );
1521 globals->add_subsystem( "route-manager", new FGRouteMgr );
1523 ////////////////////////////////////////////////////////////////////
1524 // Initialize the view manager subsystem.
1525 ////////////////////////////////////////////////////////////////////
1529 ////////////////////////////////////////////////////////////////////
1530 // Create and register the logger.
1531 ////////////////////////////////////////////////////////////////////
1533 globals->add_subsystem("logger", new FGLogger);
1535 ////////////////////////////////////////////////////////////////////
1536 // Create and register the XML GUI.
1537 ////////////////////////////////////////////////////////////////////
1539 globals->add_subsystem("gui", new NewGUI, SGSubsystemMgr::INIT);
1542 ////////////////////////////////////////////////////////////////////
1543 // Initialize the local time subsystem.
1544 ////////////////////////////////////////////////////////////////////
1546 // update the current timezone each 30 minutes
1547 globals->get_event_mgr()->addTask( "fgUpdateLocalTime()",
1548 &fgUpdateLocalTime, 30*60 );
1551 ////////////////////////////////////////////////////////////////////
1552 // Initialize the weather subsystem.
1553 ////////////////////////////////////////////////////////////////////
1555 // Initialize the weather modeling subsystem
1557 // Initialize the WeatherDatabase
1558 SG_LOG(SG_GENERAL, SG_INFO, "Creating LocalWeatherDatabase");
1560 sgSetVec3( position, current_aircraft.fdm_state->get_Latitude(),
1561 current_aircraft.fdm_state->get_Longitude(),
1562 current_aircraft.fdm_state->get_Altitude() * SG_FEET_TO_METER );
1563 double init_vis = fgGetDouble("/environment/visibility-m");
1565 FGLocalWeatherDatabase::DatabaseWorkingType working_type;
1567 if (!strcmp(fgGetString("/environment/weather/working-type"), "internet"))
1569 working_type = FGLocalWeatherDatabase::use_internet;
1571 working_type = FGLocalWeatherDatabase::default_mode;
1574 if ( init_vis > 0 ) {
1575 FGLocalWeatherDatabase::theFGLocalWeatherDatabase =
1576 new FGLocalWeatherDatabase( position,
1577 globals->get_fg_root(),
1581 FGLocalWeatherDatabase::theFGLocalWeatherDatabase =
1582 new FGLocalWeatherDatabase( position,
1583 globals->get_fg_root(),
1587 // cout << theFGLocalWeatherDatabase << endl;
1588 // cout << "visibility = "
1589 // << theFGLocalWeatherDatabase->getWeatherVisibility() << endl;
1591 WeatherDatabase = FGLocalWeatherDatabase::theFGLocalWeatherDatabase;
1593 // register the periodic update of the weather
1594 globals->get_event_mgr()->add( "weather update",
1595 &fgUpdateWeatherDatabase, 30000);
1597 globals->add_subsystem("environment", new FGEnvironmentMgr);
1601 ////////////////////////////////////////////////////////////////////
1602 // Initialize the lighting subsystem.
1603 ////////////////////////////////////////////////////////////////////
1605 globals->add_subsystem("lighting", new FGLight);
1608 #ifdef FG_USE_CLOUDS_3D
1609 ////////////////////////////////////////////////////////////////////
1610 // Initialize the 3D cloud subsystem.
1611 ////////////////////////////////////////////////////////////////////
1612 if ( fgGetBool("/sim/rendering/clouds3d") ) {
1613 static const SGPropertyNode *longitude
1614 = fgGetNode("/sim/presets/longitude-deg");
1615 static const SGPropertyNode *latitude
1616 = fgGetNode("/sim/presets/latitude-deg");
1617 static const SGPropertyNode *altitude
1618 = fgGetNode("/sim/presets/altitude-ft");
1620 SGPath cloud_path(globals->get_fg_root());
1622 cloud_path.append("Textures/Sky/scattered.rgba");
1623 SG_LOG(SG_GENERAL, SG_INFO, "Loading CLOUDS3d from: " << cloud_path.c_str());
1626 tx.read_rgba_texture(cloud_path.c_str());
1627 if ( !sgCloud3d->Load( tx.texture(), tx.width(),
1629 cloud_path.append("large.sky");
1630 if ( !sgCloud3d->Load( cloud_path.str(),
1632 latitude->getDoubleValue(),
1633 longitude->getDoubleValue()) )
1635 fgSetBool("/sim/rendering/clouds3d", false);
1636 SG_LOG(SG_GENERAL, SG_INFO, "CLOUDS3d FAILED: ");
1638 SG_LOG(SG_GENERAL, SG_INFO, "CLOUDS3d Loaded: ");
1642 ////////////////////////////////////////////////////////////////////
1643 // Initialise ATC display system
1644 ////////////////////////////////////////////////////////////////////
1646 SG_LOG(SG_GENERAL, SG_INFO, " ATC Display");
1647 globals->set_ATC_display(new FGATCDisplay);
1648 globals->get_ATC_display()->init();
1650 ////////////////////////////////////////////////////////////////////
1651 // Initialise the ATC Manager
1652 ////////////////////////////////////////////////////////////////////
1654 SG_LOG(SG_GENERAL, SG_INFO, " ATC Manager");
1655 globals->set_ATC_mgr(new FGATCMgr);
1656 globals->get_ATC_mgr()->init();
1658 ////////////////////////////////////////////////////////////////////
1659 // Initialise the AI Manager
1660 ////////////////////////////////////////////////////////////////////
1662 SG_LOG(SG_GENERAL, SG_INFO, " AI Manager");
1663 globals->set_AI_mgr(new FGAIMgr);
1664 globals->get_AI_mgr()->init();
1667 ////////////////////////////////////////////////////////////////////
1668 // Initialise the AI Model Manager
1669 ////////////////////////////////////////////////////////////////////
1671 SG_LOG(SG_GENERAL, SG_INFO, " AI Model Manager");
1672 globals->add_subsystem("ai_model", new FGAIManager);
1675 #ifdef ENABLE_AUDIO_SUPPORT
1676 ////////////////////////////////////////////////////////////////////
1677 // Initialize the sound subsystem.
1678 ////////////////////////////////////////////////////////////////////
1680 globals->set_soundmgr(new SGSoundMgr);
1681 globals->get_soundmgr()->init();
1682 globals->get_soundmgr()->bind();
1685 ////////////////////////////////////////////////////////////////////
1686 // Initialize the sound-effects subsystem.
1687 ////////////////////////////////////////////////////////////////////
1689 globals->add_subsystem("fx", new FGFX);
1693 globals->add_subsystem("instrumentation", new FGInstrumentMgr);
1694 globals->add_subsystem("systems", new FGSystemMgr);
1696 ////////////////////////////////////////////////////////////////////
1697 // Initialize the radio stack subsystem.
1698 ////////////////////////////////////////////////////////////////////
1700 current_radiostack = new FGRadioStack;
1701 current_radiostack->init();
1702 current_radiostack->bind();
1705 ////////////////////////////////////////////////////////////////////
1706 // Initialize the cockpit subsystem
1707 ////////////////////////////////////////////////////////////////////
1709 if( fgCockpitInit( ¤t_aircraft )) {
1710 // Cockpit initialized ok.
1712 SG_LOG( SG_GENERAL, SG_ALERT, "Error in Cockpit initialization!" );
1717 ////////////////////////////////////////////////////////////////////
1718 // Initialize the autopilot subsystem.
1719 ////////////////////////////////////////////////////////////////////
1721 // FIXME: these should go in the
1722 // GUI initialization code, not here.
1723 // fgAPAdjustInit();
1724 NewTgtAirportInit();
1728 ////////////////////////////////////////////////////////////////////
1729 // Initialize I/O subsystem.
1730 ////////////////////////////////////////////////////////////////////
1732 globals->get_io()->init();
1733 globals->get_io()->bind();
1736 ////////////////////////////////////////////////////////////////////
1737 // Add a new 2D panel.
1738 ////////////////////////////////////////////////////////////////////
1740 string panel_path = fgGetString("/sim/panel/path",
1741 "Panels/Default/default.xml");
1743 globals->set_current_panel( fgReadPanel(panel_path) );
1744 if (globals->get_current_panel() == 0) {
1745 SG_LOG( SG_INPUT, SG_ALERT,
1746 "Error reading new panel from " << panel_path );
1748 SG_LOG( SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path );
1749 globals->get_current_panel()->init();
1750 globals->get_current_panel()->bind();
1754 ////////////////////////////////////////////////////////////////////
1755 // Initialize the controls subsystem.
1756 ////////////////////////////////////////////////////////////////////
1758 globals->get_controls()->init();
1759 globals->get_controls()->bind();
1762 ////////////////////////////////////////////////////////////////////
1763 // Initialize the input subsystem.
1764 ////////////////////////////////////////////////////////////////////
1766 globals->add_subsystem("input", new FGInput);
1769 ////////////////////////////////////////////////////////////////////
1770 // Initialize the replay subsystem
1771 ////////////////////////////////////////////////////////////////////
1772 globals->add_subsystem("replay", new FGReplay);
1774 ////////////////////////////////////////////////////////////////////
1775 // Bind and initialize subsystems.
1776 ////////////////////////////////////////////////////////////////////
1778 globals->get_subsystem_mgr()->bind();
1779 globals->get_subsystem_mgr()->init();
1781 #ifdef FG_MPLAYER_AS
1782 ////////////////////////////////////////////////////////////////////
1783 // Initialize multiplayer subsystem
1784 ////////////////////////////////////////////////////////////////////
1786 globals->set_multiplayer_tx_mgr(new FGMultiplayTxMgr);
1787 globals->get_multiplayer_tx_mgr()->init();
1789 globals->set_multiplayer_rx_mgr(new FGMultiplayRxMgr);
1790 globals->get_multiplayer_rx_mgr()->init();
1793 ////////////////////////////////////////////////////////////////////////
1794 // Initialize the Nasal interpreter.
1795 // Do this last, so that the loaded scripts see initialized state
1796 ////////////////////////////////////////////////////////////////////////
1797 FGNasalSys* nasal = new FGNasalSys();
1798 globals->add_subsystem("nasal", nasal);
1801 ////////////////////////////////////////////////////////////////////////
1802 // End of subsystem initialization.
1803 ////////////////////////////////////////////////////////////////////
1805 SG_LOG( SG_GENERAL, SG_INFO, endl);
1807 // Save the initial state for future
1809 globals->saveInitialState();
1815 void fgReInitSubsystems()
1817 // static const SGPropertyNode *longitude
1818 // = fgGetNode("/sim/presets/longitude-deg");
1819 // static const SGPropertyNode *latitude
1820 // = fgGetNode("/sim/presets/latitude-deg");
1821 static const SGPropertyNode *altitude
1822 = fgGetNode("/sim/presets/altitude-ft");
1823 static const SGPropertyNode *master_freeze
1824 = fgGetNode("/sim/freeze/master");
1826 SG_LOG( SG_GENERAL, SG_INFO,
1827 "fgReInitSubsystems(): /position/altitude = "
1828 << altitude->getDoubleValue() );
1830 bool freeze = master_freeze->getBoolValue();
1832 fgSetBool("/sim/freeze/master", true);
1834 fgSetBool("/sim/crashed", false);
1836 // Initialize the FDM
1839 // allocates structures so must happen before any of the flight
1840 // model or control parameters are set
1841 fgAircraftInit(); // In the future this might not be the case.
1843 // reload offsets from config defaults
1844 globals->get_viewmgr()->reinit();
1848 globals->get_controls()->reset_all();
1850 fgUpdateLocalTime();
1852 // re-init to proper time of day setting
1856 fgSetBool("/sim/freeze/master", false);