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;
117 game = cvar("gamecfg"); // load game options
119 // game cvars get reset before map changes
120 // then map's cfg sets them as desired
122 // FIXME: also set a message or game mode name to print to players when the join
124 // set both here, gamemode can override it later
125 timelimit_override = cvar("timelimit_override");
126 fraglimit_override = cvar("fraglimit_override");
128 if(game == GAME_DOMINATION || cvar("g_domination"))
131 game = GAME_DOMINATION;
132 cvar_set("g_domination", "1");
136 fraglimit_override = cvar("g_domination_point_limit");
138 gamemode_name = "Domination";
141 else if(game == GAME_CTF || cvar("g_ctf"))
145 cvar_set("g_ctf", "1");
149 fraglimit_override = cvar("g_ctf_capture_limit");
151 gamemode_name = "Capture the Flag";
154 else if(game == GAME_RUNEMATCH || cvar("g_runematch"))
156 game = GAME_RUNEMATCH;
157 cvar_set("g_runematch", "1");
158 cvar_set("g_minstagib", "0");
160 if(cvar("deathmatch_force_teamplay"))
163 fraglimit_override = cvar("g_runematch_point_limit");
165 gamemode_name = "Rune Match";
171 else if(game == GAME_DEATHMATCH || game == GAME_TEAM_DEATHMATCH || cvar("g_tdm"))
173 if(!cvar("deathmatch"))
174 cvar_set("deathmatch", "1");
177 if(game == GAME_TEAM_DEATHMATCH || cvar("g_tdm") || cvar("deathmatch_force_teamplay"))
180 game = GAME_TEAM_DEATHMATCH;
181 gamemode_name = "Team Deathmatch";
184 cvar_set("g_tdm", "1");
189 game = GAME_DEATHMATCH;
190 gamemode_name = "Deathmatch";
194 fraglimit_override = cvar("fraglimit_override");
196 else if(game == GAME_ASSAULT || cvar("g_assault"))
200 gamemode_name = "Assault";
203 cvar_set("g_assault", "1");
205 else if(game == GAME_LMS || cvar("g_lms"))
209 cvar_set("g_lms", "1");
210 fraglimit_override = cvar("g_lms_lives_override");
211 if(fraglimit_override == 0)
212 fraglimit_override = -1;
213 gamemode_name = "Last Man Standing";
215 lms_lowest_lives = 999;
217 else if(game == GAME_ARENA || cvar("g_arena"))
221 cvar_set("g_arena", "1");
222 fraglimit_override = cvar("g_arena_point_limit");
223 maxspawned = cvar("g_arena_maxspawned");
226 arena_roundbased = cvar("g_arena_roundbased");
227 gamemode_name = "Arena";
230 else if(game == GAME_KEYHUNT || cvar("g_keyhunt"))
234 cvar_set("g_keyhunt", "1");
235 fraglimit_override = cvar("g_keyhunt_point_limit");
237 gamemode_name = "Key Hunt";
240 else if(game == GAME_ONSLAUGHT || cvar("g_onslaught"))
243 game = GAME_ONSLAUGHT;
244 cvar_set("g_onslaught", "1");
248 gamemode_name = "Onslaught";
253 // we can only assume...
255 cvar_set("g_dm", "1");
256 gamemode_name = "Deathmatch";
259 /* else if(game == GAME_TEAM_DEATHMATCH)
261 if(!cvar("deathmatch"))
262 cvar_set("deathmatch", "1");
264 //if(!cvar("teamplay"))
265 // cvar_set("teamplay", "3");
268 fraglimit_override = cvar("fraglimit_override");
271 // those mutators rule each other out
272 if(cvar("g_minstagib"))
274 cvar_set("g_instagib", "0");
275 cvar_set("g_rocketarena", "0");
277 if(cvar("g_instagib"))
279 cvar_set("g_minstagib", "0");
280 cvar_set("g_rocketarena", "0");
282 if(cvar("g_rocketarena"))
284 cvar_set("g_instagib", "0");
285 cvar_set("g_minstagib", "0");
288 registercvar("_motd", "");
289 registercvar("_mutatormsg", "");
290 cvar_set("_motd", wordwrap(cvar_string("sv_motd"), 50));
291 cvar_set("_mutatormsg", wordwrap(cvar_string("g_mutatormsg"), 50));
293 g_domination = cvar("g_domination");
294 g_ctf = cvar("g_ctf");
295 g_lms = cvar("g_lms");
296 g_tdm = cvar("g_tdm");
297 g_runematch = cvar("g_runematch");
298 g_keyhunt = cvar("g_keyhunt");
299 g_onslaught = cvar("g_onslaught");
300 g_assault = cvar("g_assault");
301 g_arena = cvar("g_arena");
303 // enforce the server's universal frag/time limits
304 if(!cvar("g_campaign"))
306 if(fraglimit_override >= 0)
307 cvar_set("fraglimit", ftos(fraglimit_override));
308 if(timelimit_override >= 0)
309 cvar_set("timelimit", ftos(timelimit_override));
312 if (game == GAME_DOMINATION)//cvar("g_domination"))
314 else if (game == GAME_CTF)//cvar("g_ctf"))
316 else if (game == GAME_RUNEMATCH)//cvar("g_runematch"))
318 else if (game == GAME_TEAM_DEATHMATCH)//cvar("g_runematch"))
320 else if (game == GAME_KEYHUNT)//cvar("g_keyhunt"))
324 string GetClientVersionMessage(float v) {
325 local string versionmsg;
327 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";
328 // either that or someone wants to be funny
329 } else if (v != cvar("gameversion")) {
330 if(v < cvar("gameversion")) {
331 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
333 versionmsg = "^3This server is using an outdated Nexuiz version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
336 versionmsg = "^2client version and server version are compatible.^8";
338 return strzone(versionmsg);
343 void PrintWelcomeMessage(entity pl)
345 string s, mutator, modifications, padding;
347 /*if(self.welcomemessage_time > time)
349 self.welcomemessage_time = time + 0.8; */
351 if(self.cvar_scr_centertime == 0) return;
352 if(self.welcomemessage_time > time) return;
353 self.welcomemessage_time = time + self.cvar_scr_centertime * 0.6;
355 if(cvar("g_campaign"))
357 centerprint(pl, campaign_message);
363 if(self.classname == "observer")
365 if(g_lms && self.frags <= 0 && self.frags > -666)
366 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"));
367 else if(g_lms && self.frags == -666)
368 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"));
370 else if(self.classname == "spectator")
372 if ((g_lms && self.frags < 1) || g_arena)
373 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"));
375 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"));
380 mutator = "^2Minstagib ^1";
382 mutator = "^2Instagib ^1";
383 else if(g_rocketarena)
384 mutator = "^2Rocketarena ^1";
386 mutator = "^2No Items Nexuiz ^1";
389 // to protect against unheedingly made changes
391 modifications = strcat(modifications, ", ");
393 modifications = "midair";
397 modifications = strcat(modifications, ", ");
399 modifications = strcat(modifications, "vampire");
401 if(g_laserguided_missile) {
403 modifications = strcat(modifications, ", ");
405 modifications = strcat(modifications, "laser-guided-missiles");
408 local string versionmessage;
409 versionmessage = GetClientVersionMessage(self.version);
411 s = strcat(s, NEWLINES, "This is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
412 s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
414 if(modifications != "")
415 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
417 if(time < restart_countdown)
418 s = strcat(s, "\n^1Game starts in ", ftos(ceil(restart_countdown-time)), " seconds^7");
422 if (g_grappling_hook)
423 s = strcat(s, "\n\n^8grappling hook is enabled, press 'e' to use it\n");
425 if (cvar_string("_mutatormsg") != "") {
426 s = strcat(s, "\n\n^8special gameplay tips: ^7", cvar_string("_mutatormsg"));
429 if (cvar_string("_motd") != "") {
430 s = strcat(s, "\n\n^8MOTD: ^7", cvar_string("_motd"));
434 if(cvar("fraglimit"))
437 if(cvar("timelimit"))
440 s = strcat(s, "\n^8frag limit: ^7", cvar_string("fraglimit"), padding);
442 if(cvar("timelimit"))
443 s = strcat(s, "\n^8time limit: ^7", cvar_string("timelimit"), " minutes");
454 void SetPlayerColors(entity pl, float _color)
458 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
460 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
461 pl.clientcolors = 16*cl + cl;*/
464 pants = _color & 0x0F;
465 shirt = _color & 0xF0;
469 setcolor(pl, 16*pants + pants);
471 setcolor(pl, shirt + pants);
475 void SetPlayerTeam(entity pl, float t, float s, float noprint)
480 _color = COLOR_TEAM4 - 1;
482 _color = COLOR_TEAM3 - 1;
484 _color = COLOR_TEAM2 - 1;
486 _color = COLOR_TEAM1 - 1;
488 SetPlayerColors(pl,_color);
490 if(!noprint && t != s)
492 //bprint(pl.netname, " has changed to ", TeamNoName(t), "\n");
493 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
505 // set c1...c4 to show what teams are allowed
506 void CheckAllowedTeams (entity for_whom)
515 c1 = c2 = c3 = c4 = -1;
516 cb1 = cb2 = cb3 = cb4 = 0;
518 // onslaught is special
521 head = findchain(classname, "onslaught_generator");
524 if (head.team == COLOR_TEAM1) c1 = 0;
525 if (head.team == COLOR_TEAM2) c2 = 0;
526 if (head.team == COLOR_TEAM3) c3 = 0;
527 if (head.team == COLOR_TEAM4) c4 = 0;
534 teament_name = "dom_team";
536 teament_name = "ctf_team";
538 teament_name = "tdm_team";
541 c1 = c2 = 0; // Assault always has 2 teams
546 // cover anything else by treating it like tdm with no teams spawned
550 dm = cvar("g_tdm_teams");
552 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
556 c1 = c2 = c3 = c4 = 0;
569 // first find out what teams are allowed
570 head = find(world, classname, teament_name);
573 if(!(g_domination && head.netname == ""))
575 if(head.team == COLOR_TEAM1)
579 if(head.team == COLOR_TEAM2)
583 if(head.team == COLOR_TEAM3)
587 if(head.team == COLOR_TEAM4)
592 head = find(head, classname, teament_name);
595 if(cvar("bot_vs_human") > 0)
598 if(clienttype(for_whom) == CLIENTTYPE_BOT)
603 else if(cvar("bot_vs_human") < 0)
606 if(clienttype(for_whom) == CLIENTTYPE_BOT)
613 float PlayerValue(entity p)
615 if(IsTeamBalanceForced() == 1)
620 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
621 // teams that are allowed will now have their player counts stored in c1...c4
622 void GetTeamCounts(entity ignore)
626 // now count how many players are on each team already
628 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
629 // also remember the lowest-scoring player
631 FOR_EACH_PLAYER(head)
633 if(head != ignore)// && head.netname != "")
635 value = PlayerValue(head);
636 if(clienttype(head) == CLIENTTYPE_BOT)
640 if(head.team == COLOR_TEAM1)
648 if(head.team == COLOR_TEAM2)
656 if(head.team == COLOR_TEAM3)
664 if(head.team == COLOR_TEAM4)
676 // returns # of smallest team (1, 2, 3, 4)
677 // NOTE: Assumes CheckAllowedTeams has already been called!
678 float FindSmallestTeam(entity pl, float ignore_pl)
680 float totalteams, smallestteam, smallestteam_count, smallestteam_score, balance_type;
683 // find out what teams are available
684 //CheckAllowedTeams();
686 // make sure there are at least 2 teams to join
688 totalteams = totalteams + 1;
690 totalteams = totalteams + 1;
692 totalteams = totalteams + 1;
694 totalteams = totalteams + 1;
696 if(cvar("bot_vs_human"))
702 error("Too few teams available for domination\n");
704 error("Too few teams available for ctf\n");
706 error("Too few teams available for key hunt\n");
708 error("Too few teams available for team deathmatch\n");
712 // count how many players are in each team
716 GetTeamCounts(world);
718 // c1...c4 now have counts of each team
719 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
722 smallestteam_count = 999999999;
723 smallestteam_score = 999999999;
725 // 2 gives priority to what team you're already on, 1 goes in order
726 // 2 doesn't seem to work though...
730 //if(pl.classname != "player")
731 if(clienttype(pl) != CLIENTTYPE_BOT)
733 c1 -= cb1 * 255.0/256;
734 c2 -= cb2 * 255.0/256;
735 c3 -= cb3 * 255.0/256;
736 c4 -= cb4 * 255.0/256;
739 if(balance_type == 1)
741 if(c1 >= 0 && (c1 < smallestteam_count || (c1 <= smallestteam_count && team1_score < smallestteam_score)))
744 smallestteam_count = c1;
745 smallestteam_score = team1_score;
747 if(c2 >= 0 && (c2 < smallestteam_count || (c2 <= smallestteam_count && team2_score < smallestteam_score)))
750 smallestteam_count = c2;
751 smallestteam_score = team2_score;
753 if(c3 >= 0 && (c3 < smallestteam_count || (c3 <= smallestteam_count && team3_score < smallestteam_score)))
756 smallestteam_count = c3;
757 smallestteam_score = team3_score;
759 if(c4 >= 0 && (c4 < smallestteam_count || (c4 <= smallestteam_count && team4_score < smallestteam_score)))
762 smallestteam_count = c4;
763 smallestteam_score = team4_score;
768 if(c1 >= 0 && (c1 < smallestteam_count ||
769 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
772 smallestteam_count = c1;
774 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
775 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
778 smallestteam_count = c2;
780 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
781 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
784 smallestteam_count = c3;
786 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
787 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
790 smallestteam_count = c4;
797 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
799 float smallest, selectedteam;
801 // don't join a team if we're not playing a team game
802 if(!cvar("teamplay") && !g_domination && !g_ctf && !g_keyhunt)
805 // find out what teams are available
806 CheckAllowedTeams(pl);
810 if(cvar("g_domination_default_teams") < 3)
812 if(cvar("g_domination_default_teams") < 4)
816 // if we don't care what team he ends up on, put him on whatever team he entered as.
817 // if he's not on a valid team, then let other code put him on the smallest team
820 if( c1 >= 0 && pl.team == COLOR_TEAM1)
821 selectedteam = pl.team;
822 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
823 selectedteam = pl.team;
824 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
825 selectedteam = pl.team;
826 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
827 selectedteam = pl.team;
832 if(!only_return_best)
834 SetPlayerColors(pl, selectedteam - 1);
839 // otherwise end up on the smallest team (handled below)
842 smallest = FindSmallestTeam(pl, TRUE);
845 if(!only_return_best)
847 TeamchangeFrags(self);
850 SetPlayerColors(pl, COLOR_TEAM1 - 1);
852 else if(smallest == 2)
854 SetPlayerColors(pl, COLOR_TEAM2 - 1);
856 else if(smallest == 3)
858 SetPlayerColors(pl, COLOR_TEAM3 - 1);
860 else if(smallest == 4)
862 SetPlayerColors(pl, COLOR_TEAM4 - 1);
866 error("smallest team: invalid team\n");
869 if(pl.deadflag == DEAD_NO)
870 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
877 void SV_ChangeTeam(float _color)
879 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
881 // in normal deathmatch we can just apply the color and we're done
882 if(!cvar("teamplay")) {
883 SetPlayerColors(self, _color);
887 scolor = self.clientcolors & 0x0F;
888 dcolor = _color & 0x0F;
890 if(scolor == COLOR_TEAM1 - 1)
892 else if(scolor == COLOR_TEAM2 - 1)
894 else if(scolor == COLOR_TEAM3 - 1)
896 else if(scolor == COLOR_TEAM4 - 1)
898 if(dcolor == COLOR_TEAM1 - 1)
900 else if(dcolor == COLOR_TEAM2 - 1)
902 else if(dcolor == COLOR_TEAM3 - 1)
904 else if(dcolor == COLOR_TEAM4 - 1)
907 CheckAllowedTeams(self);
909 if(dteam == 1 && c1 < 0) dteam = 4;
910 if(dteam == 4 && c4 < 0) dteam = 3;
911 if(dteam == 3 && c3 < 0) dteam = 2;
912 if(dteam == 2 && c2 < 0) dteam = 1;
914 // not changing teams
917 //bprint("same team change\n");
918 SetPlayerTeam(self, dteam, steam, TRUE);
924 if(cvar("g_campaign") || cvar("g_changeteam_banned"))
926 sprint(self, "Team changes not allowed\n");
927 return; // changing teams is not allowed
930 if(!cvar("g_campaign") && cvar("g_balance_teams_prevent_imbalance"))
932 // only allow changing to a smaller or equal size team
934 // find out what teams are available
935 //CheckAllowedTeams();
936 // count how many players on each team
937 GetTeamCounts(world);
940 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
945 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
950 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
955 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
962 sprint(self, "Cannot change to an invalid team\n");
968 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
970 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
972 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
974 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
977 if(scount) // started at a valid, nonempty team
979 // check if we're trying to change to a larger team that doens't have bots to swap with
980 if(dcount >= scount && dbotcount <= 0)
982 sprint(self, "Cannot change to a larger team\n");
983 return; // can't change to a larger team
989 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
991 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
993 // reduce frags during a team change
994 TeamchangeFrags(self);
997 SetPlayerTeam(self, dteam, steam, FALSE);
999 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1001 // kill player when changing teams
1002 if(self.deadflag == DEAD_NO)
1003 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
1007 void ShufflePlayerOutOfTeam (float source_team)
1009 float smallestteam, smallestteam_count, steam;
1010 float lowest_bot_score, lowest_player_score;
1011 entity head, lowest_bot, lowest_player, selected;
1014 smallestteam_count = 999999999;
1016 if(c1 >= 0 && c1 < smallestteam_count)
1019 smallestteam_count = c1;
1021 if(c2 >= 0 && c2 < smallestteam_count)
1024 smallestteam_count = c2;
1026 if(c3 >= 0 && c3 < smallestteam_count)
1029 smallestteam_count = c3;
1031 if(c4 >= 0 && c4 < smallestteam_count)
1034 smallestteam_count = c4;
1039 bprint("warning: no smallest team\n");
1043 if(source_team == 1)
1044 steam = COLOR_TEAM1;
1045 else if(source_team == 2)
1046 steam = COLOR_TEAM2;
1047 else if(source_team == 3)
1048 steam = COLOR_TEAM3;
1049 else if(source_team == 4)
1050 steam = COLOR_TEAM4;
1053 lowest_bot_score = 999999999;
1054 lowest_player = world;
1055 lowest_player_score = 999999999;
1057 // find the lowest-scoring player & bot of that team
1058 FOR_EACH_PLAYER(head)
1060 if(head.team == steam)
1064 if(head.frags < lowest_bot_score)
1067 lowest_bot_score = head.frags;
1072 if(head.frags < lowest_player_score)
1074 lowest_player = head;
1075 lowest_player_score = head.frags;
1081 // prefers to move a bot...
1082 if(lowest_bot != world)
1083 selected = lowest_bot;
1084 // but it will move a player if it has to
1086 selected = lowest_player;
1087 // don't do anything if it couldn't find anyone
1090 bprint("warning: couldn't find a player to move from team\n");
1094 // smallest team gains a member
1095 if(smallestteam == 1)
1099 else if(smallestteam == 2)
1103 else if(smallestteam == 3)
1107 else if(smallestteam == 4)
1113 bprint("warning: destination team invalid\n");
1116 // source team loses a member
1117 if(source_team == 1)
1121 else if(source_team == 2)
1125 else if(source_team == 3)
1129 else if(source_team == 4)
1135 bprint("warning: source team invalid\n");
1139 // move the player to the new team
1140 TeamchangeFrags(selected);
1141 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1143 if(selected.deadflag == DEAD_NO)
1144 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1145 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1148 float lastRebalanceInfo;
1149 void CauseRebalance(float source_team, float howmany_toomany)
1154 if(IsTeamBalanceForced() == 1)
1156 bprint("Rebalancing Teams\n");
1157 ShufflePlayerOutOfTeam(source_team);
1161 if(howmany_toomany < cvar("g_balance_teams_complain"))
1163 if(time < lastRebalanceInfo + 90)
1165 lastRebalanceInfo = time;
1166 if(source_team == 1)
1167 steam = COLOR_TEAM1;
1168 else if(source_team == 2)
1169 steam = COLOR_TEAM2;
1170 else if(source_team == 3)
1171 steam = COLOR_TEAM3;
1172 else if(source_team == 4)
1173 steam = COLOR_TEAM4;
1174 ServerConsoleEcho(strcat("Team ", ftos(source_team), " too large, complaining."), TRUE);
1175 FOR_EACH_REALPLAYER(head)
1177 if(head.team == steam)
1179 sprint(head, "\{1}\{13}^3SERVER NOTICE:^7 One of you please change teams!\n");
1180 centerprint_atprio(head, CENTERPRIO_REBALANCE, "^3SERVER NOTICE:\n\n^7Someone of you please change teams!");
1186 // part of g_balance_teams_force
1187 // occasionally perform an audit of the teams to make
1188 // sure they're more or less balanced in player count.
1191 float numplayers, numteams, smallest, toomany;
1193 balance = IsTeamBalanceForced();
1197 if(audit_teams_time > time)
1200 audit_teams_time = time + 4 + random();
1202 // bprint("Auditing teams\n");
1204 CheckAllowedTeams(world);
1205 GetTeamCounts(world);
1208 numteams = numplayers = smallest = 0;
1211 numteams = numteams + 1;
1212 numplayers = numplayers + c1;
1217 numteams = numteams + 1;
1218 numplayers = numplayers + c2;
1224 numteams = numteams + 1;
1225 numplayers = numplayers + c3;
1231 numteams = numteams + 1;
1232 numplayers = numplayers + c4;
1238 return; // no players to move around
1240 return; // don't bother shuffling if for some reason there aren't any teams
1242 toomany = smallest + 1;
1244 if(c1 && c1 > toomany)
1245 CauseRebalance(1, c1 - toomany);
1246 if(c2 && c2 > toomany)
1247 CauseRebalance(2, c2 - toomany);
1248 if(c3 && c3 > toomany)
1249 CauseRebalance(3, c3 - toomany);
1250 if(c4 && c4 > toomany)
1251 CauseRebalance(4, c4 - toomany);
1253 // if teams are still unbalanced, balance them further in the next audit,
1254 // which will happen sooner (keep doing rapid audits until things are in order)
1255 audit_teams_time = time + 0.7 + random()*0.3;
1260 /*void(entity e, float first) UpdateTeamScore =
1265 WriteByte (MSG_ALL, SVC_UPDATENAME);
1266 WriteByte (MSG_ALL, clientno);
1267 WriteString (MSG_ALL, e.netname);
1269 WriteByte (MSG_ALL, SVC_UPDATECOLORS);
1270 WriteByte (MSG_ALL, clientno);
1271 WriteByte (MSG_ALL, e.b_shirt * 16 + who.b_pants);
1274 WriteByte (MSG_ALL, SVC_UPDATEFRAGS);
1275 WriteByte (MSG_ALL, clientno);
1276 WriteShort (MSG_ALL, e.frags + 10000);
1282 // code from here on is just to support maps that don't have team entities
1283 void tdm_spawnteam (string teamname, float teamcolor)
1287 e.classname = "tdm_team";
1288 e.netname = teamname;
1293 // spawn some default teams if the map is not set up for tdm
1294 void() tdm_spawnteams =
1298 numteams = cvar("g_tdm_teams");
1300 tdm_spawnteam("Red", COLOR_TEAM1-1);
1301 tdm_spawnteam("Blue", COLOR_TEAM2-1);
1303 tdm_spawnteam("Yellow", COLOR_TEAM3-1);
1305 tdm_spawnteam("Pink", COLOR_TEAM4-1);
1308 void() tdm_delayedinit =
1310 self.think = SUB_Remove;
1311 self.nextthink = time;
1312 // if no teams are found, spawn defaults
1313 if (find(world, classname, "tdm_team") == world)
1321 e.think = tdm_delayedinit;
1322 e.nextthink = time + 0.1;