1 float GAME_DEATHMATCH = 1;
2 float GAME_TEAM_DEATHMATCH = 2;
3 float GAME_DOMINATION = 3;
5 float GAME_RUNEMATCH = 5;
8 float GAME_KEYHUNT = 8;
9 float GAME_ASSAULT = 9;
11 // client counts for each team
13 // # of bots on those teams
14 float cb1, cb2, cb3, cb4;
16 float g_domination, g_ctf, g_tdm, g_keyhunt;
18 float audit_teams_time;
20 float() IsTeamBalanceForced = {
21 if(intermission_running)
22 return 0; // no rebalancing whatsoever please
25 if(cvar("g_campaign"))
27 if(!cvar("g_balance_teams_force"))
32 void TeamchangeFrags(entity e)
34 if(e.classname == "player")
36 // reduce frags during a team change
37 DistributeFragsAmongTeam(e, (e.clientcolors & 0x0F) + 1, 1 - cvar("g_changeteam_fragtransfer") / 100);
41 string TeamName(float t)
43 // fixme: Search for team entities and get their .netname's!
52 return "Neutral Team";
54 string ColoredTeamName(float t)
56 // fixme: Search for team entities and get their .netname's!
58 return "^1Red Team^7";
60 return "^4Blue Team^7";
62 return "^3Yellow Team^7";
64 return "^6Pink Team^7";
65 return "Neutral Team";
67 string TeamNoName(float t)
69 // fixme: Search for team entities and get their .netname's!
78 return "Neutral Team";
83 void runematch_init();
86 void(entity pl) LogTeamchange =
89 if(!cvar("sv_eventlog"))
93 str = strcat(":team:", ftos(pl.playerid), ":");
94 str = strcat(str, ftos(pl.team));
95 GameLogEcho(str, FALSE);
100 cvar_set("g_tdm", "0");
101 cvar_set("g_domination", "0");
102 cvar_set("g_ctf", "0");
103 cvar_set("g_runematch", "0");
104 cvar_set("g_lms", "0");
105 cvar_set("g_arena", "0");
106 cvar_set("g_keyhunt", "0");
107 cvar_set("g_assault", "0");
108 cvar_set("teamplay", "0");
111 void ActivateTeamplay()
113 float teamplay_default;
114 teamplay_default = cvar("teamplay_default");
117 cvar_set("teamplay", ftos(teamplay_default));
119 cvar_set("teamplay", "3");
122 void InitGameplayMode()
124 float fraglimit_override, timelimit_override;
128 game = cvar ("gamecfg"); // load game options
130 // game cvars get reset before map changes
131 // then map's cfg sets them as desired
133 // FIXME: also set a message or game mode name to print to players when the join
135 // set both here, gamemode can override it later
136 timelimit_override = cvar("timelimit_override");
137 fraglimit_override = cvar("fraglimit_override");
139 if(game == GAME_DOMINATION || cvar("g_domination"))
142 game = GAME_DOMINATION;
143 cvar_set("g_domination", "1");
147 fraglimit_override = cvar("g_domination_point_limit");
149 gamemode_name = "Domination";
152 else if(game == GAME_CTF || cvar("g_ctf"))
156 cvar_set("g_ctf", "1");
160 fraglimit_override = cvar("g_ctf_capture_limit");
162 gamemode_name = "Capture the Flag";
165 else if((game == GAME_RUNEMATCH || cvar("g_runematch")) && !cvar("g_minstagib"))
167 game = GAME_RUNEMATCH;
168 cvar_set("g_runematch", "1");
170 if(cvar("deathmatch_force_teamplay"))
173 fraglimit_override = cvar("g_runematch_point_limit");
175 gamemode_name = "Rune Match";
181 else if(game == GAME_DEATHMATCH || game == GAME_TEAM_DEATHMATCH || cvar("g_tdm"))
183 if(!cvar("deathmatch"))
184 cvar_set("deathmatch", "1");
187 if(game == GAME_TEAM_DEATHMATCH || cvar("g_tdm") || cvar("deathmatch_force_teamplay"))
190 game = GAME_TEAM_DEATHMATCH;
191 gamemode_name = "Team Deathmatch";
194 cvar_set("g_tdm", "1");
199 game = GAME_DEATHMATCH;
200 gamemode_name = "Deathmatch";
204 fraglimit_override = cvar("fraglimit_override");
206 else if(game == GAME_ASSAULT || cvar("g_assault"))
210 gamemode_name = "Assault";
213 cvar_set("g_assault", "1");
215 else if(game == GAME_LMS || cvar("g_lms"))
219 cvar_set("g_lms", "1");
220 fraglimit_override = cvar("g_lms_lives_override");
221 if(fraglimit_override == 0)
222 fraglimit_override = -1;
223 gamemode_name = "Last Man Standing";
225 lms_lowest_lives = 999;
227 else if(game == GAME_ARENA || cvar("g_arena"))
231 cvar_set("g_arena", "1");
232 fraglimit_override = cvar("g_arena_point_limit");
233 maxspawned = cvar("g_arena_maxspawned");
236 arena_roundbased = cvar("g_arena_roundbased");
237 gamemode_name = "Arena";
240 else if(game == GAME_KEYHUNT || cvar("g_keyhunt"))
244 cvar_set("g_keyhunt", "1");
245 fraglimit_override = cvar("g_keyhunt_point_limit");
247 gamemode_name = "Key Hunt";
252 // we can only assume...
254 gamemode_name = "Deathmatch";
257 /* else if(game == GAME_TEAM_DEATHMATCH)
259 if(!cvar("deathmatch"))
260 cvar_set("deathmatch", "1");
262 //if(!cvar("teamplay"))
263 // cvar_set("teamplay", "3");
266 fraglimit_override = cvar("fraglimit_override");
269 // enforce the server's universal frag/time limits
270 if(!cvar("g_campaign"))
272 if(fraglimit_override >= 0)
273 cvar_set("fraglimit", ftos(fraglimit_override));
274 if(timelimit_override >= 0)
275 cvar_set("timelimit", ftos(timelimit_override));
278 if (game == GAME_DOMINATION)//cvar("g_domination"))
280 else if (game == GAME_CTF)//cvar("g_ctf"))
282 else if (game == GAME_RUNEMATCH)//cvar("g_runematch"))
284 else if (game == GAME_TEAM_DEATHMATCH)//cvar("g_runematch"))
286 else if (game == GAME_KEYHUNT)//cvar("g_keyhunt"))
289 // those mutators rule each other out
290 if(cvar("g_minstagib"))
292 cvar_set("g_instagib", "0");
293 cvar_set("g_rocketarena", "0");
295 if(cvar("g_instagib"))
297 cvar_set("g_minstagib", "0");
298 cvar_set("g_rocketarena", "0");
300 if(cvar("g_rocketarena"))
302 cvar_set("g_instagib", "0");
303 cvar_set("g_minstagib", "0");
306 registercvar("_motd", "");
307 registercvar("_mutatormsg", "");
308 cvar_set("_motd", wordwrap(cvar_string("sv_motd"), 50));
309 cvar_set("_mutatormsg", wordwrap(cvar_string("g_mutatormsg"), 50));
311 g_domination = cvar("g_domination");
312 g_ctf = cvar("g_ctf");
313 g_tdm = cvar("g_tdm");
314 g_keyhunt = cvar("g_keyhunt");
317 string GetClientVersionMessage(float v) {
318 local string versionmsg;
320 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";
321 // either that or someone wants to be funny
322 } else if (v != cvar("gameversion")) {
323 if(v < cvar("gameversion")) {
324 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
326 versionmsg = "^3This server is using an outdated Nexuiz version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
329 versionmsg = "^2client version and server version are compatible.^8";
331 return strzone(versionmsg);
336 void PrintWelcomeMessage(entity pl)
338 string s, mutator, modifications, padding;
340 /*if(self.welcomemessage_time > time)
342 self.welcomemessage_time = time + 0.8; */
344 if(self.cvar_scr_centertime == 0) return;
345 if(self.welcomemessage_time > time) return;
346 self.welcomemessage_time = time + self.cvar_scr_centertime * 0.6;
348 if(cvar("g_campaign"))
350 centerprint(pl, campaign_message);
354 if(self.classname == "observer")
356 if(cvar("g_lms") && self.frags <= 0 && self.frags > -666)
357 return centerprint(self, strcat(NEWLINES, "^1You have no more lives left\nwait for next round\n\n\n^7press attack to spectate other players"));
358 else if(cvar("g_lms") && self.frags == -666)
359 return centerprint(self, strcat(NEWLINES, "^1Match has already begun\nwait for next round\n\n\n^7press attack to spectate other players"));
361 else if(self.classname == "spectator")
363 if ((cvar("g_lms") && self.frags < 1) || cvar("g_arena"))
364 return centerprint(self, strcat(NEWLINES, "spectating ", self.enemy.netname, "\n\n\n^7press attack for next player\npress attack2 for free fly mode"));
366 return centerprint(self, 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"));
370 if(cvar("g_minstagib"))
371 mutator = "^2Minstagib ^1";
372 else if(cvar("g_instagib"))
373 mutator = "^2Instagib ^1";
374 else if(cvar("g_rocketarena"))
375 mutator = "^2Rocketarena ^1";
376 else if(cvar("g_nixnex"))
377 mutator = "^2No Items Nexuiz ^1";
379 if(cvar("g_midair")) {
380 // to protect against unheedingly made changes
382 modifications = strcat(modifications, ", ");
384 modifications = "midair";
386 if(cvar("g_vampire")) {
388 modifications = strcat(modifications, ", ");
390 modifications = strcat(modifications, "vampire");
392 if(cvar("g_laserguided_missile")) {
394 modifications = strcat(modifications, ", ");
396 modifications = strcat(modifications, "laser-guided-missiles");
399 local string versionmessage;
400 versionmessage = GetClientVersionMessage(self.version);
402 s = strcat(s, NEWLINES, "This is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
403 s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
405 if(modifications != "")
406 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
408 if((self.classname == "observer" || self.classname == "spectator") && self.version == cvar("gameversion")) {
410 s = strcat(s,"^7\n\n\npress jump to play\npress attack to spectate other players\n\n");
411 else if(player_count < 2 && arena_roundbased)
413 s = strcat(s, "\n\n\n^1waiting for second player to start match^7\n\n");
417 s = strcat(s, "\n\n\n");
419 s = strcat(s, "^7current champion is: ", champion.netname, "\n\n");
420 s = strcat(s,"^7press attack to spectate other players\n\n");
427 if (cvar("g_grappling_hook"))
428 s = strcat(s, "\n\n^8grappling hook is enabled, press 'e' to use it\n");
430 if (cvar_string("_mutatormsg") != "") {
431 s = strcat(s, "\n\n^8special gameplay tips: ^7", cvar_string("_mutatormsg"));
434 if (cvar_string("_motd") != "") {
435 s = strcat(s, "\n\n^8MOTD: ^7", cvar_string("_motd"));
439 if(cvar("fraglimit"))
442 if(cvar("timelimit"))
445 s = strcat(s, "\n^8frag limit: ^7", cvar_string("fraglimit"), padding);
447 if(cvar("timelimit"))
448 s = strcat(s, "\n^8time limit: ^7", cvar_string("timelimit"), " minutes");
459 void SetPlayerColors(entity pl, float _color)
463 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
465 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
466 pl.clientcolors = 16*cl + cl;*/
469 pants = _color & 0x0F;
470 shirt = _color & 0xF0;
474 setcolor(pl, 16*pants + pants);
476 setcolor(pl, shirt + pants);
480 void SetPlayerTeam(entity pl, float t, float s, float noprint)
485 _color = COLOR_TEAM4 - 1;
487 _color = COLOR_TEAM3 - 1;
489 _color = COLOR_TEAM2 - 1;
491 _color = COLOR_TEAM1 - 1;
493 SetPlayerColors(pl,_color);
495 if(!noprint && t != s)
497 //bprint(pl.netname, " has changed to ", TeamNoName(t), "\n");
498 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
510 // set c1...c4 to show what teams are allowed
511 void CheckAllowedTeams ()
520 c1 = c2 = c3 = c4 = -1;
521 cb1 = cb2 = cb3 = cb4 = 0;
524 teament_name = "dom_team";
526 teament_name = "ctf_team";
528 teament_name = "tdm_team";
531 // cover anything else by treating it like tdm with no teams spawned
535 dm = cvar("g_tdm_teams");
537 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
541 c1 = c2 = c3 = c4 = 0;
554 // first find out what teams are allowed
555 head = find(world, classname, teament_name);
558 if(!(g_domination && head.netname == ""))
560 if(head.team == COLOR_TEAM1)
564 if(head.team == COLOR_TEAM2)
568 if(head.team == COLOR_TEAM3)
572 if(head.team == COLOR_TEAM4)
577 head = find(head, classname, teament_name);
581 float PlayerValue(entity p)
583 if(IsTeamBalanceForced() == 1)
588 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
589 // teams that are allowed will now have their player counts stored in c1...c4
590 void GetTeamCounts(entity ignore)
594 // now count how many players are on each team already
596 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
597 // also remember the lowest-scoring player
599 FOR_EACH_PLAYER(head)
601 if(head != ignore)// && head.netname != "")
603 value = PlayerValue(head);
604 if(clienttype(head) == CLIENTTYPE_BOT)
608 if(head.team == COLOR_TEAM1)
616 if(head.team == COLOR_TEAM2)
624 if(head.team == COLOR_TEAM3)
632 if(head.team == COLOR_TEAM4)
644 // returns # of smallest team (1, 2, 3, 4)
645 // NOTE: Assumes CheckAllowedTeams has already been called!
646 float FindSmallestTeam(entity pl, float ignore_pl)
648 float totalteams, smallestteam, smallestteam_count, smallestteam_score, balance_type;
651 // find out what teams are available
652 //CheckAllowedTeams();
654 // make sure there are at least 2 teams to join
656 totalteams = totalteams + 1;
658 totalteams = totalteams + 1;
660 totalteams = totalteams + 1;
662 totalteams = totalteams + 1;
667 error("Too few teams available for domination\n");
669 error("Too few teams available for ctf\n");
671 error("Too few teams available for key hunt\n");
673 error("Too few teams available for team deathmatch\n");
677 // count how many players are in each team
681 GetTeamCounts(world);
683 // c1...c4 now have counts of each team
684 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
687 smallestteam_count = 999999999;
688 smallestteam_score = 999999999;
690 // 2 gives priority to what team you're already on, 1 goes in order
691 // 2 doesn't seem to work though...
695 //if(pl.classname != "player")
696 if(clienttype(pl) != CLIENTTYPE_BOT)
698 c1 -= cb1 * 255.0/256;
699 c2 -= cb2 * 255.0/256;
700 c3 -= cb3 * 255.0/256;
701 c4 -= cb4 * 255.0/256;
704 if(balance_type == 1)
706 if(c1 >= 0 && (c1 < smallestteam_count || (c1 <= smallestteam_count && team1_score < smallestteam_score)))
709 smallestteam_count = c1;
710 smallestteam_score = team1_score;
712 if(c2 >= 0 && (c2 < smallestteam_count || (c2 <= smallestteam_count && team2_score < smallestteam_score)))
715 smallestteam_count = c2;
716 smallestteam_score = team2_score;
718 if(c3 >= 0 && (c3 < smallestteam_count || (c3 <= smallestteam_count && team3_score < smallestteam_score)))
721 smallestteam_count = c3;
722 smallestteam_score = team3_score;
724 if(c4 >= 0 && (c4 < smallestteam_count || (c4 <= smallestteam_count && team4_score < smallestteam_score)))
727 smallestteam_count = c4;
728 smallestteam_score = team4_score;
733 if(c1 >= 0 && (c1 < smallestteam_count ||
734 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
737 smallestteam_count = c1;
739 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
740 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
743 smallestteam_count = c2;
745 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
746 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
749 smallestteam_count = c3;
751 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
752 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
755 smallestteam_count = c4;
762 float JoinBestTeam(entity pl, float only_return_best)
764 float smallest, selectedteam;
766 // don't join a team if we're not playing a team game
767 if(!cvar("teamplay") && !g_domination && !g_ctf && !g_keyhunt)
770 // find out what teams are available
773 if(cvar("g_domination"))
775 if(cvar("g_domination_default_teams") < 3)
777 if(cvar("g_domination_default_teams") < 4)
781 // if we don't care what team he ends up on, put him on whatever team he entered as.
782 // if he's not on a valid team, then let other code put him on the smallest team
783 if(!cvar("g_campaign") && !cvar("g_balance_teams") && !cvar("g_balance_teams_force"))
785 if( c1 >= 0 && pl.team == COLOR_TEAM1)
786 selectedteam = pl.team;
787 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
788 selectedteam = pl.team;
789 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
790 selectedteam = pl.team;
791 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
792 selectedteam = pl.team;
797 if(!only_return_best)
799 SetPlayerColors(pl, selectedteam - 1);
804 // otherwise end up on the smallest team (handled below)
807 smallest = FindSmallestTeam(pl, TRUE);
810 if(!only_return_best)
812 TeamchangeFrags(self);
815 SetPlayerColors(pl, COLOR_TEAM1 - 1);
817 else if(smallest == 2)
819 SetPlayerColors(pl, COLOR_TEAM2 - 1);
821 else if(smallest == 3)
823 SetPlayerColors(pl, COLOR_TEAM3 - 1);
825 else if(smallest == 4)
827 SetPlayerColors(pl, COLOR_TEAM4 - 1);
831 error("smallest team: invalid team\n");
834 if(pl.deadflag == DEAD_NO)
835 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
842 void SV_ChangeTeam(float _color)
844 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
846 // in normal deathmatch we can just apply the color and we're done
847 if(!cvar("teamplay")) {
848 SetPlayerColors(self, _color);
852 scolor = self.clientcolors & 0x0F;
853 dcolor = _color & 0x0F;
855 if(scolor == COLOR_TEAM1 - 1)
857 else if(scolor == COLOR_TEAM2 - 1)
859 else if(scolor == COLOR_TEAM3 - 1)
861 else if(scolor == COLOR_TEAM4 - 1)
863 if(dcolor == COLOR_TEAM1 - 1)
865 else if(dcolor == COLOR_TEAM2 - 1)
867 else if(dcolor == COLOR_TEAM3 - 1)
869 else if(dcolor == COLOR_TEAM4 - 1)
872 // remap invalid teams in dom & ctf
874 if(cvar("g_ctf") && dteam == 3)
876 else if(cvar("g_ctf") && dteam == 4)
878 else if((cvar("g_domination") && cvar("g_domination_default_teams") < 3) || (cvar("g_tdm") && cvar("g_tdm_teams") < 3))
885 else if((cvar("g_domination") && cvar("g_domination_default_teams") < 4) || (cvar("g_tdm") && cvar("g_tdm_teams") < 4))
893 if(dteam == 3 && c3 < 0)
895 if(dteam == 4 && c4 < 0)
898 // not changing teams
901 //bprint("same team change\n");
902 SetPlayerTeam(self, dteam, steam, TRUE);
908 if(cvar("g_campaign") || cvar("g_changeteam_banned"))
910 sprint(self, "Team changes not allowed\n");
911 return; // changing teams is not allowed
914 if(!cvar("g_campaign") && cvar("g_balance_teams_prevent_imbalance"))
916 // only allow changing to a smaller or equal size team
918 // find out what teams are available
920 // count how many players on each team
921 GetTeamCounts(world);
924 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
929 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
934 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
939 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
946 sprint(self, "Cannot change to an invalid team\n");
952 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
954 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
956 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
958 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
961 if(scount) // started at a valid, nonempty team
963 // check if we're trying to change to a larger team that doens't have bots to swap with
964 if(dcount >= scount && dbotcount <= 0)
966 sprint(self, "Cannot change to a larger team\n");
967 return; // can't change to a larger team
973 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
975 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
977 // reduce frags during a team change
978 TeamchangeFrags(self);
981 SetPlayerTeam(self, dteam, steam, FALSE);
983 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
985 // kill player when changing teams
986 if(self.deadflag == DEAD_NO)
987 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
991 void ShufflePlayerOutOfTeam (float source_team)
993 float smallestteam, smallestteam_count, steam;
994 float lowest_bot_score, lowest_player_score;
995 entity head, lowest_bot, lowest_player, selected;
998 smallestteam_count = 999999999;
1000 if(c1 >= 0 && c1 < smallestteam_count)
1003 smallestteam_count = c1;
1005 if(c2 >= 0 && c2 < smallestteam_count)
1008 smallestteam_count = c2;
1010 if(c3 >= 0 && c3 < smallestteam_count)
1013 smallestteam_count = c3;
1015 if(c4 >= 0 && c4 < smallestteam_count)
1018 smallestteam_count = c4;
1023 bprint("warning: no smallest team\n");
1027 if(source_team == 1)
1028 steam = COLOR_TEAM1;
1029 else if(source_team == 2)
1030 steam = COLOR_TEAM2;
1031 else if(source_team == 3)
1032 steam = COLOR_TEAM3;
1033 else if(source_team == 4)
1034 steam = COLOR_TEAM4;
1037 lowest_bot_score = 999999999;
1038 lowest_player = world;
1039 lowest_player_score = 999999999;
1041 // find the lowest-scoring player & bot of that team
1042 FOR_EACH_PLAYER(head)
1044 if(head.team == steam)
1048 if(head.frags < lowest_bot_score)
1051 lowest_bot_score = head.frags;
1056 if(head.frags < lowest_player_score)
1058 lowest_player = head;
1059 lowest_player_score = head.frags;
1065 // prefers to move a bot...
1066 if(lowest_bot != world)
1067 selected = lowest_bot;
1068 // but it will move a player if it has to
1070 selected = lowest_player;
1071 // don't do anything if it couldn't find anyone
1074 bprint("warning: couldn't find a player to move from team\n");
1078 // smallest team gains a member
1079 if(smallestteam == 1)
1083 else if(smallestteam == 2)
1087 else if(smallestteam == 3)
1091 else if(smallestteam == 4)
1097 bprint("warning: destination team invalid\n");
1100 // source team loses a member
1101 if(source_team == 1)
1105 else if(source_team == 2)
1109 else if(source_team == 3)
1113 else if(source_team == 4)
1119 bprint("warning: source team invalid\n");
1123 // move the player to the new team
1124 TeamchangeFrags(selected);
1125 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1127 if(selected.deadflag == DEAD_NO)
1128 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1129 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1132 float lastRebalanceInfo;
1133 void CauseRebalance(float source_team, float howmany_toomany)
1138 if(IsTeamBalanceForced() == 1)
1140 bprint("Rebalancing Teams\n");
1141 ShufflePlayerOutOfTeam(source_team);
1145 if(howmany_toomany < cvar("g_balance_teams_complain"))
1147 if(time < lastRebalanceInfo + 90)
1149 lastRebalanceInfo = time;
1150 if(source_team == 1)
1151 steam = COLOR_TEAM1;
1152 else if(source_team == 2)
1153 steam = COLOR_TEAM2;
1154 else if(source_team == 3)
1155 steam = COLOR_TEAM3;
1156 else if(source_team == 4)
1157 steam = COLOR_TEAM4;
1158 ServerConsoleEcho(strcat("Team ", ftos(source_team), " too large, complaining."), TRUE);
1159 FOR_EACH_REALPLAYER(head)
1161 if(head.team == steam)
1163 sprint(head, "\{1}\{13}^3SERVER NOTICE:^7 One of you please change teams!\n");
1164 centerprint_atprio(head, CENTERPRIO_REBALANCE, "^3SERVER NOTICE:\n\n^7Someone of you please change teams!");
1170 // part of g_balance_teams_force
1171 // occasionally perform an audit of the teams to make
1172 // sure they're more or less balanced in player count.
1175 float numplayers, numteams, smallest, toomany;
1177 balance = IsTeamBalanceForced();
1181 if(audit_teams_time > time)
1184 audit_teams_time = time + 4 + random();
1186 // bprint("Auditing teams\n");
1188 CheckAllowedTeams();
1189 GetTeamCounts(world);
1192 numteams = numplayers = smallest = 0;
1195 numteams = numteams + 1;
1196 numplayers = numplayers + c1;
1201 numteams = numteams + 1;
1202 numplayers = numplayers + c2;
1208 numteams = numteams + 1;
1209 numplayers = numplayers + c3;
1215 numteams = numteams + 1;
1216 numplayers = numplayers + c4;
1222 return; // no players to move around
1224 return; // don't bother shuffling if for some reason there aren't any teams
1226 toomany = smallest + 1;
1228 if(c1 && c1 > toomany)
1229 CauseRebalance(1, c1 - toomany);
1230 if(c2 && c2 > toomany)
1231 CauseRebalance(2, c2 - toomany);
1232 if(c3 && c3 > toomany)
1233 CauseRebalance(3, c3 - toomany);
1234 if(c4 && c4 > toomany)
1235 CauseRebalance(4, c4 - toomany);
1237 // if teams are still unbalanced, balance them further in the next audit,
1238 // which will happen sooner (keep doing rapid audits until things are in order)
1239 audit_teams_time = time + 0.7 + random()*0.3;
1244 /*void(entity e, float first) UpdateTeamScore =
1249 WriteByte (MSG_ALL, SVC_UPDATENAME);
1250 WriteByte (MSG_ALL, clientno);
1251 WriteString (MSG_ALL, e.netname);
1253 WriteByte (MSG_ALL, SVC_UPDATECOLORS);
1254 WriteByte (MSG_ALL, clientno);
1255 WriteByte (MSG_ALL, e.b_shirt * 16 + who.b_pants);
1258 WriteByte (MSG_ALL, SVC_UPDATEFRAGS);
1259 WriteByte (MSG_ALL, clientno);
1260 WriteShort (MSG_ALL, e.frags + 10000);
1266 // code from here on is just to support maps that don't have team entities
1267 void tdm_spawnteam (string teamname, float teamcolor)
1271 e.classname = "tdm_team";
1272 e.netname = teamname;
1277 // spawn some default teams if the map is not set up for tdm
1278 void() tdm_spawnteams =
1282 numteams = cvar("g_tdm_teams");
1284 tdm_spawnteam("Red", COLOR_TEAM1-1);
1285 tdm_spawnteam("Blue", COLOR_TEAM2-1);
1288 void() tdm_delayedinit =
1290 self.think = SUB_Remove;
1291 self.nextthink = time;
1292 // if no teams are found, spawn defaults
1293 if (find(world, classname, "tdm_team") == world)
1301 e.think = tdm_delayedinit;
1302 e.nextthink = time + 0.1;