From ebcc6359b97a64321226a8ed37cf1fce9d1c14d9 Mon Sep 17 00:00:00 2001
From: Erik Hofman <erik@ehofman.com>
Date: Fri, 25 Nov 2011 13:39:10 +0100
Subject: [PATCH] Initials sound support for AI models.

---
 src/AIModel/AIBase.cxx | 49 ++++++++++++++++++++++++++++++------------
 src/AIModel/AIBase.hxx | 24 +++++++++++++++++++++
 src/Sound/fg_fx.cxx    |  2 +-
 3 files changed, 60 insertions(+), 15 deletions(-)

diff --git a/src/AIModel/AIBase.cxx b/src/AIModel/AIBase.cxx
index 2d7edd550..52eaea041 100644
--- a/src/AIModel/AIBase.cxx
+++ b/src/AIModel/AIBase.cxx
@@ -24,6 +24,8 @@
 #  include <config.h>
 #endif
 
+#include <string.h>
+
 #include <simgear/compiler.h>
 
 #include <string>
@@ -72,6 +74,8 @@ FGAIBase::FGAIBase(object_type ot, bool enableHot) :
     _refID( _newAIModelID() ),
     _otype(ot),
     _initialized(false),
+    _aimodel(0),
+    _fxpath(""),
     _fx(0)
 
 {
@@ -139,11 +143,12 @@ FGAIBase::~FGAIBase() {
         if (parent)
             model_removed->setStringValue(props->getPath());
     }
-    delete _fx;
-    _fx = 0;
 
     delete fp;
     fp = 0;
+
+//  delete _fx;
+//  _fx = 0;
 }
 
 /** Cleanly remove the model
@@ -216,6 +221,20 @@ void FGAIBase::update(double dt) {
         velocity = SGVec3d( speed_north_deg_sec, speed_east_deg_sec, pitch*speed );
         _fx->set_velocity( velocity );
     }
+    else if (_aimodel)
+    {
+        string fxpath = _aimodel->get_sound_path();
+        if (fxpath != "")
+        {
+            _fxpath = fxpath;
+            props->setStringValue("sim/sound/path", _fxpath.c_str());
+
+            // initialize the sound configuration
+            SGSoundMgr *smgr = globals->get_soundmgr();
+            _fx = new FGFX(smgr, "aifx:"+_name+"-"+_callsign, props);
+            _fx->init();
+        }
+    }
 }
 
 /** update LOD properties of the model */
@@ -285,7 +304,8 @@ bool FGAIBase::init(bool search_in_AI_path) {
     else
         _installed = true;
 
-    osg::Node * mdl = SGModelLib::loadDeferredModel(f, props, new FGNasalModelData(props));
+    _aimodel = new FGAIModelData(props);
+    osg::Node * mdl = SGModelLib::loadDeferredModel(f, props, _aimodel);
 
     if (_model.valid())
     {
@@ -314,17 +334,6 @@ bool FGAIBase::init(bool search_in_AI_path) {
 
         // Get the sound-path tag from the configuration file and store it
         // in the property tree.
-        string fxpath = props->getStringValue("sim/sound/path");
-        if ( !fxpath.empty() )
-        {
-            props->setStringValue("sim/sound/path", fxpath.c_str());
-
-            // initialize the sound configuration
-            SGSoundMgr *smgr = globals->get_soundmgr();
-            _fx = new FGFX(smgr, "aifx:"+f, props);
-            _fx->init();
-        }
-
         _initialized = true;
 
     } else if (!model_path.empty()) {
@@ -884,3 +893,15 @@ int FGAIBase::_newAIModelID() {
     return id;
 }
 
+
+void FGAIModelData::modelLoaded(const string& path, SGPropertyNode *prop, osg::Node *n)
+{
+    const char* fxpath = prop->getStringValue("sound/path");
+    if (fxpath) {
+        string sound_path = string(fxpath);
+        if (sound_path != "") {
+            _path = "/AI/"+sound_path;
+        }
+    }
+    _nasal->modelLoaded(path, prop, n);
+}
diff --git a/src/AIModel/AIBase.hxx b/src/AIModel/AIBase.hxx
index 726ce303b..9990387f5 100644
--- a/src/AIModel/AIBase.hxx
+++ b/src/AIModel/AIBase.hxx
@@ -34,6 +34,7 @@
 #include <simgear/math/sg_geodesy.hxx>
 
 
+#include <Scripting/NasalSys.hxx>
 #include <Main/fg_props.hxx>
 
 
@@ -44,6 +45,8 @@ class SGMaterial;
 class FGAIManager;
 class FGAIFlightPlan;
 class FGFX;
+class FGAIModelData;	// define dblow
+
 
 class FGAIBase : public SGReferenced {
 
@@ -226,6 +229,10 @@ private:
     object_type _otype;
     bool _initialized;
     osg::ref_ptr<osg::LOD> _model; //The 3D model LOD object
+
+    FGAIModelData* _aimodel;
+
+    string _fxpath;
     SGSharedPtr<FGFX>  _fx;
 
 public:
@@ -441,4 +448,21 @@ inline void FGAIBase::setMaxSpeed(double m) {
     _max_speed = m;
 }
 
+
+class FGAIModelData : public simgear::SGModelData {
+public:
+    FGAIModelData(SGPropertyNode *root = 0)
+       : _nasal( new FGNasalModelData(root) ),
+         _path("") {};
+    ~FGAIModelData() {
+        delete _nasal;
+    };
+    void modelLoaded(const string& path, SGPropertyNode *prop, osg::Node *n);
+    inline string& get_sound_path() { return _path; };
+
+private:
+    FGNasalModelData *_nasal;
+    string _path;
+};
+
 #endif	// _FG_AIBASE_HXX
diff --git a/src/Sound/fg_fx.cxx b/src/Sound/fg_fx.cxx
index 7aad56b0b..c8c6b49f3 100644
--- a/src/Sound/fg_fx.cxx
+++ b/src/Sound/fg_fx.cxx
@@ -79,7 +79,7 @@ FGFX::init()
 
     string path_str = node->getStringValue("path");
     if (path_str.empty()) {
-        SG_LOG(SG_SOUND, SG_ALERT, "No path in /sim/sound/path");
+        SG_LOG(SG_SOUND, SG_ALERT, "No path in sim/sound/path");
         return;
     }