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