Sign in to follow this  
arbait01

AntiAFK and AutoTeamBalancer

Recommended Posts

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:

level thread AutoTeamsBalancer();

and the function AFK_Camp_Watcher in the spawnPlayer() function, always in the _globallogic file:

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:

#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:

#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!");
           }
       }
   }
}

AntiAFK_AutoTeamsBalancer_Script.zip

  • Thanks 2

Share this post


Link to post
Share on other sites

hello mate

nice idea

tell me please

work for all gametypes?

my big problem is SAB and SD

if it possible FIX time dayle "teambalance_check_interval" 30sec

 

PS : your script format GSX if I rename GSC will work ?

Edited by {cool.G} ingre

Share this post


Link to post
Share on other sites
20 hours ago, {cool.G} ingre said:

hello mate

nice idea

tell me please

work for all gametypes?

my big problem is SAB and SD

if it possible FIX time dayle "teambalance_check_interval" 30sec

 

PS : your script format GSX if I rename GSC will work ?

COD4x will accept both GSX and GSC. For the sake of simplicity use GSX then you know its a COD4X script rather than stock gsc.

  • Like 1

Share this post


Link to post
Share on other sites
On 1/8/2017 at 3:43 PM, arbait01 said:

I called the AutoTeamsBalancer function in the init() function of the _globallogic file:

Can anybody specify this?

Share this post


Link to post
Share on other sites
25 minutes ago, D4edalus said:

I edited my _globallogic.gsx like this, Is that right the calling the function?

include maps\mp\_utility;
#include maps\mp\gametypes\_hud_util;
#include crazy\_globallogic_utils;

init()
{
    thread maps\mp\gametypes\_autoteambalancer::init();
    if(!isDefined(level.tweakablesInitialized))
    maps\mp\gametypes\_tweakables::init();
    level.splitscreen=0;
    level.xenon=0;
    level.ps3=0;
    level.console=0;
    level.oldschool=0;

Share this post


Link to post
Share on other sites
On 09.01.2017 at 6:43 AM, arbait01 said:

AFK_Camp_Watcher:


#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!");
       }
   }
}
 

i have some bug - after death player can camp and not move to spect.. i think it because:

   level waittill("prematch_over");

on my TDM server, i think, it's not necessarily...

edited like this

On 09.01.2017 at 6:43 AM, arbait01 said:

and the function AFK_Camp_Watcher in the spawnPlayer() function, always in the _globallogic file:


self thread Camp_Watcher();

 

Camp_Watcher()
{
	//level waittill("prematch_over");
	self endon("death");
	self endon("disconnect");
	self endon("joined_spectators");
	self endon("game_ended");
	my_camp_time = 0;
	have_i_been_warned = false;
	max_distance = 240;
	camp_time = 20;

	while( 1 )
	{
		old_position = self.origin;
		wait 1;
	
		new_position = self.origin;
		distance = distance2d( old_position, new_position );
	
		if( distance < max_distance )
			my_camp_time++;
		else
		{
			my_camp_time = 0;
			have_i_been_warned = false;
		}
	
		if( my_camp_time == camp_time && !have_i_been_warned )
		{
			self IprintLnBold("^7Stop ^1CAMP^7, noob!");
			self IprintLnBold("^710 seconds to move!");
			have_i_been_warned = true;
		}

		if( my_camp_time == ( camp_time + 10 ) && have_i_been_warned )
		{
			self IprintLnBold("^7You will be moved to spectators for ^1Camping^7!");
			wait 2;
			self.sessionteam = "spectator";
			self.sessionstate = "spectator";
			self [[level.spawnSpectator]]();
			iPrintln("^7" +self.name + " ^7was moved to spectators for ^1Camping^7!");
			self notify("joined_spectators");
		}
	}
}

couple of days i will test this!

ThX!

Edited by score4fun

Share this post


Link to post
Share on other sites

Hey, I am having some difficulty getting the anticamp script to work, keep crashing the server, I have followed the guide D4edalus suggested but I must be calling the function incorrectly or in the wrong area in the _globallogic 

 

Thanks again guys

Share this post


Link to post
Share on other sites

I threaded it from top of the _teams.gsc works fine for me.

init()
{
    thread maps\mp\gametypes\_anticamper::init();

   
    switch(game["allies"])
    {
 

Share this post


Link to post
Share on other sites

Hello, I tried to install on my cod4x 1.8 server. But I couldnt' get it running.

callback isn't working. I got this warning.

******* script compile error *******
Error: Could not find script 'autoteamsbalancer'

Can anyone help me?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this