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