Commit 63a214df authored by Mikleo's avatar Mikleo
Browse files

Add cvars to auto-enable draft at certain playercount.

parent 146b02f5
ConVar dp_simplebalance,dp_simplebalance_mmrmargin,dp_simplebalance_nocomm,dp_simplebalance_playerratio;
int wasTeamBalanced [MAXPLAYERS+1] = {false,...};
void OnPluginStart_SimpleBalance()
{
dp_simplebalance = CreateConVar("dp_simplebalance", "1", "Teambalance with mmr");
dp_simplebalance_mmrmargin = CreateConVar("dp_simplebalance_mmrmargin", "40", "margin of mmr difference before players are autobalanced");
dp_simplebalance_nocomm = CreateConVar("dp_simplebalance_nocomm", "0", "teambalance on infantry maps");
dp_simplebalance_playerratio = CreateConVar("dp_simplebalance_playerratio", "0.4", "proportion of players in teams before teambalance enabled ");
dp_simplebalance.AddChangeHook(dp_simplebalance_changed);
}
public void dp_simplebalance_changed(ConVar convar, char[] oldValue, char[] newValue)
{
RefreshTeamBalanceCvars();
}
void OnClientPutInServer_SimpleBalance(int client)
{
wasTeamBalanced[client] = false;
}
// true is allow team
// false is block team
bool CheckTeamBalance(int client,int currentTeam,int clientTeam)
{
// dont teambalance if it is not enabled or we are in a draft.
if(dp_simplebalance.IntValue == 0 )
return true;
int numplayers[2];
numplayers[0] = GetTeamClientCount(2);
numplayers[1] = GetTeamClientCount(3);
if(numplayers[0] > numplayers[1])
{
if(clientTeam == 2)
{
ExecuteAutoBalance(client,3);
return false;
}
}
else if(numplayers[0] < numplayers[1] )
{
if(clientTeam == 3)
{
ExecuteAutoBalance(client,2);
return false;
}
}
// dont teambalance infantry maps unless specified.
if(!EU_IsClassicMap() && dp_simplebalance_nocomm.IntValue == 0)
return true;
float ratings[MAXPLAYERS+1];
// only use main ratings for matchmaking, not ranked
ES_GetStats("rating",ratings);
float totalmmr[3] = {0.0,0.0,0.0};
int mmrcount[3] = {0,0,0};
float averagemmr[3] = {0.0,0.0,0.0};
for (int i=1; i<=MaxClients; i++)
{
// ignore the client searching.
if(IsClientInGame(i) )
{
int team = GetClientTeam(i);
if(team == 2)
{
totalmmr[0] += ratings[i];
mmrcount[0] ++;
}
else if(team ==3)
{
totalmmr[1] += ratings[i];
mmrcount[1] ++;
}
totalmmr[2]++;
}
}
for(int j = 0;j<3;j++)
{
if(mmrcount[j] > 0)
{
averagemmr[j] = totalmmr[j]/mmrcount[j];
}
}
int totalPlayersInTeam = numplayers[0] + numplayers[1];
float playerRatio = float(totalPlayersInTeam) / float(GetClientCount(false));
// if teams are not even and we are not in a team
if(numplayers[0] != numplayers[1] || currentTeam == clientTeam)
return true;
// if there are less players than our playerratio in teams dont teambalance
if(playerRatio < dp_simplebalance_playerratio.FloatValue)
return true;
float clientmmr = ratings[client];
int margin = dp_simplebalance_mmrmargin.IntValue;
float avgmmr = averagemmr[2];
if(!wasTeamBalanced[client] && (clientTeam == 2 || clientTeam == 3))
{
int t = clientTeam -2;
float diff = averagemmr[t] - averagemmr [OppTeam(t)];
int team = t + 2;
// if you exactly have equal avgmmr, you can join both teams.
// autobalance to opposite team if outside of range
if((diff > margin && clientmmr > avgmmr) || (diff <-margin && clientmmr <avgmmr))
{
wasTeamBalanced[client] = true;
ExecuteAutoBalance(client,OppTeam(team));
return false;
}
}
// if autoassign assign a team based on mmr.
if(clientTeam == 4)
{
if((averagemmr[0] < averagemmr[1] && clientmmr > avgmmr) || (averagemmr[0] > averagemmr[1] && clientmmr <avgmmr))
{
ForceTeam(client,2);
return false;
}
else
{
ForceTeam(client,3);
return false;
}
}
return true;
}
void ExecuteAutoBalance(client,team)
{
ForceTeam(client,team);
PrintCenterText(client,"Auto-Balance");
SetHudTextParams(-1.0, 0.9, 5.0, 255, 255, 255, 255);
ShowHudText(client, 3, "Auto-Balance");
}
......@@ -8,7 +8,7 @@
#include <empstats>
#include <updater>
#define PluginVersion "0.83"
#define PluginVersion "0.84"
#define UPDATE_URL "https://sourcemod.docs.empiresmod.com/DraftPick/dist/updater.txt"
......@@ -37,6 +37,7 @@ public Plugin myinfo =
#define MODE_AUTOSQUADDRAFT 4
#define MODE_RANKEDMATCH 5
#define SPECMODE_FREELOOK 7
#define SPECMODE_NONE 0
......@@ -50,7 +51,10 @@ int stage = STAGE_DISABLED;
#define TEAM_BE 1;
ConVar cv_autobalance,cv_autoassign,cv_allowspectators,dp_draft,dp_captain_vote_time,dp_pick_wait_time,dp_pick_initial_multiplier,dp_time_increment,dp_in_draft,dp_maxpick,dp_squadlimitdefault,dp_autopick_randomizer,dp_forceauto_max_imbalance;
ConVar mp_autoteambalance,mp_teams_unbalance_limit,cv_autoassign,cv_allowspectators,dp_default_draftmode, dp_default_draftmode_min_players,dp_captain_vote_time,dp_pick_wait_time,dp_pick_initial_multiplier,dp_time_increment,dp_in_draft,dp_maxpick,dp_squadlimitdefault,dp_autopick_randomizer,dp_forceauto_max_imbalance;
int mp_autoteambalance_default = 1;
int mp_teams_unbalance_limit_default = 0;
//sound convars
ConVar dp_music,dp_music_volume,dp_pick_music,dp_pick_music_repeat,dp_pick_end_sound,dp_join_music,dp_your_turn_sound,dp_opp_turn_sound,dp_min_players;
......@@ -102,9 +106,6 @@ new Handle:TickHandle = INVALID_HANDLE;
int tickTime = 0;
int pauseHandle;
new Handle:AutobalanceHandle = INVALID_HANDLE;
new Handle:AutoAssignHandle = INVALID_HANDLE;
new Handle:SpectatorHandle = INVALID_HANDLE;
bool pluginEnded;
......@@ -184,8 +185,13 @@ bool waitMusicPlayed = false;
bool usedAutoAssign[MAXPLAYERS+1] = {false, ...};
#include "dp_simplebalance.sp"
public void OnPluginStart()
{
OnPluginStart_SimpleBalance();
pluginEnded = false;
......@@ -221,14 +227,18 @@ public void OnPluginStart()
RegConsoleCmd("sm_dmusic", Command_DraftMusic);
cv_autobalance = FindConVar("mp_autoteambalance");
mp_autoteambalance = FindConVar("mp_autoteambalance");
mp_teams_unbalance_limit = FindConVar("mp_teams_unbalance_limit");
cv_autoassign = FindConVar("emp_sv_forceautoassign");
cv_allowspectators = FindConVar("emp_allowspectators");
AddCommandListener(Command_Plugin_Version, "dp_version");
AddCommandListener(Command_Join_Team, "jointeam");
dp_draft = CreateConVar("dp_draft", "0", "The draft mode");
dp_default_draftmode = CreateConVar("dp_default_draftmode", "None", "The default draft mode");
dp_default_draftmode_min_players = CreateConVar("dp_default_draftmode_min_players", "25", "The minimum players required to intiate the default draft mode");
dp_min_players = CreateConVar("dp_minplayers", "10", "The minimum number of players after which draft mode is disabled.");
dp_captain_vote_time = CreateConVar("dp_captain_vote_time", "100", "The time set in the captain vote stage");
dp_pick_wait_time = CreateConVar("dp_pick_wait_time", "60", "The time set in the pick wait stage");
......@@ -310,6 +320,8 @@ public void OnClientPutInServer(int client)
CreateTimer(2.0,NotifyDraftOn,client);
}
OnClientPutInServer_SimpleBalance(client);
dmusic[client] = 1;
}
......@@ -613,7 +625,12 @@ public Action Command_Join_Team(int client, const String:command[], args)
if(!draftEnabled)
{
if(!CheckTeamBalance(client,oldTeam,team))
return Plugin_Handled;
return Plugin_Continue;
}
......@@ -637,7 +654,7 @@ public Action Command_Join_Team(int client, const String:command[], args)
{
if(stage != STAGE_GAME)
{
PrintToChat(client,"\x04[DP] \x01You missed the start of the drafting proccess. You must wait until \x073399ff1\x01 minute after the draft ends to join a team");
PrintToChat(client,"\x04[DP] \x01You missed the start of the drafting proccess. You must wait until \x073399ff30\x01 seconds after the draft ends to join a team");
return Plugin_Handled;
}
else
......@@ -682,7 +699,7 @@ public Action Command_Join_Team(int client, const String:command[], args)
numPlayers[1] = GetTeamClientCount(3);
int t = team - 2;
if(team != 4 && numPlayers[t] > numPlayers[OppTeam(t)] && cv_autobalance.IntValue == 1 && cv_autoassign.IntValue == 0)
if(team != 4 && numPlayers[t] > numPlayers[OppTeam(t)] && cv_autoassign.IntValue == 0)
{
PrintToChat(client,"\x04[DP] \x01Can't join team,numbers not balanced. ",teamcolors[gameTeam],teamnames[gameTeam]);
return Plugin_Handled;
......@@ -965,6 +982,7 @@ void ExecuteBatchJoin()
}
}
int OppBinaryTeam(int team)
{
if(team == 0)
......@@ -1571,7 +1589,7 @@ void Pick(int client,int target)
{
cName = "AutoPick";
}
SetHudTextParams(-1.0, 0.75, 5.0, 255, 255, 255, 255);
char targetName[64];
......@@ -1628,6 +1646,7 @@ void Pick(int client,int target)
}
void OnClientDrafted(int target)
{
SetHudTextParams(-1.0, 0.75, 5.0, 255, 255, 255, 255);
ShowHudText(target, 3, "You were drafted into %s",teamnames[teamToPick]);
AddToTeam(target,teamToPick);
picksLeft --;
......@@ -1880,7 +1899,6 @@ void SetUpDraft(mode)
draftBegun = false;
draftPaused = false;
ClearDraft();
cv_autobalance.IntValue = 0;
pickStartTime = 0;
unlockTime = 0;
ChangeStage(startStage);
......@@ -1916,17 +1934,12 @@ DraftEnded()
{
// make sure this expensive hook is unhooked
SDKUnhook(resourceEntity, SDKHook_ThinkPost, Hook_OnThinkPost);
AutobalanceHandle = CreateTimer(0.1, correctAutobalance);
RemoveCommandListener(Command_Join_Squad_Draft, "emp_squad_join");
RemoveCommandListener(Command_Invite_Draft, "emp_squad_invite");
RemoveCommandListener(Command_Menu_Draft, "emp_menu_command");
}
else
{
// just correct autobalance now
cv_autobalance.IntValue = 1;
}
dp_in_draft.IntValue = 0;
......@@ -1961,36 +1974,9 @@ DraftEnded()
Call_Finish();
}
stock TempDisableAutobalance()
{
if(cv_autobalance.IntValue == 1)
{
AutobalanceHandle = CreateTimer(0.1, correctAutobalance);
cv_autobalance.IntValue = 0;
}
}
public Action correctAutobalance(Handle timer)
{
if(timer == AutobalanceHandle && (stage == STAGE_GAME || stage == STAGE_DISABLED))
cv_autobalance.IntValue = 1;
}
stock TempDisableForceAutoAssign()
{
if(cv_autoassign.IntValue == 1)
{
AutoAssignHandle = CreateTimer(0.1, correctAutoassign);
cv_autoassign.IntValue = 0;
}
}
public Action correctAutoassign(Handle timer)
{
if(stage == STAGE_GAME && timer == AutoAssignHandle)
{
cv_autoassign.IntValue = 1;
}
}
TempAllowSpec()
{
if(cv_allowspectators.IntValue == 0)
......@@ -2011,7 +1997,7 @@ public Action correctSpec(Handle timer)
}
// we don't know if comm map initially
// we don't know if it is comm map initially
public Action StartDraft(Handle timer)
{
// continuous modes only work on comm maps at the moment.
......@@ -2021,16 +2007,28 @@ public Action StartDraft(Handle timer)
{
SetUpDraft(continuousMode);
}
else if(dp_draft.IntValue > 0)
else if(GetClientCount(false) >= dp_default_draftmode_min_players.IntValue)
{
SetUpDraft(dp_draft.IntValue);
char modeString[32];
dp_default_draftmode.GetString(modeString,sizeof(modeString));
int mode = GetDraftModeIndex(modeString);
if(mode > 0)
{
SetUpDraft(mode);
}
}
}
}
public OnConfigsExecuted()
{
CreateTimer(1.0, StartDraft);
mp_autoteambalance_default = mp_autoteambalance.IntValue;
mp_teams_unbalance_limit_default = mp_teams_unbalance_limit.IntValue;
RefreshTeamBalanceCvars();
CreateTimer(2.0, StartDraft);
dp_opp_turn_sound.GetString(sound_opp_turn,128);
dp_your_turn_sound.GetString(sound_your_turn,128);
dp_pick_end_sound.GetString(sound_pick_end,128);
......@@ -2099,6 +2097,7 @@ public OnMapStart()
{
AutoExecConfig(true, "draftpick");
overlayEnt = 0;
draftBegun = false;
......@@ -2525,9 +2524,19 @@ int OppTeam(int team)
{
if(team == 1)
return 0;
else return 1;
else if(team == 0)
return 1;
else if(team == 2)
return 3;
else if(team == 3)
return 2;
return 0;
}
// should remove any incorrect prefixs and add the correct one.
AdjustPrefix(int client)
{
......@@ -3001,12 +3010,12 @@ Stage_Disabled_Start(int prevStage)
{
SetRanked(false);
draftMode = MODE_NONE;
dp_draft.IntValue = 0;
ClearDraft();
draftEnabled = false;
RefreshTeamBalanceCvars();
UnhookEvent("player_changename", Event_NameChange,EventHookMode_Pre);
RemoveCommandListener(Command_Opt_Out, "emp_commander_vote_drop_out");
RemoveCommandListener(Command_Opt_In, "emp_commander_vote_add_in");
......@@ -3028,6 +3037,7 @@ Stage_Disabled_Start(int prevStage)
}
Stage_Disabled_End()
{
......@@ -3043,6 +3053,8 @@ Stage_Disabled_End()
Call_StartForward(g_DraftStart);
Call_Finish();
RefreshTeamBalanceCvars();
PrintToChatAll("\x04[DP] \x01Draft pick enabled");
}
ChangeStage(int stg)
......@@ -4549,7 +4561,49 @@ CheckForceAuto()
}
}
int GetDraftModeIndex(char[] mode)
{
if(StrEqual(mode,"autodraft",false))
{
return MODE_AUTODRAFT;
}
else if(StrEqual(mode,"autosquaddraft",false))
{
return MODE_AUTOSQUADDRAFT;
}
else if(StrEqual(mode,"draft",false))
{
return MODE_DRAFT;
}
else if(StrEqual(mode,"squaddraft",false))
{
return MODE_SQUADDRAFT;
}
else if(StrEqual(mode,"rankedmatch",false))
{
return MODE_RANKEDMATCH;
}
return 0;
}
stock abs(x)
{
return x>0 ? x : -x;
}
\ No newline at end of file
}
RefreshTeamBalanceCvars()
{
if(draftEnabled || dp_simplebalance.IntValue > 0)
{
mp_autoteambalance.IntValue = 0;
mp_teams_unbalance_limit.IntValue = 0;
}
else
{
mp_autoteambalance.IntValue = mp_autoteambalance_default;
mp_teams_unbalance_limit.IntValue = mp_teams_unbalance_limit_default;
}
}
// This file was auto-generated by SourceMod (v1.8.0.6014)
// ConVars for plugin "draftpick.smx"
// -
// Default: "15.0"
dp_autopick_randomizer "15.0"
// The time set in the captain vote stage
// -
// Default: "100"
dp_captain_vote_time "100"
// The draft mode
// -
// Default: "0"
dp_draft "0"
// Notification of drafting. ignore
// -
// Default: "0"
dp_in_draft "0"
// -
// Default: ""
dp_join_music ""
// maximum number of picks before autopick
// -
// Default: "40"
dp_maxpick "40"
// If music is enabled
// -
// Default: "1"
dp_music "1"
// -
// Default: "draftpick/opponent_turn.wav"
dp_opp_turn_sound "draftpick/opponent_turn.wav"
// -
// Default: "draftpick/draft_complete.mp3"
dp_pick_end_sound "draftpick/draft_complete.mp3"
// Amount of initial time given to each captain per player
// -
// Default: "2"
dp_pick_initial_multiplier "2"
// -
// Default: "draftpick/draft_pick2.mp3"
dp_pick_music "draftpick/draft_pick2.mp3"
// -
// Default: "102"
dp_pick_music_repeat "102"
// -
// Default: "draftpick/draft_wait2.mp3"
dp_wait_music "draftpick/draft_wait2.mp3"
// -
// Default: "114"
dp_wait_music_repeat "114"
// The time set in the pick wait stage
// -
// Default: "60"
dp_pick_wait_time "60"
// -
// Default: "4"
dp_squadlimitdefault "4"
// The time increment given to each captain per player in the pick phase
// -
// Default: "3"
dp_time_increment "3"
// -
// Default: "draftpick/your_turn.wav"
dp_your_turn_sound "draftpick/your_turn.wav"
......@@ -4,7 +4,7 @@
{
"Version"
{
"Latest" "0.83"
"Latest" "0.84"
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment