Create Account


AntiAFK and AutoTeamBalancer

#1

Hello,
I share antiAFK and autobalance script that I started to use on my server.
This is the first version, so the script can be optimized and maybe contain bugs.

I'm not sure (I tested it only in promod) but the script can be used on any mod and default gametypes.

I called the AutoTeamsBalancer function in the init() function of the _globallogic file:
Code:
level thread AutoTeamsBalancer();

and the function AFK_Camp_Watcher in the spawnPlayer() function, always in the _globallogic file:
Code:
self thread AFK_Camp_Watcher();

but it is possible to call these functions from sd.gsx, dm.gsx, etc... file in the right callback (onSpawnPlayer, init...).

The AFK_Camp_Watcher function is based on the example made in the Call of Duty 4: CoD Script Handbook.

This autobalance version move the 'extra' players that have the lowest score in the team.
Of course you can change the sort order in the function getLowScorePlayers (for example on spawnTime) and script should work the same.

You can freely use the shared code.

I hope I'll help someone sharing it.

Regards,
arbait


AFK_Camp_Watcher:
Code:
#include maps\mp\_utility;
#include maps\mp\gametypes\_hud_util;
#include common_scripts\utility;

AFK_Camp_Watcher()
{
   level waittill("prematch_over");
   self endon("death");
   self endon("disconnect");
   self endon("joined_spectators");
   self endon("game_ended");
   my_afk_camp_time = 0;
   have_i_been_warned = false;
   camp_time = 20;
   while( 1 )
   {
       old_position = self.origin;
       old_angles = self.angles;
       wait 1;
       new_position = self.origin;
       new_angles = self.angles;
       distance = distance2d( old_position, new_position );
       if( distance == 0 && old_angles == new_angles ) {
           my_afk_camp_time++;
       }
       else {
           my_afk_camp_time = 0;
           have_i_been_warned = false;
       }
       if( my_afk_camp_time == camp_time && !have_i_been_warned )
       {
           self IprintLnBold("^7You appear to be ^1AFK^7!");
           self IprintLnBold("^710 seconds to move!");
           have_i_been_warned = true;
       }
       if( my_afk_camp_time == ( camp_time + 10 ) && have_i_been_warned ) {
           self IprintLnBold("^7You will be moved to spectators for ^1AFK ^7/ ^1Camping^7!");
           wait 2;
           self.sessionteam = "spectator";
           self.sessionstate = "spectator";
           self [[level.spawnSpectator]]();
           iPrintln("^7" +self.name + " ^7was moved to spectators for ^1AFK ^7/ ^1Camping^7!");
       }
   }
}

AutoTeamsBalancer:
Code:
#include maps\mp\_utility;
#include maps\mp\gametypes\_hud_util;
#include common_scripts\utility;

getTeamPlayers(team) {
   result = [];
   players = level.players;
   for(i = 0; i < players.size; i++) {
       if (isDefined(players[i]) && players[i].pers["team"] == team){
           result[result.size] = players[i];
       }
   }
   return result;
}

getLowScorePlayers(team, nPlayers) {
   result = [];
   if (team.size > 0 && nPlayers > 0 && team.size >= nPlayers) {
       //Sorting team by score (bubble sort algorithm @TODO optimize)
       for (x = 0; x < team.size; x++) {
           for (y = 0; y < team.size - 1; y++) {
               if (isDefined(team[y]) && isDefined(team[y+1]) && team[y].pers["score"] > team[y+1].pers["score"]) {
                   temp = team[y+1];
                   team[y+1] = team[y];
                   team[y] = temp;
               }
           }
       }
       for (i = 0; i < nPlayers; i++) {
           if (isDefined(team[i])) {
               result[i] = team[i];
           }
       }
   }
   return result;
}

AutoTeamsBalancer() {
   if(level.gametype == "dm")
       return;
   pl_change_team = [];
   changeteam = "";
   offset = 0;
   while(1) {
       wait 20;
       if (isDefined(game["state"]) && game["state"] == "playing") {
           pl_change_team = [];
           changeteam = "";
           offset = 0;
           team["axis"] = getTeamPlayers("axis");
           team["allies"] = getTeamPlayers("allies");
           if(team["axis"].size == team["allies"].size)
               continue;
           
           if(team["axis"].size < team["allies"].size) {
               changeteam = "axis";
               offset = team["allies"].size - team["axis"].size;
           }
           else {
               changeteam = "allies";
               offset = team["axis"].size - team["allies"].size;
           }
           if (offset < 2)
               continue;
           iPrintln("^7Teams will be balanced in 5 sec...");
           wait 5;
           if (isDefined(game["state"]) && game["state"] == "playing") {
               team["axis"] = getTeamPlayers("axis");
               team["allies"] = getTeamPlayers("allies");
               if(team["axis"].size == team["allies"].size) {
                   iPrintln("^7AutoBalance aborted: teams are already balanced!");
                   continue;
               }
               if(team["axis"].size < team["allies"].size) {
                   changeteam = "axis";
                   offset = team["allies"].size - team["axis"].size;
               }
               else {
                   changeteam = "allies";
                   offset = team["axis"].size - team["allies"].size;
               }
               if (offset < 2) {
                   iPrintln("^7AutoBalance aborted: teams are already balanced!");
                   continue;
               }
               offset = offset / 2;
               pl_to_add = int(offset) - (int(offset) > offset);
               pl_change_team = [];
               bigger_team = [];
               if (changeteam == "allies"){
                   bigger_team = team["axis"];
               }
               else {
                   bigger_team = team["allies"];
               }
               pl_change_team = getLowScorePlayers(bigger_team, pl_to_add);
               for(i = 0; i < pl_change_team.size; i++) {
                   if(changeteam == "axis")
                       pl_change_team[i] [[level.axis]]();
                   else
                       pl_change_team[i] [[level.allies]]();
               }
               iPrintln("^7Teams were balanced!");
               iPrintlnbold("^7Teams were balanced!");
           }
       }
   }
}


Attached Files
.zip   AntiAFK_AutoTeamsBalancer_Script.zip (Size: 2.02 KB / Downloads: 19)
Reply

#2

thank you
Reply






ABOUT US
A client and server modification for Call of Duty: Modern Warfare with the aim to fix bugs and enhance gameplay!