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