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