Skip to content

Commit

Permalink
Send silenced weapon sounds using !silencesound
Browse files Browse the repository at this point in the history
  • Loading branch information
xen-000 committed Oct 20, 2023
1 parent 37e4deb commit 2bdb7a4
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 54 deletions.
41 changes: 22 additions & 19 deletions src/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,40 +193,43 @@ CON_COMMAND_CHAT(stopsound, "stop weapon sounds")
return;

int iPlayer = player->GetPlayerSlot();
bool bSet = !g_playerManager->IsPlayerUsingStopSound(iPlayer);

ZEPlayer *pZEPlayer = g_playerManager->GetPlayer(iPlayer);

// Something has to really go wrong for this to happen
if (!pZEPlayer)
{
Warning("%s Tried to access a null ZEPlayer!!\n", player->GetPlayerName());
return;
}
g_playerManager->SetPlayerStopSound(iPlayer, bSet);

pZEPlayer->ToggleStopSound();
if (bSet)
g_playerManager->SetPlayerSilenceSound(iPlayer, false);

ClientPrint(player, HUD_PRINTTALK, CHAT_PREFIX "You have %s weapon effects", pZEPlayer->IsUsingStopSound() ? "disabled" : "enabled");
ClientPrint(player, HUD_PRINTTALK, CHAT_PREFIX "You have %s weapon effects.", bSet ? "disabled" : "enabled");
}

CON_COMMAND_CHAT(toggledecals, "toggle world decals, if you're into having 10 fps in ZE")
CON_COMMAND_CHAT(silencesound, "silence weapon sounds")
{
if (!player)
return;

int iPlayer = player->GetPlayerSlot();
bool bSet = !g_playerManager->IsPlayerUsingSilenceSound(iPlayer);

ZEPlayer *pZEPlayer = g_playerManager->GetPlayer(iPlayer);
g_playerManager->SetPlayerSilenceSound(iPlayer, bSet);

// Something has to really go wrong for this to happen
if (!pZEPlayer)
{
Warning("%s Tried to access a null ZEPlayer!!\n", player->GetPlayerName());
if (bSet)
g_playerManager->SetPlayerStopSound(iPlayer, false);

ClientPrint(player, HUD_PRINTTALK, CHAT_PREFIX "You have %s weapon sounds.", bSet ? "silenced" : "unsilenced");
}

CON_COMMAND_CHAT(toggledecals, "toggle world decals, if you're into having 10 fps in ZE")
{
if (!player)
return;
}

pZEPlayer->ToggleStopDecals();
int iPlayer = player->GetPlayerSlot();
bool bSet = !g_playerManager->IsPlayerUsingStopDecals(iPlayer);

g_playerManager->SetPlayerStopDecals(iPlayer, bSet);

ClientPrint(player, HUD_PRINTTALK, CHAT_PREFIX "You have %s world decals", pZEPlayer->IsUsingStopDecals() ? "disabled" : "enabled");
ClientPrint(player, HUD_PRINTTALK, CHAT_PREFIX "You have %s world decals.", bSet ? "disabled" : "enabled");
}

CON_COMMAND_CHAT(myuid, "test")
Expand Down
57 changes: 31 additions & 26 deletions src/cs2fixes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,42 +300,47 @@ void CS2Fixes::Hook_PostEvent(CSplitScreenSlot nSlot, bool bLocalOnly, int nClie
INetworkSerializable* pEvent, const void* pData, unsigned long nSize, NetChannelBufType_t bufType)
{
// Message( "Hook_PostEvent(%d, %d, %d, %lli)\n", nSlot, bLocalOnly, nClientCount, clients );
// Need to explicitly get a pointer to the right function as it's overloaded and SH_CALL can't resolve that
static void (IGameEventSystem::*PostEventAbstract)(CSplitScreenSlot, bool, int, const uint64 *,
INetworkSerializable *, const void *, unsigned long, NetChannelBufType_t) = &IGameEventSystem::PostEventAbstract;

NetMessageInfo_t* info = pEvent->GetNetMessageInfo();
NetMessageInfo_t *info = pEvent->GetNetMessageInfo();

// Default silenced weapon sounds
// Ideally this would be toggleable betwen default/silenced, but does not seem possible to send new protobuf msgs currently
if (info->m_MessageId == GE_FireBulletsId)
{
CMsgTEFireBullets* msg = (CMsgTEFireBullets*)pData;
if (g_playerManager->GetSilenceSoundMask())
{
// Post the silenced sound to those who use silencesound
// Creating a new event object requires us to include the protobuf c files which I didn't feel like doing yet
// So instead just edit the event in place and reset later
CMsgTEFireBullets *msg = (CMsgTEFireBullets *)pData;

// original weapon_id will override new settings if not removed
msg->set_weapon_id(0);
msg->set_sound_type(10);
msg->set_item_def_index(60); // weapon_m4a1_silencer
}
int32_t weapon_id = msg->weapon_id();
int32_t sound_type = msg->sound_type();
int32_t item_def_index = msg->item_def_index();

if (info->m_MessageId == GE_FireBulletsId || info->m_MessageId == TE_WorldDecalId)
{
// Can later do a bit mask for players using stopsound but this will do for now
for (uint64 i = 0; i < MAXPLAYERS; i++)
{
ZEPlayer *pPlayer = g_playerManager->GetPlayer(i);
// original weapon_id will override new settings if not removed
msg->set_weapon_id(0);
msg->set_sound_type(10);
msg->set_item_def_index(60); // weapon_m4a1_silencer

// A client might be already excluded from the event possibly due to being too far away, so ignore them
if (!(*(uint64 *)clients & ((uint64)1 << i)))
continue;
uint64 clientMask = *(uint64 *)clients & g_playerManager->GetSilenceSoundMask();

if (!pPlayer)
continue;
SH_CALL(g_gameEventSystem, PostEventAbstract)
(nSlot, bLocalOnly, nClientCount, &clientMask, pEvent, msg, nSize, bufType);

if ((info->m_MessageId == GE_FireBulletsId && pPlayer->IsUsingStopSound()) ||
(info->m_MessageId == TE_WorldDecalId && pPlayer->IsUsingStopDecals()))
{
*(uint64*)clients &= ~((uint64)1 << i);
nClientCount--;
}
msg->set_weapon_id(weapon_id);
msg->set_sound_type(sound_type);
msg->set_item_def_index(item_def_index);
}

// Filter out people using stop/silence sound from the original event
*(uint64 *)clients &= ~g_playerManager->GetStopSoundMask();
*(uint64 *)clients &= ~g_playerManager->GetSilenceSoundMask();
}
else if (info->m_MessageId == TE_WorldDecalId)
{
*(uint64 *)clients &= ~g_playerManager->GetStopDecalsMask();
}
}

Expand Down
37 changes: 36 additions & 1 deletion src/playermanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ bool CPlayerManager::OnClientConnected(CPlayerSlot slot)
pPlayer->SetConnected();
m_vecPlayers[slot.Get()] = pPlayer;
m_UserIdLookup[g_pEngineServer2->GetPlayerUserId(slot).Get()] = slot.Get();

ResetPlayerFlags(slot.Get());

return true;
}
Expand All @@ -96,6 +98,8 @@ void CPlayerManager::OnClientDisconnect(CPlayerSlot slot)
delete m_vecPlayers[slot.Get()];
m_vecPlayers[slot.Get()] = nullptr;
m_UserIdLookup[g_pEngineServer2->GetPlayerUserId(slot).Get()] = -1;

ResetPlayerFlags(slot.Get());
}

void CPlayerManager::TryAuthenticate()
Expand Down Expand Up @@ -302,4 +306,35 @@ ZEPlayer *CPlayerManager::GetPlayerFromUserId(int userid)
return nullptr;

return m_vecPlayers[m_UserIdLookup[userid]];
}
}

void CPlayerManager::SetPlayerStopSound(int slot, bool set)
{
if (set)
m_nUsingStopSound |= ((uint64)1 << slot);
else
m_nUsingStopSound &= ~((uint64)1 << slot);
}

void CPlayerManager::SetPlayerSilenceSound(int slot, bool set)
{
if (set)
m_nUsingSilenceSound |= ((uint64)1 << slot);
else
m_nUsingSilenceSound &= ~((uint64)1 << slot);
}

void CPlayerManager::SetPlayerStopDecals(int slot, bool set)
{
if (set)
m_nUsingStopDecals |= ((uint64)1 << slot);
else
m_nUsingStopDecals &= ~((uint64)1 << slot);
}

void CPlayerManager::ResetPlayerFlags(int slot)
{
SetPlayerStopSound(slot, false);
SetPlayerSilenceSound(slot, false);
SetPlayerStopDecals(slot, true);
}
29 changes: 21 additions & 8 deletions src/playermanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ class ZEPlayer
ZEPlayer(CPlayerSlot slot, bool m_bFakeClient = false): m_slot(slot), m_bFakeClient(m_bFakeClient)
{
m_bAuthenticated = false;
m_bStopSound = false;
m_bStopDecals = true;
m_iAdminFlags = 0;
m_SteamID = nullptr;
m_bGagged = false;
Expand All @@ -70,12 +68,8 @@ class ZEPlayer
void SetTransmit(int index, bool shouldTransmit) { shouldTransmit ? m_shouldTransmit.Set(index) : m_shouldTransmit.Clear(index); }
void ClearTransmit() { m_shouldTransmit.ClearAll(); }
void SetHideDistance(int distance) { m_iHideDistance = distance; }
void ToggleStopSound() { m_bStopSound = !m_bStopSound; }
void ToggleStopDecals() { m_bStopDecals = !m_bStopDecals; }
void SetTotalDamage(int damage) { m_iTotalDamage = damage; }

bool IsUsingStopSound() { return m_bStopSound; }
bool IsUsingStopDecals() { return m_bStopDecals; }
bool IsMuted() { return m_bMuted; }
bool IsGagged() { return m_bGagged; }
bool ShouldBlockTransmit(int index) { return m_shouldTransmit.Get(index); }
Expand All @@ -91,8 +85,6 @@ class ZEPlayer
bool m_bAuthenticated;
bool m_bConnected;
const CSteamID* m_SteamID;
bool m_bStopSound;
bool m_bStopDecals;
bool m_bFakeClient;
bool m_bMuted;
bool m_bGagged;
Expand All @@ -110,6 +102,9 @@ class CPlayerManager
{
V_memset(m_vecPlayers, 0, sizeof(m_vecPlayers));
V_memset(m_UserIdLookup, -1, sizeof(m_UserIdLookup));
m_nUsingStopSound = 0;
m_nUsingSilenceSound = 0;
m_nUsingStopDecals = -1; // On by default
}

bool OnClientConnected(CPlayerSlot slot);
Expand All @@ -123,9 +118,27 @@ class CPlayerManager
ETargetType TargetPlayerString(int iCommandClient, const char* target, int &iNumClients, int *clients);
ZEPlayer *GetPlayer(CPlayerSlot slot) { return m_vecPlayers[slot.Get()]; };

uint64 GetStopSoundMask() { return m_nUsingStopSound; }
uint64 GetSilenceSoundMask() { return m_nUsingSilenceSound; }
uint64 GetStopDecalsMask() { return m_nUsingStopDecals; }

void SetPlayerStopSound(int slot, bool set);
void SetPlayerSilenceSound(int slot, bool set);
void SetPlayerStopDecals(int slot, bool set);

void ResetPlayerFlags(int slot);

bool IsPlayerUsingStopSound(int slot) { return m_nUsingStopSound & ((uint64)1 << slot); }
bool IsPlayerUsingSilenceSound(int slot) { return m_nUsingSilenceSound & ((uint64)1 << slot); }
bool IsPlayerUsingStopDecals(int slot) { return m_nUsingStopDecals & ((uint64)1 << slot); }

private:
ZEPlayer* m_vecPlayers[MAXPLAYERS];
uint16 m_UserIdLookup[USHRT_MAX+1];

uint64 m_nUsingStopSound;
uint64 m_nUsingSilenceSound;
uint64 m_nUsingStopDecals;
};

extern CPlayerManager *g_playerManager;

0 comments on commit 2bdb7a4

Please sign in to comment.