1 // client counts for each team
3 // # of bots on those teams
4 float cb1, cb2, cb3, cb4;
6 float audit_teams_time;
8 float() IsTeamBalanceForced = {
9 if(intermission_running)
10 return 0; // no rebalancing whatsoever please
13 if(cvar("g_campaign"))
15 if(!cvar("g_balance_teams_force"))
20 void TeamchangeFrags(entity e)
22 if(e.classname == "player")
24 // reduce frags during a team change
25 DistributeFragsAmongTeam(e, (e.clientcolors & 0x0F) + 1, 1 - cvar("g_changeteam_fragtransfer") / 100);
29 vector TeamColor(float teem)
34 return '1 0.0625 0.0625';
36 return '0.0625 0.0625 1';
46 string TeamName(float t)
48 return strcat(Team_ColorName(t), " Team");
50 string ColoredTeamName(float t)
52 return strcat(Team_ColorCode(t), Team_ColorName(t), " Team^7");
54 string TeamNoName(float t)
56 // fixme: Search for team entities and get their .netname's!
65 return "Neutral Team";
70 void runematch_init();
73 void(entity pl) LogTeamchange =
76 if(!cvar("sv_eventlog"))
80 str = strcat(":team:", ftos(pl.playerid), ":");
81 str = strcat(str, ftos(pl.team));
82 GameLogEcho(str, FALSE);
87 cvar_set("g_dm", "0");
88 cvar_set("g_tdm", "0");
89 cvar_set("g_domination", "0");
90 cvar_set("g_ctf", "0");
91 cvar_set("g_runematch", "0");
92 cvar_set("g_lms", "0");
93 cvar_set("g_arena", "0");
94 cvar_set("g_keyhunt", "0");
95 cvar_set("g_assault", "0");
96 cvar_set("g_onslaught", "0");
97 cvar_set("teamplay", "0");
100 void ActivateTeamplay()
102 float teamplay_default;
103 teamplay_default = cvar("teamplay_default");
106 cvar_set("teamplay", ftos(teamplay_default));
108 cvar_set("teamplay", "3");
111 void InitGameplayMode()
113 float fraglimit_override, timelimit_override;
118 game = cvar("gamecfg"); // load game options
120 // game cvars get reset before map changes
121 // then map's cfg sets them as desired
123 // FIXME: also set a message or game mode name to print to players when the join
125 // set both here, gamemode can override it later
126 timelimit_override = cvar("timelimit_override");
127 fraglimit_override = cvar("fraglimit_override");
129 if(game == GAME_DOMINATION || cvar("g_domination"))
132 game = GAME_DOMINATION;
133 cvar_set("g_domination", "1");
137 fraglimit_override = cvar("g_domination_point_limit");
139 gamemode_name = "Domination";
142 else if(game == GAME_CTF || cvar("g_ctf"))
146 cvar_set("g_ctf", "1");
150 fraglimit_override = cvar("g_ctf_capture_limit");
152 gamemode_name = "Capture the Flag";
155 else if(game == GAME_RUNEMATCH || cvar("g_runematch"))
157 game = GAME_RUNEMATCH;
158 cvar_set("g_runematch", "1");
159 cvar_set("g_minstagib", "0");
161 if(cvar("deathmatch_force_teamplay"))
164 fraglimit_override = cvar("g_runematch_point_limit");
166 gamemode_name = "Rune Match";
172 else if(game == GAME_DEATHMATCH || game == GAME_TEAM_DEATHMATCH || cvar("g_tdm"))
174 if(!cvar("deathmatch"))
175 cvar_set("deathmatch", "1");
178 if(game == GAME_TEAM_DEATHMATCH || cvar("g_tdm") || cvar("deathmatch_force_teamplay"))
181 game = GAME_TEAM_DEATHMATCH;
182 gamemode_name = "Team Deathmatch";
185 cvar_set("g_tdm", "1");
190 game = GAME_DEATHMATCH;
191 gamemode_name = "Deathmatch";
195 fraglimit_override = cvar("fraglimit_override");
197 else if(game == GAME_ASSAULT || cvar("g_assault"))
201 gamemode_name = "Assault";
204 cvar_set("g_assault", "1");
206 else if(game == GAME_LMS || cvar("g_lms"))
210 cvar_set("g_lms", "1");
211 fraglimit_override = cvar("g_lms_lives_override");
212 if(fraglimit_override == 0)
213 fraglimit_override = -1;
214 gamemode_name = "Last Man Standing";
216 lms_lowest_lives = 999;
218 else if(game == GAME_ARENA || cvar("g_arena"))
222 cvar_set("g_arena", "1");
223 fraglimit_override = cvar("g_arena_point_limit");
224 maxspawned = cvar("g_arena_maxspawned");
227 arena_roundbased = cvar("g_arena_roundbased");
228 gamemode_name = "Arena";
231 else if(game == GAME_KEYHUNT || cvar("g_keyhunt"))
235 cvar_set("g_keyhunt", "1");
236 fraglimit_override = cvar("g_keyhunt_point_limit");
238 gamemode_name = "Key Hunt";
241 else if(game == GAME_ONSLAUGHT || cvar("g_onslaught"))
244 game = GAME_ONSLAUGHT;
245 cvar_set("g_onslaught", "1");
249 gamemode_name = "Onslaught";
254 // we can only assume...
256 cvar_set("g_dm", "1");
257 gamemode_name = "Deathmatch";
260 /* else if(game == GAME_TEAM_DEATHMATCH)
262 if(!cvar("deathmatch"))
263 cvar_set("deathmatch", "1");
265 //if(!cvar("teamplay"))
266 // cvar_set("teamplay", "3");
269 fraglimit_override = cvar("fraglimit_override");
272 // those mutators rule each other out
273 if(cvar("g_minstagib"))
275 cvar_set("g_instagib", "0");
276 cvar_set("g_rocketarena", "0");
278 if(cvar("g_instagib"))
280 cvar_set("g_minstagib", "0");
281 cvar_set("g_rocketarena", "0");
283 if(cvar("g_rocketarena"))
285 cvar_set("g_instagib", "0");
286 cvar_set("g_minstagib", "0");
289 registercvar("_motd", "");
290 registercvar("_mutatormsg", "");
291 cvar_set("_motd", wordwrap(cvar_string("sv_motd"), 50));
292 cvar_set("_mutatormsg", wordwrap(cvar_string("g_mutatormsg"), 50));
294 g_domination = cvar("g_domination");
295 g_ctf = cvar("g_ctf");
296 g_lms = cvar("g_lms");
297 g_tdm = cvar("g_tdm");
298 g_runematch = cvar("g_runematch");
299 g_keyhunt = cvar("g_keyhunt");
300 g_onslaught = cvar("g_onslaught");
301 g_assault = cvar("g_assault");
302 g_arena = cvar("g_arena");
304 // enforce the server's universal frag/time limits
305 if(!cvar("g_campaign"))
307 if(fraglimit_override >= 0)
308 cvar_set("fraglimit", ftos(fraglimit_override));
309 if(timelimit_override >= 0)
310 cvar_set("timelimit", ftos(timelimit_override));
313 FOR_EACH_REALCLIENT(pl)
315 stuffcmd(pl, strcat("\nset gametype ", ftos(game), ";"));
318 if (game == GAME_DOMINATION)//cvar("g_domination"))
320 else if (game == GAME_CTF)//cvar("g_ctf"))
322 else if (game == GAME_RUNEMATCH)//cvar("g_runematch"))
324 else if (game == GAME_TEAM_DEATHMATCH)//cvar("g_runematch"))
326 else if (game == GAME_KEYHUNT)//cvar("g_keyhunt"))
330 string GetClientVersionMessage(float v) {
331 local string versionmsg;
333 versionmsg = "^1client is too old to get versioninfo.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nUPDATE!!! (http://www.nexuiz.com)^8";
334 // either that or someone wants to be funny
335 } else if (v != cvar("gameversion")) {
336 if(v < cvar("gameversion")) {
337 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
339 versionmsg = "^3This server is using an outdated Nexuiz version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
342 versionmsg = "^2client version and server version are compatible.^8";
344 return strzone(versionmsg);
349 void PrintWelcomeMessage(entity pl)
351 string s, mutator, modifications, padding;
353 /*if(self.welcomemessage_time > time)
355 self.welcomemessage_time = time + 0.8; */
357 if(self.cvar_scr_centertime == 0) return;
358 if(self.welcomemessage_time > time) return;
359 self.welcomemessage_time = time + self.cvar_scr_centertime * 0.6;
361 if(cvar("g_campaign"))
363 centerprint(pl, campaign_message);
369 if(self.classname == "observer")
371 if(g_lms && self.frags <= 0 && self.frags > -666)
372 return centerprint_atprio(self, CENTERPRIO_SPAM, strcat(NEWLINES, "^1You have no more lives left\nwait for next round\n\n\n^7press attack to spectate other players"));
373 else if(g_lms && self.frags == -666)
374 return centerprint_atprio(self, CENTERPRIO_SPAM, strcat(NEWLINES, "^1Match has already begun\nwait for next round\n\n\n^7press attack to spectate other players"));
376 else if(self.classname == "spectator")
378 if ((g_lms && self.frags < 1) || g_arena)
379 return centerprint_atprio(self, CENTERPRIO_SPAM, strcat(NEWLINES, "spectating ", self.enemy.netname, "\n\n\n^7press attack for next player\npress attack2 for free fly mode"));
381 return centerprint_atprio(self, CENTERPRIO_SPAM, strcat(NEWLINES, "spectating ", self.enemy.netname, "\n\n\n^7press jump to play\n^7press attack for next player\npress attack2 for free fly mode"));
386 mutator = "^2Minstagib ^1";
388 mutator = "^2Instagib ^1";
389 else if(g_rocketarena)
390 mutator = "^2Rocketarena ^1";
392 mutator = "^2No Items Nexuiz ^1";
395 // to protect against unheedingly made changes
397 modifications = strcat(modifications, ", ");
399 modifications = "midair";
403 modifications = strcat(modifications, ", ");
405 modifications = strcat(modifications, "vampire");
407 if(g_laserguided_missile) {
409 modifications = strcat(modifications, ", ");
411 modifications = strcat(modifications, "laser-guided-missiles");
414 local string versionmessage;
415 versionmessage = GetClientVersionMessage(self.version);
417 s = strcat(s, NEWLINES, "This is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
418 s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
420 if(modifications != "")
421 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
423 if(time < restart_countdown)
424 s = strcat(s, "\n^1Game starts in ", ftos(ceil(restart_countdown-time)), " seconds^7");
428 if (g_grappling_hook)
429 s = strcat(s, "\n\n^8grappling hook is enabled, press 'e' to use it\n");
431 if (cvar_string("_mutatormsg") != "") {
432 s = strcat(s, "\n\n^8special gameplay tips: ^7", cvar_string("_mutatormsg"));
435 if (cvar_string("_motd") != "") {
436 s = strcat(s, "\n\n^8MOTD: ^7", cvar_string("_motd"));
440 if(cvar("fraglimit"))
443 if(cvar("timelimit"))
446 s = strcat(s, "\n^8frag limit: ^7", cvar_string("fraglimit"), padding);
448 if(cvar("timelimit"))
449 s = strcat(s, "\n^8time limit: ^7", cvar_string("timelimit"), " minutes");
460 void SetPlayerColors(entity pl, float _color)
464 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
466 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
467 pl.clientcolors = 16*cl + cl;*/
470 pants = _color & 0x0F;
471 shirt = _color & 0xF0;
475 setcolor(pl, 16*pants + pants);
477 setcolor(pl, shirt + pants);
481 void SetPlayerTeam(entity pl, float t, float s, float noprint)
486 _color = COLOR_TEAM4 - 1;
488 _color = COLOR_TEAM3 - 1;
490 _color = COLOR_TEAM2 - 1;
492 _color = COLOR_TEAM1 - 1;
494 SetPlayerColors(pl,_color);
496 if(!noprint && t != s)
498 //bprint(pl.netname, " has changed to ", TeamNoName(t), "\n");
499 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
511 // set c1...c4 to show what teams are allowed
512 void CheckAllowedTeams (entity for_whom)
521 c1 = c2 = c3 = c4 = -1;
522 cb1 = cb2 = cb3 = cb4 = 0;
524 // onslaught is special
527 head = findchain(classname, "onslaught_generator");
530 if (head.team == COLOR_TEAM1) c1 = 0;
531 if (head.team == COLOR_TEAM2) c2 = 0;
532 if (head.team == COLOR_TEAM3) c3 = 0;
533 if (head.team == COLOR_TEAM4) c4 = 0;
540 teament_name = "dom_team";
542 teament_name = "ctf_team";
544 teament_name = "tdm_team";
547 c1 = c2 = 0; // Assault always has 2 teams
552 // cover anything else by treating it like tdm with no teams spawned
556 dm = cvar("g_tdm_teams");
558 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
562 c1 = c2 = c3 = c4 = 0;
575 // first find out what teams are allowed
576 head = find(world, classname, teament_name);
579 if(!(g_domination && head.netname == ""))
581 if(head.team == COLOR_TEAM1)
585 if(head.team == COLOR_TEAM2)
589 if(head.team == COLOR_TEAM3)
593 if(head.team == COLOR_TEAM4)
598 head = find(head, classname, teament_name);
601 if(cvar("bot_vs_human") > 0)
604 if(clienttype(for_whom) == CLIENTTYPE_BOT)
609 else if(cvar("bot_vs_human") < 0)
612 if(clienttype(for_whom) == CLIENTTYPE_BOT)
619 float PlayerValue(entity p)
621 if(IsTeamBalanceForced() == 1)
626 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
627 // teams that are allowed will now have their player counts stored in c1...c4
628 void GetTeamCounts(entity ignore)
632 // now count how many players are on each team already
634 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
635 // also remember the lowest-scoring player
637 FOR_EACH_PLAYER(head)
639 if(head != ignore)// && head.netname != "")
641 value = PlayerValue(head);
642 if(clienttype(head) == CLIENTTYPE_BOT)
646 if(head.team == COLOR_TEAM1)
654 if(head.team == COLOR_TEAM2)
662 if(head.team == COLOR_TEAM3)
670 if(head.team == COLOR_TEAM4)
682 // returns # of smallest team (1, 2, 3, 4)
683 // NOTE: Assumes CheckAllowedTeams has already been called!
684 float FindSmallestTeam(entity pl, float ignore_pl)
686 float totalteams, smallestteam, smallestteam_count, smallestteam_score, balance_type;
689 // find out what teams are available
690 //CheckAllowedTeams();
692 // make sure there are at least 2 teams to join
694 totalteams = totalteams + 1;
696 totalteams = totalteams + 1;
698 totalteams = totalteams + 1;
700 totalteams = totalteams + 1;
702 if(cvar("bot_vs_human"))
708 error("Too few teams available for domination\n");
710 error("Too few teams available for ctf\n");
712 error("Too few teams available for key hunt\n");
714 error("Too few teams available for team deathmatch\n");
718 // count how many players are in each team
722 GetTeamCounts(world);
724 // c1...c4 now have counts of each team
725 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
728 smallestteam_count = 999999999;
729 smallestteam_score = 999999999;
731 // 2 gives priority to what team you're already on, 1 goes in order
732 // 2 doesn't seem to work though...
736 //if(pl.classname != "player")
737 if(clienttype(pl) != CLIENTTYPE_BOT)
739 c1 -= cb1 * 255.0/256;
740 c2 -= cb2 * 255.0/256;
741 c3 -= cb3 * 255.0/256;
742 c4 -= cb4 * 255.0/256;
745 if(balance_type == 1)
747 if(c1 >= 0 && (c1 < smallestteam_count || (c1 <= smallestteam_count && team1_score < smallestteam_score)))
750 smallestteam_count = c1;
751 smallestteam_score = team1_score;
753 if(c2 >= 0 && (c2 < smallestteam_count || (c2 <= smallestteam_count && team2_score < smallestteam_score)))
756 smallestteam_count = c2;
757 smallestteam_score = team2_score;
759 if(c3 >= 0 && (c3 < smallestteam_count || (c3 <= smallestteam_count && team3_score < smallestteam_score)))
762 smallestteam_count = c3;
763 smallestteam_score = team3_score;
765 if(c4 >= 0 && (c4 < smallestteam_count || (c4 <= smallestteam_count && team4_score < smallestteam_score)))
768 smallestteam_count = c4;
769 smallestteam_score = team4_score;
774 if(c1 >= 0 && (c1 < smallestteam_count ||
775 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
778 smallestteam_count = c1;
780 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
781 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
784 smallestteam_count = c2;
786 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
787 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
790 smallestteam_count = c3;
792 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
793 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
796 smallestteam_count = c4;
803 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
805 float smallest, selectedteam;
807 // don't join a team if we're not playing a team game
808 if(!cvar("teamplay") && !g_domination && !g_ctf && !g_keyhunt)
811 // find out what teams are available
812 CheckAllowedTeams(pl);
816 if(cvar("g_domination_default_teams") < 3)
818 if(cvar("g_domination_default_teams") < 4)
822 // if we don't care what team he ends up on, put him on whatever team he entered as.
823 // if he's not on a valid team, then let other code put him on the smallest team
826 if( c1 >= 0 && pl.team == COLOR_TEAM1)
827 selectedteam = pl.team;
828 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
829 selectedteam = pl.team;
830 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
831 selectedteam = pl.team;
832 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
833 selectedteam = pl.team;
838 if(!only_return_best)
840 SetPlayerColors(pl, selectedteam - 1);
845 // otherwise end up on the smallest team (handled below)
848 smallest = FindSmallestTeam(pl, TRUE);
851 if(!only_return_best)
853 TeamchangeFrags(self);
856 SetPlayerColors(pl, COLOR_TEAM1 - 1);
858 else if(smallest == 2)
860 SetPlayerColors(pl, COLOR_TEAM2 - 1);
862 else if(smallest == 3)
864 SetPlayerColors(pl, COLOR_TEAM3 - 1);
866 else if(smallest == 4)
868 SetPlayerColors(pl, COLOR_TEAM4 - 1);
872 error("smallest team: invalid team\n");
875 if(pl.deadflag == DEAD_NO)
876 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
883 void SV_ChangeTeam(float _color)
885 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
887 // in normal deathmatch we can just apply the color and we're done
888 if(!cvar("teamplay")) {
889 SetPlayerColors(self, _color);
893 scolor = self.clientcolors & 0x0F;
894 dcolor = _color & 0x0F;
896 if(scolor == COLOR_TEAM1 - 1)
898 else if(scolor == COLOR_TEAM2 - 1)
900 else if(scolor == COLOR_TEAM3 - 1)
902 else if(scolor == COLOR_TEAM4 - 1)
904 if(dcolor == COLOR_TEAM1 - 1)
906 else if(dcolor == COLOR_TEAM2 - 1)
908 else if(dcolor == COLOR_TEAM3 - 1)
910 else if(dcolor == COLOR_TEAM4 - 1)
913 CheckAllowedTeams(self);
915 if(dteam == 1 && c1 < 0) dteam = 4;
916 if(dteam == 4 && c4 < 0) dteam = 3;
917 if(dteam == 3 && c3 < 0) dteam = 2;
918 if(dteam == 2 && c2 < 0) dteam = 1;
920 // not changing teams
923 //bprint("same team change\n");
924 SetPlayerTeam(self, dteam, steam, TRUE);
930 if(cvar("g_campaign") || cvar("g_changeteam_banned"))
932 sprint(self, "Team changes not allowed\n");
933 return; // changing teams is not allowed
936 if(!cvar("g_campaign") && cvar("g_balance_teams_prevent_imbalance"))
938 // only allow changing to a smaller or equal size team
940 // find out what teams are available
941 //CheckAllowedTeams();
942 // count how many players on each team
943 GetTeamCounts(world);
946 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
951 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
956 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
961 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
968 sprint(self, "Cannot change to an invalid team\n");
974 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
976 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
978 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
980 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
983 if(scount) // started at a valid, nonempty team
985 // check if we're trying to change to a larger team that doens't have bots to swap with
986 if(dcount >= scount && dbotcount <= 0)
988 sprint(self, "Cannot change to a larger team\n");
989 return; // can't change to a larger team
995 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
997 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
999 // reduce frags during a team change
1000 TeamchangeFrags(self);
1003 SetPlayerTeam(self, dteam, steam, FALSE);
1005 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1007 // kill player when changing teams
1008 if(self.deadflag == DEAD_NO)
1009 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
1013 void ShufflePlayerOutOfTeam (float source_team)
1015 float smallestteam, smallestteam_count, steam;
1016 float lowest_bot_score, lowest_player_score;
1017 entity head, lowest_bot, lowest_player, selected;
1020 smallestteam_count = 999999999;
1022 if(c1 >= 0 && c1 < smallestteam_count)
1025 smallestteam_count = c1;
1027 if(c2 >= 0 && c2 < smallestteam_count)
1030 smallestteam_count = c2;
1032 if(c3 >= 0 && c3 < smallestteam_count)
1035 smallestteam_count = c3;
1037 if(c4 >= 0 && c4 < smallestteam_count)
1040 smallestteam_count = c4;
1045 bprint("warning: no smallest team\n");
1049 if(source_team == 1)
1050 steam = COLOR_TEAM1;
1051 else if(source_team == 2)
1052 steam = COLOR_TEAM2;
1053 else if(source_team == 3)
1054 steam = COLOR_TEAM3;
1055 else if(source_team == 4)
1056 steam = COLOR_TEAM4;
1059 lowest_bot_score = 999999999;
1060 lowest_player = world;
1061 lowest_player_score = 999999999;
1063 // find the lowest-scoring player & bot of that team
1064 FOR_EACH_PLAYER(head)
1066 if(head.team == steam)
1070 if(head.frags < lowest_bot_score)
1073 lowest_bot_score = head.frags;
1078 if(head.frags < lowest_player_score)
1080 lowest_player = head;
1081 lowest_player_score = head.frags;
1087 // prefers to move a bot...
1088 if(lowest_bot != world)
1089 selected = lowest_bot;
1090 // but it will move a player if it has to
1092 selected = lowest_player;
1093 // don't do anything if it couldn't find anyone
1096 bprint("warning: couldn't find a player to move from team\n");
1100 // smallest team gains a member
1101 if(smallestteam == 1)
1105 else if(smallestteam == 2)
1109 else if(smallestteam == 3)
1113 else if(smallestteam == 4)
1119 bprint("warning: destination team invalid\n");
1122 // source team loses a member
1123 if(source_team == 1)
1127 else if(source_team == 2)
1131 else if(source_team == 3)
1135 else if(source_team == 4)
1141 bprint("warning: source team invalid\n");
1145 // move the player to the new team
1146 TeamchangeFrags(selected);
1147 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1149 if(selected.deadflag == DEAD_NO)
1150 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1151 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1154 float lastRebalanceInfo;
1155 void CauseRebalance(float source_team, float howmany_toomany)
1160 if(IsTeamBalanceForced() == 1)
1162 bprint("Rebalancing Teams\n");
1163 ShufflePlayerOutOfTeam(source_team);
1167 if(howmany_toomany < cvar("g_balance_teams_complain"))
1169 if(time < lastRebalanceInfo + 90)
1171 lastRebalanceInfo = time;
1172 if(source_team == 1)
1173 steam = COLOR_TEAM1;
1174 else if(source_team == 2)
1175 steam = COLOR_TEAM2;
1176 else if(source_team == 3)
1177 steam = COLOR_TEAM3;
1178 else if(source_team == 4)
1179 steam = COLOR_TEAM4;
1180 ServerConsoleEcho(strcat("Team ", ftos(source_team), " too large, complaining."), TRUE);
1181 FOR_EACH_REALPLAYER(head)
1183 if(head.team == steam)
1185 sprint(head, "\{1}\{13}^3SERVER NOTICE:^7 One of you please change teams!\n");
1186 centerprint_atprio(head, CENTERPRIO_REBALANCE, "^3SERVER NOTICE:\n\n^7Someone of you please change teams!");
1192 // part of g_balance_teams_force
1193 // occasionally perform an audit of the teams to make
1194 // sure they're more or less balanced in player count.
1197 float numplayers, numteams, smallest, toomany;
1199 balance = IsTeamBalanceForced();
1203 if(audit_teams_time > time)
1206 audit_teams_time = time + 4 + random();
1208 // bprint("Auditing teams\n");
1210 CheckAllowedTeams(world);
1211 GetTeamCounts(world);
1214 numteams = numplayers = smallest = 0;
1217 numteams = numteams + 1;
1218 numplayers = numplayers + c1;
1223 numteams = numteams + 1;
1224 numplayers = numplayers + c2;
1230 numteams = numteams + 1;
1231 numplayers = numplayers + c3;
1237 numteams = numteams + 1;
1238 numplayers = numplayers + c4;
1244 return; // no players to move around
1246 return; // don't bother shuffling if for some reason there aren't any teams
1248 toomany = smallest + 1;
1250 if(c1 && c1 > toomany)
1251 CauseRebalance(1, c1 - toomany);
1252 if(c2 && c2 > toomany)
1253 CauseRebalance(2, c2 - toomany);
1254 if(c3 && c3 > toomany)
1255 CauseRebalance(3, c3 - toomany);
1256 if(c4 && c4 > toomany)
1257 CauseRebalance(4, c4 - toomany);
1259 // if teams are still unbalanced, balance them further in the next audit,
1260 // which will happen sooner (keep doing rapid audits until things are in order)
1261 audit_teams_time = time + 0.7 + random()*0.3;
1266 /*void(entity e, float first) UpdateTeamScore =
1271 WriteByte (MSG_ALL, SVC_UPDATENAME);
1272 WriteByte (MSG_ALL, clientno);
1273 WriteString (MSG_ALL, e.netname);
1275 WriteByte (MSG_ALL, SVC_UPDATECOLORS);
1276 WriteByte (MSG_ALL, clientno);
1277 WriteByte (MSG_ALL, e.b_shirt * 16 + who.b_pants);
1280 WriteByte (MSG_ALL, SVC_UPDATEFRAGS);
1281 WriteByte (MSG_ALL, clientno);
1282 WriteShort (MSG_ALL, e.frags + 10000);
1288 // code from here on is just to support maps that don't have team entities
1289 void tdm_spawnteam (string teamname, float teamcolor)
1293 e.classname = "tdm_team";
1294 e.netname = teamname;
1299 // spawn some default teams if the map is not set up for tdm
1300 void() tdm_spawnteams =
1304 numteams = cvar("g_tdm_teams");
1306 tdm_spawnteam("Red", COLOR_TEAM1-1);
1307 tdm_spawnteam("Blue", COLOR_TEAM2-1);
1309 tdm_spawnteam("Yellow", COLOR_TEAM3-1);
1311 tdm_spawnteam("Pink", COLOR_TEAM4-1);
1314 void() tdm_delayedinit =
1316 self.think = SUB_Remove;
1317 self.nextthink = time;
1318 // if no teams are found, spawn defaults
1319 if (find(world, classname, "tdm_team") == world)
1327 e.think = tdm_delayedinit;
1328 e.nextthink = time + 0.1;