Author Topic: CTF Team Balancer  (Read 4999 times)

0 Members and 1 Guest are viewing this topic.

Offline Swompie

  • Camper
  • ***
  • Posts: 390
CTF Team Balancer
« on: August 03, 2010, 01:35:07 pm »
Before you post anything: Yes I know there've been s**tloads of other balancers, but they may be no longer working on never versions or cause other problems.

Author: Ofc. me (Swompie)
Server/Core Version: 2.6.5+
Commands: !balance (to balance the teams ::))

You can adjust the color of the balance messages and
 the automatic balance interval at the top of the script.






« Last Edit: August 03, 2010, 02:50:49 pm by Swompie »

Offline Falcon`

  • Flagrunner
  • ****
  • Posts: 792
  • A wanted lagger
Re: CTF Team Balancer
« Reply #1 on: August 03, 2010, 03:46:17 pm »
You know, build in balancer should always work.
If you're not paying for something, you're not the customer; you're the product being sold.
- Andrew Lewis

Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

Offline Bonecrusher

  • Global Moderator
  • Veteran
  • *****
  • Posts: 1397
  • High above
    • Zabijaka.pl
Re: CTF Team Balancer
« Reply #2 on: August 03, 2010, 04:11:35 pm »
There's no built-in team balancer, only bot balance.

Im chill like that

Offline StayAlive

  • Major(1)
  • Posts: 8
Re: CTF Team Balancer
« Reply #3 on: June 08, 2012, 07:26:07 am »
So, I've tried to run this script on my Soldat Dedicated Server 1.6.3 (v2.7.3) but everytime when I used it when there was no balance server crashed.
I've looked into code and make some experiments.
I've discovered that when there are for exmaple 2 bravo players and 1 alpha player variable called 'Difference' have value 255, when there are 3 bravo players and 1 alpha player this variable have value 254 and so on. I've checked values of Alphaplayers and BravoPlayers by showing them on screen by WriteConsole but they were working fine. There is a problem when we want to save result of substraction these two variables. Strange thing. If we try to do this, as I said variable 'Difference' goes crazy and have values above 200 and then none of 'if procedures' are working. Result - crash because of 'Balance()' call in Balance procedure (inifinite loop). I don't know why is this happening but I don't care. I fixed this script by making 'A' and 'B' variables first and then save to it values of AlphaPlayers and BravoPlayers. Now it's working fine.
I'm not sure if I describe it well but code will be definitely enough:
Code: [Select]
        {|  CTF Team Balancer by Swompie (©2o1o) - Modified by StayAlive (@2012)   |}
{|  Working on Soldat Dedicated Server 1.6.3 (v2.7.3) |}

const
  cBalance = $FF0000;   // Color of the messages
  BalanceInterval = 60; // Delay beetween the teams will be balanced in seconds.

function GetTeamArray(const Team: byte): Array of byte;
var i, x: integer;
begin
  i := 1;
  x := 0;
  while i < 32 do begin
    if GetPlayerStat(i, 'Active') = true then
      if GetPlayerStat(i, 'Team') = Team then begin
        SetArrayLength(Result, x + 1);
        Result[x] := i;
        x := x + 1;
      end;
    i := i + 1;
  end;
end;

procedure Balance();
var i, Difference: integer;
    A: integer;
B: integer;
    Players: Array of byte;
begin
  //changed by StayAlive (@2012)
  A := AlphaPlayers;
  B := BravoPlayers;
  Difference := A - B;
  //end of change
  if (Difference > 1) and (AlphaPlayers > 1) then begin
    Players := GetTeamArray(1);
    while (true) do begin
      i := Players[Random(0, GetArrayLength(Players) - 1)];
      if GetPlayerStat(i, 'Flagger') = false then
        break;
    end;
Command('/setteam2 ' + inttostr(i));
    WriteConsole(i, 'You were balanced due to unbalanced teams.', cBalance);
  end;

  if (Difference < -1) and (BravoPlayers > 1) then begin
Players := GetTeamArray(2);
    while (true) do begin
      i := Players[Random(0, GetArrayLength(Players) - 1)];
      if GetPlayerStat(i, 'Flagger') = false then
        break;
    end;
    Command('/setteam1 ' + inttostr(i));
    WriteConsole(i, 'You were balanced due to unbalanced teams.', cBalance);
  end;
  //changed by StayAlive (@2012)
  A := AlphaPlayers;
  B := BravoPlayers;
  Difference := A - B;
  //end of change

  if (Difference > 1) or (Difference < -1) then
    Balance()
  else WriteConsole(0, 'Teams balanced. If you see no result teams are balanced.', cBalance);

end;

procedure OnPlayerSpeak(ID: byte; Text: string);
begin
  if LowerCase(Copy(Text, 1, 8)) = '!balance' then
    Balance();
end;

procedure AppOnIdle(Ticks: integer);
begin
  if Ticks mod (60 * BalanceInterval) = 0 then
    Balance();
end;


Oh, and I've changed top sign cause of my work, will it be okay?

Offline Bonecrusher

  • Global Moderator
  • Veteran
  • *****
  • Posts: 1397
  • High above
    • Zabijaka.pl
Re: CTF Team Balancer
« Reply #4 on: June 08, 2012, 09:51:39 am »

Im chill like that

Offline smurf22a

  • Major(1)
  • Posts: 5
Re: CTF Team Balancer
« Reply #5 on: December 31, 2014, 06:05:01 am »
Long thought how to fix this script :)
Well, I fixed the best and most efficient script for balance teams. (By  Ofc. me (Swompie))
Is 10x times better than those stupid cube script which is inefficient. So many people are searching for something so simple, and here's the best and most popular script returns the older times. I added only antibalance for specators :)
Download: Attachment! Click here to download the latest version.
Core Version: 2.7.7+
Code:
Code: [Select]
const
cBalance = $FF0000; // Color of the messages.
BalanceInterval = 120; // Delay beetween the teams will be balanced in seconds.
var
a,b: integer;

function GetTeamArray(const Team: byte): Array of byte;
var i,x: integer;
begin
i := 1;
x := 0;
while (i < 32) do begin
if (GetPlayerStat(i, 'Active') = true) then if (GetPlayerStat(i, 'Team') = Team) then begin
SetArrayLength(Result, x + 1);
Result[x] := i;
inc(x,1);
end;
inc(i,1);
end;
end;

procedure Balance();
var i,Difference: integer;
    Players: Array of byte;
begin
Difference:=(a-b);
if (Difference > 1) and (a > 1) then begin
Players := GetTeamArray(1);
while (true) do begin
i := Players[Random(1, GetArrayLength(Players) - 1)];
if (GetPlayerStat(i, 'Flagger') = false) then break;
end;
Command('/setteam2 ' + inttostr(i));
dec(a,1);
WriteConsole(i, 'You were balanced due to unbalanced teams.', cBalance);
end;
if (Difference < -1) and (b > 1) then begin
Players := GetTeamArray(2);
while (true) do begin
i := Players[Random(1, GetArrayLength(Players) - 1)];
if (GetPlayerStat(i,'Flagger') = false) then break;
end;
Command('/setteam1 ' + inttostr(i));
dec(b,1);
WriteConsole(i, 'You were balanced due to unbalanced teams.', cBalance);
end;
Difference:=(a-b);
if (Difference > 1) or (Difference < -1) then Balance() else WriteConsole(0, 'Teams balanced. If you see no result teams are balanced.', cBalance);
end;

procedure OnPlayerSpeak(ID: byte; Text: string);
begin
if regExpMatch('^!(balance|bal|teams)$',lowercase(Text)) then if (GetPlayerStat(ID,'Team')<>5) begin
a:=AlphaPlayers;
b:=BravoPlayers;
Balance();
end else WriteConsole(ID, 'You can`t balance the teams being on spec.', cBalance);
end;

procedure AppOnIdle(Ticks: integer);
begin
if (Ticks mod (60 * BalanceInterval) = 0) then begin
a:=AlphaPlayers;
b:=BravoPlayers;
Balance();
end;
end;

More recent my scripts you can find here.
« Last Edit: December 31, 2014, 06:11:47 am by smurf22a »

DarkCrusade

  • Guest
Re: CTF Team Balancer
« Reply #6 on: December 31, 2014, 08:57:45 am »
Yo dominikk26