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";
345 return strzone(versionmsg);
350 void PrintWelcomeMessage(entity pl)
352 string s, mutator, modifications, padding;
354 /*if(self.welcomemessage_time > time)
356 self.welcomemessage_time = time + 0.8; */
358 if(self.cvar_scr_centertime == 0) return;
359 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
360 if(self.welcomemessage_time > time) return;
361 self.welcomemessage_time = time + self.cvar_scr_centertime * 0.6;
364 if(cvar("g_campaign"))
366 centerprint(pl, campaign_message);
370 if(!self.BUTTON_INFO)
372 if(self.classname == "observer")
374 if(g_lms && self.frags <= 0 && self.frags > -666)
375 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"));
376 else if(g_lms && self.frags == -666)
377 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"));
379 else if(self.classname == "spectator")
381 if ((g_lms && self.frags < 1) || g_arena)
382 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"));
384 local string specString;
385 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");
387 if(time < restart_countdown) //also show the countdown when being a spectator
388 specString = strcat(specString, "\n\n^1Game starts in ", ftos(restartAnnouncer.cnt + 1), " seconds^7");
389 else if (timeoutStatus != 0)
390 specString = strcat(specString, "\n\n", getTimeoutText(1));
391 return centerprint_atprio(self, CENTERPRIO_SPAM, specString);
397 mutator = "^2Minstagib ^1";
399 mutator = "^2Instagib ^1";
400 else if(g_rocketarena)
401 mutator = "^2Rocketarena ^1";
403 mutator = "^2No Items Nexuiz ^1";
406 // to protect against unheedingly made changes
408 modifications = strcat(modifications, ", ");
410 modifications = "midair";
414 modifications = strcat(modifications, ", ");
416 modifications = strcat(modifications, "vampire");
418 if(g_laserguided_missile) {
420 modifications = strcat(modifications, ", ");
422 modifications = strcat(modifications, "laser-guided-missiles");
426 modifications = strcat(modifications, ", ");
428 modifications = strcat(modifications, "Tournament");
431 local string versionmessage;
432 versionmessage = GetClientVersionMessage(self.version);
434 s = strcat(s, NEWLINES, "This is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
435 s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
437 if(modifications != "")
438 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
441 if(!tourneyInMatchStage)
442 s = strcat(s, "\n^8The game is currently in ^3warmup-stage ^8!\n");
444 s = strcat(s, "\n^8The game is currently in ^3match-stage ^8!\n");
447 if(time < restart_countdown)
448 s = strcat(s, "\n^1Game starts in ", ftos(restartAnnouncer.cnt + 1), " seconds^7");
450 if(timeoutStatus != 0)
451 s = strcat(s, "\n\n", getTimeoutText(1));
455 if (g_grappling_hook)
456 s = strcat(s, "\n\n^8grappling hook is enabled, press 'e' to use it\n");
458 if(cache_lastmutatormsg != cvar_string("g_mutatormsg"))
460 if(cache_lastmutatormsg)
461 strunzone(cache_lastmutatormsg);
463 strunzone(cache_mutatormsg);
464 cache_lastmutatormsg = strzone(cvar_string("g_mutatormsg"));
465 cache_mutatormsg = strzone(wordwrap(cache_lastmutatormsg, 50));
468 if (cache_mutatormsg != "") {
469 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
472 if(cache_lastmotd != cvar_string("sv_motd"))
475 strunzone(cache_lastmotd);
477 strunzone(cache_motd);
478 cache_lastmotd = strzone(cvar_string("sv_motd"));
479 cache_motd = strzone(wordwrap(cache_lastmotd, 50));
482 if (cache_motd != "") {
483 s = strcat(s, "\n\n^8MOTD: ^7", cache_motd);
487 if(cvar("fraglimit"))
490 if(cvar("timelimit"))
493 s = strcat(s, "\n^8frag limit: ^7", cvar_string("fraglimit"), padding);
495 if(cvar("timelimit"))
496 s = strcat(s, "\n^8time limit: ^7", cvar_string("timelimit"), " minutes");
507 void SetPlayerColors(entity pl, float _color)
511 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
513 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
514 pl.clientcolors = 16*cl + cl;*/
517 pants = _color & 0x0F;
518 shirt = _color & 0xF0;
522 setcolor(pl, 16*pants + pants);
524 setcolor(pl, shirt + pants);
528 void SetPlayerTeam(entity pl, float t, float s, float noprint)
533 _color = COLOR_TEAM4 - 1;
535 _color = COLOR_TEAM3 - 1;
537 _color = COLOR_TEAM2 - 1;
539 _color = COLOR_TEAM1 - 1;
541 SetPlayerColors(pl,_color);
543 if(!noprint && t != s)
545 //bprint(pl.netname, " has changed to ", TeamNoName(t), "\n");
546 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
558 // set c1...c4 to show what teams are allowed
559 void CheckAllowedTeams (entity for_whom)
568 c1 = c2 = c3 = c4 = -1;
569 cb1 = cb2 = cb3 = cb4 = 0;
571 // onslaught is special
574 head = findchain(classname, "onslaught_generator");
577 if (head.team == COLOR_TEAM1) c1 = 0;
578 if (head.team == COLOR_TEAM2) c2 = 0;
579 if (head.team == COLOR_TEAM3) c3 = 0;
580 if (head.team == COLOR_TEAM4) c4 = 0;
587 teament_name = "dom_team";
589 teament_name = "ctf_team";
591 teament_name = "tdm_team";
594 c1 = c2 = 0; // Assault always has 2 teams
599 // cover anything else by treating it like tdm with no teams spawned
603 dm = cvar("g_tdm_teams");
605 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
609 c1 = c2 = c3 = c4 = 0;
622 // first find out what teams are allowed
623 head = find(world, classname, teament_name);
626 if(!(g_domination && head.netname == ""))
628 if(head.team == COLOR_TEAM1)
632 if(head.team == COLOR_TEAM2)
636 if(head.team == COLOR_TEAM3)
640 if(head.team == COLOR_TEAM4)
645 head = find(head, classname, teament_name);
648 if(cvar("bot_vs_human") > 0)
651 if(clienttype(for_whom) == CLIENTTYPE_BOT)
656 else if(cvar("bot_vs_human") < 0)
659 if(clienttype(for_whom) == CLIENTTYPE_BOT)
666 float PlayerValue(entity p)
668 if(IsTeamBalanceForced() == 1)
673 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
674 // teams that are allowed will now have their player counts stored in c1...c4
675 void GetTeamCounts(entity ignore)
679 // now count how many players are on each team already
681 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
682 // also remember the lowest-scoring player
684 FOR_EACH_PLAYER(head)
686 if(head != ignore)// && head.netname != "")
688 value = PlayerValue(head);
689 if(clienttype(head) == CLIENTTYPE_BOT)
693 if(head.team == COLOR_TEAM1)
701 if(head.team == COLOR_TEAM2)
709 if(head.team == COLOR_TEAM3)
717 if(head.team == COLOR_TEAM4)
729 // returns # of smallest team (1, 2, 3, 4)
730 // NOTE: Assumes CheckAllowedTeams has already been called!
731 float FindSmallestTeam(entity pl, float ignore_pl)
733 float totalteams, smallestteam, smallestteam_count, smallestteam_score, balance_type;
736 // find out what teams are available
737 //CheckAllowedTeams();
739 // make sure there are at least 2 teams to join
741 totalteams = totalteams + 1;
743 totalteams = totalteams + 1;
745 totalteams = totalteams + 1;
747 totalteams = totalteams + 1;
749 if(cvar("bot_vs_human"))
755 error("Too few teams available for domination\n");
757 error("Too few teams available for ctf\n");
759 error("Too few teams available for key hunt\n");
761 error("Too few teams available for team deathmatch\n");
764 // count how many players are in each team
768 GetTeamCounts(world);
770 // c1...c4 now have counts of each team
771 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
774 smallestteam_count = 999999999;
775 smallestteam_score = 999999999;
777 // 2 gives priority to what team you're already on, 1 goes in order
778 // 2 doesn't seem to work though...
782 //if(pl.classname != "player")
783 if(clienttype(pl) != CLIENTTYPE_BOT)
785 c1 -= cb1 * 255.0/256;
786 c2 -= cb2 * 255.0/256;
787 c3 -= cb3 * 255.0/256;
788 c4 -= cb4 * 255.0/256;
791 if(balance_type == 1)
793 if(c1 >= 0 && (c1 < smallestteam_count || (c1 <= smallestteam_count && team1_score < smallestteam_score)))
796 smallestteam_count = c1;
797 smallestteam_score = team1_score;
799 if(c2 >= 0 && (c2 < smallestteam_count || (c2 <= smallestteam_count && team2_score < smallestteam_score)))
802 smallestteam_count = c2;
803 smallestteam_score = team2_score;
805 if(c3 >= 0 && (c3 < smallestteam_count || (c3 <= smallestteam_count && team3_score < smallestteam_score)))
808 smallestteam_count = c3;
809 smallestteam_score = team3_score;
811 if(c4 >= 0 && (c4 < smallestteam_count || (c4 <= smallestteam_count && team4_score < smallestteam_score)))
814 smallestteam_count = c4;
815 smallestteam_score = team4_score;
820 if(c1 >= 0 && (c1 < smallestteam_count ||
821 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
824 smallestteam_count = c1;
826 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
827 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
830 smallestteam_count = c2;
832 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
833 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
836 smallestteam_count = c3;
838 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
839 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
842 smallestteam_count = c4;
849 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
851 float smallest, selectedteam;
853 // don't join a team if we're not playing a team game
854 if(!cvar("teamplay") && !g_domination && !g_ctf && !g_keyhunt)
857 // find out what teams are available
858 CheckAllowedTeams(pl);
863 if(cvar("g_domination_default_teams") < 3)
865 if(cvar("g_domination_default_teams") < 4)
869 // if we don't care what team he ends up on, put him on whatever team he entered as.
870 // if he's not on a valid team, then let other code put him on the smallest team
873 if( c1 >= 0 && pl.team == COLOR_TEAM1)
874 selectedteam = pl.team;
875 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
876 selectedteam = pl.team;
877 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
878 selectedteam = pl.team;
879 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
880 selectedteam = pl.team;
885 if(!only_return_best)
887 SetPlayerColors(pl, selectedteam - 1);
892 // otherwise end up on the smallest team (handled below)
895 smallest = FindSmallestTeam(pl, TRUE);
898 if(!only_return_best)
900 TeamchangeFrags(self);
903 SetPlayerColors(pl, COLOR_TEAM1 - 1);
905 else if(smallest == 2)
907 SetPlayerColors(pl, COLOR_TEAM2 - 1);
909 else if(smallest == 3)
911 SetPlayerColors(pl, COLOR_TEAM3 - 1);
913 else if(smallest == 4)
915 SetPlayerColors(pl, COLOR_TEAM4 - 1);
919 error("smallest team: invalid team\n");
922 if(pl.deadflag == DEAD_NO)
923 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
929 //void() ctf_playerchanged;
930 void SV_ChangeTeam(float _color)
932 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
934 // in normal deathmatch we can just apply the color and we're done
935 if(!cvar("teamplay")) {
936 SetPlayerColors(self, _color);
940 scolor = self.clientcolors & 0x0F;
941 dcolor = _color & 0x0F;
943 if(scolor == COLOR_TEAM1 - 1)
945 else if(scolor == COLOR_TEAM2 - 1)
947 else if(scolor == COLOR_TEAM3 - 1)
949 else if(scolor == COLOR_TEAM4 - 1)
951 if(dcolor == COLOR_TEAM1 - 1)
953 else if(dcolor == COLOR_TEAM2 - 1)
955 else if(dcolor == COLOR_TEAM3 - 1)
957 else if(dcolor == COLOR_TEAM4 - 1)
960 CheckAllowedTeams(self);
962 if(dteam == 1 && c1 < 0) dteam = 4;
963 if(dteam == 4 && c4 < 0) dteam = 3;
964 if(dteam == 3 && c3 < 0) dteam = 2;
965 if(dteam == 2 && c2 < 0) dteam = 1;
967 // not changing teams
970 //bprint("same team change\n");
971 SetPlayerTeam(self, dteam, steam, TRUE);
977 if(cvar("g_campaign") || cvar("g_changeteam_banned"))
979 sprint(self, "Team changes not allowed\n");
980 return; // changing teams is not allowed
983 if(!cvar("g_campaign") && cvar("g_balance_teams_prevent_imbalance"))
985 // only allow changing to a smaller or equal size team
987 // find out what teams are available
988 //CheckAllowedTeams();
989 // count how many players on each team
990 GetTeamCounts(world);
993 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
998 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
1003 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
1008 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
1015 sprint(self, "Cannot change to an invalid team\n");
1020 // get starting team
1021 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
1023 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
1025 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
1027 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
1030 if(scount) // started at a valid, nonempty team
1032 // check if we're trying to change to a larger team that doens't have bots to swap with
1033 if(dcount >= scount && dbotcount <= 0)
1035 sprint(self, "Cannot change to a larger team\n");
1036 return; // can't change to a larger team
1042 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
1044 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1046 // reduce frags during a team change
1047 TeamchangeFrags(self);
1050 SetPlayerTeam(self, dteam, steam, FALSE);
1052 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1054 // kill player when changing teams
1055 if(self.deadflag == DEAD_NO)
1056 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
1058 //ctf_playerchanged();
1061 void ShufflePlayerOutOfTeam (float source_team)
1063 float smallestteam, smallestteam_count, steam;
1064 float lowest_bot_score, lowest_player_score;
1065 entity head, lowest_bot, lowest_player, selected;
1068 smallestteam_count = 999999999;
1070 if(c1 >= 0 && c1 < smallestteam_count)
1073 smallestteam_count = c1;
1075 if(c2 >= 0 && c2 < smallestteam_count)
1078 smallestteam_count = c2;
1080 if(c3 >= 0 && c3 < smallestteam_count)
1083 smallestteam_count = c3;
1085 if(c4 >= 0 && c4 < smallestteam_count)
1088 smallestteam_count = c4;
1093 bprint("warning: no smallest team\n");
1097 if(source_team == 1)
1098 steam = COLOR_TEAM1;
1099 else if(source_team == 2)
1100 steam = COLOR_TEAM2;
1101 else if(source_team == 3)
1102 steam = COLOR_TEAM3;
1103 else if(source_team == 4)
1104 steam = COLOR_TEAM4;
1107 lowest_bot_score = 999999999;
1108 lowest_player = world;
1109 lowest_player_score = 999999999;
1111 // find the lowest-scoring player & bot of that team
1112 FOR_EACH_PLAYER(head)
1114 if(head.team == steam)
1118 if(head.frags < lowest_bot_score)
1121 lowest_bot_score = head.frags;
1126 if(head.frags < lowest_player_score)
1128 lowest_player = head;
1129 lowest_player_score = head.frags;
1135 // prefers to move a bot...
1136 if(lowest_bot != world)
1137 selected = lowest_bot;
1138 // but it will move a player if it has to
1140 selected = lowest_player;
1141 // don't do anything if it couldn't find anyone
1144 bprint("warning: couldn't find a player to move from team\n");
1148 // smallest team gains a member
1149 if(smallestteam == 1)
1153 else if(smallestteam == 2)
1157 else if(smallestteam == 3)
1161 else if(smallestteam == 4)
1167 bprint("warning: destination team invalid\n");
1170 // source team loses a member
1171 if(source_team == 1)
1175 else if(source_team == 2)
1179 else if(source_team == 3)
1183 else if(source_team == 4)
1189 bprint("warning: source team invalid\n");
1193 // move the player to the new team
1194 TeamchangeFrags(selected);
1195 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1197 if(selected.deadflag == DEAD_NO)
1198 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1199 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1202 float lastRebalanceInfo;
1203 void CauseRebalance(float source_team, float howmany_toomany)
1208 if(IsTeamBalanceForced() == 1)
1210 bprint("Rebalancing Teams\n");
1211 ShufflePlayerOutOfTeam(source_team);
1215 if(howmany_toomany < cvar("g_balance_teams_complain"))
1217 if(time < lastRebalanceInfo + 90)
1219 lastRebalanceInfo = time;
1220 if(source_team == 1)
1221 steam = COLOR_TEAM1;
1222 else if(source_team == 2)
1223 steam = COLOR_TEAM2;
1224 else if(source_team == 3)
1225 steam = COLOR_TEAM3;
1226 else if(source_team == 4)
1227 steam = COLOR_TEAM4;
1228 ServerConsoleEcho(strcat("Team ", ftos(source_team), " too large, complaining."), TRUE);
1229 FOR_EACH_REALPLAYER(head)
1231 if(head.team == steam)
1233 sprint(head, "\{1}\{13}^3SERVER NOTICE:^7 One of you please change teams!\n");
1234 centerprint_atprio(head, CENTERPRIO_REBALANCE, "^3SERVER NOTICE:\n\n^7Someone of you please change teams!");
1240 // part of g_balance_teams_force
1241 // occasionally perform an audit of the teams to make
1242 // sure they're more or less balanced in player count.
1245 float numplayers, numteams, smallest, toomany;
1247 balance = IsTeamBalanceForced();
1251 if(audit_teams_time > time)
1254 audit_teams_time = time + 4 + random();
1256 // bprint("Auditing teams\n");
1258 CheckAllowedTeams(world);
1259 GetTeamCounts(world);
1262 numteams = numplayers = smallest = 0;
1265 numteams = numteams + 1;
1266 numplayers = numplayers + c1;
1271 numteams = numteams + 1;
1272 numplayers = numplayers + c2;
1278 numteams = numteams + 1;
1279 numplayers = numplayers + c3;
1285 numteams = numteams + 1;
1286 numplayers = numplayers + c4;
1292 return; // no players to move around
1294 return; // don't bother shuffling if for some reason there aren't any teams
1296 toomany = smallest + 1;
1298 if(c1 && c1 > toomany)
1299 CauseRebalance(1, c1 - toomany);
1300 if(c2 && c2 > toomany)
1301 CauseRebalance(2, c2 - toomany);
1302 if(c3 && c3 > toomany)
1303 CauseRebalance(3, c3 - toomany);
1304 if(c4 && c4 > toomany)
1305 CauseRebalance(4, c4 - toomany);
1307 // if teams are still unbalanced, balance them further in the next audit,
1308 // which will happen sooner (keep doing rapid audits until things are in order)
1309 audit_teams_time = time + 0.7 + random()*0.3;
1314 /*void UpdateTeamScore(entity e, float first)
1319 WriteByte (MSG_ALL, SVC_UPDATENAME);
1320 WriteByte (MSG_ALL, clientno);
1321 WriteString (MSG_ALL, e.netname);
1323 WriteByte (MSG_ALL, SVC_UPDATECOLORS);
1324 WriteByte (MSG_ALL, clientno);
1325 WriteByte (MSG_ALL, e.b_shirt * 16 + who.b_pants);
1328 WriteByte (MSG_ALL, SVC_UPDATEFRAGS);
1329 WriteByte (MSG_ALL, clientno);
1330 WriteShort (MSG_ALL, e.frags + 10000);
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;