]> 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 3cb462ec2a688ce4d49ef859b19b8d5176d35609..bd0818d9a81c0f0a32998b5e463e46be9a2804a0 100644 (file)
@@ -1,4 +1,3 @@
-//
 // fg_init.cxx -- Flight Gear top level initialization routines
 //
 // Written by Curtis Olson, started August 1997.
@@ -57,6 +56,7 @@
 #include <simgear/timing/sg_time.hxx>
 
 #include <Aircraft/aircraft.hxx>
+#include <Airports/runways.hxx>
 #include <Airports/simple.hxx>
 #include <Autopilot/auto_gui.hxx>
 #include <Autopilot/newauto.hxx>
@@ -66,7 +66,7 @@
 #include <Cockpit/sp_panel.hxx>
 #include <FDM/Balloon.h>
 #include <FDM/External.hxx>
-#include <FDM/JSBsim.hxx>
+#include <FDM/JSBSim.hxx>
 #include <FDM/LaRCsim.hxx>
 #include <FDM/MagicCarpet.hxx>
 #include <Include/general.hxx>
@@ -137,14 +137,11 @@ bool fgInitConfig ( int argc, char **argv ) {
 }
 
 
-// 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
 
@@ -152,7 +149,6 @@ bool fgInitPosition( void ) {
        path.append( "Airports" );
        path.append( "simple.mk4" );
        FGAirports airports( path.c_str() );
-       FGAirport a;
 
        FG_LOG( FG_GENERAL, FG_INFO,
                "Attempting to set starting position from airport code "
@@ -171,16 +167,138 @@ bool fgInitPosition( void ) {
        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 {
+       return false;
+    }
+
+    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 {
-       // set initial position from default or command line coordinates
+       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();
 
-       f->set_Longitude( current_options.get_lon() * DEG_TO_RAD );
-       f->set_Latitude( current_options.get_lat() * DEG_TO_RAD );
+    // 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,
@@ -188,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: ("
@@ -250,32 +368,6 @@ bool fgInitSubsystems( void ) {
     FG_LOG( FG_GENERAL, FG_INFO, "Initialize Subsystems");
     FG_LOG( FG_GENERAL, FG_INFO, "========== ==========");
 
-    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();
-
     // Initialize the material property lib
     FGPath mpath( current_options.get_fg_root() );
     mpath.append( "materials" );
@@ -293,10 +385,10 @@ bool fgInitSubsystems( void ) {
        exit(-1);
     }
 
-    if( global_tile_mgr.init() ) {
+    if ( global_tile_mgr.init() ) {
        // Load the local scenery data
-       global_tile_mgr.update( cur_fdm_state->get_Longitude(),
-                               cur_fdm_state->get_Latitude() );
+       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);
@@ -306,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
     //
@@ -597,15 +715,18 @@ void fgReInitSubsystems( void )
     if( !freeze )
         globals->set_freeze( true );
     
-    fgInitPosition();
     if( global_tile_mgr.init() ) {
        // Load the local scenery data
-       global_tile_mgr.update( cur_fdm_state->get_Longitude(),
-                               cur_fdm_state->get_Latitude() );
+       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 );