]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/teamplay.qc
only respawn/exit intermission using attack, jump, attack2, hook, use keys
[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         if(self.welcomemessage_time2 > time)
306                 return;
307         self.welcomemessage_time2 = time + 0.8; */
308
309         if(self.classname == "observer")
310         {
311                 if(cvar("g_lms") && self.frags <= 0 && self.frags > -666)
312                         return centerprint(self, strcat(newlines, "^1You have no more lives left\nwait for next round\n\n\n^7press attack to spectate other players"));
313                 else if(cvar("g_lms") && self.frags == -666)
314                         return centerprint(self, strcat(newlines, "^1Match has already begun\nwait for next round\n\n\n^7press attack to spectate other players"));
315         }
316         else if(self.classname == "spectator")
317         {
318                 if ((cvar("g_lms") && self.frags < 1) || cvar("g_arena"))
319                         return centerprint(self, strcat(newlines, "spectating ", self.enemy.netname, "\n\n\n^7press attack for next player\npress attack2 for free fly mode"));
320                 else
321                         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"));
322         }
323
324
325         if(self.welcomemessage_time2 > time) return;
326         self.welcomemessage_time2 = time + 1.0;
327
328         if(cvar("g_campaign"))
329         {
330                 centerprint(pl, campaign_message);
331                 return;
332         }
333
334         if(cvar("g_minstagib"))
335                 mutator = "^2Minstagib ^1";
336         else if(cvar("g_instagib"))
337                 mutator = "^2Instagib ^1";
338         else if(cvar("g_rocketarena"))
339                 mutator = "^2Rocketarena ^1";
340         else if(cvar("g_nixnex"))
341                 mutator = "^2No Items Nexuiz ^1";
342
343         if(cvar("g_midair")) {
344                 // to protect against unheedingly made changes
345                 if (modifications) {
346                         modifications = strcat(modifications, ", ");
347                 }
348                 modifications = "midair";
349         }
350         if(cvar("g_vampire")) {
351                 if (modifications) {
352                         modifications = strcat(modifications, ", ");
353                 }
354                 modifications = strcat(modifications, "vampire");
355         }
356         if(cvar("g_laserguided_missile")) {
357                 if (modifications) {
358                         modifications = strcat(modifications, ", ");
359                 }
360                 modifications = strcat(modifications, "laser-guided-missiles");
361         }
362
363         local string versionmessage;
364         versionmessage = GetClientVersionMessage(self.version);
365
366         s = strcat(s, newlines, "This is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
367         s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
368
369         if(modifications != "")
370                 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
371
372         if((self.classname == "observer" || self.classname == "spectator") && self.version == cvar("gameversion")) {
373                 if(!cvar("g_arena"))
374                         s = strcat(s,"^7\n\n\npress jump to play\npress attack to spectate other players\n\n");
375                 else if(player_count < 2 && arena_roundbased)
376                 {
377                         s = strcat(s, "\n\n\n^1waiting for second player to start match^7\n\n");
378                 }
379                 else
380                 {
381                         s = strcat(s, "\n\n\n");
382                         if(champion)
383                                 s = strcat(s, "^7current champion is: ", champion.netname, "\n\n");
384                         s = strcat(s,"^7press attack to spectate other players\n\n");
385                 }
386         }
387
388
389         s = strzone(s);
390
391         if (cvar("g_grappling_hook"))
392                 s = strcat(s, "\n\n^8grappling hook is enabled, press 'e' to use it\n");
393
394         if (cvar_string("_mutatormsg") != "") {
395                 s = strcat(s, "\n\n^8special gameplay tips: ^7", cvar_string("_mutatormsg"));
396         }
397
398         if (cvar_string("_motd") != "") {
399                 s = strcat(s, "\n\n^8MOTD: ^7", cvar_string("_motd"));
400         }
401
402         s = strcat(s, "\n");
403         if(cvar("fraglimit"))
404         {
405                 padding = "";
406                 if(cvar("timelimit"))
407                         padding = "        ";
408                         //        " minutes"
409                 s = strcat(s, "\n^8frag limit: ^7", cvar_string("fraglimit"), padding);
410         }
411         if(cvar("timelimit"))
412                 s = strcat(s, "\n^8time limit: ^7", cvar_string("timelimit"), " minutes");
413
414         s = strzone(s);
415
416         centerprint(pl, s);
417         //sprint(pl, s);
418
419         strunzone(s);
420 }
421
422
423 void SetPlayerColors(entity pl, float _color)
424 {
425         /*string s;
426         s = ftos(cl);
427         stuffcmd(pl, strcat("color ", s, " ", s, "\n")  );
428         pl.team = cl + 1;
429         //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
430         pl.clientcolors = 16*cl + cl;*/
431
432         float pants, shirt;
433         pants = _color & 0x0F;
434         shirt = _color & 0xF0;
435
436
437         if(teamplay) {
438                 setcolor(pl, 16*pants + pants);
439         } else {
440                 setcolor(pl, shirt + pants);
441         }
442 }
443
444 void SetPlayerTeam(entity pl, float t, float s, float noprint)
445 {
446         float _color;
447
448         if(t == 4)
449                 _color = COLOR_TEAM4 - 1;
450         else if(t == 3)
451                 _color = COLOR_TEAM3 - 1;
452         else if(t == 2)
453                 _color = COLOR_TEAM2 - 1;
454         else
455                 _color = COLOR_TEAM1 - 1;
456
457         SetPlayerColors(pl,_color);
458
459         if(!noprint && t != s)
460         {
461                 //bprint(strcat(pl.netname, " has changed to ", TeamNoName(t), "\n"));
462                 bprint(strcat(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n"));
463         }
464
465         if(t != s)
466                 LogTeamchange(pl);
467 }
468
469
470
471
472
473
474 // set c1...c4 to show what teams are allowed
475 void CheckAllowedTeams ()
476 {
477         string teament_name;
478         float dm;
479         entity head;
480
481 //      if(!dom && !ctf)
482 //              dm = 1;
483
484         c1 = c2 = c3 = c4 = -1;
485         cb1 = cb2 = cb3 = cb4 = 0;
486
487         if(g_domination)
488                 teament_name = "dom_team";
489         else if(g_ctf)
490                 teament_name = "ctf_team";
491         else if(g_tdm)
492                 teament_name = "tdm_team";
493         else
494         {
495                 // cover anything else by treating it like tdm with no teams spawned
496                 dm = cvar("g_tdm_teams");
497                 if(dm < 2)
498                         error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
499
500                 if(dm >= 4)
501                 {
502                         c1 = c2 = c3 = c4 = 0;
503                 }
504                 else if(dm >= 3)
505                 {
506                         c1 = c2 = c3 = 0;
507                 }
508                 else// if(dm >= 2)
509                 {
510                         c1 = c2 = 0;
511                 }
512                 return;
513         }
514
515         // first find out what teams are allowed
516         head = find(world, classname, teament_name);
517         while(head)
518         {
519                 if(!(g_domination && head.netname == ""))
520                 {
521                         if(head.team == COLOR_TEAM1)
522                         {
523                                 c1 = 0;
524                         }
525                         if(head.team == COLOR_TEAM2)
526                         {
527                                 c2 = 0;
528                         }
529                         if(head.team == COLOR_TEAM3)
530                         {
531                                 c3 = 0;
532                         }
533                         if(head.team == COLOR_TEAM4)
534                         {
535                                 c4 = 0;
536                         }
537                 }
538                 head = find(head, classname, teament_name);
539         }
540 }
541
542 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
543 // teams that are allowed will now have their player counts stored in c1...c4
544 void GetTeamCounts(entity ignore)
545 {
546         entity head;
547         // now count how many players are on each team already
548
549         // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
550         // also remember the lowest-scoring player
551
552         head = find(world, classname, "player");
553         while(head)
554         {
555                 if(head != ignore)// && head.netname != "")
556                 {
557                         if(head.team == COLOR_TEAM1)
558                         {
559                                 if(c1 >= 0)
560                                 {
561                                         c1 = c1 + 1;
562                                         cb1 = cb1 + 1;
563                                 }
564                         }
565                         if(head.team == COLOR_TEAM2)
566                         {
567                                 if(c2 >= 0)
568                                 {
569                                         c2 = c2 + 1;
570                                         cb2 = cb2 + 1;
571                                 }
572                         }
573                         if(head.team == COLOR_TEAM3)
574                         {
575                                 if(c3 >= 0)
576                                 {
577                                         c3 = c3 + 1;
578                                         cb3 = cb3 + 1;
579                                 }
580                         }
581                         if(head.team == COLOR_TEAM4)
582                         {
583                                 if(c4 >= 0)
584                                 {
585                                         c4 = c4 + 1;
586                                         cb4 = cb4 + 1;
587                                 }
588                         }
589                 }
590                 head = find(head, classname, "player");
591         }
592 }
593
594 // returns # of smallest team (1, 2, 3, 4)
595 // NOTE: Assumes CheckAllowedTeams has already been called!
596 float FindSmallestTeam(entity pl, float ignore_pl)
597 {
598         float totalteams, smallestteam, smallestteam_count, smallestteam_score, balance_type;
599         totalteams = 0;
600
601         // find out what teams are available
602         //CheckAllowedTeams();
603
604         // make sure there are at least 2 teams to join
605         if(c1 >= 0)
606                 totalteams = totalteams + 1;
607         if(c2 >= 0)
608                 totalteams = totalteams + 1;
609         if(c3 >= 0)
610                 totalteams = totalteams + 1;
611         if(c4 >= 0)
612                 totalteams = totalteams + 1;
613
614         if(totalteams <= 1)
615         {
616                 if(g_domination)
617                         error("Too few teams available for domination\n");
618                 else if(g_ctf)
619                         error("Too few teams available for ctf\n");
620                 else
621                         error("Too few teams available for team deathmatch\n");
622         }
623
624
625         // count how many players are in each team
626         if(ignore_pl)
627                 GetTeamCounts(pl);
628         else
629                 GetTeamCounts(world);
630
631         // c1...c4 now have counts of each team
632         // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
633
634         smallestteam = 0;
635         smallestteam_count = 999999999;
636         smallestteam_score = 999999999;
637
638         // 2 gives priority to what team you're already on, 1 goes in order
639         // 2 doesn't seem to work though...
640         balance_type = 1;
641
642         if(balance_type == 1)
643         {
644                 if(c1 >= 0 && (c1 < smallestteam_count || (c1 <= smallestteam_count && team1_score < smallestteam_score)))
645                 {
646                         smallestteam = 1;
647                         smallestteam_count = c1;
648                         smallestteam_score = team1_score;
649                 }
650                 if(c2 >= 0 && (c2 < smallestteam_count || (c2 <= smallestteam_count && team2_score < smallestteam_score)))
651                 {
652                         smallestteam = 2;
653                         smallestteam_count = c2;
654                         smallestteam_score = team2_score;
655                 }
656                 if(c3 >= 0 && (c3 < smallestteam_count || (c3 <= smallestteam_count && team3_score < smallestteam_score)))
657                 {
658                         smallestteam = 3;
659                         smallestteam_count = c3;
660                         smallestteam_score = team3_score;
661                 }
662                 if(c4 >= 0 && (c4 < smallestteam_count || (c4 <= smallestteam_count && team4_score < smallestteam_score)))
663                 {
664                         smallestteam = 4;
665                         smallestteam_count = c4;
666                         smallestteam_score = team4_score;
667                 }
668         }
669         else
670         {
671                 if(c1 >= 0 && (c1 < smallestteam_count ||
672                                         (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
673                 {
674                         smallestteam = 1;
675                         smallestteam_count = c1;
676                 }
677                 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
678                                         (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
679                 {
680                         smallestteam = 2;
681                         smallestteam_count = c2;
682                 }
683                 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
684                                         (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
685                 {
686                         smallestteam = 3;
687                         smallestteam_count = c3;
688                 }
689                 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
690                                         (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
691                 {
692                         smallestteam = 4;
693                         smallestteam_count = c4;
694                 }
695         }
696
697         return smallestteam;
698 }
699
700 float JoinBestTeam(entity pl, float only_return_best)
701 {
702         float smallest, selectedteam;
703
704         // don't join a team if we're not playing a team game
705         if(!cvar("teamplay") && !g_domination && !g_ctf)
706                 return 0;
707
708         // find out what teams are available
709         CheckAllowedTeams();
710
711         if(cvar("g_domination"))
712         {
713                 if(cvar("g_domination_default_teams") < 3)
714                         c3 = 999999999;
715                 if(cvar("g_domination_default_teams") < 4)
716                         c4 = 999999999;
717         }
718
719         // if we don't care what team he ends up on, put him on whatever team he entered as.
720         // if he's not on a valid team, then let other code put him on the smallest team
721         if(!cvar("g_campaign") && !cvar("g_balance_teams") && !cvar("g_balance_teams_force"))
722         {
723                 if(     c1 >= 0 && pl.team == COLOR_TEAM1)
724                         selectedteam = pl.team;
725                 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
726                         selectedteam = pl.team;
727                 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
728                         selectedteam = pl.team;
729                 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
730                         selectedteam = pl.team;
731                 else
732                         selectedteam = -1;
733                 if(selectedteam > 0)
734                 {
735                         if(!only_return_best)
736                         {
737                                 SetPlayerColors(pl, selectedteam - 1);
738                                 LogTeamchange(pl);
739                         }
740                         return selectedteam;
741                 }
742                 // otherwise end up on the smallest team (handled below)
743         }
744
745         smallest = FindSmallestTeam(pl, TRUE);
746
747
748         if(!only_return_best)
749         {
750                 TeamchangeFrags(self);
751                 if(smallest == 1)
752                 {
753                         SetPlayerColors(pl, COLOR_TEAM1 - 1);
754                 }
755                 else if(smallest == 2)
756                 {
757                         SetPlayerColors(pl, COLOR_TEAM2 - 1);
758                 }
759                 else if(smallest == 3)
760                 {
761                         SetPlayerColors(pl, COLOR_TEAM3 - 1);
762                 }
763                 else if(smallest == 4)
764                 {
765                         SetPlayerColors(pl, COLOR_TEAM4 - 1);
766                 }
767                 else
768                 {
769                         error("smallest team: invalid team\n");
770                 }
771                 LogTeamchange(pl);
772                 if(pl.deadflag == DEAD_NO)
773                         Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
774         }
775
776         return smallest;
777 }
778
779
780 void SV_ChangeTeam(float _color)
781 {
782         float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
783
784         // in normal deathmatch we can just apply the color and we're done
785         if(!cvar("teamplay")) {
786                 SetPlayerColors(self, _color);
787                 return;
788         }
789
790         scolor = self.clientcolors & 0x0F;
791         dcolor = _color & 0x0F;
792
793         if(scolor == COLOR_TEAM1 - 1)
794                 steam = 1;
795         else if(scolor == COLOR_TEAM2 - 1)
796                 steam = 2;
797         else if(scolor == COLOR_TEAM3 - 1)
798                 steam = 3;
799         else if(scolor == COLOR_TEAM4 - 1)
800                 steam = 4;
801         if(dcolor == COLOR_TEAM1 - 1)
802                 dteam = 1;
803         else if(dcolor == COLOR_TEAM2 - 1)
804                 dteam = 2;
805         else if(dcolor == COLOR_TEAM3 - 1)
806                 dteam = 3;
807         else if(dcolor == COLOR_TEAM4 - 1)
808                 dteam = 4;
809
810         // remap invalid teams in dom & ctf
811         /*
812         if(cvar("g_ctf") && dteam == 3)
813                 dteam = 2;
814         else if(cvar("g_ctf") && dteam == 4)
815                 dteam = 1;
816         else if((cvar("g_domination") && cvar("g_domination_default_teams") < 3) || (cvar("g_tdm") && cvar("g_tdm_teams") < 3))
817         {
818                 if(dteam == 3)
819                         dteam = 2;
820                 else if(dteam == 4)
821                         dteam = 1;
822         }
823         else if((cvar("g_domination") && cvar("g_domination_default_teams") < 4) || (cvar("g_tdm") && cvar("g_tdm_teams") < 4))
824         {
825                 if(dteam == 4)
826                         dteam = 1;
827         }
828         */
829
830         CheckAllowedTeams();
831         if(dteam == 3 && c3 < 0)
832                 dteam = 2;
833         if(dteam == 4 && c4 < 0)
834                 dteam = 1;
835
836         // not changing teams
837         if(scolor == dcolor)
838         {
839                 //bprint("same team change\n");
840                 SetPlayerTeam(self, dteam, steam, TRUE);
841                 return;
842         }
843
844         if(cvar("teamplay"))
845         {
846                 if(cvar("g_campaign") || cvar("g_changeteam_banned"))
847                 {
848                         sprint(self, "Team changes not allowed\n");
849                         return; // changing teams is not allowed
850                 }
851
852                 if(!cvar("g_campaign") && cvar("g_balance_teams_prevent_imbalance"))
853                 {
854                         // only allow changing to a smaller or equal size team
855
856                         // find out what teams are available
857                         CheckAllowedTeams();
858                         // count how many players on each team
859                         GetTeamCounts(world);
860
861                         // get desired team
862                         if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
863                         {
864                                 dcount = c1;
865                                 dbotcount = cb1;
866                         }
867                         else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
868                         {
869                                 dcount = c2;
870                                 dbotcount = cb2;
871                         }
872                         else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
873                         {
874                                 dcount = c3;
875                                 dbotcount = cb3;
876                         }
877                         else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
878                         {
879                                 dcount = c4;
880                                 dbotcount = cb4;
881                         }
882                         else
883                         {
884                                 sprint(self, "Cannot change to an invalid team\n");
885
886                                 return;
887                         }
888
889                         // get starting team
890                         if(steam == 1)//scolor == COLOR_TEAM1 - 1)
891                                 scount = c1;
892                         else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
893                                 scount = c2;
894                         else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
895                                 scount = c3;
896                         else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
897                                 scount = c4;
898
899                         if(scount) // started at a valid, nonempty team
900                         {
901                                 // check if we're trying to change to a larger team that doens't have bots to swap with
902                                 if(dcount >= scount && dbotcount <= 0)
903                                 {
904                                         sprint(self, "Cannot change to a larger team\n");
905                                         return; // can't change to a larger team
906                                 }
907                         }
908                 }
909         }
910
911 //      bprint(strcat("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n"));
912
913         if(cvar("teamplay") && self.classname == "player" && steam != dteam)
914         {
915                 // reduce frags during a team change
916                 TeamchangeFrags(self);
917         }
918
919         SetPlayerTeam(self, dteam, steam, FALSE);
920
921         if(cvar("teamplay") && self.classname == "player" && steam != dteam)
922         {
923                 // kill player when changing teams
924                 if(self.deadflag == DEAD_NO)
925                         Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
926         }
927 }
928
929 void ShufflePlayerOutOfTeam (float source_team)
930 {
931         float smallestteam, smallestteam_count, steam;
932         float lowest_bot_score, lowest_player_score;
933         entity head, lowest_bot, lowest_player, selected;
934         string m;
935
936         smallestteam = 0;
937         smallestteam_count = 999999999;
938
939         if(c1 >= 0 && c1 < smallestteam_count)
940         {
941                 smallestteam = 1;
942                 smallestteam_count = c1;
943         }
944         if(c2 >= 0 && c2 < smallestteam_count)
945         {
946                 smallestteam = 2;
947                 smallestteam_count = c2;
948         }
949         if(c3 >= 0 && c3 < smallestteam_count)
950         {
951                 smallestteam = 3;
952                 smallestteam_count = c3;
953         }
954         if(c4 >= 0 && c4 < smallestteam_count)
955         {
956                 smallestteam = 4;
957                 smallestteam_count = c4;
958         }
959
960         if(!smallestteam)
961         {
962                 bprint("warning: no smallest team\n");
963                 return;
964         }
965
966         if(source_team == 1)
967                 steam = COLOR_TEAM1;
968         else if(source_team == 2)
969                 steam = COLOR_TEAM2;
970         else if(source_team == 3)
971                 steam = COLOR_TEAM3;
972         else if(source_team == 4)
973                 steam = COLOR_TEAM4;
974
975         lowest_bot = world;
976         lowest_bot_score = 999999999;
977         lowest_player = world;
978         lowest_player_score = 999999999;
979
980         // find the lowest-scoring player & bot of that team
981         head = find(world, classname, "player");
982         while(head)
983         {
984                 if(head.team == steam)
985                 {
986                         if(head.isbot)
987                         {
988                                 if(head.frags < lowest_bot_score)
989                                 {
990                                         lowest_bot = head;
991                                         lowest_bot_score = head.frags;
992                                 }
993                         }
994                         else
995                         {
996                                 if(head.frags < lowest_player_score)
997                                 {
998                                         lowest_player = head;
999                                         lowest_player_score = head.frags;
1000                                 }
1001                         }
1002                 }
1003                 head = find(head, classname, "player");
1004         }
1005
1006         // prefers to move a bot...
1007         if(lowest_bot != world)
1008                 selected = lowest_bot;
1009         // but it will move a player if it has to
1010         else
1011                 selected = lowest_player;
1012         // don't do anything if it couldn't find anyone
1013         if(!selected)
1014         {
1015                 bprint("warning: couldn't find a player to move from team\n");
1016                 return;
1017         }
1018
1019         // smallest team gains a member
1020         if(smallestteam == 1)
1021         {
1022                 c1 = c1 + 1;
1023         }
1024         else if(smallestteam == 2)
1025         {
1026                 c2 = c2 + 1;
1027         }
1028         else if(smallestteam == 3)
1029         {
1030                 c3 = c3 + 1;
1031         }
1032         else if(smallestteam == 4)
1033         {
1034                 c4 = c4 + 1;
1035         }
1036         else
1037         {
1038                 bprint("warning: destination team invalid\n");
1039                 return;
1040         }
1041         // source team loses a member
1042         if(source_team == 1)
1043         {
1044                 c1 = c1 + 1;
1045         }
1046         else if(source_team == 2)
1047         {
1048                 c2 = c2 + 2;
1049         }
1050         else if(source_team == 3)
1051         {
1052                 c3 = c3 + 3;
1053         }
1054         else if(source_team == 4)
1055         {
1056                 c4 = c4 + 4;
1057         }
1058         else
1059         {
1060                 bprint("warning: source team invalid\n");
1061                 return;
1062         }
1063
1064         // move the player to the new team
1065         TeamchangeFrags(selected);
1066         SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1067
1068         if(selected.deadflag == DEAD_NO)
1069                         Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1070         m = "You have been moved into a different team to improve team balance\nYou are now on: ";
1071         if (selected.team == 5)
1072                 m = strcat(m, "^1Red Team");
1073         else if (selected.team == 14)
1074                 m = strcat(m, "^4Blue Team");
1075         else if (selected.team == 10)
1076                 m = strcat(m, "^6Pink Team");
1077         else if (selected.team == 13)
1078                 m = strcat(m, "^3Yellow Team");
1079         centerprint(selected, m);
1080 }
1081
1082 // part of g_balance_teams_force
1083 // occasionally perform an audit of the teams to make
1084 // sure they're more or less balanced in player count.
1085 void AuditTeams()
1086 {
1087         float numplayers, numteams, average;
1088         if(cvar("g_campaign"))
1089                 return;
1090         if(!cvar("g_balance_teams_force"))
1091                 return;
1092         if(!cvar("teamplay"))
1093                 return;
1094
1095         if(audit_teams_time > time)
1096                 return;
1097
1098         audit_teams_time = time + 4 + random();
1099
1100 //      bprint("Auditing teams\n");
1101
1102         CheckAllowedTeams();
1103         GetTeamCounts(world);
1104
1105
1106         numteams = numplayers = 0;
1107         if(c1 >= 0)
1108         {
1109                 numteams = numteams + 1;
1110                 numplayers = numplayers + c1;
1111         }
1112         if(c2 >= 0)
1113         {
1114                 numteams = numteams + 1;
1115                 numplayers = numplayers + c2;
1116         }
1117         if(c3 >= 0)
1118         {
1119                 numteams = numteams + 1;
1120                 numplayers = numplayers + c3;
1121         }
1122         if(c4 >= 0)
1123         {
1124                 numteams = numteams + 1;
1125                 numplayers = numplayers + c4;
1126         }
1127
1128         if(numplayers <= 0)
1129                 return; // no players to move around
1130         if(numteams < 2)
1131                 return; // don't bother shuffling if for some reason there aren't any teams
1132
1133         average = ceil(numplayers / numteams);
1134
1135         if(average <= 0)
1136                 return; // that's weird...
1137
1138         if(c1 && c1 > average)
1139         {
1140                 bprint("Rebalancing Teams\n");
1141                 //bprint("Shuffle from team 1\n");
1142                 ShufflePlayerOutOfTeam(1);
1143         }
1144         if(c2 && c2 > average)
1145         {
1146                 bprint("Rebalancing Teams\n");
1147                 //bprint("Shuffle from team 2\n");
1148                 ShufflePlayerOutOfTeam(2);
1149         }
1150         if(c3 && c3 > average)
1151         {
1152                 bprint("Rebalancing Teams\n");
1153                 //bprint("Shuffle from team 3\n");
1154                 ShufflePlayerOutOfTeam(3);
1155         }
1156         if(c4 && c4 > average)
1157         {
1158                 bprint("Rebalancing Teams\n");
1159                 //bprint("Shuffle from team 4\n");
1160                 ShufflePlayerOutOfTeam(4);
1161         }
1162
1163         // if teams are still unbalanced, balance them further in the next audit,
1164         // which will happen sooner (keep doing rapid audits until things are in order)
1165         audit_teams_time = time + 0.7 + random()*0.3;
1166 }
1167
1168
1169
1170 /*void(entity e, float first) UpdateTeamScore =
1171 {
1172         clientno = e.FIXME;
1173         if(first)
1174         {
1175                 WriteByte (MSG_ALL, SVC_UPDATENAME);
1176                 WriteByte (MSG_ALL, clientno);
1177                 WriteString (MSG_ALL, e.netname);
1178
1179                 WriteByte (MSG_ALL, SVC_UPDATECOLORS);
1180                 WriteByte (MSG_ALL, clientno);
1181                 WriteByte (MSG_ALL, e.b_shirt * 16 + who.b_pants);
1182         }
1183
1184         WriteByte (MSG_ALL, SVC_UPDATEFRAGS);
1185         WriteByte (MSG_ALL, clientno);
1186         WriteShort (MSG_ALL, e.frags + 10000);
1187 };
1188
1189 */
1190
1191
1192 // code from here on is just to support maps that don't have team entities
1193 void tdm_spawnteam (string teamname, float teamcolor)
1194 {
1195         local entity e;
1196         e = spawn();
1197         e.classname = "tdm_team";
1198         e.netname = teamname;
1199         e.cnt = teamcolor;
1200         e.team = e.cnt + 1;
1201 };
1202
1203 // spawn some default teams if the map is not set up for tdm
1204 void() tdm_spawnteams =
1205 {
1206         float numteams;
1207
1208         numteams = cvar("g_tdm_teams");
1209
1210         tdm_spawnteam("Red", 4);
1211         tdm_spawnteam("Blue", 13);
1212 };
1213
1214 void() tdm_delayedinit =
1215 {
1216         self.think = SUB_Remove;
1217         self.nextthink = time;
1218         // if no teams are found, spawn defaults
1219         if (find(world, classname, "tdm_team") == world)
1220                 tdm_spawnteams();
1221 };
1222
1223 void() tdm_init =
1224 {
1225         local entity e;
1226         e = spawn();
1227         e.think = tdm_delayedinit;
1228         e.nextthink = time + 0.1;
1229 };
1230
1231