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