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 ActivateTeamplay()
105 float teamplay_default;
106 teamplay_default = cvar("teamplay_default");
109 cvar_set("teamplay", ftos(teamplay_default));
111 cvar_set("teamplay", "3");
114 void InitGameplayMode()
116 float fraglimit_override, timelimit_override, capturelimit_override;
120 game = cvar("gamecfg"); // load game options
122 // game cvars get reset before map changes
123 // then map's cfg sets them as desired
125 // FIXME: also set a message or game mode name to print to players when the join
127 // set both here, gamemode can override it later
128 timelimit_override = cvar("timelimit_override");
129 fraglimit_override = cvar("fraglimit_override");
130 capturelimit_override = cvar("capturelimit_override");
132 if(game == GAME_DOMINATION || cvar("g_domination"))
135 game = GAME_DOMINATION;
136 cvar_set("g_domination", "1");
140 fraglimit_override = cvar("g_domination_point_limit");
142 gamemode_name = "Domination";
145 else if(game == GAME_CTF || cvar("g_ctf"))
149 cvar_set("g_ctf", "1");
153 fraglimit_override = cvar("g_ctf_capture_limit");
154 //no capture limit override here
156 gamemode_name = "Capture the Flag";
159 else if(game == GAME_RUNEMATCH || cvar("g_runematch"))
161 game = GAME_RUNEMATCH;
162 cvar_set("g_runematch", "1");
163 cvar_set("g_minstagib", "0");
165 if(cvar("deathmatch_force_teamplay"))
168 fraglimit_override = cvar("g_runematch_point_limit");
170 gamemode_name = "Rune Match";
176 else if(game == GAME_DEATHMATCH || game == GAME_TEAM_DEATHMATCH || cvar("g_tdm"))
178 if(!cvar("deathmatch"))
179 cvar_set("deathmatch", "1");
182 if(game == GAME_TEAM_DEATHMATCH || cvar("g_tdm") || cvar("deathmatch_force_teamplay"))
185 game = GAME_TEAM_DEATHMATCH;
186 gamemode_name = "Team Deathmatch";
189 cvar_set("g_tdm", "1");
194 game = GAME_DEATHMATCH;
195 gamemode_name = "Deathmatch";
199 fraglimit_override = cvar("fraglimit_override");
201 else if(game == GAME_ASSAULT || cvar("g_assault"))
205 gamemode_name = "Assault";
208 cvar_set("g_assault", "1");
210 else if(game == GAME_LMS || cvar("g_lms"))
214 cvar_set("g_lms", "1");
215 fraglimit_override = cvar("g_lms_lives_override");
216 if(fraglimit_override == 0)
217 fraglimit_override = -1;
218 gamemode_name = "Last Man Standing";
220 lms_lowest_lives = 999;
222 else if(game == GAME_ARENA || cvar("g_arena"))
226 cvar_set("g_arena", "1");
227 fraglimit_override = cvar("g_arena_point_limit");
228 maxspawned = cvar("g_arena_maxspawned");
231 arena_roundbased = cvar("g_arena_roundbased");
232 gamemode_name = "Arena";
235 else if(game == GAME_KEYHUNT || cvar("g_keyhunt"))
239 cvar_set("g_keyhunt", "1");
240 fraglimit_override = cvar("g_keyhunt_point_limit");
242 gamemode_name = "Key Hunt";
245 else if(game == GAME_ONSLAUGHT || cvar("g_onslaught"))
248 game = GAME_ONSLAUGHT;
249 cvar_set("g_onslaught", "1");
253 gamemode_name = "Onslaught";
258 // we can only assume...
260 game = GAME_DEATHMATCH;
261 cvar_set("g_dm", "1");
262 gamemode_name = "Deathmatch";
266 // those mutators rule each other out
267 if(cvar("g_minstagib"))
269 cvar_set("g_instagib", "0");
270 cvar_set("g_rocketarena", "0");
272 if(cvar("g_instagib"))
274 cvar_set("g_minstagib", "0");
275 cvar_set("g_rocketarena", "0");
277 if(cvar("g_rocketarena"))
279 cvar_set("g_instagib", "0");
280 cvar_set("g_minstagib", "0");
283 g_domination = cvar("g_domination");
284 g_ctf = cvar("g_ctf");
285 g_lms = cvar("g_lms");
286 g_tdm = cvar("g_tdm");
287 g_runematch = cvar("g_runematch");
288 g_keyhunt = cvar("g_keyhunt");
289 g_onslaught = cvar("g_onslaught");
290 g_assault = cvar("g_assault");
291 g_arena = cvar("g_arena");
293 cache_mutatormsg = strzone("");
294 cache_motd = strzone("");
295 cache_lastmutatormsg = strzone("");
296 cache_lastmotd = strzone("");
298 // enforce the server's universal frag/time limits
299 if(!cvar("g_campaign"))
301 if(fraglimit_override >= 0)
302 cvar_set("fraglimit", ftos(fraglimit_override));
303 if(timelimit_override >= 0)
304 cvar_set("timelimit", ftos(timelimit_override));
305 if(capturelimit_override >= 0)
306 cvar_set("capturelimit", ftos(capturelimit_override));
309 if (game == GAME_DOMINATION)//cvar("g_domination"))
311 else if (game == GAME_CTF)//cvar("g_ctf"))
313 else if (game == GAME_RUNEMATCH)//cvar("g_runematch"))
315 else if (game == GAME_TEAM_DEATHMATCH)//cvar("g_runematch"))
317 else if (game == GAME_KEYHUNT)//cvar("g_keyhunt"))
319 else if (game == GAME_ONSLAUGHT)
323 string GetClientVersionMessage(float v) {
324 local string versionmsg;
326 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";
327 // either that or someone wants to be funny
328 } else if (v != cvar("gameversion")) {
329 if(v < cvar("gameversion")) {
330 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
332 versionmsg = "^3This server is using an outdated Nexuiz version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
335 versionmsg = "^2client version and server version are compatible.^8";
341 void PrintWelcomeMessage(entity pl)
343 string s, mutator, modifications, padding;
345 /*if(self.welcomemessage_time > time)
347 self.welcomemessage_time = time + 0.8; */
349 if(self.cvar_scr_centertime == 0) return;
350 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
351 if(self.welcomemessage_time > time) return;
352 self.welcomemessage_time = time + self.cvar_scr_centertime * 0.6;
355 if(cvar("g_campaign"))
357 centerprint(pl, campaign_message);
361 if(!self.BUTTON_INFO)
363 if(self.classname == "observer")
365 // FIXME this is broken for LMS, fix it
366 if(g_lms && self.frags <= 0 && self.frags > -666)
367 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"));
368 else if(g_lms && self.frags == -666)
369 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"));
371 else if(self.classname == "spectator")
373 // FIXME this is broken for LMS, fix it
374 if ((g_lms && self.frags < 1) || g_arena)
375 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"));
377 local string specString;
378 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");
380 if(time < restart_countdown) //also show the countdown when being a spectator
381 specString = strcat(specString, "\n\n^1Game starts in ", ftos(restartAnnouncer.cnt + 1), " seconds^7");
382 else if (timeoutStatus != 0)
383 specString = strcat(specString, "\n\n", getTimeoutText(1));
384 return centerprint_atprio(self, CENTERPRIO_SPAM, specString);
390 mutator = "^2Minstagib ^1";
392 mutator = "^2Instagib ^1";
393 else if(g_rocketarena)
394 mutator = "^2Rocketarena ^1";
396 mutator = "^2No Items Nexuiz ^1";
399 // to protect against unheedingly made changes
401 modifications = strcat(modifications, ", ");
403 modifications = "midair";
407 modifications = strcat(modifications, ", ");
409 modifications = strcat(modifications, "vampire");
411 if(g_laserguided_missile) {
413 modifications = strcat(modifications, ", ");
415 modifications = strcat(modifications, "laser-guided-missiles");
419 modifications = strcat(modifications, ", ");
421 modifications = strcat(modifications, "Tournament");
424 local string versionmessage;
425 versionmessage = GetClientVersionMessage(self.version);
427 s = strcat(s, NEWLINES, "This is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
428 s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
430 if(modifications != "")
431 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
434 if(!tourneyInMatchStage)
435 s = strcat(s, "\n^8The game is currently in ^3warmup-stage ^8!\n");
437 s = strcat(s, "\n^8The game is currently in ^3match-stage ^8!\n");
440 if(time < restart_countdown)
441 s = strcat(s, "\n^1Game starts in ", ftos(restartAnnouncer.cnt + 1), " seconds^7");
443 if(timeoutStatus != 0)
444 s = strcat(s, "\n\n", getTimeoutText(1));
446 if (g_grappling_hook)
447 s = strcat(s, "\n\n^8grappling hook is enabled, press 'e' to use it\n");
449 if(cache_lastmutatormsg != cvar_string("g_mutatormsg"))
451 if(cache_lastmutatormsg)
452 strunzone(cache_lastmutatormsg);
454 strunzone(cache_mutatormsg);
455 cache_lastmutatormsg = strzone(cvar_string("g_mutatormsg"));
456 cache_mutatormsg = strzone(wordwrap(cache_lastmutatormsg, 50));
459 if (cache_mutatormsg != "") {
460 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
463 if(cache_lastmotd != cvar_string("sv_motd"))
466 strunzone(cache_lastmotd);
468 strunzone(cache_motd);
469 cache_lastmotd = strzone(cvar_string("sv_motd"));
470 cache_motd = strzone(wordwrap(cache_lastmotd, 50));
473 if (cache_motd != "") {
474 s = strcat(s, "\n\n^8MOTD: ^7", cache_motd);
478 if(cvar("fraglimit"))
481 if(cvar("timelimit"))
484 s = strcat(s, "\n^8frag limit: ^7", cvar_string("fraglimit"), padding);
486 if(cvar("timelimit"))
487 s = strcat(s, "\n^8time limit: ^7", cvar_string("timelimit"), " minutes");
494 void SetPlayerColors(entity pl, float _color)
498 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
500 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
501 pl.clientcolors = 16*cl + cl;*/
504 pants = _color & 0x0F;
505 shirt = _color & 0xF0;
509 setcolor(pl, 16*pants + pants);
511 setcolor(pl, shirt + pants);
515 void SetPlayerTeam(entity pl, float t, float s, float noprint)
520 _color = COLOR_TEAM4 - 1;
522 _color = COLOR_TEAM3 - 1;
524 _color = COLOR_TEAM2 - 1;
526 _color = COLOR_TEAM1 - 1;
528 SetPlayerColors(pl,_color);
530 if(!noprint && t != s)
532 //bprint(pl.netname, " has changed to ", TeamNoName(t), "\n");
533 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
545 // set c1...c4 to show what teams are allowed
546 void CheckAllowedTeams (entity for_whom)
555 c1 = c2 = c3 = c4 = -1;
556 cb1 = cb2 = cb3 = cb4 = 0;
558 // onslaught is special
561 head = findchain(classname, "onslaught_generator");
564 if (head.team == COLOR_TEAM1) c1 = 0;
565 if (head.team == COLOR_TEAM2) c2 = 0;
566 if (head.team == COLOR_TEAM3) c3 = 0;
567 if (head.team == COLOR_TEAM4) c4 = 0;
574 teament_name = "dom_team";
576 teament_name = "ctf_team";
578 teament_name = "tdm_team";
581 c1 = c2 = 0; // Assault always has 2 teams
586 // cover anything else by treating it like tdm with no teams spawned
590 dm = cvar("g_tdm_teams");
592 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
596 c1 = c2 = c3 = c4 = 0;
609 // first find out what teams are allowed
610 head = find(world, classname, teament_name);
613 if(!(g_domination && head.netname == ""))
615 if(head.team == COLOR_TEAM1)
619 if(head.team == COLOR_TEAM2)
623 if(head.team == COLOR_TEAM3)
627 if(head.team == COLOR_TEAM4)
632 head = find(head, classname, teament_name);
635 if(cvar("bot_vs_human") > 0)
638 if(clienttype(for_whom) == CLIENTTYPE_BOT)
643 else if(cvar("bot_vs_human") < 0)
646 if(clienttype(for_whom) == CLIENTTYPE_BOT)
653 float PlayerValue(entity p)
655 if(IsTeamBalanceForced() == 1)
660 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
661 // teams that are allowed will now have their player counts stored in c1...c4
662 void GetTeamCounts(entity ignore)
666 // now count how many players are on each team already
668 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
669 // also remember the lowest-scoring player
671 FOR_EACH_PLAYER(head)
673 if(head != ignore)// && head.netname != "")
675 value = PlayerValue(head);
676 if(clienttype(head) == CLIENTTYPE_BOT)
680 if(head.team == COLOR_TEAM1)
688 if(head.team == COLOR_TEAM2)
696 if(head.team == COLOR_TEAM3)
704 if(head.team == COLOR_TEAM4)
716 // returns # of smallest team (1, 2, 3, 4)
717 // NOTE: Assumes CheckAllowedTeams has already been called!
718 float FindSmallestTeam(entity pl, float ignore_pl)
720 float totalteams, smallestteam, smallestteam_count, smallestteam_score, balance_type;
723 // find out what teams are available
724 //CheckAllowedTeams();
726 // make sure there are at least 2 teams to join
728 totalteams = totalteams + 1;
730 totalteams = totalteams + 1;
732 totalteams = totalteams + 1;
734 totalteams = totalteams + 1;
736 if(cvar("bot_vs_human"))
742 error("Too few teams available for domination\n");
744 error("Too few teams available for ctf\n");
746 error("Too few teams available for key hunt\n");
748 error("Too few teams available for team deathmatch\n");
751 // count how many players are in each team
755 GetTeamCounts(world);
757 // c1...c4 now have counts of each team
758 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
761 smallestteam_count = 999999999;
762 smallestteam_score = 999999999;
764 // 2 gives priority to what team you're already on, 1 goes in order
765 // 2 doesn't seem to work though...
769 //if(pl.classname != "player")
770 if(clienttype(pl) != CLIENTTYPE_BOT)
772 c1 -= cb1 * 255.0/256;
773 c2 -= cb2 * 255.0/256;
774 c3 -= cb3 * 255.0/256;
775 c4 -= cb4 * 255.0/256;
778 if(balance_type == 1)
780 if(c1 >= 0 && (c1 < smallestteam_count || (c1 <= smallestteam_count && team1_score < smallestteam_score)))
783 smallestteam_count = c1;
784 smallestteam_score = team1_score;
786 if(c2 >= 0 && (c2 < smallestteam_count || (c2 <= smallestteam_count && team2_score < smallestteam_score)))
789 smallestteam_count = c2;
790 smallestteam_score = team2_score;
792 if(c3 >= 0 && (c3 < smallestteam_count || (c3 <= smallestteam_count && team3_score < smallestteam_score)))
795 smallestteam_count = c3;
796 smallestteam_score = team3_score;
798 if(c4 >= 0 && (c4 < smallestteam_count || (c4 <= smallestteam_count && team4_score < smallestteam_score)))
801 smallestteam_count = c4;
802 smallestteam_score = team4_score;
807 if(c1 >= 0 && (c1 < smallestteam_count ||
808 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
811 smallestteam_count = c1;
813 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
814 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
817 smallestteam_count = c2;
819 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
820 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
823 smallestteam_count = c3;
825 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
826 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
829 smallestteam_count = c4;
836 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
838 float smallest, selectedteam;
840 // don't join a team if we're not playing a team game
841 if(!cvar("teamplay") && !g_domination && !g_ctf && !g_keyhunt)
844 // find out what teams are available
845 CheckAllowedTeams(pl);
850 if(cvar("g_domination_default_teams") < 3)
852 if(cvar("g_domination_default_teams") < 4)
856 // if we don't care what team he ends up on, put him on whatever team he entered as.
857 // if he's not on a valid team, then let other code put him on the smallest team
860 if( c1 >= 0 && pl.team == COLOR_TEAM1)
861 selectedteam = pl.team;
862 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
863 selectedteam = pl.team;
864 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
865 selectedteam = pl.team;
866 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
867 selectedteam = pl.team;
872 if(!only_return_best)
874 SetPlayerColors(pl, selectedteam - 1);
879 // otherwise end up on the smallest team (handled below)
882 smallest = FindSmallestTeam(pl, TRUE);
885 if(!only_return_best)
887 TeamchangeFrags(self);
890 SetPlayerColors(pl, COLOR_TEAM1 - 1);
892 else if(smallest == 2)
894 SetPlayerColors(pl, COLOR_TEAM2 - 1);
896 else if(smallest == 3)
898 SetPlayerColors(pl, COLOR_TEAM3 - 1);
900 else if(smallest == 4)
902 SetPlayerColors(pl, COLOR_TEAM4 - 1);
906 error("smallest team: invalid team\n");
909 if(pl.deadflag == DEAD_NO)
910 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
916 //void() ctf_playerchanged;
917 void SV_ChangeTeam(float _color)
919 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
921 // in normal deathmatch we can just apply the color and we're done
922 if(!cvar("teamplay")) {
923 SetPlayerColors(self, _color);
927 scolor = self.clientcolors & 0x0F;
928 dcolor = _color & 0x0F;
930 if(scolor == COLOR_TEAM1 - 1)
932 else if(scolor == COLOR_TEAM2 - 1)
934 else if(scolor == COLOR_TEAM3 - 1)
936 else if(scolor == COLOR_TEAM4 - 1)
938 if(dcolor == COLOR_TEAM1 - 1)
940 else if(dcolor == COLOR_TEAM2 - 1)
942 else if(dcolor == COLOR_TEAM3 - 1)
944 else if(dcolor == COLOR_TEAM4 - 1)
947 CheckAllowedTeams(self);
949 if(dteam == 1 && c1 < 0) dteam = 4;
950 if(dteam == 4 && c4 < 0) dteam = 3;
951 if(dteam == 3 && c3 < 0) dteam = 2;
952 if(dteam == 2 && c2 < 0) dteam = 1;
954 // not changing teams
957 //bprint("same team change\n");
958 SetPlayerTeam(self, dteam, steam, TRUE);
964 if(cvar("g_campaign") || cvar("g_changeteam_banned"))
966 sprint(self, "Team changes not allowed\n");
967 return; // changing teams is not allowed
970 if(!cvar("g_campaign") && cvar("g_balance_teams_prevent_imbalance"))
972 // only allow changing to a smaller or equal size team
974 // find out what teams are available
975 //CheckAllowedTeams();
976 // count how many players on each team
977 GetTeamCounts(world);
980 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
985 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
990 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
995 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
1002 sprint(self, "Cannot change to an invalid team\n");
1007 // get starting team
1008 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
1010 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
1012 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
1014 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
1017 if(scount) // started at a valid, nonempty team
1019 // check if we're trying to change to a larger team that doens't have bots to swap with
1020 if(dcount >= scount && dbotcount <= 0)
1022 sprint(self, "Cannot change to a larger team\n");
1023 return; // can't change to a larger team
1029 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
1031 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1033 // reduce frags during a team change
1034 TeamchangeFrags(self);
1037 SetPlayerTeam(self, dteam, steam, FALSE);
1039 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1041 // kill player when changing teams
1042 if(self.deadflag == DEAD_NO)
1043 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
1045 //ctf_playerchanged();
1048 void ShufflePlayerOutOfTeam (float source_team)
1050 float smallestteam, smallestteam_count, steam;
1051 float lowest_bot_score, lowest_player_score;
1052 entity head, lowest_bot, lowest_player, selected;
1055 smallestteam_count = 999999999;
1057 if(c1 >= 0 && c1 < smallestteam_count)
1060 smallestteam_count = c1;
1062 if(c2 >= 0 && c2 < smallestteam_count)
1065 smallestteam_count = c2;
1067 if(c3 >= 0 && c3 < smallestteam_count)
1070 smallestteam_count = c3;
1072 if(c4 >= 0 && c4 < smallestteam_count)
1075 smallestteam_count = c4;
1080 bprint("warning: no smallest team\n");
1084 if(source_team == 1)
1085 steam = COLOR_TEAM1;
1086 else if(source_team == 2)
1087 steam = COLOR_TEAM2;
1088 else if(source_team == 3)
1089 steam = COLOR_TEAM3;
1090 else if(source_team == 4)
1091 steam = COLOR_TEAM4;
1094 lowest_bot_score = 999999999;
1095 lowest_player = world;
1096 lowest_player_score = 999999999;
1098 // find the lowest-scoring player & bot of that team
1099 // FIXME this is broken in new scoring system, add a helper to find the worst bot of a team
1100 FOR_EACH_PLAYER(head)
1102 if(head.team == steam)
1106 if(head.frags < lowest_bot_score)
1109 lowest_bot_score = head.frags;
1114 if(head.frags < lowest_player_score)
1116 lowest_player = head;
1117 lowest_player_score = head.frags;
1123 // prefers to move a bot...
1124 if(lowest_bot != world)
1125 selected = lowest_bot;
1126 // but it will move a player if it has to
1128 selected = lowest_player;
1129 // don't do anything if it couldn't find anyone
1132 bprint("warning: couldn't find a player to move from team\n");
1136 // smallest team gains a member
1137 if(smallestteam == 1)
1141 else if(smallestteam == 2)
1145 else if(smallestteam == 3)
1149 else if(smallestteam == 4)
1155 bprint("warning: destination team invalid\n");
1158 // source team loses a member
1159 if(source_team == 1)
1163 else if(source_team == 2)
1167 else if(source_team == 3)
1171 else if(source_team == 4)
1177 bprint("warning: source team invalid\n");
1181 // move the player to the new team
1182 TeamchangeFrags(selected);
1183 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1185 if(selected.deadflag == DEAD_NO)
1186 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1187 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1190 float lastRebalanceInfo;
1191 void CauseRebalance(float source_team, float howmany_toomany)
1196 if(IsTeamBalanceForced() == 1)
1198 bprint("Rebalancing Teams\n");
1199 ShufflePlayerOutOfTeam(source_team);
1203 if(howmany_toomany < cvar("g_balance_teams_complain"))
1205 if(time < lastRebalanceInfo + 90)
1207 lastRebalanceInfo = time;
1208 if(source_team == 1)
1209 steam = COLOR_TEAM1;
1210 else if(source_team == 2)
1211 steam = COLOR_TEAM2;
1212 else if(source_team == 3)
1213 steam = COLOR_TEAM3;
1214 else if(source_team == 4)
1215 steam = COLOR_TEAM4;
1216 ServerConsoleEcho(strcat("Team ", ftos(source_team), " too large, complaining."), TRUE);
1217 FOR_EACH_REALPLAYER(head)
1219 if(head.team == steam)
1221 sprint(head, "\{1}\{13}^3SERVER NOTICE:^7 One of you please change teams!\n");
1222 centerprint_atprio(head, CENTERPRIO_REBALANCE, "^3SERVER NOTICE:\n\n^7Someone of you please change teams!");
1228 // part of g_balance_teams_force
1229 // occasionally perform an audit of the teams to make
1230 // sure they're more or less balanced in player count.
1233 float numplayers, numteams, smallest, toomany;
1235 balance = IsTeamBalanceForced();
1239 if(audit_teams_time > time)
1242 audit_teams_time = time + 4 + random();
1244 // bprint("Auditing teams\n");
1246 CheckAllowedTeams(world);
1247 GetTeamCounts(world);
1250 numteams = numplayers = smallest = 0;
1253 numteams = numteams + 1;
1254 numplayers = numplayers + c1;
1259 numteams = numteams + 1;
1260 numplayers = numplayers + c2;
1266 numteams = numteams + 1;
1267 numplayers = numplayers + c3;
1273 numteams = numteams + 1;
1274 numplayers = numplayers + c4;
1280 return; // no players to move around
1282 return; // don't bother shuffling if for some reason there aren't any teams
1284 toomany = smallest + 1;
1286 if(c1 && c1 > toomany)
1287 CauseRebalance(1, c1 - toomany);
1288 if(c2 && c2 > toomany)
1289 CauseRebalance(2, c2 - toomany);
1290 if(c3 && c3 > toomany)
1291 CauseRebalance(3, c3 - toomany);
1292 if(c4 && c4 > toomany)
1293 CauseRebalance(4, c4 - toomany);
1295 // if teams are still unbalanced, balance them further in the next audit,
1296 // which will happen sooner (keep doing rapid audits until things are in order)
1297 audit_teams_time = time + 0.7 + random()*0.3;
1302 /*void UpdateTeamScore(entity e, float first)
1307 WriteByte (MSG_ALL, SVC_UPDATENAME);
1308 WriteByte (MSG_ALL, clientno);
1309 WriteString (MSG_ALL, e.netname);
1311 WriteByte (MSG_ALL, SVC_UPDATECOLORS);
1312 WriteByte (MSG_ALL, clientno);
1313 WriteByte (MSG_ALL, e.b_shirt * 16 + who.b_pants);
1316 WriteByte (MSG_ALL, SVC_UPDATEFRAGS);
1317 WriteByte (MSG_ALL, clientno);
1318 WriteShort (MSG_ALL, e.frags + 10000);
1324 // code from here on is just to support maps that don't have team entities
1325 void tdm_spawnteam (string teamname, float teamcolor)
1329 e.classname = "tdm_team";
1330 e.netname = teamname;
1335 // spawn some default teams if the map is not set up for tdm
1336 void tdm_spawnteams()
1340 numteams = cvar("g_tdm_teams");
1342 tdm_spawnteam("Red", COLOR_TEAM1-1);
1343 tdm_spawnteam("Blue", COLOR_TEAM2-1);
1345 tdm_spawnteam("Yellow", COLOR_TEAM3-1);
1347 tdm_spawnteam("Pink", COLOR_TEAM4-1);
1350 void tdm_delayedinit()
1352 self.think = SUB_Remove;
1353 self.nextthink = time;
1354 // if no teams are found, spawn defaults
1355 if (find(world, classname, "tdm_team") == world)
1363 e.think = tdm_delayedinit;
1364 e.nextthink = time + 0.1;