]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/teamplay.qc
Use snake_case in teamplay.
[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 (CS(this).version_mismatch) {
91                 if(CS(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 player, float _color)
173 {
174         float pants = _color & 0x0F;
175         float shirt = _color & 0xF0;
176         if (teamplay)
177         {
178                 setcolor(player, 16 * pants + pants);
179         }
180         else
181         {
182                 setcolor(player, shirt + pants);
183         }
184 }
185
186 bool SetPlayerTeamSimple(entity player, int team_num)
187 {
188         if (player.team == team_num)
189         {
190                 // This is important when players join the game and one of their color
191                 // matches the team color while other doesn't. For example [BOT]Lion.
192                 SetPlayerColors(player, team_num - 1);
193                 return true;
194         }
195         if (MUTATOR_CALLHOOK(Player_ChangeTeam, player, Team_TeamToNumber(
196                 player.team), Team_TeamToNumber(team_num)) == true)
197         {
198                 // Mutator has blocked team change.
199                 return false;
200         }
201         int old_team = player.team;
202         SetPlayerColors(player, team_num - 1);
203         MUTATOR_CALLHOOK(Player_ChangedTeam, player, old_team, player.team);
204         return true;
205 }
206
207 bool SetPlayerTeam(entity player, int destination_team, int source_team,
208         bool no_print)
209 {
210         int team_num = Team_NumberToTeam(destination_team);
211         if (!SetPlayerTeamSimple(player, team_num))
212         {
213                 return false;
214         }
215         LogTeamchange(player.playerid, player.team, 3);  // log manual team join
216         if (no_print)
217         {
218                 return true;
219         }
220         bprint(playername(player, false), "^7 has changed from ", Team_NumberToColoredFullName(source_team), "^7 to ", Team_NumberToColoredFullName(destination_team), "\n");
221         return true;
222 }
223
224 // set c1...c4 to show what teams are allowed
225 void CheckAllowedTeams(entity for_whom)
226 {
227         int teams_mask = 0;
228
229         c1 = c2 = c3 = c4 = -1;
230         num_bots_team1 = num_bots_team2 = num_bots_team3 = num_bots_team4 = 0;
231
232         string teament_name = string_null;
233
234         bool mutator_returnvalue = MUTATOR_CALLHOOK(CheckAllowedTeams, teams_mask, teament_name, for_whom);
235         teams_mask = M_ARGV(0, float);
236         teament_name = M_ARGV(1, string);
237
238         if(!mutator_returnvalue)
239         {
240                 if(teams_mask & BIT(0)) c1 = 0;
241                 if(teams_mask & BIT(1)) c2 = 0;
242                 if(teams_mask & BIT(2)) c3 = 0;
243                 if(teams_mask & BIT(3)) c4 = 0;
244         }
245
246         // find out what teams are allowed if necessary
247         if(teament_name)
248         {
249                 entity head = find(NULL, classname, teament_name);
250                 while(head)
251                 {
252                         switch(head.team)
253                         {
254                                 case NUM_TEAM_1: c1 = 0; break;
255                                 case NUM_TEAM_2: c2 = 0; break;
256                                 case NUM_TEAM_3: c3 = 0; break;
257                                 case NUM_TEAM_4: c4 = 0; break;
258                         }
259
260                         head = find(head, classname, teament_name);
261                 }
262         }
263
264         // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
265         if(AvailableTeams() == 2)
266         if(autocvar_bot_vs_human && for_whom)
267         {
268                 if(autocvar_bot_vs_human > 0)
269                 {
270                         // find last team available
271
272                         if(IS_BOT_CLIENT(for_whom))
273                         {
274                                 if(c4 >= 0) { c3 = c2 = c1 = -1; }
275                                 else if(c3 >= 0) { c4 = c2 = c1 = -1; }
276                                 else { c4 = c3 = c1 = -1; }
277                                 // no further cases, we know at least 2 teams exist
278                         }
279                         else
280                         {
281                                 if(c1 >= 0) { c2 = c3 = c4 = -1; }
282                                 else if(c2 >= 0) { c1 = c3 = c4 = -1; }
283                                 else { c1 = c2 = c4 = -1; }
284                                 // no further cases, bots have one of the teams
285                         }
286                 }
287                 else
288                 {
289                         // find first team available
290
291                         if(IS_BOT_CLIENT(for_whom))
292                         {
293                                 if(c1 >= 0) { c2 = c3 = c4 = -1; }
294                                 else if(c2 >= 0) { c1 = c3 = c4 = -1; }
295                                 else { c1 = c2 = c4 = -1; }
296                                 // no further cases, we know at least 2 teams exist
297                         }
298                         else
299                         {
300                                 if(c4 >= 0) { c3 = c2 = c1 = -1; }
301                                 else if(c3 >= 0) { c4 = c2 = c1 = -1; }
302                                 else { c4 = c3 = c1 = -1; }
303                                 // no further cases, bots have one of the teams
304                         }
305                 }
306         }
307
308         if(!for_whom)
309                 return;
310
311         // if player has a forced team, ONLY allow that one
312         if(for_whom.team_forced == NUM_TEAM_1 && c1 >= 0)
313                 c2 = c3 = c4 = -1;
314         else if(for_whom.team_forced == NUM_TEAM_2 && c2 >= 0)
315                 c1 = c3 = c4 = -1;
316         else if(for_whom.team_forced == NUM_TEAM_3 && c3 >= 0)
317                 c1 = c2 = c4 = -1;
318         else if(for_whom.team_forced == NUM_TEAM_4 && c4 >= 0)
319                 c1 = c2 = c3 = -1;
320 }
321
322 float PlayerValue(entity p)
323 {
324         return 1;
325         // FIXME: it always returns 1...
326 }
327
328 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
329 // teams that are allowed will now have their player counts stored in c1...c4
330 void GetTeamCounts(entity ignore)
331 {
332         if (MUTATOR_CALLHOOK(GetTeamCounts) == true)
333         {
334                 if (c1 >= 0)
335                 {
336                         MUTATOR_CALLHOOK(GetTeamCount, NUM_TEAM_1, ignore, c1,
337                                 num_bots_team1, lowest_human_team1, lowest_bot_team1);
338                         c1 = M_ARGV(2, float);
339                         num_bots_team1 = M_ARGV(3, float);
340                         lowest_human_team1 = M_ARGV(4, entity);
341                         lowest_bot_team1 = M_ARGV(5, entity);
342                 }
343                 if (c2 >= 0)
344                 {
345                         MUTATOR_CALLHOOK(GetTeamCount, NUM_TEAM_2, ignore, c2,
346                                 num_bots_team2, lowest_human_team2, lowest_bot_team2);
347                         c2 = M_ARGV(2, float);
348                         num_bots_team2 = M_ARGV(3, float);
349                         lowest_human_team2 = M_ARGV(4, entity);
350                         lowest_bot_team2 = M_ARGV(5, entity);
351                 }
352                 if (c3 >= 0)
353                 {
354                         MUTATOR_CALLHOOK(GetTeamCount, NUM_TEAM_3, ignore, c3,
355                                 num_bots_team3, lowest_human_team3, lowest_bot_team3);
356                         c3 = M_ARGV(2, float);
357                         num_bots_team3 = M_ARGV(3, float);
358                         lowest_human_team3 = M_ARGV(4, entity);
359                         lowest_bot_team3 = M_ARGV(5, entity);
360                 }
361                 if (c4 >= 0)
362                 {
363                         MUTATOR_CALLHOOK(GetTeamCount, NUM_TEAM_4, ignore,
364                                 c4, num_bots_team4, lowest_human_team4, lowest_bot_team4);
365                         c4 = M_ARGV(2, float);
366                         num_bots_team4 = M_ARGV(3, float);
367                         lowest_human_team4 = M_ARGV(4, entity);
368                         lowest_bot_team4 = M_ARGV(5, entity);
369                 }
370         }
371         else
372         {
373                 float value, bvalue;
374                 // now count how many players are on each team already
375                 float lowest_human_score1 = FLOAT_MAX;
376                 float lowest_bot_score1 = FLOAT_MAX;
377                 float lowest_human_score2 = FLOAT_MAX;
378                 float lowest_bot_score2 = FLOAT_MAX;
379                 float lowest_human_score3 = FLOAT_MAX;
380                 float lowest_bot_score3 = FLOAT_MAX;
381                 float lowest_human_score4 = FLOAT_MAX;
382                 float lowest_bot_score4 = FLOAT_MAX;
383                 FOREACH_CLIENT(true,
384                 {
385                         float t;
386                         if (IS_PLAYER(it) || it.caplayer)
387                         {
388                                 t = it.team;
389                         }
390                         else if (it.team_forced > 0)
391                         {
392                                 t = it.team_forced; // reserve the spot
393                         }
394                         else
395                         {
396                                 continue;
397                         }
398                         if (it == ignore)
399                         {
400                                 continue;
401                         }
402                         value = PlayerValue(it);
403                         if (IS_BOT_CLIENT(it))
404                         {
405                                 bvalue = value;
406                         }
407                         else
408                         {
409                                 bvalue = 0;
410                         }
411                         if (value == 0)
412                         {
413                                 continue;
414                         }
415                         switch (t)
416                         {
417                                 case NUM_TEAM_1:
418                                 {
419                                         if (c1 < 0)
420                                         {
421                                                 break;
422                                         }
423                                         c1 += value;
424                                         num_bots_team1 += bvalue;
425                                         float temp_score = PlayerScore_Get(it, SP_SCORE);
426                                         if (!bvalue)
427                                         {
428                                                 if (temp_score < lowest_human_score1)
429                                                 {
430                                                         lowest_human_team1 = it;
431                                                         lowest_human_score1 = temp_score;
432                                                 }
433                                                 break;
434                                         }
435                                         if (temp_score < lowest_bot_score1)
436                                         {
437                                                 lowest_bot_team1 = it;
438                                                 lowest_bot_score1 = temp_score;
439                                         }
440                                         break;
441                                 }
442                                 case NUM_TEAM_2:
443                                 {
444                                         if (c2 < 0)
445                                         {
446                                                 break;
447                                         }
448                                         c2 += value;
449                                         num_bots_team2 += bvalue;
450                                         float temp_score = PlayerScore_Get(it, SP_SCORE);
451                                         if (!bvalue)
452                                         {
453                                                 if (temp_score < lowest_human_score2)
454                                                 {
455                                                         lowest_human_team2 = it;
456                                                         lowest_human_score2 = temp_score;
457                                                 }
458                                                 break;
459                                         }
460                                         if (temp_score < lowest_bot_score2)
461                                         {
462                                                 lowest_bot_team2 = it;
463                                                 lowest_bot_score2 = temp_score;
464                                         }
465                                         break;
466                                 }
467                                 case NUM_TEAM_3:
468                                 {
469                                         if (c3 < 0)
470                                         {
471                                                 break;
472                                         }
473                                         c3 += value;
474                                         num_bots_team3 += bvalue;
475                                         float temp_score = PlayerScore_Get(it, SP_SCORE);
476                                         if (!bvalue)
477                                         {
478                                                 if (temp_score < lowest_human_score3)
479                                                 {
480                                                         lowest_human_team3 = it;
481                                                         lowest_human_score3 = temp_score;
482                                                 }
483                                                 break;
484                                         }
485                                         if (temp_score < lowest_bot_score3)
486                                         {
487                                                 lowest_bot_team3 = it;
488                                                 lowest_bot_score3 = temp_score;
489                                         }
490                                         break;
491                                 }
492                                 case NUM_TEAM_4:
493                                 {
494                                         if (c4 < 0)
495                                         {
496                                                 break;
497                                         }
498                                         c4 += value;
499                                         num_bots_team4 += bvalue;
500                                         float temp_score = PlayerScore_Get(it, SP_SCORE);
501                                         if (!bvalue)
502                                         {
503                                                 if (temp_score < lowest_human_score4)
504                                                 {
505                                                         lowest_human_team4 = it;
506                                                         lowest_human_score4 = temp_score;
507                                                 }
508                                                 break;
509                                         }
510                                         if (temp_score < lowest_bot_score4)
511                                         {
512                                                 lowest_bot_team4 = it;
513                                                 lowest_bot_score4 = temp_score;
514                                         }
515                                         break;
516                                 }
517                         }
518                 });
519         }
520
521         // if the player who has a forced team has not joined yet, reserve the spot
522         if(autocvar_g_campaign)
523         {
524                 switch(autocvar_g_campaign_forceteam)
525                 {
526                         case 1: if(c1 == num_bots_team1) ++c1; break;
527                         case 2: if(c2 == num_bots_team2) ++c2; break;
528                         case 3: if(c3 == num_bots_team3) ++c3; break;
529                         case 4: if(c4 == num_bots_team4) ++c4; break;
530                 }
531         }
532 }
533
534 bool IsTeamSmallerThanTeam(int team_a, int team_b, entity player,
535         bool use_score)
536 {
537         if (team_a == team_b)
538         {
539                 return false;
540         }
541         // we assume that CheckAllowedTeams and GetTeamCounts have already been called
542         int num_players_team_a = -1, num_players_team_b = -1;
543         int num_bots_team_a = 0, num_bots_team_b = 0;
544         float score_team_a = 0, score_team_b = 0;
545         switch (team_a)
546         {
547                 case 1:
548                 {
549                         num_players_team_a = c1;
550                         num_bots_team_a = num_bots_team1;
551                         score_team_a = team1_score;
552                         break;
553                 }
554                 case 2:
555                 {
556                         num_players_team_a = c2;
557                         num_bots_team_a = num_bots_team2;
558                         score_team_a = team2_score;
559                         break;
560                 }
561                 case 3:
562                 {
563                         num_players_team_a = c3;
564                         num_bots_team_a = num_bots_team3;
565                         score_team_a = team3_score;
566                         break;
567                 }
568                 case 4:
569                 {
570                         num_players_team_a = c4;
571                         num_bots_team_a = num_bots_team4;
572                         score_team_a = team4_score;
573                         break;
574                 }
575         }
576         switch (team_b)
577         {
578                 case 1:
579                 {
580                         num_players_team_b = c1;
581                         num_bots_team_b = num_bots_team1;
582                         score_team_b = team1_score;
583                         break;
584                 }
585                 case 2:
586                 {
587                         num_players_team_b = c2;
588                         num_bots_team_b = num_bots_team2;
589                         score_team_b = team2_score;
590                         break;
591                 }
592                 case 3:
593                 {
594                         num_players_team_b = c3;
595                         num_bots_team_b = num_bots_team3;
596                         score_team_b = team3_score;
597                         break;
598                 }
599                 case 4:
600                 {
601                         num_players_team_b = c4;
602                         num_bots_team_b = num_bots_team4;
603                         score_team_b = team4_score;
604                         break;
605                 }
606         }
607         // invalid
608         if (num_players_team_a < 0 || num_players_team_b < 0)
609         {
610                 return false;
611         }
612         if (IS_REAL_CLIENT(player) && bots_would_leave)
613         {
614                 num_players_team_a -= num_bots_team_a;
615                 num_players_team_b -= num_bots_team_b;
616         }
617         if (!use_score)
618         {
619                 return num_players_team_a < num_players_team_b;
620         }
621         if (num_players_team_a < num_players_team_b)
622         {
623                 return true;
624         }
625         if (num_players_team_a > num_players_team_b)
626         {
627                 return false;
628         }
629         return score_team_a < score_team_b;
630 }
631
632 bool IsTeamEqualToTeam(int team_a, int team_b, entity player, bool use_score)
633 {
634         if (team_a == team_b)
635         {
636                 return true;
637         }
638         // we assume that CheckAllowedTeams and GetTeamCounts have already been called
639         int num_players_team_a = -1, num_players_team_b = -1;
640         int num_bots_team_a = 0, num_bots_team_b = 0;
641         float score_team_a = 0, score_team_b = 0;
642         switch (team_a)
643         {
644                 case 1:
645                 {
646                         num_players_team_a = c1;
647                         num_bots_team_a = num_bots_team1;
648                         score_team_a = team1_score;
649                         break;
650                 }
651                 case 2:
652                 {
653                         num_players_team_a = c2;
654                         num_bots_team_a = num_bots_team2;
655                         score_team_a = team2_score;
656                         break;
657                 }
658                 case 3:
659                 {
660                         num_players_team_a = c3;
661                         num_bots_team_a = num_bots_team3;
662                         score_team_a = team3_score;
663                         break;
664                 }
665                 case 4:
666                 {
667                         num_players_team_a = c4;
668                         num_bots_team_a = num_bots_team4;
669                         score_team_a = team4_score;
670                         break;
671                 }
672         }
673         switch (team_b)
674         {
675                 case 1:
676                 {
677                         num_players_team_b = c1;
678                         num_bots_team_b = num_bots_team1;
679                         score_team_b = team1_score;
680                         break;
681                 }
682                 case 2:
683                 {
684                         num_players_team_b = c2;
685                         num_bots_team_b = num_bots_team2;
686                         score_team_b = team2_score;
687                         break;
688                 }
689                 case 3:
690                 {
691                         num_players_team_b = c3;
692                         num_bots_team_b = num_bots_team3;
693                         score_team_b = team3_score;
694                         break;
695                 }
696                 case 4:
697                 {
698                         num_players_team_b = c4;
699                         num_bots_team_b = num_bots_team4;
700                         score_team_b = team4_score;
701                         break;
702                 }
703         }
704         // invalid
705         if (num_players_team_a < 0 || num_players_team_b < 0)
706                 return false;
707
708         if (IS_REAL_CLIENT(player) && bots_would_leave)
709         {
710                 num_players_team_a -= num_bots_team_a;
711                 num_players_team_b -= num_bots_team_b;
712         }
713         if (!use_score)
714         {
715                 return num_players_team_a == num_players_team_b;
716         }
717         if (num_players_team_a != num_players_team_b)
718         {
719                 return false;
720         }
721         return score_team_a == score_team_b;
722 }
723
724 int FindBestTeams(entity player, bool use_score)
725 {
726         if (MUTATOR_CALLHOOK(FindBestTeams, player) == true)
727         {
728                 return M_ARGV(1, float);
729         }
730         int team_bits = 0;
731         int previous_team = 0;
732         if (c1 >= 0)
733         {
734                 team_bits = BIT(0);
735                 previous_team = 1;
736         }
737         if (c2 >= 0)
738         {
739                 if (previous_team == 0)
740                 {
741                         team_bits = BIT(1);
742                         previous_team = 2;
743                 }
744                 else if (IsTeamSmallerThanTeam(2, previous_team, player, use_score))
745                 {
746                         team_bits = BIT(1);
747                         previous_team = 2;
748                 }
749                 else if (IsTeamEqualToTeam(2, previous_team, player, use_score))
750                 {
751                         team_bits |= BIT(1);
752                         previous_team = 2;
753                 }
754         }
755         if (c3 >= 0)
756         {
757                 if (previous_team == 0)
758                 {
759                         team_bits = BIT(2);
760                         previous_team = 3;
761                 }
762                 else if (IsTeamSmallerThanTeam(3, previous_team, player, use_score))
763                 {
764                         team_bits = BIT(2);
765                         previous_team = 3;
766                 }
767                 else if (IsTeamEqualToTeam(3, previous_team, player, use_score))
768                 {
769                         team_bits |= BIT(2);
770                         previous_team = 3;
771                 }
772         }
773         if (c4 >= 0)
774         {
775                 if (previous_team == 0)
776                 {
777                         team_bits = BIT(3);
778                 }
779                 else if (IsTeamSmallerThanTeam(4, previous_team, player, use_score))
780                 {
781                         team_bits = BIT(3);
782                 }
783                 else if (IsTeamEqualToTeam(4, previous_team, player, use_score))
784                 {
785                         team_bits |= BIT(3);
786                 }
787         }
788         return team_bits;
789 }
790
791 // returns # of smallest team (1, 2, 3, 4)
792 // NOTE: Assumes CheckAllowedTeams has already been called!
793 int FindSmallestTeam(entity player, float ignore_player)
794 {
795         // count how many players are in each team
796         if (ignore_player)
797         {
798                 GetTeamCounts(player);
799         }
800         else
801         {
802                 GetTeamCounts(NULL);
803         }
804         int team_bits = FindBestTeams(player, true);
805         if (team_bits == 0)
806         {
807                 error(sprintf("No teams available for %s\n", MapInfo_Type_ToString(MapInfo_CurrentGametype())));
808         }
809         RandomSelection_Init();
810         if ((team_bits & BIT(0)) != 0)
811         {
812                 RandomSelection_AddFloat(1, 1, 1);
813         }
814         if ((team_bits & BIT(1)) != 0)
815         {
816                 RandomSelection_AddFloat(2, 1, 1);
817         }
818         if ((team_bits & BIT(2)) != 0)
819         {
820                 RandomSelection_AddFloat(3, 1, 1);
821         }
822         if ((team_bits & BIT(3)) != 0)
823         {
824                 RandomSelection_AddFloat(4, 1, 1);
825         }
826         return RandomSelection_chosen_float;
827 }
828
829 int JoinBestTeam(entity this, bool only_return_best, bool force_best_team)
830 {
831         // don't join a team if we're not playing a team game
832         if (!teamplay)
833         {
834                 return 0;
835         }
836
837         // find out what teams are available
838         CheckAllowedTeams(this);
839
840         int selected_team;
841
842         // if we don't care what team he ends up on, put him on whatever team he entered as.
843         // if he's not on a valid team, then let other code put him on the smallest team
844         if (!force_best_team)
845         {
846                 if(     c1 >= 0 && this.team == NUM_TEAM_1)
847                         selected_team = this.team;
848                 else if(c2 >= 0 && this.team == NUM_TEAM_2)
849                         selected_team = this.team;
850                 else if(c3 >= 0 && this.team == NUM_TEAM_3)
851                         selected_team = this.team;
852                 else if(c4 >= 0 && this.team == NUM_TEAM_4)
853                         selected_team = this.team;
854                 else
855                         selected_team = -1;
856
857                 if (selected_team > 0)
858                 {
859                         if (!only_return_best)
860                         {
861                                 SetPlayerTeamSimple(this, selected_team);
862
863                                 // when JoinBestTeam is called by client.qc/ClientKill_Now_TeamChange the players team is -1 and thus skipped
864                                 // when JoinBestTeam is called by client.qc/ClientConnect the player_id is 0 the log attempt is rejected
865                                 LogTeamchange(this.playerid, this.team, 99);
866                         }
867                         return selected_team;
868                 }
869                 // otherwise end up on the smallest team (handled below)
870         }
871
872         int best_team = FindSmallestTeam(this, true);
873         if (only_return_best || this.bot_forced_team)
874         {
875                 return best_team;
876         }
877         best_team = Team_NumberToTeam(best_team);
878         if (best_team == -1)
879         {
880                 error("JoinBestTeam: invalid team\n");
881         }
882         int old_team = Team_TeamToNumber(this.team);
883         TeamchangeFrags(this);
884         SetPlayerTeamSimple(this, best_team);
885         LogTeamchange(this.playerid, this.team, 2); // log auto join
886         if (!IS_BOT_CLIENT(this))
887         {
888                 AutoBalanceBots(old_team, Team_TeamToNumber(best_team));
889         }
890         if (!IS_DEAD(this) && (MUTATOR_CALLHOOK(Player_ChangeTeamKill, this) ==
891                 false))
892         {
893                 Damage(this, this, this, 100000, DEATH_TEAMCHANGE.m_id, this.origin, '0 0 0');
894         }
895         return best_team;
896 }
897
898 void SV_ChangeTeam(entity this, float _color)
899 {
900         float source_color, destination_color, source_team, destination_team;
901
902         // in normal deathmatch we can just apply the color and we're done
903         if(!teamplay)
904                 SetPlayerColors(this, _color);
905
906         if(!IS_CLIENT(this))
907         {
908                 // since this is an engine function, and gamecode doesn't have any calls earlier than this, do the connecting message here
909                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_CONNECTING, this.netname);
910                 return;
911         }
912
913         if(!teamplay)
914                 return;
915
916         source_color = this.clientcolors & 0x0F;
917         destination_color = _color & 0x0F;
918
919         source_team = Team_TeamToNumber(source_color + 1);
920         destination_team = Team_TeamToNumber(destination_color + 1);
921
922         CheckAllowedTeams(this);
923
924         if (destination_team == 1 && c1 < 0) destination_team = 4;
925         if (destination_team == 4 && c4 < 0) destination_team = 3;
926         if (destination_team == 3 && c3 < 0) destination_team = 2;
927         if (destination_team == 2 && c2 < 0) destination_team = 1;
928
929         // not changing teams
930         if (source_color == destination_color)
931         {
932                 SetPlayerTeam(this, destination_team, source_team, true);
933                 return;
934         }
935
936         if((autocvar_g_campaign) || (autocvar_g_changeteam_banned && CS(this).wasplayer)) {
937                 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_TEAMCHANGE_NOTALLOWED);
938                 return; // changing teams is not allowed
939         }
940
941         // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
942         if (autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
943         {
944                 GetTeamCounts(this);
945                 if ((BIT(destination_team - 1) & FindBestTeams(this, false)) == 0)
946                 {
947                         Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_TEAMCHANGE_LARGERTEAM);
948                         return;
949                 }
950         }
951         if(IS_PLAYER(this) && source_team != destination_team)
952         {
953                 // reduce frags during a team change
954                 TeamchangeFrags(this);
955         }
956         if (!SetPlayerTeam(this, destination_team, source_team, !IS_CLIENT(this)))
957         {
958                 return;
959         }
960         AutoBalanceBots(source_team, destination_team);
961         if (!IS_PLAYER(this) || (source_team == destination_team))
962         {
963                 return;
964         }
965         // kill player when changing teams
966         if (IS_DEAD(this) || (MUTATOR_CALLHOOK(Player_ChangeTeamKill, this) == true))
967         {
968                 return;
969         }
970         Damage(this, this, this, 100000, DEATH_TEAMCHANGE.m_id, this.origin, '0 0 0');
971 }
972
973 void AutoBalanceBots(int source_team, int destination_team)
974 {
975         if ((source_team == -1) || (destination_team == -1))
976         {
977                 return;
978         }
979         if (!autocvar_g_balance_teams ||
980                 !autocvar_g_balance_teams_prevent_imbalance)
981         {
982                 return;
983         }
984         int num_players_source_team = 0;
985         int num_players_destination_team = 0;
986         entity lowest_bot_destination_team = NULL;
987         switch (source_team)
988         {
989                 case 1:
990                 {
991                         num_players_source_team = c1;
992                         break;
993                 }
994                 case 2:
995                 {
996                         num_players_source_team = c2;
997                         break;
998                 }
999                 case 3:
1000                 {
1001                         num_players_source_team = c3;
1002                         break;
1003                 }
1004                 case 4:
1005                 {
1006                         num_players_source_team = c4;
1007                         break;
1008                 }
1009         }
1010         switch (destination_team)
1011         {
1012                 case 1:
1013                 {
1014                         num_players_destination_team = c1;
1015                         lowest_bot_destination_team = lowest_bot_team1;
1016                         break;
1017                 }
1018                 case 2:
1019                 {
1020                         num_players_destination_team = c2;
1021                         lowest_bot_destination_team = lowest_bot_team2;
1022                         break;
1023                 }
1024                 case 3:
1025                 {
1026                         num_players_destination_team = c3;
1027                         lowest_bot_destination_team = lowest_bot_team3;
1028                         break;
1029                 }
1030                 case 4:
1031                 {
1032                         num_players_destination_team = c4;
1033                         lowest_bot_destination_team = lowest_bot_team4;
1034                         break;
1035                 }
1036         }
1037         if ((num_players_destination_team <= num_players_source_team) ||
1038                 (lowest_bot_destination_team == NULL))
1039         {
1040                 return;
1041         }
1042         SetPlayerTeamSimple(lowest_bot_destination_team,
1043                 Team_NumberToTeam(source_team));
1044         if (IS_DEAD(lowest_bot_destination_team) || (MUTATOR_CALLHOOK(
1045                 Player_ChangeTeamKill, lowest_bot_destination_team) == true))
1046         {
1047                 return;
1048         }
1049         Damage(lowest_bot_destination_team, lowest_bot_destination_team,
1050                 lowest_bot_destination_team, 100000, DEATH_TEAMCHANGE.m_id,
1051                 lowest_bot_destination_team.origin, '0 0 0');
1052 }