From d8158b7e76abcb02599069780cd548291e509074 Mon Sep 17 00:00:00 2001 From: ChocoTaco Date: Sun, 3 Feb 2019 18:00:24 -0500 Subject: [PATCH] Added Autobalance --- Classic/scripts/autoexec/Autobalance.cs | 79 +++++++++++++++++++ Classic/scripts/autoexec/TeamBalanceNotify.cs | 19 +++-- 2 files changed, 88 insertions(+), 10 deletions(-) create mode 100644 Classic/scripts/autoexec/Autobalance.cs diff --git a/Classic/scripts/autoexec/Autobalance.cs b/Classic/scripts/autoexec/Autobalance.cs new file mode 100644 index 0000000..368328c --- /dev/null +++ b/Classic/scripts/autoexec/Autobalance.cs @@ -0,0 +1,79 @@ +//Fire Autobalance +function Autobalance( %game, %client, %respawn ) +{ + if( $CurrentMissionType !$= "LakRabbit" && $Host::EnableTeamBalanceNotify && $StatsMsgPlayed $= 1 && !$Host::TournamentMode ) + { + //Generate random to get random client + %team1random = getRandom(1,$PlayerCount[1]); + %team2random = getRandom(1,$PlayerCount[2]); + + %AutobalanceCount[0] = 0; + %AutobalanceCount[1] = 0; + %AutobalanceCount[2] = 0; + + for(%i = 0; %i < ClientGroup.getCount(); %i++) + { + %client = ClientGroup.getObject(%i); + + //For autobalance + //Pick a random client on a team + if( %client.team == 1 && %team1random == %AutobalanceCount[1] ) + %team1canidate = %client; + if( %client.team == 2 && %team2random == %AutobalanceCount[2] ) + %team2canidate = %client; + + //if(!%client.isAIControlled()) + %AutobalanceCount[%client.team]++; + + //Safetynet + if( %team1canidate $= "" ) + %team1canidate = %client; + if( %team2canidate $= "" ) + %team2canidate = %client; + } + + if( $Team1Difference == 1 || $Team2Difference == 1 || $PlayerCount[1] == $PlayerCount[2] ) + { + $StatsMsgPlayed = 0; + return; + } + //Safetynet + else if( team1canidate $= "" || team2canidate $= "" ) + { + schedule(2500, 0, "Autobalance", %game, %client, %respawn); + return; + } + //Team 1 + else if( $Team1Difference >= 2 ) + { + %client = %team1canidate; + %team = %team1canidate.team; + %otherTeam = ( %team == 1 ) ? 2 : 1; + + Game.clientChangeTeam( %client, %otherTeam, 0 ); + messageAll('MsgTeamBalanceNotify', '~wfx/powered/vehicle_screen_on.wav'); + + //Trigger GetCounts + ResetClientChangedTeams(); + //Reset Stats. + $StatsMsgPlayed = 0; + return; + } + //Team 2 + else if( $Team2Difference >= 2 ) + { + %client = %team2canidate; + %team = %team2canidate.team; + %otherTeam = ( %team == 1 ) ? 2 : 1; + + Game.clientChangeTeam( %client, %otherTeam, 0 ); + messageAll('MsgTeamBalanceNotify', '~wfx/powered/vehicle_screen_on.wav'); + + //Trigger GetCounts + ResetClientChangedTeams(); + //Reset Stats. + $StatsMsgPlayed = 0; + return; + } + } +} \ No newline at end of file diff --git a/Classic/scripts/autoexec/TeamBalanceNotify.cs b/Classic/scripts/autoexec/TeamBalanceNotify.cs index cb787de..4dd1f05 100644 --- a/Classic/scripts/autoexec/TeamBalanceNotify.cs +++ b/Classic/scripts/autoexec/TeamBalanceNotify.cs @@ -3,9 +3,9 @@ // //Give the client a notification on the current state of balancing. //This function is in GetTeamCounts.cs -function TeamBalanceNotify( %game ) +function TeamBalanceNotify( %game, %client, %respawn ) { - if( $CurrentMissionType !$= "LakRabbit" && $TotalTeamPlayerCount !$= 0 && $Host::EnableTeamBalanceNotify ) + if( $CurrentMissionType !$= "LakRabbit" && $TotalTeamPlayerCount !$= 0 && $Host::EnableTeamBalanceNotify && !$Host::TournamentMode ) { //echo ("%Team1Difference " @ %Team1Difference); //echo ("%Team2Difference " @ %Team2Difference); @@ -19,11 +19,10 @@ function TeamBalanceNotify( %game ) { if( $StatsMsgPlayed !$= 1) { - messageAll('MsgTeamBalanceNotify', '\c1Teams are unbalanced: \c0%1 vs %2 with %3 observers.', $PlayerCount[1], $PlayerCount[2], $PlayerCount[0] ); //Run once. $StatsMsgPlayed = 1; //Start Sound Schedule for 60 secs - schedule(60000, 0, "StatsUnbalanceSound", %game); + schedule(15000, 0, "StatsUnbalanceSound", %game, %client, %respawn ); } } } @@ -47,11 +46,11 @@ function ResetTeamBalanceNotifyGameOver() $StatsMsgPlayed = -1; } -//Called every 30 seconds -//2 or more difference -function StatsUnbalanceSound( %game ) +//Check to see if teams are still unbalanced +//Fire AutoBalance in 30 sec +function StatsUnbalanceSound( %game, %client, %respawn ) { - if( $CurrentMissionType !$= "LakRabbit" && $Host::EnableTeamBalanceNotify && $StatsMsgPlayed $= 1 ) + if( $CurrentMissionType !$= "LakRabbit" && $Host::EnableTeamBalanceNotify && $StatsMsgPlayed $= 1 && !$Host::TournamentMode ) { if( $Team1Difference == 1 || $Team2Difference == 1 || $PlayerCount[1] == $PlayerCount[2] ) { @@ -60,8 +59,8 @@ function StatsUnbalanceSound( %game ) } else if( $Team1Difference >= 2 || $Team2Difference >= 2 ) { - messageAll('MsgTeamBalanceNotify', '\c1Teams are unbalanced: \c0%1 vs %2 with %3 observers.~wgui/vote_nopass.wav', $PlayerCount[1], $PlayerCount[2], $PlayerCount[0] ); - schedule(30000, 0, "StatsUnbalanceSound", %game); + messageAll('MsgTeamBalanceNotify', '\c1Teams are unbalanced: \c0Autobalance Initializing.~wgui/vote_nopass.wav'); + schedule(30000, 0, "Autobalance", %game, %client, %respawn); } } } \ No newline at end of file