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