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