1 float GAME_DEATHMATCH = 1;
2 float GAME_TEAM_DEATHMATCH = 2;
3 float GAME_DOMINATION = 3;
5 float GAME_RUNEMATCH = 5;
8 float GAME_KEYHUNT = 8;
10 // client counts for each team
12 // # of bots on those teams
13 float cb1, cb2, cb3, cb4;
15 float g_domination, g_ctf, g_tdm, g_keyhunt;
17 float audit_teams_time;
19 float() IsTeamBalanceForced = {
20 if(intermission_running)
21 return 0; // no rebalancing whatsoever please
24 if(cvar("g_campaign"))
26 if(!cvar("g_balance_teams_force"))
31 void TeamchangeFrags(entity e)
33 if(e.classname == "player")
35 // reduce frags during a team change
36 DistributeFragsAmongTeam(e, (e.clientcolors & 0x0F) + 1, 1 - cvar("g_changeteam_fragtransfer") / 100);
40 string TeamName(float t)
42 // fixme: Search for team entities and get their .netname's!
51 return "Neutral Team";
53 string ColoredTeamName(float t)
55 // fixme: Search for team entities and get their .netname's!
57 return "^1Red Team^7";
59 return "^4Blue Team^7";
61 return "^3Yellow Team^7";
63 return "^6Pink Team^7";
64 return "Neutral Team";
66 string TeamNoName(float t)
68 // fixme: Search for team entities and get their .netname's!
77 return "Neutral Team";
82 void runematch_init();
85 void(entity pl) LogTeamchange =
88 if(!cvar("sv_eventlog"))
92 str = strcat(":team:", ftos(pl.playerid), ":");
93 str = strcat(str, ftos(pl.team));
94 GameLogEcho(str, FALSE);
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("teamplay", "0");
109 void ActivateTeamplay()
111 float teamplay_default;
112 teamplay_default = cvar("teamplay_default");
115 cvar_set("teamplay", ftos(teamplay_default));
117 cvar_set("teamplay", "3");
120 void InitGameplayMode()
122 float fraglimit_override, timelimit_override;
126 game = cvar ("gamecfg"); // load game options
128 // game cvars get reset before map changes
129 // then map's cfg sets them as desired
131 // FIXME: also set a message or game mode name to print to players when the join
133 // set both here, gamemode can override it later
134 timelimit_override = cvar("timelimit_override");
135 fraglimit_override = cvar("fraglimit_override");
137 if(game == GAME_DOMINATION || cvar("g_domination"))
140 game = GAME_DOMINATION;
141 cvar_set("g_domination", "1");
145 fraglimit_override = cvar("g_domination_point_limit");
147 gamemode_name = "Domination";
150 else if(game == GAME_CTF || cvar("g_ctf"))
154 cvar_set("g_ctf", "1");
158 fraglimit_override = cvar("g_ctf_capture_limit");
160 gamemode_name = "Capture the Flag";
163 else if((game == GAME_RUNEMATCH || cvar("g_runematch")) && !cvar("g_minstagib"))
165 game = GAME_RUNEMATCH;
166 cvar_set("g_runematch", "1");
168 if(cvar("deathmatch_force_teamplay"))
171 fraglimit_override = cvar("g_runematch_point_limit");
173 gamemode_name = "Rune Match";
179 else if(game == GAME_DEATHMATCH || game == GAME_TEAM_DEATHMATCH || cvar("g_tdm"))
181 if(!cvar("deathmatch"))
182 cvar_set("deathmatch", "1");
185 if(game == GAME_TEAM_DEATHMATCH || cvar("g_tdm") || cvar("deathmatch_force_teamplay"))
188 game = GAME_TEAM_DEATHMATCH;
189 gamemode_name = "Team Deathmatch";
192 cvar_set("g_tdm", "1");
197 game = GAME_DEATHMATCH;
198 gamemode_name = "Deathmatch";
202 fraglimit_override = cvar("fraglimit_override");
204 else if(game == GAME_LMS || cvar("g_lms"))
208 cvar_set("g_lms", "1");
209 fraglimit_override = cvar("g_lms_lives_override");
210 if(fraglimit_override == 0)
211 fraglimit_override = -1;
212 gamemode_name = "Last Man Standing";
214 lms_lowest_lives = 999;
216 else if(game == GAME_ARENA || cvar("g_arena"))
220 cvar_set("g_arena", "1");
221 fraglimit_override = cvar("g_arena_point_limit");
222 maxspawned = cvar("g_arena_maxspawned");
225 arena_roundbased = cvar("g_arena_roundbased");
226 gamemode_name = "Arena";
229 else if(game == GAME_KEYHUNT || cvar("g_keyhunt"))
233 cvar_set("g_keyhunt", "1");
234 fraglimit_override = cvar("g_keyhunt_point_limit");
236 gamemode_name = "Key Hunt";
241 // we can only assume...
243 gamemode_name = "Deathmatch";
246 /* else if(game == GAME_TEAM_DEATHMATCH)
248 if(!cvar("deathmatch"))
249 cvar_set("deathmatch", "1");
251 //if(!cvar("teamplay"))
252 // cvar_set("teamplay", "3");
255 fraglimit_override = cvar("fraglimit_override");
258 // enforce the server's universal frag/time limits
259 if(!cvar("g_campaign"))
261 if(fraglimit_override >= 0)
262 cvar_set("fraglimit", ftos(fraglimit_override));
263 if(timelimit_override >= 0)
264 cvar_set("timelimit", ftos(timelimit_override));
267 if (game == GAME_DOMINATION)//cvar("g_domination"))
269 else if (game == GAME_CTF)//cvar("g_ctf"))
271 else if (game == GAME_RUNEMATCH)//cvar("g_runematch"))
273 else if (game == GAME_TEAM_DEATHMATCH)//cvar("g_runematch"))
275 else if (game == GAME_KEYHUNT)//cvar("g_keyhunt"))
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 registercvar("_motd", "");
296 registercvar("_mutatormsg", "");
297 cvar_set("_motd", wordwrap(cvar_string("sv_motd"), 50));
298 cvar_set("_mutatormsg", wordwrap(cvar_string("g_mutatormsg"), 50));
300 g_domination = cvar("g_domination");
301 g_ctf = cvar("g_ctf");
302 g_tdm = cvar("g_tdm");
303 g_keyhunt = cvar("g_keyhunt");
306 string GetClientVersionMessage(float v) {
307 local string versionmsg;
309 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";
310 // either that or someone wants to be funny
311 } else if (v != cvar("gameversion")) {
312 if(v < cvar("gameversion")) {
313 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
315 versionmsg = "^3This server is using an outdated Nexuiz version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
318 versionmsg = "^2client version and server version are compatible.^8";
320 return strzone(versionmsg);
325 void PrintWelcomeMessage(entity pl)
327 string s, mutator, modifications, padding;
329 /*if(self.welcomemessage_time > time)
331 self.welcomemessage_time = time + 0.8; */
333 if(self.cvar_scr_centertime == 0) return;
334 if(self.welcomemessage_time > time) return;
335 self.welcomemessage_time = time + self.cvar_scr_centertime * 0.6;
337 if(cvar("g_campaign"))
339 centerprint(pl, campaign_message);
343 if(self.classname == "observer")
345 if(cvar("g_lms") && self.frags <= 0 && self.frags > -666)
346 return centerprint(self, strcat(NEWLINES, "^1You have no more lives left\nwait for next round\n\n\n^7press attack to spectate other players"));
347 else if(cvar("g_lms") && self.frags == -666)
348 return centerprint(self, strcat(NEWLINES, "^1Match has already begun\nwait for next round\n\n\n^7press attack to spectate other players"));
350 else if(self.classname == "spectator")
352 if ((cvar("g_lms") && self.frags < 1) || cvar("g_arena"))
353 return centerprint(self, strcat(NEWLINES, "spectating ", self.enemy.netname, "\n\n\n^7press attack for next player\npress attack2 for free fly mode"));
355 return centerprint(self, 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"));
359 if(cvar("g_minstagib"))
360 mutator = "^2Minstagib ^1";
361 else if(cvar("g_instagib"))
362 mutator = "^2Instagib ^1";
363 else if(cvar("g_rocketarena"))
364 mutator = "^2Rocketarena ^1";
365 else if(cvar("g_nixnex"))
366 mutator = "^2No Items Nexuiz ^1";
368 if(cvar("g_midair")) {
369 // to protect against unheedingly made changes
371 modifications = strcat(modifications, ", ");
373 modifications = "midair";
375 if(cvar("g_vampire")) {
377 modifications = strcat(modifications, ", ");
379 modifications = strcat(modifications, "vampire");
381 if(cvar("g_laserguided_missile")) {
383 modifications = strcat(modifications, ", ");
385 modifications = strcat(modifications, "laser-guided-missiles");
388 local string versionmessage;
389 versionmessage = GetClientVersionMessage(self.version);
391 s = strcat(s, NEWLINES, "This is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
392 s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
394 if(modifications != "")
395 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
397 if((self.classname == "observer" || self.classname == "spectator") && self.version == cvar("gameversion")) {
399 s = strcat(s,"^7\n\n\npress jump to play\npress attack to spectate other players\n\n");
400 else if(player_count < 2 && arena_roundbased)
402 s = strcat(s, "\n\n\n^1waiting for second player to start match^7\n\n");
406 s = strcat(s, "\n\n\n");
408 s = strcat(s, "^7current champion is: ", champion.netname, "\n\n");
409 s = strcat(s,"^7press attack to spectate other players\n\n");
416 if (cvar("g_grappling_hook"))
417 s = strcat(s, "\n\n^8grappling hook is enabled, press 'e' to use it\n");
419 if (cvar_string("_mutatormsg") != "") {
420 s = strcat(s, "\n\n^8special gameplay tips: ^7", cvar_string("_mutatormsg"));
423 if (cvar_string("_motd") != "") {
424 s = strcat(s, "\n\n^8MOTD: ^7", cvar_string("_motd"));
428 if(cvar("fraglimit"))
431 if(cvar("timelimit"))
434 s = strcat(s, "\n^8frag limit: ^7", cvar_string("fraglimit"), padding);
436 if(cvar("timelimit"))
437 s = strcat(s, "\n^8time limit: ^7", cvar_string("timelimit"), " minutes");
448 void SetPlayerColors(entity pl, float _color)
452 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
454 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
455 pl.clientcolors = 16*cl + cl;*/
458 pants = _color & 0x0F;
459 shirt = _color & 0xF0;
463 setcolor(pl, 16*pants + pants);
465 setcolor(pl, shirt + pants);
469 void SetPlayerTeam(entity pl, float t, float s, float noprint)
474 _color = COLOR_TEAM4 - 1;
476 _color = COLOR_TEAM3 - 1;
478 _color = COLOR_TEAM2 - 1;
480 _color = COLOR_TEAM1 - 1;
482 SetPlayerColors(pl,_color);
484 if(!noprint && t != s)
486 //bprint(pl.netname, " has changed to ", TeamNoName(t), "\n");
487 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
499 // set c1...c4 to show what teams are allowed
500 void CheckAllowedTeams ()
509 c1 = c2 = c3 = c4 = -1;
510 cb1 = cb2 = cb3 = cb4 = 0;
513 teament_name = "dom_team";
515 teament_name = "ctf_team";
517 teament_name = "tdm_team";
520 // cover anything else by treating it like tdm with no teams spawned
524 dm = cvar("g_tdm_teams");
526 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
530 c1 = c2 = c3 = c4 = 0;
543 // first find out what teams are allowed
544 head = find(world, classname, teament_name);
547 if(!(g_domination && head.netname == ""))
549 if(head.team == COLOR_TEAM1)
553 if(head.team == COLOR_TEAM2)
557 if(head.team == COLOR_TEAM3)
561 if(head.team == COLOR_TEAM4)
566 head = find(head, classname, teament_name);
570 float PlayerValue(entity p)
572 if(IsTeamBalanceForced() == 1)
577 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
578 // teams that are allowed will now have their player counts stored in c1...c4
579 void GetTeamCounts(entity ignore)
583 // now count how many players are on each team already
585 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
586 // also remember the lowest-scoring player
588 FOR_EACH_PLAYER(head)
590 if(head != ignore)// && head.netname != "")
592 value = PlayerValue(head);
593 if(clienttype(head) == CLIENTTYPE_BOT)
597 if(head.team == COLOR_TEAM1)
605 if(head.team == COLOR_TEAM2)
613 if(head.team == COLOR_TEAM3)
621 if(head.team == COLOR_TEAM4)
633 // returns # of smallest team (1, 2, 3, 4)
634 // NOTE: Assumes CheckAllowedTeams has already been called!
635 float FindSmallestTeam(entity pl, float ignore_pl)
637 float totalteams, smallestteam, smallestteam_count, smallestteam_score, balance_type;
640 // find out what teams are available
641 //CheckAllowedTeams();
643 // make sure there are at least 2 teams to join
645 totalteams = totalteams + 1;
647 totalteams = totalteams + 1;
649 totalteams = totalteams + 1;
651 totalteams = totalteams + 1;
656 error("Too few teams available for domination\n");
658 error("Too few teams available for ctf\n");
660 error("Too few teams available for key hunt\n");
662 error("Too few teams available for team deathmatch\n");
666 // count how many players are in each team
670 GetTeamCounts(world);
672 // c1...c4 now have counts of each team
673 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
676 smallestteam_count = 999999999;
677 smallestteam_score = 999999999;
679 // 2 gives priority to what team you're already on, 1 goes in order
680 // 2 doesn't seem to work though...
684 //if(pl.classname != "player")
685 if(clienttype(pl) != CLIENTTYPE_BOT)
687 c1 -= cb1 * 255.0/256;
688 c2 -= cb2 * 255.0/256;
689 c3 -= cb3 * 255.0/256;
690 c4 -= cb4 * 255.0/256;
693 if(balance_type == 1)
695 if(c1 >= 0 && (c1 < smallestteam_count || (c1 <= smallestteam_count && team1_score < smallestteam_score)))
698 smallestteam_count = c1;
699 smallestteam_score = team1_score;
701 if(c2 >= 0 && (c2 < smallestteam_count || (c2 <= smallestteam_count && team2_score < smallestteam_score)))
704 smallestteam_count = c2;
705 smallestteam_score = team2_score;
707 if(c3 >= 0 && (c3 < smallestteam_count || (c3 <= smallestteam_count && team3_score < smallestteam_score)))
710 smallestteam_count = c3;
711 smallestteam_score = team3_score;
713 if(c4 >= 0 && (c4 < smallestteam_count || (c4 <= smallestteam_count && team4_score < smallestteam_score)))
716 smallestteam_count = c4;
717 smallestteam_score = team4_score;
722 if(c1 >= 0 && (c1 < smallestteam_count ||
723 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
726 smallestteam_count = c1;
728 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
729 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
732 smallestteam_count = c2;
734 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
735 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
738 smallestteam_count = c3;
740 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
741 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
744 smallestteam_count = c4;
751 float JoinBestTeam(entity pl, float only_return_best)
753 float smallest, selectedteam;
755 // don't join a team if we're not playing a team game
756 if(!cvar("teamplay") && !g_domination && !g_ctf && !g_keyhunt)
759 // find out what teams are available
762 if(cvar("g_domination"))
764 if(cvar("g_domination_default_teams") < 3)
766 if(cvar("g_domination_default_teams") < 4)
770 // if we don't care what team he ends up on, put him on whatever team he entered as.
771 // if he's not on a valid team, then let other code put him on the smallest team
772 if(!cvar("g_campaign") && !cvar("g_balance_teams") && !cvar("g_balance_teams_force"))
774 if( c1 >= 0 && pl.team == COLOR_TEAM1)
775 selectedteam = pl.team;
776 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
777 selectedteam = pl.team;
778 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
779 selectedteam = pl.team;
780 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
781 selectedteam = pl.team;
786 if(!only_return_best)
788 SetPlayerColors(pl, selectedteam - 1);
793 // otherwise end up on the smallest team (handled below)
796 smallest = FindSmallestTeam(pl, TRUE);
799 if(!only_return_best)
801 TeamchangeFrags(self);
804 SetPlayerColors(pl, COLOR_TEAM1 - 1);
806 else if(smallest == 2)
808 SetPlayerColors(pl, COLOR_TEAM2 - 1);
810 else if(smallest == 3)
812 SetPlayerColors(pl, COLOR_TEAM3 - 1);
814 else if(smallest == 4)
816 SetPlayerColors(pl, COLOR_TEAM4 - 1);
820 error("smallest team: invalid team\n");
823 if(pl.deadflag == DEAD_NO)
824 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
831 void SV_ChangeTeam(float _color)
833 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
835 // in normal deathmatch we can just apply the color and we're done
836 if(!cvar("teamplay")) {
837 SetPlayerColors(self, _color);
841 scolor = self.clientcolors & 0x0F;
842 dcolor = _color & 0x0F;
844 if(scolor == COLOR_TEAM1 - 1)
846 else if(scolor == COLOR_TEAM2 - 1)
848 else if(scolor == COLOR_TEAM3 - 1)
850 else if(scolor == COLOR_TEAM4 - 1)
852 if(dcolor == COLOR_TEAM1 - 1)
854 else if(dcolor == COLOR_TEAM2 - 1)
856 else if(dcolor == COLOR_TEAM3 - 1)
858 else if(dcolor == COLOR_TEAM4 - 1)
861 // remap invalid teams in dom & ctf
863 if(cvar("g_ctf") && dteam == 3)
865 else if(cvar("g_ctf") && dteam == 4)
867 else if((cvar("g_domination") && cvar("g_domination_default_teams") < 3) || (cvar("g_tdm") && cvar("g_tdm_teams") < 3))
874 else if((cvar("g_domination") && cvar("g_domination_default_teams") < 4) || (cvar("g_tdm") && cvar("g_tdm_teams") < 4))
882 if(dteam == 3 && c3 < 0)
884 if(dteam == 4 && c4 < 0)
887 // not changing teams
890 //bprint("same team change\n");
891 SetPlayerTeam(self, dteam, steam, TRUE);
897 if(cvar("g_campaign") || cvar("g_changeteam_banned"))
899 sprint(self, "Team changes not allowed\n");
900 return; // changing teams is not allowed
903 if(!cvar("g_campaign") && cvar("g_balance_teams_prevent_imbalance"))
905 // only allow changing to a smaller or equal size team
907 // find out what teams are available
909 // count how many players on each team
910 GetTeamCounts(world);
913 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
918 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
923 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
928 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
935 sprint(self, "Cannot change to an invalid team\n");
941 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
943 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
945 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
947 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
950 if(scount) // started at a valid, nonempty team
952 // check if we're trying to change to a larger team that doens't have bots to swap with
953 if(dcount >= scount && dbotcount <= 0)
955 sprint(self, "Cannot change to a larger team\n");
956 return; // can't change to a larger team
962 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
964 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
966 // reduce frags during a team change
967 TeamchangeFrags(self);
970 SetPlayerTeam(self, dteam, steam, FALSE);
972 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
974 // kill player when changing teams
975 if(self.deadflag == DEAD_NO)
976 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
980 void ShufflePlayerOutOfTeam (float source_team)
982 float smallestteam, smallestteam_count, steam;
983 float lowest_bot_score, lowest_player_score;
984 entity head, lowest_bot, lowest_player, selected;
987 smallestteam_count = 999999999;
989 if(c1 >= 0 && c1 < smallestteam_count)
992 smallestteam_count = c1;
994 if(c2 >= 0 && c2 < smallestteam_count)
997 smallestteam_count = c2;
999 if(c3 >= 0 && c3 < smallestteam_count)
1002 smallestteam_count = c3;
1004 if(c4 >= 0 && c4 < smallestteam_count)
1007 smallestteam_count = c4;
1012 bprint("warning: no smallest team\n");
1016 if(source_team == 1)
1017 steam = COLOR_TEAM1;
1018 else if(source_team == 2)
1019 steam = COLOR_TEAM2;
1020 else if(source_team == 3)
1021 steam = COLOR_TEAM3;
1022 else if(source_team == 4)
1023 steam = COLOR_TEAM4;
1026 lowest_bot_score = 999999999;
1027 lowest_player = world;
1028 lowest_player_score = 999999999;
1030 // find the lowest-scoring player & bot of that team
1031 FOR_EACH_PLAYER(head)
1033 if(head.team == steam)
1037 if(head.frags < lowest_bot_score)
1040 lowest_bot_score = head.frags;
1045 if(head.frags < lowest_player_score)
1047 lowest_player = head;
1048 lowest_player_score = head.frags;
1054 // prefers to move a bot...
1055 if(lowest_bot != world)
1056 selected = lowest_bot;
1057 // but it will move a player if it has to
1059 selected = lowest_player;
1060 // don't do anything if it couldn't find anyone
1063 bprint("warning: couldn't find a player to move from team\n");
1067 // smallest team gains a member
1068 if(smallestteam == 1)
1072 else if(smallestteam == 2)
1076 else if(smallestteam == 3)
1080 else if(smallestteam == 4)
1086 bprint("warning: destination team invalid\n");
1089 // source team loses a member
1090 if(source_team == 1)
1094 else if(source_team == 2)
1098 else if(source_team == 3)
1102 else if(source_team == 4)
1108 bprint("warning: source team invalid\n");
1112 // move the player to the new team
1113 TeamchangeFrags(selected);
1114 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1116 if(selected.deadflag == DEAD_NO)
1117 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1118 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1121 float lastRebalanceInfo;
1122 void CauseRebalance(float source_team, float howmany_toomany)
1127 if(IsTeamBalanceForced() == 1)
1129 bprint("Rebalancing Teams\n");
1130 ShufflePlayerOutOfTeam(source_team);
1134 if(howmany_toomany < cvar("g_balance_teams_complain"))
1136 if(time < lastRebalanceInfo + 90)
1138 lastRebalanceInfo = time;
1139 if(source_team == 1)
1140 steam = COLOR_TEAM1;
1141 else if(source_team == 2)
1142 steam = COLOR_TEAM2;
1143 else if(source_team == 3)
1144 steam = COLOR_TEAM3;
1145 else if(source_team == 4)
1146 steam = COLOR_TEAM4;
1147 ServerConsoleEcho(strcat("Team ", ftos(source_team), " too large, complaining."), TRUE);
1148 FOR_EACH_REALPLAYER(head)
1150 if(head.team == steam)
1152 sprint(head, "\{1}\{13}^3SERVER NOTICE:^7 One of you please change teams!\n");
1153 centerprint_atprio(head, CENTERPRIO_REBALANCE, "^3SERVER NOTICE:\n\n^7Someone of you please change teams!");
1159 // part of g_balance_teams_force
1160 // occasionally perform an audit of the teams to make
1161 // sure they're more or less balanced in player count.
1164 float numplayers, numteams, smallest, toomany;
1166 balance = IsTeamBalanceForced();
1170 if(audit_teams_time > time)
1173 audit_teams_time = time + 4 + random();
1175 // bprint("Auditing teams\n");
1177 CheckAllowedTeams();
1178 GetTeamCounts(world);
1181 numteams = numplayers = smallest = 0;
1184 numteams = numteams + 1;
1185 numplayers = numplayers + c1;
1190 numteams = numteams + 1;
1191 numplayers = numplayers + c2;
1197 numteams = numteams + 1;
1198 numplayers = numplayers + c3;
1204 numteams = numteams + 1;
1205 numplayers = numplayers + c4;
1211 return; // no players to move around
1213 return; // don't bother shuffling if for some reason there aren't any teams
1215 toomany = smallest + 1;
1217 if(c1 && c1 > toomany)
1218 CauseRebalance(1, c1 - toomany);
1219 if(c2 && c2 > toomany)
1220 CauseRebalance(2, c2 - toomany);
1221 if(c3 && c3 > toomany)
1222 CauseRebalance(3, c3 - toomany);
1223 if(c4 && c4 > toomany)
1224 CauseRebalance(4, c4 - toomany);
1226 // if teams are still unbalanced, balance them further in the next audit,
1227 // which will happen sooner (keep doing rapid audits until things are in order)
1228 audit_teams_time = time + 0.7 + random()*0.3;
1233 /*void(entity e, float first) UpdateTeamScore =
1238 WriteByte (MSG_ALL, SVC_UPDATENAME);
1239 WriteByte (MSG_ALL, clientno);
1240 WriteString (MSG_ALL, e.netname);
1242 WriteByte (MSG_ALL, SVC_UPDATECOLORS);
1243 WriteByte (MSG_ALL, clientno);
1244 WriteByte (MSG_ALL, e.b_shirt * 16 + who.b_pants);
1247 WriteByte (MSG_ALL, SVC_UPDATEFRAGS);
1248 WriteByte (MSG_ALL, clientno);
1249 WriteShort (MSG_ALL, e.frags + 10000);
1255 // code from here on is just to support maps that don't have team entities
1256 void tdm_spawnteam (string teamname, float teamcolor)
1260 e.classname = "tdm_team";
1261 e.netname = teamname;
1266 // spawn some default teams if the map is not set up for tdm
1267 void() tdm_spawnteams =
1271 numteams = cvar("g_tdm_teams");
1273 tdm_spawnteam("Red", COLOR_TEAM1-1);
1274 tdm_spawnteam("Blue", COLOR_TEAM2-1);
1277 void() tdm_delayedinit =
1279 self.think = SUB_Remove;
1280 self.nextthink = time;
1281 // if no teams are found, spawn defaults
1282 if (find(world, classname, "tdm_team") == world)
1290 e.think = tdm_delayedinit;
1291 e.nextthink = time + 0.1;