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