1 string cache_mutatormsg;
2 string cache_lastmutatormsg;
4 // client counts for each team
6 // # of bots on those teams
7 float cb1, cb2, cb3, cb4;
9 float audit_teams_time;
11 float IsTeamBalanceForced()
13 if(intermission_running)
14 return 0; // no rebalancing whatsoever please
17 if(autocvar_g_campaign)
19 if(autocvar_bot_vs_human && (c3==-1 && c4==-1))
21 if(!autocvar_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();
77 void LogTeamchange(float player_id, float team_number, float type)
79 if(!autocvar_sv_eventlog)
85 GameLogEcho(strcat(":team:", ftos(player_id), ":", ftos(team_number), ":", ftos(type)));
88 void default_delayedinit()
90 if(!scores_initialized)
94 void ActivateTeamplay()
96 serverflags |= SERVERFLAG_TEAMPLAY;
100 void InitGameplayMode()
102 float fraglimit_override, timelimit_override, leadlimit_override, qualifying_override;
104 qualifying_override = -1;
108 // find out good world mins/maxs bounds, either the static bounds found by looking for solid, or the mapinfo specified bounds
113 MapInfo_LoadMapSettings(mapname);
115 serverflags &~= SERVERFLAG_TEAMPLAY;
117 if not(cvar_value_issafe(world.fog))
119 print("The current map contains a potentially harmful fog setting, ignored\n");
120 world.fog = string_null;
122 if(MapInfo_Map_fog != "")
123 if(MapInfo_Map_fog == "none")
124 world.fog = string_null;
126 world.fog = strzone(MapInfo_Map_fog);
127 clientstuff = strzone(MapInfo_Map_clientstuff);
129 MapInfo_ClearTemps();
131 // set both here, gamemode can override it later
132 timelimit_override = autocvar_timelimit_override;
133 fraglimit_override = autocvar_fraglimit_override;
134 leadlimit_override = autocvar_leadlimit_override;
138 gamemode_name = "Deathmatch";
143 gamemode_name = "Team Deathmatch";
146 if(autocvar_g_tdm_team_spawns)
147 have_team_spawns = -1; // request team spawns
152 gamemode_name = "Domination";
154 fraglimit_override = autocvar_g_domination_point_limit;
155 leadlimit_override = autocvar_g_domination_point_leadlimit;
157 have_team_spawns = -1; // request team spawns
162 gamemode_name = "Capture the Flag";
164 g_ctf_ignore_frags = autocvar_g_ctf_ignore_frags;
165 if(g_ctf_win_mode == 2)
167 fraglimit_override = autocvar_g_ctf_capture_limit;
168 leadlimit_override = autocvar_g_ctf_capture_leadlimit;
172 fraglimit_override = autocvar_capturelimit_override;
173 leadlimit_override = autocvar_captureleadlimit_override;
176 have_team_spawns = -1; // request team spawns
181 gamemode_name = "Rune Match";
182 // ActivateTeamplay();
183 fraglimit_override = autocvar_g_runematch_point_limit;
184 leadlimit_override = autocvar_g_runematch_point_leadlimit;
190 gamemode_name = "Last Man Standing";
191 fraglimit_override = autocvar_g_lms_lives_override;
192 leadlimit_override = 0; // not supported by LMS
193 if(fraglimit_override == 0)
194 fraglimit_override = -1;
195 lms_lowest_lives = 9999;
202 gamemode_name = "Arena";
203 fraglimit_override = autocvar_g_arena_point_limit;
204 leadlimit_override = autocvar_g_arena_point_leadlimit;
205 maxspawned = autocvar_g_arena_maxspawned;
208 arena_roundbased = autocvar_g_arena_roundbased;
213 gamemode_name = "Clan Arena";
215 fraglimit_override = autocvar_g_ca_point_limit;
216 leadlimit_override = autocvar_g_ca_point_leadlimit;
217 precache_sound("ctf/red_capture.wav");
218 precache_sound("ctf/blue_capture.wav");
222 gamemode_name = "Key Hunt";
224 fraglimit_override = autocvar_g_keyhunt_point_limit;
225 leadlimit_override = autocvar_g_keyhunt_point_leadlimit;
226 MUTATOR_ADD(gamemode_keyhunt);
231 gamemode_name = "Freeze Tag";
233 fraglimit_override = autocvar_g_freezetag_point_limit;
234 leadlimit_override = autocvar_g_freezetag_point_leadlimit;
235 MUTATOR_ADD(gamemode_freezetag);
240 gamemode_name = "Assault";
242 ScoreRules_assault();
243 have_team_spawns = -1; // request team spawns
248 gamemode_name = "Onslaught";
250 have_team_spawns = -1; // request team spawns
255 gamemode_name = "Race";
257 if(autocvar_g_race_teams)
260 race_teams = bound(2, autocvar_g_race_teams, 4);
261 have_team_spawns = -1; // request team spawns
266 qualifying_override = autocvar_g_race_qualifying_timelimit_override;
267 fraglimit_override = autocvar_g_race_laps_limit;
268 leadlimit_override = 0; // currently not supported by race
273 gamemode_name = "CTS";
274 g_race_qualifying = 1;
275 fraglimit_override = 0;
276 leadlimit_override = 0;
281 gamemode_name = "Nexball";
282 fraglimit_override = autocvar_g_nexball_goallimit;
283 leadlimit_override = autocvar_g_nexball_goalleadlimit;
286 have_team_spawns = -1; // request team spawns
291 gamemode_name = "Keepaway";
292 MUTATOR_ADD(gamemode_keepaway);
298 cache_mutatormsg = strzone("");
299 cache_lastmutatormsg = strzone("");
301 // enforce the server's universal frag/time limits
302 if(!autocvar_g_campaign)
304 if(fraglimit_override >= 0)
305 cvar_set("fraglimit", ftos(fraglimit_override));
306 if(timelimit_override >= 0)
307 cvar_set("timelimit", ftos(timelimit_override));
308 if(leadlimit_override >= 0)
309 cvar_set("leadlimit", ftos(leadlimit_override));
310 if(qualifying_override >= 0)
311 cvar_set("g_race_qualifying_timelimit", ftos(qualifying_override));
316 // we need to find out the correct value for g_race_qualifying
317 if(autocvar_g_campaign)
319 g_race_qualifying = 1;
321 else if(!autocvar_g_campaign && autocvar_g_race_qualifying_timelimit > 0)
323 g_race_qualifying = 2;
324 race_fraglimit = autocvar_fraglimit;
325 race_leadlimit = autocvar_leadlimit;
326 race_timelimit = autocvar_timelimit;
327 cvar_set("fraglimit", "0");
328 cvar_set("leadlimit", "0");
329 cvar_set("timelimit", ftos(autocvar_g_race_qualifying_timelimit));
332 g_race_qualifying = 0;
337 if(g_race_qualifying)
338 independent_players = 1;
343 InitializeEntity(world, default_delayedinit, INITPRIO_GAMETYPE_FALLBACK);
346 string GetClientVersionMessage() {
348 if (self.version_mismatch) {
349 if(self.version < autocvar_gameversion) {
350 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
352 versionmsg = "^3This server is using an outdated Xonotic version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
355 versionmsg = "^2client version and server version are compatible.^8";
360 string getwelcomemessage(void)
362 string s, modifications, motd;
365 MUTATOR_CALLHOOK(BuildMutatorsPrettyString);
366 modifications = ret_string;
369 modifications = strcat(modifications, ", MinstaGib");
372 if(g_weaponarena_random)
373 modifications = strcat(modifications, ", ", ftos(g_weaponarena_random), " of ", g_weaponarena_list, " Arena");
375 modifications = strcat(modifications, ", ", g_weaponarena_list, " Arena");
377 if(autocvar_g_start_weapon_laser == 0)
378 modifications = strcat(modifications, ", No start weapons");
379 if(autocvar_sv_gravity < 800)
380 modifications = strcat(modifications, ", Low gravity");
381 if(g_cloaked && !g_cts)
382 modifications = strcat(modifications, ", Cloaked");
384 modifications = strcat(modifications, ", Hook");
386 modifications = strcat(modifications, ", Midair");
388 modifications = strcat(modifications, ", Piñata");
389 if(g_weapon_stay && !g_cts)
390 modifications = strcat(modifications, ", Weapons stay");
392 modifications = strcat(modifications, ", Blood loss");
394 modifications = strcat(modifications, ", Jet pack");
395 if(autocvar_g_powerups == 0)
396 modifications = strcat(modifications, ", No powerups");
397 if(autocvar_g_powerups > 0)
398 modifications = strcat(modifications, ", Powerups");
399 modifications = substring(modifications, 2, strlen(modifications) - 2);
401 string versionmessage;
402 versionmessage = GetClientVersionMessage();
404 s = strcat("This is Xonotic ", autocvar_g_xonoticversion, "\n", versionmessage);
405 s = strcat(s, "^8\n\nmatch type is ^1", gamemode_name, "^8\n");
407 if(modifications != "")
408 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
410 if (g_grappling_hook)
411 s = strcat(s, "\n\n^3grappling hook^8 is enabled, press 'e' to use it\n");
413 if(cache_lastmutatormsg != autocvar_g_mutatormsg)
415 if(cache_lastmutatormsg)
416 strunzone(cache_lastmutatormsg);
418 strunzone(cache_mutatormsg);
419 cache_lastmutatormsg = strzone(autocvar_g_mutatormsg);
420 cache_mutatormsg = strzone(cache_lastmutatormsg);
423 if (cache_mutatormsg != "") {
424 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
427 motd = autocvar_sv_motd;
429 s = strcat(s, "\n\n^8MOTD: ^7", strreplace("\\n", "\n", motd));
434 void SetPlayerColors(entity pl, float _color)
438 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
440 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
441 pl.clientcolors = 16*cl + cl;*/
444 pants = _color & 0x0F;
445 shirt = _color & 0xF0;
449 setcolor(pl, 16*pants + pants);
451 setcolor(pl, shirt + pants);
455 void SetPlayerTeam(entity pl, float t, float s, float noprint)
460 _color = COLOR_TEAM4 - 1;
462 _color = COLOR_TEAM3 - 1;
464 _color = COLOR_TEAM2 - 1;
466 _color = COLOR_TEAM1 - 1;
468 SetPlayerColors(pl,_color);
471 LogTeamchange(pl.playerid, pl.team, 3); // log manual team join
474 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
479 // set c1...c4 to show what teams are allowed
480 void CheckAllowedTeams (entity for_whom)
486 c1 = c2 = c3 = c4 = -1;
487 cb1 = cb2 = cb3 = cb4 = 0;
491 // onslaught is special
492 head = findchain(classname, "onslaught_generator");
495 if (head.team == COLOR_TEAM1) c1 = 0;
496 if (head.team == COLOR_TEAM2) c2 = 0;
497 if (head.team == COLOR_TEAM3) c3 = 0;
498 if (head.team == COLOR_TEAM4) c4 = 0;
502 else if(g_domination)
503 teament_name = "dom_team";
505 teament_name = "ctf_team";
507 teament_name = "tdm_team";
509 teament_name = "nexball_team";
511 c1 = c2 = 0; // Assault always has 2 teams
514 // cover anything else by treating it like tdm with no teams spawned
521 MUTATOR_CALLHOOK(GetTeamCount);
525 c1 = c2 = c3 = c4 = 0;
532 // find out what teams are allowed if necessary
535 head = find(world, classname, teament_name);
538 if(!(g_domination && head.netname == ""))
540 if(head.team == COLOR_TEAM1)
542 else if(head.team == COLOR_TEAM2)
544 else if(head.team == COLOR_TEAM3)
546 else if(head.team == COLOR_TEAM4)
549 head = find(head, classname, teament_name);
553 // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
555 if(autocvar_bot_vs_human && for_whom)
557 if(autocvar_bot_vs_human > 0)
560 if(clienttype(for_whom) == CLIENTTYPE_BOT)
568 if(clienttype(for_whom) == CLIENTTYPE_BOT)
575 // if player has a forced team, ONLY allow that one
576 if(self.team_forced == COLOR_TEAM1 && c1 >= 0)
578 else if(self.team_forced == COLOR_TEAM2 && c2 >= 0)
580 else if(self.team_forced == COLOR_TEAM3 && c3 >= 0)
582 else if(self.team_forced == COLOR_TEAM4 && c4 >= 0)
586 float PlayerValue(entity p)
588 if(IsTeamBalanceForced() == 1)
591 // FIXME: it always returns 1...
594 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
595 // teams that are allowed will now have their player counts stored in c1...c4
596 void GetTeamCounts(entity ignore)
600 // now count how many players are on each team already
602 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
603 // also remember the lowest-scoring player
605 FOR_EACH_CLIENT(head)
608 if(head.classname == "player")
610 else if(head.team_forced > 0)
611 t = head.team_forced; // reserve the spot
614 if(head != ignore)// && head.netname != "")
616 value = PlayerValue(head);
617 if(clienttype(head) == CLIENTTYPE_BOT)
656 // if the player who has a forced team has not joined yet, reserve the spot
657 if(autocvar_g_campaign)
659 switch(autocvar_g_campaign_forceteam)
661 case 1: if(c1 == cb1) ++c1; break;
662 case 2: if(c2 == cb2) ++c2; break;
663 case 3: if(c3 == cb3) ++c3; break;
664 case 4: if(c4 == cb4) ++c4; break;
669 // returns # of smallest team (1, 2, 3, 4)
670 // NOTE: Assumes CheckAllowedTeams has already been called!
671 float FindSmallestTeam(entity pl, float ignore_pl)
673 float totalteams, balance_type, maxc;
676 // find out what teams are available
677 //CheckAllowedTeams();
679 // make sure there are at least 2 teams to join
681 totalteams = totalteams + 1;
683 totalteams = totalteams + 1;
685 totalteams = totalteams + 1;
687 totalteams = totalteams + 1;
689 if((autocvar_bot_vs_human || pl.team_forced > 0) && totalteams == 1)
694 if(autocvar_g_campaign && pl && clienttype(pl) == CLIENTTYPE_REAL)
695 return 1; // special case for campaign and player joining
696 else if(g_domination)
697 error("Too few teams available for domination\n");
699 error("Too few teams available for ctf\n");
701 error("Too few teams available for key hunt\n");
703 error("Too few teams available for freeze tag\n");
705 error("Too few teams available for team deathmatch\n");
708 // count how many players are in each team
712 GetTeamCounts(world);
714 // c1...c4 now have counts of each team
715 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
717 // 2 gives priority to what team you're already on, 1 goes in order
718 // 2 doesn't seem to work though...
722 //if(pl.classname != "player")
723 if(clienttype(pl) != CLIENTTYPE_BOT)
725 c1 -= cb1 * 255.0/256.0;
726 c2 -= cb2 * 255.0/256.0;
727 c3 -= cb3 * 255.0/256.0;
728 c4 -= cb4 * 255.0/256.0;
730 maxc = max4(c1, c2, c3, c4);
732 RandomSelection_Init();
733 if(balance_type == 1)
735 // 1: use team count, then score (note: can only use 8 significant bits of score)
736 if(c1 >= 0) RandomSelection_Add(world, 1, string_null, 1, (maxc - c1) + float2range01(-team1_score) / 256.0);
737 if(c2 >= 0) RandomSelection_Add(world, 2, string_null, 1, (maxc - c2) + float2range01(-team2_score) / 256.0);
738 if(c3 >= 0) RandomSelection_Add(world, 3, string_null, 1, (maxc - c3) + float2range01(-team3_score) / 256.0);
739 if(c4 >= 0) RandomSelection_Add(world, 4, string_null, 1, (maxc - c4) + float2range01(-team4_score) / 256.0);
741 else if(balance_type == 2)
743 // 1: use team count, if equal prefer own team
744 if(c1 >= 0) RandomSelection_Add(world, 1, string_null, 1, (maxc - c1) + (self.team == COLOR_TEAM1) / 512.0);
745 if(c2 >= 0) RandomSelection_Add(world, 2, string_null, 1, (maxc - c1) + (self.team == COLOR_TEAM2) / 512.0);
746 if(c3 >= 0) RandomSelection_Add(world, 3, string_null, 1, (maxc - c1) + (self.team == COLOR_TEAM3) / 512.0);
747 if(c4 >= 0) RandomSelection_Add(world, 4, string_null, 1, (maxc - c1) + (self.team == COLOR_TEAM4) / 512.0);
749 else if(balance_type == 3)
751 // 1: use team count, then score, if equal prefer own team (probably fails due to float accuracy problems)
752 if(c1 >= 0) RandomSelection_Add(world, 1, string_null, 1, (maxc - c1) + float2range01(-team1_score + 0.5 * (self.team == COLOR_TEAM1)) / 256.0);
753 if(c2 >= 0) RandomSelection_Add(world, 2, string_null, 1, (maxc - c2) + float2range01(-team2_score + 0.5 * (self.team == COLOR_TEAM2)) / 256.0);
754 if(c3 >= 0) RandomSelection_Add(world, 3, string_null, 1, (maxc - c3) + float2range01(-team3_score + 0.5 * (self.team == COLOR_TEAM3)) / 256.0);
755 if(c4 >= 0) RandomSelection_Add(world, 4, string_null, 1, (maxc - c4) + float2range01(-team4_score + 0.5 * (self.team == COLOR_TEAM4)) / 256.0);
757 return RandomSelection_chosen_float;
760 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
762 float smallest, selectedteam;
764 // don't join a team if we're not playing a team game
768 // find out what teams are available
769 CheckAllowedTeams(pl);
771 // if we don't care what team he ends up on, put him on whatever team he entered as.
772 // if he's not on a valid team, then let other code put him on the smallest team
775 if( c1 >= 0 && pl.team == COLOR_TEAM1)
776 selectedteam = pl.team;
777 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
778 selectedteam = pl.team;
779 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
780 selectedteam = pl.team;
781 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
782 selectedteam = pl.team;
788 if(!only_return_best)
790 SetPlayerColors(pl, selectedteam - 1);
792 // when JoinBestTeam is called by client.qc/ClientKill_Now_TeamChange the players team is -1 and thus skipped
793 // when JoinBestTeam is called by cl_client.qc/ClientConnect the player_id is 0 the log attempt is rejected
794 LogTeamchange(pl.playerid, pl.team, 99);
798 // otherwise end up on the smallest team (handled below)
801 smallest = FindSmallestTeam(pl, TRUE);
803 if(!only_return_best && !pl.bot_forced_team)
805 TeamchangeFrags(self);
808 SetPlayerColors(pl, COLOR_TEAM1 - 1);
810 else if(smallest == 2)
812 SetPlayerColors(pl, COLOR_TEAM2 - 1);
814 else if(smallest == 3)
816 SetPlayerColors(pl, COLOR_TEAM3 - 1);
818 else if(smallest == 4)
820 SetPlayerColors(pl, COLOR_TEAM4 - 1);
824 error("smallest team: invalid team\n");
827 LogTeamchange(pl.playerid, pl.team, 2); // log auto join
829 if(pl.deadflag == DEAD_NO)
830 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
836 //void() ctf_playerchanged;
837 void SV_ChangeTeam(float _color)
839 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
841 // in normal deathmatch we can just apply the color and we're done
843 SetPlayerColors(self, _color);
847 scolor = self.clientcolors & 0x0F;
848 dcolor = _color & 0x0F;
850 if(scolor == COLOR_TEAM1 - 1)
852 else if(scolor == COLOR_TEAM2 - 1)
854 else if(scolor == COLOR_TEAM3 - 1)
856 else // if(scolor == COLOR_TEAM4 - 1)
858 if(dcolor == COLOR_TEAM1 - 1)
860 else if(dcolor == COLOR_TEAM2 - 1)
862 else if(dcolor == COLOR_TEAM3 - 1)
864 else // if(dcolor == COLOR_TEAM4 - 1)
867 CheckAllowedTeams(self);
869 if(dteam == 1 && c1 < 0) dteam = 4;
870 if(dteam == 4 && c4 < 0) dteam = 3;
871 if(dteam == 3 && c3 < 0) dteam = 2;
872 if(dteam == 2 && c2 < 0) dteam = 1;
874 // not changing teams
877 //bprint("same team change\n");
878 SetPlayerTeam(self, dteam, steam, TRUE);
882 if((autocvar_g_campaign) || (autocvar_g_changeteam_banned && self.wasplayer)) {
883 sprint(self, "Team changes not allowed\n");
884 return; // changing teams is not allowed
887 if(autocvar_g_balance_teams_prevent_imbalance)
889 // only allow changing to a smaller or equal size team
891 // find out what teams are available
892 //CheckAllowedTeams();
893 // count how many players on each team
894 GetTeamCounts(world);
897 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
902 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
907 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
912 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
919 sprint(self, "Cannot change to an invalid team\n");
925 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
927 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
929 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
931 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
934 if(scount) // started at a valid, nonempty team
936 // check if we're trying to change to a larger team that doens't have bots to swap with
937 if(dcount >= scount && dbotcount <= 0)
939 sprint(self, "Cannot change to a larger team\n");
940 return; // can't change to a larger team
945 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
947 if(self.classname == "player" && steam != dteam)
949 // reduce frags during a team change
950 TeamchangeFrags(self);
953 SetPlayerTeam(self, dteam, steam, FALSE);
955 if(self.classname == "player" && steam != dteam)
957 // kill player when changing teams
958 if(self.deadflag == DEAD_NO)
959 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
961 //ctf_playerchanged();
964 void ShufflePlayerOutOfTeam (float source_team)
966 float smallestteam, smallestteam_count, steam;
967 float lowest_bot_score, lowest_player_score;
968 entity head, lowest_bot, lowest_player, selected;
971 smallestteam_count = 999999999;
973 if(c1 >= 0 && c1 < smallestteam_count)
976 smallestteam_count = c1;
978 if(c2 >= 0 && c2 < smallestteam_count)
981 smallestteam_count = c2;
983 if(c3 >= 0 && c3 < smallestteam_count)
986 smallestteam_count = c3;
988 if(c4 >= 0 && c4 < smallestteam_count)
991 smallestteam_count = c4;
996 bprint("warning: no smallest team\n");
1000 if(source_team == 1)
1001 steam = COLOR_TEAM1;
1002 else if(source_team == 2)
1003 steam = COLOR_TEAM2;
1004 else if(source_team == 3)
1005 steam = COLOR_TEAM3;
1006 else if(source_team == 4)
1007 steam = COLOR_TEAM4;
1010 lowest_bot_score = 999999999;
1011 lowest_player = world;
1012 lowest_player_score = 999999999;
1014 // find the lowest-scoring player & bot of that team
1015 FOR_EACH_PLAYER(head)
1017 if(head.team == steam)
1021 if(head.totalfrags < lowest_bot_score)
1024 lowest_bot_score = head.totalfrags;
1029 if(head.totalfrags < lowest_player_score)
1031 lowest_player = head;
1032 lowest_player_score = head.totalfrags;
1038 // prefers to move a bot...
1039 if(lowest_bot != world)
1040 selected = lowest_bot;
1041 // but it will move a player if it has to
1043 selected = lowest_player;
1044 // don't do anything if it couldn't find anyone
1047 bprint("warning: couldn't find a player to move from team\n");
1051 // smallest team gains a member
1052 if(smallestteam == 1)
1056 else if(smallestteam == 2)
1060 else if(smallestteam == 3)
1064 else if(smallestteam == 4)
1070 bprint("warning: destination team invalid\n");
1073 // source team loses a member
1074 if(source_team == 1)
1078 else if(source_team == 2)
1082 else if(source_team == 3)
1086 else if(source_team == 4)
1092 bprint("warning: source team invalid\n");
1096 // move the player to the new team
1097 TeamchangeFrags(selected);
1098 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1100 if(selected.deadflag == DEAD_NO)
1101 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1102 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1105 void CauseRebalance(float source_team, float howmany_toomany)
1107 if(IsTeamBalanceForced() == 1)
1109 bprint("Rebalancing Teams\n");
1110 ShufflePlayerOutOfTeam(source_team);
1114 // part of g_balance_teams_force
1115 // occasionally perform an audit of the teams to make
1116 // sure they're more or less balanced in player count.
1119 float numplayers, numteams, smallest, toomany;
1121 balance = IsTeamBalanceForced();
1125 if(audit_teams_time > time)
1128 audit_teams_time = time + 4 + random();
1130 // bprint("Auditing teams\n");
1132 CheckAllowedTeams(world);
1133 GetTeamCounts(world);
1136 numteams = numplayers = smallest = 0;
1139 numteams = numteams + 1;
1140 numplayers = numplayers + c1;
1145 numteams = numteams + 1;
1146 numplayers = numplayers + c2;
1152 numteams = numteams + 1;
1153 numplayers = numplayers + c3;
1159 numteams = numteams + 1;
1160 numplayers = numplayers + c4;
1166 return; // no players to move around
1168 return; // don't bother shuffling if for some reason there aren't any teams
1170 toomany = smallest + 1;
1172 if(c1 && c1 > toomany)
1173 CauseRebalance(1, c1 - toomany);
1174 if(c2 && c2 > toomany)
1175 CauseRebalance(2, c2 - toomany);
1176 if(c3 && c3 > toomany)
1177 CauseRebalance(3, c3 - toomany);
1178 if(c4 && c4 > toomany)
1179 CauseRebalance(4, c4 - toomany);
1181 // if teams are still unbalanced, balance them further in the next audit,
1182 // which will happen sooner (keep doing rapid audits until things are in order)
1183 audit_teams_time = time + 0.7 + random()*0.3;
1186 // code from here on is just to support maps that don't have team entities
1187 void tdm_spawnteam (string teamname, float teamcolor)
1191 e.classname = "tdm_team";
1192 e.netname = teamname;
1197 // spawn some default teams if the map is not set up for tdm
1198 void tdm_spawnteams()
1202 numteams = autocvar_g_tdm_teams_override;
1204 numteams = autocvar_g_tdm_teams;
1205 numteams = bound(2, numteams, 4);
1207 tdm_spawnteam("Red", COLOR_TEAM1-1);
1208 tdm_spawnteam("Blue", COLOR_TEAM2-1);
1210 tdm_spawnteam("Yellow", COLOR_TEAM3-1);
1212 tdm_spawnteam("Pink", COLOR_TEAM4-1);
1215 void tdm_delayedinit()
1217 // if no teams are found, spawn defaults
1218 if (find(world, classname, "tdm_team") == world)
1224 InitializeEntity(world, tdm_delayedinit, INITPRIO_GAMETYPE);