]> git.mxchange.org Git - flightgear.git/blobdiff - src/AIModel/AIBase.cxx
#545: Fix ATC chatter sound settings being ignored
[flightgear.git] / src / AIModel / AIBase.cxx
index d2e2fe25dce9c7956b475377a00ff7c51d989738..07fc6c31a49c38f86883ec0ee03ffd0da94efb70 100644 (file)
@@ -145,12 +145,16 @@ FGAIBase::~FGAIBase() {
             model_removed->setStringValue(props->getPath());
     }
 
-    if (_refID != 0 && _refID !=  1) {
+    if (_fx && _refID != 0 && _refID !=  1) {
         SGSoundMgr *smgr = globals->get_soundmgr();
-        smgr->remove("aifx:"+_refID);
+        stringstream name; 
+        name <<  "aifx:";
+        name << _refID;
+        smgr->remove(name.str());
     }
 
-    delete fp;
+    if (fp)
+        delete fp;
     fp = 0;
 }
 
@@ -160,6 +164,9 @@ FGAIBase::~FGAIBase() {
 void
 FGAIBase::removeModel()
 {
+    if (!_model.valid())
+        return;
+
     FGScenery* pSceneryManager = globals->get_scenery();
     if (pSceneryManager)
     {
@@ -221,10 +228,11 @@ void FGAIBase::update(double dt) {
         _fx->set_orientation( orient );
 
         SGVec3d velocity;
-        velocity = SGVec3d( speed_north_deg_sec, speed_east_deg_sec, pitch*speed );
+        velocity = SGVec3d( speed_north_deg_sec, speed_east_deg_sec,
+                            pitch*speed );
         _fx->set_velocity( velocity );
     }
-    else if (_aimodel)
+    else if ((_aimodel)&&(fgGetBool("/sim/sound/aimodels/enabled",false)))
     {
         string fxpath = _aimodel->get_sound_path();
         if (fxpath != "")
@@ -234,7 +242,10 @@ void FGAIBase::update(double dt) {
 
             // initialize the sound configuration
             SGSoundMgr *smgr = globals->get_soundmgr();
-            _fx = new FGFX(smgr, "aifx:"+_refID, props);
+            stringstream name;
+            name <<  "aifx:";
+            name << _refID;
+            _fx = new FGFX(smgr, name.str(), props);
             _fx->init();
         }
     }
@@ -280,8 +291,14 @@ void FGAIBase::Transform() {
 
 }
 
-bool FGAIBase::init(bool search_in_AI_path) {
-    
+bool FGAIBase::init(bool search_in_AI_path)
+{
+    if (_model.valid())
+    {
+        SG_LOG(SG_AI, SG_ALERT, "AIBase: Cannot initialize a model multiple times! " << model_path);
+        return false;
+    }
+
     string f;
     if(search_in_AI_path)
     {
@@ -310,12 +327,6 @@ bool FGAIBase::init(bool search_in_AI_path) {
     _aimodel = new FGAIModelData(props);
     osg::Node * mdl = SGModelLib::loadDeferredModel(f, props, _aimodel);
 
-    if (_model.valid())
-    {
-        // reinit, dump the old model
-        removeModel();
-    }
-
     _model = new osg::LOD;
     _model->setName("AI-model range animation node");
 
@@ -485,6 +496,8 @@ void FGAIBase::unbind() {
 
     props->setBoolValue("/sim/controls/radar/", true);
 
+    // drop reference to sound effects now
+    _fx = 0;
 }
 
 double FGAIBase::UpdateRadar(FGAIManager* manager) {
@@ -912,10 +925,7 @@ void FGAIModelData::modelLoaded(const string& path, SGPropertyNode *prop, osg::N
 {
     const char* fxpath = prop->getStringValue("sound/path");
     if (fxpath) {
-        string sound_path = string(fxpath);
-        if (sound_path != "") {
-            _path = "/AI/"+sound_path;
-        }
+        _path = string(fxpath);
     }
     _nasal->modelLoaded(path, prop, n);
 }