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();
79 void LogTeamchange(entity pl)
82 if(!cvar("sv_eventlog"))
86 str = strcat(":team:", ftos(pl.playerid), ":");
87 str = strcat(str, ftos(pl.team));
88 GameLogEcho(str, FALSE);
93 cvar_set("g_dm", "0");
94 cvar_set("g_tdm", "0");
95 cvar_set("g_domination", "0");
96 cvar_set("g_ctf", "0");
97 cvar_set("g_runematch", "0");
98 cvar_set("g_lms", "0");
99 cvar_set("g_arena", "0");
100 cvar_set("g_keyhunt", "0");
101 cvar_set("g_assault", "0");
102 cvar_set("g_onslaught", "0");
103 cvar_set("teamplay", "0");
106 void ActivateTeamplay()
108 float teamplay_default;
109 teamplay_default = cvar("teamplay_default");
112 cvar_set("teamplay", ftos(teamplay_default));
114 cvar_set("teamplay", "3");
117 void InitGameplayMode()
119 float fraglimit_override, timelimit_override;
123 game = cvar("gamecfg"); // load game options
125 // game cvars get reset before map changes
126 // then map's cfg sets them as desired
128 // FIXME: also set a message or game mode name to print to players when the join
130 // set both here, gamemode can override it later
131 timelimit_override = cvar("timelimit_override");
132 fraglimit_override = cvar("fraglimit_override");
134 if(game == GAME_DOMINATION || cvar("g_domination"))
137 game = GAME_DOMINATION;
138 cvar_set("g_domination", "1");
142 fraglimit_override = cvar("g_domination_point_limit");
144 gamemode_name = "Domination";
147 else if(game == GAME_CTF || cvar("g_ctf"))
151 cvar_set("g_ctf", "1");
155 fraglimit_override = cvar("g_ctf_capture_limit");
157 gamemode_name = "Capture the Flag";
160 else if(game == GAME_RUNEMATCH || cvar("g_runematch"))
162 game = GAME_RUNEMATCH;
163 cvar_set("g_runematch", "1");
164 cvar_set("g_minstagib", "0");
166 if(cvar("deathmatch_force_teamplay"))
169 fraglimit_override = cvar("g_runematch_point_limit");
171 gamemode_name = "Rune Match";
177 else if(game == GAME_DEATHMATCH || game == GAME_TEAM_DEATHMATCH || cvar("g_tdm"))
179 if(!cvar("deathmatch"))
180 cvar_set("deathmatch", "1");
183 if(game == GAME_TEAM_DEATHMATCH || cvar("g_tdm") || cvar("deathmatch_force_teamplay"))
186 game = GAME_TEAM_DEATHMATCH;
187 gamemode_name = "Team Deathmatch";
190 cvar_set("g_tdm", "1");
195 game = GAME_DEATHMATCH;
196 gamemode_name = "Deathmatch";
200 fraglimit_override = cvar("fraglimit_override");
202 else if(game == GAME_ASSAULT || cvar("g_assault"))
206 gamemode_name = "Assault";
209 cvar_set("g_assault", "1");
211 else if(game == GAME_LMS || cvar("g_lms"))
215 cvar_set("g_lms", "1");
216 fraglimit_override = cvar("g_lms_lives_override");
217 if(fraglimit_override == 0)
218 fraglimit_override = -1;
219 gamemode_name = "Last Man Standing";
221 lms_lowest_lives = 999;
223 else if(game == GAME_ARENA || cvar("g_arena"))
227 cvar_set("g_arena", "1");
228 fraglimit_override = cvar("g_arena_point_limit");
229 maxspawned = cvar("g_arena_maxspawned");
232 arena_roundbased = cvar("g_arena_roundbased");
233 gamemode_name = "Arena";
236 else if(game == GAME_KEYHUNT || cvar("g_keyhunt"))
240 cvar_set("g_keyhunt", "1");
241 fraglimit_override = cvar("g_keyhunt_point_limit");
243 gamemode_name = "Key Hunt";
246 else if(game == GAME_ONSLAUGHT || cvar("g_onslaught"))
249 game = GAME_ONSLAUGHT;
250 cvar_set("g_onslaught", "1");
254 gamemode_name = "Onslaught";
259 // we can only assume...
261 cvar_set("g_dm", "1");
262 gamemode_name = "Deathmatch";
265 /* else if(game == GAME_TEAM_DEATHMATCH)
267 if(!cvar("deathmatch"))
268 cvar_set("deathmatch", "1");
270 //if(!cvar("teamplay"))
271 // cvar_set("teamplay", "3");
274 fraglimit_override = cvar("fraglimit_override");
277 // those mutators rule each other out
278 if(cvar("g_minstagib"))
280 cvar_set("g_instagib", "0");
281 cvar_set("g_rocketarena", "0");
283 if(cvar("g_instagib"))
285 cvar_set("g_minstagib", "0");
286 cvar_set("g_rocketarena", "0");
288 if(cvar("g_rocketarena"))
290 cvar_set("g_instagib", "0");
291 cvar_set("g_minstagib", "0");
294 g_domination = cvar("g_domination");
295 g_ctf = cvar("g_ctf");
296 g_lms = cvar("g_lms");
297 g_tdm = cvar("g_tdm");
298 g_runematch = cvar("g_runematch");
299 g_keyhunt = cvar("g_keyhunt");
300 g_onslaught = cvar("g_onslaught");
301 g_assault = cvar("g_assault");
302 g_arena = cvar("g_arena");
304 cache_mutatormsg = strzone("");
305 cache_motd = strzone("");
306 cache_lastmutatormsg = strzone("");
307 cache_lastmotd = strzone("");
309 // enforce the server's universal frag/time limits
310 if(!cvar("g_campaign"))
312 if(fraglimit_override >= 0)
313 cvar_set("fraglimit", ftos(fraglimit_override));
314 if(timelimit_override >= 0)
315 cvar_set("timelimit", ftos(timelimit_override));
318 if (game == GAME_DOMINATION)//cvar("g_domination"))
320 else if (game == GAME_CTF)//cvar("g_ctf"))
322 else if (game == GAME_RUNEMATCH)//cvar("g_runematch"))
324 else if (game == GAME_TEAM_DEATHMATCH)//cvar("g_runematch"))
326 else if (game == GAME_KEYHUNT)//cvar("g_keyhunt"))
330 string GetClientVersionMessage(float v) {
331 local string versionmsg;
333 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";
334 // either that or someone wants to be funny
335 } else if (v != cvar("gameversion")) {
336 if(v < cvar("gameversion")) {
337 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
339 versionmsg = "^3This server is using an outdated Nexuiz version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
342 versionmsg = "^2client version and server version are compatible.^8";
348 void PrintWelcomeMessage(entity pl)
350 string s, mutator, modifications, padding;
352 /*if(self.welcomemessage_time > time)
354 self.welcomemessage_time = time + 0.8; */
356 if(self.cvar_scr_centertime == 0) return;
357 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
358 if(self.welcomemessage_time > time) return;
359 self.welcomemessage_time = time + self.cvar_scr_centertime * 0.6;
362 if(cvar("g_campaign"))
364 centerprint(pl, campaign_message);
368 if(!self.BUTTON_INFO)
370 if(self.classname == "observer")
372 if(g_lms && self.frags <= 0 && self.frags > -666)
373 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"));
374 else if(g_lms && self.frags == -666)
375 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"));
377 else if(self.classname == "spectator")
379 if ((g_lms && self.frags < 1) || g_arena)
380 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"));
382 local string specString;
383 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");
385 if(time < restart_countdown) //also show the countdown when being a spectator
386 specString = strcat(specString, "\n\n^1Game starts in ", ftos(restartAnnouncer.cnt + 1), " seconds^7");
387 else if (timeoutStatus != 0)
388 specString = strcat(specString, "\n\n", getTimeoutText(1));
389 return centerprint_atprio(self, CENTERPRIO_SPAM, specString);
395 mutator = "^2Minstagib ^1";
397 mutator = "^2Instagib ^1";
398 else if(g_rocketarena)
399 mutator = "^2Rocketarena ^1";
401 mutator = "^2No Items Nexuiz ^1";
404 // to protect against unheedingly made changes
406 modifications = strcat(modifications, ", ");
408 modifications = "midair";
412 modifications = strcat(modifications, ", ");
414 modifications = strcat(modifications, "vampire");
416 if(g_laserguided_missile) {
418 modifications = strcat(modifications, ", ");
420 modifications = strcat(modifications, "laser-guided-missiles");
424 modifications = strcat(modifications, ", ");
426 modifications = strcat(modifications, "Tournament");
429 local string versionmessage;
430 versionmessage = GetClientVersionMessage(self.version);
432 s = strcat(s, NEWLINES, "This is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
433 s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
435 if(modifications != "")
436 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
439 if(!tourneyInMatchStage)
440 s = strcat(s, "\n^8The game is currently in ^3warmup-stage ^8!\n");
442 s = strcat(s, "\n^8The game is currently in ^3match-stage ^8!\n");
445 if(time < restart_countdown)
446 s = strcat(s, "\n^1Game starts in ", ftos(restartAnnouncer.cnt + 1), " seconds^7");
448 if(timeoutStatus != 0)
449 s = strcat(s, "\n\n", getTimeoutText(1));
451 if (g_grappling_hook)
452 s = strcat(s, "\n\n^8grappling hook is enabled, press 'e' to use it\n");
454 if(cache_lastmutatormsg != cvar_string("g_mutatormsg"))
456 if(cache_lastmutatormsg)
457 strunzone(cache_lastmutatormsg);
459 strunzone(cache_mutatormsg);
460 cache_lastmutatormsg = strzone(cvar_string("g_mutatormsg"));
461 cache_mutatormsg = strzone(wordwrap(cache_lastmutatormsg, 50));
464 if (cache_mutatormsg != "") {
465 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
468 if(cache_lastmotd != cvar_string("sv_motd"))
471 strunzone(cache_lastmotd);
473 strunzone(cache_motd);
474 cache_lastmotd = strzone(cvar_string("sv_motd"));
475 cache_motd = strzone(wordwrap(cache_lastmotd, 50));
478 if (cache_motd != "") {
479 s = strcat(s, "\n\n^8MOTD: ^7", cache_motd);
483 if(cvar("fraglimit"))
486 if(cvar("timelimit"))
489 s = strcat(s, "\n^8frag limit: ^7", cvar_string("fraglimit"), padding);
491 if(cvar("timelimit"))
492 s = strcat(s, "\n^8time limit: ^7", cvar_string("timelimit"), " minutes");
499 void SetPlayerColors(entity pl, float _color)
503 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
505 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
506 pl.clientcolors = 16*cl + cl;*/
509 pants = _color & 0x0F;
510 shirt = _color & 0xF0;
514 setcolor(pl, 16*pants + pants);
516 setcolor(pl, shirt + pants);
520 void SetPlayerTeam(entity pl, float t, float s, float noprint)
525 _color = COLOR_TEAM4 - 1;
527 _color = COLOR_TEAM3 - 1;
529 _color = COLOR_TEAM2 - 1;
531 _color = COLOR_TEAM1 - 1;
533 SetPlayerColors(pl,_color);
535 if(!noprint && t != s)
537 //bprint(pl.netname, " has changed to ", TeamNoName(t), "\n");
538 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
550 // set c1...c4 to show what teams are allowed
551 void CheckAllowedTeams (entity for_whom)
560 c1 = c2 = c3 = c4 = -1;
561 cb1 = cb2 = cb3 = cb4 = 0;
563 // onslaught is special
566 head = findchain(classname, "onslaught_generator");
569 if (head.team == COLOR_TEAM1) c1 = 0;
570 if (head.team == COLOR_TEAM2) c2 = 0;
571 if (head.team == COLOR_TEAM3) c3 = 0;
572 if (head.team == COLOR_TEAM4) c4 = 0;
579 teament_name = "dom_team";
581 teament_name = "ctf_team";
583 teament_name = "tdm_team";
586 c1 = c2 = 0; // Assault always has 2 teams
591 // cover anything else by treating it like tdm with no teams spawned
595 dm = cvar("g_tdm_teams");
597 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
601 c1 = c2 = c3 = c4 = 0;
614 // first find out what teams are allowed
615 head = find(world, classname, teament_name);
618 if(!(g_domination && head.netname == ""))
620 if(head.team == COLOR_TEAM1)
624 if(head.team == COLOR_TEAM2)
628 if(head.team == COLOR_TEAM3)
632 if(head.team == COLOR_TEAM4)
637 head = find(head, classname, teament_name);
640 if(cvar("bot_vs_human") > 0)
643 if(clienttype(for_whom) == CLIENTTYPE_BOT)
648 else if(cvar("bot_vs_human") < 0)
651 if(clienttype(for_whom) == CLIENTTYPE_BOT)
658 float PlayerValue(entity p)
660 if(IsTeamBalanceForced() == 1)
665 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
666 // teams that are allowed will now have their player counts stored in c1...c4
667 void GetTeamCounts(entity ignore)
671 // now count how many players are on each team already
673 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
674 // also remember the lowest-scoring player
676 FOR_EACH_PLAYER(head)
678 if(head != ignore)// && head.netname != "")
680 value = PlayerValue(head);
681 if(clienttype(head) == CLIENTTYPE_BOT)
685 if(head.team == COLOR_TEAM1)
693 if(head.team == COLOR_TEAM2)
701 if(head.team == COLOR_TEAM3)
709 if(head.team == COLOR_TEAM4)
721 // returns # of smallest team (1, 2, 3, 4)
722 // NOTE: Assumes CheckAllowedTeams has already been called!
723 float FindSmallestTeam(entity pl, float ignore_pl)
725 float totalteams, smallestteam, smallestteam_count, smallestteam_score, balance_type;
728 // find out what teams are available
729 //CheckAllowedTeams();
731 // make sure there are at least 2 teams to join
733 totalteams = totalteams + 1;
735 totalteams = totalteams + 1;
737 totalteams = totalteams + 1;
739 totalteams = totalteams + 1;
741 if(cvar("bot_vs_human"))
747 error("Too few teams available for domination\n");
749 error("Too few teams available for ctf\n");
751 error("Too few teams available for key hunt\n");
753 error("Too few teams available for team deathmatch\n");
756 // count how many players are in each team
760 GetTeamCounts(world);
762 // c1...c4 now have counts of each team
763 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
766 smallestteam_count = 999999999;
767 smallestteam_score = 999999999;
769 // 2 gives priority to what team you're already on, 1 goes in order
770 // 2 doesn't seem to work though...
774 //if(pl.classname != "player")
775 if(clienttype(pl) != CLIENTTYPE_BOT)
777 c1 -= cb1 * 255.0/256;
778 c2 -= cb2 * 255.0/256;
779 c3 -= cb3 * 255.0/256;
780 c4 -= cb4 * 255.0/256;
783 if(balance_type == 1)
785 if(c1 >= 0 && (c1 < smallestteam_count || (c1 <= smallestteam_count && team1_score < smallestteam_score)))
788 smallestteam_count = c1;
789 smallestteam_score = team1_score;
791 if(c2 >= 0 && (c2 < smallestteam_count || (c2 <= smallestteam_count && team2_score < smallestteam_score)))
794 smallestteam_count = c2;
795 smallestteam_score = team2_score;
797 if(c3 >= 0 && (c3 < smallestteam_count || (c3 <= smallestteam_count && team3_score < smallestteam_score)))
800 smallestteam_count = c3;
801 smallestteam_score = team3_score;
803 if(c4 >= 0 && (c4 < smallestteam_count || (c4 <= smallestteam_count && team4_score < smallestteam_score)))
806 smallestteam_count = c4;
807 smallestteam_score = team4_score;
812 if(c1 >= 0 && (c1 < smallestteam_count ||
813 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
816 smallestteam_count = c1;
818 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
819 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
822 smallestteam_count = c2;
824 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
825 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
828 smallestteam_count = c3;
830 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
831 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
834 smallestteam_count = c4;
841 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
843 float smallest, selectedteam;
845 // don't join a team if we're not playing a team game
846 if(!cvar("teamplay") && !g_domination && !g_ctf && !g_keyhunt)
849 // find out what teams are available
850 CheckAllowedTeams(pl);
855 if(cvar("g_domination_default_teams") < 3)
857 if(cvar("g_domination_default_teams") < 4)
861 // if we don't care what team he ends up on, put him on whatever team he entered as.
862 // if he's not on a valid team, then let other code put him on the smallest team
865 if( c1 >= 0 && pl.team == COLOR_TEAM1)
866 selectedteam = pl.team;
867 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
868 selectedteam = pl.team;
869 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
870 selectedteam = pl.team;
871 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
872 selectedteam = pl.team;
877 if(!only_return_best)
879 SetPlayerColors(pl, selectedteam - 1);
884 // otherwise end up on the smallest team (handled below)
887 smallest = FindSmallestTeam(pl, TRUE);
890 if(!only_return_best)
892 TeamchangeFrags(self);
895 SetPlayerColors(pl, COLOR_TEAM1 - 1);
897 else if(smallest == 2)
899 SetPlayerColors(pl, COLOR_TEAM2 - 1);
901 else if(smallest == 3)
903 SetPlayerColors(pl, COLOR_TEAM3 - 1);
905 else if(smallest == 4)
907 SetPlayerColors(pl, COLOR_TEAM4 - 1);
911 error("smallest team: invalid team\n");
914 if(pl.deadflag == DEAD_NO)
915 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
922 void SV_ChangeTeam(float _color)
924 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
926 // in normal deathmatch we can just apply the color and we're done
927 if(!cvar("teamplay")) {
928 SetPlayerColors(self, _color);
932 scolor = self.clientcolors & 0x0F;
933 dcolor = _color & 0x0F;
935 if(scolor == COLOR_TEAM1 - 1)
937 else if(scolor == COLOR_TEAM2 - 1)
939 else if(scolor == COLOR_TEAM3 - 1)
941 else if(scolor == COLOR_TEAM4 - 1)
943 if(dcolor == COLOR_TEAM1 - 1)
945 else if(dcolor == COLOR_TEAM2 - 1)
947 else if(dcolor == COLOR_TEAM3 - 1)
949 else if(dcolor == COLOR_TEAM4 - 1)
952 CheckAllowedTeams(self);
954 if(dteam == 1 && c1 < 0) dteam = 4;
955 if(dteam == 4 && c4 < 0) dteam = 3;
956 if(dteam == 3 && c3 < 0) dteam = 2;
957 if(dteam == 2 && c2 < 0) dteam = 1;
959 // not changing teams
962 //bprint("same team change\n");
963 SetPlayerTeam(self, dteam, steam, TRUE);
969 if(cvar("g_campaign") || cvar("g_changeteam_banned"))
971 sprint(self, "Team changes not allowed\n");
972 return; // changing teams is not allowed
975 if(!cvar("g_campaign") && cvar("g_balance_teams_prevent_imbalance"))
977 // only allow changing to a smaller or equal size team
979 // find out what teams are available
980 //CheckAllowedTeams();
981 // count how many players on each team
982 GetTeamCounts(world);
985 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
990 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
995 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
1000 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
1007 sprint(self, "Cannot change to an invalid team\n");
1012 // get starting team
1013 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
1015 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
1017 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
1019 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
1022 if(scount) // started at a valid, nonempty team
1024 // check if we're trying to change to a larger team that doens't have bots to swap with
1025 if(dcount >= scount && dbotcount <= 0)
1027 sprint(self, "Cannot change to a larger team\n");
1028 return; // can't change to a larger team
1034 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
1036 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1038 // reduce frags during a team change
1039 TeamchangeFrags(self);
1042 SetPlayerTeam(self, dteam, steam, FALSE);
1044 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1046 // kill player when changing teams
1047 if(self.deadflag == DEAD_NO)
1048 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
1052 void ShufflePlayerOutOfTeam (float source_team)
1054 float smallestteam, smallestteam_count, steam;
1055 float lowest_bot_score, lowest_player_score;
1056 entity head, lowest_bot, lowest_player, selected;
1059 smallestteam_count = 999999999;
1061 if(c1 >= 0 && c1 < smallestteam_count)
1064 smallestteam_count = c1;
1066 if(c2 >= 0 && c2 < smallestteam_count)
1069 smallestteam_count = c2;
1071 if(c3 >= 0 && c3 < smallestteam_count)
1074 smallestteam_count = c3;
1076 if(c4 >= 0 && c4 < smallestteam_count)
1079 smallestteam_count = c4;
1084 bprint("warning: no smallest team\n");
1088 if(source_team == 1)
1089 steam = COLOR_TEAM1;
1090 else if(source_team == 2)
1091 steam = COLOR_TEAM2;
1092 else if(source_team == 3)
1093 steam = COLOR_TEAM3;
1094 else if(source_team == 4)
1095 steam = COLOR_TEAM4;
1098 lowest_bot_score = 999999999;
1099 lowest_player = world;
1100 lowest_player_score = 999999999;
1102 // find the lowest-scoring player & bot of that team
1103 FOR_EACH_PLAYER(head)
1105 if(head.team == steam)
1109 if(head.frags < lowest_bot_score)
1112 lowest_bot_score = head.frags;
1117 if(head.frags < lowest_player_score)
1119 lowest_player = head;
1120 lowest_player_score = head.frags;
1126 // prefers to move a bot...
1127 if(lowest_bot != world)
1128 selected = lowest_bot;
1129 // but it will move a player if it has to
1131 selected = lowest_player;
1132 // don't do anything if it couldn't find anyone
1135 bprint("warning: couldn't find a player to move from team\n");
1139 // smallest team gains a member
1140 if(smallestteam == 1)
1144 else if(smallestteam == 2)
1148 else if(smallestteam == 3)
1152 else if(smallestteam == 4)
1158 bprint("warning: destination team invalid\n");
1161 // source team loses a member
1162 if(source_team == 1)
1166 else if(source_team == 2)
1170 else if(source_team == 3)
1174 else if(source_team == 4)
1180 bprint("warning: source team invalid\n");
1184 // move the player to the new team
1185 TeamchangeFrags(selected);
1186 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1188 if(selected.deadflag == DEAD_NO)
1189 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1190 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1193 float lastRebalanceInfo;
1194 void CauseRebalance(float source_team, float howmany_toomany)
1199 if(IsTeamBalanceForced() == 1)
1201 bprint("Rebalancing Teams\n");
1202 ShufflePlayerOutOfTeam(source_team);
1206 if(howmany_toomany < cvar("g_balance_teams_complain"))
1208 if(time < lastRebalanceInfo + 90)
1210 lastRebalanceInfo = time;
1211 if(source_team == 1)
1212 steam = COLOR_TEAM1;
1213 else if(source_team == 2)
1214 steam = COLOR_TEAM2;
1215 else if(source_team == 3)
1216 steam = COLOR_TEAM3;
1217 else if(source_team == 4)
1218 steam = COLOR_TEAM4;
1219 ServerConsoleEcho(strcat("Team ", ftos(source_team), " too large, complaining."), TRUE);
1220 FOR_EACH_REALPLAYER(head)
1222 if(head.team == steam)
1224 sprint(head, "\{1}\{13}^3SERVER NOTICE:^7 One of you please change teams!\n");
1225 centerprint_atprio(head, CENTERPRIO_REBALANCE, "^3SERVER NOTICE:\n\n^7Someone of you please change teams!");
1231 // part of g_balance_teams_force
1232 // occasionally perform an audit of the teams to make
1233 // sure they're more or less balanced in player count.
1236 float numplayers, numteams, smallest, toomany;
1238 balance = IsTeamBalanceForced();
1242 if(audit_teams_time > time)
1245 audit_teams_time = time + 4 + random();
1247 // bprint("Auditing teams\n");
1249 CheckAllowedTeams(world);
1250 GetTeamCounts(world);
1253 numteams = numplayers = smallest = 0;
1256 numteams = numteams + 1;
1257 numplayers = numplayers + c1;
1262 numteams = numteams + 1;
1263 numplayers = numplayers + c2;
1269 numteams = numteams + 1;
1270 numplayers = numplayers + c3;
1276 numteams = numteams + 1;
1277 numplayers = numplayers + c4;
1283 return; // no players to move around
1285 return; // don't bother shuffling if for some reason there aren't any teams
1287 toomany = smallest + 1;
1289 if(c1 && c1 > toomany)
1290 CauseRebalance(1, c1 - toomany);
1291 if(c2 && c2 > toomany)
1292 CauseRebalance(2, c2 - toomany);
1293 if(c3 && c3 > toomany)
1294 CauseRebalance(3, c3 - toomany);
1295 if(c4 && c4 > toomany)
1296 CauseRebalance(4, c4 - toomany);
1298 // if teams are still unbalanced, balance them further in the next audit,
1299 // which will happen sooner (keep doing rapid audits until things are in order)
1300 audit_teams_time = time + 0.7 + random()*0.3;
1305 /*void UpdateTeamScore(entity e, float first)
1310 WriteByte (MSG_ALL, SVC_UPDATENAME);
1311 WriteByte (MSG_ALL, clientno);
1312 WriteString (MSG_ALL, e.netname);
1314 WriteByte (MSG_ALL, SVC_UPDATECOLORS);
1315 WriteByte (MSG_ALL, clientno);
1316 WriteByte (MSG_ALL, e.b_shirt * 16 + who.b_pants);
1319 WriteByte (MSG_ALL, SVC_UPDATEFRAGS);
1320 WriteByte (MSG_ALL, clientno);
1321 WriteShort (MSG_ALL, e.frags + 10000);
1327 // code from here on is just to support maps that don't have team entities
1328 void tdm_spawnteam (string teamname, float teamcolor)
1332 e.classname = "tdm_team";
1333 e.netname = teamname;
1338 // spawn some default teams if the map is not set up for tdm
1339 void tdm_spawnteams()
1343 numteams = cvar("g_tdm_teams");
1345 tdm_spawnteam("Red", COLOR_TEAM1-1);
1346 tdm_spawnteam("Blue", COLOR_TEAM2-1);
1348 tdm_spawnteam("Yellow", COLOR_TEAM3-1);
1350 tdm_spawnteam("Pink", COLOR_TEAM4-1);
1353 void tdm_delayedinit()
1355 self.think = SUB_Remove;
1356 self.nextthink = time;
1357 // if no teams are found, spawn defaults
1358 if (find(world, classname, "tdm_team") == world)
1366 e.think = tdm_delayedinit;
1367 e.nextthink = time + 0.1;