+void AuditLists()
+{
+ entity e;
+ entity prev;
+
+ prev = players;
+ for(e = prev.sort_next; e; prev = e, e = e.sort_next)
+ {
+ if(prev != e.sort_prev)
+ error(strcat("sort list chain error\nplease submit the output of 'prvm_edicts client' to the developers"));
+ }
+
+ prev = teams;
+ for(e = prev.sort_next; e; prev = e, e = e.sort_next)
+ {
+ if(prev != e.sort_prev)
+ error(strcat("sort list chain error\nplease submit the output of 'prvm_edicts client' to the developers"));
+ }
+}
+
+float RegisterPlayer(entity player)
+{
+ entity pl;
+ AuditLists();
+ for(pl = players.sort_next; pl; pl = pl.sort_next)
+ if(pl == player)
+ error("Player already registered!");
+ player.sort_next = players.sort_next;
+ player.sort_prev = players;
+ if(players.sort_next)
+ players.sort_next.sort_prev = player;
+ players.sort_next = player;
+ AuditLists();
+ return true;
+}
+
+void RemovePlayer(entity player)
+{
+ entity pl, parent;
+ AuditLists();
+ parent = players;
+ for(pl = players.sort_next; pl && pl != player; pl = pl.sort_next)
+ parent = pl;
+
+ if(!pl)
+ {
+ error("Trying to remove a player which is not in the playerlist!");
+ return;
+ }
+ parent.sort_next = player.sort_next;
+ if(player.sort_next)
+ player.sort_next.sort_prev = parent;
+ AuditLists();
+}
+
+void MoveToLast(entity e)
+{
+ AuditLists();
+ entity ent = e.sort_next;
+ while(ent)
+ {
+ SORT_SWAP(ent, e);
+ ent = e.sort_next;
+ }
+ AuditLists();
+}
+
+float RegisterTeam(entity Team)
+{
+ assert_once(Team.team, eprint(Team));
+ entity tm;
+ AuditLists();
+ for(tm = teams.sort_next; tm; tm = tm.sort_next)
+ if(tm == Team)
+ error("Team already registered!");
+ Team.sort_next = teams.sort_next;
+ Team.sort_prev = teams;
+ if(teams.sort_next)
+ teams.sort_next.sort_prev = Team;
+ teams.sort_next = Team;
+ if(Team.team && Team.team != NUM_SPECTATOR)
+ ++team_count;
+ AuditLists();
+ return true;
+}
+
+void RemoveTeam(entity Team)
+{
+ entity tm, parent;
+ AuditLists();
+ parent = teams;
+ for(tm = teams.sort_next; tm && tm != Team; tm = tm.sort_next)
+ parent = tm;
+
+ if(!tm)
+ {
+ LOG_INFO(_("Trying to remove a team which is not in the teamlist!"));
+ return;
+ }
+ parent.sort_next = Team.sort_next;
+ if(Team.sort_next)
+ Team.sort_next.sort_prev = parent;
+ if(Team.team && Team.team != NUM_SPECTATOR)
+ --team_count;
+ AuditLists();
+}
+
+entity GetTeam(int Team, bool add)
+{
+ TC(int, Team); TC(bool, add);
+ int num = (Team == NUM_SPECTATOR) ? 16 : Team;
+ if(teamslots[num])
+ return teamslots[num];
+ if (!add)
+ return NULL;
+ entity tm = new_pure(team);
+ tm.team = Team;
+ teamslots[num] = tm;
+ RegisterTeam(tm);
+ return tm;
+}
+