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