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));
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
175 MapInfo_LoadMapSettings(mapname);
176 clientstuff = strzone(MapInfo_Map_clientstuff);
177 MapInfo_ClearTemps();
179 // in case mapinfo switched the type
182 // set both here, gamemode can override it later
183 timelimit_override = cvar("timelimit_override");
184 fraglimit_override = cvar("fraglimit_override");
188 game = GAME_DEATHMATCH;
189 gamemode_name = "Deathmatch";
194 game = GAME_TEAM_DEATHMATCH;
195 gamemode_name = "Team Deathmatch";
202 game = GAME_DOMINATION;
203 gamemode_name = "Domination";
205 fraglimit_override = cvar("g_domination_point_limit");
212 gamemode_name = "Capture the Flag";
214 g_ctf_win_mode = cvar("g_ctf_win_mode");
215 if(g_ctf_win_mode == 2)
216 fraglimit_override = cvar("g_ctf_capture_limit");
218 fraglimit_override = cvar("capturelimit_override");
224 game = GAME_RUNEMATCH;
225 gamemode_name = "Rune Match";
226 if(cvar("deathmatch_force_teamplay"))
228 fraglimit_override = cvar("g_runematch_point_limit");
235 gamemode_name = "Last Man Standing";
236 fraglimit_override = cvar("g_lms_lives_override");
237 if(fraglimit_override == 0)
238 fraglimit_override = -1;
239 lms_lowest_lives = 9999;
247 gamemode_name = "Arena";
248 fraglimit_override = cvar("g_arena_point_limit");
249 maxspawned = cvar("g_arena_maxspawned");
252 arena_roundbased = cvar("g_arena_roundbased");
258 gamemode_name = "Key Hunt";
260 fraglimit_override = cvar("g_keyhunt_point_limit");
267 gamemode_name = "Assault";
273 game = GAME_ONSLAUGHT;
274 gamemode_name = "Onslaught";
282 gamemode_name = "Race";
283 g_race_qualifying = cvar("g_race_qualifying");
285 // In campaign, only this mode makes sense...
286 if(cvar("g_campaign"))
287 g_race_qualifying = 1;
289 if(cvar("g_race_teams"))
291 g_race_qualifying = 0; // not supported!
293 race_teams = bound(2, cvar("g_race_teams"), 4);
298 if(g_race_qualifying == 1)
299 fraglimit_override = 0;
301 fraglimit_override = cvar("g_race_laps_limit");
303 if(g_race_qualifying)
304 independent_players = 1;
309 // save it (for the next startup)
310 cvar_set("gamecfg", ftos(game));
312 cache_mutatormsg = strzone("");
313 cache_motd = strzone("");
314 cache_lastmutatormsg = strzone("");
315 cache_lastmotd = strzone("");
317 // enforce the server's universal frag/time limits
318 if(!cvar("g_campaign"))
320 if(fraglimit_override >= 0)
321 cvar_set("fraglimit", ftos(fraglimit_override));
322 if(timelimit_override >= 0)
323 cvar_set("timelimit", ftos(timelimit_override));
326 if(g_race && g_race_qualifying == 2)
328 race_fraglimit = cvar("fraglimit");
329 cvar_set("fraglimit", "0");
332 InitializeEntity(world, default_delayedinit, INITPRIO_GAMETYPE_FALLBACK);
335 string GetClientVersionMessage() {
336 local string versionmsg;
337 if (self.version_mismatch) {
338 if(self.version < cvar("gameversion")) {
339 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
341 versionmsg = "^3This server is using an outdated Nexuiz version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
344 versionmsg = "^2client version and server version are compatible.^8";
350 void PrintWelcomeMessage(entity pl)
352 string s, mutator, modifications;
354 /*if(self.welcomemessage_time > time)
356 self.welcomemessage_time = time + 0.8; */
358 if(self.cvar_scr_centertime == 0) return;
359 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
360 if(self.welcomemessage_time > time) return;
361 self.welcomemessage_time = time + self.cvar_scr_centertime * 0.6;
364 if(cvar("g_campaign"))
366 centerprint(pl, campaign_message);
370 if(!self.BUTTON_INFO)
372 // TODO get rid of this too
373 local string specString;
374 specString = NEWLINES;
375 if(time < restart_countdown) //also show the countdown when being a spectator
376 specString = strcat(specString, "\n\n^1Game starts in ", ftos(restartAnnouncer.cnt + 1), " seconds^7");
377 else if (timeoutStatus != 0)
378 specString = strcat(specString, "\n\n", getTimeoutText(1));
381 return centerprint_atprio(self, CENTERPRIO_SPAM, specString);
385 mutator = "^2Minstagib ^1";
386 else if(g_rocketarena)
387 mutator = "^2Rocketarena ^1";
389 mutator = "^2No Items Nexuiz ^1";
392 // to protect against unheedingly made changes
394 modifications = strcat(modifications, ", ");
396 modifications = "cloaked";
400 modifications = strcat(modifications, ", ");
402 modifications = strcat(modifications, "footsteps");
406 modifications = strcat(modifications, ", ");
408 modifications = strcat(modifications, "midair");
412 modifications = strcat(modifications, ", ");
414 modifications = strcat(modifications, "vampire");
416 if(g_laserguided_missile) {
418 modifications = strcat(modifications, ", ");
420 modifications = strcat(modifications, "laser guided missiles");
422 if(cvar("sv_gravity") < 800) {
424 modifications = strcat(modifications, ", ");
426 modifications = strcat(modifications, "low gravity");
429 local string versionmessage;
430 versionmessage = GetClientVersionMessage();
432 s = strcat(s, NEWLINES, "This is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
433 s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
435 if(modifications != "")
436 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
438 if(time < restart_countdown)
439 s = strcat(s, "\n^1Game starts in ", ftos(restartAnnouncer.cnt + 1), " seconds^7");
441 if(timeoutStatus != 0)
442 s = strcat(s, "\n\n", getTimeoutText(1));
444 if (g_grappling_hook)
445 s = strcat(s, "\n\n^3grappling hook^8 is enabled, press 'e' to use it\n");
447 if(cache_lastmutatormsg != cvar_string("g_mutatormsg"))
449 if(cache_lastmutatormsg)
450 strunzone(cache_lastmutatormsg);
452 strunzone(cache_mutatormsg);
453 cache_lastmutatormsg = strzone(cvar_string("g_mutatormsg"));
454 cache_mutatormsg = strzone(wordwrap(cache_lastmutatormsg, 50));
457 if (cache_mutatormsg != "") {
458 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
461 if(cache_lastmotd != cvar_string("sv_motd"))
464 strunzone(cache_lastmotd);
466 strunzone(cache_motd);
467 cache_lastmotd = strzone(cvar_string("sv_motd"));
468 cache_motd = strzone(wordwrap(cache_lastmotd, 50));
471 if (cache_motd != "") {
472 s = strcat(s, "\n\n^8MOTD: ^7", cache_motd);
481 void SetPlayerColors(entity pl, float _color)
485 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
487 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
488 pl.clientcolors = 16*cl + cl;*/
491 pants = _color & 0x0F;
492 shirt = _color & 0xF0;
496 setcolor(pl, 16*pants + pants);
498 setcolor(pl, shirt + pants);
502 void SetPlayerTeam(entity pl, float t, float s, float noprint)
507 _color = COLOR_TEAM4 - 1;
509 _color = COLOR_TEAM3 - 1;
511 _color = COLOR_TEAM2 - 1;
513 _color = COLOR_TEAM1 - 1;
515 SetPlayerColors(pl,_color);
517 if(!noprint && t != s)
519 //bprint(pl.netname, " has changed to ", TeamNoName(t), "\n");
520 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
532 // set c1...c4 to show what teams are allowed
533 void CheckAllowedTeams (entity for_whom)
542 c1 = c2 = c3 = c4 = -1;
543 cb1 = cb2 = cb3 = cb4 = 0;
545 // onslaught is special
548 head = findchain(classname, "onslaught_generator");
551 if (head.team == COLOR_TEAM1) c1 = 0;
552 if (head.team == COLOR_TEAM2) c2 = 0;
553 if (head.team == COLOR_TEAM3) c3 = 0;
554 if (head.team == COLOR_TEAM4) c4 = 0;
561 teament_name = "dom_team";
563 teament_name = "ctf_team";
565 teament_name = "tdm_team";
568 c1 = c2 = 0; // Assault always has 2 teams
573 // cover anything else by treating it like tdm with no teams spawned
579 dm = cvar("g_tdm_teams");
581 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
585 c1 = c2 = c3 = c4 = 0;
598 // first find out what teams are allowed
599 head = find(world, classname, teament_name);
602 if(!(g_domination && head.netname == ""))
604 if(head.team == COLOR_TEAM1)
608 if(head.team == COLOR_TEAM2)
612 if(head.team == COLOR_TEAM3)
616 if(head.team == COLOR_TEAM4)
621 head = find(head, classname, teament_name);
626 if(cvar("bot_vs_human") > 0)
629 if(clienttype(for_whom) == CLIENTTYPE_BOT)
634 else if(cvar("bot_vs_human") < 0)
637 if(clienttype(for_whom) == CLIENTTYPE_BOT)
645 float PlayerValue(entity p)
647 if(IsTeamBalanceForced() == 1)
652 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
653 // teams that are allowed will now have their player counts stored in c1...c4
654 void GetTeamCounts(entity ignore)
658 // now count how many players are on each team already
660 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
661 // also remember the lowest-scoring player
663 FOR_EACH_PLAYER(head)
665 if(head != ignore)// && head.netname != "")
667 value = PlayerValue(head);
668 if(clienttype(head) == CLIENTTYPE_BOT)
672 if(head.team == COLOR_TEAM1)
680 if(head.team == COLOR_TEAM2)
688 if(head.team == COLOR_TEAM3)
696 if(head.team == COLOR_TEAM4)
708 // returns # of smallest team (1, 2, 3, 4)
709 // NOTE: Assumes CheckAllowedTeams has already been called!
710 float FindSmallestTeam(entity pl, float ignore_pl)
712 float totalteams, smallestteam, smallestteam_count, smallestteam_score, balance_type;
715 // find out what teams are available
716 //CheckAllowedTeams();
718 // make sure there are at least 2 teams to join
720 totalteams = totalteams + 1;
722 totalteams = totalteams + 1;
724 totalteams = totalteams + 1;
726 totalteams = totalteams + 1;
728 if(cvar("bot_vs_human"))
734 error("Too few teams available for domination\n");
736 error("Too few teams available for ctf\n");
738 error("Too few teams available for key hunt\n");
740 error("Too few teams available for team deathmatch\n");
743 // count how many players are in each team
747 GetTeamCounts(world);
749 // c1...c4 now have counts of each team
750 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
753 smallestteam_count = 999999999;
754 smallestteam_score = 999999999;
756 // 2 gives priority to what team you're already on, 1 goes in order
757 // 2 doesn't seem to work though...
761 //if(pl.classname != "player")
762 if(clienttype(pl) != CLIENTTYPE_BOT)
764 c1 -= cb1 * 255.0/256;
765 c2 -= cb2 * 255.0/256;
766 c3 -= cb3 * 255.0/256;
767 c4 -= cb4 * 255.0/256;
770 if(balance_type == 1)
772 if(c1 >= 0 && (c1 < smallestteam_count || (c1 <= smallestteam_count && team1_score < smallestteam_score)))
775 smallestteam_count = c1;
776 smallestteam_score = team1_score;
778 if(c2 >= 0 && (c2 < smallestteam_count || (c2 <= smallestteam_count && team2_score < smallestteam_score)))
781 smallestteam_count = c2;
782 smallestteam_score = team2_score;
784 if(c3 >= 0 && (c3 < smallestteam_count || (c3 <= smallestteam_count && team3_score < smallestteam_score)))
787 smallestteam_count = c3;
788 smallestteam_score = team3_score;
790 if(c4 >= 0 && (c4 < smallestteam_count || (c4 <= smallestteam_count && team4_score < smallestteam_score)))
793 smallestteam_count = c4;
794 smallestteam_score = team4_score;
799 if(c1 >= 0 && (c1 < smallestteam_count ||
800 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
803 smallestteam_count = c1;
805 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
806 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
809 smallestteam_count = c2;
811 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
812 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
815 smallestteam_count = c3;
817 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
818 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
821 smallestteam_count = c4;
828 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
830 float smallest, selectedteam;
832 // don't join a team if we're not playing a team game
833 if(!cvar("teamplay") && !g_domination && !g_ctf && !g_keyhunt)
836 // find out what teams are available
837 CheckAllowedTeams(pl);
842 if(cvar("g_domination_default_teams") < 3)
844 if(cvar("g_domination_default_teams") < 4)
848 // if we don't care what team he ends up on, put him on whatever team he entered as.
849 // if he's not on a valid team, then let other code put him on the smallest team
852 if( c1 >= 0 && pl.team == COLOR_TEAM1)
853 selectedteam = pl.team;
854 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
855 selectedteam = pl.team;
856 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
857 selectedteam = pl.team;
858 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
859 selectedteam = pl.team;
864 if(!only_return_best)
866 SetPlayerColors(pl, selectedteam - 1);
871 // otherwise end up on the smallest team (handled below)
874 smallest = FindSmallestTeam(pl, TRUE);
877 if(!only_return_best)
879 TeamchangeFrags(self);
882 SetPlayerColors(pl, COLOR_TEAM1 - 1);
884 else if(smallest == 2)
886 SetPlayerColors(pl, COLOR_TEAM2 - 1);
888 else if(smallest == 3)
890 SetPlayerColors(pl, COLOR_TEAM3 - 1);
892 else if(smallest == 4)
894 SetPlayerColors(pl, COLOR_TEAM4 - 1);
898 error("smallest team: invalid team\n");
901 if(pl.deadflag == DEAD_NO)
902 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
908 //void() ctf_playerchanged;
909 void SV_ChangeTeam(float _color)
911 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
913 // in normal deathmatch we can just apply the color and we're done
914 if(!cvar("teamplay")) {
915 SetPlayerColors(self, _color);
919 scolor = self.clientcolors & 0x0F;
920 dcolor = _color & 0x0F;
922 if(scolor == COLOR_TEAM1 - 1)
924 else if(scolor == COLOR_TEAM2 - 1)
926 else if(scolor == COLOR_TEAM3 - 1)
928 else if(scolor == COLOR_TEAM4 - 1)
930 if(dcolor == COLOR_TEAM1 - 1)
932 else if(dcolor == COLOR_TEAM2 - 1)
934 else if(dcolor == COLOR_TEAM3 - 1)
936 else if(dcolor == COLOR_TEAM4 - 1)
939 CheckAllowedTeams(self);
941 if(dteam == 1 && c1 < 0) dteam = 4;
942 if(dteam == 4 && c4 < 0) dteam = 3;
943 if(dteam == 3 && c3 < 0) dteam = 2;
944 if(dteam == 2 && c2 < 0) dteam = 1;
946 // not changing teams
949 //bprint("same team change\n");
950 SetPlayerTeam(self, dteam, steam, TRUE);
956 if(cvar("g_campaign") || cvar("g_changeteam_banned"))
958 sprint(self, "Team changes not allowed\n");
959 return; // changing teams is not allowed
962 if(!cvar("g_campaign") && cvar("g_balance_teams_prevent_imbalance"))
964 // only allow changing to a smaller or equal size team
966 // find out what teams are available
967 //CheckAllowedTeams();
968 // count how many players on each team
969 GetTeamCounts(world);
972 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
977 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
982 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
987 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
994 sprint(self, "Cannot change to an invalid team\n");
1000 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
1002 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
1004 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
1006 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
1009 if(scount) // started at a valid, nonempty team
1011 // check if we're trying to change to a larger team that doens't have bots to swap with
1012 if(dcount >= scount && dbotcount <= 0)
1014 sprint(self, "Cannot change to a larger team\n");
1015 return; // can't change to a larger team
1021 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
1023 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1025 // reduce frags during a team change
1026 TeamchangeFrags(self);
1029 SetPlayerTeam(self, dteam, steam, FALSE);
1031 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1033 // kill player when changing teams
1034 if(self.deadflag == DEAD_NO)
1035 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
1037 //ctf_playerchanged();
1040 void ShufflePlayerOutOfTeam (float source_team)
1042 float smallestteam, smallestteam_count, steam;
1043 float lowest_bot_score, lowest_player_score;
1044 entity head, lowest_bot, lowest_player, selected;
1047 smallestteam_count = 999999999;
1049 if(c1 >= 0 && c1 < smallestteam_count)
1052 smallestteam_count = c1;
1054 if(c2 >= 0 && c2 < smallestteam_count)
1057 smallestteam_count = c2;
1059 if(c3 >= 0 && c3 < smallestteam_count)
1062 smallestteam_count = c3;
1064 if(c4 >= 0 && c4 < smallestteam_count)
1067 smallestteam_count = c4;
1072 bprint("warning: no smallest team\n");
1076 if(source_team == 1)
1077 steam = COLOR_TEAM1;
1078 else if(source_team == 2)
1079 steam = COLOR_TEAM2;
1080 else if(source_team == 3)
1081 steam = COLOR_TEAM3;
1082 else if(source_team == 4)
1083 steam = COLOR_TEAM4;
1086 lowest_bot_score = 999999999;
1087 lowest_player = world;
1088 lowest_player_score = 999999999;
1090 // find the lowest-scoring player & bot of that team
1091 FOR_EACH_PLAYER(head)
1093 if(head.team == steam)
1097 if(head.totalfrags < lowest_bot_score)
1100 lowest_bot_score = head.totalfrags;
1105 if(head.totalfrags < lowest_player_score)
1107 lowest_player = head;
1108 lowest_player_score = head.totalfrags;
1114 // prefers to move a bot...
1115 if(lowest_bot != world)
1116 selected = lowest_bot;
1117 // but it will move a player if it has to
1119 selected = lowest_player;
1120 // don't do anything if it couldn't find anyone
1123 bprint("warning: couldn't find a player to move from team\n");
1127 // smallest team gains a member
1128 if(smallestteam == 1)
1132 else if(smallestteam == 2)
1136 else if(smallestteam == 3)
1140 else if(smallestteam == 4)
1146 bprint("warning: destination team invalid\n");
1149 // source team loses a member
1150 if(source_team == 1)
1154 else if(source_team == 2)
1158 else if(source_team == 3)
1162 else if(source_team == 4)
1168 bprint("warning: source team invalid\n");
1172 // move the player to the new team
1173 TeamchangeFrags(selected);
1174 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1176 if(selected.deadflag == DEAD_NO)
1177 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1178 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1181 float lastRebalanceInfo;
1182 void CauseRebalance(float source_team, float howmany_toomany)
1187 if(IsTeamBalanceForced() == 1)
1189 bprint("Rebalancing Teams\n");
1190 ShufflePlayerOutOfTeam(source_team);
1194 if(howmany_toomany < cvar("g_balance_teams_complain"))
1196 if(time < lastRebalanceInfo + 90)
1198 lastRebalanceInfo = time;
1199 if(source_team == 1)
1200 steam = COLOR_TEAM1;
1201 else if(source_team == 2)
1202 steam = COLOR_TEAM2;
1203 else if(source_team == 3)
1204 steam = COLOR_TEAM3;
1205 else if(source_team == 4)
1206 steam = COLOR_TEAM4;
1207 print("Team ", ftos(source_team), " too large, complaining.\n");
1208 FOR_EACH_REALPLAYER(head)
1210 if(head.team == steam)
1212 sprint(head, "\{1}\{13}^3SERVER NOTICE:^7 One of you please change teams!\n");
1213 centerprint_atprio(head, CENTERPRIO_REBALANCE, "^3SERVER NOTICE:\n\n^7Someone of you please change teams!");
1219 // part of g_balance_teams_force
1220 // occasionally perform an audit of the teams to make
1221 // sure they're more or less balanced in player count.
1224 float numplayers, numteams, smallest, toomany;
1226 balance = IsTeamBalanceForced();
1230 if(audit_teams_time > time)
1233 audit_teams_time = time + 4 + random();
1235 // bprint("Auditing teams\n");
1237 CheckAllowedTeams(world);
1238 GetTeamCounts(world);
1241 numteams = numplayers = smallest = 0;
1244 numteams = numteams + 1;
1245 numplayers = numplayers + c1;
1250 numteams = numteams + 1;
1251 numplayers = numplayers + c2;
1257 numteams = numteams + 1;
1258 numplayers = numplayers + c3;
1264 numteams = numteams + 1;
1265 numplayers = numplayers + c4;
1271 return; // no players to move around
1273 return; // don't bother shuffling if for some reason there aren't any teams
1275 toomany = smallest + 1;
1277 if(c1 && c1 > toomany)
1278 CauseRebalance(1, c1 - toomany);
1279 if(c2 && c2 > toomany)
1280 CauseRebalance(2, c2 - toomany);
1281 if(c3 && c3 > toomany)
1282 CauseRebalance(3, c3 - toomany);
1283 if(c4 && c4 > toomany)
1284 CauseRebalance(4, c4 - toomany);
1286 // if teams are still unbalanced, balance them further in the next audit,
1287 // which will happen sooner (keep doing rapid audits until things are in order)
1288 audit_teams_time = time + 0.7 + random()*0.3;
1293 // code from here on is just to support maps that don't have team entities
1294 void tdm_spawnteam (string teamname, float teamcolor)
1298 e.classname = "tdm_team";
1299 e.netname = teamname;
1304 // spawn some default teams if the map is not set up for tdm
1305 void tdm_spawnteams()
1309 numteams = cvar("g_tdm_teams");
1311 tdm_spawnteam("Red", COLOR_TEAM1-1);
1312 tdm_spawnteam("Blue", COLOR_TEAM2-1);
1314 tdm_spawnteam("Yellow", COLOR_TEAM3-1);
1316 tdm_spawnteam("Pink", COLOR_TEAM4-1);
1319 void tdm_delayedinit()
1321 // if no teams are found, spawn defaults
1322 if (find(world, classname, "tdm_team") == world)
1328 InitializeEntity(world, tdm_delayedinit, INITPRIO_GAMETYPE);