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