]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_init.cxx
Use try/catch to flag FDM initialization errors.
[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 // Read in configuration (file and command line)
185 bool fgInitConfig ( int argc, char **argv ) {
186
187                                 // First, set some sane default values
188     fgSetDefaults();
189
190     // Read global preferences from $FG_ROOT/preferences.xml
191     SGPath props_path(globals->get_fg_root());
192     props_path.append("preferences.xml");
193     SG_LOG(SG_INPUT, SG_INFO, "Reading global preferences");
194     if (!readProperties(props_path.str(), globals->get_props())) {
195       SG_LOG(SG_INPUT, SG_ALERT, "Failed to read global preferences from "
196              << props_path.str());
197     } else {
198       SG_LOG(SG_INPUT, SG_INFO, "Finished Reading global preferences");
199     }
200
201     // Attempt to locate and parse the various config files in order
202     // from least precidence to greatest precidence
203
204     // Check for $fg_root/system.fgfsrc
205     SGPath config( globals->get_fg_root() );
206     config.append( "system.fgfsrc" );
207     fgParseOptions(config.str());
208
209     char name[256];
210 #if defined( unix ) || defined( __CYGWIN__ )
211     // Check for $fg_root/system.fgfsrc.hostname
212     gethostname( name, 256 );
213     config.concat( "." );
214     config.concat( name );
215     fgParseOptions(config.str());
216 #endif
217
218     // Check for ~/.fgfsrc
219     char* envp = ::getenv( "HOME" );
220     if ( envp != NULL ) {
221         config.set( envp );
222         config.append( ".fgfsrc" );
223         fgParseOptions(config.str());
224     }
225
226 #if defined( unix ) || defined( __CYGWIN__ )
227     // Check for ~/.fgfsrc.hostname
228     gethostname( name, 256 );
229     config.concat( "." );
230     config.concat( name );
231     fgParseOptions(config.str());
232 #endif
233
234     // Parse remaining command line options
235     // These will override anything specified in a config file
236     fgParseOptions(argc, argv);
237
238     return true;
239 }
240
241
242 // find basic airport location info from airport database
243 bool fgFindAirportID( const string& id, FGAirport *a ) {
244     if ( id.length() ) {
245         SGPath path( globals->get_fg_root() );
246         path.append( "Airports" );
247         path.append( "simple.mk4" );
248         FGAirports airports( path.c_str() );
249
250         SG_LOG( SG_GENERAL, SG_INFO, "Searching for airport code = " << id );
251
252         if ( ! airports.search( id, a ) ) {
253             SG_LOG( SG_GENERAL, SG_ALERT,
254                     "Failed to find " << id << " in " << path.str() );
255             return false;
256         }
257     } else {
258         return false;
259     }
260
261     SG_LOG( SG_GENERAL, SG_INFO,
262             "Position for " << id << " is ("
263             << a->longitude << ", "
264             << a->latitude << ")" );
265
266     return true;
267 }
268
269
270 // Set current_options lon/lat given an airport id
271 bool fgSetPosFromAirportID( const string& id ) {
272     FGAirport a;
273     // double lon, lat;
274
275     SG_LOG( SG_GENERAL, SG_INFO,
276             "Attempting to set starting position from airport code " << id );
277
278     if ( fgFindAirportID( id, &a ) ) {
279         fgSetDouble("/position/longitude-deg",  a.longitude );
280         fgSetDouble("/position/latitude-deg",  a.latitude );
281         SG_LOG( SG_GENERAL, SG_INFO,
282                 "Position for " << id << " is ("
283                 << a.longitude << ", "
284                 << a.latitude << ")" );
285
286         return true;
287     } else {
288         return false;
289     }
290
291 }
292
293
294 // Set current_options lon/lat given an airport id and heading (degrees)
295 bool fgSetPosFromAirportIDandHdg( const string& id, double tgt_hdg ) {
296     FGRunway r;
297     FGRunway found_r;
298     double found_dir = 0.0;
299
300     if ( id.length() ) {
301         // set initial position from runway and heading
302
303         SGPath path( globals->get_fg_root() );
304         path.append( "Airports" );
305         path.append( "runways.mk4" );
306         FGRunways runways( path.c_str() );
307
308         SG_LOG( SG_GENERAL, SG_INFO,
309                 "Attempting to set starting position from runway code "
310                 << id << " heading " << tgt_hdg );
311
312         // SGPath inpath( globals->get_fg_root() );
313         // inpath.append( "Airports" );
314         // inpath.append( "apt_simple" );
315         // airports.load( inpath.c_str() );
316
317         // SGPath outpath( globals->get_fg_root() );
318         // outpath.append( "Airports" );
319         // outpath.append( "simple.gdbm" );
320         // airports.dump_gdbm( outpath.c_str() );
321
322         if ( ! runways.search( id, &r ) ) {
323             SG_LOG( SG_GENERAL, SG_ALERT,
324                     "Failed to find " << id << " in database." );
325             return false;
326         }
327
328         double diff;
329         double min_diff = 360.0;
330
331         while ( r.id == id ) {
332             // forward direction
333             diff = tgt_hdg - r.heading;
334             while ( diff < -180.0 ) { diff += 360.0; }
335             while ( diff >  180.0 ) { diff -= 360.0; }
336             diff = fabs(diff);
337             SG_LOG( SG_GENERAL, SG_INFO,
338                     "Runway " << r.rwy_no << " heading = " << r.heading <<
339                     " diff = " << diff );
340             if ( diff < min_diff ) {
341                 min_diff = diff;
342                 found_r = r;
343                 found_dir = 0;
344             }
345
346             // reverse direction
347             diff = tgt_hdg - r.heading - 180.0;
348             while ( diff < -180.0 ) { diff += 360.0; }
349             while ( diff >  180.0 ) { diff -= 360.0; }
350             diff = fabs(diff);
351             SG_LOG( SG_GENERAL, SG_INFO,
352                     "Runway -" << r.rwy_no << " heading = " <<
353                     r.heading + 180.0 <<
354                     " diff = " << diff );
355             if ( diff < min_diff ) {
356                 min_diff = diff;
357                 found_r = r;
358                 found_dir = 180.0;
359             }
360
361             runways.next( &r );
362         }
363
364         SG_LOG( SG_GENERAL, SG_INFO, "closest runway = " << found_r.rwy_no
365                 << " + " << found_dir );
366
367     } else {
368         return false;
369     }
370
371     double heading = found_r.heading + found_dir;
372     while ( heading >= 360.0 ) { heading -= 360.0; }
373
374     double lat2, lon2, az2;
375     double azimuth = found_r.heading + found_dir + 180.0;
376     while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
377
378     SG_LOG( SG_GENERAL, SG_INFO,
379             "runway =  " << found_r.lon << ", " << found_r.lat
380             << " length = " << found_r.length * SG_FEET_TO_METER * 0.5 
381             << " heading = " << azimuth );
382     
383     geo_direct_wgs_84 ( 0, found_r.lat, found_r.lon, 
384                         azimuth, found_r.length * SG_FEET_TO_METER * 0.5 - 5.0,
385                         &lat2, &lon2, &az2 );
386
387     if ( fabs( fgGetDouble("/sim/startup/offset-distance") ) > SG_EPSILON ) {
388         double olat, olon;
389         double odist = fgGetDouble("/sim/startup/offset-distance");
390         odist *= SG_NM_TO_METER;
391         double oaz = azimuth;
392         if ( fabs(fgGetDouble("/sim/startup/offset-azimuth")) > SG_EPSILON ) {
393             oaz = fgGetDouble("/sim/startup/offset-azimuth") + 180;
394         }
395         while ( oaz >= 360.0 ) { oaz -= 360.0; }
396         geo_direct_wgs_84 ( 0, lat2, lon2, oaz, odist, &olat, &olon, &az2 );
397         lat2=olat;
398         lon2=olon;
399     }
400     fgSetDouble("/position/longitude-deg",  lon2 );
401     fgSetDouble("/position/latitude-deg",  lat2 );
402     fgSetDouble("/orientation/heading-deg", heading );
403
404     SG_LOG( SG_GENERAL, SG_INFO,
405             "Position for " << id << " is ("
406             << lon2 << ", "
407             << lat2 << ") new heading is "
408             << heading );
409
410     return true;
411 }
412
413
414 // General house keeping initializations
415 bool fgInitGeneral( void ) {
416     string root;
417
418 #if defined(FX) && defined(XMESA)
419     char *mesa_win_state;
420 #endif
421
422     SG_LOG( SG_GENERAL, SG_INFO, "General Initialization" );
423     SG_LOG( SG_GENERAL, SG_INFO, "======= ==============" );
424
425     root = globals->get_fg_root();
426     if ( ! root.length() ) {
427         // No root path set? Then bail ...
428         SG_LOG( SG_GENERAL, SG_ALERT,
429                 "Cannot continue without environment variable FG_ROOT"
430                 << "being defined." );
431         exit(-1);
432     }
433     SG_LOG( SG_GENERAL, SG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
434
435 #if defined(FX) && defined(XMESA)
436     // initialize full screen flag
437     global_fullscreen = false;
438     if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
439         // Test for the MESA_GLX_FX env variable
440         if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
441             // test if we are fullscreen mesa/glide
442             if ( (mesa_win_state[0] == 'f') ||
443                  (mesa_win_state[0] == 'F') ) {
444                 global_fullscreen = true;
445             }
446         }
447     }
448 #endif
449
450     return true;
451 }
452
453
454 // This is the top level init routine which calls all the other
455 // initialization routines.  If you are adding a subsystem to flight
456 // gear, its initialization call should located in this routine.
457 // Returns non-zero if a problem encountered.
458 bool fgInitSubsystems( void ) {
459     fgLIGHT *l = &cur_light_params;
460
461     SG_LOG( SG_GENERAL, SG_INFO, "Initialize Subsystems");
462     SG_LOG( SG_GENERAL, SG_INFO, "========== ==========");
463
464
465     ////////////////////////////////////////////////////////////////////
466     // Initialize the material property subsystem.
467     ////////////////////////////////////////////////////////////////////
468
469     SGPath mpath( globals->get_fg_root() );
470     mpath.append( "materials" );
471     if ( material_lib.load( mpath.str() ) ) {
472     } else {
473         SG_LOG( SG_GENERAL, SG_ALERT, "Error loading material lib!" );
474         exit(-1);
475     }
476
477
478     ////////////////////////////////////////////////////////////////////
479     // Initialize the scenery management subsystem.
480     ////////////////////////////////////////////////////////////////////
481
482     if ( fgSceneryInit() ) {
483         // Material lib initialized ok.
484     } else {
485         SG_LOG( SG_GENERAL, SG_ALERT, "Error in Scenery initialization!" );
486         exit(-1);
487     }
488
489     if ( global_tile_mgr.init() ) {
490         // Load the local scenery data
491         global_tile_mgr.update( fgGetDouble("/position/longitude-deg"),
492                                 fgGetDouble("/position/latitude-deg") );
493     } else {
494         SG_LOG( SG_GENERAL, SG_ALERT, "Error in Tile Manager initialization!" );
495         exit(-1);
496     }
497
498     SG_LOG( SG_GENERAL, SG_DEBUG,
499             "Current terrain elevation after tile mgr init " <<
500             scenery.cur_elev );
501
502
503     ////////////////////////////////////////////////////////////////////
504     // Initialize the flight model subsystem.
505     ////////////////////////////////////////////////////////////////////
506
507     double dt = 1.0 / fgGetInt("/sim/model-hz");
508     // cout << "dt = " << dt << endl;
509
510     aircraft_dir = fgGetString("/sim/aircraft-dir");
511     const string &model = fgGetString("/sim/flight-model");
512     try {
513         if (model == "larcsim") {
514             cur_fdm_state = new FGLaRCsim( dt );
515         } else if (model == "jsb") {
516             cur_fdm_state = new FGJSBsim( dt );
517         } else if (model == "ada") {
518             cur_fdm_state = new FGADA( dt );
519         } else if (model == "balloon") {
520             cur_fdm_state = new FGBalloonSim( dt );
521         } else if (model == "magic") {
522             cur_fdm_state = new FGMagicCarpet( dt );
523         } else if (model == "external") {
524             cur_fdm_state = new FGExternal( dt );
525         } else {
526             SG_LOG(SG_GENERAL, SG_ALERT,
527                    "Unrecognized flight model '" << model
528                    << ", can't init aircraft");
529             exit(-1);
530         }
531     } catch ( ... ) {
532         SG_LOG(SG_GENERAL, SG_ALERT, "FlightGear aborting\n\n");
533         exit(-1);
534     }
535
536     cur_fdm_state->init();
537     cur_fdm_state->bind();
538     
539     // allocates structures so must happen before any of the flight
540     // model or control parameters are set
541     fgAircraftInit();   // In the future this might not be the case.
542
543
544     ////////////////////////////////////////////////////////////////////
545     // Initialize the event manager subsystem.
546     ////////////////////////////////////////////////////////////////////
547
548     global_events.Init();
549
550     // Output event stats every 60 seconds
551     global_events.Register( "fgEVENT_MGR::PrintStats()",
552                             fgMethodCallback<fgEVENT_MGR>( &global_events,
553                                                    &fgEVENT_MGR::PrintStats),
554                             fgEVENT::FG_EVENT_READY, 60000 );
555
556
557     ////////////////////////////////////////////////////////////////////
558     // Initialize the view manager subsystem.
559     ////////////////////////////////////////////////////////////////////
560
561     // Initialize win_ratio parameters
562     for ( int i = 0; i < globals->get_viewmgr()->size(); ++i ) {
563         globals->get_viewmgr()->get_view(i)->
564             set_win_ratio( fgGetInt("/sim/startup/xsize") /
565                            fgGetInt("/sim/startup/ysize") );
566     }
567
568     // Initialize pilot view
569     FGViewerRPH *pilot_view =
570         (FGViewerRPH *)globals->get_viewmgr()->get_view( 0 );
571
572     pilot_view->set_geod_view_pos( cur_fdm_state->get_Longitude(), 
573                                    cur_fdm_state->get_Lat_geocentric(), 
574                                    cur_fdm_state->get_Altitude() *
575                                    SG_FEET_TO_METER );
576     pilot_view->set_sea_level_radius( cur_fdm_state->get_Sea_level_radius() *
577                                       SG_FEET_TO_METER ); 
578     pilot_view->set_rph( cur_fdm_state->get_Phi(),
579                          cur_fdm_state->get_Theta(),
580                          cur_fdm_state->get_Psi() );
581
582     // set current view to 0 (first) which is our main pilot view
583     globals->set_current_view( pilot_view );
584
585     SG_LOG( SG_GENERAL, SG_DEBUG, "  abs_view_pos = "
586             << globals->get_current_view()->get_abs_view_pos());
587
588
589     ////////////////////////////////////////////////////////////////////
590     // Initialize the lighting subsystem.
591     ////////////////////////////////////////////////////////////////////
592
593     // fgUpdateSunPos() needs a few position and view parameters set
594     // so it can calculate local relative sun angle and a few other
595     // things for correctly orienting the sky.
596     fgUpdateSunPos();
597     fgUpdateMoonPos();
598     global_events.Register( "fgUpdateSunPos()", fgUpdateSunPos,
599                             fgEVENT::FG_EVENT_READY, 60000);
600     global_events.Register( "fgUpdateMoonPos()", fgUpdateMoonPos,
601                             fgEVENT::FG_EVENT_READY, 60000);
602
603     // Initialize Lighting interpolation tables
604     l->Init();
605
606     // update the lighting parameters (based on sun angle)
607     global_events.Register( "fgLight::Update()",
608                             fgMethodCallback<fgLIGHT>( &cur_light_params,
609                                                        &fgLIGHT::Update),
610                             fgEVENT::FG_EVENT_READY, 30000 );
611
612
613     ////////////////////////////////////////////////////////////////////
614     // Initialize the local time subsystem.
615     ////////////////////////////////////////////////////////////////////
616
617     // update the current timezone each 30 minutes
618     global_events.Register( "fgUpdateLocalTime()", fgUpdateLocalTime,
619                             fgEVENT::FG_EVENT_READY, 1800000);
620
621
622     ////////////////////////////////////////////////////////////////////
623     // Initialize the weather subsystem.
624     ////////////////////////////////////////////////////////////////////
625
626     // Initialize the weather modeling subsystem
627 #ifndef FG_OLD_WEATHER
628     // Initialize the WeatherDatabase
629     SG_LOG(SG_GENERAL, SG_INFO, "Creating LocalWeatherDatabase");
630     sgVec3 position;
631     sgSetVec3( position, current_aircraft.fdm_state->get_Latitude(),
632                current_aircraft.fdm_state->get_Longitude(),
633                current_aircraft.fdm_state->get_Altitude() * SG_FEET_TO_METER );
634     FGLocalWeatherDatabase::theFGLocalWeatherDatabase = 
635         new FGLocalWeatherDatabase( position,
636                                     globals->get_fg_root() );
637     // cout << theFGLocalWeatherDatabase << endl;
638     // cout << "visibility = " 
639     //      << theFGLocalWeatherDatabase->getWeatherVisibility() << endl;
640
641     WeatherDatabase = FGLocalWeatherDatabase::theFGLocalWeatherDatabase;
642     
643     double init_vis = fgGetDouble("/environment/visibility-m");
644     if ( init_vis > 0 ) {
645         WeatherDatabase->setWeatherVisibility( init_vis );
646     }
647
648     // register the periodic update of the weather
649     global_events.Register( "weather update", fgUpdateWeatherDatabase,
650                             fgEVENT::FG_EVENT_READY, 30000);
651 #else
652     current_weather.Init();
653 #endif
654
655     ////////////////////////////////////////////////////////////////////
656     // Initialize vor/ndb/ils/fix list management and query systems
657     ////////////////////////////////////////////////////////////////////
658
659     SG_LOG(SG_GENERAL, SG_INFO, "Loading Navaids");
660
661     SG_LOG(SG_GENERAL, SG_INFO, "  VOR/NDB");
662     current_navlist = new FGNavList;
663     SGPath p_nav( globals->get_fg_root() );
664     p_nav.append( "Navaids/default.nav" );
665     current_navlist->init( p_nav );
666
667     SG_LOG(SG_GENERAL, SG_INFO, "  ILS and Marker Beacons");
668     current_beacons = new FGMarkerBeacons;
669     current_beacons->init();
670     current_ilslist = new FGILSList;
671     SGPath p_ils( globals->get_fg_root() );
672     p_ils.append( "Navaids/default.ils" );
673     current_ilslist->init( p_ils );
674
675     SG_LOG(SG_GENERAL, SG_INFO, "  Fixes");
676     current_fixlist = new FGFixList;
677     SGPath p_fix( globals->get_fg_root() );
678     p_fix.append( "Navaids/default.fix" );
679     current_fixlist->init( p_fix );
680
681
682     ////////////////////////////////////////////////////////////////////
683     // Initialize the built-in commands.
684     ////////////////////////////////////////////////////////////////////
685     fgInitCommands();
686
687
688     ////////////////////////////////////////////////////////////////////
689     // Initialize the radio stack subsystem.
690     ////////////////////////////////////////////////////////////////////
691
692                                 // A textbook example of how FGSubsystem
693                                 // should work...
694     current_radiostack = new FGRadioStack;
695     current_radiostack->init();
696     current_radiostack->bind();
697
698
699     ////////////////////////////////////////////////////////////////////
700     // Initialize the cockpit subsystem
701     ////////////////////////////////////////////////////////////////////
702
703     if( fgCockpitInit( &current_aircraft )) {
704         // Cockpit initialized ok.
705     } else {
706         SG_LOG( SG_GENERAL, SG_ALERT, "Error in Cockpit initialization!" );
707         exit(-1);
708     }
709
710
711     ////////////////////////////////////////////////////////////////////
712     // Initialize the joystick subsystem.
713     ////////////////////////////////////////////////////////////////////
714
715     // if ( ! fgJoystickInit() ) {
716     //   SG_LOG( SG_GENERAL, SG_ALERT, "Error in Joystick initialization!" );
717     // }
718
719
720     ////////////////////////////////////////////////////////////////////
721     // Initialize the autopilot subsystem.
722     ////////////////////////////////////////////////////////////////////
723
724     current_autopilot = new FGAutopilot;
725     current_autopilot->init();
726
727     // initialize the gui parts of the autopilot
728     NewTgtAirportInit();
729     fgAPAdjustInit() ;
730     NewHeadingInit();
731     NewAltitudeInit();
732
733     
734     ////////////////////////////////////////////////////////////////////
735     // Initialize I/O subsystem.
736     ////////////////////////////////////////////////////////////////////
737
738 #if ! defined( macintosh )
739     fgIOInit();
740 #endif
741
742     // Initialize the 2D panel.
743     string panel_path = fgGetString("/sim/panel/path",
744                                     "Panels/Default/default.xml");
745     current_panel = fgReadPanel(panel_path);
746     if (current_panel == 0) {
747         SG_LOG( SG_INPUT, SG_ALERT, 
748                 "Error reading new panel from " << panel_path );
749     } else {
750         SG_LOG( SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path );
751         current_panel->init();
752         current_panel->bind();
753     }
754
755     
756     ////////////////////////////////////////////////////////////////////
757     // Initialize the default (kludged) properties.
758     ////////////////////////////////////////////////////////////////////
759
760     fgInitProps();
761
762
763     ////////////////////////////////////////////////////////////////////
764     // Initialize the controls subsystem.
765     ////////////////////////////////////////////////////////////////////
766
767     controls.init();
768     controls.bind();
769
770
771     ////////////////////////////////////////////////////////////////////
772     // Initialize the input subsystem.
773     ////////////////////////////////////////////////////////////////////
774
775     current_input.init();
776     current_input.bind();
777
778
779     ////////////////////////////////////////////////////////////////////////
780     // End of subsystem initialization.
781     ////////////////////////////////////////////////////////////////////
782
783     SG_LOG( SG_GENERAL, SG_INFO, endl);
784
785                                 // Save the initial state for future
786                                 // reference.
787     globals->saveInitialState();
788
789     return true;
790 }
791
792
793 void fgReInitSubsystems( void )
794 {
795     SG_LOG( SG_GENERAL, SG_INFO,
796             "/position/altitude = " << fgGetDouble("/position/altitude-ft") );
797
798     bool freeze = globals->get_freeze();
799     if( !freeze )
800         globals->set_freeze( true );
801     
802     // Initialize the Scenery Management subsystem
803     if ( ! fgSceneryInit() ) {
804         SG_LOG( SG_GENERAL, SG_ALERT, "Error in Scenery initialization!" );
805         exit(-1);
806     }
807
808     if( global_tile_mgr.init() ) {
809         // Load the local scenery data
810         global_tile_mgr.update( fgGetDouble("/position/longitude-deg"),
811                                 fgGetDouble("/position/latitude-deg") );
812     } else {
813         SG_LOG( SG_GENERAL, SG_ALERT, "Error in Tile Manager initialization!" );
814                 exit(-1);
815     }
816
817     // Initialize view parameters
818     FGViewerRPH *pilot_view =
819         (FGViewerRPH *)globals->get_viewmgr()->get_view( 0 );
820
821     pilot_view->set_view_offset( 0.0 );
822     pilot_view->set_goal_view_offset( 0.0 );
823
824     pilot_view->set_geod_view_pos( cur_fdm_state->get_Longitude(), 
825                                    cur_fdm_state->get_Lat_geocentric(), 
826                                    cur_fdm_state->get_Altitude() *
827                                    SG_FEET_TO_METER );
828     pilot_view->set_sea_level_radius( cur_fdm_state->get_Sea_level_radius() *
829                                       SG_FEET_TO_METER ); 
830     pilot_view->set_rph( cur_fdm_state->get_Phi(),
831                          cur_fdm_state->get_Theta(),
832                          cur_fdm_state->get_Psi() );
833
834     // set current view to 0 (first) which is our main pilot view
835     globals->set_current_view( pilot_view );
836
837     SG_LOG( SG_GENERAL, SG_DEBUG, "  abs_view_pos = "
838             << globals->get_current_view()->get_abs_view_pos());
839
840     cur_fdm_state->init();
841
842     controls.reset_all();
843     current_autopilot->reset();
844
845     fgUpdateSunPos();
846     fgUpdateMoonPos();
847     cur_light_params.Update();
848     fgUpdateLocalTime();
849
850     if( !freeze )
851         globals->set_freeze( false );
852 }