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