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