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