]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/teamplay.qc
g_balance_teams_complain to complain about team balance being off
[divverent/nexuiz.git] / data / qcsrc / server / teamplay.qc
1 float GAME_DEATHMATCH           = 1;
2 float GAME_TEAM_DEATHMATCH      = 2;
3 float GAME_DOMINATION           = 3;
4 float GAME_CTF                  = 4;
5 float GAME_RUNEMATCH            = 5;
6 float GAME_LMS                  = 6;
7 float GAME_ARENA                = 7;
8
9 // client counts for each team
10 float c1, c2, c3, c4;
11 // # of bots on those teams
12 float cb1, cb2, cb3, cb4;
13
14 float g_domination, g_ctf, g_tdm;
15
16 float audit_teams_time;
17
18 float() IsTeamBalanceForced = {
19         if(!cvar("teamplay"))
20                 return 0;
21         if(cvar("g_campaign"))
22                 return 0;
23         if(!cvar("g_balance_teams_force"))
24                 return -1;
25         return 1;
26 }
27
28 void TeamchangeFrags(entity e)
29 {
30         if(e.classname == "player")
31         {
32                 // reduce frags during a team change
33                 DistributeFragsAmongTeam(e, (e.clientcolors & 0x0F) + 1, 1 - cvar("g_changeteam_fragtransfer") / 100);
34         }
35 }
36
37 string TeamName(float t)
38 {
39         // fixme: Search for team entities and get their .netname's!
40         if(t == COLOR_TEAM1)
41                 return "Red Team";
42         if(t == COLOR_TEAM2)
43                 return "Blue Team";
44         if(t == COLOR_TEAM3)
45                 return "Pink Team";
46         if(t == COLOR_TEAM4)
47                 return "Yellow Team";
48         return "Neutral Team";
49 }
50 string ColoredTeamName(float t)
51 {
52         // fixme: Search for team entities and get their .netname's!
53         if(t == COLOR_TEAM1)
54                 return "^1Red Team^7";
55         if(t == COLOR_TEAM2)
56                 return "^4Blue Team^7";
57         if(t == COLOR_TEAM3)
58                 return "^6Pink Team^7";
59         if(t == COLOR_TEAM4)
60                 return "^3Yellow Team^7";
61         return "Neutral Team";
62 }
63 string TeamNoName(float t)
64 {
65         // fixme: Search for team entities and get their .netname's!
66         if(t == 1)
67                 return "Red Team";
68         if(t == 2)
69                 return "Blue Team";
70         if(t == 3)
71                 return "Pink Team";
72         if(t == 4)
73                 return "Yellow Team";
74         return "Neutral Team";
75 }
76
77 void dom_init();
78 void ctf_init();
79 void runematch_init();
80 void tdm_init();
81
82 void(entity pl) LogTeamchange =
83 {
84         string str;
85         if(!cvar("sv_eventlog"))
86                 return;
87         if(pl.playerid < 1)
88                 return;
89         str = strcat(":team:", ftos(pl.playerid), ":");
90         str = strcat(str, ftos(pl.team));
91         GameLogEcho(str, FALSE);
92 }
93
94 void ResetGameCvars()
95 {
96         cvar_set("g_tdm", "0");
97         cvar_set("g_domination", "0");
98         cvar_set("g_ctf", "0");
99         cvar_set("g_runematch", "0");
100         cvar_set("g_lms", "0");
101         cvar_set("g_arena", "0");
102         cvar_set("teamplay", "0");
103 }
104
105 void ActivateTeamplay()
106 {
107         float teamplay_default;
108         teamplay_default = cvar("teamplay_default");
109
110         if(teamplay_default)
111                 cvar_set("teamplay", ftos(teamplay_default));
112         else
113                 cvar_set("teamplay", "3");
114 }
115
116 void InitGameplayMode()
117 {
118         float fraglimit_override, timelimit_override;
119
120         VoteReset();
121
122         game = cvar ("gamecfg");        // load game options
123
124         // game cvars get reset before map changes
125         // then map's cfg sets them as desired
126
127         // FIXME: also set a message or game mode name to print to players when the join
128
129         // set both here, gamemode can override it later
130         timelimit_override = cvar("timelimit_override");
131         fraglimit_override = cvar("fraglimit_override");
132
133         if(game == GAME_DOMINATION || cvar("g_domination"))
134         {
135                 ResetGameCvars();
136                 game = GAME_DOMINATION;
137                 cvar_set("g_domination", "1");
138
139                 ActivateTeamplay();
140
141                 fraglimit_override = cvar("g_domination_point_limit");
142
143                 gamemode_name = "Domination";
144                 teams_matter = 1;
145         }
146         else if(game == GAME_CTF || cvar("g_ctf"))
147         {
148                 ResetGameCvars();
149                 game = GAME_CTF;
150                 cvar_set("g_ctf", "1");
151
152                 ActivateTeamplay();
153
154                 fraglimit_override = cvar("g_ctf_capture_limit");
155
156                 gamemode_name = "Capture the Flag";
157                 teams_matter = 1;
158         }
159         else if((game == GAME_RUNEMATCH || cvar("g_runematch")) && !cvar("g_minstagib"))
160         {
161                 game = GAME_RUNEMATCH;
162                 cvar_set("g_runematch", "1");
163
164                 if(cvar("deathmatch_force_teamplay"))
165                         ActivateTeamplay();
166
167                 fraglimit_override = cvar("g_runematch_point_limit");
168
169                 gamemode_name = "Rune Match";
170                 if(cvar("teamplay"))
171                         teams_matter = 1;
172                 else
173                         teams_matter = 0;
174         }
175         else if(game == GAME_DEATHMATCH || game == GAME_TEAM_DEATHMATCH || cvar("g_tdm"))
176         {
177                 if(!cvar("deathmatch"))
178                         cvar_set("deathmatch", "1");
179
180
181                 if(game == GAME_TEAM_DEATHMATCH || cvar("g_tdm") || cvar("deathmatch_force_teamplay"))
182                 {
183                         ResetGameCvars();
184                         game = GAME_TEAM_DEATHMATCH;
185                         gamemode_name = "Team Deathmatch";
186                         ActivateTeamplay();
187                         teams_matter = 1;
188                         cvar_set("g_tdm", "1");
189                 }
190                 else
191                 {
192                         ResetGameCvars();
193                         game = GAME_DEATHMATCH;
194                         gamemode_name = "Deathmatch";
195                         teams_matter = 0;
196                 }
197
198                 fraglimit_override = cvar("fraglimit_override");
199         }
200         else if(game == GAME_LMS || cvar("g_lms"))
201         {
202                 ResetGameCvars();
203                 game = GAME_LMS;
204                 cvar_set("g_lms", "1");
205                 fraglimit_override = cvar("g_lms_lives_override");
206                 if(fraglimit_override == 0)
207                         fraglimit_override = -1;
208                 gamemode_name = "Last Man Standing";
209                 teams_matter = 0;
210                 lms_lowest_lives = 999;
211         }
212         else if(game == GAME_ARENA || cvar("g_arena"))
213         {
214                 ResetGameCvars();
215                 game = GAME_ARENA;
216                 cvar_set("g_arena", "1");
217                 fraglimit_override = cvar("g_arena_point_limit");
218                 maxspawned = cvar("g_arena_maxspawned");
219                 if(maxspawned < 2)
220                         maxspawned = 2;
221                 arena_roundbased = cvar("g_arena_roundbased");
222                 gamemode_name = "Arena";
223                 teams_matter = 0;
224         }
225         else
226         {
227                 // we can only assume...
228                 ResetGameCvars();
229                 gamemode_name = "Deathmatch";
230                 teams_matter = 0;
231         }
232 /*      else if(game == GAME_TEAM_DEATHMATCH)
233         {
234                 if(!cvar("deathmatch"))
235                         cvar_set("deathmatch", "1");
236
237                 //if(!cvar("teamplay"))
238                 //      cvar_set("teamplay", "3");
239                 ActivateTeamplay();
240
241                 fraglimit_override = cvar("fraglimit_override");
242         }*/
243
244         // enforce the server's universal frag/time limits
245         if(!cvar("g_campaign"))
246         {
247                 if(fraglimit_override >= 0)
248                         cvar_set("fraglimit", ftos(fraglimit_override));
249                 if(timelimit_override >= 0)
250                         cvar_set("timelimit", ftos(timelimit_override));
251         }
252
253         if (game == GAME_DOMINATION)//cvar("g_domination"))
254                 dom_init();
255         else if (game == GAME_CTF)//cvar("g_ctf"))
256                 ctf_init();
257         else if (game == GAME_RUNEMATCH)//cvar("g_runematch"))
258                 runematch_init();
259         else if (game == GAME_TEAM_DEATHMATCH)//cvar("g_runematch"))
260                 tdm_init();
261
262         // those mutators rule each other out
263         if(cvar("g_minstagib"))
264         {
265                 cvar_set("g_instagib", "0");
266                 cvar_set("g_rocketarena", "0");
267         }
268         if(cvar("g_instagib"))
269         {
270                 cvar_set("g_minstagib", "0");
271                 cvar_set("g_rocketarena", "0");
272         }
273         if(cvar("g_rocketarena"))
274         {
275                 cvar_set("g_instagib", "0");
276                 cvar_set("g_minstagib", "0");
277         }
278
279         registercvar("_motd", "");
280         registercvar("_mutatormsg", "");
281         cvar_set("_motd", wordwrap(cvar_string("sv_motd"), 50));
282         cvar_set("_mutatormsg", wordwrap(cvar_string("g_mutatormsg"), 50));
283
284         g_domination = cvar("g_domination");
285         g_ctf = cvar("g_ctf");
286         g_tdm = cvar("g_tdm");
287 }
288
289 string GetClientVersionMessage(float v) {
290         local string versionmsg;
291         if (v == 1) {
292                 versionmsg = "^1client is too old to get versioninfo.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nUPDATE!!! (http://www.nexuiz.com)^8";
293                 // either that or someone wants to be funny
294         } else if (v != cvar("gameversion")) {
295                 if(v < cvar("gameversion")) {
296                         versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
297                 } else {
298                         versionmsg = "^3This server is using an outdated Nexuiz version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
299                 }
300         } else {
301                 versionmsg = "^2client version and server version are compatible.^8";
302         }
303         return strzone(versionmsg);
304
305 }
306
307
308 void PrintWelcomeMessage(entity pl)
309 {
310         string s, mutator, modifications, padding;
311
312         /*if(self.welcomemessage_time > time)
313                 return;
314         self.welcomemessage_time = time + 0.8; */
315
316         if(self.cvar_scr_centertime == 0) return;
317         if(self.welcomemessage_time > time) return;
318         self.welcomemessage_time = time + self.cvar_scr_centertime * 0.6;
319
320         if(cvar("g_campaign"))
321         {
322                 centerprint(pl, campaign_message);
323                 return;
324         }
325
326         if(self.classname == "observer")
327         {
328                 if(cvar("g_lms") && self.frags <= 0 && self.frags > -666)
329                         return centerprint(self, strcat(newlines, "^1You have no more lives left\nwait for next round\n\n\n^7press attack to spectate other players"));
330                 else if(cvar("g_lms") && self.frags == -666)
331                         return centerprint(self, strcat(newlines, "^1Match has already begun\nwait for next round\n\n\n^7press attack to spectate other players"));
332         }
333         else if(self.classname == "spectator")
334         {
335                 if ((cvar("g_lms") && self.frags < 1) || cvar("g_arena"))
336                         return centerprint(self, strcat(newlines, "spectating ", self.enemy.netname, "\n\n\n^7press attack for next player\npress attack2 for free fly mode"));
337                 else
338                         return centerprint(self, strcat(newlines, "spectating ", self.enemy.netname, "\n\n\n^7press jump to play\n^7press attack for next player\npress attack2 for free fly mode"));
339         }
340
341
342         if(cvar("g_minstagib"))
343                 mutator = "^2Minstagib ^1";
344         else if(cvar("g_instagib"))
345                 mutator = "^2Instagib ^1";
346         else if(cvar("g_rocketarena"))
347                 mutator = "^2Rocketarena ^1";
348         else if(cvar("g_nixnex"))
349                 mutator = "^2No Items Nexuiz ^1";
350
351         if(cvar("g_midair")) {
352                 // to protect against unheedingly made changes
353                 if (modifications) {
354                         modifications = strcat(modifications, ", ");
355                 }
356                 modifications = "midair";
357         }
358         if(cvar("g_vampire")) {
359                 if (modifications) {
360                         modifications = strcat(modifications, ", ");
361                 }
362                 modifications = strcat(modifications, "vampire");
363         }
364         if(cvar("g_laserguided_missile")) {
365                 if (modifications) {
366                         modifications = strcat(modifications, ", ");
367                 }
368                 modifications = strcat(modifications, "laser-guided-missiles");
369         }
370
371         local string versionmessage;
372         versionmessage = GetClientVersionMessage(self.version);
373
374         s = strcat(s, newlines, "This is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
375         s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
376
377         if(modifications != "")
378                 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
379
380         if((self.classname == "observer" || self.classname == "spectator") && self.version == cvar("gameversion")) {
381                 if(!cvar("g_arena"))
382                         s = strcat(s,"^7\n\n\npress jump to play\npress attack to spectate other players\n\n");
383                 else if(player_count < 2 && arena_roundbased)
384                 {
385                         s = strcat(s, "\n\n\n^1waiting for second player to start match^7\n\n");
386                 }
387                 else
388                 {
389                         s = strcat(s, "\n\n\n");
390                         if(champion)
391                                 s = strcat(s, "^7current champion is: ", champion.netname, "\n\n");
392                         s = strcat(s,"^7press attack to spectate other players\n\n");
393                 }
394         }
395
396
397         s = strzone(s);
398
399         if (cvar("g_grappling_hook"))
400                 s = strcat(s, "\n\n^8grappling hook is enabled, press 'e' to use it\n");
401
402         if (cvar_string("_mutatormsg") != "") {
403                 s = strcat(s, "\n\n^8special gameplay tips: ^7", cvar_string("_mutatormsg"));
404         }
405
406         if (cvar_string("_motd") != "") {
407                 s = strcat(s, "\n\n^8MOTD: ^7", cvar_string("_motd"));
408         }
409
410         s = strcat(s, "\n");
411         if(cvar("fraglimit"))
412         {
413                 padding = "";
414                 if(cvar("timelimit"))
415                         padding = "        ";
416                         //        " minutes"
417                 s = strcat(s, "\n^8frag limit: ^7", cvar_string("fraglimit"), padding);
418         }
419         if(cvar("timelimit"))
420                 s = strcat(s, "\n^8time limit: ^7", cvar_string("timelimit"), " minutes");
421
422         s = strzone(s);
423
424         centerprint(pl, s);
425         //sprint(pl, s);
426
427         strunzone(s);
428 }
429
430
431 void SetPlayerColors(entity pl, float _color)
432 {
433         /*string s;
434         s = ftos(cl);
435         stuffcmd(pl, strcat("color ", s, " ", s, "\n")  );
436         pl.team = cl + 1;
437         //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
438         pl.clientcolors = 16*cl + cl;*/
439
440         float pants, shirt;
441         pants = _color & 0x0F;
442         shirt = _color & 0xF0;
443
444
445         if(teamplay) {
446                 setcolor(pl, 16*pants + pants);
447         } else {
448                 setcolor(pl, shirt + pants);
449         }
450 }
451
452 void SetPlayerTeam(entity pl, float t, float s, float noprint)
453 {
454         float _color;
455
456         if(t == 4)
457                 _color = COLOR_TEAM4 - 1;
458         else if(t == 3)
459                 _color = COLOR_TEAM3 - 1;
460         else if(t == 2)
461                 _color = COLOR_TEAM2 - 1;
462         else
463                 _color = COLOR_TEAM1 - 1;
464
465         SetPlayerColors(pl,_color);
466
467         if(!noprint && t != s)
468         {
469                 //bprint(pl.netname, " has changed to ", TeamNoName(t), "\n");
470                 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
471         }
472
473         if(t != s)
474                 LogTeamchange(pl);
475 }
476
477
478
479
480
481
482 // set c1...c4 to show what teams are allowed
483 void CheckAllowedTeams ()
484 {
485         string teament_name;
486         float dm;
487         entity head;
488
489 //      if(!dom && !ctf)
490 //              dm = 1;
491
492         c1 = c2 = c3 = c4 = -1;
493         cb1 = cb2 = cb3 = cb4 = 0;
494
495         if(g_domination)
496                 teament_name = "dom_team";
497         else if(g_ctf)
498                 teament_name = "ctf_team";
499         else if(g_tdm)
500                 teament_name = "tdm_team";
501         else
502         {
503                 // cover anything else by treating it like tdm with no teams spawned
504                 dm = cvar("g_tdm_teams");
505                 if(dm < 2)
506                         error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
507
508                 if(dm >= 4)
509                 {
510                         c1 = c2 = c3 = c4 = 0;
511                 }
512                 else if(dm >= 3)
513                 {
514                         c1 = c2 = c3 = 0;
515                 }
516                 else// if(dm >= 2)
517                 {
518                         c1 = c2 = 0;
519                 }
520                 return;
521         }
522
523         // first find out what teams are allowed
524         head = find(world, classname, teament_name);
525         while(head)
526         {
527                 if(!(g_domination && head.netname == ""))
528                 {
529                         if(head.team == COLOR_TEAM1)
530                         {
531                                 c1 = 0;
532                         }
533                         if(head.team == COLOR_TEAM2)
534                         {
535                                 c2 = 0;
536                         }
537                         if(head.team == COLOR_TEAM3)
538                         {
539                                 c3 = 0;
540                         }
541                         if(head.team == COLOR_TEAM4)
542                         {
543                                 c4 = 0;
544                         }
545                 }
546                 head = find(head, classname, teament_name);
547         }
548 }
549
550 float PlayerValue(entity p)
551 {
552         if(IsTeamBalanceForced() == 1)
553                 return 1;
554         return 1;
555 }
556
557 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
558 // teams that are allowed will now have their player counts stored in c1...c4
559 void GetTeamCounts(entity ignore)
560 {
561         entity head;
562         float value;
563         // now count how many players are on each team already
564
565         // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
566         // also remember the lowest-scoring player
567
568         FOR_EACH_PLAYER(head)
569         {
570                 if(head != ignore)// && head.netname != "")
571                 {
572                         value = PlayerValue(head);
573                         if(head.team == COLOR_TEAM1)
574                         {
575                                 if(c1 >= 0)
576                                 {
577                                         c1 = c1 + value;
578                                         cb1 = cb1 + value;
579                                 }
580                         }
581                         if(head.team == COLOR_TEAM2)
582                         {
583                                 if(c2 >= 0)
584                                 {
585                                         c2 = c2 + value;
586                                         cb2 = cb2 + value;
587                                 }
588                         }
589                         if(head.team == COLOR_TEAM3)
590                         {
591                                 if(c3 >= 0)
592                                 {
593                                         c3 = c3 + value;
594                                         cb3 = cb3 + value;
595                                 }
596                         }
597                         if(head.team == COLOR_TEAM4)
598                         {
599                                 if(c4 >= 0)
600                                 {
601                                         c4 = c4 + value;
602                                         cb4 = cb4 + value;
603                                 }
604                         }
605                 }
606         }
607 }
608
609 // returns # of smallest team (1, 2, 3, 4)
610 // NOTE: Assumes CheckAllowedTeams has already been called!
611 float FindSmallestTeam(entity pl, float ignore_pl)
612 {
613         float totalteams, smallestteam, smallestteam_count, smallestteam_score, balance_type;
614         totalteams = 0;
615
616         // find out what teams are available
617         //CheckAllowedTeams();
618
619         // make sure there are at least 2 teams to join
620         if(c1 >= 0)
621                 totalteams = totalteams + 1;
622         if(c2 >= 0)
623                 totalteams = totalteams + 1;
624         if(c3 >= 0)
625                 totalteams = totalteams + 1;
626         if(c4 >= 0)
627                 totalteams = totalteams + 1;
628
629         if(totalteams <= 1)
630         {
631                 if(g_domination)
632                         error("Too few teams available for domination\n");
633                 else if(g_ctf)
634                         error("Too few teams available for ctf\n");
635                 else
636                         error("Too few teams available for team deathmatch\n");
637         }
638
639
640         // count how many players are in each team
641         if(ignore_pl)
642                 GetTeamCounts(pl);
643         else
644                 GetTeamCounts(world);
645
646         // c1...c4 now have counts of each team
647         // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
648
649         smallestteam = 0;
650         smallestteam_count = 999999999;
651         smallestteam_score = 999999999;
652
653         // 2 gives priority to what team you're already on, 1 goes in order
654         // 2 doesn't seem to work though...
655         balance_type = 1;
656
657         if(balance_type == 1)
658         {
659                 if(c1 >= 0 && (c1 < smallestteam_count || (c1 <= smallestteam_count && team1_score < smallestteam_score)))
660                 {
661                         smallestteam = 1;
662                         smallestteam_count = c1;
663                         smallestteam_score = team1_score;
664                 }
665                 if(c2 >= 0 && (c2 < smallestteam_count || (c2 <= smallestteam_count && team2_score < smallestteam_score)))
666                 {
667                         smallestteam = 2;
668                         smallestteam_count = c2;
669                         smallestteam_score = team2_score;
670                 }
671                 if(c3 >= 0 && (c3 < smallestteam_count || (c3 <= smallestteam_count && team3_score < smallestteam_score)))
672                 {
673                         smallestteam = 3;
674                         smallestteam_count = c3;
675                         smallestteam_score = team3_score;
676                 }
677                 if(c4 >= 0 && (c4 < smallestteam_count || (c4 <= smallestteam_count && team4_score < smallestteam_score)))
678                 {
679                         smallestteam = 4;
680                         smallestteam_count = c4;
681                         smallestteam_score = team4_score;
682                 }
683         }
684         else
685         {
686                 if(c1 >= 0 && (c1 < smallestteam_count ||
687                                         (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
688                 {
689                         smallestteam = 1;
690                         smallestteam_count = c1;
691                 }
692                 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
693                                         (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
694                 {
695                         smallestteam = 2;
696                         smallestteam_count = c2;
697                 }
698                 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
699                                         (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
700                 {
701                         smallestteam = 3;
702                         smallestteam_count = c3;
703                 }
704                 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
705                                         (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
706                 {
707                         smallestteam = 4;
708                         smallestteam_count = c4;
709                 }
710         }
711
712         return smallestteam;
713 }
714
715 float JoinBestTeam(entity pl, float only_return_best)
716 {
717         float smallest, selectedteam;
718
719         // don't join a team if we're not playing a team game
720         if(!cvar("teamplay") && !g_domination && !g_ctf)
721                 return 0;
722
723         // find out what teams are available
724         CheckAllowedTeams();
725
726         if(cvar("g_domination"))
727         {
728                 if(cvar("g_domination_default_teams") < 3)
729                         c3 = 999999999;
730                 if(cvar("g_domination_default_teams") < 4)
731                         c4 = 999999999;
732         }
733
734         // if we don't care what team he ends up on, put him on whatever team he entered as.
735         // if he's not on a valid team, then let other code put him on the smallest team
736         if(!cvar("g_campaign") && !cvar("g_balance_teams") && !cvar("g_balance_teams_force"))
737         {
738                 if(     c1 >= 0 && pl.team == COLOR_TEAM1)
739                         selectedteam = pl.team;
740                 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
741                         selectedteam = pl.team;
742                 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
743                         selectedteam = pl.team;
744                 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
745                         selectedteam = pl.team;
746                 else
747                         selectedteam = -1;
748                 if(selectedteam > 0)
749                 {
750                         if(!only_return_best)
751                         {
752                                 SetPlayerColors(pl, selectedteam - 1);
753                                 LogTeamchange(pl);
754                         }
755                         return selectedteam;
756                 }
757                 // otherwise end up on the smallest team (handled below)
758         }
759
760         smallest = FindSmallestTeam(pl, TRUE);
761
762
763         if(!only_return_best)
764         {
765                 TeamchangeFrags(self);
766                 if(smallest == 1)
767                 {
768                         SetPlayerColors(pl, COLOR_TEAM1 - 1);
769                 }
770                 else if(smallest == 2)
771                 {
772                         SetPlayerColors(pl, COLOR_TEAM2 - 1);
773                 }
774                 else if(smallest == 3)
775                 {
776                         SetPlayerColors(pl, COLOR_TEAM3 - 1);
777                 }
778                 else if(smallest == 4)
779                 {
780                         SetPlayerColors(pl, COLOR_TEAM4 - 1);
781                 }
782                 else
783                 {
784                         error("smallest team: invalid team\n");
785                 }
786                 LogTeamchange(pl);
787                 if(pl.deadflag == DEAD_NO)
788                         Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
789         }
790
791         return smallest;
792 }
793
794
795 void SV_ChangeTeam(float _color)
796 {
797         float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
798
799         // in normal deathmatch we can just apply the color and we're done
800         if(!cvar("teamplay")) {
801                 SetPlayerColors(self, _color);
802                 return;
803         }
804
805         scolor = self.clientcolors & 0x0F;
806         dcolor = _color & 0x0F;
807
808         if(scolor == COLOR_TEAM1 - 1)
809                 steam = 1;
810         else if(scolor == COLOR_TEAM2 - 1)
811                 steam = 2;
812         else if(scolor == COLOR_TEAM3 - 1)
813                 steam = 3;
814         else if(scolor == COLOR_TEAM4 - 1)
815                 steam = 4;
816         if(dcolor == COLOR_TEAM1 - 1)
817                 dteam = 1;
818         else if(dcolor == COLOR_TEAM2 - 1)
819                 dteam = 2;
820         else if(dcolor == COLOR_TEAM3 - 1)
821                 dteam = 3;
822         else if(dcolor == COLOR_TEAM4 - 1)
823                 dteam = 4;
824
825         // remap invalid teams in dom & ctf
826         /*
827         if(cvar("g_ctf") && dteam == 3)
828                 dteam = 2;
829         else if(cvar("g_ctf") && dteam == 4)
830                 dteam = 1;
831         else if((cvar("g_domination") && cvar("g_domination_default_teams") < 3) || (cvar("g_tdm") && cvar("g_tdm_teams") < 3))
832         {
833                 if(dteam == 3)
834                         dteam = 2;
835                 else if(dteam == 4)
836                         dteam = 1;
837         }
838         else if((cvar("g_domination") && cvar("g_domination_default_teams") < 4) || (cvar("g_tdm") && cvar("g_tdm_teams") < 4))
839         {
840                 if(dteam == 4)
841                         dteam = 1;
842         }
843         */
844
845         CheckAllowedTeams();
846         if(dteam == 3 && c3 < 0)
847                 dteam = 2;
848         if(dteam == 4 && c4 < 0)
849                 dteam = 1;
850
851         // not changing teams
852         if(scolor == dcolor)
853         {
854                 //bprint("same team change\n");
855                 SetPlayerTeam(self, dteam, steam, TRUE);
856                 return;
857         }
858
859         if(cvar("teamplay"))
860         {
861                 if(cvar("g_campaign") || cvar("g_changeteam_banned"))
862                 {
863                         sprint(self, "Team changes not allowed\n");
864                         return; // changing teams is not allowed
865                 }
866
867                 if(!cvar("g_campaign") && cvar("g_balance_teams_prevent_imbalance"))
868                 {
869                         // only allow changing to a smaller or equal size team
870
871                         // find out what teams are available
872                         CheckAllowedTeams();
873                         // count how many players on each team
874                         GetTeamCounts(world);
875
876                         // get desired team
877                         if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
878                         {
879                                 dcount = c1;
880                                 dbotcount = cb1;
881                         }
882                         else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
883                         {
884                                 dcount = c2;
885                                 dbotcount = cb2;
886                         }
887                         else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
888                         {
889                                 dcount = c3;
890                                 dbotcount = cb3;
891                         }
892                         else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
893                         {
894                                 dcount = c4;
895                                 dbotcount = cb4;
896                         }
897                         else
898                         {
899                                 sprint(self, "Cannot change to an invalid team\n");
900
901                                 return;
902                         }
903
904                         // get starting team
905                         if(steam == 1)//scolor == COLOR_TEAM1 - 1)
906                                 scount = c1;
907                         else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
908                                 scount = c2;
909                         else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
910                                 scount = c3;
911                         else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
912                                 scount = c4;
913
914                         if(scount) // started at a valid, nonempty team
915                         {
916                                 // check if we're trying to change to a larger team that doens't have bots to swap with
917                                 if(dcount >= scount && dbotcount <= 0)
918                                 {
919                                         sprint(self, "Cannot change to a larger team\n");
920                                         return; // can't change to a larger team
921                                 }
922                         }
923                 }
924         }
925
926 //      bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
927
928         if(cvar("teamplay") && self.classname == "player" && steam != dteam)
929         {
930                 // reduce frags during a team change
931                 TeamchangeFrags(self);
932         }
933
934         SetPlayerTeam(self, dteam, steam, FALSE);
935
936         if(cvar("teamplay") && self.classname == "player" && steam != dteam)
937         {
938                 // kill player when changing teams
939                 if(self.deadflag == DEAD_NO)
940                         Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
941         }
942 }
943
944 void ShufflePlayerOutOfTeam (float source_team)
945 {
946         float smallestteam, smallestteam_count, steam;
947         float lowest_bot_score, lowest_player_score;
948         entity head, lowest_bot, lowest_player, selected;
949         string m;
950
951         smallestteam = 0;
952         smallestteam_count = 999999999;
953
954         if(c1 >= 0 && c1 < smallestteam_count)
955         {
956                 smallestteam = 1;
957                 smallestteam_count = c1;
958         }
959         if(c2 >= 0 && c2 < smallestteam_count)
960         {
961                 smallestteam = 2;
962                 smallestteam_count = c2;
963         }
964         if(c3 >= 0 && c3 < smallestteam_count)
965         {
966                 smallestteam = 3;
967                 smallestteam_count = c3;
968         }
969         if(c4 >= 0 && c4 < smallestteam_count)
970         {
971                 smallestteam = 4;
972                 smallestteam_count = c4;
973         }
974
975         if(!smallestteam)
976         {
977                 bprint("warning: no smallest team\n");
978                 return;
979         }
980
981         if(source_team == 1)
982                 steam = COLOR_TEAM1;
983         else if(source_team == 2)
984                 steam = COLOR_TEAM2;
985         else if(source_team == 3)
986                 steam = COLOR_TEAM3;
987         else if(source_team == 4)
988                 steam = COLOR_TEAM4;
989
990         lowest_bot = world;
991         lowest_bot_score = 999999999;
992         lowest_player = world;
993         lowest_player_score = 999999999;
994
995         // find the lowest-scoring player & bot of that team
996         FOR_EACH_PLAYER(head)
997         {
998                 if(head.team == steam)
999                 {
1000                         if(head.isbot)
1001                         {
1002                                 if(head.frags < lowest_bot_score)
1003                                 {
1004                                         lowest_bot = head;
1005                                         lowest_bot_score = head.frags;
1006                                 }
1007                         }
1008                         else
1009                         {
1010                                 if(head.frags < lowest_player_score)
1011                                 {
1012                                         lowest_player = head;
1013                                         lowest_player_score = head.frags;
1014                                 }
1015                         }
1016                 }
1017         }
1018
1019         // prefers to move a bot...
1020         if(lowest_bot != world)
1021                 selected = lowest_bot;
1022         // but it will move a player if it has to
1023         else
1024                 selected = lowest_player;
1025         // don't do anything if it couldn't find anyone
1026         if(!selected)
1027         {
1028                 bprint("warning: couldn't find a player to move from team\n");
1029                 return;
1030         }
1031
1032         // smallest team gains a member
1033         if(smallestteam == 1)
1034         {
1035                 c1 = c1 + 1;
1036         }
1037         else if(smallestteam == 2)
1038         {
1039                 c2 = c2 + 1;
1040         }
1041         else if(smallestteam == 3)
1042         {
1043                 c3 = c3 + 1;
1044         }
1045         else if(smallestteam == 4)
1046         {
1047                 c4 = c4 + 1;
1048         }
1049         else
1050         {
1051                 bprint("warning: destination team invalid\n");
1052                 return;
1053         }
1054         // source team loses a member
1055         if(source_team == 1)
1056         {
1057                 c1 = c1 + 1;
1058         }
1059         else if(source_team == 2)
1060         {
1061                 c2 = c2 + 2;
1062         }
1063         else if(source_team == 3)
1064         {
1065                 c3 = c3 + 3;
1066         }
1067         else if(source_team == 4)
1068         {
1069                 c4 = c4 + 4;
1070         }
1071         else
1072         {
1073                 bprint("warning: source team invalid\n");
1074                 return;
1075         }
1076
1077         // move the player to the new team
1078         TeamchangeFrags(selected);
1079         SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1080
1081         if(selected.deadflag == DEAD_NO)
1082                         Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1083         m = "You have been moved into a different team to improve team balance\nYou are now on: ";
1084         if (selected.team == 5)
1085                 m = strcat(m, "^1Red Team");
1086         else if (selected.team == 14)
1087                 m = strcat(m, "^4Blue Team");
1088         else if (selected.team == 10)
1089                 m = strcat(m, "^6Pink Team");
1090         else if (selected.team == 13)
1091                 m = strcat(m, "^3Yellow Team");
1092         centerprint(selected, m);
1093 }
1094
1095 float lastRebalanceInfo;
1096 void CauseRebalance(float source_team, float howmany_toomany)
1097 {
1098         float steam;
1099         entity head;
1100
1101         if(IsTeamBalanceForced() == 1)
1102         {
1103                 bprint("Rebalancing Teams\n");
1104                 ShufflePlayerOutOfTeam(source_team);
1105         }
1106         else
1107         {
1108                 if(1+floor(howmany_toomany) < cvar("g_balance_teams_complain"))
1109                         return;
1110                 if(time < lastRebalanceInfo + 90)
1111                         return;
1112                 lastRebalanceInfo = time;
1113                 if(source_team == 1)
1114                         steam = COLOR_TEAM1;
1115                 else if(source_team == 2)
1116                         steam = COLOR_TEAM2;
1117                 else if(source_team == 3)
1118                         steam = COLOR_TEAM3;
1119                 else if(source_team == 4)
1120                         steam = COLOR_TEAM4;
1121                 ServerConsoleEcho(strcat("Team ", ftos(source_team), " too large, complaining."), TRUE);
1122                 FOR_EACH_REALPLAYER(head)
1123                 {
1124                         if(head.team == steam)
1125                         {
1126                                 sprint(head, "\{1}\{13}^3SERVER NOTICE:^7 One of you please change teams!\n");
1127                                 centerprint_atprio(head, CENTERPRIO_REBALANCE, "^3SERVER NOTICE:\n\n^7Someone of you please change teams!");
1128                         }
1129                 }
1130         }
1131 }
1132
1133 // part of g_balance_teams_force
1134 // occasionally perform an audit of the teams to make
1135 // sure they're more or less balanced in player count.
1136 void AuditTeams()
1137 {
1138         float numplayers, numteams, toomany;
1139         float balance;
1140         balance = IsTeamBalanceForced();
1141         if(balance == 0)
1142                 return;
1143
1144         if(audit_teams_time > time)
1145                 return;
1146
1147         audit_teams_time = time + 4 + random();
1148
1149 //      bprint("Auditing teams\n");
1150
1151         CheckAllowedTeams();
1152         GetTeamCounts(world);
1153
1154
1155         numteams = numplayers = 0;
1156         if(c1 >= 0)
1157         {
1158                 numteams = numteams + 1;
1159                 numplayers = numplayers + c1;
1160         }
1161         if(c2 >= 0)
1162         {
1163                 numteams = numteams + 1;
1164                 numplayers = numplayers + c2;
1165         }
1166         if(c3 >= 0)
1167         {
1168                 numteams = numteams + 1;
1169                 numplayers = numplayers + c3;
1170         }
1171         if(c4 >= 0)
1172         {
1173                 numteams = numteams + 1;
1174                 numplayers = numplayers + c4;
1175         }
1176
1177         if(numplayers <= 0)
1178                 return; // no players to move around
1179         if(numteams < 2)
1180                 return; // don't bother shuffling if for some reason there aren't any teams
1181
1182         toomany = (numplayers / numteams) + 1;
1183
1184         if(toomany <= 0)
1185                 return; // that's weird...
1186
1187         if(c1 && c1 >= toomany)
1188                 CauseRebalance(1, c1 - toomany);
1189         if(c2 && c2 >= toomany)
1190                 CauseRebalance(2, c2 - toomany);
1191         if(c3 && c3 >= toomany)
1192                 CauseRebalance(3, c3 - toomany);
1193         if(c4 && c4 >= toomany)
1194                 CauseRebalance(4, c4 - toomany);
1195
1196         // if teams are still unbalanced, balance them further in the next audit,
1197         // which will happen sooner (keep doing rapid audits until things are in order)
1198         audit_teams_time = time + 0.7 + random()*0.3;
1199 }
1200
1201
1202
1203 /*void(entity e, float first) UpdateTeamScore =
1204 {
1205         clientno = e.FIXME;
1206         if(first)
1207         {
1208                 WriteByte (MSG_ALL, SVC_UPDATENAME);
1209                 WriteByte (MSG_ALL, clientno);
1210                 WriteString (MSG_ALL, e.netname);
1211
1212                 WriteByte (MSG_ALL, SVC_UPDATECOLORS);
1213                 WriteByte (MSG_ALL, clientno);
1214                 WriteByte (MSG_ALL, e.b_shirt * 16 + who.b_pants);
1215         }
1216
1217         WriteByte (MSG_ALL, SVC_UPDATEFRAGS);
1218         WriteByte (MSG_ALL, clientno);
1219         WriteShort (MSG_ALL, e.frags + 10000);
1220 };
1221
1222 */
1223
1224
1225 // code from here on is just to support maps that don't have team entities
1226 void tdm_spawnteam (string teamname, float teamcolor)
1227 {
1228         local entity e;
1229         e = spawn();
1230         e.classname = "tdm_team";
1231         e.netname = teamname;
1232         e.cnt = teamcolor;
1233         e.team = e.cnt + 1;
1234 };
1235
1236 // spawn some default teams if the map is not set up for tdm
1237 void() tdm_spawnteams =
1238 {
1239         float numteams;
1240
1241         numteams = cvar("g_tdm_teams");
1242
1243         tdm_spawnteam("Red", 4);
1244         tdm_spawnteam("Blue", 13);
1245 };
1246
1247 void() tdm_delayedinit =
1248 {
1249         self.think = SUB_Remove;
1250         self.nextthink = time;
1251         // if no teams are found, spawn defaults
1252         if (find(world, classname, "tdm_team") == world)
1253                 tdm_spawnteams();
1254 };
1255
1256 void() tdm_init =
1257 {
1258         local entity e;
1259         e = spawn();
1260         e.think = tdm_delayedinit;
1261         e.nextthink = time + 0.1;
1262 };
1263
1264