2019-09-13 19:29:42 +00:00
// Team Autobalance Script
//
2020-08-29 18:39:32 +00:00
// Determines which team needs players and proceeds to switch them
// Goon style: At respawn
2019-09-13 19:29:42 +00:00
//
2019-02-06 18:39:36 +00:00
// Enable or Disable Autobalance
// $Host::EnableAutobalance = 1;
2020-08-29 18:39:32 +00:00
//
// exec("scripts/autoexec/Autobalance.cs");
2020-09-28 18:54:01 +00:00
//
// If it takes too long for specific canidates to die. After a time choose anyone.
2020-10-12 06:20:38 +00:00
$ Autobalance : : Fallback = 120000 ; //60000 is 1 minute
2019-09-13 19:29:42 +00:00
2020-08-29 18:39:32 +00:00
// Run from TeamBalanceNotify.cs via NotifyUnbalanced
function Autobalance ( % game )
2020-10-01 16:10:39 +00:00
{
if ( isEventPending ( $ AutoBalanceSchedule ) )
2019-12-11 21:30:37 +00:00
cancel ( $ AutoBalanceSchedule ) ;
2020-10-01 16:10:39 +00:00
2020-09-28 18:54:01 +00:00
if ( $ TBNStatus ! $ = "NOTIFY" ) //If Status has changed to EVEN or anything else (GameOver reset).
2019-12-11 21:30:37 +00:00
return ;
2020-10-01 16:10:39 +00:00
2019-09-13 19:29:42 +00:00
//Difference Variables
2020-03-26 03:26:45 +00:00
% team1difference = $ TeamRank [ 1 , count ] - $ TeamRank [ 2 , count ] ;
% team2difference = $ TeamRank [ 2 , count ] - $ TeamRank [ 1 , count ] ;
2020-10-01 16:10:39 +00:00
2020-08-29 18:39:32 +00:00
//Determine BigTeam
2020-08-18 17:58:49 +00:00
if ( % team1difference > = 2 )
2020-08-29 18:39:32 +00:00
$ BigTeam = 1 ;
2019-09-13 19:29:42 +00:00
else if ( % team2difference > = 2 )
2020-08-29 18:39:32 +00:00
$ BigTeam = 2 ;
2020-08-18 17:58:49 +00:00
else
return ;
2020-09-28 18:54:01 +00:00
2020-10-02 23:52:42 +00:00
$ Autobalance : : UseAllMode = 0 ;
2020-10-22 19:32:41 +00:00
$ Autobalance : : FallbackTime = "" ;
2020-09-28 18:54:01 +00:00
% otherTeam = $ BigTeam = = 1 ? 2 : 1 ;
2020-09-30 17:36:22 +00:00
$ Autobalance : : AMThreshold = mCeil ( MissionGroup . CTF_scoreLimit / 3 ) * 100 ;
2020-10-01 16:10:39 +00:00
2020-09-29 18:30:47 +00:00
//If BigTeam score is greater than otherteam score + threshold
if ( $ TeamScore [ $ BigTeam ] > ( $ TeamScore [ % otherTeam ] + $ Autobalance : : AMThreshold ) | | $ TeamRank [ % otherTeam , count ] $ = 0 )
2020-10-02 23:52:42 +00:00
$ Autobalance : : UseAllMode = 1 ;
//If BigTeam Top Players score is greater than otherTeam Top Players score + threshold
2020-09-29 18:30:47 +00:00
else if ( $ TeamRank [ $ BigTeam , count ] > = 5 & & $ TeamRank [ % otherTeam , count ] > = 3 )
{
% max = mfloor ( $ TeamRank [ $ BigTeam , count ] / 2 ) ;
2020-09-30 17:36:22 +00:00
if ( % max > $ TeamRank [ % otherTeam , count ] )
2020-09-29 18:30:47 +00:00
% max = $ TeamRank [ % otherTeam , count ] ;
% threshold = % max * 100 ;
for ( % i = 0 ; % i < % max ; % i + + )
{
% bigTeamTop = % bigTeamTop + $ TeamRank [ $ BigTeam , % i ] . score ;
% otherTeamTop = % otherTeamTop + $ TeamRank [ % otherTeam , % i ] . score ;
}
2020-10-01 16:10:39 +00:00
2020-09-29 18:30:47 +00:00
if ( % bigTeamTop > ( % otherTeamTop + % threshold ) )
2020-10-02 23:52:42 +00:00
$ Autobalance : : UseAllMode = 1 ;
2020-09-29 18:30:47 +00:00
}
2020-10-02 23:52:42 +00:00
//echo("Allmode " @ $Autobalance::UseAllMode);
2020-10-01 16:10:39 +00:00
2020-09-28 18:54:01 +00:00
//Select lower half of team rank as canidates for team change
2020-10-02 23:52:42 +00:00
if ( ! $ Autobalance : : UseAllMode )
2020-09-28 18:54:01 +00:00
{
2020-10-22 19:02:44 +00:00
//Reset clients canidate var
ResetABClients ( ) ;
2020-09-28 18:54:01 +00:00
$ Autobalance : : Max = mFloor ( $ TeamRank [ $ BigTeam , count ] / 2 ) ;
for ( % i = $ Autobalance : : Max ; % i < $ TeamRank [ $ BigTeam , count ] ; % i + + )
{
2020-10-12 06:20:38 +00:00
//echo("[Autobalance]: Selected" SPC $TeamRank[$BigTeam, %i].nameBase @ ", " @ %i);
2020-10-22 19:02:44 +00:00
$ TeamRank [ $ BigTeam , % i ] . abCanidate = true ;
2020-09-28 18:54:01 +00:00
}
% a = " selected" ;
}
2020-10-01 16:10:39 +00:00
2020-09-28 18:54:01 +00:00
if ( $ TeamRank [ $ BigTeam , count ] - $ TeamRank [ % otherTeam , count ] > = 3 )
2020-08-29 18:39:32 +00:00
% s = "s" ;
2020-10-01 16:10:39 +00:00
2020-08-29 18:39:32 +00:00
//Warning message
2020-09-28 18:54:01 +00:00
messageAll ( ' MsgTeamBalanceNotify ' , ' \ c1Teams are unbalanced : \ c0Autobalance will switch the next % 3 respawning player % 2 on Team % 1. ' , $ TeamName [ $ BigTeam ] , % s , % a ) ;
}
2020-10-22 19:02:44 +00:00
function ResetABClients ( )
2020-09-28 18:54:01 +00:00
{
2020-10-22 19:02:44 +00:00
for ( % i = 0 ; % i < $ TeamRank [ $ BigTeam , count ] ; % i + + )
2020-10-02 23:52:42 +00:00
{
2020-10-22 19:02:44 +00:00
$ TeamRank [ $ BigTeam , % i ] . abCanidate = false ;
2020-10-02 23:52:42 +00:00
}
2020-08-29 18:39:32 +00:00
}
package Autobalance
{
function DefaultGame : : onClientKilled ( % game , % clVictim , % clKiller , % damageType , % implement , % damageLocation )
{
2020-10-12 06:20:38 +00:00
parent : : onClientKilled ( % game , % clVictim , % clKiller , % damageType , % implement , % damageLocation ) ;
2020-08-29 18:39:32 +00:00
2020-10-12 06:20:38 +00:00
if ( $ BigTeam ! $ = "" & & % clVictim . team = = $ BigTeam )
{
2020-09-28 18:54:01 +00:00
% otherTeam = $ BigTeam = = 1 ? 2 : 1 ;
if ( $ TeamRank [ $ BigTeam , count ] - $ TeamRank [ % otherTeam , count ] > = 2 )
2020-10-01 16:10:39 +00:00
{
2020-10-12 06:20:38 +00:00
% fallback = 0 ;
2020-10-22 19:02:44 +00:00
if ( $ Autobalance : : FallbackTime $ = "" )
$ Autobalance : : FallbackTime = getSimTime ( ) ;
else if ( ( getSimTime ( ) - $ Autobalance : : FallbackTime ) < $ Autobalance : : Fallback )
2020-10-12 06:20:38 +00:00
% fallback = 1 ;
2020-10-01 16:10:39 +00:00
2020-09-28 18:54:01 +00:00
//damageType 0: If someone switches to observer or disconnects
2020-10-22 19:02:44 +00:00
if ( % damageType ! $ = 0 & & ( % clVictim . abCanidate | | $ Autobalance : : UseAllMode | | % fallback ) )
2020-09-03 22:45:29 +00:00
{
2020-10-14 19:59:24 +00:00
echo ( "[Autobalance]" SPC % clVictim . nameBase @ " has been moved to Team " @ % otherTeam @ " for balancing. [AM:" @ $ Autobalance : : UseAllMode SPC "#BT:" @ ( $ TeamRank [ $ BigTeam , count ] - 1 ) SPC "#OT:" @ ( $ TeamRank [ % otherTeam , count ] + 1 ) SPC "FB:" @ % fallback @ "]" ) ;
2020-09-28 18:54:01 +00:00
messageClient ( % clVictim , ' MsgTeamBalanceNotify ' , ' \ c0You were switched to Team % 1 for balancing . ~ wfx / powered / vehicle_screen_on . wav ' , $ TeamName [ % otherTeam ] ) ;
2020-09-03 22:45:29 +00:00
messageAllExcept ( % clVictim , - 1 , ' MsgTeamBalanceNotify ' , ' ~ wfx / powered / vehicle_screen_on . wav ' ) ;
2020-10-01 16:10:39 +00:00
2020-09-03 22:45:29 +00:00
Game . clientChangeTeam ( % clVictim , % otherTeam , 0 ) ;
}
2020-08-24 21:56:36 +00:00
}
2020-08-29 18:39:32 +00:00
else
2019-11-20 19:51:02 +00:00
{
2020-10-22 19:02:44 +00:00
ResetABClients ( ) ;
2020-08-29 18:39:32 +00:00
ResetTBNStatus ( ) ;
2020-10-22 19:02:44 +00:00
$ BigTeam = "" ;
2019-11-20 19:51:02 +00:00
}
2020-10-12 06:20:38 +00:00
}
2019-09-27 16:48:11 +00:00
}
2020-08-29 18:39:32 +00:00
function DefaultGame : : gameOver ( % game )
2019-09-27 16:48:11 +00:00
{
2020-08-29 18:39:32 +00:00
Parent : : gameOver ( % game ) ;
2020-10-01 16:10:39 +00:00
2020-08-29 18:39:32 +00:00
//Reset Autobalance
$ BigTeam = "" ;
2020-10-22 19:02:44 +00:00
//Reset all clients canidate var
for ( % i = 0 ; % i < ClientGroup . getCount ( ) ; % i + + )
{
% client = ClientGroup . getObject ( % i ) ;
% client . abCanidate = false ;
}
2020-08-29 18:39:32 +00:00
}
} ;
// Prevent package from being activated if it is already
if ( ! isActivePackage ( Autobalance ) )
2020-10-01 16:10:39 +00:00
activatePackage ( Autobalance ) ;