2 string cache_mutatormsg;
4 string cache_lastmutatormsg;
6 // client counts for each team
8 // # of bots on those teams
9 float cb1, cb2, cb3, cb4;
11 float audit_teams_time;
13 float IsTeamBalanceForced()
15 if(intermission_running)
16 return 0; // no rebalancing whatsoever please
19 if(cvar("g_campaign"))
21 if(!cvar("g_balance_teams_force"))
26 void TeamchangeFrags(entity e)
31 vector TeamColor(float teem)
36 return '1 0.0625 0.0625';
38 return '0.0625 0.0625 1';
48 string TeamName(float t)
50 return strcat(Team_ColorName(t), " Team");
52 string ColoredTeamName(float t)
54 return strcat(Team_ColorCode(t), Team_ColorName(t), " Team^7");
56 string TeamNoName(float t)
58 // fixme: Search for team entities and get their .netname's!
67 return "Neutral Team";
72 void runematch_init();
76 void LogTeamchange(entity pl)
79 if(!cvar("sv_eventlog"))
83 str = strcat(":team:", ftos(pl.playerid), ":");
84 str = strcat(str, ftos(pl.team));
85 GameLogEcho(str, FALSE);
90 cvar_set("g_dm", ftos(g_dm));
91 cvar_set("g_tdm", ftos(g_tdm));
92 cvar_set("g_domination", ftos(g_domination));
93 cvar_set("g_ctf", ftos(g_ctf));
94 cvar_set("g_runematch", ftos(g_runematch));
95 cvar_set("g_lms", ftos(g_lms));
96 cvar_set("g_arena", ftos(g_arena));
97 cvar_set("g_keyhunt", ftos(g_keyhunt));
98 cvar_set("g_assault", ftos(g_assault));
99 cvar_set("g_onslaught", ftos(g_onslaught));
100 cvar_set("g_race", ftos(g_race));
110 prev = cvar("gamecfg");
111 for(i = 0; i < 2; ++i)
113 found += (g_dm = (!found && (prev != GAME_DEATHMATCH) && cvar("g_dm")));
114 found += (g_tdm = (!found && (prev != GAME_TEAM_DEATHMATCH) && cvar("g_tdm")));
115 found += (g_domination = (!found && (prev != GAME_DOMINATION) && cvar("g_domination")));
116 found += (g_ctf = (!found && (prev != GAME_CTF) && cvar("g_ctf")));
117 found += (g_runematch = (!found && (prev != GAME_RUNEMATCH) && cvar("g_runematch")));
118 found += (g_lms = (!found && (prev != GAME_LMS) && cvar("g_lms")));
119 found += (g_arena = (!found && (prev != GAME_ARENA) && cvar("g_arena")));
120 found += (g_keyhunt = (!found && (prev != GAME_KEYHUNT) && cvar("g_keyhunt")));
121 found += (g_assault = (!found && (prev != GAME_ASSAULT) && cvar("g_assault")));
122 found += (g_onslaught = (!found && (prev != GAME_ONSLAUGHT) && cvar("g_onslaught")));
123 found += (g_race = (!found && (prev != GAME_RACE) && cvar("g_race")));
128 prev = -1; // second attempt takes place WITHOUT prev set
134 if(g_dm && cvar("deathmatch_force_teamplay"))
143 void default_delayedinit()
147 if(!scores_initialized)
148 ScoreRules_generic();
151 void ActivateTeamplay()
153 float teamplay_default;
154 teamplay_default = cvar("teamplay_default");
157 cvar_set("teamplay", ftos(teamplay_default));
159 cvar_set("teamplay", "3");
164 void InitGameplayMode()
166 float fraglimit_override, timelimit_override, capturelimit_override;
171 cvar_set("teamplay", "0");
173 // make sure only ONE type is selected
178 MapInfo_LoadMapSettings(mapname);
179 clientstuff = strzone(MapInfo_Map_clientstuff);
180 MapInfo_ClearTemps();
182 // in case mapinfo switched the type
186 // set both here, gamemode can override it later
187 timelimit_override = cvar("timelimit_override");
188 fraglimit_override = cvar("fraglimit_override");
192 game = GAME_DEATHMATCH;
193 gamemode_name = "Deathmatch";
198 game = GAME_TEAM_DEATHMATCH;
199 gamemode_name = "Team Deathmatch";
206 game = GAME_DOMINATION;
207 gamemode_name = "Domination";
209 fraglimit_override = cvar("g_domination_point_limit");
216 gamemode_name = "Capture the Flag";
218 g_ctf_win_mode = cvar("g_ctf_win_mode");
219 if(g_ctf_win_mode == 2)
220 fraglimit_override = cvar("g_ctf_capture_limit");
222 fraglimit_override = cvar("capturelimit_override");
228 game = GAME_RUNEMATCH;
229 gamemode_name = "Rune Match";
230 if(cvar("deathmatch_force_teamplay"))
232 fraglimit_override = cvar("g_runematch_point_limit");
239 gamemode_name = "Last Man Standing";
240 fraglimit_override = cvar("g_lms_lives_override");
241 if(fraglimit_override == 0)
242 fraglimit_override = -1;
243 lms_lowest_lives = 9999;
251 gamemode_name = "Arena";
252 fraglimit_override = cvar("g_arena_point_limit");
253 maxspawned = cvar("g_arena_maxspawned");
256 arena_roundbased = cvar("g_arena_roundbased");
262 gamemode_name = "Key Hunt";
264 fraglimit_override = cvar("g_keyhunt_point_limit");
271 gamemode_name = "Assault";
277 game = GAME_ONSLAUGHT;
278 gamemode_name = "Onslaught";
286 gamemode_name = "Race";
287 g_race_qualifying = cvar("g_race_qualifying");
289 // In campaign, only this mode makes sense...
290 if(cvar("g_campaign"))
291 g_race_qualifying = 1;
293 if(cvar("g_race_teams"))
295 g_race_qualifying = 0; // not supported!
297 race_teams = bound(2, cvar("g_race_teams"), 4);
302 if(g_race_qualifying == 1)
303 fraglimit_override = 0;
305 fraglimit_override = cvar("g_race_laps_limit");
307 if(g_race_qualifying)
308 independent_players = 1;
313 // save it (for the next startup)
314 cvar_set("gamecfg", ftos(game));
316 cache_mutatormsg = strzone("");
317 cache_motd = strzone("");
318 cache_lastmutatormsg = strzone("");
319 cache_lastmotd = strzone("");
321 // enforce the server's universal frag/time limits
322 if(!cvar("g_campaign"))
324 if(fraglimit_override >= 0)
325 cvar_set("fraglimit", ftos(fraglimit_override));
326 if(timelimit_override >= 0)
327 cvar_set("timelimit", ftos(timelimit_override));
330 if(g_race && g_race_qualifying == 2)
332 race_fraglimit = cvar("fraglimit");
333 cvar_set("fraglimit", "0");
338 e.nextthink = time + 0.3; // MUST be after all other delayed inits!
339 e.think = default_delayedinit;
342 string GetClientVersionMessage() {
343 local string versionmsg;
344 if (self.version_mismatch) {
345 if(self.version < cvar("gameversion")) {
346 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
348 versionmsg = "^3This server is using an outdated Nexuiz version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
351 versionmsg = "^2client version and server version are compatible.^8";
357 void PrintWelcomeMessage(entity pl)
359 string s, mutator, modifications;
362 /*if(self.welcomemessage_time > time)
364 self.welcomemessage_time = time + 0.8; */
366 if(self.cvar_scr_centertime == 0) return;
367 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
368 if(self.welcomemessage_time > time) return;
369 self.welcomemessage_time = time + self.cvar_scr_centertime * 0.6;
372 if(cvar("g_campaign"))
374 centerprint(pl, campaign_message);
378 if(!self.BUTTON_INFO)
380 // TODO get rid of this too
381 local string specString;
382 specString = NEWLINES;
383 if(time < restart_countdown) //also show the countdown when being a spectator
384 specString = strcat(specString, "\n\n^1Game starts in ", ftos(restartAnnouncer.cnt + 1), " seconds^7");
385 else if (timeoutStatus != 0)
386 specString = strcat(specString, "\n\n", getTimeoutText(1));
389 return centerprint_atprio(self, CENTERPRIO_SPAM, specString);
393 mutator = "^2Minstagib ^1";
395 mutator = "^2Instagib ^1";
396 else if(g_rocketarena)
397 mutator = "^2Rocketarena ^1";
399 mutator = "^2No Items Nexuiz ^1";
402 // to protect against unheedingly made changes
404 modifications = strcat(modifications, ", ");
406 modifications = "midair";
410 modifications = strcat(modifications, ", ");
412 modifications = strcat(modifications, "vampire");
414 if(g_laserguided_missile) {
416 modifications = strcat(modifications, ", ");
418 modifications = strcat(modifications, "laser-guided-missiles");
422 modifications = strcat(modifications, ", ");
424 modifications = strcat(modifications, "Tournament");
427 local string versionmessage;
428 versionmessage = GetClientVersionMessage();
430 s = strcat(s, NEWLINES, "This is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
431 s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
433 if(modifications != "")
434 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
437 if(!tourneyInMatchStage)
438 s = strcat(s, "\n^8The game is currently in ^3warmup-stage ^8!\n");
440 s = strcat(s, "\n^8The game is currently in ^3match-stage ^8!\n");
443 if(time < restart_countdown)
444 s = strcat(s, "\n^1Game starts in ", ftos(restartAnnouncer.cnt + 1), " seconds^7");
446 if(timeoutStatus != 0)
447 s = strcat(s, "\n\n", getTimeoutText(1));
449 if (g_grappling_hook)
450 s = strcat(s, "\n\n^8grappling hook is enabled, press 'e' to use it\n");
452 if(cache_lastmutatormsg != cvar_string("g_mutatormsg"))
454 if(cache_lastmutatormsg)
455 strunzone(cache_lastmutatormsg);
457 strunzone(cache_mutatormsg);
458 cache_lastmutatormsg = strzone(cvar_string("g_mutatormsg"));
459 cache_mutatormsg = strzone(wordwrap(cache_lastmutatormsg, 50));
462 if (cache_mutatormsg != "") {
463 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
466 if(cache_lastmotd != cvar_string("sv_motd"))
469 strunzone(cache_lastmotd);
471 strunzone(cache_motd);
472 cache_lastmotd = strzone(cvar_string("sv_motd"));
473 cache_motd = strzone(wordwrap(cache_lastmotd, 50));
476 if (cache_motd != "") {
477 s = strcat(s, "\n\n^8MOTD: ^7", cache_motd);
486 void SetPlayerColors(entity pl, float _color)
490 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
492 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
493 pl.clientcolors = 16*cl + cl;*/
496 pants = _color & 0x0F;
497 shirt = _color & 0xF0;
501 setcolor(pl, 16*pants + pants);
503 setcolor(pl, shirt + pants);
507 void SetPlayerTeam(entity pl, float t, float s, float noprint)
512 _color = COLOR_TEAM4 - 1;
514 _color = COLOR_TEAM3 - 1;
516 _color = COLOR_TEAM2 - 1;
518 _color = COLOR_TEAM1 - 1;
520 SetPlayerColors(pl,_color);
522 if(!noprint && t != s)
524 //bprint(pl.netname, " has changed to ", TeamNoName(t), "\n");
525 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
537 // set c1...c4 to show what teams are allowed
538 void CheckAllowedTeams (entity for_whom)
547 c1 = c2 = c3 = c4 = -1;
548 cb1 = cb2 = cb3 = cb4 = 0;
550 // onslaught is special
553 head = findchain(classname, "onslaught_generator");
556 if (head.team == COLOR_TEAM1) c1 = 0;
557 if (head.team == COLOR_TEAM2) c2 = 0;
558 if (head.team == COLOR_TEAM3) c3 = 0;
559 if (head.team == COLOR_TEAM4) c4 = 0;
566 teament_name = "dom_team";
568 teament_name = "ctf_team";
570 teament_name = "tdm_team";
573 c1 = c2 = 0; // Assault always has 2 teams
578 // cover anything else by treating it like tdm with no teams spawned
584 dm = cvar("g_tdm_teams");
586 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
590 c1 = c2 = c3 = c4 = 0;
603 // first find out what teams are allowed
604 head = find(world, classname, teament_name);
607 if(!(g_domination && head.netname == ""))
609 if(head.team == COLOR_TEAM1)
613 if(head.team == COLOR_TEAM2)
617 if(head.team == COLOR_TEAM3)
621 if(head.team == COLOR_TEAM4)
626 head = find(head, classname, teament_name);
631 if(cvar("bot_vs_human") > 0)
634 if(clienttype(for_whom) == CLIENTTYPE_BOT)
639 else if(cvar("bot_vs_human") < 0)
642 if(clienttype(for_whom) == CLIENTTYPE_BOT)
650 float PlayerValue(entity p)
652 if(IsTeamBalanceForced() == 1)
657 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
658 // teams that are allowed will now have their player counts stored in c1...c4
659 void GetTeamCounts(entity ignore)
663 // now count how many players are on each team already
665 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
666 // also remember the lowest-scoring player
668 FOR_EACH_PLAYER(head)
670 if(head != ignore)// && head.netname != "")
672 value = PlayerValue(head);
673 if(clienttype(head) == CLIENTTYPE_BOT)
677 if(head.team == COLOR_TEAM1)
685 if(head.team == COLOR_TEAM2)
693 if(head.team == COLOR_TEAM3)
701 if(head.team == COLOR_TEAM4)
713 // returns # of smallest team (1, 2, 3, 4)
714 // NOTE: Assumes CheckAllowedTeams has already been called!
715 float FindSmallestTeam(entity pl, float ignore_pl)
717 float totalteams, smallestteam, smallestteam_count, smallestteam_score, balance_type;
720 // find out what teams are available
721 //CheckAllowedTeams();
723 // make sure there are at least 2 teams to join
725 totalteams = totalteams + 1;
727 totalteams = totalteams + 1;
729 totalteams = totalteams + 1;
731 totalteams = totalteams + 1;
733 if(cvar("bot_vs_human"))
739 error("Too few teams available for domination\n");
741 error("Too few teams available for ctf\n");
743 error("Too few teams available for key hunt\n");
745 error("Too few teams available for team deathmatch\n");
748 // count how many players are in each team
752 GetTeamCounts(world);
754 // c1...c4 now have counts of each team
755 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
758 smallestteam_count = 999999999;
759 smallestteam_score = 999999999;
761 // 2 gives priority to what team you're already on, 1 goes in order
762 // 2 doesn't seem to work though...
766 //if(pl.classname != "player")
767 if(clienttype(pl) != CLIENTTYPE_BOT)
769 c1 -= cb1 * 255.0/256;
770 c2 -= cb2 * 255.0/256;
771 c3 -= cb3 * 255.0/256;
772 c4 -= cb4 * 255.0/256;
775 if(balance_type == 1)
777 if(c1 >= 0 && (c1 < smallestteam_count || (c1 <= smallestteam_count && team1_score < smallestteam_score)))
780 smallestteam_count = c1;
781 smallestteam_score = team1_score;
783 if(c2 >= 0 && (c2 < smallestteam_count || (c2 <= smallestteam_count && team2_score < smallestteam_score)))
786 smallestteam_count = c2;
787 smallestteam_score = team2_score;
789 if(c3 >= 0 && (c3 < smallestteam_count || (c3 <= smallestteam_count && team3_score < smallestteam_score)))
792 smallestteam_count = c3;
793 smallestteam_score = team3_score;
795 if(c4 >= 0 && (c4 < smallestteam_count || (c4 <= smallestteam_count && team4_score < smallestteam_score)))
798 smallestteam_count = c4;
799 smallestteam_score = team4_score;
804 if(c1 >= 0 && (c1 < smallestteam_count ||
805 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
808 smallestteam_count = c1;
810 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
811 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
814 smallestteam_count = c2;
816 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
817 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
820 smallestteam_count = c3;
822 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
823 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
826 smallestteam_count = c4;
833 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
835 float smallest, selectedteam;
837 // don't join a team if we're not playing a team game
838 if(!cvar("teamplay") && !g_domination && !g_ctf && !g_keyhunt)
841 // find out what teams are available
842 CheckAllowedTeams(pl);
847 if(cvar("g_domination_default_teams") < 3)
849 if(cvar("g_domination_default_teams") < 4)
853 // if we don't care what team he ends up on, put him on whatever team he entered as.
854 // if he's not on a valid team, then let other code put him on the smallest team
857 if( c1 >= 0 && pl.team == COLOR_TEAM1)
858 selectedteam = pl.team;
859 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
860 selectedteam = pl.team;
861 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
862 selectedteam = pl.team;
863 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
864 selectedteam = pl.team;
869 if(!only_return_best)
871 SetPlayerColors(pl, selectedteam - 1);
876 // otherwise end up on the smallest team (handled below)
879 smallest = FindSmallestTeam(pl, TRUE);
882 if(!only_return_best)
884 TeamchangeFrags(self);
887 SetPlayerColors(pl, COLOR_TEAM1 - 1);
889 else if(smallest == 2)
891 SetPlayerColors(pl, COLOR_TEAM2 - 1);
893 else if(smallest == 3)
895 SetPlayerColors(pl, COLOR_TEAM3 - 1);
897 else if(smallest == 4)
899 SetPlayerColors(pl, COLOR_TEAM4 - 1);
903 error("smallest team: invalid team\n");
906 if(pl.deadflag == DEAD_NO)
907 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
913 //void() ctf_playerchanged;
914 void SV_ChangeTeam(float _color)
916 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
918 // in normal deathmatch we can just apply the color and we're done
919 if(!cvar("teamplay")) {
920 SetPlayerColors(self, _color);
924 scolor = self.clientcolors & 0x0F;
925 dcolor = _color & 0x0F;
927 if(scolor == COLOR_TEAM1 - 1)
929 else if(scolor == COLOR_TEAM2 - 1)
931 else if(scolor == COLOR_TEAM3 - 1)
933 else if(scolor == COLOR_TEAM4 - 1)
935 if(dcolor == COLOR_TEAM1 - 1)
937 else if(dcolor == COLOR_TEAM2 - 1)
939 else if(dcolor == COLOR_TEAM3 - 1)
941 else if(dcolor == COLOR_TEAM4 - 1)
944 CheckAllowedTeams(self);
946 if(dteam == 1 && c1 < 0) dteam = 4;
947 if(dteam == 4 && c4 < 0) dteam = 3;
948 if(dteam == 3 && c3 < 0) dteam = 2;
949 if(dteam == 2 && c2 < 0) dteam = 1;
951 // not changing teams
954 //bprint("same team change\n");
955 SetPlayerTeam(self, dteam, steam, TRUE);
961 if(cvar("g_campaign") || cvar("g_changeteam_banned"))
963 sprint(self, "Team changes not allowed\n");
964 return; // changing teams is not allowed
967 if(!cvar("g_campaign") && cvar("g_balance_teams_prevent_imbalance"))
969 // only allow changing to a smaller or equal size team
971 // find out what teams are available
972 //CheckAllowedTeams();
973 // count how many players on each team
974 GetTeamCounts(world);
977 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
982 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
987 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
992 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
999 sprint(self, "Cannot change to an invalid team\n");
1004 // get starting team
1005 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
1007 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
1009 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
1011 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
1014 if(scount) // started at a valid, nonempty team
1016 // check if we're trying to change to a larger team that doens't have bots to swap with
1017 if(dcount >= scount && dbotcount <= 0)
1019 sprint(self, "Cannot change to a larger team\n");
1020 return; // can't change to a larger team
1026 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
1028 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1030 // reduce frags during a team change
1031 TeamchangeFrags(self);
1034 SetPlayerTeam(self, dteam, steam, FALSE);
1036 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1038 // kill player when changing teams
1039 if(self.deadflag == DEAD_NO)
1040 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
1042 //ctf_playerchanged();
1045 void ShufflePlayerOutOfTeam (float source_team)
1047 float smallestteam, smallestteam_count, steam;
1048 float lowest_bot_score, lowest_player_score;
1049 entity head, lowest_bot, lowest_player, selected;
1052 smallestteam_count = 999999999;
1054 if(c1 >= 0 && c1 < smallestteam_count)
1057 smallestteam_count = c1;
1059 if(c2 >= 0 && c2 < smallestteam_count)
1062 smallestteam_count = c2;
1064 if(c3 >= 0 && c3 < smallestteam_count)
1067 smallestteam_count = c3;
1069 if(c4 >= 0 && c4 < smallestteam_count)
1072 smallestteam_count = c4;
1077 bprint("warning: no smallest team\n");
1081 if(source_team == 1)
1082 steam = COLOR_TEAM1;
1083 else if(source_team == 2)
1084 steam = COLOR_TEAM2;
1085 else if(source_team == 3)
1086 steam = COLOR_TEAM3;
1087 else if(source_team == 4)
1088 steam = COLOR_TEAM4;
1091 lowest_bot_score = 999999999;
1092 lowest_player = world;
1093 lowest_player_score = 999999999;
1095 // find the lowest-scoring player & bot of that team
1096 FOR_EACH_PLAYER(head)
1098 if(head.team == steam)
1102 if(head.totalfrags < lowest_bot_score)
1105 lowest_bot_score = head.totalfrags;
1110 if(head.totalfrags < lowest_player_score)
1112 lowest_player = head;
1113 lowest_player_score = head.totalfrags;
1119 // prefers to move a bot...
1120 if(lowest_bot != world)
1121 selected = lowest_bot;
1122 // but it will move a player if it has to
1124 selected = lowest_player;
1125 // don't do anything if it couldn't find anyone
1128 bprint("warning: couldn't find a player to move from team\n");
1132 // smallest team gains a member
1133 if(smallestteam == 1)
1137 else if(smallestteam == 2)
1141 else if(smallestteam == 3)
1145 else if(smallestteam == 4)
1151 bprint("warning: destination team invalid\n");
1154 // source team loses a member
1155 if(source_team == 1)
1159 else if(source_team == 2)
1163 else if(source_team == 3)
1167 else if(source_team == 4)
1173 bprint("warning: source team invalid\n");
1177 // move the player to the new team
1178 TeamchangeFrags(selected);
1179 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1181 if(selected.deadflag == DEAD_NO)
1182 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1183 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1186 float lastRebalanceInfo;
1187 void CauseRebalance(float source_team, float howmany_toomany)
1192 if(IsTeamBalanceForced() == 1)
1194 bprint("Rebalancing Teams\n");
1195 ShufflePlayerOutOfTeam(source_team);
1199 if(howmany_toomany < cvar("g_balance_teams_complain"))
1201 if(time < lastRebalanceInfo + 90)
1203 lastRebalanceInfo = time;
1204 if(source_team == 1)
1205 steam = COLOR_TEAM1;
1206 else if(source_team == 2)
1207 steam = COLOR_TEAM2;
1208 else if(source_team == 3)
1209 steam = COLOR_TEAM3;
1210 else if(source_team == 4)
1211 steam = COLOR_TEAM4;
1212 ServerConsoleEcho(strcat("Team ", ftos(source_team), " too large, complaining."), TRUE);
1213 FOR_EACH_REALPLAYER(head)
1215 if(head.team == steam)
1217 sprint(head, "\{1}\{13}^3SERVER NOTICE:^7 One of you please change teams!\n");
1218 centerprint_atprio(head, CENTERPRIO_REBALANCE, "^3SERVER NOTICE:\n\n^7Someone of you please change teams!");
1224 // part of g_balance_teams_force
1225 // occasionally perform an audit of the teams to make
1226 // sure they're more or less balanced in player count.
1229 float numplayers, numteams, smallest, toomany;
1231 balance = IsTeamBalanceForced();
1235 if(audit_teams_time > time)
1238 audit_teams_time = time + 4 + random();
1240 // bprint("Auditing teams\n");
1242 CheckAllowedTeams(world);
1243 GetTeamCounts(world);
1246 numteams = numplayers = smallest = 0;
1249 numteams = numteams + 1;
1250 numplayers = numplayers + c1;
1255 numteams = numteams + 1;
1256 numplayers = numplayers + c2;
1262 numteams = numteams + 1;
1263 numplayers = numplayers + c3;
1269 numteams = numteams + 1;
1270 numplayers = numplayers + c4;
1276 return; // no players to move around
1278 return; // don't bother shuffling if for some reason there aren't any teams
1280 toomany = smallest + 1;
1282 if(c1 && c1 > toomany)
1283 CauseRebalance(1, c1 - toomany);
1284 if(c2 && c2 > toomany)
1285 CauseRebalance(2, c2 - toomany);
1286 if(c3 && c3 > toomany)
1287 CauseRebalance(3, c3 - toomany);
1288 if(c4 && c4 > toomany)
1289 CauseRebalance(4, c4 - toomany);
1291 // if teams are still unbalanced, balance them further in the next audit,
1292 // which will happen sooner (keep doing rapid audits until things are in order)
1293 audit_teams_time = time + 0.7 + random()*0.3;
1298 // code from here on is just to support maps that don't have team entities
1299 void tdm_spawnteam (string teamname, float teamcolor)
1303 e.classname = "tdm_team";
1304 e.netname = teamname;
1309 // spawn some default teams if the map is not set up for tdm
1310 void tdm_spawnteams()
1314 numteams = cvar("g_tdm_teams");
1316 tdm_spawnteam("Red", COLOR_TEAM1-1);
1317 tdm_spawnteam("Blue", COLOR_TEAM2-1);
1319 tdm_spawnteam("Yellow", COLOR_TEAM3-1);
1321 tdm_spawnteam("Pink", COLOR_TEAM4-1);
1324 void tdm_delayedinit()
1326 self.think = SUB_Remove;
1327 self.nextthink = time;
1328 // if no teams are found, spawn defaults
1329 if (find(world, classname, "tdm_team") == world)
1337 e.think = tdm_delayedinit;
1338 e.nextthink = time + 0.1;