]> 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 542273cdbeab9fa3b71a8825af32e3add7c53124..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 <Cockpit/radiostack.hxx>
+#include <Cockpit/panel.hxx>
+#include <Cockpit/sp_panel.hxx>
 #include <FDM/Balloon.h>
-#include <FDM/JSBsim.hxx>
+#include <FDM/External.hxx>
+#include <FDM/JSBSim.hxx>
 #include <FDM/LaRCsim.hxx>
 #include <FDM/MagicCarpet.hxx>
-#include <Include/fg_constants.h>
 #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 <Time/tmp.hxx>
 
 #ifndef FG_OLD_WEATHER
 #  include <WeatherCM/FGLocalWeatherDatabase.h>
 #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>
@@ -127,38 +137,168 @@ 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
 
-       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 {
+       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,
@@ -166,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: ("
@@ -181,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, "======= ==============" );
@@ -220,14 +363,41 @@ 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();
-
     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, "========== ==========");
 
+    // 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() ) {
+       // Material lib initialized ok.
+    } else {
+       FG_LOG( FG_GENERAL, FG_ALERT, "Error in Scenery initialization!" );
+       exit(-1);
+    }
+
+    if ( global_tile_mgr.init() ) {
+       // Load the local scenery data
+       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);
+    }
+
+    FG_LOG( FG_GENERAL, FG_DEBUG,
+           "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 ) {
@@ -238,6 +408,9 @@ bool fgInitSubsystems( void ) {
     } 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" );
@@ -251,26 +424,6 @@ bool fgInitSubsystems( void ) {
     // set the initial position
     fgInitPosition();
 
-    // Initialize the Scenery Management subsystem
-    if ( fgSceneryInit() ) {
-       // Scenery initialized ok.
-    } else {
-       FG_LOG( FG_GENERAL, FG_ALERT, "Error in Scenery initialization!" );
-       exit(-1);
-    }
-
-    if( global_tile_mgr.init() ) {
-       // Load the local scenery data
-       global_tile_mgr.update();
-    } else {
-       FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
-       exit(-1);
-    }
-
-    FG_LOG( FG_GENERAL, FG_DEBUG,
-           "Current terrain elevation after tile mgr init " <<
-           scenery.cur_elev );
-
     // Calculate ground elevation at starting point (we didn't have
     // tmp_abs_view_pos calculated when fgTileMgrUpdate() was called above
     //
@@ -366,10 +519,6 @@ bool fgInitSubsystems( void ) {
                                                   &fgEVENT_MGR::PrintStats),
                            fgEVENT::FG_EVENT_READY, 60000 );
 
-    // Initialize the time dependent variables
-    t->init(*cur_fdm_state);
-    t->update(*cur_fdm_state);
-
     // Initialize view parameters
     FG_LOG( FG_GENERAL, FG_DEBUG, "Before current_view.init()");
     current_view.Init();
@@ -380,19 +529,6 @@ bool fgInitSubsystems( void ) {
     FG_LOG( FG_GENERAL, FG_DEBUG, "  abs_view_pos = " << current_view.get_abs_view_pos());
     // current_view.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);
-    }
-
     // Initialize the planetary subsystem
     // global_events.Register( "fgPlanetsInit()", fgPlanetsInit,
     //                     fgEVENT::FG_EVENT_READY, 600000);
@@ -405,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.
@@ -428,9 +560,7 @@ bool fgInitSubsystems( void ) {
                                                       &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
@@ -442,7 +572,7 @@ bool fgInitSubsystems( void ) {
               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;
@@ -456,6 +586,65 @@ bool fgInitSubsystems( void ) {
     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 )) {
        // Cockpit initialized ok.
@@ -464,9 +653,6 @@ bool fgInitSubsystems( void ) {
        exit(-1);
     }
 
-    // Initialize the "sky"
-    fgSkyInit();
-
     // Initialize the flight model subsystem data structures base on
     // above values
 
@@ -496,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;
@@ -512,21 +711,22 @@ bool fgInitSubsystems( void ) {
 
 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();
+       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 );
 
@@ -601,8 +801,8 @@ void fgReInitSubsystems( void )
     }
 
     controls.reset_all();
-    fgAPReset();
+    current_autopilot->reset();
 
-    if( !toggle_pause )
-        t->togglePauseMode();
+    if( !freeze )
+        globals->set_freeze( false );
 }