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