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