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 // In campaign, only this mode makes sense...
302 if(cvar("g_campaign"))
303 g_race_qualifying = 1;
305 if(cvar("g_race_teams"))
307 g_race_qualifying = 0; // not supported!
309 race_teams = bound(2, cvar("g_race_teams"), 4);
314 if(g_race_qualifying == 1)
315 fraglimit_override = 0;
317 fraglimit_override = cvar("g_race_laps_limit");
319 if(g_race_qualifying)
320 independent_players = 1;
328 // save it (for the next startup)
329 cvar_set("gamecfg", ftos(game));
331 cache_mutatormsg = strzone("");
332 cache_motd = strzone("");
333 cache_lastmutatormsg = strzone("");
334 cache_lastmotd = strzone("");
336 // enforce the server's universal frag/time limits
337 if(!cvar("g_campaign"))
339 if(fraglimit_override >= 0)
340 cvar_set("fraglimit", ftos(fraglimit_override));
341 if(timelimit_override >= 0)
342 cvar_set("timelimit", ftos(timelimit_override));
345 if(g_race && g_race_qualifying == 2)
347 race_fraglimit = cvar("fraglimit");
348 cvar_set("fraglimit", "0");
351 InitializeEntity(world, default_delayedinit, INITPRIO_GAMETYPE_FALLBACK);
354 string GetClientVersionMessage() {
355 local string versionmsg;
356 if (self.version_mismatch) {
357 if(self.version < cvar("gameversion")) {
358 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
360 versionmsg = "^3This server is using an outdated Nexuiz version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
363 versionmsg = "^2client version and server version are compatible.^8";
369 void PrintWelcomeMessage(entity pl)
371 string s, mutator, modifications;
373 /*if(self.welcomemessage_time > time)
375 self.welcomemessage_time = time + 0.8; */
377 if(self.cvar_scr_centertime == 0) return;
378 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
379 if(self.welcomemessage_time > time) return;
380 self.welcomemessage_time = time + self.cvar_scr_centertime * 0.6;
383 if(cvar("g_campaign"))
385 centerprint(pl, campaign_message);
389 if(!self.BUTTON_INFO)
391 // TODO get rid of this too
392 local string specString;
393 specString = NEWLINES;
394 if(time < game_starttime) //also show the countdown when being a spectator
395 specString = strcat(specString, "\n\n^1Game starts in ", ftos(ceil(game_starttime - time)), " seconds^7");
396 else if (timeoutStatus != 0)
397 specString = strcat(specString, "\n\n", getTimeoutText(1));
400 if(!self.BUTTON_INFO && self.classname == "player")
404 return centerprint_atprio(self, CENTERPRIO_SPAM, specString);
409 mutator = "^2Minstagib ^1";
410 else if(g_rocketarena)
411 mutator = "^2Rocketarena ^1";
413 mutator = "^2No Items Nexuiz ^1";
416 // to protect against unheedingly made changes
418 modifications = strcat(modifications, ", ");
420 modifications = "cloaked";
424 modifications = strcat(modifications, ", ");
426 modifications = strcat(modifications, "footsteps");
430 modifications = strcat(modifications, ", ");
432 modifications = strcat(modifications, "midair");
436 modifications = strcat(modifications, ", ");
438 modifications = strcat(modifications, "vampire");
440 if(g_laserguided_missile) {
442 modifications = strcat(modifications, ", ");
444 modifications = strcat(modifications, "laser guided missiles");
446 if(cvar("sv_gravity") < 800) {
448 modifications = strcat(modifications, ", ");
450 modifications = strcat(modifications, "low gravity");
453 local string versionmessage;
454 versionmessage = GetClientVersionMessage();
456 s = strcat(s, NEWLINES, "This is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
457 s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
459 if(modifications != "")
460 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
462 if(time < game_starttime)
463 s = strcat(s, "\n^1Game starts in ", ftos(restartAnnouncer.cnt + 1), " seconds^7");
465 if(timeoutStatus != 0)
466 s = strcat(s, "\n\n", getTimeoutText(1));
468 if (g_grappling_hook)
469 s = strcat(s, "\n\n^3grappling hook^8 is enabled, press 'e' to use it\n");
471 if(cache_lastmutatormsg != cvar_string("g_mutatormsg"))
473 if(cache_lastmutatormsg)
474 strunzone(cache_lastmutatormsg);
476 strunzone(cache_mutatormsg);
477 cache_lastmutatormsg = strzone(cvar_string("g_mutatormsg"));
478 cache_mutatormsg = strzone(wordwrap(cache_lastmutatormsg, 50));
481 if (cache_mutatormsg != "") {
482 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
485 if(cache_lastmotd != cvar_string("sv_motd"))
488 strunzone(cache_lastmotd);
490 strunzone(cache_motd);
491 cache_lastmotd = strzone(cvar_string("sv_motd"));
492 cache_motd = strzone(wordwrap(cache_lastmotd, 50));
495 if (cache_motd != "") {
496 s = strcat(s, "\n\n^8MOTD: ^7", cache_motd);
505 void SetPlayerColors(entity pl, float _color)
509 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
511 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
512 pl.clientcolors = 16*cl + cl;*/
515 pants = _color & 0x0F;
516 shirt = _color & 0xF0;
520 setcolor(pl, 16*pants + pants);
522 setcolor(pl, shirt + pants);
526 void SetPlayerTeam(entity pl, float t, float s, float noprint)
531 _color = COLOR_TEAM4 - 1;
533 _color = COLOR_TEAM3 - 1;
535 _color = COLOR_TEAM2 - 1;
537 _color = COLOR_TEAM1 - 1;
539 SetPlayerColors(pl,_color);
541 if(!noprint && t != s)
543 //bprint(pl.netname, " has changed to ", TeamNoName(t), "\n");
544 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
556 // set c1...c4 to show what teams are allowed
557 void CheckAllowedTeams (entity for_whom)
566 c1 = c2 = c3 = c4 = -1;
567 cb1 = cb2 = cb3 = cb4 = 0;
569 // onslaught is special
572 head = findchain(classname, "onslaught_generator");
575 if (head.team == COLOR_TEAM1) c1 = 0;
576 if (head.team == COLOR_TEAM2) c2 = 0;
577 if (head.team == COLOR_TEAM3) c3 = 0;
578 if (head.team == COLOR_TEAM4) c4 = 0;
585 teament_name = "dom_team";
587 teament_name = "ctf_team";
589 teament_name = "tdm_team";
592 c1 = c2 = 0; // Assault always has 2 teams
597 // cover anything else by treating it like tdm with no teams spawned
603 dm = cvar("g_tdm_teams");
605 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
609 c1 = c2 = c3 = c4 = 0;
622 // first find out what teams are allowed
623 head = find(world, classname, teament_name);
626 if(!(g_domination && head.netname == ""))
628 if(head.team == COLOR_TEAM1)
632 if(head.team == COLOR_TEAM2)
636 if(head.team == COLOR_TEAM3)
640 if(head.team == COLOR_TEAM4)
645 head = find(head, classname, teament_name);
650 if(cvar("bot_vs_human") > 0)
653 if(clienttype(for_whom) == CLIENTTYPE_BOT)
658 else if(cvar("bot_vs_human") < 0)
661 if(clienttype(for_whom) == CLIENTTYPE_BOT)
669 float PlayerValue(entity p)
671 if(IsTeamBalanceForced() == 1)
676 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
677 // teams that are allowed will now have their player counts stored in c1...c4
678 void GetTeamCounts(entity ignore)
682 // now count how many players are on each team already
684 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
685 // also remember the lowest-scoring player
687 FOR_EACH_PLAYER(head)
689 if(head != ignore)// && head.netname != "")
691 value = PlayerValue(head);
692 if(clienttype(head) == CLIENTTYPE_BOT)
696 if(head.team == COLOR_TEAM1)
704 if(head.team == COLOR_TEAM2)
712 if(head.team == COLOR_TEAM3)
720 if(head.team == COLOR_TEAM4)
732 // returns # of smallest team (1, 2, 3, 4)
733 // NOTE: Assumes CheckAllowedTeams has already been called!
734 float FindSmallestTeam(entity pl, float ignore_pl)
736 float totalteams, smallestteam, smallestteam_count, smallestteam_score, balance_type;
739 // find out what teams are available
740 //CheckAllowedTeams();
742 // make sure there are at least 2 teams to join
744 totalteams = totalteams + 1;
746 totalteams = totalteams + 1;
748 totalteams = totalteams + 1;
750 totalteams = totalteams + 1;
752 if(cvar("bot_vs_human"))
758 error("Too few teams available for domination\n");
760 error("Too few teams available for ctf\n");
762 error("Too few teams available for key hunt\n");
764 error("Too few teams available for team deathmatch\n");
767 // count how many players are in each team
771 GetTeamCounts(world);
773 // c1...c4 now have counts of each team
774 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
777 smallestteam_count = 999999999;
778 smallestteam_score = 999999999;
780 // 2 gives priority to what team you're already on, 1 goes in order
781 // 2 doesn't seem to work though...
785 //if(pl.classname != "player")
786 if(clienttype(pl) != CLIENTTYPE_BOT)
788 c1 -= cb1 * 255.0/256;
789 c2 -= cb2 * 255.0/256;
790 c3 -= cb3 * 255.0/256;
791 c4 -= cb4 * 255.0/256;
794 if(balance_type == 1)
796 if(c1 >= 0 && (c1 < smallestteam_count || (c1 <= smallestteam_count && team1_score < smallestteam_score)))
799 smallestteam_count = c1;
800 smallestteam_score = team1_score;
802 if(c2 >= 0 && (c2 < smallestteam_count || (c2 <= smallestteam_count && team2_score < smallestteam_score)))
805 smallestteam_count = c2;
806 smallestteam_score = team2_score;
808 if(c3 >= 0 && (c3 < smallestteam_count || (c3 <= smallestteam_count && team3_score < smallestteam_score)))
811 smallestteam_count = c3;
812 smallestteam_score = team3_score;
814 if(c4 >= 0 && (c4 < smallestteam_count || (c4 <= smallestteam_count && team4_score < smallestteam_score)))
817 smallestteam_count = c4;
818 smallestteam_score = team4_score;
823 if(c1 >= 0 && (c1 < smallestteam_count ||
824 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
827 smallestteam_count = c1;
829 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
830 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
833 smallestteam_count = c2;
835 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
836 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
839 smallestteam_count = c3;
841 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
842 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
845 smallestteam_count = c4;
852 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
854 float smallest, selectedteam;
856 // don't join a team if we're not playing a team game
857 if(!cvar("teamplay") && !g_domination && !g_ctf && !g_keyhunt)
860 // find out what teams are available
861 CheckAllowedTeams(pl);
866 if(cvar("g_domination_default_teams") < 3)
868 if(cvar("g_domination_default_teams") < 4)
872 // if we don't care what team he ends up on, put him on whatever team he entered as.
873 // if he's not on a valid team, then let other code put him on the smallest team
876 if( c1 >= 0 && pl.team == COLOR_TEAM1)
877 selectedteam = pl.team;
878 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
879 selectedteam = pl.team;
880 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
881 selectedteam = pl.team;
882 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
883 selectedteam = pl.team;
888 if(!only_return_best)
890 SetPlayerColors(pl, selectedteam - 1);
895 // otherwise end up on the smallest team (handled below)
898 smallest = FindSmallestTeam(pl, TRUE);
901 if(!only_return_best)
903 TeamchangeFrags(self);
906 SetPlayerColors(pl, COLOR_TEAM1 - 1);
908 else if(smallest == 2)
910 SetPlayerColors(pl, COLOR_TEAM2 - 1);
912 else if(smallest == 3)
914 SetPlayerColors(pl, COLOR_TEAM3 - 1);
916 else if(smallest == 4)
918 SetPlayerColors(pl, COLOR_TEAM4 - 1);
922 error("smallest team: invalid team\n");
925 if(pl.deadflag == DEAD_NO)
926 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
932 //void() ctf_playerchanged;
933 void SV_ChangeTeam(float _color)
935 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
937 // in normal deathmatch we can just apply the color and we're done
938 if(!cvar("teamplay")) {
939 SetPlayerColors(self, _color);
943 scolor = self.clientcolors & 0x0F;
944 dcolor = _color & 0x0F;
946 if(scolor == COLOR_TEAM1 - 1)
948 else if(scolor == COLOR_TEAM2 - 1)
950 else if(scolor == COLOR_TEAM3 - 1)
952 else if(scolor == COLOR_TEAM4 - 1)
954 if(dcolor == COLOR_TEAM1 - 1)
956 else if(dcolor == COLOR_TEAM2 - 1)
958 else if(dcolor == COLOR_TEAM3 - 1)
960 else if(dcolor == COLOR_TEAM4 - 1)
963 CheckAllowedTeams(self);
965 if(dteam == 1 && c1 < 0) dteam = 4;
966 if(dteam == 4 && c4 < 0) dteam = 3;
967 if(dteam == 3 && c3 < 0) dteam = 2;
968 if(dteam == 2 && c2 < 0) dteam = 1;
970 // not changing teams
973 //bprint("same team change\n");
974 SetPlayerTeam(self, dteam, steam, TRUE);
980 if(cvar("g_campaign") || cvar("g_changeteam_banned"))
982 sprint(self, "Team changes not allowed\n");
983 return; // changing teams is not allowed
986 if(!cvar("g_campaign") && cvar("g_balance_teams_prevent_imbalance"))
988 // only allow changing to a smaller or equal size team
990 // find out what teams are available
991 //CheckAllowedTeams();
992 // count how many players on each team
993 GetTeamCounts(world);
996 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
1001 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
1006 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
1011 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
1018 sprint(self, "Cannot change to an invalid team\n");
1023 // get starting team
1024 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
1026 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
1028 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
1030 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
1033 if(scount) // started at a valid, nonempty team
1035 // check if we're trying to change to a larger team that doens't have bots to swap with
1036 if(dcount >= scount && dbotcount <= 0)
1038 sprint(self, "Cannot change to a larger team\n");
1039 return; // can't change to a larger team
1045 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
1047 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1049 // reduce frags during a team change
1050 TeamchangeFrags(self);
1053 SetPlayerTeam(self, dteam, steam, FALSE);
1055 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1057 // kill player when changing teams
1058 if(self.deadflag == DEAD_NO)
1059 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
1061 //ctf_playerchanged();
1064 void ShufflePlayerOutOfTeam (float source_team)
1066 float smallestteam, smallestteam_count, steam;
1067 float lowest_bot_score, lowest_player_score;
1068 entity head, lowest_bot, lowest_player, selected;
1071 smallestteam_count = 999999999;
1073 if(c1 >= 0 && c1 < smallestteam_count)
1076 smallestteam_count = c1;
1078 if(c2 >= 0 && c2 < smallestteam_count)
1081 smallestteam_count = c2;
1083 if(c3 >= 0 && c3 < smallestteam_count)
1086 smallestteam_count = c3;
1088 if(c4 >= 0 && c4 < smallestteam_count)
1091 smallestteam_count = c4;
1096 bprint("warning: no smallest team\n");
1100 if(source_team == 1)
1101 steam = COLOR_TEAM1;
1102 else if(source_team == 2)
1103 steam = COLOR_TEAM2;
1104 else if(source_team == 3)
1105 steam = COLOR_TEAM3;
1106 else if(source_team == 4)
1107 steam = COLOR_TEAM4;
1110 lowest_bot_score = 999999999;
1111 lowest_player = world;
1112 lowest_player_score = 999999999;
1114 // find the lowest-scoring player & bot of that team
1115 FOR_EACH_PLAYER(head)
1117 if(head.team == steam)
1121 if(head.totalfrags < lowest_bot_score)
1124 lowest_bot_score = head.totalfrags;
1129 if(head.totalfrags < lowest_player_score)
1131 lowest_player = head;
1132 lowest_player_score = head.totalfrags;
1138 // prefers to move a bot...
1139 if(lowest_bot != world)
1140 selected = lowest_bot;
1141 // but it will move a player if it has to
1143 selected = lowest_player;
1144 // don't do anything if it couldn't find anyone
1147 bprint("warning: couldn't find a player to move from team\n");
1151 // smallest team gains a member
1152 if(smallestteam == 1)
1156 else if(smallestteam == 2)
1160 else if(smallestteam == 3)
1164 else if(smallestteam == 4)
1170 bprint("warning: destination team invalid\n");
1173 // source team loses a member
1174 if(source_team == 1)
1178 else if(source_team == 2)
1182 else if(source_team == 3)
1186 else if(source_team == 4)
1192 bprint("warning: source team invalid\n");
1196 // move the player to the new team
1197 TeamchangeFrags(selected);
1198 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1200 if(selected.deadflag == DEAD_NO)
1201 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1202 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1205 float lastRebalanceInfo;
1206 void CauseRebalance(float source_team, float howmany_toomany)
1211 if(IsTeamBalanceForced() == 1)
1213 bprint("Rebalancing Teams\n");
1214 ShufflePlayerOutOfTeam(source_team);
1218 if(cvar("g_balance_teams_complain"))
1219 if(howmany_toomany < cvar("g_balance_teams_complain"))
1221 if(time < lastRebalanceInfo + 90)
1223 lastRebalanceInfo = time;
1224 if(source_team == 1)
1225 steam = COLOR_TEAM1;
1226 else if(source_team == 2)
1227 steam = COLOR_TEAM2;
1228 else if(source_team == 3)
1229 steam = COLOR_TEAM3;
1230 else if(source_team == 4)
1231 steam = COLOR_TEAM4;
1232 print("Team ", ftos(source_team), " too large, complaining.\n");
1233 FOR_EACH_REALPLAYER(head)
1235 if(head.team == steam)
1237 sprint(head, "\{1}\{13}^3SERVER NOTICE:^7 One of you please change teams!\n");
1238 centerprint_atprio(head, CENTERPRIO_REBALANCE, "^3SERVER NOTICE:\n\n^7Someone of you please change teams!");
1244 // part of g_balance_teams_force
1245 // occasionally perform an audit of the teams to make
1246 // sure they're more or less balanced in player count.
1249 float numplayers, numteams, smallest, toomany;
1251 balance = IsTeamBalanceForced();
1255 if(audit_teams_time > time)
1258 audit_teams_time = time + 4 + random();
1260 // bprint("Auditing teams\n");
1262 CheckAllowedTeams(world);
1263 GetTeamCounts(world);
1266 numteams = numplayers = smallest = 0;
1269 numteams = numteams + 1;
1270 numplayers = numplayers + c1;
1275 numteams = numteams + 1;
1276 numplayers = numplayers + c2;
1282 numteams = numteams + 1;
1283 numplayers = numplayers + c3;
1289 numteams = numteams + 1;
1290 numplayers = numplayers + c4;
1296 return; // no players to move around
1298 return; // don't bother shuffling if for some reason there aren't any teams
1300 toomany = smallest + 1;
1302 if(c1 && c1 > toomany)
1303 CauseRebalance(1, c1 - toomany);
1304 if(c2 && c2 > toomany)
1305 CauseRebalance(2, c2 - toomany);
1306 if(c3 && c3 > toomany)
1307 CauseRebalance(3, c3 - toomany);
1308 if(c4 && c4 > toomany)
1309 CauseRebalance(4, c4 - toomany);
1311 // if teams are still unbalanced, balance them further in the next audit,
1312 // which will happen sooner (keep doing rapid audits until things are in order)
1313 audit_teams_time = time + 0.7 + random()*0.3;
1318 // code from here on is just to support maps that don't have team entities
1319 void tdm_spawnteam (string teamname, float teamcolor)
1323 e.classname = "tdm_team";
1324 e.netname = teamname;
1329 // spawn some default teams if the map is not set up for tdm
1330 void tdm_spawnteams()
1334 numteams = cvar("g_tdm_teams");
1336 tdm_spawnteam("Red", COLOR_TEAM1-1);
1337 tdm_spawnteam("Blue", COLOR_TEAM2-1);
1339 tdm_spawnteam("Yellow", COLOR_TEAM3-1);
1341 tdm_spawnteam("Pink", COLOR_TEAM4-1);
1344 void tdm_delayedinit()
1346 // if no teams are found, spawn defaults
1347 if (find(world, classname, "tdm_team") == world)
1353 InitializeEntity(world, tdm_delayedinit, INITPRIO_GAMETYPE);