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_dm", "0");
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("g_onslaught", "0");
109 cvar_set("teamplay", "0");
112 void ActivateTeamplay()
114 float teamplay_default;
115 teamplay_default = cvar("teamplay_default");
118 cvar_set("teamplay", ftos(teamplay_default));
120 cvar_set("teamplay", "3");
123 void InitGameplayMode()
125 float fraglimit_override, timelimit_override;
129 game = cvar("gamecfg"); // load game options
131 // game cvars get reset before map changes
132 // then map's cfg sets them as desired
134 // FIXME: also set a message or game mode name to print to players when the join
136 // set both here, gamemode can override it later
137 timelimit_override = cvar("timelimit_override");
138 fraglimit_override = cvar("fraglimit_override");
140 if(game == GAME_DOMINATION || cvar("g_domination"))
143 game = GAME_DOMINATION;
144 cvar_set("g_domination", "1");
148 fraglimit_override = cvar("g_domination_point_limit");
150 gamemode_name = "Domination";
153 else if(game == GAME_CTF || cvar("g_ctf"))
157 cvar_set("g_ctf", "1");
161 fraglimit_override = cvar("g_ctf_capture_limit");
163 gamemode_name = "Capture the Flag";
166 else if(game == GAME_RUNEMATCH || cvar("g_runematch"))
168 game = GAME_RUNEMATCH;
169 cvar_set("g_runematch", "1");
170 cvar_set("g_minstagib", "0");
172 if(cvar("deathmatch_force_teamplay"))
175 fraglimit_override = cvar("g_runematch_point_limit");
177 gamemode_name = "Rune Match";
183 else if(game == GAME_DEATHMATCH || game == GAME_TEAM_DEATHMATCH || cvar("g_tdm"))
185 if(!cvar("deathmatch"))
186 cvar_set("deathmatch", "1");
189 if(game == GAME_TEAM_DEATHMATCH || cvar("g_tdm") || cvar("deathmatch_force_teamplay"))
192 game = GAME_TEAM_DEATHMATCH;
193 gamemode_name = "Team Deathmatch";
196 cvar_set("g_tdm", "1");
201 game = GAME_DEATHMATCH;
202 gamemode_name = "Deathmatch";
206 fraglimit_override = cvar("fraglimit_override");
208 else if(game == GAME_ASSAULT || cvar("g_assault"))
212 gamemode_name = "Assault";
215 cvar_set("g_assault", "1");
217 else if(game == GAME_LMS || cvar("g_lms"))
221 cvar_set("g_lms", "1");
222 fraglimit_override = cvar("g_lms_lives_override");
223 if(fraglimit_override == 0)
224 fraglimit_override = -1;
225 gamemode_name = "Last Man Standing";
227 lms_lowest_lives = 999;
229 else if(game == GAME_ARENA || cvar("g_arena"))
233 cvar_set("g_arena", "1");
234 fraglimit_override = cvar("g_arena_point_limit");
235 maxspawned = cvar("g_arena_maxspawned");
238 arena_roundbased = cvar("g_arena_roundbased");
239 gamemode_name = "Arena";
242 else if(game == GAME_KEYHUNT || cvar("g_keyhunt"))
246 cvar_set("g_keyhunt", "1");
247 fraglimit_override = cvar("g_keyhunt_point_limit");
249 gamemode_name = "Key Hunt";
252 else if(game == GAME_ONSLAUGHT || cvar("g_onslaught"))
255 game = GAME_ONSLAUGHT;
256 cvar_set("g_onslaught", "1");
260 gamemode_name = "Onslaught";
265 // we can only assume...
267 cvar_set("g_dm", "1");
268 gamemode_name = "Deathmatch";
271 /* else if(game == GAME_TEAM_DEATHMATCH)
273 if(!cvar("deathmatch"))
274 cvar_set("deathmatch", "1");
276 //if(!cvar("teamplay"))
277 // cvar_set("teamplay", "3");
280 fraglimit_override = cvar("fraglimit_override");
283 // those mutators rule each other out
284 if(cvar("g_minstagib"))
286 cvar_set("g_instagib", "0");
287 cvar_set("g_rocketarena", "0");
289 if(cvar("g_instagib"))
291 cvar_set("g_minstagib", "0");
292 cvar_set("g_rocketarena", "0");
294 if(cvar("g_rocketarena"))
296 cvar_set("g_instagib", "0");
297 cvar_set("g_minstagib", "0");
300 registercvar("_motd", "");
301 registercvar("_mutatormsg", "");
302 cvar_set("_motd", wordwrap(cvar_string("sv_motd"), 50));
303 cvar_set("_mutatormsg", wordwrap(cvar_string("g_mutatormsg"), 50));
305 g_domination = cvar("g_domination");
306 g_ctf = cvar("g_ctf");
307 g_lms = cvar("g_lms");
308 g_tdm = cvar("g_tdm");
309 g_runematch = cvar("g_runematch");
310 g_keyhunt = cvar("g_keyhunt");
311 g_onslaught = cvar("g_onslaught");
312 g_assault = cvar("g_assault");
313 g_arena = cvar("g_arena");
315 // enforce the server's universal frag/time limits
316 if(!cvar("g_campaign"))
318 if(fraglimit_override >= 0)
319 cvar_set("fraglimit", ftos(fraglimit_override));
320 if(timelimit_override >= 0)
321 cvar_set("timelimit", ftos(timelimit_override));
324 if (game == GAME_DOMINATION)//cvar("g_domination"))
326 else if (game == GAME_CTF)//cvar("g_ctf"))
328 else if (game == GAME_RUNEMATCH)//cvar("g_runematch"))
330 else if (game == GAME_TEAM_DEATHMATCH)//cvar("g_runematch"))
332 else if (game == GAME_KEYHUNT)//cvar("g_keyhunt"))
336 string GetClientVersionMessage(float v) {
337 local string versionmsg;
339 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";
340 // either that or someone wants to be funny
341 } else if (v != cvar("gameversion")) {
342 if(v < cvar("gameversion")) {
343 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
345 versionmsg = "^3This server is using an outdated Nexuiz version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
348 versionmsg = "^2client version and server version are compatible.^8";
350 return strzone(versionmsg);
355 void PrintWelcomeMessage(entity pl)
357 string s, mutator, modifications, padding;
359 /*if(self.welcomemessage_time > time)
361 self.welcomemessage_time = time + 0.8; */
363 if(self.cvar_scr_centertime == 0) return;
364 if(self.welcomemessage_time > time) return;
365 self.welcomemessage_time = time + self.cvar_scr_centertime * 0.6;
367 if(cvar("g_campaign"))
369 centerprint(pl, campaign_message);
375 if(self.classname == "observer")
377 if(g_lms && self.frags <= 0 && self.frags > -666)
378 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"));
379 else if(g_lms && self.frags == -666)
380 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"));
382 else if(self.classname == "spectator")
384 if ((g_lms && self.frags < 1) || g_arena)
385 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"));
387 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"));
392 mutator = "^2Minstagib ^1";
394 mutator = "^2Instagib ^1";
395 else if(g_rocketarena)
396 mutator = "^2Rocketarena ^1";
398 mutator = "^2No Items Nexuiz ^1";
401 // to protect against unheedingly made changes
403 modifications = strcat(modifications, ", ");
405 modifications = "midair";
409 modifications = strcat(modifications, ", ");
411 modifications = strcat(modifications, "vampire");
413 if(g_laserguided_missile) {
415 modifications = strcat(modifications, ", ");
417 modifications = strcat(modifications, "laser-guided-missiles");
420 local string versionmessage;
421 versionmessage = GetClientVersionMessage(self.version);
423 s = strcat(s, NEWLINES, "This is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
424 s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
426 if(modifications != "")
427 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
429 if(time < restart_countdown)
430 s = strcat(s, "\n^1Game starts in ", ftos(ceil(restart_countdown-time)), " seconds^7");
434 if (g_grappling_hook)
435 s = strcat(s, "\n\n^8grappling hook is enabled, press 'e' to use it\n");
437 if (cvar_string("_mutatormsg") != "") {
438 s = strcat(s, "\n\n^8special gameplay tips: ^7", cvar_string("_mutatormsg"));
441 if (cvar_string("_motd") != "") {
442 s = strcat(s, "\n\n^8MOTD: ^7", cvar_string("_motd"));
446 if(cvar("fraglimit"))
449 if(cvar("timelimit"))
452 s = strcat(s, "\n^8frag limit: ^7", cvar_string("fraglimit"), padding);
454 if(cvar("timelimit"))
455 s = strcat(s, "\n^8time limit: ^7", cvar_string("timelimit"), " minutes");
466 void SetPlayerColors(entity pl, float _color)
470 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
472 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
473 pl.clientcolors = 16*cl + cl;*/
476 pants = _color & 0x0F;
477 shirt = _color & 0xF0;
481 setcolor(pl, 16*pants + pants);
483 setcolor(pl, shirt + pants);
487 void SetPlayerTeam(entity pl, float t, float s, float noprint)
492 _color = COLOR_TEAM4 - 1;
494 _color = COLOR_TEAM3 - 1;
496 _color = COLOR_TEAM2 - 1;
498 _color = COLOR_TEAM1 - 1;
500 SetPlayerColors(pl,_color);
502 if(!noprint && t != s)
504 //bprint(pl.netname, " has changed to ", TeamNoName(t), "\n");
505 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
517 // set c1...c4 to show what teams are allowed
518 void CheckAllowedTeams (entity for_whom)
527 c1 = c2 = c3 = c4 = -1;
528 cb1 = cb2 = cb3 = cb4 = 0;
530 // onslaught is special
533 head = findchain(classname, "onslaught_generator");
536 if (head.team == COLOR_TEAM1) c1 = 0;
537 if (head.team == COLOR_TEAM2) c2 = 0;
538 if (head.team == COLOR_TEAM3) c3 = 0;
539 if (head.team == COLOR_TEAM4) c4 = 0;
546 teament_name = "dom_team";
548 teament_name = "ctf_team";
550 teament_name = "tdm_team";
553 c1 = c2 = 0; // Assault always has 2 teams
558 // cover anything else by treating it like tdm with no teams spawned
562 dm = cvar("g_tdm_teams");
564 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
568 c1 = c2 = c3 = c4 = 0;
581 // first find out what teams are allowed
582 head = find(world, classname, teament_name);
585 if(!(g_domination && head.netname == ""))
587 if(head.team == COLOR_TEAM1)
591 if(head.team == COLOR_TEAM2)
595 if(head.team == COLOR_TEAM3)
599 if(head.team == COLOR_TEAM4)
604 head = find(head, classname, teament_name);
607 if(cvar("bot_vs_human") > 0)
610 if(clienttype(for_whom) == CLIENTTYPE_BOT)
615 else if(cvar("bot_vs_human") < 0)
618 if(clienttype(for_whom) == CLIENTTYPE_BOT)
625 float PlayerValue(entity p)
627 if(IsTeamBalanceForced() == 1)
632 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
633 // teams that are allowed will now have their player counts stored in c1...c4
634 void GetTeamCounts(entity ignore)
638 // now count how many players are on each team already
640 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
641 // also remember the lowest-scoring player
643 FOR_EACH_PLAYER(head)
645 if(head != ignore)// && head.netname != "")
647 value = PlayerValue(head);
648 if(clienttype(head) == CLIENTTYPE_BOT)
652 if(head.team == COLOR_TEAM1)
660 if(head.team == COLOR_TEAM2)
668 if(head.team == COLOR_TEAM3)
676 if(head.team == COLOR_TEAM4)
688 // returns # of smallest team (1, 2, 3, 4)
689 // NOTE: Assumes CheckAllowedTeams has already been called!
690 float FindSmallestTeam(entity pl, float ignore_pl)
692 float totalteams, smallestteam, smallestteam_count, smallestteam_score, balance_type;
695 // find out what teams are available
696 //CheckAllowedTeams();
698 // make sure there are at least 2 teams to join
700 totalteams = totalteams + 1;
702 totalteams = totalteams + 1;
704 totalteams = totalteams + 1;
706 totalteams = totalteams + 1;
708 if(cvar("bot_vs_human"))
714 error("Too few teams available for domination\n");
716 error("Too few teams available for ctf\n");
718 error("Too few teams available for key hunt\n");
720 error("Too few teams available for team deathmatch\n");
724 // count how many players are in each team
728 GetTeamCounts(world);
730 // c1...c4 now have counts of each team
731 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
734 smallestteam_count = 999999999;
735 smallestteam_score = 999999999;
737 // 2 gives priority to what team you're already on, 1 goes in order
738 // 2 doesn't seem to work though...
742 //if(pl.classname != "player")
743 if(clienttype(pl) != CLIENTTYPE_BOT)
745 c1 -= cb1 * 255.0/256;
746 c2 -= cb2 * 255.0/256;
747 c3 -= cb3 * 255.0/256;
748 c4 -= cb4 * 255.0/256;
751 if(balance_type == 1)
753 if(c1 >= 0 && (c1 < smallestteam_count || (c1 <= smallestteam_count && team1_score < smallestteam_score)))
756 smallestteam_count = c1;
757 smallestteam_score = team1_score;
759 if(c2 >= 0 && (c2 < smallestteam_count || (c2 <= smallestteam_count && team2_score < smallestteam_score)))
762 smallestteam_count = c2;
763 smallestteam_score = team2_score;
765 if(c3 >= 0 && (c3 < smallestteam_count || (c3 <= smallestteam_count && team3_score < smallestteam_score)))
768 smallestteam_count = c3;
769 smallestteam_score = team3_score;
771 if(c4 >= 0 && (c4 < smallestteam_count || (c4 <= smallestteam_count && team4_score < smallestteam_score)))
774 smallestteam_count = c4;
775 smallestteam_score = team4_score;
780 if(c1 >= 0 && (c1 < smallestteam_count ||
781 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
784 smallestteam_count = c1;
786 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
787 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
790 smallestteam_count = c2;
792 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
793 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
796 smallestteam_count = c3;
798 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
799 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
802 smallestteam_count = c4;
809 float JoinBestTeam(entity pl, float only_return_best)
811 float smallest, selectedteam;
813 // don't join a team if we're not playing a team game
814 if(!cvar("teamplay") && !g_domination && !g_ctf && !g_keyhunt)
817 // find out what teams are available
818 CheckAllowedTeams(pl);
822 if(cvar("g_domination_default_teams") < 3)
824 if(cvar("g_domination_default_teams") < 4)
828 // if we don't care what team he ends up on, put him on whatever team he entered as.
829 // if he's not on a valid team, then let other code put him on the smallest team
830 if(!cvar("g_campaign") && !cvar("g_balance_teams") && !cvar("g_balance_teams_force"))
832 if( c1 >= 0 && pl.team == COLOR_TEAM1)
833 selectedteam = pl.team;
834 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
835 selectedteam = pl.team;
836 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
837 selectedteam = pl.team;
838 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
839 selectedteam = pl.team;
844 if(!only_return_best)
846 SetPlayerColors(pl, selectedteam - 1);
851 // otherwise end up on the smallest team (handled below)
854 smallest = FindSmallestTeam(pl, TRUE);
857 if(!only_return_best)
859 TeamchangeFrags(self);
862 SetPlayerColors(pl, COLOR_TEAM1 - 1);
864 else if(smallest == 2)
866 SetPlayerColors(pl, COLOR_TEAM2 - 1);
868 else if(smallest == 3)
870 SetPlayerColors(pl, COLOR_TEAM3 - 1);
872 else if(smallest == 4)
874 SetPlayerColors(pl, COLOR_TEAM4 - 1);
878 error("smallest team: invalid team\n");
881 if(pl.deadflag == DEAD_NO)
882 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
889 void SV_ChangeTeam(float _color)
891 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
893 // in normal deathmatch we can just apply the color and we're done
894 if(!cvar("teamplay")) {
895 SetPlayerColors(self, _color);
899 scolor = self.clientcolors & 0x0F;
900 dcolor = _color & 0x0F;
902 if(scolor == COLOR_TEAM1 - 1)
904 else if(scolor == COLOR_TEAM2 - 1)
906 else if(scolor == COLOR_TEAM3 - 1)
908 else if(scolor == COLOR_TEAM4 - 1)
910 if(dcolor == COLOR_TEAM1 - 1)
912 else if(dcolor == COLOR_TEAM2 - 1)
914 else if(dcolor == COLOR_TEAM3 - 1)
916 else if(dcolor == COLOR_TEAM4 - 1)
919 CheckAllowedTeams(self);
921 if(dteam == 1 && c1 < 0) dteam = 4;
922 if(dteam == 4 && c4 < 0) dteam = 3;
923 if(dteam == 3 && c3 < 0) dteam = 2;
924 if(dteam == 2 && c2 < 0) dteam = 1;
926 // not changing teams
929 //bprint("same team change\n");
930 SetPlayerTeam(self, dteam, steam, TRUE);
936 if(cvar("g_campaign") || cvar("g_changeteam_banned"))
938 sprint(self, "Team changes not allowed\n");
939 return; // changing teams is not allowed
942 if(!cvar("g_campaign") && cvar("g_balance_teams_prevent_imbalance"))
944 // only allow changing to a smaller or equal size team
946 // find out what teams are available
947 //CheckAllowedTeams();
948 // count how many players on each team
949 GetTeamCounts(world);
952 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
957 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
962 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
967 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
974 sprint(self, "Cannot change to an invalid team\n");
980 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
982 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
984 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
986 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
989 if(scount) // started at a valid, nonempty team
991 // check if we're trying to change to a larger team that doens't have bots to swap with
992 if(dcount >= scount && dbotcount <= 0)
994 sprint(self, "Cannot change to a larger team\n");
995 return; // can't change to a larger team
1001 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
1003 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1005 // reduce frags during a team change
1006 TeamchangeFrags(self);
1009 SetPlayerTeam(self, dteam, steam, FALSE);
1011 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1013 // kill player when changing teams
1014 if(self.deadflag == DEAD_NO)
1015 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
1019 void ShufflePlayerOutOfTeam (float source_team)
1021 float smallestteam, smallestteam_count, steam;
1022 float lowest_bot_score, lowest_player_score;
1023 entity head, lowest_bot, lowest_player, selected;
1026 smallestteam_count = 999999999;
1028 if(c1 >= 0 && c1 < smallestteam_count)
1031 smallestteam_count = c1;
1033 if(c2 >= 0 && c2 < smallestteam_count)
1036 smallestteam_count = c2;
1038 if(c3 >= 0 && c3 < smallestteam_count)
1041 smallestteam_count = c3;
1043 if(c4 >= 0 && c4 < smallestteam_count)
1046 smallestteam_count = c4;
1051 bprint("warning: no smallest team\n");
1055 if(source_team == 1)
1056 steam = COLOR_TEAM1;
1057 else if(source_team == 2)
1058 steam = COLOR_TEAM2;
1059 else if(source_team == 3)
1060 steam = COLOR_TEAM3;
1061 else if(source_team == 4)
1062 steam = COLOR_TEAM4;
1065 lowest_bot_score = 999999999;
1066 lowest_player = world;
1067 lowest_player_score = 999999999;
1069 // find the lowest-scoring player & bot of that team
1070 FOR_EACH_PLAYER(head)
1072 if(head.team == steam)
1076 if(head.frags < lowest_bot_score)
1079 lowest_bot_score = head.frags;
1084 if(head.frags < lowest_player_score)
1086 lowest_player = head;
1087 lowest_player_score = head.frags;
1093 // prefers to move a bot...
1094 if(lowest_bot != world)
1095 selected = lowest_bot;
1096 // but it will move a player if it has to
1098 selected = lowest_player;
1099 // don't do anything if it couldn't find anyone
1102 bprint("warning: couldn't find a player to move from team\n");
1106 // smallest team gains a member
1107 if(smallestteam == 1)
1111 else if(smallestteam == 2)
1115 else if(smallestteam == 3)
1119 else if(smallestteam == 4)
1125 bprint("warning: destination team invalid\n");
1128 // source team loses a member
1129 if(source_team == 1)
1133 else if(source_team == 2)
1137 else if(source_team == 3)
1141 else if(source_team == 4)
1147 bprint("warning: source team invalid\n");
1151 // move the player to the new team
1152 TeamchangeFrags(selected);
1153 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1155 if(selected.deadflag == DEAD_NO)
1156 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1157 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1160 float lastRebalanceInfo;
1161 void CauseRebalance(float source_team, float howmany_toomany)
1166 if(IsTeamBalanceForced() == 1)
1168 bprint("Rebalancing Teams\n");
1169 ShufflePlayerOutOfTeam(source_team);
1173 if(howmany_toomany < cvar("g_balance_teams_complain"))
1175 if(time < lastRebalanceInfo + 90)
1177 lastRebalanceInfo = time;
1178 if(source_team == 1)
1179 steam = COLOR_TEAM1;
1180 else if(source_team == 2)
1181 steam = COLOR_TEAM2;
1182 else if(source_team == 3)
1183 steam = COLOR_TEAM3;
1184 else if(source_team == 4)
1185 steam = COLOR_TEAM4;
1186 ServerConsoleEcho(strcat("Team ", ftos(source_team), " too large, complaining."), TRUE);
1187 FOR_EACH_REALPLAYER(head)
1189 if(head.team == steam)
1191 sprint(head, "\{1}\{13}^3SERVER NOTICE:^7 One of you please change teams!\n");
1192 centerprint_atprio(head, CENTERPRIO_REBALANCE, "^3SERVER NOTICE:\n\n^7Someone of you please change teams!");
1198 // part of g_balance_teams_force
1199 // occasionally perform an audit of the teams to make
1200 // sure they're more or less balanced in player count.
1203 float numplayers, numteams, smallest, toomany;
1205 balance = IsTeamBalanceForced();
1209 if(audit_teams_time > time)
1212 audit_teams_time = time + 4 + random();
1214 // bprint("Auditing teams\n");
1216 CheckAllowedTeams(world);
1217 GetTeamCounts(world);
1220 numteams = numplayers = smallest = 0;
1223 numteams = numteams + 1;
1224 numplayers = numplayers + c1;
1229 numteams = numteams + 1;
1230 numplayers = numplayers + c2;
1236 numteams = numteams + 1;
1237 numplayers = numplayers + c3;
1243 numteams = numteams + 1;
1244 numplayers = numplayers + c4;
1250 return; // no players to move around
1252 return; // don't bother shuffling if for some reason there aren't any teams
1254 toomany = smallest + 1;
1256 if(c1 && c1 > toomany)
1257 CauseRebalance(1, c1 - toomany);
1258 if(c2 && c2 > toomany)
1259 CauseRebalance(2, c2 - toomany);
1260 if(c3 && c3 > toomany)
1261 CauseRebalance(3, c3 - toomany);
1262 if(c4 && c4 > toomany)
1263 CauseRebalance(4, c4 - toomany);
1265 // if teams are still unbalanced, balance them further in the next audit,
1266 // which will happen sooner (keep doing rapid audits until things are in order)
1267 audit_teams_time = time + 0.7 + random()*0.3;
1272 /*void(entity e, float first) UpdateTeamScore =
1277 WriteByte (MSG_ALL, SVC_UPDATENAME);
1278 WriteByte (MSG_ALL, clientno);
1279 WriteString (MSG_ALL, e.netname);
1281 WriteByte (MSG_ALL, SVC_UPDATECOLORS);
1282 WriteByte (MSG_ALL, clientno);
1283 WriteByte (MSG_ALL, e.b_shirt * 16 + who.b_pants);
1286 WriteByte (MSG_ALL, SVC_UPDATEFRAGS);
1287 WriteByte (MSG_ALL, clientno);
1288 WriteShort (MSG_ALL, e.frags + 10000);
1294 // code from here on is just to support maps that don't have team entities
1295 void tdm_spawnteam (string teamname, float teamcolor)
1299 e.classname = "tdm_team";
1300 e.netname = teamname;
1305 // spawn some default teams if the map is not set up for tdm
1306 void() tdm_spawnteams =
1310 numteams = cvar("g_tdm_teams");
1312 tdm_spawnteam("Red", COLOR_TEAM1-1);
1313 tdm_spawnteam("Blue", COLOR_TEAM2-1);
1315 tdm_spawnteam("Yellow", COLOR_TEAM3-1);
1317 tdm_spawnteam("Pink", COLOR_TEAM4-1);
1320 void() tdm_delayedinit =
1322 self.think = SUB_Remove;
1323 self.nextthink = time;
1324 // if no teams are found, spawn defaults
1325 if (find(world, classname, "tdm_team") == world)
1333 e.think = tdm_delayedinit;
1334 e.nextthink = time + 0.1;