]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_init.cxx
d6659673dca1b0e3f52d02eac9d883e5df817652
[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 FG_MATH_EXCEPTION_CLASH
31 #  include <math.h>
32 #endif
33
34 #include <GL/glut.h>
35 #include <simgear/xgl/xgl.h>
36
37 #include <stdio.h>
38 #include <stdlib.h>
39
40 // work around a stdc++ lib bug in some versions of linux, but doesn't
41 // seem to hurt to have this here for all versions of Linux.
42 #ifdef linux
43 #  define _G_NO_EXTERN_TEMPLATES
44 #endif
45
46 #include <simgear/compiler.h>
47
48 #include STL_STRING
49
50 #include <simgear/constants.h>
51 #include <simgear/debug/logstream.hxx>
52 #include <simgear/math/point3d.hxx>
53 #include <simgear/math/polar3d.hxx>
54 #include <simgear/math/sg_geodesy.hxx>
55 #include <simgear/misc/fgpath.hxx>
56 #include <simgear/timing/sg_time.hxx>
57
58 #include <Aircraft/aircraft.hxx>
59 #include <Airports/runways.hxx>
60 #include <Airports/simple.hxx>
61 #include <Autopilot/auto_gui.hxx>
62 #include <Autopilot/newauto.hxx>
63 #include <Cockpit/cockpit.hxx>
64 #include <Cockpit/radiostack.hxx>
65 #include <Cockpit/panel.hxx>
66 #include <Cockpit/panel_io.hxx>
67 #include <FDM/Balloon.h>
68 #include <FDM/External.hxx>
69 #include <FDM/JSBSim.hxx>
70 #include <FDM/LaRCsim.hxx>
71 #include <FDM/MagicCarpet.hxx>
72 #include <Include/general.hxx>
73 #include <Joystick/joystick.hxx>
74 #include <Objects/matlib.hxx>
75 #include <Navaids/fixlist.hxx>
76 #include <Navaids/ilslist.hxx>
77 #include <Navaids/navlist.hxx>
78 #include <Scenery/scenery.hxx>
79 #include <Scenery/tilemgr.hxx>
80 #include <Time/event.hxx>
81 #include <Time/light.hxx>
82 #include <Time/sunpos.hxx>
83 #include <Time/moonpos.hxx>
84 #include <Time/tmp.hxx>
85
86 #ifndef FG_OLD_WEATHER
87 #  include <WeatherCM/FGLocalWeatherDatabase.h>
88 #else
89 #  include <Weather/weather.hxx>
90 #endif
91
92 #include "fg_init.hxx"
93 #include "fg_io.hxx"
94 #include "globals.hxx"
95 #include "options.hxx"
96 #include "views.hxx"
97 #include "bfi.hxx"
98
99 #if defined(FX) && defined(XMESA)
100 #include <GL/xmesa.h>
101 #endif
102
103 FG_USING_STD(string);
104
105 extern const char *default_root;
106
107
108 // Read in configuration (file and command line) and just set fg_root
109 bool fgInitFGRoot ( int argc, char **argv ) {
110     // Attempt to locate and parse a config file
111     // First check fg_root
112     FGPath config( current_options.get_fg_root() );
113     config.append( "system.fgfsrc" );
114     current_options.scan_config_file_for_root( config.str() );
115
116     // Next check home directory
117     char* envp = ::getenv( "HOME" );
118     if ( envp != NULL ) {
119         config.set( envp );
120         config.append( ".fgfsrc" );
121         current_options.scan_config_file_for_root( config.str() );
122     }
123
124     // Parse remaining command line options
125     // These will override anything specified in a config file
126     current_options.scan_command_line_for_root(argc, argv);
127
128     return true;
129 }
130
131
132 // Read in configuration (file and command line)
133 bool fgInitConfig ( int argc, char **argv ) {
134     // Attempt to locate and parse a config file
135     // First check fg_root
136     FGPath config( current_options.get_fg_root() );
137     config.append( "system.fgfsrc" );
138     current_options.parse_config_file( config.str() );
139
140     // Next check home directory
141     char* envp = ::getenv( "HOME" );
142     if ( envp != NULL ) {
143         config.set( envp );
144         config.append( ".fgfsrc" );
145         current_options.parse_config_file( config.str() );
146     }
147
148     // Parse remaining command line options
149     // These will override anything specified in a config file
150     if ( current_options.parse_command_line(argc, argv) !=
151          fgOPTIONS::FG_OPTIONS_OK )
152     {
153         // Something must have gone horribly wrong with the command
154         // line parsing or maybe the user just requested help ... :-)
155         current_options.usage();
156         FG_LOG( FG_GENERAL, FG_ALERT, "\nExiting ...");
157         return false;
158     }
159
160     return true;
161 }
162
163
164 // Set current_options lon/lat given an airport id
165 bool fgSetPosFromAirportID( const string& id ) {
166     FGAirport a;
167     double lon, lat;
168
169     if ( id.length() ) {
170         // set initial position from airport id
171
172         FGPath path( current_options.get_fg_root() );
173         path.append( "Airports" );
174         path.append( "simple.mk4" );
175         FGAirports airports( path.c_str() );
176
177         FG_LOG( FG_GENERAL, FG_INFO,
178                 "Attempting to set starting position from airport code "
179                 << id );
180
181         // FGPath inpath( current_options.get_fg_root() );
182         // inpath.append( "Airports" );
183         // inpath.append( "apt_simple" );
184         // airports.load( inpath.c_str() );
185
186         // FGPath outpath( current_options.get_fg_root() );
187         // outpath.append( "Airports" );
188         // outpath.append( "simple.gdbm" );
189         // airports.dump_gdbm( outpath.c_str() );
190
191         if ( ! airports.search( id, &a ) ) {
192             FG_LOG( FG_GENERAL, FG_ALERT,
193                     "Failed to find " << id << " in database." );
194             return false;
195         } else {
196             current_options.set_lon( a.longitude );
197             current_options.set_lat( a.latitude );
198             current_properties.setDoubleValue("/position/longitude",
199                                               a.longitude);
200             current_properties.setDoubleValue("/position/latitude",
201                                               a.latitude);
202         }
203     } else {
204         return false;
205     }
206
207     FG_LOG( FG_GENERAL, FG_INFO,
208             "Position for " << id << " is ("
209             << a.longitude << ", "
210             << a.latitude << ")" );
211
212     return true;
213 }
214
215
216 // Set current_options lon/lat given an airport id and heading (degrees)
217 bool fgSetPosFromAirportIDandHdg( const string& id, double tgt_hdg ) {
218     FGRunway r;
219     FGRunway found_r;
220     double found_dir;
221
222     if ( id.length() ) {
223         // set initial position from runway and heading
224
225         FGPath path( current_options.get_fg_root() );
226         path.append( "Airports" );
227         path.append( "runways.mk4" );
228         FGRunways runways( path.c_str() );
229
230         FG_LOG( FG_GENERAL, FG_INFO,
231                 "Attempting to set starting position from runway code "
232                 << id << " heading " << tgt_hdg );
233
234         // FGPath inpath( current_options.get_fg_root() );
235         // inpath.append( "Airports" );
236         // inpath.append( "apt_simple" );
237         // airports.load( inpath.c_str() );
238
239         // FGPath outpath( current_options.get_fg_root() );
240         // outpath.append( "Airports" );
241         // outpath.append( "simple.gdbm" );
242         // airports.dump_gdbm( outpath.c_str() );
243
244         if ( ! runways.search( id, &r ) ) {
245             FG_LOG( FG_GENERAL, FG_ALERT,
246                     "Failed to find " << id << " in database." );
247             return false;
248         }
249
250         double diff;
251         double min_diff = 360.0;
252
253         while ( r.id == id ) {
254             // forward direction
255             diff = tgt_hdg - r.heading;
256             while ( diff < -180.0 ) { diff += 360.0; }
257             while ( diff >  180.0 ) { diff -= 360.0; }
258             diff = fabs(diff);
259             FG_LOG( FG_GENERAL, FG_INFO,
260                     "Runway " << r.rwy_no << " heading = " << r.heading <<
261                     " diff = " << diff );
262             if ( diff < min_diff ) {
263                 min_diff = diff;
264                 found_r = r;
265                 found_dir = 0;
266             }
267
268             // reverse direction
269             diff = tgt_hdg - r.heading - 180.0;
270             while ( diff < -180.0 ) { diff += 360.0; }
271             while ( diff >  180.0 ) { diff -= 360.0; }
272             diff = fabs(diff);
273             FG_LOG( FG_GENERAL, FG_INFO,
274                     "Runway -" << r.rwy_no << " heading = " <<
275                     r.heading + 180.0 <<
276                     " diff = " << diff );
277             if ( diff < min_diff ) {
278                 min_diff = diff;
279                 found_r = r;
280                 found_dir = 180.0;
281             }
282
283             runways.next( &r );
284         }
285
286         FG_LOG( FG_GENERAL, FG_INFO, "closest runway = " << found_r.rwy_no
287                 << " + " << found_dir );
288
289     } else {
290         return false;
291     }
292
293     double heading = found_r.heading + found_dir;
294     while ( heading >= 360.0 ) { heading -= 360.0; }
295
296     double lat2, lon2, az2;
297     double azimuth = found_r.heading + found_dir + 180.0;
298     while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
299
300     FG_LOG( FG_GENERAL, FG_INFO,
301             "runway =  " << found_r.lon << ", " << found_r.lat
302             << " length = " << found_r.length * FEET_TO_METER * 0.5 
303             << " heading = " << azimuth );
304     geo_direct_wgs_84 ( 0, found_r.lat, found_r.lon, 
305                         azimuth, found_r.length * FEET_TO_METER * 0.5 - 5.0,
306                         &lat2, &lon2, &az2 );
307     current_options.set_lon( lon2 );
308     current_options.set_lat( lat2 );
309     current_options.set_heading( heading );
310     current_properties.setDoubleValue("/position/longitude", lon2);
311     current_properties.setDoubleValue("/position/latitude", lat2);
312     current_properties.setDoubleValue("/orientation/heading", heading);
313
314     FG_LOG( FG_GENERAL, FG_INFO,
315             "Position for " << id << " is ("
316             << lon2 << ", "
317             << lat2 << ") new heading is "
318             << heading );
319
320     return true;
321 }
322
323
324 // Set initial position and orientation
325 bool fgInitPosition( void ) {
326     FGInterface *f = current_aircraft.fdm_state;
327     string id = current_options.get_airport_id();
328
329     // set initial position from default or command line coordinates
330     f->set_Longitude( current_options.get_lon() * DEG_TO_RAD );
331     f->set_Latitude( current_options.get_lat() * DEG_TO_RAD );
332
333     if ( scenery.cur_elev > current_options.get_altitude() - 1) {
334         current_options.set_altitude( scenery.cur_elev + 1 );
335     }
336
337     FG_LOG( FG_GENERAL, FG_INFO,
338             "starting altitude is = " << current_options.get_altitude() );
339
340     f->set_Altitude( current_options.get_altitude() * METER_TO_FEET );
341     fgFDMSetGroundElevation( current_options.get_flight_model(),
342                              f->get_Altitude() * FEET_TO_METER );
343
344     FG_LOG( FG_GENERAL, FG_INFO,
345             "Initial position is: ("
346             << (f->get_Longitude() * RAD_TO_DEG) << ", "
347             << (f->get_Latitude() * RAD_TO_DEG) << ", "
348             << (f->get_Altitude() * FEET_TO_METER) << ")" );
349
350     return true;
351 }
352
353
354 // General house keeping initializations
355 bool fgInitGeneral( void ) {
356     string root;
357
358 #if defined(FX) && defined(XMESA)
359     char *mesa_win_state;
360 #endif
361
362     FG_LOG( FG_GENERAL, FG_INFO, "General Initialization" );
363     FG_LOG( FG_GENERAL, FG_INFO, "======= ==============" );
364
365     root = current_options.get_fg_root();
366     if ( ! root.length() ) {
367         // No root path set? Then bail ...
368         FG_LOG( FG_GENERAL, FG_ALERT,
369                 "Cannot continue without environment variable FG_ROOT"
370                 << "being defined." );
371         exit(-1);
372     }
373     FG_LOG( FG_GENERAL, FG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
374
375 #if defined(FX) && defined(XMESA)
376     // initialize full screen flag
377     global_fullscreen = false;
378     if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
379         // Test for the MESA_GLX_FX env variable
380         if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
381             // test if we are fullscreen mesa/glide
382             if ( (mesa_win_state[0] == 'f') ||
383                  (mesa_win_state[0] == 'F') ) {
384                 global_fullscreen = true;
385             }
386         }
387     }
388 #endif
389
390     return true;
391 }
392
393
394 // This is the top level init routine which calls all the other
395 // initialization routines.  If you are adding a subsystem to flight
396 // gear, its initialization call should located in this routine.
397 // Returns non-zero if a problem encountered.
398 bool fgInitSubsystems( void ) {
399     fgLIGHT *l = &cur_light_params;
400
401     FG_LOG( FG_GENERAL, FG_INFO, "Initialize Subsystems");
402     FG_LOG( FG_GENERAL, FG_INFO, "========== ==========");
403
404     // Initialize the material property lib
405     FGPath mpath( current_options.get_fg_root() );
406     mpath.append( "materials" );
407     if ( material_lib.load( mpath.str() ) ) {
408     } else {
409         FG_LOG( FG_GENERAL, FG_ALERT, "Error loading material lib!" );
410         exit(-1);
411     }
412
413     // Initialize the Scenery Management subsystem
414     if ( fgSceneryInit() ) {
415         // Material lib initialized ok.
416     } else {
417         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Scenery initialization!" );
418         exit(-1);
419     }
420
421     if ( global_tile_mgr.init() ) {
422         // Load the local scenery data
423         global_tile_mgr.update( current_options.get_lon(),
424                                 current_options.get_lat() );
425     } else {
426         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
427         exit(-1);
428     }
429
430     FG_LOG( FG_GENERAL, FG_DEBUG,
431             "Current terrain elevation after tile mgr init " <<
432             scenery.cur_elev );
433
434     if ( current_options.get_flight_model() == FGInterface::FG_LARCSIM ) {
435         cur_fdm_state = new FGLaRCsim;
436     } else if ( current_options.get_flight_model() == FGInterface::FG_JSBSIM ) {
437         cur_fdm_state = new FGJSBsim;
438     } else if ( current_options.get_flight_model() == 
439                 FGInterface::FG_BALLOONSIM ) {
440         cur_fdm_state = new FGBalloonSim;
441     } else if ( current_options.get_flight_model() == 
442                 FGInterface::FG_MAGICCARPET ) {
443         cur_fdm_state = new FGMagicCarpet;
444     } else if ( current_options.get_flight_model() == 
445                 FGInterface::FG_EXTERNAL ) {
446         cur_fdm_state = new FGExternal;
447     } else {
448         FG_LOG( FG_GENERAL, FG_ALERT,
449                 "No flight model, can't init aircraft" );
450         exit(-1);
451     }
452
453     // allocates structures so must happen before any of the flight
454     // model or control parameters are set
455     fgAircraftInit();   // In the future this might not be the case.
456
457     // set the initial position
458     fgInitPosition();
459
460     // Calculate ground elevation at starting point (we didn't have
461     // tmp_abs_view_pos calculated when fgTileMgrUpdate() was called above
462     //
463     // calculalate a cartesian point somewhere along the line between
464     // the center of the earth and our view position.  Doesn't have to
465     // be the exact elevation (this is good because we don't know it
466     // yet :-)
467
468     // now handled inside of the fgTileMgrUpdate()
469
470     /*
471     geod_pos = Point3D( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(), 0.0);
472     tmp_abs_view_pos = sgGeodToCart(geod_pos);
473
474     FG_LOG( FG_GENERAL, FG_DEBUG,
475             "Initial abs_view_pos = " << tmp_abs_view_pos );
476     scenery.cur_elev =
477         fgTileMgrCurElev( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(),
478                           tmp_abs_view_pos );
479     FG_LOG( FG_GENERAL, FG_DEBUG,
480             "Altitude after update " << scenery.cur_elev );
481     */
482
483     fgFDMSetGroundElevation( current_options.get_flight_model(),
484                              scenery.cur_elev );
485
486     // Reset our altitude if we are below ground
487     FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
488     FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " <<
489             cur_fdm_state->get_Runway_altitude() );
490
491     if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
492         cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
493     }
494
495     FG_LOG( FG_GENERAL, FG_INFO,
496             "Updated position (after elevation adj): ("
497             << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
498             << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
499             << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
500
501     // We need to calculate a few more values here that would normally
502     // be calculated by the FDM so that the current_view.UpdateViewMath()
503     // routine doesn't get hosed.
504
505     double sea_level_radius_meters;
506     double lat_geoc;
507     // Set the FG variables first
508     sgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(),
509                   &sea_level_radius_meters, &lat_geoc);
510     cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(),
511                                 cur_fdm_state->get_Altitude() +
512                                 (sea_level_radius_meters * METER_TO_FEET) );
513     cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
514
515     cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
516     cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
517         
518     cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
519     cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
520
521     // The following section sets up the flight model EOM parameters
522     // and should really be read in from one or more files.
523
524     // Initial Velocity
525     cur_fdm_state->set_Velocities_Local( current_options.get_uBody(),
526                              current_options.get_vBody(),
527                              current_options.get_wBody());
528
529     // Initial Orientation
530     cur_fdm_state->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
531                          current_options.get_pitch() * DEG_TO_RAD,
532                          current_options.get_heading() * DEG_TO_RAD );
533
534     // Initial Angular Body rates
535     cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
536
537     cur_fdm_state->set_Earth_position_angle( 0.0 );
538
539     // Mass properties and geometry values
540     cur_fdm_state->set_Inertias( 8.547270E+01,
541                      1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
542
543     // CG position w.r.t. ref. point
544     cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
545
546     // Initialize the event manager
547     global_events.Init();
548
549     // Output event stats every 60 seconds
550     global_events.Register( "fgEVENT_MGR::PrintStats()",
551                             fgMethodCallback<fgEVENT_MGR>( &global_events,
552                                                    &fgEVENT_MGR::PrintStats),
553                             fgEVENT::FG_EVENT_READY, 60000 );
554
555     // Initialize view parameters
556     FG_LOG( FG_GENERAL, FG_DEBUG, "Before current_view.init()");
557     current_view.Init();
558     pilot_view.Init();
559     FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
560     current_view.UpdateViewMath(*cur_fdm_state);
561     pilot_view.UpdateViewMath(*cur_fdm_state);
562     FG_LOG( FG_GENERAL, FG_DEBUG, "  abs_view_pos = " << current_view.get_abs_view_pos());
563     // current_view.UpdateWorldToEye(f);
564
565     // Initialize the planetary subsystem
566     // global_events.Register( "fgPlanetsInit()", fgPlanetsInit,
567     //                      fgEVENT::FG_EVENT_READY, 600000);
568
569     // Initialize the sun's position
570     // global_events.Register( "fgSunInit()", fgSunInit,
571     //                      fgEVENT::FG_EVENT_READY, 30000 );
572
573     // Intialize the moon's position
574     // global_events.Register( "fgMoonInit()", fgMoonInit,
575     //                      fgEVENT::FG_EVENT_READY, 600000 );
576
577     // fgUpdateSunPos() needs a few position and view parameters set
578     // so it can calculate local relative sun angle and a few other
579     // things for correctly orienting the sky.
580     fgUpdateSunPos();
581     fgUpdateMoonPos();
582     global_events.Register( "fgUpdateSunPos()", fgUpdateSunPos,
583                             fgEVENT::FG_EVENT_READY, 60000);
584     global_events.Register( "fgUpdateMoonPos()", fgUpdateMoonPos,
585                             fgEVENT::FG_EVENT_READY, 60000);
586
587     // Initialize Lighting interpolation tables
588     l->Init();
589
590     // update the lighting parameters (based on sun angle)
591     global_events.Register( "fgLight::Update()",
592                             fgMethodCallback<fgLIGHT>( &cur_light_params,
593                                                        &fgLIGHT::Update),
594                             fgEVENT::FG_EVENT_READY, 30000 );
595     // update the current timezone each 30 minutes
596     global_events.Register( "fgUpdateLocalTime()", fgUpdateLocalTime,
597                             fgEVENT::FG_EVENT_READY, 1800000);
598
599     // Initialize the weather modeling subsystem
600 #ifndef FG_OLD_WEATHER
601     // Initialize the WeatherDatabase
602     FG_LOG(FG_GENERAL, FG_INFO, "Creating LocalWeatherDatabase");
603     sgVec3 position;
604     sgSetVec3( position, current_aircraft.fdm_state->get_Latitude(),
605                current_aircraft.fdm_state->get_Longitude(),
606                current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER );
607     FGLocalWeatherDatabase::theFGLocalWeatherDatabase = 
608         new FGLocalWeatherDatabase( position, current_options.get_fg_root() );
609     // cout << theFGLocalWeatherDatabase << endl;
610     // cout << "visibility = " 
611     //      << theFGLocalWeatherDatabase->getWeatherVisibility() << endl;
612
613     WeatherDatabase = FGLocalWeatherDatabase::theFGLocalWeatherDatabase;
614      
615     // register the periodic update of the weather
616     global_events.Register( "weather update", fgUpdateWeatherDatabase,
617                             fgEVENT::FG_EVENT_READY, 30000);
618 #else
619     current_weather.Init();
620 #endif
621
622     // Initialize vor/ndb/ils/fix list management and query systems
623     FG_LOG(FG_GENERAL, FG_INFO, "Loading Navaids");
624
625     FG_LOG(FG_GENERAL, FG_INFO, "  VOR/NDB");
626     current_navlist = new FGNavList;
627     FGPath p_nav( current_options.get_fg_root() );
628     p_nav.append( "Navaids/default.nav" );
629     current_navlist->init( p_nav );
630
631     FG_LOG(FG_GENERAL, FG_INFO, "  ILS");
632     current_ilslist = new FGILSList;
633     FGPath p_ils( current_options.get_fg_root() );
634     p_ils.append( "Navaids/default.ils" );
635     current_ilslist->init( p_ils );
636
637     FG_LOG(FG_GENERAL, FG_INFO, "  Fixes");
638     current_fixlist = new FGFixList;
639     FGPath p_fix( current_options.get_fg_root() );
640     p_fix.append( "Navaids/default.fix" );
641     current_fixlist->init( p_fix );
642
643     // Initialize the underlying radio stack model
644     current_radiostack = new FGRadioStack;
645
646 //     current_radiostack->set_nav1_freq( 117.30 );
647 //     current_radiostack->set_nav1_alt_freq( 110.30 );
648 //     current_radiostack->set_nav1_sel_radial( 119.0 );
649
650 //     current_radiostack->set_nav2_freq( 111.80 );
651 //     current_radiostack->set_nav2_alt_freq( 115.70 );
652 //     current_radiostack->set_nav2_sel_radial( 029.0 );
653
654 //     current_radiostack->set_adf_freq( 266.0 );
655
656 #if 0
657     // This block of settings are Alex's defaults for San Diego
658     current_radiostack->set_nav1_freq( 111.70 );
659     current_radiostack->set_nav1_alt_freq( 115.30 );
660     current_radiostack->set_nav1_sel_radial( 280.0 );
661     current_radiostack->set_nav2_freq( 117.80 );
662     current_radiostack->set_nav2_alt_freq( 114.00 );
663     current_radiostack->set_nav2_sel_radial( 68.0 );
664     current_radiostack->set_adf_freq( 210.0 );
665     // End of Alex's custom settings
666 #endif
667
668     current_radiostack->search( cur_fdm_state->get_Longitude(),
669                                 cur_fdm_state->get_Latitude(),
670                                 cur_fdm_state->get_Altitude() * FEET_TO_METER );
671
672     current_radiostack->update( cur_fdm_state->get_Longitude(),
673                                 cur_fdm_state->get_Latitude(),
674                                 cur_fdm_state->get_Altitude() * FEET_TO_METER );
675
676     // Search radio database once per second
677     global_events.Register( "fgRadioSearch()", fgRadioSearch,
678                             fgEVENT::FG_EVENT_READY, 1000);
679
680
681     // Initialize the Cockpit subsystem
682     if( fgCockpitInit( &current_aircraft )) {
683         // Cockpit initialized ok.
684     } else {
685         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Cockpit initialization!" );
686         exit(-1);
687     }
688
689     // Initialize the flight model subsystem data structures base on
690     // above values
691
692     // fgFDMInit( current_options.get_flight_model(), cur_fdm_state,
693     //            1.0 / current_options.get_model_hz() );
694     cur_fdm_state->init( 1.0 / current_options.get_model_hz() );
695
696     // I'm just sticking this here for now, it should probably move
697     // eventually
698     scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
699
700     if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
701         cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
702     }
703
704     FG_LOG( FG_GENERAL, FG_INFO,
705             "Updated position (after elevation adj): ("
706             << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
707             << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
708             << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
709     // end of thing that I just stuck in that I should probably move
710
711     // Joystick support
712     if ( fgJoystickInit() ) {
713         // Joystick initialized ok.
714     } else {
715         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Joystick initialization!" );
716     }
717
718     // Autopilot init
719     current_autopilot = new FGAutopilot;
720     current_autopilot->init();
721
722     // initialize the gui parts of the autopilot
723     NewTgtAirportInit();
724     fgAPAdjustInit() ;
725     NewHeadingInit();
726     NewAltitudeInit();
727
728     // Initialize I/O channels
729 #if ! defined( macintosh )
730     fgIOInit();
731 #endif
732
733     // Initialize the 2D panel.
734     string panel_path =
735         current_properties.getStringValue("/sim/panel/path",
736                                           "Panels/Default/default.xml");
737     current_panel = fgReadPanel(panel_path);
738     if (current_panel == 0) {
739         FG_LOG(FG_INPUT, FG_ALERT,
740                "Error reading new panel from " << panel_path);
741     }
742     FG_LOG(FG_INPUT, FG_INFO, "Loaded new panel from " << panel_path);
743
744     // Initialize the BFI
745     FGBFI::init();
746
747     FG_LOG( FG_GENERAL, FG_INFO, endl);
748
749     return true;
750 }
751
752
753 void fgReInitSubsystems( void )
754 {
755     bool freeze = globals->get_freeze();
756     if( !freeze )
757         globals->set_freeze( true );
758     
759     if( global_tile_mgr.init() ) {
760         // Load the local scenery data
761         global_tile_mgr.update( current_options.get_lon(),
762                                 current_options.get_lat() );
763     } else {
764         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
765                 exit(-1);
766     }
767
768     // cout << "current scenery elev = " << scenery.cur_elev << endl;
769
770     fgInitPosition();
771     fgFDMSetGroundElevation( current_options.get_flight_model(), 
772                              scenery.cur_elev );
773
774     // Reset our altitude if we are below ground
775     FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
776     FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " << 
777             cur_fdm_state->get_Runway_altitude() );
778
779     if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
780         cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
781     }
782     double sea_level_radius_meters;
783     double lat_geoc;
784     // Set the FG variables first
785     sgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(), 
786                   &sea_level_radius_meters, &lat_geoc);
787     cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(), 
788                                 cur_fdm_state->get_Altitude() + 
789                                 (sea_level_radius_meters * METER_TO_FEET) );
790     cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
791         
792     cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
793     cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
794         
795     cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
796     cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
797
798     // The following section sets up the flight model EOM parameters
799     // and should really be read in from one or more files.
800
801     // Initial Velocity
802     cur_fdm_state->set_Velocities_Local( current_options.get_uBody(),
803                              current_options.get_vBody(),
804                              current_options.get_wBody());
805
806     // Initial Orientation
807     cur_fdm_state->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
808                          current_options.get_pitch() * DEG_TO_RAD,
809                          current_options.get_heading() * DEG_TO_RAD );
810
811     // Initial Angular Body rates
812     cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
813
814     cur_fdm_state->set_Earth_position_angle( 0.0 );
815
816     // Mass properties and geometry values
817     cur_fdm_state->set_Inertias( 8.547270E+01, 
818                      1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
819
820     // CG position w.r.t. ref. point
821     cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
822
823     // Initialize view parameters
824     current_view.set_view_offset( 0.0 );
825     current_view.set_goal_view_offset( 0.0 );
826     pilot_view.set_view_offset( 0.0 );
827     pilot_view.set_goal_view_offset( 0.0 );
828
829     FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
830     current_view.UpdateViewMath(*cur_fdm_state);
831     pilot_view.UpdateViewMath(*cur_fdm_state);
832     FG_LOG( FG_GENERAL, FG_DEBUG, "  abs_view_pos = " << current_view.get_abs_view_pos());
833
834     // fgFDMInit( current_options.get_flight_model(), cur_fdm_state, 
835     //            1.0 / current_options.get_model_hz() );
836     cur_fdm_state->init( 1.0 / current_options.get_model_hz() );
837
838     scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
839
840     if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
841         cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
842     }
843
844     controls.reset_all();
845     current_autopilot->reset();
846
847     if( !freeze )
848         globals->set_freeze( false );
849 }