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