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