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