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