Scripts:Autoteam by Tickets

From BF2 Technical Information Wiki
Jump to navigation Jump to search


I was toying with the idea of using the teamBalanceRatio server setting to alter the team balance based on team ticket counts. This script basically gives more people to the team with less tickets in inverse proportion to the ticket ratios. This little toy could be a real pain in the ass on servers where all users are eligible to be autoteamed, but some might find it useful anyway. We use it on our main server at in conjuntion with a modified to basically put new players on the losing team. Should you want similar results, certain edits will have to be made to your If you need more details, visit my website.

What It Does

If autoteam balance is on, the sever setting sv.teamRatioPercent is adjusted according to the current ticket ratio in favor of the team with less tickets on every PlayerKilled event. Regardless of the sv.autoBalanceTeam value, sv.teamRatioPercent is saved on every GameStatus.PreGame Event, and restored on every GameStatus.EndGame Event. There are various reasons for this, and most come from my inability to see if sv.autoBalanceTeam has been toggled at any point. There are workaround to this that are more elegant, but none that I can see are as efficient as this one.


  • I plan to add certain features to improve the user-friendliness of this script, such as stopping the automove when the team balance ratio gets to a certain degree, or if the game is coming to an end within 'x' tickets or 'y' seconds.
  • As with all scripts I write, my comments suck. I apologize --dst 22:36, 28 Jul 2005 (MDT) Battlefield 2 auto team balance based on ticket ratio
 # v1.2
 # Place in admin/standard_admin/
 # Add "import dc_teamtickets" and "dc_teamtickets.init()" to the file in that directory.
 # Once this is imported, if auto team balance is on, the team balance ratio will be set inverse to the ticket ratio.
 import host
 import bf2
 from bf2 import g_debug
 autobalance = 0
 originalratio = 0
 def init():
     if g_debug: print 'initializing Team/Ticket manager'
     host.registerHandler('PlayerKilled', onPlayerKilled, 1)
 #Check ticket ratio every time someone get killed
 def onPlayerKilled(victimPlayerObject, attackerPlayerObject, weaponObject, assists, victimSoldierObject):
     global autobalance
     #if autobalance is off, don't change the ratio.
     autobalance = int(bf2.serverSettings.getAutoBalanceTeam())
     if autobalance:
         t1tickets = host.sgl_getParam('tickets', 1, 0)
         t2tickets = host.sgl_getParam('tickets', 2, 0)
             teamratio = int(float(t2tickets) / float(t1tickets) * 100)
             #hardcoded a cap of balance ratios to prevent ridiculous team stacking by autoteam
             #this is a 4/3 ratio
             if teamratio > 133: teamratio = 133
             if teamratio < 75: teamratio = 75
             host.rcon_invoke('sv.teamRatioPercent %d' % teamratio)
         except ZeroDivisionError:
 #Grab the teamratiopercent on pregame, reset it on endgame.
 #Assuming that this is the only script messing with the ratio, all will be well.
 def onStatusChange(status):
     global originalratio
     if status == bf2.GameStatus.PreGame:
         originalratio = int(bf2.serverSettings.getTeamRatioPercent())
     if status == bf2.GameStatus.EndGame:
         host.rcon_invoke('sv.teamRatioPercent %d' % originalratio)

Add these lines:

import dc_teamtickets