2018-12-16 00:22:29 +01:00
# pragma semicolon 1
# include <sourcemod>
# include <sdktools>
# include <cstrike>
# include <clientprefs>
2019-01-16 15:02:45 +01:00
# include <multicolors>
2018-12-16 00:22:29 +01:00
# define MESSAGE_RADIOTEXT 1
# define MESSAGE_SENDAUDIO 2
bool g_bStopRadioSounds [ MAXPLAYERS + 1 ] = { false , . . . } ;
2018-12-18 18:24:20 +01:00
bool g_bStopNadeSounds [ MAXPLAYERS + 1 ] = { false , . . . } ;
2019-01-16 15:02:45 +01:00
bool g_bStopRadioSoundsHooked = false ;
2018-12-16 00:22:29 +01:00
Handle g_hCookieStopRadio = null ;
2018-12-18 18:24:20 +01:00
Handle g_hCookieStopNade = null ;
2018-12-16 00:22:29 +01:00
public Plugin myinfo =
{
name = " SelfMuteRadio " ,
author = " Dogan + zaCade " ,
description = " Make it possible to self mute the radio (aka ignoread via command) " ,
version = " 1.0.0 " ,
url = " "
} ;
public void OnPluginStart ( )
{
UserMsg RadioText = GetUserMessageId ( " RadioText " ) ;
if ( RadioText = = INVALID_MESSAGE_ID )
SetFailState ( " This game does not support the \" RadioText \" UserMessage. " ) ;
UserMsg SendAudio = GetUserMessageId ( " SendAudio " ) ;
if ( SendAudio = = INVALID_MESSAGE_ID )
SetFailState ( " This game does not support the \" SendAudio \" UserMessage. " ) ;
2019-01-16 15:02:45 +01:00
RegConsoleCmd ( " sm_smradio " , OnToggleSelfMuteRadio , " Toggle Radio Self Mute " ) ;
//RegConsoleCmd("sm_radio", OnToggleSelfMuteRadio, "Toggle Radio Self Mute"); //GFL only
RegConsoleCmd ( " sm_smradio_nades " , OnToggleSelfMuteNade , " Toggle only Radio 'Fire in the hole' Self Mute " ) ;
2018-12-16 00:22:29 +01:00
g_hCookieStopRadio = RegClientCookie ( " radio_blocked " , " is the radio blocked " , CookieAccess_Protected ) ;
2018-12-18 18:24:20 +01:00
g_hCookieStopNade = RegClientCookie ( " nades_blocked " , " is the 'fire in the hole' radio blocked " , CookieAccess_Protected ) ;
2018-12-16 00:22:29 +01:00
2019-01-16 15:02:45 +01:00
SetCookieMenuItem ( MenuHandler_CookieMenu , 0 , " Radio Self Mute " ) ;
2018-12-16 00:22:29 +01:00
HookUserMessage ( RadioText , Hook_RadioText , true ) ;
HookUserMessage ( SendAudio , Hook_SendAudio , true ) ;
}
public Action Hook_RadioText ( UserMsg msg_id , Handle bf , const int [ ] players , int playersNum , bool reliable , bool init )
{
if ( ! g_bStopRadioSoundsHooked )
return Plugin_Continue ;
2018-12-18 18:24:20 +01:00
int dest = BfReadByte ( bf ) ;
int client = BfReadByte ( bf ) ;
char sSoundType [ 128 ] ;
BfReadString ( bf , sSoundType , sizeof ( sSoundType ) , false ) ;
char sSoundName [ 128 ] ;
BfReadString ( bf , sSoundName , sizeof ( sSoundName ) , false ) ;
char sSoundFile [ 128 ] ;
BfReadString ( bf , sSoundFile , sizeof ( sSoundFile ) , false ) ;
2018-12-16 00:22:29 +01:00
// Check which clients need to be excluded.
int [ ] newPlayers = new int [ playersNum ] ;
int newPlayersNum = 0 ;
for ( int i = 0 ; i < playersNum ; i + + )
{
int player = players [ i ] ;
2018-12-18 18:24:20 +01:00
if ( IsClientInGame ( player ) & & ! g_bStopRadioSounds [ player ] & & ! ( g_bStopNadeSounds [ player ] & & StrContains ( sSoundFile , " hole " , false ) ! = - 1 ) )
2018-12-16 00:22:29 +01:00
{
newPlayers [ newPlayersNum + + ] = player ;
}
}
if ( newPlayersNum = = playersNum )
{
// No clients where excluded.
return Plugin_Continue ;
}
else if ( newPlayersNum = = 0 )
{
// All clients were excluded and there is no need to broadcast.
return Plugin_Handled ;
}
DataPack pack = new DataPack ( ) ;
pack . WriteString ( sSoundType ) ;
pack . WriteString ( sSoundName ) ;
pack . WriteString ( sSoundFile ) ;
pack . WriteCell ( dest ) ;
pack . WriteCell ( client ) ;
pack . WriteCell ( newPlayersNum ) ;
for ( int i = 0 ; i < newPlayersNum ; i + + )
{
pack . WriteCell ( newPlayers [ i ] ) ;
}
RequestFrame ( OnRadioText , pack ) ;
return Plugin_Handled ;
}
public void OnRadioText ( DataPack pack )
{
pack . Reset ( ) ;
char sSoundType [ 128 ] ;
pack . ReadString ( sSoundType , sizeof ( sSoundType ) ) ;
char sSoundName [ 128 ] ;
pack . ReadString ( sSoundName , sizeof ( sSoundName ) ) ;
char sSoundFile [ 128 ] ;
pack . ReadString ( sSoundFile , sizeof ( sSoundFile ) ) ;
int dest = pack . ReadCell ( ) ;
int client = pack . ReadCell ( ) ;
int newPlayersNum = pack . ReadCell ( ) ;
int [ ] players = new int [ newPlayersNum ] ;
int playersNum = 0 ;
for ( int i = 0 ; i < newPlayersNum ; i + + )
{
int player = pack . ReadCell ( ) ;
if ( IsClientInGame ( player ) )
{
players [ playersNum + + ] = player ;
}
}
CloseHandle ( pack ) ;
Handle RadioText = StartMessage ( " RadioText " , players , playersNum , USERMSG_RELIABLE | USERMSG_BLOCKHOOKS ) ;
if ( RadioText ! = INVALID_HANDLE )
{
BfWriteByte ( RadioText , dest ) ;
BfWriteByte ( RadioText , client ) ;
BfWriteString ( RadioText , sSoundType ) ;
BfWriteString ( RadioText , sSoundName ) ;
BfWriteString ( RadioText , sSoundFile ) ;
}
EndMessage ( ) ;
}
public Action Hook_SendAudio ( UserMsg msg_id , Handle bf , const int [ ] players , int playersNum , bool reliable , bool init )
{
if ( ! g_bStopRadioSoundsHooked )
return Plugin_Continue ;
2018-12-18 18:24:20 +01:00
char sSoundFile [ 128 ] ;
BfReadString ( bf , sSoundFile , sizeof ( sSoundFile ) , false ) ;
2018-12-16 00:22:29 +01:00
// Check which clients need to be excluded.
int [ ] newPlayers = new int [ playersNum ] ;
int newPlayersNum = 0 ;
for ( int i = 0 ; i < playersNum ; i + + )
{
int player = players [ i ] ;
2018-12-18 18:24:20 +01:00
if ( IsClientInGame ( player ) & & ! g_bStopRadioSounds [ player ] & & ! ( g_bStopNadeSounds [ player ] & & StrContains ( sSoundFile , " hole " , false ) ! = - 1 ) )
2018-12-16 00:22:29 +01:00
{
newPlayers [ newPlayersNum + + ] = player ;
}
}
if ( newPlayersNum = = playersNum )
{
// No clients where excluded.
return Plugin_Continue ;
}
else if ( newPlayersNum = = 0 )
{
// All clients were excluded and there is no need to broadcast.
return Plugin_Handled ;
}
DataPack pack = new DataPack ( ) ;
pack . WriteString ( sSoundFile ) ;
pack . WriteCell ( newPlayersNum ) ;
for ( int i = 0 ; i < newPlayersNum ; i + + )
{
pack . WriteCell ( newPlayers [ i ] ) ;
}
RequestFrame ( OnSendAudio , pack ) ;
return Plugin_Handled ;
}
public void OnSendAudio ( DataPack pack )
{
pack . Reset ( ) ;
char sSoundFile [ 128 ] ;
pack . ReadString ( sSoundFile , sizeof ( sSoundFile ) ) ;
int newPlayersNum = pack . ReadCell ( ) ;
int [ ] players = new int [ newPlayersNum ] ;
int playersNum = 0 ;
for ( int i = 0 ; i < newPlayersNum ; i + + )
{
int player = pack . ReadCell ( ) ;
if ( IsClientInGame ( player ) )
{
players [ playersNum + + ] = player ;
}
}
CloseHandle ( pack ) ;
Handle SendAudio = StartMessage ( " SendAudio " , players , playersNum , USERMSG_RELIABLE | USERMSG_BLOCKHOOKS ) ;
if ( SendAudio ! = INVALID_HANDLE )
{
BfWriteString ( SendAudio , sSoundFile ) ;
}
EndMessage ( ) ;
}
2019-01-16 15:02:45 +01:00
public Action OnToggleSelfMuteRadio ( int client , int args )
{
ToggleSelfMuteRadio ( client ) ;
return Plugin_Handled ;
}
public Action OnToggleSelfMuteNade ( int client , int args )
{
ToggleSelfMuteNade ( client ) ;
return Plugin_Handled ;
}
public Action ToggleSelfMuteRadio ( int client )
2018-12-16 00:22:29 +01:00
{
g_bStopRadioSounds [ client ] = ! g_bStopRadioSounds [ client ] ;
2018-12-18 18:24:20 +01:00
CheckHooks ( ) ;
2018-12-16 00:22:29 +01:00
2019-01-16 15:02:45 +01:00
SetClientCookie ( client , g_hCookieStopRadio , g_bStopRadioSounds [ client ] ? " 1 " : " " ) ;
CPrintToChat ( client , " {cyan}[SelfMuteRadio] {white}%s " , g_bStopRadioSounds [ client ] ? " You self-muted all Radio Messages and Sounds. " : " You self-unmuted all Radio Messages and Sounds. " ) ;
2018-12-18 18:24:20 +01:00
}
2019-01-16 15:02:45 +01:00
public Action ToggleSelfMuteNade ( int client )
2018-12-18 18:24:20 +01:00
{
g_bStopNadeSounds [ client ] = ! g_bStopNadeSounds [ client ] ;
CheckHooks ( ) ;
2019-01-16 15:02:45 +01:00
SetClientCookie ( client , g_hCookieStopNade , g_bStopNadeSounds [ client ] ? " 1 " : " " ) ;
CPrintToChat ( client , " {cyan}[SelfMuteRadio] {white}%s " , g_bStopNadeSounds [ client ] ? " You self-muted 'Fire in the Hole' Radio Messages and Sounds. " : " You self-unmuted 'Fire in the Hole' Radio Messages and Sounds. " ) ;
2018-12-16 00:22:29 +01:00
}
public void OnClientCookiesCached ( int client )
{
char sBuffer [ 2 ] ;
GetClientCookie ( client , g_hCookieStopRadio , sBuffer , sizeof ( sBuffer ) ) ;
if ( sBuffer [ 0 ] ! = '\0' )
{
g_bStopRadioSounds [ client ] = true ;
g_bStopRadioSoundsHooked = true ;
}
else
{
g_bStopRadioSounds [ client ] = false ;
}
2018-12-18 18:24:20 +01:00
GetClientCookie ( client , g_hCookieStopRadio , sBuffer , sizeof ( sBuffer ) ) ;
if ( sBuffer [ 0 ] ! = '\0' )
{
g_bStopNadeSounds [ client ] = true ;
g_bStopRadioSoundsHooked = true ;
}
else
{
g_bStopNadeSounds [ client ] = false ;
}
2018-12-16 00:22:29 +01:00
}
public void OnClientDisconnect ( int client )
{
g_bStopRadioSounds [ client ] = false ;
2018-12-18 18:24:20 +01:00
g_bStopNadeSounds [ client ] = false ;
CheckHooks ( ) ;
2018-12-16 00:22:29 +01:00
}
2018-12-18 18:24:20 +01:00
public void CheckHooks ( )
2018-12-16 00:22:29 +01:00
{
bool bShouldHook = false ;
for ( int i = 1 ; i < = MaxClients ; i + + )
{
2018-12-18 18:24:20 +01:00
if ( g_bStopRadioSounds [ i ] | | g_bStopNadeSounds [ i ] )
2018-12-16 00:22:29 +01:00
{
bShouldHook = true ;
break ;
}
}
g_bStopRadioSoundsHooked = bShouldHook ;
}
2019-01-16 15:02:45 +01:00
public void ShowSettingsMenu ( int client )
{
Menu menu = new Menu ( MenuHandler_MainMenu ) ;
menu . SetTitle ( " SelfMuteRadio Settings " , client ) ;
char sBuffer [ 128 ] ;
Format ( sBuffer , sizeof ( sBuffer ) , " Self-Muting all Radio: %s " , g_bStopRadioSounds [ client ] ? " Enabled " : " Disabled " ) ;
menu . AddItem ( " 0 " , sBuffer ) ;
Format ( sBuffer , sizeof ( sBuffer ) , " Self-Muting Nades Radio only: %s " , g_bStopNadeSounds [ client ] ? " Enabled " : " Disabled " ) ;
menu . AddItem ( " 1 " , sBuffer ) ;
menu . ExitBackButton = true ;
menu . Display ( client , MENU_TIME_FOREVER ) ;
}
public void MenuHandler_CookieMenu ( int client , CookieMenuAction action , any info , char [ ] buffer , int maxlen )
{
switch ( action )
{
case ( CookieMenuAction_DisplayOption ) :
{
Format ( buffer , maxlen , " SelfMuteRadio " , client ) ;
}
case ( CookieMenuAction_SelectOption ) :
{
ShowSettingsMenu ( client ) ;
}
}
}
public int MenuHandler_MainMenu ( Menu menu , MenuAction action , int client , int selection )
{
switch ( action )
{
case ( MenuAction_Select ) :
{
switch ( selection )
{
case ( 0 ) : ToggleSelfMuteRadio ( client ) ;
case ( 1 ) : ToggleSelfMuteNade ( client ) ;
}
ShowSettingsMenu ( client ) ;
}
case ( MenuAction_Cancel ) :
{
ShowCookieMenu ( client ) ;
}
case ( MenuAction_End ) :
{
delete menu ;
}
}
}