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