Skip to content

Commit

Permalink
fix distant gunshot sounds not playing
Browse files Browse the repository at this point in the history
and use flags instead of function args
  • Loading branch information
wootguy committed Nov 16, 2024
1 parent 80fa178 commit a9a9cb5
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 24 deletions.
2 changes: 2 additions & 0 deletions common/const.h
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,8 @@
#define SND_FL_SPAWNING (1<<8) // we're spawning, used in some cases for ambients (not sent across network)
#define SND_FL_PREDICTED (1<<9) // sound is predicted by the client that emitted it (not sent across network)
#define SND_FL_MOD (1<<10) // sound message is being sent by the mod, not the engine (not sent across network) (for plugins)
#define SND_FL_GLOBAL (1<<11) // sound message skips the PAS check (not sent across network)
#define SND_FL_RELIABLE (1<<12) // sound uses the reliable channel (not sent across network)

// plats
#define PLAT_LOW_TRIGGER 1
Expand Down
7 changes: 5 additions & 2 deletions dlls/eng_wrappers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -657,10 +657,13 @@ void MESSAGE_END() {

void EMIT_SOUND_DYN2(edict_t* pEntity, int channel, const char* pszSample, float volume, float attenuation, int fFlags, int pitch) {
bool sendPAS = channel != CHAN_STATIC && !(fFlags & SND_STOP);
bool reliable = !sendPAS;
int hookFlags = fFlags;
if (!sendPAS) {
fFlags |= SND_FL_RELIABLE | SND_FL_GLOBAL;
}
Vector origin = (pEntity->v.maxs + pEntity->v.mins) * 0.5f + pEntity->v.origin;

CALL_HOOKS_VOID(pfnEmitSound, pEntity, channel, pszSample, volume, attenuation, fFlags, pitch, origin, 0, reliable);
CALL_HOOKS_VOID(pfnEmitSound, pEntity, channel, pszSample, volume, attenuation, hookFlags, pitch, origin, 0);
g_engfuncs.pfnEmitSound(pEntity, channel, pszSample, volume, attenuation, fFlags, pitch);
}

Expand Down
2 changes: 1 addition & 1 deletion dlls/hooks/PluginHooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ struct HLCOOP_PLUGIN_HOOKS {
HOOK_RETURN_DATA (*pfnStartFrame)();

// called when a sound is about to be played, after the mod has handled sound replacement
HOOK_RETURN_DATA (*pfnEmitSound)(edict_t* pEntity, int channel, const char* pszSample, float volume, float attenuation, int fFlags, int pitch, const float* origin, uint32_t recipients, bool reliable);
HOOK_RETURN_DATA (*pfnEmitSound)(edict_t* pEntity, int channel, const char* pszSample, float volume, float attenuation, int fFlags, int pitch, const float* origin, uint32_t recipients);

// called when an ambient sound is about to be played, after the mod has handled sound replacement
HOOK_RETURN_DATA (*pfnEmitAmbientSound)(edict_t* pEntity, const float* vecPos, const char* pszSample, float vol, float attenuation, int fFlags, int pitch);
Expand Down
4 changes: 2 additions & 2 deletions dlls/monster/CApache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ void CApache :: Flight( void )
// make rotor, engine sounds
if (m_iSoundState == 0)
{
StartSound(edict(), CHAN_ITEM, "apache/ap_rotor2.wav", 1.0f, 0.3f, 0, 110, g_vecZero, 0xffffffff, false);
StartSound(edict(), CHAN_ITEM, "apache/ap_rotor2.wav", 1.0f, 0.3f, 0, 110, g_vecZero, 0xffffffff);
//EMIT_SOUND_DYN(ENT(pev), CHAN_STATIC, "apache/ap_rotor2.wav", 1.0, 0.3, 0, 110 );
// EMIT_SOUND_DYN(ENT(pev), CHAN_STATIC, "apache/ap_whine1.wav", 0.5, 0.2, 0, 110 );

Expand Down Expand Up @@ -771,7 +771,7 @@ void CApache :: Flight( void )
}

StartSound(edict(), CHAN_ITEM, "apache/ap_rotor2.wav", 1.0f, 0.3f, SND_CHANGE_PITCH,
pitch, g_vecZero, PLRBIT(pPlayer->edict()), false);
pitch, g_vecZero, PLRBIT(pPlayer->edict()));
lastPitch[pPlayer->entindex()] = pitch;
}
// EMIT_SOUND_DYN(ENT(pev), CHAN_STATIC, "apache/ap_whine1.wav", flVol, 0.2, SND_CHANGE_PITCH | SND_CHANGE_VOL, pitch);
Expand Down
4 changes: 2 additions & 2 deletions dlls/monster/COsprey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ void COsprey::Flight( )
// make rotor, engine sounds
if (m_iSoundState == 0)
{
StartSound(edict(), CHAN_ITEM, "apache/ap_rotor4.wav", 1.0f, 0.3f, 0, 110, g_vecZero, 0xffffffff, false);
StartSound(edict(), CHAN_ITEM, "apache/ap_rotor4.wav", 1.0f, 0.3f, 0, 110, g_vecZero, 0xffffffff);
memset(lastPitch, 0, sizeof(int) * 33);
m_iSoundState = SND_CHANGE_PITCH; // hack for going through level transitions
}
Expand Down Expand Up @@ -514,7 +514,7 @@ void COsprey::Flight( )
}

StartSound(edict(), CHAN_ITEM, "apache/ap_rotor4.wav", 1.0f, 0.3f, SND_CHANGE_PITCH,
pitch, g_vecZero, PLRBIT(pPlayer->edict()), false);
pitch, g_vecZero, PLRBIT(pPlayer->edict()));
lastPitch[pPlayer->entindex()] = pitch;
}
}
Expand Down
2 changes: 1 addition & 1 deletion dlls/monster/CRoboGrunt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ void CRoboGrunt::TraceAttack(entvars_t* pevAttacker, float flDamage, Vector vecD
}

if (ptr->iHitgroup == HITGROUP_HEAD) {
StartSound(edict(), CHAN_BODY, RANDOM_SOUND_ARRAY(pHeadshotSounds), 1.0f, ATTN_NORM, 0, RANDOM_LONG(90, 110), pev->origin, 0xffffffff, false);
StartSound(edict(), CHAN_BODY, RANDOM_SOUND_ARRAY(pHeadshotSounds), 1.0f, ATTN_NORM, 0, RANDOM_LONG(90, 110), pev->origin, 0xffffffff);

Vector sprPos = ptr->vecEndPos - Vector(0, 0, 10);

Expand Down
18 changes: 10 additions & 8 deletions dlls/sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,9 +520,9 @@ mstream* BuildStartSoundMessage(edict_t* ent, int channel, const char* sample, f


void StartSound(edict_t* ent, int channel, const char* sample, float fvolume, float attenuation,
int fFlags, int pitch, const float* origin, uint32_t messageTargets, BOOL reliable)
int fFlags, int pitch, const float* origin, uint32_t messageTargets)
{
CALL_HOOKS_VOID(pfnEmitSound, ent, channel, sample, fvolume, attenuation, fFlags | SND_FL_MOD, pitch, origin, messageTargets, reliable);
CALL_HOOKS_VOID(pfnEmitSound, ent, channel, sample, fvolume, attenuation, fFlags | SND_FL_MOD, pitch, origin, messageTargets);

mstream* bitbuffer = BuildStartSoundMessage(ent, channel, sample, fvolume, attenuation, fFlags, pitch, origin);

Expand All @@ -535,6 +535,8 @@ void StartSound(edict_t* ent, int channel, const char* sample, float fvolume, fl
int msgSz = bitbuffer->tell() + 1;
//bool anyMessagesWritten = false;

int sendMode = (fFlags & SND_FL_RELIABLE) ? MSG_ONE : MSG_ONE_UNRELIABLE;

for (int i = 1; i <= gpGlobals->maxClients; i++) {
edict_t* plent = INDEXENT(i);
uint32_t playerBit = PLRBIT(plent);
Expand All @@ -543,8 +545,8 @@ void StartSound(edict_t* ent, int channel, const char* sample, float fvolume, fl
continue;
}

if (bent->InPAS(plent)) {
MESSAGE_BEGIN(reliable ? MSG_ONE : MSG_ONE_UNRELIABLE, SVC_SOUND, NULL, plent);
if ((fFlags & SND_FL_GLOBAL) || bent->InPAS(plent)) {
MESSAGE_BEGIN(sendMode, SVC_SOUND, NULL, plent);
WRITE_BYTES((uint8_t*)bitbuffer->getBuffer(), msgSz);
MESSAGE_END();
}
Expand All @@ -561,9 +563,9 @@ void StartSound(edict_t* ent, int channel, const char* sample, float fvolume, fl
}

void StartSound(int eidx, int channel, const char* sample, float volume, float attenuation,
int fFlags, int pitch, const float* origin, uint32_t messageTargets, BOOL reliable) {
int fFlags, int pitch, const float* origin, uint32_t messageTargets) {
StartSound(INDEXENT(eidx), channel, sample, volume, attenuation, fFlags, pitch, origin,
messageTargets, reliable);
messageTargets);
}

void EMIT_SOUND_DYN(edict_t *entity, int channel, const char *sample, float volume, float attenuation,
Expand Down Expand Up @@ -635,7 +637,7 @@ void EMIT_SOUND_DYN(edict_t *entity, int channel, const char *sample, float volu
}
else {
// play the sound normally for players that can see the ent
StartSound(entity, channel, sample, volume, attenuation, flags, pitch, ori, PLRBIT(plr), true);
StartSound(entity, channel, sample, volume, attenuation, flags | SND_FL_RELIABLE, pitch, ori, PLRBIT(plr));
}
}
}
Expand Down Expand Up @@ -715,7 +717,7 @@ void PLAY_DISTANT_SOUND(edict_t* emitter, int soundType) {
// randomize pitch per entity, so you get a better idea of how many players/npcs are shooting
int pitch = 95 + ((ENTINDEX(emitter) * 7) % 11);

StartSound((edict_t*)NULL, CHAN_STATIC, sample, volume, attn, 0, pitch, emitter->v.origin, pbits, false);
StartSound((edict_t*)NULL, CHAN_STATIC, sample, volume, attn, SND_FL_GLOBAL, pitch, emitter->v.origin, pbits);
}
}

Expand Down
4 changes: 2 additions & 2 deletions dlls/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -661,10 +661,10 @@ EXPORT mstream* BuildStartSoundMessage(edict_t* entity, int channel, const char*
// play the sound for players with bits contained in messageTargets
// a player bit = 1 << (ENTINDEX(player_edict) % 31)
EXPORT void StartSound(edict_t* entity, int channel, const char* sample, float volume, float attenuation,
int fFlags, int pitch, const float* origin, uint32_t messageTargets, BOOL reliable);
int fFlags, int pitch, const float* origin, uint32_t messageTargets);

EXPORT void StartSound(int eidx, int channel, const char* sample, float volume, float attenuation,
int fFlags, int pitch, const float* origin, uint32_t messageTargets, BOOL reliable);
int fFlags, int pitch, const float* origin, uint32_t messageTargets);

inline void EMIT_SOUND(edict_t *entity, int channel, const char *sample, float volume, float attenuation)
{
Expand Down
2 changes: 1 addition & 1 deletion dlls/weapon/CBasePlayerWeapon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ BOOL CBasePlayerWeapon::PlayEmptySound(void)
edict_t* plr = m_pPlayer->edict();
uint32_t messageTargets = 0xffffffff & ~PLRBIT(plr);
StartSound(plr, CHAN_WEAPON, "weapons/357_cock1.wav", 0.8f,
ATTN_NORM, SND_FL_PREDICTED, 100, m_pPlayer->pev->origin, messageTargets, false);
ATTN_NORM, SND_FL_PREDICTED, 100, m_pPlayer->pev->origin, messageTargets);
#endif
m_iPlayEmptySound = 0;
return 0;
Expand Down
2 changes: 1 addition & 1 deletion dlls/weapon/CGlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ void CGlock::Reload( void )
messageTargets &= ~PLRBIT(m_pPlayer->edict());
}
StartSound(m_pPlayer->edict(), CHAN_ITEM, "weapons/glock_reload.wav", 0.8f,
ATTN_NORM, SND_FL_PREDICTED, 93 + RANDOM_LONG(0, 15), m_pPlayer->pev->origin, messageTargets, false);
ATTN_NORM, SND_FL_PREDICTED, 93 + RANDOM_LONG(0, 15), m_pPlayer->pev->origin, messageTargets);

m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + UTIL_SharedRandomFloat( m_pPlayer->random_seed, 10, 15 );
#endif
Expand Down
2 changes: 1 addition & 1 deletion dlls/weapon/CMP5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ void CMP5::Reload( void )
messageTargets &= ~PLRBIT(m_pPlayer->edict());
}
StartSound(m_pPlayer->edict(), CHAN_ITEM, "weapons/9mmar_reload.wav", 0.8f,
ATTN_NORM, SND_FL_PREDICTED, 93 + RANDOM_LONG(0, 15), m_pPlayer->pev->origin, messageTargets, false);
ATTN_NORM, SND_FL_PREDICTED, 93 + RANDOM_LONG(0, 15), m_pPlayer->pev->origin, messageTargets);
#endif
}
}
Expand Down
2 changes: 1 addition & 1 deletion dlls/weapon/CPython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ void CPython::Reload( void )
messageTargets &= ~PLRBIT(m_pPlayer->edict());
}
StartSound(m_pPlayer->edict(), CHAN_ITEM, "weapons/357_reload1.wav", 0.8f,
ATTN_NORM, SND_FL_PREDICTED, 93 + RANDOM_LONG(0, 15), m_pPlayer->pev->origin, messageTargets, false);
ATTN_NORM, SND_FL_PREDICTED, 93 + RANDOM_LONG(0, 15), m_pPlayer->pev->origin, messageTargets);
#endif
}
}
Expand Down
4 changes: 2 additions & 2 deletions pm_shared/pm_shared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ typedef struct hull_s
} hull_t;

void StartSound(int eidx, int channel, const char* sample, float volume, float attenuation,
int fFlags, int pitch, const float* origin, uint32_t messageTargets, BOOL reliable);
int fFlags, int pitch, const float* origin, uint32_t messageTargets);

inline uint32_t PLRBIT(int eidx) { return 1 << (eidx & 31); }

Expand All @@ -95,7 +95,7 @@ inline uint32_t PLRBIT(int eidx) { return 1 << (eidx & 31); }
#else
#define PLAY_MOVEMENT_SOUND(channel, sample, volume, attenuation, fFlags, pitch) \
StartSound(pmove->player_index+1, channel, sample, volume, attenuation, fFlags | SND_FL_PREDICTED, \
pitch, shared_vec3_origin, 0xffffffff & ~PLRBIT(pmove->player_index+1), false);
pitch, shared_vec3_origin, 0xffffffff & ~PLRBIT(pmove->player_index+1));
#endif


Expand Down

0 comments on commit a9a9cb5

Please sign in to comment.