Skip to content

Commit

Permalink
Updated to version 0.4.0
Browse files Browse the repository at this point in the history
- Added two new cvars. "dmtb_skip_suicide" and "dmtb_skip_disconnect"
- Added API. See "dmtb_api.inc"
- Some fixes
  • Loading branch information
Nord1cWarr1or committed Jan 13, 2020
1 parent 7f0bee9 commit 79ed84d
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 40 deletions.
4 changes: 4 additions & 0 deletions data/lang/dm_autobalance.txt
Expand Up @@ -6,6 +6,8 @@ DMTB_CVAR_ADMIN_FLAG = A flag that defines the admin
DMTB_CVAR_BOTS = Do you have to consider the bots when balancing?
DMTB_CVAR_ADMIN_MODE = Admins balancing mode.^n0 - switch like normal players.^n1 - do not switch team.^n2 - distribute between teams
DMTB_CVAR_MAX_DIFF_ADMINS = Maximum allowable difference between the number of admins in the commands
DMTB_CVAR_SKIP_SUICIDE = Do I skip player recalculation in case of player suicide?
DMTB_CVAR_SKIP_DISCONNECT = Do I skip player recalculation in case of player disconnect?
DMTB_DHUD_WILL_BALANCED = You will be switched to opposite team via %.0f sec.
DMTB_DHUD_BALANCED_CT = You've been switched to team ct
DMTB_DHUD_BALANCED_TE = You've been switched to team terrorists
Expand All @@ -20,6 +22,8 @@ DMTB_CVAR_ADMIN_FLAG = Флаг, определяющий админа
DMTB_CVAR_BOTS = Учитывать ли ботов при балансе?
DMTB_CVAR_ADMIN_MODE = Режим балансировки админов.^n0 - переводить вместе со всеми^n1 - иммунитет от баланса^n2 - распределять между командами
DMTB_CVAR_MAX_DIFF_ADMINS = Максимально допустимая разница между количеством админов в командах
DMTB_CVAR_SKIP_SUICIDE = Пропускать ли пересчёт игроков при суициде игрока?
DMTB_CVAR_SKIP_DISCONNECT = Пропускать ли пересчёт игроков при выходе игрока из игры?
DMTB_DHUD_WILL_BALANCED = Вы будете перемещены за другую команду через %.0f сек.
DMTB_DHUD_BALANCED_CT = Вы были перемещены за контр-террористов
DMTB_DHUD_BALANCED_TE = Вы были перемещены за террористов
Expand Down
112 changes: 72 additions & 40 deletions scripting/AutoBalance.sma
@@ -1,26 +1,28 @@
/* * * * * * * * * * * * * * * * * * * * * * * *
* *
* Plugin: Automatic team balance for DM servers *
* *
* Official plugin support: https://dev-cs.ru/threads/8029/ *
* Contacts of the author: Telegram: @NordicWarrior *
* *
* * * * * * * * * * * * * * * * * * * * * * * *
* *
* Плагин: Автоматический баланс команд для DM сереров *
* *
* Официальная поддержка плагина: https://dev-cs.ru/threads/8029/ *
* Связь с автором: Telegram: @NordicWarrior *
* *
* * * * * * * * * * * * * * * * * * * * * * * */
/* * * * * * * * * * * * * * * * * * * *
* *
* Plugin: Automatic team balance for DM servers *
* *
* Official plugin support: https://dev-cs.ru/threads/8029/ *
* Official repository: https://github.com/Nord1cWarr1or/AutoBalance *
* Contacts of the author: Telegram: @NordicWarrior *
* *
* * * * * * * * * * * * * * * * * * * *
* *
* Плагин: Автоматический баланс команд для DM сереров *
* *
* Официальная поддержка плагина: https://dev-cs.ru/threads/8029/ *
* Официальный репозиторий: https://github.com/Nord1cWarr1or/AutoBalance *
* Связь с автором: Telegram: @NordicWarrior *
* *
* * * * * * * * * * * * * * * * * * * */

#include <amxmodx>
#include <amxmisc>
#include <reapi>
#include <xs>
#include <screenfade_util>

new const PLUGIN_VERSION[] = "0.3.12";
new const PLUGIN_VERSION[] = "0.4.0";

#if !defined MAX_MAPNAME_LENGTH
#define MAX_MAPNAME_LENGTH 64
Expand All @@ -44,10 +46,12 @@ enum _:Cvars
ADMIN_FLAG[2],
BOTS,
ADMIN_MODE,
MAX_DIFF_ADMINS
MAX_DIFF_ADMINS,
SKIP_SUICIDE,
SKIP_DISCONNECT
};

new g_Cvar[Cvars];
new g_pCvar[Cvars];

new TeamName:g_iNewPlayerTeam[MAX_PLAYERS + 1];

Expand All @@ -72,6 +76,8 @@ new g_pCvarMode;
new g_iPlayersInTeam[TeamName][MAX_PLAYERS], g_iCountPlayersInTeam[TeamName];
new g_iAdminsInTeam[TeamName][MAX_PLAYERS], g_iCountAdminsInTeam[TeamName];

new g_iFwdBalancePlayerPre, g_iFwdBalancePlayerPost;

public plugin_init()
{
register_plugin("DM AutoBalance", PLUGIN_VERSION, "Nordic Warrior");
Expand All @@ -82,6 +88,9 @@ public plugin_init()

CreateCvars();

g_iFwdBalancePlayerPre = CreateMultiForward("OnBalancePlayerPre", ET_STOP, FP_CELL);
g_iFwdBalancePlayerPost = CreateMultiForward("OnBalancePlayerPost", ET_IGNORE, FP_CELL);

#if defined AUTO_CFG
AutoExecConfig(true, "AutoBalance");
#endif
Expand Down Expand Up @@ -143,14 +152,17 @@ public client_remove(id)
{
if(GetBit(g_bitIsUserConnected, id))
{
CheckTeams();
if(!g_pCvar[SKIP_DISCONNECT])
{
CheckTeams();
}
ClrBit(g_bitIsUserConnected, id);
}
}

public OnPlayerKilledPost(iVictim, iKiller)
{
if(!GetBit(g_bitIsUserConnected, iKiller) || iKiller == iVictim)
if(!GetBit(g_bitIsUserConnected, iKiller) || (g_pCvar[SKIP_SUICIDE] && iKiller == iVictim))
return;

#if defined DEBUG
Expand All @@ -169,7 +181,7 @@ public CheckTeams()

new iPlayers[MAX_PLAYERS], iPlayersNum;

get_players_ex(iPlayers, iPlayersNum, g_Cvar[BOTS] ? GetPlayers_ExcludeHLTV : (GetPlayers_ExcludeBots | GetPlayers_ExcludeHLTV));
get_players_ex(iPlayers, iPlayersNum, g_pCvar[BOTS] ? GetPlayers_ExcludeHLTV : (GetPlayers_ExcludeBots | GetPlayers_ExcludeHLTV));

new TeamName:iTeam;
new iPlayer;
Expand All @@ -182,7 +194,7 @@ public CheckTeams()

g_iPlayersInTeam[iTeam][g_iCountPlayersInTeam[iTeam]++] = iPlayer;

if(has_flag(iPlayer, g_Cvar[ADMIN_FLAG]) && g_Cvar[ADMIN_MODE] == 2)
if(has_flag(iPlayer, g_pCvar[ADMIN_FLAG]) && g_pCvar[ADMIN_MODE] == 2)
{
g_iAdminsInTeam[iTeam][g_iCountAdminsInTeam[iTeam]++] = iPlayer;
}
Expand All @@ -191,17 +203,17 @@ public CheckTeams()
#if defined DEBUG
log_amx("TE = %i, CT = %i", g_iCountPlayersInTeam[TEAM_TERRORIST], g_iCountPlayersInTeam[TEAM_CT]);

if(g_Cvar[ADMIN_MODE] == 2)
if(g_pCvar[ADMIN_MODE] == 2)
{
log_amx("ADM TE = %i, ADM CT = %i", g_iCountAdminsInTeam[TEAM_TERRORIST], g_iCountAdminsInTeam[TEAM_CT]);
}
#endif

if(xs_abs(g_iCountPlayersInTeam[TEAM_TERRORIST] - g_iCountPlayersInTeam[TEAM_CT]) > g_Cvar[MAX_DIFF])
if(xs_abs(g_iCountPlayersInTeam[TEAM_TERRORIST] - g_iCountPlayersInTeam[TEAM_CT]) > g_pCvar[MAX_DIFF])
{
new iTeamPlayersForBalance = xs_sign(g_iCountPlayersInTeam[TEAM_TERRORIST] - g_iCountPlayersInTeam[TEAM_CT]);

if(g_Cvar[ADMIN_MODE] == 2 && xs_abs(g_iCountAdminsInTeam[TEAM_TERRORIST] - g_iCountAdminsInTeam[TEAM_CT]) > g_Cvar[MAX_DIFF_ADMINS])
if(g_pCvar[ADMIN_MODE] == 2 && xs_abs(g_iCountAdminsInTeam[TEAM_TERRORIST] - g_iCountAdminsInTeam[TEAM_CT]) > g_pCvar[MAX_DIFF_ADMINS])
{
new iTeamAdminsForBalance = xs_sign(g_iCountAdminsInTeam[TEAM_TERRORIST] - g_iCountAdminsInTeam[TEAM_CT]);

Expand All @@ -210,7 +222,7 @@ public CheckTeams()
}
GetPlayerForBalance(iTeamPlayersForBalance);
}
else if(g_Cvar[ADMIN_MODE] == 2 && xs_abs(g_iCountAdminsInTeam[TEAM_TERRORIST] - g_iCountAdminsInTeam[TEAM_CT]) > g_Cvar[MAX_DIFF_ADMINS])
else if(g_pCvar[ADMIN_MODE] == 2 && xs_abs(g_iCountAdminsInTeam[TEAM_TERRORIST] - g_iCountAdminsInTeam[TEAM_CT]) > g_pCvar[MAX_DIFF_ADMINS])
{
new iTeamAdminsForBalance = xs_sign(g_iCountAdminsInTeam[TEAM_TERRORIST] - g_iCountAdminsInTeam[TEAM_CT]);

Expand Down Expand Up @@ -252,7 +264,7 @@ GetPlayerForBalance(const iTeamToBalance, bool:bAdmins = false)
log_amx("Balanced player: <%n>, ID: %i", iRandomPlayer, iRandomPlayer);
#endif

if(!bAdmins && has_flag(iRandomPlayer, g_Cvar[ADMIN_FLAG]) && g_Cvar[ADMIN_MODE] != 0)
if(!bAdmins && has_flag(iRandomPlayer, g_pCvar[ADMIN_FLAG]) && g_pCvar[ADMIN_MODE] != 0)
{
#if defined DEBUG
log_amx("Player <%n> has immunity", iRandomPlayer);
Expand All @@ -270,17 +282,17 @@ NotifyAndBalancePlayer(const id)
{
new iData[1]; iData[0] = id;

set_task(g_Cvar[TIME_TO_PREPARE], "BalancePlayer", TASKID__BALANCE_PLAYER, iData, sizeof iData);
set_task(g_pCvar[TIME_TO_PREPARE], "BalancePlayer", TASKID__BALANCE_PLAYER, iData, sizeof iData);

set_dhudmessage(255, 255, 255, -1.0, 0.42, 0, 0.0, 3.0, 0.1, 0.1);
show_dhudmessage(id, "%l", "DMTB_DHUD_WILL_BALANCED", g_Cvar[TIME_TO_PREPARE]);
show_dhudmessage(id, "%l", "DMTB_DHUD_WILL_BALANCED", g_pCvar[TIME_TO_PREPARE]);
}

public BalancePlayer(iData[])
{
new id = iData[0];

if(!is_user_connected(id))
if(!GetBit(g_bitIsUserConnected, id))
return PLUGIN_HANDLED;

new TeamName:iTeam = get_member(id, m_iTeam);
Expand All @@ -291,12 +303,18 @@ public BalancePlayer(iData[])
return PLUGIN_HANDLED;
}

new iReturn;
ExecuteForward(g_iFwdBalancePlayerPre, iReturn, id);

if(iReturn == PLUGIN_HANDLED)
return PLUGIN_HANDLED;

if(user_has_weapon(id, CSW_C4))
rg_drop_items_by_slot(id, C4_SLOT);

rg_switch_team(id);

switch(g_Cvar[MODE])
switch(g_pCvar[MODE])
{
case 0:
{
Expand All @@ -318,6 +336,8 @@ public BalancePlayer(iData[])

set_task(0.1, "ShowHud", TASKID__SHOW_HUD + id);

ExecuteForward(g_iFwdBalancePlayerPost, iReturn, id);

RequestFrame("CheckTeams");
return PLUGIN_CONTINUE;
}
Expand All @@ -336,7 +356,7 @@ ArraysZeroing()
{
arrayset(g_iPlayersInTeam[any:0][0], 0, sizeof g_iPlayersInTeam * sizeof g_iPlayersInTeam[]);
arrayset(g_iCountPlayersInTeam[any:0], 0, sizeof g_iCountPlayersInTeam);
arrayset(g_iAdminsInTeam[any:0][0], 0, sizeof g_iAdminsInTeam * sizeof g_iPlayersInTeam[]);
arrayset(g_iAdminsInTeam[any:0][0], 0, sizeof g_iAdminsInTeam * sizeof g_iAdminsInTeam[]);
arrayset(g_iCountAdminsInTeam[any:0], 0, sizeof g_iCountAdminsInTeam);
}

Expand All @@ -345,45 +365,56 @@ public CreateCvars()
bind_pcvar_num(create_cvar("dmtb_max_diff", "1",
.description = GetCvarDesc("DMTB_CVAR_MAX_DIFF"),
.has_min = true, .min_val = 1.0),
g_Cvar[MAX_DIFF]);
g_pCvar[MAX_DIFF]);

bind_pcvar_num(g_pCvarMode = create_cvar("dmtb_mode", "1",
.description = GetCvarDesc("DMTB_CVAR_MODE"),
.has_min = true, .min_val = 0.0,
.has_max = true, .max_val = 2.0),
g_Cvar[MODE]);
g_pCvar[MODE]);

bind_pcvar_float(create_cvar("dmtb_time", "3.0",
.description = GetCvarDesc("DMTB_CVAR_TIME"),
.has_min = true, .min_val = 1.0),
g_Cvar[TIME_TO_PREPARE]);
g_pCvar[TIME_TO_PREPARE]);

bind_pcvar_string(create_cvar("dmtb_admin_flag", "a",
.description = GetCvarDesc("DMTB_CVAR_ADMIN_FLAG")),
g_Cvar[ADMIN_FLAG], charsmax(g_Cvar[ADMIN_FLAG]));
g_pCvar[ADMIN_FLAG], charsmax(g_pCvar[ADMIN_FLAG]));

bind_pcvar_num(create_cvar("dmtb_bots", "0",
.description = GetCvarDesc("DMTB_CVAR_BOTS"),
.has_min = true, .min_val = 0.0,
.has_max = true, .max_val = 1.0),
g_Cvar[BOTS]);
g_pCvar[BOTS]);

bind_pcvar_num(create_cvar("dmtb_admin_mode", "1",
.description = GetCvarDesc("DMTB_CVAR_ADMIN_MODE"),
.has_min = true, .min_val = 0.0,
.has_max = true, .max_val = 2.0),
g_Cvar[ADMIN_MODE]);
g_pCvar[ADMIN_MODE]);

bind_pcvar_num(create_cvar("dmtb_max_diff_admins", "1",
.description = GetCvarDesc("DMTB_CVAR_MAX_DIFF_ADMINS"),
.has_min = true, .min_val = 1.0),
g_Cvar[MAX_DIFF_ADMINS]);
g_pCvar[MAX_DIFF_ADMINS]);

bind_pcvar_num(create_cvar("dmtb_skip_suicide", "1",
.description = GetCvarDesc("DMTB_CVAR_SKIP_SUICIDE"),
.has_min = true, .min_val = 0.0,
.has_max = true, .max_val = 1.0),
g_pCvar[SKIP_SUICIDE]);

bind_pcvar_num(create_cvar("dmtb_skip_disconnect", "0",
.description = GetCvarDesc("DMTB_CVAR_SKIP_DISCONNECT"),
.has_min = true, .min_val = 0.0,
.has_max = true, .max_val = 1.0),
g_pCvar[SKIP_DISCONNECT]);
}

stock ClientPrintToAllExcludeOne(const iExcludePlayer, const iSender, const szMessage[], any:...)
{
new szText[192];
vformat(szText, charsmax(szText), szMessage, 4);

new iPlayers[MAX_PLAYERS], iNumPlayers;
get_players(iPlayers, iNumPlayers, "ch");
Expand All @@ -395,6 +426,7 @@ stock ClientPrintToAllExcludeOne(const iExcludePlayer, const iSender, const szMe
if(iPlayer != iExcludePlayer)
{
SetGlobalTransTarget(iPlayer);
vformat(szText, charsmax(szText), szMessage, 4);
client_print_color(iPlayer, iSender, szText);
}
}
Expand Down
22 changes: 22 additions & 0 deletions scripting/include/dmtb_api.inc
@@ -0,0 +1,22 @@
#if defined _dmtb_api_included
#endinput
#endif
#define _dmtb_api_included
/*
* Called before a player will be balanced
*
* @param id Index of a player
*
* @noreturn
*/
forward OnBalancePlayerPre(id);
/*
* Called after a player is balanced
*
* @param id Index of a player
*
* @noreturn
*/
forward OnBalancePlayerPost(id);

0 comments on commit 79ed84d

Please sign in to comment.