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 = 9999;
223 ScoreInfo_SetLabel_PlayerScore(SP_KILLS, "kills", 0);
224 ScoreInfo_SetLabel_PlayerScore(SP_DEATHS, "deaths", 0);
225 ScoreInfo_SetLabel_PlayerScore(SP_SUICIDES, "suicides", SFL_LOWER_IS_BETTER);
226 // do not define SP_SCORE for LMS
227 ScoreInfo_SetLabel_PlayerScore(SP_LMS_LIVES, "lives", SFL_SORT_PRIO_SECONDARY);
228 ScoreInfo_SetLabel_PlayerScore(SP_LMS_PLACE, "place", SFL_LOWER_IS_BETTER | SFL_HIDE_ZERO | SFL_SORT_PRIO_PRIMARY);
230 else if(game == GAME_ARENA || cvar("g_arena"))
234 cvar_set("g_arena", "1");
235 fraglimit_override = cvar("g_arena_point_limit");
236 maxspawned = cvar("g_arena_maxspawned");
239 arena_roundbased = cvar("g_arena_roundbased");
240 gamemode_name = "Arena";
243 else if(game == GAME_KEYHUNT || cvar("g_keyhunt"))
247 cvar_set("g_keyhunt", "1");
248 fraglimit_override = cvar("g_keyhunt_point_limit");
250 gamemode_name = "Key Hunt";
253 else if(game == GAME_ONSLAUGHT || cvar("g_onslaught"))
256 game = GAME_ONSLAUGHT;
257 cvar_set("g_onslaught", "1");
261 gamemode_name = "Onslaught";
266 // we can only assume...
268 game = GAME_DEATHMATCH;
269 cvar_set("g_dm", "1");
270 gamemode_name = "Deathmatch";
274 // those mutators rule each other out
275 if(cvar("g_minstagib"))
277 cvar_set("g_instagib", "0");
278 cvar_set("g_rocketarena", "0");
280 if(cvar("g_instagib"))
282 cvar_set("g_minstagib", "0");
283 cvar_set("g_rocketarena", "0");
285 if(cvar("g_rocketarena"))
287 cvar_set("g_instagib", "0");
288 cvar_set("g_minstagib", "0");
291 g_domination = cvar("g_domination");
292 g_ctf = cvar("g_ctf");
293 g_lms = cvar("g_lms");
294 g_tdm = cvar("g_tdm");
295 g_runematch = cvar("g_runematch");
296 g_keyhunt = cvar("g_keyhunt");
297 g_onslaught = cvar("g_onslaught");
298 g_assault = cvar("g_assault");
299 g_arena = cvar("g_arena");
301 cache_mutatormsg = strzone("");
302 cache_motd = strzone("");
303 cache_lastmutatormsg = strzone("");
304 cache_lastmotd = strzone("");
306 // enforce the server's universal frag/time limits
307 if(!cvar("g_campaign"))
309 if(fraglimit_override >= 0)
310 cvar_set("fraglimit", ftos(fraglimit_override));
311 if(timelimit_override >= 0)
312 cvar_set("timelimit", ftos(timelimit_override));
313 if(capturelimit_override >= 0)
314 cvar_set("capturelimit", ftos(capturelimit_override));
317 if (game == GAME_DOMINATION)//cvar("g_domination"))
319 else if (game == GAME_CTF)//cvar("g_ctf"))
321 else if (game == GAME_RUNEMATCH)//cvar("g_runematch"))
323 else if (game == GAME_TEAM_DEATHMATCH)//cvar("g_runematch"))
325 else if (game == GAME_KEYHUNT)//cvar("g_keyhunt"))
327 else if (game == GAME_ONSLAUGHT)
330 if(!scores_initialized)
334 CheckAllowedTeams(world);
335 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!
338 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!
339 ScoreInfo_SetLabel_TeamScore (ST_SCORE, "score", SFL_SORT_PRIO_PRIMARY);
340 ScoreInfo_SetLabel_PlayerScore(SP_KILLS, "kills", 0);
341 ScoreInfo_SetLabel_PlayerScore(SP_DEATHS, "deaths", 0);
342 ScoreInfo_SetLabel_PlayerScore(SP_SUICIDES, "suicides", SFL_LOWER_IS_BETTER);
343 ScoreInfo_SetLabel_PlayerScore(SP_SCORE, "score", SFL_SORT_PRIO_PRIMARY);
347 string GetClientVersionMessage(float v) {
348 local string versionmsg;
350 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";
351 // either that or someone wants to be funny
352 } else if (v != cvar("gameversion")) {
353 if(v < cvar("gameversion")) {
354 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
356 versionmsg = "^3This server is using an outdated Nexuiz version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
359 versionmsg = "^2client version and server version are compatible.^8";
365 void PrintWelcomeMessage(entity pl)
367 string s, mutator, modifications, padding;
370 /*if(self.welcomemessage_time > time)
372 self.welcomemessage_time = time + 0.8; */
374 if(self.cvar_scr_centertime == 0) return;
375 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
376 if(self.welcomemessage_time > time) return;
377 self.welcomemessage_time = time + self.cvar_scr_centertime * 0.6;
380 if(cvar("g_campaign"))
382 centerprint(pl, campaign_message);
386 if(!self.BUTTON_INFO)
388 if(self.classname == "observer")
392 p = PlayerScore_Add(self, SP_LMS_PLACE, 0);
394 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"));
396 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"));
399 else if(self.classname == "spectator")
403 p = PlayerScore_Add(self, SP_LMS_PLACE, 0);
405 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"));
408 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"));
410 local string specString;
411 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");
413 if(time < restart_countdown) //also show the countdown when being a spectator
414 specString = strcat(specString, "\n\n^1Game starts in ", ftos(restartAnnouncer.cnt + 1), " seconds^7");
415 else if (timeoutStatus != 0)
416 specString = strcat(specString, "\n\n", getTimeoutText(1));
417 return centerprint_atprio(self, CENTERPRIO_SPAM, specString);
422 mutator = "^2Minstagib ^1";
424 mutator = "^2Instagib ^1";
425 else if(g_rocketarena)
426 mutator = "^2Rocketarena ^1";
428 mutator = "^2No Items Nexuiz ^1";
431 // to protect against unheedingly made changes
433 modifications = strcat(modifications, ", ");
435 modifications = "midair";
439 modifications = strcat(modifications, ", ");
441 modifications = strcat(modifications, "vampire");
443 if(g_laserguided_missile) {
445 modifications = strcat(modifications, ", ");
447 modifications = strcat(modifications, "laser-guided-missiles");
451 modifications = strcat(modifications, ", ");
453 modifications = strcat(modifications, "Tournament");
456 local string versionmessage;
457 versionmessage = GetClientVersionMessage(self.version);
459 s = strcat(s, NEWLINES, "This is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
460 s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
462 if(modifications != "")
463 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
466 if(!tourneyInMatchStage)
467 s = strcat(s, "\n^8The game is currently in ^3warmup-stage ^8!\n");
469 s = strcat(s, "\n^8The game is currently in ^3match-stage ^8!\n");
472 if(time < restart_countdown)
473 s = strcat(s, "\n^1Game starts in ", ftos(restartAnnouncer.cnt + 1), " seconds^7");
475 if(timeoutStatus != 0)
476 s = strcat(s, "\n\n", getTimeoutText(1));
478 if (g_grappling_hook)
479 s = strcat(s, "\n\n^8grappling hook is enabled, press 'e' to use it\n");
481 if(cache_lastmutatormsg != cvar_string("g_mutatormsg"))
483 if(cache_lastmutatormsg)
484 strunzone(cache_lastmutatormsg);
486 strunzone(cache_mutatormsg);
487 cache_lastmutatormsg = strzone(cvar_string("g_mutatormsg"));
488 cache_mutatormsg = strzone(wordwrap(cache_lastmutatormsg, 50));
491 if (cache_mutatormsg != "") {
492 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
495 if(cache_lastmotd != cvar_string("sv_motd"))
498 strunzone(cache_lastmotd);
500 strunzone(cache_motd);
501 cache_lastmotd = strzone(cvar_string("sv_motd"));
502 cache_motd = strzone(wordwrap(cache_lastmotd, 50));
505 if (cache_motd != "") {
506 s = strcat(s, "\n\n^8MOTD: ^7", cache_motd);
510 if(cvar("fraglimit"))
513 if(cvar("timelimit"))
516 s = strcat(s, "\n^8frag limit: ^7", cvar_string("fraglimit"), padding);
518 if(cvar("timelimit"))
519 s = strcat(s, "\n^8time limit: ^7", cvar_string("timelimit"), " minutes");
526 void SetPlayerColors(entity pl, float _color)
530 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
532 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
533 pl.clientcolors = 16*cl + cl;*/
536 pants = _color & 0x0F;
537 shirt = _color & 0xF0;
541 setcolor(pl, 16*pants + pants);
543 setcolor(pl, shirt + pants);
547 void SetPlayerTeam(entity pl, float t, float s, float noprint)
552 _color = COLOR_TEAM4 - 1;
554 _color = COLOR_TEAM3 - 1;
556 _color = COLOR_TEAM2 - 1;
558 _color = COLOR_TEAM1 - 1;
560 SetPlayerColors(pl,_color);
562 if(!noprint && t != s)
564 //bprint(pl.netname, " has changed to ", TeamNoName(t), "\n");
565 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
577 // set c1...c4 to show what teams are allowed
578 void CheckAllowedTeams (entity for_whom)
587 c1 = c2 = c3 = c4 = -1;
588 cb1 = cb2 = cb3 = cb4 = 0;
590 // onslaught is special
593 head = findchain(classname, "onslaught_generator");
596 if (head.team == COLOR_TEAM1) c1 = 0;
597 if (head.team == COLOR_TEAM2) c2 = 0;
598 if (head.team == COLOR_TEAM3) c3 = 0;
599 if (head.team == COLOR_TEAM4) c4 = 0;
606 teament_name = "dom_team";
608 teament_name = "ctf_team";
610 teament_name = "tdm_team";
613 c1 = c2 = 0; // Assault always has 2 teams
618 // cover anything else by treating it like tdm with no teams spawned
622 dm = cvar("g_tdm_teams");
624 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
628 c1 = c2 = c3 = c4 = 0;
641 // first find out what teams are allowed
642 head = find(world, classname, teament_name);
645 if(!(g_domination && head.netname == ""))
647 if(head.team == COLOR_TEAM1)
651 if(head.team == COLOR_TEAM2)
655 if(head.team == COLOR_TEAM3)
659 if(head.team == COLOR_TEAM4)
664 head = find(head, classname, teament_name);
669 if(cvar("bot_vs_human") > 0)
672 if(clienttype(for_whom) == CLIENTTYPE_BOT)
677 else if(cvar("bot_vs_human") < 0)
680 if(clienttype(for_whom) == CLIENTTYPE_BOT)
688 float PlayerValue(entity p)
690 if(IsTeamBalanceForced() == 1)
695 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
696 // teams that are allowed will now have their player counts stored in c1...c4
697 void GetTeamCounts(entity ignore)
701 // now count how many players are on each team already
703 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
704 // also remember the lowest-scoring player
706 FOR_EACH_PLAYER(head)
708 if(head != ignore)// && head.netname != "")
710 value = PlayerValue(head);
711 if(clienttype(head) == CLIENTTYPE_BOT)
715 if(head.team == COLOR_TEAM1)
723 if(head.team == COLOR_TEAM2)
731 if(head.team == COLOR_TEAM3)
739 if(head.team == COLOR_TEAM4)
751 // returns # of smallest team (1, 2, 3, 4)
752 // NOTE: Assumes CheckAllowedTeams has already been called!
753 float FindSmallestTeam(entity pl, float ignore_pl)
755 float totalteams, smallestteam, smallestteam_count, smallestteam_score, balance_type;
758 // find out what teams are available
759 //CheckAllowedTeams();
761 // make sure there are at least 2 teams to join
763 totalteams = totalteams + 1;
765 totalteams = totalteams + 1;
767 totalteams = totalteams + 1;
769 totalteams = totalteams + 1;
771 if(cvar("bot_vs_human"))
777 error("Too few teams available for domination\n");
779 error("Too few teams available for ctf\n");
781 error("Too few teams available for key hunt\n");
783 error("Too few teams available for team deathmatch\n");
786 // count how many players are in each team
790 GetTeamCounts(world);
792 // c1...c4 now have counts of each team
793 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
796 smallestteam_count = 999999999;
797 smallestteam_score = 999999999;
799 // 2 gives priority to what team you're already on, 1 goes in order
800 // 2 doesn't seem to work though...
804 //if(pl.classname != "player")
805 if(clienttype(pl) != CLIENTTYPE_BOT)
807 c1 -= cb1 * 255.0/256;
808 c2 -= cb2 * 255.0/256;
809 c3 -= cb3 * 255.0/256;
810 c4 -= cb4 * 255.0/256;
813 if(balance_type == 1)
815 if(c1 >= 0 && (c1 < smallestteam_count || (c1 <= smallestteam_count && team1_score < smallestteam_score)))
818 smallestteam_count = c1;
819 smallestteam_score = team1_score;
821 if(c2 >= 0 && (c2 < smallestteam_count || (c2 <= smallestteam_count && team2_score < smallestteam_score)))
824 smallestteam_count = c2;
825 smallestteam_score = team2_score;
827 if(c3 >= 0 && (c3 < smallestteam_count || (c3 <= smallestteam_count && team3_score < smallestteam_score)))
830 smallestteam_count = c3;
831 smallestteam_score = team3_score;
833 if(c4 >= 0 && (c4 < smallestteam_count || (c4 <= smallestteam_count && team4_score < smallestteam_score)))
836 smallestteam_count = c4;
837 smallestteam_score = team4_score;
842 if(c1 >= 0 && (c1 < smallestteam_count ||
843 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
846 smallestteam_count = c1;
848 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
849 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
852 smallestteam_count = c2;
854 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
855 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
858 smallestteam_count = c3;
860 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
861 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
864 smallestteam_count = c4;
871 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
873 float smallest, selectedteam;
875 // don't join a team if we're not playing a team game
876 if(!cvar("teamplay") && !g_domination && !g_ctf && !g_keyhunt)
879 // find out what teams are available
880 CheckAllowedTeams(pl);
885 if(cvar("g_domination_default_teams") < 3)
887 if(cvar("g_domination_default_teams") < 4)
891 // if we don't care what team he ends up on, put him on whatever team he entered as.
892 // if he's not on a valid team, then let other code put him on the smallest team
895 if( c1 >= 0 && pl.team == COLOR_TEAM1)
896 selectedteam = pl.team;
897 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
898 selectedteam = pl.team;
899 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
900 selectedteam = pl.team;
901 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
902 selectedteam = pl.team;
907 if(!only_return_best)
909 SetPlayerColors(pl, selectedteam - 1);
914 // otherwise end up on the smallest team (handled below)
917 smallest = FindSmallestTeam(pl, TRUE);
920 if(!only_return_best)
922 TeamchangeFrags(self);
925 SetPlayerColors(pl, COLOR_TEAM1 - 1);
927 else if(smallest == 2)
929 SetPlayerColors(pl, COLOR_TEAM2 - 1);
931 else if(smallest == 3)
933 SetPlayerColors(pl, COLOR_TEAM3 - 1);
935 else if(smallest == 4)
937 SetPlayerColors(pl, COLOR_TEAM4 - 1);
941 error("smallest team: invalid team\n");
944 if(pl.deadflag == DEAD_NO)
945 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
951 //void() ctf_playerchanged;
952 void SV_ChangeTeam(float _color)
954 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
956 // in normal deathmatch we can just apply the color and we're done
957 if(!cvar("teamplay")) {
958 SetPlayerColors(self, _color);
962 scolor = self.clientcolors & 0x0F;
963 dcolor = _color & 0x0F;
965 if(scolor == COLOR_TEAM1 - 1)
967 else if(scolor == COLOR_TEAM2 - 1)
969 else if(scolor == COLOR_TEAM3 - 1)
971 else if(scolor == COLOR_TEAM4 - 1)
973 if(dcolor == COLOR_TEAM1 - 1)
975 else if(dcolor == COLOR_TEAM2 - 1)
977 else if(dcolor == COLOR_TEAM3 - 1)
979 else if(dcolor == COLOR_TEAM4 - 1)
982 CheckAllowedTeams(self);
984 if(dteam == 1 && c1 < 0) dteam = 4;
985 if(dteam == 4 && c4 < 0) dteam = 3;
986 if(dteam == 3 && c3 < 0) dteam = 2;
987 if(dteam == 2 && c2 < 0) dteam = 1;
989 // not changing teams
992 //bprint("same team change\n");
993 SetPlayerTeam(self, dteam, steam, TRUE);
999 if(cvar("g_campaign") || cvar("g_changeteam_banned"))
1001 sprint(self, "Team changes not allowed\n");
1002 return; // changing teams is not allowed
1005 if(!cvar("g_campaign") && cvar("g_balance_teams_prevent_imbalance"))
1007 // only allow changing to a smaller or equal size team
1009 // find out what teams are available
1010 //CheckAllowedTeams();
1011 // count how many players on each team
1012 GetTeamCounts(world);
1015 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
1020 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
1025 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
1030 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
1037 sprint(self, "Cannot change to an invalid team\n");
1042 // get starting team
1043 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
1045 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
1047 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
1049 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
1052 if(scount) // started at a valid, nonempty team
1054 // check if we're trying to change to a larger team that doens't have bots to swap with
1055 if(dcount >= scount && dbotcount <= 0)
1057 sprint(self, "Cannot change to a larger team\n");
1058 return; // can't change to a larger team
1064 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
1066 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1068 // reduce frags during a team change
1069 TeamchangeFrags(self);
1072 SetPlayerTeam(self, dteam, steam, FALSE);
1074 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1076 // kill player when changing teams
1077 if(self.deadflag == DEAD_NO)
1078 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
1080 //ctf_playerchanged();
1083 void ShufflePlayerOutOfTeam (float source_team)
1085 float smallestteam, smallestteam_count, steam;
1086 float lowest_bot_score, lowest_player_score;
1087 entity head, lowest_bot, lowest_player, selected;
1090 smallestteam_count = 999999999;
1092 if(c1 >= 0 && c1 < smallestteam_count)
1095 smallestteam_count = c1;
1097 if(c2 >= 0 && c2 < smallestteam_count)
1100 smallestteam_count = c2;
1102 if(c3 >= 0 && c3 < smallestteam_count)
1105 smallestteam_count = c3;
1107 if(c4 >= 0 && c4 < smallestteam_count)
1110 smallestteam_count = c4;
1115 bprint("warning: no smallest team\n");
1119 if(source_team == 1)
1120 steam = COLOR_TEAM1;
1121 else if(source_team == 2)
1122 steam = COLOR_TEAM2;
1123 else if(source_team == 3)
1124 steam = COLOR_TEAM3;
1125 else if(source_team == 4)
1126 steam = COLOR_TEAM4;
1129 lowest_bot_score = 999999999;
1130 lowest_player = world;
1131 lowest_player_score = 999999999;
1133 // find the lowest-scoring player & bot of that team
1134 FOR_EACH_PLAYER(head)
1136 if(head.team == steam)
1140 if(head.totalfrags < lowest_bot_score)
1143 lowest_bot_score = head.totalfrags;
1148 if(head.totalfrags < lowest_player_score)
1150 lowest_player = head;
1151 lowest_player_score = head.totalfrags;
1157 // prefers to move a bot...
1158 if(lowest_bot != world)
1159 selected = lowest_bot;
1160 // but it will move a player if it has to
1162 selected = lowest_player;
1163 // don't do anything if it couldn't find anyone
1166 bprint("warning: couldn't find a player to move from team\n");
1170 // smallest team gains a member
1171 if(smallestteam == 1)
1175 else if(smallestteam == 2)
1179 else if(smallestteam == 3)
1183 else if(smallestteam == 4)
1189 bprint("warning: destination team invalid\n");
1192 // source team loses a member
1193 if(source_team == 1)
1197 else if(source_team == 2)
1201 else if(source_team == 3)
1205 else if(source_team == 4)
1211 bprint("warning: source team invalid\n");
1215 // move the player to the new team
1216 TeamchangeFrags(selected);
1217 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1219 if(selected.deadflag == DEAD_NO)
1220 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1221 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1224 float lastRebalanceInfo;
1225 void CauseRebalance(float source_team, float howmany_toomany)
1230 if(IsTeamBalanceForced() == 1)
1232 bprint("Rebalancing Teams\n");
1233 ShufflePlayerOutOfTeam(source_team);
1237 if(howmany_toomany < cvar("g_balance_teams_complain"))
1239 if(time < lastRebalanceInfo + 90)
1241 lastRebalanceInfo = time;
1242 if(source_team == 1)
1243 steam = COLOR_TEAM1;
1244 else if(source_team == 2)
1245 steam = COLOR_TEAM2;
1246 else if(source_team == 3)
1247 steam = COLOR_TEAM3;
1248 else if(source_team == 4)
1249 steam = COLOR_TEAM4;
1250 ServerConsoleEcho(strcat("Team ", ftos(source_team), " too large, complaining."), TRUE);
1251 FOR_EACH_REALPLAYER(head)
1253 if(head.team == steam)
1255 sprint(head, "\{1}\{13}^3SERVER NOTICE:^7 One of you please change teams!\n");
1256 centerprint_atprio(head, CENTERPRIO_REBALANCE, "^3SERVER NOTICE:\n\n^7Someone of you please change teams!");
1262 // part of g_balance_teams_force
1263 // occasionally perform an audit of the teams to make
1264 // sure they're more or less balanced in player count.
1267 float numplayers, numteams, smallest, toomany;
1269 balance = IsTeamBalanceForced();
1273 if(audit_teams_time > time)
1276 audit_teams_time = time + 4 + random();
1278 // bprint("Auditing teams\n");
1280 CheckAllowedTeams(world);
1281 GetTeamCounts(world);
1284 numteams = numplayers = smallest = 0;
1287 numteams = numteams + 1;
1288 numplayers = numplayers + c1;
1293 numteams = numteams + 1;
1294 numplayers = numplayers + c2;
1300 numteams = numteams + 1;
1301 numplayers = numplayers + c3;
1307 numteams = numteams + 1;
1308 numplayers = numplayers + c4;
1314 return; // no players to move around
1316 return; // don't bother shuffling if for some reason there aren't any teams
1318 toomany = smallest + 1;
1320 if(c1 && c1 > toomany)
1321 CauseRebalance(1, c1 - toomany);
1322 if(c2 && c2 > toomany)
1323 CauseRebalance(2, c2 - toomany);
1324 if(c3 && c3 > toomany)
1325 CauseRebalance(3, c3 - toomany);
1326 if(c4 && c4 > toomany)
1327 CauseRebalance(4, c4 - toomany);
1329 // if teams are still unbalanced, balance them further in the next audit,
1330 // which will happen sooner (keep doing rapid audits until things are in order)
1331 audit_teams_time = time + 0.7 + random()*0.3;
1336 // code from here on is just to support maps that don't have team entities
1337 void tdm_spawnteam (string teamname, float teamcolor)
1341 e.classname = "tdm_team";
1342 e.netname = teamname;
1347 // spawn some default teams if the map is not set up for tdm
1348 void tdm_spawnteams()
1352 numteams = cvar("g_tdm_teams");
1354 tdm_spawnteam("Red", COLOR_TEAM1-1);
1355 tdm_spawnteam("Blue", COLOR_TEAM2-1);
1357 tdm_spawnteam("Yellow", COLOR_TEAM3-1);
1359 tdm_spawnteam("Pink", COLOR_TEAM4-1);
1362 void tdm_delayedinit()
1364 self.think = SUB_Remove;
1365 self.nextthink = time;
1366 // if no teams are found, spawn defaults
1367 if (find(world, classname, "tdm_team") == world)
1375 e.think = tdm_delayedinit;
1376 e.nextthink = time + 0.1;