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