Skip to content

Commit

Permalink
Merge pull request #225 from FozeSt/fixes
Browse files Browse the repository at this point in the history
Fixed sound of a weapon with a silencer
  • Loading branch information
Xottab-DUTY committed Jul 20, 2018
2 parents 727846c + c299169 commit 9b1aedc
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 23 deletions.
29 changes: 26 additions & 3 deletions src/xrGame/Explosive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,23 @@ void CExplosive::LightCreate()
m_pLight->set_shadow(true);
}

void CExplosive::LightDestroy() { m_pLight.destroy(); }
CExplosive::~CExplosive(void) { sndExplode.destroy(); }
void CExplosive::Load(LPCSTR section) { Load(pSettings, section); }
void CExplosive::LightDestroy()
{
m_pLight.destroy();
}

CExplosive::~CExplosive(void)
{
#ifndef LAYERED_SND_SHOOT
sndExplode.destroy();
#endif
}

void CExplosive::Load(LPCSTR section)
{
Load(pSettings, section);
}

void CExplosive::Load(CInifile const* ini, LPCSTR section)
{
m_fBlastHit = ini->r_float(section, "blast");
Expand Down Expand Up @@ -106,8 +120,13 @@ void CExplosive::Load(CInifile const* ini, LPCSTR section)
//трассы для разлета осколков
m_fFragmentSpeed = ini->r_float(section, "fragment_speed");

//Alundaio: LAYERED_SND_SHOOT
#ifdef LAYERED_SND_SHOOT
m_layered_sounds.LoadSound(ini, section, "snd_explode", "sndExplode", false, m_eSoundExplode);
#else
LPCSTR snd_name = ini->r_string(section, "snd_explode");
sndExplode.create(snd_name, st_Effect, m_eSoundExplode);
#endif

m_fExplodeDurationMax = ini->r_float(section, "explode_duration");

Expand Down Expand Up @@ -333,7 +352,11 @@ void CExplosive::Explode()
// Msg("---------CExplosive Explode [%d] frame[%d]",cast_game_object()->ID(), Device.dwFrame);
OnBeforeExplosion();
//играем звук взрыва
#ifdef LAYERED_SND_SHOOT
m_layered_sounds.PlaySound("sndExplode", pos, smart_cast<IGameObject*>(this), false, false, (u8)-1);
#else
GEnv.Sound->play_at_pos(sndExplode, 0, pos, false);
#endif

//показываем эффекты

Expand Down
7 changes: 7 additions & 0 deletions src/xrGame/Explosive.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ class CExplosive : public IDamageSource
void LightDestroy();

protected:

//Alundaio: LAYERED_SND_SHOOT
#ifdef LAYERED_SND_SHOOT
HUD_SOUND_COLLECTION_LAYERED m_layered_sounds;
#endif
//-Alundaio

CWalmarkManager m_wallmark_manager;
// ID персонажа который иницировал действие
u16 m_iCurrentParentID;
Expand Down
47 changes: 40 additions & 7 deletions src/xrGame/HudSound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,12 @@ void HUD_SOUND_COLLECTION::LoadSound(LPCSTR section, LPCSTR line, LPCSTR alias,
snd_item.m_alias = alias;
snd_item.m_b_exclusive = exclusive;
}

//Alundaio:
/*
It's usage is to play a group of sounds HUD_SOUND_ITEMs as if they were a single layered entity. This is a achieved by
wrapping the class around HUD_SOUND_COLLECTION and tagging them with the same alias. This way, when one for example
sndShot is played, it will play all the sound items with the same alias.
It's usage is to play a group of sounds HUD_SOUND_ITEMs as if they were a single layered entity. This is a achieved by
wrapping the class around HUD_SOUND_COLLECTION and tagging them with the same alias. This way, when one for example
sndShot is played, it will play all the sound items with the same alias.
*/
//----------------------------------------------------------
#ifdef LAYERED_SND_SHOOT
Expand Down Expand Up @@ -205,8 +206,7 @@ void HUD_SOUND_COLLECTION_LAYERED::SetPosition(pcstr alias, const Fvector& pos)
sound_item.SetPosition(alias, pos);
}

void HUD_SOUND_COLLECTION_LAYERED::PlaySound(pcstr alias, const Fvector& position, const IGameObject* parent,
bool hud_mode, bool looped, u8 index)
void HUD_SOUND_COLLECTION_LAYERED::PlaySound(pcstr alias, const Fvector& position, const IGameObject* parent, bool hud_mode, bool looped, u8 index)
{
for (auto& sound_item : m_sound_items)
if (sound_item.m_alias == alias)
Expand Down Expand Up @@ -236,9 +236,42 @@ void HUD_SOUND_COLLECTION_LAYERED::LoadSound(pcstr section, pcstr line, pcstr al
if (pSettings->section_exist(buf_str))
{
string256 sound_line;
xr_strcpy(sound_line, "snd_1_layer");
xr_strcpy(sound_line,"snd_1_layer");
int k = 1;
while (pSettings->line_exist(buf_str, sound_line))
{
m_sound_items.resize(m_sound_items.size() + 1);
HUD_SOUND_COLLECTION& snd_item = m_sound_items.back();
snd_item.LoadSound(buf_str, sound_line, alias, exclusive, type);
snd_item.m_alias = alias;
xr_sprintf(sound_line,"snd_%d_layer", ++k);
}
}
else // For compatibility with normal HUD_SOUND_COLLECTION sounds
{
m_sound_items.resize(m_sound_items.size() + 1);
HUD_SOUND_COLLECTION& snd_item = m_sound_items.back();
snd_item.LoadSound(section, line, alias, exclusive, type);
snd_item.m_alias = alias;
}
}

void HUD_SOUND_COLLECTION_LAYERED::LoadSound(CInifile const *ini, pcstr section, pcstr line, pcstr alias, bool exclusive, int type)
{
pcstr str = ini->r_string(section, line);
string256 buf_str;

int count = _GetItemCount(str);
R_ASSERT(count);

_GetItem(str, 0, buf_str);

if (ini->section_exist(buf_str))
{
string256 sound_line;
xr_strcpy(sound_line, "snd_1_layer");
int k = 1;
while (ini->line_exist(buf_str, sound_line))
{
m_sound_items.resize(m_sound_items.size() + 1);
HUD_SOUND_COLLECTION& snd_item = m_sound_items.back();
Expand All @@ -256,4 +289,4 @@ void HUD_SOUND_COLLECTION_LAYERED::LoadSound(pcstr section, pcstr line, pcstr al
}
}
#endif
//-Alundaio
//-Alundaio
14 changes: 11 additions & 3 deletions src/xrGame/HudSound.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class HUD_SOUND_COLLECTION
~HUD_SOUND_COLLECTION();

#ifdef LAYERED_SND_SHOOT
HUD_SOUND_COLLECTION() : m_alias(nullptr) {};
shared_str m_alias; //Alundaio: For use when it's part of a layered Collection
#endif

Expand All @@ -83,13 +84,20 @@ class HUD_SOUND_COLLECTION_LAYERED

public:
~HUD_SOUND_COLLECTION_LAYERED();

HUD_SOUND_ITEM* FindSoundItem(pcstr alias, bool b_assert);
void PlaySound(pcstr alias, const Fvector& position, const IGameObject* parent, bool hud_mode, bool looped = false,
u8 index = u8(-1));

void PlaySound(pcstr alias, const Fvector& position, const IGameObject* parent,
bool hud_mode, bool looped = false, u8 index = u8(-1));

void StopSound(pcstr alias);
void StopAllSounds();

void LoadSound(pcstr section, pcstr line, pcstr alias, bool exclusive = false, int type = sg_SourceType);
void LoadSound(CInifile const* ini, pcstr section, pcstr line, pcstr alias,
bool exclusive = false, int type = sg_SourceType);

void SetPosition(pcstr alias, const Fvector& pos);
};
#endif
//-Alundaio
//-Alundaio
38 changes: 28 additions & 10 deletions src/xrGame/WeaponMagazined.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ void CWeaponMagazined::Load(LPCSTR section)

//Alundaio: LAYERED_SND_SHOOT
#ifdef LAYERED_SND_SHOOT
m_layered_sounds.LoadSound(section, "snd_shoot", "sndShot", true, m_eSoundShot);
m_layered_sounds.LoadSound(section, "snd_shoot", "sndShot", false, m_eSoundShot);
#else
m_sounds.LoadSound(section, "snd_shoot", "sndShot", true, m_eSoundShot); //Alundaio: Set exclusive to true
m_sounds.LoadSound(section, "snd_shoot", "sndShot", false, m_eSoundShot);
#endif
//-Alundaio

Expand All @@ -114,7 +114,17 @@ void CWeaponMagazined::Load(LPCSTR section)
if (pSettings->line_exist(section, "silencer_smoke_particles"))
m_sSilencerSmokeParticles = pSettings->r_string(section, "silencer_smoke_particles");

//Alundaio: LAYERED_SND_SHOOT Silencer
#ifdef LAYERED_SND_SHOOT
m_layered_sounds.LoadSound(section, "snd_silncer_shot", "sndSilencerShot", false, m_eSoundShot);
if (WeaponSoundExist(section, "snd_silncer_shot_actor"))
m_layered_sounds.LoadSound(section, "snd_silncer_shot_actor", "sndSilencerShotActor", false, m_eSoundShot);
#else
m_sounds.LoadSound(section, "snd_silncer_shot", "sndSilencerShot", false, m_eSoundShot);
if (WeaponSoundExist(section, "snd_silncer_shot_actor"))
m_sounds.LoadSound(section, "snd_silncer_shot_actor", "sndSilencerShotActor", false, m_eSoundShot);
#endif
//-Alundaio
}

m_iBaseDispersionedBulletsCount = READ_IF_EXISTS(pSettings, r_u8, section, "base_dispersioned_bullets_count", 0);
Expand Down Expand Up @@ -183,7 +193,7 @@ void CWeaponMagazined::FireStart()
{ // misfire
//Alundaio
#ifdef EXTENDED_WEAPON_CALLBACKS
CGameObject *object = smart_cast<CGameObject*>(H_Parent());
CGameObject *object = smart_cast<CGameObject*>(H_Parent());
if (object)
object->callback(GameObject::eOnWeaponJammed)(object->lua_game_object(), this->lua_game_object());
#endif
Expand Down Expand Up @@ -629,7 +639,7 @@ void CWeaponMagazined::OnShot()
#ifdef LAYERED_SND_SHOOT
m_layered_sounds.PlaySound(m_sSndShotCurrent.c_str(), get_LastFP(), H_Root(), !!GetHUDmode(), false, (u8)-1);
#else
PlaySound(m_sSndShotCurrent.c_str(), get_LastFP(), (u8)(m_iShotNum - 1)); //Alundaio: Play sound at index (ie. snd_shoot, snd_shoot1, snd_shoot2, snd_shoot3)
PlaySound(m_sSndShotCurrent.c_str(), get_LastFP(), (u8)-1); //Alundaio: Play sound at index (ie. snd_shoot, snd_shoot1, snd_shoot2, snd_shoot3)
#endif
//-Alundaio

Expand Down Expand Up @@ -1194,7 +1204,7 @@ void CWeaponMagazined::OnZoomIn()

//Alundaio: callback not sure why vs2013 gives error, it's fine
#ifdef EXTENDED_WEAPON_CALLBACKS
CGameObject *object = smart_cast<CGameObject*>(H_Parent());
CGameObject *object = smart_cast<CGameObject*>(H_Parent());
if (object)
object->callback(GameObject::eOnWeaponZoomIn)(object->lua_game_object(),this->lua_game_object());
#endif
Expand Down Expand Up @@ -1223,13 +1233,13 @@ void CWeaponMagazined::OnZoomOut()
if (GetState() == eIdle)
PlayAnimIdle();

//Alundaio
//Alundaio
#ifdef EXTENDED_WEAPON_CALLBACKS
CGameObject *object = smart_cast<CGameObject*>(H_Parent());
if (object)
object->callback(GameObject::eOnWeaponZoomOut)(object->lua_game_object(), this->lua_game_object());
CGameObject *object = smart_cast<CGameObject*>(H_Parent());
if (object)
object->callback(GameObject::eOnWeaponZoomOut)(object->lua_game_object(), this->lua_game_object());
#endif
//-Alundaio
//-Alundaio

CActor* pActor = smart_cast<CActor*>(H_Parent());

Expand Down Expand Up @@ -1453,7 +1463,11 @@ bool CWeaponMagazined::install_upgrade_impl(LPCSTR section, bool test)
result2 = process_if_exists_set(section, "snd_shoot", &CInifile::r_string, str, test);
if (result2 && !test)
{
#ifdef LAYERED_SND_SHOOT
m_layered_sounds.LoadSound(section, "snd_shoot", "sndShot", false, m_eSoundShot);
#else
m_sounds.LoadSound(section, "snd_shoot", "sndShot", false, m_eSoundShot);
#endif
}
result |= result2;

Expand Down Expand Up @@ -1494,7 +1508,11 @@ bool CWeaponMagazined::install_upgrade_impl(LPCSTR section, bool test)
result2 = process_if_exists_set(section, "snd_silncer_shot", &CInifile::r_string, str, test);
if (result2 && !test)
{
#ifdef LAYERED_SND_SHOOT
m_layered_sounds.LoadSound(section, "snd_silncer_shot", "sndSilencerShot", false, m_eSoundShot);
#else
m_sounds.LoadSound(section, "snd_silncer_shot", "sndSilencerShot", false, m_eSoundShot);
#endif
}
result |= result2;
}
Expand Down

0 comments on commit 9b1aedc

Please sign in to comment.