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)
111 CheckAllowedTeams(world);
112 ScoreInfo_Init((c1>=0) + (c2>=0) + (c3>=0) + (c4>=0)); // NOTE this assumes that if team 2 is allowed, 1 is too, and if 3 is, 2 is, and if 4 is, 3 is!
115 ScoreInfo_Init(0); // NOTE this assumes that if team 2 is allowed, 1 is too, and if 3 is, 2 is, and if 4 is, 3 is!
116 ScoreInfo_SetLabel_TeamScore (ST_SCORE, "score", SFL_SORT_PRIO_PRIMARY);
117 ScoreInfo_SetLabel_PlayerScore(SP_KILLS, "kills", 0);
118 ScoreInfo_SetLabel_PlayerScore(SP_DEATHS, "deaths", SFL_LOWER_IS_BETTER);
119 ScoreInfo_SetLabel_PlayerScore(SP_SUICIDES, "suicides", SFL_LOWER_IS_BETTER);
120 ScoreInfo_SetLabel_PlayerScore(SP_SCORE, "score", SFL_SORT_PRIO_PRIMARY);
124 void ActivateTeamplay()
126 float teamplay_default;
127 teamplay_default = cvar("teamplay_default");
130 cvar_set("teamplay", ftos(teamplay_default));
132 cvar_set("teamplay", "3");
135 void InitGameplayMode()
137 float fraglimit_override, timelimit_override, capturelimit_override;
141 game = cvar("gamecfg"); // load game options
143 // game cvars get reset before map changes
144 // then map's cfg sets them as desired
146 // FIXME: also set a message or game mode name to print to players when the join
148 // set both here, gamemode can override it later
149 timelimit_override = cvar("timelimit_override");
150 fraglimit_override = cvar("fraglimit_override");
151 capturelimit_override = cvar("capturelimit_override");
153 if(game == GAME_DOMINATION || cvar("g_domination"))
156 game = GAME_DOMINATION;
157 cvar_set("g_domination", "1");
161 fraglimit_override = cvar("g_domination_point_limit");
163 gamemode_name = "Domination";
166 else if(game == GAME_CTF || cvar("g_ctf"))
170 cvar_set("g_ctf", "1");
174 fraglimit_override = cvar("g_ctf_capture_limit");
175 //no capture limit override here
177 gamemode_name = "Capture the Flag";
180 else if(game == GAME_RUNEMATCH || cvar("g_runematch"))
182 game = GAME_RUNEMATCH;
183 cvar_set("g_runematch", "1");
184 cvar_set("g_minstagib", "0");
186 if(cvar("deathmatch_force_teamplay"))
189 fraglimit_override = cvar("g_runematch_point_limit");
191 gamemode_name = "Rune Match";
197 else if(game == GAME_DEATHMATCH || game == GAME_TEAM_DEATHMATCH || cvar("g_tdm"))
199 if(!cvar("deathmatch"))
200 cvar_set("deathmatch", "1");
203 if(game == GAME_TEAM_DEATHMATCH || cvar("g_tdm") || cvar("deathmatch_force_teamplay"))
206 game = GAME_TEAM_DEATHMATCH;
207 gamemode_name = "Team Deathmatch";
210 cvar_set("g_tdm", "1");
215 game = GAME_DEATHMATCH;
216 gamemode_name = "Deathmatch";
220 fraglimit_override = cvar("fraglimit_override");
222 else if(game == GAME_ASSAULT || cvar("g_assault"))
226 gamemode_name = "Assault";
229 cvar_set("g_assault", "1");
231 else if(game == GAME_LMS || cvar("g_lms"))
235 cvar_set("g_lms", "1");
236 fraglimit_override = cvar("g_lms_lives_override");
237 if(fraglimit_override == 0)
238 fraglimit_override = -1;
239 gamemode_name = "Last Man Standing";
241 lms_lowest_lives = 9999;
244 ScoreInfo_SetLabel_PlayerScore(SP_KILLS, "kills", 0);
245 ScoreInfo_SetLabel_PlayerScore(SP_DEATHS, "deaths", 0);
246 ScoreInfo_SetLabel_PlayerScore(SP_SUICIDES, "suicides", SFL_LOWER_IS_BETTER);
247 // do not define SP_SCORE for LMS
248 ScoreInfo_SetLabel_PlayerScore(SP_LMS_LIVES, "lives", SFL_SORT_PRIO_SECONDARY);
249 ScoreInfo_SetLabel_PlayerScore(SP_LMS_PLACE, "place", SFL_LOWER_IS_BETTER | SFL_HIDE_ZERO | SFL_SORT_PRIO_PRIMARY);
251 else if(game == GAME_ARENA || cvar("g_arena"))
255 cvar_set("g_arena", "1");
256 fraglimit_override = cvar("g_arena_point_limit");
257 maxspawned = cvar("g_arena_maxspawned");
260 arena_roundbased = cvar("g_arena_roundbased");
261 gamemode_name = "Arena";
264 else if(game == GAME_KEYHUNT || cvar("g_keyhunt"))
268 cvar_set("g_keyhunt", "1");
269 fraglimit_override = cvar("g_keyhunt_point_limit");
271 gamemode_name = "Key Hunt";
274 else if(game == GAME_ONSLAUGHT || cvar("g_onslaught"))
277 game = GAME_ONSLAUGHT;
278 cvar_set("g_onslaught", "1");
282 gamemode_name = "Onslaught";
287 // we can only assume...
289 game = GAME_DEATHMATCH;
290 cvar_set("g_dm", "1");
291 gamemode_name = "Deathmatch";
295 // those mutators rule each other out
296 if(cvar("g_minstagib"))
298 cvar_set("g_instagib", "0");
299 cvar_set("g_rocketarena", "0");
301 if(cvar("g_instagib"))
303 cvar_set("g_minstagib", "0");
304 cvar_set("g_rocketarena", "0");
306 if(cvar("g_rocketarena"))
308 cvar_set("g_instagib", "0");
309 cvar_set("g_minstagib", "0");
312 g_domination = cvar("g_domination");
313 g_ctf = cvar("g_ctf");
314 g_lms = cvar("g_lms");
315 g_tdm = cvar("g_tdm");
316 g_runematch = cvar("g_runematch");
317 g_keyhunt = cvar("g_keyhunt");
318 g_onslaught = cvar("g_onslaught");
319 g_assault = cvar("g_assault");
320 g_arena = cvar("g_arena");
322 cache_mutatormsg = strzone("");
323 cache_motd = strzone("");
324 cache_lastmutatormsg = strzone("");
325 cache_lastmotd = strzone("");
328 if(g_ctf_win_mode != 2)
329 fraglimit_override = capturelimit_override;
331 // enforce the server's universal frag/time limits
332 if(!cvar("g_campaign"))
334 if(fraglimit_override >= 0)
335 cvar_set("fraglimit", ftos(fraglimit_override));
336 if(timelimit_override >= 0)
337 cvar_set("timelimit", ftos(timelimit_override));
340 if (game == GAME_DOMINATION)//cvar("g_domination"))
342 else if (game == GAME_CTF)//cvar("g_ctf"))
344 else if (game == GAME_RUNEMATCH)//cvar("g_runematch"))
346 else if (game == GAME_TEAM_DEATHMATCH)//cvar("g_runematch"))
348 else if (game == GAME_KEYHUNT)//cvar("g_keyhunt"))
350 else if (game == GAME_ONSLAUGHT)
355 e.nextthink = time + 0.3; // MUST be after all other delayed inits!
356 e.think = default_delayedinit;
359 string GetClientVersionMessage(float v) {
360 local string versionmsg;
362 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";
363 // either that or someone wants to be funny
364 } else if (v != cvar("gameversion")) {
365 if(v < cvar("gameversion")) {
366 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
368 versionmsg = "^3This server is using an outdated Nexuiz version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
371 versionmsg = "^2client version and server version are compatible.^8";
377 void PrintWelcomeMessage(entity pl)
379 string s, mutator, modifications, padding;
382 /*if(self.welcomemessage_time > time)
384 self.welcomemessage_time = time + 0.8; */
386 if(self.cvar_scr_centertime == 0) return;
387 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
388 if(self.welcomemessage_time > time) return;
389 self.welcomemessage_time = time + self.cvar_scr_centertime * 0.6;
392 if(cvar("g_campaign"))
394 centerprint(pl, campaign_message);
398 if(!self.BUTTON_INFO)
400 if(self.classname == "observer")
404 p = PlayerScore_Add(self, SP_LMS_PLACE, 0);
406 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"));
408 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"));
411 else if(self.classname == "spectator")
415 p = PlayerScore_Add(self, SP_LMS_PLACE, 0);
417 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"));
420 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"));
422 local string specString;
423 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");
425 if(time < restart_countdown) //also show the countdown when being a spectator
426 specString = strcat(specString, "\n\n^1Game starts in ", ftos(restartAnnouncer.cnt + 1), " seconds^7");
427 else if (timeoutStatus != 0)
428 specString = strcat(specString, "\n\n", getTimeoutText(1));
429 return centerprint_atprio(self, CENTERPRIO_SPAM, specString);
434 mutator = "^2Minstagib ^1";
436 mutator = "^2Instagib ^1";
437 else if(g_rocketarena)
438 mutator = "^2Rocketarena ^1";
440 mutator = "^2No Items Nexuiz ^1";
443 // to protect against unheedingly made changes
445 modifications = strcat(modifications, ", ");
447 modifications = "midair";
451 modifications = strcat(modifications, ", ");
453 modifications = strcat(modifications, "vampire");
455 if(g_laserguided_missile) {
457 modifications = strcat(modifications, ", ");
459 modifications = strcat(modifications, "laser-guided-missiles");
463 modifications = strcat(modifications, ", ");
465 modifications = strcat(modifications, "Tournament");
468 local string versionmessage;
469 versionmessage = GetClientVersionMessage(self.version);
471 s = strcat(s, NEWLINES, "This is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
472 s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
474 if(modifications != "")
475 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
478 if(!tourneyInMatchStage)
479 s = strcat(s, "\n^8The game is currently in ^3warmup-stage ^8!\n");
481 s = strcat(s, "\n^8The game is currently in ^3match-stage ^8!\n");
484 if(time < restart_countdown)
485 s = strcat(s, "\n^1Game starts in ", ftos(restartAnnouncer.cnt + 1), " seconds^7");
487 if(timeoutStatus != 0)
488 s = strcat(s, "\n\n", getTimeoutText(1));
490 if (g_grappling_hook)
491 s = strcat(s, "\n\n^8grappling hook is enabled, press 'e' to use it\n");
493 if(cache_lastmutatormsg != cvar_string("g_mutatormsg"))
495 if(cache_lastmutatormsg)
496 strunzone(cache_lastmutatormsg);
498 strunzone(cache_mutatormsg);
499 cache_lastmutatormsg = strzone(cvar_string("g_mutatormsg"));
500 cache_mutatormsg = strzone(wordwrap(cache_lastmutatormsg, 50));
503 if (cache_mutatormsg != "") {
504 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
507 if(cache_lastmotd != cvar_string("sv_motd"))
510 strunzone(cache_lastmotd);
512 strunzone(cache_motd);
513 cache_lastmotd = strzone(cvar_string("sv_motd"));
514 cache_motd = strzone(wordwrap(cache_lastmotd, 50));
517 if (cache_motd != "") {
518 s = strcat(s, "\n\n^8MOTD: ^7", cache_motd);
527 void SetPlayerColors(entity pl, float _color)
531 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
533 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
534 pl.clientcolors = 16*cl + cl;*/
537 pants = _color & 0x0F;
538 shirt = _color & 0xF0;
542 setcolor(pl, 16*pants + pants);
544 setcolor(pl, shirt + pants);
548 void SetPlayerTeam(entity pl, float t, float s, float noprint)
553 _color = COLOR_TEAM4 - 1;
555 _color = COLOR_TEAM3 - 1;
557 _color = COLOR_TEAM2 - 1;
559 _color = COLOR_TEAM1 - 1;
561 SetPlayerColors(pl,_color);
563 if(!noprint && t != s)
565 //bprint(pl.netname, " has changed to ", TeamNoName(t), "\n");
566 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
578 // set c1...c4 to show what teams are allowed
579 void CheckAllowedTeams (entity for_whom)
588 c1 = c2 = c3 = c4 = -1;
589 cb1 = cb2 = cb3 = cb4 = 0;
591 // onslaught is special
594 head = findchain(classname, "onslaught_generator");
597 if (head.team == COLOR_TEAM1) c1 = 0;
598 if (head.team == COLOR_TEAM2) c2 = 0;
599 if (head.team == COLOR_TEAM3) c3 = 0;
600 if (head.team == COLOR_TEAM4) c4 = 0;
607 teament_name = "dom_team";
609 teament_name = "ctf_team";
611 teament_name = "tdm_team";
614 c1 = c2 = 0; // Assault always has 2 teams
619 // cover anything else by treating it like tdm with no teams spawned
623 dm = cvar("g_tdm_teams");
625 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
629 c1 = c2 = c3 = c4 = 0;
642 // first find out what teams are allowed
643 head = find(world, classname, teament_name);
646 if(!(g_domination && head.netname == ""))
648 if(head.team == COLOR_TEAM1)
652 if(head.team == COLOR_TEAM2)
656 if(head.team == COLOR_TEAM3)
660 if(head.team == COLOR_TEAM4)
665 head = find(head, classname, teament_name);
670 if(cvar("bot_vs_human") > 0)
673 if(clienttype(for_whom) == CLIENTTYPE_BOT)
678 else if(cvar("bot_vs_human") < 0)
681 if(clienttype(for_whom) == CLIENTTYPE_BOT)
689 float PlayerValue(entity p)
691 if(IsTeamBalanceForced() == 1)
696 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
697 // teams that are allowed will now have their player counts stored in c1...c4
698 void GetTeamCounts(entity ignore)
702 // now count how many players are on each team already
704 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
705 // also remember the lowest-scoring player
707 FOR_EACH_PLAYER(head)
709 if(head != ignore)// && head.netname != "")
711 value = PlayerValue(head);
712 if(clienttype(head) == CLIENTTYPE_BOT)
716 if(head.team == COLOR_TEAM1)
724 if(head.team == COLOR_TEAM2)
732 if(head.team == COLOR_TEAM3)
740 if(head.team == COLOR_TEAM4)
752 // returns # of smallest team (1, 2, 3, 4)
753 // NOTE: Assumes CheckAllowedTeams has already been called!
754 float FindSmallestTeam(entity pl, float ignore_pl)
756 float totalteams, smallestteam, smallestteam_count, smallestteam_score, balance_type;
759 // find out what teams are available
760 //CheckAllowedTeams();
762 // make sure there are at least 2 teams to join
764 totalteams = totalteams + 1;
766 totalteams = totalteams + 1;
768 totalteams = totalteams + 1;
770 totalteams = totalteams + 1;
772 if(cvar("bot_vs_human"))
778 error("Too few teams available for domination\n");
780 error("Too few teams available for ctf\n");
782 error("Too few teams available for key hunt\n");
784 error("Too few teams available for team deathmatch\n");
787 // count how many players are in each team
791 GetTeamCounts(world);
793 // c1...c4 now have counts of each team
794 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
797 smallestteam_count = 999999999;
798 smallestteam_score = 999999999;
800 // 2 gives priority to what team you're already on, 1 goes in order
801 // 2 doesn't seem to work though...
805 //if(pl.classname != "player")
806 if(clienttype(pl) != CLIENTTYPE_BOT)
808 c1 -= cb1 * 255.0/256;
809 c2 -= cb2 * 255.0/256;
810 c3 -= cb3 * 255.0/256;
811 c4 -= cb4 * 255.0/256;
814 if(balance_type == 1)
816 if(c1 >= 0 && (c1 < smallestteam_count || (c1 <= smallestteam_count && team1_score < smallestteam_score)))
819 smallestteam_count = c1;
820 smallestteam_score = team1_score;
822 if(c2 >= 0 && (c2 < smallestteam_count || (c2 <= smallestteam_count && team2_score < smallestteam_score)))
825 smallestteam_count = c2;
826 smallestteam_score = team2_score;
828 if(c3 >= 0 && (c3 < smallestteam_count || (c3 <= smallestteam_count && team3_score < smallestteam_score)))
831 smallestteam_count = c3;
832 smallestteam_score = team3_score;
834 if(c4 >= 0 && (c4 < smallestteam_count || (c4 <= smallestteam_count && team4_score < smallestteam_score)))
837 smallestteam_count = c4;
838 smallestteam_score = team4_score;
843 if(c1 >= 0 && (c1 < smallestteam_count ||
844 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
847 smallestteam_count = c1;
849 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
850 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
853 smallestteam_count = c2;
855 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
856 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
859 smallestteam_count = c3;
861 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
862 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
865 smallestteam_count = c4;
872 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
874 float smallest, selectedteam;
876 // don't join a team if we're not playing a team game
877 if(!cvar("teamplay") && !g_domination && !g_ctf && !g_keyhunt)
880 // find out what teams are available
881 CheckAllowedTeams(pl);
886 if(cvar("g_domination_default_teams") < 3)
888 if(cvar("g_domination_default_teams") < 4)
892 // if we don't care what team he ends up on, put him on whatever team he entered as.
893 // if he's not on a valid team, then let other code put him on the smallest team
896 if( c1 >= 0 && pl.team == COLOR_TEAM1)
897 selectedteam = pl.team;
898 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
899 selectedteam = pl.team;
900 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
901 selectedteam = pl.team;
902 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
903 selectedteam = pl.team;
908 if(!only_return_best)
910 SetPlayerColors(pl, selectedteam - 1);
915 // otherwise end up on the smallest team (handled below)
918 smallest = FindSmallestTeam(pl, TRUE);
921 if(!only_return_best)
923 TeamchangeFrags(self);
926 SetPlayerColors(pl, COLOR_TEAM1 - 1);
928 else if(smallest == 2)
930 SetPlayerColors(pl, COLOR_TEAM2 - 1);
932 else if(smallest == 3)
934 SetPlayerColors(pl, COLOR_TEAM3 - 1);
936 else if(smallest == 4)
938 SetPlayerColors(pl, COLOR_TEAM4 - 1);
942 error("smallest team: invalid team\n");
945 if(pl.deadflag == DEAD_NO)
946 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
952 //void() ctf_playerchanged;
953 void SV_ChangeTeam(float _color)
955 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
957 // in normal deathmatch we can just apply the color and we're done
958 if(!cvar("teamplay")) {
959 SetPlayerColors(self, _color);
963 scolor = self.clientcolors & 0x0F;
964 dcolor = _color & 0x0F;
966 if(scolor == COLOR_TEAM1 - 1)
968 else if(scolor == COLOR_TEAM2 - 1)
970 else if(scolor == COLOR_TEAM3 - 1)
972 else if(scolor == COLOR_TEAM4 - 1)
974 if(dcolor == COLOR_TEAM1 - 1)
976 else if(dcolor == COLOR_TEAM2 - 1)
978 else if(dcolor == COLOR_TEAM3 - 1)
980 else if(dcolor == COLOR_TEAM4 - 1)
983 CheckAllowedTeams(self);
985 if(dteam == 1 && c1 < 0) dteam = 4;
986 if(dteam == 4 && c4 < 0) dteam = 3;
987 if(dteam == 3 && c3 < 0) dteam = 2;
988 if(dteam == 2 && c2 < 0) dteam = 1;
990 // not changing teams
993 //bprint("same team change\n");
994 SetPlayerTeam(self, dteam, steam, TRUE);
1000 if(cvar("g_campaign") || cvar("g_changeteam_banned"))
1002 sprint(self, "Team changes not allowed\n");
1003 return; // changing teams is not allowed
1006 if(!cvar("g_campaign") && cvar("g_balance_teams_prevent_imbalance"))
1008 // only allow changing to a smaller or equal size team
1010 // find out what teams are available
1011 //CheckAllowedTeams();
1012 // count how many players on each team
1013 GetTeamCounts(world);
1016 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
1021 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
1026 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
1031 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
1038 sprint(self, "Cannot change to an invalid team\n");
1043 // get starting team
1044 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
1046 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
1048 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
1050 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
1053 if(scount) // started at a valid, nonempty team
1055 // check if we're trying to change to a larger team that doens't have bots to swap with
1056 if(dcount >= scount && dbotcount <= 0)
1058 sprint(self, "Cannot change to a larger team\n");
1059 return; // can't change to a larger team
1065 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
1067 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1069 // reduce frags during a team change
1070 TeamchangeFrags(self);
1073 SetPlayerTeam(self, dteam, steam, FALSE);
1075 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1077 // kill player when changing teams
1078 if(self.deadflag == DEAD_NO)
1079 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
1081 //ctf_playerchanged();
1084 void ShufflePlayerOutOfTeam (float source_team)
1086 float smallestteam, smallestteam_count, steam;
1087 float lowest_bot_score, lowest_player_score;
1088 entity head, lowest_bot, lowest_player, selected;
1091 smallestteam_count = 999999999;
1093 if(c1 >= 0 && c1 < smallestteam_count)
1096 smallestteam_count = c1;
1098 if(c2 >= 0 && c2 < smallestteam_count)
1101 smallestteam_count = c2;
1103 if(c3 >= 0 && c3 < smallestteam_count)
1106 smallestteam_count = c3;
1108 if(c4 >= 0 && c4 < smallestteam_count)
1111 smallestteam_count = c4;
1116 bprint("warning: no smallest team\n");
1120 if(source_team == 1)
1121 steam = COLOR_TEAM1;
1122 else if(source_team == 2)
1123 steam = COLOR_TEAM2;
1124 else if(source_team == 3)
1125 steam = COLOR_TEAM3;
1126 else if(source_team == 4)
1127 steam = COLOR_TEAM4;
1130 lowest_bot_score = 999999999;
1131 lowest_player = world;
1132 lowest_player_score = 999999999;
1134 // find the lowest-scoring player & bot of that team
1135 FOR_EACH_PLAYER(head)
1137 if(head.team == steam)
1141 if(head.totalfrags < lowest_bot_score)
1144 lowest_bot_score = head.totalfrags;
1149 if(head.totalfrags < lowest_player_score)
1151 lowest_player = head;
1152 lowest_player_score = head.totalfrags;
1158 // prefers to move a bot...
1159 if(lowest_bot != world)
1160 selected = lowest_bot;
1161 // but it will move a player if it has to
1163 selected = lowest_player;
1164 // don't do anything if it couldn't find anyone
1167 bprint("warning: couldn't find a player to move from team\n");
1171 // smallest team gains a member
1172 if(smallestteam == 1)
1176 else if(smallestteam == 2)
1180 else if(smallestteam == 3)
1184 else if(smallestteam == 4)
1190 bprint("warning: destination team invalid\n");
1193 // source team loses a member
1194 if(source_team == 1)
1198 else if(source_team == 2)
1202 else if(source_team == 3)
1206 else if(source_team == 4)
1212 bprint("warning: source team invalid\n");
1216 // move the player to the new team
1217 TeamchangeFrags(selected);
1218 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1220 if(selected.deadflag == DEAD_NO)
1221 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1222 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1225 float lastRebalanceInfo;
1226 void CauseRebalance(float source_team, float howmany_toomany)
1231 if(IsTeamBalanceForced() == 1)
1233 bprint("Rebalancing Teams\n");
1234 ShufflePlayerOutOfTeam(source_team);
1238 if(howmany_toomany < cvar("g_balance_teams_complain"))
1240 if(time < lastRebalanceInfo + 90)
1242 lastRebalanceInfo = time;
1243 if(source_team == 1)
1244 steam = COLOR_TEAM1;
1245 else if(source_team == 2)
1246 steam = COLOR_TEAM2;
1247 else if(source_team == 3)
1248 steam = COLOR_TEAM3;
1249 else if(source_team == 4)
1250 steam = COLOR_TEAM4;
1251 ServerConsoleEcho(strcat("Team ", ftos(source_team), " too large, complaining."), TRUE);
1252 FOR_EACH_REALPLAYER(head)
1254 if(head.team == steam)
1256 sprint(head, "\{1}\{13}^3SERVER NOTICE:^7 One of you please change teams!\n");
1257 centerprint_atprio(head, CENTERPRIO_REBALANCE, "^3SERVER NOTICE:\n\n^7Someone of you please change teams!");
1263 // part of g_balance_teams_force
1264 // occasionally perform an audit of the teams to make
1265 // sure they're more or less balanced in player count.
1268 float numplayers, numteams, smallest, toomany;
1270 balance = IsTeamBalanceForced();
1274 if(audit_teams_time > time)
1277 audit_teams_time = time + 4 + random();
1279 // bprint("Auditing teams\n");
1281 CheckAllowedTeams(world);
1282 GetTeamCounts(world);
1285 numteams = numplayers = smallest = 0;
1288 numteams = numteams + 1;
1289 numplayers = numplayers + c1;
1294 numteams = numteams + 1;
1295 numplayers = numplayers + c2;
1301 numteams = numteams + 1;
1302 numplayers = numplayers + c3;
1308 numteams = numteams + 1;
1309 numplayers = numplayers + c4;
1315 return; // no players to move around
1317 return; // don't bother shuffling if for some reason there aren't any teams
1319 toomany = smallest + 1;
1321 if(c1 && c1 > toomany)
1322 CauseRebalance(1, c1 - toomany);
1323 if(c2 && c2 > toomany)
1324 CauseRebalance(2, c2 - toomany);
1325 if(c3 && c3 > toomany)
1326 CauseRebalance(3, c3 - toomany);
1327 if(c4 && c4 > toomany)
1328 CauseRebalance(4, c4 - toomany);
1330 // if teams are still unbalanced, balance them further in the next audit,
1331 // which will happen sooner (keep doing rapid audits until things are in order)
1332 audit_teams_time = time + 0.7 + random()*0.3;
1337 // code from here on is just to support maps that don't have team entities
1338 void tdm_spawnteam (string teamname, float teamcolor)
1342 e.classname = "tdm_team";
1343 e.netname = teamname;
1348 // spawn some default teams if the map is not set up for tdm
1349 void tdm_spawnteams()
1353 numteams = cvar("g_tdm_teams");
1355 tdm_spawnteam("Red", COLOR_TEAM1-1);
1356 tdm_spawnteam("Blue", COLOR_TEAM2-1);
1358 tdm_spawnteam("Yellow", COLOR_TEAM3-1);
1360 tdm_spawnteam("Pink", COLOR_TEAM4-1);
1363 void tdm_delayedinit()
1365 self.think = SUB_Remove;
1366 self.nextthink = time;
1367 // if no teams are found, spawn defaults
1368 if (find(world, classname, "tdm_team") == world)
1376 e.think = tdm_delayedinit;
1377 e.nextthink = time + 0.1;