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