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 // enforce the server's universal frag/time limits
284 if(!cvar("g_campaign"))
286 if(fraglimit_override >= 0)
287 cvar_set("fraglimit", ftos(fraglimit_override));
288 if(timelimit_override >= 0)
289 cvar_set("timelimit", ftos(timelimit_override));
292 if (game == GAME_DOMINATION)//cvar("g_domination"))
294 else if (game == GAME_CTF)//cvar("g_ctf"))
296 else if (game == GAME_RUNEMATCH)//cvar("g_runematch"))
298 else if (game == GAME_TEAM_DEATHMATCH)//cvar("g_runematch"))
300 else if (game == GAME_KEYHUNT)//cvar("g_keyhunt"))
303 // those mutators rule each other out
306 cvar_set("g_instagib", "0");
307 cvar_set("g_rocketarena", "0");
311 cvar_set("g_minstagib", "0");
312 cvar_set("g_rocketarena", "0");
316 cvar_set("g_instagib", "0");
317 cvar_set("g_minstagib", "0");
320 registercvar("_motd", "");
321 registercvar("_mutatormsg", "");
322 cvar_set("_motd", wordwrap(cvar_string("sv_motd"), 50));
323 cvar_set("_mutatormsg", wordwrap(cvar_string("g_mutatormsg"), 50));
325 g_domination = cvar("g_domination");
326 g_ctf = cvar("g_ctf");
327 g_lms = cvar("g_lms");
328 g_tdm = cvar("g_tdm");
329 g_keyhunt = cvar("g_keyhunt");
330 g_onslaught = cvar("g_onslaught");
331 g_assault = cvar("g_assault");
332 g_arena = cvar("g_arena");
335 string GetClientVersionMessage(float v) {
336 local string versionmsg;
338 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";
339 // either that or someone wants to be funny
340 } else if (v != cvar("gameversion")) {
341 if(v < cvar("gameversion")) {
342 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
344 versionmsg = "^3This server is using an outdated Nexuiz version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
347 versionmsg = "^2client version and server version are compatible.^8";
349 return strzone(versionmsg);
354 void PrintWelcomeMessage(entity pl)
356 string s, mutator, modifications, padding;
358 /*if(self.welcomemessage_time > time)
360 self.welcomemessage_time = time + 0.8; */
362 if(self.cvar_scr_centertime == 0) return;
363 if(self.welcomemessage_time > time) return;
364 self.welcomemessage_time = time + self.cvar_scr_centertime * 0.6;
366 if(cvar("g_campaign"))
368 centerprint(pl, campaign_message);
372 if(self.classname == "observer")
374 if(g_lms && self.frags <= 0 && self.frags > -666)
375 return centerprint(self, strcat(NEWLINES, "^1You have no more lives left\nwait for next round\n\n\n^7press attack to spectate other players"));
376 else if(g_lms && self.frags == -666)
377 return centerprint(self, strcat(NEWLINES, "^1Match has already begun\nwait for next round\n\n\n^7press attack to spectate other players"));
379 else if(self.classname == "spectator")
381 if ((g_lms && self.frags < 1) || g_arena)
382 return centerprint(self, strcat(NEWLINES, "spectating ", self.enemy.netname, "\n\n\n^7press attack for next player\npress attack2 for free fly mode"));
384 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"));
389 mutator = "^2Minstagib ^1";
391 mutator = "^2Instagib ^1";
392 else if(g_rocketarena)
393 mutator = "^2Rocketarena ^1";
395 mutator = "^2No Items Nexuiz ^1";
398 // to protect against unheedingly made changes
400 modifications = strcat(modifications, ", ");
402 modifications = "midair";
406 modifications = strcat(modifications, ", ");
408 modifications = strcat(modifications, "vampire");
410 if(g_laserguided_missile) {
412 modifications = strcat(modifications, ", ");
414 modifications = strcat(modifications, "laser-guided-missiles");
417 local string versionmessage;
418 versionmessage = GetClientVersionMessage(self.version);
420 s = strcat(s, NEWLINES, "This is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
421 s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
423 if(modifications != "")
424 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
426 if((self.classname == "observer" || self.classname == "spectator") && self.version == cvar("gameversion")) {
428 s = strcat(s,"^7\n\n\npress jump to play\npress attack to spectate other players\n\n");
429 else if(player_count < 2 && arena_roundbased)
431 s = strcat(s, "\n\n\n^1waiting for second player to start match^7\n\n");
435 s = strcat(s, "\n\n\n");
437 s = strcat(s, "^7current champion is: ", champion.netname, "\n\n");
438 s = strcat(s,"^7press attack to spectate other players\n\n");
445 if (g_grappling_hook)
446 s = strcat(s, "\n\n^8grappling hook is enabled, press 'e' to use it\n");
448 if (cvar_string("_mutatormsg") != "") {
449 s = strcat(s, "\n\n^8special gameplay tips: ^7", cvar_string("_mutatormsg"));
452 if (cvar_string("_motd") != "") {
453 s = strcat(s, "\n\n^8MOTD: ^7", cvar_string("_motd"));
457 if(cvar("fraglimit"))
460 if(cvar("timelimit"))
463 s = strcat(s, "\n^8frag limit: ^7", cvar_string("fraglimit"), padding);
465 if(cvar("timelimit"))
466 s = strcat(s, "\n^8time limit: ^7", cvar_string("timelimit"), " minutes");
477 void SetPlayerColors(entity pl, float _color)
481 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
483 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
484 pl.clientcolors = 16*cl + cl;*/
487 pants = _color & 0x0F;
488 shirt = _color & 0xF0;
492 setcolor(pl, 16*pants + pants);
494 setcolor(pl, shirt + pants);
498 void SetPlayerTeam(entity pl, float t, float s, float noprint)
503 _color = COLOR_TEAM4 - 1;
505 _color = COLOR_TEAM3 - 1;
507 _color = COLOR_TEAM2 - 1;
509 _color = COLOR_TEAM1 - 1;
511 SetPlayerColors(pl,_color);
513 if(!noprint && t != s)
515 //bprint(pl.netname, " has changed to ", TeamNoName(t), "\n");
516 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
528 // set c1...c4 to show what teams are allowed
529 void CheckAllowedTeams (entity for_whom)
538 c1 = c2 = c3 = c4 = -1;
539 cb1 = cb2 = cb3 = cb4 = 0;
541 // onslaught is special
544 head = findchain(classname, "onslaught_generator");
547 if (head.team == COLOR_TEAM1) c1 = 0;
548 if (head.team == COLOR_TEAM2) c2 = 0;
549 if (head.team == COLOR_TEAM3) c3 = 0;
550 if (head.team == COLOR_TEAM4) c4 = 0;
557 teament_name = "dom_team";
559 teament_name = "ctf_team";
561 teament_name = "tdm_team";
564 c1 = c2 = 0; // Assault always has 2 teams
569 // cover anything else by treating it like tdm with no teams spawned
573 dm = cvar("g_tdm_teams");
575 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
579 c1 = c2 = c3 = c4 = 0;
592 // first find out what teams are allowed
593 head = find(world, classname, teament_name);
596 if(!(g_domination && head.netname == ""))
598 if(head.team == COLOR_TEAM1)
602 if(head.team == COLOR_TEAM2)
606 if(head.team == COLOR_TEAM3)
610 if(head.team == COLOR_TEAM4)
615 head = find(head, classname, teament_name);
618 if(cvar("bot_vs_human") > 0)
621 if(clienttype(for_whom) == CLIENTTYPE_BOT)
626 else if(cvar("bot_vs_human") < 0)
629 if(clienttype(for_whom) == CLIENTTYPE_BOT)
636 float PlayerValue(entity p)
638 if(IsTeamBalanceForced() == 1)
643 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
644 // teams that are allowed will now have their player counts stored in c1...c4
645 void GetTeamCounts(entity ignore)
649 // now count how many players are on each team already
651 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
652 // also remember the lowest-scoring player
654 FOR_EACH_PLAYER(head)
656 if(head != ignore)// && head.netname != "")
658 value = PlayerValue(head);
659 if(clienttype(head) == CLIENTTYPE_BOT)
663 if(head.team == COLOR_TEAM1)
671 if(head.team == COLOR_TEAM2)
679 if(head.team == COLOR_TEAM3)
687 if(head.team == COLOR_TEAM4)
699 // returns # of smallest team (1, 2, 3, 4)
700 // NOTE: Assumes CheckAllowedTeams has already been called!
701 float FindSmallestTeam(entity pl, float ignore_pl)
703 float totalteams, smallestteam, smallestteam_count, smallestteam_score, balance_type;
706 // find out what teams are available
707 //CheckAllowedTeams();
709 // make sure there are at least 2 teams to join
711 totalteams = totalteams + 1;
713 totalteams = totalteams + 1;
715 totalteams = totalteams + 1;
717 totalteams = totalteams + 1;
719 if(cvar("bot_vs_human"))
725 error("Too few teams available for domination\n");
727 error("Too few teams available for ctf\n");
729 error("Too few teams available for key hunt\n");
731 error("Too few teams available for team deathmatch\n");
735 // count how many players are in each team
739 GetTeamCounts(world);
741 // c1...c4 now have counts of each team
742 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
745 smallestteam_count = 999999999;
746 smallestteam_score = 999999999;
748 // 2 gives priority to what team you're already on, 1 goes in order
749 // 2 doesn't seem to work though...
753 //if(pl.classname != "player")
754 if(clienttype(pl) != CLIENTTYPE_BOT)
756 c1 -= cb1 * 255.0/256;
757 c2 -= cb2 * 255.0/256;
758 c3 -= cb3 * 255.0/256;
759 c4 -= cb4 * 255.0/256;
762 if(balance_type == 1)
764 if(c1 >= 0 && (c1 < smallestteam_count || (c1 <= smallestteam_count && team1_score < smallestteam_score)))
767 smallestteam_count = c1;
768 smallestteam_score = team1_score;
770 if(c2 >= 0 && (c2 < smallestteam_count || (c2 <= smallestteam_count && team2_score < smallestteam_score)))
773 smallestteam_count = c2;
774 smallestteam_score = team2_score;
776 if(c3 >= 0 && (c3 < smallestteam_count || (c3 <= smallestteam_count && team3_score < smallestteam_score)))
779 smallestteam_count = c3;
780 smallestteam_score = team3_score;
782 if(c4 >= 0 && (c4 < smallestteam_count || (c4 <= smallestteam_count && team4_score < smallestteam_score)))
785 smallestteam_count = c4;
786 smallestteam_score = team4_score;
791 if(c1 >= 0 && (c1 < smallestteam_count ||
792 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
795 smallestteam_count = c1;
797 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
798 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
801 smallestteam_count = c2;
803 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
804 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
807 smallestteam_count = c3;
809 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
810 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
813 smallestteam_count = c4;
820 float JoinBestTeam(entity pl, float only_return_best)
822 float smallest, selectedteam;
824 // don't join a team if we're not playing a team game
825 if(!cvar("teamplay") && !g_domination && !g_ctf && !g_keyhunt)
828 // find out what teams are available
829 CheckAllowedTeams(pl);
833 if(cvar("g_domination_default_teams") < 3)
835 if(cvar("g_domination_default_teams") < 4)
839 // if we don't care what team he ends up on, put him on whatever team he entered as.
840 // if he's not on a valid team, then let other code put him on the smallest team
841 if(!cvar("g_campaign") && !cvar("g_balance_teams") && !cvar("g_balance_teams_force"))
843 if( c1 >= 0 && pl.team == COLOR_TEAM1)
844 selectedteam = pl.team;
845 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
846 selectedteam = pl.team;
847 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
848 selectedteam = pl.team;
849 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
850 selectedteam = pl.team;
855 if(!only_return_best)
857 SetPlayerColors(pl, selectedteam - 1);
862 // otherwise end up on the smallest team (handled below)
865 smallest = FindSmallestTeam(pl, TRUE);
868 if(!only_return_best)
870 TeamchangeFrags(self);
873 SetPlayerColors(pl, COLOR_TEAM1 - 1);
875 else if(smallest == 2)
877 SetPlayerColors(pl, COLOR_TEAM2 - 1);
879 else if(smallest == 3)
881 SetPlayerColors(pl, COLOR_TEAM3 - 1);
883 else if(smallest == 4)
885 SetPlayerColors(pl, COLOR_TEAM4 - 1);
889 error("smallest team: invalid team\n");
892 if(pl.deadflag == DEAD_NO)
893 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
900 void SV_ChangeTeam(float _color)
902 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
904 // in normal deathmatch we can just apply the color and we're done
905 if(!cvar("teamplay")) {
906 SetPlayerColors(self, _color);
910 scolor = self.clientcolors & 0x0F;
911 dcolor = _color & 0x0F;
913 if(scolor == COLOR_TEAM1 - 1)
915 else if(scolor == COLOR_TEAM2 - 1)
917 else if(scolor == COLOR_TEAM3 - 1)
919 else if(scolor == COLOR_TEAM4 - 1)
921 if(dcolor == COLOR_TEAM1 - 1)
923 else if(dcolor == COLOR_TEAM2 - 1)
925 else if(dcolor == COLOR_TEAM3 - 1)
927 else if(dcolor == COLOR_TEAM4 - 1)
930 CheckAllowedTeams(self);
932 if(dteam == 1 && c1 < 0) dteam = 4;
933 if(dteam == 4 && c4 < 0) dteam = 3;
934 if(dteam == 3 && c3 < 0) dteam = 2;
935 if(dteam == 2 && c2 < 0) dteam = 1;
937 // not changing teams
940 //bprint("same team change\n");
941 SetPlayerTeam(self, dteam, steam, TRUE);
947 if(cvar("g_campaign") || cvar("g_changeteam_banned"))
949 sprint(self, "Team changes not allowed\n");
950 return; // changing teams is not allowed
953 if(!cvar("g_campaign") && cvar("g_balance_teams_prevent_imbalance"))
955 // only allow changing to a smaller or equal size team
957 // find out what teams are available
958 //CheckAllowedTeams();
959 // count how many players on each team
960 GetTeamCounts(world);
963 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
968 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
973 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
978 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
985 sprint(self, "Cannot change to an invalid team\n");
991 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
993 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
995 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
997 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
1000 if(scount) // started at a valid, nonempty team
1002 // check if we're trying to change to a larger team that doens't have bots to swap with
1003 if(dcount >= scount && dbotcount <= 0)
1005 sprint(self, "Cannot change to a larger team\n");
1006 return; // can't change to a larger team
1012 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
1014 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1016 // reduce frags during a team change
1017 TeamchangeFrags(self);
1020 SetPlayerTeam(self, dteam, steam, FALSE);
1022 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1024 // kill player when changing teams
1025 if(self.deadflag == DEAD_NO)
1026 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
1030 void ShufflePlayerOutOfTeam (float source_team)
1032 float smallestteam, smallestteam_count, steam;
1033 float lowest_bot_score, lowest_player_score;
1034 entity head, lowest_bot, lowest_player, selected;
1037 smallestteam_count = 999999999;
1039 if(c1 >= 0 && c1 < smallestteam_count)
1042 smallestteam_count = c1;
1044 if(c2 >= 0 && c2 < smallestteam_count)
1047 smallestteam_count = c2;
1049 if(c3 >= 0 && c3 < smallestteam_count)
1052 smallestteam_count = c3;
1054 if(c4 >= 0 && c4 < smallestteam_count)
1057 smallestteam_count = c4;
1062 bprint("warning: no smallest team\n");
1066 if(source_team == 1)
1067 steam = COLOR_TEAM1;
1068 else if(source_team == 2)
1069 steam = COLOR_TEAM2;
1070 else if(source_team == 3)
1071 steam = COLOR_TEAM3;
1072 else if(source_team == 4)
1073 steam = COLOR_TEAM4;
1076 lowest_bot_score = 999999999;
1077 lowest_player = world;
1078 lowest_player_score = 999999999;
1080 // find the lowest-scoring player & bot of that team
1081 FOR_EACH_PLAYER(head)
1083 if(head.team == steam)
1087 if(head.frags < lowest_bot_score)
1090 lowest_bot_score = head.frags;
1095 if(head.frags < lowest_player_score)
1097 lowest_player = head;
1098 lowest_player_score = head.frags;
1104 // prefers to move a bot...
1105 if(lowest_bot != world)
1106 selected = lowest_bot;
1107 // but it will move a player if it has to
1109 selected = lowest_player;
1110 // don't do anything if it couldn't find anyone
1113 bprint("warning: couldn't find a player to move from team\n");
1117 // smallest team gains a member
1118 if(smallestteam == 1)
1122 else if(smallestteam == 2)
1126 else if(smallestteam == 3)
1130 else if(smallestteam == 4)
1136 bprint("warning: destination team invalid\n");
1139 // source team loses a member
1140 if(source_team == 1)
1144 else if(source_team == 2)
1148 else if(source_team == 3)
1152 else if(source_team == 4)
1158 bprint("warning: source team invalid\n");
1162 // move the player to the new team
1163 TeamchangeFrags(selected);
1164 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1166 if(selected.deadflag == DEAD_NO)
1167 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1168 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1171 float lastRebalanceInfo;
1172 void CauseRebalance(float source_team, float howmany_toomany)
1177 if(IsTeamBalanceForced() == 1)
1179 bprint("Rebalancing Teams\n");
1180 ShufflePlayerOutOfTeam(source_team);
1184 if(howmany_toomany < cvar("g_balance_teams_complain"))
1186 if(time < lastRebalanceInfo + 90)
1188 lastRebalanceInfo = time;
1189 if(source_team == 1)
1190 steam = COLOR_TEAM1;
1191 else if(source_team == 2)
1192 steam = COLOR_TEAM2;
1193 else if(source_team == 3)
1194 steam = COLOR_TEAM3;
1195 else if(source_team == 4)
1196 steam = COLOR_TEAM4;
1197 ServerConsoleEcho(strcat("Team ", ftos(source_team), " too large, complaining."), TRUE);
1198 FOR_EACH_REALPLAYER(head)
1200 if(head.team == steam)
1202 sprint(head, "\{1}\{13}^3SERVER NOTICE:^7 One of you please change teams!\n");
1203 centerprint_atprio(head, CENTERPRIO_REBALANCE, "^3SERVER NOTICE:\n\n^7Someone of you please change teams!");
1209 // part of g_balance_teams_force
1210 // occasionally perform an audit of the teams to make
1211 // sure they're more or less balanced in player count.
1214 float numplayers, numteams, smallest, toomany;
1216 balance = IsTeamBalanceForced();
1220 if(audit_teams_time > time)
1223 audit_teams_time = time + 4 + random();
1225 // bprint("Auditing teams\n");
1227 CheckAllowedTeams(world);
1228 GetTeamCounts(world);
1231 numteams = numplayers = smallest = 0;
1234 numteams = numteams + 1;
1235 numplayers = numplayers + c1;
1240 numteams = numteams + 1;
1241 numplayers = numplayers + c2;
1247 numteams = numteams + 1;
1248 numplayers = numplayers + c3;
1254 numteams = numteams + 1;
1255 numplayers = numplayers + c4;
1261 return; // no players to move around
1263 return; // don't bother shuffling if for some reason there aren't any teams
1265 toomany = smallest + 1;
1267 if(c1 && c1 > toomany)
1268 CauseRebalance(1, c1 - toomany);
1269 if(c2 && c2 > toomany)
1270 CauseRebalance(2, c2 - toomany);
1271 if(c3 && c3 > toomany)
1272 CauseRebalance(3, c3 - toomany);
1273 if(c4 && c4 > toomany)
1274 CauseRebalance(4, c4 - toomany);
1276 // if teams are still unbalanced, balance them further in the next audit,
1277 // which will happen sooner (keep doing rapid audits until things are in order)
1278 audit_teams_time = time + 0.7 + random()*0.3;
1283 /*void(entity e, float first) UpdateTeamScore =
1288 WriteByte (MSG_ALL, SVC_UPDATENAME);
1289 WriteByte (MSG_ALL, clientno);
1290 WriteString (MSG_ALL, e.netname);
1292 WriteByte (MSG_ALL, SVC_UPDATECOLORS);
1293 WriteByte (MSG_ALL, clientno);
1294 WriteByte (MSG_ALL, e.b_shirt * 16 + who.b_pants);
1297 WriteByte (MSG_ALL, SVC_UPDATEFRAGS);
1298 WriteByte (MSG_ALL, clientno);
1299 WriteShort (MSG_ALL, e.frags + 10000);
1305 // code from here on is just to support maps that don't have team entities
1306 void tdm_spawnteam (string teamname, float teamcolor)
1310 e.classname = "tdm_team";
1311 e.netname = teamname;
1316 // spawn some default teams if the map is not set up for tdm
1317 void() tdm_spawnteams =
1321 numteams = cvar("g_tdm_teams");
1323 tdm_spawnteam("Red", COLOR_TEAM1-1);
1324 tdm_spawnteam("Blue", COLOR_TEAM2-1);
1326 tdm_spawnteam("Yellow", COLOR_TEAM3-1);
1328 tdm_spawnteam("Pink", COLOR_TEAM4-1);
1331 void() tdm_delayedinit =
1333 self.think = SUB_Remove;
1334 self.nextthink = time;
1335 // if no teams are found, spawn defaults
1336 if (find(world, classname, "tdm_team") == world)
1344 e.think = tdm_delayedinit;
1345 e.nextthink = time + 0.1;