Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma semicolon 1
- #define TEAM_NONE 0
- #define TEAM_SPEC 1
- #define TEAM_T 2
- #define TEAM_CT 3
- #include <cstrike>
- #include <sourcemod>
- #include <sdktools>
- new g_Tcount, g_CTcount;
- new bool:g_balance;
- public Plugin:myinfo =
- {
- name = "Team Balance",
- author = "Otstrel.ru Team",
- description = "Team Balance for SourceMod.",
- version = "1.2",
- url = "http://otstrel.ru"
- };
- public OnPluginStart()
- {
- HookEvent("player_death", Event_PlayerDeath);
- HookEvent("player_team", Event_PlayerTeam);
- }
- public OnMapStart()
- {
- g_Tcount = 0;
- g_CTcount = 0;
- for ( new i = 1; i <= MaxClients; i++ )
- {
- if ( IsClientInGame(i) )
- {
- ChangeTeamCount(GetClientTeam(i), 1);
- }
- }
- CheckBalance();
- }
- public Action:Event_PlayerDeath(Handle:event,const String:name[],bool:dontBroadcast)
- {
- if ( g_balance )
- {
- return;
- }
- new killer = GetClientOfUserId(GetEventInt(event, "killer"));
- new client = GetClientOfUserId(GetEventInt(event, "userid"));
- if ( client == killer || IsFakeClient(client) )
- {
- return;
- }
- new team = GetClientTeam(client);
- if ( team != TEAM_T && team != TEAM_CT )
- {
- return;
- }
- if ( team != ( (g_Tcount > g_CTcount) ? TEAM_T : TEAM_CT ) )
- {
- return;
- }
- team = team == TEAM_T ? TEAM_CT : TEAM_T;
- if(!IsValidClient(client))
- return;
- new Handle:pack = CreateDataPack();
- WritePackCell(pack, client);
- WritePackCell(pack, team);
- CreateTimer(0.1, Timer_ChangeClientTeam, pack);
- }
- public Action:Timer_ChangeClientTeam(Handle:timer, any:pack)
- {
- ResetPack(pack);
- new client = ReadPackCell(pack);
- new team = ReadPackCell(pack);
- CloseHandle(pack);
- ChangeClientTeam(client, team);
- }
- public Event_PlayerTeam(Handle:event, const String:name[], bool:dontBroadcast)
- {
- new oldTeam = GetEventInt(event, "oldteam");
- new newTeam = GetEventInt(event, "team");
- new bool:disconnect = GetEventBool(event, "disconnect");
- ChangeTeamCount(oldTeam, -1);
- if ( !disconnect )
- ChangeTeamCount(newTeam, 1);
- CheckBalance();
- }
- CheckBalance()
- {
- new diff = g_Tcount - g_CTcount;
- if ( diff < 0 ) diff = -diff;
- g_balance = diff <= 1;
- }
- ChangeTeamCount(team, diff)
- {
- switch ( team ) {
- case TEAM_T: {
- g_Tcount += diff;
- }
- case TEAM_CT: {
- g_CTcount += diff;
- }
- }
- }
- stock bool IsValidClient(int client)
- {
- if(client >= 1 && client <= MaxClients && IsClientInGame(client) && IsClientConnected(client) && !IsFakeClient(client) && !IsClientReplay(client) && !IsClientSourceTV(client))
- return true;
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement