Skip to content

Instantly share code, notes, and snippets.

@D4edalus
Last active August 29, 2016 14:25
Show Gist options
  • Save D4edalus/92c2d87903e7589db1ad1a8ddf36574d to your computer and use it in GitHub Desktop.
Save D4edalus/92c2d87903e7589db1ad1a8ddf36574d to your computer and use it in GitHub Desktop.
team balance via script command
findPlayerByNumber( entityNumber )
{
foundPlayer = undefined;
// Search for the player
for ( index = 0; index < level.players.size; index++ )
{
player = level.players[index];
// Check if this the player we are looking for
if ( player getEntityNumber() == entityNumber ) {
foundPlayer = player;
break;
}
}
return foundPlayer;
}
findWorstPlayerInTeam( team )
{
worstPlayer = undefined;
for ( index = 0; index < level.players.size; index++ )
{
player = level.players[index];
if(player.pers["team"] == team)
{
if(!isDefined(worstPlayer) || player.pers["score"] < worstPlayer.pers["score"])
worstPlayer = player;
}
}
return worstPlayer;
}
playerSwitchTeam( player, newTeam )
{
if ( !isDefined( player ) || player.pers["team"] == newTeam )
return;
// Kill the player if it's alive
if ( isAlive( player ) )
{
// Set a flag on the player to they aren't robbed points for dying - the callback will remove the flag
player.switching_teams = true;
player.joining_team = newTeam;
player.leaving_team = player.pers["team"];
// Suicide the player so they can't hit escape
player suicide();
}
player.pers["team"] = newTeam;
player.team = newTeam;
if ( newTeam != "spectator" )
{
player iprintlnbold( "teamswitched" );
player.pers["teamTime"] = undefined;
player.sessionteam = player.pers["team"];
player maps\mp\gametypes\_globallogic::updateObjectiveText();
// update spectator permissions immediately on change of team
player maps\mp\gametypes\_spectating::setSpectatePermissions();
if ( player.pers["team"] == "allies" )
{
player setclientdvar("g_scriptMainMenu", game["menu_class_allies"]);
player openMenu( game[ "menu_changeclass_allies" ] );
}
else if ( player.pers["team"] == "axis" )
{
player setclientdvar("g_scriptMainMenu", game["menu_class_axis"]);
player openMenu( game[ "menu_changeclass_axis" ] );
}
player notify( "end_respawn" );
}
}
Callback_ScriptCommand(command, arguments)
{
waittillframeend;
/*if( isDefined( self.name ) )
iprintlnbold("Executed by: " + self.name + " Command:" + command + " Args: " + arguments + "\n");
else
iprintlnbold("Executed by: Rcon Command:" + command + " Args: " + arguments + "\n");*/
//iprintlnbold("test: " + level.players.size);
//for ( t=0; t < level.players.size; t++ )
//{
if(command == "rules_function")
{
player = findPlayerByNumber(int(arguments));
if( isDefined(player))
{
notifyData = spawnstruct();
notifyData.duration = 15;
notifyData.textSpeed = 20;
notifyData.titleText = ">>> Rules <<<";
notifyData.notifyText = "1. Juggernaut, Ls, Gl, Martyr and 3x Frags are disabled\n2. Camping is only allowed with sniper (no overkill)\n3. No script/scrollfire\n4. No glitching/elevating/333fps jumps\n5. No insult/racism\n6. English only\n7. No unbalancing";
notifyData.glowColor = (1.0, 0.2, 0.2); //RGB Color array divided by 100
player thread maps\mp\gametypes\_hud_message::notifyMessage( notifyData );
}
}
else if(command == "team_balance")
{
//iprintlnbold(arguments);
teamCount = 0;
for ( index = 0; index < level.players.size; index++ )
{
player = level.players[index];
//iprintln(player.pers["team"]);
if(player.pers["team"] == "allies")
teamCount++;
else if(player.pers["team"] == "axis")
teamCount--;
}
//println("teamcount " + teamCount);
while(abs(teamCount) > 1) // we have to balance some players
{
if(teamCount > 1)
{
p = findWorstPlayerInTeam("allies");
playerSwitchTeam(p, "axis");
teamCount--;
teamCount--;
}
else if(teamCount < -1)
{
p = findWorstPlayerInTeam("axis");
playerSwitchTeam(p, "allies");
teamCount++;
teamCount++;
}
}
}
else if(command == "add_test_client")
{
entity = AddTestClient();
}
else if(command == "remove_test_clients")
{
removeAllTestClients();
}
//}
}
@ekroot
Copy link

ekroot commented Aug 29, 2016

So if i add this code to _globallogic.gsc will it work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment