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