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