]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/teamplay.qc
Merge branch 'master' into TimePath/scoreboard_elo
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / teamplay.qc
1 #include "teamplay.qh"
2
3 #include "cl_client.qh"
4 #include "race.qh"
5 #include "scores.qh"
6 #include "scores_rules.qh"
7
8 #include "bot/bot.qh"
9
10 #include "command/vote.qh"
11
12 #include "mutators/all.qh"
13
14 #include "../common/deathtypes/all.qh"
15 #include "../common/gamemodes/all.qh"
16 #include "../common/teams.qh"
17
18 void TeamchangeFrags(entity e)
19 {
20         PlayerScore_Clear(e);
21 }
22
23 void LogTeamchange(float player_id, float team_number, float type)
24 {
25         if(!autocvar_sv_eventlog)
26                 return;
27
28         if(player_id < 1)
29                 return;
30
31         GameLogEcho(strcat(":team:", ftos(player_id), ":", ftos(team_number), ":", ftos(type)));
32 }
33
34 void default_delayedinit(entity this)
35 {
36         if(!scores_initialized)
37                 ScoreRules_generic();
38 }
39
40 void ActivateTeamplay()
41 {
42         serverflags |= SERVERFLAG_TEAMPLAY;
43         teamplay = 1;
44         cvar_set("teamplay", "2");  // DP needs this for sending proper getstatus replies.
45 }
46
47 void InitGameplayMode()
48 {
49         VoteReset();
50
51         // find out good world mins/maxs bounds, either the static bounds found by looking for solid, or the mapinfo specified bounds
52         get_mi_min_max(1);
53         world.mins = mi_min;
54         world.maxs = mi_max;
55
56         MapInfo_LoadMapSettings(mapname);
57         serverflags &= ~SERVERFLAG_TEAMPLAY;
58         teamplay = 0;
59         cvar_set("teamplay", "0");  // DP needs this for sending proper getstatus replies.
60
61         if (!cvar_value_issafe(world.fog))
62         {
63                 LOG_INFO("The current map contains a potentially harmful fog setting, ignored\n");
64                 world.fog = string_null;
65         }
66         if(MapInfo_Map_fog != "")
67                 if(MapInfo_Map_fog == "none")
68                         world.fog = string_null;
69                 else
70                         world.fog = strzone(MapInfo_Map_fog);
71         clientstuff = strzone(MapInfo_Map_clientstuff);
72
73         MapInfo_ClearTemps();
74
75         gamemode_name = MapInfo_Type_ToText(MapInfo_LoadedGametype);
76
77         cache_mutatormsg = strzone("");
78         cache_lastmutatormsg = strzone("");
79
80         InitializeEntity(NULL, default_delayedinit, INITPRIO_GAMETYPE_FALLBACK);
81 }
82
83 string GetClientVersionMessage(entity this)
84 {
85         string versionmsg;
86         if (this.version_mismatch) {
87                 if(this.version < autocvar_gameversion) {
88                         versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
89                 } else {
90                         versionmsg = "^3This server is using an outdated Xonotic version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
91                 }
92         } else {
93                 versionmsg = "^2client version and server version are compatible.^8";
94         }
95         return versionmsg;
96 }
97
98 string getwelcomemessage(entity this)
99 {
100         string s, modifications, motd;
101
102         MUTATOR_CALLHOOK(BuildMutatorsPrettyString, "");
103         modifications = M_ARGV(0, string);
104
105         if(g_weaponarena)
106         {
107                 if(g_weaponarena_random)
108                         modifications = strcat(modifications, ", ", ftos(g_weaponarena_random), " of ", g_weaponarena_list, " Arena");
109                 else
110                         modifications = strcat(modifications, ", ", g_weaponarena_list, " Arena");
111         }
112         else if(cvar("g_balance_blaster_weaponstart") == 0)
113                 modifications = strcat(modifications, ", No start weapons");
114         if(cvar("sv_gravity") < stof(cvar_defstring("sv_gravity")))
115                 modifications = strcat(modifications, ", Low gravity");
116         if(g_weapon_stay && !g_cts)
117                 modifications = strcat(modifications, ", Weapons stay");
118         if(g_jetpack)
119                 modifications = strcat(modifications, ", Jet pack");
120         if(autocvar_g_powerups == 0)
121                 modifications = strcat(modifications, ", No powerups");
122         if(autocvar_g_powerups > 0)
123                 modifications = strcat(modifications, ", Powerups");
124         modifications = substring(modifications, 2, strlen(modifications) - 2);
125
126         string versionmessage = GetClientVersionMessage(this);
127
128         s = strcat("This is Xonotic ", autocvar_g_xonoticversion, "\n", versionmessage);
129         s = strcat(s, "^8\n\nmatch type is ^1", gamemode_name, "^8\n");
130
131         if(modifications != "")
132                 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
133
134         if(cache_lastmutatormsg != autocvar_g_mutatormsg)
135         {
136                 if(cache_lastmutatormsg)
137                         strunzone(cache_lastmutatormsg);
138                 if(cache_mutatormsg)
139                         strunzone(cache_mutatormsg);
140                 cache_lastmutatormsg = strzone(autocvar_g_mutatormsg);
141                 cache_mutatormsg = strzone(cache_lastmutatormsg);
142         }
143
144         if (cache_mutatormsg != "") {
145                 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
146         }
147
148         string mutator_msg = "";
149         MUTATOR_CALLHOOK(BuildGameplayTipsString, mutator_msg);
150         mutator_msg = M_ARGV(0, string);
151
152         s = strcat(s, mutator_msg); // trust that the mutator will do proper formatting
153
154         motd = autocvar_sv_motd;
155         if (motd != "") {
156                 s = strcat(s, "\n\n^8MOTD: ^7", strreplace("\\n", "\n", motd));
157         }
158         return s;
159 }
160
161 void SetPlayerColors(entity pl, float _color)
162 {
163         /*string s;
164         s = ftos(cl);
165         stuffcmd(pl, strcat("color ", s, " ", s, "\n")  );
166         pl.team = cl + 1;
167         //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
168         pl.clientcolors = 16*cl + cl;*/
169
170         float pants, shirt;
171         pants = _color & 0x0F;
172         shirt = _color & 0xF0;
173
174
175         if(teamplay) {
176                 setcolor(pl, 16*pants + pants);
177         } else {
178                 setcolor(pl, shirt + pants);
179         }
180 }
181
182 void SetPlayerTeam(entity pl, float t, float s, float noprint)
183 {
184         float _color;
185
186         if(t == 4)
187                 _color = NUM_TEAM_4 - 1;
188         else if(t == 3)
189                 _color = NUM_TEAM_3 - 1;
190         else if(t == 2)
191                 _color = NUM_TEAM_2 - 1;
192         else
193                 _color = NUM_TEAM_1 - 1;
194
195         SetPlayerColors(pl,_color);
196
197         if(t != s) {
198                 LogTeamchange(pl.playerid, pl.team, 3);  // log manual team join
199
200                 if(!noprint)
201                 bprint(pl.netname, "^7 has changed from ", Team_NumberToColoredFullName(s), "^7 to ", Team_NumberToColoredFullName(t), "\n");
202         }
203
204 }
205
206 // set c1...c4 to show what teams are allowed
207 void CheckAllowedTeams (entity for_whom)
208 {
209         int dm = 0;
210
211         c1 = c2 = c3 = c4 = -1;
212         cb1 = cb2 = cb3 = cb4 = 0;
213
214         string teament_name = string_null;
215
216         bool mutator_returnvalue = MUTATOR_CALLHOOK(GetTeamCount, dm, teament_name);
217         dm = M_ARGV(0, float);
218         teament_name = M_ARGV(1, string);
219
220         if(!mutator_returnvalue)
221         {
222                 if(dm >= 4)
223                         c1 = c2 = c3 = c4 = 0;
224                 else if(dm >= 3)
225                         c1 = c2 = c3 = 0;
226                 else
227                         c1 = c2 = 0;
228         }
229
230         // find out what teams are allowed if necessary
231         if(teament_name)
232         {
233                 entity head = find(NULL, classname, teament_name);
234                 while(head)
235                 {
236                         switch(head.team)
237                         {
238                                 case NUM_TEAM_1: c1 = 0; break;
239                                 case NUM_TEAM_2: c2 = 0; break;
240                                 case NUM_TEAM_3: c3 = 0; break;
241                                 case NUM_TEAM_4: c4 = 0; break;
242                         }
243
244                         head = find(head, classname, teament_name);
245                 }
246         }
247
248         // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
249         if(c3==-1 && c4==-1)
250         if(autocvar_bot_vs_human && for_whom)
251         {
252                 if(autocvar_bot_vs_human > 0)
253                 {
254                         // bots are all blue
255                         if(IS_BOT_CLIENT(for_whom))
256                                 c1 = c3 = c4 = -1;
257                         else
258                                 c2 = -1;
259                 }
260                 else
261                 {
262                         // bots are all red
263                         if(IS_BOT_CLIENT(for_whom))
264                                 c2 = c3 = c4 = -1;
265                         else
266                                 c1 = -1;
267                 }
268         }
269
270         if(!for_whom)
271                 return;
272
273         // if player has a forced team, ONLY allow that one
274         if(for_whom.team_forced == NUM_TEAM_1 && c1 >= 0)
275                 c2 = c3 = c4 = -1;
276         else if(for_whom.team_forced == NUM_TEAM_2 && c2 >= 0)
277                 c1 = c3 = c4 = -1;
278         else if(for_whom.team_forced == NUM_TEAM_3 && c3 >= 0)
279                 c1 = c2 = c4 = -1;
280         else if(for_whom.team_forced == NUM_TEAM_4 && c4 >= 0)
281                 c1 = c2 = c3 = -1;
282 }
283
284 float PlayerValue(entity p)
285 {
286         return 1;
287         // FIXME: it always returns 1...
288 }
289
290 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
291 // teams that are allowed will now have their player counts stored in c1...c4
292 void GetTeamCounts(entity ignore)
293 {
294         float value, bvalue;
295         // now count how many players are on each team already
296
297         // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
298         // also remember the lowest-scoring player
299
300         FOREACH_CLIENT(true, LAMBDA(
301                 float t;
302                 if(IS_PLAYER(it) || it.caplayer)
303                         t = it.team;
304                 else if(it.team_forced > 0)
305                         t = it.team_forced; // reserve the spot
306                 else
307                         continue;
308                 if(it != ignore)// && it.netname != "")
309                 {
310                         value = PlayerValue(it);
311                         if(IS_BOT_CLIENT(it))
312                                 bvalue = value;
313                         else
314                                 bvalue = 0;
315                         if(t == NUM_TEAM_1)
316                         {
317                                 if(c1 >= 0)
318                                 {
319                                         c1 = c1 + value;
320                                         cb1 = cb1 + bvalue;
321                                 }
322                         }
323                         if(t == NUM_TEAM_2)
324                         {
325                                 if(c2 >= 0)
326                                 {
327                                         c2 = c2 + value;
328                                         cb2 = cb2 + bvalue;
329                                 }
330                         }
331                         if(t == NUM_TEAM_3)
332                         {
333                                 if(c3 >= 0)
334                                 {
335                                         c3 = c3 + value;
336                                         cb3 = cb3 + bvalue;
337                                 }
338                         }
339                         if(t == NUM_TEAM_4)
340                         {
341                                 if(c4 >= 0)
342                                 {
343                                         c4 = c4 + value;
344                                         cb4 = cb4 + bvalue;
345                                 }
346                         }
347                 }
348         ));
349
350         // if the player who has a forced team has not joined yet, reserve the spot
351         if(autocvar_g_campaign)
352         {
353                 switch(autocvar_g_campaign_forceteam)
354                 {
355                         case 1: if(c1 == cb1) ++c1; break;
356                         case 2: if(c2 == cb2) ++c2; break;
357                         case 3: if(c3 == cb3) ++c3; break;
358                         case 4: if(c4 == cb4) ++c4; break;
359                 }
360         }
361 }
362
363 float TeamSmallerEqThanTeam(float ta, float tb, entity e)
364 {
365         // we assume that CheckAllowedTeams and GetTeamCounts have already been called
366         float f;
367         float ca = -1, cb = -1, cba = 0, cbb = 0, sa = 0, sb = 0;
368
369         switch(ta)
370         {
371                 case 1: ca = c1; cba = cb1; sa = team1_score; break;
372                 case 2: ca = c2; cba = cb2; sa = team2_score; break;
373                 case 3: ca = c3; cba = cb3; sa = team3_score; break;
374                 case 4: ca = c4; cba = cb4; sa = team4_score; break;
375         }
376         switch(tb)
377         {
378                 case 1: cb = c1; cbb = cb1; sb = team1_score; break;
379                 case 2: cb = c2; cbb = cb2; sb = team2_score; break;
380                 case 3: cb = c3; cbb = cb3; sb = team3_score; break;
381                 case 4: cb = c4; cbb = cb4; sb = team4_score; break;
382         }
383
384         // invalid
385         if(ca < 0 || cb < 0)
386                 return false;
387
388         // equal
389         if(ta == tb)
390                 return true;
391
392         if(IS_REAL_CLIENT(e))
393         {
394                 if(bots_would_leave)
395                 {
396                         ca -= cba * 0.999;
397                         cb -= cbb * 0.999;
398                 }
399         }
400
401         // keep teams alive (teams of size 0 always count as smaller, ignoring score)
402         if(ca < 1)
403                 if(cb >= 1)
404                         return true;
405         if(ca >= 1)
406                 if(cb < 1)
407                         return false;
408
409         // first, normalize
410         f = max(ca, cb, 1);
411         ca /= f;
412         cb /= f;
413         f = max(sa, sb, 1);
414         sa /= f;
415         sb /= f;
416
417         // the more we're at the end of the match, the more take scores into account
418         f = bound(0, game_completion_ratio * autocvar_g_balance_teams_scorefactor, 1);
419         ca += (sa - ca) * f;
420         cb += (sb - cb) * f;
421
422         return ca <= cb;
423 }
424
425 // returns # of smallest team (1, 2, 3, 4)
426 // NOTE: Assumes CheckAllowedTeams has already been called!
427 float FindSmallestTeam(entity pl, float ignore_pl)
428 {
429         float totalteams, t;
430         totalteams = 0;
431
432         // find out what teams are available
433         //CheckAllowedTeams();
434
435         // make sure there are at least 2 teams to join
436         if(c1 >= 0)
437                 totalteams = totalteams + 1;
438         if(c2 >= 0)
439                 totalteams = totalteams + 1;
440         if(c3 >= 0)
441                 totalteams = totalteams + 1;
442         if(c4 >= 0)
443                 totalteams = totalteams + 1;
444
445         if((autocvar_bot_vs_human || pl.team_forced > 0) && totalteams == 1)
446                 totalteams += 1;
447
448         if(totalteams <= 1)
449         {
450                 if(autocvar_g_campaign && pl && IS_REAL_CLIENT(pl))
451                         return 1; // special case for campaign and player joining
452                 else
453                         error(sprintf("Too few teams available for %s\n", MapInfo_Type_ToString(MapInfo_CurrentGametype())));
454         }
455
456         // count how many players are in each team
457         if(ignore_pl)
458                 GetTeamCounts(pl);
459         else
460                 GetTeamCounts(NULL);
461
462         RandomSelection_Init();
463
464         t = 1;
465         if(TeamSmallerEqThanTeam(2, t, pl))
466                 t = 2;
467         if(TeamSmallerEqThanTeam(3, t, pl))
468                 t = 3;
469         if(TeamSmallerEqThanTeam(4, t, pl))
470                 t = 4;
471
472         // now t is the minimum, or A minimum!
473         if(t == 1 || TeamSmallerEqThanTeam(1, t, pl))
474                 RandomSelection_Add(NULL, 1, string_null, 1, 1);
475         if(t == 2 || TeamSmallerEqThanTeam(2, t, pl))
476                 RandomSelection_Add(NULL, 2, string_null, 1, 1);
477         if(t == 3 || TeamSmallerEqThanTeam(3, t, pl))
478                 RandomSelection_Add(NULL, 3, string_null, 1, 1);
479         if(t == 4 || TeamSmallerEqThanTeam(4, t, pl))
480                 RandomSelection_Add(NULL, 4, string_null, 1, 1);
481
482         return RandomSelection_chosen_float;
483 }
484
485 int JoinBestTeam(entity this, bool only_return_best, bool forcebestteam)
486 {
487         float smallest, selectedteam;
488
489         // don't join a team if we're not playing a team game
490         if(!teamplay)
491                 return 0;
492
493         // find out what teams are available
494         CheckAllowedTeams(this);
495
496         // if we don't care what team he ends up on, put him on whatever team he entered as.
497         // if he's not on a valid team, then let other code put him on the smallest team
498         if(!forcebestteam)
499         {
500                 if(     c1 >= 0 && this.team == NUM_TEAM_1)
501                         selectedteam = this.team;
502                 else if(c2 >= 0 && this.team == NUM_TEAM_2)
503                         selectedteam = this.team;
504                 else if(c3 >= 0 && this.team == NUM_TEAM_3)
505                         selectedteam = this.team;
506                 else if(c4 >= 0 && this.team == NUM_TEAM_4)
507                         selectedteam = this.team;
508                 else
509                         selectedteam = -1;
510
511                 if(selectedteam > 0)
512                 {
513                         if(!only_return_best)
514                         {
515                                 SetPlayerColors(this, selectedteam - 1);
516
517                                 // when JoinBestTeam is called by client.qc/ClientKill_Now_TeamChange the players team is -1 and thus skipped
518                                 // when JoinBestTeam is called by cl_client.qc/ClientConnect the player_id is 0 the log attempt is rejected
519                                 LogTeamchange(this.playerid, this.team, 99);
520                         }
521                         return selectedteam;
522                 }
523                 // otherwise end up on the smallest team (handled below)
524         }
525
526         smallest = FindSmallestTeam(this, true);
527
528         if(!only_return_best && !this.bot_forced_team)
529         {
530                 TeamchangeFrags(this);
531                 if(smallest == 1)
532                 {
533                         SetPlayerColors(this, NUM_TEAM_1 - 1);
534                 }
535                 else if(smallest == 2)
536                 {
537                         SetPlayerColors(this, NUM_TEAM_2 - 1);
538                 }
539                 else if(smallest == 3)
540                 {
541                         SetPlayerColors(this, NUM_TEAM_3 - 1);
542                 }
543                 else if(smallest == 4)
544                 {
545                         SetPlayerColors(this, NUM_TEAM_4 - 1);
546                 }
547                 else
548                 {
549                         error("smallest team: invalid team\n");
550                 }
551
552                 LogTeamchange(this.playerid, this.team, 2); // log auto join
553
554                 if(!IS_DEAD(this))
555                         Damage(this, this, this, 100000, DEATH_TEAMCHANGE.m_id, this.origin, '0 0 0');
556         }
557
558         return smallest;
559 }
560
561 //void() ctf_playerchanged;
562 void SV_ChangeTeam(float _color)
563 {ENGINE_EVENT();
564         float scolor, dcolor, steam, dteam; //, dbotcount, scount, dcount;
565
566         // in normal deathmatch we can just apply the color and we're done
567         if(!teamplay)
568                 SetPlayerColors(this, _color);
569
570         if(!IS_CLIENT(this))
571         {
572                 // since this is an engine function, and gamecode doesn't have any calls earlier than this, do the connecting message here
573                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_CONNECTING, this.netname);
574                 return;
575         }
576
577         if(!teamplay)
578                 return;
579
580         scolor = this.clientcolors & 0x0F;
581         dcolor = _color & 0x0F;
582
583         if(scolor == NUM_TEAM_1 - 1)
584                 steam = 1;
585         else if(scolor == NUM_TEAM_2 - 1)
586                 steam = 2;
587         else if(scolor == NUM_TEAM_3 - 1)
588                 steam = 3;
589         else // if(scolor == NUM_TEAM_4 - 1)
590                 steam = 4;
591         if(dcolor == NUM_TEAM_1 - 1)
592                 dteam = 1;
593         else if(dcolor == NUM_TEAM_2 - 1)
594                 dteam = 2;
595         else if(dcolor == NUM_TEAM_3 - 1)
596                 dteam = 3;
597         else // if(dcolor == NUM_TEAM_4 - 1)
598                 dteam = 4;
599
600         CheckAllowedTeams(this);
601
602         if(dteam == 1 && c1 < 0) dteam = 4;
603         if(dteam == 4 && c4 < 0) dteam = 3;
604         if(dteam == 3 && c3 < 0) dteam = 2;
605         if(dteam == 2 && c2 < 0) dteam = 1;
606
607         // not changing teams
608         if(scolor == dcolor)
609         {
610                 //bprint("same team change\n");
611                 SetPlayerTeam(this, dteam, steam, true);
612                 return;
613         }
614
615         if((autocvar_g_campaign) || (autocvar_g_changeteam_banned && this.wasplayer)) {
616                 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_TEAMCHANGE_NOTALLOWED);
617                 return; // changing teams is not allowed
618         }
619
620         // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
621         if(autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
622         {
623                 GetTeamCounts(this);
624                 if(!TeamSmallerEqThanTeam(dteam, steam, this))
625                 {
626                         Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_TEAMCHANGE_LARGERTEAM);
627                         return;
628                 }
629         }
630
631 //      bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
632
633         if(IS_PLAYER(this) && steam != dteam)
634         {
635                 // reduce frags during a team change
636                 TeamchangeFrags(this);
637         }
638
639         MUTATOR_CALLHOOK(Player_ChangeTeam, this, steam, dteam);
640
641         SetPlayerTeam(this, dteam, steam, !IS_CLIENT(this));
642
643         if(IS_PLAYER(this) && steam != dteam)
644         {
645                 // kill player when changing teams
646                 if(!IS_DEAD(this))
647                         Damage(this, this, this, 100000, DEATH_TEAMCHANGE.m_id, this.origin, '0 0 0');
648         }
649 }
650
651 void ShufflePlayerOutOfTeam (float source_team)
652 {
653         float smallestteam, smallestteam_count, steam;
654         float lowest_bot_score, lowest_player_score;
655         entity lowest_bot, lowest_player, selected;
656
657         smallestteam = 0;
658         smallestteam_count = 999999999;
659
660         if(c1 >= 0 && c1 < smallestteam_count)
661         {
662                 smallestteam = 1;
663                 smallestteam_count = c1;
664         }
665         if(c2 >= 0 && c2 < smallestteam_count)
666         {
667                 smallestteam = 2;
668                 smallestteam_count = c2;
669         }
670         if(c3 >= 0 && c3 < smallestteam_count)
671         {
672                 smallestteam = 3;
673                 smallestteam_count = c3;
674         }
675         if(c4 >= 0 && c4 < smallestteam_count)
676         {
677                 smallestteam = 4;
678                 smallestteam_count = c4;
679         }
680
681         if(!smallestteam)
682         {
683                 bprint("warning: no smallest team\n");
684                 return;
685         }
686
687         if(source_team == 1)
688                 steam = NUM_TEAM_1;
689         else if(source_team == 2)
690                 steam = NUM_TEAM_2;
691         else if(source_team == 3)
692                 steam = NUM_TEAM_3;
693         else // if(source_team == 4)
694                 steam = NUM_TEAM_4;
695
696         lowest_bot = NULL;
697         lowest_bot_score = 999999999;
698         lowest_player = NULL;
699         lowest_player_score = 999999999;
700
701         // find the lowest-scoring player & bot of that team
702         FOREACH_CLIENT(IS_PLAYER(it) && it.team == steam, LAMBDA(
703                 if(it.isbot)
704                 {
705                         if(it.totalfrags < lowest_bot_score)
706                         {
707                                 lowest_bot = it;
708                                 lowest_bot_score = it.totalfrags;
709                         }
710                 }
711                 else
712                 {
713                         if(it.totalfrags < lowest_player_score)
714                         {
715                                 lowest_player = it;
716                                 lowest_player_score = it.totalfrags;
717                         }
718                 }
719         ));
720
721         // prefers to move a bot...
722         if(lowest_bot != NULL)
723                 selected = lowest_bot;
724         // but it will move a player if it has to
725         else
726                 selected = lowest_player;
727         // don't do anything if it couldn't find anyone
728         if(!selected)
729         {
730                 bprint("warning: couldn't find a player to move from team\n");
731                 return;
732         }
733
734         // smallest team gains a member
735         if(smallestteam == 1)
736         {
737                 c1 = c1 + 1;
738         }
739         else if(smallestteam == 2)
740         {
741                 c2 = c2 + 1;
742         }
743         else if(smallestteam == 3)
744         {
745                 c3 = c3 + 1;
746         }
747         else if(smallestteam == 4)
748         {
749                 c4 = c4 + 1;
750         }
751         else
752         {
753                 bprint("warning: destination team invalid\n");
754                 return;
755         }
756         // source team loses a member
757         if(source_team == 1)
758         {
759                 c1 = c1 + 1;
760         }
761         else if(source_team == 2)
762         {
763                 c2 = c2 + 2;
764         }
765         else if(source_team == 3)
766         {
767                 c3 = c3 + 3;
768         }
769         else if(source_team == 4)
770         {
771                 c4 = c4 + 4;
772         }
773         else
774         {
775                 bprint("warning: source team invalid\n");
776                 return;
777         }
778
779         // move the player to the new team
780         TeamchangeFrags(selected);
781         SetPlayerTeam(selected, smallestteam, source_team, false);
782
783         if(!IS_DEAD(selected))
784                 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE.m_id, selected.origin, '0 0 0');
785         Send_Notification(NOTIF_ONE, selected, MSG_CENTER, CENTER_DEATH_SELF_AUTOTEAMCHANGE, selected.team);
786 }