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