Jump to content

Balance Teams Lua.


Meva

Recommended Posts

Hi, everyone!

 

I have a question about balancing teams with lua. I have this option on server, but can i excluded bots with this balance? Exactly, when on the server play people(2 on axis and 5 on allies) and bots, balance doesn't work, because all players on the server is 12 (6 on allies and 6 on axis with bots). 

 

This is my lua code:

 

modname = "balance"

version = "0.1"
 
function et_InitGame(levelTime,randomSeed,restart)
et.RegisterModname(modname .. " " .. version)
end
 
unevenDiff = 2
max_unevenTime = 45
max_unevenDiff = 4
 
axisPlayers = {}
alliedPlayers = {}
unevenTime = 15
 
function et_RunFrame( levelTime )
   local numAlliedPlayers = table.getn( alliedPlayers )
   local numAxisPlayers = table.getn( axisPlayers )
   if numAlliedPlayers >= numAxisPlayers + max_unevenDiff then
      local clientNum = alliedPlayers[ numAlliedPlayers ]
      et.trap_SendConsoleCommand( et.EXEC_APPEND, "putteam " .. clientNum .. " r" )
      et.G_globalSound("lua/playermove.wav")
 et.trap_SendServerCommand(-1, "chat \"balancing teams... " .. et.gentity_get( clientNum, "pers.netname" ) .. "^7 moved to ^1AXIS\"" ) 
   elseif numAxisPlayers >= numAlliedPlayers + max_unevenDiff then
      local clientNum = axisPlayers[ numAxisPlayers ]
      et.trap_SendConsoleCommand( et.EXEC_APPEND, "putteam " .. clientNum .. " b" )
      et.G_globalSound("lua/playermove.wav")
 et.trap_SendServerCommand(-1, "chat \"balancing teams... " .. et.gentity_get( clientNum, "pers.netname" ) .. "^7 moved to ^4ALLIES\"" ) 
   elseif numAlliedPlayers >= numAxisPlayers + unevenDiff then
      if unevenTime > 0 then
         if tonumber( levelTime ) - unevenTime >= max_unevenTime * 1000 then
            local clientNum = alliedPlayers[ numAlliedPlayers ]
            et.trap_SendConsoleCommand( et.EXEC_APPEND, "putteam " .. clientNum .. " r" )
            et.G_globalSound("lua/playermove.wav")
et.trap_SendServerCommand(-1, "chat \"balancing teams... " .. et.gentity_get( clientNum, "pers.netname" ) .. "^7 moved to ^1AXIS\"" ) 
         end
      else
         unevenTime = tonumber( levelTime )
      end
   elseif numAxisPlayers >= numAlliedPlayers + unevenDiff then
      if unevenTime > 0 then
         if tonumber( levelTime ) - unevenTime >= max_unevenTime * 1000 then
            local clientNum = axisPlayers[ numAxisPlayers ]
            et.trap_SendConsoleCommand( et.EXEC_APPEND, "putteam " .. clientNum .. " b" )
            et.G_globalSound("lua/playermove.wav")
et.trap_SendServerCommand(-1, "chat \"balancing teams... " .. et.gentity_get( clientNum, "pers.netname" ) .. "^7 moved to ^4ALLIES\"" ) 
         end
      else
         unevenTime = tonumber( levelTime )
      end
   else
      unevenTime = -1
   end
end
 
function et_ClientSpawn( clientNum, revived, teamChange, restoreHealth )
   if teamChange ~= 0 then
      local team = tonumber( et.gentity_get( clientNum, "sess.sessionTeam" ) )
      -- these were the teamnumbers prior to the move
      local numAlliedPlayers = table.getn( alliedPlayers )
      local numAxisPlayers = table.getn( axisPlayers )
      if team == 1 then
         for i, num in ipairs( alliedPlayers ) do
            if num == clientNum then
               table.remove( alliedPlayers, i )
               break
            end
         end
         -- this should not happen but still check for it to avoid doubles
         for i, num in ipairs( axisPlayers ) do
            if num == clientNum then
               return
            end
         end
         -- make sure a player who (got) moved when teams were uneven doesn't get moved right back
         if numAlliedPlayers >= numAxisPlayers + unevenDiff then
            table.insert( axisPlayers, 1, clientNum )
         else
            table.insert( axisPlayers, clientNum )
         end
      elseif team == 2 then
         for i, num in ipairs( axisPlayers ) do
            if num == clientNum then
               table.remove( axisPlayers, i )
               break
            end
         end
         for i, num in ipairs( alliedPlayers ) do
            if num == clientNum then
               return
            end
         end
         if numAxisPlayers >= numAlliedPlayers + unevenDiff then
            table.insert( alliedPlayers, 1, clientNum )
         else
            table.insert( alliedPlayers, clientNum )
         end
      else
         for i, num in ipairs( alliedPlayers ) do
            if num == clientNum then
               table.remove( alliedPlayers, i )
               return
            end
         end
         for i, num in ipairs( axisPlayers ) do
            if num == clientNum then
               table.remove( axisPlayers, i )
               return
            end
         end
      end
   end
end
 
function et_ClientDisconnect( clientNum )
   for i, num in ipairs( alliedPlayers ) do
      if num == clientNum then
         table.remove( alliedPlayers, i )
         return
      end
   end
   for i, num in ipairs( axisPlayers ) do
      if num == clientNum then
         table.remove( axisPlayers, i )
         return
      end
   end
end

 

 

 

Link to comment
Share on other sites

I have a code, it's a modification of omnibot "balanceteams" function, that makes always equal number of bots, so if you have 2 axis humans and 4 allies humans, and maxbots 12, teams would not be "6vs6", they would be 5vs7, so new players are forced to join the teams with less bots.

 

 

If you don't want to touch omnibot code (they recommend/ encourage not, but i didn't know another way to do it), then you just have to verify if it's bot, you can do it by making a function that check if first 7 characters of GUID is "OMNIBOT", and do nothing. If it's not "OMNIBOT", then add the clientnum to table. Also, check ping. If ping = 0, then exclude the bot. (I don't know how it works with people that really have 0 ping).

 

An example (untested) would be this:

modname = "balance"
version = "0.1"
 
function et_InitGame(levelTime,randomSeed,restart)
et.RegisterModname(modname .. " " .. version)
end
 
unevenDiff = 2
max_unevenTime = 45
max_unevenDiff = 4
 
axisPlayers = {}
alliedPlayers = {}
unevenTime = 15
function isBot(clientNum)
   if et.gentity_get(clientNum,"ps.ping") == 0 then
       return true -- is Bot
   end
end

function et_RunFrame( levelTime )
   local numAlliedPlayers = table.getn( alliedPlayers )
   local numAxisPlayers = table.getn( axisPlayers )
   if numAlliedPlayers >= numAxisPlayers + max_unevenDiff then
      local clientNum = alliedPlayers[ numAlliedPlayers ]
      et.trap_SendConsoleCommand( et.EXEC_APPEND, "putteam " .. clientNum .. " r" )
      et.G_globalSound("lua/playermove.wav")
 et.trap_SendServerCommand(-1, "chat \"balancing teams... " .. et.gentity_get( clientNum, "pers.netname" ) .. "^7 moved to ^1AXIS\"" ) 
   elseif numAxisPlayers >= numAlliedPlayers + max_unevenDiff then
      local clientNum = axisPlayers[ numAxisPlayers ]
      et.trap_SendConsoleCommand( et.EXEC_APPEND, "putteam " .. clientNum .. " b" )
      et.G_globalSound("lua/playermove.wav")
 et.trap_SendServerCommand(-1, "chat \"balancing teams... " .. et.gentity_get( clientNum, "pers.netname" ) .. "^7 moved to ^4ALLIES\"" ) 
   elseif numAlliedPlayers >= numAxisPlayers + unevenDiff then
      if unevenTime > 0 then
         if tonumber( levelTime ) - unevenTime >= max_unevenTime * 1000 then
            local clientNum = alliedPlayers[ numAlliedPlayers ]
            et.trap_SendConsoleCommand( et.EXEC_APPEND, "putteam " .. clientNum .. " r" )
            et.G_globalSound("lua/playermove.wav")
et.trap_SendServerCommand(-1, "chat \"balancing teams... " .. et.gentity_get( clientNum, "pers.netname" ) .. "^7 moved to ^1AXIS\"" ) 
         end
      else
         unevenTime = tonumber( levelTime )
      end
   elseif numAxisPlayers >= numAlliedPlayers + unevenDiff then
      if unevenTime > 0 then
         if tonumber( levelTime ) - unevenTime >= max_unevenTime * 1000 then
            local clientNum = axisPlayers[ numAxisPlayers ]
            et.trap_SendConsoleCommand( et.EXEC_APPEND, "putteam " .. clientNum .. " b" )
            et.G_globalSound("lua/playermove.wav")
et.trap_SendServerCommand(-1, "chat \"balancing teams... " .. et.gentity_get( clientNum, "pers.netname" ) .. "^7 moved to ^4ALLIES\"" ) 
         end
      else
         unevenTime = tonumber( levelTime )
      end
   else
      unevenTime = -1
   end
end
 
function et_ClientSpawn( clientNum, revived, teamChange, restoreHealth )
   if teamChange ~= 0 then
      local team = tonumber( et.gentity_get( clientNum, "sess.sessionTeam" ) )
      -- these were the teamnumbers prior to the move
      local numAlliedPlayers = table.getn( alliedPlayers )
      local numAxisPlayers = table.getn( axisPlayers )
      if team == 1 then
         for i, num in ipairs( alliedPlayers ) do
            if num == clientNum then
               table.remove( alliedPlayers, i )
               break
            end
         end
         -- this should not happen but still check for it to avoid doubles
         for i, num in ipairs( axisPlayers ) do
            if num == clientNum then
               return
            end
         end
         -- make sure a player who (got) moved when teams were uneven doesn't get moved right back
         if numAlliedPlayers >= numAxisPlayers + unevenDiff then
            table.insert( axisPlayers, 1, clientNum )
         else
		 	if isBot(clientNum) then 
				--do nothing
			else
				table.insert( axisPlayers, clientNum )
			end
         end
      elseif team == 2 then
         for i, num in ipairs( axisPlayers ) do
            if num == clientNum then
               table.remove( axisPlayers, i )
               break
            end
         end
         for i, num in ipairs( alliedPlayers ) do
            if num == clientNum then
               return
            end
         end
         if numAxisPlayers >= numAlliedPlayers + unevenDiff then
            table.insert( alliedPlayers, 1, clientNum )
         else
			if isBot(clientNum) then 
				--do nothing
			else
				table.insert( alliedPlayers, clientNum )
			end
         end
      else
         for i, num in ipairs( alliedPlayers ) do
            if num == clientNum then
               table.remove( alliedPlayers, i )
               return
            end
         end
         for i, num in ipairs( axisPlayers ) do
            if num == clientNum then
               table.remove( axisPlayers, i )
               return
            end
         end
      end
   end
end
 
function et_ClientDisconnect( clientNum )
   for i, num in ipairs( alliedPlayers ) do
      if num == clientNum then
         table.remove( alliedPlayers, i )
         return
      end
   end
   for i, num in ipairs( axisPlayers ) do
      if num == clientNum then
         table.remove( axisPlayers, i )
         return
      end
   end
end

I did it with the other method (omnibot), because it was better for me to have bots auto balancing themselves, and forcing new players to join weaker team. Also it's more visual the team balance this way :P

 

Also, you can check this: http://mygamingtalk.com/forums/topic/4626-lua-enhanced-server-module/It has a great function to balance teams :)

Edited by lolroy
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...