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