]> git.mxchange.org Git - flightgear.git/blobdiff - src/Main/fg_init.cxx
Add David Culp's AI model manager code which is derived from David Luff's AI/ATC...
[flightgear.git] / src / Main / fg_init.cxx
index 80a9f868d71a14e8425a26390036c82c1474cf5f..873e82aac22f3a4e5424726fdf02c34c323abce9 100644 (file)
 #endif
 
 #include <simgear/compiler.h>
-#include <simgear/misc/exception.hxx>
 
 #include STL_STRING
 
 #include <simgear/constants.h>
 #include <simgear/debug/logstream.hxx>
+#include <simgear/structure/exception.hxx>
+#include <simgear/structure/event_mgr.hxx>
 #include <simgear/math/point3d.hxx>
 #include <simgear/math/polar3d.hxx>
 #include <simgear/math/sg_geodesy.hxx>
@@ -94,6 +95,7 @@
 #include <Input/input.hxx>
 #include <Instrumentation/instrument_mgr.hxx>
 #include <Model/acmodel.hxx>
+#include <AIModel/AIManager.hxx>
 #include <Navaids/fixlist.hxx>
 #include <Navaids/ilslist.hxx>
 #include <Navaids/mkrbeacons.hxx>
 #if defined(HAVE_PLIB_PSL)
 #include <Scripting/scriptmgr.hxx>
 #endif
+#include <Scripting/NasalSys.hxx>
 #include <Sound/fg_fx.hxx>
 #include <Systems/system_mgr.hxx>
-#include <Time/FGEventMgr.hxx>
 #include <Time/light.hxx>
 #include <Time/moonpos.hxx>
 #include <Time/sunpos.hxx>
@@ -268,8 +270,27 @@ bool fgInitFGRoot ( int argc, char **argv ) {
         root = "/FlightGear";
 #elif defined( WIN32 )
         root = "\\FlightGear";
-#elif defined( macintosh )
-        root = "";
+#elif defined(OSX_BUNDLE) 
+        /* the following code looks for the base package directly inside
+            the application bundle. This can be changed fairly easily by
+            fiddling with the code below. And yes, I know it's ugly and verbose.
+        */
+        CFBundleRef appBundle = CFBundleGetMainBundle();
+        CFURLRef appUrl = CFBundleCopyBundleURL(appBundle);
+        CFRelease(appBundle);
+
+        // look for a 'data' subdir directly inside the bundle : is there
+        // a better place? maybe in Resources? I don't know ...
+        CFURLRef dataDir = CFURLCreateCopyAppendingPathComponent(NULL, appUrl, CFSTR("data"), true);
+
+        // now convert down to a path, and the a c-string
+        CFStringRef path = CFURLCopyFileSystemPath(dataDir, kCFURLPOSIXPathStyle);
+        root = CFStringGetCStringPtr(path, CFStringGetSystemEncoding());
+
+        // tidy up.
+        CFRelease(appBundle);
+        CFRelease(dataDir);
+        CFRelease(path);
 #else
         root = PKGLIBDIR;
 #endif
@@ -837,6 +858,7 @@ static bool fgSetPosFromAirportIDandRwy( const string& id, const string& rwy ) {
 
 
 static void fgSetDistOrAltFromGlideSlope() {
+    // cout << "fgSetDistOrAltFromGlideSlope()" << endl;
     string apt_id = fgGetString("/sim/presets/airport-id");
     double gs = fgGetDouble("/sim/presets/glideslope-deg")
         * SG_DEGREES_TO_RADIANS ;
@@ -1024,12 +1046,18 @@ fgInitNav ()
 
 // Set the initial position based on presets (or defaults)
 bool fgInitPosition() {
+    // cout << "fgInitPosition()" << endl;
+    double gs = fgGetDouble("/sim/presets/glideslope-deg")
+        * SG_DEGREES_TO_RADIANS ;
+    double od = fgGetDouble("/sim/presets/offset-distance");
+    double alt = fgGetDouble("/sim/presets/altitude-ft");
+
     bool set_pos = false;
 
     // If glideslope is specified, then calculate offset-distance or
     // altitude relative to glide slope if either of those was not
     // specified.
-    if ( fgGetDouble("/sim/presets/glideslope-deg") > 0.1 ) {
+    if ( fabs( gs ) > 0.01 ) {
         fgSetDistOrAltFromGlideSlope();
     }
 
@@ -1112,6 +1140,13 @@ bool fgInitPosition() {
     fgSetDouble( "/orientation/heading-deg",
                  fgGetDouble("/sim/presets/heading-deg") );
 
+    // determine if this should be an on-ground or in-air start
+    if ( fabs(gs) > 0.01 || fabs(od) > 0.1 || alt > 0.1 ) {
+        fgSetBool("/sim/presets/onground", false);
+    } else {
+        fgSetBool("/sim/presets/onground", true);
+    }                              
+
     return true;
 }
 
@@ -1391,8 +1426,6 @@ void fgInitTimeOffset() {
 
     SG_LOG( SG_GENERAL, SG_INFO, "After fgInitTimeOffset(): warp = " 
             << globals->get_warp() );
-
-    fgUpdateSkyAndLightingParams();
 }
 
 // This is the top level init routine which calls all the other
@@ -1407,11 +1440,17 @@ bool fgInitSubsystems() {
     // static const SGPropertyNode *altitude
     //     = fgGetNode("/sim/presets/altitude-ft");
 
-    fgLIGHT *l = &cur_light_params;
+    FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
 
     SG_LOG( SG_GENERAL, SG_INFO, "Initialize Subsystems");
     SG_LOG( SG_GENERAL, SG_INFO, "========== ==========");
 
+    ////////////////////////////////////////////////////////////////////
+    // Initialize the event manager subsystem.
+    ////////////////////////////////////////////////////////////////////
+
+     globals->get_event_mgr()->init();
+     globals->get_event_mgr()->setFreezeProperty(fgGetNode("/sim/freeze/clock"));
 
     ////////////////////////////////////////////////////////////////////
     // Initialize the material property subsystem.
@@ -1424,17 +1463,6 @@ bool fgInitSubsystems() {
         exit(-1);
     }
 
-    ////////////////////////////////////////////////////////////////////
-    // Initialize the event manager subsystem.
-    ////////////////////////////////////////////////////////////////////
-
-    global_events.init();
-
-    // Output event stats every 60 seconds
-    global_events.Register( "FGEventMgr::print_stats()",
-                            &global_events, &FGEventMgr::print_stats,
-                            60000 );
-
 
     ////////////////////////////////////////////////////////////////////
     // Initialize the scenery management subsystem.
@@ -1451,10 +1479,10 @@ bool fgInitSubsystems() {
     }
 
     // cause refresh of viewer scenery timestamps every 15 seconds...
-    global_events.Register( "FGTileMgr::refresh_view_timestamps()",
-                            globals->get_tile_mgr(),
-                            &FGTileMgr::refresh_view_timestamps,
-                            15000 );
+    globals->get_event_mgr()->addTask( "FGTileMgr::refresh_view_timestamps()",
+                                       globals->get_tile_mgr(),
+                                       &FGTileMgr::refresh_view_timestamps,
+                                       15 );
 
     SG_LOG( SG_GENERAL, SG_DEBUG,
             "Current terrain elevation after tile mgr init " <<
@@ -1479,31 +1507,6 @@ bool fgInitSubsystems() {
     fgInitView();
 
 
-    ////////////////////////////////////////////////////////////////////
-    // Initialize the lighting subsystem.
-    ////////////////////////////////////////////////////////////////////
-
-    // 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.
-    fgUpdateSunPos();
-    fgUpdateMoonPos();
-    global_events.Register( "fgUpdateSunPos()", &fgUpdateSunPos,
-                            60000);
-    global_events.Register( "fgUpdateMoonPos()", &fgUpdateMoonPos,
-                            60000);
-
-    // Initialize Lighting interpolation tables
-    l->Init();
-
-    // force one lighting update to make it right to start with...
-    l->Update();
-    // update the lighting parameters (based on sun angle)
-    global_events.Register( "fgLight::Update()",
-                            &cur_light_params, &fgLIGHT::Update,
-                            30000 );
-
-
     ////////////////////////////////////////////////////////////////////
     // Create and register the logger.
     ////////////////////////////////////////////////////////////////////
@@ -1524,7 +1527,7 @@ bool fgInitSubsystems() {
     // Create and register the XML GUI.
     ////////////////////////////////////////////////////////////////////
 
-    globals->add_subsystem("gui", new NewGUI, FGSubsystemMgr::INIT);
+    globals->add_subsystem("gui", new NewGUI, SGSubsystemMgr::INIT);
 
 
     ////////////////////////////////////////////////////////////////////
@@ -1532,8 +1535,8 @@ bool fgInitSubsystems() {
     ////////////////////////////////////////////////////////////////////
 
     // update the current timezone each 30 minutes
-    global_events.Register( "fgUpdateLocalTime()", &fgUpdateLocalTime,
-                            30*60*1000 );
+    globals->get_event_mgr()->addTask( "fgUpdateLocalTime()",
+                                       &fgUpdateLocalTime, 30*60 );
 
 
     ////////////////////////////////////////////////////////////////////
@@ -1579,12 +1582,20 @@ bool fgInitSubsystems() {
     WeatherDatabase = FGLocalWeatherDatabase::theFGLocalWeatherDatabase;
 
     // register the periodic update of the weather
-    global_events.Register( "weather update", &fgUpdateWeatherDatabase,
-                            30000);
+    globals->get_event_mgr()->add( "weather update",
+                                   &fgUpdateWeatherDatabase, 30000);
 #else
     globals->add_subsystem("environment", new FGEnvironmentMgr);
 #endif
 
+
+    ////////////////////////////////////////////////////////////////////
+    // Initialize the lighting subsystem.
+    ////////////////////////////////////////////////////////////////////
+
+    globals->add_subsystem("lighting", new FGLight);
+
+
 #ifdef FG_USE_CLOUDS_3D
     ////////////////////////////////////////////////////////////////////
     // Initialize the 3D cloud subsystem.
@@ -1644,6 +1655,14 @@ bool fgInitSubsystems() {
     globals->get_AI_mgr()->init();
 
 
+    ////////////////////////////////////////////////////////////////////
+    // Initialise the AI Model Manager
+    ////////////////////////////////////////////////////////////////////
+
+    SG_LOG(SG_GENERAL, SG_INFO, "  AI Model Manager");
+    globals->add_subsystem("ai_model", new FGAIManager);
+
+
 #ifdef ENABLE_AUDIO_SUPPORT
     ////////////////////////////////////////////////////////////////////
     // Initialize the sound subsystem.
@@ -1761,7 +1780,6 @@ bool fgInitSubsystems() {
     globals->get_subsystem_mgr()->bind();
     globals->get_subsystem_mgr()->init();
 
-
 #ifdef FG_MPLAYER_AS
     ////////////////////////////////////////////////////////////////////
     // Initialize multiplayer subsystem
@@ -1774,6 +1792,14 @@ bool fgInitSubsystems() {
     globals->get_multiplayer_rx_mgr()->init();
 #endif
 
+    ////////////////////////////////////////////////////////////////////////
+    // Initialize the Nasal interpreter.
+    // Do this last, so that the loaded scripts see initialized state
+    ////////////////////////////////////////////////////////////////////////
+    FGNasalSys* nasal = new FGNasalSys();
+    globals->add_subsystem("nasal", nasal);
+    nasal->init();
+
     ////////////////////////////////////////////////////////////////////////
     // End of subsystem initialization.
     ////////////////////////////////////////////////////////////////////
@@ -1823,11 +1849,11 @@ void fgReInitSubsystems()
     globals->get_controls()->reset_all();
     globals->get_autopilot()->reset();
 
-    fgUpdateSunPos();
-    fgUpdateMoonPos();
-    cur_light_params.Update();
     fgUpdateLocalTime();
 
+    // re-init to proper time of day setting
+    fgInitTimeOffset();
+
     if ( !freeze ) {
         fgSetBool("/sim/freeze/master", false);
     }