2 string cache_mutatormsg;
4 string cache_lastmutatormsg;
6 // client counts for each team
8 // # of bots on those teams
9 float cb1, cb2, cb3, cb4;
11 float audit_teams_time;
13 float IsTeamBalanceForced()
15 if(intermission_running)
16 return 0; // no rebalancing whatsoever please
19 if(cvar("g_campaign"))
21 if(!cvar("g_balance_teams_force"))
26 void TeamchangeFrags(entity e)
31 vector TeamColor(float teem)
36 return '1 0.0625 0.0625';
38 return '0.0625 0.0625 1';
48 string TeamName(float t)
50 return strcat(Team_ColorName(t), " Team");
52 string ColoredTeamName(float t)
54 return strcat(Team_ColorCode(t), Team_ColorName(t), " Team^7");
56 string TeamNoName(float t)
58 // fixme: Search for team entities and get their .netname's!
67 return "Neutral Team";
72 void runematch_init();
76 void LogTeamchange(entity pl)
79 if(!cvar("sv_eventlog"))
83 str = strcat(":team:", ftos(pl.playerid), ":");
84 str = strcat(str, ftos(pl.team));
85 GameLogEcho(str, FALSE);
90 cvar_set("g_dm", "0");
91 cvar_set("g_tdm", "0");
92 cvar_set("g_domination", "0");
93 cvar_set("g_ctf", "0");
94 cvar_set("g_runematch", "0");
95 cvar_set("g_lms", "0");
96 cvar_set("g_arena", "0");
97 cvar_set("g_keyhunt", "0");
98 cvar_set("g_assault", "0");
99 cvar_set("g_onslaught", "0");
100 cvar_set("g_race", "0");
101 cvar_set("teamplay", "0");
104 void default_delayedinit()
108 if(!scores_initialized)
109 ScoreRules_generic();
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, capturelimit_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");
139 capturelimit_override = cvar("capturelimit_override");
141 if(game == GAME_DOMINATION || cvar("g_domination"))
144 game = GAME_DOMINATION;
145 cvar_set("g_domination", "1");
149 fraglimit_override = cvar("g_domination_point_limit");
151 gamemode_name = "Domination";
154 else if(game == GAME_CTF || cvar("g_ctf"))
158 cvar_set("g_ctf", "1");
162 fraglimit_override = cvar("g_ctf_capture_limit");
163 //no capture limit override here
165 gamemode_name = "Capture the Flag";
168 else if(game == GAME_RUNEMATCH || cvar("g_runematch"))
170 game = GAME_RUNEMATCH;
171 cvar_set("g_runematch", "1");
172 cvar_set("g_minstagib", "0");
174 if(cvar("deathmatch_force_teamplay"))
177 fraglimit_override = cvar("g_runematch_point_limit");
179 gamemode_name = "Rune Match";
185 else if(game == GAME_DEATHMATCH || game == GAME_TEAM_DEATHMATCH || cvar("g_tdm"))
187 if(!cvar("deathmatch"))
188 cvar_set("deathmatch", "1");
191 if(game == GAME_TEAM_DEATHMATCH || cvar("g_tdm") || cvar("deathmatch_force_teamplay"))
194 game = GAME_TEAM_DEATHMATCH;
195 gamemode_name = "Team Deathmatch";
198 cvar_set("g_tdm", "1");
203 game = GAME_DEATHMATCH;
204 gamemode_name = "Deathmatch";
208 fraglimit_override = cvar("fraglimit_override");
210 else if(game == GAME_ASSAULT || cvar("g_assault"))
214 gamemode_name = "Assault";
217 cvar_set("g_assault", "1");
219 else if(game == GAME_LMS || cvar("g_lms"))
223 cvar_set("g_lms", "1");
224 fraglimit_override = cvar("g_lms_lives_override");
225 if(fraglimit_override == 0)
226 fraglimit_override = -1;
227 gamemode_name = "Last Man Standing";
229 lms_lowest_lives = 9999;
233 else if(game == GAME_ARENA || cvar("g_arena"))
237 cvar_set("g_arena", "1");
238 fraglimit_override = cvar("g_arena_point_limit");
239 maxspawned = cvar("g_arena_maxspawned");
242 arena_roundbased = cvar("g_arena_roundbased");
243 gamemode_name = "Arena";
246 else if(game == GAME_KEYHUNT || cvar("g_keyhunt"))
250 cvar_set("g_keyhunt", "1");
251 fraglimit_override = cvar("g_keyhunt_point_limit");
253 gamemode_name = "Key Hunt";
256 else if(game == GAME_ONSLAUGHT || cvar("g_onslaught"))
259 game = GAME_ONSLAUGHT;
260 cvar_set("g_onslaught", "1");
264 gamemode_name = "Onslaught";
267 else if(game == GAME_RACE || cvar("g_race"))
271 cvar_set("g_race", "1");
272 fraglimit_override = cvar("g_race_laps_limit");
273 gamemode_name = "Race";
275 if(cvar("g_race_teams"))
279 race_teams = bound(2, cvar("g_race_teams"), 4);
285 if(cvar("g_race_qualifying"))
286 independent_players = 1;
293 // we can only assume...
295 game = GAME_DEATHMATCH;
296 cvar_set("g_dm", "1");
297 gamemode_name = "Deathmatch";
301 // those mutators rule each other out
302 if(cvar("g_minstagib"))
304 cvar_set("g_instagib", "0");
305 cvar_set("g_rocketarena", "0");
307 if(cvar("g_instagib"))
309 cvar_set("g_minstagib", "0");
310 cvar_set("g_rocketarena", "0");
312 if(cvar("g_rocketarena"))
314 cvar_set("g_instagib", "0");
315 cvar_set("g_minstagib", "0");
318 g_domination = cvar("g_domination");
319 g_ctf = cvar("g_ctf");
320 g_lms = cvar("g_lms");
321 g_tdm = cvar("g_tdm");
322 g_runematch = cvar("g_runematch");
323 g_keyhunt = cvar("g_keyhunt");
324 g_onslaught = cvar("g_onslaught");
325 g_assault = cvar("g_assault");
326 g_race = cvar("g_race");
327 g_arena = cvar("g_arena");
329 cache_mutatormsg = strzone("");
330 cache_motd = strzone("");
331 cache_lastmutatormsg = strzone("");
332 cache_lastmotd = strzone("");
335 if(g_ctf_win_mode != 2)
336 fraglimit_override = capturelimit_override;
338 // enforce the server's universal frag/time limits
339 if(!cvar("g_campaign"))
341 if(fraglimit_override >= 0)
342 cvar_set("fraglimit", ftos(fraglimit_override));
343 if(timelimit_override >= 0)
344 cvar_set("timelimit", ftos(timelimit_override));
347 if (game == GAME_DOMINATION)//cvar("g_domination"))
349 else if (game == GAME_CTF)//cvar("g_ctf"))
351 else if (game == GAME_RUNEMATCH)//cvar("g_runematch"))
353 else if (game == GAME_TEAM_DEATHMATCH)//cvar("g_runematch"))
355 else if (game == GAME_KEYHUNT)//cvar("g_keyhunt"))
357 else if (game == GAME_ONSLAUGHT)
362 e.nextthink = time + 0.3; // MUST be after all other delayed inits!
363 e.think = default_delayedinit;
366 string GetClientVersionMessage() {
367 local string versionmsg;
368 if (self.version_mismatch) {
369 if(self.version < cvar("gameversion")) {
370 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
372 versionmsg = "^3This server is using an outdated Nexuiz version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
375 versionmsg = "^2client version and server version are compatible.^8";
381 void PrintWelcomeMessage(entity pl)
383 string s, mutator, modifications, padding;
386 /*if(self.welcomemessage_time > time)
388 self.welcomemessage_time = time + 0.8; */
390 if(self.cvar_scr_centertime == 0) return;
391 if( !(timeoutStatus >= 1 || (time < restart_countdown) ) ) { //really print the WelcomeMessage to the player every frame when timeout-seconds are shown or the game is restarted, to make sure that the shown number is accurate
392 if(self.welcomemessage_time > time) return;
393 self.welcomemessage_time = time + self.cvar_scr_centertime * 0.6;
396 if(cvar("g_campaign"))
398 centerprint(pl, campaign_message);
402 if(!self.BUTTON_INFO)
404 if(self.classname == "observer")
408 p = PlayerScore_Add(self, SP_LMS_RANK, 0);
410 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"));
412 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"));
415 else if(self.classname == "spectator")
419 p = PlayerScore_Add(self, SP_LMS_RANK, 0);
421 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"));
424 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"));
426 local string specString;
427 specString = 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");
429 if(time < restart_countdown) //also show the countdown when being a spectator
430 specString = strcat(specString, "\n\n^1Game starts in ", ftos(restartAnnouncer.cnt + 1), " seconds^7");
431 else if (timeoutStatus != 0)
432 specString = strcat(specString, "\n\n", getTimeoutText(1));
433 return centerprint_atprio(self, CENTERPRIO_SPAM, specString);
438 mutator = "^2Minstagib ^1";
440 mutator = "^2Instagib ^1";
441 else if(g_rocketarena)
442 mutator = "^2Rocketarena ^1";
444 mutator = "^2No Items Nexuiz ^1";
447 // to protect against unheedingly made changes
449 modifications = strcat(modifications, ", ");
451 modifications = "midair";
455 modifications = strcat(modifications, ", ");
457 modifications = strcat(modifications, "vampire");
459 if(g_laserguided_missile) {
461 modifications = strcat(modifications, ", ");
463 modifications = strcat(modifications, "laser-guided-missiles");
467 modifications = strcat(modifications, ", ");
469 modifications = strcat(modifications, "Tournament");
472 local string versionmessage;
473 versionmessage = GetClientVersionMessage();
475 s = strcat(s, NEWLINES, "This is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
476 s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
478 if(modifications != "")
479 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
482 if(!tourneyInMatchStage)
483 s = strcat(s, "\n^8The game is currently in ^3warmup-stage ^8!\n");
485 s = strcat(s, "\n^8The game is currently in ^3match-stage ^8!\n");
488 if(time < restart_countdown)
489 s = strcat(s, "\n^1Game starts in ", ftos(restartAnnouncer.cnt + 1), " seconds^7");
491 if(timeoutStatus != 0)
492 s = strcat(s, "\n\n", getTimeoutText(1));
494 if (g_grappling_hook)
495 s = strcat(s, "\n\n^8grappling hook is enabled, press 'e' to use it\n");
497 if(cache_lastmutatormsg != cvar_string("g_mutatormsg"))
499 if(cache_lastmutatormsg)
500 strunzone(cache_lastmutatormsg);
502 strunzone(cache_mutatormsg);
503 cache_lastmutatormsg = strzone(cvar_string("g_mutatormsg"));
504 cache_mutatormsg = strzone(wordwrap(cache_lastmutatormsg, 50));
507 if (cache_mutatormsg != "") {
508 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
511 if(cache_lastmotd != cvar_string("sv_motd"))
514 strunzone(cache_lastmotd);
516 strunzone(cache_motd);
517 cache_lastmotd = strzone(cvar_string("sv_motd"));
518 cache_motd = strzone(wordwrap(cache_lastmotd, 50));
521 if (cache_motd != "") {
522 s = strcat(s, "\n\n^8MOTD: ^7", cache_motd);
531 void SetPlayerColors(entity pl, float _color)
535 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
537 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
538 pl.clientcolors = 16*cl + cl;*/
541 pants = _color & 0x0F;
542 shirt = _color & 0xF0;
546 setcolor(pl, 16*pants + pants);
548 setcolor(pl, shirt + pants);
552 void SetPlayerTeam(entity pl, float t, float s, float noprint)
557 _color = COLOR_TEAM4 - 1;
559 _color = COLOR_TEAM3 - 1;
561 _color = COLOR_TEAM2 - 1;
563 _color = COLOR_TEAM1 - 1;
565 SetPlayerColors(pl,_color);
567 if(!noprint && t != s)
569 //bprint(pl.netname, " has changed to ", TeamNoName(t), "\n");
570 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
582 // set c1...c4 to show what teams are allowed
583 void CheckAllowedTeams (entity for_whom)
592 c1 = c2 = c3 = c4 = -1;
593 cb1 = cb2 = cb3 = cb4 = 0;
595 // onslaught is special
598 head = findchain(classname, "onslaught_generator");
601 if (head.team == COLOR_TEAM1) c1 = 0;
602 if (head.team == COLOR_TEAM2) c2 = 0;
603 if (head.team == COLOR_TEAM3) c3 = 0;
604 if (head.team == COLOR_TEAM4) c4 = 0;
611 teament_name = "dom_team";
613 teament_name = "ctf_team";
615 teament_name = "tdm_team";
618 c1 = c2 = 0; // Assault always has 2 teams
623 // cover anything else by treating it like tdm with no teams spawned
629 dm = cvar("g_tdm_teams");
631 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
635 c1 = c2 = c3 = c4 = 0;
648 // first find out what teams are allowed
649 head = find(world, classname, teament_name);
652 if(!(g_domination && head.netname == ""))
654 if(head.team == COLOR_TEAM1)
658 if(head.team == COLOR_TEAM2)
662 if(head.team == COLOR_TEAM3)
666 if(head.team == COLOR_TEAM4)
671 head = find(head, classname, teament_name);
676 if(cvar("bot_vs_human") > 0)
679 if(clienttype(for_whom) == CLIENTTYPE_BOT)
684 else if(cvar("bot_vs_human") < 0)
687 if(clienttype(for_whom) == CLIENTTYPE_BOT)
695 float PlayerValue(entity p)
697 if(IsTeamBalanceForced() == 1)
702 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
703 // teams that are allowed will now have their player counts stored in c1...c4
704 void GetTeamCounts(entity ignore)
708 // now count how many players are on each team already
710 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
711 // also remember the lowest-scoring player
713 FOR_EACH_PLAYER(head)
715 if(head != ignore)// && head.netname != "")
717 value = PlayerValue(head);
718 if(clienttype(head) == CLIENTTYPE_BOT)
722 if(head.team == COLOR_TEAM1)
730 if(head.team == COLOR_TEAM2)
738 if(head.team == COLOR_TEAM3)
746 if(head.team == COLOR_TEAM4)
758 // returns # of smallest team (1, 2, 3, 4)
759 // NOTE: Assumes CheckAllowedTeams has already been called!
760 float FindSmallestTeam(entity pl, float ignore_pl)
762 float totalteams, smallestteam, smallestteam_count, smallestteam_score, balance_type;
765 // find out what teams are available
766 //CheckAllowedTeams();
768 // make sure there are at least 2 teams to join
770 totalteams = totalteams + 1;
772 totalteams = totalteams + 1;
774 totalteams = totalteams + 1;
776 totalteams = totalteams + 1;
778 if(cvar("bot_vs_human"))
784 error("Too few teams available for domination\n");
786 error("Too few teams available for ctf\n");
788 error("Too few teams available for key hunt\n");
790 error("Too few teams available for team deathmatch\n");
793 // count how many players are in each team
797 GetTeamCounts(world);
799 // c1...c4 now have counts of each team
800 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
803 smallestteam_count = 999999999;
804 smallestteam_score = 999999999;
806 // 2 gives priority to what team you're already on, 1 goes in order
807 // 2 doesn't seem to work though...
811 //if(pl.classname != "player")
812 if(clienttype(pl) != CLIENTTYPE_BOT)
814 c1 -= cb1 * 255.0/256;
815 c2 -= cb2 * 255.0/256;
816 c3 -= cb3 * 255.0/256;
817 c4 -= cb4 * 255.0/256;
820 if(balance_type == 1)
822 if(c1 >= 0 && (c1 < smallestteam_count || (c1 <= smallestteam_count && team1_score < smallestteam_score)))
825 smallestteam_count = c1;
826 smallestteam_score = team1_score;
828 if(c2 >= 0 && (c2 < smallestteam_count || (c2 <= smallestteam_count && team2_score < smallestteam_score)))
831 smallestteam_count = c2;
832 smallestteam_score = team2_score;
834 if(c3 >= 0 && (c3 < smallestteam_count || (c3 <= smallestteam_count && team3_score < smallestteam_score)))
837 smallestteam_count = c3;
838 smallestteam_score = team3_score;
840 if(c4 >= 0 && (c4 < smallestteam_count || (c4 <= smallestteam_count && team4_score < smallestteam_score)))
843 smallestteam_count = c4;
844 smallestteam_score = team4_score;
849 if(c1 >= 0 && (c1 < smallestteam_count ||
850 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
853 smallestteam_count = c1;
855 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
856 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
859 smallestteam_count = c2;
861 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
862 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
865 smallestteam_count = c3;
867 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
868 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
871 smallestteam_count = c4;
878 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
880 float smallest, selectedteam;
882 // don't join a team if we're not playing a team game
883 if(!cvar("teamplay") && !g_domination && !g_ctf && !g_keyhunt)
886 // find out what teams are available
887 CheckAllowedTeams(pl);
892 if(cvar("g_domination_default_teams") < 3)
894 if(cvar("g_domination_default_teams") < 4)
898 // if we don't care what team he ends up on, put him on whatever team he entered as.
899 // if he's not on a valid team, then let other code put him on the smallest team
902 if( c1 >= 0 && pl.team == COLOR_TEAM1)
903 selectedteam = pl.team;
904 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
905 selectedteam = pl.team;
906 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
907 selectedteam = pl.team;
908 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
909 selectedteam = pl.team;
914 if(!only_return_best)
916 SetPlayerColors(pl, selectedteam - 1);
921 // otherwise end up on the smallest team (handled below)
924 smallest = FindSmallestTeam(pl, TRUE);
927 if(!only_return_best)
929 TeamchangeFrags(self);
932 SetPlayerColors(pl, COLOR_TEAM1 - 1);
934 else if(smallest == 2)
936 SetPlayerColors(pl, COLOR_TEAM2 - 1);
938 else if(smallest == 3)
940 SetPlayerColors(pl, COLOR_TEAM3 - 1);
942 else if(smallest == 4)
944 SetPlayerColors(pl, COLOR_TEAM4 - 1);
948 error("smallest team: invalid team\n");
951 if(pl.deadflag == DEAD_NO)
952 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
958 //void() ctf_playerchanged;
959 void SV_ChangeTeam(float _color)
961 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
963 // in normal deathmatch we can just apply the color and we're done
964 if(!cvar("teamplay")) {
965 SetPlayerColors(self, _color);
969 scolor = self.clientcolors & 0x0F;
970 dcolor = _color & 0x0F;
972 if(scolor == COLOR_TEAM1 - 1)
974 else if(scolor == COLOR_TEAM2 - 1)
976 else if(scolor == COLOR_TEAM3 - 1)
978 else if(scolor == COLOR_TEAM4 - 1)
980 if(dcolor == COLOR_TEAM1 - 1)
982 else if(dcolor == COLOR_TEAM2 - 1)
984 else if(dcolor == COLOR_TEAM3 - 1)
986 else if(dcolor == COLOR_TEAM4 - 1)
989 CheckAllowedTeams(self);
991 if(dteam == 1 && c1 < 0) dteam = 4;
992 if(dteam == 4 && c4 < 0) dteam = 3;
993 if(dteam == 3 && c3 < 0) dteam = 2;
994 if(dteam == 2 && c2 < 0) dteam = 1;
996 // not changing teams
999 //bprint("same team change\n");
1000 SetPlayerTeam(self, dteam, steam, TRUE);
1004 if(cvar("teamplay"))
1006 if(cvar("g_campaign") || cvar("g_changeteam_banned"))
1008 sprint(self, "Team changes not allowed\n");
1009 return; // changing teams is not allowed
1012 if(!cvar("g_campaign") && cvar("g_balance_teams_prevent_imbalance"))
1014 // only allow changing to a smaller or equal size team
1016 // find out what teams are available
1017 //CheckAllowedTeams();
1018 // count how many players on each team
1019 GetTeamCounts(world);
1022 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
1027 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
1032 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
1037 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
1044 sprint(self, "Cannot change to an invalid team\n");
1049 // get starting team
1050 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
1052 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
1054 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
1056 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
1059 if(scount) // started at a valid, nonempty team
1061 // check if we're trying to change to a larger team that doens't have bots to swap with
1062 if(dcount >= scount && dbotcount <= 0)
1064 sprint(self, "Cannot change to a larger team\n");
1065 return; // can't change to a larger team
1071 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
1073 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1075 // reduce frags during a team change
1076 TeamchangeFrags(self);
1079 SetPlayerTeam(self, dteam, steam, FALSE);
1081 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1083 // kill player when changing teams
1084 if(self.deadflag == DEAD_NO)
1085 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
1087 //ctf_playerchanged();
1090 void ShufflePlayerOutOfTeam (float source_team)
1092 float smallestteam, smallestteam_count, steam;
1093 float lowest_bot_score, lowest_player_score;
1094 entity head, lowest_bot, lowest_player, selected;
1097 smallestteam_count = 999999999;
1099 if(c1 >= 0 && c1 < smallestteam_count)
1102 smallestteam_count = c1;
1104 if(c2 >= 0 && c2 < smallestteam_count)
1107 smallestteam_count = c2;
1109 if(c3 >= 0 && c3 < smallestteam_count)
1112 smallestteam_count = c3;
1114 if(c4 >= 0 && c4 < smallestteam_count)
1117 smallestteam_count = c4;
1122 bprint("warning: no smallest team\n");
1126 if(source_team == 1)
1127 steam = COLOR_TEAM1;
1128 else if(source_team == 2)
1129 steam = COLOR_TEAM2;
1130 else if(source_team == 3)
1131 steam = COLOR_TEAM3;
1132 else if(source_team == 4)
1133 steam = COLOR_TEAM4;
1136 lowest_bot_score = 999999999;
1137 lowest_player = world;
1138 lowest_player_score = 999999999;
1140 // find the lowest-scoring player & bot of that team
1141 FOR_EACH_PLAYER(head)
1143 if(head.team == steam)
1147 if(head.totalfrags < lowest_bot_score)
1150 lowest_bot_score = head.totalfrags;
1155 if(head.totalfrags < lowest_player_score)
1157 lowest_player = head;
1158 lowest_player_score = head.totalfrags;
1164 // prefers to move a bot...
1165 if(lowest_bot != world)
1166 selected = lowest_bot;
1167 // but it will move a player if it has to
1169 selected = lowest_player;
1170 // don't do anything if it couldn't find anyone
1173 bprint("warning: couldn't find a player to move from team\n");
1177 // smallest team gains a member
1178 if(smallestteam == 1)
1182 else if(smallestteam == 2)
1186 else if(smallestteam == 3)
1190 else if(smallestteam == 4)
1196 bprint("warning: destination team invalid\n");
1199 // source team loses a member
1200 if(source_team == 1)
1204 else if(source_team == 2)
1208 else if(source_team == 3)
1212 else if(source_team == 4)
1218 bprint("warning: source team invalid\n");
1222 // move the player to the new team
1223 TeamchangeFrags(selected);
1224 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1226 if(selected.deadflag == DEAD_NO)
1227 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1228 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1231 float lastRebalanceInfo;
1232 void CauseRebalance(float source_team, float howmany_toomany)
1237 if(IsTeamBalanceForced() == 1)
1239 bprint("Rebalancing Teams\n");
1240 ShufflePlayerOutOfTeam(source_team);
1244 if(howmany_toomany < cvar("g_balance_teams_complain"))
1246 if(time < lastRebalanceInfo + 90)
1248 lastRebalanceInfo = time;
1249 if(source_team == 1)
1250 steam = COLOR_TEAM1;
1251 else if(source_team == 2)
1252 steam = COLOR_TEAM2;
1253 else if(source_team == 3)
1254 steam = COLOR_TEAM3;
1255 else if(source_team == 4)
1256 steam = COLOR_TEAM4;
1257 ServerConsoleEcho(strcat("Team ", ftos(source_team), " too large, complaining."), TRUE);
1258 FOR_EACH_REALPLAYER(head)
1260 if(head.team == steam)
1262 sprint(head, "\{1}\{13}^3SERVER NOTICE:^7 One of you please change teams!\n");
1263 centerprint_atprio(head, CENTERPRIO_REBALANCE, "^3SERVER NOTICE:\n\n^7Someone of you please change teams!");
1269 // part of g_balance_teams_force
1270 // occasionally perform an audit of the teams to make
1271 // sure they're more or less balanced in player count.
1274 float numplayers, numteams, smallest, toomany;
1276 balance = IsTeamBalanceForced();
1280 if(audit_teams_time > time)
1283 audit_teams_time = time + 4 + random();
1285 // bprint("Auditing teams\n");
1287 CheckAllowedTeams(world);
1288 GetTeamCounts(world);
1291 numteams = numplayers = smallest = 0;
1294 numteams = numteams + 1;
1295 numplayers = numplayers + c1;
1300 numteams = numteams + 1;
1301 numplayers = numplayers + c2;
1307 numteams = numteams + 1;
1308 numplayers = numplayers + c3;
1314 numteams = numteams + 1;
1315 numplayers = numplayers + c4;
1321 return; // no players to move around
1323 return; // don't bother shuffling if for some reason there aren't any teams
1325 toomany = smallest + 1;
1327 if(c1 && c1 > toomany)
1328 CauseRebalance(1, c1 - toomany);
1329 if(c2 && c2 > toomany)
1330 CauseRebalance(2, c2 - toomany);
1331 if(c3 && c3 > toomany)
1332 CauseRebalance(3, c3 - toomany);
1333 if(c4 && c4 > toomany)
1334 CauseRebalance(4, c4 - toomany);
1336 // if teams are still unbalanced, balance them further in the next audit,
1337 // which will happen sooner (keep doing rapid audits until things are in order)
1338 audit_teams_time = time + 0.7 + random()*0.3;
1343 // code from here on is just to support maps that don't have team entities
1344 void tdm_spawnteam (string teamname, float teamcolor)
1348 e.classname = "tdm_team";
1349 e.netname = teamname;
1354 // spawn some default teams if the map is not set up for tdm
1355 void tdm_spawnteams()
1359 numteams = cvar("g_tdm_teams");
1361 tdm_spawnteam("Red", COLOR_TEAM1-1);
1362 tdm_spawnteam("Blue", COLOR_TEAM2-1);
1364 tdm_spawnteam("Yellow", COLOR_TEAM3-1);
1366 tdm_spawnteam("Pink", COLOR_TEAM4-1);
1369 void tdm_delayedinit()
1371 self.think = SUB_Remove;
1372 self.nextthink = time;
1373 // if no teams are found, spawn defaults
1374 if (find(world, classname, "tdm_team") == world)
1382 e.think = tdm_delayedinit;
1383 e.nextthink = time + 0.1;