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)
28 if(e.classname == "player")
30 // reduce frags during a team change
31 DistributeFragsAmongTeam(e, (e.clientcolors & 0x0F) + 1, 1 - cvar("g_changeteam_fragtransfer") / 100);
35 vector TeamColor(float teem)
40 return '1 0.0625 0.0625';
42 return '0.0625 0.0625 1';
52 string TeamName(float t)
54 return strcat(Team_ColorName(t), " Team");
56 string ColoredTeamName(float t)
58 return strcat(Team_ColorCode(t), Team_ColorName(t), " Team^7");
60 string TeamNoName(float t)
62 // fixme: Search for team entities and get their .netname's!
71 return "Neutral Team";
76 void runematch_init();
80 void LogTeamchange(entity pl)
83 if(!cvar("sv_eventlog"))
87 str = strcat(":team:", ftos(pl.playerid), ":");
88 str = strcat(str, ftos(pl.team));
89 GameLogEcho(str, FALSE);
94 cvar_set("g_dm", "0");
95 cvar_set("g_tdm", "0");
96 cvar_set("g_domination", "0");
97 cvar_set("g_ctf", "0");
98 cvar_set("g_runematch", "0");
99 cvar_set("g_lms", "0");
100 cvar_set("g_arena", "0");
101 cvar_set("g_keyhunt", "0");
102 cvar_set("g_assault", "0");
103 cvar_set("g_onslaught", "0");
104 cvar_set("teamplay", "0");
107 void ActivateTeamplay()
109 float teamplay_default;
110 teamplay_default = cvar("teamplay_default");
113 cvar_set("teamplay", ftos(teamplay_default));
115 cvar_set("teamplay", "3");
118 void InitGameplayMode()
120 float fraglimit_override, timelimit_override;
124 game = cvar("gamecfg"); // load game options
126 // game cvars get reset before map changes
127 // then map's cfg sets them as desired
129 // FIXME: also set a message or game mode name to print to players when the join
131 // set both here, gamemode can override it later
132 timelimit_override = cvar("timelimit_override");
133 fraglimit_override = cvar("fraglimit_override");
135 if(game == GAME_DOMINATION || cvar("g_domination"))
138 game = GAME_DOMINATION;
139 cvar_set("g_domination", "1");
143 fraglimit_override = cvar("g_domination_point_limit");
145 gamemode_name = "Domination";
148 else if(game == GAME_CTF || cvar("g_ctf"))
152 cvar_set("g_ctf", "1");
156 fraglimit_override = cvar("g_ctf_capture_limit");
158 gamemode_name = "Capture the Flag";
161 else if(game == GAME_RUNEMATCH || cvar("g_runematch"))
163 game = GAME_RUNEMATCH;
164 cvar_set("g_runematch", "1");
165 cvar_set("g_minstagib", "0");
167 if(cvar("deathmatch_force_teamplay"))
170 fraglimit_override = cvar("g_runematch_point_limit");
172 gamemode_name = "Rune Match";
178 else if(game == GAME_DEATHMATCH || game == GAME_TEAM_DEATHMATCH || cvar("g_tdm"))
180 if(!cvar("deathmatch"))
181 cvar_set("deathmatch", "1");
184 if(game == GAME_TEAM_DEATHMATCH || cvar("g_tdm") || cvar("deathmatch_force_teamplay"))
187 game = GAME_TEAM_DEATHMATCH;
188 gamemode_name = "Team Deathmatch";
191 cvar_set("g_tdm", "1");
196 game = GAME_DEATHMATCH;
197 gamemode_name = "Deathmatch";
201 fraglimit_override = cvar("fraglimit_override");
203 else if(game == GAME_ASSAULT || cvar("g_assault"))
207 gamemode_name = "Assault";
210 cvar_set("g_assault", "1");
212 else if(game == GAME_LMS || cvar("g_lms"))
216 cvar_set("g_lms", "1");
217 fraglimit_override = cvar("g_lms_lives_override");
218 if(fraglimit_override == 0)
219 fraglimit_override = -1;
220 gamemode_name = "Last Man Standing";
222 lms_lowest_lives = 999;
224 else if(game == GAME_ARENA || cvar("g_arena"))
228 cvar_set("g_arena", "1");
229 fraglimit_override = cvar("g_arena_point_limit");
230 maxspawned = cvar("g_arena_maxspawned");
233 arena_roundbased = cvar("g_arena_roundbased");
234 gamemode_name = "Arena";
237 else if(game == GAME_KEYHUNT || cvar("g_keyhunt"))
241 cvar_set("g_keyhunt", "1");
242 fraglimit_override = cvar("g_keyhunt_point_limit");
244 gamemode_name = "Key Hunt";
247 else if(game == GAME_ONSLAUGHT || cvar("g_onslaught"))
250 game = GAME_ONSLAUGHT;
251 cvar_set("g_onslaught", "1");
255 gamemode_name = "Onslaught";
260 // we can only assume...
262 cvar_set("g_dm", "1");
263 gamemode_name = "Deathmatch";
266 /* else if(game == GAME_TEAM_DEATHMATCH)
268 if(!cvar("deathmatch"))
269 cvar_set("deathmatch", "1");
271 //if(!cvar("teamplay"))
272 // cvar_set("teamplay", "3");
275 fraglimit_override = cvar("fraglimit_override");
278 // those mutators rule each other out
279 if(cvar("g_minstagib"))
281 cvar_set("g_instagib", "0");
282 cvar_set("g_rocketarena", "0");
284 if(cvar("g_instagib"))
286 cvar_set("g_minstagib", "0");
287 cvar_set("g_rocketarena", "0");
289 if(cvar("g_rocketarena"))
291 cvar_set("g_instagib", "0");
292 cvar_set("g_minstagib", "0");
295 g_domination = cvar("g_domination");
296 g_ctf = cvar("g_ctf");
297 g_lms = cvar("g_lms");
298 g_tdm = cvar("g_tdm");
299 g_runematch = cvar("g_runematch");
300 g_keyhunt = cvar("g_keyhunt");
301 g_onslaught = cvar("g_onslaught");
302 g_assault = cvar("g_assault");
303 g_arena = cvar("g_arena");
305 cache_mutatormsg = strzone("");
306 cache_motd = strzone("");
307 cache_lastmutatormsg = strzone("");
308 cache_lastmotd = strzone("");
310 // enforce the server's universal frag/time limits
311 if(!cvar("g_campaign"))
313 if(fraglimit_override >= 0)
314 cvar_set("fraglimit", ftos(fraglimit_override));
315 if(timelimit_override >= 0)
316 cvar_set("timelimit", ftos(timelimit_override));
319 if (game == GAME_DOMINATION)//cvar("g_domination"))
321 else if (game == GAME_CTF)//cvar("g_ctf"))
323 else if (game == GAME_RUNEMATCH)//cvar("g_runematch"))
325 else if (game == GAME_TEAM_DEATHMATCH)//cvar("g_runematch"))
327 else if (game == GAME_KEYHUNT)//cvar("g_keyhunt"))
329 //else if (game == GAME_ONSLAUGHT)
333 string GetClientVersionMessage(float v) {
334 local string versionmsg;
336 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";
337 // either that or someone wants to be funny
338 } else if (v != cvar("gameversion")) {
339 if(v < cvar("gameversion")) {
340 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
342 versionmsg = "^3This server is using an outdated Nexuiz version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
345 versionmsg = "^2client version and server version are compatible.^8";
347 return strzone(versionmsg);
352 void PrintWelcomeMessage(entity pl)
354 string s, mutator, modifications, padding;
356 /*if(self.welcomemessage_time > time)
358 self.welcomemessage_time = time + 0.8; */
360 if(self.cvar_scr_centertime == 0) return;
361 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
362 if(self.welcomemessage_time > time) return;
363 self.welcomemessage_time = time + self.cvar_scr_centertime * 0.6;
366 if(cvar("g_campaign"))
368 centerprint(pl, campaign_message);
372 if(!self.BUTTON_INFO)
374 if(self.classname == "observer")
376 if(g_lms && self.frags <= 0 && self.frags > -666)
377 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"));
378 else if(g_lms && self.frags == -666)
379 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"));
381 else if(self.classname == "spectator")
383 if ((g_lms && self.frags < 1) || g_arena)
384 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"));
386 local string specString;
387 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");
389 if(time < restart_countdown) //also show the countdown when being a spectator
390 specString = strcat(specString, "\n\n^1Game starts in ", ftos(restartAnnouncer.cnt + 1), " seconds^7");
391 else if (timeoutStatus != 0)
392 specString = strcat(specString, "\n\n", getTimeoutText(1));
393 return centerprint_atprio(self, CENTERPRIO_SPAM, specString);
399 mutator = "^2Minstagib ^1";
401 mutator = "^2Instagib ^1";
402 else if(g_rocketarena)
403 mutator = "^2Rocketarena ^1";
405 mutator = "^2No Items Nexuiz ^1";
408 // to protect against unheedingly made changes
410 modifications = strcat(modifications, ", ");
412 modifications = "midair";
416 modifications = strcat(modifications, ", ");
418 modifications = strcat(modifications, "vampire");
420 if(g_laserguided_missile) {
422 modifications = strcat(modifications, ", ");
424 modifications = strcat(modifications, "laser-guided-missiles");
428 modifications = strcat(modifications, ", ");
430 modifications = strcat(modifications, "Tournament");
433 local string versionmessage;
434 versionmessage = GetClientVersionMessage(self.version);
436 s = strcat(s, NEWLINES, "This is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
437 s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
439 if(modifications != "")
440 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
443 if(!tourneyInMatchStage)
444 s = strcat(s, "\n^8The game is currently in ^3warmup-stage ^8!\n");
446 s = strcat(s, "\n^8The game is currently in ^3match-stage ^8!\n");
449 if(time < restart_countdown)
450 s = strcat(s, "\n^1Game starts in ", ftos(restartAnnouncer.cnt + 1), " seconds^7");
452 if(timeoutStatus != 0)
453 s = strcat(s, "\n\n", getTimeoutText(1));
457 if (g_grappling_hook)
458 s = strcat(s, "\n\n^8grappling hook is enabled, press 'e' to use it\n");
460 if(cache_lastmutatormsg != cvar_string("g_mutatormsg"))
462 if(cache_lastmutatormsg)
463 strunzone(cache_lastmutatormsg);
465 strunzone(cache_mutatormsg);
466 cache_lastmutatormsg = strzone(cvar_string("g_mutatormsg"));
467 cache_mutatormsg = strzone(wordwrap(cache_lastmutatormsg, 50));
470 if (cache_mutatormsg != "") {
471 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
474 if(cache_lastmotd != cvar_string("sv_motd"))
477 strunzone(cache_lastmotd);
479 strunzone(cache_motd);
480 cache_lastmotd = strzone(cvar_string("sv_motd"));
481 cache_motd = strzone(wordwrap(cache_lastmotd, 50));
484 if (cache_motd != "") {
485 s = strcat(s, "\n\n^8MOTD: ^7", cache_motd);
489 if(cvar("fraglimit"))
492 if(cvar("timelimit"))
495 s = strcat(s, "\n^8frag limit: ^7", cvar_string("fraglimit"), padding);
497 if(cvar("timelimit"))
498 s = strcat(s, "\n^8time limit: ^7", cvar_string("timelimit"), " minutes");
509 void SetPlayerColors(entity pl, float _color)
513 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
515 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
516 pl.clientcolors = 16*cl + cl;*/
519 pants = _color & 0x0F;
520 shirt = _color & 0xF0;
524 setcolor(pl, 16*pants + pants);
526 setcolor(pl, shirt + pants);
530 void SetPlayerTeam(entity pl, float t, float s, float noprint)
535 _color = COLOR_TEAM4 - 1;
537 _color = COLOR_TEAM3 - 1;
539 _color = COLOR_TEAM2 - 1;
541 _color = COLOR_TEAM1 - 1;
543 SetPlayerColors(pl,_color);
545 if(!noprint && t != s)
547 //bprint(pl.netname, " has changed to ", TeamNoName(t), "\n");
548 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
560 // set c1...c4 to show what teams are allowed
561 void CheckAllowedTeams (entity for_whom)
570 c1 = c2 = c3 = c4 = -1;
571 cb1 = cb2 = cb3 = cb4 = 0;
573 // onslaught is special
576 head = findchain(classname, "onslaught_generator");
579 if (head.team == COLOR_TEAM1) c1 = 0;
580 if (head.team == COLOR_TEAM2) c2 = 0;
581 if (head.team == COLOR_TEAM3) c3 = 0;
582 if (head.team == COLOR_TEAM4) c4 = 0;
589 teament_name = "dom_team";
591 teament_name = "ctf_team";
593 teament_name = "tdm_team";
596 c1 = c2 = 0; // Assault always has 2 teams
601 // cover anything else by treating it like tdm with no teams spawned
605 dm = cvar("g_tdm_teams");
607 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
611 c1 = c2 = c3 = c4 = 0;
624 // first find out what teams are allowed
625 head = find(world, classname, teament_name);
628 if(!(g_domination && head.netname == ""))
630 if(head.team == COLOR_TEAM1)
634 if(head.team == COLOR_TEAM2)
638 if(head.team == COLOR_TEAM3)
642 if(head.team == COLOR_TEAM4)
647 head = find(head, classname, teament_name);
650 if(cvar("bot_vs_human") > 0)
653 if(clienttype(for_whom) == CLIENTTYPE_BOT)
658 else if(cvar("bot_vs_human") < 0)
661 if(clienttype(for_whom) == CLIENTTYPE_BOT)
668 float PlayerValue(entity p)
670 if(IsTeamBalanceForced() == 1)
675 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
676 // teams that are allowed will now have their player counts stored in c1...c4
677 void GetTeamCounts(entity ignore)
681 // now count how many players are on each team already
683 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
684 // also remember the lowest-scoring player
686 FOR_EACH_PLAYER(head)
688 if(head != ignore)// && head.netname != "")
690 value = PlayerValue(head);
691 if(clienttype(head) == CLIENTTYPE_BOT)
695 if(head.team == COLOR_TEAM1)
703 if(head.team == COLOR_TEAM2)
711 if(head.team == COLOR_TEAM3)
719 if(head.team == COLOR_TEAM4)
731 // returns # of smallest team (1, 2, 3, 4)
732 // NOTE: Assumes CheckAllowedTeams has already been called!
733 float FindSmallestTeam(entity pl, float ignore_pl)
735 float totalteams, smallestteam, smallestteam_count, smallestteam_score, balance_type;
738 // find out what teams are available
739 //CheckAllowedTeams();
741 // make sure there are at least 2 teams to join
743 totalteams = totalteams + 1;
745 totalteams = totalteams + 1;
747 totalteams = totalteams + 1;
749 totalteams = totalteams + 1;
751 if(cvar("bot_vs_human"))
757 error("Too few teams available for domination\n");
759 error("Too few teams available for ctf\n");
761 error("Too few teams available for key hunt\n");
763 error("Too few teams available for team deathmatch\n");
766 // count how many players are in each team
770 GetTeamCounts(world);
772 // c1...c4 now have counts of each team
773 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
776 smallestteam_count = 999999999;
777 smallestteam_score = 999999999;
779 // 2 gives priority to what team you're already on, 1 goes in order
780 // 2 doesn't seem to work though...
784 //if(pl.classname != "player")
785 if(clienttype(pl) != CLIENTTYPE_BOT)
787 c1 -= cb1 * 255.0/256;
788 c2 -= cb2 * 255.0/256;
789 c3 -= cb3 * 255.0/256;
790 c4 -= cb4 * 255.0/256;
793 if(balance_type == 1)
795 if(c1 >= 0 && (c1 < smallestteam_count || (c1 <= smallestteam_count && team1_score < smallestteam_score)))
798 smallestteam_count = c1;
799 smallestteam_score = team1_score;
801 if(c2 >= 0 && (c2 < smallestteam_count || (c2 <= smallestteam_count && team2_score < smallestteam_score)))
804 smallestteam_count = c2;
805 smallestteam_score = team2_score;
807 if(c3 >= 0 && (c3 < smallestteam_count || (c3 <= smallestteam_count && team3_score < smallestteam_score)))
810 smallestteam_count = c3;
811 smallestteam_score = team3_score;
813 if(c4 >= 0 && (c4 < smallestteam_count || (c4 <= smallestteam_count && team4_score < smallestteam_score)))
816 smallestteam_count = c4;
817 smallestteam_score = team4_score;
822 if(c1 >= 0 && (c1 < smallestteam_count ||
823 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
826 smallestteam_count = c1;
828 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
829 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
832 smallestteam_count = c2;
834 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
835 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
838 smallestteam_count = c3;
840 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
841 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
844 smallestteam_count = c4;
851 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
853 float smallest, selectedteam;
855 // don't join a team if we're not playing a team game
856 if(!cvar("teamplay") && !g_domination && !g_ctf && !g_keyhunt)
859 // find out what teams are available
860 CheckAllowedTeams(pl);
865 if(cvar("g_domination_default_teams") < 3)
867 if(cvar("g_domination_default_teams") < 4)
871 // if we don't care what team he ends up on, put him on whatever team he entered as.
872 // if he's not on a valid team, then let other code put him on the smallest team
875 if( c1 >= 0 && pl.team == COLOR_TEAM1)
876 selectedteam = pl.team;
877 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
878 selectedteam = pl.team;
879 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
880 selectedteam = pl.team;
881 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
882 selectedteam = pl.team;
887 if(!only_return_best)
889 SetPlayerColors(pl, selectedteam - 1);
894 // otherwise end up on the smallest team (handled below)
897 smallest = FindSmallestTeam(pl, TRUE);
900 if(!only_return_best)
902 TeamchangeFrags(self);
905 SetPlayerColors(pl, COLOR_TEAM1 - 1);
907 else if(smallest == 2)
909 SetPlayerColors(pl, COLOR_TEAM2 - 1);
911 else if(smallest == 3)
913 SetPlayerColors(pl, COLOR_TEAM3 - 1);
915 else if(smallest == 4)
917 SetPlayerColors(pl, COLOR_TEAM4 - 1);
921 error("smallest team: invalid team\n");
924 if(pl.deadflag == DEAD_NO)
925 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
931 //void() ctf_playerchanged;
932 void SV_ChangeTeam(float _color)
934 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
936 // in normal deathmatch we can just apply the color and we're done
937 if(!cvar("teamplay")) {
938 SetPlayerColors(self, _color);
942 scolor = self.clientcolors & 0x0F;
943 dcolor = _color & 0x0F;
945 if(scolor == COLOR_TEAM1 - 1)
947 else if(scolor == COLOR_TEAM2 - 1)
949 else if(scolor == COLOR_TEAM3 - 1)
951 else if(scolor == COLOR_TEAM4 - 1)
953 if(dcolor == COLOR_TEAM1 - 1)
955 else if(dcolor == COLOR_TEAM2 - 1)
957 else if(dcolor == COLOR_TEAM3 - 1)
959 else if(dcolor == COLOR_TEAM4 - 1)
962 CheckAllowedTeams(self);
964 if(dteam == 1 && c1 < 0) dteam = 4;
965 if(dteam == 4 && c4 < 0) dteam = 3;
966 if(dteam == 3 && c3 < 0) dteam = 2;
967 if(dteam == 2 && c2 < 0) dteam = 1;
969 // not changing teams
972 //bprint("same team change\n");
973 SetPlayerTeam(self, dteam, steam, TRUE);
979 if(cvar("g_campaign") || cvar("g_changeteam_banned"))
981 sprint(self, "Team changes not allowed\n");
982 return; // changing teams is not allowed
985 if(!cvar("g_campaign") && cvar("g_balance_teams_prevent_imbalance"))
987 // only allow changing to a smaller or equal size team
989 // find out what teams are available
990 //CheckAllowedTeams();
991 // count how many players on each team
992 GetTeamCounts(world);
995 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
1000 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
1005 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
1010 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
1017 sprint(self, "Cannot change to an invalid team\n");
1022 // get starting team
1023 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
1025 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
1027 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
1029 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
1032 if(scount) // started at a valid, nonempty team
1034 // check if we're trying to change to a larger team that doens't have bots to swap with
1035 if(dcount >= scount && dbotcount <= 0)
1037 sprint(self, "Cannot change to a larger team\n");
1038 return; // can't change to a larger team
1044 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
1046 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1048 // reduce frags during a team change
1049 TeamchangeFrags(self);
1052 SetPlayerTeam(self, dteam, steam, FALSE);
1054 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1056 // kill player when changing teams
1057 if(self.deadflag == DEAD_NO)
1058 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
1060 //ctf_playerchanged();
1063 void ShufflePlayerOutOfTeam (float source_team)
1065 float smallestteam, smallestteam_count, steam;
1066 float lowest_bot_score, lowest_player_score;
1067 entity head, lowest_bot, lowest_player, selected;
1070 smallestteam_count = 999999999;
1072 if(c1 >= 0 && c1 < smallestteam_count)
1075 smallestteam_count = c1;
1077 if(c2 >= 0 && c2 < smallestteam_count)
1080 smallestteam_count = c2;
1082 if(c3 >= 0 && c3 < smallestteam_count)
1085 smallestteam_count = c3;
1087 if(c4 >= 0 && c4 < smallestteam_count)
1090 smallestteam_count = c4;
1095 bprint("warning: no smallest team\n");
1099 if(source_team == 1)
1100 steam = COLOR_TEAM1;
1101 else if(source_team == 2)
1102 steam = COLOR_TEAM2;
1103 else if(source_team == 3)
1104 steam = COLOR_TEAM3;
1105 else if(source_team == 4)
1106 steam = COLOR_TEAM4;
1109 lowest_bot_score = 999999999;
1110 lowest_player = world;
1111 lowest_player_score = 999999999;
1113 // find the lowest-scoring player & bot of that team
1114 FOR_EACH_PLAYER(head)
1116 if(head.team == steam)
1120 if(head.frags < lowest_bot_score)
1123 lowest_bot_score = head.frags;
1128 if(head.frags < lowest_player_score)
1130 lowest_player = head;
1131 lowest_player_score = head.frags;
1137 // prefers to move a bot...
1138 if(lowest_bot != world)
1139 selected = lowest_bot;
1140 // but it will move a player if it has to
1142 selected = lowest_player;
1143 // don't do anything if it couldn't find anyone
1146 bprint("warning: couldn't find a player to move from team\n");
1150 // smallest team gains a member
1151 if(smallestteam == 1)
1155 else if(smallestteam == 2)
1159 else if(smallestteam == 3)
1163 else if(smallestteam == 4)
1169 bprint("warning: destination team invalid\n");
1172 // source team loses a member
1173 if(source_team == 1)
1177 else if(source_team == 2)
1181 else if(source_team == 3)
1185 else if(source_team == 4)
1191 bprint("warning: source team invalid\n");
1195 // move the player to the new team
1196 TeamchangeFrags(selected);
1197 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1199 if(selected.deadflag == DEAD_NO)
1200 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1201 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1204 float lastRebalanceInfo;
1205 void CauseRebalance(float source_team, float howmany_toomany)
1210 if(IsTeamBalanceForced() == 1)
1212 bprint("Rebalancing Teams\n");
1213 ShufflePlayerOutOfTeam(source_team);
1217 if(howmany_toomany < cvar("g_balance_teams_complain"))
1219 if(time < lastRebalanceInfo + 90)
1221 lastRebalanceInfo = time;
1222 if(source_team == 1)
1223 steam = COLOR_TEAM1;
1224 else if(source_team == 2)
1225 steam = COLOR_TEAM2;
1226 else if(source_team == 3)
1227 steam = COLOR_TEAM3;
1228 else if(source_team == 4)
1229 steam = COLOR_TEAM4;
1230 ServerConsoleEcho(strcat("Team ", ftos(source_team), " too large, complaining."), TRUE);
1231 FOR_EACH_REALPLAYER(head)
1233 if(head.team == steam)
1235 sprint(head, "\{1}\{13}^3SERVER NOTICE:^7 One of you please change teams!\n");
1236 centerprint_atprio(head, CENTERPRIO_REBALANCE, "^3SERVER NOTICE:\n\n^7Someone of you please change teams!");
1242 // part of g_balance_teams_force
1243 // occasionally perform an audit of the teams to make
1244 // sure they're more or less balanced in player count.
1247 float numplayers, numteams, smallest, toomany;
1249 balance = IsTeamBalanceForced();
1253 if(audit_teams_time > time)
1256 audit_teams_time = time + 4 + random();
1258 // bprint("Auditing teams\n");
1260 CheckAllowedTeams(world);
1261 GetTeamCounts(world);
1264 numteams = numplayers = smallest = 0;
1267 numteams = numteams + 1;
1268 numplayers = numplayers + c1;
1273 numteams = numteams + 1;
1274 numplayers = numplayers + c2;
1280 numteams = numteams + 1;
1281 numplayers = numplayers + c3;
1287 numteams = numteams + 1;
1288 numplayers = numplayers + c4;
1294 return; // no players to move around
1296 return; // don't bother shuffling if for some reason there aren't any teams
1298 toomany = smallest + 1;
1300 if(c1 && c1 > toomany)
1301 CauseRebalance(1, c1 - toomany);
1302 if(c2 && c2 > toomany)
1303 CauseRebalance(2, c2 - toomany);
1304 if(c3 && c3 > toomany)
1305 CauseRebalance(3, c3 - toomany);
1306 if(c4 && c4 > toomany)
1307 CauseRebalance(4, c4 - toomany);
1309 // if teams are still unbalanced, balance them further in the next audit,
1310 // which will happen sooner (keep doing rapid audits until things are in order)
1311 audit_teams_time = time + 0.7 + random()*0.3;
1316 /*void UpdateTeamScore(entity e, float first)
1321 WriteByte (MSG_ALL, SVC_UPDATENAME);
1322 WriteByte (MSG_ALL, clientno);
1323 WriteString (MSG_ALL, e.netname);
1325 WriteByte (MSG_ALL, SVC_UPDATECOLORS);
1326 WriteByte (MSG_ALL, clientno);
1327 WriteByte (MSG_ALL, e.b_shirt * 16 + who.b_pants);
1330 WriteByte (MSG_ALL, SVC_UPDATEFRAGS);
1331 WriteByte (MSG_ALL, clientno);
1332 WriteShort (MSG_ALL, e.frags + 10000);
1338 // code from here on is just to support maps that don't have team entities
1339 void tdm_spawnteam (string teamname, float teamcolor)
1343 e.classname = "tdm_team";
1344 e.netname = teamname;
1349 // spawn some default teams if the map is not set up for tdm
1350 void tdm_spawnteams()
1354 numteams = cvar("g_tdm_teams");
1356 tdm_spawnteam("Red", COLOR_TEAM1-1);
1357 tdm_spawnteam("Blue", COLOR_TEAM2-1);
1359 tdm_spawnteam("Yellow", COLOR_TEAM3-1);
1361 tdm_spawnteam("Pink", COLOR_TEAM4-1);
1364 void tdm_delayedinit()
1366 self.think = SUB_Remove;
1367 self.nextthink = time;
1368 // if no teams are found, spawn defaults
1369 if (find(world, classname, "tdm_team") == world)
1377 e.think = tdm_delayedinit;
1378 e.nextthink = time + 0.1;