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