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