]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/teamplay.qc
centerprints now are managed independently per line
[divverent/nexuiz.git] / data / qcsrc / server / teamplay.qc
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", wordwrap(cvar_string("sv_motd"), 50));
277         cvar_set("_mutatormsg", wordwrap(cvar_string("g_mutatormsg"), 50));
278
279         g_domination = cvar("g_domination");
280         g_ctf = cvar("g_ctf");
281         g_tdm = cvar("g_tdm");
282 }
283
284 string GetClientVersionMessage(float v) {
285         local string versionmsg;
286         if (v == 1) {
287                 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";
288                 // either that or someone wants to be funny
289         } else if (v != cvar("gameversion")) {
290                 if(v < cvar("gameversion")) {
291                         versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
292                 } else {
293                         versionmsg = "^3This server is using an outdated Nexuiz version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
294                 }
295         } else {
296                 versionmsg = "^2client version and server version are compatible.^8";
297         }
298         return strzone(versionmsg);
299
300 }
301
302
303 void PrintWelcomeMessage(entity pl)
304 {
305         string s, mutator, modifications;
306
307         /*if(self.welcomemessage_time < time)
308                 return;
309         if(self.welcomemessage_time2 > time)
310                 return;
311         self.welcomemessage_time2 = time + 0.8; */
312
313         if(self.classname == "observer")
314         {
315                 if(cvar("g_lms") && self.frags <= 0 && self.frags > -666)
316                         return centermsg_setall(strcat(newlines, "^1You have no more lives left\nwait for next round\n\n\n^7press attack to spectate other players"));
317                 else if(cvar("g_lms") && self.frags == -666)
318                         return centermsg_setall(strcat(newlines, "^1Match has already begun\nwait for next round\n\n\n^7press attack to spectate other players"));
319         }
320         else if(self.classname == "spectator")
321         {
322                 if ((cvar("g_lms") && self.frags < 1) || cvar("g_arena"))
323                         return centermsg_setall(strcat(newlines, "spectating ", self.enemy.netname, "\n\n\n^7press attack for next player\npress attack2 for free fly mode"));
324                 else
325                         return centermsg_setall(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"));
326         }
327
328
329         if(self.welcomemessage_time2 > time) return;
330         self.welcomemessage_time2 = time + 1.0;
331
332         if(cvar("g_campaign"))
333         {
334                 centermsg_setallfor(pl, campaign_message);
335                 return;
336         }
337
338         if(cvar("g_minstagib"))
339                 mutator = "^2Minstagib ^1";
340         else if(cvar("g_instagib"))
341                 mutator = "^2Instagib ^1";
342         else if(cvar("g_rocketarena"))
343                 mutator = "^2Rocketarena ^1";
344         else if(cvar("g_nixnex"))
345                 mutator = "^2No Items Nexuiz ^1";
346
347         if(cvar("g_midair")) {
348                 // to protect against unheedingly made changes
349                 if (modifications) {
350                         modifications = strcat(modifications, ", ");
351                 }
352                 modifications = "midair";
353         }
354         if(cvar("g_vampire")) {
355                 if (modifications) {
356                         modifications = strcat(modifications, ", ");
357                 }
358                 modifications = strcat(modifications, "vampire");
359         }
360         if(cvar("g_laserguided_missile")) {
361                 if (modifications) {
362                         modifications = strcat(modifications, ", ");
363                 }
364                 modifications = strcat(modifications, "laser-guided-missiles");
365         }
366
367         local string versionmessage;
368         versionmessage = GetClientVersionMessage(self.version);
369
370         s = strcat(s, newlines, "This is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
371         s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
372
373         if(modifications != "")
374                 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
375
376         if((self.classname == "observer" || self.classname == "spectator") && self.version == cvar("gameversion")) {
377                 if(!cvar("g_arena"))
378                         s = strcat(s,"^7\n\n\npress jump to play\npress attack to spectate other players\n\n");
379                 else if(player_count < 2 && arena_roundbased)
380                 {
381                         s = strcat(s, "\n\n\n^1waiting for second player to start match^7\n\n");
382                 }
383                 else
384                 {
385                         s = strcat(s, "\n\n\n");
386                         if(champion)
387                                 s = strcat(s, "^7current champion is: ", champion.netname, "\n\n");
388                         s = strcat(s,"^7press attack to spectate other players\n\n");
389                 }
390         }
391
392
393         s = strzone(s);
394
395         if (cvar("g_grappling_hook"))
396                 s = strcat(s, "\n\n^8grappling hook is enabled, press 'e' to use it\n");
397
398         if (cvar_string("_mutatormsg") != "") {
399                 s = strcat(s, "\n\n^8special gameplay tips: ^7", cvar_string("_mutatormsg"));
400         }
401
402         if (cvar_string("_motd") != "") {
403                 s = strcat(s, "\n\n^8MOTD: ^7", cvar_string("_motd"));
404         }
405
406         s = strzone(s);
407
408         centermsg_setallfor(pl, s);
409         //sprint(pl, s);
410
411         strunzone(s);
412 }
413
414
415 void SetPlayerColors(entity pl, float _color)
416 {
417         /*string s;
418         s = ftos(cl);
419         stuffcmd(pl, strcat("color ", s, " ", s, "\n")  );
420         pl.team = cl + 1;
421         //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
422         pl.clientcolors = 16*cl + cl;*/
423
424         float pants, shirt;
425         pants = _color & 0x0F;
426         shirt = _color & 0xF0;
427
428
429         if(teamplay) {
430                 setcolor(pl, 16*pants + pants);
431         } else {
432                 setcolor(pl, shirt + pants);
433         }
434 }
435
436 void SetPlayerTeam(entity pl, float t, float s, float noprint)
437 {
438         float _color;
439
440         if(t == 4)
441                 _color = COLOR_TEAM4 - 1;
442         else if(t == 3)
443                 _color = COLOR_TEAM3 - 1;
444         else if(t == 2)
445                 _color = COLOR_TEAM2 - 1;
446         else
447                 _color = COLOR_TEAM1 - 1;
448
449         SetPlayerColors(pl,_color);
450
451         if(!noprint && t != s)
452         {
453                 //bprint(strcat(pl.netname, " has changed to ", TeamNoName(t), "\n"));
454                 bprint(strcat(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n"));
455         }
456
457         if(t != s)
458                 LogTeamchange(pl);
459 }
460
461
462
463
464
465
466 // set c1...c4 to show what teams are allowed
467 void CheckAllowedTeams ()
468 {
469         string teament_name;
470         float dm;
471         entity head;
472
473 //      if(!dom && !ctf)
474 //              dm = 1;
475
476         c1 = c2 = c3 = c4 = -1;
477         cb1 = cb2 = cb3 = cb4 = 0;
478
479         if(g_domination)
480                 teament_name = "dom_team";
481         else if(g_ctf)
482                 teament_name = "ctf_team";
483         else if(g_tdm)
484                 teament_name = "tdm_team";
485         else
486         {
487                 // cover anything else by treating it like tdm with no teams spawned
488                 dm = cvar("g_tdm_teams");
489                 if(dm < 2)
490                         error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
491
492                 if(dm >= 4)
493                 {
494                         c1 = c2 = c3 = c4 = 0;
495                 }
496                 else if(dm >= 3)
497                 {
498                         c1 = c2 = c3 = 0;
499                 }
500                 else// if(dm >= 2)
501                 {
502                         c1 = c2 = 0;
503                 }
504                 return;
505         }
506
507         // first find out what teams are allowed
508         head = find(world, classname, teament_name);
509         while(head)
510         {
511                 if(!(g_domination && head.netname == ""))
512                 {
513                         if(head.team == COLOR_TEAM1)
514                         {
515                                 c1 = 0;
516                         }
517                         if(head.team == COLOR_TEAM2)
518                         {
519                                 c2 = 0;
520                         }
521                         if(head.team == COLOR_TEAM3)
522                         {
523                                 c3 = 0;
524                         }
525                         if(head.team == COLOR_TEAM4)
526                         {
527                                 c4 = 0;
528                         }
529                 }
530                 head = find(head, classname, teament_name);
531         }
532 }
533
534 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
535 // teams that are allowed will now have their player counts stored in c1...c4
536 void GetTeamCounts(entity ignore)
537 {
538         entity head;
539         // now count how many players are on each team already
540
541         // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
542         // also remember the lowest-scoring player
543
544         head = find(world, classname, "player");
545         while(head)
546         {
547                 if(head != ignore)// && head.netname != "")
548                 {
549                         if(head.team == COLOR_TEAM1)
550                         {
551                                 if(c1 >= 0)
552                                 {
553                                         c1 = c1 + 1;
554                                         cb1 = cb1 + 1;
555                                 }
556                         }
557                         if(head.team == COLOR_TEAM2)
558                         {
559                                 if(c2 >= 0)
560                                 {
561                                         c2 = c2 + 1;
562                                         cb2 = cb2 + 1;
563                                 }
564                         }
565                         if(head.team == COLOR_TEAM3)
566                         {
567                                 if(c3 >= 0)
568                                 {
569                                         c3 = c3 + 1;
570                                         cb3 = cb3 + 1;
571                                 }
572                         }
573                         if(head.team == COLOR_TEAM4)
574                         {
575                                 if(c4 >= 0)
576                                 {
577                                         c4 = c4 + 1;
578                                         cb4 = cb4 + 1;
579                                 }
580                         }
581                 }
582                 head = find(head, classname, "player");
583         }
584 }
585
586 // returns # of smallest team (1, 2, 3, 4)
587 // NOTE: Assumes CheckAllowedTeams has already been called!
588 float FindSmallestTeam(entity pl, float ignore_pl)
589 {
590         float totalteams, smallestteam, smallestteam_count, balance_type;
591         totalteams = 0;
592
593         // find out what teams are available
594         //CheckAllowedTeams();
595
596         // make sure there are at least 2 teams to join
597         if(c1 >= 0)
598                 totalteams = totalteams + 1;
599         if(c2 >= 0)
600                 totalteams = totalteams + 1;
601         if(c3 >= 0)
602                 totalteams = totalteams + 1;
603         if(c4 >= 0)
604                 totalteams = totalteams + 1;
605
606         if(totalteams <= 1)
607         {
608                 if(g_domination)
609                         error("Too few teams available for domination\n");
610                 else if(g_ctf)
611                         error("Too few teams available for ctf\n");
612                 else
613                         error("Too few teams available for team deathmatch\n");
614         }
615
616
617         // count how many players are in each team
618         if(ignore_pl)
619                 GetTeamCounts(pl);
620         else
621                 GetTeamCounts(world);
622
623         // c1...c4 now have counts of each team
624         // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
625
626         smallestteam = 0;
627         smallestteam_count = 999;
628
629         // 2 gives priority to what team you're already on, 1 goes in order
630         // 2 doesn't seem to work though...
631         balance_type = 1;
632
633         if(balance_type == 1)
634         {
635                 if(c1 >= 0 && c1 < smallestteam_count)
636                 {
637                         smallestteam = 1;
638                         smallestteam_count = c1;
639                 }
640                 if(c2 >= 0 && c2 < smallestteam_count)
641                 {
642                         smallestteam = 2;
643                         smallestteam_count = c2;
644                 }
645                 if(c3 >= 0 && c3 < smallestteam_count)
646                 {
647                         smallestteam = 3;
648                         smallestteam_count = c3;
649                 }
650                 if(c4 >= 0 && c4 < smallestteam_count)
651                 {
652                         smallestteam = 4;
653                         smallestteam_count = c4;
654                 }
655         }
656         else
657         {
658                 if(c1 >= 0 && (c1 < smallestteam_count ||
659                                         (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
660                 {
661                         smallestteam = 1;
662                         smallestteam_count = c1;
663                 }
664                 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
665                                         (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
666                 {
667                         smallestteam = 2;
668                         smallestteam_count = c2;
669                 }
670                 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
671                                         (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
672                 {
673                         smallestteam = 3;
674                         smallestteam_count = c3;
675                 }
676                 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
677                                         (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
678                 {
679                         smallestteam = 4;
680                         smallestteam_count = c4;
681                 }
682         }
683
684         return smallestteam;
685 }
686
687 float JoinBestTeam(entity pl, float only_return_best)
688 {
689         float smallest, selectedteam;
690
691         // don't join a team if we're not playing a team game
692         if(!cvar("teamplay") && !g_domination && !g_ctf)
693                 return 0;
694
695         // find out what teams are available
696         CheckAllowedTeams();
697
698         if(cvar("g_domination"))
699         {
700                 if(cvar("g_domination_default_teams") < 3)
701                         c3 = 9999;
702                 if(cvar("g_domination_default_teams") < 4)
703                         c4 = 9999;
704         }
705
706         // if we don't care what team he ends up on, put him on whatever team he entered as.
707         // if he's not on a valid team, then let other code put him on the smallest team
708         if(!cvar("g_campaign") && !cvar("g_balance_teams") && !cvar("g_balance_teams_force"))
709         {
710                 if(     c1 >= 0 && pl.team == COLOR_TEAM1)
711                         selectedteam = pl.team;
712                 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
713                         selectedteam = pl.team;
714                 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
715                         selectedteam = pl.team;
716                 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
717                         selectedteam = pl.team;
718                 else
719                         selectedteam = -1;
720                 if(selectedteam > 0)
721                 {
722                         if(!only_return_best)
723                         {
724                                 SetPlayerColors(pl, selectedteam - 1);
725                                 LogTeamchange(pl);
726                         }
727                         return selectedteam;
728                 }
729                 // otherwise end up on the smallest team (handled below)
730         }
731
732         smallest = FindSmallestTeam(pl, TRUE);
733
734
735         if(!only_return_best)
736         {
737                 if(smallest == 1)
738                 {
739                         SetPlayerColors(pl, COLOR_TEAM1 - 1);
740                 }
741                 else if(smallest == 2)
742                 {
743                         SetPlayerColors(pl, COLOR_TEAM2 - 1);
744                 }
745                 else if(smallest == 3)
746                 {
747                         SetPlayerColors(pl, COLOR_TEAM3 - 1);
748                 }
749                 else if(smallest == 4)
750                 {
751                         SetPlayerColors(pl, COLOR_TEAM4 - 1);
752                 }
753                 else
754                 {
755                         error("smallest team: invalid team\n");
756                 }
757                 LogTeamchange(pl);
758                 if(pl.deadflag == DEAD_NO)
759                         Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
760         }
761
762         return smallest;
763 }
764
765
766 void SV_ChangeTeam(float _color)
767 {
768         float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
769
770         // in normal deathmatch we can just apply the color and we're done
771         if(!cvar("teamplay")) {
772                 SetPlayerColors(self, _color);
773                 return;
774         }
775
776         scolor = self.clientcolors & 0x0F;
777         dcolor = _color & 0x0F;
778
779         if(scolor == COLOR_TEAM1 - 1)
780                 steam = 1;
781         else if(scolor == COLOR_TEAM2 - 1)
782                 steam = 2;
783         else if(scolor == COLOR_TEAM3 - 1)
784                 steam = 3;
785         else if(scolor == COLOR_TEAM4 - 1)
786                 steam = 4;
787         if(dcolor == COLOR_TEAM1 - 1)
788                 dteam = 1;
789         else if(dcolor == COLOR_TEAM2 - 1)
790                 dteam = 2;
791         else if(dcolor == COLOR_TEAM3 - 1)
792                 dteam = 3;
793         else if(dcolor == COLOR_TEAM4 - 1)
794                 dteam = 4;
795
796         // remap invalid teams in dom & ctf
797         /*
798         if(cvar("g_ctf") && dteam == 3)
799                 dteam = 2;
800         else if(cvar("g_ctf") && dteam == 4)
801                 dteam = 1;
802         else if((cvar("g_domination") && cvar("g_domination_default_teams") < 3) || (cvar("g_tdm") && cvar("g_tdm_teams") < 3))
803         {
804                 if(dteam == 3)
805                         dteam = 2;
806                 else if(dteam == 4)
807                         dteam = 1;
808         }
809         else if((cvar("g_domination") && cvar("g_domination_default_teams") < 4) || (cvar("g_tdm") && cvar("g_tdm_teams") < 4))
810         {
811                 if(dteam == 4)
812                         dteam = 1;
813         }
814         */
815
816         CheckAllowedTeams();
817         if(dteam == 3 && c3 < 0)
818                 dteam = 2;
819         if(dteam == 4 && c4 < 0)
820                 dteam = 1;
821
822         // not changing teams
823         if(scolor == dcolor)
824         {
825                 //bprint("same team change\n");
826                 SetPlayerTeam(self, dteam, steam, TRUE);
827                 return;
828         }
829
830         if(cvar("teamplay"))
831         {
832                 if(cvar("g_campaign") || cvar("g_changeteam_banned"))
833                 {
834                         sprint(self, "Team changes not allowed\n");
835                         return; // changing teams is not allowed
836                 }
837
838                 if(!cvar("g_campaign") && cvar("g_balance_teams_prevent_imbalance"))
839                 {
840                         // only allow changing to a smaller or equal size team
841
842                         // find out what teams are available
843                         CheckAllowedTeams();
844                         // count how many players on each team
845                         GetTeamCounts(world);
846
847                         // get desired team
848                         if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
849                         {
850                                 dcount = c1;
851                                 dbotcount = cb1;
852                         }
853                         else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
854                         {
855                                 dcount = c2;
856                                 dbotcount = cb2;
857                         }
858                         else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
859                         {
860                                 dcount = c3;
861                                 dbotcount = cb3;
862                         }
863                         else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
864                         {
865                                 dcount = c4;
866                                 dbotcount = cb4;
867                         }
868                         else
869                         {
870                                 sprint(self, "Cannot change to an invalid team\n");
871
872                                 return;
873                         }
874
875                         // get starting team
876                         if(steam == 1)//scolor == COLOR_TEAM1 - 1)
877                                 scount = c1;
878                         else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
879                                 scount = c2;
880                         else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
881                                 scount = c3;
882                         else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
883                                 scount = c4;
884
885                         if(scount) // started at a valid, nonempty team
886                         {
887                                 // check if we're trying to change to a larger team that doens't have bots to swap with
888                                 if(dcount >= scount && dbotcount <= 0)
889                                 {
890                                         sprint(self, "Cannot change to a larger team\n");
891                                         return; // can't change to a larger team
892                                 }
893                         }
894                 }
895         }
896
897 //      bprint(strcat("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n"));
898
899         SetPlayerTeam(self, dteam, steam, FALSE);
900         if(cvar("teamplay") && self.classname == "player" && steam != dteam)
901         {
902                 // kill player when changing teams
903                 if(self.deadflag == DEAD_NO)
904                         Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
905                         // reduce frags during a team change
906                         self.frags = floor(self.frags * (cvar("g_changeteam_fragtransfer") / 100));
907         }
908 }
909
910
911 void ShufflePlayerOutOfTeam (float source_team)
912 {
913         float smallestteam, smallestteam_count, steam;
914         float lowest_bot_score, lowest_player_score;
915         entity head, lowest_bot, lowest_player, selected;
916         string m;
917
918         smallestteam = 0;
919         smallestteam_count = 999;
920
921         if(c1 >= 0 && c1 < smallestteam_count)
922         {
923                 smallestteam = 1;
924                 smallestteam_count = c1;
925         }
926         if(c2 >= 0 && c2 < smallestteam_count)
927         {
928                 smallestteam = 2;
929                 smallestteam_count = c2;
930         }
931         if(c3 >= 0 && c3 < smallestteam_count)
932         {
933                 smallestteam = 3;
934                 smallestteam_count = c3;
935         }
936         if(c4 >= 0 && c4 < smallestteam_count)
937         {
938                 smallestteam = 4;
939                 smallestteam_count = c4;
940         }
941
942         if(!smallestteam)
943         {
944                 bprint("warning: no smallest team\n");
945                 return;
946         }
947
948         if(source_team == 1)
949                 steam = COLOR_TEAM1;
950         else if(source_team == 2)
951                 steam = COLOR_TEAM2;
952         else if(source_team == 3)
953                 steam = COLOR_TEAM3;
954         else if(source_team == 4)
955                 steam = COLOR_TEAM4;
956
957         lowest_bot = world;
958         lowest_bot_score = 9999;
959         lowest_player = world;
960         lowest_player_score = 9999;
961
962         // find the lowest-scoring player & bot of that team
963         head = find(world, classname, "player");
964         while(head)
965         {
966                 if(head.team == steam)
967                 {
968                         if(head.isbot)
969                         {
970                                 if(head.frags < lowest_bot_score)
971                                 {
972                                         lowest_bot = head;
973                                         lowest_bot_score = head.frags;
974                                 }
975                         }
976                         else
977                         {
978                                 if(head.frags < lowest_player_score)
979                                 {
980                                         lowest_player = head;
981                                         lowest_player_score = head.frags;
982                                 }
983                         }
984                 }
985                 head = find(head, classname, "player");
986         }
987
988         // prefers to move a bot...
989         if(lowest_bot != world)
990                 selected = lowest_bot;
991         // but it will move a player if it has to
992         else
993                 selected = lowest_player;
994         // don't do anything if it couldn't find anyone
995         if(!selected)
996         {
997                 bprint("warning: couldn't find a player to move from team\n");
998                 return;
999         }
1000
1001         // smallest team gains a member
1002         if(smallestteam == 1)
1003         {
1004                 c1 = c1 + 1;
1005         }
1006         else if(smallestteam == 2)
1007         {
1008                 c2 = c2 + 1;
1009         }
1010         else if(smallestteam == 3)
1011         {
1012                 c3 = c3 + 1;
1013         }
1014         else if(smallestteam == 4)
1015         {
1016                 c4 = c4 + 1;
1017         }
1018         else
1019         {
1020                 bprint("warning: destination team invalid\n");
1021                 return;
1022         }
1023         // source team loses a member
1024         if(source_team == 1)
1025         {
1026                 c1 = c1 + 1;
1027         }
1028         else if(source_team == 2)
1029         {
1030                 c2 = c2 + 2;
1031         }
1032         else if(source_team == 3)
1033         {
1034                 c3 = c3 + 3;
1035         }
1036         else if(source_team == 4)
1037         {
1038                 c4 = c4 + 4;
1039         }
1040         else
1041         {
1042                 bprint("warning: source team invalid\n");
1043                 return;
1044         }
1045
1046         // move the player to the new team
1047         SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1048
1049         if(selected.deadflag == DEAD_NO)
1050                         Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1051         /*
1052         m = "You have been moved into a different team to improve team balance\nYou are now on: ";
1053         if (selected.team == 5)
1054                 m = strcat(m, "^1Red Team");
1055         else if (selected.team == 14)
1056                 m = strcat(m, "^4Blue Team");
1057         else if (selected.team == 10)
1058                 m = strcat(m, "^6Pink Team");
1059         else if (selected.team == 13)
1060                 m = strcat(m, "^3Yellow Team");
1061         centerprint(selected, m);
1062         */
1063 }
1064
1065 // part of g_balance_teams_force
1066 // occasionally perform an audit of the teams to make
1067 // sure they're more or less balanced in player count.
1068 void AuditTeams()
1069 {
1070         float numplayers, numteams, average;
1071         if(cvar("g_campaign"))
1072                 return;
1073         if(!cvar("g_balance_teams_force"))
1074                 return;
1075         if(!cvar("teamplay"))
1076                 return;
1077
1078         if(audit_teams_time > time)
1079                 return;
1080
1081         audit_teams_time = time + 4 + random();
1082
1083 //      bprint("Auditing teams\n");
1084
1085         CheckAllowedTeams();
1086         GetTeamCounts(world);
1087
1088
1089         numteams = numplayers = 0;
1090         if(c1 >= 0)
1091         {
1092                 numteams = numteams + 1;
1093                 numplayers = numplayers + c1;
1094         }
1095         if(c2 >= 0)
1096         {
1097                 numteams = numteams + 1;
1098                 numplayers = numplayers + c2;
1099         }
1100         if(c3 >= 0)
1101         {
1102                 numteams = numteams + 1;
1103                 numplayers = numplayers + c3;
1104         }
1105         if(c4 >= 0)
1106         {
1107                 numteams = numteams + 1;
1108                 numplayers = numplayers + c4;
1109         }
1110
1111         if(numplayers <= 0)
1112                 return; // no players to move around
1113         if(numteams < 2)
1114                 return; // don't bother shuffling if for some reason there aren't any teams
1115
1116         average = ceil(numplayers / numteams);
1117
1118         if(average <= 0)
1119                 return; // that's weird...
1120
1121         if(c1 && c1 > average)
1122         {
1123                 bprint("Rebalancing Teams\n");
1124                 //bprint("Shuffle from team 1\n");
1125                 ShufflePlayerOutOfTeam(1);
1126         }
1127         if(c2 && c2 > average)
1128         {
1129                 bprint("Rebalancing Teams\n");
1130                 //bprint("Shuffle from team 2\n");
1131                 ShufflePlayerOutOfTeam(2);
1132         }
1133         if(c3 && c3 > average)
1134         {
1135                 bprint("Rebalancing Teams\n");
1136                 //bprint("Shuffle from team 3\n");
1137                 ShufflePlayerOutOfTeam(3);
1138         }
1139         if(c4 && c4 > average)
1140         {
1141                 bprint("Rebalancing Teams\n");
1142                 //bprint("Shuffle from team 4\n");
1143                 ShufflePlayerOutOfTeam(4);
1144         }
1145
1146         // if teams are still unbalanced, balance them further in the next audit,
1147         // which will happen sooner (keep doing rapid audits until things are in order)
1148         audit_teams_time = time + 0.7 + random()*0.3;
1149 }
1150
1151
1152
1153 /*void(entity e, float first) UpdateTeamScore =
1154 {
1155         clientno = e.FIXME;
1156         if(first)
1157         {
1158                 WriteByte (MSG_ALL, SVC_UPDATENAME);
1159                 WriteByte (MSG_ALL, clientno);
1160                 WriteString (MSG_ALL, e.netname);
1161
1162                 WriteByte (MSG_ALL, SVC_UPDATECOLORS);
1163                 WriteByte (MSG_ALL, clientno);
1164                 WriteByte (MSG_ALL, e.b_shirt * 16 + who.b_pants);
1165         }
1166
1167         WriteByte (MSG_ALL, SVC_UPDATEFRAGS);
1168         WriteByte (MSG_ALL, clientno);
1169         WriteShort (MSG_ALL, e.frags + 10000);
1170 };
1171
1172 */
1173
1174
1175 // code from here on is just to support maps that don't have team entities
1176 void tdm_spawnteam (string teamname, float teamcolor)
1177 {
1178         local entity e;
1179         e = spawn();
1180         e.classname = "tdm_team";
1181         e.netname = teamname;
1182         e.cnt = teamcolor;
1183         e.team = e.cnt + 1;
1184 };
1185
1186 // spawn some default teams if the map is not set up for tdm
1187 void() tdm_spawnteams =
1188 {
1189         float numteams;
1190
1191         numteams = cvar("g_tdm_teams");
1192
1193         tdm_spawnteam("Red", 4);
1194         tdm_spawnteam("Blue", 13);
1195 };
1196
1197 void() tdm_delayedinit =
1198 {
1199         self.think = SUB_Remove;
1200         self.nextthink = time;
1201         // if no teams are found, spawn defaults
1202         if (find(world, classname, "tdm_team") == world)
1203                 tdm_spawnteams();
1204 };
1205
1206 void() tdm_init =
1207 {
1208         local entity e;
1209         e = spawn();
1210         e.think = tdm_delayedinit;
1211         e.nextthink = time + 0.1;
1212 };
1213
1214