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 if(MapInfo_Map_mins_x < MapInfo_Map_maxs_x)
178 world.mins = MapInfo_Map_mins;
179 world.maxs = MapInfo_Map_maxs;
181 if not(cvar_value_issafe(world.fog))
183 print("The current map contains a potentially harmful fog setting, ignored\n");
184 world.fog = string_null;
186 if(MapInfo_Map_fog != "")
187 if(MapInfo_Map_fog == "none")
188 world.fog = string_null;
190 world.fog = strzone(MapInfo_Map_fog);
191 clientstuff = strzone(MapInfo_Map_clientstuff);
192 MapInfo_ClearTemps();
194 // in case mapinfo switched the type
197 // set both here, gamemode can override it later
198 timelimit_override = cvar("timelimit_override");
199 fraglimit_override = cvar("fraglimit_override");
203 game = GAME_DEATHMATCH;
204 gamemode_name = "Deathmatch";
209 game = GAME_TEAM_DEATHMATCH;
210 gamemode_name = "Team Deathmatch";
217 game = GAME_DOMINATION;
218 gamemode_name = "Domination";
220 fraglimit_override = cvar("g_domination_point_limit");
227 gamemode_name = "Capture the Flag";
229 g_ctf_win_mode = cvar("g_ctf_win_mode");
230 g_ctf_ignore_frags = cvar("g_ctf_ignore_frags");
231 if(g_ctf_win_mode == 2)
232 fraglimit_override = cvar("g_ctf_capture_limit");
234 fraglimit_override = cvar("capturelimit_override");
240 game = GAME_RUNEMATCH;
241 gamemode_name = "Rune Match";
242 if(cvar("deathmatch_force_teamplay"))
244 fraglimit_override = cvar("g_runematch_point_limit");
251 gamemode_name = "Last Man Standing";
252 fraglimit_override = cvar("g_lms_lives_override");
253 if(fraglimit_override == 0)
254 fraglimit_override = -1;
255 lms_lowest_lives = 9999;
263 gamemode_name = "Arena";
264 fraglimit_override = cvar("g_arena_point_limit");
265 maxspawned = cvar("g_arena_maxspawned");
268 arena_roundbased = cvar("g_arena_roundbased");
274 gamemode_name = "Key Hunt";
276 fraglimit_override = cvar("g_keyhunt_point_limit");
283 gamemode_name = "Assault";
289 game = GAME_ONSLAUGHT;
290 gamemode_name = "Onslaught";
297 gamemode_name = "Race";
298 g_race_qualifying = cvar("g_race_qualifying");
300 // In campaign, only this mode makes sense...
301 if(cvar("g_campaign"))
302 g_race_qualifying = 1;
304 if(cvar("g_race_teams"))
306 g_race_qualifying = 0; // not supported!
308 race_teams = bound(2, cvar("g_race_teams"), 4);
313 if(g_race_qualifying == 1)
314 fraglimit_override = 0;
316 fraglimit_override = cvar("g_race_laps_limit");
318 if(g_race_qualifying)
319 independent_players = 1;
327 // save it (for the next startup)
328 cvar_set("gamecfg", ftos(game));
330 cache_mutatormsg = strzone("");
331 cache_motd = strzone("");
332 cache_lastmutatormsg = strzone("");
333 cache_lastmotd = strzone("");
335 // enforce the server's universal frag/time limits
336 if(!cvar("g_campaign"))
338 if(fraglimit_override >= 0)
339 cvar_set("fraglimit", ftos(fraglimit_override));
340 if(timelimit_override >= 0)
341 cvar_set("timelimit", ftos(timelimit_override));
344 if(g_race && g_race_qualifying == 2)
346 race_fraglimit = cvar("fraglimit");
347 cvar_set("fraglimit", "0");
350 InitializeEntity(world, default_delayedinit, INITPRIO_GAMETYPE_FALLBACK);
353 string GetClientVersionMessage() {
354 local string versionmsg;
355 if (self.version_mismatch) {
356 if(self.version < cvar("gameversion")) {
357 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
359 versionmsg = "^3This server is using an outdated Nexuiz version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
362 versionmsg = "^2client version and server version are compatible.^8";
368 void PrintWelcomeMessage(entity pl)
370 string s, mutator, modifications;
372 /*if(self.welcomemessage_time > time)
374 self.welcomemessage_time = time + 0.8; */
376 if(self.cvar_scr_centertime == 0) return;
377 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
378 if(self.welcomemessage_time > time) return;
379 self.welcomemessage_time = time + self.cvar_scr_centertime * 0.6;
382 if(cvar("g_campaign"))
384 centerprint(pl, campaign_message);
388 if(!self.BUTTON_INFO)
390 // TODO get rid of this too
391 local string specString;
392 specString = NEWLINES;
393 if(time < game_starttime) //also show the countdown when being a spectator
394 specString = strcat(specString, "\n\n^1Game starts in ", ftos(restartAnnouncer.cnt + 1), " seconds^7");
395 else if (timeoutStatus != 0)
396 specString = strcat(specString, "\n\n", getTimeoutText(1));
399 if(!self.BUTTON_INFO && self.classname == "player")
403 return centerprint_atprio(self, CENTERPRIO_SPAM, specString);
408 mutator = "^2Minstagib ^1";
409 else if(g_rocketarena)
410 mutator = "^2Rocketarena ^1";
412 mutator = "^2No Items Nexuiz ^1";
415 // to protect against unheedingly made changes
417 modifications = strcat(modifications, ", ");
419 modifications = "cloaked";
423 modifications = strcat(modifications, ", ");
425 modifications = strcat(modifications, "footsteps");
429 modifications = strcat(modifications, ", ");
431 modifications = strcat(modifications, "midair");
435 modifications = strcat(modifications, ", ");
437 modifications = strcat(modifications, "vampire");
439 if(g_laserguided_missile) {
441 modifications = strcat(modifications, ", ");
443 modifications = strcat(modifications, "laser guided missiles");
445 if(cvar("sv_gravity") < 800) {
447 modifications = strcat(modifications, ", ");
449 modifications = strcat(modifications, "low gravity");
452 local string versionmessage;
453 versionmessage = GetClientVersionMessage();
455 s = strcat(s, NEWLINES, "This is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
456 s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
458 if(modifications != "")
459 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
461 if(time < game_starttime)
462 s = strcat(s, "\n^1Game starts in ", ftos(restartAnnouncer.cnt + 1), " seconds^7");
464 if(timeoutStatus != 0)
465 s = strcat(s, "\n\n", getTimeoutText(1));
467 if (g_grappling_hook)
468 s = strcat(s, "\n\n^3grappling hook^8 is enabled, press 'e' to use it\n");
470 if(cache_lastmutatormsg != cvar_string("g_mutatormsg"))
472 if(cache_lastmutatormsg)
473 strunzone(cache_lastmutatormsg);
475 strunzone(cache_mutatormsg);
476 cache_lastmutatormsg = strzone(cvar_string("g_mutatormsg"));
477 cache_mutatormsg = strzone(wordwrap(cache_lastmutatormsg, 50));
480 if (cache_mutatormsg != "") {
481 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
484 if(cache_lastmotd != cvar_string("sv_motd"))
487 strunzone(cache_lastmotd);
489 strunzone(cache_motd);
490 cache_lastmotd = strzone(cvar_string("sv_motd"));
491 cache_motd = strzone(wordwrap(cache_lastmotd, 50));
494 if (cache_motd != "") {
495 s = strcat(s, "\n\n^8MOTD: ^7", cache_motd);
504 void SetPlayerColors(entity pl, float _color)
508 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
510 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
511 pl.clientcolors = 16*cl + cl;*/
514 pants = _color & 0x0F;
515 shirt = _color & 0xF0;
519 setcolor(pl, 16*pants + pants);
521 setcolor(pl, shirt + pants);
525 void SetPlayerTeam(entity pl, float t, float s, float noprint)
530 _color = COLOR_TEAM4 - 1;
532 _color = COLOR_TEAM3 - 1;
534 _color = COLOR_TEAM2 - 1;
536 _color = COLOR_TEAM1 - 1;
538 SetPlayerColors(pl,_color);
540 if(!noprint && t != s)
542 //bprint(pl.netname, " has changed to ", TeamNoName(t), "\n");
543 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
555 // set c1...c4 to show what teams are allowed
556 void CheckAllowedTeams (entity for_whom)
565 c1 = c2 = c3 = c4 = -1;
566 cb1 = cb2 = cb3 = cb4 = 0;
568 // onslaught is special
571 head = findchain(classname, "onslaught_generator");
574 if (head.team == COLOR_TEAM1) c1 = 0;
575 if (head.team == COLOR_TEAM2) c2 = 0;
576 if (head.team == COLOR_TEAM3) c3 = 0;
577 if (head.team == COLOR_TEAM4) c4 = 0;
584 teament_name = "dom_team";
586 teament_name = "ctf_team";
588 teament_name = "tdm_team";
591 c1 = c2 = 0; // Assault always has 2 teams
596 // cover anything else by treating it like tdm with no teams spawned
602 dm = cvar("g_tdm_teams");
604 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
608 c1 = c2 = c3 = c4 = 0;
621 // first find out what teams are allowed
622 head = find(world, classname, teament_name);
625 if(!(g_domination && head.netname == ""))
627 if(head.team == COLOR_TEAM1)
631 if(head.team == COLOR_TEAM2)
635 if(head.team == COLOR_TEAM3)
639 if(head.team == COLOR_TEAM4)
644 head = find(head, classname, teament_name);
649 if(cvar("bot_vs_human") > 0)
652 if(clienttype(for_whom) == CLIENTTYPE_BOT)
657 else if(cvar("bot_vs_human") < 0)
660 if(clienttype(for_whom) == CLIENTTYPE_BOT)
668 float PlayerValue(entity p)
670 if(IsTeamBalanceForced() == 1)
675 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
676 // teams that are allowed will now have their player counts stored in c1...c4
677 void GetTeamCounts(entity ignore)
681 // now count how many players are on each team already
683 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
684 // also remember the lowest-scoring player
686 FOR_EACH_PLAYER(head)
688 if(head != ignore)// && head.netname != "")
690 value = PlayerValue(head);
691 if(clienttype(head) == CLIENTTYPE_BOT)
695 if(head.team == COLOR_TEAM1)
703 if(head.team == COLOR_TEAM2)
711 if(head.team == COLOR_TEAM3)
719 if(head.team == COLOR_TEAM4)
731 // returns # of smallest team (1, 2, 3, 4)
732 // NOTE: Assumes CheckAllowedTeams has already been called!
733 float FindSmallestTeam(entity pl, float ignore_pl)
735 float totalteams, smallestteam, smallestteam_count, smallestteam_score, balance_type;
738 // find out what teams are available
739 //CheckAllowedTeams();
741 // make sure there are at least 2 teams to join
743 totalteams = totalteams + 1;
745 totalteams = totalteams + 1;
747 totalteams = totalteams + 1;
749 totalteams = totalteams + 1;
751 if(cvar("bot_vs_human"))
757 error("Too few teams available for domination\n");
759 error("Too few teams available for ctf\n");
761 error("Too few teams available for key hunt\n");
763 error("Too few teams available for team deathmatch\n");
766 // count how many players are in each team
770 GetTeamCounts(world);
772 // c1...c4 now have counts of each team
773 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
776 smallestteam_count = 999999999;
777 smallestteam_score = 999999999;
779 // 2 gives priority to what team you're already on, 1 goes in order
780 // 2 doesn't seem to work though...
784 //if(pl.classname != "player")
785 if(clienttype(pl) != CLIENTTYPE_BOT)
787 c1 -= cb1 * 255.0/256;
788 c2 -= cb2 * 255.0/256;
789 c3 -= cb3 * 255.0/256;
790 c4 -= cb4 * 255.0/256;
793 if(balance_type == 1)
795 if(c1 >= 0 && (c1 < smallestteam_count || (c1 <= smallestteam_count && team1_score < smallestteam_score)))
798 smallestteam_count = c1;
799 smallestteam_score = team1_score;
801 if(c2 >= 0 && (c2 < smallestteam_count || (c2 <= smallestteam_count && team2_score < smallestteam_score)))
804 smallestteam_count = c2;
805 smallestteam_score = team2_score;
807 if(c3 >= 0 && (c3 < smallestteam_count || (c3 <= smallestteam_count && team3_score < smallestteam_score)))
810 smallestteam_count = c3;
811 smallestteam_score = team3_score;
813 if(c4 >= 0 && (c4 < smallestteam_count || (c4 <= smallestteam_count && team4_score < smallestteam_score)))
816 smallestteam_count = c4;
817 smallestteam_score = team4_score;
822 if(c1 >= 0 && (c1 < smallestteam_count ||
823 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
826 smallestteam_count = c1;
828 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
829 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
832 smallestteam_count = c2;
834 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
835 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
838 smallestteam_count = c3;
840 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
841 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
844 smallestteam_count = c4;
851 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
853 float smallest, selectedteam;
855 // don't join a team if we're not playing a team game
856 if(!cvar("teamplay") && !g_domination && !g_ctf && !g_keyhunt)
859 // find out what teams are available
860 CheckAllowedTeams(pl);
865 if(cvar("g_domination_default_teams") < 3)
867 if(cvar("g_domination_default_teams") < 4)
871 // if we don't care what team he ends up on, put him on whatever team he entered as.
872 // if he's not on a valid team, then let other code put him on the smallest team
875 if( c1 >= 0 && pl.team == COLOR_TEAM1)
876 selectedteam = pl.team;
877 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
878 selectedteam = pl.team;
879 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
880 selectedteam = pl.team;
881 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
882 selectedteam = pl.team;
887 if(!only_return_best)
889 SetPlayerColors(pl, selectedteam - 1);
894 // otherwise end up on the smallest team (handled below)
897 smallest = FindSmallestTeam(pl, TRUE);
900 if(!only_return_best)
902 TeamchangeFrags(self);
905 SetPlayerColors(pl, COLOR_TEAM1 - 1);
907 else if(smallest == 2)
909 SetPlayerColors(pl, COLOR_TEAM2 - 1);
911 else if(smallest == 3)
913 SetPlayerColors(pl, COLOR_TEAM3 - 1);
915 else if(smallest == 4)
917 SetPlayerColors(pl, COLOR_TEAM4 - 1);
921 error("smallest team: invalid team\n");
924 if(pl.deadflag == DEAD_NO)
925 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
931 //void() ctf_playerchanged;
932 void SV_ChangeTeam(float _color)
934 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
936 // in normal deathmatch we can just apply the color and we're done
937 if(!cvar("teamplay")) {
938 SetPlayerColors(self, _color);
942 scolor = self.clientcolors & 0x0F;
943 dcolor = _color & 0x0F;
945 if(scolor == COLOR_TEAM1 - 1)
947 else if(scolor == COLOR_TEAM2 - 1)
949 else if(scolor == COLOR_TEAM3 - 1)
951 else if(scolor == COLOR_TEAM4 - 1)
953 if(dcolor == COLOR_TEAM1 - 1)
955 else if(dcolor == COLOR_TEAM2 - 1)
957 else if(dcolor == COLOR_TEAM3 - 1)
959 else if(dcolor == COLOR_TEAM4 - 1)
962 CheckAllowedTeams(self);
964 if(dteam == 1 && c1 < 0) dteam = 4;
965 if(dteam == 4 && c4 < 0) dteam = 3;
966 if(dteam == 3 && c3 < 0) dteam = 2;
967 if(dteam == 2 && c2 < 0) dteam = 1;
969 // not changing teams
972 //bprint("same team change\n");
973 SetPlayerTeam(self, dteam, steam, TRUE);
979 if(cvar("g_campaign") || cvar("g_changeteam_banned"))
981 sprint(self, "Team changes not allowed\n");
982 return; // changing teams is not allowed
985 if(!cvar("g_campaign") && cvar("g_balance_teams_prevent_imbalance"))
987 // only allow changing to a smaller or equal size team
989 // find out what teams are available
990 //CheckAllowedTeams();
991 // count how many players on each team
992 GetTeamCounts(world);
995 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
1000 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
1005 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
1010 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
1017 sprint(self, "Cannot change to an invalid team\n");
1022 // get starting team
1023 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
1025 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
1027 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
1029 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
1032 if(scount) // started at a valid, nonempty team
1034 // check if we're trying to change to a larger team that doens't have bots to swap with
1035 if(dcount >= scount && dbotcount <= 0)
1037 sprint(self, "Cannot change to a larger team\n");
1038 return; // can't change to a larger team
1044 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
1046 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1048 // reduce frags during a team change
1049 TeamchangeFrags(self);
1052 SetPlayerTeam(self, dteam, steam, FALSE);
1054 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1056 // kill player when changing teams
1057 if(self.deadflag == DEAD_NO)
1058 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
1060 //ctf_playerchanged();
1063 void ShufflePlayerOutOfTeam (float source_team)
1065 float smallestteam, smallestteam_count, steam;
1066 float lowest_bot_score, lowest_player_score;
1067 entity head, lowest_bot, lowest_player, selected;
1070 smallestteam_count = 999999999;
1072 if(c1 >= 0 && c1 < smallestteam_count)
1075 smallestteam_count = c1;
1077 if(c2 >= 0 && c2 < smallestteam_count)
1080 smallestteam_count = c2;
1082 if(c3 >= 0 && c3 < smallestteam_count)
1085 smallestteam_count = c3;
1087 if(c4 >= 0 && c4 < smallestteam_count)
1090 smallestteam_count = c4;
1095 bprint("warning: no smallest team\n");
1099 if(source_team == 1)
1100 steam = COLOR_TEAM1;
1101 else if(source_team == 2)
1102 steam = COLOR_TEAM2;
1103 else if(source_team == 3)
1104 steam = COLOR_TEAM3;
1105 else if(source_team == 4)
1106 steam = COLOR_TEAM4;
1109 lowest_bot_score = 999999999;
1110 lowest_player = world;
1111 lowest_player_score = 999999999;
1113 // find the lowest-scoring player & bot of that team
1114 FOR_EACH_PLAYER(head)
1116 if(head.team == steam)
1120 if(head.totalfrags < lowest_bot_score)
1123 lowest_bot_score = head.totalfrags;
1128 if(head.totalfrags < lowest_player_score)
1130 lowest_player = head;
1131 lowest_player_score = head.totalfrags;
1137 // prefers to move a bot...
1138 if(lowest_bot != world)
1139 selected = lowest_bot;
1140 // but it will move a player if it has to
1142 selected = lowest_player;
1143 // don't do anything if it couldn't find anyone
1146 bprint("warning: couldn't find a player to move from team\n");
1150 // smallest team gains a member
1151 if(smallestteam == 1)
1155 else if(smallestteam == 2)
1159 else if(smallestteam == 3)
1163 else if(smallestteam == 4)
1169 bprint("warning: destination team invalid\n");
1172 // source team loses a member
1173 if(source_team == 1)
1177 else if(source_team == 2)
1181 else if(source_team == 3)
1185 else if(source_team == 4)
1191 bprint("warning: source team invalid\n");
1195 // move the player to the new team
1196 TeamchangeFrags(selected);
1197 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1199 if(selected.deadflag == DEAD_NO)
1200 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1201 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1204 float lastRebalanceInfo;
1205 void CauseRebalance(float source_team, float howmany_toomany)
1210 if(IsTeamBalanceForced() == 1)
1212 bprint("Rebalancing Teams\n");
1213 ShufflePlayerOutOfTeam(source_team);
1217 if(cvar("g_balance_teams_complain"))
1218 if(howmany_toomany < cvar("g_balance_teams_complain"))
1220 if(time < lastRebalanceInfo + 90)
1222 lastRebalanceInfo = time;
1223 if(source_team == 1)
1224 steam = COLOR_TEAM1;
1225 else if(source_team == 2)
1226 steam = COLOR_TEAM2;
1227 else if(source_team == 3)
1228 steam = COLOR_TEAM3;
1229 else if(source_team == 4)
1230 steam = COLOR_TEAM4;
1231 print("Team ", ftos(source_team), " too large, complaining.\n");
1232 FOR_EACH_REALPLAYER(head)
1234 if(head.team == steam)
1236 sprint(head, "\{1}\{13}^3SERVER NOTICE:^7 One of you please change teams!\n");
1237 centerprint_atprio(head, CENTERPRIO_REBALANCE, "^3SERVER NOTICE:\n\n^7Someone of you please change teams!");
1243 // part of g_balance_teams_force
1244 // occasionally perform an audit of the teams to make
1245 // sure they're more or less balanced in player count.
1248 float numplayers, numteams, smallest, toomany;
1250 balance = IsTeamBalanceForced();
1254 if(audit_teams_time > time)
1257 audit_teams_time = time + 4 + random();
1259 // bprint("Auditing teams\n");
1261 CheckAllowedTeams(world);
1262 GetTeamCounts(world);
1265 numteams = numplayers = smallest = 0;
1268 numteams = numteams + 1;
1269 numplayers = numplayers + c1;
1274 numteams = numteams + 1;
1275 numplayers = numplayers + c2;
1281 numteams = numteams + 1;
1282 numplayers = numplayers + c3;
1288 numteams = numteams + 1;
1289 numplayers = numplayers + c4;
1295 return; // no players to move around
1297 return; // don't bother shuffling if for some reason there aren't any teams
1299 toomany = smallest + 1;
1301 if(c1 && c1 > toomany)
1302 CauseRebalance(1, c1 - toomany);
1303 if(c2 && c2 > toomany)
1304 CauseRebalance(2, c2 - toomany);
1305 if(c3 && c3 > toomany)
1306 CauseRebalance(3, c3 - toomany);
1307 if(c4 && c4 > toomany)
1308 CauseRebalance(4, c4 - toomany);
1310 // if teams are still unbalanced, balance them further in the next audit,
1311 // which will happen sooner (keep doing rapid audits until things are in order)
1312 audit_teams_time = time + 0.7 + random()*0.3;
1317 // code from here on is just to support maps that don't have team entities
1318 void tdm_spawnteam (string teamname, float teamcolor)
1322 e.classname = "tdm_team";
1323 e.netname = teamname;
1328 // spawn some default teams if the map is not set up for tdm
1329 void tdm_spawnteams()
1333 numteams = cvar("g_tdm_teams");
1335 tdm_spawnteam("Red", COLOR_TEAM1-1);
1336 tdm_spawnteam("Blue", COLOR_TEAM2-1);
1338 tdm_spawnteam("Yellow", COLOR_TEAM3-1);
1340 tdm_spawnteam("Pink", COLOR_TEAM4-1);
1343 void tdm_delayedinit()
1345 // if no teams are found, spawn defaults
1346 if (find(world, classname, "tdm_team") == world)
1352 InitializeEntity(world, tdm_delayedinit, INITPRIO_GAMETYPE);