-
Notifications
You must be signed in to change notification settings - Fork 6
/
teambalancer.sp
159 lines (129 loc) · 3.54 KB
/
teambalancer.sp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <sourcemod>
#pragma semicolon 1
#define PLUGIN_VERSION "1.0"
public Plugin:myinfo =
{
name = "Team Balancer",
author = "Stevo.TVR",
description = "Keeps the teams even",
version = PLUGIN_VERSION,
url = "http://www.theville.org/"
}
#define TEAM1 1
#define TEAM2 2
new Handle:sm_tb_enabled = INVALID_HANDLE;
new Handle:sm_tb_limitteams = INVALID_HANDLE;
new Handle:sm_tb_immunity = INVALID_HANDLE;
new bool:active = false;
new bool:playerDead[MAXPLAYERS+1];
public OnPluginStart()
{
CreateConVar("sm_teambalancer_version", PLUGIN_VERSION, "Insurgency Team Balancer version", FCVAR_PLUGIN|FCVAR_SPONLY|FCVAR_REPLICATED|FCVAR_NOTIFY);
sm_tb_enabled = CreateConVar("sm_tb_enabled", "1", "Enable team balancing", FCVAR_PLUGIN, true, 0.0, true, 1.0);
sm_tb_limitteams = CreateConVar("sm_tb_limitteams", "1", "Maximum difference between team counts", FCVAR_PLUGIN, true, 0.0);
sm_tb_immunity = CreateConVar("sm_tb_immunity", "1", "Make admins immune to team balancing", FCVAR_PLUGIN, true, 0.0, true, 1.0);
AutoExecConfig(true, "teambalancer");
HookEvent("player_death", Event_PlayerDeath);
HookEvent("player_spawn", Event_PlayerSpawn);
CreateTimer(10.0, Timer_CheckTeams, _, TIMER_REPEAT);
}
public OnMapStart()
{
new maxPlayers = GetMaxClients();
for(new i = 1; i <= maxPlayers; i++)
playerDead[i] = true;
}
public Event_PlayerDeath(Handle:event, const String:name[], bool:dontBroadcast)
{
new client = GetClientOfUserId(GetEventInt(event, "userid"));
playerDead[client] = true;
}
public Event_PlayerSpawn(Handle:event, const String:name[], bool:dontBroadcast)
{
new client = GetClientOfUserId(GetEventInt(event, "userid"));
playerDead[client] = false;
}
public Action:Timer_CheckTeams(Handle:Timer)
{
if(!GetConVarBool(sm_tb_enabled) || active)
return;
if(findUnevenTeam())
{
active = true;
PrintToChatAll("Teams will auto-balance in 5 seconds...");
CreateTimer(4.0, triggerBalance);
}
}
public Action:triggerBalance(Handle:Timer)
{
CreateTimer(1.0, balanceTeams, _, TIMER_REPEAT);
}
public Action:balanceTeams(Handle:Timer)
{
new team = findUnevenTeam();
if(team)
{
new maxPlayers = GetMaxClients();
// new arrayPlayers[maxPlayers];
// for(new i = 1; i <= maxPlayers; i++)
// arrayPlayers[i] = GetClientUserId(i);
// SortIntegers(arrayPlayers, maxPlayers, Sort_Descending);
for(new i = 1; i <= maxPlayers; i++)
{
if(IsClientConnected(i) && IsClientInGame(i) && playerDead[i] && !IsImmune(i))
{
if(GetClientTeam(i) == team)
{
switchPlayerTeam(i);
active = false;
return Plugin_Stop;
}
}
}
} else {
active = false;
return Plugin_Stop;
}
return Plugin_Continue;
}
public switchPlayerTeam(client)
{
new team = GetClientTeam(client);
if(team == TEAM1)
ChangeClientTeam(client, TEAM2);
if(team == TEAM2)
ChangeClientTeam(client, TEAM1);
}
public findUnevenTeam()
{
new team1 = 0;
new team2 = 0;
new maxPlayers = GetMaxClients();
for(new i = 1; i <= maxPlayers; i++)
{
if(IsClientConnected(i) && IsClientInGame(i))
{
new clientTeam = GetClientTeam(i);
if(clientTeam == TEAM1)
team1++;
if(clientTeam == TEAM2)
team2++;
}
}
if((team1 - team2) > GetConVarInt(sm_tb_limitteams))
return TEAM1;
if((team2 - team1) > GetConVarInt(sm_tb_limitteams))
return TEAM2;
return false;
}
public IsImmune(client)
{
new bool:immune = false;
if(GetConVarBool(sm_tb_immunity))
{
if(((GetUserFlagBits(client) & ADMFLAG_ROOT) == ADMFLAG_ROOT)
|| ((GetUserFlagBits(client) & ADMFLAG_CUSTOM1) == ADMFLAG_CUSTOM1))
immune = true;
}
return immune;
}