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
42 #include <string.h> // strcmp()
45 #if defined( unix ) || defined( __CYGWIN__ )
46 # include <unistd.h> // for gethostname()
49 // work around a stdc++ lib bug in some versions of linux, but doesn't
50 // seem to hurt to have this here for all versions of Linux.
52 # define _G_NO_EXTERN_TEMPLATES
55 #include <simgear/compiler.h>
56 #include <simgear/misc/exception.hxx>
60 #include <simgear/constants.h>
61 #include <simgear/debug/logstream.hxx>
62 #include <simgear/math/point3d.hxx>
63 #include <simgear/math/polar3d.hxx>
64 #include <simgear/math/sg_geodesy.hxx>
65 #include <simgear/misc/sg_path.hxx>
66 #include <simgear/timing/sg_time.hxx>
67 #include <simgear/timing/lowleveltime.h>
69 #include <Aircraft/aircraft.hxx>
70 #include <FDM/UIUCModel/uiuc_aircraftdir.h>
71 #include <Airports/runways.hxx>
72 #include <Airports/simple.hxx>
73 #include <ATC/ATCdisplay.hxx>
74 #include <ATC/ATCmgr.hxx>
75 #include <ATC/atislist.hxx>
76 #include <ATC/towerlist.hxx>
77 #include <ATC/approachlist.hxx>
78 #include <ATC/AIMgr.hxx>
79 #include <Autopilot/auto_gui.hxx>
80 #include <Autopilot/newauto.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/External.hxx>
88 #include <FDM/ExternalNet.hxx>
89 #include <FDM/JSBSim/JSBSim.hxx>
90 #include <FDM/LaRCsim.hxx>
91 #include <FDM/MagicCarpet.hxx>
92 #include <FDM/NullFDM.hxx>
93 #include <FDM/YASim/YASim.hxx>
94 #include <Include/general.hxx>
95 #include <Input/input.hxx>
96 // #include <Joystick/joystick.hxx>
97 #include <Objects/matlib.hxx>
98 #include <Model/acmodel.hxx>
99 #include <Navaids/fixlist.hxx>
100 #include <Navaids/ilslist.hxx>
101 #include <Navaids/mkrbeacons.hxx>
102 #include <Navaids/navlist.hxx>
103 #include <Scenery/scenery.hxx>
104 #include <Scenery/tilemgr.hxx>
105 #include <Sound/fg_fx.hxx>
106 #include <Sound/soundmgr.hxx>
107 #include <Time/FGEventMgr.hxx>
108 #include <boost/bind.hpp>
109 #include <Time/light.hxx>
110 #include <Time/sunpos.hxx>
111 #include <Time/moonpos.hxx>
112 #include <Time/tmp.hxx>
114 #ifndef FG_NEW_ENVIRONMENT
115 # include <WeatherCM/FGLocalWeatherDatabase.h>
117 # include <Environment/environment_mgr.hxx>
120 #include "fg_init.hxx"
122 #include "fg_commands.hxx"
123 #include "fg_props.hxx"
124 #include "options.hxx"
125 #include "globals.hxx"
126 #include "logger.hxx"
127 #include "viewmgr.hxx"
129 #if defined(FX) && defined(XMESA)
130 #include <GL/xmesa.h>
133 SG_USING_STD(string);
135 extern const char *default_root;
138 // Read in configuration (file and command line) and just set fg_root
139 bool fgInitFGRoot ( int argc, char **argv ) {
143 // First parse command line options looking for fg-root, this will
144 // override anything specified in a config file
145 root = fgScanForRoot(argc, argv);
147 #if defined( unix ) || defined( __CYGWIN__ )
148 // Next check home directory for .fgfsrc.hostname file
149 if ( root.empty() ) {
150 envp = ::getenv( "HOME" );
151 if ( envp != NULL ) {
152 SGPath config( envp );
153 config.append( ".fgfsrc" );
155 gethostname( name, 256 );
156 config.concat( "." );
157 config.concat( name );
158 root = fgScanForRoot(config.str());
163 // Next check home directory for .fgfsrc file
164 if ( root.empty() ) {
165 envp = ::getenv( "HOME" );
166 if ( envp != NULL ) {
167 SGPath config( envp );
168 config.append( ".fgfsrc" );
169 root = fgScanForRoot(config.str());
173 // Next check if fg-root is set as an env variable
174 if ( root.empty() ) {
175 envp = ::getenv( "FG_ROOT" );
176 if ( envp != NULL ) {
181 // Otherwise, default to a random compiled-in location if we can't
182 // find fg-root any other way.
183 if ( root.empty() ) {
184 #if defined( __CYGWIN__ )
185 root = "/FlightGear";
186 #elif defined( WIN32 )
187 root = "\\FlightGear";
188 #elif defined( macintosh )
195 SG_LOG(SG_INPUT, SG_INFO, "fg_root = " << root );
196 globals->set_fg_root(root);
202 // Return the current base package version
203 string fgBasePackageVersion() {
204 SGPath base_path( globals->get_fg_root() );
205 base_path.append("version");
207 sg_gzifstream in( base_path.str() );
208 if ( !in.is_open() ) {
209 SGPath old_path( globals->get_fg_root() );
210 old_path.append( "Thanks" );
211 sg_gzifstream old( old_path.str() );
212 if ( !old.is_open() ) {
215 return "[old version]";
226 // Read in configuration (file and command line)
227 bool fgInitConfig ( int argc, char **argv ) {
229 // First, set some sane default values
232 // Read global preferences from $FG_ROOT/preferences.xml
233 SGPath props_path(globals->get_fg_root());
234 props_path.append("preferences.xml");
235 SG_LOG(SG_INPUT, SG_INFO, "Reading global preferences");
236 readProperties(props_path.str(), globals->get_props());
237 SG_LOG(SG_INPUT, SG_INFO, "Finished Reading global preferences");
239 // Read the default aircraft config file.
240 string aircraft = fgGetString("/sim/aircraft", "");
241 if (aircraft.size() > 0) {
242 SGPath aircraft_path(globals->get_fg_root());
243 aircraft_path.append("Aircraft");
244 aircraft_path.append(aircraft);
245 aircraft_path.concat("-set.xml");
246 SG_LOG(SG_INPUT, SG_INFO, "Reading default aircraft: " << aircraft
247 << " from " << aircraft_path.str());
249 readProperties(aircraft_path.str(), globals->get_props());
250 } catch (const sg_exception &e) {
251 string message = "Error reading default aircraft: ";
252 message += e.getFormattedMessage();
253 SG_LOG(SG_INPUT, SG_ALERT, message);
257 SG_LOG(SG_INPUT, SG_ALERT, "No default aircraft specified");
260 // Attempt to locate and parse the various config files in order
261 // from least precidence to greatest precidence
263 // Check for $fg_root/system.fgfsrc
264 SGPath config( globals->get_fg_root() );
265 config.append( "system.fgfsrc" );
266 fgParseOptions(config.str());
269 #if defined( unix ) || defined( __CYGWIN__ )
270 // Check for $fg_root/system.fgfsrc.hostname
271 gethostname( name, 256 );
272 config.concat( "." );
273 config.concat( name );
274 fgParseOptions(config.str());
277 // Check for ~/.fgfsrc
278 char* envp = ::getenv( "HOME" );
279 if ( envp != NULL ) {
281 config.append( ".fgfsrc" );
282 fgParseOptions(config.str());
285 #if defined( unix ) || defined( __CYGWIN__ )
286 // Check for ~/.fgfsrc.hostname
287 gethostname( name, 256 );
288 config.concat( "." );
289 config.concat( name );
290 fgParseOptions(config.str());
293 // Parse remaining command line options
294 // These will override anything specified in a config file
295 fgParseArgs(argc, argv);
301 // find basic airport location info from airport database
302 bool fgFindAirportID( const string& id, FGAirport *a ) {
304 SGPath path( globals->get_fg_root() );
305 path.append( "Airports" );
306 path.append( "simple.mk4" );
307 FGAirports airports( path.c_str() );
309 SG_LOG( SG_GENERAL, SG_INFO, "Searching for airport code = " << id );
311 if ( ! airports.search( id, a ) ) {
312 SG_LOG( SG_GENERAL, SG_ALERT,
313 "Failed to find " << id << " in " << path.str() );
320 SG_LOG( SG_GENERAL, SG_INFO,
321 "Position for " << id << " is ("
322 << a->longitude << ", "
323 << a->latitude << ")" );
329 // Set current_options lon/lat given an airport id
330 bool fgSetPosFromAirportID( const string& id ) {
334 SG_LOG( SG_GENERAL, SG_INFO,
335 "Attempting to set starting position from airport code " << id );
337 if ( fgFindAirportID( id, &a ) ) {
338 fgSetDouble("/position/longitude-deg", a.longitude );
339 fgSetDouble("/position/latitude-deg", a.latitude );
340 SG_LOG( SG_GENERAL, SG_INFO,
341 "Position for " << id << " is ("
342 << a.longitude << ", "
343 << a.latitude << ")" );
354 // Set current tower position lon/lat given an airport id
355 bool fgSetTowerPosFromAirportID( const string& id, double hdg ) {
357 // tower height hard coded for now...
358 float towerheight=50.0f;
360 // make a little off the heading for 1 runway airports...
361 float fudge_lon = fabs(sin(hdg)) * .003f;
362 float fudge_lat = .003f - fudge_lon;
364 if ( fgFindAirportID( id, &a ) ) {
365 fgSetDouble("/sim/tower/longitude-deg", a.longitude + fudge_lon);
366 fgSetDouble("/sim/tower/latitude-deg", a.latitude + fudge_lat);
367 fgSetDouble("/sim/tower/altitude-ft", a.elevation + towerheight);
376 // Set current_options lon/lat given an airport id and heading (degrees)
377 bool fgSetPosFromAirportIDandHdg( const string& id, double tgt_hdg ) {
380 double found_dir = 0.0;
383 // set initial position from runway and heading
385 SGPath path( globals->get_fg_root() );
386 path.append( "Airports" );
387 path.append( "runways.mk4" );
388 FGRunways runways( path.c_str() );
390 SG_LOG( SG_GENERAL, SG_INFO,
391 "Attempting to set starting position from runway code "
392 << id << " heading " << tgt_hdg );
394 // SGPath inpath( globals->get_fg_root() );
395 // inpath.append( "Airports" );
396 // inpath.append( "apt_simple" );
397 // airports.load( inpath.c_str() );
399 // SGPath outpath( globals->get_fg_root() );
400 // outpath.append( "Airports" );
401 // outpath.append( "simple.gdbm" );
402 // airports.dump_gdbm( outpath.c_str() );
404 if ( ! runways.search( id, &r ) ) {
405 SG_LOG( SG_GENERAL, SG_ALERT,
406 "Failed to find " << id << " in database." );
411 double min_diff = 360.0;
413 while ( r.id == id ) {
415 diff = tgt_hdg - r.heading;
416 while ( diff < -180.0 ) { diff += 360.0; }
417 while ( diff > 180.0 ) { diff -= 360.0; }
419 SG_LOG( SG_GENERAL, SG_INFO,
420 "Runway " << r.rwy_no << " heading = " << r.heading <<
421 " diff = " << diff );
422 if ( diff < min_diff ) {
429 diff = tgt_hdg - r.heading - 180.0;
430 while ( diff < -180.0 ) { diff += 360.0; }
431 while ( diff > 180.0 ) { diff -= 360.0; }
433 SG_LOG( SG_GENERAL, SG_INFO,
434 "Runway -" << r.rwy_no << " heading = " <<
436 " diff = " << diff );
437 if ( diff < min_diff ) {
446 SG_LOG( SG_GENERAL, SG_INFO, "closest runway = " << found_r.rwy_no
447 << " + " << found_dir );
453 double heading = found_r.heading + found_dir;
454 while ( heading >= 360.0 ) { heading -= 360.0; }
456 double lat2, lon2, az2;
457 double azimuth = found_r.heading + found_dir + 180.0;
458 while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
460 SG_LOG( SG_GENERAL, SG_INFO,
461 "runway = " << found_r.lon << ", " << found_r.lat
462 << " length = " << found_r.length * SG_FEET_TO_METER * 0.5
463 << " heading = " << azimuth );
465 geo_direct_wgs_84 ( 0, found_r.lat, found_r.lon,
466 azimuth, found_r.length * SG_FEET_TO_METER * 0.5 - 5.0,
467 &lat2, &lon2, &az2 );
469 if ( fabs( fgGetDouble("/sim/startup/offset-distance") ) > SG_EPSILON ) {
471 double odist = fgGetDouble("/sim/startup/offset-distance");
472 odist *= SG_NM_TO_METER;
473 double oaz = azimuth;
474 if ( fabs(fgGetDouble("/sim/startup/offset-azimuth")) > SG_EPSILON ) {
475 oaz = fgGetDouble("/sim/startup/offset-azimuth") + 180;
477 while ( oaz >= 360.0 ) { oaz -= 360.0; }
478 geo_direct_wgs_84 ( 0, lat2, lon2, oaz, odist, &olat, &olon, &az2 );
483 fgSetDouble("/position/longitude-deg", lon2 );
484 fgSetDouble("/position/latitude-deg", lat2 );
485 fgSetDouble("/orientation/heading-deg", heading );
487 SG_LOG( SG_GENERAL, SG_INFO,
488 "Position for " << id << " is ("
490 << lat2 << ") new heading is "
496 void fgSetPosFromGlideSlope(void) {
497 double gs = fgGetDouble("/velocities/glideslope");
498 double od = fgGetDouble("/sim/startup/offset-distance");
499 double alt = fgGetDouble("/position/altitude-ft");
501 //if glideslope and offset-distance are set and altitude is
502 //not, calculate the initial altitude
503 if( fabs(gs) > 0.01 && fabs(od) > 0.1 && alt < -9990 ) {
504 od *= SG_NM_TO_METER * SG_METER_TO_FEET;
505 alt = fabs(od*tan(gs));
506 fgSetDouble("/position/altitude-ft",alt);
507 fgSetBool("/sim/startup/onground", false);
508 SG_LOG(SG_GENERAL,SG_INFO, "Calculated altitude as: " << alt << " ft");
509 } else if( fabs(gs) > 0.01 && alt > 0 && fabs(od) < 0.1) {
511 od *= -1*SG_FEET_TO_METER * SG_METER_TO_NM;
512 fgSetDouble("/sim/startup/offset-distance",od);
513 SG_LOG(SG_GENERAL,SG_INFO, "Calculated offset distance as: "
515 } else if( fabs(gs) > 0.01 ) {
516 SG_LOG(SG_GENERAL,SG_ALERT, "Glideslope given but not altitude"
517 << " or offset-distance. Resetting"
518 << " glideslope to zero" );
519 fgSetDouble("/velocities/glideslope",0);
524 // General house keeping initializations
525 bool fgInitGeneral( void ) {
528 #if defined(FX) && defined(XMESA)
529 char *mesa_win_state;
532 SG_LOG( SG_GENERAL, SG_INFO, "General Initialization" );
533 SG_LOG( SG_GENERAL, SG_INFO, "======= ==============" );
535 root = globals->get_fg_root();
536 if ( ! root.length() ) {
537 // No root path set? Then bail ...
538 SG_LOG( SG_GENERAL, SG_ALERT,
539 "Cannot continue without a path to the base package "
540 << "being defined." );
543 SG_LOG( SG_GENERAL, SG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
545 #if defined(FX) && defined(XMESA)
546 // initialize full screen flag
547 globals->set_fullscreen(false);
548 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
549 // Test for the MESA_GLX_FX env variable
550 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
551 // test if we are fullscreen mesa/glide
552 if ( (mesa_win_state[0] == 'f') ||
553 (mesa_win_state[0] == 'F') ) {
554 globals->set_fullscreen(true);
564 // Initialize the flight model subsystem. This just creates the
565 // object. The actual fdm initialization is delayed until we get a
566 // proper scenery elevation hit. This is checked for in main.cxx
570 if ( cur_fdm_state ) {
571 delete cur_fdm_state;
575 double dt = 1.0 / fgGetInt("/sim/model-hz");
576 aircraft_dir = fgGetString("/sim/aircraft-dir");
577 const string &model = fgGetString("/sim/flight-model");
580 if ( model == "larcsim" ) {
581 cur_fdm_state = new FGLaRCsim( dt );
582 } else if ( model == "jsb" ) {
583 cur_fdm_state = new FGJSBsim( dt );
584 } else if ( model == "ada" ) {
585 cur_fdm_state = new FGADA( dt );
586 } else if ( model == "balloon" ) {
587 cur_fdm_state = new FGBalloonSim( dt );
588 } else if ( model == "magic" ) {
589 cur_fdm_state = new FGMagicCarpet( dt );
590 } else if ( model == "external" ) {
591 cur_fdm_state = new FGExternal( dt );
592 } else if ( model.find("network,") == 0 ) {
593 string host = "localhost";
597 string net_options = model.substr(8);
598 string::size_type begin, end;
601 end = net_options.find( ",", begin );
602 if ( end != string::npos ) {
603 host = net_options.substr(begin, end - begin);
607 end = net_options.find( ",", begin );
608 if ( end != string::npos ) {
609 port1 = atoi( net_options.substr(begin, end - begin).c_str() );
613 end = net_options.find( ",", begin );
614 if ( end != string::npos ) {
615 port2 = atoi( net_options.substr(begin, end - begin).c_str() );
619 end = net_options.find( ",", begin );
620 if ( end != string::npos ) {
621 port3 = atoi( net_options.substr(begin, end - begin).c_str() );
624 cur_fdm_state = new FGExternalNet( dt, host, port1, port2, port3 );
625 } else if ( model == "null" ) {
626 cur_fdm_state = new FGNullFDM( dt );
627 } else if ( model == "yasim" ) {
628 cur_fdm_state = new YASim( dt );
630 SG_LOG(SG_GENERAL, SG_ALERT,
631 "Unrecognized flight model '" << model
632 << "', cannot init flight dynamics model.");
636 SG_LOG(SG_GENERAL, SG_ALERT, "FlightGear aborting\n\n");
642 // Initialize view parameters
644 globals->get_viewmgr()->update(0);
648 SGTime *fgInitTime() {
650 static const SGPropertyNode *longitude
651 = fgGetNode("/position/longitude-deg");
652 static const SGPropertyNode *latitude
653 = fgGetNode("/position/latitude-deg");
654 static const SGPropertyNode *cur_time_override
655 = fgGetNode("/sim/time/cur-time-override", true);
657 SGPath zone( globals->get_fg_root() );
658 zone.append( "Timezone" );
659 SGTime *t = new SGTime( longitude->getDoubleValue()
660 * SGD_DEGREES_TO_RADIANS,
661 latitude->getDoubleValue()
662 * SGD_DEGREES_TO_RADIANS,
664 cur_time_override->getLongValue() );
666 // Handle potential user specified time offsets
667 time_t cur_time = t->get_cur_time();
668 time_t currGMT = sgTimeGetGMT( gmtime(&cur_time) );
669 time_t systemLocalTime = sgTimeGetGMT( localtime(&cur_time) );
670 time_t aircraftLocalTime =
671 sgTimeGetGMT( fgLocaltime(&cur_time, t->get_zonename() ) );
673 // Okay, we now have six possible scenarios
674 int offset = fgGetInt("/sim/startup/time-offset");
675 const string &offset_type = fgGetString("/sim/startup/time-offset-type");
676 if (offset_type == "system-offset") {
677 globals->set_warp( offset );
678 } else if (offset_type == "gmt-offset") {
679 globals->set_warp( offset - (currGMT - systemLocalTime) );
680 } else if (offset_type == "latitude-offset") {
681 globals->set_warp( offset - (aircraftLocalTime - systemLocalTime) );
682 } else if (offset_type == "system") {
683 globals->set_warp( offset - cur_time );
684 } else if (offset_type == "gmt") {
685 globals->set_warp( offset - currGMT );
686 } else if (offset_type == "latitude") {
687 globals->set_warp( offset - (aircraftLocalTime - systemLocalTime) -
690 SG_LOG( SG_GENERAL, SG_ALERT,
691 "FG_TIME::Unsupported offset type " << offset_type );
695 SG_LOG( SG_GENERAL, SG_INFO, "After time init, warp = "
696 << globals->get_warp() );
698 globals->set_warp_delta( 0 );
701 cur_time_override->getLongValue(),
702 globals->get_warp() );
708 // This is the top level init routine which calls all the other
709 // initialization routines. If you are adding a subsystem to flight
710 // gear, its initialization call should located in this routine.
711 // Returns non-zero if a problem encountered.
712 bool fgInitSubsystems( void ) {
713 static const SGPropertyNode *longitude
714 = fgGetNode("/position/longitude-deg");
715 static const SGPropertyNode *latitude
716 = fgGetNode("/position/latitude-deg");
717 static const SGPropertyNode *altitude
718 = fgGetNode("/position/altitude-ft");
720 fgLIGHT *l = &cur_light_params;
722 SG_LOG( SG_GENERAL, SG_INFO, "Initialize Subsystems");
723 SG_LOG( SG_GENERAL, SG_INFO, "========== ==========");
726 ////////////////////////////////////////////////////////////////////
727 // Initialize the material property subsystem.
728 ////////////////////////////////////////////////////////////////////
730 SGPath mpath( globals->get_fg_root() );
731 mpath.append( "materials.xml" );
732 if ( ! material_lib.load( mpath.str() ) ) {
733 SG_LOG( SG_GENERAL, SG_ALERT, "Error loading material lib!" );
738 ////////////////////////////////////////////////////////////////////
739 // Initialize the scenery management subsystem.
740 ////////////////////////////////////////////////////////////////////
745 if ( global_tile_mgr.init() ) {
746 // Load the local scenery data
747 double visibility_meters = fgGetDouble("/environment/visibility-m");
749 global_tile_mgr.update( longitude->getDoubleValue(),
750 latitude->getDoubleValue(),
753 SG_LOG( SG_GENERAL, SG_ALERT, "Error in Tile Manager initialization!" );
757 SG_LOG( SG_GENERAL, SG_DEBUG,
758 "Current terrain elevation after tile mgr init " <<
759 scenery.get_cur_elev() );
762 ////////////////////////////////////////////////////////////////////
763 // Initialize the flight model subsystem.
764 ////////////////////////////////////////////////////////////////////
768 // allocates structures so must happen before any of the flight
769 // model or control parameters are set
770 fgAircraftInit(); // In the future this might not be the case.
773 ////////////////////////////////////////////////////////////////////
774 // Initialize the event manager subsystem.
775 ////////////////////////////////////////////////////////////////////
777 global_events.init();
779 // Output event stats every 60 seconds
780 global_events.Register( "fgEVENT_MGR::PrintStats()",
781 boost::bind( &FGEventMgr::print_stats,
786 ////////////////////////////////////////////////////////////////////
787 // Initialize the view manager subsystem.
788 ////////////////////////////////////////////////////////////////////
793 ////////////////////////////////////////////////////////////////////
794 // Initialize the lighting subsystem.
795 ////////////////////////////////////////////////////////////////////
797 // fgUpdateSunPos() needs a few position and view parameters set
798 // so it can calculate local relative sun angle and a few other
799 // things for correctly orienting the sky.
802 global_events.Register( "fgUpdateSunPos()", &fgUpdateSunPos,
804 global_events.Register( "fgUpdateMoonPos()", &fgUpdateMoonPos,
807 // Initialize Lighting interpolation tables
810 // update the lighting parameters (based on sun angle)
811 global_events.Register( "fgLight::Update()",
812 boost::bind( &fgLIGHT::Update, &cur_light_params ),
816 ////////////////////////////////////////////////////////////////////
817 // Initialize the logger.
818 ////////////////////////////////////////////////////////////////////
820 globals->set_logger(new FGLogger);
821 globals->get_logger()->init();
822 globals->get_logger()->bind();
825 ////////////////////////////////////////////////////////////////////
826 // Initialize the local time subsystem.
827 ////////////////////////////////////////////////////////////////////
829 // update the current timezone each 30 minutes
830 global_events.Register( "fgUpdateLocalTime()", &fgUpdateLocalTime,
834 ////////////////////////////////////////////////////////////////////
835 // Initialize the weather subsystem.
836 ////////////////////////////////////////////////////////////////////
838 // Initialize the weather modeling subsystem
839 #ifndef FG_NEW_ENVIRONMENT
840 // Initialize the WeatherDatabase
841 SG_LOG(SG_GENERAL, SG_INFO, "Creating LocalWeatherDatabase");
843 sgSetVec3( position, current_aircraft.fdm_state->get_Latitude(),
844 current_aircraft.fdm_state->get_Longitude(),
845 current_aircraft.fdm_state->get_Altitude() * SG_FEET_TO_METER );
846 double init_vis = fgGetDouble("/environment/visibility-m");
848 FGLocalWeatherDatabase::DatabaseWorkingType working_type;
850 if (!strcmp(fgGetString("/environment/weather/working-type"), "internet"))
852 working_type = FGLocalWeatherDatabase::use_internet;
854 working_type = FGLocalWeatherDatabase::default_mode;
857 if ( init_vis > 0 ) {
858 FGLocalWeatherDatabase::theFGLocalWeatherDatabase =
859 new FGLocalWeatherDatabase( position,
860 globals->get_fg_root(),
864 FGLocalWeatherDatabase::theFGLocalWeatherDatabase =
865 new FGLocalWeatherDatabase( position,
866 globals->get_fg_root(),
870 // cout << theFGLocalWeatherDatabase << endl;
871 // cout << "visibility = "
872 // << theFGLocalWeatherDatabase->getWeatherVisibility() << endl;
874 WeatherDatabase = FGLocalWeatherDatabase::theFGLocalWeatherDatabase;
876 // register the periodic update of the weather
877 global_events.Register( "weather update", &fgUpdateWeatherDatabase,
880 globals->get_environment_mgr()->init();
881 globals->get_environment_mgr()->bind();
884 ////////////////////////////////////////////////////////////////////
885 // Initialize vor/ndb/ils/fix list management and query systems
886 ////////////////////////////////////////////////////////////////////
888 SG_LOG(SG_GENERAL, SG_INFO, "Loading Navaids");
890 SG_LOG(SG_GENERAL, SG_INFO, " VOR/NDB");
891 current_navlist = new FGNavList;
892 SGPath p_nav( globals->get_fg_root() );
893 p_nav.append( "Navaids/default.nav" );
894 current_navlist->init( p_nav );
896 SG_LOG(SG_GENERAL, SG_INFO, " ILS and Marker Beacons");
897 current_beacons = new FGMarkerBeacons;
898 current_beacons->init();
899 current_ilslist = new FGILSList;
900 SGPath p_ils( globals->get_fg_root() );
901 p_ils.append( "Navaids/default.ils" );
902 current_ilslist->init( p_ils );
904 SG_LOG(SG_GENERAL, SG_INFO, " Fixes");
905 current_fixlist = new FGFixList;
906 SGPath p_fix( globals->get_fg_root() );
907 p_fix.append( "Navaids/default.fix" );
908 current_fixlist->init( p_fix );
910 ////////////////////////////////////////////////////////////////////
911 // Initialize ATC list management and query systems
912 ////////////////////////////////////////////////////////////////////
914 SG_LOG(SG_GENERAL, SG_INFO, " ATIS");
915 current_atislist = new FGATISList;
916 SGPath p_atis( globals->get_fg_root() );
917 p_atis.append( "ATC/default.atis" );
918 current_atislist->init( p_atis );
920 SG_LOG(SG_GENERAL, SG_INFO, " Tower");
921 current_towerlist = new FGTowerList;
922 SGPath p_tower( globals->get_fg_root() );
923 p_tower.append( "ATC/default.tower" );
924 current_towerlist->init( p_tower );
926 SG_LOG(SG_GENERAL, SG_INFO, " Approach");
927 current_approachlist = new FGApproachList;
928 SGPath p_approach( globals->get_fg_root() );
929 p_approach.append( "ATC/default.approach" );
930 current_approachlist->init( p_approach );
932 ////////////////////////////////////////////////////////////////////
933 // Initialise ATC display system
934 ////////////////////////////////////////////////////////////////////
936 SG_LOG(SG_GENERAL, SG_INFO, " ATC Display");
937 globals->set_ATC_display(new FGATCDisplay);
938 globals->get_ATC_display()->init();
940 ////////////////////////////////////////////////////////////////////
941 // Initialise the ATC Manager
942 ////////////////////////////////////////////////////////////////////
944 SG_LOG(SG_GENERAL, SG_INFO, " ATC Manager");
945 globals->set_ATC_mgr(new FGATCMgr);
946 globals->get_ATC_mgr()->init();
948 ////////////////////////////////////////////////////////////////////
949 // Initialise the AI Manager
950 ////////////////////////////////////////////////////////////////////
952 SG_LOG(SG_GENERAL, SG_INFO, " AI Manager");
953 globals->set_AI_mgr(new FGAIMgr);
954 globals->get_AI_mgr()->init();
956 ////////////////////////////////////////////////////////////////////
957 // Initialize the built-in commands.
958 ////////////////////////////////////////////////////////////////////
962 #ifdef ENABLE_AUDIO_SUPPORT
963 ////////////////////////////////////////////////////////////////////
964 // Initialize the sound subsystem.
965 ////////////////////////////////////////////////////////////////////
967 globals->set_soundmgr(new FGSoundMgr);
968 globals->get_soundmgr()->init();
969 globals->get_soundmgr()->bind();
972 ////////////////////////////////////////////////////////////////////
973 // Initialize the sound-effects subsystem.
974 ////////////////////////////////////////////////////////////////////
975 globals->set_fx(new FGFX);
976 globals->get_fx()->init();
977 globals->get_fx()->bind();
981 ////////////////////////////////////////////////////////////////////
982 // Initialize the radio stack subsystem.
983 ////////////////////////////////////////////////////////////////////
985 // A textbook example of how FGSubsystem
987 current_radiostack = new FGRadioStack;
988 current_radiostack->init();
989 current_radiostack->bind();
992 ////////////////////////////////////////////////////////////////////
993 // Initialize the cockpit subsystem
994 ////////////////////////////////////////////////////////////////////
996 if( fgCockpitInit( ¤t_aircraft )) {
997 // Cockpit initialized ok.
999 SG_LOG( SG_GENERAL, SG_ALERT, "Error in Cockpit initialization!" );
1004 ////////////////////////////////////////////////////////////////////
1005 // Initialize the autopilot subsystem.
1006 ////////////////////////////////////////////////////////////////////
1008 globals->set_autopilot(new FGAutopilot);
1009 globals->get_autopilot()->init();
1010 globals->get_autopilot()->bind();
1012 // FIXME: these should go in the
1013 // GUI initialization code, not here.
1015 NewTgtAirportInit();
1019 ////////////////////////////////////////////////////////////////////
1020 // Initialize I/O subsystem.
1021 ////////////////////////////////////////////////////////////////////
1023 #if ! defined( macintosh )
1027 // Initialize the 2D panel.
1028 string panel_path = fgGetString("/sim/panel/path",
1029 "Panels/Default/default.xml");
1030 current_panel = fgReadPanel(panel_path);
1031 if (current_panel == 0) {
1032 SG_LOG( SG_INPUT, SG_ALERT,
1033 "Error reading new panel from " << panel_path );
1035 SG_LOG( SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path );
1036 current_panel->init();
1037 current_panel->bind();
1041 ////////////////////////////////////////////////////////////////////
1042 // Initialize the default (kludged) properties.
1043 ////////////////////////////////////////////////////////////////////
1048 ////////////////////////////////////////////////////////////////////
1049 // Initialize the controls subsystem.
1050 ////////////////////////////////////////////////////////////////////
1052 globals->get_controls()->init();
1053 globals->get_controls()->bind();
1056 ////////////////////////////////////////////////////////////////////
1057 // Initialize the input subsystem.
1058 ////////////////////////////////////////////////////////////////////
1060 current_input.init();
1061 current_input.bind();
1064 ////////////////////////////////////////////////////////////////////////
1065 // End of subsystem initialization.
1066 ////////////////////////////////////////////////////////////////////
1068 SG_LOG( SG_GENERAL, SG_INFO, endl);
1070 // Save the initial state for future
1072 globals->saveInitialState();
1078 void fgReInitSubsystems( void )
1080 static const SGPropertyNode *longitude
1081 = fgGetNode("/position/longitude-deg");
1082 static const SGPropertyNode *latitude
1083 = fgGetNode("/position/latitude-deg");
1084 static const SGPropertyNode *altitude
1085 = fgGetNode("/position/altitude-ft");
1086 static const SGPropertyNode *master_freeze
1087 = fgGetNode("/sim/freeze/master");
1089 SG_LOG( SG_GENERAL, SG_INFO,
1090 "/position/altitude = " << altitude->getDoubleValue() );
1092 bool freeze = master_freeze->getBoolValue();
1094 fgSetBool("/sim/freeze/master", true);
1097 // Initialize the Scenery Management subsystem
1101 if( global_tile_mgr.init() ) {
1102 Load the local scenery data
1103 global_tile_mgr.update( longitude->getDoubleValue(),
1104 latitude->getDoubleValue() );
1106 SG_LOG( SG_GENERAL, SG_ALERT, "Error in Tile Manager initialization!" );
1113 // allocates structures so must happen before any of the flight
1114 // model or control parameters are set
1115 fgAircraftInit(); // In the future this might not be the case.
1119 globals->get_controls()->reset_all();
1120 globals->get_autopilot()->reset();
1124 cur_light_params.Update();
1125 fgUpdateLocalTime();
1128 fgSetBool("/sim/freeze/master", false);