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