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