]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/teamplay.qc
Merge branch 'master' into TimePath/modules
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / teamplay.qc
1 #include "teamplay.qh"
2
3 #include "client.qh"
4 #include "race.qh"
5 #include "scores.qh"
6 #include "scores_rules.qh"
7
8 #include "bot/api.qh"
9
10 #include "command/vote.qh"
11
12 #include "mutators/_mod.qh"
13
14 #include "../common/deathtypes/all.qh"
15 #include "../common/gamemodes/_mod.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 & BIT(0)) c1 = 0;
223                 if(dm & BIT(1)) c2 = 0;
224                 if(dm & BIT(2)) c3 = 0;
225                 if(dm & BIT(3)) c4 = 0;
226         }
227
228         // find out what teams are allowed if necessary
229         if(teament_name)
230         {
231                 entity head = find(NULL, classname, teament_name);
232                 while(head)
233                 {
234                         switch(head.team)
235                         {
236                                 case NUM_TEAM_1: c1 = 0; break;
237                                 case NUM_TEAM_2: c2 = 0; break;
238                                 case NUM_TEAM_3: c3 = 0; break;
239                                 case NUM_TEAM_4: c4 = 0; break;
240                         }
241
242                         head = find(head, classname, teament_name);
243                 }
244         }
245
246         // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
247         if(AvailableTeams() == 2)
248         if(autocvar_bot_vs_human && for_whom)
249         {
250                 if(autocvar_bot_vs_human > 0)
251                 {
252                         // find last team available
253
254                         if(IS_BOT_CLIENT(for_whom))
255                         {
256                                 if(c4 >= 0) { c3 = c2 = c1 = -1; }
257                                 else if(c3 >= 0) { c4 = c2 = c1 = -1; }
258                                 else { c4 = c3 = c1 = -1; }
259                                 // no further cases, we know at least 2 teams exist
260                         }
261                         else
262                         {
263                                 if(c1 >= 0) { c2 = c3 = c4 = -1; }
264                                 else if(c2 >= 0) { c1 = c3 = c4 = -1; }
265                                 else { c1 = c2 = c4 = -1; }
266                                 // no further cases, bots have one of the teams
267                         }
268                 }
269                 else
270                 {
271                         // find first team available
272
273                         if(IS_BOT_CLIENT(for_whom))
274                         {
275                                 if(c1 >= 0) { c2 = c3 = c4 = -1; }
276                                 else if(c2 >= 0) { c1 = c3 = c4 = -1; }
277                                 else { c1 = c2 = c4 = -1; }
278                                 // no further cases, we know at least 2 teams exist
279                         }
280                         else
281                         {
282                                 if(c4 >= 0) { c3 = c2 = c1 = -1; }
283                                 else if(c3 >= 0) { c4 = c2 = c1 = -1; }
284                                 else { c4 = c3 = c1 = -1; }
285                                 // no further cases, bots have one of the teams
286                         }
287                 }
288         }
289
290         if(!for_whom)
291                 return;
292
293         // if player has a forced team, ONLY allow that one
294         if(for_whom.team_forced == NUM_TEAM_1 && c1 >= 0)
295                 c2 = c3 = c4 = -1;
296         else if(for_whom.team_forced == NUM_TEAM_2 && c2 >= 0)
297                 c1 = c3 = c4 = -1;
298         else if(for_whom.team_forced == NUM_TEAM_3 && c3 >= 0)
299                 c1 = c2 = c4 = -1;
300         else if(for_whom.team_forced == NUM_TEAM_4 && c4 >= 0)
301                 c1 = c2 = c3 = -1;
302 }
303
304 float PlayerValue(entity p)
305 {
306         return 1;
307         // FIXME: it always returns 1...
308 }
309
310 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
311 // teams that are allowed will now have their player counts stored in c1...c4
312 void GetTeamCounts(entity ignore)
313 {
314         float value, bvalue;
315         // now count how many players are on each team already
316
317         // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
318         // also remember the lowest-scoring player
319
320         FOREACH_CLIENT(true, LAMBDA(
321                 float t;
322                 if(IS_PLAYER(it) || it.caplayer)
323                         t = it.team;
324                 else if(it.team_forced > 0)
325                         t = it.team_forced; // reserve the spot
326                 else
327                         continue;
328                 if(it != ignore)// && it.netname != "")
329                 {
330                         value = PlayerValue(it);
331                         if(IS_BOT_CLIENT(it))
332                                 bvalue = value;
333                         else
334                                 bvalue = 0;
335                         if(t == NUM_TEAM_1)
336                         {
337                                 if(c1 >= 0)
338                                 {
339                                         c1 = c1 + value;
340                                         cb1 = cb1 + bvalue;
341                                 }
342                         }
343                         if(t == NUM_TEAM_2)
344                         {
345                                 if(c2 >= 0)
346                                 {
347                                         c2 = c2 + value;
348                                         cb2 = cb2 + bvalue;
349                                 }
350                         }
351                         if(t == NUM_TEAM_3)
352                         {
353                                 if(c3 >= 0)
354                                 {
355                                         c3 = c3 + value;
356                                         cb3 = cb3 + bvalue;
357                                 }
358                         }
359                         if(t == NUM_TEAM_4)
360                         {
361                                 if(c4 >= 0)
362                                 {
363                                         c4 = c4 + value;
364                                         cb4 = cb4 + bvalue;
365                                 }
366                         }
367                 }
368         ));
369
370         // if the player who has a forced team has not joined yet, reserve the spot
371         if(autocvar_g_campaign)
372         {
373                 switch(autocvar_g_campaign_forceteam)
374                 {
375                         case 1: if(c1 == cb1) ++c1; break;
376                         case 2: if(c2 == cb2) ++c2; break;
377                         case 3: if(c3 == cb3) ++c3; break;
378                         case 4: if(c4 == cb4) ++c4; break;
379                 }
380         }
381 }
382
383 float TeamSmallerEqThanTeam(float ta, float tb, entity e)
384 {
385         // we assume that CheckAllowedTeams and GetTeamCounts have already been called
386         float f;
387         float ca = -1, cb = -1, cba = 0, cbb = 0, sa = 0, sb = 0;
388
389         switch(ta)
390         {
391                 case 1: ca = c1; cba = cb1; sa = team1_score; break;
392                 case 2: ca = c2; cba = cb2; sa = team2_score; break;
393                 case 3: ca = c3; cba = cb3; sa = team3_score; break;
394                 case 4: ca = c4; cba = cb4; sa = team4_score; break;
395         }
396         switch(tb)
397         {
398                 case 1: cb = c1; cbb = cb1; sb = team1_score; break;
399                 case 2: cb = c2; cbb = cb2; sb = team2_score; break;
400                 case 3: cb = c3; cbb = cb3; sb = team3_score; break;
401                 case 4: cb = c4; cbb = cb4; sb = team4_score; break;
402         }
403
404         // invalid
405         if(ca < 0 || cb < 0)
406                 return false;
407
408         // equal
409         if(ta == tb)
410                 return true;
411
412         if(IS_REAL_CLIENT(e))
413         {
414                 if(bots_would_leave)
415                 {
416                         ca -= cba * 0.999;
417                         cb -= cbb * 0.999;
418                 }
419         }
420
421         // keep teams alive (teams of size 0 always count as smaller, ignoring score)
422         if(ca < 1)
423                 if(cb >= 1)
424                         return true;
425         if(ca >= 1)
426                 if(cb < 1)
427                         return false;
428
429         // first, normalize
430         f = max(ca, cb, 1);
431         ca /= f;
432         cb /= f;
433         f = max(sa, sb, 1);
434         sa /= f;
435         sb /= f;
436
437         // the more we're at the end of the match, the more take scores into account
438         f = bound(0, game_completion_ratio * autocvar_g_balance_teams_scorefactor, 1);
439         ca += (sa - ca) * f;
440         cb += (sb - cb) * f;
441
442         return ca <= cb;
443 }
444
445 // returns # of smallest team (1, 2, 3, 4)
446 // NOTE: Assumes CheckAllowedTeams has already been called!
447 float FindSmallestTeam(entity pl, float ignore_pl)
448 {
449         int totalteams = 0;
450         int t = 1; // initialize with a random team?
451         if(c4 >= 0) t = 4;
452         if(c3 >= 0) t = 3;
453         if(c2 >= 0) t = 2;
454         if(c1 >= 0) t = 1;
455
456         // find out what teams are available
457         //CheckAllowedTeams();
458
459         // make sure there are at least 2 teams to join
460         if(c1 >= 0)
461                 totalteams = totalteams + 1;
462         if(c2 >= 0)
463                 totalteams = totalteams + 1;
464         if(c3 >= 0)
465                 totalteams = totalteams + 1;
466         if(c4 >= 0)
467                 totalteams = totalteams + 1;
468
469         if((autocvar_bot_vs_human || pl.team_forced > 0) && totalteams == 1)
470                 totalteams += 1;
471
472         if(totalteams <= 1)
473         {
474                 if(autocvar_g_campaign && pl && IS_REAL_CLIENT(pl))
475                         return 1; // special case for campaign and player joining
476                 else if(totalteams == 1) // single team
477                         LOG_TRACEF("Only 1 team available for %s, you may need to fix your map", MapInfo_Type_ToString(MapInfo_CurrentGametype()));
478                 else // no teams, major no no
479                         error(sprintf("No teams available for %s\n", MapInfo_Type_ToString(MapInfo_CurrentGametype())));
480         }
481
482         // count how many players are in each team
483         if(ignore_pl)
484                 GetTeamCounts(pl);
485         else
486                 GetTeamCounts(NULL);
487
488         RandomSelection_Init();
489
490         if(TeamSmallerEqThanTeam(1, t, pl))
491                 t = 1;
492         if(TeamSmallerEqThanTeam(2, t, pl))
493                 t = 2;
494         if(TeamSmallerEqThanTeam(3, t, pl))
495                 t = 3;
496         if(TeamSmallerEqThanTeam(4, t, pl))
497                 t = 4;
498
499         // now t is the minimum, or A minimum!
500         if(t == 1 || TeamSmallerEqThanTeam(1, t, pl))
501                 RandomSelection_Add(NULL, 1, string_null, 1, 1);
502         if(t == 2 || TeamSmallerEqThanTeam(2, t, pl))
503                 RandomSelection_Add(NULL, 2, string_null, 1, 1);
504         if(t == 3 || TeamSmallerEqThanTeam(3, t, pl))
505                 RandomSelection_Add(NULL, 3, string_null, 1, 1);
506         if(t == 4 || TeamSmallerEqThanTeam(4, t, pl))
507                 RandomSelection_Add(NULL, 4, string_null, 1, 1);
508
509         return RandomSelection_chosen_float;
510 }
511
512 int JoinBestTeam(entity this, bool only_return_best, bool forcebestteam)
513 {
514         float smallest, selectedteam;
515
516         // don't join a team if we're not playing a team game
517         if(!teamplay)
518                 return 0;
519
520         // find out what teams are available
521         CheckAllowedTeams(this);
522
523         // if we don't care what team he ends up on, put him on whatever team he entered as.
524         // if he's not on a valid team, then let other code put him on the smallest team
525         if(!forcebestteam)
526         {
527                 if(     c1 >= 0 && this.team == NUM_TEAM_1)
528                         selectedteam = this.team;
529                 else if(c2 >= 0 && this.team == NUM_TEAM_2)
530                         selectedteam = this.team;
531                 else if(c3 >= 0 && this.team == NUM_TEAM_3)
532                         selectedteam = this.team;
533                 else if(c4 >= 0 && this.team == NUM_TEAM_4)
534                         selectedteam = this.team;
535                 else
536                         selectedteam = -1;
537
538                 if(selectedteam > 0)
539                 {
540                         if(!only_return_best)
541                         {
542                                 SetPlayerColors(this, selectedteam - 1);
543
544                                 // when JoinBestTeam is called by client.qc/ClientKill_Now_TeamChange the players team is -1 and thus skipped
545                                 // when JoinBestTeam is called by client.qc/ClientConnect the player_id is 0 the log attempt is rejected
546                                 LogTeamchange(this.playerid, this.team, 99);
547                         }
548                         return selectedteam;
549                 }
550                 // otherwise end up on the smallest team (handled below)
551         }
552
553         smallest = FindSmallestTeam(this, true);
554
555         if(!only_return_best && !this.bot_forced_team)
556         {
557                 TeamchangeFrags(this);
558                 if(smallest == 1)
559                 {
560                         SetPlayerColors(this, NUM_TEAM_1 - 1);
561                 }
562                 else if(smallest == 2)
563                 {
564                         SetPlayerColors(this, NUM_TEAM_2 - 1);
565                 }
566                 else if(smallest == 3)
567                 {
568                         SetPlayerColors(this, NUM_TEAM_3 - 1);
569                 }
570                 else if(smallest == 4)
571                 {
572                         SetPlayerColors(this, NUM_TEAM_4 - 1);
573                 }
574                 else
575                 {
576                         error("smallest team: invalid team\n");
577                 }
578
579                 LogTeamchange(this.playerid, this.team, 2); // log auto join
580
581                 if(!IS_DEAD(this))
582                         Damage(this, this, this, 100000, DEATH_TEAMCHANGE.m_id, this.origin, '0 0 0');
583         }
584
585         return smallest;
586 }
587
588 //void() ctf_playerchanged;
589 void SV_ChangeTeam(entity this, float _color)
590 {
591         float scolor, dcolor, steam, dteam; //, dbotcount, scount, dcount;
592
593         // in normal deathmatch we can just apply the color and we're done
594         if(!teamplay)
595                 SetPlayerColors(this, _color);
596
597         if(!IS_CLIENT(this))
598         {
599                 // since this is an engine function, and gamecode doesn't have any calls earlier than this, do the connecting message here
600                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_CONNECTING, this.netname);
601                 return;
602         }
603
604         if(!teamplay)
605                 return;
606
607         scolor = this.clientcolors & 0x0F;
608         dcolor = _color & 0x0F;
609
610         if(scolor == NUM_TEAM_1 - 1)
611                 steam = 1;
612         else if(scolor == NUM_TEAM_2 - 1)
613                 steam = 2;
614         else if(scolor == NUM_TEAM_3 - 1)
615                 steam = 3;
616         else // if(scolor == NUM_TEAM_4 - 1)
617                 steam = 4;
618         if(dcolor == NUM_TEAM_1 - 1)
619                 dteam = 1;
620         else if(dcolor == NUM_TEAM_2 - 1)
621                 dteam = 2;
622         else if(dcolor == NUM_TEAM_3 - 1)
623                 dteam = 3;
624         else // if(dcolor == NUM_TEAM_4 - 1)
625                 dteam = 4;
626
627         CheckAllowedTeams(this);
628
629         if(dteam == 1 && c1 < 0) dteam = 4;
630         if(dteam == 4 && c4 < 0) dteam = 3;
631         if(dteam == 3 && c3 < 0) dteam = 2;
632         if(dteam == 2 && c2 < 0) dteam = 1;
633
634         // not changing teams
635         if(scolor == dcolor)
636         {
637                 //bprint("same team change\n");
638                 SetPlayerTeam(this, dteam, steam, true);
639                 return;
640         }
641
642         if((autocvar_g_campaign) || (autocvar_g_changeteam_banned && this.wasplayer)) {
643                 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_TEAMCHANGE_NOTALLOWED);
644                 return; // changing teams is not allowed
645         }
646
647         // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
648         if(autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
649         {
650                 GetTeamCounts(this);
651                 if(!TeamSmallerEqThanTeam(dteam, steam, this))
652                 {
653                         Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_TEAMCHANGE_LARGERTEAM);
654                         return;
655                 }
656         }
657
658 //      bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
659
660         if(IS_PLAYER(this) && steam != dteam)
661         {
662                 // reduce frags during a team change
663                 TeamchangeFrags(this);
664         }
665
666         MUTATOR_CALLHOOK(Player_ChangeTeam, this, steam, dteam);
667
668         SetPlayerTeam(this, dteam, steam, !IS_CLIENT(this));
669
670         if(IS_PLAYER(this) && steam != dteam)
671         {
672                 // kill player when changing teams
673                 if(!IS_DEAD(this))
674                         Damage(this, this, this, 100000, DEATH_TEAMCHANGE.m_id, this.origin, '0 0 0');
675         }
676 }
677
678 void ShufflePlayerOutOfTeam (float source_team)
679 {
680         float smallestteam, smallestteam_count, steam;
681         float lowest_bot_score, lowest_player_score;
682         entity lowest_bot, lowest_player, selected;
683
684         smallestteam = 0;
685         smallestteam_count = 999999999;
686
687         if(c1 >= 0 && c1 < smallestteam_count)
688         {
689                 smallestteam = 1;
690                 smallestteam_count = c1;
691         }
692         if(c2 >= 0 && c2 < smallestteam_count)
693         {
694                 smallestteam = 2;
695                 smallestteam_count = c2;
696         }
697         if(c3 >= 0 && c3 < smallestteam_count)
698         {
699                 smallestteam = 3;
700                 smallestteam_count = c3;
701         }
702         if(c4 >= 0 && c4 < smallestteam_count)
703         {
704                 smallestteam = 4;
705                 smallestteam_count = c4;
706         }
707
708         if(!smallestteam)
709         {
710                 bprint("warning: no smallest team\n");
711                 return;
712         }
713
714         if(source_team == 1)
715                 steam = NUM_TEAM_1;
716         else if(source_team == 2)
717                 steam = NUM_TEAM_2;
718         else if(source_team == 3)
719                 steam = NUM_TEAM_3;
720         else // if(source_team == 4)
721                 steam = NUM_TEAM_4;
722
723         lowest_bot = NULL;
724         lowest_bot_score = 999999999;
725         lowest_player = NULL;
726         lowest_player_score = 999999999;
727
728         // find the lowest-scoring player & bot of that team
729         FOREACH_CLIENT(IS_PLAYER(it) && it.team == steam, LAMBDA(
730                 if(it.isbot)
731                 {
732                         if(it.totalfrags < lowest_bot_score)
733                         {
734                                 lowest_bot = it;
735                                 lowest_bot_score = it.totalfrags;
736                         }
737                 }
738                 else
739                 {
740                         if(it.totalfrags < lowest_player_score)
741                         {
742                                 lowest_player = it;
743                                 lowest_player_score = it.totalfrags;
744                         }
745                 }
746         ));
747
748         // prefers to move a bot...
749         if(lowest_bot != NULL)
750                 selected = lowest_bot;
751         // but it will move a player if it has to
752         else
753                 selected = lowest_player;
754         // don't do anything if it couldn't find anyone
755         if(!selected)
756         {
757                 bprint("warning: couldn't find a player to move from team\n");
758                 return;
759         }
760
761         // smallest team gains a member
762         if(smallestteam == 1)
763         {
764                 c1 = c1 + 1;
765         }
766         else if(smallestteam == 2)
767         {
768                 c2 = c2 + 1;
769         }
770         else if(smallestteam == 3)
771         {
772                 c3 = c3 + 1;
773         }
774         else if(smallestteam == 4)
775         {
776                 c4 = c4 + 1;
777         }
778         else
779         {
780                 bprint("warning: destination team invalid\n");
781                 return;
782         }
783         // source team loses a member
784         if(source_team == 1)
785         {
786                 c1 = c1 + 1;
787         }
788         else if(source_team == 2)
789         {
790                 c2 = c2 + 2;
791         }
792         else if(source_team == 3)
793         {
794                 c3 = c3 + 3;
795         }
796         else if(source_team == 4)
797         {
798                 c4 = c4 + 4;
799         }
800         else
801         {
802                 bprint("warning: source team invalid\n");
803                 return;
804         }
805
806         // move the player to the new team
807         TeamchangeFrags(selected);
808         SetPlayerTeam(selected, smallestteam, source_team, false);
809
810         if(!IS_DEAD(selected))
811                 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE.m_id, selected.origin, '0 0 0');
812         Send_Notification(NOTIF_ONE, selected, MSG_CENTER, CENTER_DEATH_SELF_AUTOTEAMCHANGE, selected.team);
813 }