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 <Navaids/fixlist.hxx>
99 #include <Navaids/ilslist.hxx>
100 #include <Navaids/mkrbeacons.hxx>
101 #include <Navaids/navlist.hxx>
102 #include <Scenery/scenery.hxx>
103 #include <Scenery/tilemgr.hxx>
104 #include <Sound/fg_fx.hxx>
105 #include <Sound/soundmgr.hxx>
106 #include <Time/FGEventMgr.hxx>
107 #include <boost/bind.hpp>
108 #include <Time/light.hxx>
109 #include <Time/sunpos.hxx>
110 #include <Time/moonpos.hxx>
111 #include <Time/tmp.hxx>
113 #ifndef FG_NEW_ENVIRONMENT
114 # include <WeatherCM/FGLocalWeatherDatabase.h>
116 # include <Environment/environment_mgr.hxx>
119 #include "fg_init.hxx"
121 #include "fg_commands.hxx"
122 #include "fg_props.hxx"
123 #include "options.hxx"
124 #include "globals.hxx"
125 #include "logger.hxx"
126 #include "viewmgr.hxx"
128 #if defined(FX) && defined(XMESA)
129 #include <GL/xmesa.h>
132 SG_USING_STD(string);
134 extern const char *default_root;
137 // Read in configuration (file and command line) and just set fg_root
138 bool fgInitFGRoot ( int argc, char **argv ) {
142 // First parse command line options looking for fg-root, this will
143 // override anything specified in a config file
144 root = fgScanForRoot(argc, argv);
146 #if defined( unix ) || defined( __CYGWIN__ )
147 // Next check home directory for .fgfsrc.hostname file
148 if ( root.empty() ) {
149 envp = ::getenv( "HOME" );
150 if ( envp != NULL ) {
151 SGPath config( envp );
152 config.append( ".fgfsrc" );
154 gethostname( name, 256 );
155 config.concat( "." );
156 config.concat( name );
157 root = fgScanForRoot(config.str());
162 // Next check home directory for .fgfsrc file
163 if ( root.empty() ) {
164 envp = ::getenv( "HOME" );
165 if ( envp != NULL ) {
166 SGPath config( envp );
167 config.append( ".fgfsrc" );
168 root = fgScanForRoot(config.str());
172 // Next check if fg-root is set as an env variable
173 if ( root.empty() ) {
174 envp = ::getenv( "FG_ROOT" );
175 if ( envp != NULL ) {
180 // Otherwise, default to a random compiled-in location if we can't
181 // find fg-root any other way.
182 if ( root.empty() ) {
183 #if defined( __CYGWIN__ )
184 root = "/FlightGear";
185 #elif defined( WIN32 )
186 root = "\\FlightGear";
187 #elif defined( macintosh )
194 SG_LOG(SG_INPUT, SG_INFO, "fg_root = " << root );
195 globals->set_fg_root(root);
201 // Return the current base package version
202 string fgBasePackageVersion() {
203 SGPath base_path( globals->get_fg_root() );
204 base_path.append("version");
206 sg_gzifstream in( base_path.str() );
207 if ( !in.is_open() ) {
208 SGPath old_path( globals->get_fg_root() );
209 old_path.append( "Thanks" );
210 sg_gzifstream old( old_path.str() );
211 if ( !old.is_open() ) {
214 return "[old version]";
225 // Read in configuration (file and command line)
226 bool fgInitConfig ( int argc, char **argv ) {
228 // First, set some sane default values
231 // Read global preferences from $FG_ROOT/preferences.xml
232 SGPath props_path(globals->get_fg_root());
233 props_path.append("preferences.xml");
234 SG_LOG(SG_INPUT, SG_INFO, "Reading global preferences");
235 readProperties(props_path.str(), globals->get_props());
236 SG_LOG(SG_INPUT, SG_INFO, "Finished Reading global preferences");
238 // Read the default aircraft config file.
239 string aircraft = fgGetString("/sim/aircraft", "");
240 if (aircraft.size() > 0) {
241 SGPath aircraft_path(globals->get_fg_root());
242 aircraft_path.append("Aircraft");
243 aircraft_path.append(aircraft);
244 aircraft_path.concat("-set.xml");
245 SG_LOG(SG_INPUT, SG_INFO, "Reading default aircraft: " << aircraft
246 << " from " << aircraft_path.str());
248 readProperties(aircraft_path.str(), globals->get_props());
249 } catch (const sg_exception &e) {
250 string message = "Error reading default aircraft: ";
251 message += e.getFormattedMessage();
252 SG_LOG(SG_INPUT, SG_ALERT, message);
256 SG_LOG(SG_INPUT, SG_ALERT, "No default aircraft specified");
259 // Attempt to locate and parse the various config files in order
260 // from least precidence to greatest precidence
262 // Check for $fg_root/system.fgfsrc
263 SGPath config( globals->get_fg_root() );
264 config.append( "system.fgfsrc" );
265 fgParseOptions(config.str());
268 #if defined( unix ) || defined( __CYGWIN__ )
269 // Check for $fg_root/system.fgfsrc.hostname
270 gethostname( name, 256 );
271 config.concat( "." );
272 config.concat( name );
273 fgParseOptions(config.str());
276 // Check for ~/.fgfsrc
277 char* envp = ::getenv( "HOME" );
278 if ( envp != NULL ) {
280 config.append( ".fgfsrc" );
281 fgParseOptions(config.str());
284 #if defined( unix ) || defined( __CYGWIN__ )
285 // Check for ~/.fgfsrc.hostname
286 gethostname( name, 256 );
287 config.concat( "." );
288 config.concat( name );
289 fgParseOptions(config.str());
292 // Parse remaining command line options
293 // These will override anything specified in a config file
294 fgParseArgs(argc, argv);
300 // find basic airport location info from airport database
301 bool fgFindAirportID( const string& id, FGAirport *a ) {
303 SGPath path( globals->get_fg_root() );
304 path.append( "Airports" );
305 path.append( "simple.mk4" );
306 FGAirports airports( path.c_str() );
308 SG_LOG( SG_GENERAL, SG_INFO, "Searching for airport code = " << id );
310 if ( ! airports.search( id, a ) ) {
311 SG_LOG( SG_GENERAL, SG_ALERT,
312 "Failed to find " << id << " in " << path.str() );
319 SG_LOG( SG_GENERAL, SG_INFO,
320 "Position for " << id << " is ("
321 << a->longitude << ", "
322 << a->latitude << ")" );
328 // Set current_options lon/lat given an airport id
329 bool fgSetPosFromAirportID( const string& id ) {
333 SG_LOG( SG_GENERAL, SG_INFO,
334 "Attempting to set starting position from airport code " << id );
336 if ( fgFindAirportID( id, &a ) ) {
337 fgSetDouble("/position/longitude-deg", a.longitude );
338 fgSetDouble("/position/latitude-deg", a.latitude );
339 SG_LOG( SG_GENERAL, SG_INFO,
340 "Position for " << id << " is ("
341 << a.longitude << ", "
342 << a.latitude << ")" );
353 // Set current tower position lon/lat given an airport id
354 bool fgSetTowerPosFromAirportID( const string& id, double hdg ) {
356 // tower height hard coded for now...
357 float towerheight=50.0f;
359 // make a little off the heading for 1 runway airports...
360 float fudge_lon = fabs(sin(hdg)) * .003f;
361 float fudge_lat = .003f - fudge_lon;
363 if ( fgFindAirportID( id, &a ) ) {
364 fgSetDouble("/sim/tower/longitude-deg", a.longitude + fudge_lon);
365 fgSetDouble("/sim/tower/latitude-deg", a.latitude + fudge_lat);
366 fgSetDouble("/sim/tower/altitude-ft", a.elevation + towerheight);
375 // Set current_options lon/lat given an airport id and heading (degrees)
376 bool fgSetPosFromAirportIDandHdg( const string& id, double tgt_hdg ) {
379 double found_dir = 0.0;
382 // set initial position from runway and heading
384 SGPath path( globals->get_fg_root() );
385 path.append( "Airports" );
386 path.append( "runways.mk4" );
387 FGRunways runways( path.c_str() );
389 SG_LOG( SG_GENERAL, SG_INFO,
390 "Attempting to set starting position from runway code "
391 << id << " heading " << tgt_hdg );
393 // SGPath inpath( globals->get_fg_root() );
394 // inpath.append( "Airports" );
395 // inpath.append( "apt_simple" );
396 // airports.load( inpath.c_str() );
398 // SGPath outpath( globals->get_fg_root() );
399 // outpath.append( "Airports" );
400 // outpath.append( "simple.gdbm" );
401 // airports.dump_gdbm( outpath.c_str() );
403 if ( ! runways.search( id, &r ) ) {
404 SG_LOG( SG_GENERAL, SG_ALERT,
405 "Failed to find " << id << " in database." );
410 double min_diff = 360.0;
412 while ( r.id == id ) {
414 diff = tgt_hdg - r.heading;
415 while ( diff < -180.0 ) { diff += 360.0; }
416 while ( diff > 180.0 ) { diff -= 360.0; }
418 SG_LOG( SG_GENERAL, SG_INFO,
419 "Runway " << r.rwy_no << " heading = " << r.heading <<
420 " diff = " << diff );
421 if ( diff < min_diff ) {
428 diff = tgt_hdg - r.heading - 180.0;
429 while ( diff < -180.0 ) { diff += 360.0; }
430 while ( diff > 180.0 ) { diff -= 360.0; }
432 SG_LOG( SG_GENERAL, SG_INFO,
433 "Runway -" << r.rwy_no << " heading = " <<
435 " diff = " << diff );
436 if ( diff < min_diff ) {
445 SG_LOG( SG_GENERAL, SG_INFO, "closest runway = " << found_r.rwy_no
446 << " + " << found_dir );
452 double heading = found_r.heading + found_dir;
453 while ( heading >= 360.0 ) { heading -= 360.0; }
455 double lat2, lon2, az2;
456 double azimuth = found_r.heading + found_dir + 180.0;
457 while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
459 SG_LOG( SG_GENERAL, SG_INFO,
460 "runway = " << found_r.lon << ", " << found_r.lat
461 << " length = " << found_r.length * SG_FEET_TO_METER * 0.5
462 << " heading = " << azimuth );
464 geo_direct_wgs_84 ( 0, found_r.lat, found_r.lon,
465 azimuth, found_r.length * SG_FEET_TO_METER * 0.5 - 5.0,
466 &lat2, &lon2, &az2 );
468 if ( fabs( fgGetDouble("/sim/startup/offset-distance") ) > SG_EPSILON ) {
470 double odist = fgGetDouble("/sim/startup/offset-distance");
471 odist *= SG_NM_TO_METER;
472 double oaz = azimuth;
473 if ( fabs(fgGetDouble("/sim/startup/offset-azimuth")) > SG_EPSILON ) {
474 oaz = fgGetDouble("/sim/startup/offset-azimuth") + 180;
476 while ( oaz >= 360.0 ) { oaz -= 360.0; }
477 geo_direct_wgs_84 ( 0, lat2, lon2, oaz, odist, &olat, &olon, &az2 );
482 fgSetDouble("/position/longitude-deg", lon2 );
483 fgSetDouble("/position/latitude-deg", lat2 );
484 fgSetDouble("/orientation/heading-deg", heading );
486 SG_LOG( SG_GENERAL, SG_INFO,
487 "Position for " << id << " is ("
489 << lat2 << ") new heading is "
495 void fgSetPosFromGlideSlope(void) {
496 double gs = fgGetDouble("/velocities/glideslope");
497 double od = fgGetDouble("/sim/startup/offset-distance");
498 double alt = fgGetDouble("/position/altitude-ft");
500 //if glideslope and offset-distance are set and altitude is
501 //not, calculate the initial altitude
502 if( fabs(gs) > 0.01 && fabs(od) > 0.1 && alt < -9990 ) {
503 od *= SG_NM_TO_METER * SG_METER_TO_FEET;
504 alt = fabs(od*tan(gs));
505 fgSetDouble("/position/altitude-ft",alt);
506 fgSetBool("/sim/startup/onground", false);
507 SG_LOG(SG_GENERAL,SG_INFO, "Calculated altitude as: " << alt << " ft");
508 } else if( fabs(gs) > 0.01 && alt > 0 && fabs(od) < 0.1) {
510 od *= -1*SG_FEET_TO_METER * SG_METER_TO_NM;
511 fgSetDouble("/sim/startup/offset-distance",od);
512 SG_LOG(SG_GENERAL,SG_INFO, "Calculated offset distance as: "
514 } else if( fabs(gs) > 0.01 ) {
515 SG_LOG(SG_GENERAL,SG_ALERT, "Glideslope given but not altitude"
516 << " or offset-distance. Resetting"
517 << " glideslope to zero" );
518 fgSetDouble("/velocities/glideslope",0);
523 // General house keeping initializations
524 bool fgInitGeneral( void ) {
527 #if defined(FX) && defined(XMESA)
528 char *mesa_win_state;
531 SG_LOG( SG_GENERAL, SG_INFO, "General Initialization" );
532 SG_LOG( SG_GENERAL, SG_INFO, "======= ==============" );
534 root = globals->get_fg_root();
535 if ( ! root.length() ) {
536 // No root path set? Then bail ...
537 SG_LOG( SG_GENERAL, SG_ALERT,
538 "Cannot continue without a path to the base package "
539 << "being defined." );
542 SG_LOG( SG_GENERAL, SG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
544 #if defined(FX) && defined(XMESA)
545 // initialize full screen flag
546 globals->set_fullscreen(false);
547 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
548 // Test for the MESA_GLX_FX env variable
549 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
550 // test if we are fullscreen mesa/glide
551 if ( (mesa_win_state[0] == 'f') ||
552 (mesa_win_state[0] == 'F') ) {
553 globals->set_fullscreen(true);
563 // Initialize the flight model subsystem. This just creates the
564 // object. The actual fdm initialization is delayed until we get a
565 // proper scenery elevation hit. This is checked for in main.cxx
569 if ( cur_fdm_state ) {
570 delete cur_fdm_state;
574 double dt = 1.0 / fgGetInt("/sim/model-hz");
575 aircraft_dir = fgGetString("/sim/aircraft-dir");
576 const string &model = fgGetString("/sim/flight-model");
579 if (model == "larcsim") {
580 cur_fdm_state = new FGLaRCsim( dt );
581 } else if (model == "jsb") {
582 cur_fdm_state = new FGJSBsim( dt );
583 } else if (model == "ada") {
584 cur_fdm_state = new FGADA( dt );
585 } else if (model == "balloon") {
586 cur_fdm_state = new FGBalloonSim( dt );
587 } else if (model == "magic") {
588 cur_fdm_state = new FGMagicCarpet( dt );
589 } else if (model == "external") {
590 cur_fdm_state = new FGExternal( dt );
591 } else if (model == "network") {
592 cur_fdm_state = new FGExternalNet( dt, 5501, 5502, 5503, "localhost" );
593 } else if (model == "null") {
594 cur_fdm_state = new FGNullFDM( dt );
595 } else if (model == "yasim") {
596 cur_fdm_state = new YASim( dt );
598 SG_LOG(SG_GENERAL, SG_ALERT,
599 "Unrecognized flight model '" << model
600 << "', cannot init flight dynamics model.");
604 SG_LOG(SG_GENERAL, SG_ALERT, "FlightGear aborting\n\n");
610 // Initialize view parameters
612 globals->get_viewmgr()->update(0);
616 SGTime *fgInitTime() {
618 static const SGPropertyNode *longitude
619 = fgGetNode("/position/longitude-deg");
620 static const SGPropertyNode *latitude
621 = fgGetNode("/position/latitude-deg");
622 static const SGPropertyNode *cur_time_override
623 = fgGetNode("/sim/time/cur-time-override", true);
625 SGPath zone( globals->get_fg_root() );
626 zone.append( "Timezone" );
627 SGTime *t = new SGTime( longitude->getDoubleValue()
628 * SGD_DEGREES_TO_RADIANS,
629 latitude->getDoubleValue()
630 * SGD_DEGREES_TO_RADIANS,
632 cur_time_override->getLongValue() );
634 // Handle potential user specified time offsets
635 time_t cur_time = t->get_cur_time();
636 time_t currGMT = sgTimeGetGMT( gmtime(&cur_time) );
637 time_t systemLocalTime = sgTimeGetGMT( localtime(&cur_time) );
638 time_t aircraftLocalTime =
639 sgTimeGetGMT( fgLocaltime(&cur_time, t->get_zonename() ) );
641 // Okay, we now have six possible scenarios
642 int offset = fgGetInt("/sim/startup/time-offset");
643 const string &offset_type = fgGetString("/sim/startup/time-offset-type");
644 if (offset_type == "system-offset") {
645 globals->set_warp( offset );
646 } else if (offset_type == "gmt-offset") {
647 globals->set_warp( offset - (currGMT - systemLocalTime) );
648 } else if (offset_type == "latitude-offset") {
649 globals->set_warp( offset - (aircraftLocalTime - systemLocalTime) );
650 } else if (offset_type == "system") {
651 globals->set_warp( offset - cur_time );
652 } else if (offset_type == "gmt") {
653 globals->set_warp( offset - currGMT );
654 } else if (offset_type == "latitude") {
655 globals->set_warp( offset - (aircraftLocalTime - systemLocalTime) -
658 SG_LOG( SG_GENERAL, SG_ALERT,
659 "FG_TIME::Unsupported offset type " << offset_type );
663 SG_LOG( SG_GENERAL, SG_INFO, "After time init, warp = "
664 << globals->get_warp() );
666 globals->set_warp_delta( 0 );
669 cur_time_override->getLongValue(),
670 globals->get_warp() );
676 // This is the top level init routine which calls all the other
677 // initialization routines. If you are adding a subsystem to flight
678 // gear, its initialization call should located in this routine.
679 // Returns non-zero if a problem encountered.
680 bool fgInitSubsystems( void ) {
681 static const SGPropertyNode *longitude
682 = fgGetNode("/position/longitude-deg");
683 static const SGPropertyNode *latitude
684 = fgGetNode("/position/latitude-deg");
685 static const SGPropertyNode *altitude
686 = fgGetNode("/position/altitude-ft");
688 fgLIGHT *l = &cur_light_params;
690 SG_LOG( SG_GENERAL, SG_INFO, "Initialize Subsystems");
691 SG_LOG( SG_GENERAL, SG_INFO, "========== ==========");
694 ////////////////////////////////////////////////////////////////////
695 // Initialize the material property subsystem.
696 ////////////////////////////////////////////////////////////////////
698 SGPath mpath( globals->get_fg_root() );
699 mpath.append( "materials.xml" );
700 if ( ! material_lib.load( mpath.str() ) ) {
701 SG_LOG( SG_GENERAL, SG_ALERT, "Error loading material lib!" );
706 ////////////////////////////////////////////////////////////////////
707 // Initialize the scenery management subsystem.
708 ////////////////////////////////////////////////////////////////////
713 if ( global_tile_mgr.init() ) {
714 // Load the local scenery data
715 double visibility_meters = fgGetDouble("/environment/visibility-m");
717 global_tile_mgr.update( longitude->getDoubleValue(),
718 latitude->getDoubleValue(),
721 SG_LOG( SG_GENERAL, SG_ALERT, "Error in Tile Manager initialization!" );
725 SG_LOG( SG_GENERAL, SG_DEBUG,
726 "Current terrain elevation after tile mgr init " <<
727 scenery.get_cur_elev() );
730 ////////////////////////////////////////////////////////////////////
731 // Initialize the flight model subsystem.
732 ////////////////////////////////////////////////////////////////////
736 // allocates structures so must happen before any of the flight
737 // model or control parameters are set
738 fgAircraftInit(); // In the future this might not be the case.
741 ////////////////////////////////////////////////////////////////////
742 // Initialize the event manager subsystem.
743 ////////////////////////////////////////////////////////////////////
745 global_events.init();
747 // Output event stats every 60 seconds
748 global_events.Register( "fgEVENT_MGR::PrintStats()",
749 boost::bind( &FGEventMgr::print_stats,
754 ////////////////////////////////////////////////////////////////////
755 // Initialize the view manager subsystem.
756 ////////////////////////////////////////////////////////////////////
761 ////////////////////////////////////////////////////////////////////
762 // Initialize the lighting subsystem.
763 ////////////////////////////////////////////////////////////////////
765 // fgUpdateSunPos() needs a few position and view parameters set
766 // so it can calculate local relative sun angle and a few other
767 // things for correctly orienting the sky.
770 global_events.Register( "fgUpdateSunPos()", &fgUpdateSunPos,
772 global_events.Register( "fgUpdateMoonPos()", &fgUpdateMoonPos,
775 // Initialize Lighting interpolation tables
778 // update the lighting parameters (based on sun angle)
779 global_events.Register( "fgLight::Update()",
780 boost::bind( &fgLIGHT::Update, &cur_light_params ),
784 ////////////////////////////////////////////////////////////////////
785 // Initialize the logger.
786 ////////////////////////////////////////////////////////////////////
788 globals->set_logger(new FGLogger);
789 globals->get_logger()->init();
790 globals->get_logger()->bind();
792 ////////////////////////////////////////////////////////////////////
793 // Initialize the local time subsystem.
794 ////////////////////////////////////////////////////////////////////
796 // update the current timezone each 30 minutes
797 global_events.Register( "fgUpdateLocalTime()", &fgUpdateLocalTime,
801 ////////////////////////////////////////////////////////////////////
802 // Initialize the weather subsystem.
803 ////////////////////////////////////////////////////////////////////
805 // Initialize the weather modeling subsystem
806 #ifndef FG_NEW_ENVIRONMENT
807 // Initialize the WeatherDatabase
808 SG_LOG(SG_GENERAL, SG_INFO, "Creating LocalWeatherDatabase");
810 sgSetVec3( position, current_aircraft.fdm_state->get_Latitude(),
811 current_aircraft.fdm_state->get_Longitude(),
812 current_aircraft.fdm_state->get_Altitude() * SG_FEET_TO_METER );
813 double init_vis = fgGetDouble("/environment/visibility-m");
815 FGLocalWeatherDatabase::DatabaseWorkingType working_type;
817 if (!strcmp(fgGetString("/environment/weather/working-type"), "internet"))
819 working_type = FGLocalWeatherDatabase::use_internet;
821 working_type = FGLocalWeatherDatabase::default_mode;
824 if ( init_vis > 0 ) {
825 FGLocalWeatherDatabase::theFGLocalWeatherDatabase =
826 new FGLocalWeatherDatabase( position,
827 globals->get_fg_root(),
831 FGLocalWeatherDatabase::theFGLocalWeatherDatabase =
832 new FGLocalWeatherDatabase( position,
833 globals->get_fg_root(),
837 // cout << theFGLocalWeatherDatabase << endl;
838 // cout << "visibility = "
839 // << theFGLocalWeatherDatabase->getWeatherVisibility() << endl;
841 WeatherDatabase = FGLocalWeatherDatabase::theFGLocalWeatherDatabase;
843 // register the periodic update of the weather
844 global_events.Register( "weather update", &fgUpdateWeatherDatabase,
847 globals->get_environment_mgr()->init();
848 globals->get_environment_mgr()->bind();
851 ////////////////////////////////////////////////////////////////////
852 // Initialize vor/ndb/ils/fix list management and query systems
853 ////////////////////////////////////////////////////////////////////
855 SG_LOG(SG_GENERAL, SG_INFO, "Loading Navaids");
857 SG_LOG(SG_GENERAL, SG_INFO, " VOR/NDB");
858 current_navlist = new FGNavList;
859 SGPath p_nav( globals->get_fg_root() );
860 p_nav.append( "Navaids/default.nav" );
861 current_navlist->init( p_nav );
863 SG_LOG(SG_GENERAL, SG_INFO, " ILS and Marker Beacons");
864 current_beacons = new FGMarkerBeacons;
865 current_beacons->init();
866 current_ilslist = new FGILSList;
867 SGPath p_ils( globals->get_fg_root() );
868 p_ils.append( "Navaids/default.ils" );
869 current_ilslist->init( p_ils );
871 SG_LOG(SG_GENERAL, SG_INFO, " Fixes");
872 current_fixlist = new FGFixList;
873 SGPath p_fix( globals->get_fg_root() );
874 p_fix.append( "Navaids/default.fix" );
875 current_fixlist->init( p_fix );
877 ////////////////////////////////////////////////////////////////////
878 // Initialize ATC list management and query systems
879 ////////////////////////////////////////////////////////////////////
881 SG_LOG(SG_GENERAL, SG_INFO, " ATIS");
882 current_atislist = new FGATISList;
883 SGPath p_atis( globals->get_fg_root() );
884 p_atis.append( "ATC/default.atis" );
885 current_atislist->init( p_atis );
887 SG_LOG(SG_GENERAL, SG_INFO, " Tower");
888 current_towerlist = new FGTowerList;
889 SGPath p_tower( globals->get_fg_root() );
890 p_tower.append( "ATC/default.tower" );
891 current_towerlist->init( p_tower );
893 SG_LOG(SG_GENERAL, SG_INFO, " Approach");
894 current_approachlist = new FGApproachList;
895 SGPath p_approach( globals->get_fg_root() );
896 p_approach.append( "ATC/default.approach" );
897 current_approachlist->init( p_approach );
899 ////////////////////////////////////////////////////////////////////
900 // Initialise ATC display system
901 ////////////////////////////////////////////////////////////////////
903 SG_LOG(SG_GENERAL, SG_INFO, " ATC Display");
904 globals->set_ATC_display(new FGATCDisplay);
905 globals->get_ATC_display()->init();
907 ////////////////////////////////////////////////////////////////////
908 // Initialise the ATC Manager
909 ////////////////////////////////////////////////////////////////////
911 SG_LOG(SG_GENERAL, SG_INFO, " ATC Manager");
912 globals->set_ATC_mgr(new FGATCMgr);
913 globals->get_ATC_mgr()->init();
915 ////////////////////////////////////////////////////////////////////
916 // Initialise the AI Manager
917 ////////////////////////////////////////////////////////////////////
919 SG_LOG(SG_GENERAL, SG_INFO, " AI Manager");
920 globals->set_AI_mgr(new FGAIMgr);
921 globals->get_AI_mgr()->init();
923 ////////////////////////////////////////////////////////////////////
924 // Initialize the built-in commands.
925 ////////////////////////////////////////////////////////////////////
929 #ifdef ENABLE_AUDIO_SUPPORT
930 ////////////////////////////////////////////////////////////////////
931 // Initialize the sound subsystem.
932 ////////////////////////////////////////////////////////////////////
934 globals->set_soundmgr(new FGSoundMgr);
935 globals->get_soundmgr()->init();
936 globals->get_soundmgr()->bind();
939 ////////////////////////////////////////////////////////////////////
940 // Initialize the sound-effects subsystem.
941 ////////////////////////////////////////////////////////////////////
942 globals->set_fx(new FGFX);
943 globals->get_fx()->init();
944 globals->get_fx()->bind();
948 ////////////////////////////////////////////////////////////////////
949 // Initialize the radio stack subsystem.
950 ////////////////////////////////////////////////////////////////////
952 // A textbook example of how FGSubsystem
954 current_radiostack = new FGRadioStack;
955 current_radiostack->init();
956 current_radiostack->bind();
959 ////////////////////////////////////////////////////////////////////
960 // Initialize the cockpit subsystem
961 ////////////////////////////////////////////////////////////////////
963 if( fgCockpitInit( ¤t_aircraft )) {
964 // Cockpit initialized ok.
966 SG_LOG( SG_GENERAL, SG_ALERT, "Error in Cockpit initialization!" );
971 ////////////////////////////////////////////////////////////////////
972 // Initialize the joystick subsystem.
973 ////////////////////////////////////////////////////////////////////
975 // if ( ! fgJoystickInit() ) {
976 // SG_LOG( SG_GENERAL, SG_ALERT, "Error in Joystick initialization!" );
980 ////////////////////////////////////////////////////////////////////
981 // Initialize the autopilot subsystem.
982 ////////////////////////////////////////////////////////////////////
984 globals->set_autopilot(new FGAutopilot);
985 globals->get_autopilot()->init();
986 globals->get_autopilot()->bind();
988 // FIXME: these should go in the
989 // GUI initialization code, not here.
995 ////////////////////////////////////////////////////////////////////
996 // Initialize I/O subsystem.
997 ////////////////////////////////////////////////////////////////////
999 #if ! defined( macintosh )
1003 // Initialize the 2D panel.
1004 string panel_path = fgGetString("/sim/panel/path",
1005 "Panels/Default/default.xml");
1006 current_panel = fgReadPanel(panel_path);
1007 if (current_panel == 0) {
1008 SG_LOG( SG_INPUT, SG_ALERT,
1009 "Error reading new panel from " << panel_path );
1011 SG_LOG( SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path );
1012 current_panel->init();
1013 current_panel->bind();
1017 ////////////////////////////////////////////////////////////////////
1018 // Initialize the default (kludged) properties.
1019 ////////////////////////////////////////////////////////////////////
1024 ////////////////////////////////////////////////////////////////////
1025 // Initialize the controls subsystem.
1026 ////////////////////////////////////////////////////////////////////
1028 globals->get_controls()->init();
1029 globals->get_controls()->bind();
1032 ////////////////////////////////////////////////////////////////////
1033 // Initialize the input subsystem.
1034 ////////////////////////////////////////////////////////////////////
1036 current_input.init();
1037 current_input.bind();
1040 ////////////////////////////////////////////////////////////////////////
1041 // End of subsystem initialization.
1042 ////////////////////////////////////////////////////////////////////
1044 SG_LOG( SG_GENERAL, SG_INFO, endl);
1046 // Save the initial state for future
1048 globals->saveInitialState();
1054 void fgReInitSubsystems( void )
1056 static const SGPropertyNode *longitude
1057 = fgGetNode("/position/longitude-deg");
1058 static const SGPropertyNode *latitude
1059 = fgGetNode("/position/latitude-deg");
1060 static const SGPropertyNode *altitude
1061 = fgGetNode("/position/altitude-ft");
1062 static const SGPropertyNode *master_freeze
1063 = fgGetNode("/sim/freeze/master");
1065 SG_LOG( SG_GENERAL, SG_INFO,
1066 "/position/altitude = " << altitude->getDoubleValue() );
1068 bool freeze = master_freeze->getBoolValue();
1070 fgSetBool("/sim/freeze/master", true);
1073 // Initialize the Scenery Management subsystem
1077 if( global_tile_mgr.init() ) {
1078 Load the local scenery data
1079 global_tile_mgr.update( longitude->getDoubleValue(),
1080 latitude->getDoubleValue() );
1082 SG_LOG( SG_GENERAL, SG_ALERT, "Error in Tile Manager initialization!" );
1089 // allocates structures so must happen before any of the flight
1090 // model or control parameters are set
1091 fgAircraftInit(); // In the future this might not be the case.
1095 globals->get_controls()->reset_all();
1096 globals->get_autopilot()->reset();
1100 cur_light_params.Update();
1101 fgUpdateLocalTime();
1104 fgSetBool("/sim/freeze/master", false);