From: <wha...@us...> - 2011-06-16 22:03:09
|
Revision: 7420 http://planeshift.svn.sourceforge.net/planeshift/?rev=7420&view=rev Author: whacko88 Date: 2011-06-16 22:03:03 +0000 (Thu, 16 Jun 2011) Log Message: ----------- added the support for factory sounds that depend on the actor mode removed bug that make the plugin crash on closure removed bug about psEntity references Modified Paths: -------------- soc/2011/sound/include/isoundmngr.h soc/2011/sound/src/client/modehandler.cpp soc/2011/sound/src/plugins/common/dummysndmngr/dummysndmngr.cpp soc/2011/sound/src/plugins/common/dummysndmngr/dummysndmngr.h soc/2011/sound/src/plugins/common/soundmanager/psentity.cpp soc/2011/sound/src/plugins/common/soundmanager/psentity.h soc/2011/sound/src/plugins/common/soundmanager/pssoundsector.cpp soc/2011/sound/src/plugins/common/soundmanager/pssoundsector.h soc/2011/sound/src/plugins/common/soundmanager/soundmanager.cpp soc/2011/sound/src/plugins/common/soundmanager/soundmanager.h Modified: soc/2011/sound/include/isoundmngr.h =================================================================== --- soc/2011/sound/include/isoundmngr.h 2011-06-16 20:40:36 UTC (rev 7419) +++ soc/2011/sound/include/isoundmngr.h 2011-06-16 22:03:03 UTC (rev 7420) @@ -34,6 +34,7 @@ //------------------------------------------------------------------------------------ // Forward Declarations //------------------------------------------------------------------------------------ +struct iMeshWrapper; struct iView; class csVector3; @@ -255,6 +256,20 @@ */ virtual int GetWeather() const = 0; + /** + * Sets the new state for the entity associated to the given mesh and + * plays the start resource (if defined). If it is already playing a + * sound, it is stopped. + * + * @param state the new state > 0 for the entity. For negative value + * the function is not defined. + * @param mesh the mesh associated to the entity. + * @param forceChange if it is false the entity does not change its + * state if the new one is not defined. If it is true the entity stops + * play any sound until a new valid state is defined. + */ + virtual void SetEntityState(int state, iMeshWrapper* mesh, bool forceChange) = 0; + //------------------// // TOGGLES MANAGING // //------------------// Modified: soc/2011/sound/src/client/modehandler.cpp =================================================================== --- soc/2011/sound/src/client/modehandler.cpp 2011-06-16 20:40:36 UTC (rev 7419) +++ soc/2011/sound/src/client/modehandler.cpp 2011-06-16 22:03:03 UTC (rev 7420) @@ -370,6 +370,10 @@ } } } + else + { + psengine->GetSoundManager()->SetEntityState(msg.mode, actor->GetMesh(), false); + } } void ModeHandler::SetModeSounds(uint8_t mode) Modified: soc/2011/sound/src/plugins/common/dummysndmngr/dummysndmngr.cpp =================================================================== --- soc/2011/sound/src/plugins/common/dummysndmngr/dummysndmngr.cpp 2011-06-16 20:40:36 UTC (rev 7419) +++ soc/2011/sound/src/plugins/common/dummysndmngr/dummysndmngr.cpp 2011-06-16 22:03:03 UTC (rev 7420) @@ -146,6 +146,11 @@ return weather; } +void DummySoundManager::SetEntityState(int state, iMeshWrapper* mesh, bool forceChange) +{ + return; +} + void DummySoundManager::SetLoopBGMToggle(bool toggle) { loopToggle = toggle; Modified: soc/2011/sound/src/plugins/common/dummysndmngr/dummysndmngr.h =================================================================== --- soc/2011/sound/src/plugins/common/dummysndmngr/dummysndmngr.h 2011-06-16 20:40:36 UTC (rev 7419) +++ soc/2011/sound/src/plugins/common/dummysndmngr/dummysndmngr.h 2011-06-16 22:03:03 UTC (rev 7420) @@ -77,6 +77,7 @@ virtual int GetTimeOfDay() const; virtual void SetWeather(int newWeather); virtual int GetWeather() const; + virtual void SetEntityState(int state, iMeshWrapper* mesh, bool forceChange); //Toggles virtual void SetLoopBGMToggle(bool toggle); Modified: soc/2011/sound/src/plugins/common/soundmanager/psentity.cpp =================================================================== --- soc/2011/sound/src/plugins/common/soundmanager/psentity.cpp 2011-06-16 20:40:36 UTC (rev 7419) +++ soc/2011/sound/src/plugins/common/soundmanager/psentity.cpp 2011-06-16 22:03:03 UTC (rev 7420) @@ -31,9 +31,14 @@ { active = false; when = 0; - state = 0; + state = 1; id = 0; handle = 0; + + minMinRange = 0.0f; + maxMaxRange = 0.0f; + minTimeOfDayStart = 25; + maxTimeOfDayEnd = -1; } psEntity::psEntity(psEntity* const& entity) @@ -56,8 +61,13 @@ active = entity->active; when = entity->when; state = entity->state; - id = 0; + id = entity->id; handle = 0; + + minMinRange = entity->minMinRange; + maxMaxRange = entity->maxMaxRange; + minTimeOfDayStart = entity->minTimeOfDayStart; + maxTimeOfDayEnd = entity->maxTimeOfDayEnd; } psEntity::~psEntity() @@ -79,6 +89,10 @@ { delete sp; } + else + { + sp->references--; + } } statePar.DeleteAll(); @@ -138,6 +152,24 @@ statePar.Put(state, sp); + // updating variables that keep track of the extremes values + if(minRange < minMinRange) + { + minMinRange = minRange; + } + if(maxRange > maxMaxRange) + { + maxMaxRange = maxRange; + } + if(timeOfDayStart < minTimeOfDayStart) + { + minTimeOfDayStart = timeOfDayStart; + } + if(timeOfDayEnd > maxTimeOfDayEnd) + { + maxTimeOfDayEnd = timeOfDayEnd; + } + return true; } @@ -159,14 +191,29 @@ return (handle != 0); } -bool psEntity::IsReadyToPlay() const +bool psEntity::IsReadyToPlay(int time, float range) const { + StateParameters* sp; + + // checking if it is in the undefined state if(state < 0) { + return true; + } + + sp = statePar.Get(state, 0); + + // checking time, range and delay + if(range < sp->minRange || range > sp->maxRange) + { return false; } - if(when <= 0) + else if(time < sp->timeOfDayStart || sp->timeOfDayEnd < time) { + return false; + } + else if(when <= 0) + { return true; } @@ -175,22 +222,12 @@ bool psEntity::CheckTimeAndRange(int time, float range) const { - StateParameters* sp; - // checking if it is in the undefined state - if(state < 0) + if(range < minMinRange || range > maxMaxRange) { - return true; - } - - sp = statePar.Get(state, 0); - - // checking time and range - if(range < sp->minRange || range > sp->maxRange) - { return false; } - if(sp->timeOfDayStart <= time && time <= sp->timeOfDayEnd) + if(minTimeOfDayStart <= time && time <= maxTimeOfDayEnd) { return true; } @@ -207,7 +244,16 @@ { return; } + + // stopping previous sound if any + if(handle != 0) + { + handle->RemoveCallback(); + sndSysMgr->StopSound(handle->name); + handle = 0; + } + // setting state sp = statePar.Get(stat, 0); if(sp == 0) { @@ -219,17 +265,9 @@ return; } - // stopping previous sound if any - if(handle != 0) - { - handle->RemoveCallback(); - sndSysMgr->StopSound(handle->name); - handle = 0; - } - - // setting and playing the starting sound state = stat; + // playing the starting sound if(!(sp->startResource.IsEmpty())) { if(sndSysMgr->Play3DSound(sp->startResource, DONT_LOOP, 0, 0, Modified: soc/2011/sound/src/plugins/common/soundmanager/psentity.h =================================================================== --- soc/2011/sound/src/plugins/common/soundmanager/psentity.h 2011-06-16 20:40:36 UTC (rev 7419) +++ soc/2011/sound/src/plugins/common/soundmanager/psentity.h 2011-06-16 22:03:03 UTC (rev 7420) @@ -35,14 +35,14 @@ class psEntity { public: - csString factoryName; ///< name of this entity's factory - csString meshName; ///< name of this entity's mesh + csString factoryName; ///< name of this entity's factory + csString meshName; ///< name of this entity's mesh - bool active; ///< is this psEntity active, used for caching purposes + bool active; ///< is this psEntity active, used for caching purposes /** - * Create an empty psEntity. The initial state is set to 0. + * Create an empty psEntity. The initial state is set to 1. */ psEntity(); @@ -91,8 +91,8 @@ * @param maxRange maximum distance at which the sound is heard * @param probability the probability that resource is played when in this * state. - * @param timeOfDayStart time when this entity starts playing. - * @param timeOfDayEnd timewhen this entity stops playing. + * @param timeOfDayStart time when this entity starts playing in hours. + * @param timeOfDayEnd timewhen this entity stops playing in hours. * @param delayAfter how much time this entity waits before playing again * from the moment the sound ends in seconds. * @return true if the state has been defined, false if already exists. @@ -122,20 +122,27 @@ bool IsPlaying() const; /** - * Checks if the delay is done and if the state is defined. - * @return true if the dalayed is done and if the entity is not in - * an undefined state, false otherwise. + * Checks if all condition for the sound to play are satisfied in + * the current state. + * + * @param time <24 && >0 is resonable but can be any valid int. + * @param range the distance to test. + * @return true if the dalayed is done, the entity is not in an + * undefined state, the given time is within this entity's time + * window and if the distance is between the minimum and maximum + * range. False otherwise. */ - bool IsReadyToPlay() const; + bool IsReadyToPlay(int time, float range) const; /** * Check if time is within this entity's timewindow and if the distance - * is between the minimum and maximum range. + * is between the minimum and maximum range in any defined state. This + * control for only the current state is performed by IsReadyToPlay(). * * @param time <24 && >0 is resonable but can be any valid int. * @param range the distance to test. - * @return true if conditions are satisfied or if the entity is in an - * undefined state, false otherwise. + * @return true if conditions are satisfied for at least one defined + * state, false otherwise. */ bool CheckTimeAndRange(int time, float range) const; @@ -175,7 +182,7 @@ csString startResource; ///< resource to be played when the entity state chage float volume; ///< volume of the sound float minRange; ///< minimum distance at which the sound is heard - float maxRange ; ///< maximum distance at which the sound is heard + float maxRange; ///< maximum distance at which the sound is heard float probability; ///< how high is the probability that this entity makes this sound int timeOfDayStart; ///< time when this entity starts playing int timeOfDayEnd; ///< time when this entity stops. @@ -190,6 +197,11 @@ int when; ///< counter to keep track when it has been played - zero means i may play at any time (in ms) uint id; ///< the id of the mesh object whose sound is controlled by this entity . + float minMinRange; ///< minimum minRange between all the defined state of this entity + float maxMaxRange; ///< maximum maxRange between all the defined state of this entity + int minTimeOfDayStart; ///< minimum timeOfDayStart between all the defined state of this entity + int maxTimeOfDayEnd; ///< maximum timeOfDayEnd between all the defined state of this entity + /** * The Callback gets called if the SoundHandle is destroyed. * It sets handle to NULL. Modified: soc/2011/sound/src/plugins/common/soundmanager/pssoundsector.cpp =================================================================== --- soc/2011/sound/src/plugins/common/soundmanager/pssoundsector.cpp 2011-06-16 20:40:36 UTC (rev 7419) +++ soc/2011/sound/src/plugins/common/soundmanager/pssoundsector.cpp 2011-06-16 22:03:03 UTC (rev 7420) @@ -356,6 +356,18 @@ if(entity == 0) { entity = new psEntity(); + + // handle mesh/factory entities + if(meshName == 0) + { + entity->factoryName = factoryName; + factories.Put(factoryName, entity); + } + else + { + entity->meshName = meshName; + meshes.Put(meshName, entity); + } } // set all parameters @@ -368,24 +380,8 @@ // adjusting the probability/s on the update time prob = prob / 1000 * 50; // TODO make this coherent with the update throttle (now 50) - if(!(entity->DefineState(state, resource, startResource, volume, - minRange, maxRange, prob, timeOfDayStart, timeOfDayEnd, delayAfter))) - { - return; - } - - // handle mesh/factory entities - if(meshName == 0) - { - entity->factoryName = factoryName; - factories.Put(factoryName, entity); - } - else - { - entity->meshName = meshName; - meshes.Put(meshName, entity); - } - + entity->DefineState(state, resource, startResource, volume, + minRange, maxRange, prob, timeOfDayStart, timeOfDayEnd, delayAfter); } void psSoundSector::UpdateEntity(SoundControl* &ctrl, psSoundSector* commonSector) @@ -511,6 +507,18 @@ delete entity; } +void psSoundSector::SetEntityState(int state, SoundControl* ctrl, iMeshWrapper* mesh, bool forceChange) +{ + psEntity* entity = tempEntities.Get(mesh->QueryObject()->GetID(), 0); + + if(entity == 0) + { + return; + } + + entity->SetState(state, ctrl, mesh->GetMovable()->GetFullPosition(), forceChange); +} + void psSoundSector::Load(csRef<iDocumentNode> sector) { csRef<iDocumentNodeIterator> Itr; @@ -604,22 +612,31 @@ csVector3 rangeVec; float range; + if(entity->IsPlaying()) + { + entity->active = true; + return; + } + rangeVec = mesh->GetMovable()->GetFullPosition() - playerposition; range = rangeVec.Norm(); if(active && entity->CheckTimeAndRange(timeofday, range)) { - if(!(entity->IsPlaying()) - && rng.Get() <= entity->GetProbability() - && entity->IsReadyToPlay()) + // we need to create the entity even if it won't play anything + // so that the entity will be ready to play a sound when it change + // to a state with an higher maxRange for example + if(!(entity->IsTemporary())) { - if(!(entity->IsTemporary())) - { - entity = new psEntity(entity); - entity->SetMeshID(mesh->QueryObject()->GetID()); - tempEntities.Put(entity->GetMeshID(), entity); - } + entity = new psEntity(entity); + entity->SetMeshID(mesh->QueryObject()->GetID()); + tempEntities.Put(entity->GetMeshID(), entity); + } + // Check if it can play + if(entity->IsReadyToPlay(timeofday, range) + && rng.Get() <= entity->GetProbability()) + { entity->Play(ctrl, mesh->GetMovable()->GetFullPosition()); } Modified: soc/2011/sound/src/plugins/common/soundmanager/pssoundsector.h =================================================================== --- soc/2011/sound/src/plugins/common/soundmanager/pssoundsector.h 2011-06-16 20:40:36 UTC (rev 7419) +++ soc/2011/sound/src/plugins/common/soundmanager/pssoundsector.h 2011-06-16 22:03:03 UTC (rev 7420) @@ -77,6 +77,21 @@ void AddEntity(csRef<iDocumentNode> Node); void UpdateEntity(SoundControl* &ctrl, psSoundSector* commonSector); void DeleteEntity(psEntity* &entity); + + /** + * Sets the new state for the entity associated to the given mesh and + * plays the start resource (if defined). If it is already playing a + * sound, it is stopped. + * + * @param state the new state > 0 for the entity. For negative value + * the function is not defined. + * @param ctrl the sound control used to play the start resource. + * @param mesh the mesh associated to the entity. + * @param forceChange if it is false the entity does not change its + * state if the new one is not defined. If it is true the entity stops + * play any sound until a new valid state is defined. + */ + void SetEntityState(int state, SoundControl* ctrl, iMeshWrapper* mesh, bool forceChange); void Load(csRef<iDocumentNode> sector); void Reload(csRef<iDocumentNode> sector); void Delete(); Modified: soc/2011/sound/src/plugins/common/soundmanager/soundmanager.cpp =================================================================== --- soc/2011/sound/src/plugins/common/soundmanager/soundmanager.cpp 2011-06-16 20:40:36 UTC (rev 7419) +++ soc/2011/sound/src/plugins/common/soundmanager/soundmanager.cpp 2011-06-16 22:03:03 UTC (rev 7420) @@ -475,6 +475,10 @@ return weather; } +void SoundManager::SetEntityState(int state, iMeshWrapper* mesh, bool forceChange) +{ + activeSector->SetEntityState(state, ambientSndCtrl, mesh, forceChange); +} void SoundManager::SetLoopBGMToggle(bool toggle) { Modified: soc/2011/sound/src/plugins/common/soundmanager/soundmanager.h =================================================================== --- soc/2011/sound/src/plugins/common/soundmanager/soundmanager.h 2011-06-16 20:40:36 UTC (rev 7419) +++ soc/2011/sound/src/plugins/common/soundmanager/soundmanager.h 2011-06-16 22:03:03 UTC (rev 7420) @@ -89,6 +89,7 @@ virtual int GetTimeOfDay() const; virtual void SetWeather(int newWeather); virtual int GetWeather() const; + virtual void SetEntityState(int state, iMeshWrapper* mesh, bool forceChange); //Toggles virtual void SetLoopBGMToggle(bool toggle); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |