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