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)
32 if(e.classname == "player")
34 // reduce frags during a team change
35 DistributeFragsAmongTeam(e, (e.clientcolors & 0x0F) + 1, 1 - cvar("g_changeteam_fragtransfer") / 100);
39 vector TeamColor(float teem)
44 return '1 0.0625 0.0625';
46 return '0.0625 0.0625 1';
56 string TeamName(float t)
58 return strcat(Team_ColorName(t), " Team");
60 string ColoredTeamName(float t)
62 return strcat(Team_ColorCode(t), Team_ColorName(t), " Team^7");
64 string TeamNoName(float t)
66 // fixme: Search for team entities and get their .netname's!
75 return "Neutral Team";
80 void runematch_init();
84 void LogTeamchange(entity pl)
87 if(!cvar("sv_eventlog"))
91 str = strcat(":team:", ftos(pl.playerid), ":");
92 str = strcat(str, ftos(pl.team));
93 GameLogEcho(str, FALSE);
98 cvar_set("g_dm", "0");
99 cvar_set("g_tdm", "0");
100 cvar_set("g_domination", "0");
101 cvar_set("g_ctf", "0");
102 cvar_set("g_runematch", "0");
103 cvar_set("g_lms", "0");
104 cvar_set("g_arena", "0");
105 cvar_set("g_keyhunt", "0");
106 cvar_set("g_assault", "0");
107 cvar_set("g_onslaught", "0");
108 cvar_set("teamplay", "0");
111 void ActivateTeamplay()
113 float teamplay_default;
114 teamplay_default = cvar("teamplay_default");
117 cvar_set("teamplay", ftos(teamplay_default));
119 cvar_set("teamplay", "3");
122 void InitGameplayMode()
124 float fraglimit_override, timelimit_override, capturelimit_override;
128 game = cvar("gamecfg"); // load game options
130 // game cvars get reset before map changes
131 // then map's cfg sets them as desired
133 // FIXME: also set a message or game mode name to print to players when the join
135 // set both here, gamemode can override it later
136 timelimit_override = cvar("timelimit_override");
137 fraglimit_override = cvar("fraglimit_override");
138 capturelimit_override = cvar("capturelimit_override");
140 if(game == GAME_DOMINATION || cvar("g_domination"))
143 game = GAME_DOMINATION;
144 cvar_set("g_domination", "1");
148 fraglimit_override = cvar("g_domination_point_limit");
150 gamemode_name = "Domination";
153 else if(game == GAME_CTF || cvar("g_ctf"))
157 cvar_set("g_ctf", "1");
161 fraglimit_override = cvar("g_ctf_capture_limit");
162 //no capture limit override here
164 gamemode_name = "Capture the Flag";
167 else if(game == GAME_RUNEMATCH || cvar("g_runematch"))
169 game = GAME_RUNEMATCH;
170 cvar_set("g_runematch", "1");
171 cvar_set("g_minstagib", "0");
173 if(cvar("deathmatch_force_teamplay"))
176 fraglimit_override = cvar("g_runematch_point_limit");
178 gamemode_name = "Rune Match";
184 else if(game == GAME_DEATHMATCH || game == GAME_TEAM_DEATHMATCH || cvar("g_tdm"))
186 if(!cvar("deathmatch"))
187 cvar_set("deathmatch", "1");
190 if(game == GAME_TEAM_DEATHMATCH || cvar("g_tdm") || cvar("deathmatch_force_teamplay"))
193 game = GAME_TEAM_DEATHMATCH;
194 gamemode_name = "Team Deathmatch";
197 cvar_set("g_tdm", "1");
202 game = GAME_DEATHMATCH;
203 gamemode_name = "Deathmatch";
207 fraglimit_override = cvar("fraglimit_override");
209 else if(game == GAME_ASSAULT || cvar("g_assault"))
213 gamemode_name = "Assault";
216 cvar_set("g_assault", "1");
218 else if(game == GAME_LMS || cvar("g_lms"))
222 cvar_set("g_lms", "1");
223 fraglimit_override = cvar("g_lms_lives_override");
224 if(fraglimit_override == 0)
225 fraglimit_override = -1;
226 gamemode_name = "Last Man Standing";
228 lms_lowest_lives = 999;
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)
331 string GetClientVersionMessage(float v) {
332 local string versionmsg;
334 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";
335 // either that or someone wants to be funny
336 } else if (v != cvar("gameversion")) {
337 if(v < cvar("gameversion")) {
338 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
340 versionmsg = "^3This server is using an outdated Nexuiz version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
343 versionmsg = "^2client version and server version are compatible.^8";
349 void PrintWelcomeMessage(entity pl)
351 string s, mutator, modifications, padding;
353 /*if(self.welcomemessage_time > time)
355 self.welcomemessage_time = time + 0.8; */
357 if(self.cvar_scr_centertime == 0) return;
358 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
359 if(self.welcomemessage_time > time) return;
360 self.welcomemessage_time = time + self.cvar_scr_centertime * 0.6;
363 if(cvar("g_campaign"))
365 centerprint(pl, campaign_message);
369 if(!self.BUTTON_INFO)
371 if(self.classname == "observer")
373 if(g_lms && self.frags <= 0 && self.frags > -666)
374 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"));
375 else if(g_lms && self.frags == -666)
376 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"));
378 else if(self.classname == "spectator")
380 if ((g_lms && self.frags < 1) || g_arena)
381 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"));
383 local string specString;
384 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");
386 if(time < restart_countdown) //also show the countdown when being a spectator
387 specString = strcat(specString, "\n\n^1Game starts in ", ftos(restartAnnouncer.cnt + 1), " seconds^7");
388 else if (timeoutStatus != 0)
389 specString = strcat(specString, "\n\n", getTimeoutText(1));
390 return centerprint_atprio(self, CENTERPRIO_SPAM, specString);
396 mutator = "^2Minstagib ^1";
398 mutator = "^2Instagib ^1";
399 else if(g_rocketarena)
400 mutator = "^2Rocketarena ^1";
402 mutator = "^2No Items Nexuiz ^1";
405 // to protect against unheedingly made changes
407 modifications = strcat(modifications, ", ");
409 modifications = "midair";
413 modifications = strcat(modifications, ", ");
415 modifications = strcat(modifications, "vampire");
417 if(g_laserguided_missile) {
419 modifications = strcat(modifications, ", ");
421 modifications = strcat(modifications, "laser-guided-missiles");
425 modifications = strcat(modifications, ", ");
427 modifications = strcat(modifications, "Tournament");
430 local string versionmessage;
431 versionmessage = GetClientVersionMessage(self.version);
433 s = strcat(s, NEWLINES, "This is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
434 s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
436 if(modifications != "")
437 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
440 if(!tourneyInMatchStage)
441 s = strcat(s, "\n^8The game is currently in ^3warmup-stage ^8!\n");
443 s = strcat(s, "\n^8The game is currently in ^3match-stage ^8!\n");
446 if(time < restart_countdown)
447 s = strcat(s, "\n^1Game starts in ", ftos(restartAnnouncer.cnt + 1), " seconds^7");
449 if(timeoutStatus != 0)
450 s = strcat(s, "\n\n", getTimeoutText(1));
452 if (g_grappling_hook)
453 s = strcat(s, "\n\n^8grappling hook is enabled, press 'e' to use it\n");
455 if(cache_lastmutatormsg != cvar_string("g_mutatormsg"))
457 if(cache_lastmutatormsg)
458 strunzone(cache_lastmutatormsg);
460 strunzone(cache_mutatormsg);
461 cache_lastmutatormsg = strzone(cvar_string("g_mutatormsg"));
462 cache_mutatormsg = strzone(wordwrap(cache_lastmutatormsg, 50));
465 if (cache_mutatormsg != "") {
466 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
469 if(cache_lastmotd != cvar_string("sv_motd"))
472 strunzone(cache_lastmotd);
474 strunzone(cache_motd);
475 cache_lastmotd = strzone(cvar_string("sv_motd"));
476 cache_motd = strzone(wordwrap(cache_lastmotd, 50));
479 if (cache_motd != "") {
480 s = strcat(s, "\n\n^8MOTD: ^7", cache_motd);
484 if(cvar("fraglimit"))
487 if(cvar("timelimit"))
490 s = strcat(s, "\n^8frag limit: ^7", cvar_string("fraglimit"), padding);
492 if(cvar("timelimit"))
493 s = strcat(s, "\n^8time limit: ^7", cvar_string("timelimit"), " minutes");
500 void SetPlayerColors(entity pl, float _color)
504 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
506 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
507 pl.clientcolors = 16*cl + cl;*/
510 pants = _color & 0x0F;
511 shirt = _color & 0xF0;
515 setcolor(pl, 16*pants + pants);
517 setcolor(pl, shirt + pants);
521 void SetPlayerTeam(entity pl, float t, float s, float noprint)
526 _color = COLOR_TEAM4 - 1;
528 _color = COLOR_TEAM3 - 1;
530 _color = COLOR_TEAM2 - 1;
532 _color = COLOR_TEAM1 - 1;
534 SetPlayerColors(pl,_color);
536 if(!noprint && t != s)
538 //bprint(pl.netname, " has changed to ", TeamNoName(t), "\n");
539 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
551 // set c1...c4 to show what teams are allowed
552 void CheckAllowedTeams (entity for_whom)
561 c1 = c2 = c3 = c4 = -1;
562 cb1 = cb2 = cb3 = cb4 = 0;
564 // onslaught is special
567 head = findchain(classname, "onslaught_generator");
570 if (head.team == COLOR_TEAM1) c1 = 0;
571 if (head.team == COLOR_TEAM2) c2 = 0;
572 if (head.team == COLOR_TEAM3) c3 = 0;
573 if (head.team == COLOR_TEAM4) c4 = 0;
580 teament_name = "dom_team";
582 teament_name = "ctf_team";
584 teament_name = "tdm_team";
587 c1 = c2 = 0; // Assault always has 2 teams
592 // cover anything else by treating it like tdm with no teams spawned
596 dm = cvar("g_tdm_teams");
598 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
602 c1 = c2 = c3 = c4 = 0;
615 // first find out what teams are allowed
616 head = find(world, classname, teament_name);
619 if(!(g_domination && head.netname == ""))
621 if(head.team == COLOR_TEAM1)
625 if(head.team == COLOR_TEAM2)
629 if(head.team == COLOR_TEAM3)
633 if(head.team == COLOR_TEAM4)
638 head = find(head, classname, teament_name);
641 if(cvar("bot_vs_human") > 0)
644 if(clienttype(for_whom) == CLIENTTYPE_BOT)
649 else if(cvar("bot_vs_human") < 0)
652 if(clienttype(for_whom) == CLIENTTYPE_BOT)
659 float PlayerValue(entity p)
661 if(IsTeamBalanceForced() == 1)
666 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
667 // teams that are allowed will now have their player counts stored in c1...c4
668 void GetTeamCounts(entity ignore)
672 // now count how many players are on each team already
674 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
675 // also remember the lowest-scoring player
677 FOR_EACH_PLAYER(head)
679 if(head != ignore)// && head.netname != "")
681 value = PlayerValue(head);
682 if(clienttype(head) == CLIENTTYPE_BOT)
686 if(head.team == COLOR_TEAM1)
694 if(head.team == COLOR_TEAM2)
702 if(head.team == COLOR_TEAM3)
710 if(head.team == COLOR_TEAM4)
722 // returns # of smallest team (1, 2, 3, 4)
723 // NOTE: Assumes CheckAllowedTeams has already been called!
724 float FindSmallestTeam(entity pl, float ignore_pl)
726 float totalteams, smallestteam, smallestteam_count, smallestteam_score, balance_type;
729 // find out what teams are available
730 //CheckAllowedTeams();
732 // make sure there are at least 2 teams to join
734 totalteams = totalteams + 1;
736 totalteams = totalteams + 1;
738 totalteams = totalteams + 1;
740 totalteams = totalteams + 1;
742 if(cvar("bot_vs_human"))
748 error("Too few teams available for domination\n");
750 error("Too few teams available for ctf\n");
752 error("Too few teams available for key hunt\n");
754 error("Too few teams available for team deathmatch\n");
757 // count how many players are in each team
761 GetTeamCounts(world);
763 // c1...c4 now have counts of each team
764 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
767 smallestteam_count = 999999999;
768 smallestteam_score = 999999999;
770 // 2 gives priority to what team you're already on, 1 goes in order
771 // 2 doesn't seem to work though...
775 //if(pl.classname != "player")
776 if(clienttype(pl) != CLIENTTYPE_BOT)
778 c1 -= cb1 * 255.0/256;
779 c2 -= cb2 * 255.0/256;
780 c3 -= cb3 * 255.0/256;
781 c4 -= cb4 * 255.0/256;
784 if(balance_type == 1)
786 if(c1 >= 0 && (c1 < smallestteam_count || (c1 <= smallestteam_count && team1_score < smallestteam_score)))
789 smallestteam_count = c1;
790 smallestteam_score = team1_score;
792 if(c2 >= 0 && (c2 < smallestteam_count || (c2 <= smallestteam_count && team2_score < smallestteam_score)))
795 smallestteam_count = c2;
796 smallestteam_score = team2_score;
798 if(c3 >= 0 && (c3 < smallestteam_count || (c3 <= smallestteam_count && team3_score < smallestteam_score)))
801 smallestteam_count = c3;
802 smallestteam_score = team3_score;
804 if(c4 >= 0 && (c4 < smallestteam_count || (c4 <= smallestteam_count && team4_score < smallestteam_score)))
807 smallestteam_count = c4;
808 smallestteam_score = team4_score;
813 if(c1 >= 0 && (c1 < smallestteam_count ||
814 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
817 smallestteam_count = c1;
819 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
820 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
823 smallestteam_count = c2;
825 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
826 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
829 smallestteam_count = c3;
831 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
832 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
835 smallestteam_count = c4;
842 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
844 float smallest, selectedteam;
846 // don't join a team if we're not playing a team game
847 if(!cvar("teamplay") && !g_domination && !g_ctf && !g_keyhunt)
850 // find out what teams are available
851 CheckAllowedTeams(pl);
856 if(cvar("g_domination_default_teams") < 3)
858 if(cvar("g_domination_default_teams") < 4)
862 // if we don't care what team he ends up on, put him on whatever team he entered as.
863 // if he's not on a valid team, then let other code put him on the smallest team
866 if( c1 >= 0 && pl.team == COLOR_TEAM1)
867 selectedteam = pl.team;
868 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
869 selectedteam = pl.team;
870 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
871 selectedteam = pl.team;
872 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
873 selectedteam = pl.team;
878 if(!only_return_best)
880 SetPlayerColors(pl, selectedteam - 1);
885 // otherwise end up on the smallest team (handled below)
888 smallest = FindSmallestTeam(pl, TRUE);
891 if(!only_return_best)
893 TeamchangeFrags(self);
896 SetPlayerColors(pl, COLOR_TEAM1 - 1);
898 else if(smallest == 2)
900 SetPlayerColors(pl, COLOR_TEAM2 - 1);
902 else if(smallest == 3)
904 SetPlayerColors(pl, COLOR_TEAM3 - 1);
906 else if(smallest == 4)
908 SetPlayerColors(pl, COLOR_TEAM4 - 1);
912 error("smallest team: invalid team\n");
915 if(pl.deadflag == DEAD_NO)
916 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
922 //void() ctf_playerchanged;
923 void SV_ChangeTeam(float _color)
925 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
927 // in normal deathmatch we can just apply the color and we're done
928 if(!cvar("teamplay")) {
929 SetPlayerColors(self, _color);
933 scolor = self.clientcolors & 0x0F;
934 dcolor = _color & 0x0F;
936 if(scolor == COLOR_TEAM1 - 1)
938 else if(scolor == COLOR_TEAM2 - 1)
940 else if(scolor == COLOR_TEAM3 - 1)
942 else if(scolor == COLOR_TEAM4 - 1)
944 if(dcolor == COLOR_TEAM1 - 1)
946 else if(dcolor == COLOR_TEAM2 - 1)
948 else if(dcolor == COLOR_TEAM3 - 1)
950 else if(dcolor == COLOR_TEAM4 - 1)
953 CheckAllowedTeams(self);
955 if(dteam == 1 && c1 < 0) dteam = 4;
956 if(dteam == 4 && c4 < 0) dteam = 3;
957 if(dteam == 3 && c3 < 0) dteam = 2;
958 if(dteam == 2 && c2 < 0) dteam = 1;
960 // not changing teams
963 //bprint("same team change\n");
964 SetPlayerTeam(self, dteam, steam, TRUE);
970 if(cvar("g_campaign") || cvar("g_changeteam_banned"))
972 sprint(self, "Team changes not allowed\n");
973 return; // changing teams is not allowed
976 if(!cvar("g_campaign") && cvar("g_balance_teams_prevent_imbalance"))
978 // only allow changing to a smaller or equal size team
980 // find out what teams are available
981 //CheckAllowedTeams();
982 // count how many players on each team
983 GetTeamCounts(world);
986 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
991 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
996 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
1001 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
1008 sprint(self, "Cannot change to an invalid team\n");
1013 // get starting team
1014 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
1016 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
1018 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
1020 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
1023 if(scount) // started at a valid, nonempty team
1025 // check if we're trying to change to a larger team that doens't have bots to swap with
1026 if(dcount >= scount && dbotcount <= 0)
1028 sprint(self, "Cannot change to a larger team\n");
1029 return; // can't change to a larger team
1035 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
1037 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1039 // reduce frags during a team change
1040 TeamchangeFrags(self);
1043 SetPlayerTeam(self, dteam, steam, FALSE);
1045 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1047 // kill player when changing teams
1048 if(self.deadflag == DEAD_NO)
1049 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
1051 //ctf_playerchanged();
1054 void ShufflePlayerOutOfTeam (float source_team)
1056 float smallestteam, smallestteam_count, steam;
1057 float lowest_bot_score, lowest_player_score;
1058 entity head, lowest_bot, lowest_player, selected;
1061 smallestteam_count = 999999999;
1063 if(c1 >= 0 && c1 < smallestteam_count)
1066 smallestteam_count = c1;
1068 if(c2 >= 0 && c2 < smallestteam_count)
1071 smallestteam_count = c2;
1073 if(c3 >= 0 && c3 < smallestteam_count)
1076 smallestteam_count = c3;
1078 if(c4 >= 0 && c4 < smallestteam_count)
1081 smallestteam_count = c4;
1086 bprint("warning: no smallest team\n");
1090 if(source_team == 1)
1091 steam = COLOR_TEAM1;
1092 else if(source_team == 2)
1093 steam = COLOR_TEAM2;
1094 else if(source_team == 3)
1095 steam = COLOR_TEAM3;
1096 else if(source_team == 4)
1097 steam = COLOR_TEAM4;
1100 lowest_bot_score = 999999999;
1101 lowest_player = world;
1102 lowest_player_score = 999999999;
1104 // find the lowest-scoring player & bot of that team
1105 FOR_EACH_PLAYER(head)
1107 if(head.team == steam)
1111 if(head.frags < lowest_bot_score)
1114 lowest_bot_score = head.frags;
1119 if(head.frags < lowest_player_score)
1121 lowest_player = head;
1122 lowest_player_score = head.frags;
1128 // prefers to move a bot...
1129 if(lowest_bot != world)
1130 selected = lowest_bot;
1131 // but it will move a player if it has to
1133 selected = lowest_player;
1134 // don't do anything if it couldn't find anyone
1137 bprint("warning: couldn't find a player to move from team\n");
1141 // smallest team gains a member
1142 if(smallestteam == 1)
1146 else if(smallestteam == 2)
1150 else if(smallestteam == 3)
1154 else if(smallestteam == 4)
1160 bprint("warning: destination team invalid\n");
1163 // source team loses a member
1164 if(source_team == 1)
1168 else if(source_team == 2)
1172 else if(source_team == 3)
1176 else if(source_team == 4)
1182 bprint("warning: source team invalid\n");
1186 // move the player to the new team
1187 TeamchangeFrags(selected);
1188 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1190 if(selected.deadflag == DEAD_NO)
1191 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1192 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1195 float lastRebalanceInfo;
1196 void CauseRebalance(float source_team, float howmany_toomany)
1201 if(IsTeamBalanceForced() == 1)
1203 bprint("Rebalancing Teams\n");
1204 ShufflePlayerOutOfTeam(source_team);
1208 if(howmany_toomany < cvar("g_balance_teams_complain"))
1210 if(time < lastRebalanceInfo + 90)
1212 lastRebalanceInfo = time;
1213 if(source_team == 1)
1214 steam = COLOR_TEAM1;
1215 else if(source_team == 2)
1216 steam = COLOR_TEAM2;
1217 else if(source_team == 3)
1218 steam = COLOR_TEAM3;
1219 else if(source_team == 4)
1220 steam = COLOR_TEAM4;
1221 ServerConsoleEcho(strcat("Team ", ftos(source_team), " too large, complaining."), TRUE);
1222 FOR_EACH_REALPLAYER(head)
1224 if(head.team == steam)
1226 sprint(head, "\{1}\{13}^3SERVER NOTICE:^7 One of you please change teams!\n");
1227 centerprint_atprio(head, CENTERPRIO_REBALANCE, "^3SERVER NOTICE:\n\n^7Someone of you please change teams!");
1233 // part of g_balance_teams_force
1234 // occasionally perform an audit of the teams to make
1235 // sure they're more or less balanced in player count.
1238 float numplayers, numteams, smallest, toomany;
1240 balance = IsTeamBalanceForced();
1244 if(audit_teams_time > time)
1247 audit_teams_time = time + 4 + random();
1249 // bprint("Auditing teams\n");
1251 CheckAllowedTeams(world);
1252 GetTeamCounts(world);
1255 numteams = numplayers = smallest = 0;
1258 numteams = numteams + 1;
1259 numplayers = numplayers + c1;
1264 numteams = numteams + 1;
1265 numplayers = numplayers + c2;
1271 numteams = numteams + 1;
1272 numplayers = numplayers + c3;
1278 numteams = numteams + 1;
1279 numplayers = numplayers + c4;
1285 return; // no players to move around
1287 return; // don't bother shuffling if for some reason there aren't any teams
1289 toomany = smallest + 1;
1291 if(c1 && c1 > toomany)
1292 CauseRebalance(1, c1 - toomany);
1293 if(c2 && c2 > toomany)
1294 CauseRebalance(2, c2 - toomany);
1295 if(c3 && c3 > toomany)
1296 CauseRebalance(3, c3 - toomany);
1297 if(c4 && c4 > toomany)
1298 CauseRebalance(4, c4 - toomany);
1300 // if teams are still unbalanced, balance them further in the next audit,
1301 // which will happen sooner (keep doing rapid audits until things are in order)
1302 audit_teams_time = time + 0.7 + random()*0.3;
1307 /*void UpdateTeamScore(entity e, float first)
1312 WriteByte (MSG_ALL, SVC_UPDATENAME);
1313 WriteByte (MSG_ALL, clientno);
1314 WriteString (MSG_ALL, e.netname);
1316 WriteByte (MSG_ALL, SVC_UPDATECOLORS);
1317 WriteByte (MSG_ALL, clientno);
1318 WriteByte (MSG_ALL, e.b_shirt * 16 + who.b_pants);
1321 WriteByte (MSG_ALL, SVC_UPDATEFRAGS);
1322 WriteByte (MSG_ALL, clientno);
1323 WriteShort (MSG_ALL, e.frags + 10000);
1329 // code from here on is just to support maps that don't have team entities
1330 void tdm_spawnteam (string teamname, float teamcolor)
1334 e.classname = "tdm_team";
1335 e.netname = teamname;
1340 // spawn some default teams if the map is not set up for tdm
1341 void tdm_spawnteams()
1345 numteams = cvar("g_tdm_teams");
1347 tdm_spawnteam("Red", COLOR_TEAM1-1);
1348 tdm_spawnteam("Blue", COLOR_TEAM2-1);
1350 tdm_spawnteam("Yellow", COLOR_TEAM3-1);
1352 tdm_spawnteam("Pink", COLOR_TEAM4-1);
1355 void tdm_delayedinit()
1357 self.think = SUB_Remove;
1358 self.nextthink = time;
1359 // if no teams are found, spawn defaults
1360 if (find(world, classname, "tdm_team") == world)
1368 e.think = tdm_delayedinit;
1369 e.nextthink = time + 0.1;