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("");
327 // enforce the server's universal frag/time limits
328 if(!cvar("g_campaign"))
330 if(fraglimit_override >= 0)
331 cvar_set("fraglimit", ftos(fraglimit_override));
332 if(timelimit_override >= 0)
333 cvar_set("timelimit", ftos(timelimit_override));
334 if(capturelimit_override >= 0)
335 cvar_set("capturelimit", ftos(capturelimit_override));
338 if (game == GAME_DOMINATION)//cvar("g_domination"))
340 else if (game == GAME_CTF)//cvar("g_ctf"))
342 else if (game == GAME_RUNEMATCH)//cvar("g_runematch"))
344 else if (game == GAME_TEAM_DEATHMATCH)//cvar("g_runematch"))
346 else if (game == GAME_KEYHUNT)//cvar("g_keyhunt"))
348 else if (game == GAME_ONSLAUGHT)
353 e.nextthink = time + 0.3; // MUST be after all other delayed inits!
354 e.think = default_delayedinit;
357 string GetClientVersionMessage(float v) {
358 local string versionmsg;
360 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";
361 // either that or someone wants to be funny
362 } else if (v != cvar("gameversion")) {
363 if(v < cvar("gameversion")) {
364 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
366 versionmsg = "^3This server is using an outdated Nexuiz version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
369 versionmsg = "^2client version and server version are compatible.^8";
375 void PrintWelcomeMessage(entity pl)
377 string s, mutator, modifications, padding;
380 /*if(self.welcomemessage_time > time)
382 self.welcomemessage_time = time + 0.8; */
384 if(self.cvar_scr_centertime == 0) return;
385 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
386 if(self.welcomemessage_time > time) return;
387 self.welcomemessage_time = time + self.cvar_scr_centertime * 0.6;
390 if(cvar("g_campaign"))
392 centerprint(pl, campaign_message);
396 if(!self.BUTTON_INFO)
398 if(self.classname == "observer")
402 p = PlayerScore_Add(self, SP_LMS_PLACE, 0);
404 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"));
406 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"));
409 else if(self.classname == "spectator")
413 p = PlayerScore_Add(self, SP_LMS_PLACE, 0);
415 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"));
418 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 local string specString;
421 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");
423 if(time < restart_countdown) //also show the countdown when being a spectator
424 specString = strcat(specString, "\n\n^1Game starts in ", ftos(restartAnnouncer.cnt + 1), " seconds^7");
425 else if (timeoutStatus != 0)
426 specString = strcat(specString, "\n\n", getTimeoutText(1));
427 return centerprint_atprio(self, CENTERPRIO_SPAM, specString);
432 mutator = "^2Minstagib ^1";
434 mutator = "^2Instagib ^1";
435 else if(g_rocketarena)
436 mutator = "^2Rocketarena ^1";
438 mutator = "^2No Items Nexuiz ^1";
441 // to protect against unheedingly made changes
443 modifications = strcat(modifications, ", ");
445 modifications = "midair";
449 modifications = strcat(modifications, ", ");
451 modifications = strcat(modifications, "vampire");
453 if(g_laserguided_missile) {
455 modifications = strcat(modifications, ", ");
457 modifications = strcat(modifications, "laser-guided-missiles");
461 modifications = strcat(modifications, ", ");
463 modifications = strcat(modifications, "Tournament");
466 local string versionmessage;
467 versionmessage = GetClientVersionMessage(self.version);
469 s = strcat(s, NEWLINES, "This is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
470 s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
472 if(modifications != "")
473 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
476 if(!tourneyInMatchStage)
477 s = strcat(s, "\n^8The game is currently in ^3warmup-stage ^8!\n");
479 s = strcat(s, "\n^8The game is currently in ^3match-stage ^8!\n");
482 if(time < restart_countdown)
483 s = strcat(s, "\n^1Game starts in ", ftos(restartAnnouncer.cnt + 1), " seconds^7");
485 if(timeoutStatus != 0)
486 s = strcat(s, "\n\n", getTimeoutText(1));
488 if (g_grappling_hook)
489 s = strcat(s, "\n\n^8grappling hook is enabled, press 'e' to use it\n");
491 if(cache_lastmutatormsg != cvar_string("g_mutatormsg"))
493 if(cache_lastmutatormsg)
494 strunzone(cache_lastmutatormsg);
496 strunzone(cache_mutatormsg);
497 cache_lastmutatormsg = strzone(cvar_string("g_mutatormsg"));
498 cache_mutatormsg = strzone(wordwrap(cache_lastmutatormsg, 50));
501 if (cache_mutatormsg != "") {
502 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
505 if(cache_lastmotd != cvar_string("sv_motd"))
508 strunzone(cache_lastmotd);
510 strunzone(cache_motd);
511 cache_lastmotd = strzone(cvar_string("sv_motd"));
512 cache_motd = strzone(wordwrap(cache_lastmotd, 50));
515 if (cache_motd != "") {
516 s = strcat(s, "\n\n^8MOTD: ^7", cache_motd);
520 if(cvar("fraglimit"))
523 if(cvar("timelimit"))
526 s = strcat(s, "\n^8frag limit: ^7", cvar_string("fraglimit"), padding);
528 if(cvar("timelimit"))
529 s = strcat(s, "\n^8time limit: ^7", cvar_string("timelimit"), " minutes");
536 void SetPlayerColors(entity pl, float _color)
540 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
542 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
543 pl.clientcolors = 16*cl + cl;*/
546 pants = _color & 0x0F;
547 shirt = _color & 0xF0;
551 setcolor(pl, 16*pants + pants);
553 setcolor(pl, shirt + pants);
557 void SetPlayerTeam(entity pl, float t, float s, float noprint)
562 _color = COLOR_TEAM4 - 1;
564 _color = COLOR_TEAM3 - 1;
566 _color = COLOR_TEAM2 - 1;
568 _color = COLOR_TEAM1 - 1;
570 SetPlayerColors(pl,_color);
572 if(!noprint && t != s)
574 //bprint(pl.netname, " has changed to ", TeamNoName(t), "\n");
575 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
587 // set c1...c4 to show what teams are allowed
588 void CheckAllowedTeams (entity for_whom)
597 c1 = c2 = c3 = c4 = -1;
598 cb1 = cb2 = cb3 = cb4 = 0;
600 // onslaught is special
603 head = findchain(classname, "onslaught_generator");
606 if (head.team == COLOR_TEAM1) c1 = 0;
607 if (head.team == COLOR_TEAM2) c2 = 0;
608 if (head.team == COLOR_TEAM3) c3 = 0;
609 if (head.team == COLOR_TEAM4) c4 = 0;
616 teament_name = "dom_team";
618 teament_name = "ctf_team";
620 teament_name = "tdm_team";
623 c1 = c2 = 0; // Assault always has 2 teams
628 // cover anything else by treating it like tdm with no teams spawned
632 dm = cvar("g_tdm_teams");
634 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
638 c1 = c2 = c3 = c4 = 0;
651 // first find out what teams are allowed
652 head = find(world, classname, teament_name);
655 if(!(g_domination && head.netname == ""))
657 if(head.team == COLOR_TEAM1)
661 if(head.team == COLOR_TEAM2)
665 if(head.team == COLOR_TEAM3)
669 if(head.team == COLOR_TEAM4)
674 head = find(head, classname, teament_name);
679 if(cvar("bot_vs_human") > 0)
682 if(clienttype(for_whom) == CLIENTTYPE_BOT)
687 else if(cvar("bot_vs_human") < 0)
690 if(clienttype(for_whom) == CLIENTTYPE_BOT)
698 float PlayerValue(entity p)
700 if(IsTeamBalanceForced() == 1)
705 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
706 // teams that are allowed will now have their player counts stored in c1...c4
707 void GetTeamCounts(entity ignore)
711 // now count how many players are on each team already
713 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
714 // also remember the lowest-scoring player
716 FOR_EACH_PLAYER(head)
718 if(head != ignore)// && head.netname != "")
720 value = PlayerValue(head);
721 if(clienttype(head) == CLIENTTYPE_BOT)
725 if(head.team == COLOR_TEAM1)
733 if(head.team == COLOR_TEAM2)
741 if(head.team == COLOR_TEAM3)
749 if(head.team == COLOR_TEAM4)
761 // returns # of smallest team (1, 2, 3, 4)
762 // NOTE: Assumes CheckAllowedTeams has already been called!
763 float FindSmallestTeam(entity pl, float ignore_pl)
765 float totalteams, smallestteam, smallestteam_count, smallestteam_score, balance_type;
768 // find out what teams are available
769 //CheckAllowedTeams();
771 // make sure there are at least 2 teams to join
773 totalteams = totalteams + 1;
775 totalteams = totalteams + 1;
777 totalteams = totalteams + 1;
779 totalteams = totalteams + 1;
781 if(cvar("bot_vs_human"))
787 error("Too few teams available for domination\n");
789 error("Too few teams available for ctf\n");
791 error("Too few teams available for key hunt\n");
793 error("Too few teams available for team deathmatch\n");
796 // count how many players are in each team
800 GetTeamCounts(world);
802 // c1...c4 now have counts of each team
803 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
806 smallestteam_count = 999999999;
807 smallestteam_score = 999999999;
809 // 2 gives priority to what team you're already on, 1 goes in order
810 // 2 doesn't seem to work though...
814 //if(pl.classname != "player")
815 if(clienttype(pl) != CLIENTTYPE_BOT)
817 c1 -= cb1 * 255.0/256;
818 c2 -= cb2 * 255.0/256;
819 c3 -= cb3 * 255.0/256;
820 c4 -= cb4 * 255.0/256;
823 if(balance_type == 1)
825 if(c1 >= 0 && (c1 < smallestteam_count || (c1 <= smallestteam_count && team1_score < smallestteam_score)))
828 smallestteam_count = c1;
829 smallestteam_score = team1_score;
831 if(c2 >= 0 && (c2 < smallestteam_count || (c2 <= smallestteam_count && team2_score < smallestteam_score)))
834 smallestteam_count = c2;
835 smallestteam_score = team2_score;
837 if(c3 >= 0 && (c3 < smallestteam_count || (c3 <= smallestteam_count && team3_score < smallestteam_score)))
840 smallestteam_count = c3;
841 smallestteam_score = team3_score;
843 if(c4 >= 0 && (c4 < smallestteam_count || (c4 <= smallestteam_count && team4_score < smallestteam_score)))
846 smallestteam_count = c4;
847 smallestteam_score = team4_score;
852 if(c1 >= 0 && (c1 < smallestteam_count ||
853 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
856 smallestteam_count = c1;
858 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
859 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
862 smallestteam_count = c2;
864 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
865 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
868 smallestteam_count = c3;
870 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
871 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
874 smallestteam_count = c4;
881 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
883 float smallest, selectedteam;
885 // don't join a team if we're not playing a team game
886 if(!cvar("teamplay") && !g_domination && !g_ctf && !g_keyhunt)
889 // find out what teams are available
890 CheckAllowedTeams(pl);
895 if(cvar("g_domination_default_teams") < 3)
897 if(cvar("g_domination_default_teams") < 4)
901 // if we don't care what team he ends up on, put him on whatever team he entered as.
902 // if he's not on a valid team, then let other code put him on the smallest team
905 if( c1 >= 0 && pl.team == COLOR_TEAM1)
906 selectedteam = pl.team;
907 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
908 selectedteam = pl.team;
909 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
910 selectedteam = pl.team;
911 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
912 selectedteam = pl.team;
917 if(!only_return_best)
919 SetPlayerColors(pl, selectedteam - 1);
924 // otherwise end up on the smallest team (handled below)
927 smallest = FindSmallestTeam(pl, TRUE);
930 if(!only_return_best)
932 TeamchangeFrags(self);
935 SetPlayerColors(pl, COLOR_TEAM1 - 1);
937 else if(smallest == 2)
939 SetPlayerColors(pl, COLOR_TEAM2 - 1);
941 else if(smallest == 3)
943 SetPlayerColors(pl, COLOR_TEAM3 - 1);
945 else if(smallest == 4)
947 SetPlayerColors(pl, COLOR_TEAM4 - 1);
951 error("smallest team: invalid team\n");
954 if(pl.deadflag == DEAD_NO)
955 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
961 //void() ctf_playerchanged;
962 void SV_ChangeTeam(float _color)
964 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
966 // in normal deathmatch we can just apply the color and we're done
967 if(!cvar("teamplay")) {
968 SetPlayerColors(self, _color);
972 scolor = self.clientcolors & 0x0F;
973 dcolor = _color & 0x0F;
975 if(scolor == COLOR_TEAM1 - 1)
977 else if(scolor == COLOR_TEAM2 - 1)
979 else if(scolor == COLOR_TEAM3 - 1)
981 else if(scolor == COLOR_TEAM4 - 1)
983 if(dcolor == COLOR_TEAM1 - 1)
985 else if(dcolor == COLOR_TEAM2 - 1)
987 else if(dcolor == COLOR_TEAM3 - 1)
989 else if(dcolor == COLOR_TEAM4 - 1)
992 CheckAllowedTeams(self);
994 if(dteam == 1 && c1 < 0) dteam = 4;
995 if(dteam == 4 && c4 < 0) dteam = 3;
996 if(dteam == 3 && c3 < 0) dteam = 2;
997 if(dteam == 2 && c2 < 0) dteam = 1;
999 // not changing teams
1000 if(scolor == dcolor)
1002 //bprint("same team change\n");
1003 SetPlayerTeam(self, dteam, steam, TRUE);
1007 if(cvar("teamplay"))
1009 if(cvar("g_campaign") || cvar("g_changeteam_banned"))
1011 sprint(self, "Team changes not allowed\n");
1012 return; // changing teams is not allowed
1015 if(!cvar("g_campaign") && cvar("g_balance_teams_prevent_imbalance"))
1017 // only allow changing to a smaller or equal size team
1019 // find out what teams are available
1020 //CheckAllowedTeams();
1021 // count how many players on each team
1022 GetTeamCounts(world);
1025 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
1030 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
1035 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
1040 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
1047 sprint(self, "Cannot change to an invalid team\n");
1052 // get starting team
1053 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
1055 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
1057 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
1059 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
1062 if(scount) // started at a valid, nonempty team
1064 // check if we're trying to change to a larger team that doens't have bots to swap with
1065 if(dcount >= scount && dbotcount <= 0)
1067 sprint(self, "Cannot change to a larger team\n");
1068 return; // can't change to a larger team
1074 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
1076 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1078 // reduce frags during a team change
1079 TeamchangeFrags(self);
1082 SetPlayerTeam(self, dteam, steam, FALSE);
1084 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1086 // kill player when changing teams
1087 if(self.deadflag == DEAD_NO)
1088 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
1090 //ctf_playerchanged();
1093 void ShufflePlayerOutOfTeam (float source_team)
1095 float smallestteam, smallestteam_count, steam;
1096 float lowest_bot_score, lowest_player_score;
1097 entity head, lowest_bot, lowest_player, selected;
1100 smallestteam_count = 999999999;
1102 if(c1 >= 0 && c1 < smallestteam_count)
1105 smallestteam_count = c1;
1107 if(c2 >= 0 && c2 < smallestteam_count)
1110 smallestteam_count = c2;
1112 if(c3 >= 0 && c3 < smallestteam_count)
1115 smallestteam_count = c3;
1117 if(c4 >= 0 && c4 < smallestteam_count)
1120 smallestteam_count = c4;
1125 bprint("warning: no smallest team\n");
1129 if(source_team == 1)
1130 steam = COLOR_TEAM1;
1131 else if(source_team == 2)
1132 steam = COLOR_TEAM2;
1133 else if(source_team == 3)
1134 steam = COLOR_TEAM3;
1135 else if(source_team == 4)
1136 steam = COLOR_TEAM4;
1139 lowest_bot_score = 999999999;
1140 lowest_player = world;
1141 lowest_player_score = 999999999;
1143 // find the lowest-scoring player & bot of that team
1144 FOR_EACH_PLAYER(head)
1146 if(head.team == steam)
1150 if(head.totalfrags < lowest_bot_score)
1153 lowest_bot_score = head.totalfrags;
1158 if(head.totalfrags < lowest_player_score)
1160 lowest_player = head;
1161 lowest_player_score = head.totalfrags;
1167 // prefers to move a bot...
1168 if(lowest_bot != world)
1169 selected = lowest_bot;
1170 // but it will move a player if it has to
1172 selected = lowest_player;
1173 // don't do anything if it couldn't find anyone
1176 bprint("warning: couldn't find a player to move from team\n");
1180 // smallest team gains a member
1181 if(smallestteam == 1)
1185 else if(smallestteam == 2)
1189 else if(smallestteam == 3)
1193 else if(smallestteam == 4)
1199 bprint("warning: destination team invalid\n");
1202 // source team loses a member
1203 if(source_team == 1)
1207 else if(source_team == 2)
1211 else if(source_team == 3)
1215 else if(source_team == 4)
1221 bprint("warning: source team invalid\n");
1225 // move the player to the new team
1226 TeamchangeFrags(selected);
1227 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1229 if(selected.deadflag == DEAD_NO)
1230 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1231 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1234 float lastRebalanceInfo;
1235 void CauseRebalance(float source_team, float howmany_toomany)
1240 if(IsTeamBalanceForced() == 1)
1242 bprint("Rebalancing Teams\n");
1243 ShufflePlayerOutOfTeam(source_team);
1247 if(howmany_toomany < cvar("g_balance_teams_complain"))
1249 if(time < lastRebalanceInfo + 90)
1251 lastRebalanceInfo = time;
1252 if(source_team == 1)
1253 steam = COLOR_TEAM1;
1254 else if(source_team == 2)
1255 steam = COLOR_TEAM2;
1256 else if(source_team == 3)
1257 steam = COLOR_TEAM3;
1258 else if(source_team == 4)
1259 steam = COLOR_TEAM4;
1260 ServerConsoleEcho(strcat("Team ", ftos(source_team), " too large, complaining."), TRUE);
1261 FOR_EACH_REALPLAYER(head)
1263 if(head.team == steam)
1265 sprint(head, "\{1}\{13}^3SERVER NOTICE:^7 One of you please change teams!\n");
1266 centerprint_atprio(head, CENTERPRIO_REBALANCE, "^3SERVER NOTICE:\n\n^7Someone of you please change teams!");
1272 // part of g_balance_teams_force
1273 // occasionally perform an audit of the teams to make
1274 // sure they're more or less balanced in player count.
1277 float numplayers, numteams, smallest, toomany;
1279 balance = IsTeamBalanceForced();
1283 if(audit_teams_time > time)
1286 audit_teams_time = time + 4 + random();
1288 // bprint("Auditing teams\n");
1290 CheckAllowedTeams(world);
1291 GetTeamCounts(world);
1294 numteams = numplayers = smallest = 0;
1297 numteams = numteams + 1;
1298 numplayers = numplayers + c1;
1303 numteams = numteams + 1;
1304 numplayers = numplayers + c2;
1310 numteams = numteams + 1;
1311 numplayers = numplayers + c3;
1317 numteams = numteams + 1;
1318 numplayers = numplayers + c4;
1324 return; // no players to move around
1326 return; // don't bother shuffling if for some reason there aren't any teams
1328 toomany = smallest + 1;
1330 if(c1 && c1 > toomany)
1331 CauseRebalance(1, c1 - toomany);
1332 if(c2 && c2 > toomany)
1333 CauseRebalance(2, c2 - toomany);
1334 if(c3 && c3 > toomany)
1335 CauseRebalance(3, c3 - toomany);
1336 if(c4 && c4 > toomany)
1337 CauseRebalance(4, c4 - toomany);
1339 // if teams are still unbalanced, balance them further in the next audit,
1340 // which will happen sooner (keep doing rapid audits until things are in order)
1341 audit_teams_time = time + 0.7 + random()*0.3;
1346 // code from here on is just to support maps that don't have team entities
1347 void tdm_spawnteam (string teamname, float teamcolor)
1351 e.classname = "tdm_team";
1352 e.netname = teamname;
1357 // spawn some default teams if the map is not set up for tdm
1358 void tdm_spawnteams()
1362 numteams = cvar("g_tdm_teams");
1364 tdm_spawnteam("Red", COLOR_TEAM1-1);
1365 tdm_spawnteam("Blue", COLOR_TEAM2-1);
1367 tdm_spawnteam("Yellow", COLOR_TEAM3-1);
1369 tdm_spawnteam("Pink", COLOR_TEAM4-1);
1372 void tdm_delayedinit()
1374 self.think = SUB_Remove;
1375 self.nextthink = time;
1376 // if no teams are found, spawn defaults
1377 if (find(world, classname, "tdm_team") == world)
1385 e.think = tdm_delayedinit;
1386 e.nextthink = time + 0.1;