#pragma semicolon 1
#include <sourcemod>
#include <sdktools>
#include <clientprefs>
#pragma newdecls required
#define PLUGIN_NAME "Toggle Map Music"
#define PLUGIN_VERSION "1.5"
#define MAX_EDICTS 2048
//Global Handles
Handle g_hClientVolCookie;
//Create ConVar handles
ConVar g_ConVar_FSubString;
ConVar g_ConVar_NSubString;
ConVar g_ConVar_Replay;
ConVar g_ConVar_Debug;
//Create global variables
float g_fCmdTime[MAXPLAYERS+1];
float g_fClientVol[MAXPLAYERS+1];
bool g_iSndDisabled[MAX_EDICTS];
bool disabled[MAXPLAYERS + 1];
bool g_bDebug;
bool g_bReplay;
char currentSample[PLATFORM_MAX_PATH];
char g_sFSubString[64];
char g_sNSubString[64];
public Plugin myinfo =
{
name = PLUGIN_NAME,
author = "GoD-Tony, The Count [Updated by Agent Wesker]",
description = "Allows clients to toggle ambient sounds played by the map",
version = PLUGIN_VERSION,
url = "http://steam-gamers.net"
};
public void OnPluginStart()
{
CreateConVar("sm_stopmusic_version", PLUGIN_VERSION, "Toggle Map Music", FCVAR_NOTIFY|FCVAR_DONTRECORD);
//File SubString ConVar
g_ConVar_FSubString = CreateConVar("sm_stopmusic_fsubstring", "music", "String to search for in file name, blank disables this function. Default = music");
GetConVarString(g_ConVar_FSubString, g_sFSubString, sizeof(g_sFSubString));
HookConVarChange(g_ConVar_FSubString, OnConVarChanged);
//Name SubString ConVar
g_ConVar_NSubString = CreateConVar("sm_stopmusic_nsubstring", "", "String to search for in targetname, blank disables this function. Default = blank");
GetConVarString(g_ConVar_NSubString, g_sNSubString, sizeof(g_sNSubString));
HookConVarChange(g_ConVar_NSubString, OnConVarChanged);
//Replay ConVar
g_ConVar_Replay = CreateConVar("sm_stopmusic_replay", "1", "Replay late sounds? Enable = 1", _, true, 0.0, true, 1.0);
g_bReplay = GetConVarBool(g_ConVar_Replay);
HookConVarChange(g_ConVar_Replay, OnConVarChanged);
//Debug ConVar
g_ConVar_Debug = CreateConVar("sm_stopmusic_debug", "0", "Print debug to chat. Enable = 1", _, true, 0.0, true, 1.0);
g_bDebug = GetConVarBool(g_ConVar_Debug);
HookConVarChange(g_ConVar_Debug, OnConVarChanged);
g_hClientVolCookie = RegClientCookie("togglemapmusic_vol", "Toggle Map Music volume cookie", CookieAccess_Protected);
HookEvent("round_start", Event_RoundStart, EventHookMode_PostNoCopy);
AddAmbientSoundHook(Hook_AmbientSound);
RegConsoleCmd("sm_music", Command_StopMusic, "Toggles map music");
RegConsoleCmd("sm_stopmusic", Command_StopMusic, "Toggles map music");
//Set volume level to default (late load)
for (int j = 1; j <= MaxClients; j++) {
OnClientPostAdminCheck(j);
}
}
public void OnConVarChanged(ConVar convar, const char[] oldVal, const char[] newVal)
{
if (convar == g_ConVar_FSubString) {
GetConVarString(convar, g_sFSubString, sizeof(g_sFSubString));
TrimString(g_sFSubString);
PrintToServer("StopMusic - Changed Music File SubString to: %s", g_sFSubString);
} else if (convar == g_ConVar_NSubString) {
GetConVarString(convar, g_sNSubString, sizeof(g_sNSubString));
TrimString(g_sNSubString);
PrintToServer("StopMusic - Changed TargetName SubString to: %s", g_sNSubString);
} else if (convar == g_ConVar_Debug) {
if (StringToInt(newVal) == 1) {
g_bDebug = true;
} else {
g_bDebug = false;
}
} else if (convar == g_ConVar_Replay) {
if (StringToInt(newVal) == 1) {
g_bReplay = true;
} else {
g_bReplay = false;
}
}
}
public Action Hook_AmbientSound(char sample[PLATFORM_MAX_PATH], int &entity, float &volume, int &level, int &pitch, float pos[3], int &flags, float &delay)
{
if (g_iSndDisabled[entity])
return Plugin_Handled;
if (flags != SND_SPAWNING)
return Plugin_Continue;
char entClass[64];
GetEntityClassname(entity, entClass, sizeof(entClass));
if (!StrEqual(entClass, "ambient_generic", false)) {
return Plugin_Continue;
}
char tName[64];
GetEntPropString(entity, Prop_Data, "m_iName", tName, sizeof(tName));
if (CheckSample(sample, tName)) {
if (g_bDebug) {
PrintToChatAll("Ambient: (%s) TargetName (%s) Flags (%i) ", sample, tName, flags);
}
volume = 0.0;
flags = SND_STOP;
g_iSndDisabled[entity] = true;
for (int j = 1; j <= MaxClients; j++) {
if (IsValidClient(j) && !disabled[j] && g_fClientVol[j] > 0.0) {
if (g_bDebug) {
PrintToConsole(j, "REPLAY: (%s) TargetName (%s) Flags (%i)", sample, tName, flags);
}
if (currentSample[0]) {
Client_StopSound(j, currentSample);
}
Client_SendSound(j, sample, g_fClientVol[j]);
}
}
currentSample = sample;
CreateTimer(5.0, Delay_Music, entity);
return Plugin_Changed;
}
return Plugin_Continue;
}
public Action Delay_Music(Handle timer, int entity) {
g_iSndDisabled[entity] = false;
return Plugin_Continue;
}
static bool IsValidClient(int client) {
if (!IsClientInGame(client)) {
return false;
}
if (!IsPlayerAlive(client)) {
return false;
}
return true;
}
public void OnClientPostAdminCheck(int client)
{
if (AreClientCookiesCached(client))
{
char sCookieValue[12];
GetClientCookie(client, g_hClientVolCookie, sCookieValue, sizeof(sCookieValue));
if (sCookieValue[0]) {
float cookieValue = StringToFloat(sCookieValue);
g_fClientVol[client] = cookieValue;
} else {
g_fClientVol[client] = 1.0;
}
} else {
g_fClientVol[client] = 1.0;
}
if (g_bReplay) {
if (currentSample[0]) {
Client_SendSound(client, currentSample, g_fClientVol[client]);
}
}
}
public void OnClientDisconnect_Post(int client)
{
g_fCmdTime[client] = 0.0;
disabled[client] = false;
}
public void Event_RoundStart(Handle event, const char[] name, bool dontBroadcast)
{
//Reset the current song
currentSample = "";
}
static bool CheckSample(const char[] sample, const char[] targetname)
{
int len = strlen(sample);
if (len > 4 && (StrEqual(sample[len-3], "mp3") || StrEqual(sample[len-3], "wav"))) {
if (g_sFSubString[0]) {
if (StrContains(sample, g_sFSubString, false) == -1) {
return false; //not found
}
}
if (g_sNSubString[0]) {
if (StrContains(targetname, g_sNSubString, false) == -1) {
return false; //not found
}
}
return true;
}
return false;
}
public int Music_Menu(Menu menu, MenuAction action, int client, int param)
{
if (action == MenuAction_Select)
{
if (param == 0) {
disabled[client] = true;
} else {
if (param == 1) {
g_fClientVol[client] = 0.25;
} else if (param == 2) {
g_fClientVol[client] = 0.5;
} else if (param == 3) {
g_fClientVol[client] = 1.0;
}
if (disabled[client]) {
if (g_bReplay) {
if (currentSample[0]) {
Client_SendSound(client, currentSample, g_fClientVol[client]);
}
}
}
disabled[client] = false;
}
char sCookieValue[12];
FloatToString(g_fClientVol[client], sCookieValue, sizeof(sCookieValue));
SetClientCookie(client, g_hClientVolCookie, sCookieValue);
char info[32];
menu.GetItem(param, info, sizeof(info));
PrintCenterText(client, "Map Music set to: %s", info);
if (disabled[client]) {
if (currentSample[0]) {
Client_StopSound(client, currentSample);
}
}
} else if (action == MenuAction_End)
{
delete menu;
}
}
public Action Command_StopMusic(int client, any args)
{
// Prevent this command from being spammed.
if (!client || g_fCmdTime[client] > GetGameTime())
return Plugin_Handled;
char toggleSelection[32];
if (disabled[client]) {
toggleSelection = "Off";
} else {
if (g_fClientVol[client] == 0.25) {
toggleSelection = "Low";
} else if (g_fClientVol[client] == 0.5) {
toggleSelection = "Medium";
} else if (g_fClientVol[client] == 1.0) {
toggleSelection = "High";
} else {
g_fClientVol[client] = 1.0;
toggleSelection = "High";
}
}
Menu menu = new Menu(Music_Menu);
menu.SetTitle("Toggle Map Music (Currently: %s)", toggleSelection);
menu.AddItem("off", "Off");
menu.AddItem("low", "Low");
menu.AddItem("medium", "Medium");
menu.AddItem("high", "High (Default)");
menu.ExitButton = false;
menu.Display(client, 20);
g_fCmdTime[client] = GetGameTime() + 5.0;
return Plugin_Handled;
}
stock void Client_SendSound(int client, const char[] name, float volume)
{
EmitSoundToClient(client, name, SOUND_FROM_PLAYER, SNDCHAN_STATIC, SNDLEVEL_NORMAL, SND_NOFLAGS, volume, SNDPITCH_NORMAL, _, _, _, true);
}
stock void Client_StopSound(int client, const char[] name)
{
EmitSoundToClient(client, name, SOUND_FROM_PLAYER, SNDCHAN_STATIC, SNDLEVEL_NONE, SND_STOP, 0.0, SNDPITCH_NORMAL, _, _, _, true);
}