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("teamplay", "0");
103 void default_delayedinit()
107 if(!scores_initialized)
108 ScoreRules_generic();
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, capturelimit_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");
138 capturelimit_override = cvar("capturelimit_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");
162 //no capture limit override here
164 gamemode_name = "Capture the Flag";
167 else if(game == GAME_RUNEMATCH || cvar("g_runematch"))
169 game = GAME_RUNEMATCH;
170 cvar_set("g_runematch", "1");
171 cvar_set("g_minstagib", "0");
173 if(cvar("deathmatch_force_teamplay"))
176 fraglimit_override = cvar("g_runematch_point_limit");
178 gamemode_name = "Rune Match";
184 else if(game == GAME_DEATHMATCH || game == GAME_TEAM_DEATHMATCH || cvar("g_tdm"))
186 if(!cvar("deathmatch"))
187 cvar_set("deathmatch", "1");
190 if(game == GAME_TEAM_DEATHMATCH || cvar("g_tdm") || cvar("deathmatch_force_teamplay"))
193 game = GAME_TEAM_DEATHMATCH;
194 gamemode_name = "Team Deathmatch";
197 cvar_set("g_tdm", "1");
202 game = GAME_DEATHMATCH;
203 gamemode_name = "Deathmatch";
207 fraglimit_override = cvar("fraglimit_override");
209 else if(game == GAME_ASSAULT || cvar("g_assault"))
213 gamemode_name = "Assault";
216 cvar_set("g_assault", "1");
218 else if(game == GAME_LMS || cvar("g_lms"))
222 cvar_set("g_lms", "1");
223 fraglimit_override = cvar("g_lms_lives_override");
224 if(fraglimit_override == 0)
225 fraglimit_override = -1;
226 gamemode_name = "Last Man Standing";
228 lms_lowest_lives = 9999;
232 else if(game == GAME_ARENA || cvar("g_arena"))
236 cvar_set("g_arena", "1");
237 fraglimit_override = cvar("g_arena_point_limit");
238 maxspawned = cvar("g_arena_maxspawned");
241 arena_roundbased = cvar("g_arena_roundbased");
242 gamemode_name = "Arena";
245 else if(game == GAME_KEYHUNT || cvar("g_keyhunt"))
249 cvar_set("g_keyhunt", "1");
250 fraglimit_override = cvar("g_keyhunt_point_limit");
252 gamemode_name = "Key Hunt";
255 else if(game == GAME_ONSLAUGHT || cvar("g_onslaught"))
258 game = GAME_ONSLAUGHT;
259 cvar_set("g_onslaught", "1");
263 gamemode_name = "Onslaught";
268 // we can only assume...
270 game = GAME_DEATHMATCH;
271 cvar_set("g_dm", "1");
272 gamemode_name = "Deathmatch";
276 // those mutators rule each other out
277 if(cvar("g_minstagib"))
279 cvar_set("g_instagib", "0");
280 cvar_set("g_rocketarena", "0");
282 if(cvar("g_instagib"))
284 cvar_set("g_minstagib", "0");
285 cvar_set("g_rocketarena", "0");
287 if(cvar("g_rocketarena"))
289 cvar_set("g_instagib", "0");
290 cvar_set("g_minstagib", "0");
293 g_domination = cvar("g_domination");
294 g_ctf = cvar("g_ctf");
295 g_lms = cvar("g_lms");
296 g_tdm = cvar("g_tdm");
297 g_runematch = cvar("g_runematch");
298 g_keyhunt = cvar("g_keyhunt");
299 g_onslaught = cvar("g_onslaught");
300 g_assault = cvar("g_assault");
301 g_arena = cvar("g_arena");
303 cache_mutatormsg = strzone("");
304 cache_motd = strzone("");
305 cache_lastmutatormsg = strzone("");
306 cache_lastmotd = strzone("");
309 if(g_ctf_win_mode != 2)
310 fraglimit_override = capturelimit_override;
312 // enforce the server's universal frag/time limits
313 if(!cvar("g_campaign"))
315 if(fraglimit_override >= 0)
316 cvar_set("fraglimit", ftos(fraglimit_override));
317 if(timelimit_override >= 0)
318 cvar_set("timelimit", ftos(timelimit_override));
321 if (game == GAME_DOMINATION)//cvar("g_domination"))
323 else if (game == GAME_CTF)//cvar("g_ctf"))
325 else if (game == GAME_RUNEMATCH)//cvar("g_runematch"))
327 else if (game == GAME_TEAM_DEATHMATCH)//cvar("g_runematch"))
329 else if (game == GAME_KEYHUNT)//cvar("g_keyhunt"))
331 else if (game == GAME_ONSLAUGHT)
336 e.nextthink = time + 0.3; // MUST be after all other delayed inits!
337 e.think = default_delayedinit;
340 string GetClientVersionMessage(float v) {
341 local string versionmsg;
343 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";
344 // either that or someone wants to be funny
345 } else if (v != cvar("gameversion")) {
346 if(v < cvar("gameversion")) {
347 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
349 versionmsg = "^3This server is using an outdated Nexuiz version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
352 versionmsg = "^2client version and server version are compatible.^8";
358 void PrintWelcomeMessage(entity pl)
360 string s, mutator, modifications, padding;
363 /*if(self.welcomemessage_time > time)
365 self.welcomemessage_time = time + 0.8; */
367 if(self.cvar_scr_centertime == 0) return;
368 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
369 if(self.welcomemessage_time > time) return;
370 self.welcomemessage_time = time + self.cvar_scr_centertime * 0.6;
373 if(cvar("g_campaign"))
375 centerprint(pl, campaign_message);
379 if(!self.BUTTON_INFO)
381 if(self.classname == "observer")
385 p = PlayerScore_Add(self, SP_LMS_RANK, 0);
387 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"));
389 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"));
392 else if(self.classname == "spectator")
396 p = PlayerScore_Add(self, SP_LMS_RANK, 0);
398 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"));
401 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"));
403 local string specString;
404 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");
406 if(time < restart_countdown) //also show the countdown when being a spectator
407 specString = strcat(specString, "\n\n^1Game starts in ", ftos(restartAnnouncer.cnt + 1), " seconds^7");
408 else if (timeoutStatus != 0)
409 specString = strcat(specString, "\n\n", getTimeoutText(1));
410 return centerprint_atprio(self, CENTERPRIO_SPAM, specString);
415 mutator = "^2Minstagib ^1";
417 mutator = "^2Instagib ^1";
418 else if(g_rocketarena)
419 mutator = "^2Rocketarena ^1";
421 mutator = "^2No Items Nexuiz ^1";
424 // to protect against unheedingly made changes
426 modifications = strcat(modifications, ", ");
428 modifications = "midair";
432 modifications = strcat(modifications, ", ");
434 modifications = strcat(modifications, "vampire");
436 if(g_laserguided_missile) {
438 modifications = strcat(modifications, ", ");
440 modifications = strcat(modifications, "laser-guided-missiles");
444 modifications = strcat(modifications, ", ");
446 modifications = strcat(modifications, "Tournament");
449 local string versionmessage;
450 versionmessage = GetClientVersionMessage(self.version);
452 s = strcat(s, NEWLINES, "This is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
453 s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
455 if(modifications != "")
456 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
459 if(!tourneyInMatchStage)
460 s = strcat(s, "\n^8The game is currently in ^3warmup-stage ^8!\n");
462 s = strcat(s, "\n^8The game is currently in ^3match-stage ^8!\n");
465 if(time < restart_countdown)
466 s = strcat(s, "\n^1Game starts in ", ftos(restartAnnouncer.cnt + 1), " seconds^7");
468 if(timeoutStatus != 0)
469 s = strcat(s, "\n\n", getTimeoutText(1));
471 if (g_grappling_hook)
472 s = strcat(s, "\n\n^8grappling hook is enabled, press 'e' to use it\n");
474 if(cache_lastmutatormsg != cvar_string("g_mutatormsg"))
476 if(cache_lastmutatormsg)
477 strunzone(cache_lastmutatormsg);
479 strunzone(cache_mutatormsg);
480 cache_lastmutatormsg = strzone(cvar_string("g_mutatormsg"));
481 cache_mutatormsg = strzone(wordwrap(cache_lastmutatormsg, 50));
484 if (cache_mutatormsg != "") {
485 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
488 if(cache_lastmotd != cvar_string("sv_motd"))
491 strunzone(cache_lastmotd);
493 strunzone(cache_motd);
494 cache_lastmotd = strzone(cvar_string("sv_motd"));
495 cache_motd = strzone(wordwrap(cache_lastmotd, 50));
498 if (cache_motd != "") {
499 s = strcat(s, "\n\n^8MOTD: ^7", cache_motd);
508 void SetPlayerColors(entity pl, float _color)
512 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
514 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
515 pl.clientcolors = 16*cl + cl;*/
518 pants = _color & 0x0F;
519 shirt = _color & 0xF0;
523 setcolor(pl, 16*pants + pants);
525 setcolor(pl, shirt + pants);
529 void SetPlayerTeam(entity pl, float t, float s, float noprint)
534 _color = COLOR_TEAM4 - 1;
536 _color = COLOR_TEAM3 - 1;
538 _color = COLOR_TEAM2 - 1;
540 _color = COLOR_TEAM1 - 1;
542 SetPlayerColors(pl,_color);
544 if(!noprint && t != s)
546 //bprint(pl.netname, " has changed to ", TeamNoName(t), "\n");
547 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
559 // set c1...c4 to show what teams are allowed
560 void CheckAllowedTeams (entity for_whom)
569 c1 = c2 = c3 = c4 = -1;
570 cb1 = cb2 = cb3 = cb4 = 0;
572 // onslaught is special
575 head = findchain(classname, "onslaught_generator");
578 if (head.team == COLOR_TEAM1) c1 = 0;
579 if (head.team == COLOR_TEAM2) c2 = 0;
580 if (head.team == COLOR_TEAM3) c3 = 0;
581 if (head.team == COLOR_TEAM4) c4 = 0;
588 teament_name = "dom_team";
590 teament_name = "ctf_team";
592 teament_name = "tdm_team";
595 c1 = c2 = 0; // Assault always has 2 teams
600 // cover anything else by treating it like tdm with no teams spawned
604 dm = cvar("g_tdm_teams");
606 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
610 c1 = c2 = c3 = c4 = 0;
623 // first find out what teams are allowed
624 head = find(world, classname, teament_name);
627 if(!(g_domination && head.netname == ""))
629 if(head.team == COLOR_TEAM1)
633 if(head.team == COLOR_TEAM2)
637 if(head.team == COLOR_TEAM3)
641 if(head.team == COLOR_TEAM4)
646 head = find(head, classname, teament_name);
651 if(cvar("bot_vs_human") > 0)
654 if(clienttype(for_whom) == CLIENTTYPE_BOT)
659 else if(cvar("bot_vs_human") < 0)
662 if(clienttype(for_whom) == CLIENTTYPE_BOT)
670 float PlayerValue(entity p)
672 if(IsTeamBalanceForced() == 1)
677 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
678 // teams that are allowed will now have their player counts stored in c1...c4
679 void GetTeamCounts(entity ignore)
683 // now count how many players are on each team already
685 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
686 // also remember the lowest-scoring player
688 FOR_EACH_PLAYER(head)
690 if(head != ignore)// && head.netname != "")
692 value = PlayerValue(head);
693 if(clienttype(head) == CLIENTTYPE_BOT)
697 if(head.team == COLOR_TEAM1)
705 if(head.team == COLOR_TEAM2)
713 if(head.team == COLOR_TEAM3)
721 if(head.team == COLOR_TEAM4)
733 // returns # of smallest team (1, 2, 3, 4)
734 // NOTE: Assumes CheckAllowedTeams has already been called!
735 float FindSmallestTeam(entity pl, float ignore_pl)
737 float totalteams, smallestteam, smallestteam_count, smallestteam_score, balance_type;
740 // find out what teams are available
741 //CheckAllowedTeams();
743 // make sure there are at least 2 teams to join
745 totalteams = totalteams + 1;
747 totalteams = totalteams + 1;
749 totalteams = totalteams + 1;
751 totalteams = totalteams + 1;
753 if(cvar("bot_vs_human"))
759 error("Too few teams available for domination\n");
761 error("Too few teams available for ctf\n");
763 error("Too few teams available for key hunt\n");
765 error("Too few teams available for team deathmatch\n");
768 // count how many players are in each team
772 GetTeamCounts(world);
774 // c1...c4 now have counts of each team
775 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
778 smallestteam_count = 999999999;
779 smallestteam_score = 999999999;
781 // 2 gives priority to what team you're already on, 1 goes in order
782 // 2 doesn't seem to work though...
786 //if(pl.classname != "player")
787 if(clienttype(pl) != CLIENTTYPE_BOT)
789 c1 -= cb1 * 255.0/256;
790 c2 -= cb2 * 255.0/256;
791 c3 -= cb3 * 255.0/256;
792 c4 -= cb4 * 255.0/256;
795 if(balance_type == 1)
797 if(c1 >= 0 && (c1 < smallestteam_count || (c1 <= smallestteam_count && team1_score < smallestteam_score)))
800 smallestteam_count = c1;
801 smallestteam_score = team1_score;
803 if(c2 >= 0 && (c2 < smallestteam_count || (c2 <= smallestteam_count && team2_score < smallestteam_score)))
806 smallestteam_count = c2;
807 smallestteam_score = team2_score;
809 if(c3 >= 0 && (c3 < smallestteam_count || (c3 <= smallestteam_count && team3_score < smallestteam_score)))
812 smallestteam_count = c3;
813 smallestteam_score = team3_score;
815 if(c4 >= 0 && (c4 < smallestteam_count || (c4 <= smallestteam_count && team4_score < smallestteam_score)))
818 smallestteam_count = c4;
819 smallestteam_score = team4_score;
824 if(c1 >= 0 && (c1 < smallestteam_count ||
825 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
828 smallestteam_count = c1;
830 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
831 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
834 smallestteam_count = c2;
836 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
837 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
840 smallestteam_count = c3;
842 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
843 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
846 smallestteam_count = c4;
853 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
855 float smallest, selectedteam;
857 // don't join a team if we're not playing a team game
858 if(!cvar("teamplay") && !g_domination && !g_ctf && !g_keyhunt)
861 // find out what teams are available
862 CheckAllowedTeams(pl);
867 if(cvar("g_domination_default_teams") < 3)
869 if(cvar("g_domination_default_teams") < 4)
873 // if we don't care what team he ends up on, put him on whatever team he entered as.
874 // if he's not on a valid team, then let other code put him on the smallest team
877 if( c1 >= 0 && pl.team == COLOR_TEAM1)
878 selectedteam = pl.team;
879 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
880 selectedteam = pl.team;
881 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
882 selectedteam = pl.team;
883 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
884 selectedteam = pl.team;
889 if(!only_return_best)
891 SetPlayerColors(pl, selectedteam - 1);
896 // otherwise end up on the smallest team (handled below)
899 smallest = FindSmallestTeam(pl, TRUE);
902 if(!only_return_best)
904 TeamchangeFrags(self);
907 SetPlayerColors(pl, COLOR_TEAM1 - 1);
909 else if(smallest == 2)
911 SetPlayerColors(pl, COLOR_TEAM2 - 1);
913 else if(smallest == 3)
915 SetPlayerColors(pl, COLOR_TEAM3 - 1);
917 else if(smallest == 4)
919 SetPlayerColors(pl, COLOR_TEAM4 - 1);
923 error("smallest team: invalid team\n");
926 if(pl.deadflag == DEAD_NO)
927 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
933 //void() ctf_playerchanged;
934 void SV_ChangeTeam(float _color)
936 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
938 // in normal deathmatch we can just apply the color and we're done
939 if(!cvar("teamplay")) {
940 SetPlayerColors(self, _color);
944 scolor = self.clientcolors & 0x0F;
945 dcolor = _color & 0x0F;
947 if(scolor == COLOR_TEAM1 - 1)
949 else if(scolor == COLOR_TEAM2 - 1)
951 else if(scolor == COLOR_TEAM3 - 1)
953 else if(scolor == COLOR_TEAM4 - 1)
955 if(dcolor == COLOR_TEAM1 - 1)
957 else if(dcolor == COLOR_TEAM2 - 1)
959 else if(dcolor == COLOR_TEAM3 - 1)
961 else if(dcolor == COLOR_TEAM4 - 1)
964 CheckAllowedTeams(self);
966 if(dteam == 1 && c1 < 0) dteam = 4;
967 if(dteam == 4 && c4 < 0) dteam = 3;
968 if(dteam == 3 && c3 < 0) dteam = 2;
969 if(dteam == 2 && c2 < 0) dteam = 1;
971 // not changing teams
974 //bprint("same team change\n");
975 SetPlayerTeam(self, dteam, steam, TRUE);
981 if(cvar("g_campaign") || cvar("g_changeteam_banned"))
983 sprint(self, "Team changes not allowed\n");
984 return; // changing teams is not allowed
987 if(!cvar("g_campaign") && cvar("g_balance_teams_prevent_imbalance"))
989 // only allow changing to a smaller or equal size team
991 // find out what teams are available
992 //CheckAllowedTeams();
993 // count how many players on each team
994 GetTeamCounts(world);
997 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
1002 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
1007 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
1012 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
1019 sprint(self, "Cannot change to an invalid team\n");
1024 // get starting team
1025 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
1027 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
1029 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
1031 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
1034 if(scount) // started at a valid, nonempty team
1036 // check if we're trying to change to a larger team that doens't have bots to swap with
1037 if(dcount >= scount && dbotcount <= 0)
1039 sprint(self, "Cannot change to a larger team\n");
1040 return; // can't change to a larger team
1046 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
1048 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1050 // reduce frags during a team change
1051 TeamchangeFrags(self);
1054 SetPlayerTeam(self, dteam, steam, FALSE);
1056 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1058 // kill player when changing teams
1059 if(self.deadflag == DEAD_NO)
1060 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
1062 //ctf_playerchanged();
1065 void ShufflePlayerOutOfTeam (float source_team)
1067 float smallestteam, smallestteam_count, steam;
1068 float lowest_bot_score, lowest_player_score;
1069 entity head, lowest_bot, lowest_player, selected;
1072 smallestteam_count = 999999999;
1074 if(c1 >= 0 && c1 < smallestteam_count)
1077 smallestteam_count = c1;
1079 if(c2 >= 0 && c2 < smallestteam_count)
1082 smallestteam_count = c2;
1084 if(c3 >= 0 && c3 < smallestteam_count)
1087 smallestteam_count = c3;
1089 if(c4 >= 0 && c4 < smallestteam_count)
1092 smallestteam_count = c4;
1097 bprint("warning: no smallest team\n");
1101 if(source_team == 1)
1102 steam = COLOR_TEAM1;
1103 else if(source_team == 2)
1104 steam = COLOR_TEAM2;
1105 else if(source_team == 3)
1106 steam = COLOR_TEAM3;
1107 else if(source_team == 4)
1108 steam = COLOR_TEAM4;
1111 lowest_bot_score = 999999999;
1112 lowest_player = world;
1113 lowest_player_score = 999999999;
1115 // find the lowest-scoring player & bot of that team
1116 FOR_EACH_PLAYER(head)
1118 if(head.team == steam)
1122 if(head.totalfrags < lowest_bot_score)
1125 lowest_bot_score = head.totalfrags;
1130 if(head.totalfrags < lowest_player_score)
1132 lowest_player = head;
1133 lowest_player_score = head.totalfrags;
1139 // prefers to move a bot...
1140 if(lowest_bot != world)
1141 selected = lowest_bot;
1142 // but it will move a player if it has to
1144 selected = lowest_player;
1145 // don't do anything if it couldn't find anyone
1148 bprint("warning: couldn't find a player to move from team\n");
1152 // smallest team gains a member
1153 if(smallestteam == 1)
1157 else if(smallestteam == 2)
1161 else if(smallestteam == 3)
1165 else if(smallestteam == 4)
1171 bprint("warning: destination team invalid\n");
1174 // source team loses a member
1175 if(source_team == 1)
1179 else if(source_team == 2)
1183 else if(source_team == 3)
1187 else if(source_team == 4)
1193 bprint("warning: source team invalid\n");
1197 // move the player to the new team
1198 TeamchangeFrags(selected);
1199 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1201 if(selected.deadflag == DEAD_NO)
1202 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1203 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1206 float lastRebalanceInfo;
1207 void CauseRebalance(float source_team, float howmany_toomany)
1212 if(IsTeamBalanceForced() == 1)
1214 bprint("Rebalancing Teams\n");
1215 ShufflePlayerOutOfTeam(source_team);
1219 if(howmany_toomany < cvar("g_balance_teams_complain"))
1221 if(time < lastRebalanceInfo + 90)
1223 lastRebalanceInfo = time;
1224 if(source_team == 1)
1225 steam = COLOR_TEAM1;
1226 else if(source_team == 2)
1227 steam = COLOR_TEAM2;
1228 else if(source_team == 3)
1229 steam = COLOR_TEAM3;
1230 else if(source_team == 4)
1231 steam = COLOR_TEAM4;
1232 ServerConsoleEcho(strcat("Team ", ftos(source_team), " too large, complaining."), TRUE);
1233 FOR_EACH_REALPLAYER(head)
1235 if(head.team == steam)
1237 sprint(head, "\{1}\{13}^3SERVER NOTICE:^7 One of you please change teams!\n");
1238 centerprint_atprio(head, CENTERPRIO_REBALANCE, "^3SERVER NOTICE:\n\n^7Someone of you please change teams!");
1244 // part of g_balance_teams_force
1245 // occasionally perform an audit of the teams to make
1246 // sure they're more or less balanced in player count.
1249 float numplayers, numteams, smallest, toomany;
1251 balance = IsTeamBalanceForced();
1255 if(audit_teams_time > time)
1258 audit_teams_time = time + 4 + random();
1260 // bprint("Auditing teams\n");
1262 CheckAllowedTeams(world);
1263 GetTeamCounts(world);
1266 numteams = numplayers = smallest = 0;
1269 numteams = numteams + 1;
1270 numplayers = numplayers + c1;
1275 numteams = numteams + 1;
1276 numplayers = numplayers + c2;
1282 numteams = numteams + 1;
1283 numplayers = numplayers + c3;
1289 numteams = numteams + 1;
1290 numplayers = numplayers + c4;
1296 return; // no players to move around
1298 return; // don't bother shuffling if for some reason there aren't any teams
1300 toomany = smallest + 1;
1302 if(c1 && c1 > toomany)
1303 CauseRebalance(1, c1 - toomany);
1304 if(c2 && c2 > toomany)
1305 CauseRebalance(2, c2 - toomany);
1306 if(c3 && c3 > toomany)
1307 CauseRebalance(3, c3 - toomany);
1308 if(c4 && c4 > toomany)
1309 CauseRebalance(4, c4 - toomany);
1311 // if teams are still unbalanced, balance them further in the next audit,
1312 // which will happen sooner (keep doing rapid audits until things are in order)
1313 audit_teams_time = time + 0.7 + random()*0.3;
1318 // code from here on is just to support maps that don't have team entities
1319 void tdm_spawnteam (string teamname, float teamcolor)
1323 e.classname = "tdm_team";
1324 e.netname = teamname;
1329 // spawn some default teams if the map is not set up for tdm
1330 void tdm_spawnteams()
1334 numteams = cvar("g_tdm_teams");
1336 tdm_spawnteam("Red", COLOR_TEAM1-1);
1337 tdm_spawnteam("Blue", COLOR_TEAM2-1);
1339 tdm_spawnteam("Yellow", COLOR_TEAM3-1);
1341 tdm_spawnteam("Pink", COLOR_TEAM4-1);
1344 void tdm_delayedinit()
1346 self.think = SUB_Remove;
1347 self.nextthink = time;
1348 // if no teams are found, spawn defaults
1349 if (find(world, classname, "tdm_team") == world)
1357 e.think = tdm_delayedinit;
1358 e.nextthink = time + 0.1;