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/sky/clouds3d/SkySceneLoader.hpp>
67 #include <simgear/sky/clouds3d/SkyUtil.hpp>
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/atislist.hxx>
78 #include <ATC/towerlist.hxx>
79 #include <ATC/approachlist.hxx>
80 #include <ATC/AIMgr.hxx>
81 #include <Autopilot/auto_gui.hxx>
82 #include <Autopilot/newauto.hxx>
83 #include <Cockpit/cockpit.hxx>
84 #include <Cockpit/radiostack.hxx>
85 #include <Cockpit/steam.hxx>
86 #include <Cockpit/panel.hxx>
87 #include <Cockpit/panel_io.hxx>
88 #include <FDM/ADA.hxx>
89 #include <FDM/Balloon.h>
90 #include <FDM/External.hxx>
91 #include <FDM/ExternalNet.hxx>
92 #include <FDM/JSBSim/JSBSim.hxx>
93 #include <FDM/LaRCsim.hxx>
94 #include <FDM/MagicCarpet.hxx>
95 #include <FDM/UFO.hxx>
96 #include <FDM/NullFDM.hxx>
97 #include <FDM/YASim/YASim.hxx>
98 #include <Include/general.hxx>
99 #include <Input/input.hxx>
100 // #include <Joystick/joystick.hxx>
101 #include <Objects/matlib.hxx>
102 #include <Model/acmodel.hxx>
103 #include <Navaids/fixlist.hxx>
104 #include <Navaids/ilslist.hxx>
105 #include <Navaids/mkrbeacons.hxx>
106 #include <Navaids/navlist.hxx>
107 #include <Scenery/scenery.hxx>
108 #include <Scenery/tilemgr.hxx>
109 #include <Sound/fg_fx.hxx>
110 #include <Sound/soundmgr.hxx>
111 #include <Time/FGEventMgr.hxx>
112 #include <Time/light.hxx>
113 #include <Time/sunpos.hxx>
114 #include <Time/moonpos.hxx>
115 #include <Time/tmp.hxx>
118 # include <WeatherCM/FGLocalWeatherDatabase.h>
120 # include <Environment/environment_mgr.hxx>
123 #include "fg_init.hxx"
125 #include "fg_commands.hxx"
126 #include "fg_props.hxx"
127 #include "options.hxx"
128 #include "globals.hxx"
129 #include "logger.hxx"
130 #include "viewmgr.hxx"
132 #if defined(FX) && defined(XMESA)
133 #include <GL/xmesa.h>
136 SG_USING_STD(string);
138 extern const char *default_root;
140 SkySceneLoader *sgCloud3d;
142 // Read in configuration (file and command line) and just set fg_root
143 bool fgInitFGRoot ( int argc, char **argv ) {
147 // First parse command line options looking for fg-root, this will
148 // override anything specified in a config file
149 root = fgScanForRoot(argc, argv);
151 #if defined( unix ) || defined( __CYGWIN__ )
152 // Next check home directory for .fgfsrc.hostname file
153 if ( root.empty() ) {
154 envp = ::getenv( "HOME" );
155 if ( envp != NULL ) {
156 SGPath config( envp );
157 config.append( ".fgfsrc" );
159 gethostname( name, 256 );
160 config.concat( "." );
161 config.concat( name );
162 root = fgScanForRoot(config.str());
167 // Next check home directory for .fgfsrc file
168 if ( root.empty() ) {
169 envp = ::getenv( "HOME" );
170 if ( envp != NULL ) {
171 SGPath config( envp );
172 config.append( ".fgfsrc" );
173 root = fgScanForRoot(config.str());
177 // Next check if fg-root is set as an env variable
178 if ( root.empty() ) {
179 envp = ::getenv( "FG_ROOT" );
180 if ( envp != NULL ) {
185 // Otherwise, default to a random compiled-in location if we can't
186 // find fg-root any other way.
187 if ( root.empty() ) {
188 #if defined( __CYGWIN__ )
189 root = "/FlightGear";
190 #elif defined( WIN32 )
191 root = "\\FlightGear";
192 #elif defined( macintosh )
199 SG_LOG(SG_INPUT, SG_INFO, "fg_root = " << root );
200 globals->set_fg_root(root);
206 // Return the current base package version
207 string fgBasePackageVersion() {
208 SGPath base_path( globals->get_fg_root() );
209 base_path.append("version");
211 sg_gzifstream in( base_path.str() );
212 if ( !in.is_open() ) {
213 SGPath old_path( globals->get_fg_root() );
214 old_path.append( "Thanks" );
215 sg_gzifstream old( old_path.str() );
216 if ( !old.is_open() ) {
219 return "[old version]";
230 // Read in configuration (file and command line)
231 bool fgInitConfig ( int argc, char **argv ) {
233 // First, set some sane default values
236 // Read global preferences from $FG_ROOT/preferences.xml
237 SGPath props_path(globals->get_fg_root());
238 props_path.append("preferences.xml");
239 SG_LOG(SG_INPUT, SG_INFO, "Reading global preferences");
240 readProperties(props_path.str(), globals->get_props());
241 SG_LOG(SG_INPUT, SG_INFO, "Finished Reading global preferences");
243 // Read the default aircraft config file.
244 string aircraft = fgGetString("/sim/aircraft", "");
245 if (aircraft.size() > 0) {
246 SGPath aircraft_path(globals->get_fg_root());
247 aircraft_path.append("Aircraft");
248 aircraft_path.append(aircraft);
249 aircraft_path.concat("-set.xml");
250 SG_LOG(SG_INPUT, SG_INFO, "Reading default aircraft: " << aircraft
251 << " from " << aircraft_path.str());
253 readProperties(aircraft_path.str(), globals->get_props());
254 } catch (const sg_exception &e) {
255 string message = "Error reading default aircraft: ";
256 message += e.getFormattedMessage();
257 SG_LOG(SG_INPUT, SG_ALERT, message);
261 SG_LOG(SG_INPUT, SG_ALERT, "No default aircraft specified");
264 // Attempt to locate and parse the various config files in order
265 // from least precidence to greatest precidence
267 // Check for $fg_root/system.fgfsrc
268 SGPath config( globals->get_fg_root() );
269 config.append( "system.fgfsrc" );
270 fgParseOptions(config.str());
272 #if defined( unix ) || defined( __CYGWIN__ )
274 // Check for $fg_root/system.fgfsrc.hostname
275 gethostname( name, 256 );
276 config.concat( "." );
277 config.concat( name );
278 fgParseOptions(config.str());
281 // Check for ~/.fgfsrc
282 char* envp = ::getenv( "HOME" );
283 if ( envp != NULL ) {
285 config.append( ".fgfsrc" );
286 fgParseOptions(config.str());
289 #if defined( unix ) || defined( __CYGWIN__ )
290 // Check for ~/.fgfsrc.hostname
291 gethostname( name, 256 );
292 config.concat( "." );
293 config.concat( name );
294 fgParseOptions(config.str());
297 // Parse remaining command line options
298 // These will override anything specified in a config file
299 fgParseArgs(argc, argv);
305 // find basic airport location info from airport database
306 bool fgFindAirportID( const string& id, FGAirport *a ) {
308 SGPath path( globals->get_fg_root() );
309 path.append( "Airports" );
310 path.append( "simple.mk4" );
311 FGAirports airports( path.c_str() );
313 SG_LOG( SG_GENERAL, SG_INFO, "Searching for airport code = " << id );
315 if ( ! airports.search( id, a ) ) {
316 SG_LOG( SG_GENERAL, SG_ALERT,
317 "Failed to find " << id << " in " << path.str() );
324 SG_LOG( SG_GENERAL, SG_INFO,
325 "Position for " << id << " is ("
326 << a->longitude << ", "
327 << a->latitude << ")" );
333 // Set current_options lon/lat given an airport id
334 bool fgSetPosFromAirportID( const string& id ) {
338 SG_LOG( SG_GENERAL, SG_INFO,
339 "Attempting to set starting position from airport code " << id );
341 if ( fgFindAirportID( id, &a ) ) {
342 fgSetDouble("/position/longitude-deg", a.longitude );
343 fgSetDouble("/position/latitude-deg", a.latitude );
344 SG_LOG( SG_GENERAL, SG_INFO,
345 "Position for " << id << " is ("
346 << a.longitude << ", "
347 << a.latitude << ")" );
358 // Set current tower position lon/lat given an airport id
359 bool fgSetTowerPosFromAirportID( const string& id, double hdg ) {
361 // tower height hard coded for now...
362 float towerheight=50.0f;
364 // make a little off the heading for 1 runway airports...
365 float fudge_lon = fabs(sin(hdg)) * .003f;
366 float fudge_lat = .003f - fudge_lon;
368 if ( fgFindAirportID( id, &a ) ) {
369 fgSetDouble("/sim/tower/longitude-deg", a.longitude + fudge_lon);
370 fgSetDouble("/sim/tower/latitude-deg", a.latitude + fudge_lat);
371 fgSetDouble("/sim/tower/altitude-ft", a.elevation + towerheight);
380 // Set current_options lon/lat given an airport id and heading (degrees)
381 bool fgSetPosFromAirportIDandHdg( const string& id, double tgt_hdg ) {
384 double found_dir = 0.0;
387 // set initial position from runway and heading
389 SGPath path( globals->get_fg_root() );
390 path.append( "Airports" );
391 path.append( "runways.mk4" );
392 FGRunways runways( path.c_str() );
394 SG_LOG( SG_GENERAL, SG_INFO,
395 "Attempting to set starting position from runway code "
396 << id << " heading " << tgt_hdg );
398 // SGPath inpath( globals->get_fg_root() );
399 // inpath.append( "Airports" );
400 // inpath.append( "apt_simple" );
401 // airports.load( inpath.c_str() );
403 // SGPath outpath( globals->get_fg_root() );
404 // outpath.append( "Airports" );
405 // outpath.append( "simple.gdbm" );
406 // airports.dump_gdbm( outpath.c_str() );
408 if ( ! runways.search( id, &r ) ) {
409 SG_LOG( SG_GENERAL, SG_ALERT,
410 "Failed to find " << id << " in database." );
415 double min_diff = 360.0;
417 while ( r.id == id ) {
419 diff = tgt_hdg - r.heading;
420 while ( diff < -180.0 ) { diff += 360.0; }
421 while ( diff > 180.0 ) { diff -= 360.0; }
423 SG_LOG( SG_GENERAL, SG_INFO,
424 "Runway " << r.rwy_no << " heading = " << r.heading <<
425 " diff = " << diff );
426 if ( diff < min_diff ) {
433 diff = tgt_hdg - r.heading - 180.0;
434 while ( diff < -180.0 ) { diff += 360.0; }
435 while ( diff > 180.0 ) { diff -= 360.0; }
437 SG_LOG( SG_GENERAL, SG_INFO,
438 "Runway -" << r.rwy_no << " heading = " <<
440 " diff = " << diff );
441 if ( diff < min_diff ) {
450 SG_LOG( SG_GENERAL, SG_INFO, "closest runway = " << found_r.rwy_no
451 << " + " << found_dir );
457 double heading = found_r.heading + found_dir;
458 while ( heading >= 360.0 ) { heading -= 360.0; }
460 double lat2, lon2, az2;
461 double azimuth = found_r.heading + found_dir + 180.0;
462 while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
464 SG_LOG( SG_GENERAL, SG_INFO,
465 "runway = " << found_r.lon << ", " << found_r.lat
466 << " length = " << found_r.length * SG_FEET_TO_METER * 0.5
467 << " heading = " << azimuth );
469 geo_direct_wgs_84 ( 0, found_r.lat, found_r.lon,
470 azimuth, found_r.length * SG_FEET_TO_METER * 0.5 - 5.0,
471 &lat2, &lon2, &az2 );
473 if ( fabs( fgGetDouble("/sim/startup/offset-distance") ) > SG_EPSILON ) {
475 double odist = fgGetDouble("/sim/startup/offset-distance");
476 odist *= SG_NM_TO_METER;
477 double oaz = azimuth;
478 if ( fabs(fgGetDouble("/sim/startup/offset-azimuth")) > SG_EPSILON ) {
479 oaz = fgGetDouble("/sim/startup/offset-azimuth") + 180;
481 while ( oaz >= 360.0 ) { oaz -= 360.0; }
482 geo_direct_wgs_84 ( 0, lat2, lon2, oaz, odist, &olat, &olon, &az2 );
487 fgSetDouble("/position/longitude-deg", lon2 );
488 fgSetDouble("/position/latitude-deg", lat2 );
489 fgSetDouble("/orientation/heading-deg", heading );
491 SG_LOG( SG_GENERAL, SG_INFO,
492 "Position for " << id << " is ("
494 << lat2 << ") new heading is "
501 void fgSetPosFromGlideSlope(void) {
502 double gs = fgGetDouble("/velocities/glideslope");
503 double od = fgGetDouble("/sim/startup/offset-distance");
504 double alt = fgGetDouble("/position/altitude-ft");
506 //if glideslope and offset-distance are set and altitude is
507 //not, calculate the initial altitude
508 if( fabs(gs) > 0.01 && fabs(od) > 0.1 && alt < -9990 ) {
509 od *= SG_NM_TO_METER * SG_METER_TO_FEET;
510 alt = fabs(od*tan(gs));
511 fgSetDouble("/position/altitude-ft",alt);
512 fgSetBool("/sim/startup/onground", false);
513 SG_LOG(SG_GENERAL,SG_INFO, "Calculated altitude as: " << alt << " ft");
514 } else if( fabs(gs) > 0.01 && alt > 0 && fabs(od) < 0.1) {
516 od *= -1*SG_FEET_TO_METER * SG_METER_TO_NM;
517 fgSetDouble("/sim/startup/offset-distance",od);
518 SG_LOG(SG_GENERAL,SG_INFO, "Calculated offset distance as: "
520 } else if( fabs(gs) > 0.01 ) {
521 SG_LOG(SG_GENERAL,SG_ALERT, "Glideslope given but not altitude"
522 << " or offset-distance. Resetting"
523 << " glideslope to zero" );
524 fgSetDouble("/velocities/glideslope",0);
529 // General house keeping initializations
530 bool fgInitGeneral( void ) {
533 #if defined(FX) && defined(XMESA)
534 char *mesa_win_state;
537 SG_LOG( SG_GENERAL, SG_INFO, "General Initialization" );
538 SG_LOG( SG_GENERAL, SG_INFO, "======= ==============" );
540 root = globals->get_fg_root();
541 if ( ! root.length() ) {
542 // No root path set? Then bail ...
543 SG_LOG( SG_GENERAL, SG_ALERT,
544 "Cannot continue without a path to the base package "
545 << "being defined." );
548 SG_LOG( SG_GENERAL, SG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
550 #if defined(FX) && defined(XMESA)
551 // initialize full screen flag
552 globals->set_fullscreen(false);
553 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
554 // Test for the MESA_GLX_FX env variable
555 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
556 // test if we are fullscreen mesa/glide
557 if ( (mesa_win_state[0] == 'f') ||
558 (mesa_win_state[0] == 'F') ) {
559 globals->set_fullscreen(true);
569 // Initialize the flight model subsystem. This just creates the
570 // object. The actual fdm initialization is delayed until we get a
571 // proper scenery elevation hit. This is checked for in main.cxx
575 if ( cur_fdm_state ) {
576 delete cur_fdm_state;
580 double dt = 1.0 / fgGetInt("/sim/model-hz");
581 aircraft_dir = fgGetString("/sim/aircraft-dir");
582 const string &model = fgGetString("/sim/flight-model");
585 if ( model == "larcsim" ) {
586 cur_fdm_state = new FGLaRCsim( dt );
587 } else if ( model == "jsb" ) {
588 cur_fdm_state = new FGJSBsim( dt );
589 } else if ( model == "ada" ) {
590 cur_fdm_state = new FGADA( dt );
591 } else if ( model == "balloon" ) {
592 cur_fdm_state = new FGBalloonSim( dt );
593 } else if ( model == "magic" ) {
594 cur_fdm_state = new FGMagicCarpet( dt );
595 } else if ( model == "ufo" ) {
596 cur_fdm_state = new FGUFO( dt );
597 } else if ( model == "external" ) {
598 cur_fdm_state = new FGExternal( dt );
599 } else if ( model.find("network") == 0 ) {
600 string host = "localhost";
604 string net_options = model.substr(8);
605 string::size_type begin, end;
608 end = net_options.find( ",", begin );
609 if ( end != string::npos ) {
610 host = net_options.substr(begin, end - begin);
614 end = net_options.find( ",", begin );
615 if ( end != string::npos ) {
616 port1 = atoi( net_options.substr(begin, end - begin).c_str() );
620 end = net_options.find( ",", begin );
621 if ( end != string::npos ) {
622 port2 = atoi( net_options.substr(begin, end - begin).c_str() );
626 end = net_options.find( ",", begin );
627 if ( end != string::npos ) {
628 port3 = atoi( net_options.substr(begin, end - begin).c_str() );
631 cur_fdm_state = new FGExternalNet( dt, host, port1, port2, port3 );
632 } else if ( model == "null" ) {
633 cur_fdm_state = new FGNullFDM( dt );
634 } else if ( model == "yasim" ) {
635 cur_fdm_state = new YASim( dt );
637 SG_LOG(SG_GENERAL, SG_ALERT,
638 "Unrecognized flight model '" << model
639 << "', cannot init flight dynamics model.");
643 SG_LOG(SG_GENERAL, SG_ALERT, "FlightGear aborting\n\n");
649 // Initialize view parameters
651 // force update of model so that viewer can get some data...
652 globals->get_aircraft_model()->update(0);
653 // run update for current view so that data is current...
654 globals->get_viewmgr()->update(0);
658 SGTime *fgInitTime() {
660 static const SGPropertyNode *longitude
661 = fgGetNode("/position/longitude-deg");
662 static const SGPropertyNode *latitude
663 = fgGetNode("/position/latitude-deg");
664 static const SGPropertyNode *cur_time_override
665 = fgGetNode("/sim/time/cur-time-override", true);
667 SGPath zone( globals->get_fg_root() );
668 zone.append( "Timezone" );
669 SGTime *t = new SGTime( longitude->getDoubleValue()
670 * SGD_DEGREES_TO_RADIANS,
671 latitude->getDoubleValue()
672 * SGD_DEGREES_TO_RADIANS,
674 cur_time_override->getLongValue() );
676 // Handle potential user specified time offsets
677 time_t cur_time = t->get_cur_time();
678 time_t currGMT = sgTimeGetGMT( gmtime(&cur_time) );
679 time_t systemLocalTime = sgTimeGetGMT( localtime(&cur_time) );
680 time_t aircraftLocalTime =
681 sgTimeGetGMT( fgLocaltime(&cur_time, t->get_zonename() ) );
683 // Okay, we now have six possible scenarios
684 int offset = fgGetInt("/sim/startup/time-offset");
685 const string &offset_type = fgGetString("/sim/startup/time-offset-type");
686 if (offset_type == "system-offset") {
687 globals->set_warp( offset );
688 } else if (offset_type == "gmt-offset") {
689 globals->set_warp( offset - (currGMT - systemLocalTime) );
690 } else if (offset_type == "latitude-offset") {
691 globals->set_warp( offset - (aircraftLocalTime - systemLocalTime) );
692 } else if (offset_type == "system") {
693 globals->set_warp( offset - cur_time );
694 } else if (offset_type == "gmt") {
695 globals->set_warp( offset - currGMT );
696 } else if (offset_type == "latitude") {
697 globals->set_warp( offset - (aircraftLocalTime - systemLocalTime) -
700 SG_LOG( SG_GENERAL, SG_ALERT,
701 "FG_TIME::Unsupported offset type " << offset_type );
705 SG_LOG( SG_GENERAL, SG_INFO, "After time init, warp = "
706 << globals->get_warp() );
708 globals->set_warp_delta( 0 );
711 cur_time_override->getLongValue(),
712 globals->get_warp() );
718 // This is the top level init routine which calls all the other
719 // initialization routines. If you are adding a subsystem to flight
720 // gear, its initialization call should located in this routine.
721 // Returns non-zero if a problem encountered.
722 bool fgInitSubsystems( void ) {
723 static const SGPropertyNode *longitude
724 = fgGetNode("/position/longitude-deg");
725 static const SGPropertyNode *latitude
726 = fgGetNode("/position/latitude-deg");
727 static const SGPropertyNode *altitude
728 = fgGetNode("/position/altitude-ft");
730 fgLIGHT *l = &cur_light_params;
732 SG_LOG( SG_GENERAL, SG_INFO, "Initialize Subsystems");
733 SG_LOG( SG_GENERAL, SG_INFO, "========== ==========");
736 ////////////////////////////////////////////////////////////////////
737 // Initialize the material property subsystem.
738 ////////////////////////////////////////////////////////////////////
740 SGPath mpath( globals->get_fg_root() );
741 mpath.append( "materials.xml" );
742 if ( ! material_lib.load( mpath.str() ) ) {
743 SG_LOG( SG_GENERAL, SG_ALERT, "Error loading material lib!" );
747 ////////////////////////////////////////////////////////////////////
748 // Initialize the event manager subsystem.
749 ////////////////////////////////////////////////////////////////////
751 global_events.init();
753 // Output event stats every 60 seconds
754 global_events.Register( "FGEventMgr::print_stats()",
755 &global_events, &FGEventMgr::print_stats,
759 ////////////////////////////////////////////////////////////////////
760 // Initialize the scenery management subsystem.
761 ////////////////////////////////////////////////////////////////////
763 if ( global_tile_mgr.init() ) {
764 // Load the local scenery data
765 double visibility_meters = fgGetDouble("/environment/visibility-m");
767 global_tile_mgr.update( longitude->getDoubleValue(),
768 latitude->getDoubleValue(),
771 SG_LOG( SG_GENERAL, SG_ALERT, "Error in Tile Manager initialization!" );
775 // cause refresh of viewer scenery timestamps every 15 seconds...
776 global_events.Register( "FGTileMgr::refresh_view_timestamps()",
777 &global_tile_mgr, &FGTileMgr::refresh_view_timestamps,
780 SG_LOG( SG_GENERAL, SG_DEBUG,
781 "Current terrain elevation after tile mgr init " <<
782 globals->get_scenery()->get_cur_elev() );
785 ////////////////////////////////////////////////////////////////////
786 // Initialize the flight model subsystem.
787 ////////////////////////////////////////////////////////////////////
791 // allocates structures so must happen before any of the flight
792 // model or control parameters are set
793 fgAircraftInit(); // In the future this might not be the case.
796 ////////////////////////////////////////////////////////////////////
797 // Initialize the view manager subsystem.
798 ////////////////////////////////////////////////////////////////////
803 ////////////////////////////////////////////////////////////////////
804 // Initialize the lighting subsystem.
805 ////////////////////////////////////////////////////////////////////
807 // fgUpdateSunPos() needs a few position and view parameters set
808 // so it can calculate local relative sun angle and a few other
809 // things for correctly orienting the sky.
812 global_events.Register( "fgUpdateSunPos()", &fgUpdateSunPos,
814 global_events.Register( "fgUpdateMoonPos()", &fgUpdateMoonPos,
817 // Initialize Lighting interpolation tables
820 // force one lighting update to make it right to start with...
822 // update the lighting parameters (based on sun angle)
823 global_events.Register( "fgLight::Update()",
824 &cur_light_params, &fgLIGHT::Update,
828 ////////////////////////////////////////////////////////////////////
829 // Initialize the logger.
830 ////////////////////////////////////////////////////////////////////
832 globals->set_logger(new FGLogger);
833 globals->get_logger()->init();
834 globals->get_logger()->bind();
837 ////////////////////////////////////////////////////////////////////
838 // Initialize the local time subsystem.
839 ////////////////////////////////////////////////////////////////////
841 // update the current timezone each 30 minutes
842 global_events.Register( "fgUpdateLocalTime()", &fgUpdateLocalTime,
846 ////////////////////////////////////////////////////////////////////
847 // Initialize the weather subsystem.
848 ////////////////////////////////////////////////////////////////////
850 // Initialize the weather modeling subsystem
852 // Initialize the WeatherDatabase
853 SG_LOG(SG_GENERAL, SG_INFO, "Creating LocalWeatherDatabase");
855 sgSetVec3( position, current_aircraft.fdm_state->get_Latitude(),
856 current_aircraft.fdm_state->get_Longitude(),
857 current_aircraft.fdm_state->get_Altitude() * SG_FEET_TO_METER );
858 double init_vis = fgGetDouble("/environment/visibility-m");
860 FGLocalWeatherDatabase::DatabaseWorkingType working_type;
862 if (!strcmp(fgGetString("/environment/weather/working-type"), "internet"))
864 working_type = FGLocalWeatherDatabase::use_internet;
866 working_type = FGLocalWeatherDatabase::default_mode;
869 if ( init_vis > 0 ) {
870 FGLocalWeatherDatabase::theFGLocalWeatherDatabase =
871 new FGLocalWeatherDatabase( position,
872 globals->get_fg_root(),
876 FGLocalWeatherDatabase::theFGLocalWeatherDatabase =
877 new FGLocalWeatherDatabase( position,
878 globals->get_fg_root(),
882 // cout << theFGLocalWeatherDatabase << endl;
883 // cout << "visibility = "
884 // << theFGLocalWeatherDatabase->getWeatherVisibility() << endl;
886 WeatherDatabase = FGLocalWeatherDatabase::theFGLocalWeatherDatabase;
888 // register the periodic update of the weather
889 global_events.Register( "weather update", &fgUpdateWeatherDatabase,
892 globals->get_environment_mgr()->init();
893 globals->get_environment_mgr()->bind();
896 ////////////////////////////////////////////////////////////////////
897 // Initialize the 3D cloud subsystem.
898 ////////////////////////////////////////////////////////////////////
900 if ( fgGetBool("/sim/rendering/clouds3d") ) {
901 SGPath cloud_path(globals->get_fg_root());
902 cloud_path.append("large.sky");
903 if ( !sgCloud3d->Load( cloud_path.str() ) ) {
904 fgSetBool("/sim/rendering/clouds3d", false);
909 ////////////////////////////////////////////////////////////////////
910 // Initialize vor/ndb/ils/fix list management and query systems
911 ////////////////////////////////////////////////////////////////////
913 SG_LOG(SG_GENERAL, SG_INFO, "Loading Navaids");
915 SG_LOG(SG_GENERAL, SG_INFO, " VOR/NDB");
916 current_navlist = new FGNavList;
917 SGPath p_nav( globals->get_fg_root() );
918 p_nav.append( "Navaids/default.nav" );
919 current_navlist->init( p_nav );
921 SG_LOG(SG_GENERAL, SG_INFO, " ILS and Marker Beacons");
922 current_beacons = new FGMarkerBeacons;
923 current_beacons->init();
924 current_ilslist = new FGILSList;
925 SGPath p_ils( globals->get_fg_root() );
926 p_ils.append( "Navaids/default.ils" );
927 current_ilslist->init( p_ils );
929 SG_LOG(SG_GENERAL, SG_INFO, " Fixes");
930 current_fixlist = new FGFixList;
931 SGPath p_fix( globals->get_fg_root() );
932 p_fix.append( "Navaids/default.fix" );
933 current_fixlist->init( p_fix );
935 ////////////////////////////////////////////////////////////////////
936 // Initialize ATC list management and query systems
937 ////////////////////////////////////////////////////////////////////
939 SG_LOG(SG_GENERAL, SG_INFO, " ATIS");
940 current_atislist = new FGATISList;
941 SGPath p_atis( globals->get_fg_root() );
942 p_atis.append( "ATC/default.atis" );
943 current_atislist->init( p_atis );
945 SG_LOG(SG_GENERAL, SG_INFO, " Tower");
946 current_towerlist = new FGTowerList;
947 SGPath p_tower( globals->get_fg_root() );
948 p_tower.append( "ATC/default.tower" );
949 current_towerlist->init( p_tower );
951 SG_LOG(SG_GENERAL, SG_INFO, " Approach");
952 current_approachlist = new FGApproachList;
953 SGPath p_approach( globals->get_fg_root() );
954 p_approach.append( "ATC/default.approach" );
955 current_approachlist->init( p_approach );
957 ////////////////////////////////////////////////////////////////////
958 // Initialise ATC display system
959 ////////////////////////////////////////////////////////////////////
961 SG_LOG(SG_GENERAL, SG_INFO, " ATC Display");
962 globals->set_ATC_display(new FGATCDisplay);
963 globals->get_ATC_display()->init();
965 ////////////////////////////////////////////////////////////////////
966 // Initialise the ATC Manager
967 ////////////////////////////////////////////////////////////////////
969 SG_LOG(SG_GENERAL, SG_INFO, " ATC Manager");
970 globals->set_ATC_mgr(new FGATCMgr);
971 globals->get_ATC_mgr()->init();
973 ////////////////////////////////////////////////////////////////////
974 // Initialise the AI Manager
975 ////////////////////////////////////////////////////////////////////
977 SG_LOG(SG_GENERAL, SG_INFO, " AI Manager");
978 // globals->set_AI_mgr(new FGAIMgr);
979 // globals->get_AI_mgr()->init();
981 ////////////////////////////////////////////////////////////////////
982 // Initialize the built-in commands.
983 ////////////////////////////////////////////////////////////////////
987 #ifdef ENABLE_AUDIO_SUPPORT
988 ////////////////////////////////////////////////////////////////////
989 // Initialize the sound subsystem.
990 ////////////////////////////////////////////////////////////////////
992 globals->set_soundmgr(new FGSoundMgr);
993 globals->get_soundmgr()->init();
994 globals->get_soundmgr()->bind();
997 ////////////////////////////////////////////////////////////////////
998 // Initialize the sound-effects subsystem.
999 ////////////////////////////////////////////////////////////////////
1000 globals->set_fx(new FGFX);
1001 globals->get_fx()->init();
1002 globals->get_fx()->bind();
1006 ////////////////////////////////////////////////////////////////////
1007 // Initialize the radio stack subsystem.
1008 ////////////////////////////////////////////////////////////////////
1010 // A textbook example of how FGSubsystem
1012 current_radiostack = new FGRadioStack;
1013 current_radiostack->init();
1014 current_radiostack->bind();
1017 ////////////////////////////////////////////////////////////////////
1018 // Initialize the cockpit subsystem
1019 ////////////////////////////////////////////////////////////////////
1021 if( fgCockpitInit( ¤t_aircraft )) {
1022 // Cockpit initialized ok.
1024 SG_LOG( SG_GENERAL, SG_ALERT, "Error in Cockpit initialization!" );
1029 ////////////////////////////////////////////////////////////////////
1030 // Initialize the autopilot subsystem.
1031 ////////////////////////////////////////////////////////////////////
1033 globals->set_autopilot(new FGAutopilot);
1034 globals->get_autopilot()->init();
1035 globals->get_autopilot()->bind();
1037 // FIXME: these should go in the
1038 // GUI initialization code, not here.
1040 NewTgtAirportInit();
1044 ////////////////////////////////////////////////////////////////////
1045 // Initialize I/O subsystem.
1046 ////////////////////////////////////////////////////////////////////
1048 globals->get_io()->init();
1049 globals->get_io()->bind();
1051 // Initialize the 2D panel.
1052 string panel_path = fgGetString("/sim/panel/path",
1053 "Panels/Default/default.xml");
1054 current_panel = fgReadPanel(panel_path);
1055 if (current_panel == 0) {
1056 SG_LOG( SG_INPUT, SG_ALERT,
1057 "Error reading new panel from " << panel_path );
1059 SG_LOG( SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path );
1060 current_panel->init();
1061 current_panel->bind();
1065 ////////////////////////////////////////////////////////////////////
1066 // Initialize the default (kludged) properties.
1067 ////////////////////////////////////////////////////////////////////
1072 ////////////////////////////////////////////////////////////////////
1073 // Initialize the controls subsystem.
1074 ////////////////////////////////////////////////////////////////////
1076 globals->get_controls()->init();
1077 globals->get_controls()->bind();
1080 ////////////////////////////////////////////////////////////////////
1081 // Initialize the steam subsystem.
1082 ////////////////////////////////////////////////////////////////////
1084 globals->get_steam()->init();
1085 globals->get_steam()->bind();
1088 ////////////////////////////////////////////////////////////////////
1089 // Initialize the input subsystem.
1090 ////////////////////////////////////////////////////////////////////
1092 current_input.init();
1093 current_input.bind();
1096 ////////////////////////////////////////////////////////////////////////
1097 // End of subsystem initialization.
1098 ////////////////////////////////////////////////////////////////////
1100 SG_LOG( SG_GENERAL, SG_INFO, endl);
1102 // Save the initial state for future
1104 globals->saveInitialState();
1110 void fgReInitSubsystems( void )
1112 static const SGPropertyNode *longitude
1113 = fgGetNode("/position/longitude-deg");
1114 static const SGPropertyNode *latitude
1115 = fgGetNode("/position/latitude-deg");
1116 static const SGPropertyNode *altitude
1117 = fgGetNode("/position/altitude-ft");
1118 static const SGPropertyNode *master_freeze
1119 = fgGetNode("/sim/freeze/master");
1121 SG_LOG( SG_GENERAL, SG_INFO,
1122 "fgReInitSubsystems(): /position/altitude = "
1123 << altitude->getDoubleValue() );
1125 bool freeze = master_freeze->getBoolValue();
1127 fgSetBool("/sim/freeze/master", true);
1130 // Initialize the Scenery Management subsystem
1131 // FIXME, what really needs to get initialized here, at the time
1132 // this was commented out, scenery.init() was a noop
1137 // allocates structures so must happen before any of the flight
1138 // model or control parameters are set
1139 fgAircraftInit(); // In the future this might not be the case.
1141 // copy viewer settings into current-view path
1142 globals->get_viewmgr()->copyToCurrent();
1146 globals->get_controls()->reset_all();
1147 globals->get_autopilot()->reset();
1151 cur_light_params.Update();
1152 fgUpdateLocalTime();
1155 fgSetBool("/sim/freeze/master", false);