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