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