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