Files
sm-plugins/Radio/scripting/radio/settings.sp
T
2026-10-22 16:17:56 +01:00

192 lines
4.6 KiB
SourcePawn

//----------------------------------------------------------------------------------------------------
// Radio settings: per-player preferences stored as clientprefs cookies, exposed as a "Radio" entry
// in the stock Client Preferences menu (sm_settings / !settings) that opens a sub-menu with the
// voice-line volume and a toggle for receiving the custom radio commands (thanks, cheer).
//----------------------------------------------------------------------------------------------------
static Cookie g_hVolumeCookie;
static Cookie g_hCustomCookie;
static int g_iVolume[MAXPLAYERS + 1];
static bool g_bCustom[MAXPLAYERS + 1];
static const int g_iVolumeSteps[] = {100, 75, 50, 25, 0};
#define RADIO_VOLUME_DEFAULT 100
void Settings_Init()
{
g_hVolumeCookie = new Cookie("radio_volume", "Radio voice line volume in percent (0 = off)", CookieAccess_Protected);
g_hCustomCookie = new Cookie("radio_custom", "Receive the custom radio commands (thanks, cheer)", CookieAccess_Protected);
SetCookieMenuItem(CookieMenu_Radio, 0, "Radio");
// Late load: clients may already be in game with cached cookies.
for (int i = 1; i <= MaxClients; i++)
{
g_iVolume[i] = RADIO_VOLUME_DEFAULT;
g_bCustom[i] = true;
if (IsClientConnected(i) && AreClientCookiesCached(i))
OnClientCookiesCached(i);
}
}
void Settings_OnClientConnected(int client)
{
// until the cookies arrive
g_iVolume[client] = RADIO_VOLUME_DEFAULT;
g_bCustom[client] = true;
}
public void OnClientCookiesCached(int client)
{
char val[8];
g_hVolumeCookie.Get(client, val, sizeof(val));
if (val[0]) // never set = keep the default
{
int volume = StringToInt(val);
if (volume < 0)
volume = 0;
else if (volume > 100)
volume = 100;
g_iVolume[client] = volume;
}
g_hCustomCookie.Get(client, val, sizeof(val));
if (val[0])
g_bCustom[client] = StringToInt(val) != 0;
}
float Settings_GetVolume(int client)
{
return g_iVolume[client] / 100.0;
}
bool Settings_GetCustom(int client)
{
return g_bCustom[client];
}
public void CookieMenu_Radio(int client, CookieMenuAction action, any info, char[] buffer, int maxlen)
{
switch (action)
{
case CookieMenuAction_DisplayOption:
{
FormatEx(buffer, maxlen, "Radio");
}
case CookieMenuAction_SelectOption:
{
Settings_ShowMainMenu(client);
}
}
}
static void Settings_ShowMainMenu(int client)
{
Menu menu = new Menu(Menu_RadioMain);
menu.SetTitle("Radio Settings");
char display[64];
FormatEx(display, sizeof(display), "Sound volume: %d%%", g_iVolume[client]);
menu.AddItem("volume", display);
FormatEx(display, sizeof(display), "Custom Radio (thanks, cheer): %s",
g_bCustom[client] ? "Enabled" : "Disabled");
menu.AddItem("custom", display);
menu.ExitBackButton = true; // back to sm_settings
menu.Display(client, MENU_TIME_FOREVER);
}
public int Menu_RadioMain(Menu menu, MenuAction action, int client, int param)
{
switch (action)
{
case MenuAction_Select:
{
char item[8];
menu.GetItem(param, item, sizeof(item));
if (StrEqual(item, "volume"))
{
Settings_ShowVolumeMenu(client);
}
else if (StrEqual(item, "custom"))
{
g_bCustom[client] = !g_bCustom[client];
g_hCustomCookie.Set(client, g_bCustom[client] ? "1" : "0");
Settings_ShowMainMenu(client); // re-show with the new state
}
}
case MenuAction_Cancel:
{
if (param == MenuCancel_ExitBack)
ShowCookieMenu(client);
}
case MenuAction_End:
{
delete menu;
}
}
return 0;
}
static void Settings_ShowVolumeMenu(int client)
{
Menu menu = new Menu(Menu_RadioVolume);
menu.SetTitle("Radio Sound Volume");
char item[8], display[16];
for (int i = 0; i < sizeof(g_iVolumeSteps); i++)
{
int volume = g_iVolumeSteps[i];
IntToString(volume, item, sizeof(item));
if (volume > 0)
FormatEx(display, sizeof(display), "%d%%", volume);
else
strcopy(display, sizeof(display), "Off");
if (volume == g_iVolume[client])
StrCat(display, sizeof(display), " <<");
menu.AddItem(item, display);
}
menu.ExitBackButton = true; // back to the Radio sub-menu
menu.Display(client, MENU_TIME_FOREVER);
}
public int Menu_RadioVolume(Menu menu, MenuAction action, int client, int param)
{
switch (action)
{
case MenuAction_Select:
{
char item[8];
menu.GetItem(param, item, sizeof(item));
int volume = StringToInt(item);
g_iVolume[client] = volume;
char val[8];
IntToString(volume, val, sizeof(val));
g_hVolumeCookie.Set(client, val);
Settings_ShowVolumeMenu(client); // re-show with the new pick marked
}
case MenuAction_Cancel:
{
if (param == MenuCancel_ExitBack)
Settings_ShowMainMenu(client);
}
case MenuAction_End:
{
delete menu;
}
}
return 0;
}