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