Skip to content

Commit 9b1aedc

Browse files
authored
Merge pull request #225 from FozeSt/fixes
Fixed sound of a weapon with a silencer
2 parents 727846c + c299169 commit 9b1aedc

File tree

5 files changed

+112
-23
lines changed

5 files changed

+112
-23
lines changed

src/xrGame/Explosive.cpp

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,23 @@ void CExplosive::LightCreate()
7575
m_pLight->set_shadow(true);
7676
}
7777

78-
void CExplosive::LightDestroy() { m_pLight.destroy(); }
79-
CExplosive::~CExplosive(void) { sndExplode.destroy(); }
80-
void CExplosive::Load(LPCSTR section) { Load(pSettings, section); }
78+
void CExplosive::LightDestroy()
79+
{
80+
m_pLight.destroy();
81+
}
82+
83+
CExplosive::~CExplosive(void)
84+
{
85+
#ifndef LAYERED_SND_SHOOT
86+
sndExplode.destroy();
87+
#endif
88+
}
89+
90+
void CExplosive::Load(LPCSTR section)
91+
{
92+
Load(pSettings, section);
93+
}
94+
8195
void CExplosive::Load(CInifile const* ini, LPCSTR section)
8296
{
8397
m_fBlastHit = ini->r_float(section, "blast");
@@ -106,8 +120,13 @@ void CExplosive::Load(CInifile const* ini, LPCSTR section)
106120
//трассы для разлета осколков
107121
m_fFragmentSpeed = ini->r_float(section, "fragment_speed");
108122

123+
//Alundaio: LAYERED_SND_SHOOT
124+
#ifdef LAYERED_SND_SHOOT
125+
m_layered_sounds.LoadSound(ini, section, "snd_explode", "sndExplode", false, m_eSoundExplode);
126+
#else
109127
LPCSTR snd_name = ini->r_string(section, "snd_explode");
110128
sndExplode.create(snd_name, st_Effect, m_eSoundExplode);
129+
#endif
111130

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

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

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

src/xrGame/Explosive.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ class CExplosive : public IDamageSource
7777
void LightDestroy();
7878

7979
protected:
80+
81+
//Alundaio: LAYERED_SND_SHOOT
82+
#ifdef LAYERED_SND_SHOOT
83+
HUD_SOUND_COLLECTION_LAYERED m_layered_sounds;
84+
#endif
85+
//-Alundaio
86+
8087
CWalmarkManager m_wallmark_manager;
8188
// ID персонажа который иницировал действие
8289
u16 m_iCurrentParentID;

src/xrGame/HudSound.cpp

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,12 @@ void HUD_SOUND_COLLECTION::LoadSound(LPCSTR section, LPCSTR line, LPCSTR alias,
169169
snd_item.m_alias = alias;
170170
snd_item.m_b_exclusive = exclusive;
171171
}
172+
172173
//Alundaio:
173174
/*
174-
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
175-
wrapping the class around HUD_SOUND_COLLECTION and tagging them with the same alias. This way, when one for example
176-
sndShot is played, it will play all the sound items with the same alias.
175+
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
176+
wrapping the class around HUD_SOUND_COLLECTION and tagging them with the same alias. This way, when one for example
177+
sndShot is played, it will play all the sound items with the same alias.
177178
*/
178179
//----------------------------------------------------------
179180
#ifdef LAYERED_SND_SHOOT
@@ -205,8 +206,7 @@ void HUD_SOUND_COLLECTION_LAYERED::SetPosition(pcstr alias, const Fvector& pos)
205206
sound_item.SetPosition(alias, pos);
206207
}
207208

208-
void HUD_SOUND_COLLECTION_LAYERED::PlaySound(pcstr alias, const Fvector& position, const IGameObject* parent,
209-
bool hud_mode, bool looped, u8 index)
209+
void HUD_SOUND_COLLECTION_LAYERED::PlaySound(pcstr alias, const Fvector& position, const IGameObject* parent, bool hud_mode, bool looped, u8 index)
210210
{
211211
for (auto& sound_item : m_sound_items)
212212
if (sound_item.m_alias == alias)
@@ -236,9 +236,42 @@ void HUD_SOUND_COLLECTION_LAYERED::LoadSound(pcstr section, pcstr line, pcstr al
236236
if (pSettings->section_exist(buf_str))
237237
{
238238
string256 sound_line;
239-
xr_strcpy(sound_line, "snd_1_layer");
239+
xr_strcpy(sound_line,"snd_1_layer");
240240
int k = 1;
241241
while (pSettings->line_exist(buf_str, sound_line))
242+
{
243+
m_sound_items.resize(m_sound_items.size() + 1);
244+
HUD_SOUND_COLLECTION& snd_item = m_sound_items.back();
245+
snd_item.LoadSound(buf_str, sound_line, alias, exclusive, type);
246+
snd_item.m_alias = alias;
247+
xr_sprintf(sound_line,"snd_%d_layer", ++k);
248+
}
249+
}
250+
else // For compatibility with normal HUD_SOUND_COLLECTION sounds
251+
{
252+
m_sound_items.resize(m_sound_items.size() + 1);
253+
HUD_SOUND_COLLECTION& snd_item = m_sound_items.back();
254+
snd_item.LoadSound(section, line, alias, exclusive, type);
255+
snd_item.m_alias = alias;
256+
}
257+
}
258+
259+
void HUD_SOUND_COLLECTION_LAYERED::LoadSound(CInifile const *ini, pcstr section, pcstr line, pcstr alias, bool exclusive, int type)
260+
{
261+
pcstr str = ini->r_string(section, line);
262+
string256 buf_str;
263+
264+
int count = _GetItemCount(str);
265+
R_ASSERT(count);
266+
267+
_GetItem(str, 0, buf_str);
268+
269+
if (ini->section_exist(buf_str))
270+
{
271+
string256 sound_line;
272+
xr_strcpy(sound_line, "snd_1_layer");
273+
int k = 1;
274+
while (ini->line_exist(buf_str, sound_line))
242275
{
243276
m_sound_items.resize(m_sound_items.size() + 1);
244277
HUD_SOUND_COLLECTION& snd_item = m_sound_items.back();
@@ -256,4 +289,4 @@ void HUD_SOUND_COLLECTION_LAYERED::LoadSound(pcstr section, pcstr line, pcstr al
256289
}
257290
}
258291
#endif
259-
//-Alundaio
292+
//-Alundaio

src/xrGame/HudSound.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class HUD_SOUND_COLLECTION
5757
~HUD_SOUND_COLLECTION();
5858

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

@@ -83,13 +84,20 @@ class HUD_SOUND_COLLECTION_LAYERED
8384

8485
public:
8586
~HUD_SOUND_COLLECTION_LAYERED();
87+
8688
HUD_SOUND_ITEM* FindSoundItem(pcstr alias, bool b_assert);
87-
void PlaySound(pcstr alias, const Fvector& position, const IGameObject* parent, bool hud_mode, bool looped = false,
88-
u8 index = u8(-1));
89+
90+
void PlaySound(pcstr alias, const Fvector& position, const IGameObject* parent,
91+
bool hud_mode, bool looped = false, u8 index = u8(-1));
92+
8993
void StopSound(pcstr alias);
9094
void StopAllSounds();
95+
9196
void LoadSound(pcstr section, pcstr line, pcstr alias, bool exclusive = false, int type = sg_SourceType);
97+
void LoadSound(CInifile const* ini, pcstr section, pcstr line, pcstr alias,
98+
bool exclusive = false, int type = sg_SourceType);
99+
92100
void SetPosition(pcstr alias, const Fvector& pos);
93101
};
94102
#endif
95-
//-Alundaio
103+
//-Alundaio

src/xrGame/WeaponMagazined.cpp

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ void CWeaponMagazined::Load(LPCSTR section)
8888

8989
//Alundaio: LAYERED_SND_SHOOT
9090
#ifdef LAYERED_SND_SHOOT
91-
m_layered_sounds.LoadSound(section, "snd_shoot", "sndShot", true, m_eSoundShot);
91+
m_layered_sounds.LoadSound(section, "snd_shoot", "sndShot", false, m_eSoundShot);
9292
#else
93-
m_sounds.LoadSound(section, "snd_shoot", "sndShot", true, m_eSoundShot); //Alundaio: Set exclusive to true
93+
m_sounds.LoadSound(section, "snd_shoot", "sndShot", false, m_eSoundShot);
9494
#endif
9595
//-Alundaio
9696

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

117+
//Alundaio: LAYERED_SND_SHOOT Silencer
118+
#ifdef LAYERED_SND_SHOOT
119+
m_layered_sounds.LoadSound(section, "snd_silncer_shot", "sndSilencerShot", false, m_eSoundShot);
120+
if (WeaponSoundExist(section, "snd_silncer_shot_actor"))
121+
m_layered_sounds.LoadSound(section, "snd_silncer_shot_actor", "sndSilencerShotActor", false, m_eSoundShot);
122+
#else
117123
m_sounds.LoadSound(section, "snd_silncer_shot", "sndSilencerShot", false, m_eSoundShot);
124+
if (WeaponSoundExist(section, "snd_silncer_shot_actor"))
125+
m_sounds.LoadSound(section, "snd_silncer_shot_actor", "sndSilencerShotActor", false, m_eSoundShot);
126+
#endif
127+
//-Alundaio
118128
}
119129

120130
m_iBaseDispersionedBulletsCount = READ_IF_EXISTS(pSettings, r_u8, section, "base_dispersioned_bullets_count", 0);
@@ -183,7 +193,7 @@ void CWeaponMagazined::FireStart()
183193
{ // misfire
184194
//Alundaio
185195
#ifdef EXTENDED_WEAPON_CALLBACKS
186-
CGameObject *object = smart_cast<CGameObject*>(H_Parent());
196+
CGameObject *object = smart_cast<CGameObject*>(H_Parent());
187197
if (object)
188198
object->callback(GameObject::eOnWeaponJammed)(object->lua_game_object(), this->lua_game_object());
189199
#endif
@@ -629,7 +639,7 @@ void CWeaponMagazined::OnShot()
629639
#ifdef LAYERED_SND_SHOOT
630640
m_layered_sounds.PlaySound(m_sSndShotCurrent.c_str(), get_LastFP(), H_Root(), !!GetHUDmode(), false, (u8)-1);
631641
#else
632-
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)
642+
PlaySound(m_sSndShotCurrent.c_str(), get_LastFP(), (u8)-1); //Alundaio: Play sound at index (ie. snd_shoot, snd_shoot1, snd_shoot2, snd_shoot3)
633643
#endif
634644
//-Alundaio
635645

@@ -1194,7 +1204,7 @@ void CWeaponMagazined::OnZoomIn()
11941204

11951205
//Alundaio: callback not sure why vs2013 gives error, it's fine
11961206
#ifdef EXTENDED_WEAPON_CALLBACKS
1197-
CGameObject *object = smart_cast<CGameObject*>(H_Parent());
1207+
CGameObject *object = smart_cast<CGameObject*>(H_Parent());
11981208
if (object)
11991209
object->callback(GameObject::eOnWeaponZoomIn)(object->lua_game_object(),this->lua_game_object());
12001210
#endif
@@ -1223,13 +1233,13 @@ void CWeaponMagazined::OnZoomOut()
12231233
if (GetState() == eIdle)
12241234
PlayAnimIdle();
12251235

1226-
//Alundaio
1236+
//Alundaio
12271237
#ifdef EXTENDED_WEAPON_CALLBACKS
1228-
CGameObject *object = smart_cast<CGameObject*>(H_Parent());
1229-
if (object)
1230-
object->callback(GameObject::eOnWeaponZoomOut)(object->lua_game_object(), this->lua_game_object());
1238+
CGameObject *object = smart_cast<CGameObject*>(H_Parent());
1239+
if (object)
1240+
object->callback(GameObject::eOnWeaponZoomOut)(object->lua_game_object(), this->lua_game_object());
12311241
#endif
1232-
//-Alundaio
1242+
//-Alundaio
12331243

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

@@ -1453,7 +1463,11 @@ bool CWeaponMagazined::install_upgrade_impl(LPCSTR section, bool test)
14531463
result2 = process_if_exists_set(section, "snd_shoot", &CInifile::r_string, str, test);
14541464
if (result2 && !test)
14551465
{
1466+
#ifdef LAYERED_SND_SHOOT
1467+
m_layered_sounds.LoadSound(section, "snd_shoot", "sndShot", false, m_eSoundShot);
1468+
#else
14561469
m_sounds.LoadSound(section, "snd_shoot", "sndShot", false, m_eSoundShot);
1470+
#endif
14571471
}
14581472
result |= result2;
14591473

@@ -1494,7 +1508,11 @@ bool CWeaponMagazined::install_upgrade_impl(LPCSTR section, bool test)
14941508
result2 = process_if_exists_set(section, "snd_silncer_shot", &CInifile::r_string, str, test);
14951509
if (result2 && !test)
14961510
{
1511+
#ifdef LAYERED_SND_SHOOT
1512+
m_layered_sounds.LoadSound(section, "snd_silncer_shot", "sndSilencerShot", false, m_eSoundShot);
1513+
#else
14971514
m_sounds.LoadSound(section, "snd_silncer_shot", "sndSilencerShot", false, m_eSoundShot);
1515+
#endif
14981516
}
14991517
result |= result2;
15001518
}

0 commit comments

Comments
 (0)