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