Код плагина:
#pragma semicolon 1
#define PLUGIN_VERSION "2.0.0"
#define LoopValidPlayers(%1) for(int %1 = 1; %1 <= MaxClients; %1++) if(IsValidClient(%1))
#include <sourcemod>
#include <sdkhooks>
#include <sdktools>
#include <menus>
#pragma newdecls required
int g_iMaxTeamPlayers = 2;
ConVar gcvTeamCnt;
int gSavedPlayerTeams[MAXPLAYERS+1] = {-1, ...};
Menu gCurrentMenu;
Menu gEmptyMenu;
char a_sTeamNames[][] = { "Alpha", "Beta", "Gamma", "Delta", "Zeta", "Lambda", "Omicron", "Sigma", "Omega" };
public Plugin myinfo =
{
name = "Danger Zone Team Selection",
author = "That One Guy + Lion Doge",
description = "Provides menus for team selection at the start of a match in danger zone (CS:GO).",
version = PLUGIN_VERSION,
url = "https://www.togcoding.com/togcoding/"
}
public void OnPluginStart()
{
gEmptyMenu = new Menu(MenuCB_TeamSelection);
CreateConVar("tdzts_version", PLUGIN_VERSION, "TOG Danger Zone Team Selection - version number.", FCVAR_NOTIFY|FCVAR_DONTRECORD);
//HookEvent("player_team", Event_TeamJoined, EventHookMode_Post);
HookEvent("round_start", Event_RoundStart, EventHookMode_Post);
HookEvent("player_spawn", Event_PlayerSpawn);
RegConsoleCmd("sm_jointeam", Cmd_JoinTeam, "Joins a team in danger zone.");
RegConsoleCmd("sm_redrawmenu", CMD_RedrawMenu);
gcvTeamCnt = FindConVar("sv_dz_team_count");
HookConVarChange(gcvTeamCnt, ConVarChanged);
gcvTeamCnt.SetInt(2);
g_iMaxTeamPlayers = GetConVarInt(gcvTeamCnt);
gCurrentMenu = GenerateMenu();
RedrawMenus();
}
public void ConVarChanged(ConVar convar, const char[] oldValue, const char[] newValue)
{
g_iMaxTeamPlayers = GetConVarInt(gcvTeamCnt);
CreateTimer(0.05, Timer_RedrawMenus);
}
void UpdateTeamCount()
{
int count = GetClientCount();
if(count >= 21)
{
SetConVarInt(gcvTeamCnt, 3);
}
else
{
SetConVarInt(gcvTeamCnt, 2);
}
}
public void OnClientPutInServer(int client)
{
ShowPlayerMenu(client);
UpdateTeamCount();
}
public void OnClientDisconnect(int client)
{
gSavedPlayerTeams[client] = 0;
RedrawMenus();
UpdateTeamCount();
}
Menu GenerateMenu()
{
int a_iSurvivalTeam[MAXPLAYERS + 1] = {-1, ...};
Menu oMenu = new Menu(MenuCB_TeamSelection);
oMenu.SetTitle("- Team Selection (Max %i per team) -", g_iMaxTeamPlayers);
oMenu.ExitButton = false;
oMenu.ExitBackButton = false;
SetMenuPagination(oMenu, MENU_NO_PAGINATION);
ArrayList a_sEmptyTeams;
a_sEmptyTeams = new ArrayList(50);
a_sEmptyTeams.Clear();
// int desiredTeamCount = RoundToCeil(GetClientCount() / 2.0)
// if(GetClientCount() > g_iMaxTeamPlayers * sizeof(a_sTeamNames)) // add more additional teams
// {
// for(int i=0; i<desiredTeamCount; i++)
// {
// char sBuffer[64];
// Format(sBuffer, sizeof(sBuffer), "Team %i", i+1+sizeof(a_sTeamNames));
// oMenu.AddItem(sBuffer, sBuffer)
// }
// }
// else
// {
// oMenu.Pagination = MENU_NO_PAGINATION;
// }
for(int iTeamNameSlot = 0; iTeamNameSlot < sizeof(a_sTeamNames); iTeamNameSlot++)
{
char sBuffer[300];
Format(sBuffer, sizeof(sBuffer), "%s", a_sTeamNames[iTeamNameSlot], iTeamNameSlot + 1);
int iPlyrCnt = 0;
bool bFirst = true;
LoopValidPlayers(i)
{
a_iSurvivalTeam[i] = GetEntProp(i, Prop_Send, "m_nSurvivalTeam");
if(a_iSurvivalTeam[i] != iTeamNameSlot)
{
continue;
}
if(bFirst)
{
Format(sBuffer, sizeof(sBuffer), "%s (%N)", sBuffer, i);
bFirst = false;
}
else
{
Format(sBuffer, sizeof(sBuffer), "%s, (%N)", sBuffer, i);
}
iPlyrCnt++;
}
oMenu.AddItem(a_sTeamNames[iTeamNameSlot], sBuffer);
}
return oMenu;
}
void ShowPlayerMenu(int client)
{
if(gCurrentMenu == INVALID_HANDLE) GenerateMenu();
if(!!GameRules_GetProp("m_bWarmupPeriod")==false) {
return;
}
if(IsValidClient(client))
gCurrentMenu.Display(client, MENU_TIME_FOREVER);
}
void JoinDZTeam(int client, int team)
{
if(!!GameRules_GetProp("m_bWarmupPeriod")==false) return;
ServerCommand("dz_jointeam %i %i", team, GetClientUserId(client));
gSavedPlayerTeams[client] = team;
CreateTimer(0.04, Timer_RedrawMenus);
}
public Action Timer_RedrawMenus(Handle timer)
{
RedrawMenus();
return Plugin_Handled;
}
public int MenuCB_TeamSelection(Menu oMenu, MenuAction action, int client, int param2)
{
if(action == MenuAction_End)
{
return 0;
}
if(action == MenuAction_Select)
{
JoinDZTeam(client, param2+1);
gSavedPlayerTeams[client] = param2+1;
}
return MenuStyle_Radio;
}
public Action CMD_RedrawMenu(int client, int args)
{
if(!!GameRules_GetProp("m_bWarmupPeriod")==false)
return Plugin_Handled;
RedrawMenus();
return Plugin_Handled;
}
void RedrawMenus()
{
gCurrentMenu.Cancel();
delete gCurrentMenu;
gCurrentMenu = GenerateMenu();
LoopValidPlayers(i)
{
ShowPlayerMenu(i);
}
}
public Action Event_PlayerSpawn(Event hEvent, const char[] sName, bool bDontBroadcast)
{
int iUserID = hEvent.GetInt("userid");
int client = GetClientOfUserId(iUserID);
if(!IsValidClient(client))
{
return Plugin_Continue;
}
ShowPlayerMenu(client);
CreateTimer(0.04, Timer_RedrawMenus); // workaround
return Plugin_Continue;
}
public Action Event_RoundStart(Event hEvent, const char[] sName, bool bDontBroadcast)
{
if(!!GameRules_GetProp("m_bWarmupPeriod")==false)
{
// close menus
LoopValidPlayers(i)
{
gCurrentMenu.Cancel();
CancelClientMenu(i, true);
gEmptyMenu.Display(i, 1);
}
return Plugin_Continue;
}
else {
gCurrentMenu.Cancel();
}
for(int i=0; i<sizeof(gSavedPlayerTeams); i++)
{
if(IsValidClient(i) && gSavedPlayerTeams[i] > 0)
{
JoinDZTeam(i, gSavedPlayerTeams[i]);
}
}
return Plugin_Continue;
}
public Action Event_TeamJoined(Event hEvent, const char[] sName, bool bDontBroadcast)
{
int client = GetClientOfUserId(hEvent.GetInt("userid"));
if(IsValidClient(client))
{
RedrawMenus();
}
return Plugin_Continue;
}
public Action Cmd_JoinTeam(int client, int iArgs)
{
if(!IsValidClient(client))
{
ReplyToCommand(client, "You must be in game to use this command!");
return Plugin_Handled;
}
char sTeam[50];
int iValue = 0;
GetCmdArg(1, sTeam, sizeof(sTeam));
if(IsNumeric(sTeam))
{
iValue = StringToInt(sTeam);
JoinDZTeam(client, iValue);
}
else
{
for(int iTeamNameSlot = 0; iTeamNameSlot < sizeof(a_sTeamNames); iTeamNameSlot++)
{
char sTeamName[50], sTeamName2[50];
Format(sTeamName, sizeof(sTeamName), "%s", a_sTeamNames[iTeamNameSlot]);
Format(sTeamName2, sizeof(sTeamName2), "Team %s", a_sTeamNames[iTeamNameSlot]);
if(StrEqual(sTeamName, sTeam, false) || StrEqual(sTeamName2, sTeam, false))
{
JoinDZTeam(client, iTeamNameSlot + 1);
return Plugin_Handled;
}
}
ReplyToCommand(client, "Team not found. Please try again using either a team number or match the team name.");
return Plugin_Handled;
}
}
bool IsNumeric(char[] sString)
{
for(int i = sString[0] == '-' ? 1 : 0; i < strlen(sString); i++) //first char can be - for neg num
{
if(!IsCharNumeric(sString[i]))
{
return false;
}
}
return true;
}
bool IsValidClient(int client)
{
if(!(1 <= client <= MaxClients) || !IsClientInGame(client))
{
return false;
}
return true;
}
stock void Log(char[] sPath, const char[] sMsg, any ...) //TOG logging function - path is relative to logs folder.
{
char sLogFilePath[PLATFORM_MAX_PATH], sFormattedMsg[1500];
BuildPath(Path_SM, sLogFilePath, sizeof(sLogFilePath), "logs/%s", sPath);
VFormat(sFormattedMsg, sizeof(sFormattedMsg), sMsg, 3);
LogToFileEx(sLogFilePath, "%s", sFormattedMsg);
}
Последнее редактирование: