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