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;
10 float GAME_ONSLAUGHT = 10;
12 // client counts for each team
14 // # of bots on those teams
15 float cb1, cb2, cb3, cb4;
17 float audit_teams_time;
19 float() IsTeamBalanceForced = {
20 if(intermission_running)
21 return 0; // no rebalancing whatsoever please
24 if(cvar("g_campaign"))
26 if(!cvar("g_balance_teams_force"))
31 void TeamchangeFrags(entity e)
33 if(e.classname == "player")
35 // reduce frags during a team change
36 DistributeFragsAmongTeam(e, (e.clientcolors & 0x0F) + 1, 1 - cvar("g_changeteam_fragtransfer") / 100);
40 string TeamName(float t)
42 // fixme: Search for team entities and get their .netname's!
51 return "Neutral Team";
53 string ColoredTeamName(float t)
55 // fixme: Search for team entities and get their .netname's!
57 return "^1Red Team^7";
59 return "^4Blue Team^7";
61 return "^3Yellow Team^7";
63 return "^6Pink Team^7";
64 return "Neutral Team";
66 string TeamNoName(float t)
68 // fixme: Search for team entities and get their .netname's!
77 return "Neutral Team";
82 void runematch_init();
85 void(entity pl) LogTeamchange =
88 if(!cvar("sv_eventlog"))
92 str = strcat(":team:", ftos(pl.playerid), ":");
93 str = strcat(str, ftos(pl.team));
94 GameLogEcho(str, FALSE);
99 cvar_set("g_tdm", "0");
100 cvar_set("g_domination", "0");
101 cvar_set("g_ctf", "0");
102 cvar_set("g_runematch", "0");
103 cvar_set("g_lms", "0");
104 cvar_set("g_arena", "0");
105 cvar_set("g_keyhunt", "0");
106 cvar_set("g_assault", "0");
107 cvar_set("g_onslaught", "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";
250 else if(game == GAME_ONSLAUGHT || cvar("g_onslaught"))
253 game = GAME_ONSLAUGHT;
254 cvar_set("g_onslaught", "1");
258 gamemode_name = "Onslaught";
263 // we can only assume...
265 gamemode_name = "Deathmatch";
268 /* else if(game == GAME_TEAM_DEATHMATCH)
270 if(!cvar("deathmatch"))
271 cvar_set("deathmatch", "1");
273 //if(!cvar("teamplay"))
274 // cvar_set("teamplay", "3");
277 fraglimit_override = cvar("fraglimit_override");
280 // enforce the server's universal frag/time limits
281 if(!cvar("g_campaign"))
283 if(fraglimit_override >= 0)
284 cvar_set("fraglimit", ftos(fraglimit_override));
285 if(timelimit_override >= 0)
286 cvar_set("timelimit", ftos(timelimit_override));
289 if (game == GAME_DOMINATION)//cvar("g_domination"))
291 else if (game == GAME_CTF)//cvar("g_ctf"))
293 else if (game == GAME_RUNEMATCH)//cvar("g_runematch"))
295 else if (game == GAME_TEAM_DEATHMATCH)//cvar("g_runematch"))
297 else if (game == GAME_KEYHUNT)//cvar("g_keyhunt"))
300 // those mutators rule each other out
301 if(cvar("g_minstagib"))
303 cvar_set("g_instagib", "0");
304 cvar_set("g_rocketarena", "0");
306 if(cvar("g_instagib"))
308 cvar_set("g_minstagib", "0");
309 cvar_set("g_rocketarena", "0");
311 if(cvar("g_rocketarena"))
313 cvar_set("g_instagib", "0");
314 cvar_set("g_minstagib", "0");
317 registercvar("_motd", "");
318 registercvar("_mutatormsg", "");
319 cvar_set("_motd", wordwrap(cvar_string("sv_motd"), 50));
320 cvar_set("_mutatormsg", wordwrap(cvar_string("g_mutatormsg"), 50));
322 g_domination = cvar("g_domination");
323 g_ctf = cvar("g_ctf");
324 g_lms = cvar("g_lms");
325 g_tdm = cvar("g_tdm");
326 g_keyhunt = cvar("g_keyhunt");
327 g_onslaught = cvar("g_onslaught");
328 g_assault = cvar("g_assault");
329 g_arena = cvar("g_arena");
332 string GetClientVersionMessage(float v) {
333 local string versionmsg;
335 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";
336 // either that or someone wants to be funny
337 } else if (v != cvar("gameversion")) {
338 if(v < cvar("gameversion")) {
339 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
341 versionmsg = "^3This server is using an outdated Nexuiz version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
344 versionmsg = "^2client version and server version are compatible.^8";
346 return strzone(versionmsg);
351 void PrintWelcomeMessage(entity pl)
353 string s, mutator, modifications, padding;
355 /*if(self.welcomemessage_time > time)
357 self.welcomemessage_time = time + 0.8; */
359 if(self.cvar_scr_centertime == 0) return;
360 if(self.welcomemessage_time > time) return;
361 self.welcomemessage_time = time + self.cvar_scr_centertime * 0.6;
363 if(cvar("g_campaign"))
365 centerprint(pl, campaign_message);
369 if(self.classname == "observer")
371 if(g_lms && self.frags <= 0 && self.frags > -666)
372 return centerprint(self, 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(self, 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(self, strcat(NEWLINES, "spectating ", self.enemy.netname, "\n\n\n^7press attack for next player\npress attack2 for free fly mode"));
381 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"));
385 if(cvar("g_minstagib"))
386 mutator = "^2Minstagib ^1";
387 else if(cvar("g_instagib"))
388 mutator = "^2Instagib ^1";
389 else if(cvar("g_rocketarena"))
390 mutator = "^2Rocketarena ^1";
391 else if(cvar("g_nixnex"))
392 mutator = "^2No Items Nexuiz ^1";
394 if(cvar("g_midair")) {
395 // to protect against unheedingly made changes
397 modifications = strcat(modifications, ", ");
399 modifications = "midair";
401 if(cvar("g_vampire")) {
403 modifications = strcat(modifications, ", ");
405 modifications = strcat(modifications, "vampire");
407 if(cvar("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((self.classname == "observer" || self.classname == "spectator") && self.version == cvar("gameversion")) {
425 s = strcat(s,"^7\n\n\npress jump to play\npress attack to spectate other players\n\n");
426 else if(player_count < 2 && arena_roundbased)
428 s = strcat(s, "\n\n\n^1waiting for second player to start match^7\n\n");
432 s = strcat(s, "\n\n\n");
434 s = strcat(s, "^7current champion is: ", champion.netname, "\n\n");
435 s = strcat(s,"^7press attack to spectate other players\n\n");
442 if (cvar("g_grappling_hook"))
443 s = strcat(s, "\n\n^8grappling hook is enabled, press 'e' to use it\n");
445 if (cvar_string("_mutatormsg") != "") {
446 s = strcat(s, "\n\n^8special gameplay tips: ^7", cvar_string("_mutatormsg"));
449 if (cvar_string("_motd") != "") {
450 s = strcat(s, "\n\n^8MOTD: ^7", cvar_string("_motd"));
454 if(cvar("fraglimit"))
457 if(cvar("timelimit"))
460 s = strcat(s, "\n^8frag limit: ^7", cvar_string("fraglimit"), padding);
462 if(cvar("timelimit"))
463 s = strcat(s, "\n^8time limit: ^7", cvar_string("timelimit"), " minutes");
474 void SetPlayerColors(entity pl, float _color)
478 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
480 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
481 pl.clientcolors = 16*cl + cl;*/
484 pants = _color & 0x0F;
485 shirt = _color & 0xF0;
489 setcolor(pl, 16*pants + pants);
491 setcolor(pl, shirt + pants);
495 void SetPlayerTeam(entity pl, float t, float s, float noprint)
500 _color = COLOR_TEAM4 - 1;
502 _color = COLOR_TEAM3 - 1;
504 _color = COLOR_TEAM2 - 1;
506 _color = COLOR_TEAM1 - 1;
508 SetPlayerColors(pl,_color);
510 if(!noprint && t != s)
512 //bprint(pl.netname, " has changed to ", TeamNoName(t), "\n");
513 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
525 // set c1...c4 to show what teams are allowed
526 void CheckAllowedTeams (entity for_whom)
535 c1 = c2 = c3 = c4 = -1;
536 cb1 = cb2 = cb3 = cb4 = 0;
538 // onslaught is special
541 head = findchain(classname, "onslaught_generator");
544 if (head.team == COLOR_TEAM1) c1 = 0;
545 if (head.team == COLOR_TEAM2) c2 = 0;
546 if (head.team == COLOR_TEAM3) c3 = 0;
547 if (head.team == COLOR_TEAM4) c4 = 0;
554 teament_name = "dom_team";
556 teament_name = "ctf_team";
558 teament_name = "tdm_team";
561 c1 = c2 = 0; // Assault always has 2 teams
566 // cover anything else by treating it like tdm with no teams spawned
570 dm = cvar("g_tdm_teams");
572 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
576 c1 = c2 = c3 = c4 = 0;
589 // first find out what teams are allowed
590 head = find(world, classname, teament_name);
593 if(!(g_domination && head.netname == ""))
595 if(head.team == COLOR_TEAM1)
599 if(head.team == COLOR_TEAM2)
603 if(head.team == COLOR_TEAM3)
607 if(head.team == COLOR_TEAM4)
612 head = find(head, classname, teament_name);
615 if(cvar("bot_vs_human") > 0)
618 if(clienttype(for_whom) == CLIENTTYPE_BOT)
623 else if(cvar("bot_vs_human") < 0)
626 if(clienttype(for_whom) == CLIENTTYPE_BOT)
633 float PlayerValue(entity p)
635 if(IsTeamBalanceForced() == 1)
640 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
641 // teams that are allowed will now have their player counts stored in c1...c4
642 void GetTeamCounts(entity ignore)
646 // now count how many players are on each team already
648 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
649 // also remember the lowest-scoring player
651 FOR_EACH_PLAYER(head)
653 if(head != ignore)// && head.netname != "")
655 value = PlayerValue(head);
656 if(clienttype(head) == CLIENTTYPE_BOT)
660 if(head.team == COLOR_TEAM1)
668 if(head.team == COLOR_TEAM2)
676 if(head.team == COLOR_TEAM3)
684 if(head.team == COLOR_TEAM4)
696 // returns # of smallest team (1, 2, 3, 4)
697 // NOTE: Assumes CheckAllowedTeams has already been called!
698 float FindSmallestTeam(entity pl, float ignore_pl)
700 float totalteams, smallestteam, smallestteam_count, smallestteam_score, balance_type;
703 // find out what teams are available
704 //CheckAllowedTeams();
706 // make sure there are at least 2 teams to join
708 totalteams = totalteams + 1;
710 totalteams = totalteams + 1;
712 totalteams = totalteams + 1;
714 totalteams = totalteams + 1;
716 if(cvar("bot_vs_human"))
722 error("Too few teams available for domination\n");
724 error("Too few teams available for ctf\n");
726 error("Too few teams available for key hunt\n");
728 error("Too few teams available for team deathmatch\n");
732 // count how many players are in each team
736 GetTeamCounts(world);
738 // c1...c4 now have counts of each team
739 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
742 smallestteam_count = 999999999;
743 smallestteam_score = 999999999;
745 // 2 gives priority to what team you're already on, 1 goes in order
746 // 2 doesn't seem to work though...
750 //if(pl.classname != "player")
751 if(clienttype(pl) != CLIENTTYPE_BOT)
753 c1 -= cb1 * 255.0/256;
754 c2 -= cb2 * 255.0/256;
755 c3 -= cb3 * 255.0/256;
756 c4 -= cb4 * 255.0/256;
759 if(balance_type == 1)
761 if(c1 >= 0 && (c1 < smallestteam_count || (c1 <= smallestteam_count && team1_score < smallestteam_score)))
764 smallestteam_count = c1;
765 smallestteam_score = team1_score;
767 if(c2 >= 0 && (c2 < smallestteam_count || (c2 <= smallestteam_count && team2_score < smallestteam_score)))
770 smallestteam_count = c2;
771 smallestteam_score = team2_score;
773 if(c3 >= 0 && (c3 < smallestteam_count || (c3 <= smallestteam_count && team3_score < smallestteam_score)))
776 smallestteam_count = c3;
777 smallestteam_score = team3_score;
779 if(c4 >= 0 && (c4 < smallestteam_count || (c4 <= smallestteam_count && team4_score < smallestteam_score)))
782 smallestteam_count = c4;
783 smallestteam_score = team4_score;
788 if(c1 >= 0 && (c1 < smallestteam_count ||
789 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
792 smallestteam_count = c1;
794 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
795 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
798 smallestteam_count = c2;
800 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
801 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
804 smallestteam_count = c3;
806 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
807 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
810 smallestteam_count = c4;
817 float JoinBestTeam(entity pl, float only_return_best)
819 float smallest, selectedteam;
821 // don't join a team if we're not playing a team game
822 if(!cvar("teamplay") && !g_domination && !g_ctf && !g_keyhunt)
825 // find out what teams are available
826 CheckAllowedTeams(pl);
830 if(cvar("g_domination_default_teams") < 3)
832 if(cvar("g_domination_default_teams") < 4)
836 // if we don't care what team he ends up on, put him on whatever team he entered as.
837 // if he's not on a valid team, then let other code put him on the smallest team
838 if(!cvar("g_campaign") && !cvar("g_balance_teams") && !cvar("g_balance_teams_force"))
840 if( c1 >= 0 && pl.team == COLOR_TEAM1)
841 selectedteam = pl.team;
842 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
843 selectedteam = pl.team;
844 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
845 selectedteam = pl.team;
846 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
847 selectedteam = pl.team;
852 if(!only_return_best)
854 SetPlayerColors(pl, selectedteam - 1);
859 // otherwise end up on the smallest team (handled below)
862 smallest = FindSmallestTeam(pl, TRUE);
865 if(!only_return_best)
867 TeamchangeFrags(self);
870 SetPlayerColors(pl, COLOR_TEAM1 - 1);
872 else if(smallest == 2)
874 SetPlayerColors(pl, COLOR_TEAM2 - 1);
876 else if(smallest == 3)
878 SetPlayerColors(pl, COLOR_TEAM3 - 1);
880 else if(smallest == 4)
882 SetPlayerColors(pl, COLOR_TEAM4 - 1);
886 error("smallest team: invalid team\n");
889 if(pl.deadflag == DEAD_NO)
890 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
897 void SV_ChangeTeam(float _color)
899 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
901 // in normal deathmatch we can just apply the color and we're done
902 if(!cvar("teamplay")) {
903 SetPlayerColors(self, _color);
907 scolor = self.clientcolors & 0x0F;
908 dcolor = _color & 0x0F;
910 if(scolor == COLOR_TEAM1 - 1)
912 else if(scolor == COLOR_TEAM2 - 1)
914 else if(scolor == COLOR_TEAM3 - 1)
916 else if(scolor == COLOR_TEAM4 - 1)
918 if(dcolor == COLOR_TEAM1 - 1)
920 else if(dcolor == COLOR_TEAM2 - 1)
922 else if(dcolor == COLOR_TEAM3 - 1)
924 else if(dcolor == COLOR_TEAM4 - 1)
927 CheckAllowedTeams(self);
929 if(dteam == 1 && c1 < 0) dteam = 4;
930 if(dteam == 4 && c4 < 0) dteam = 3;
931 if(dteam == 3 && c3 < 0) dteam = 2;
932 if(dteam == 2 && c2 < 0) dteam = 1;
934 // not changing teams
937 //bprint("same team change\n");
938 SetPlayerTeam(self, dteam, steam, TRUE);
944 if(cvar("g_campaign") || cvar("g_changeteam_banned"))
946 sprint(self, "Team changes not allowed\n");
947 return; // changing teams is not allowed
950 if(!cvar("g_campaign") && cvar("g_balance_teams_prevent_imbalance"))
952 // only allow changing to a smaller or equal size team
954 // find out what teams are available
955 //CheckAllowedTeams();
956 // count how many players on each team
957 GetTeamCounts(world);
960 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
965 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
970 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
975 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
982 sprint(self, "Cannot change to an invalid team\n");
988 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
990 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
992 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
994 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
997 if(scount) // started at a valid, nonempty team
999 // check if we're trying to change to a larger team that doens't have bots to swap with
1000 if(dcount >= scount && dbotcount <= 0)
1002 sprint(self, "Cannot change to a larger team\n");
1003 return; // can't change to a larger team
1009 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
1011 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1013 // reduce frags during a team change
1014 TeamchangeFrags(self);
1017 SetPlayerTeam(self, dteam, steam, FALSE);
1019 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1021 // kill player when changing teams
1022 if(self.deadflag == DEAD_NO)
1023 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
1027 void ShufflePlayerOutOfTeam (float source_team)
1029 float smallestteam, smallestteam_count, steam;
1030 float lowest_bot_score, lowest_player_score;
1031 entity head, lowest_bot, lowest_player, selected;
1034 smallestteam_count = 999999999;
1036 if(c1 >= 0 && c1 < smallestteam_count)
1039 smallestteam_count = c1;
1041 if(c2 >= 0 && c2 < smallestteam_count)
1044 smallestteam_count = c2;
1046 if(c3 >= 0 && c3 < smallestteam_count)
1049 smallestteam_count = c3;
1051 if(c4 >= 0 && c4 < smallestteam_count)
1054 smallestteam_count = c4;
1059 bprint("warning: no smallest team\n");
1063 if(source_team == 1)
1064 steam = COLOR_TEAM1;
1065 else if(source_team == 2)
1066 steam = COLOR_TEAM2;
1067 else if(source_team == 3)
1068 steam = COLOR_TEAM3;
1069 else if(source_team == 4)
1070 steam = COLOR_TEAM4;
1073 lowest_bot_score = 999999999;
1074 lowest_player = world;
1075 lowest_player_score = 999999999;
1077 // find the lowest-scoring player & bot of that team
1078 FOR_EACH_PLAYER(head)
1080 if(head.team == steam)
1084 if(head.frags < lowest_bot_score)
1087 lowest_bot_score = head.frags;
1092 if(head.frags < lowest_player_score)
1094 lowest_player = head;
1095 lowest_player_score = head.frags;
1101 // prefers to move a bot...
1102 if(lowest_bot != world)
1103 selected = lowest_bot;
1104 // but it will move a player if it has to
1106 selected = lowest_player;
1107 // don't do anything if it couldn't find anyone
1110 bprint("warning: couldn't find a player to move from team\n");
1114 // smallest team gains a member
1115 if(smallestteam == 1)
1119 else if(smallestteam == 2)
1123 else if(smallestteam == 3)
1127 else if(smallestteam == 4)
1133 bprint("warning: destination team invalid\n");
1136 // source team loses a member
1137 if(source_team == 1)
1141 else if(source_team == 2)
1145 else if(source_team == 3)
1149 else if(source_team == 4)
1155 bprint("warning: source team invalid\n");
1159 // move the player to the new team
1160 TeamchangeFrags(selected);
1161 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1163 if(selected.deadflag == DEAD_NO)
1164 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1165 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1168 float lastRebalanceInfo;
1169 void CauseRebalance(float source_team, float howmany_toomany)
1174 if(IsTeamBalanceForced() == 1)
1176 bprint("Rebalancing Teams\n");
1177 ShufflePlayerOutOfTeam(source_team);
1181 if(howmany_toomany < cvar("g_balance_teams_complain"))
1183 if(time < lastRebalanceInfo + 90)
1185 lastRebalanceInfo = time;
1186 if(source_team == 1)
1187 steam = COLOR_TEAM1;
1188 else if(source_team == 2)
1189 steam = COLOR_TEAM2;
1190 else if(source_team == 3)
1191 steam = COLOR_TEAM3;
1192 else if(source_team == 4)
1193 steam = COLOR_TEAM4;
1194 ServerConsoleEcho(strcat("Team ", ftos(source_team), " too large, complaining."), TRUE);
1195 FOR_EACH_REALPLAYER(head)
1197 if(head.team == steam)
1199 sprint(head, "\{1}\{13}^3SERVER NOTICE:^7 One of you please change teams!\n");
1200 centerprint_atprio(head, CENTERPRIO_REBALANCE, "^3SERVER NOTICE:\n\n^7Someone of you please change teams!");
1206 // part of g_balance_teams_force
1207 // occasionally perform an audit of the teams to make
1208 // sure they're more or less balanced in player count.
1211 float numplayers, numteams, smallest, toomany;
1213 balance = IsTeamBalanceForced();
1217 if(audit_teams_time > time)
1220 audit_teams_time = time + 4 + random();
1222 // bprint("Auditing teams\n");
1224 CheckAllowedTeams(world);
1225 GetTeamCounts(world);
1228 numteams = numplayers = smallest = 0;
1231 numteams = numteams + 1;
1232 numplayers = numplayers + c1;
1237 numteams = numteams + 1;
1238 numplayers = numplayers + c2;
1244 numteams = numteams + 1;
1245 numplayers = numplayers + c3;
1251 numteams = numteams + 1;
1252 numplayers = numplayers + c4;
1258 return; // no players to move around
1260 return; // don't bother shuffling if for some reason there aren't any teams
1262 toomany = smallest + 1;
1264 if(c1 && c1 > toomany)
1265 CauseRebalance(1, c1 - toomany);
1266 if(c2 && c2 > toomany)
1267 CauseRebalance(2, c2 - toomany);
1268 if(c3 && c3 > toomany)
1269 CauseRebalance(3, c3 - toomany);
1270 if(c4 && c4 > toomany)
1271 CauseRebalance(4, c4 - toomany);
1273 // if teams are still unbalanced, balance them further in the next audit,
1274 // which will happen sooner (keep doing rapid audits until things are in order)
1275 audit_teams_time = time + 0.7 + random()*0.3;
1280 /*void(entity e, float first) UpdateTeamScore =
1285 WriteByte (MSG_ALL, SVC_UPDATENAME);
1286 WriteByte (MSG_ALL, clientno);
1287 WriteString (MSG_ALL, e.netname);
1289 WriteByte (MSG_ALL, SVC_UPDATECOLORS);
1290 WriteByte (MSG_ALL, clientno);
1291 WriteByte (MSG_ALL, e.b_shirt * 16 + who.b_pants);
1294 WriteByte (MSG_ALL, SVC_UPDATEFRAGS);
1295 WriteByte (MSG_ALL, clientno);
1296 WriteShort (MSG_ALL, e.frags + 10000);
1302 // code from here on is just to support maps that don't have team entities
1303 void tdm_spawnteam (string teamname, float teamcolor)
1307 e.classname = "tdm_team";
1308 e.netname = teamname;
1313 // spawn some default teams if the map is not set up for tdm
1314 void() tdm_spawnteams =
1318 numteams = cvar("g_tdm_teams");
1320 tdm_spawnteam("Red", COLOR_TEAM1-1);
1321 tdm_spawnteam("Blue", COLOR_TEAM2-1);
1323 tdm_spawnteam("Yellow", COLOR_TEAM3-1);
1325 tdm_spawnteam("Pink", COLOR_TEAM4-1);
1328 void() tdm_delayedinit =
1330 self.think = SUB_Remove;
1331 self.nextthink = time;
1332 // if no teams are found, spawn defaults
1333 if (find(world, classname, "tdm_team") == world)
1341 e.think = tdm_delayedinit;
1342 e.nextthink = time + 0.1;