TacoServer/Classic/scripts/autoexec/Autobalance.cs

113 lines
3 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
// 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
{
2019-09-28 12:01:30 -04:00
//Debug: Uncomment to enable
2019-09-13 15:29:42 -04:00
//%AutobalanceDebug = true;
2019-09-28 12:01:30 -04:00
//Reset
2019-09-13 15:29:42 -04:00
%lastclient1 = "";
%lastclient2 = "";
2019-02-04 22:54:58 -05:00
2019-09-13 15:29:42 -04:00
//Team Count code by Keen
$PlayerCount[0] = 0;
$PlayerCount[1] = 0;
$PlayerCount[2] = 0;
2019-02-04 22:54:58 -05:00
2019-09-13 15:29:42 -04:00
for(%i = 0; %i < ClientGroup.getCount(); %i++)
{
%client = ClientGroup.getObject(%i);
2019-02-04 22:54:58 -05:00
2019-09-13 15:29:42 -04:00
//Pick a client for autobalance
2019-09-27 12:48:11 -04:00
//Try to pick a low scoring player
2019-09-28 12:01:30 -04:00
%team = %client.team;
2019-09-13 15:29:42 -04:00
if(%client.score < %lastclient[%team].score || %lastclient[%team] $= "")
%teamcanidate[%team] = %client;
%lastclient[%team] = %client;
//if(!%client.isAIControlled())
$PlayerCount[%client.team]++;
}
//Difference Variables
%team1difference = $PlayerCount[1] - $PlayerCount[2];
%team2difference = $PlayerCount[2] - $PlayerCount[1];
2019-09-28 12:01:30 -04:00
//If even, stop.
2019-09-13 15:29:42 -04:00
if( %team1difference == 1 || %team2difference == 1 || $PlayerCount[1] == $PlayerCount[2] )
{
$StatsMsgPlayed = 0;
return;
}
2019-09-28 12:01:30 -04:00
//Determine bigTeam
2019-09-13 15:29:42 -04:00
else 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;
//Debug
if( %AutobalanceDebug )
2019-09-28 12:01:30 -04:00
AutobalanceDebug(%teamcanidate1, %teamcanidate2, %team1difference, %team2difference, %bigTeam, %AutobalanceSafetynetTrys);
2019-09-27 12:48:11 -04:00
%client = %teamcanidate[%bigTeam];
%team = %teamcanidate[%bigTeam].team;
%otherTeam = ( %team == 1 ) ? 2 : 1;
2019-02-03 18:00:24 -05:00
2019-09-27 12:48:11 -04:00
if( %teamcanidate[%bigTeam].team $= %bigTeam )
{
2019-09-28 12:01:30 -04:00
// Fire Autobalance
2019-09-27 12:48:11 -04:00
Game.clientChangeTeam( %client, %otherTeam, 0 );
messageAll('MsgTeamBalanceNotify', '~wfx/powered/vehicle_screen_on.wav');
}
else
messageAll('MsgTeamBalanceNotify', '\c0Autobalance error: Team %1 mismatch.', %bigTeam );
2019-02-03 18:00:24 -05:00
2019-09-27 12:48:11 -04:00
//Trigger GetCounts
ResetClientChangedTeams();
//Reset Unbalanced
$UnbalancedMsgPlayed = 0;
return;
}
2019-09-28 12:01:30 -04:00
function AutobalanceDebug(%teamcanidate1, %teamcanidate2, %team1difference, %team2difference, %bigTeam, %AutobalanceSafetynetTrys)
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( %teamcanidate1 $= "" && %teamcanidate2 $= "" )
%error = "Both Teams";
else if( %teamcanidate[%bigTeam] $= "" )
%error = "Team " @ %bigTeam;
if( %error !$= "" )
messageAll('MsgTeamBalanceNotify', '\c0Autobalance error: %1', %error );
//Trigger GetCounts
ResetClientChangedTeams();
//Rerun in 10 secs
schedule(10000, 0, "Autobalance", %game, %AutobalanceSafetynetTrys );
}
if( %teamcanidate1 $= "" )
%teamcanidate1 = "NULL";
if( %teamcanidate2 $= "" )
%teamcanidate2 = "NULL";
2019-09-13 15:29:42 -04:00
2019-09-28 12:01:30 -04:00
messageAll('MsgTeamBalanceNotify', '\c0Autobalance stat: %1, %2, %3, %4', %teamcanidate1, %team1difference, %teamcanidate2, %team2difference );
return;
2019-02-03 18:00:24 -05:00
}