]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_init.cxx
58fa1f1b541e2962c1b628e6043841c9618394c2
[flightgear.git] / src / Main / fg_init.cxx
1 // fg_init.cxx -- Flight Gear top level initialization routines
2 //
3 // Written by Curtis Olson, started August 1997.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
6 //
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.
11 //
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.
16 //
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.
20 //
21 //
22 // $Id$
23
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 // For BC 5.01 this must be included before OpenGL includes.
30 #ifdef SG_MATH_EXCEPTION_CLASH
31 #  include <math.h>
32 #endif
33
34 #ifdef HAVE_WINDOWS_H
35 #  include <windows.h>
36 #endif
37
38 #include <GL/glut.h>
39
40 #include <stdio.h>
41 #include <stdlib.h>
42
43
44 #if defined( unix ) || defined( __CYGWIN__ )
45 #  include <unistd.h>           // for gethostname()
46 #endif
47
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.
50 #ifdef linux
51 #  define _G_NO_EXTERN_TEMPLATES
52 #endif
53
54 #include <simgear/compiler.h>
55
56 #include STL_STRING
57
58 #include <simgear/constants.h>
59 #include <simgear/debug/logstream.hxx>
60 #include <simgear/math/point3d.hxx>
61 #include <simgear/math/polar3d.hxx>
62 #include <simgear/math/sg_geodesy.hxx>
63 #include <simgear/misc/sg_path.hxx>
64 #include <simgear/timing/sg_time.hxx>
65
66 #include <Aircraft/aircraft.hxx>
67 #include <FDM/UIUCModel/uiuc_aircraftdir.h>
68 #include <Airports/runways.hxx>
69 #include <Airports/simple.hxx>
70 #include <Autopilot/auto_gui.hxx>
71 #include <Autopilot/newauto.hxx>
72 #include <Cockpit/cockpit.hxx>
73 #include <Cockpit/radiostack.hxx>
74 #include <Cockpit/panel.hxx>
75 #include <Cockpit/panel_io.hxx>
76 #include <FDM/ADA.hxx>
77 #include <FDM/Balloon.h>
78 #include <FDM/External.hxx>
79 #include <FDM/JSBSim.hxx>
80 #include <FDM/LaRCsim.hxx>
81 #include <FDM/MagicCarpet.hxx>
82 #include <Include/general.hxx>
83 #include <Input/input.hxx>
84 // #include <Joystick/joystick.hxx>
85 #include <Objects/matlib.hxx>
86 #include <Navaids/fixlist.hxx>
87 #include <Navaids/ilslist.hxx>
88 #include <Navaids/mkrbeacons.hxx>
89 #include <Navaids/navlist.hxx>
90 #include <Scenery/scenery.hxx>
91 #include <Scenery/tilemgr.hxx>
92 #include <Time/event.hxx>
93 #include <Time/light.hxx>
94 #include <Time/sunpos.hxx>
95 #include <Time/moonpos.hxx>
96 #include <Time/tmp.hxx>
97
98 #ifndef FG_OLD_WEATHER
99 #  include <WeatherCM/FGLocalWeatherDatabase.h>
100 #else
101 #  include <Weather/weather.hxx>
102 #endif
103
104 #include "fg_init.hxx"
105 #include "fg_io.hxx"
106 #include "fg_commands.hxx"
107 #include "fg_props.hxx"
108 #include "options.hxx"
109 #include "globals.hxx"
110
111 #if defined(FX) && defined(XMESA)
112 #include <GL/xmesa.h>
113 #endif
114
115 SG_USING_STD(string);
116
117 extern const char *default_root;
118
119
120 // Read in configuration (file and command line) and just set fg_root
121 bool fgInitFGRoot ( int argc, char **argv ) {
122     string root;
123     char* envp;
124
125     // First parse command line options looking for fg-root, this will
126     // override anything specified in a config file
127     root = fgScanForRoot(argc, argv);
128
129 #if defined( unix ) || defined( __CYGWIN__ )
130     // Next check home directory for .fgfsrc.hostname file
131     if ( root == "" ) {
132         envp = ::getenv( "HOME" );
133         if ( envp != NULL ) {
134             SGPath config( envp );
135             config.append( ".fgfsrc" );
136             char name[256];
137             gethostname( name, 256 );
138             config.concat( "." );
139             config.concat( name );
140             root = fgScanForRoot(config.str());
141         }
142     }
143 #endif
144
145     // Next check home directory for .fgfsrc file
146     if ( root == "" ) {
147         envp = ::getenv( "HOME" );
148         if ( envp != NULL ) {
149             SGPath config( envp );
150             config.append( ".fgfsrc" );
151             root = fgScanForRoot(config.str());
152         }
153     }
154     
155     // Next check if fg-root is set as an env variable
156     if ( root == "" ) {
157         envp = ::getenv( "FG_ROOT" );
158         if ( envp != NULL ) {
159             root = envp;
160         }
161     }
162
163     // Otherwise, default to a random compiled-in location if we can't
164     // find fg-root any other way.
165     if ( root == "" ) {
166 #if defined( __CYGWIN__ )
167         root = "/FlightGear";
168 #elif defined( WIN32 )
169         root = "\\FlightGear";
170 #elif defined( macintosh )
171         root = "";
172 #else
173         root = PKGLIBDIR;
174 #endif
175     }
176
177     SG_LOG(SG_INPUT, SG_INFO, "fg_root = " << root );
178     globals->set_fg_root(root);
179
180     return true;
181 }
182
183
184 // Return the current base package version
185 string fgBasePackageVersion() {
186     SGPath base_path( globals->get_fg_root() );
187     base_path.append("version");
188
189     sg_gzifstream in( base_path.str() );
190     if ( !in.is_open() ) {
191         SGPath old_path( globals->get_fg_root() );
192         old_path.append( "Thanks" );
193         sg_gzifstream old( old_path.str() );
194         if ( !old.is_open() ) {
195             return "[none found]";
196         } else {
197             return "[old version, please upgrade]";
198         }
199     }
200
201     string version;
202     in >> version;
203
204     return version;
205 }
206
207
208 // Read in configuration (file and command line)
209 bool fgInitConfig ( int argc, char **argv ) {
210
211                                 // First, set some sane default values
212     fgSetDefaults();
213
214     // Read global preferences from $FG_ROOT/preferences.xml
215     SGPath props_path(globals->get_fg_root());
216     props_path.append("preferences.xml");
217     SG_LOG(SG_INPUT, SG_INFO, "Reading global preferences");
218     if (!readProperties(props_path.str(), globals->get_props())) {
219       SG_LOG(SG_INPUT, SG_ALERT, "Failed to read global preferences from "
220              << props_path.str());
221     } else {
222       SG_LOG(SG_INPUT, SG_INFO, "Finished Reading global preferences");
223     }
224
225     // Attempt to locate and parse the various config files in order
226     // from least precidence to greatest precidence
227
228     // Check for $fg_root/system.fgfsrc
229     SGPath config( globals->get_fg_root() );
230     config.append( "system.fgfsrc" );
231     fgParseOptions(config.str());
232
233     char name[256];
234 #if defined( unix ) || defined( __CYGWIN__ )
235     // Check for $fg_root/system.fgfsrc.hostname
236     gethostname( name, 256 );
237     config.concat( "." );
238     config.concat( name );
239     fgParseOptions(config.str());
240 #endif
241
242     // Check for ~/.fgfsrc
243     char* envp = ::getenv( "HOME" );
244     if ( envp != NULL ) {
245         config.set( envp );
246         config.append( ".fgfsrc" );
247         fgParseOptions(config.str());
248     }
249
250 #if defined( unix ) || defined( __CYGWIN__ )
251     // Check for ~/.fgfsrc.hostname
252     gethostname( name, 256 );
253     config.concat( "." );
254     config.concat( name );
255     fgParseOptions(config.str());
256 #endif
257
258     // Parse remaining command line options
259     // These will override anything specified in a config file
260     fgParseOptions(argc, argv);
261
262     return true;
263 }
264
265
266 // find basic airport location info from airport database
267 bool fgFindAirportID( const string& id, FGAirport *a ) {
268     if ( id.length() ) {
269         SGPath path( globals->get_fg_root() );
270         path.append( "Airports" );
271         path.append( "simple.mk4" );
272         FGAirports airports( path.c_str() );
273
274         SG_LOG( SG_GENERAL, SG_INFO, "Searching for airport code = " << id );
275
276         if ( ! airports.search( id, a ) ) {
277             SG_LOG( SG_GENERAL, SG_ALERT,
278                     "Failed to find " << id << " in " << path.str() );
279             return false;
280         }
281     } else {
282         return false;
283     }
284
285     SG_LOG( SG_GENERAL, SG_INFO,
286             "Position for " << id << " is ("
287             << a->longitude << ", "
288             << a->latitude << ")" );
289
290     return true;
291 }
292
293
294 // Set current_options lon/lat given an airport id
295 bool fgSetPosFromAirportID( const string& id ) {
296     FGAirport a;
297     // double lon, lat;
298
299     SG_LOG( SG_GENERAL, SG_INFO,
300             "Attempting to set starting position from airport code " << id );
301
302     if ( fgFindAirportID( id, &a ) ) {
303         fgSetDouble("/position/longitude-deg",  a.longitude );
304         fgSetDouble("/position/latitude-deg",  a.latitude );
305         SG_LOG( SG_GENERAL, SG_INFO,
306                 "Position for " << id << " is ("
307                 << a.longitude << ", "
308                 << a.latitude << ")" );
309
310         return true;
311     } else {
312         return false;
313     }
314
315 }
316
317
318 // Set current_options lon/lat given an airport id and heading (degrees)
319 bool fgSetPosFromAirportIDandHdg( const string& id, double tgt_hdg ) {
320     FGRunway r;
321     FGRunway found_r;
322     double found_dir = 0.0;
323
324     if ( id.length() ) {
325         // set initial position from runway and heading
326
327         SGPath path( globals->get_fg_root() );
328         path.append( "Airports" );
329         path.append( "runways.mk4" );
330         FGRunways runways( path.c_str() );
331
332         SG_LOG( SG_GENERAL, SG_INFO,
333                 "Attempting to set starting position from runway code "
334                 << id << " heading " << tgt_hdg );
335
336         // SGPath inpath( globals->get_fg_root() );
337         // inpath.append( "Airports" );
338         // inpath.append( "apt_simple" );
339         // airports.load( inpath.c_str() );
340
341         // SGPath outpath( globals->get_fg_root() );
342         // outpath.append( "Airports" );
343         // outpath.append( "simple.gdbm" );
344         // airports.dump_gdbm( outpath.c_str() );
345
346         if ( ! runways.search( id, &r ) ) {
347             SG_LOG( SG_GENERAL, SG_ALERT,
348                     "Failed to find " << id << " in database." );
349             return false;
350         }
351
352         double diff;
353         double min_diff = 360.0;
354
355         while ( r.id == id ) {
356             // forward direction
357             diff = tgt_hdg - r.heading;
358             while ( diff < -180.0 ) { diff += 360.0; }
359             while ( diff >  180.0 ) { diff -= 360.0; }
360             diff = fabs(diff);
361             SG_LOG( SG_GENERAL, SG_INFO,
362                     "Runway " << r.rwy_no << " heading = " << r.heading <<
363                     " diff = " << diff );
364             if ( diff < min_diff ) {
365                 min_diff = diff;
366                 found_r = r;
367                 found_dir = 0;
368             }
369
370             // reverse direction
371             diff = tgt_hdg - r.heading - 180.0;
372             while ( diff < -180.0 ) { diff += 360.0; }
373             while ( diff >  180.0 ) { diff -= 360.0; }
374             diff = fabs(diff);
375             SG_LOG( SG_GENERAL, SG_INFO,
376                     "Runway -" << r.rwy_no << " heading = " <<
377                     r.heading + 180.0 <<
378                     " diff = " << diff );
379             if ( diff < min_diff ) {
380                 min_diff = diff;
381                 found_r = r;
382                 found_dir = 180.0;
383             }
384
385             runways.next( &r );
386         }
387
388         SG_LOG( SG_GENERAL, SG_INFO, "closest runway = " << found_r.rwy_no
389                 << " + " << found_dir );
390
391     } else {
392         return false;
393     }
394
395     double heading = found_r.heading + found_dir;
396     while ( heading >= 360.0 ) { heading -= 360.0; }
397
398     double lat2, lon2, az2;
399     double azimuth = found_r.heading + found_dir + 180.0;
400     while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
401
402     SG_LOG( SG_GENERAL, SG_INFO,
403             "runway =  " << found_r.lon << ", " << found_r.lat
404             << " length = " << found_r.length * SG_FEET_TO_METER * 0.5 
405             << " heading = " << azimuth );
406     
407     geo_direct_wgs_84 ( 0, found_r.lat, found_r.lon, 
408                         azimuth, found_r.length * SG_FEET_TO_METER * 0.5 - 5.0,
409                         &lat2, &lon2, &az2 );
410
411     if ( fabs( fgGetDouble("/sim/startup/offset-distance") ) > SG_EPSILON ) {
412         double olat, olon;
413         double odist = fgGetDouble("/sim/startup/offset-distance");
414         odist *= SG_NM_TO_METER;
415         double oaz = azimuth;
416         if ( fabs(fgGetDouble("/sim/startup/offset-azimuth")) > SG_EPSILON ) {
417             oaz = fgGetDouble("/sim/startup/offset-azimuth") + 180;
418         }
419         while ( oaz >= 360.0 ) { oaz -= 360.0; }
420         geo_direct_wgs_84 ( 0, lat2, lon2, oaz, odist, &olat, &olon, &az2 );
421         lat2=olat;
422         lon2=olon;
423     }
424     fgSetDouble("/position/longitude-deg",  lon2 );
425     fgSetDouble("/position/latitude-deg",  lat2 );
426     fgSetDouble("/orientation/heading-deg", heading );
427
428     SG_LOG( SG_GENERAL, SG_INFO,
429             "Position for " << id << " is ("
430             << lon2 << ", "
431             << lat2 << ") new heading is "
432             << heading );
433
434     return true;
435 }
436
437
438 // General house keeping initializations
439 bool fgInitGeneral( void ) {
440     string root;
441
442 #if defined(FX) && defined(XMESA)
443     char *mesa_win_state;
444 #endif
445
446     SG_LOG( SG_GENERAL, SG_INFO, "General Initialization" );
447     SG_LOG( SG_GENERAL, SG_INFO, "======= ==============" );
448
449     root = globals->get_fg_root();
450     if ( ! root.length() ) {
451         // No root path set? Then bail ...
452         SG_LOG( SG_GENERAL, SG_ALERT,
453                 "Cannot continue without environment variable FG_ROOT"
454                 << "being defined." );
455         exit(-1);
456     }
457     SG_LOG( SG_GENERAL, SG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
458
459 #if defined(FX) && defined(XMESA)
460     // initialize full screen flag
461     global_fullscreen = false;
462     if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
463         // Test for the MESA_GLX_FX env variable
464         if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
465             // test if we are fullscreen mesa/glide
466             if ( (mesa_win_state[0] == 'f') ||
467                  (mesa_win_state[0] == 'F') ) {
468                 global_fullscreen = true;
469             }
470         }
471     }
472 #endif
473
474     return true;
475 }
476
477
478 // This is the top level init routine which calls all the other
479 // initialization routines.  If you are adding a subsystem to flight
480 // gear, its initialization call should located in this routine.
481 // Returns non-zero if a problem encountered.
482 bool fgInitSubsystems( void ) {
483     fgLIGHT *l = &cur_light_params;
484
485     SG_LOG( SG_GENERAL, SG_INFO, "Initialize Subsystems");
486     SG_LOG( SG_GENERAL, SG_INFO, "========== ==========");
487
488
489     ////////////////////////////////////////////////////////////////////
490     // Initialize the material property subsystem.
491     ////////////////////////////////////////////////////////////////////
492
493     SGPath mpath( globals->get_fg_root() );
494     mpath.append( "materials" );
495     if ( material_lib.load( mpath.str() ) ) {
496     } else {
497         SG_LOG( SG_GENERAL, SG_ALERT, "Error loading material lib!" );
498         exit(-1);
499     }
500
501
502     ////////////////////////////////////////////////////////////////////
503     // Initialize the scenery management subsystem.
504     ////////////////////////////////////////////////////////////////////
505
506     if ( fgSceneryInit() ) {
507         // Material lib initialized ok.
508     } else {
509         SG_LOG( SG_GENERAL, SG_ALERT, "Error in Scenery initialization!" );
510         exit(-1);
511     }
512
513     if ( global_tile_mgr.init() ) {
514         // Load the local scenery data
515         global_tile_mgr.update( fgGetDouble("/position/longitude-deg"),
516                                 fgGetDouble("/position/latitude-deg") );
517     } else {
518         SG_LOG( SG_GENERAL, SG_ALERT, "Error in Tile Manager initialization!" );
519         exit(-1);
520     }
521
522     SG_LOG( SG_GENERAL, SG_DEBUG,
523             "Current terrain elevation after tile mgr init " <<
524             scenery.cur_elev );
525
526
527     ////////////////////////////////////////////////////////////////////
528     // Initialize the flight model subsystem.
529     ////////////////////////////////////////////////////////////////////
530
531     double dt = 1.0 / fgGetInt("/sim/model-hz");
532     // cout << "dt = " << dt << endl;
533
534     aircraft_dir = fgGetString("/sim/aircraft-dir");
535     const string &model = fgGetString("/sim/flight-model");
536     try {
537         if (model == "larcsim") {
538             cur_fdm_state = new FGLaRCsim( dt );
539         } else if (model == "jsb") {
540             cur_fdm_state = new FGJSBsim( dt );
541         } else if (model == "ada") {
542             cur_fdm_state = new FGADA( dt );
543         } else if (model == "balloon") {
544             cur_fdm_state = new FGBalloonSim( dt );
545         } else if (model == "magic") {
546             cur_fdm_state = new FGMagicCarpet( dt );
547         } else if (model == "external") {
548             cur_fdm_state = new FGExternal( dt );
549         } else {
550             SG_LOG(SG_GENERAL, SG_ALERT,
551                    "Unrecognized flight model '" << model
552                    << ", can't init aircraft");
553             exit(-1);
554         }
555     } catch ( ... ) {
556         SG_LOG(SG_GENERAL, SG_ALERT, "FlightGear aborting\n\n");
557         exit(-1);
558     }
559
560     cur_fdm_state->init();
561     cur_fdm_state->bind();
562     
563     // allocates structures so must happen before any of the flight
564     // model or control parameters are set
565     fgAircraftInit();   // In the future this might not be the case.
566
567
568     ////////////////////////////////////////////////////////////////////
569     // Initialize the event manager subsystem.
570     ////////////////////////////////////////////////////////////////////
571
572     global_events.Init();
573
574     // Output event stats every 60 seconds
575     global_events.Register( "fgEVENT_MGR::PrintStats()",
576                             fgMethodCallback<fgEVENT_MGR>( &global_events,
577                                                    &fgEVENT_MGR::PrintStats),
578                             fgEVENT::FG_EVENT_READY, 60000 );
579
580
581     ////////////////////////////////////////////////////////////////////
582     // Initialize the view manager subsystem.
583     ////////////////////////////////////////////////////////////////////
584
585     // Initialize win_ratio parameters
586     for ( int i = 0; i < globals->get_viewmgr()->size(); ++i ) {
587         globals->get_viewmgr()->get_view(i)->
588             set_win_ratio( fgGetInt("/sim/startup/xsize") /
589                            fgGetInt("/sim/startup/ysize") );
590     }
591
592     // Initialize pilot view
593     FGViewerRPH *pilot_view =
594         (FGViewerRPH *)globals->get_viewmgr()->get_view( 0 );
595
596     pilot_view->set_geod_view_pos( cur_fdm_state->get_Longitude(), 
597                                    cur_fdm_state->get_Lat_geocentric(), 
598                                    cur_fdm_state->get_Altitude() *
599                                    SG_FEET_TO_METER );
600     pilot_view->set_sea_level_radius( cur_fdm_state->get_Sea_level_radius() *
601                                       SG_FEET_TO_METER ); 
602     pilot_view->set_rph( cur_fdm_state->get_Phi(),
603                          cur_fdm_state->get_Theta(),
604                          cur_fdm_state->get_Psi() );
605
606     // set current view to 0 (first) which is our main pilot view
607     globals->set_current_view( pilot_view );
608
609     SG_LOG( SG_GENERAL, SG_DEBUG, "  abs_view_pos = "
610             << globals->get_current_view()->get_abs_view_pos());
611
612
613     ////////////////////////////////////////////////////////////////////
614     // Initialize the lighting subsystem.
615     ////////////////////////////////////////////////////////////////////
616
617     // fgUpdateSunPos() needs a few position and view parameters set
618     // so it can calculate local relative sun angle and a few other
619     // things for correctly orienting the sky.
620     fgUpdateSunPos();
621     fgUpdateMoonPos();
622     global_events.Register( "fgUpdateSunPos()", fgUpdateSunPos,
623                             fgEVENT::FG_EVENT_READY, 60000);
624     global_events.Register( "fgUpdateMoonPos()", fgUpdateMoonPos,
625                             fgEVENT::FG_EVENT_READY, 60000);
626
627     // Initialize Lighting interpolation tables
628     l->Init();
629
630     // update the lighting parameters (based on sun angle)
631     global_events.Register( "fgLight::Update()",
632                             fgMethodCallback<fgLIGHT>( &cur_light_params,
633                                                        &fgLIGHT::Update),
634                             fgEVENT::FG_EVENT_READY, 30000 );
635
636
637     ////////////////////////////////////////////////////////////////////
638     // Initialize the local time subsystem.
639     ////////////////////////////////////////////////////////////////////
640
641     // update the current timezone each 30 minutes
642     global_events.Register( "fgUpdateLocalTime()", fgUpdateLocalTime,
643                             fgEVENT::FG_EVENT_READY, 1800000);
644
645
646     ////////////////////////////////////////////////////////////////////
647     // Initialize the weather subsystem.
648     ////////////////////////////////////////////////////////////////////
649
650     // Initialize the weather modeling subsystem
651 #ifndef FG_OLD_WEATHER
652     // Initialize the WeatherDatabase
653     SG_LOG(SG_GENERAL, SG_INFO, "Creating LocalWeatherDatabase");
654     sgVec3 position;
655     sgSetVec3( position, current_aircraft.fdm_state->get_Latitude(),
656                current_aircraft.fdm_state->get_Longitude(),
657                current_aircraft.fdm_state->get_Altitude() * SG_FEET_TO_METER );
658     FGLocalWeatherDatabase::theFGLocalWeatherDatabase = 
659         new FGLocalWeatherDatabase( position,
660                                     globals->get_fg_root() );
661     // cout << theFGLocalWeatherDatabase << endl;
662     // cout << "visibility = " 
663     //      << theFGLocalWeatherDatabase->getWeatherVisibility() << endl;
664
665     WeatherDatabase = FGLocalWeatherDatabase::theFGLocalWeatherDatabase;
666     
667     double init_vis = fgGetDouble("/environment/visibility-m");
668     if ( init_vis > 0 ) {
669         WeatherDatabase->setWeatherVisibility( init_vis );
670     }
671
672     // register the periodic update of the weather
673     global_events.Register( "weather update", fgUpdateWeatherDatabase,
674                             fgEVENT::FG_EVENT_READY, 30000);
675 #else
676     current_weather.Init();
677 #endif
678
679     ////////////////////////////////////////////////////////////////////
680     // Initialize vor/ndb/ils/fix list management and query systems
681     ////////////////////////////////////////////////////////////////////
682
683     SG_LOG(SG_GENERAL, SG_INFO, "Loading Navaids");
684
685     SG_LOG(SG_GENERAL, SG_INFO, "  VOR/NDB");
686     current_navlist = new FGNavList;
687     SGPath p_nav( globals->get_fg_root() );
688     p_nav.append( "Navaids/default.nav" );
689     current_navlist->init( p_nav );
690
691     SG_LOG(SG_GENERAL, SG_INFO, "  ILS and Marker Beacons");
692     current_beacons = new FGMarkerBeacons;
693     current_beacons->init();
694     current_ilslist = new FGILSList;
695     SGPath p_ils( globals->get_fg_root() );
696     p_ils.append( "Navaids/default.ils" );
697     current_ilslist->init( p_ils );
698
699     SG_LOG(SG_GENERAL, SG_INFO, "  Fixes");
700     current_fixlist = new FGFixList;
701     SGPath p_fix( globals->get_fg_root() );
702     p_fix.append( "Navaids/default.fix" );
703     current_fixlist->init( p_fix );
704
705
706     ////////////////////////////////////////////////////////////////////
707     // Initialize the built-in commands.
708     ////////////////////////////////////////////////////////////////////
709     fgInitCommands();
710
711
712     ////////////////////////////////////////////////////////////////////
713     // Initialize the radio stack subsystem.
714     ////////////////////////////////////////////////////////////////////
715
716                                 // A textbook example of how FGSubsystem
717                                 // should work...
718     current_radiostack = new FGRadioStack;
719     current_radiostack->init();
720     current_radiostack->bind();
721
722
723     ////////////////////////////////////////////////////////////////////
724     // Initialize the cockpit subsystem
725     ////////////////////////////////////////////////////////////////////
726
727     if( fgCockpitInit( &current_aircraft )) {
728         // Cockpit initialized ok.
729     } else {
730         SG_LOG( SG_GENERAL, SG_ALERT, "Error in Cockpit initialization!" );
731         exit(-1);
732     }
733
734
735     ////////////////////////////////////////////////////////////////////
736     // Initialize the joystick subsystem.
737     ////////////////////////////////////////////////////////////////////
738
739     // if ( ! fgJoystickInit() ) {
740     //   SG_LOG( SG_GENERAL, SG_ALERT, "Error in Joystick initialization!" );
741     // }
742
743
744     ////////////////////////////////////////////////////////////////////
745     // Initialize the autopilot subsystem.
746     ////////////////////////////////////////////////////////////////////
747
748     current_autopilot = new FGAutopilot;
749     current_autopilot->init();
750
751     // initialize the gui parts of the autopilot
752     NewTgtAirportInit();
753     fgAPAdjustInit() ;
754     NewHeadingInit();
755     NewAltitudeInit();
756
757     
758     ////////////////////////////////////////////////////////////////////
759     // Initialize I/O subsystem.
760     ////////////////////////////////////////////////////////////////////
761
762 #if ! defined( macintosh )
763     fgIOInit();
764 #endif
765
766     // Initialize the 2D panel.
767     string panel_path = fgGetString("/sim/panel/path",
768                                     "Panels/Default/default.xml");
769     current_panel = fgReadPanel(panel_path);
770     if (current_panel == 0) {
771         SG_LOG( SG_INPUT, SG_ALERT, 
772                 "Error reading new panel from " << panel_path );
773     } else {
774         SG_LOG( SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path );
775         current_panel->init();
776         current_panel->bind();
777     }
778
779     
780     ////////////////////////////////////////////////////////////////////
781     // Initialize the default (kludged) properties.
782     ////////////////////////////////////////////////////////////////////
783
784     fgInitProps();
785
786
787     ////////////////////////////////////////////////////////////////////
788     // Initialize the controls subsystem.
789     ////////////////////////////////////////////////////////////////////
790
791     controls.init();
792     controls.bind();
793
794
795     ////////////////////////////////////////////////////////////////////
796     // Initialize the input subsystem.
797     ////////////////////////////////////////////////////////////////////
798
799     current_input.init();
800     current_input.bind();
801
802
803     ////////////////////////////////////////////////////////////////////////
804     // End of subsystem initialization.
805     ////////////////////////////////////////////////////////////////////
806
807     SG_LOG( SG_GENERAL, SG_INFO, endl);
808
809                                 // Save the initial state for future
810                                 // reference.
811     globals->saveInitialState();
812
813     return true;
814 }
815
816
817 void fgReInitSubsystems( void )
818 {
819     SG_LOG( SG_GENERAL, SG_INFO,
820             "/position/altitude = " << fgGetDouble("/position/altitude-ft") );
821
822     bool freeze = globals->get_freeze();
823     if( !freeze )
824         globals->set_freeze( true );
825     
826     // Initialize the Scenery Management subsystem
827     if ( ! fgSceneryInit() ) {
828         SG_LOG( SG_GENERAL, SG_ALERT, "Error in Scenery initialization!" );
829         exit(-1);
830     }
831
832     if( global_tile_mgr.init() ) {
833         // Load the local scenery data
834         global_tile_mgr.update( fgGetDouble("/position/longitude-deg"),
835                                 fgGetDouble("/position/latitude-deg") );
836     } else {
837         SG_LOG( SG_GENERAL, SG_ALERT, "Error in Tile Manager initialization!" );
838                 exit(-1);
839     }
840
841     // Initialize view parameters
842     FGViewerRPH *pilot_view =
843         (FGViewerRPH *)globals->get_viewmgr()->get_view( 0 );
844
845     pilot_view->set_view_offset( 0.0 );
846     pilot_view->set_goal_view_offset( 0.0 );
847
848     pilot_view->set_geod_view_pos( cur_fdm_state->get_Longitude(), 
849                                    cur_fdm_state->get_Lat_geocentric(), 
850                                    cur_fdm_state->get_Altitude() *
851                                    SG_FEET_TO_METER );
852     pilot_view->set_sea_level_radius( cur_fdm_state->get_Sea_level_radius() *
853                                       SG_FEET_TO_METER ); 
854     pilot_view->set_rph( cur_fdm_state->get_Phi(),
855                          cur_fdm_state->get_Theta(),
856                          cur_fdm_state->get_Psi() );
857
858     // set current view to 0 (first) which is our main pilot view
859     globals->set_current_view( pilot_view );
860
861     SG_LOG( SG_GENERAL, SG_DEBUG, "  abs_view_pos = "
862             << globals->get_current_view()->get_abs_view_pos());
863
864     cur_fdm_state->init();
865
866     controls.reset_all();
867     current_autopilot->reset();
868
869     fgUpdateSunPos();
870     fgUpdateMoonPos();
871     cur_light_params.Update();
872     fgUpdateLocalTime();
873
874     if( !freeze )
875         globals->set_freeze( false );
876 }