PHP Code:
/* Copyright © 2008, ConnorMcLeod
Instant AutoTeamBalance is free software;
you can redistribute it and/or modify it under the terms of the
GNU General Public License as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Instant AutoTeamBalance; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
#include <amxmodx>
#include <cstrike>
#define VERSION "DM 0.4.0"
#define BALANCE_IMMUNITY ADMIN_RCON
#define MAX_PLAYERS 32
enum {
aTerro,
aCt
}
new bool:g_bImmuned[MAX_PLAYERS+1]
new Float:g_fJoinedTeam[MAX_PLAYERS+1] = {-1.0, ...}
new g_pcvarCount, g_pcvarImmune, g_pCvarMessage
new g_iCounter
new mp_limitteams, mp_autoteambalance
public plugin_init()
{
register_plugin("AutoTeamBalance", VERSION, "ConnorMcLeod")
g_pcvarCount = register_cvar("atb_death_freq", "50")
g_pcvarImmune = register_cvar("atb_admins_immunity", "1")
g_pCvarMessage = register_cvar("iatb_message", "Teams Auto Balanced")
register_logevent("LogEvent_JoinTeam", 3, "1=joined team")
register_event("DeathMsg", "Event_DeathMsg", "a")
mp_limitteams = get_cvar_pointer("mp_limitteams")
mp_autoteambalance = get_cvar_pointer("mp_autoteambalance")
}
public Event_DeathMsg()
{
new iFreq = get_pcvar_num(g_pcvarCount)
if( !iFreq || !get_pcvar_num(mp_autoteambalance) )
return
if( ++g_iCounter < iFreq )
return
g_iCounter = 0
balance_teams()
}
public LogEvent_JoinTeam()
{
new loguser[80], name[32], id
read_logargv(0, loguser, 79)
parse_loguser(loguser, name, 31)
id = get_user_index(name)
g_fJoinedTeam[id] = get_gametime()
}
public client_authorized(id)
{
g_bImmuned[id] = bool:(get_user_flags(id) & BALANCE_IMMUNITY)
}
public client_disconnect(id)
{
g_iCounter = max(0, get_pcvar_num(g_pcvarCount) - 1)
}
balance_teams()
{
new iPlayers[MAX_PLAYERS], iNum
new aTeams[2][MAX_PLAYERS], aNum[2], id
get_players(iPlayers, iNum, "h")
for(new i; i<iNum; i++)
{
id = iPlayers[i]
switch( cs_get_user_team(id) )
{
case CS_TEAM_T:
{
aTeams[aTerro][aNum[aTerro]++] = id
}
case CS_TEAM_CT:
{
aTeams[aCt][aNum[aCt]++] = id
}
}
}
new iCheck
new iTimes = aNum[aCt] - aNum[aTerro]
if(iTimes > 0)
{
iCheck = aCt
}
else if(iTimes < 0)
{
iCheck = aTerro
}
else
{
return
}
iTimes = abs(iTimes)
// at this place iTimes is the players num difference between teams
if( iTimes < 2 || iTimes <= get_pcvar_num(mp_limitteams) )
{
return
}
iTimes /= 2
new bool:bTransfered[MAX_PLAYERS+1],
bool:bAdminsImmune = bool:get_pcvar_num(g_pcvarImmune)
new iLast, iCount
while( iTimes > 0 )
{
iLast = 0
for(new i=0; i <aNum[iCheck]; i++)
{
id = aTeams[iCheck][i]
if( g_bImmuned[id] && bAdminsImmune )
{
continue
}
if(bTransfered[id])
{
continue
}
if(g_fJoinedTeam[id] > g_fJoinedTeam[iLast])
{
iLast = id
}
}
if(!iLast)
{
return
}
if( iCheck )
{
cs_set_user_team(iLast, CS_TEAM_T)
}
else
{
cs_set_user_team(iLast, CS_TEAM_CT)
}
bTransfered[iLast] = true
iCount++
iTimes--
}
new szMessage[128]
get_pcvar_string(g_pCvarMessage, szMessage, charsmax(szMessage))
client_print(0, print_center, szMessage)
}