Revision: 2490
          http://rigsofrods.svn.sourceforge.net/rigsofrods/?rev=2490&view=rev
Author:   ulteq
Date:     2012-05-02 05:22:51 +0000 (Wed, 02 May 2012)
Log Message:
-----------
-Cleanup

Modified Paths:
--------------
    trunk/source/main/audio/SoundScriptManager.cpp
    trunk/source/main/audio/SoundScriptManager.h

Modified: trunk/source/main/audio/SoundScriptManager.cpp
===================================================================
--- trunk/source/main/audio/SoundScriptManager.cpp      2012-05-02 05:08:50 UTC 
(rev 2489)
+++ trunk/source/main/audio/SoundScriptManager.cpp      2012-05-02 05:22:51 UTC 
(rev 2490)
@@ -46,24 +46,27 @@
 {
        for (int i=0; i < SS_MAX_TRIG; i++)
        {
-               free_trigs[i]=0;
+               free_trigs[i] = 0;
        }
+
        for (int i=0; i < SS_MAX_MOD; i++)
        {
-               free_pitches[i]=0;
-               free_gains[i]=0;
+               free_pitches[i] = 0;
+               free_gains[i]   = 0;
        }
 
        // TODO: there is a memory corruption going on here, need to fix
-       for(int i=0; i < SS_MAX_TRIG * MAX_INSTANCES_PER_GROUP; i++)
+       for (int i=0; i < SS_MAX_TRIG * MAX_INSTANCES_PER_GROUP; i++)
+       {
                trigs[i] = 0;
+       }
 
-       for(int i=0; i < SS_MAX_MOD * MAX_INSTANCES_PER_GROUP; i++)
+       for (int i=0; i < SS_MAX_MOD * MAX_INSTANCES_PER_GROUP; i++)
+       {
                pitches[i] = 0;
+               gains[i]   = 0;
+       }
 
-       for(int i=0; i < SS_MAX_MOD * MAX_INSTANCES_PER_GROUP; i++)
-               gains[i] = 0;
-
        // reset all states
        statemap.clear();
 
@@ -77,72 +80,100 @@
 
 void SoundScriptManager::trigOnce(Beam *truck, int trig, int linkType, int 
linkItemID)
 {
-       if(soundsDisabled) return;
-       if(truck) trigOnce(truck->trucknum, trig, linkType, linkItemID);
+       if (soundsDisabled) return;
+
+       if (truck)
+       {
+               trigOnce(truck->trucknum, trig, linkType, linkItemID);
+       }
 }
 
 void SoundScriptManager::trigOnce(int truck, int trig, int linkType, int 
linkItemID)
 {
-       if(soundsDisabled) return;
-       for (int i=0; i<free_trigs[trig]; i++)
+       if (soundsDisabled) return;
+
+       for (int i=0; i < free_trigs[trig]; i++)
        {
-               SoundScriptInstance* inst=trigs[trig+i*SS_MAX_TRIG];
+               // cycle through all instance groups
+               SoundScriptInstance* inst = trigs[trig+i*SS_MAX_TRIG];
+
                if (inst->truck == truck && inst->soundLinkType == linkType && 
inst->soundLinkItemId == linkItemID)
+               {
                        inst->runOnce();
+               }
        }
 }
 
 void SoundScriptManager::trigStart(Beam *truck, int trig, int linkType, int 
linkItemID)
 {
-       if(soundsDisabled) return;
-       if(truck) trigStart(truck->trucknum, trig, linkType, linkItemID);
+       if (soundsDisabled) return;
+
+       if (truck)
+       {
+               trigStart(truck->trucknum, trig, linkType, linkItemID);
+       }
 }
 
 void SoundScriptManager::trigStart(int truck, int trig, int linkType, int 
linkItemID)
 {
-       if(soundsDisabled) return;
+       if (soundsDisabled) return;
        if (getTrigState(truck, trig, linkType, linkItemID)) return;
 
        statemap[linkType][linkItemID][truck][trig] = true;
 
-       for (int i=0; i<free_trigs[trig]; i++)
+       for (int i=0; i < free_trigs[trig]; i++)
        {
                SoundScriptInstance* inst=trigs[trig+i*SS_MAX_TRIG];
+
                if (inst->truck == truck && inst->soundLinkType == linkType && 
inst->soundLinkItemId == linkItemID)
+               {
                        inst->start();
+               }
        }
 }
 
 void SoundScriptManager::trigStop(Beam *truck, int trig, int linkType, int 
linkItemID)
 {
-       if(soundsDisabled) return;
-       if(truck) trigStop(truck->trucknum, trig, linkType, linkItemID);
+       if (soundsDisabled) return;
+
+       if (truck)
+       {
+               trigStop(truck->trucknum, trig, linkType, linkItemID);
+       }
 }
 
 void SoundScriptManager::trigStop(int truck, int trig, int linkType, int 
linkItemID)
 {
-       if(soundsDisabled) return;
+       if (soundsDisabled) return;
        if (!getTrigState(truck, trig, linkType, linkItemID)) return;
        
        statemap[linkType][linkItemID][truck][trig] = false;
 
-       for (int i=0; i<free_trigs[trig]; i++)
+       for (int i=0; i < free_trigs[trig]; i++)
        {
                SoundScriptInstance* inst=trigs[trig+i*SS_MAX_TRIG];
+
                if (inst->truck == truck && inst->soundLinkType == linkType && 
inst->soundLinkItemId == linkItemID)
+               {
                        inst->stop();
+               }
        }
 }
 
 void SoundScriptManager::trigToggle(Beam *truck, int trig, int linkType, int 
linkItemID)
 {
-       if(soundsDisabled) return;
-       if(truck) trigToggle(truck->trucknum, trig, linkType, linkItemID);
+       if (soundsDisabled) return;
+       
+       if (truck)
+       {
+               trigToggle(truck->trucknum, trig, linkType, linkItemID);
+       }
 }
 
 void SoundScriptManager::trigToggle(int truck, int trig, int linkType, int 
linkItemID)
 {
-       if(soundsDisabled) return;
+       if (soundsDisabled) return;
+
        if (getTrigState(truck, trig, linkType, linkItemID))
                trigStop(truck, trig, linkType, linkItemID);
        else
@@ -151,48 +182,59 @@
 
 bool SoundScriptManager::getTrigState(Beam *truck, int trig, int linkType, int 
linkItemID)
 {
-       if(soundsDisabled) return false;
-       if(truck) return getTrigState(truck->trucknum, trig, linkType, 
linkItemID);
-       return false;
+       if (soundsDisabled) return false;
+
+       if (truck)
+               return getTrigState(truck->trucknum, trig, linkType, 
linkItemID);
+       else
+               return false;
 }
 
 bool SoundScriptManager::getTrigState(int truck, int trig, int linkType, int 
linkItemID)
 {
-       if(soundsDisabled) return false;
+       if (soundsDisabled) return false;
+
        return statemap[linkType][linkItemID][truck][trig];
 }
 
 void SoundScriptManager::modulate(Beam *truck, int mod, float value, int 
linkType, int linkItemID)
 {
-       if(soundsDisabled) return;
-       if(truck) modulate(truck->trucknum, mod, value, linkType, linkItemID);
+       if (soundsDisabled) return;
+
+       if (truck)
+       {
+               modulate(truck->trucknum, mod, value, linkType, linkItemID);
+       }
 }
 
+// TODO: Fix this! Produces crash on trainvalley with henschel train.
 void SoundScriptManager::modulate(int truck, int mod, float value, int 
linkType, int linkItemID)
 {
-       if(soundsDisabled) return;
-       for (int i=0; i<free_gains[mod]; i++)
+       if (soundsDisabled) return;
+
+       for (int i=0; i < free_gains[mod]; i++)
        {
-               SoundScriptInstance* inst=gains[mod+i*SS_MAX_MOD];
-               if(!inst) continue;
+               SoundScriptInstance* inst = gains[mod + i * SS_MAX_MOD];
+               if (!inst) continue;
                if (inst->truck == truck && inst->soundLinkType == linkType && 
inst->soundLinkItemId == linkItemID)
                {
-                       //this one requires modulation
+                       // this one requires modulation
                        float 
gain=value*value*inst->templ->gain_square+value*inst->templ->gain_multiplier+inst->templ->gain_offset;
-                       if (gain<0.0) gain=0.0;
-                       if (gain>1.0) gain=1.0;
+                       gain = std::max(0.0f, gain);
+                       gain = std::min(gain, 1.0f);
                        inst->setGain(gain);
                }
        }
+
        for (int i=0; i<free_pitches[mod]; i++)
        {
                SoundScriptInstance* inst=pitches[mod+i*SS_MAX_MOD];
-               if(!inst) continue;
+               if (!inst) continue;
                if (inst->truck == truck && inst->soundLinkType == linkType && 
inst->soundLinkItemId == linkItemID)
                {
-                       //this one requires modulation
+                       // this one requires modulation
                        float 
pitch=value*value*inst->templ->pitch_square+value*inst->templ->pitch_multiplier+inst->templ->pitch_offset;
-                       if (pitch<0) pitch=0;
+                       pitch = std::max(0.0f, pitch);
                        inst->setPitch(pitch);
                }
        }
@@ -200,7 +242,7 @@
 
 void SoundScriptManager::setCamera(Vector3 position, Vector3 direction, 
Vector3 up, Vector3 velocity)
 {
-       if(soundsDisabled) return;
+       if (soundsDisabled) return;
        sm->setCamera(position, direction, up, velocity);
 }
 
@@ -211,17 +253,16 @@
 
 Real SoundScriptManager::getLoadingOrder(void) const
 {
-    /// Load late
+    // load late
     return 1000.0f;
 }
 
 SoundScriptTemplate* SoundScriptManager::createTemplate(String name, String 
groupname, String filename)
 {
-       //first, search if there is a template name collision
-       if(templates.find(name) != templates.end())
+       // first, search if there is a template name collision
+       if (templates.find(name) != templates.end())
        {
-               OGRE_EXCEPT(Exception::ERR_DUPLICATE_ITEM, "SoundScript with 
the name " + name +
-            " already exists.", "SoundScriptManager::createTemplate");
+               OGRE_EXCEPT(Exception::ERR_DUPLICATE_ITEM, "SoundScript with 
the name " + name + " already exists.", "SoundScriptManager::createTemplate");
                return NULL;
        }
 
@@ -232,11 +273,13 @@
 
 void SoundScriptManager::unloadResourceGroup(String groupname)
 {
-       //first, search if there is a template name collision
+       // first, search if there is a template name collision
        for(std::map<Ogre::String, SoundScriptTemplate*>::iterator it = 
templates.begin(); it!=templates.end();)
        {
-               if (it->second->groupname == groupname)
+               if (it->second && it->second->groupname == groupname)
                {
+                       delete(it->second);
+                       it->second = 0;
                        templates.erase(it++);
                } else
                {
@@ -248,12 +291,13 @@
 void SoundScriptManager::clearNonBaseTemplates()
 {
        int counter = 0;
-       for(std::map<Ogre::String, SoundScriptTemplate*>::iterator it = 
templates.begin(); it!=templates.end();)
+
+       for(std::map<Ogre::String, SoundScriptTemplate*>::iterator it = 
templates.begin(); it != templates.end();)
        {
                if (it->second && !it->second->baseTemplate)
                {
                        delete(it->second);
-                       it->second=0;
+                       it->second = 0;
                        templates.erase(it++);
                        counter++;
                } else
@@ -261,91 +305,111 @@
                        ++it;
                }
        }
-       if(counter>0)
+
+       if (counter > 0)
+       {
                LOG("SoundScriptManager: removed " + TOSTRING(counter) + " 
non-base templates");
+       }
 }
 
 SoundScriptInstance* SoundScriptManager::createInstance(Ogre::String 
templatename, int truck, Ogre::SceneNode *toAttach, int soundLinkType, int 
soundLinkItemId)
 {
        //first, search template
-       SoundScriptTemplate* templ=NULL;
+       SoundScriptTemplate* templ = NULL;
 
-       if(templates.find(templatename) == templates.end())
-               // no template with that name found
-               return NULL;
+       if (templates.find(templatename) == templates.end())
+       {
+               return NULL; // found no template with this name
+       }
+
        templ = templates[templatename];
-       if (templ->trigger_source==SS_TRIG_NONE) return NULL; //invalid 
template!
-       //ok create instance
-       SoundScriptInstance* inst=new SoundScriptInstance(truck, templ, sm, 
templ->filename+"-"+TOSTRING(truck)+"-"+TOSTRING(instance_counter), 
soundLinkType, soundLinkItemId);
+
+       if (templ->trigger_source == SS_TRIG_NONE)
+       {
+               return NULL; // invalid template!
+       }
+
+       if (   free_trigs[templ->trigger_source]   >= MAX_INSTANCES_PER_GROUP
+               || free_gains[templ->trigger_source]   >= 
MAX_INSTANCES_PER_GROUP && templ->gain_source  != SS_MOD_NONE
+               || free_pitches[templ->trigger_source] >= 
MAX_INSTANCES_PER_GROUP && templ->pitch_source != SS_MOD_NONE )
+       {
+               LOG("SoundScriptManager: Reached MAX_INSTANCES_PER_GROUP limit 
(" + TOSTRING(MAX_INSTANCES_PER_GROUP) + ")");
+               return NULL; // reached limit!
+       }
+
+       SoundScriptInstance* inst = new SoundScriptInstance(truck, templ, sm, 
templ->filename+"-"+TOSTRING(truck)+"-"+TOSTRING(instance_counter), 
soundLinkType, soundLinkItemId);
        instance_counter++;
-       //register to lookup tables
-       
trigs[templ->trigger_source+free_trigs[templ->trigger_source]*SS_MAX_TRIG]=inst;
-       free_trigs[templ->trigger_source]=free_trigs[templ->trigger_source]+1;
-       if (templ->gain_source!=SS_MOD_NONE)
+
+       // register to lookup tables
+       trigs[templ->trigger_source + free_trigs[templ->trigger_source] * 
SS_MAX_TRIG] = inst;
+       free_trigs[templ->trigger_source]++;
+
+       if (templ->gain_source != SS_MOD_NONE)
        {
-               
gains[templ->gain_source+free_gains[templ->gain_source]*SS_MAX_MOD]=inst;
-               free_gains[templ->gain_source]=free_gains[templ->gain_source]+1;
+               gains[templ->gain_source + free_gains[templ->gain_source] * 
SS_MAX_MOD] = inst;
+               free_gains[templ->gain_source]++;
        }
-       if (templ->pitch_source!=SS_MOD_NONE)
+       if (templ->pitch_source != SS_MOD_NONE)
        {
-               
pitches[templ->pitch_source+free_pitches[templ->pitch_source]*SS_MAX_MOD]=inst;
-               
free_pitches[templ->pitch_source]=free_pitches[templ->pitch_source]+1;
+               pitches[templ->pitch_source + free_pitches[templ->pitch_source] 
* SS_MAX_MOD] = inst;
+               free_pitches[templ->pitch_source]++;
        }
-       //for always on
-       if (templ->trigger_source==SS_TRIG_ALWAYSON) inst->start();
+
+       // SoundTrigger: SS_TRIG_ALWAYSON
+       if (templ->trigger_source == SS_TRIG_ALWAYSON)
+       {
+               inst->start();
+       }
+
        return inst;
 }
 
 void SoundScriptManager::parseScript(DataStreamPtr& stream, const String& 
groupName)
 {
-       String line;
-       SoundScriptTemplate* sst;
+       SoundScriptTemplate* sst = 0;
+       String line = "";
        std::vector<String> vecparams;
 
        LOG("SoundScriptManager: Parsing script "+stream->getName());
-       sst = 0;
 
        while(!stream->eof())
        {
                line = stream->getLine();
-               // Ignore comments & blanks
+               // ignore comments & blanks
                if (!(line.length() == 0 || line.substr(0,2) == "//"))
                {
                        if (sst == 0)
                        {
-                               // No current SoundScript
-                               // So first valid data should be a SoundScript 
name
+                               // no current SoundScript
+                               // so first valid data should be a SoundScript 
name
                                LOG("SoundScriptManager: creating template 
"+line);
                                sst = createTemplate(line, groupName, 
stream->getName());
                                if (!sst)
                                {
-                                       //there is a name collision for this 
Sound Script
+                                       // there is a name collision for this 
Sound Script
                                        LOG("SoundScriptManager: Error, this 
sound script is already defined: "+line);
                                        skipToNextOpenBrace(stream);
                                        skipToNextCloseBrace(stream);
                                        continue;
                                }
-                               // Skip to and over next {
+                               // skip to and over next {
                                skipToNextOpenBrace(stream);
-                       }
-                       else
+                       } else
                        {
-                               // Already in a ss
+                               // already in a ss
                                if (line == "}")
                                {
-                                       // Finished ss
+                                       // finished ss
                                        sst = 0;
-                               }
-                               else
+                               } else
                                {
-                                       // Attribute
-                                       // Split params on space
+                                       // attribute
+                                       // split params on space
                                        Ogre::StringVector veclineparams = 
StringUtil::split(line, "\t ", 0);
 
                                        if (!sst->setParameter(veclineparams))
                                        {
-                                               LOG("Bad SoundScript attribute 
line: '"
-                                                       + line + "' in " + 
stream->getName());
+                                               LOG("Bad SoundScript attribute 
line: '" + line + "' in " + stream->getName());
                                        }
                                }
                        }
@@ -356,7 +420,8 @@
 
 void SoundScriptManager::skipToNextCloseBrace(DataStreamPtr& stream)
 {
-       String line;
+       String line = "";
+
        while (!stream->eof() && line != "}")
        {
                line = stream->getLine();
@@ -365,7 +430,8 @@
 
 void SoundScriptManager::skipToNextOpenBrace(DataStreamPtr& stream)
 {
-       String line;
+       String line = "";
+
        while (!stream->eof() && line != "{")
        {
                line = stream->getLine();
@@ -382,196 +448,226 @@
 
 //=====================================================================
 
-SoundScriptTemplate::SoundScriptTemplate(String name, String groupname, String 
filename, bool _baseTemplate)
+SoundScriptTemplate::SoundScriptTemplate(String name, String groupname, String 
filename, bool _baseTemplate) :
+         baseTemplate(_baseTemplate)
+       , filename(filename)
+       , free_sound(0)
+       , gain_multiplier(1.0f)
+       , gain_offset(0.0f)
+       , gain_source(SS_MOD_NONE)
+       , gain_square(0.0f)
+       , groupname(groupname)
+       , has_start_sound(false)
+       , has_stop_sound(false)
+       , name(name)
+       , pitch_multiplier(1.0f)
+       , pitch_offset(0.0f)
+       , pitch_source(SS_MOD_NONE)
+       , pitch_square(0.0f)
+       , start_sound_pitch(0.0f)
+       , stop_sound_pitch(0.0f)
+       , trigger_source(SS_TRIG_NONE)
+       , unpitchable(false)
 {
-       this->name=name;
-       this->groupname=groupname;
-       this->filename=filename;
-       trigger_source=SS_TRIG_NONE;
-       pitch_source=SS_MOD_NONE;
-       gain_source=SS_MOD_NONE;
-       start_sound_pitch=0;
-       has_start_sound=false;
-       stop_sound_pitch=0;
-       has_stop_sound=false;
-       unpitchable=false;
-       free_sound=0;
-       pitch_multiplier=1;
-       pitch_square=0;
-       pitch_offset=0;
-       gain_multiplier=1;
-       gain_square=0;
-       gain_offset=0;
-       baseTemplate = _baseTemplate;
 }
 
 bool SoundScriptTemplate::setParameter(Ogre::StringVector vec)
 {
-//     for (int i=0; i<vec.size(); i++) LOG("SoundScriptManager: Parsing line 
'"+vec[i]+"'");
+       if (vec.empty()) return false;
 
-       if (vec.size()<1) return false;
-       if (vec[0]==String("trigger_source"))
+       if (vec[0] == String("trigger_source"))
        {
-               if (vec.size()<2) return false;
-               if (vec[1]==String("engine")) 
{trigger_source=SS_TRIG_ENGINE;return true;};
-               if (vec[1]==String("aeroengine1")) 
{trigger_source=SS_TRIG_AEROENGINE1;return true;};
-               if (vec[1]==String("aeroengine2")) 
{trigger_source=SS_TRIG_AEROENGINE2;return true;};
-               if (vec[1]==String("aeroengine3")) 
{trigger_source=SS_TRIG_AEROENGINE3;return true;};
-               if (vec[1]==String("aeroengine4")) 
{trigger_source=SS_TRIG_AEROENGINE4;return true;};
-               if (vec[1]==String("aeroengine5")) 
{trigger_source=SS_TRIG_AEROENGINE5;return true;};
-               if (vec[1]==String("aeroengine6")) 
{trigger_source=SS_TRIG_AEROENGINE6;return true;};
-               if (vec[1]==String("aeroengine7")) 
{trigger_source=SS_TRIG_AEROENGINE7;return true;};
-               if (vec[1]==String("aeroengine8")) 
{trigger_source=SS_TRIG_AEROENGINE8;return true;};
-               if (vec[1]==String("horn")) {trigger_source=SS_TRIG_HORN;return 
true;};
-               if (vec[1]==String("brake")) 
{trigger_source=SS_TRIG_BRAKE;return true;};
-               if (vec[1]==String("pump")) {trigger_source=SS_TRIG_PUMP;return 
true;};
-               if (vec[1]==String("starter")) 
{trigger_source=SS_TRIG_STARTER;return true;};
-               if (vec[1]==String("always_on")) 
{trigger_source=SS_TRIG_ALWAYSON;return true;};
-               if (vec[1]==String("repair")) 
{trigger_source=SS_TRIG_REPAIR;return true;};
-               if (vec[1]==String("air")) {trigger_source=SS_TRIG_AIR;return 
true;};
-               if (vec[1]==String("gpws_ap_disconnect")) 
{trigger_source=SS_TRIG_GPWS_APDISCONNECT;return true;};
-               if (vec[1]==String("gpws_10")) 
{trigger_source=SS_TRIG_GPWS_10;return true;};
-               if (vec[1]==String("gpws_20")) 
{trigger_source=SS_TRIG_GPWS_20;return true;};
-               if (vec[1]==String("gpws_30")) 
{trigger_source=SS_TRIG_GPWS_30;return true;};
-               if (vec[1]==String("gpws_40")) 
{trigger_source=SS_TRIG_GPWS_40;return true;};
-               if (vec[1]==String("gpws_50")) 
{trigger_source=SS_TRIG_GPWS_50;return true;};
-               if (vec[1]==String("gpws_100")) 
{trigger_source=SS_TRIG_GPWS_100;return true;};
-               if (vec[1]==String("gpws_pull_up")) 
{trigger_source=SS_TRIG_GPWS_PULLUP;return true;};
-               if (vec[1]==String("gpws_minimums")) 
{trigger_source=SS_TRIG_GPWS_MINIMUMS;return true;};
-               if (vec[1]==String("air_purge")) 
{trigger_source=SS_TRIG_AIR_PURGE;return true;};
-               if (vec[1]==String("shift")) 
{trigger_source=SS_TRIG_SHIFT;return true;};
-               if (vec[1]==String("gear_slide")) 
{trigger_source=SS_TRIG_GEARSLIDE;return true;};
-               if (vec[1]==String("creak") && BSETTING("Creak Sound", false)) 
{trigger_source=SS_TRIG_CREAK;return true;};
-               if (vec[1]==String("break")) 
{trigger_source=SS_TRIG_BREAK;return true;};
-               if (vec[1]==String("screetch")) 
{trigger_source=SS_TRIG_SCREETCH;return true;};
-               if (vec[1]==String("parking_brake")) 
{trigger_source=SS_TRIG_PARK;return true;};
-               if (vec[1]==String("antilock")) 
{trigger_source=SS_TRIG_ALB_ACTIVE;return true;};
-               if (vec[1]==String("tractioncontrol")) 
{trigger_source=SS_TRIG_TC_ACTIVE;return true;};
-               if (vec[1]==String("afterburner1")) 
{trigger_source=SS_TRIG_AFTERBURNER1;return true;};
-               if (vec[1]==String("afterburner2")) 
{trigger_source=SS_TRIG_AFTERBURNER2;return true;};
-               if (vec[1]==String("afterburner3")) 
{trigger_source=SS_TRIG_AFTERBURNER3;return true;};
-               if (vec[1]==String("afterburner4")) 
{trigger_source=SS_TRIG_AFTERBURNER4;return true;};
-               if (vec[1]==String("afterburner5")) 
{trigger_source=SS_TRIG_AFTERBURNER5;return true;};
-               if (vec[1]==String("afterburner6")) 
{trigger_source=SS_TRIG_AFTERBURNER6;return true;};
-               if (vec[1]==String("afterburner7")) 
{trigger_source=SS_TRIG_AFTERBURNER7;return true;};
-               if (vec[1]==String("afterburner8")) 
{trigger_source=SS_TRIG_AFTERBURNER8;return true;};
-               if (vec[1]==String("avionic_chat_01")) 
{trigger_source=SS_TRIG_AVICHAT01;return true;};
-               if (vec[1]==String("avionic_chat_02")) 
{trigger_source=SS_TRIG_AVICHAT02;return true;};
-               if (vec[1]==String("avionic_chat_03")) 
{trigger_source=SS_TRIG_AVICHAT03;return true;};
-               if (vec[1]==String("avionic_chat_04")) 
{trigger_source=SS_TRIG_AVICHAT04;return true;};
-               if (vec[1]==String("avionic_chat_05")) 
{trigger_source=SS_TRIG_AVICHAT05;return true;};
-               if (vec[1]==String("avionic_chat_06")) 
{trigger_source=SS_TRIG_AVICHAT06;return true;};
-               if (vec[1]==String("avionic_chat_07")) 
{trigger_source=SS_TRIG_AVICHAT07;return true;};
-               if (vec[1]==String("avionic_chat_08")) 
{trigger_source=SS_TRIG_AVICHAT08;return true;};
-               if (vec[1]==String("avionic_chat_09")) 
{trigger_source=SS_TRIG_AVICHAT09;return true;};
-               if (vec[1]==String("avionic_chat_10")) 
{trigger_source=SS_TRIG_AVICHAT10;return true;};
-               if (vec[1]==String("avionic_chat_11")) 
{trigger_source=SS_TRIG_AVICHAT11;return true;};
-               if (vec[1]==String("avionic_chat_12")) 
{trigger_source=SS_TRIG_AVICHAT12;return true;};
-               if (vec[1]==String("avionic_chat_13")) 
{trigger_source=SS_TRIG_AVICHAT13;return true;};
-               if (vec[1]==String("aoa_horn")) 
{trigger_source=SS_TRIG_AOA;return true;};
-               if (vec[1]==String("ignition")) 
{trigger_source=SS_TRIG_IGNITION;return true;};
-               if (vec[1]==String("reverse_gear")) 
{trigger_source=SS_TRIG_REVERSE_GEAR;return true;};
-               if (vec[1]==String("turn_signal")) 
{trigger_source=SS_TRIG_TURN_SIGNAL;return true;};
-               if (vec[1]==String("turn_signal_tick")) 
{trigger_source=SS_TRIG_TURN_SIGNAL_TICK;return true;};
-               if (vec[1]==String("turn_signal_warn_tick")) 
{trigger_source=SS_TRIG_TURN_SIGNAL_WARN_TICK;return true;};
-               if (vec[1]==String("linked_command")) 
{trigger_source=SS_LINKED_COMMAND;return true;};
+               if (vec.size() < 2) return false;
+               if (vec[1] == String("engine")) {trigger_source=SS_TRIG_ENGINE; 
return true;};
+               if (vec[1] == String("aeroengine1")) 
{trigger_source=SS_TRIG_AEROENGINE1; return true;};
+               if (vec[1] == String("aeroengine2")) 
{trigger_source=SS_TRIG_AEROENGINE2; return true;};
+               if (vec[1] == String("aeroengine3")) 
{trigger_source=SS_TRIG_AEROENGINE3; return true;};
+               if (vec[1] == String("aeroengine4")) 
{trigger_source=SS_TRIG_AEROENGINE4; return true;};
+               if (vec[1] == String("aeroengine5")) 
{trigger_source=SS_TRIG_AEROENGINE5; return true;};
+               if (vec[1] == String("aeroengine6")) 
{trigger_source=SS_TRIG_AEROENGINE6; return true;};
+               if (vec[1] == String("aeroengine7")) 
{trigger_source=SS_TRIG_AEROENGINE7; return true;};
+               if (vec[1] == String("aeroengine8")) 
{trigger_source=SS_TRIG_AEROENGINE8; return true;};
+               if (vec[1] == String("horn")) {trigger_source=SS_TRIG_HORN; 
return true;};
+               if (vec[1] == String("brake")) {trigger_source=SS_TRIG_BRAKE; 
return true;};
+               if (vec[1] == String("pump")) {trigger_source=SS_TRIG_PUMP; 
return true;};
+               if (vec[1] == String("starter")) 
{trigger_source=SS_TRIG_STARTER; return true;};
+               if (vec[1] == String("always_on")) 
{trigger_source=SS_TRIG_ALWAYSON; return true;};
+               if (vec[1] == String("repair")) {trigger_source=SS_TRIG_REPAIR; 
return true;};
+               if (vec[1] == String("air")) {trigger_source=SS_TRIG_AIR; 
return true;};
+               if (vec[1] == String("gpws_ap_disconnect")) 
{trigger_source=SS_TRIG_GPWS_APDISCONNECT; return true;};
+               if (vec[1] == String("gpws_10")) 
{trigger_source=SS_TRIG_GPWS_10; return true;};
+               if (vec[1] == String("gpws_20")) 
{trigger_source=SS_TRIG_GPWS_20; return true;};
+               if (vec[1] == String("gpws_30")) 
{trigger_source=SS_TRIG_GPWS_30; return true;};
+               if (vec[1] == String("gpws_40")) 
{trigger_source=SS_TRIG_GPWS_40; return true;};
+               if (vec[1] == String("gpws_50")) 
{trigger_source=SS_TRIG_GPWS_50; return true;};
+               if (vec[1] == String("gpws_100")) 
{trigger_source=SS_TRIG_GPWS_100; return true;};
+               if (vec[1] == String("gpws_pull_up")) 
{trigger_source=SS_TRIG_GPWS_PULLUP; return true;};
+               if (vec[1] == String("gpws_minimums")) 
{trigger_source=SS_TRIG_GPWS_MINIMUMS; return true;};
+               if (vec[1] == String("air_purge")) 
{trigger_source=SS_TRIG_AIR_PURGE; return true;};
+               if (vec[1] == String("shift")) {trigger_source=SS_TRIG_SHIFT; 
return true;};
+               if (vec[1] == String("gear_slide")) 
{trigger_source=SS_TRIG_GEARSLIDE; return true;};
+               if (vec[1] == String("creak") && BSETTING("Creak Sound", 
false)) {trigger_source=SS_TRIG_CREAK; return true;};
+               if (vec[1] == String("break")) {trigger_source=SS_TRIG_BREAK; 
return true;};
+               if (vec[1] == String("screetch")) 
{trigger_source=SS_TRIG_SCREETCH; return true;};
+               if (vec[1] == String("parking_brake")) 
{trigger_source=SS_TRIG_PARK; return true;};
+               if (vec[1] == String("antilock")) 
{trigger_source=SS_TRIG_ALB_ACTIVE; return true;};
+               if (vec[1] == String("tractioncontrol")) 
{trigger_source=SS_TRIG_TC_ACTIVE; return true;};
+               if (vec[1] == String("afterburner1")) 
{trigger_source=SS_TRIG_AFTERBURNER1; return true;};
+               if (vec[1] == String("afterburner2")) 
{trigger_source=SS_TRIG_AFTERBURNER2; return true;};
+               if (vec[1] == String("afterburner3")) 
{trigger_source=SS_TRIG_AFTERBURNER3; return true;};
+               if (vec[1] == String("afterburner4")) 
{trigger_source=SS_TRIG_AFTERBURNER4; return true;};
+               if (vec[1] == String("afterburner5")) 
{trigger_source=SS_TRIG_AFTERBURNER5; return true;};
+               if (vec[1] == String("afterburner6")) 
{trigger_source=SS_TRIG_AFTERBURNER6; return true;};
+               if (vec[1] == String("afterburner7")) 
{trigger_source=SS_TRIG_AFTERBURNER7; return true;};
+               if (vec[1] == String("afterburner8")) 
{trigger_source=SS_TRIG_AFTERBURNER8; return true;};
+               if (vec[1] == String("avionic_chat_01")) 
{trigger_source=SS_TRIG_AVICHAT01; return true;};
+               if (vec[1] == String("avionic_chat_02")) 
{trigger_source=SS_TRIG_AVICHAT02; return true;};
+               if (vec[1] == String("avionic_chat_03")) 
{trigger_source=SS_TRIG_AVICHAT03; return true;};
+               if (vec[1] == String("avionic_chat_04")) 
{trigger_source=SS_TRIG_AVICHAT04; return true;};
+               if (vec[1] == String("avionic_chat_05")) 
{trigger_source=SS_TRIG_AVICHAT05; return true;};
+               if (vec[1] == String("avionic_chat_06")) 
{trigger_source=SS_TRIG_AVICHAT06; return true;};
+               if (vec[1] == String("avionic_chat_07")) 
{trigger_source=SS_TRIG_AVICHAT07; return true;};
+               if (vec[1] == String("avionic_chat_08")) 
{trigger_source=SS_TRIG_AVICHAT08; return true;};
+               if (vec[1] == String("avionic_chat_09")) 
{trigger_source=SS_TRIG_AVICHAT09; return true;};
+               if (vec[1] == String("avionic_chat_10")) 
{trigger_source=SS_TRIG_AVICHAT10; return true;};
+               if (vec[1] == String("avionic_chat_11")) 
{trigger_source=SS_TRIG_AVICHAT11; return true;};
+               if (vec[1] == String("avionic_chat_12")) 
{trigger_source=SS_TRIG_AVICHAT12; return true;};
+               if (vec[1] == String("avionic_chat_13")) 
{trigger_source=SS_TRIG_AVICHAT13; return true;};
+               if (vec[1] == String("aoa_horn")) {trigger_source=SS_TRIG_AOA; 
return true;};
+               if (vec[1] == String("ignition")) 
{trigger_source=SS_TRIG_IGNITION; return true;};
+               if (vec[1] == String("reverse_gear")) 
{trigger_source=SS_TRIG_REVERSE_GEAR; return true;};
+               if (vec[1] == String("turn_signal")) 
{trigger_source=SS_TRIG_TURN_SIGNAL; return true;};
+               if (vec[1] == String("turn_signal_tick")) 
{trigger_source=SS_TRIG_TURN_SIGNAL_TICK; return true;};
+               if (vec[1] == String("turn_signal_warn_tick")) 
{trigger_source=SS_TRIG_TURN_SIGNAL_WARN_TICK; return true;};
+               if (vec[1] == String("linked_command")) 
{trigger_source=SS_TRIG_LINKED_COMMAND; return true;};
 
-               
-               
                return false;
        }
-       if (vec[0]==String("pitch_source"))
+
+       if (vec[0] == String("pitch_source"))
        {
-               if (vec.size()<2) return false;
-               int mod=parseModulation(vec[1]);
-               if (mod>=0) {pitch_source=mod;return true;}
+               if (vec.size() < 2) return false;
+               int mod = parseModulation(vec[1]);
+               if (mod >= 0)
+               {
+                       pitch_source = mod;
+                       return true;
+               }
                return false;
        }
-       if (vec[0]==String("pitch_factors"))
+
+       if (vec[0] == String("pitch_factors"))
        {
-               if (vec.size()<3) return false;
-               pitch_offset=StringConverter::parseReal(vec[1]);
-               pitch_multiplier=StringConverter::parseReal(vec[2]);
-               if (vec.size()==4) 
pitch_square=StringConverter::parseReal(vec[3]);
+               if (vec.size() < 3) return false;
+               pitch_offset     = StringConverter::parseReal(vec[1]);
+               pitch_multiplier = StringConverter::parseReal(vec[2]);
+               if (vec.size() == 4)
+               {
+                       pitch_square=StringConverter::parseReal(vec[3]);
+               }
                return true;
        }
-       if (vec[0]==String("gain_source"))
+
+       if (vec[0] == String("gain_source"))
        {
-               if (vec.size()<2) return false;
-               int mod=parseModulation(vec[1]);
-               if (mod>=0) {gain_source=mod;return true;}
+               if (vec.size() < 2) return false;
+               int mod = parseModulation(vec[1]);
+               if (mod >= 0)
+               {
+                       gain_source = mod;
+                       return true;
+               }
                return false;
        }
-       if (vec[0]==String("gain_factors"))
+
+       if (vec[0] == String("gain_factors"))
        {
-               if (vec.size()<3) return false;
-               gain_offset=StringConverter::parseReal(vec[1]);
-               gain_multiplier=StringConverter::parseReal(vec[2]);
-               if (vec.size()==4) 
gain_square=StringConverter::parseReal(vec[3]);
+               if (vec.size() < 3) return false;
+               gain_offset     = StringConverter::parseReal(vec[1]);
+               gain_multiplier = StringConverter::parseReal(vec[2]);
+               if (vec.size() == 4)
+               {
+                       gain_square = StringConverter::parseReal(vec[3]);
+               }
                return true;
        }
-       if (vec[0]==String("start_sound"))
+
+       if (vec[0] == String("start_sound"))
        {
-               if (vec.size()<3) return false;
-               start_sound_pitch=StringConverter::parseReal(vec[1]); 
//unparsable (e.g. "unpitched") will result in value 0.0
-               start_sound_name=vec[2];
-               has_start_sound=true;
+               if (vec.size() < 3) return false;
+               start_sound_pitch = StringConverter::parseReal(vec[1]); // 
unparsable (e.g. "unpitched") will result in value 0.0
+               start_sound_name  = vec[2];
+               has_start_sound   = true;
                return true;
        }
-       if (vec[0]==String("stop_sound"))
+
+       if (vec[0] == String("stop_sound"))
        {
-               if (vec.size()<3) return false;
-               stop_sound_pitch=StringConverter::parseReal(vec[1]); 
//unparsable (e.g. "unpitched") will result in value 0.0
-               stop_sound_name=vec[2];
-               has_stop_sound=true;
+               if (vec.size() < 3) return false;
+               stop_sound_pitch = StringConverter::parseReal(vec[1]); // 
unparsable (e.g. "unpitched") will result in value 0.0
+               stop_sound_name  = vec[2];
+               has_stop_sound   = true;
                return true;
        }
-       if (vec[0]==String("sound"))
+
+       if (vec[0] == String("sound"))
        {
-               if (vec.size()<3) return false;
-               sound_pitches[free_sound]=StringConverter::parseReal(vec[1]); 
//unparsable (e.g. "unpitched") will result in value 0.0
-               if (sound_pitches[free_sound]==0) unpitchable=true;
-               if (free_sound>0 && !unpitchable && 
sound_pitches[free_sound]<=sound_pitches[free_sound-1]) return false;
-               sound_names[free_sound]=vec[2];
+               if (vec.size() < 3) return false;
+               if (free_sound >= MAX_SOUNDS_PER_SCRIPT)
+               {
+                       LOG("SoundScriptManager: Reached MAX_SOUNDS_PER_SCRIPT 
limit (" + TOSTRING(MAX_SOUNDS_PER_SCRIPT) + ")");
+                       return false;
+               }
+               sound_pitches[free_sound] = StringConverter::parseReal(vec[1]); 
// unparsable (e.g. "unpitched") will result in value 0.0
+               if (sound_pitches[free_sound] == 0)
+               {
+                       unpitchable = true;
+               }
+               if (free_sound > 0 && !unpitchable && sound_pitches[free_sound] 
<= sound_pitches[free_sound - 1])
+               {
+                       return false;
+               }
+               sound_names[free_sound] = vec[2];
                free_sound++;
                return true;
        }
+
        return false;
 }
 
 int SoundScriptTemplate::parseModulation(String str)
 {
-       if (str==String("none")) return SS_MOD_NONE;
-       if (str==String("engine_rpm")) return SS_MOD_ENGINE;
-       if (str==String("turbo_rpm")) return SS_MOD_TURBO;
-       if (str==String("aeroengine1_rpm")) return SS_MOD_AEROENGINE1;
-       if (str==String("aeroengine2_rpm")) return SS_MOD_AEROENGINE2;
-       if (str==String("aeroengine3_rpm")) return SS_MOD_AEROENGINE3;
-       if (str==String("aeroengine4_rpm")) return SS_MOD_AEROENGINE4;
-       if (str==String("aeroengine5_rpm")) return SS_MOD_AEROENGINE5;
-       if (str==String("aeroengine6_rpm")) return SS_MOD_AEROENGINE6;
-       if (str==String("aeroengine7_rpm")) return SS_MOD_AEROENGINE7;
-       if (str==String("aeroengine8_rpm")) return SS_MOD_AEROENGINE8;
-       if (str==String("wheel_speed_kmph")) return SS_MOD_WHEELSPEED;
-       if (str==String("injector_ratio")) return SS_MOD_INJECTOR;
-       if (str==String("torque_nm")) return SS_MOD_TORQUE;
-       if (str==String("gearbox_rpm")) return SS_MOD_GEARBOX;
-       if (str==String("creak")) return SS_MOD_CREAK;
-       if (str==String("break")) return SS_MOD_BREAK;
-       if (str==String("screetch")) return SS_MOD_SCREETCH;
-       if (str==String("pump_rpm")) return SS_MOD_PUMP;
-       if (str==String("aeroengine1_throttle")) return SS_MOD_THROTTLE1;
-       if (str==String("aeroengine2_throttle")) return SS_MOD_THROTTLE2;
-       if (str==String("aeroengine3_throttle")) return SS_MOD_THROTTLE3;
-       if (str==String("aeroengine4_throttle")) return SS_MOD_THROTTLE4;
-       if (str==String("aeroengine5_throttle")) return SS_MOD_THROTTLE5;
-       if (str==String("aeroengine6_throttle")) return SS_MOD_THROTTLE6;
-       if (str==String("aeroengine7_throttle")) return SS_MOD_THROTTLE7;
-       if (str==String("aeroengine8_throttle")) return SS_MOD_THROTTLE8;
-       if (str==String("air_speed_knots")) return SS_MOD_AIRSPEED;
-       if (str==String("angle_of_attack_degree")) return SS_MOD_AOA;
+       if (str == String("none"))                   return SS_MOD_NONE;
+       if (str == String("engine_rpm"))             return SS_MOD_ENGINE;
+       if (str == String("turbo_rpm"))              return SS_MOD_TURBO;
+       if (str == String("aeroengine1_rpm"))        return SS_MOD_AEROENGINE1;
+       if (str == String("aeroengine2_rpm"))        return SS_MOD_AEROENGINE2;
+       if (str == String("aeroengine3_rpm"))        return SS_MOD_AEROENGINE3;
+       if (str == String("aeroengine4_rpm"))        return SS_MOD_AEROENGINE4;
+       if (str == String("aeroengine5_rpm"))        return SS_MOD_AEROENGINE5;
+       if (str == String("aeroengine6_rpm"))        return SS_MOD_AEROENGINE6;
+       if (str == String("aeroengine7_rpm"))        return SS_MOD_AEROENGINE7;
+       if (str == String("aeroengine8_rpm"))        return SS_MOD_AEROENGINE8;
+       if (str == String("wheel_speed_kmph"))       return SS_MOD_WHEELSPEED;
+       if (str == String("injector_ratio"))         return SS_MOD_INJECTOR;
+       if (str == String("torque_nm"))              return SS_MOD_TORQUE;
+       if (str == String("gearbox_rpm"))            return SS_MOD_GEARBOX;
+       if (str == String("creak"))                  return SS_MOD_CREAK;
+       if (str == String("break"))                  return SS_MOD_BREAK;
+       if (str == String("screetch"))               return SS_MOD_SCREETCH;
+       if (str == String("pump_rpm"))               return SS_MOD_PUMP;
+       if (str == String("aeroengine1_throttle"))   return SS_MOD_THROTTLE1;
+       if (str == String("aeroengine2_throttle"))   return SS_MOD_THROTTLE2;
+       if (str == String("aeroengine3_throttle"))   return SS_MOD_THROTTLE3;
+       if (str == String("aeroengine4_throttle"))   return SS_MOD_THROTTLE4;
+       if (str == String("aeroengine5_throttle"))   return SS_MOD_THROTTLE5;
+       if (str == String("aeroengine6_throttle"))   return SS_MOD_THROTTLE6;
+       if (str == String("aeroengine7_throttle"))   return SS_MOD_THROTTLE7;
+       if (str == String("aeroengine8_throttle"))   return SS_MOD_THROTTLE8;
+       if (str == String("air_speed_knots"))        return SS_MOD_AIRSPEED;
+       if (str == String("angle_of_attack_degree")) return SS_MOD_AOA;
+       if (str == String("linked_command_rate"))    return 
SS_MOD_LINKED_COMMANDRATE;
 
-       if (str==String("linked_command_rate")) return 
SS_MOD_LINKED_COMMANDRATE;
        return -1;
 }
 
@@ -584,18 +680,30 @@
        , soundLinkType(soundLinkType)
        , soundLinkItemId(soundLinkItemId)
        , startSound(NULL)
+       , startSound_pitchgain(0.0f)
        , stopSound(NULL)
+       , stopSound_pitchgain(0.0f)
+       , lastgain(1.0f)
 {
-       //create sounds
+       // create sounds
        if (templ->has_start_sound)
+       {
                startSound = sm->createSound(templ->start_sound_name);
+       }
+       
        if (templ->has_stop_sound)
+       {
                stopSound = sm->createSound(templ->stop_sound_name);
-       for (int i=0; i<templ->free_sound; i++)
+       }
+       
+       for (int i=0; i < templ->free_sound; i++)
+       {
                sounds[i] = sm->createSound(templ->sound_names[i]);
-       lastgain=1.0;
-       setPitch(0.0);
-       setGain(1.0);
+       }
+       
+       setPitch(0.0f);
+       setGain(1.0f);
+
        LOG("SoundScriptInstance: instance created: "+instancename);
 }
 
@@ -603,102 +711,113 @@
 {
        if (startSound)
        {
-               startSound_pitchgain=pitchgain_cutoff(templ->start_sound_pitch, 
value);
-               if (startSound_pitchgain!=0.0 && templ->start_sound_pitch!=0.0)
-                       startSound->setPitch(value/templ->start_sound_pitch);
+               startSound_pitchgain = 
pitchgain_cutoff(templ->start_sound_pitch, value);
+
+               if (startSound_pitchgain != 0.0f && templ->start_sound_pitch != 
0.0f)
+               {
+                       startSound->setPitch(value / templ->start_sound_pitch);
+               }
        }
+
        if (templ->free_sound)
        {
-               //searching the interval
-               int up=0;
-               for (up=0; up<templ->free_sound; up++)
+               // searching the interval
+               int up = 0;
+
+               for (up=0; up < templ->free_sound; up++)
                {
-                       if (templ->sound_pitches[up]>value) break;
+                       if (templ->sound_pitches[up] > value)
+                       {
+                               break;
+                       }
                }
-               if (up==0)
+
+               if (up == 0)
                {
-                       //low sound case
-                       
sounds_pitchgain[0]=pitchgain_cutoff(templ->sound_pitches[0], value);
-                       if (sounds_pitchgain[0]!=0.0 && 
templ->sound_pitches[0]!=0.0 && sounds[0])
-                               
sounds[0]->setPitch(value/templ->sound_pitches[0]);
-                       for (int i=1; i<templ->free_sound; i++)
+                       // low sound case
+                       sounds_pitchgain[0] = 
pitchgain_cutoff(templ->sound_pitches[0], value);
+
+                       if (sounds_pitchgain[0] != 0.0f && 
templ->sound_pitches[0] != 0.0f && sounds[0])
                        {
-                               if (templ->sound_pitches[i]!=0.0)
+                               sounds[0]->setPitch(value / 
templ->sound_pitches[0]);
+                       }
+
+                       for (int i=1; i < templ->free_sound; i++)
+                       {
+                               if (templ->sound_pitches[i] != 0.0f)
                                {
-                                       sounds_pitchgain[i]=0.0;
-                                       //pause?
-                               }
-                               else
+                                       sounds_pitchgain[i] = 0.0f;
+                                       // pause?
+                               } else
                                {
-                                       sounds_pitchgain[i]=1.0; //unpitched
+                                       sounds_pitchgain[i] = 1.0f; // unpitched
                                }
                        }
-               }
-               else if (up==templ->free_sound)
+               } else if (up == templ->free_sound)
                {
-                       //high sound case
-                       for (int i=0; i<templ->free_sound-1; i++)
+                       // high sound case
+                       for (int i=0; i < templ->free_sound-1; i++)
                        {
-                               if (templ->sound_pitches[i]!=0.0)
+                               if (templ->sound_pitches[i] != 0.0f)
                                {
-                                       sounds_pitchgain[i]=0.0;
-                                       //pause?
-                               }
-                               else
+                                       sounds_pitchgain[i] = 0.0f;
+                                       // pause?
+                               } else
                                {
-                                       sounds_pitchgain[i]=1.0; //unpitched
+                                       sounds_pitchgain[i] = 1.0f; // unpitched
                                }
                        }
-                       sounds_pitchgain[templ->free_sound-1]=1.0;
-                       if (templ->sound_pitches[templ->free_sound-1]!=0.0 && 
sounds[templ->free_sound-1])
+                       
+                       sounds_pitchgain[templ->free_sound - 1] = 1.0f;
+
+                       if (templ->sound_pitches[templ->free_sound - 1] != 0.0f 
&& sounds[templ->free_sound-1])
                        {
-                               
sounds[templ->free_sound-1]->setPitch(value/templ->sound_pitches[templ->free_sound-1]);
+                               sounds[templ->free_sound-1]->setPitch(value / 
templ->sound_pitches[templ->free_sound - 1]);
                        }
-               }
-               else
+               } else
                {
-                       //middle sound case
-                       int low=up-1;
-                       for (int i=0; i<low; i++)
+                       // middle sound case
+                       int low = up - 1;
+
+                       for (int i=0; i < low; i++)
                        {
-                               if (templ->sound_pitches[i]!=0.0)
+                               if (templ->sound_pitches[i] != 0.0f)
                                {
-                                       sounds_pitchgain[i]=0.0;
-                                       //pause?
-                               }
-                               else
+                                       sounds_pitchgain[i] = 0.0f;
+                                       // pause?
+                               } else
                                {
-                                       sounds_pitchgain[i]=1.0; //unpitched
+                                       sounds_pitchgain[i] = 1.0f; // unpitched
                                }
                        }
-                       if (templ->sound_pitches[low]!=0.0 && sounds[low])
+
+                       if (templ->sound_pitches[low] != 0.0f && sounds[low])
                        {
-                               
sounds_pitchgain[low]=(templ->sound_pitches[up]-value)/(templ->sound_pitches[up]-templ->sound_pitches[low]);
-                               
sounds[low]->setPitch(value/templ->sound_pitches[low]);
-                       }
-                       else
+                               sounds_pitchgain[low] = 
(templ->sound_pitches[up] - value) / (templ->sound_pitches[up] - 
templ->sound_pitches[low]);
+                               sounds[low]->setPitch(value / 
templ->sound_pitches[low]);
+                       } else
                        {
-                               sounds_pitchgain[low]=1.0; //unpitched
+                               sounds_pitchgain[low] = 1.0f; // unpitched
                        }
-                       if (templ->sound_pitches[up]!=0.0 && sounds[up])
+
+                       if (templ->sound_pitches[up] != 0.0f && sounds[up])
                        {
-                               
sounds_pitchgain[up]=(value-templ->sound_pitches[low])/(templ->sound_pitches[up]-templ->sound_pitches[low]);
-                               
sounds[up]->setPitch(value/templ->sound_pitches[up]);
-                       }
-                       else
+                               sounds_pitchgain[up]=(value - 
templ->sound_pitches[low]) / (templ->sound_pitches[up] - 
templ->sound_pitches[low]);
+                               sounds[up]->setPitch(value / 
templ->sound_pitches[up]);
+                       } else
                        {
-                               sounds_pitchgain[up]=1.0; //unpitched
+                               sounds_pitchgain[up] = 1.0f; // unpitched
                        }
-                       for (int i=up+1; i<templ->free_sound; i++)
+
+                       for (int i=up+1; i < templ->free_sound; i++)
                        {
-                               if (templ->sound_pitches[i]!=0.0)
+                               if (templ->sound_pitches[i] != 0.0f)
                                {
-                                       sounds_pitchgain[i]=0.0;
-                                       //pause?
-                               }
-                               else
+                                       sounds_pitchgain[i] = 0.0f;
+                                       // pause?
+                               } else
                                {
-                                       sounds_pitchgain[i]=1.0; //unpitched
+                                       sounds_pitchgain[i] = 1.0f; // unpitched
                                }
                        }
                }
@@ -706,30 +825,60 @@
 
        if (stopSound)
        {
-               stopSound_pitchgain=pitchgain_cutoff(templ->stop_sound_pitch, 
value);
-               if (stopSound_pitchgain!=0.0 && templ->stop_sound_pitch!=0.0)
-                       stopSound->setPitch(value/templ->stop_sound_pitch);
+               stopSound_pitchgain = pitchgain_cutoff(templ->stop_sound_pitch, 
value);
+
+               if (stopSound_pitchgain != 0.0f && templ->stop_sound_pitch != 
0.0f)
+               {
+                       stopSound->setPitch(value / templ->stop_sound_pitch);
+               }
        }
-       //propagate new gains
+
+       // propagate new gains
        setGain(lastgain);
 }
 
 float SoundScriptInstance::pitchgain_cutoff(float sourcepitch, float 
targetpitch)
 {
-       if (sourcepitch==0.0) return 1.0; //unpitchable
-       if (targetpitch>sourcepitch/PITCHDOWN_FADE_FACTOR) return 1.0; //pass
-       if (targetpitch<sourcepitch/PITCHDOWN_CUTOFF_FACTOR) return 0.0; 
//cutoff
-       //linear fading
-       return 
(targetpitch-sourcepitch/PITCHDOWN_CUTOFF_FACTOR)/(sourcepitch/PITCHDOWN_FADE_FACTOR-sourcepitch/PITCHDOWN_CUTOFF_FACTOR);
+       if (sourcepitch == 0.0f)
+       {
+               return 1.0f; // unpitchable
+       }
+
+       if (targetpitch>sourcepitch / PITCHDOWN_FADE_FACTOR)
+       {
+               return 1.0f; // pass
+       }
+
+       if (targetpitch<sourcepitch / PITCHDOWN_CUTOFF_FACTOR)
+       {
+               return 0.0f; // cutoff
+       }
+
+       // linear fading
+       return (targetpitch - sourcepitch / PITCHDOWN_CUTOFF_FACTOR) / 
(sourcepitch / PITCHDOWN_FADE_FACTOR - sourcepitch / PITCHDOWN_CUTOFF_FACTOR);
 }
 
 void SoundScriptInstance::setGain(float value)
 {
-       if (startSound) startSound->setGain(value*startSound_pitchgain);
-       for (int i=0; i<templ->free_sound; i++)
-               if (sounds[i]) sounds[i]->setGain(value*sounds_pitchgain[i]);
-       if (stopSound) stopSound->setGain(value*stopSound_pitchgain);
-       lastgain=value;
+       if (startSound)
+       {
+               startSound->setGain(value * startSound_pitchgain);
+       }
+
+       for (int i=0; i < templ->free_sound; i++)
+       {
+               if (sounds[i])
+               {
+                       sounds[i]->setGain(value * sounds_pitchgain[i]);
+               }
+       }
+
+       if (stopSound)
+       {
+               stopSound->setGain(value * stopSound_pitchgain);
+       }
+
+       lastgain = value;
 }
 
 void SoundScriptInstance::setPosition(Vector3 pos, Vector3 velocity)
@@ -739,6 +888,7 @@
                startSound->setPosition(pos);
                startSound->setVelocity(velocity);
        }
+
        for (int i=0; i<templ->free_sound; i++)
        {
                if (sounds[i])
@@ -747,6 +897,7 @@
                        sounds[i]->setVelocity(velocity);
                }
        }
+
        if (stopSound)
        {
                stopSound->setPosition(pos);
@@ -758,21 +909,32 @@
 {
        if (startSound)
        {
-               if (startSound->isPlaying()) return;
+               if (startSound->isPlaying())
+               {
+                       return;
+               }
                startSound->play();
        }
+
        for (int i=0; i<templ->free_sound; i++)
        {
                if (sounds[i])
                {
-                       if (sounds[i]->isPlaying()) continue;
+                       if (sounds[i]->isPlaying())
+                       {
+                               continue;
+                       }
                        sounds[i]->setLoop(false);
                        sounds[i]->play();
                }
        }
+
        if (stopSound)
        {
-               if (stopSound->isPlaying()) return;
+               if (stopSound->isPlaying())
+               {
+                       return;
+               }
                stopSound->play();
        }
 }
@@ -784,6 +946,7 @@
                startSound->stop();
                startSound->play();
        }
+
        for (int i=0; i<templ->free_sound; i++)
        {
                if (sounds[i])
@@ -800,6 +963,7 @@
        {
                if (sounds[i]) sounds[i]->stop();
        }
+
        if (stopSound)
        {
                stopSound->stop();
@@ -809,12 +973,23 @@
 
 void SoundScriptInstance::setEnabled(bool e)
 {
-       if(startSound) startSound->setEnabled(e);
-       if(stopSound)  stopSound->setEnabled(e);
-       for (int i=0; i<templ->free_sound; i++)
+       if (startSound)
        {
-               if(sounds[i]) sounds[i]->setEnabled(e);
+               startSound->setEnabled(e);
        }
+
+       if (stopSound)
+       {
+               stopSound->setEnabled(e);
+       }
+
+       for (int i=0; i < templ->free_sound; i++)
+       {
+               if (sounds[i])
+               {
+                       sounds[i]->setEnabled(e);
+               }
+       }
 }
 
 #endif // USE_OPENAL

Modified: trunk/source/main/audio/SoundScriptManager.h
===================================================================
--- trunk/source/main/audio/SoundScriptManager.h        2012-05-02 05:08:50 UTC 
(rev 2489)
+++ trunk/source/main/audio/SoundScriptManager.h        2012-05-02 05:22:51 UTC 
(rev 2490)
@@ -33,8 +33,7 @@
        MAX_INSTANCES_PER_GROUP = 256
 };
 
-// list of sound triggers
-enum {
+enum SoundTriggers {
        SS_TRIG_NONE = -1,
        SS_TRIG_ENGINE = 0,
        SS_TRIG_AEROENGINE1,
@@ -97,12 +96,11 @@
        SS_TRIG_AVICHAT11,
        SS_TRIG_AVICHAT12,
        SS_TRIG_AVICHAT13,
-       SS_LINKED_COMMAND,
+       SS_TRIG_LINKED_COMMAND,
        SS_MAX_TRIG
 };
 
-// list of modulation sources
-enum {
+enum ModulationSources {
        SS_MOD_NONE,
        SS_MOD_ENGINE,
        SS_MOD_TURBO,
@@ -136,7 +134,7 @@
        SS_MAX_MOD
 };
 
-enum {
+enum SoundLinkTypes {
        SL_DEFAULT,
        SL_COMMAND, 
        SL_HYDRO, 
@@ -250,8 +248,8 @@
     Ogre::Real getLoadingOrder(void) const;
 
        SoundScriptInstance* createInstance(Ogre::String templatename, int 
truck, Ogre::SceneNode *toAttach=NULL, int soundLinkType=SL_DEFAULT, int 
soundLinkItemId=-1);
-       void unloadResourceGroup(Ogre::String groupname);
        void clearNonBaseTemplates();
+       void unloadResourceGroup(Ogre::String groupname);
 
        // functions
        void trigOnce    (int truck, int trig, int linkType = SL_DEFAULT, int 
linkItemID=-1);

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Rigsofrods-devel mailing list
Rigsofrods-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rigsofrods-devel

Reply via email to