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()
145 if(!scores_initialized)
146 ScoreRules_generic();
149 void ActivateTeamplay()
151 float teamplay_default;
152 teamplay_default = cvar("teamplay_default");
155 cvar_set("teamplay", ftos(teamplay_default));
157 cvar_set("teamplay", "3");
162 void InitGameplayMode()
164 float fraglimit_override, timelimit_override;
169 cvar_set("teamplay", "0");
171 // make sure only ONE type is selected
176 MapInfo_LoadMapSettings(mapname);
177 clientstuff = strzone(MapInfo_Map_clientstuff);
178 MapInfo_ClearTemps();
180 // in case mapinfo switched the type
184 // set both here, gamemode can override it later
185 timelimit_override = cvar("timelimit_override");
186 fraglimit_override = cvar("fraglimit_override");
190 game = GAME_DEATHMATCH;
191 gamemode_name = "Deathmatch";
196 game = GAME_TEAM_DEATHMATCH;
197 gamemode_name = "Team Deathmatch";
204 game = GAME_DOMINATION;
205 gamemode_name = "Domination";
207 fraglimit_override = cvar("g_domination_point_limit");
214 gamemode_name = "Capture the Flag";
216 g_ctf_win_mode = cvar("g_ctf_win_mode");
217 if(g_ctf_win_mode == 2)
218 fraglimit_override = cvar("g_ctf_capture_limit");
220 fraglimit_override = cvar("capturelimit_override");
226 game = GAME_RUNEMATCH;
227 gamemode_name = "Rune Match";
228 if(cvar("deathmatch_force_teamplay"))
230 fraglimit_override = cvar("g_runematch_point_limit");
237 gamemode_name = "Last Man Standing";
238 fraglimit_override = cvar("g_lms_lives_override");
239 if(fraglimit_override == 0)
240 fraglimit_override = -1;
241 lms_lowest_lives = 9999;
249 gamemode_name = "Arena";
250 fraglimit_override = cvar("g_arena_point_limit");
251 maxspawned = cvar("g_arena_maxspawned");
254 arena_roundbased = cvar("g_arena_roundbased");
260 gamemode_name = "Key Hunt";
262 fraglimit_override = cvar("g_keyhunt_point_limit");
269 gamemode_name = "Assault";
275 game = GAME_ONSLAUGHT;
276 gamemode_name = "Onslaught";
284 gamemode_name = "Race";
285 g_race_qualifying = cvar("g_race_qualifying");
287 // In campaign, only this mode makes sense...
288 if(cvar("g_campaign"))
289 g_race_qualifying = 1;
291 if(cvar("g_race_teams"))
293 g_race_qualifying = 0; // not supported!
295 race_teams = bound(2, cvar("g_race_teams"), 4);
300 if(g_race_qualifying == 1)
301 fraglimit_override = 0;
303 fraglimit_override = cvar("g_race_laps_limit");
305 if(g_race_qualifying)
306 independent_players = 1;
311 // save it (for the next startup)
312 cvar_set("gamecfg", ftos(game));
314 cache_mutatormsg = strzone("");
315 cache_motd = strzone("");
316 cache_lastmutatormsg = strzone("");
317 cache_lastmotd = strzone("");
319 // enforce the server's universal frag/time limits
320 if(!cvar("g_campaign"))
322 if(fraglimit_override >= 0)
323 cvar_set("fraglimit", ftos(fraglimit_override));
324 if(timelimit_override >= 0)
325 cvar_set("timelimit", ftos(timelimit_override));
328 if(g_race && g_race_qualifying == 2)
330 race_fraglimit = cvar("fraglimit");
331 cvar_set("fraglimit", "0");
334 InitializeEntity(world, default_delayedinit, INITPRIO_GAMETYPE_FALLBACK);
337 string GetClientVersionMessage() {
338 local string versionmsg;
339 if (self.version_mismatch) {
340 if(self.version < cvar("gameversion")) {
341 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
343 versionmsg = "^3This server is using an outdated Nexuiz version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
346 versionmsg = "^2client version and server version are compatible.^8";
352 void PrintWelcomeMessage(entity pl)
354 string s, mutator, modifications;
356 /*if(self.welcomemessage_time > time)
358 self.welcomemessage_time = time + 0.8; */
360 if(self.cvar_scr_centertime == 0) return;
361 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
362 if(self.welcomemessage_time > time) return;
363 self.welcomemessage_time = time + self.cvar_scr_centertime * 0.6;
366 if(cvar("g_campaign"))
368 centerprint(pl, campaign_message);
372 if(!self.BUTTON_INFO)
374 // TODO get rid of this too
375 local string specString;
376 specString = NEWLINES;
377 if(time < restart_countdown) //also show the countdown when being a spectator
378 specString = strcat(specString, "\n\n^1Game starts in ", ftos(restartAnnouncer.cnt + 1), " seconds^7");
379 else if (timeoutStatus != 0)
380 specString = strcat(specString, "\n\n", getTimeoutText(1));
383 return centerprint_atprio(self, CENTERPRIO_SPAM, specString);
387 mutator = "^2Minstagib ^1";
389 mutator = "^2Instagib ^1";
390 else if(g_rocketarena)
391 mutator = "^2Rocketarena ^1";
393 mutator = "^2No Items Nexuiz ^1";
396 // to protect against unheedingly made changes
398 modifications = strcat(modifications, ", ");
400 modifications = "midair";
404 modifications = strcat(modifications, ", ");
406 modifications = strcat(modifications, "vampire");
408 if(g_laserguided_missile) {
410 modifications = strcat(modifications, ", ");
412 modifications = strcat(modifications, "laser-guided-missiles");
416 modifications = strcat(modifications, ", ");
418 modifications = strcat(modifications, "Tournament");
421 local string versionmessage;
422 versionmessage = GetClientVersionMessage();
424 s = strcat(s, NEWLINES, "This is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
425 s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
427 if(modifications != "")
428 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
431 if(!tourneyInMatchStage)
432 s = strcat(s, "\n^8The game is currently in ^3warmup-stage ^8!\n");
434 s = strcat(s, "\n^8The game is currently in ^3match-stage ^8!\n");
437 if(time < restart_countdown)
438 s = strcat(s, "\n^1Game starts in ", ftos(restartAnnouncer.cnt + 1), " seconds^7");
440 if(timeoutStatus != 0)
441 s = strcat(s, "\n\n", getTimeoutText(1));
443 if (g_grappling_hook)
444 s = strcat(s, "\n\n^8grappling hook is enabled, press 'e' to use it\n");
446 if(cache_lastmutatormsg != cvar_string("g_mutatormsg"))
448 if(cache_lastmutatormsg)
449 strunzone(cache_lastmutatormsg);
451 strunzone(cache_mutatormsg);
452 cache_lastmutatormsg = strzone(cvar_string("g_mutatormsg"));
453 cache_mutatormsg = strzone(wordwrap(cache_lastmutatormsg, 50));
456 if (cache_mutatormsg != "") {
457 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
460 if(cache_lastmotd != cvar_string("sv_motd"))
463 strunzone(cache_lastmotd);
465 strunzone(cache_motd);
466 cache_lastmotd = strzone(cvar_string("sv_motd"));
467 cache_motd = strzone(wordwrap(cache_lastmotd, 50));
470 if (cache_motd != "") {
471 s = strcat(s, "\n\n^8MOTD: ^7", cache_motd);
480 void SetPlayerColors(entity pl, float _color)
484 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
486 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
487 pl.clientcolors = 16*cl + cl;*/
490 pants = _color & 0x0F;
491 shirt = _color & 0xF0;
495 setcolor(pl, 16*pants + pants);
497 setcolor(pl, shirt + pants);
501 void SetPlayerTeam(entity pl, float t, float s, float noprint)
506 _color = COLOR_TEAM4 - 1;
508 _color = COLOR_TEAM3 - 1;
510 _color = COLOR_TEAM2 - 1;
512 _color = COLOR_TEAM1 - 1;
514 SetPlayerColors(pl,_color);
516 if(!noprint && t != s)
518 //bprint(pl.netname, " has changed to ", TeamNoName(t), "\n");
519 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
531 // set c1...c4 to show what teams are allowed
532 void CheckAllowedTeams (entity for_whom)
541 c1 = c2 = c3 = c4 = -1;
542 cb1 = cb2 = cb3 = cb4 = 0;
544 // onslaught is special
547 head = findchain(classname, "onslaught_generator");
550 if (head.team == COLOR_TEAM1) c1 = 0;
551 if (head.team == COLOR_TEAM2) c2 = 0;
552 if (head.team == COLOR_TEAM3) c3 = 0;
553 if (head.team == COLOR_TEAM4) c4 = 0;
560 teament_name = "dom_team";
562 teament_name = "ctf_team";
564 teament_name = "tdm_team";
567 c1 = c2 = 0; // Assault always has 2 teams
572 // cover anything else by treating it like tdm with no teams spawned
578 dm = cvar("g_tdm_teams");
580 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
584 c1 = c2 = c3 = c4 = 0;
597 // first find out what teams are allowed
598 head = find(world, classname, teament_name);
601 if(!(g_domination && head.netname == ""))
603 if(head.team == COLOR_TEAM1)
607 if(head.team == COLOR_TEAM2)
611 if(head.team == COLOR_TEAM3)
615 if(head.team == COLOR_TEAM4)
620 head = find(head, classname, teament_name);
625 if(cvar("bot_vs_human") > 0)
628 if(clienttype(for_whom) == CLIENTTYPE_BOT)
633 else if(cvar("bot_vs_human") < 0)
636 if(clienttype(for_whom) == CLIENTTYPE_BOT)
644 float PlayerValue(entity p)
646 if(IsTeamBalanceForced() == 1)
651 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
652 // teams that are allowed will now have their player counts stored in c1...c4
653 void GetTeamCounts(entity ignore)
657 // now count how many players are on each team already
659 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
660 // also remember the lowest-scoring player
662 FOR_EACH_PLAYER(head)
664 if(head != ignore)// && head.netname != "")
666 value = PlayerValue(head);
667 if(clienttype(head) == CLIENTTYPE_BOT)
671 if(head.team == COLOR_TEAM1)
679 if(head.team == COLOR_TEAM2)
687 if(head.team == COLOR_TEAM3)
695 if(head.team == COLOR_TEAM4)
707 // returns # of smallest team (1, 2, 3, 4)
708 // NOTE: Assumes CheckAllowedTeams has already been called!
709 float FindSmallestTeam(entity pl, float ignore_pl)
711 float totalteams, smallestteam, smallestteam_count, smallestteam_score, balance_type;
714 // find out what teams are available
715 //CheckAllowedTeams();
717 // make sure there are at least 2 teams to join
719 totalteams = totalteams + 1;
721 totalteams = totalteams + 1;
723 totalteams = totalteams + 1;
725 totalteams = totalteams + 1;
727 if(cvar("bot_vs_human"))
733 error("Too few teams available for domination\n");
735 error("Too few teams available for ctf\n");
737 error("Too few teams available for key hunt\n");
739 error("Too few teams available for team deathmatch\n");
742 // count how many players are in each team
746 GetTeamCounts(world);
748 // c1...c4 now have counts of each team
749 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
752 smallestteam_count = 999999999;
753 smallestteam_score = 999999999;
755 // 2 gives priority to what team you're already on, 1 goes in order
756 // 2 doesn't seem to work though...
760 //if(pl.classname != "player")
761 if(clienttype(pl) != CLIENTTYPE_BOT)
763 c1 -= cb1 * 255.0/256;
764 c2 -= cb2 * 255.0/256;
765 c3 -= cb3 * 255.0/256;
766 c4 -= cb4 * 255.0/256;
769 if(balance_type == 1)
771 if(c1 >= 0 && (c1 < smallestteam_count || (c1 <= smallestteam_count && team1_score < smallestteam_score)))
774 smallestteam_count = c1;
775 smallestteam_score = team1_score;
777 if(c2 >= 0 && (c2 < smallestteam_count || (c2 <= smallestteam_count && team2_score < smallestteam_score)))
780 smallestteam_count = c2;
781 smallestteam_score = team2_score;
783 if(c3 >= 0 && (c3 < smallestteam_count || (c3 <= smallestteam_count && team3_score < smallestteam_score)))
786 smallestteam_count = c3;
787 smallestteam_score = team3_score;
789 if(c4 >= 0 && (c4 < smallestteam_count || (c4 <= smallestteam_count && team4_score < smallestteam_score)))
792 smallestteam_count = c4;
793 smallestteam_score = team4_score;
798 if(c1 >= 0 && (c1 < smallestteam_count ||
799 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
802 smallestteam_count = c1;
804 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
805 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
808 smallestteam_count = c2;
810 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
811 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
814 smallestteam_count = c3;
816 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
817 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
820 smallestteam_count = c4;
827 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
829 float smallest, selectedteam;
831 // don't join a team if we're not playing a team game
832 if(!cvar("teamplay") && !g_domination && !g_ctf && !g_keyhunt)
835 // find out what teams are available
836 CheckAllowedTeams(pl);
841 if(cvar("g_domination_default_teams") < 3)
843 if(cvar("g_domination_default_teams") < 4)
847 // if we don't care what team he ends up on, put him on whatever team he entered as.
848 // if he's not on a valid team, then let other code put him on the smallest team
851 if( c1 >= 0 && pl.team == COLOR_TEAM1)
852 selectedteam = pl.team;
853 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
854 selectedteam = pl.team;
855 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
856 selectedteam = pl.team;
857 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
858 selectedteam = pl.team;
863 if(!only_return_best)
865 SetPlayerColors(pl, selectedteam - 1);
870 // otherwise end up on the smallest team (handled below)
873 smallest = FindSmallestTeam(pl, TRUE);
876 if(!only_return_best)
878 TeamchangeFrags(self);
881 SetPlayerColors(pl, COLOR_TEAM1 - 1);
883 else if(smallest == 2)
885 SetPlayerColors(pl, COLOR_TEAM2 - 1);
887 else if(smallest == 3)
889 SetPlayerColors(pl, COLOR_TEAM3 - 1);
891 else if(smallest == 4)
893 SetPlayerColors(pl, COLOR_TEAM4 - 1);
897 error("smallest team: invalid team\n");
900 if(pl.deadflag == DEAD_NO)
901 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
907 //void() ctf_playerchanged;
908 void SV_ChangeTeam(float _color)
910 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
912 // in normal deathmatch we can just apply the color and we're done
913 if(!cvar("teamplay")) {
914 SetPlayerColors(self, _color);
918 scolor = self.clientcolors & 0x0F;
919 dcolor = _color & 0x0F;
921 if(scolor == COLOR_TEAM1 - 1)
923 else if(scolor == COLOR_TEAM2 - 1)
925 else if(scolor == COLOR_TEAM3 - 1)
927 else if(scolor == COLOR_TEAM4 - 1)
929 if(dcolor == COLOR_TEAM1 - 1)
931 else if(dcolor == COLOR_TEAM2 - 1)
933 else if(dcolor == COLOR_TEAM3 - 1)
935 else if(dcolor == COLOR_TEAM4 - 1)
938 CheckAllowedTeams(self);
940 if(dteam == 1 && c1 < 0) dteam = 4;
941 if(dteam == 4 && c4 < 0) dteam = 3;
942 if(dteam == 3 && c3 < 0) dteam = 2;
943 if(dteam == 2 && c2 < 0) dteam = 1;
945 // not changing teams
948 //bprint("same team change\n");
949 SetPlayerTeam(self, dteam, steam, TRUE);
955 if(cvar("g_campaign") || cvar("g_changeteam_banned"))
957 sprint(self, "Team changes not allowed\n");
958 return; // changing teams is not allowed
961 if(!cvar("g_campaign") && cvar("g_balance_teams_prevent_imbalance"))
963 // only allow changing to a smaller or equal size team
965 // find out what teams are available
966 //CheckAllowedTeams();
967 // count how many players on each team
968 GetTeamCounts(world);
971 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
976 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
981 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
986 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
993 sprint(self, "Cannot change to an invalid team\n");
999 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
1001 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
1003 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
1005 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
1008 if(scount) // started at a valid, nonempty team
1010 // check if we're trying to change to a larger team that doens't have bots to swap with
1011 if(dcount >= scount && dbotcount <= 0)
1013 sprint(self, "Cannot change to a larger team\n");
1014 return; // can't change to a larger team
1020 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
1022 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1024 // reduce frags during a team change
1025 TeamchangeFrags(self);
1028 SetPlayerTeam(self, dteam, steam, FALSE);
1030 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1032 // kill player when changing teams
1033 if(self.deadflag == DEAD_NO)
1034 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
1036 //ctf_playerchanged();
1039 void ShufflePlayerOutOfTeam (float source_team)
1041 float smallestteam, smallestteam_count, steam;
1042 float lowest_bot_score, lowest_player_score;
1043 entity head, lowest_bot, lowest_player, selected;
1046 smallestteam_count = 999999999;
1048 if(c1 >= 0 && c1 < smallestteam_count)
1051 smallestteam_count = c1;
1053 if(c2 >= 0 && c2 < smallestteam_count)
1056 smallestteam_count = c2;
1058 if(c3 >= 0 && c3 < smallestteam_count)
1061 smallestteam_count = c3;
1063 if(c4 >= 0 && c4 < smallestteam_count)
1066 smallestteam_count = c4;
1071 bprint("warning: no smallest team\n");
1075 if(source_team == 1)
1076 steam = COLOR_TEAM1;
1077 else if(source_team == 2)
1078 steam = COLOR_TEAM2;
1079 else if(source_team == 3)
1080 steam = COLOR_TEAM3;
1081 else if(source_team == 4)
1082 steam = COLOR_TEAM4;
1085 lowest_bot_score = 999999999;
1086 lowest_player = world;
1087 lowest_player_score = 999999999;
1089 // find the lowest-scoring player & bot of that team
1090 FOR_EACH_PLAYER(head)
1092 if(head.team == steam)
1096 if(head.totalfrags < lowest_bot_score)
1099 lowest_bot_score = head.totalfrags;
1104 if(head.totalfrags < lowest_player_score)
1106 lowest_player = head;
1107 lowest_player_score = head.totalfrags;
1113 // prefers to move a bot...
1114 if(lowest_bot != world)
1115 selected = lowest_bot;
1116 // but it will move a player if it has to
1118 selected = lowest_player;
1119 // don't do anything if it couldn't find anyone
1122 bprint("warning: couldn't find a player to move from team\n");
1126 // smallest team gains a member
1127 if(smallestteam == 1)
1131 else if(smallestteam == 2)
1135 else if(smallestteam == 3)
1139 else if(smallestteam == 4)
1145 bprint("warning: destination team invalid\n");
1148 // source team loses a member
1149 if(source_team == 1)
1153 else if(source_team == 2)
1157 else if(source_team == 3)
1161 else if(source_team == 4)
1167 bprint("warning: source team invalid\n");
1171 // move the player to the new team
1172 TeamchangeFrags(selected);
1173 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1175 if(selected.deadflag == DEAD_NO)
1176 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1177 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1180 float lastRebalanceInfo;
1181 void CauseRebalance(float source_team, float howmany_toomany)
1186 if(IsTeamBalanceForced() == 1)
1188 bprint("Rebalancing Teams\n");
1189 ShufflePlayerOutOfTeam(source_team);
1193 if(howmany_toomany < cvar("g_balance_teams_complain"))
1195 if(time < lastRebalanceInfo + 90)
1197 lastRebalanceInfo = time;
1198 if(source_team == 1)
1199 steam = COLOR_TEAM1;
1200 else if(source_team == 2)
1201 steam = COLOR_TEAM2;
1202 else if(source_team == 3)
1203 steam = COLOR_TEAM3;
1204 else if(source_team == 4)
1205 steam = COLOR_TEAM4;
1206 ServerConsoleEcho(strcat("Team ", ftos(source_team), " too large, complaining."), TRUE);
1207 FOR_EACH_REALPLAYER(head)
1209 if(head.team == steam)
1211 sprint(head, "\{1}\{13}^3SERVER NOTICE:^7 One of you please change teams!\n");
1212 centerprint_atprio(head, CENTERPRIO_REBALANCE, "^3SERVER NOTICE:\n\n^7Someone of you please change teams!");
1218 // part of g_balance_teams_force
1219 // occasionally perform an audit of the teams to make
1220 // sure they're more or less balanced in player count.
1223 float numplayers, numteams, smallest, toomany;
1225 balance = IsTeamBalanceForced();
1229 if(audit_teams_time > time)
1232 audit_teams_time = time + 4 + random();
1234 // bprint("Auditing teams\n");
1236 CheckAllowedTeams(world);
1237 GetTeamCounts(world);
1240 numteams = numplayers = smallest = 0;
1243 numteams = numteams + 1;
1244 numplayers = numplayers + c1;
1249 numteams = numteams + 1;
1250 numplayers = numplayers + c2;
1256 numteams = numteams + 1;
1257 numplayers = numplayers + c3;
1263 numteams = numteams + 1;
1264 numplayers = numplayers + c4;
1270 return; // no players to move around
1272 return; // don't bother shuffling if for some reason there aren't any teams
1274 toomany = smallest + 1;
1276 if(c1 && c1 > toomany)
1277 CauseRebalance(1, c1 - toomany);
1278 if(c2 && c2 > toomany)
1279 CauseRebalance(2, c2 - toomany);
1280 if(c3 && c3 > toomany)
1281 CauseRebalance(3, c3 - toomany);
1282 if(c4 && c4 > toomany)
1283 CauseRebalance(4, c4 - toomany);
1285 // if teams are still unbalanced, balance them further in the next audit,
1286 // which will happen sooner (keep doing rapid audits until things are in order)
1287 audit_teams_time = time + 0.7 + random()*0.3;
1292 // code from here on is just to support maps that don't have team entities
1293 void tdm_spawnteam (string teamname, float teamcolor)
1297 e.classname = "tdm_team";
1298 e.netname = teamname;
1303 // spawn some default teams if the map is not set up for tdm
1304 void tdm_spawnteams()
1308 numteams = cvar("g_tdm_teams");
1310 tdm_spawnteam("Red", COLOR_TEAM1-1);
1311 tdm_spawnteam("Blue", COLOR_TEAM2-1);
1313 tdm_spawnteam("Yellow", COLOR_TEAM3-1);
1315 tdm_spawnteam("Pink", COLOR_TEAM4-1);
1318 void tdm_delayedinit()
1320 // if no teams are found, spawn defaults
1321 if (find(world, classname, "tdm_team") == world)
1327 InitializeEntity(world, tdm_delayedinit, INITPRIO_GAMETYPE);