-//
// fg_init.cxx -- Flight Gear top level initialization routines
//
// Written by Curtis Olson, started August 1997.
#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 <Autopilot/auto_gui.hxx>
#include <Autopilot/newauto.hxx>
#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>
#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 <Time/tmp.hxx>
#ifndef FG_OLD_WEATHER
# include <WeatherCM/FGLocalWeatherDatabase.h>
#include "fg_init.hxx"
#include "fg_io.hxx"
+#include "globals.hxx"
#include "options.hxx"
#include "views.hxx"
+#include "bfi.hxx"
#if defined(FX) && defined(XMESA)
#include <GL/xmesa.h>
}
-// 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
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 "
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;
+ }
- f->set_Longitude( current_options.get_lon() * DEG_TO_RAD );
- f->set_Latitude( current_options.get_lat() * DEG_TO_RAD );
+ 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,
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: ("
// Returns non-zero if a problem encountered.
bool fgInitSubsystems( void ) {
fgLIGHT *l = &cur_light_params;
- FGTime *t = FGTime::cur_time_params;
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" );
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);
"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
//
&fgLIGHT::Update),
fgEVENT::FG_EVENT_READY, 30000 );
// update the current timezone each 30 minutes
- global_events.Register( "fgTIME::updateLocal()",
- fgMethodCallback<FGTime>(FGTime::cur_time_params,
- &FGTime::updateLocal),
+ global_events.Register( "fgUpdateLocalTime()", fgUpdateLocalTime,
fgEVENT::FG_EVENT_READY, 1800000);
// Initialize the weather modeling subsystem
current_aircraft.fdm_state->get_Longitude(),
current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER );
FGLocalWeatherDatabase::theFGLocalWeatherDatabase =
- new FGLocalWeatherDatabase( position );
+ new FGLocalWeatherDatabase( position, current_options.get_fg_root() );
// cout << theFGLocalWeatherDatabase << endl;
// cout << "visibility = "
// << theFGLocalWeatherDatabase->getWeatherVisibility() << endl;
// 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;
void fgReInitSubsystems( void )
{
- FGTime *t = FGTime::cur_time_params;
-
- int toggle_pause = t->getPause();
+ bool freeze = globals->get_freeze();
+ if( !freeze )
+ globals->set_freeze( true );
- if( !toggle_pause )
- t->togglePauseMode();
-
- 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 );
controls.reset_all();
current_autopilot->reset();
- if( !toggle_pause )
- t->togglePauseMode();
+ if( !freeze )
+ globals->set_freeze( false );
}