1 float GAME_DEATHMATCH = 1;
2 float GAME_TEAM_DEATHMATCH = 2;
3 float GAME_DOMINATION = 3;
5 float GAME_RUNEMATCH = 5;
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;
17 float audit_teams_time;
19 void TeamchangeFrags(entity e)
21 if(e.classname == "player")
23 // reduce frags during a team change
24 DistributeFragsAmongTeam(e, (e.clientcolors & 0x0F) + 1, 1 - cvar("g_changeteam_fragtransfer") / 100);
28 string TeamName(float t)
30 // fixme: Search for team entities and get their .netname's!
39 return "Neutral Team";
41 string ColoredTeamName(float t)
43 // fixme: Search for team entities and get their .netname's!
45 return "^1Red Team^7";
47 return "^4Blue Team^7";
49 return "^6Pink Team^7";
51 return "^3Yellow Team^7";
52 return "Neutral Team";
54 string TeamNoName(float t)
56 // fixme: Search for team entities and get their .netname's!
65 return "Neutral Team";
70 void runematch_init();
73 void(entity pl) LogTeamchange =
76 if(!cvar("sv_eventlog"))
80 str = strcat(":team:", ftos(pl.playerid), ":");
81 str = strcat(str, ftos(pl.team));
82 GameLogEcho(str, FALSE);
87 cvar_set("g_tdm", "0");
88 cvar_set("g_domination", "0");
89 cvar_set("g_ctf", "0");
90 cvar_set("g_runematch", "0");
91 cvar_set("g_lms", "0");
92 cvar_set("g_arena", "0");
93 cvar_set("teamplay", "0");
96 void ActivateTeamplay()
98 float teamplay_default;
99 teamplay_default = cvar("teamplay_default");
102 cvar_set("teamplay", ftos(teamplay_default));
104 cvar_set("teamplay", "3");
107 void InitGameplayMode()
109 float fraglimit_override, timelimit_override;
113 game = cvar ("gamecfg"); // load game options
115 // game cvars get reset before map changes
116 // then map's cfg sets them as desired
118 // FIXME: also set a message or game mode name to print to players when the join
120 // set both here, gamemode can override it later
121 timelimit_override = cvar("timelimit_override");
122 fraglimit_override = cvar("fraglimit_override");
124 if(game == GAME_DOMINATION || cvar("g_domination"))
127 game = GAME_DOMINATION;
128 cvar_set("g_domination", "1");
132 fraglimit_override = cvar("g_domination_point_limit");
134 gamemode_name = "Domination";
137 else if(game == GAME_CTF || cvar("g_ctf"))
141 cvar_set("g_ctf", "1");
145 fraglimit_override = cvar("g_ctf_capture_limit");
147 gamemode_name = "Capture the Flag";
150 else if((game == GAME_RUNEMATCH || cvar("g_runematch")) && !cvar("g_minstagib"))
152 game = GAME_RUNEMATCH;
153 cvar_set("g_runematch", "1");
155 if(cvar("deathmatch_force_teamplay"))
158 fraglimit_override = cvar("g_runematch_point_limit");
160 gamemode_name = "Rune Match";
166 else if(game == GAME_DEATHMATCH || game == GAME_TEAM_DEATHMATCH || cvar("g_tdm"))
168 if(!cvar("deathmatch"))
169 cvar_set("deathmatch", "1");
172 if(game == GAME_TEAM_DEATHMATCH || cvar("g_tdm") || cvar("deathmatch_force_teamplay"))
175 game = GAME_TEAM_DEATHMATCH;
176 gamemode_name = "Team Deathmatch";
179 cvar_set("g_tdm", "1");
184 game = GAME_DEATHMATCH;
185 gamemode_name = "Deathmatch";
189 fraglimit_override = cvar("fraglimit_override");
191 else if(game == GAME_LMS || cvar("g_lms"))
195 cvar_set("g_lms", "1");
196 fraglimit_override = cvar("g_lms_lives_override");
197 if(fraglimit_override == 0)
198 fraglimit_override = -1;
199 gamemode_name = "Last Man Standing";
201 lms_lowest_lives = 999;
203 else if(game == GAME_ARENA || cvar("g_arena"))
207 cvar_set("g_arena", "1");
208 fraglimit_override = cvar("g_arena_point_limit");
209 maxspawned = cvar("g_arena_maxspawned");
212 arena_roundbased = cvar("g_arena_roundbased");
213 gamemode_name = "Arena";
218 // we can only assume...
220 gamemode_name = "Deathmatch";
223 /* else if(game == GAME_TEAM_DEATHMATCH)
225 if(!cvar("deathmatch"))
226 cvar_set("deathmatch", "1");
228 //if(!cvar("teamplay"))
229 // cvar_set("teamplay", "3");
232 fraglimit_override = cvar("fraglimit_override");
235 // enforce the server's universal frag/time limits
236 if(!cvar("g_campaign"))
238 if(fraglimit_override >= 0)
239 cvar_set("fraglimit", ftos(fraglimit_override));
240 if(timelimit_override >= 0)
241 cvar_set("timelimit", ftos(timelimit_override));
244 if (game == GAME_DOMINATION)//cvar("g_domination"))
246 else if (game == GAME_CTF)//cvar("g_ctf"))
248 else if (game == GAME_RUNEMATCH)//cvar("g_runematch"))
250 else if (game == GAME_TEAM_DEATHMATCH)//cvar("g_runematch"))
253 // those mutators rule each other out
254 if(cvar("g_minstagib"))
256 cvar_set("g_instagib", "0");
257 cvar_set("g_rocketarena", "0");
259 if(cvar("g_instagib"))
261 cvar_set("g_minstagib", "0");
262 cvar_set("g_rocketarena", "0");
264 if(cvar("g_rocketarena"))
266 cvar_set("g_instagib", "0");
267 cvar_set("g_minstagib", "0");
270 registercvar("_motd", "");
271 registercvar("_mutatormsg", "");
272 cvar_set("_motd", wordwrap(cvar_string("sv_motd"), 50));
273 cvar_set("_mutatormsg", wordwrap(cvar_string("g_mutatormsg"), 50));
275 g_domination = cvar("g_domination");
276 g_ctf = cvar("g_ctf");
277 g_tdm = cvar("g_tdm");
280 string GetClientVersionMessage(float v) {
281 local string versionmsg;
283 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";
284 // either that or someone wants to be funny
285 } else if (v != cvar("gameversion")) {
286 if(v < cvar("gameversion")) {
287 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
289 versionmsg = "^3This server is using an outdated Nexuiz version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
292 versionmsg = "^2client version and server version are compatible.^8";
294 return strzone(versionmsg);
299 void PrintWelcomeMessage(entity pl)
301 string s, mutator, modifications, padding;
303 /*if(self.welcomemessage_time > time)
305 self.welcomemessage_time = time + 0.8; */
307 if(self.cvar_scr_centertime == 0) return;
308 if(self.welcomemessage_time > time) return;
309 self.welcomemessage_time = time + self.cvar_scr_centertime * 0.6;
311 if(cvar("g_campaign"))
313 centerprint(pl, campaign_message);
317 if(self.classname == "observer")
319 if(cvar("g_lms") && self.frags <= 0 && self.frags > -666)
320 return centerprint(self, strcat(newlines, "^1You have no more lives left\nwait for next round\n\n\n^7press attack to spectate other players"));
321 else if(cvar("g_lms") && self.frags == -666)
322 return centerprint(self, strcat(newlines, "^1Match has already begun\nwait for next round\n\n\n^7press attack to spectate other players"));
324 else if(self.classname == "spectator")
326 if ((cvar("g_lms") && self.frags < 1) || cvar("g_arena"))
327 return centerprint(self, strcat(newlines, "spectating ", self.enemy.netname, "\n\n\n^7press attack for next player\npress attack2 for free fly mode"));
329 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"));
333 if(cvar("g_minstagib"))
334 mutator = "^2Minstagib ^1";
335 else if(cvar("g_instagib"))
336 mutator = "^2Instagib ^1";
337 else if(cvar("g_rocketarena"))
338 mutator = "^2Rocketarena ^1";
339 else if(cvar("g_nixnex"))
340 mutator = "^2No Items Nexuiz ^1";
342 if(cvar("g_midair")) {
343 // to protect against unheedingly made changes
345 modifications = strcat(modifications, ", ");
347 modifications = "midair";
349 if(cvar("g_vampire")) {
351 modifications = strcat(modifications, ", ");
353 modifications = strcat(modifications, "vampire");
355 if(cvar("g_laserguided_missile")) {
357 modifications = strcat(modifications, ", ");
359 modifications = strcat(modifications, "laser-guided-missiles");
362 local string versionmessage;
363 versionmessage = GetClientVersionMessage(self.version);
365 s = strcat(s, newlines, "This is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
366 s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
368 if(modifications != "")
369 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
371 if((self.classname == "observer" || self.classname == "spectator") && self.version == cvar("gameversion")) {
373 s = strcat(s,"^7\n\n\npress jump to play\npress attack to spectate other players\n\n");
374 else if(player_count < 2 && arena_roundbased)
376 s = strcat(s, "\n\n\n^1waiting for second player to start match^7\n\n");
380 s = strcat(s, "\n\n\n");
382 s = strcat(s, "^7current champion is: ", champion.netname, "\n\n");
383 s = strcat(s,"^7press attack to spectate other players\n\n");
390 if (cvar("g_grappling_hook"))
391 s = strcat(s, "\n\n^8grappling hook is enabled, press 'e' to use it\n");
393 if (cvar_string("_mutatormsg") != "") {
394 s = strcat(s, "\n\n^8special gameplay tips: ^7", cvar_string("_mutatormsg"));
397 if (cvar_string("_motd") != "") {
398 s = strcat(s, "\n\n^8MOTD: ^7", cvar_string("_motd"));
402 if(cvar("fraglimit"))
405 if(cvar("timelimit"))
408 s = strcat(s, "\n^8frag limit: ^7", cvar_string("fraglimit"), padding);
410 if(cvar("timelimit"))
411 s = strcat(s, "\n^8time limit: ^7", cvar_string("timelimit"), " minutes");
422 void SetPlayerColors(entity pl, float _color)
426 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
428 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
429 pl.clientcolors = 16*cl + cl;*/
432 pants = _color & 0x0F;
433 shirt = _color & 0xF0;
437 setcolor(pl, 16*pants + pants);
439 setcolor(pl, shirt + pants);
443 void SetPlayerTeam(entity pl, float t, float s, float noprint)
448 _color = COLOR_TEAM4 - 1;
450 _color = COLOR_TEAM3 - 1;
452 _color = COLOR_TEAM2 - 1;
454 _color = COLOR_TEAM1 - 1;
456 SetPlayerColors(pl,_color);
458 if(!noprint && t != s)
460 //bprint(strcat(pl.netname, " has changed to ", TeamNoName(t), "\n"));
461 bprint(strcat(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n"));
473 // set c1...c4 to show what teams are allowed
474 void CheckAllowedTeams ()
483 c1 = c2 = c3 = c4 = -1;
484 cb1 = cb2 = cb3 = cb4 = 0;
487 teament_name = "dom_team";
489 teament_name = "ctf_team";
491 teament_name = "tdm_team";
494 // cover anything else by treating it like tdm with no teams spawned
495 dm = cvar("g_tdm_teams");
497 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
501 c1 = c2 = c3 = c4 = 0;
514 // first find out what teams are allowed
515 head = find(world, classname, teament_name);
518 if(!(g_domination && head.netname == ""))
520 if(head.team == COLOR_TEAM1)
524 if(head.team == COLOR_TEAM2)
528 if(head.team == COLOR_TEAM3)
532 if(head.team == COLOR_TEAM4)
537 head = find(head, classname, teament_name);
541 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
542 // teams that are allowed will now have their player counts stored in c1...c4
543 void GetTeamCounts(entity ignore)
546 // now count how many players are on each team already
548 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
549 // also remember the lowest-scoring player
551 head = find(world, classname, "player");
554 if(head != ignore)// && head.netname != "")
556 if(head.team == COLOR_TEAM1)
564 if(head.team == COLOR_TEAM2)
572 if(head.team == COLOR_TEAM3)
580 if(head.team == COLOR_TEAM4)
589 head = find(head, classname, "player");
593 // returns # of smallest team (1, 2, 3, 4)
594 // NOTE: Assumes CheckAllowedTeams has already been called!
595 float FindSmallestTeam(entity pl, float ignore_pl)
597 float totalteams, smallestteam, smallestteam_count, smallestteam_score, balance_type;
600 // find out what teams are available
601 //CheckAllowedTeams();
603 // make sure there are at least 2 teams to join
605 totalteams = totalteams + 1;
607 totalteams = totalteams + 1;
609 totalteams = totalteams + 1;
611 totalteams = totalteams + 1;
616 error("Too few teams available for domination\n");
618 error("Too few teams available for ctf\n");
620 error("Too few teams available for team deathmatch\n");
624 // count how many players are in each team
628 GetTeamCounts(world);
630 // c1...c4 now have counts of each team
631 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
634 smallestteam_count = 999999999;
635 smallestteam_score = 999999999;
637 // 2 gives priority to what team you're already on, 1 goes in order
638 // 2 doesn't seem to work though...
641 if(balance_type == 1)
643 if(c1 >= 0 && (c1 < smallestteam_count || (c1 <= smallestteam_count && team1_score < smallestteam_score)))
646 smallestteam_count = c1;
647 smallestteam_score = team1_score;
649 if(c2 >= 0 && (c2 < smallestteam_count || (c2 <= smallestteam_count && team2_score < smallestteam_score)))
652 smallestteam_count = c2;
653 smallestteam_score = team2_score;
655 if(c3 >= 0 && (c3 < smallestteam_count || (c3 <= smallestteam_count && team3_score < smallestteam_score)))
658 smallestteam_count = c3;
659 smallestteam_score = team3_score;
661 if(c4 >= 0 && (c4 < smallestteam_count || (c4 <= smallestteam_count && team4_score < smallestteam_score)))
664 smallestteam_count = c4;
665 smallestteam_score = team4_score;
670 if(c1 >= 0 && (c1 < smallestteam_count ||
671 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
674 smallestteam_count = c1;
676 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
677 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
680 smallestteam_count = c2;
682 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
683 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
686 smallestteam_count = c3;
688 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
689 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
692 smallestteam_count = c4;
699 float JoinBestTeam(entity pl, float only_return_best)
701 float smallest, selectedteam;
703 // don't join a team if we're not playing a team game
704 if(!cvar("teamplay") && !g_domination && !g_ctf)
707 // find out what teams are available
710 if(cvar("g_domination"))
712 if(cvar("g_domination_default_teams") < 3)
714 if(cvar("g_domination_default_teams") < 4)
718 // if we don't care what team he ends up on, put him on whatever team he entered as.
719 // if he's not on a valid team, then let other code put him on the smallest team
720 if(!cvar("g_campaign") && !cvar("g_balance_teams") && !cvar("g_balance_teams_force"))
722 if( c1 >= 0 && pl.team == COLOR_TEAM1)
723 selectedteam = pl.team;
724 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
725 selectedteam = pl.team;
726 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
727 selectedteam = pl.team;
728 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
729 selectedteam = pl.team;
734 if(!only_return_best)
736 SetPlayerColors(pl, selectedteam - 1);
741 // otherwise end up on the smallest team (handled below)
744 smallest = FindSmallestTeam(pl, TRUE);
747 if(!only_return_best)
749 TeamchangeFrags(self);
752 SetPlayerColors(pl, COLOR_TEAM1 - 1);
754 else if(smallest == 2)
756 SetPlayerColors(pl, COLOR_TEAM2 - 1);
758 else if(smallest == 3)
760 SetPlayerColors(pl, COLOR_TEAM3 - 1);
762 else if(smallest == 4)
764 SetPlayerColors(pl, COLOR_TEAM4 - 1);
768 error("smallest team: invalid team\n");
771 if(pl.deadflag == DEAD_NO)
772 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
779 void SV_ChangeTeam(float _color)
781 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
783 // in normal deathmatch we can just apply the color and we're done
784 if(!cvar("teamplay")) {
785 SetPlayerColors(self, _color);
789 scolor = self.clientcolors & 0x0F;
790 dcolor = _color & 0x0F;
792 if(scolor == COLOR_TEAM1 - 1)
794 else if(scolor == COLOR_TEAM2 - 1)
796 else if(scolor == COLOR_TEAM3 - 1)
798 else if(scolor == COLOR_TEAM4 - 1)
800 if(dcolor == COLOR_TEAM1 - 1)
802 else if(dcolor == COLOR_TEAM2 - 1)
804 else if(dcolor == COLOR_TEAM3 - 1)
806 else if(dcolor == COLOR_TEAM4 - 1)
809 // remap invalid teams in dom & ctf
811 if(cvar("g_ctf") && dteam == 3)
813 else if(cvar("g_ctf") && dteam == 4)
815 else if((cvar("g_domination") && cvar("g_domination_default_teams") < 3) || (cvar("g_tdm") && cvar("g_tdm_teams") < 3))
822 else if((cvar("g_domination") && cvar("g_domination_default_teams") < 4) || (cvar("g_tdm") && cvar("g_tdm_teams") < 4))
830 if(dteam == 3 && c3 < 0)
832 if(dteam == 4 && c4 < 0)
835 // not changing teams
838 //bprint("same team change\n");
839 SetPlayerTeam(self, dteam, steam, TRUE);
845 if(cvar("g_campaign") || cvar("g_changeteam_banned"))
847 sprint(self, "Team changes not allowed\n");
848 return; // changing teams is not allowed
851 if(!cvar("g_campaign") && cvar("g_balance_teams_prevent_imbalance"))
853 // only allow changing to a smaller or equal size team
855 // find out what teams are available
857 // count how many players on each team
858 GetTeamCounts(world);
861 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
866 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
871 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
876 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
883 sprint(self, "Cannot change to an invalid team\n");
889 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
891 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
893 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
895 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
898 if(scount) // started at a valid, nonempty team
900 // check if we're trying to change to a larger team that doens't have bots to swap with
901 if(dcount >= scount && dbotcount <= 0)
903 sprint(self, "Cannot change to a larger team\n");
904 return; // can't change to a larger team
910 // bprint(strcat("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n"));
912 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
914 // reduce frags during a team change
915 TeamchangeFrags(self);
918 SetPlayerTeam(self, dteam, steam, FALSE);
920 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
922 // kill player when changing teams
923 if(self.deadflag == DEAD_NO)
924 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
928 void ShufflePlayerOutOfTeam (float source_team)
930 float smallestteam, smallestteam_count, steam;
931 float lowest_bot_score, lowest_player_score;
932 entity head, lowest_bot, lowest_player, selected;
936 smallestteam_count = 999999999;
938 if(c1 >= 0 && c1 < smallestteam_count)
941 smallestteam_count = c1;
943 if(c2 >= 0 && c2 < smallestteam_count)
946 smallestteam_count = c2;
948 if(c3 >= 0 && c3 < smallestteam_count)
951 smallestteam_count = c3;
953 if(c4 >= 0 && c4 < smallestteam_count)
956 smallestteam_count = c4;
961 bprint("warning: no smallest team\n");
967 else if(source_team == 2)
969 else if(source_team == 3)
971 else if(source_team == 4)
975 lowest_bot_score = 999999999;
976 lowest_player = world;
977 lowest_player_score = 999999999;
979 // find the lowest-scoring player & bot of that team
980 head = find(world, classname, "player");
983 if(head.team == steam)
987 if(head.frags < lowest_bot_score)
990 lowest_bot_score = head.frags;
995 if(head.frags < lowest_player_score)
997 lowest_player = head;
998 lowest_player_score = head.frags;
1002 head = find(head, classname, "player");
1005 // prefers to move a bot...
1006 if(lowest_bot != world)
1007 selected = lowest_bot;
1008 // but it will move a player if it has to
1010 selected = lowest_player;
1011 // don't do anything if it couldn't find anyone
1014 bprint("warning: couldn't find a player to move from team\n");
1018 // smallest team gains a member
1019 if(smallestteam == 1)
1023 else if(smallestteam == 2)
1027 else if(smallestteam == 3)
1031 else if(smallestteam == 4)
1037 bprint("warning: destination team invalid\n");
1040 // source team loses a member
1041 if(source_team == 1)
1045 else if(source_team == 2)
1049 else if(source_team == 3)
1053 else if(source_team == 4)
1059 bprint("warning: source team invalid\n");
1063 // move the player to the new team
1064 TeamchangeFrags(selected);
1065 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1067 if(selected.deadflag == DEAD_NO)
1068 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1069 m = "You have been moved into a different team to improve team balance\nYou are now on: ";
1070 if (selected.team == 5)
1071 m = strcat(m, "^1Red Team");
1072 else if (selected.team == 14)
1073 m = strcat(m, "^4Blue Team");
1074 else if (selected.team == 10)
1075 m = strcat(m, "^6Pink Team");
1076 else if (selected.team == 13)
1077 m = strcat(m, "^3Yellow Team");
1078 centerprint(selected, m);
1081 // part of g_balance_teams_force
1082 // occasionally perform an audit of the teams to make
1083 // sure they're more or less balanced in player count.
1086 float numplayers, numteams, average;
1087 if(cvar("g_campaign"))
1089 if(!cvar("g_balance_teams_force"))
1091 if(!cvar("teamplay"))
1094 if(audit_teams_time > time)
1097 audit_teams_time = time + 4 + random();
1099 // bprint("Auditing teams\n");
1101 CheckAllowedTeams();
1102 GetTeamCounts(world);
1105 numteams = numplayers = 0;
1108 numteams = numteams + 1;
1109 numplayers = numplayers + c1;
1113 numteams = numteams + 1;
1114 numplayers = numplayers + c2;
1118 numteams = numteams + 1;
1119 numplayers = numplayers + c3;
1123 numteams = numteams + 1;
1124 numplayers = numplayers + c4;
1128 return; // no players to move around
1130 return; // don't bother shuffling if for some reason there aren't any teams
1132 average = ceil(numplayers / numteams);
1135 return; // that's weird...
1137 if(c1 && c1 > average)
1139 bprint("Rebalancing Teams\n");
1140 //bprint("Shuffle from team 1\n");
1141 ShufflePlayerOutOfTeam(1);
1143 if(c2 && c2 > average)
1145 bprint("Rebalancing Teams\n");
1146 //bprint("Shuffle from team 2\n");
1147 ShufflePlayerOutOfTeam(2);
1149 if(c3 && c3 > average)
1151 bprint("Rebalancing Teams\n");
1152 //bprint("Shuffle from team 3\n");
1153 ShufflePlayerOutOfTeam(3);
1155 if(c4 && c4 > average)
1157 bprint("Rebalancing Teams\n");
1158 //bprint("Shuffle from team 4\n");
1159 ShufflePlayerOutOfTeam(4);
1162 // if teams are still unbalanced, balance them further in the next audit,
1163 // which will happen sooner (keep doing rapid audits until things are in order)
1164 audit_teams_time = time + 0.7 + random()*0.3;
1169 /*void(entity e, float first) UpdateTeamScore =
1174 WriteByte (MSG_ALL, SVC_UPDATENAME);
1175 WriteByte (MSG_ALL, clientno);
1176 WriteString (MSG_ALL, e.netname);
1178 WriteByte (MSG_ALL, SVC_UPDATECOLORS);
1179 WriteByte (MSG_ALL, clientno);
1180 WriteByte (MSG_ALL, e.b_shirt * 16 + who.b_pants);
1183 WriteByte (MSG_ALL, SVC_UPDATEFRAGS);
1184 WriteByte (MSG_ALL, clientno);
1185 WriteShort (MSG_ALL, e.frags + 10000);
1191 // code from here on is just to support maps that don't have team entities
1192 void tdm_spawnteam (string teamname, float teamcolor)
1196 e.classname = "tdm_team";
1197 e.netname = teamname;
1202 // spawn some default teams if the map is not set up for tdm
1203 void() tdm_spawnteams =
1207 numteams = cvar("g_tdm_teams");
1209 tdm_spawnteam("Red", 4);
1210 tdm_spawnteam("Blue", 13);
1213 void() tdm_delayedinit =
1215 self.think = SUB_Remove;
1216 self.nextthink = time;
1217 // if no teams are found, spawn defaults
1218 if (find(world, classname, "tdm_team") == world)
1226 e.think = tdm_delayedinit;
1227 e.nextthink = time + 0.1;