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
44 #if defined( unix ) || defined( __CYGWIN__ )
45 # include <unistd.h> // for gethostname()
48 // work around a stdc++ lib bug in some versions of linux, but doesn't
49 // seem to hurt to have this here for all versions of Linux.
51 # define _G_NO_EXTERN_TEMPLATES
54 #include <simgear/compiler.h>
55 #include <simgear/misc/exception.hxx>
59 #include <simgear/constants.h>
60 #include <simgear/debug/logstream.hxx>
61 #include <simgear/math/point3d.hxx>
62 #include <simgear/math/polar3d.hxx>
63 #include <simgear/math/sg_geodesy.hxx>
64 #include <simgear/misc/sg_path.hxx>
65 #include <simgear/timing/sg_time.hxx>
66 #include <simgear/timing/lowleveltime.h>
68 #include <Aircraft/aircraft.hxx>
69 #include <FDM/UIUCModel/uiuc_aircraftdir.h>
70 #include <Airports/runways.hxx>
71 #include <Airports/simple.hxx>
72 #include <ATC/ATCdisplay.hxx>
73 #include <ATC/ATCmgr.hxx>
74 #include <ATC/atislist.hxx>
75 #include <Autopilot/auto_gui.hxx>
76 #include <Autopilot/newauto.hxx>
77 #include <Cockpit/cockpit.hxx>
78 #include <Cockpit/radiostack.hxx>
79 #include <Cockpit/panel.hxx>
80 #include <Cockpit/panel_io.hxx>
81 #include <FDM/ADA.hxx>
82 #include <FDM/Balloon.h>
83 #include <FDM/External.hxx>
84 #include <FDM/ExternalNet.hxx>
85 #include <FDM/JSBSim/JSBSim.hxx>
86 #include <FDM/LaRCsim.hxx>
87 #include <FDM/MagicCarpet.hxx>
88 #include <FDM/NullFDM.hxx>
89 #include <FDM/YASim/YASim.hxx>
90 #include <Include/general.hxx>
91 #include <Input/input.hxx>
92 // #include <Joystick/joystick.hxx>
93 #include <Objects/matlib.hxx>
94 #include <Navaids/fixlist.hxx>
95 #include <Navaids/ilslist.hxx>
96 #include <Navaids/mkrbeacons.hxx>
97 #include <Navaids/navlist.hxx>
98 #include <Scenery/scenery.hxx>
99 #include <Scenery/tilemgr.hxx>
100 #include <Sound/fg_fx.hxx>
101 #include <Sound/soundmgr.hxx>
102 #include <Time/event.hxx>
103 #include <Time/light.hxx>
104 #include <Time/sunpos.hxx>
105 #include <Time/moonpos.hxx>
106 #include <Time/tmp.hxx>
108 #ifndef FG_NEW_ENVIRONMENT
109 # include <WeatherCM/FGLocalWeatherDatabase.h>
111 # include <Environment/environment_mgr.hxx>
114 #include "fg_init.hxx"
116 #include "fg_commands.hxx"
117 #include "fg_props.hxx"
118 #include "options.hxx"
119 #include "globals.hxx"
120 #include "logger.hxx"
121 #include "viewmgr.hxx"
123 #if defined(FX) && defined(XMESA)
124 #include <GL/xmesa.h>
127 SG_USING_STD(string);
129 extern const char *default_root;
132 // Read in configuration (file and command line) and just set fg_root
133 bool fgInitFGRoot ( int argc, char **argv ) {
137 // First parse command line options looking for fg-root, this will
138 // override anything specified in a config file
139 root = fgScanForRoot(argc, argv);
141 #if defined( unix ) || defined( __CYGWIN__ )
142 // Next check home directory for .fgfsrc.hostname file
144 envp = ::getenv( "HOME" );
145 if ( envp != NULL ) {
146 SGPath config( envp );
147 config.append( ".fgfsrc" );
149 gethostname( name, 256 );
150 config.concat( "." );
151 config.concat( name );
152 root = fgScanForRoot(config.str());
157 // Next check home directory for .fgfsrc file
159 envp = ::getenv( "HOME" );
160 if ( envp != NULL ) {
161 SGPath config( envp );
162 config.append( ".fgfsrc" );
163 root = fgScanForRoot(config.str());
167 // Next check if fg-root is set as an env variable
169 envp = ::getenv( "FG_ROOT" );
170 if ( envp != NULL ) {
175 // Otherwise, default to a random compiled-in location if we can't
176 // find fg-root any other way.
178 #if defined( __CYGWIN__ )
179 root = "/FlightGear";
180 #elif defined( WIN32 )
181 root = "\\FlightGear";
182 #elif defined( macintosh )
189 SG_LOG(SG_INPUT, SG_INFO, "fg_root = " << root );
190 globals->set_fg_root(root);
196 // Return the current base package version
197 string fgBasePackageVersion() {
198 SGPath base_path( globals->get_fg_root() );
199 base_path.append("version");
201 sg_gzifstream in( base_path.str() );
202 if ( !in.is_open() ) {
203 SGPath old_path( globals->get_fg_root() );
204 old_path.append( "Thanks" );
205 sg_gzifstream old( old_path.str() );
206 if ( !old.is_open() ) {
209 return "[old version]";
220 // Read in configuration (file and command line)
221 bool fgInitConfig ( int argc, char **argv ) {
223 // First, set some sane default values
226 // Read global preferences from $FG_ROOT/preferences.xml
227 SGPath props_path(globals->get_fg_root());
228 props_path.append("preferences.xml");
229 SG_LOG(SG_INPUT, SG_INFO, "Reading global preferences");
231 readProperties(props_path.str(), globals->get_props());
232 } catch (const sg_exception &e) {
233 string message = "Error reading global preferences: ";
234 message += e.getFormattedMessage();
235 SG_LOG(SG_INPUT, SG_ALERT, message);
238 SG_LOG(SG_INPUT, SG_INFO, "Finished Reading global preferences");
240 // Read the default aircraft config file.
241 string aircraft = fgGetString("/sim/aircraft", "");
242 if (aircraft.size() > 0) {
243 SGPath aircraft_path(globals->get_fg_root());
244 aircraft_path.append("Aircraft");
245 aircraft_path.append(aircraft);
246 aircraft_path.concat("-set.xml");
247 SG_LOG(SG_INPUT, SG_INFO, "Reading default aircraft: " << aircraft
248 << " from " << aircraft_path.str());
250 readProperties(aircraft_path.str(), globals->get_props());
251 } catch (const sg_exception &e) {
252 string message = "Error reading default aircraft: ";
253 message += e.getFormattedMessage();
254 SG_LOG(SG_INPUT, SG_ALERT, message);
258 SG_LOG(SG_INPUT, SG_ALERT, "No default aircraft specified");
261 // Attempt to locate and parse the various config files in order
262 // from least precidence to greatest precidence
264 // Check for $fg_root/system.fgfsrc
265 SGPath config( globals->get_fg_root() );
266 config.append( "system.fgfsrc" );
267 fgParseOptions(config.str());
270 #if defined( unix ) || defined( __CYGWIN__ )
271 // Check for $fg_root/system.fgfsrc.hostname
272 gethostname( name, 256 );
273 config.concat( "." );
274 config.concat( name );
275 fgParseOptions(config.str());
278 // Check for ~/.fgfsrc
279 char* envp = ::getenv( "HOME" );
280 if ( envp != NULL ) {
282 config.append( ".fgfsrc" );
283 fgParseOptions(config.str());
286 #if defined( unix ) || defined( __CYGWIN__ )
287 // Check for ~/.fgfsrc.hostname
288 gethostname( name, 256 );
289 config.concat( "." );
290 config.concat( name );
291 fgParseOptions(config.str());
294 // Parse remaining command line options
295 // These will override anything specified in a config file
296 fgParseArgs(argc, argv);
302 // find basic airport location info from airport database
303 bool fgFindAirportID( const string& id, FGAirport *a ) {
305 SGPath path( globals->get_fg_root() );
306 path.append( "Airports" );
307 path.append( "simple.mk4" );
308 FGAirports airports( path.c_str() );
310 SG_LOG( SG_GENERAL, SG_INFO, "Searching for airport code = " << id );
312 if ( ! airports.search( id, a ) ) {
313 SG_LOG( SG_GENERAL, SG_ALERT,
314 "Failed to find " << id << " in " << path.str() );
321 SG_LOG( SG_GENERAL, SG_INFO,
322 "Position for " << id << " is ("
323 << a->longitude << ", "
324 << a->latitude << ")" );
330 // Set current_options lon/lat given an airport id
331 bool fgSetPosFromAirportID( const string& id ) {
335 SG_LOG( SG_GENERAL, SG_INFO,
336 "Attempting to set starting position from airport code " << id );
338 if ( fgFindAirportID( id, &a ) ) {
339 fgSetDouble("/position/longitude-deg", a.longitude );
340 fgSetDouble("/position/latitude-deg", a.latitude );
341 SG_LOG( SG_GENERAL, SG_INFO,
342 "Position for " << id << " is ("
343 << a.longitude << ", "
344 << a.latitude << ")" );
354 // Set current_options lon/lat given an airport id and heading (degrees)
355 bool fgSetPosFromAirportIDandHdg( const string& id, double tgt_hdg ) {
358 double found_dir = 0.0;
361 // set initial position from runway and heading
363 SGPath path( globals->get_fg_root() );
364 path.append( "Airports" );
365 path.append( "runways.mk4" );
366 FGRunways runways( path.c_str() );
368 SG_LOG( SG_GENERAL, SG_INFO,
369 "Attempting to set starting position from runway code "
370 << id << " heading " << tgt_hdg );
372 // SGPath inpath( globals->get_fg_root() );
373 // inpath.append( "Airports" );
374 // inpath.append( "apt_simple" );
375 // airports.load( inpath.c_str() );
377 // SGPath outpath( globals->get_fg_root() );
378 // outpath.append( "Airports" );
379 // outpath.append( "simple.gdbm" );
380 // airports.dump_gdbm( outpath.c_str() );
382 if ( ! runways.search( id, &r ) ) {
383 SG_LOG( SG_GENERAL, SG_ALERT,
384 "Failed to find " << id << " in database." );
389 double min_diff = 360.0;
391 while ( r.id == id ) {
393 diff = tgt_hdg - r.heading;
394 while ( diff < -180.0 ) { diff += 360.0; }
395 while ( diff > 180.0 ) { diff -= 360.0; }
397 SG_LOG( SG_GENERAL, SG_INFO,
398 "Runway " << r.rwy_no << " heading = " << r.heading <<
399 " diff = " << diff );
400 if ( diff < min_diff ) {
407 diff = tgt_hdg - r.heading - 180.0;
408 while ( diff < -180.0 ) { diff += 360.0; }
409 while ( diff > 180.0 ) { diff -= 360.0; }
411 SG_LOG( SG_GENERAL, SG_INFO,
412 "Runway -" << r.rwy_no << " heading = " <<
414 " diff = " << diff );
415 if ( diff < min_diff ) {
424 SG_LOG( SG_GENERAL, SG_INFO, "closest runway = " << found_r.rwy_no
425 << " + " << found_dir );
431 double heading = found_r.heading + found_dir;
432 while ( heading >= 360.0 ) { heading -= 360.0; }
434 double lat2, lon2, az2;
435 double azimuth = found_r.heading + found_dir + 180.0;
436 while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
438 SG_LOG( SG_GENERAL, SG_INFO,
439 "runway = " << found_r.lon << ", " << found_r.lat
440 << " length = " << found_r.length * SG_FEET_TO_METER * 0.5
441 << " heading = " << azimuth );
443 geo_direct_wgs_84 ( 0, found_r.lat, found_r.lon,
444 azimuth, found_r.length * SG_FEET_TO_METER * 0.5 - 5.0,
445 &lat2, &lon2, &az2 );
447 if ( fabs( fgGetDouble("/sim/startup/offset-distance") ) > SG_EPSILON ) {
449 double odist = fgGetDouble("/sim/startup/offset-distance");
450 odist *= SG_NM_TO_METER;
451 double oaz = azimuth;
452 if ( fabs(fgGetDouble("/sim/startup/offset-azimuth")) > SG_EPSILON ) {
453 oaz = fgGetDouble("/sim/startup/offset-azimuth") + 180;
455 while ( oaz >= 360.0 ) { oaz -= 360.0; }
456 geo_direct_wgs_84 ( 0, lat2, lon2, oaz, odist, &olat, &olon, &az2 );
461 fgSetDouble("/position/longitude-deg", lon2 );
462 fgSetDouble("/position/latitude-deg", lat2 );
463 fgSetDouble("/orientation/heading-deg", heading );
465 SG_LOG( SG_GENERAL, SG_INFO,
466 "Position for " << id << " is ("
468 << lat2 << ") new heading is "
474 void fgSetPosFromGlideSlope(void) {
475 double gs = fgGetDouble("/velocities/glideslope");
476 double od = fgGetDouble("/sim/startup/offset-distance");
477 double alt = fgGetDouble("/position/altitude-ft");
479 //if glideslope and offset-distance are set and altitude is
480 //not, calculate the initial altitude
481 if( fabs(gs) > 0.01 && fabs(od) > 0.1 && alt < -9990 ) {
482 od *= SG_NM_TO_METER * SG_METER_TO_FEET;
483 alt = fabs(od*tan(gs));
484 fgSetDouble("/position/altitude-ft",alt);
485 fgSetBool("/sim/startup/onground", false);
486 SG_LOG(SG_GENERAL,SG_INFO, "Calculated altitude as: " << alt << " ft");
487 } else if( fabs(gs) > 0.01 && alt > 0 && fabs(od) < 0.1) {
489 od *= -1*SG_FEET_TO_METER * SG_METER_TO_NM;
490 fgSetDouble("/sim/startup/offset-distance",od);
491 SG_LOG(SG_GENERAL,SG_INFO, "Calculated offset distance as: "
493 } else if( fabs(gs) > 0.01 ) {
494 SG_LOG(SG_GENERAL,SG_ALERT, "Glideslope given but not altitude"
495 << " or offset-distance. Resetting"
496 << " glideslope to zero" );
497 fgSetDouble("/velocities/glideslope",0);
502 // General house keeping initializations
503 bool fgInitGeneral( void ) {
506 #if defined(FX) && defined(XMESA)
507 char *mesa_win_state;
510 SG_LOG( SG_GENERAL, SG_INFO, "General Initialization" );
511 SG_LOG( SG_GENERAL, SG_INFO, "======= ==============" );
513 root = globals->get_fg_root();
514 if ( ! root.length() ) {
515 // No root path set? Then bail ...
516 SG_LOG( SG_GENERAL, SG_ALERT,
517 "Cannot continue without a path to the base package "
518 << "being defined." );
521 SG_LOG( SG_GENERAL, SG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
523 #if defined(FX) && defined(XMESA)
524 // initialize full screen flag
525 globals->set_fullscreen(false);
526 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
527 // Test for the MESA_GLX_FX env variable
528 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
529 // test if we are fullscreen mesa/glide
530 if ( (mesa_win_state[0] == 'f') ||
531 (mesa_win_state[0] == 'F') ) {
532 globals->set_fullscreen(true);
542 // Initialize the flight model subsystem. This just creates the
543 // object. The actual fdm initialization is delayed until we get a
544 // proper scenery elevation hit. This is checked for in main.cxx
548 if ( cur_fdm_state ) {
549 delete cur_fdm_state;
553 double dt = 1.0 / fgGetInt("/sim/model-hz");
554 aircraft_dir = fgGetString("/sim/aircraft-dir");
555 const string &model = fgGetString("/sim/flight-model");
558 if (model == "larcsim") {
559 cur_fdm_state = new FGLaRCsim( dt );
560 } else if (model == "jsb") {
561 cur_fdm_state = new FGJSBsim( dt );
562 } else if (model == "ada") {
563 cur_fdm_state = new FGADA( dt );
564 } else if (model == "balloon") {
565 cur_fdm_state = new FGBalloonSim( dt );
566 } else if (model == "magic") {
567 cur_fdm_state = new FGMagicCarpet( dt );
568 } else if (model == "external") {
569 cur_fdm_state = new FGExternal( dt );
570 } else if (model == "network") {
571 cur_fdm_state = new FGExternalNet( dt, 5501, 5502, 5503, "localhost" );
572 } else if (model == "null") {
573 cur_fdm_state = new FGNullFDM( dt );
574 } else if (model == "yasim") {
575 cur_fdm_state = new YASim( dt );
577 SG_LOG(SG_GENERAL, SG_ALERT,
578 "Unrecognized flight model '" << model
579 << "', cannot init flight dynamics model.");
583 SG_LOG(SG_GENERAL, SG_ALERT, "FlightGear aborting\n\n");
589 // Initialize view parameters
591 // Initialize pilot view
592 static const SGPropertyNode *longitude
593 = fgGetNode("/position/longitude-deg");
594 static const SGPropertyNode *latitude
595 = fgGetNode("/position/latitude-deg");
596 static const SGPropertyNode *altitude
597 = fgGetNode("/position/altitude-ft");
599 FGViewerRPH *pilot_view
600 = (FGViewerRPH *)globals->get_viewmgr()->get_view( 0 );
602 pilot_view->set_geod_view_pos( longitude->getDoubleValue()
603 * SGD_DEGREES_TO_RADIANS,
604 latitude->getDoubleValue()
605 * SGD_DEGREES_TO_RADIANS,
606 altitude->getDoubleValue()
607 * SG_FEET_TO_METER );
608 pilot_view->set_rph( cur_fdm_state->get_Phi(),
609 cur_fdm_state->get_Theta(),
610 cur_fdm_state->get_Psi() );
612 // set current view to 0 (first) which is our main pilot view
613 globals->set_current_view( pilot_view );
615 SG_LOG( SG_GENERAL, SG_DEBUG, " abs_view_pos = "
616 << globals->get_current_view()->get_abs_view_pos());
620 SGTime *fgInitTime() {
622 static const SGPropertyNode *longitude
623 = fgGetNode("/position/longitude-deg");
624 static const SGPropertyNode *latitude
625 = fgGetNode("/position/latitude-deg");
626 static const SGPropertyNode *cur_time_override
627 = fgGetNode("/sim/time/cur-time-override", true);
629 SGPath zone( globals->get_fg_root() );
630 zone.append( "Timezone" );
631 SGTime *t = new SGTime( longitude->getDoubleValue()
632 * SGD_DEGREES_TO_RADIANS,
633 latitude->getDoubleValue()
634 * SGD_DEGREES_TO_RADIANS,
636 cur_time_override->getLongValue() );
638 // Handle potential user specified time offsets
639 time_t cur_time = t->get_cur_time();
640 time_t currGMT = sgTimeGetGMT( gmtime(&cur_time) );
641 time_t systemLocalTime = sgTimeGetGMT( localtime(&cur_time) );
642 time_t aircraftLocalTime =
643 sgTimeGetGMT( fgLocaltime(&cur_time, t->get_zonename() ) );
645 // Okay, we now have six possible scenarios
646 int offset = fgGetInt("/sim/startup/time-offset");
647 const string &offset_type = fgGetString("/sim/startup/time-offset-type");
648 if (offset_type == "system-offset") {
649 globals->set_warp( offset );
650 } else if (offset_type == "gmt-offset") {
651 globals->set_warp( offset - (currGMT - systemLocalTime) );
652 } else if (offset_type == "latitude-offset") {
653 globals->set_warp( offset - (aircraftLocalTime - systemLocalTime) );
654 } else if (offset_type == "system") {
655 globals->set_warp( offset - cur_time );
656 } else if (offset_type == "gmt") {
657 globals->set_warp( offset - currGMT );
658 } else if (offset_type == "latitude") {
659 globals->set_warp( offset - (aircraftLocalTime - systemLocalTime) -
662 SG_LOG( SG_GENERAL, SG_ALERT,
663 "FG_TIME::Unsupported offset type " << offset_type );
667 SG_LOG( SG_GENERAL, SG_INFO, "After time init, warp = "
668 << globals->get_warp() );
670 globals->set_warp_delta( 0 );
673 cur_time_override->getLongValue(),
674 globals->get_warp() );
680 // This is the top level init routine which calls all the other
681 // initialization routines. If you are adding a subsystem to flight
682 // gear, its initialization call should located in this routine.
683 // Returns non-zero if a problem encountered.
684 bool fgInitSubsystems( void ) {
685 static const SGPropertyNode *longitude
686 = fgGetNode("/position/longitude-deg");
687 static const SGPropertyNode *latitude
688 = fgGetNode("/position/latitude-deg");
689 static const SGPropertyNode *altitude
690 = fgGetNode("/position/altitude-ft");
692 fgLIGHT *l = &cur_light_params;
694 SG_LOG( SG_GENERAL, SG_INFO, "Initialize Subsystems");
695 SG_LOG( SG_GENERAL, SG_INFO, "========== ==========");
698 ////////////////////////////////////////////////////////////////////
699 // Initialize the material property subsystem.
700 ////////////////////////////////////////////////////////////////////
702 SGPath mpath( globals->get_fg_root() );
703 mpath.append( "materials.xml" );
704 if ( ! material_lib.load( mpath.str() ) ) {
705 SG_LOG( SG_GENERAL, SG_ALERT, "Error loading material lib!" );
710 ////////////////////////////////////////////////////////////////////
711 // Initialize the scenery management subsystem.
712 ////////////////////////////////////////////////////////////////////
717 if ( global_tile_mgr.init() ) {
718 // Load the local scenery data
719 global_tile_mgr.update( longitude->getDoubleValue(),
720 latitude->getDoubleValue() );
722 SG_LOG( SG_GENERAL, SG_ALERT, "Error in Tile Manager initialization!" );
726 SG_LOG( SG_GENERAL, SG_DEBUG,
727 "Current terrain elevation after tile mgr init " <<
728 scenery.get_cur_elev() );
731 ////////////////////////////////////////////////////////////////////
732 // Initialize the flight model subsystem.
733 ////////////////////////////////////////////////////////////////////
737 // allocates structures so must happen before any of the flight
738 // model or control parameters are set
739 fgAircraftInit(); // In the future this might not be the case.
742 ////////////////////////////////////////////////////////////////////
743 // Initialize the event manager subsystem.
744 ////////////////////////////////////////////////////////////////////
746 global_events.Init();
748 // Output event stats every 60 seconds
749 global_events.Register( "fgEVENT_MGR::PrintStats()",
750 fgMethodCallback<fgEVENT_MGR>( &global_events,
751 &fgEVENT_MGR::PrintStats),
752 fgEVENT::FG_EVENT_READY, 60000 );
755 ////////////////////////////////////////////////////////////////////
756 // Initialize the view manager subsystem.
757 ////////////////////////////////////////////////////////////////////
762 ////////////////////////////////////////////////////////////////////
763 // Initialize the lighting subsystem.
764 ////////////////////////////////////////////////////////////////////
766 // fgUpdateSunPos() needs a few position and view parameters set
767 // so it can calculate local relative sun angle and a few other
768 // things for correctly orienting the sky.
771 global_events.Register( "fgUpdateSunPos()", fgUpdateSunPos,
772 fgEVENT::FG_EVENT_READY, 60000);
773 global_events.Register( "fgUpdateMoonPos()", fgUpdateMoonPos,
774 fgEVENT::FG_EVENT_READY, 60000);
776 // Initialize Lighting interpolation tables
779 // update the lighting parameters (based on sun angle)
780 global_events.Register( "fgLight::Update()",
781 fgMethodCallback<fgLIGHT>( &cur_light_params,
783 fgEVENT::FG_EVENT_READY, 30000 );
786 ////////////////////////////////////////////////////////////////////
787 // Initialize the logger.
788 ////////////////////////////////////////////////////////////////////
790 globals->set_logger(new FGLogger);
791 globals->get_logger()->init();
792 globals->get_logger()->bind();
794 ////////////////////////////////////////////////////////////////////
795 // Initialize the local time subsystem.
796 ////////////////////////////////////////////////////////////////////
798 // update the current timezone each 30 minutes
799 global_events.Register( "fgUpdateLocalTime()", fgUpdateLocalTime,
800 fgEVENT::FG_EVENT_READY, 1800000);
803 ////////////////////////////////////////////////////////////////////
804 // Initialize the weather subsystem.
805 ////////////////////////////////////////////////////////////////////
807 // Initialize the weather modeling subsystem
808 #ifndef FG_NEW_ENVIRONMENT
809 // Initialize the WeatherDatabase
810 SG_LOG(SG_GENERAL, SG_INFO, "Creating LocalWeatherDatabase");
812 sgSetVec3( position, current_aircraft.fdm_state->get_Latitude(),
813 current_aircraft.fdm_state->get_Longitude(),
814 current_aircraft.fdm_state->get_Altitude() * SG_FEET_TO_METER );
815 double init_vis = fgGetDouble("/environment/visibility-m");
817 FGLocalWeatherDatabase::DatabaseWorkingType working_type;
819 if (fgGetString("/environment/weather/working-type") == "internet")
821 working_type = FGLocalWeatherDatabase::use_internet;
823 working_type = FGLocalWeatherDatabase::default_mode;
826 if ( init_vis > 0 ) {
827 FGLocalWeatherDatabase::theFGLocalWeatherDatabase =
828 new FGLocalWeatherDatabase( position,
829 globals->get_fg_root(),
833 FGLocalWeatherDatabase::theFGLocalWeatherDatabase =
834 new FGLocalWeatherDatabase( position,
835 globals->get_fg_root(),
839 // cout << theFGLocalWeatherDatabase << endl;
840 // cout << "visibility = "
841 // << theFGLocalWeatherDatabase->getWeatherVisibility() << endl;
843 WeatherDatabase = FGLocalWeatherDatabase::theFGLocalWeatherDatabase;
845 // register the periodic update of the weather
846 global_events.Register( "weather update", fgUpdateWeatherDatabase,
847 fgEVENT::FG_EVENT_READY, 30000);
849 globals->get_environment_mgr()->init();
850 globals->get_environment_mgr()->bind();
853 ////////////////////////////////////////////////////////////////////
854 // Initialize vor/ndb/ils/fix list management and query systems
855 ////////////////////////////////////////////////////////////////////
857 SG_LOG(SG_GENERAL, SG_INFO, "Loading Navaids");
859 SG_LOG(SG_GENERAL, SG_INFO, " VOR/NDB");
860 current_navlist = new FGNavList;
861 SGPath p_nav( globals->get_fg_root() );
862 p_nav.append( "Navaids/default.nav" );
863 current_navlist->init( p_nav );
865 SG_LOG(SG_GENERAL, SG_INFO, " ILS and Marker Beacons");
866 current_beacons = new FGMarkerBeacons;
867 current_beacons->init();
868 current_ilslist = new FGILSList;
869 SGPath p_ils( globals->get_fg_root() );
870 p_ils.append( "Navaids/default.ils" );
871 current_ilslist->init( p_ils );
873 SG_LOG(SG_GENERAL, SG_INFO, " Fixes");
874 current_fixlist = new FGFixList;
875 SGPath p_fix( globals->get_fg_root() );
876 p_fix.append( "Navaids/default.fix" );
877 current_fixlist->init( p_fix );
879 ////////////////////////////////////////////////////////////////////
880 // Initialize ATC list management and query systems
881 ////////////////////////////////////////////////////////////////////
884 SG_LOG(SG_GENERAL, SG_INFO, " ATIS");
885 current_atislist = new FGATISList;
886 SGPath p_atis( globals->get_fg_root() );
887 p_atis.append( "ATC/default.atis" );
888 current_atislist->init( p_atis );
890 ////////////////////////////////////////////////////////////////////
891 // Initialise ATC display system
892 ////////////////////////////////////////////////////////////////////
894 SG_LOG(SG_GENERAL, SG_INFO, " ATC Display");
895 globals->set_ATC_display(new FGATCDisplay);
896 globals->get_ATC_display()->init();
898 ////////////////////////////////////////////////////////////////////
899 // Initialise the ATC Manager
900 ////////////////////////////////////////////////////////////////////
902 SG_LOG(SG_GENERAL, SG_INFO, " ATC Manager");
903 globals->set_ATC_mgr(new FGATCMgr);
904 globals->get_ATC_mgr()->init();
906 ////////////////////////////////////////////////////////////////////
907 // Initialize the built-in commands.
908 ////////////////////////////////////////////////////////////////////
912 #ifdef ENABLE_AUDIO_SUPPORT
913 ////////////////////////////////////////////////////////////////////
914 // Initialize the sound subsystem.
915 ////////////////////////////////////////////////////////////////////
917 globals->set_soundmgr(new FGSoundMgr);
918 globals->get_soundmgr()->init();
919 globals->get_soundmgr()->bind();
922 ////////////////////////////////////////////////////////////////////
923 // Initialize the sound-effects subsystem.
924 ////////////////////////////////////////////////////////////////////
925 globals->set_fx(new FGFX);
926 globals->get_fx()->init();
927 globals->get_fx()->bind();
931 ////////////////////////////////////////////////////////////////////
932 // Initialize the radio stack subsystem.
933 ////////////////////////////////////////////////////////////////////
935 // A textbook example of how FGSubsystem
937 current_radiostack = new FGRadioStack;
938 current_radiostack->init();
939 current_radiostack->bind();
942 ////////////////////////////////////////////////////////////////////
943 // Initialize the cockpit subsystem
944 ////////////////////////////////////////////////////////////////////
946 if( fgCockpitInit( ¤t_aircraft )) {
947 // Cockpit initialized ok.
949 SG_LOG( SG_GENERAL, SG_ALERT, "Error in Cockpit initialization!" );
954 ////////////////////////////////////////////////////////////////////
955 // Initialize the joystick subsystem.
956 ////////////////////////////////////////////////////////////////////
958 // if ( ! fgJoystickInit() ) {
959 // SG_LOG( SG_GENERAL, SG_ALERT, "Error in Joystick initialization!" );
963 ////////////////////////////////////////////////////////////////////
964 // Initialize the autopilot subsystem.
965 ////////////////////////////////////////////////////////////////////
967 current_autopilot = new FGAutopilot;
968 current_autopilot->init();
970 // initialize the gui parts of the autopilot
976 ////////////////////////////////////////////////////////////////////
977 // Initialize I/O subsystem.
978 ////////////////////////////////////////////////////////////////////
980 #if ! defined( macintosh )
984 // Initialize the 2D panel.
985 string panel_path = fgGetString("/sim/panel/path",
986 "Panels/Default/default.xml");
987 current_panel = fgReadPanel(panel_path);
988 if (current_panel == 0) {
989 SG_LOG( SG_INPUT, SG_ALERT,
990 "Error reading new panel from " << panel_path );
992 SG_LOG( SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path );
993 current_panel->init();
994 current_panel->bind();
998 ////////////////////////////////////////////////////////////////////
999 // Initialize the default (kludged) properties.
1000 ////////////////////////////////////////////////////////////////////
1005 ////////////////////////////////////////////////////////////////////
1006 // Initialize the controls subsystem.
1007 ////////////////////////////////////////////////////////////////////
1009 globals->get_controls()->init();
1010 globals->get_controls()->bind();
1013 ////////////////////////////////////////////////////////////////////
1014 // Initialize the input subsystem.
1015 ////////////////////////////////////////////////////////////////////
1017 current_input.init();
1018 current_input.bind();
1021 ////////////////////////////////////////////////////////////////////////
1022 // End of subsystem initialization.
1023 ////////////////////////////////////////////////////////////////////
1025 SG_LOG( SG_GENERAL, SG_INFO, endl);
1027 // Save the initial state for future
1029 globals->saveInitialState();
1035 void fgReInitSubsystems( void )
1037 static const SGPropertyNode *longitude
1038 = fgGetNode("/position/longitude-deg");
1039 static const SGPropertyNode *latitude
1040 = fgGetNode("/position/latitude-deg");
1041 static const SGPropertyNode *altitude
1042 = fgGetNode("/position/altitude-ft");
1043 static const SGPropertyNode *master_freeze
1044 = fgGetNode("/sim/freeze/master");
1046 SG_LOG( SG_GENERAL, SG_INFO,
1047 "/position/altitude = " << altitude->getDoubleValue() );
1049 bool freeze = master_freeze->getBoolValue();
1051 fgSetBool("/sim/freeze/master", true);
1054 // Initialize the Scenery Management subsystem
1058 if( global_tile_mgr.init() ) {
1059 Load the local scenery data
1060 global_tile_mgr.update( longitude->getDoubleValue(),
1061 latitude->getDoubleValue() );
1063 SG_LOG( SG_GENERAL, SG_ALERT, "Error in Tile Manager initialization!" );
1070 // allocates structures so must happen before any of the flight
1071 // model or control parameters are set
1072 fgAircraftInit(); // In the future this might not be the case.
1076 globals->get_controls()->reset_all();
1077 current_autopilot->reset();
1081 cur_light_params.Update();
1082 fgUpdateLocalTime();
1085 fgSetBool("/sim/freeze/master", false);