From: curt Date: Sat, 16 Nov 2002 21:34:51 +0000 (+0000) Subject: Clean ups to the code that determines initial position based on command X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=35f24baf32ebcf35cece5e405af90cfaf102b2eb;p=flightgear.git Clean ups to the code that determines initial position based on command line options or defaults. --- diff --git a/src/GUI/apt_dlg.cxx b/src/GUI/apt_dlg.cxx index c45505545..d751977c7 100644 --- a/src/GUI/apt_dlg.cxx +++ b/src/GUI/apt_dlg.cxx @@ -92,21 +92,18 @@ void AptDialog_OK (puObject *) cur_fdm_state->unbind(); AptId = a.id.c_str(); /// NHV fix wrong case crash - fgSetString("/sim/presets/airport-id", AptId.c_str() ); - // fgSetDouble("/position/altitude-ft", -9999.0 ); - // fgSetPosFromAirportID( AptId ); - fgSetPosFromAirportIDandHdg( AptId, - cur_fdm_state->get_Psi() * - SGD_RADIANS_TO_DEGREES); - fgSetTowerPosFromAirportID( AptId, - cur_fdm_state->get_Psi() * - SGD_RADIANS_TO_DEGREES); + fgSetString("/sim/presets/airport-id", AptId.c_str() ); + fgSetDouble("/sim/presets/longitude-deg", -9999.0 ); + fgSetDouble("/sim/presets/latitude-deg", -9999.0 ); + fgInitPosition(); + // BusyCursor(0); fgReInitSubsystems(); double visibility_meters = - fgGetDouble("/environment/visibility-m"); + fgGetDouble("/environment/visibility-m"); global_tile_mgr.update( longitude->getDoubleValue(), - latitude->getDoubleValue(),visibility_meters ); + latitude->getDoubleValue(), + visibility_meters ); // BusyCursor(1); } else { AptId += " not in database."; diff --git a/src/GUI/gui_local.cxx b/src/GUI/gui_local.cxx index 329c82341..1342ce2f8 100644 --- a/src/GUI/gui_local.cxx +++ b/src/GUI/gui_local.cxx @@ -73,13 +73,6 @@ void reInit(puObject *cb) globals->restoreInitialState(); - // Unsuccessful KLUDGE to fix the 'every other time' - // problem when doing a 'reset' after a 'goto airport' - - // string AptId( fgGetString("/sim/presets/airport-id") ); - // if( AptId.c_str() != "\0" ) - // fgSetPosFromAirportID( AptId ); - SGTime *t = globals->get_time_params(); delete t; t = fgInitTime(); @@ -87,10 +80,9 @@ void reInit(puObject *cb) fgReInitSubsystems(); - // reduntant(fgReInitSubsystems) ? - double visibility_meters = fgGetDouble("/environment/visibility-m"); global_tile_mgr.update( fgGetDouble("/position/longitude-deg"), - fgGetDouble("/position/latitude-deg"),visibility_meters ); + fgGetDouble("/position/latitude-deg"), + fgGetDouble("/environment/visibility-m") ); cur_light_params.Update(); diff --git a/src/Main/fg_init.cxx b/src/Main/fg_init.cxx index 4f2f996da..e876fd6a3 100644 --- a/src/Main/fg_init.cxx +++ b/src/Main/fg_init.cxx @@ -577,7 +577,7 @@ bool fgFindAirportID( const string& id, FGAirport *a ) { // Preset lon/lat given an airport id -bool fgSetPosFromAirportID( const string& id ) { +static bool fgSetPosFromAirportID( const string& id ) { FGAirport a; // double lon, lat; @@ -608,7 +608,7 @@ bool fgSetPosFromAirportID( const string& id ) { // Set current tower position lon/lat given an airport id -bool fgSetTowerPosFromAirportID( const string& id, double hdg ) { +static bool fgSetTowerPosFromAirportID( const string& id, double hdg ) { FGAirport a; // tower height hard coded for now... float towerheight=50.0f; @@ -630,7 +630,7 @@ bool fgSetTowerPosFromAirportID( const string& id, double hdg ) { // Set current_options lon/lat given an airport id and heading (degrees) -bool fgSetPosFromAirportIDandHdg( const string& id, double tgt_hdg ) { +static bool fgSetPosFromAirportIDandHdg( const string& id, double tgt_hdg ) { FGRunway r; FGRunway found_r; double found_dir = 0.0; @@ -756,36 +756,83 @@ bool fgSetPosFromAirportIDandHdg( const string& id, double tgt_hdg ) { } -void fgSetPosFromGlideSlope() { +static void fgSetDistOrAltFromGlideSlope() { double gs = fgGetDouble("/sim/presets/glideslope"); double od = fgGetDouble("/sim/presets/offset-distance"); double alt = fgGetDouble("/sim/presets/altitude-ft"); - // if glideslope and offset-distance are set and altitude is not, - // calculate the initial altitude if( fabs(gs) > 0.01 && fabs(od) > 0.1 && alt < -9990 ) { + // set altitude from glideslope and offset-distance od *= SG_NM_TO_METER * SG_METER_TO_FEET; alt = fabs(od*tan(gs)); - fgSetDouble("/sim/presets/altitude-ft",alt); + fgSetDouble("/sim/presets/altitude-ft", alt); fgSetBool("/sim/presets/onground", false); SG_LOG(SG_GENERAL,SG_INFO, "Calculated altitude as: " << alt << " ft"); } else if( fabs(gs) > 0.01 && alt > 0 && fabs(od) < 0.1) { + // set offset-distance from glideslope and altitude od = alt/tan(gs); od *= -1*SG_FEET_TO_METER * SG_METER_TO_NM; - fgSetDouble("/sim/presets/offset-distance",od); + fgSetDouble("/sim/presets/offset-distance", od); SG_LOG(SG_GENERAL, SG_INFO, "Calculated offset distance as: " << od << " nm"); } else if( fabs(gs) > 0.01 ) { SG_LOG( SG_GENERAL, SG_ALERT, "Glideslope given but not altitude or offset-distance." ); SG_LOG( SG_GENERAL, SG_ALERT, "Resetting glideslope to zero" ); - fgSetDouble("/sim/presets/glideslope",0); + fgSetDouble("/sim/presets/glideslope", 0); } } +// Set the initial position based on presets (or defaults) +bool fgInitPosition() { + // Calculate offset-distance or altitude relative to glide slope + // if either was not specified. + fgSetDistOrAltFromGlideSlope(); + + // If we have an explicit, in-range lon/lat, don't change it, just use it. + // If not, check for an airport-id and use that. + // If not, default to the middle of the KSFO field. + // The default values for lon/lat are deliberately out of range + // so that the airport-id can take effect; valid lon/lat will + // override airport-id, however. + double lon_deg = fgGetDouble("/sim/presets/longitude-deg"); + double lat_deg = fgGetDouble("/sim/presets/latitude-deg"); + if ( lon_deg >= -180.0 && lon_deg <= 180.0 + && lat_deg >= -90.0 && lat_deg <= 90.0 ) + { + // valid lon/lat specified, use it. + } else { + string apt = fgGetString("/sim/presets/airport-id"); + double hdg = fgGetDouble("/sim/presets/heading-deg"); + if ( !apt.empty() ) { + // An airport id is requested, set position from that. + fgSetPosFromAirportIDandHdg( apt, hdg ); + + // set tower position (a little off the heading for single + // runway airports) + fgSetTowerPosFromAirportID( apt, hdg ); + } else { + // No lon/lat specified, no airport specified, default to + // middle of KSFO field. + fgSetDouble("/sim/presets/longitude-deg", -122.374843); + fgSetDouble("/sim/presets/latitude-deg", 37.619002); + } + } + + fgSetDouble( "/position/longitude-deg", + fgGetDouble("/sim/presets/longitude-deg") ); + fgSetDouble( "/position/latitude-deg", + fgGetDouble("/sim/presets/latitude-deg") ); + fgSetDouble( "/orientation/heading-deg", + fgGetDouble("/sim/presets/heading-deg") ); + + return true; +} + + // General house keeping initializations -bool fgInitGeneral( void ) { +bool fgInitGeneral() { string root; #if defined(FX) && defined(XMESA) diff --git a/src/Main/fg_init.hxx b/src/Main/fg_init.hxx index d967c44c5..cd529cc6f 100644 --- a/src/Main/fg_init.hxx +++ b/src/Main/fg_init.hxx @@ -79,18 +79,8 @@ void fgReInitSubsystems( void ); // find basic airport location info from airport database bool fgFindAirportID( const string& id, FGAirport *a ); -// Set pos given an airport id -bool fgSetPosFromAirportID( const string& id ); - -// Set tower position given an airport id -bool fgSetTowerPosFromAirportID( const string& id, double hdg ); - -// Set position and heading given an airport id and heading (degrees) -bool fgSetPosFromAirportIDandHdg( const string& id, double tgt_hdg ); - -//find altitude given glideslope and offset distance or offset distance -//given glideslope and altitude -void fgSetPosFromGlideSlope(void); +// Set the initial position based on presets (or defaults) +bool fgInitPosition(); // Initialize various time dependent systems (lighting, sun position, etc.) // returns a new instance of the SGTime class diff --git a/src/Main/main.cxx b/src/Main/main.cxx index b29307dac..a3b1f325f 100644 --- a/src/Main/main.cxx +++ b/src/Main/main.cxx @@ -1609,31 +1609,9 @@ static bool fgMainInit( int argc, char **argv ) { # endif #endif - // Set position relative to glide slope if requested - fgSetPosFromGlideSlope(); - - // If we have an explicit, in-range lon/lat, use it. - // If not, check for an airport-id and use that. - // If not, default to the middle of the KSFO field. - // The default values for lon/lat are deliberately out of range - // so that the airport-id can take effect; valid lon/lat will - // override airport-id, however. - double lon_deg = fgGetDouble("/position/longitude-deg"); - double lat_deg = fgGetDouble("/position/latitude-deg"); - if (lon_deg < -180 || lon_deg > 180 || lat_deg < -90 || lat_deg > 90) { - if ( fgGetString("/sim/presets/airport-id")[0] != '\0' ) { - fgSetPosFromAirportIDandHdg( fgGetString("/sim/presets/airport-id"), - fgGetDouble("/sim/presets/heading-deg") ); - // set tower position (a little off the heading for single - // runway airports) - fgSetTowerPosFromAirportID( fgGetString("/sim/presets/airport-id"), - fgGetDouble("/sim/presets/heading-deg") ); - } else { - // Default to middle of KSFO field - fgSetDouble("/sim/presets/longitude-deg", -122.374843); - fgSetDouble("/sim/presets/latitude-deg", 37.619002); - } - } + // based on the requested presets, calculate the true starting + // lon, lat + fgInitPosition(); SGTime *t = fgInitTime(); globals->set_time_params( t );