ID:2100143
 
(See the best response by Lummox JR.)
I am trying to attempt to make a system where once a match starts people are separated into two teams. Sorta like how some Team Tournaments are created, but instead of randomly placing people it goes one by one placing each player into a team(EX: Player 1 Blue Team, Player 2 Red Team, Player 3 Blue Team,etc). I am known for having an abundance of unnecessary variables and did not want to over cloud my mind with them.
You could just add players to a team list as they join the event.

// where somelist and anotherlist are team lists...
if(somelist.len <= anotherlist.len)
// add to somelist
else
// add to anotherlist


This simply compares the size of one team to the other and adds the player to the smaller of the two. When both teams have an equal amount, the first list is used by default.
Best response
Dayvon64 wrote:
Sorta like how some Team Tournaments are created, but instead of randomly placing people it goes one by one placing each player into a team(EX: Player 1 Blue Team, Player 2 Red Team, Player 3 Blue Team,etc).

That's still random, or to be more accurate it's arbitrary. A random distribution is good; I think what you mean is you don't want the teams to end up lopsided by simply flipping a coin to see who goes on which team.

When you're distributing players to teams, generally what you want to do at each step is 1) pick a random player from the list of unassigned players, and 2) assign them to the team with the fewest people.

There are other ways to go about this of course; if players have a certain level of power/skill that you can roughly quantify, you may want to try to keep the total balanced between the two teams. Lode Wars had teams of different colors with different abilities, where a team with powerful equipment might only get 1/2 as many people as a generic team. Weighting team counts like Lode Wars did is actually pretty easy.

With teams you also need to consider what happens when someone logs in or out. You should keep track of a player's team once the game/round starts, so that if they log out and back in they're on the same team. New players should be assigned to the team that needs them most, or a random choice if more than one.

Most likely, you don't want to have vars like team1 and team2. Rather, it's best to keep your teams in a list. Consider this:

team
var/name
var/list/players // list of players
var/list/inactive // list of keys

New(name)
teams[name] = src // add to global teams list
src.name = name
players = new
inactive = new

proc/Add(mob/M) // M must be a mob
M.team = name
players[M] = null
inactive -= M.key
proc/Remove(mob/M) // M can be mob or key
if(ismob(M))
M.team = null
players -= M
inactive -= M.key
else inactive -= M
proc/Bench(mob/M) // M is a mob, and is logging out
if(players[M])
players -= M
inactive[M] = null

// global teams; this is an associative list, so teams["red"]
// should lookup the team with the name "red"
var/list/teams

proc/MakeTeams()
teams = new
new/team("red")
new/team("blue")
var/list/ready = new
for(var/client/C)
if(C.mob && C.mob.ready) // see who's ready to play
ready += C.mob
while(ready.len)
// grab a random player
ready.Swap(ready.len, rand(1, ready.len))
var/mob/M = ready[ready.len]
--ready.len
var/team/best = null
// random selection of teams
for(var/i=teams.len; i>0; --i)
teams.Swap(i, rand(1,i)) // move any considered teams to the end
var/team/T = teams[teams[i]]
if(!best || T.players.len < best.players.len) best = T
if(best) best.Add(M)

Handling login and logout shouldn't be too difficult from there.

To lookup the actual /team datum a player has, just check teams[player.team]; if it's null they don't have a team. To see if two players are on the same team, just compare their team vars.

You can also setup items in the game world that are team-specific, by giving them a team var also. In SotS II, I went ahead and gave the team var to every atom, because some turfs need it, many objs do, and of course all player mobs (when in team mode, anyway).