]> git.mxchange.org Git - flightgear.git/blobdiff - src/Main/fg_init.cxx
Added code to put aircraft at the end of the runway closest to the desired
[flightgear.git] / src / Main / fg_init.cxx
index 646e18b2deb2518bb4e1802b8f7458e457a82464..bd0818d9a81c0f0a32998b5e463e46be9a2804a0 100644 (file)
@@ -1,4 +1,3 @@
-//
 // fg_init.cxx -- Flight Gear top level initialization routines
 //
 // Written by Curtis Olson, started August 1997.
@@ -33,7 +32,7 @@
 #endif
 
 #include <GL/glut.h>
-#include <XGL/xgl.h>
+#include <simgear/xgl/xgl.h>
 
 #include <stdio.h>
 #include <stdlib.h>
 #  define _G_NO_EXTERN_TEMPLATES
 #endif
 
-#include <Include/compiler.h>
+#include <simgear/compiler.h>
 
 #include STL_STRING
 
-#include <Debug/logstream.hxx>
+#include <simgear/constants.h>
+#include <simgear/debug/logstream.hxx>
+#include <simgear/math/fg_geodesy.hxx>
+#include <simgear/math/point3d.hxx>
+#include <simgear/math/polar3d.hxx>
+#include <simgear/misc/fgpath.hxx>
+#include <simgear/timing/sg_time.hxx>
+
 #include <Aircraft/aircraft.hxx>
+#include <Airports/runways.hxx>
 #include <Airports/simple.hxx>
-#include <Astro/sky.hxx>
-#include <Astro/stars.hxx>
-#include <Astro/solarsystem.hxx>
-#include <Autopilot/autopilot.hxx>
+#include <Autopilot/auto_gui.hxx>
+#include <Autopilot/newauto.hxx>
 #include <Cockpit/cockpit.hxx>
-#include <Include/fg_constants.h>
+#include <Cockpit/radiostack.hxx>
+#include <Cockpit/panel.hxx>
+#include <Cockpit/sp_panel.hxx>
+#include <FDM/Balloon.h>
+#include <FDM/External.hxx>
+#include <FDM/JSBSim.hxx>
+#include <FDM/LaRCsim.hxx>
+#include <FDM/MagicCarpet.hxx>
 #include <Include/general.hxx>
 #include <Joystick/joystick.hxx>
-#include <Math/fg_geodesy.hxx>
-#include <Math/point3d.hxx>
-#include <Math/polar3d.hxx>
-#include <Misc/fgpath.hxx>
+#include <Objects/matlib.hxx>
+#include <Navaids/fixlist.hxx>
+#include <Navaids/ilslist.hxx>
+#include <Navaids/navlist.hxx>
 #include <Scenery/scenery.hxx>
 #include <Scenery/tilemgr.hxx>
 #include <Time/event.hxx>
-#include <Time/fg_time.hxx>
 #include <Time/light.hxx>
 #include <Time/sunpos.hxx>
 #include <Time/moonpos.hxx>
-// #include <Weather/weather.hxx>
-#include <WeatherCM/FGLocalWeatherDatabase.h>
+#include <Time/tmp.hxx>
+
+#ifndef FG_OLD_WEATHER
+#  include <WeatherCM/FGLocalWeatherDatabase.h>
+#else
+#  include <Weather/weather.hxx>
+#endif
 
 #include "fg_init.hxx"
+#include "fg_io.hxx"
+#include "globals.hxx"
 #include "options.hxx"
 #include "views.hxx"
-#include "fg_serial.hxx"
+#include "bfi.hxx"
 
 #if defined(FX) && defined(XMESA)
 #include <GL/xmesa.h>
@@ -115,42 +133,172 @@ bool fgInitConfig ( int argc, char **argv ) {
        return false;
     }
 
-    return true;
+   return true;
 }
 
 
-// Set initial position and orientation
-bool fgInitPosition( void ) {
-    string id;
-    FGInterface *f;
-
-    f = current_aircraft.fdm_state;
+// Set current_options lon/lat given an airport id
+bool fgSetPosFromAirportID( const string& id ) {
+    FGAirport a;
+    double lon, lat;
 
-    id = current_options.get_airport_id();
     if ( id.length() ) {
        // set initial position from airport id
 
-       fgAIRPORTS airports;
-       fgAIRPORT a;
+       FGPath path( current_options.get_fg_root() );
+       path.append( "Airports" );
+       path.append( "simple.mk4" );
+       FGAirports airports( path.c_str() );
 
        FG_LOG( FG_GENERAL, FG_INFO,
                "Attempting to set starting position from airport code "
                << id );
 
-       airports.load("apt_simple");
+       // FGPath inpath( current_options.get_fg_root() );
+       // inpath.append( "Airports" );
+       // inpath.append( "apt_simple" );
+       // airports.load( inpath.c_str() );
+
+       // FGPath outpath( current_options.get_fg_root() );
+       // outpath.append( "Airports" );
+       // outpath.append( "simple.gdbm" );
+       // airports.dump_gdbm( outpath.c_str() );
+
        if ( ! airports.search( id, &a ) ) {
            FG_LOG( FG_GENERAL, FG_ALERT,
                    "Failed to find " << id << " in database." );
-           exit(-1);
+           return false;
        } else {
-           f->set_Longitude( a.longitude * DEG_TO_RAD );
-           f->set_Latitude( a.latitude * DEG_TO_RAD );
+           current_options.set_lon( a.longitude );
+           current_options.set_lat( a.latitude );
        }
     } else {
-       // set initial position from default or command line coordinates
+       return false;
+    }
 
-       f->set_Longitude( current_options.get_lon() * DEG_TO_RAD );
-       f->set_Latitude( current_options.get_lat() * DEG_TO_RAD );
+    FG_LOG( FG_GENERAL, FG_INFO,
+           "Position for " << id << " is ("
+           << a.longitude << ", "
+           << a.latitude << ")" );
+
+    return true;
+}
+
+
+// Set current_options lon/lat given an airport id and heading (degrees)
+bool fgSetPosFromAirportIDandHdg( const string& id, double tgt_hdg ) {
+    FGRunway r;
+    FGRunway found_r;
+    double found_dir;
+
+    if ( id.length() ) {
+       // set initial position from runway and heading
+
+       FGPath path( current_options.get_fg_root() );
+       path.append( "Airports" );
+       path.append( "runways.mk4" );
+       FGRunways runways( path.c_str() );
+
+       FG_LOG( FG_GENERAL, FG_INFO,
+               "Attempting to set starting position from runway code "
+               << id << " heading " << tgt_hdg );
+
+       // FGPath inpath( current_options.get_fg_root() );
+       // inpath.append( "Airports" );
+       // inpath.append( "apt_simple" );
+       // airports.load( inpath.c_str() );
+
+       // FGPath outpath( current_options.get_fg_root() );
+       // outpath.append( "Airports" );
+       // outpath.append( "simple.gdbm" );
+       // airports.dump_gdbm( outpath.c_str() );
+
+       if ( ! runways.search( id, &r ) ) {
+           FG_LOG( FG_GENERAL, FG_ALERT,
+                   "Failed to find " << id << " in database." );
+           return false;
+       }
+
+       double diff;
+       double min_diff = 360.0;
+
+       while ( r.id == id ) {
+           // forward direction
+           diff = tgt_hdg - r.heading;
+           while ( diff < -180.0 ) { diff += 360.0; }
+           diff = fabs(diff);
+           FG_LOG( FG_GENERAL, FG_INFO,
+                   "Runway " << r.rwy_no << " heading = " << r.heading <<
+                   " diff = " << diff );
+           if ( diff < min_diff ) {
+               min_diff = diff;
+               found_r = r;
+               found_dir = 0;
+           }
+
+           // reverse direction
+           diff = tgt_hdg - r.heading - 180.0;
+           while ( diff < -180.0 ) { diff += 360.0; }
+           diff = fabs(diff);
+           FG_LOG( FG_GENERAL, FG_INFO,
+                   "Runway -" << r.rwy_no << " heading = " <<
+                   r.heading + 180.0 <<
+                   " diff = " << diff );
+           if ( diff < min_diff ) {
+               min_diff = diff;
+               found_r = r;
+               found_dir = 180.0;
+           }
+
+           runways.next( &r );
+       }
+
+       FG_LOG( FG_GENERAL, FG_INFO, "closest runway = " << found_r.rwy_no
+               << " + " << found_dir );
+
+    } else {
+       return false;
+    }
+
+    double heading = found_r.heading + found_dir;
+    while ( heading >= 360.0 ) { heading -= 360.0; }
+
+    double lat2, lon2, az2;
+    double azimuth = found_r.heading + found_dir + 180.0;
+    while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
+
+    FG_LOG( FG_GENERAL, FG_INFO,
+           "runway =  " << found_r.lon << ", " << found_r.lat
+           << " length = " << found_r.length * FEET_TO_METER * 0.5 
+           << " heading = " << azimuth );
+    geo_direct_wgs_84 ( 0, found_r.lat, found_r.lon, 
+                       azimuth, found_r.length * FEET_TO_METER * 0.5 - 5.0,
+                       &lat2, &lon2, &az2 );
+    current_options.set_lon( lon2 );
+    current_options.set_lat( lat2 );
+    current_options.set_heading( heading );
+
+    FG_LOG( FG_GENERAL, FG_INFO,
+           "Position for " << id << " is ("
+           << lon2 << ", "
+           << lat2 << ") new heading is "
+           << heading );
+
+    return true;
+}
+
+
+// Set initial position and orientation
+bool fgInitPosition( void ) {
+    FGInterface *f = current_aircraft.fdm_state;
+    string id = current_options.get_airport_id();
+
+    // set initial position from default or command line coordinates
+    f->set_Longitude( current_options.get_lon() * DEG_TO_RAD );
+    f->set_Latitude( current_options.get_lat() * DEG_TO_RAD );
+
+    if ( scenery.cur_elev > current_options.get_altitude() - 1) {
+       current_options.set_altitude( scenery.cur_elev + 1 );
     }
 
     FG_LOG( FG_GENERAL, FG_INFO,
@@ -158,7 +306,7 @@ bool fgInitPosition( void ) {
 
     f->set_Altitude( current_options.get_altitude() * METER_TO_FEET );
     fgFDMSetGroundElevation( current_options.get_flight_model(),
-                            (f->get_Altitude() - 3.758099) * FEET_TO_METER );
+                            f->get_Altitude() * FEET_TO_METER );
 
     FG_LOG( FG_GENERAL, FG_INFO,
            "Initial position is: ("
@@ -173,7 +321,10 @@ bool fgInitPosition( void ) {
 // General house keeping initializations
 bool fgInitGeneral( void ) {
     string root;
+
+#if defined(FX) && defined(XMESA)
     char *mesa_win_state;
+#endif
 
     FG_LOG( FG_GENERAL, FG_INFO, "General Initialization" );
     FG_LOG( FG_GENERAL, FG_INFO, "======= ==============" );
@@ -186,7 +337,7 @@ bool fgInitGeneral( void ) {
                << "being defined." );
        exit(-1);
     }
-    FG_LOG( FG_GENERAL, FG_INFO, "FG_ROOT = " << root << endl );
+    FG_LOG( FG_GENERAL, FG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
 
 #if defined(FX) && defined(XMESA)
     // initialize full screen flag
@@ -212,35 +363,32 @@ bool fgInitGeneral( void ) {
 // gear, its initialization call should located in this routine.
 // Returns non-zero if a problem encountered.
 bool fgInitSubsystems( void ) {
-    FGTime::cur_time_params = new FGTime();
-
-    FGInterface *f; // assigned later
     fgLIGHT *l = &cur_light_params;
-    FGTime *t = FGTime::cur_time_params;
-    FGView *v = &current_view;
 
     FG_LOG( FG_GENERAL, FG_INFO, "Initialize Subsystems");
     FG_LOG( FG_GENERAL, FG_INFO, "========== ==========");
 
-    // allocates structures so must happen before any of the flight
-    // model or control parameters are set
-    fgAircraftInit();   // In the future this might not be the case.
-    f = current_aircraft.fdm_state;
-
-    // set the initial position
-    fgInitPosition();
+    // Initialize the material property lib
+    FGPath mpath( current_options.get_fg_root() );
+    mpath.append( "materials" );
+    if ( material_lib.load( mpath.str() ) ) {
+    } else {
+       FG_LOG( FG_GENERAL, FG_ALERT, "Error loading material lib!" );
+       exit(-1);
+    }
 
     // Initialize the Scenery Management subsystem
     if ( fgSceneryInit() ) {
-       // Scenery initialized ok.
+       // Material lib initialized ok.
     } else {
        FG_LOG( FG_GENERAL, FG_ALERT, "Error in Scenery initialization!" );
        exit(-1);
     }
 
-    if( global_tile_mgr.init() ) {
+    if ( global_tile_mgr.init() ) {
        // Load the local scenery data
-       global_tile_mgr.update();
+       global_tile_mgr.update( current_options.get_lon(),
+                               current_options.get_lat() );
     } else {
        FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
        exit(-1);
@@ -250,6 +398,32 @@ bool fgInitSubsystems( void ) {
            "Current terrain elevation after tile mgr init " <<
            scenery.cur_elev );
 
+    if ( current_options.get_flight_model() == FGInterface::FG_LARCSIM ) {
+       cur_fdm_state = new FGLaRCsim;
+    } else if ( current_options.get_flight_model() == FGInterface::FG_JSBSIM ) {
+       cur_fdm_state = new FGJSBsim;
+    } else if ( current_options.get_flight_model() == 
+               FGInterface::FG_BALLOONSIM ) {
+       cur_fdm_state = new FGBalloonSim;
+    } else if ( current_options.get_flight_model() == 
+               FGInterface::FG_MAGICCARPET ) {
+       cur_fdm_state = new FGMagicCarpet;
+    } else if ( current_options.get_flight_model() == 
+               FGInterface::FG_EXTERNAL ) {
+       cur_fdm_state = new FGExternal;
+    } else {
+       FG_LOG( FG_GENERAL, FG_ALERT,
+               "No flight model, can't init aircraft" );
+       exit(-1);
+    }
+
+    // allocates structures so must happen before any of the flight
+    // model or control parameters are set
+    fgAircraftInit();   // In the future this might not be the case.
+
+    // set the initial position
+    fgInitPosition();
+
     // Calculate ground elevation at starting point (we didn't have
     // tmp_abs_view_pos calculated when fgTileMgrUpdate() was called above
     //
@@ -261,13 +435,13 @@ bool fgInitSubsystems( void ) {
     // now handled inside of the fgTileMgrUpdate()
 
     /*
-    geod_pos = Point3D( f->get_Longitude(), f->get_Latitude(), 0.0);
+    geod_pos = Point3D( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(), 0.0);
     tmp_abs_view_pos = fgGeodToCart(geod_pos);
 
     FG_LOG( FG_GENERAL, FG_DEBUG,
            "Initial abs_view_pos = " << tmp_abs_view_pos );
     scenery.cur_elev =
-       fgTileMgrCurElev( f->get_Longitude(), f->get_Latitude(),
+       fgTileMgrCurElev( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(),
                          tmp_abs_view_pos );
     FG_LOG( FG_GENERAL, FG_DEBUG,
            "Altitude after update " << scenery.cur_elev );
@@ -277,64 +451,64 @@ bool fgInitSubsystems( void ) {
                             scenery.cur_elev );
 
     // Reset our altitude if we are below ground
-    FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << f->get_Altitude() );
+    FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
     FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " <<
-           f->get_Runway_altitude() );
+           cur_fdm_state->get_Runway_altitude() );
 
-    if ( f->get_Altitude() < f->get_Runway_altitude() + 3.758099) {
-       f->set_Altitude( f->get_Runway_altitude() + 3.758099 );
+    if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
+       cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
     }
 
     FG_LOG( FG_GENERAL, FG_INFO,
            "Updated position (after elevation adj): ("
-           << (f->get_Latitude() * RAD_TO_DEG) << ", "
-           << (f->get_Longitude() * RAD_TO_DEG) << ", "
-           << (f->get_Altitude() * FEET_TO_METER) << ")" );
+           << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
+           << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
+           << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
 
     // We need to calculate a few more values here that would normally
-    // be calculated by the FDM so that the v->UpdateViewMath()
+    // be calculated by the FDM so that the current_view.UpdateViewMath()
     // routine doesn't get hosed.
 
     double sea_level_radius_meters;
     double lat_geoc;
     // Set the FG variables first
-    fgGeodToGeoc( f->get_Latitude(), f->get_Altitude(),
+    fgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(),
                  &sea_level_radius_meters, &lat_geoc);
-    f->set_Geocentric_Position( lat_geoc, f->get_Longitude(),
-                               f->get_Altitude() +
+    cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(),
+                               cur_fdm_state->get_Altitude() +
                                (sea_level_radius_meters * METER_TO_FEET) );
-    f->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
+    cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
 
-    f->set_sin_cos_longitude(f->get_Longitude());
-    f->set_sin_cos_latitude(f->get_Latitude());
+    cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
+    cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
        
-    f->set_sin_lat_geocentric(sin(lat_geoc));
-    f->set_cos_lat_geocentric(cos(lat_geoc));
+    cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
+    cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
 
     // The following section sets up the flight model EOM parameters
     // and should really be read in from one or more files.
 
     // Initial Velocity
-    f->set_Velocities_Local( current_options.get_uBody(),
+    cur_fdm_state->set_Velocities_Local( current_options.get_uBody(),
                              current_options.get_vBody(),
                              current_options.get_wBody());
 
     // Initial Orientation
-    f->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
+    cur_fdm_state->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
                         current_options.get_pitch() * DEG_TO_RAD,
                         current_options.get_heading() * DEG_TO_RAD );
 
     // Initial Angular Body rates
-    f->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
+    cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
 
-    f->set_Earth_position_angle( 0.0 );
+    cur_fdm_state->set_Earth_position_angle( 0.0 );
 
     // Mass properties and geometry values
-    f->set_Inertias( 8.547270E+01,
+    cur_fdm_state->set_Inertias( 8.547270E+01,
                     1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
 
     // CG position w.r.t. ref. point
-    f->set_CG_Position( 0.0, 0.0, 0.0 );
+    cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
 
     // Initialize the event manager
     global_events.Init();
@@ -345,30 +519,15 @@ bool fgInitSubsystems( void ) {
                                                   &fgEVENT_MGR::PrintStats),
                            fgEVENT::FG_EVENT_READY, 60000 );
 
-    // Initialize the time dependent variables
-    t->init();
-    t->update(f);
-
     // Initialize view parameters
-    FG_LOG( FG_GENERAL, FG_DEBUG, "Before v->init()");
-    v->Init();
-    FG_LOG( FG_GENERAL, FG_DEBUG, "After v->init()");
-    v->UpdateViewMath(f);
-    FG_LOG( FG_GENERAL, FG_DEBUG, "  abs_view_pos = " << v->get_abs_view_pos());
-    // v->UpdateWorldToEye(f);
-
-    // Build the solar system
-    //fgSolarSystemInit(*t);
-    FG_LOG(FG_GENERAL, FG_INFO, "Building SolarSystem");
-    SolarSystem::theSolarSystem = new SolarSystem(t);
-
-    // Initialize the Stars subsystem
-    if( fgStarsInit() ) {
-       // Stars initialized ok.
-    } else {
-       FG_LOG( FG_GENERAL, FG_ALERT, "Error in Stars initialization!" );
-       exit(-1);
-    }
+    FG_LOG( FG_GENERAL, FG_DEBUG, "Before current_view.init()");
+    current_view.Init();
+    pilot_view.Init();
+    FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
+    current_view.UpdateViewMath(*cur_fdm_state);
+    pilot_view.UpdateViewMath(*cur_fdm_state);
+    FG_LOG( FG_GENERAL, FG_DEBUG, "  abs_view_pos = " << current_view.get_abs_view_pos());
+    // current_view.UpdateWorldToEye(f);
 
     // Initialize the planetary subsystem
     // global_events.Register( "fgPlanetsInit()", fgPlanetsInit,
@@ -382,10 +541,6 @@ bool fgInitSubsystems( void ) {
     // global_events.Register( "fgMoonInit()", fgMoonInit,
     //                     fgEVENT::FG_EVENT_READY, 600000 );
 
-    // register the periodic update of Sun, moon, and planets
-    global_events.Register( "ssolsysUpdate", solarSystemRebuild,
-                           fgEVENT::FG_EVENT_READY, 600000);
-
     // fgUpdateSunPos() needs a few position and view parameters set
     // so it can calculate local relative sun angle and a few other
     // things for correctly orienting the sky.
@@ -404,23 +559,91 @@ bool fgInitSubsystems( void ) {
                            fgMethodCallback<fgLIGHT>( &cur_light_params,
                                                       &fgLIGHT::Update),
                            fgEVENT::FG_EVENT_READY, 30000 );
+    // update the current timezone each 30 minutes
+    global_events.Register( "fgUpdateLocalTime()", fgUpdateLocalTime,
+                           fgEVENT::FG_EVENT_READY, 1800000);
 
     // Initialize the weather modeling subsystem
-    // current_weather.Init();
+#ifndef FG_OLD_WEATHER
     // Initialize the WeatherDatabase
     FG_LOG(FG_GENERAL, FG_INFO, "Creating LocalWeatherDatabase");
+    sgVec3 position;
+    sgSetVec3( position, current_aircraft.fdm_state->get_Latitude(),
+              current_aircraft.fdm_state->get_Longitude(),
+              current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER );
     FGLocalWeatherDatabase::theFGLocalWeatherDatabase = 
-       new FGLocalWeatherDatabase(
-             Point3D( current_aircraft.fdm_state->get_Latitude(),
-                      current_aircraft.fdm_state->get_Longitude(),
-                      current_aircraft.fdm_state->get_Altitude() 
-                         * FEET_TO_METER) );
+       new FGLocalWeatherDatabase( position, current_options.get_fg_root() );
+    // cout << theFGLocalWeatherDatabase << endl;
+    // cout << "visibility = " 
+    //      << theFGLocalWeatherDatabase->getWeatherVisibility() << endl;
 
     WeatherDatabase = FGLocalWeatherDatabase::theFGLocalWeatherDatabase;
-
+     
     // register the periodic update of the weather
     global_events.Register( "weather update", fgUpdateWeatherDatabase,
-                           fgEVENT::FG_EVENT_READY, 30000);
+                            fgEVENT::FG_EVENT_READY, 30000);
+#else
+    current_weather.Init();
+#endif
+
+    // Initialize vor/ndb/ils/fix list management and query systems
+    FG_LOG(FG_GENERAL, FG_INFO, "Loading Navaids");
+
+    FG_LOG(FG_GENERAL, FG_INFO, "  VOR/NDB");
+    current_navlist = new FGNavList;
+    FGPath p_nav( current_options.get_fg_root() );
+    p_nav.append( "Navaids/default.nav" );
+    current_navlist->init( p_nav );
+
+    FG_LOG(FG_GENERAL, FG_INFO, "  ILS");
+    current_ilslist = new FGILSList;
+    FGPath p_ils( current_options.get_fg_root() );
+    p_ils.append( "Navaids/default.ils" );
+    current_ilslist->init( p_ils );
+
+    FG_LOG(FG_GENERAL, FG_INFO, "  Fixes");
+    current_fixlist = new FGFixList;
+    FGPath p_fix( current_options.get_fg_root() );
+    p_fix.append( "Navaids/default.fix" );
+    current_fixlist->init( p_fix );
+
+    // Initialize the underlying radio stack model
+    current_radiostack = new FGRadioStack;
+
+    current_radiostack->set_nav1_freq( 117.30 );
+    current_radiostack->set_nav1_alt_freq( 110.30 );
+    current_radiostack->set_nav1_sel_radial( 119.0 );
+
+    current_radiostack->set_nav2_freq( 111.80 );
+    current_radiostack->set_nav2_alt_freq( 115.70 );
+    current_radiostack->set_nav2_sel_radial( 029.0 );
+
+    current_radiostack->set_adf_freq( 266.0 );
+
+#if 0
+    // This block of settings are Alex's defaults for San Diego
+    current_radiostack->set_nav1_freq( 111.70 );
+    current_radiostack->set_nav1_alt_freq( 115.30 );
+    current_radiostack->set_nav1_sel_radial( 280.0 );
+    current_radiostack->set_nav2_freq( 117.80 );
+    current_radiostack->set_nav2_alt_freq( 114.00 );
+    current_radiostack->set_nav2_sel_radial( 68.0 );
+    current_radiostack->set_adf_freq( 210.0 );
+    // End of Alex's custom settings
+#endif
+
+    current_radiostack->search( cur_fdm_state->get_Longitude(),
+                               cur_fdm_state->get_Latitude(),
+                               cur_fdm_state->get_Altitude() * FEET_TO_METER );
+
+    current_radiostack->update( cur_fdm_state->get_Longitude(),
+                               cur_fdm_state->get_Latitude(),
+                               cur_fdm_state->get_Altitude() * FEET_TO_METER );
+
+    // Search radio database once per second
+    global_events.Register( "fgRadioSearch()", fgRadioSearch,
+                           fgEVENT::FG_EVENT_READY, 1000);
+
 
     // Initialize the Cockpit subsystem
     if( fgCockpitInit( &current_aircraft )) {
@@ -430,28 +653,26 @@ bool fgInitSubsystems( void ) {
        exit(-1);
     }
 
-    // Initialize the "sky"
-    fgSkyInit();
-
     // Initialize the flight model subsystem data structures base on
     // above values
 
-    fgFDMInit( current_options.get_flight_model(), cur_fdm_state,
-              1.0 / DEFAULT_MODEL_HZ );
+    // fgFDMInit( current_options.get_flight_model(), cur_fdm_state,
+    //            1.0 / current_options.get_model_hz() );
+    cur_fdm_state->init( 1.0 / current_options.get_model_hz() );
 
     // I'm just sticking this here for now, it should probably move
     // eventually
-    scenery.cur_elev = f->get_Runway_altitude() * FEET_TO_METER;
+    scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
 
-    if ( f->get_Altitude() < f->get_Runway_altitude() + 3.758099) {
-       f->set_Altitude( f->get_Runway_altitude() + 3.758099 );
+    if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
+       cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
     }
 
     FG_LOG( FG_GENERAL, FG_INFO,
            "Updated position (after elevation adj): ("
-           << (f->get_Latitude() * RAD_TO_DEG) << ", "
-           << (f->get_Longitude() * RAD_TO_DEG) << ", "
-           << (f->get_Altitude() * FEET_TO_METER) << ")" );
+           << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
+           << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
+           << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
     // end of thing that I just stuck in that I should probably move
 
     // Joystick support
@@ -461,14 +682,27 @@ bool fgInitSubsystems( void ) {
        FG_LOG( FG_GENERAL, FG_ALERT, "Error in Joystick initialization!" );
     }
 
-    // Autopilot init added here, by Jeff Goeke-Smith
-    fgAPInit(&current_aircraft);
+    // Autopilot init
+    current_autopilot = new FGAutopilot;
+    current_autopilot->init();
+
+    // initialize the gui parts of the autopilot
+    NewTgtAirportInit();
+    fgAPAdjustInit() ;
+    NewHeadingInit();
+    NewAltitudeInit();
 
-    // Initialize serial ports
+    // Initialize I/O channels
 #if ! defined( MACOS )
-    fgSerialInit();
+    fgIOInit();
 #endif
 
+    // Initialize the 2D panel.
+    current_panel = fgCreateSmallSinglePropPanel(0, 0, 1024, 768);
+
+    // Initialize the BFI
+    FGBFI::init();
+
     FG_LOG( FG_GENERAL, FG_INFO, endl);
 
     return true;
@@ -477,95 +711,98 @@ bool fgInitSubsystems( void ) {
 
 void fgReInitSubsystems( void )
 {
-    FGInterface *f = current_aircraft.fdm_state;
-    FGView *v = &current_view;
-    FGTime *t = FGTime::cur_time_params;
+    bool freeze = globals->get_freeze();
+    if( !freeze )
+        globals->set_freeze( true );
     
-    int toggle_pause = t->getPause();
-    
-    if( !toggle_pause )
-        t->togglePauseMode();
-    
-    fgInitPosition();
     if( global_tile_mgr.init() ) {
        // Load the local scenery data
-       global_tile_mgr.update();
+       global_tile_mgr.update( current_options.get_lon(),
+                               current_options.get_lat() );
     } else {
        FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
                exit(-1);
     }
+
+    // cout << "current scenery elev = " << scenery.cur_elev << endl;
+
+    fgInitPosition();
     fgFDMSetGroundElevation( current_options.get_flight_model(), 
                             scenery.cur_elev );
 
     // Reset our altitude if we are below ground
-    FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << f->get_Altitude() );
+    FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
     FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " << 
-           f->get_Runway_altitude() );
+           cur_fdm_state->get_Runway_altitude() );
 
-    if ( f->get_Altitude() < f->get_Runway_altitude() + 3.758099) {
-       f->set_Altitude( f->get_Runway_altitude() + 3.758099 );
+    if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
+       cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
     }
     double sea_level_radius_meters;
     double lat_geoc;
     // Set the FG variables first
-    fgGeodToGeoc( f->get_Latitude(), f->get_Altitude(), 
+    fgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(), 
                  &sea_level_radius_meters, &lat_geoc);
-    f->set_Geocentric_Position( lat_geoc, f->get_Longitude(), 
-                               f->get_Altitude() + 
+    cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(), 
+                               cur_fdm_state->get_Altitude() + 
                                (sea_level_radius_meters * METER_TO_FEET) );
-    f->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
+    cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
        
-    f->set_sin_cos_longitude(f->get_Longitude());
-    f->set_sin_cos_latitude(f->get_Latitude());
+    cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
+    cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
        
-    f->set_sin_lat_geocentric(sin(lat_geoc));
-    f->set_cos_lat_geocentric(cos(lat_geoc));
+    cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
+    cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
 
     // The following section sets up the flight model EOM parameters
     // and should really be read in from one or more files.
 
     // Initial Velocity
-    f->set_Velocities_Local( current_options.get_uBody(),
+    cur_fdm_state->set_Velocities_Local( current_options.get_uBody(),
                              current_options.get_vBody(),
                              current_options.get_wBody());
 
     // Initial Orientation
-    f->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
+    cur_fdm_state->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
                         current_options.get_pitch() * DEG_TO_RAD,
                         current_options.get_heading() * DEG_TO_RAD );
 
     // Initial Angular Body rates
-    f->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
+    cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
 
-    f->set_Earth_position_angle( 0.0 );
+    cur_fdm_state->set_Earth_position_angle( 0.0 );
 
     // Mass properties and geometry values
-    f->set_Inertias( 8.547270E+01, 
+    cur_fdm_state->set_Inertias( 8.547270E+01, 
                     1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
 
     // CG position w.r.t. ref. point
-    f->set_CG_Position( 0.0, 0.0, 0.0 );
+    cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
 
     // Initialize view parameters
-    FG_LOG( FG_GENERAL, FG_DEBUG, "Before v->init()");
-    v->Init();
-    FG_LOG( FG_GENERAL, FG_DEBUG, "After v->init()");
-    v->UpdateViewMath(f);
-    FG_LOG( FG_GENERAL, FG_DEBUG, "  abs_view_pos = " << v->get_abs_view_pos());
-    // v->UpdateWorldToEye(f);
+    current_view.set_view_offset( 0.0 );
+    current_view.set_goal_view_offset( 0.0 );
+    pilot_view.set_view_offset( 0.0 );
+    pilot_view.set_goal_view_offset( 0.0 );
+
+    FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
+    current_view.UpdateViewMath(*cur_fdm_state);
+    pilot_view.UpdateViewMath(*cur_fdm_state);
+    FG_LOG( FG_GENERAL, FG_DEBUG, "  abs_view_pos = " << current_view.get_abs_view_pos());
 
-    fgFDMInit( current_options.get_flight_model(), cur_fdm_state, 
-              1.0 / DEFAULT_MODEL_HZ );
+    // fgFDMInit( current_options.get_flight_model(), cur_fdm_state, 
+    //            1.0 / current_options.get_model_hz() );
+    cur_fdm_state->init( 1.0 / current_options.get_model_hz() );
 
-    scenery.cur_elev = f->get_Runway_altitude() * FEET_TO_METER;
+    scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
 
-    if ( f->get_Altitude() < f->get_Runway_altitude() + 3.758099) {
-       f->set_Altitude( f->get_Runway_altitude() + 3.758099 );
+    if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
+       cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
     }
 
     controls.reset_all();
-    fgAPReset();
+    current_autopilot->reset();
 
-    if( !toggle_pause )
-        t->togglePauseMode();
+    if( !freeze )
+        globals->set_freeze( false );
 }