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