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 // find out good world mins/maxs bounds, either the static bounds found by looking for solid, or the mapinfo specified bounds
180 MapInfo_LoadMapSettings(mapname);
182 if not(cvar_value_issafe(world.fog))
184 print("The current map contains a potentially harmful fog setting, ignored\n");
185 world.fog = string_null;
187 if(MapInfo_Map_fog != "")
188 if(MapInfo_Map_fog == "none")
189 world.fog = string_null;
191 world.fog = strzone(MapInfo_Map_fog);
192 clientstuff = strzone(MapInfo_Map_clientstuff);
193 MapInfo_ClearTemps();
195 // in case mapinfo switched the type
198 // set both here, gamemode can override it later
199 timelimit_override = cvar("timelimit_override");
200 fraglimit_override = cvar("fraglimit_override");
204 game = GAME_DEATHMATCH;
205 gamemode_name = "Deathmatch";
210 game = GAME_TEAM_DEATHMATCH;
211 gamemode_name = "Team Deathmatch";
218 game = GAME_DOMINATION;
219 gamemode_name = "Domination";
221 fraglimit_override = cvar("g_domination_point_limit");
228 gamemode_name = "Capture the Flag";
230 g_ctf_win_mode = cvar("g_ctf_win_mode");
231 g_ctf_ignore_frags = cvar("g_ctf_ignore_frags");
232 if(g_ctf_win_mode == 2)
233 fraglimit_override = cvar("g_ctf_capture_limit");
235 fraglimit_override = cvar("capturelimit_override");
241 game = GAME_RUNEMATCH;
242 gamemode_name = "Rune Match";
243 if(cvar("deathmatch_force_teamplay"))
245 fraglimit_override = cvar("g_runematch_point_limit");
252 gamemode_name = "Last Man Standing";
253 fraglimit_override = cvar("g_lms_lives_override");
254 if(fraglimit_override == 0)
255 fraglimit_override = -1;
256 lms_lowest_lives = 9999;
264 gamemode_name = "Arena";
265 fraglimit_override = cvar("g_arena_point_limit");
266 maxspawned = cvar("g_arena_maxspawned");
269 arena_roundbased = cvar("g_arena_roundbased");
275 gamemode_name = "Key Hunt";
277 fraglimit_override = cvar("g_keyhunt_point_limit");
284 gamemode_name = "Assault";
290 game = GAME_ONSLAUGHT;
291 gamemode_name = "Onslaught";
298 gamemode_name = "Race";
299 g_race_qualifying = cvar("g_race_qualifying");
301 if(cvar("g_race_teams"))
303 g_race_qualifying = 0; // not supported!
305 race_teams = bound(2, cvar("g_race_teams"), 4);
310 if(g_race_qualifying == 1)
311 fraglimit_override = 0;
313 fraglimit_override = cvar("g_race_laps_limit");
315 if(g_race_qualifying)
316 independent_players = 1;
324 // save it (for the next startup)
325 cvar_set("gamecfg", ftos(game));
327 cache_mutatormsg = strzone("");
328 cache_motd = strzone("");
329 cache_lastmutatormsg = strzone("");
330 cache_lastmotd = strzone("");
332 // enforce the server's universal frag/time limits
333 if(!cvar("g_campaign"))
335 if(fraglimit_override >= 0)
336 cvar_set("fraglimit", ftos(fraglimit_override));
337 if(timelimit_override >= 0)
338 cvar_set("timelimit", ftos(timelimit_override));
341 if(g_race && g_race_qualifying == 2)
343 race_fraglimit = cvar("fraglimit");
344 cvar_set("fraglimit", "0");
347 InitializeEntity(world, default_delayedinit, INITPRIO_GAMETYPE_FALLBACK);
350 string GetClientVersionMessage() {
351 local string versionmsg;
352 if (self.version_mismatch) {
353 if(self.version < cvar("gameversion")) {
354 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
356 versionmsg = "^3This server is using an outdated Nexuiz version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
359 versionmsg = "^2client version and server version are compatible.^8";
365 void PrintWelcomeMessage(entity pl)
367 string s, mutator, modifications;
369 /*if(self.welcomemessage_time > time)
371 self.welcomemessage_time = time + 0.8; */
373 if(self.cvar_scr_centertime == 0) return;
374 if( !(timeoutStatus >= 1 || (time < game_starttime) ) ) { //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
375 if(self.welcomemessage_time > time) return;
376 self.welcomemessage_time = time + self.cvar_scr_centertime * 0.6;
379 if(cvar("g_campaign"))
381 centerprint(pl, campaign_message);
385 if(!self.BUTTON_INFO)
387 // TODO get rid of this too
388 local string specString;
389 specString = NEWLINES;
390 if(time < game_starttime) //also show the countdown when being a spectator
391 specString = strcat(specString, "\n\n^1Game starts in ", ftos(ceil(game_starttime - time)), " seconds^7");
392 else if (timeoutStatus != 0)
393 specString = strcat(specString, "\n\n", getTimeoutText(1));
396 if(!self.BUTTON_INFO && self.classname == "player")
400 return centerprint_atprio(self, CENTERPRIO_SPAM, specString);
405 mutator = "^2Minstagib ^1";
406 else if(g_weaponarena)
407 mutator = "^2", g_weaponarena_list, " Arena ^1";
409 mutator = "^2No Items Nexuiz ^1";
412 // to protect against unheedingly made changes
414 modifications = strcat(modifications, ", ");
416 modifications = "cloaked";
420 modifications = strcat(modifications, ", ");
422 modifications = strcat(modifications, "footsteps");
426 modifications = strcat(modifications, ", ");
428 modifications = strcat(modifications, "midair");
432 modifications = strcat(modifications, ", ");
434 modifications = strcat(modifications, "vampire");
436 if(g_laserguided_missile) {
438 modifications = strcat(modifications, ", ");
440 modifications = strcat(modifications, "laser guided missiles");
442 if(cvar("sv_gravity") < 800) {
444 modifications = strcat(modifications, ", ");
446 modifications = strcat(modifications, "low gravity");
449 local string versionmessage;
450 versionmessage = GetClientVersionMessage();
452 s = strcat(s, NEWLINES, "This is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
453 s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
455 if(modifications != "")
456 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
458 if(time < game_starttime)
459 s = strcat(s, "\n^1Game starts in ", ftos(restartAnnouncer.cnt + 1), " seconds^7");
461 if(timeoutStatus != 0)
462 s = strcat(s, "\n\n", getTimeoutText(1));
464 if (g_grappling_hook)
465 s = strcat(s, "\n\n^3grappling hook^8 is enabled, press 'e' to use it\n");
467 if(cache_lastmutatormsg != cvar_string("g_mutatormsg"))
469 if(cache_lastmutatormsg)
470 strunzone(cache_lastmutatormsg);
472 strunzone(cache_mutatormsg);
473 cache_lastmutatormsg = strzone(cvar_string("g_mutatormsg"));
474 cache_mutatormsg = strzone(wordwrap(cache_lastmutatormsg, 50));
477 if (cache_mutatormsg != "") {
478 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
481 if(cache_lastmotd != cvar_string("sv_motd"))
484 strunzone(cache_lastmotd);
486 strunzone(cache_motd);
487 cache_lastmotd = strzone(cvar_string("sv_motd"));
488 cache_motd = strzone(wordwrap(cache_lastmotd, 50));
491 if (cache_motd != "") {
492 s = strcat(s, "\n\n^8MOTD: ^7", cache_motd);
501 void SetPlayerColors(entity pl, float _color)
505 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
507 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
508 pl.clientcolors = 16*cl + cl;*/
511 pants = _color & 0x0F;
512 shirt = _color & 0xF0;
516 setcolor(pl, 16*pants + pants);
518 setcolor(pl, shirt + pants);
522 void SetPlayerTeam(entity pl, float t, float s, float noprint)
527 _color = COLOR_TEAM4 - 1;
529 _color = COLOR_TEAM3 - 1;
531 _color = COLOR_TEAM2 - 1;
533 _color = COLOR_TEAM1 - 1;
535 SetPlayerColors(pl,_color);
537 if(!noprint && t != s)
539 //bprint(pl.netname, " has changed to ", TeamNoName(t), "\n");
540 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
552 // set c1...c4 to show what teams are allowed
553 void CheckAllowedTeams (entity for_whom)
562 c1 = c2 = c3 = c4 = -1;
563 cb1 = cb2 = cb3 = cb4 = 0;
565 // onslaught is special
568 head = findchain(classname, "onslaught_generator");
571 if (head.team == COLOR_TEAM1) c1 = 0;
572 if (head.team == COLOR_TEAM2) c2 = 0;
573 if (head.team == COLOR_TEAM3) c3 = 0;
574 if (head.team == COLOR_TEAM4) c4 = 0;
581 teament_name = "dom_team";
583 teament_name = "ctf_team";
585 teament_name = "tdm_team";
588 c1 = c2 = 0; // Assault always has 2 teams
593 // cover anything else by treating it like tdm with no teams spawned
599 dm = cvar("g_tdm_teams");
601 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
605 c1 = c2 = c3 = c4 = 0;
618 // first find out what teams are allowed
619 head = find(world, classname, teament_name);
622 if(!(g_domination && head.netname == ""))
624 if(head.team == COLOR_TEAM1)
628 if(head.team == COLOR_TEAM2)
632 if(head.team == COLOR_TEAM3)
636 if(head.team == COLOR_TEAM4)
641 head = find(head, classname, teament_name);
646 if(cvar("bot_vs_human") > 0)
649 if(clienttype(for_whom) == CLIENTTYPE_BOT)
654 else if(cvar("bot_vs_human") < 0)
657 if(clienttype(for_whom) == CLIENTTYPE_BOT)
665 float PlayerValue(entity p)
667 if(IsTeamBalanceForced() == 1)
672 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
673 // teams that are allowed will now have their player counts stored in c1...c4
674 void GetTeamCounts(entity ignore)
678 // now count how many players are on each team already
680 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
681 // also remember the lowest-scoring player
683 FOR_EACH_PLAYER(head)
685 if(head != ignore)// && head.netname != "")
687 value = PlayerValue(head);
688 if(clienttype(head) == CLIENTTYPE_BOT)
692 if(head.team == COLOR_TEAM1)
700 if(head.team == COLOR_TEAM2)
708 if(head.team == COLOR_TEAM3)
716 if(head.team == COLOR_TEAM4)
728 // returns # of smallest team (1, 2, 3, 4)
729 // NOTE: Assumes CheckAllowedTeams has already been called!
730 float FindSmallestTeam(entity pl, float ignore_pl)
732 float totalteams, smallestteam, smallestteam_count, smallestteam_score, balance_type;
735 // find out what teams are available
736 //CheckAllowedTeams();
738 // make sure there are at least 2 teams to join
740 totalteams = totalteams + 1;
742 totalteams = totalteams + 1;
744 totalteams = totalteams + 1;
746 totalteams = totalteams + 1;
748 if(cvar("bot_vs_human"))
754 error("Too few teams available for domination\n");
756 error("Too few teams available for ctf\n");
758 error("Too few teams available for key hunt\n");
760 error("Too few teams available for team deathmatch\n");
763 // count how many players are in each team
767 GetTeamCounts(world);
769 // c1...c4 now have counts of each team
770 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
773 smallestteam_count = 999999999;
774 smallestteam_score = 999999999;
776 // 2 gives priority to what team you're already on, 1 goes in order
777 // 2 doesn't seem to work though...
781 //if(pl.classname != "player")
782 if(clienttype(pl) != CLIENTTYPE_BOT)
784 c1 -= cb1 * 255.0/256;
785 c2 -= cb2 * 255.0/256;
786 c3 -= cb3 * 255.0/256;
787 c4 -= cb4 * 255.0/256;
790 if(balance_type == 1)
792 if(c1 >= 0 && (c1 < smallestteam_count || (c1 <= smallestteam_count && team1_score < smallestteam_score)))
795 smallestteam_count = c1;
796 smallestteam_score = team1_score;
798 if(c2 >= 0 && (c2 < smallestteam_count || (c2 <= smallestteam_count && team2_score < smallestteam_score)))
801 smallestteam_count = c2;
802 smallestteam_score = team2_score;
804 if(c3 >= 0 && (c3 < smallestteam_count || (c3 <= smallestteam_count && team3_score < smallestteam_score)))
807 smallestteam_count = c3;
808 smallestteam_score = team3_score;
810 if(c4 >= 0 && (c4 < smallestteam_count || (c4 <= smallestteam_count && team4_score < smallestteam_score)))
813 smallestteam_count = c4;
814 smallestteam_score = team4_score;
819 if(c1 >= 0 && (c1 < smallestteam_count ||
820 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
823 smallestteam_count = c1;
825 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
826 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
829 smallestteam_count = c2;
831 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
832 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
835 smallestteam_count = c3;
837 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
838 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
841 smallestteam_count = c4;
848 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
850 float smallest, selectedteam;
852 // don't join a team if we're not playing a team game
853 if(!cvar("teamplay") && !g_domination && !g_ctf && !g_keyhunt)
856 // find out what teams are available
857 CheckAllowedTeams(pl);
862 if(cvar("g_domination_default_teams") < 3)
864 if(cvar("g_domination_default_teams") < 4)
868 // if we don't care what team he ends up on, put him on whatever team he entered as.
869 // if he's not on a valid team, then let other code put him on the smallest team
872 if( c1 >= 0 && pl.team == COLOR_TEAM1)
873 selectedteam = pl.team;
874 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
875 selectedteam = pl.team;
876 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
877 selectedteam = pl.team;
878 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
879 selectedteam = pl.team;
884 if(!only_return_best)
886 SetPlayerColors(pl, selectedteam - 1);
891 // otherwise end up on the smallest team (handled below)
894 smallest = FindSmallestTeam(pl, TRUE);
897 if(!only_return_best)
899 TeamchangeFrags(self);
902 SetPlayerColors(pl, COLOR_TEAM1 - 1);
904 else if(smallest == 2)
906 SetPlayerColors(pl, COLOR_TEAM2 - 1);
908 else if(smallest == 3)
910 SetPlayerColors(pl, COLOR_TEAM3 - 1);
912 else if(smallest == 4)
914 SetPlayerColors(pl, COLOR_TEAM4 - 1);
918 error("smallest team: invalid team\n");
921 if(pl.deadflag == DEAD_NO)
922 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
928 //void() ctf_playerchanged;
929 void SV_ChangeTeam(float _color)
931 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
933 // in normal deathmatch we can just apply the color and we're done
934 if(!cvar("teamplay")) {
935 SetPlayerColors(self, _color);
939 scolor = self.clientcolors & 0x0F;
940 dcolor = _color & 0x0F;
942 if(scolor == COLOR_TEAM1 - 1)
944 else if(scolor == COLOR_TEAM2 - 1)
946 else if(scolor == COLOR_TEAM3 - 1)
948 else if(scolor == COLOR_TEAM4 - 1)
950 if(dcolor == COLOR_TEAM1 - 1)
952 else if(dcolor == COLOR_TEAM2 - 1)
954 else if(dcolor == COLOR_TEAM3 - 1)
956 else if(dcolor == COLOR_TEAM4 - 1)
959 CheckAllowedTeams(self);
961 if(dteam == 1 && c1 < 0) dteam = 4;
962 if(dteam == 4 && c4 < 0) dteam = 3;
963 if(dteam == 3 && c3 < 0) dteam = 2;
964 if(dteam == 2 && c2 < 0) dteam = 1;
966 // not changing teams
969 //bprint("same team change\n");
970 SetPlayerTeam(self, dteam, steam, TRUE);
976 if(cvar("g_campaign") || cvar("g_changeteam_banned"))
978 sprint(self, "Team changes not allowed\n");
979 return; // changing teams is not allowed
982 if(!cvar("g_campaign") && cvar("g_balance_teams_prevent_imbalance"))
984 // only allow changing to a smaller or equal size team
986 // find out what teams are available
987 //CheckAllowedTeams();
988 // count how many players on each team
989 GetTeamCounts(world);
992 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
997 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
1002 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
1007 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
1014 sprint(self, "Cannot change to an invalid team\n");
1019 // get starting team
1020 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
1022 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
1024 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
1026 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
1029 if(scount) // started at a valid, nonempty team
1031 // check if we're trying to change to a larger team that doens't have bots to swap with
1032 if(dcount >= scount && dbotcount <= 0)
1034 sprint(self, "Cannot change to a larger team\n");
1035 return; // can't change to a larger team
1041 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
1043 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1045 // reduce frags during a team change
1046 TeamchangeFrags(self);
1049 SetPlayerTeam(self, dteam, steam, FALSE);
1051 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1053 // kill player when changing teams
1054 if(self.deadflag == DEAD_NO)
1055 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
1057 //ctf_playerchanged();
1060 void ShufflePlayerOutOfTeam (float source_team)
1062 float smallestteam, smallestteam_count, steam;
1063 float lowest_bot_score, lowest_player_score;
1064 entity head, lowest_bot, lowest_player, selected;
1067 smallestteam_count = 999999999;
1069 if(c1 >= 0 && c1 < smallestteam_count)
1072 smallestteam_count = c1;
1074 if(c2 >= 0 && c2 < smallestteam_count)
1077 smallestteam_count = c2;
1079 if(c3 >= 0 && c3 < smallestteam_count)
1082 smallestteam_count = c3;
1084 if(c4 >= 0 && c4 < smallestteam_count)
1087 smallestteam_count = c4;
1092 bprint("warning: no smallest team\n");
1096 if(source_team == 1)
1097 steam = COLOR_TEAM1;
1098 else if(source_team == 2)
1099 steam = COLOR_TEAM2;
1100 else if(source_team == 3)
1101 steam = COLOR_TEAM3;
1102 else if(source_team == 4)
1103 steam = COLOR_TEAM4;
1106 lowest_bot_score = 999999999;
1107 lowest_player = world;
1108 lowest_player_score = 999999999;
1110 // find the lowest-scoring player & bot of that team
1111 FOR_EACH_PLAYER(head)
1113 if(head.team == steam)
1117 if(head.totalfrags < lowest_bot_score)
1120 lowest_bot_score = head.totalfrags;
1125 if(head.totalfrags < lowest_player_score)
1127 lowest_player = head;
1128 lowest_player_score = head.totalfrags;
1134 // prefers to move a bot...
1135 if(lowest_bot != world)
1136 selected = lowest_bot;
1137 // but it will move a player if it has to
1139 selected = lowest_player;
1140 // don't do anything if it couldn't find anyone
1143 bprint("warning: couldn't find a player to move from team\n");
1147 // smallest team gains a member
1148 if(smallestteam == 1)
1152 else if(smallestteam == 2)
1156 else if(smallestteam == 3)
1160 else if(smallestteam == 4)
1166 bprint("warning: destination team invalid\n");
1169 // source team loses a member
1170 if(source_team == 1)
1174 else if(source_team == 2)
1178 else if(source_team == 3)
1182 else if(source_team == 4)
1188 bprint("warning: source team invalid\n");
1192 // move the player to the new team
1193 TeamchangeFrags(selected);
1194 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1196 if(selected.deadflag == DEAD_NO)
1197 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1198 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1201 float lastRebalanceInfo;
1202 void CauseRebalance(float source_team, float howmany_toomany)
1207 if(IsTeamBalanceForced() == 1)
1209 bprint("Rebalancing Teams\n");
1210 ShufflePlayerOutOfTeam(source_team);
1214 if(cvar("g_balance_teams_complain"))
1215 if(howmany_toomany < cvar("g_balance_teams_complain"))
1217 if(time < lastRebalanceInfo + 90)
1219 lastRebalanceInfo = time;
1220 if(source_team == 1)
1221 steam = COLOR_TEAM1;
1222 else if(source_team == 2)
1223 steam = COLOR_TEAM2;
1224 else if(source_team == 3)
1225 steam = COLOR_TEAM3;
1226 else if(source_team == 4)
1227 steam = COLOR_TEAM4;
1228 print("Team ", ftos(source_team), " too large, complaining.\n");
1229 FOR_EACH_REALPLAYER(head)
1231 if(head.team == steam)
1233 sprint(head, "\{1}\{13}^3SERVER NOTICE:^7 One of you please change teams!\n");
1234 centerprint_atprio(head, CENTERPRIO_REBALANCE, "^3SERVER NOTICE:\n\n^7Someone of you please change teams!");
1240 // part of g_balance_teams_force
1241 // occasionally perform an audit of the teams to make
1242 // sure they're more or less balanced in player count.
1245 float numplayers, numteams, smallest, toomany;
1247 balance = IsTeamBalanceForced();
1251 if(audit_teams_time > time)
1254 audit_teams_time = time + 4 + random();
1256 // bprint("Auditing teams\n");
1258 CheckAllowedTeams(world);
1259 GetTeamCounts(world);
1262 numteams = numplayers = smallest = 0;
1265 numteams = numteams + 1;
1266 numplayers = numplayers + c1;
1271 numteams = numteams + 1;
1272 numplayers = numplayers + c2;
1278 numteams = numteams + 1;
1279 numplayers = numplayers + c3;
1285 numteams = numteams + 1;
1286 numplayers = numplayers + c4;
1292 return; // no players to move around
1294 return; // don't bother shuffling if for some reason there aren't any teams
1296 toomany = smallest + 1;
1298 if(c1 && c1 > toomany)
1299 CauseRebalance(1, c1 - toomany);
1300 if(c2 && c2 > toomany)
1301 CauseRebalance(2, c2 - toomany);
1302 if(c3 && c3 > toomany)
1303 CauseRebalance(3, c3 - toomany);
1304 if(c4 && c4 > toomany)
1305 CauseRebalance(4, c4 - toomany);
1307 // if teams are still unbalanced, balance them further in the next audit,
1308 // which will happen sooner (keep doing rapid audits until things are in order)
1309 audit_teams_time = time + 0.7 + random()*0.3;
1314 // code from here on is just to support maps that don't have team entities
1315 void tdm_spawnteam (string teamname, float teamcolor)
1319 e.classname = "tdm_team";
1320 e.netname = teamname;
1325 // spawn some default teams if the map is not set up for tdm
1326 void tdm_spawnteams()
1330 numteams = cvar("g_tdm_teams");
1332 tdm_spawnteam("Red", COLOR_TEAM1-1);
1333 tdm_spawnteam("Blue", COLOR_TEAM2-1);
1335 tdm_spawnteam("Yellow", COLOR_TEAM3-1);
1337 tdm_spawnteam("Pink", COLOR_TEAM4-1);
1340 void tdm_delayedinit()
1342 // if no teams are found, spawn defaults
1343 if (find(world, classname, "tdm_team") == world)
1349 InitializeEntity(world, tdm_delayedinit, INITPRIO_GAMETYPE);