PDA

View Full Version : Server Autobalance



Adrian
10th July 2005, 22:53
Found this in a forum its designed to only autobalance unassigned players and leave squads and commanders alone, which sounds good to me. Hopefully someone can i have a look over it to see if it will work and also whether we will be able to use it knowing that we have little access to the config because the server is ranked

# team autobalance system

import bf2
import host
from bf2 import g_debug



def init():
if g_debug: print 'initializing autobalance script'

host.registerHandler('PlayerConnect', onPlayerConnect)
host.registerHandler('PlayerDeath', onPlayerDeath)
host.registerHandler('PlayerChangeTeams', onPlayerChangeTeams)



def onPlayerConnect(p):

# place player on the team with least players
team1 = 0
team2 = 0
for tp in bf2.playerManager.getPlayers():
if tp != p:
if tp.getTeam() == 1: team1 += 1
else: team2 += 1

team2 = team2 * bf2.serverSettings.getTeamRatioPercent() / 100.0
if team2 > team1:
p.setTeam(1)
else:
p.setTeam(2)



def onPlayerDeath(p, vehicle):
if not bf2.serverSettings.getAutoBalanceTeam(): return None

# dont use autobalance when its suicide/changes team
if p.getSuicide():
p.setSuicide(0)
return None

aiPlayerBalance = 0
team1 = 0
team2 = 0
for tp in bf2.playerManager.getPlayers():
if tp.getTeam() == 1:
team1 += 1
else:
team2 += 1
if tp.isAIPlayer():
aiPlayerBalance += 1
else:
aiPlayerBalance -= 1

if host.sgl_getIsAIGame():
if not (aiPlayerBalance < 0):
if not p.isAIPlayer():
return None
else:
if p.isCommander(): return None
if p.isSquadLeader(): return None
if p.getSquadId() > 0: return None

team2 = team2 * bf2.serverSettings.getTeamRatioPercent() / 100.0

if (p.getTeam() == 1):
if (team2+1) < team1:
p.setTeam(2)
elif (p.getTeam() == 2):
if (team1+1) < team2:
p.setTeam(1)



def onPlayerChangeTeams(p, humanHasSpawned):
if not bf2.serverSettings.getAutoBalanceTeam(): return None
if host.sgl_getIsAIGame():
if humanHasSpawned: return None
if p.isAIPlayer(): return None

# handling aiplayer team change autobalance when round not started
team = p.getTeam()
aiplayer = 0

for tp in bf2.playerManager.getPlayers():
if aiplayer == 0 and tp.getTeam() == team and tp.isAIPlayer():
aiplayer = tp
break

if aiplayer:
if p.getTeam() == 1: aiplayer.setTeam(2)
else: aiplayer.setTeam(1)

else:
# checking to see if player is allowed to change teams
team1 = 0
team2 = 0
for tp in bf2.playerManager.getPlayers():
if tp.getTeam() == 1: team1 += 1
else: team2 += 1
if abs(team1 - team2) > 1:
if p.getTeam() == 1: p.setTeam(2)
else: p.setTeam(1)

Zyraki
11th July 2005, 08:57
In setting this, will it cause a problem with PB?

If not, I think it is certainly something worth trying.

Adrian
11th July 2005, 11:04
Don't think it would cause a prob with PB because it is server side but i'm not sure if we are able to mod the server in this way

Velox
11th July 2005, 19:32
Course it would be nice if EA just built in a setting to do it for you *coughwankkerscough*