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