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