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;
9 float GAME_ASSAULT = 9;
10 float GAME_ONSLAUGHT = 10;
12 // client counts for each team
14 // # of bots on those teams
15 float cb1, cb2, cb3, cb4;
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_dm", "0");
100 cvar_set("g_tdm", "0");
101 cvar_set("g_domination", "0");
102 cvar_set("g_ctf", "0");
103 cvar_set("g_runematch", "0");
104 cvar_set("g_lms", "0");
105 cvar_set("g_arena", "0");
106 cvar_set("g_keyhunt", "0");
107 cvar_set("g_assault", "0");
108 cvar_set("g_onslaught", "0");
109 cvar_set("teamplay", "0");
112 void ActivateTeamplay()
114 float teamplay_default;
115 teamplay_default = cvar("teamplay_default");
118 cvar_set("teamplay", ftos(teamplay_default));
120 cvar_set("teamplay", "3");
123 void InitGameplayMode()
125 float fraglimit_override, timelimit_override;
129 game = cvar("gamecfg"); // load game options
131 // game cvars get reset before map changes
132 // then map's cfg sets them as desired
134 // FIXME: also set a message or game mode name to print to players when the join
136 // set both here, gamemode can override it later
137 timelimit_override = cvar("timelimit_override");
138 fraglimit_override = cvar("fraglimit_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");
163 gamemode_name = "Capture the Flag";
166 else if(game == GAME_RUNEMATCH || cvar("g_runematch"))
168 game = GAME_RUNEMATCH;
169 cvar_set("g_runematch", "1");
170 cvar_set("g_minstagib", "0");
172 if(cvar("deathmatch_force_teamplay"))
175 fraglimit_override = cvar("g_runematch_point_limit");
177 gamemode_name = "Rune Match";
183 else if(game == GAME_DEATHMATCH || game == GAME_TEAM_DEATHMATCH || cvar("g_tdm"))
185 if(!cvar("deathmatch"))
186 cvar_set("deathmatch", "1");
189 if(game == GAME_TEAM_DEATHMATCH || cvar("g_tdm") || cvar("deathmatch_force_teamplay"))
192 game = GAME_TEAM_DEATHMATCH;
193 gamemode_name = "Team Deathmatch";
196 cvar_set("g_tdm", "1");
201 game = GAME_DEATHMATCH;
202 gamemode_name = "Deathmatch";
206 fraglimit_override = cvar("fraglimit_override");
208 else if(game == GAME_ASSAULT || cvar("g_assault"))
212 gamemode_name = "Assault";
215 cvar_set("g_assault", "1");
217 else if(game == GAME_LMS || cvar("g_lms"))
221 cvar_set("g_lms", "1");
222 fraglimit_override = cvar("g_lms_lives_override");
223 if(fraglimit_override == 0)
224 fraglimit_override = -1;
225 gamemode_name = "Last Man Standing";
227 lms_lowest_lives = 999;
229 else if(game == GAME_ARENA || cvar("g_arena"))
233 cvar_set("g_arena", "1");
234 fraglimit_override = cvar("g_arena_point_limit");
235 maxspawned = cvar("g_arena_maxspawned");
238 arena_roundbased = cvar("g_arena_roundbased");
239 gamemode_name = "Arena";
242 else if(game == GAME_KEYHUNT || cvar("g_keyhunt"))
246 cvar_set("g_keyhunt", "1");
247 fraglimit_override = cvar("g_keyhunt_point_limit");
249 gamemode_name = "Key Hunt";
252 else if(game == GAME_ONSLAUGHT || cvar("g_onslaught"))
255 game = GAME_ONSLAUGHT;
256 cvar_set("g_onslaught", "1");
260 gamemode_name = "Onslaught";
265 // we can only assume...
267 cvar_set("g_dm", "1");
268 gamemode_name = "Deathmatch";
271 /* else if(game == GAME_TEAM_DEATHMATCH)
273 if(!cvar("deathmatch"))
274 cvar_set("deathmatch", "1");
276 //if(!cvar("teamplay"))
277 // cvar_set("teamplay", "3");
280 fraglimit_override = cvar("fraglimit_override");
283 // those mutators rule each other out
284 if(cvar("g_minstagib"))
286 cvar_set("g_instagib", "0");
287 cvar_set("g_rocketarena", "0");
289 if(cvar("g_instagib"))
291 cvar_set("g_minstagib", "0");
292 cvar_set("g_rocketarena", "0");
294 if(cvar("g_rocketarena"))
296 cvar_set("g_instagib", "0");
297 cvar_set("g_minstagib", "0");
300 registercvar("_motd", "");
301 registercvar("_mutatormsg", "");
302 cvar_set("_motd", wordwrap(cvar_string("sv_motd"), 50));
303 cvar_set("_mutatormsg", wordwrap(cvar_string("g_mutatormsg"), 50));
305 g_domination = cvar("g_domination");
306 g_ctf = cvar("g_ctf");
307 g_lms = cvar("g_lms");
308 g_tdm = cvar("g_tdm");
309 g_runematch = cvar("g_runematch");
310 g_keyhunt = cvar("g_keyhunt");
311 g_onslaught = cvar("g_onslaught");
312 g_assault = cvar("g_assault");
313 g_arena = cvar("g_arena");
315 // enforce the server's universal frag/time limits
316 if(!cvar("g_campaign"))
318 if(fraglimit_override >= 0)
319 cvar_set("fraglimit", ftos(fraglimit_override));
320 if(timelimit_override >= 0)
321 cvar_set("timelimit", ftos(timelimit_override));
324 if (game == GAME_DOMINATION)//cvar("g_domination"))
326 else if (game == GAME_CTF)//cvar("g_ctf"))
328 else if (game == GAME_RUNEMATCH)//cvar("g_runematch"))
330 else if (game == GAME_TEAM_DEATHMATCH)//cvar("g_runematch"))
332 else if (game == GAME_KEYHUNT)//cvar("g_keyhunt"))
336 string GetClientVersionMessage(float v) {
337 local string versionmsg;
339 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";
340 // either that or someone wants to be funny
341 } else if (v != cvar("gameversion")) {
342 if(v < cvar("gameversion")) {
343 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
345 versionmsg = "^3This server is using an outdated Nexuiz version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
348 versionmsg = "^2client version and server version are compatible.^8";
350 return strzone(versionmsg);
355 void PrintWelcomeMessage(entity pl)
357 string s, mutator, modifications, padding;
359 /*if(self.welcomemessage_time > time)
361 self.welcomemessage_time = time + 0.8; */
363 if(self.cvar_scr_centertime == 0) return;
364 if(self.welcomemessage_time > time) return;
365 self.welcomemessage_time = time + self.cvar_scr_centertime * 0.6;
367 if(cvar("g_campaign"))
369 centerprint(pl, campaign_message);
373 if(self.classname == "observer")
375 if(g_lms && self.frags <= 0 && self.frags > -666)
376 return centerprint(self, strcat(NEWLINES, "^1You have no more lives left\nwait for next round\n\n\n^7press attack to spectate other players"));
377 else if(g_lms && self.frags == -666)
378 return centerprint(self, strcat(NEWLINES, "^1Match has already begun\nwait for next round\n\n\n^7press attack to spectate other players"));
380 else if(self.classname == "spectator")
382 if ((g_lms && self.frags < 1) || g_arena)
383 return centerprint(self, strcat(NEWLINES, "spectating ", self.enemy.netname, "\n\n\n^7press attack for next player\npress attack2 for free fly mode"));
385 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"));
390 mutator = "^2Minstagib ^1";
392 mutator = "^2Instagib ^1";
393 else if(g_rocketarena)
394 mutator = "^2Rocketarena ^1";
396 mutator = "^2No Items Nexuiz ^1";
399 // to protect against unheedingly made changes
401 modifications = strcat(modifications, ", ");
403 modifications = "midair";
407 modifications = strcat(modifications, ", ");
409 modifications = strcat(modifications, "vampire");
411 if(g_laserguided_missile) {
413 modifications = strcat(modifications, ", ");
415 modifications = strcat(modifications, "laser-guided-missiles");
418 local string versionmessage;
419 versionmessage = GetClientVersionMessage(self.version);
421 s = strcat(s, NEWLINES, "This is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
422 s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
424 if(modifications != "")
425 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
427 if(time < restart_countdown)
428 s = strcat(s, "\n^1Game starts in ", ftos(ceil(restart_countdown-time)), " seconds^7");
430 if((self.classname == "observer" || self.classname == "spectator") && self.version == cvar("gameversion")) {
432 s = strcat(s,"^7\n\n\npress jump to play\npress attack to spectate other players\n\n");
433 else if(player_count < 2 && arena_roundbased)
435 s = strcat(s, "\n\n\n^1waiting for second player to start match^7\n\n");
439 s = strcat(s, "\n\n\n");
441 s = strcat(s, "^7current champion is: ", champion.netname, "\n\n");
442 s = strcat(s,"^7press attack to spectate other players\n\n");
449 if (g_grappling_hook)
450 s = strcat(s, "\n\n^8grappling hook is enabled, press 'e' to use it\n");
452 if (cvar_string("_mutatormsg") != "") {
453 s = strcat(s, "\n\n^8special gameplay tips: ^7", cvar_string("_mutatormsg"));
456 if (cvar_string("_motd") != "") {
457 s = strcat(s, "\n\n^8MOTD: ^7", cvar_string("_motd"));
461 if(cvar("fraglimit"))
464 if(cvar("timelimit"))
467 s = strcat(s, "\n^8frag limit: ^7", cvar_string("fraglimit"), padding);
469 if(cvar("timelimit"))
470 s = strcat(s, "\n^8time limit: ^7", cvar_string("timelimit"), " minutes");
481 void SetPlayerColors(entity pl, float _color)
485 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
487 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
488 pl.clientcolors = 16*cl + cl;*/
491 pants = _color & 0x0F;
492 shirt = _color & 0xF0;
496 setcolor(pl, 16*pants + pants);
498 setcolor(pl, shirt + pants);
502 void SetPlayerTeam(entity pl, float t, float s, float noprint)
507 _color = COLOR_TEAM4 - 1;
509 _color = COLOR_TEAM3 - 1;
511 _color = COLOR_TEAM2 - 1;
513 _color = COLOR_TEAM1 - 1;
515 SetPlayerColors(pl,_color);
517 if(!noprint && t != s)
519 //bprint(pl.netname, " has changed to ", TeamNoName(t), "\n");
520 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
532 // set c1...c4 to show what teams are allowed
533 void CheckAllowedTeams (entity for_whom)
542 c1 = c2 = c3 = c4 = -1;
543 cb1 = cb2 = cb3 = cb4 = 0;
545 // onslaught is special
548 head = findchain(classname, "onslaught_generator");
551 if (head.team == COLOR_TEAM1) c1 = 0;
552 if (head.team == COLOR_TEAM2) c2 = 0;
553 if (head.team == COLOR_TEAM3) c3 = 0;
554 if (head.team == COLOR_TEAM4) c4 = 0;
561 teament_name = "dom_team";
563 teament_name = "ctf_team";
565 teament_name = "tdm_team";
568 c1 = c2 = 0; // Assault always has 2 teams
573 // cover anything else by treating it like tdm with no teams spawned
577 dm = cvar("g_tdm_teams");
579 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
583 c1 = c2 = c3 = c4 = 0;
596 // first find out what teams are allowed
597 head = find(world, classname, teament_name);
600 if(!(g_domination && head.netname == ""))
602 if(head.team == COLOR_TEAM1)
606 if(head.team == COLOR_TEAM2)
610 if(head.team == COLOR_TEAM3)
614 if(head.team == COLOR_TEAM4)
619 head = find(head, classname, teament_name);
622 if(cvar("bot_vs_human") > 0)
625 if(clienttype(for_whom) == CLIENTTYPE_BOT)
630 else if(cvar("bot_vs_human") < 0)
633 if(clienttype(for_whom) == CLIENTTYPE_BOT)
640 float PlayerValue(entity p)
642 if(IsTeamBalanceForced() == 1)
647 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
648 // teams that are allowed will now have their player counts stored in c1...c4
649 void GetTeamCounts(entity ignore)
653 // now count how many players are on each team already
655 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
656 // also remember the lowest-scoring player
658 FOR_EACH_PLAYER(head)
660 if(head != ignore)// && head.netname != "")
662 value = PlayerValue(head);
663 if(clienttype(head) == CLIENTTYPE_BOT)
667 if(head.team == COLOR_TEAM1)
675 if(head.team == COLOR_TEAM2)
683 if(head.team == COLOR_TEAM3)
691 if(head.team == COLOR_TEAM4)
703 // returns # of smallest team (1, 2, 3, 4)
704 // NOTE: Assumes CheckAllowedTeams has already been called!
705 float FindSmallestTeam(entity pl, float ignore_pl)
707 float totalteams, smallestteam, smallestteam_count, smallestteam_score, balance_type;
710 // find out what teams are available
711 //CheckAllowedTeams();
713 // make sure there are at least 2 teams to join
715 totalteams = totalteams + 1;
717 totalteams = totalteams + 1;
719 totalteams = totalteams + 1;
721 totalteams = totalteams + 1;
723 if(cvar("bot_vs_human"))
729 error("Too few teams available for domination\n");
731 error("Too few teams available for ctf\n");
733 error("Too few teams available for key hunt\n");
735 error("Too few teams available for team deathmatch\n");
739 // count how many players are in each team
743 GetTeamCounts(world);
745 // c1...c4 now have counts of each team
746 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
749 smallestteam_count = 999999999;
750 smallestteam_score = 999999999;
752 // 2 gives priority to what team you're already on, 1 goes in order
753 // 2 doesn't seem to work though...
757 //if(pl.classname != "player")
758 if(clienttype(pl) != CLIENTTYPE_BOT)
760 c1 -= cb1 * 255.0/256;
761 c2 -= cb2 * 255.0/256;
762 c3 -= cb3 * 255.0/256;
763 c4 -= cb4 * 255.0/256;
766 if(balance_type == 1)
768 if(c1 >= 0 && (c1 < smallestteam_count || (c1 <= smallestteam_count && team1_score < smallestteam_score)))
771 smallestteam_count = c1;
772 smallestteam_score = team1_score;
774 if(c2 >= 0 && (c2 < smallestteam_count || (c2 <= smallestteam_count && team2_score < smallestteam_score)))
777 smallestteam_count = c2;
778 smallestteam_score = team2_score;
780 if(c3 >= 0 && (c3 < smallestteam_count || (c3 <= smallestteam_count && team3_score < smallestteam_score)))
783 smallestteam_count = c3;
784 smallestteam_score = team3_score;
786 if(c4 >= 0 && (c4 < smallestteam_count || (c4 <= smallestteam_count && team4_score < smallestteam_score)))
789 smallestteam_count = c4;
790 smallestteam_score = team4_score;
795 if(c1 >= 0 && (c1 < smallestteam_count ||
796 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
799 smallestteam_count = c1;
801 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
802 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
805 smallestteam_count = c2;
807 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
808 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
811 smallestteam_count = c3;
813 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
814 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
817 smallestteam_count = c4;
824 float JoinBestTeam(entity pl, float only_return_best)
826 float smallest, selectedteam;
828 // don't join a team if we're not playing a team game
829 if(!cvar("teamplay") && !g_domination && !g_ctf && !g_keyhunt)
832 // find out what teams are available
833 CheckAllowedTeams(pl);
837 if(cvar("g_domination_default_teams") < 3)
839 if(cvar("g_domination_default_teams") < 4)
843 // if we don't care what team he ends up on, put him on whatever team he entered as.
844 // if he's not on a valid team, then let other code put him on the smallest team
845 if(!cvar("g_campaign") && !cvar("g_balance_teams") && !cvar("g_balance_teams_force"))
847 if( c1 >= 0 && pl.team == COLOR_TEAM1)
848 selectedteam = pl.team;
849 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
850 selectedteam = pl.team;
851 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
852 selectedteam = pl.team;
853 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
854 selectedteam = pl.team;
859 if(!only_return_best)
861 SetPlayerColors(pl, selectedteam - 1);
866 // otherwise end up on the smallest team (handled below)
869 smallest = FindSmallestTeam(pl, TRUE);
872 if(!only_return_best)
874 TeamchangeFrags(self);
877 SetPlayerColors(pl, COLOR_TEAM1 - 1);
879 else if(smallest == 2)
881 SetPlayerColors(pl, COLOR_TEAM2 - 1);
883 else if(smallest == 3)
885 SetPlayerColors(pl, COLOR_TEAM3 - 1);
887 else if(smallest == 4)
889 SetPlayerColors(pl, COLOR_TEAM4 - 1);
893 error("smallest team: invalid team\n");
896 if(pl.deadflag == DEAD_NO)
897 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
904 void SV_ChangeTeam(float _color)
906 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
908 // in normal deathmatch we can just apply the color and we're done
909 if(!cvar("teamplay")) {
910 SetPlayerColors(self, _color);
914 scolor = self.clientcolors & 0x0F;
915 dcolor = _color & 0x0F;
917 if(scolor == COLOR_TEAM1 - 1)
919 else if(scolor == COLOR_TEAM2 - 1)
921 else if(scolor == COLOR_TEAM3 - 1)
923 else if(scolor == COLOR_TEAM4 - 1)
925 if(dcolor == COLOR_TEAM1 - 1)
927 else if(dcolor == COLOR_TEAM2 - 1)
929 else if(dcolor == COLOR_TEAM3 - 1)
931 else if(dcolor == COLOR_TEAM4 - 1)
934 CheckAllowedTeams(self);
936 if(dteam == 1 && c1 < 0) dteam = 4;
937 if(dteam == 4 && c4 < 0) dteam = 3;
938 if(dteam == 3 && c3 < 0) dteam = 2;
939 if(dteam == 2 && c2 < 0) dteam = 1;
941 // not changing teams
944 //bprint("same team change\n");
945 SetPlayerTeam(self, dteam, steam, TRUE);
951 if(cvar("g_campaign") || cvar("g_changeteam_banned"))
953 sprint(self, "Team changes not allowed\n");
954 return; // changing teams is not allowed
957 if(!cvar("g_campaign") && cvar("g_balance_teams_prevent_imbalance"))
959 // only allow changing to a smaller or equal size team
961 // find out what teams are available
962 //CheckAllowedTeams();
963 // count how many players on each team
964 GetTeamCounts(world);
967 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
972 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
977 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
982 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
989 sprint(self, "Cannot change to an invalid team\n");
995 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
997 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
999 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
1001 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
1004 if(scount) // started at a valid, nonempty team
1006 // check if we're trying to change to a larger team that doens't have bots to swap with
1007 if(dcount >= scount && dbotcount <= 0)
1009 sprint(self, "Cannot change to a larger team\n");
1010 return; // can't change to a larger team
1016 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
1018 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1020 // reduce frags during a team change
1021 TeamchangeFrags(self);
1024 SetPlayerTeam(self, dteam, steam, FALSE);
1026 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1028 // kill player when changing teams
1029 if(self.deadflag == DEAD_NO)
1030 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
1034 void ShufflePlayerOutOfTeam (float source_team)
1036 float smallestteam, smallestteam_count, steam;
1037 float lowest_bot_score, lowest_player_score;
1038 entity head, lowest_bot, lowest_player, selected;
1041 smallestteam_count = 999999999;
1043 if(c1 >= 0 && c1 < smallestteam_count)
1046 smallestteam_count = c1;
1048 if(c2 >= 0 && c2 < smallestteam_count)
1051 smallestteam_count = c2;
1053 if(c3 >= 0 && c3 < smallestteam_count)
1056 smallestteam_count = c3;
1058 if(c4 >= 0 && c4 < smallestteam_count)
1061 smallestteam_count = c4;
1066 bprint("warning: no smallest team\n");
1070 if(source_team == 1)
1071 steam = COLOR_TEAM1;
1072 else if(source_team == 2)
1073 steam = COLOR_TEAM2;
1074 else if(source_team == 3)
1075 steam = COLOR_TEAM3;
1076 else if(source_team == 4)
1077 steam = COLOR_TEAM4;
1080 lowest_bot_score = 999999999;
1081 lowest_player = world;
1082 lowest_player_score = 999999999;
1084 // find the lowest-scoring player & bot of that team
1085 FOR_EACH_PLAYER(head)
1087 if(head.team == steam)
1091 if(head.frags < lowest_bot_score)
1094 lowest_bot_score = head.frags;
1099 if(head.frags < lowest_player_score)
1101 lowest_player = head;
1102 lowest_player_score = head.frags;
1108 // prefers to move a bot...
1109 if(lowest_bot != world)
1110 selected = lowest_bot;
1111 // but it will move a player if it has to
1113 selected = lowest_player;
1114 // don't do anything if it couldn't find anyone
1117 bprint("warning: couldn't find a player to move from team\n");
1121 // smallest team gains a member
1122 if(smallestteam == 1)
1126 else if(smallestteam == 2)
1130 else if(smallestteam == 3)
1134 else if(smallestteam == 4)
1140 bprint("warning: destination team invalid\n");
1143 // source team loses a member
1144 if(source_team == 1)
1148 else if(source_team == 2)
1152 else if(source_team == 3)
1156 else if(source_team == 4)
1162 bprint("warning: source team invalid\n");
1166 // move the player to the new team
1167 TeamchangeFrags(selected);
1168 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1170 if(selected.deadflag == DEAD_NO)
1171 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1172 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1175 float lastRebalanceInfo;
1176 void CauseRebalance(float source_team, float howmany_toomany)
1181 if(IsTeamBalanceForced() == 1)
1183 bprint("Rebalancing Teams\n");
1184 ShufflePlayerOutOfTeam(source_team);
1188 if(howmany_toomany < cvar("g_balance_teams_complain"))
1190 if(time < lastRebalanceInfo + 90)
1192 lastRebalanceInfo = time;
1193 if(source_team == 1)
1194 steam = COLOR_TEAM1;
1195 else if(source_team == 2)
1196 steam = COLOR_TEAM2;
1197 else if(source_team == 3)
1198 steam = COLOR_TEAM3;
1199 else if(source_team == 4)
1200 steam = COLOR_TEAM4;
1201 ServerConsoleEcho(strcat("Team ", ftos(source_team), " too large, complaining."), TRUE);
1202 FOR_EACH_REALPLAYER(head)
1204 if(head.team == steam)
1206 sprint(head, "\{1}\{13}^3SERVER NOTICE:^7 One of you please change teams!\n");
1207 centerprint_atprio(head, CENTERPRIO_REBALANCE, "^3SERVER NOTICE:\n\n^7Someone of you please change teams!");
1213 // part of g_balance_teams_force
1214 // occasionally perform an audit of the teams to make
1215 // sure they're more or less balanced in player count.
1218 float numplayers, numteams, smallest, toomany;
1220 balance = IsTeamBalanceForced();
1224 if(audit_teams_time > time)
1227 audit_teams_time = time + 4 + random();
1229 // bprint("Auditing teams\n");
1231 CheckAllowedTeams(world);
1232 GetTeamCounts(world);
1235 numteams = numplayers = smallest = 0;
1238 numteams = numteams + 1;
1239 numplayers = numplayers + c1;
1244 numteams = numteams + 1;
1245 numplayers = numplayers + c2;
1251 numteams = numteams + 1;
1252 numplayers = numplayers + c3;
1258 numteams = numteams + 1;
1259 numplayers = numplayers + c4;
1265 return; // no players to move around
1267 return; // don't bother shuffling if for some reason there aren't any teams
1269 toomany = smallest + 1;
1271 if(c1 && c1 > toomany)
1272 CauseRebalance(1, c1 - toomany);
1273 if(c2 && c2 > toomany)
1274 CauseRebalance(2, c2 - toomany);
1275 if(c3 && c3 > toomany)
1276 CauseRebalance(3, c3 - toomany);
1277 if(c4 && c4 > toomany)
1278 CauseRebalance(4, c4 - toomany);
1280 // if teams are still unbalanced, balance them further in the next audit,
1281 // which will happen sooner (keep doing rapid audits until things are in order)
1282 audit_teams_time = time + 0.7 + random()*0.3;
1287 /*void(entity e, float first) UpdateTeamScore =
1292 WriteByte (MSG_ALL, SVC_UPDATENAME);
1293 WriteByte (MSG_ALL, clientno);
1294 WriteString (MSG_ALL, e.netname);
1296 WriteByte (MSG_ALL, SVC_UPDATECOLORS);
1297 WriteByte (MSG_ALL, clientno);
1298 WriteByte (MSG_ALL, e.b_shirt * 16 + who.b_pants);
1301 WriteByte (MSG_ALL, SVC_UPDATEFRAGS);
1302 WriteByte (MSG_ALL, clientno);
1303 WriteShort (MSG_ALL, e.frags + 10000);
1309 // code from here on is just to support maps that don't have team entities
1310 void tdm_spawnteam (string teamname, float teamcolor)
1314 e.classname = "tdm_team";
1315 e.netname = teamname;
1320 // spawn some default teams if the map is not set up for tdm
1321 void() tdm_spawnteams =
1325 numteams = cvar("g_tdm_teams");
1327 tdm_spawnteam("Red", COLOR_TEAM1-1);
1328 tdm_spawnteam("Blue", COLOR_TEAM2-1);
1330 tdm_spawnteam("Yellow", COLOR_TEAM3-1);
1332 tdm_spawnteam("Pink", COLOR_TEAM4-1);
1335 void() tdm_delayedinit =
1337 self.think = SUB_Remove;
1338 self.nextthink = time;
1339 // if no teams are found, spawn defaults
1340 if (find(world, classname, "tdm_team") == world)
1348 e.think = tdm_delayedinit;
1349 e.nextthink = time + 0.1;