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