From 535ed922647d638112e540ec59d1dd1e7663219d Mon Sep 17 00:00:00 2001 From: curt Date: Wed, 16 Aug 2000 00:09:03 +0000 Subject: [PATCH] Added code to put aircraft at the end of the runway closest to the desired heading. Only changed for initial position right now. --- src/Airports/Makefile.am | 9 +- .../{buildsimple.cxx => gensimple.cxx} | 0 src/Main/fg_init.cxx | 105 ++++++++++++++++++ src/Main/fg_init.hxx | 3 + src/Main/main.cxx | 4 +- 5 files changed, 117 insertions(+), 4 deletions(-) rename src/Airports/{buildsimple.cxx => gensimple.cxx} (100%) diff --git a/src/Airports/Makefile.am b/src/Airports/Makefile.am index 6ececcd0f..9fcdb33e4 100644 --- a/src/Airports/Makefile.am +++ b/src/Airports/Makefile.am @@ -1,12 +1,15 @@ noinst_LIBRARIES = libAirports.a -noinst_PROGRAMS = buildsimple +noinst_PROGRAMS = gensimple genrunways libAirports_a_SOURCES = \ + runways.cxx runways.hxx \ simple.cxx simple.hxx -buildsimple_SOURCES = buildsimple.cxx +gensimple_SOURCES = gensimple.cxx +gensimple_LDADD = libAirports.a -lsgdebug -lsgmisc -lmk4 -lz -buildsimple_LDADD = libAirports.a -lsgdebug -lsgmisc -lmk4 -lz +genrunways_SOURCES = genrunways.cxx +genrunways_LDADD = libAirports.a -lsgdebug -lsgmisc -lmk4 -lz INCLUDES += -I$(top_builddir) -I$(top_builddir)/src diff --git a/src/Airports/buildsimple.cxx b/src/Airports/gensimple.cxx similarity index 100% rename from src/Airports/buildsimple.cxx rename to src/Airports/gensimple.cxx diff --git a/src/Main/fg_init.cxx b/src/Main/fg_init.cxx index efbdf522b..bd0818d9a 100644 --- a/src/Main/fg_init.cxx +++ b/src/Main/fg_init.cxx @@ -56,6 +56,7 @@ #include #include +#include #include #include #include @@ -183,6 +184,110 @@ bool fgSetPosFromAirportID( const string& id ) { 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; diff --git a/src/Main/fg_init.hxx b/src/Main/fg_init.hxx index 182f78ac4..1210e3daa 100644 --- a/src/Main/fg_init.hxx +++ b/src/Main/fg_init.hxx @@ -52,6 +52,9 @@ void fgReInitSubsystems( void ); // Set current_options lon/lat given an airport id bool fgSetPosFromAirportID( const string& id ); +// Set current_options lon/lat given an airport id and heading (degrees) +bool fgSetPosFromAirportIDandHdg( const string& id, double tgt_hdg ); + #endif // _FG_INIT_H diff --git a/src/Main/main.cxx b/src/Main/main.cxx index ec67ea5b6..4cd07e5a6 100644 --- a/src/Main/main.cxx +++ b/src/Main/main.cxx @@ -1317,7 +1317,9 @@ int main( int argc, char **argv ) { // set current_options lon/lat if an airport id is specified if ( current_options.get_airport_id().length() ) { - fgSetPosFromAirportID( current_options.get_airport_id() ); + // fgSetPosFromAirportID( current_options.get_airport_id() ); + fgSetPosFromAirportIDandHdg( current_options.get_airport_id(), + current_options.get_heading() ); } // Initialize time