TacoServer/Classic/scripts/autoexec/Autobalance.cs

143 lines
3.7 KiB
C#
Raw Normal View History

2019-09-13 15:29:42 -04:00
// Team Autobalance Script
//
// Determines which team needs players and proceeds to find candidates
// Candidates are based on low scores then switches the candidate
//
2019-02-06 13:39:36 -05:00
// Enable or Disable Autobalance
// $Host::EnableAutobalance = 1;
2019-09-13 15:29:42 -04:00
// How far behind littleTeam must be to use All Mode.
// Meaning picking from a pool of all players on the bigTeam instead of just the lowest scoring player.
// 400 equals 400 points. 4 caps behind.
2020-08-18 13:58:49 -04:00
$AllModeThreshold = 300;
2019-09-13 15:29:42 -04:00
// Run from TeamBalanceNotify.cs via UnbalancedSound( %game )
2019-02-04 22:54:58 -05:00
function Autobalance( %game, %AutobalanceSafetynetTrys )
2019-09-13 15:29:42 -04:00
{
if(isEventPending($AutoBalanceSchedule))
cancel($AutoBalanceSchedule);
if( $TBNStatus !$= "NOTIFY" ) //If Status has changed to EVEN or anything else (GameOver reset).
return;
2019-09-28 12:01:30 -04:00
//Debug: Uncomment to enable
2019-09-13 15:29:42 -04:00
//%AutobalanceDebug = true;
//Difference Variables
2020-03-25 23:26:45 -04:00
%team1difference = $TeamRank[1, count] - $TeamRank[2, count];
%team2difference = $TeamRank[2, count] - $TeamRank[1, count];
2019-09-13 15:29:42 -04:00
2019-09-28 12:01:30 -04:00
//Determine bigTeam
2020-08-18 13:58:49 -04:00
if( %team1difference >= 2 )
2019-09-27 12:48:11 -04:00
%bigTeam = 1;
2019-09-13 15:29:42 -04:00
else if( %team2difference >= 2 )
2019-09-27 12:48:11 -04:00
%bigTeam = 2;
2020-08-18 13:58:49 -04:00
else
return;
2019-09-27 12:48:11 -04:00
%littleTeam = ( %bigTeam == 1 ) ? 2 : 1;
//Toggle for All Mode
//If a team is behind pick anyone, not just a low scoring player
2020-03-25 23:26:45 -04:00
if( $TeamScore[%bigTeam] > ($TeamScore[%littleTeam] + $AllModeThreshold))
{
2020-08-23 03:20:42 -04:00
%UseAllMode = 1;
//Find if anyone is holding a flag for exceptions for the loop
for(%i = 0; %i < ClientGroup.getCount(); %i++)
{
%client = ClientGroup.getObject(%i);
%team = %client.team;
if(%team $= %bigTeam)
{
//Holding flag?
if(%client.player.holdingFlag !$= "")
%exception = 1;
}
}
%autobalanceRandom = getRandom(1,($TeamRank[%bigTeam, count] - %exception));
}
//Pick a client for autobalance
for(%i = 0; %i < ClientGroup.getCount(); %i++)
{
%client = ClientGroup.getObject(%i);
%team = %client.team;
if(%team $= %bigTeam)
{
2020-08-23 03:20:42 -04:00
//Holding flag?
if(%client.player.holdingFlag !$= "")
continue;
if(%UseAllMode)
{
//Pick our random
if(%autobalanceRandom == %AllmodeLoop || %canidate $= "")
%canidate = %client;
%AllmodeLoop++;
}
else
{
//Try to pick a low scoring player
if(%client.score < %canidate.score || %canidate $= "")
%canidate = %client;
}
}
}
2019-09-27 12:48:11 -04:00
//Debug
if( %AutobalanceDebug )
AutobalanceDebug(%canidate, %team1difference, %team2difference, %bigTeam, %AutobalanceSafetynetTrys, %UseAllMode);
2019-09-27 12:48:11 -04:00
%client = %canidate;
%team = %canidate.team;
2019-09-27 12:48:11 -04:00
%otherTeam = ( %team == 1 ) ? 2 : 1;
2019-02-03 18:00:24 -05:00
2020-08-22 16:57:41 -04:00
//Fire Autobalance
Game.clientChangeTeam( %client, %otherTeam, 0 );
2020-03-23 16:23:42 -04:00
messageClient(%client, 'MsgTeamBalanceNotify', "\c0You were switched to the other team for balancing.~wfx/powered/vehicle_screen_on.wav");
messageAllExcept(%client, -1, 'MsgTeamBalanceNotify', "~wfx/powered/vehicle_screen_on.wav");
2019-09-27 12:48:11 -04:00
//Trigger GetCounts
ResetGetCountsStatus();
//Reset TBN
ResetTBNStatus();
2019-09-27 12:48:11 -04:00
}
function AutobalanceDebug(%canidate, %team1difference, %team2difference, %bigTeam, %AutobalanceSafetynetTrys, %UseAllMode)
2019-09-27 12:48:11 -04:00
{
2019-09-28 12:01:30 -04:00
if( %teamcanidate[%bigTeam] $= "" )
{
%AutobalanceSafetynetTrys++;
if(%AutobalanceSafetynetTrys $= 3)
return;
if( %canidate $= "" )
2019-09-28 12:01:30 -04:00
%error = "Team " @ %bigTeam;
if( %error !$= "" )
messageAll('MsgTeamBalanceNotify', '\c0Autobalance error: %1', %error );
//Trigger GetCounts
ResetGetCountsStatus();
2019-09-28 12:01:30 -04:00
//Rerun in 10 secs
schedule(10000, 0, "Autobalance", %game, %AutobalanceSafetynetTrys );
}
if(%UseAllMode)
2020-08-22 16:57:41 -04:00
%mode = "All";
else
2020-08-22 16:57:41 -04:00
%mode = "Low";
2019-09-28 12:01:30 -04:00
if( %teamcanidate1 $= "" )
%teamcanidate1 = "NULL";
if( %teamcanidate2 $= "" )
%teamcanidate2 = "NULL";
2019-09-13 15:29:42 -04:00
messageAll('MsgTeamBalanceNotify', '\c0Autobalance stat: %1, %2, %3, %4', %canidate, %team1difference, %team2difference, %mode );
2019-09-28 12:01:30 -04:00
return;
2019-02-03 18:00:24 -05:00
}