]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/teamplay.qc
Merge branch 'master' into terencehill/string_prefixes_cleanup
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / teamplay.qc
1 #include "teamplay.qh"
2
3 void TeamchangeFrags(entity e)
4 {
5         PlayerScore_Clear(e);
6 }
7
8 void LogTeamchange(float player_id, float team_number, float type)
9 {
10         if(!autocvar_sv_eventlog)
11                 return;
12
13         if(player_id < 1)
14                 return;
15
16         GameLogEcho(strcat(":team:", ftos(player_id), ":", ftos(team_number), ":", ftos(type)));
17 }
18
19 void default_delayedinit()
20 {
21         if(!scores_initialized)
22                 ScoreRules_generic();
23 }
24
25 void ActivateTeamplay()
26 {
27         serverflags |= SERVERFLAG_TEAMPLAY;
28         teamplay = 1;
29 }
30
31 void InitGameplayMode()
32 {
33         float fraglimit_override, timelimit_override, leadlimit_override, qualifying_override;
34
35         qualifying_override = -1;
36
37         VoteReset();
38
39         // find out good world mins/maxs bounds, either the static bounds found by looking for solid, or the mapinfo specified bounds
40         get_mi_min_max(1);
41         world.mins = mi_min;
42         world.maxs = mi_max;
43
44         MapInfo_LoadMapSettings(mapname);
45         teamplay = 0;
46         serverflags &= ~SERVERFLAG_TEAMPLAY;
47
48         if (!cvar_value_issafe(world.fog))
49         {
50                 print("The current map contains a potentially harmful fog setting, ignored\n");
51                 world.fog = string_null;
52         }
53         if(MapInfo_Map_fog != "")
54                 if(MapInfo_Map_fog == "none")
55                         world.fog = string_null;
56                 else
57                         world.fog = strzone(MapInfo_Map_fog);
58         clientstuff = strzone(MapInfo_Map_clientstuff);
59
60         MapInfo_ClearTemps();
61
62         // set both here, gamemode can override it later
63         timelimit_override = autocvar_timelimit_override;
64         fraglimit_override = autocvar_fraglimit_override;
65         leadlimit_override = autocvar_leadlimit_override;
66         gamemode_name = MapInfo_Type_ToText(MapInfo_LoadedGametype);
67
68         if(g_dm)
69         {
70         }
71
72         if(g_tdm)
73         {
74                 ActivateTeamplay();
75                 fraglimit_override = autocvar_g_tdm_point_limit;
76                 leadlimit_override = autocvar_g_tdm_point_leadlimit;
77                 if(autocvar_g_tdm_team_spawns)
78                         have_team_spawns = -1; // request team spawns
79                 MUTATOR_ADD(gamemode_tdm);
80         }
81
82         if(g_domination)
83         {
84                 ActivateTeamplay();
85                 fraglimit_override = autocvar_g_domination_point_limit;
86                 leadlimit_override = autocvar_g_domination_point_leadlimit;
87                 if(autocvar_g_domination_roundbased && autocvar_g_domination_roundbased_point_limit)
88                         fraglimit_override = autocvar_g_domination_roundbased_point_limit;
89                 have_team_spawns = -1; // request team spawns
90                 MUTATOR_ADD(gamemode_domination);
91         }
92
93         if(g_ctf)
94         {
95                 ActivateTeamplay();
96                 fraglimit_override = autocvar_capturelimit_override;
97                 leadlimit_override = autocvar_captureleadlimit_override;
98                 have_team_spawns = -1; // request team spawns
99                 MUTATOR_ADD(gamemode_ctf);
100         }
101
102         if(g_lms)
103         {
104                 fraglimit_override = autocvar_g_lms_lives_override;
105                 leadlimit_override = 0; // not supported by LMS
106                 if(fraglimit_override == 0)
107                         fraglimit_override = -1;
108                 MUTATOR_ADD(gamemode_lms);
109         }
110
111         if(g_ca)
112         {
113                 ActivateTeamplay();
114                 fraglimit_override = autocvar_g_ca_point_limit;
115                 leadlimit_override = autocvar_g_ca_point_leadlimit;
116                 if(autocvar_g_ca_team_spawns)
117                         have_team_spawns = -1; // request team spawns
118                 MUTATOR_ADD(gamemode_ca);
119         }
120
121         if(g_keyhunt)
122         {
123                 ActivateTeamplay();
124                 fraglimit_override = autocvar_g_keyhunt_point_limit;
125                 leadlimit_override = autocvar_g_keyhunt_point_leadlimit;
126                 MUTATOR_ADD(gamemode_keyhunt);
127         }
128
129         if(g_freezetag)
130         {
131                 ActivateTeamplay();
132                 fraglimit_override = autocvar_g_freezetag_point_limit;
133                 leadlimit_override = autocvar_g_freezetag_point_leadlimit;
134                 if(autocvar_g_freezetag_team_spawns)
135                         have_team_spawns = -1; // request team spawns
136                 MUTATOR_ADD(gamemode_freezetag);
137         }
138
139         if(g_assault)
140         {
141                 ActivateTeamplay();
142                 have_team_spawns = -1; // request team spawns
143                 MUTATOR_ADD(gamemode_assault);
144         }
145
146         if(g_onslaught)
147         {
148                 ActivateTeamplay();
149                 have_team_spawns = -1; // request team spawns
150                 MUTATOR_ADD(gamemode_onslaught);
151         }
152
153         if(g_race)
154         {
155                 if(autocvar_g_race_teams)
156                 {
157                         ActivateTeamplay();
158                         race_teams = bound(2, autocvar_g_race_teams, 4);
159                         have_team_spawns = -1; // request team spawns
160                 }
161                 else
162                         race_teams = 0;
163                 qualifying_override = autocvar_g_race_qualifying_timelimit_override;
164                 fraglimit_override = autocvar_g_race_laps_limit;
165                 leadlimit_override = 0; // currently not supported by race
166
167                 // we need to find out the correct value for g_race_qualifying
168                 float want_qualifying = ((qualifying_override >= 0) ? qualifying_override : autocvar_g_race_qualifying_timelimit) > 0;
169
170                 if(autocvar_g_campaign)
171                 {
172                         g_race_qualifying = 1;
173                         independent_players = 1;
174                 }
175                 else if(!autocvar_g_campaign && want_qualifying)
176                 {
177                         g_race_qualifying = 2;
178                         independent_players = 1;
179                         race_fraglimit = (race_fraglimit >= 0) ? fraglimit_override : autocvar_fraglimit;
180                         race_leadlimit = (race_leadlimit >= 0) ? leadlimit_override : autocvar_leadlimit;
181                         race_timelimit = (race_timelimit >= 0) ? timelimit_override : autocvar_timelimit;
182                         fraglimit_override = 0;
183                         leadlimit_override = 0;
184                         timelimit_override = autocvar_g_race_qualifying_timelimit;
185                 }
186                 else
187                 {
188                         g_race_qualifying = 0;
189                 }
190
191                 MUTATOR_ADD(gamemode_race);
192         }
193
194         if(g_cts)
195         {
196                 g_race_qualifying = 1;
197                 fraglimit_override = 0;
198                 leadlimit_override = 0;
199                 independent_players = 1;
200                 MUTATOR_ADD(gamemode_cts);
201         }
202
203         if(g_nexball)
204         {
205                 fraglimit_override = autocvar_g_nexball_goallimit;
206                 leadlimit_override = autocvar_g_nexball_goalleadlimit;
207                 ActivateTeamplay();
208                 have_team_spawns = -1; // request team spawns
209                 MUTATOR_ADD(gamemode_nexball);
210         }
211
212         if(g_keepaway)
213         {
214                 MUTATOR_ADD(gamemode_keepaway);
215         }
216
217         if(g_invasion)
218         {
219                 fraglimit_override = autocvar_g_invasion_point_limit;
220                 if(autocvar_g_invasion_teams >= 2)
221                 {
222                         ActivateTeamplay();
223                         if(autocvar_g_invasion_team_spawns)
224                                 have_team_spawns = -1; // request team spawns
225                 }
226                 MUTATOR_ADD(gamemode_invasion);
227         }
228
229         if(teamplay)
230                 entcs_init();
231
232         cache_mutatormsg = strzone("");
233         cache_lastmutatormsg = strzone("");
234
235         // enforce the server's universal frag/time limits
236         if(!autocvar_g_campaign)
237         {
238                 if(fraglimit_override >= 0)
239                         cvar_set("fraglimit", ftos(fraglimit_override));
240                 if(timelimit_override >= 0)
241                         cvar_set("timelimit", ftos(timelimit_override));
242                 if(leadlimit_override >= 0)
243                         cvar_set("leadlimit", ftos(leadlimit_override));
244                 if(qualifying_override >= 0)
245                         cvar_set("g_race_qualifying_timelimit", ftos(qualifying_override));
246         }
247
248         InitializeEntity(world, default_delayedinit, INITPRIO_GAMETYPE_FALLBACK);
249 }
250
251 string GetClientVersionMessage() {
252         string versionmsg;
253         if (self.version_mismatch) {
254                 if(self.version < autocvar_gameversion) {
255                         versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
256                 } else {
257                         versionmsg = "^3This server is using an outdated Xonotic version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
258                 }
259         } else {
260                 versionmsg = "^2client version and server version are compatible.^8";
261         }
262         return versionmsg;
263 }
264
265 string getwelcomemessage(void)
266 {
267         string s, modifications, motd;
268
269         ret_string = "";
270         MUTATOR_CALLHOOK(BuildMutatorsPrettyString);
271         modifications = ret_string;
272
273         if(g_weaponarena)
274         {
275                 if(g_weaponarena_random)
276                         modifications = strcat(modifications, ", ", ftos(g_weaponarena_random), " of ", g_weaponarena_list, " Arena");
277                 else
278                         modifications = strcat(modifications, ", ", g_weaponarena_list, " Arena");
279         }
280         if(cvar("g_balance_blaster_weaponstart") == 0)
281                 modifications = strcat(modifications, ", No start weapons");
282         if(cvar("sv_gravity") < stof(cvar_defstring("sv_gravity")))
283                 modifications = strcat(modifications, ", Low gravity");
284         if(g_cloaked && !g_cts)
285                 modifications = strcat(modifications, ", Cloaked");
286         if(g_grappling_hook)
287                 modifications = strcat(modifications, ", Hook");
288         if(g_weapon_stay && !g_cts)
289                 modifications = strcat(modifications, ", Weapons stay");
290         if(g_jetpack)
291                 modifications = strcat(modifications, ", Jet pack");
292         if(autocvar_g_powerups == 0)
293                 modifications = strcat(modifications, ", No powerups");
294         if(autocvar_g_powerups > 0)
295                 modifications = strcat(modifications, ", Powerups");
296         modifications = substring(modifications, 2, strlen(modifications) - 2);
297
298         string versionmessage;
299         versionmessage = GetClientVersionMessage();
300
301         s = strcat("This is Xonotic ", autocvar_g_xonoticversion, "\n", versionmessage);
302         s = strcat(s, "^8\n\nmatch type is ^1", gamemode_name, "^8\n");
303
304         if(modifications != "")
305                 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
306
307         if (g_grappling_hook)
308                 s = strcat(s, "\n\n^3grappling hook^8 is enabled, press 'e' to use it\n");
309
310         if (cvar("g_nades"))
311                 s = strcat(s, "\n\n^3nades^8 are enabled, press 'g' to use them\n");
312
313         if(cache_lastmutatormsg != autocvar_g_mutatormsg)
314         {
315                 if(cache_lastmutatormsg)
316                         strunzone(cache_lastmutatormsg);
317                 if(cache_mutatormsg)
318                         strunzone(cache_mutatormsg);
319                 cache_lastmutatormsg = strzone(autocvar_g_mutatormsg);
320                 cache_mutatormsg = strzone(cache_lastmutatormsg);
321         }
322
323         if (cache_mutatormsg != "") {
324                 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
325         }
326
327         motd = autocvar_sv_motd;
328         if (motd != "") {
329                 s = strcat(s, "\n\n^8MOTD: ^7", strreplace("\\n", "\n", motd));
330         }
331         return s;
332 }
333
334 void SetPlayerColors(entity pl, float _color)
335 {
336         /*string s;
337         s = ftos(cl);
338         stuffcmd(pl, strcat("color ", s, " ", s, "\n")  );
339         pl.team = cl + 1;
340         //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
341         pl.clientcolors = 16*cl + cl;*/
342
343         float pants, shirt;
344         pants = _color & 0x0F;
345         shirt = _color & 0xF0;
346
347
348         if(teamplay) {
349                 setcolor(pl, 16*pants + pants);
350         } else {
351                 setcolor(pl, shirt + pants);
352         }
353 }
354
355 void SetPlayerTeam(entity pl, float t, float s, float noprint)
356 {
357         float _color;
358
359         if(t == 4)
360                 _color = NUM_TEAM_4 - 1;
361         else if(t == 3)
362                 _color = NUM_TEAM_3 - 1;
363         else if(t == 2)
364                 _color = NUM_TEAM_2 - 1;
365         else
366                 _color = NUM_TEAM_1 - 1;
367
368         SetPlayerColors(pl,_color);
369
370         if(t != s) {
371                 LogTeamchange(pl.playerid, pl.team, 3);  // log manual team join
372
373                 if(!noprint)
374                 bprint(pl.netname, "^7 has changed from ", Team_NumberToColoredFullName(s), "^7 to ", Team_NumberToColoredFullName(t), "\n");
375         }
376
377 }
378
379 // set c1...c4 to show what teams are allowed
380 void CheckAllowedTeams (entity for_whom)
381 {
382         float dm;
383         entity head;
384         string teament_name;
385
386         c1 = c2 = c3 = c4 = -1;
387         cb1 = cb2 = cb3 = cb4 = 0;
388
389         teament_name = string_null;
390         if(g_onslaught)
391         {
392                 // onslaught is special
393                 head = findchain(classname, "onslaught_generator");
394                 while (head)
395                 {
396                         if (head.team == NUM_TEAM_1) c1 = 0;
397                         if (head.team == NUM_TEAM_2) c2 = 0;
398                         if (head.team == NUM_TEAM_3) c3 = 0;
399                         if (head.team == NUM_TEAM_4) c4 = 0;
400                         head = head.chain;
401                 }
402         }
403         else if(g_domination)
404                 teament_name = "dom_team";
405         else if(g_ctf)
406                 teament_name = "ctf_team";
407         else if(g_tdm)
408                 teament_name = "tdm_team";
409         else if(g_nexball)
410                 teament_name = "nexball_team";
411         else if(g_assault)
412                 c1 = c2 = 0; // Assault always has 2 teams
413         else
414         {
415                 // cover anything else by treating it like tdm with no teams spawned
416                 dm = 2;
417
418                 ret_float = dm;
419                 MUTATOR_CALLHOOK(GetTeamCount);
420                 dm = ret_float;
421
422                 if(dm >= 4)
423                         c1 = c2 = c3 = c4 = 0;
424                 else if(dm >= 3)
425                         c1 = c2 = c3 = 0;
426                 else
427                         c1 = c2 = 0;
428         }
429
430         // find out what teams are allowed if necessary
431         if(teament_name)
432         {
433                 head = find(world, classname, teament_name);
434                 while(head)
435                 {
436                         if(!(g_domination && head.netname == ""))
437                         {
438                                 if(head.team == NUM_TEAM_1)
439                                         c1 = 0;
440                                 else if(head.team == NUM_TEAM_2)
441                                         c2 = 0;
442                                 else if(head.team == NUM_TEAM_3)
443                                         c3 = 0;
444                                 else if(head.team == NUM_TEAM_4)
445                                         c4 = 0;
446                         }
447                         head = find(head, classname, teament_name);
448                 }
449         }
450
451         // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
452         if(c3==-1 && c4==-1)
453         if(autocvar_bot_vs_human && for_whom)
454         {
455                 if(autocvar_bot_vs_human > 0)
456                 {
457                         // bots are all blue
458                         if(IS_BOT_CLIENT(for_whom))
459                                 c1 = c3 = c4 = -1;
460                         else
461                                 c2 = -1;
462                 }
463                 else
464                 {
465                         // bots are all red
466                         if(IS_BOT_CLIENT(for_whom))
467                                 c2 = c3 = c4 = -1;
468                         else
469                                 c1 = -1;
470                 }
471         }
472
473         // if player has a forced team, ONLY allow that one
474         if(self.team_forced == NUM_TEAM_1 && c1 >= 0)
475                 c2 = c3 = c4 = -1;
476         else if(self.team_forced == NUM_TEAM_2 && c2 >= 0)
477                 c1 = c3 = c4 = -1;
478         else if(self.team_forced == NUM_TEAM_3 && c3 >= 0)
479                 c1 = c2 = c4 = -1;
480         else if(self.team_forced == NUM_TEAM_4 && c4 >= 0)
481                 c1 = c2 = c3 = -1;
482 }
483
484 float PlayerValue(entity p)
485 {
486         return 1;
487         // FIXME: it always returns 1...
488 }
489
490 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
491 // teams that are allowed will now have their player counts stored in c1...c4
492 void GetTeamCounts(entity ignore)
493 {
494         entity head;
495         float value, bvalue;
496         // now count how many players are on each team already
497
498         // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
499         // also remember the lowest-scoring player
500
501         FOR_EACH_CLIENT(head)
502         {
503                 float t;
504                 if(IS_PLAYER(head) || head.caplayer)
505                         t = head.team;
506                 else if(head.team_forced > 0)
507                         t = head.team_forced; // reserve the spot
508                 else
509                         continue;
510                 if(head != ignore)// && head.netname != "")
511                 {
512                         value = PlayerValue(head);
513                         if(IS_BOT_CLIENT(head))
514                                 bvalue = value;
515                         else
516                                 bvalue = 0;
517                         if(t == NUM_TEAM_1)
518                         {
519                                 if(c1 >= 0)
520                                 {
521                                         c1 = c1 + value;
522                                         cb1 = cb1 + bvalue;
523                                 }
524                         }
525                         if(t == NUM_TEAM_2)
526                         {
527                                 if(c2 >= 0)
528                                 {
529                                         c2 = c2 + value;
530                                         cb2 = cb2 + bvalue;
531                                 }
532                         }
533                         if(t == NUM_TEAM_3)
534                         {
535                                 if(c3 >= 0)
536                                 {
537                                         c3 = c3 + value;
538                                         cb3 = cb3 + bvalue;
539                                 }
540                         }
541                         if(t == NUM_TEAM_4)
542                         {
543                                 if(c4 >= 0)
544                                 {
545                                         c4 = c4 + value;
546                                         cb4 = cb4 + bvalue;
547                                 }
548                         }
549                 }
550         }
551
552         // if the player who has a forced team has not joined yet, reserve the spot
553         if(autocvar_g_campaign)
554         {
555                 switch(autocvar_g_campaign_forceteam)
556                 {
557                         case 1: if(c1 == cb1) ++c1; break;
558                         case 2: if(c2 == cb2) ++c2; break;
559                         case 3: if(c3 == cb3) ++c3; break;
560                         case 4: if(c4 == cb4) ++c4; break;
561                 }
562         }
563 }
564
565 float TeamSmallerEqThanTeam(float ta, float tb, entity e)
566 {
567         // we assume that CheckAllowedTeams and GetTeamCounts have already been called
568         float f;
569         float ca = -1, cb = -1, cba = 0, cbb = 0, sa = 0, sb = 0;
570
571         switch(ta)
572         {
573                 case 1: ca = c1; cba = cb1; sa = team1_score; break;
574                 case 2: ca = c2; cba = cb2; sa = team2_score; break;
575                 case 3: ca = c3; cba = cb3; sa = team3_score; break;
576                 case 4: ca = c4; cba = cb4; sa = team4_score; break;
577         }
578         switch(tb)
579         {
580                 case 1: cb = c1; cbb = cb1; sb = team1_score; break;
581                 case 2: cb = c2; cbb = cb2; sb = team2_score; break;
582                 case 3: cb = c3; cbb = cb3; sb = team3_score; break;
583                 case 4: cb = c4; cbb = cb4; sb = team4_score; break;
584         }
585
586         // invalid
587         if(ca < 0 || cb < 0)
588                 return false;
589
590         // equal
591         if(ta == tb)
592                 return true;
593
594         if(IS_REAL_CLIENT(e))
595         {
596                 if(bots_would_leave)
597                 {
598                         ca -= cba * 0.999;
599                         cb -= cbb * 0.999;
600                 }
601         }
602
603         // keep teams alive (teams of size 0 always count as smaller, ignoring score)
604         if(ca < 1)
605                 if(cb >= 1)
606                         return true;
607         if(ca >= 1)
608                 if(cb < 1)
609                         return false;
610
611         // first, normalize
612         f = max(ca, cb, 1);
613         ca /= f;
614         cb /= f;
615         f = max(sa, sb, 1);
616         sa /= f;
617         sb /= f;
618
619         // the more we're at the end of the match, the more take scores into account
620         f = bound(0, game_completion_ratio * autocvar_g_balance_teams_scorefactor, 1);
621         ca += (sa - ca) * f;
622         cb += (sb - cb) * f;
623
624         return ca <= cb;
625 }
626
627 // returns # of smallest team (1, 2, 3, 4)
628 // NOTE: Assumes CheckAllowedTeams has already been called!
629 float FindSmallestTeam(entity pl, float ignore_pl)
630 {
631         float totalteams, t;
632         totalteams = 0;
633
634         // find out what teams are available
635         //CheckAllowedTeams();
636
637         // make sure there are at least 2 teams to join
638         if(c1 >= 0)
639                 totalteams = totalteams + 1;
640         if(c2 >= 0)
641                 totalteams = totalteams + 1;
642         if(c3 >= 0)
643                 totalteams = totalteams + 1;
644         if(c4 >= 0)
645                 totalteams = totalteams + 1;
646
647         if((autocvar_bot_vs_human || pl.team_forced > 0) && totalteams == 1)
648                 totalteams += 1;
649
650         if(totalteams <= 1)
651         {
652                 if(autocvar_g_campaign && pl && IS_REAL_CLIENT(pl))
653                         return 1; // special case for campaign and player joining
654                 else
655                         error(sprintf("Too few teams available for %s\n", MapInfo_Type_ToString(MapInfo_CurrentGametype())));
656         }
657
658         // count how many players are in each team
659         if(ignore_pl)
660                 GetTeamCounts(pl);
661         else
662                 GetTeamCounts(world);
663
664         RandomSelection_Init();
665
666         t = 1;
667         if(TeamSmallerEqThanTeam(2, t, pl))
668                 t = 2;
669         if(TeamSmallerEqThanTeam(3, t, pl))
670                 t = 3;
671         if(TeamSmallerEqThanTeam(4, t, pl))
672                 t = 4;
673
674         // now t is the minimum, or A minimum!
675         if(t == 1 || TeamSmallerEqThanTeam(1, t, pl))
676                 RandomSelection_Add(world, 1, string_null, 1, 1);
677         if(t == 2 || TeamSmallerEqThanTeam(2, t, pl))
678                 RandomSelection_Add(world, 2, string_null, 1, 1);
679         if(t == 3 || TeamSmallerEqThanTeam(3, t, pl))
680                 RandomSelection_Add(world, 3, string_null, 1, 1);
681         if(t == 4 || TeamSmallerEqThanTeam(4, t, pl))
682                 RandomSelection_Add(world, 4, string_null, 1, 1);
683
684         return RandomSelection_chosen_float;
685 }
686
687 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
688 {
689         float smallest, selectedteam;
690
691         // don't join a team if we're not playing a team game
692         if(!teamplay)
693                 return 0;
694
695         // find out what teams are available
696         CheckAllowedTeams(pl);
697
698         // if we don't care what team he ends up on, put him on whatever team he entered as.
699         // if he's not on a valid team, then let other code put him on the smallest team
700         if(!forcebestteam)
701         {
702                 if(     c1 >= 0 && pl.team == NUM_TEAM_1)
703                         selectedteam = pl.team;
704                 else if(c2 >= 0 && pl.team == NUM_TEAM_2)
705                         selectedteam = pl.team;
706                 else if(c3 >= 0 && pl.team == NUM_TEAM_3)
707                         selectedteam = pl.team;
708                 else if(c4 >= 0 && pl.team == NUM_TEAM_4)
709                         selectedteam = pl.team;
710                 else
711                         selectedteam = -1;
712
713                 if(selectedteam > 0)
714                 {
715                         if(!only_return_best)
716                         {
717                                 SetPlayerColors(pl, selectedteam - 1);
718
719                                 // when JoinBestTeam is called by client.qc/ClientKill_Now_TeamChange the players team is -1 and thus skipped
720                                 // when JoinBestTeam is called by cl_client.qc/ClientConnect the player_id is 0 the log attempt is rejected
721                                 LogTeamchange(pl.playerid, pl.team, 99);
722                         }
723                         return selectedteam;
724                 }
725                 // otherwise end up on the smallest team (handled below)
726         }
727
728         smallest = FindSmallestTeam(pl, true);
729
730         if(!only_return_best && !pl.bot_forced_team)
731         {
732                 TeamchangeFrags(self);
733                 if(smallest == 1)
734                 {
735                         SetPlayerColors(pl, NUM_TEAM_1 - 1);
736                 }
737                 else if(smallest == 2)
738                 {
739                         SetPlayerColors(pl, NUM_TEAM_2 - 1);
740                 }
741                 else if(smallest == 3)
742                 {
743                         SetPlayerColors(pl, NUM_TEAM_3 - 1);
744                 }
745                 else if(smallest == 4)
746                 {
747                         SetPlayerColors(pl, NUM_TEAM_4 - 1);
748                 }
749                 else
750                 {
751                         error("smallest team: invalid team\n");
752                 }
753
754                 LogTeamchange(pl.playerid, pl.team, 2); // log auto join
755
756                 if(pl.deadflag == DEAD_NO)
757                         Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
758         }
759
760         return smallest;
761 }
762
763 //void() ctf_playerchanged;
764 void SV_ChangeTeam(float _color)
765 {
766         float scolor, dcolor, steam, dteam; //, dbotcount, scount, dcount;
767
768         // in normal deathmatch we can just apply the color and we're done
769         if(!teamplay) {
770                 SetPlayerColors(self, _color);
771                 return;
772         }
773
774         scolor = self.clientcolors & 0x0F;
775         dcolor = _color & 0x0F;
776
777         if(scolor == NUM_TEAM_1 - 1)
778                 steam = 1;
779         else if(scolor == NUM_TEAM_2 - 1)
780                 steam = 2;
781         else if(scolor == NUM_TEAM_3 - 1)
782                 steam = 3;
783         else // if(scolor == NUM_TEAM_4 - 1)
784                 steam = 4;
785         if(dcolor == NUM_TEAM_1 - 1)
786                 dteam = 1;
787         else if(dcolor == NUM_TEAM_2 - 1)
788                 dteam = 2;
789         else if(dcolor == NUM_TEAM_3 - 1)
790                 dteam = 3;
791         else // if(dcolor == NUM_TEAM_4 - 1)
792                 dteam = 4;
793
794         CheckAllowedTeams(self);
795
796         if(dteam == 1 && c1 < 0) dteam = 4;
797         if(dteam == 4 && c4 < 0) dteam = 3;
798         if(dteam == 3 && c3 < 0) dteam = 2;
799         if(dteam == 2 && c2 < 0) dteam = 1;
800
801         // not changing teams
802         if(scolor == dcolor)
803         {
804                 //bprint("same team change\n");
805                 SetPlayerTeam(self, dteam, steam, true);
806                 return;
807         }
808
809         if((autocvar_g_campaign) || (autocvar_g_changeteam_banned && self.wasplayer)) {
810                 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_TEAMCHANGE_NOTALLOWED);
811                 return; // changing teams is not allowed
812         }
813
814         // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
815         if(autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
816         {
817                 GetTeamCounts(self);
818                 if(!TeamSmallerEqThanTeam(dteam, steam, self))
819                 {
820                         Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_TEAMCHANGE_LARGERTEAM);
821                         return;
822                 }
823         }
824
825 //      bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
826
827         if(IS_PLAYER(self) && steam != dteam)
828         {
829                 // reduce frags during a team change
830                 TeamchangeFrags(self);
831         }
832
833         SetPlayerTeam(self, dteam, steam, false);
834
835         if(IS_PLAYER(self) && steam != dteam)
836         {
837                 // kill player when changing teams
838                 if(self.deadflag == DEAD_NO)
839                         Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
840         }
841 }
842
843 void ShufflePlayerOutOfTeam (float source_team)
844 {
845         float smallestteam, smallestteam_count, steam;
846         float lowest_bot_score, lowest_player_score;
847         entity head, lowest_bot, lowest_player, selected;
848
849         smallestteam = 0;
850         smallestteam_count = 999999999;
851
852         if(c1 >= 0 && c1 < smallestteam_count)
853         {
854                 smallestteam = 1;
855                 smallestteam_count = c1;
856         }
857         if(c2 >= 0 && c2 < smallestteam_count)
858         {
859                 smallestteam = 2;
860                 smallestteam_count = c2;
861         }
862         if(c3 >= 0 && c3 < smallestteam_count)
863         {
864                 smallestteam = 3;
865                 smallestteam_count = c3;
866         }
867         if(c4 >= 0 && c4 < smallestteam_count)
868         {
869                 smallestteam = 4;
870                 smallestteam_count = c4;
871         }
872
873         if(!smallestteam)
874         {
875                 bprint("warning: no smallest team\n");
876                 return;
877         }
878
879         if(source_team == 1)
880                 steam = NUM_TEAM_1;
881         else if(source_team == 2)
882                 steam = NUM_TEAM_2;
883         else if(source_team == 3)
884                 steam = NUM_TEAM_3;
885         else // if(source_team == 4)
886                 steam = NUM_TEAM_4;
887
888         lowest_bot = world;
889         lowest_bot_score = 999999999;
890         lowest_player = world;
891         lowest_player_score = 999999999;
892
893         // find the lowest-scoring player & bot of that team
894         FOR_EACH_PLAYER(head)
895         {
896                 if(head.team == steam)
897                 {
898                         if(head.isbot)
899                         {
900                                 if(head.totalfrags < lowest_bot_score)
901                                 {
902                                         lowest_bot = head;
903                                         lowest_bot_score = head.totalfrags;
904                                 }
905                         }
906                         else
907                         {
908                                 if(head.totalfrags < lowest_player_score)
909                                 {
910                                         lowest_player = head;
911                                         lowest_player_score = head.totalfrags;
912                                 }
913                         }
914                 }
915         }
916
917         // prefers to move a bot...
918         if(lowest_bot != world)
919                 selected = lowest_bot;
920         // but it will move a player if it has to
921         else
922                 selected = lowest_player;
923         // don't do anything if it couldn't find anyone
924         if(!selected)
925         {
926                 bprint("warning: couldn't find a player to move from team\n");
927                 return;
928         }
929
930         // smallest team gains a member
931         if(smallestteam == 1)
932         {
933                 c1 = c1 + 1;
934         }
935         else if(smallestteam == 2)
936         {
937                 c2 = c2 + 1;
938         }
939         else if(smallestteam == 3)
940         {
941                 c3 = c3 + 1;
942         }
943         else if(smallestteam == 4)
944         {
945                 c4 = c4 + 1;
946         }
947         else
948         {
949                 bprint("warning: destination team invalid\n");
950                 return;
951         }
952         // source team loses a member
953         if(source_team == 1)
954         {
955                 c1 = c1 + 1;
956         }
957         else if(source_team == 2)
958         {
959                 c2 = c2 + 2;
960         }
961         else if(source_team == 3)
962         {
963                 c3 = c3 + 3;
964         }
965         else if(source_team == 4)
966         {
967                 c4 = c4 + 4;
968         }
969         else
970         {
971                 bprint("warning: source team invalid\n");
972                 return;
973         }
974
975         // move the player to the new team
976         TeamchangeFrags(selected);
977         SetPlayerTeam(selected, smallestteam, source_team, false);
978
979         if(selected.deadflag == DEAD_NO)
980                 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
981         Send_Notification(NOTIF_ONE, selected, MSG_CENTER, CENTER_DEATH_SELF_AUTOTEAMCHANGE, selected.team);
982 }