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 g_domination, g_ctf, g_tdm, g_keyhunt, g_onslaught;
19 float audit_teams_time;
21 float() IsTeamBalanceForced = {
22 if(intermission_running)
23 return 0; // no rebalancing whatsoever please
26 if(cvar("g_campaign"))
28 if(!cvar("g_balance_teams_force"))
33 void TeamchangeFrags(entity e)
35 if(e.classname == "player")
37 // reduce frags during a team change
38 DistributeFragsAmongTeam(e, (e.clientcolors & 0x0F) + 1, 1 - cvar("g_changeteam_fragtransfer") / 100);
42 string TeamName(float t)
44 // fixme: Search for team entities and get their .netname's!
53 return "Neutral Team";
55 string ColoredTeamName(float t)
57 // fixme: Search for team entities and get their .netname's!
59 return "^1Red Team^7";
61 return "^4Blue Team^7";
63 return "^3Yellow Team^7";
65 return "^6Pink Team^7";
66 return "Neutral Team";
68 string TeamNoName(float t)
70 // fixme: Search for team entities and get their .netname's!
79 return "Neutral Team";
84 void runematch_init();
87 void(entity pl) LogTeamchange =
90 if(!cvar("sv_eventlog"))
94 str = strcat(":team:", ftos(pl.playerid), ":");
95 str = strcat(str, ftos(pl.team));
96 GameLogEcho(str, FALSE);
101 cvar_set("g_tdm", "0");
102 cvar_set("g_domination", "0");
103 cvar_set("g_ctf", "0");
104 cvar_set("g_runematch", "0");
105 cvar_set("g_lms", "0");
106 cvar_set("g_arena", "0");
107 cvar_set("g_keyhunt", "0");
108 cvar_set("g_assault", "0");
109 cvar_set("g_onslaught", "0");
110 cvar_set("teamplay", "0");
113 void ActivateTeamplay()
115 float teamplay_default;
116 teamplay_default = cvar("teamplay_default");
119 cvar_set("teamplay", ftos(teamplay_default));
121 cvar_set("teamplay", "3");
124 void InitGameplayMode()
126 float fraglimit_override, timelimit_override;
130 game = cvar ("gamecfg"); // load game options
132 // game cvars get reset before map changes
133 // then map's cfg sets them as desired
135 // FIXME: also set a message or game mode name to print to players when the join
137 // set both here, gamemode can override it later
138 timelimit_override = cvar("timelimit_override");
139 fraglimit_override = cvar("fraglimit_override");
141 if(game == GAME_DOMINATION || cvar("g_domination"))
144 game = GAME_DOMINATION;
145 cvar_set("g_domination", "1");
149 fraglimit_override = cvar("g_domination_point_limit");
151 gamemode_name = "Domination";
154 else if(game == GAME_CTF || cvar("g_ctf"))
158 cvar_set("g_ctf", "1");
162 fraglimit_override = cvar("g_ctf_capture_limit");
164 gamemode_name = "Capture the Flag";
167 else if((game == GAME_RUNEMATCH || cvar("g_runematch")) && !cvar("g_minstagib"))
169 game = GAME_RUNEMATCH;
170 cvar_set("g_runematch", "1");
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 gamemode_name = "Deathmatch";
270 /* else if(game == GAME_TEAM_DEATHMATCH)
272 if(!cvar("deathmatch"))
273 cvar_set("deathmatch", "1");
275 //if(!cvar("teamplay"))
276 // cvar_set("teamplay", "3");
279 fraglimit_override = cvar("fraglimit_override");
282 // enforce the server's universal frag/time limits
283 if(!cvar("g_campaign"))
285 if(fraglimit_override >= 0)
286 cvar_set("fraglimit", ftos(fraglimit_override));
287 if(timelimit_override >= 0)
288 cvar_set("timelimit", ftos(timelimit_override));
291 if (game == GAME_DOMINATION)//cvar("g_domination"))
293 else if (game == GAME_CTF)//cvar("g_ctf"))
295 else if (game == GAME_RUNEMATCH)//cvar("g_runematch"))
297 else if (game == GAME_TEAM_DEATHMATCH)//cvar("g_runematch"))
299 else if (game == GAME_KEYHUNT)//cvar("g_keyhunt"))
302 // those mutators rule each other out
303 if(cvar("g_minstagib"))
305 cvar_set("g_instagib", "0");
306 cvar_set("g_rocketarena", "0");
308 if(cvar("g_instagib"))
310 cvar_set("g_minstagib", "0");
311 cvar_set("g_rocketarena", "0");
313 if(cvar("g_rocketarena"))
315 cvar_set("g_instagib", "0");
316 cvar_set("g_minstagib", "0");
319 registercvar("_motd", "");
320 registercvar("_mutatormsg", "");
321 cvar_set("_motd", wordwrap(cvar_string("sv_motd"), 50));
322 cvar_set("_mutatormsg", wordwrap(cvar_string("g_mutatormsg"), 50));
324 g_domination = cvar("g_domination");
325 g_ctf = cvar("g_ctf");
326 g_tdm = cvar("g_tdm");
327 g_keyhunt = cvar("g_keyhunt");
328 g_onslaught = cvar("g_onslaught");
331 string GetClientVersionMessage(float v) {
332 local string versionmsg;
334 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";
335 // either that or someone wants to be funny
336 } else if (v != cvar("gameversion")) {
337 if(v < cvar("gameversion")) {
338 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
340 versionmsg = "^3This server is using an outdated Nexuiz version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
343 versionmsg = "^2client version and server version are compatible.^8";
345 return strzone(versionmsg);
350 void PrintWelcomeMessage(entity pl)
352 string s, mutator, modifications, padding;
354 /*if(self.welcomemessage_time > time)
356 self.welcomemessage_time = time + 0.8; */
358 if(self.cvar_scr_centertime == 0) return;
359 if(self.welcomemessage_time > time) return;
360 self.welcomemessage_time = time + self.cvar_scr_centertime * 0.6;
362 if(cvar("g_campaign"))
364 centerprint(pl, campaign_message);
368 if(self.classname == "observer")
370 if(cvar("g_lms") && self.frags <= 0 && self.frags > -666)
371 return centerprint(self, strcat(NEWLINES, "^1You have no more lives left\nwait for next round\n\n\n^7press attack to spectate other players"));
372 else if(cvar("g_lms") && self.frags == -666)
373 return centerprint(self, strcat(NEWLINES, "^1Match has already begun\nwait for next round\n\n\n^7press attack to spectate other players"));
375 else if(self.classname == "spectator")
377 if ((cvar("g_lms") && self.frags < 1) || cvar("g_arena"))
378 return centerprint(self, strcat(NEWLINES, "spectating ", self.enemy.netname, "\n\n\n^7press attack for next player\npress attack2 for free fly mode"));
380 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"));
384 if(cvar("g_minstagib"))
385 mutator = "^2Minstagib ^1";
386 else if(cvar("g_instagib"))
387 mutator = "^2Instagib ^1";
388 else if(cvar("g_rocketarena"))
389 mutator = "^2Rocketarena ^1";
390 else if(cvar("g_nixnex"))
391 mutator = "^2No Items Nexuiz ^1";
393 if(cvar("g_midair")) {
394 // to protect against unheedingly made changes
396 modifications = strcat(modifications, ", ");
398 modifications = "midair";
400 if(cvar("g_vampire")) {
402 modifications = strcat(modifications, ", ");
404 modifications = strcat(modifications, "vampire");
406 if(cvar("g_laserguided_missile")) {
408 modifications = strcat(modifications, ", ");
410 modifications = strcat(modifications, "laser-guided-missiles");
413 local string versionmessage;
414 versionmessage = GetClientVersionMessage(self.version);
416 s = strcat(s, NEWLINES, "This is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
417 s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
419 if(modifications != "")
420 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
422 if((self.classname == "observer" || self.classname == "spectator") && self.version == cvar("gameversion")) {
424 s = strcat(s,"^7\n\n\npress jump to play\npress attack to spectate other players\n\n");
425 else if(player_count < 2 && arena_roundbased)
427 s = strcat(s, "\n\n\n^1waiting for second player to start match^7\n\n");
431 s = strcat(s, "\n\n\n");
433 s = strcat(s, "^7current champion is: ", champion.netname, "\n\n");
434 s = strcat(s,"^7press attack to spectate other players\n\n");
441 if (cvar("g_grappling_hook"))
442 s = strcat(s, "\n\n^8grappling hook is enabled, press 'e' to use it\n");
444 if (cvar_string("_mutatormsg") != "") {
445 s = strcat(s, "\n\n^8special gameplay tips: ^7", cvar_string("_mutatormsg"));
448 if (cvar_string("_motd") != "") {
449 s = strcat(s, "\n\n^8MOTD: ^7", cvar_string("_motd"));
453 if(cvar("fraglimit"))
456 if(cvar("timelimit"))
459 s = strcat(s, "\n^8frag limit: ^7", cvar_string("fraglimit"), padding);
461 if(cvar("timelimit"))
462 s = strcat(s, "\n^8time limit: ^7", cvar_string("timelimit"), " minutes");
473 void SetPlayerColors(entity pl, float _color)
477 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
479 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
480 pl.clientcolors = 16*cl + cl;*/
483 pants = _color & 0x0F;
484 shirt = _color & 0xF0;
488 setcolor(pl, 16*pants + pants);
490 setcolor(pl, shirt + pants);
494 void SetPlayerTeam(entity pl, float t, float s, float noprint)
499 _color = COLOR_TEAM4 - 1;
501 _color = COLOR_TEAM3 - 1;
503 _color = COLOR_TEAM2 - 1;
505 _color = COLOR_TEAM1 - 1;
507 SetPlayerColors(pl,_color);
509 if(!noprint && t != s)
511 //bprint(pl.netname, " has changed to ", TeamNoName(t), "\n");
512 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
524 // set c1...c4 to show what teams are allowed
525 void CheckAllowedTeams (entity for_whom)
534 c1 = c2 = c3 = c4 = -1;
535 cb1 = cb2 = cb3 = cb4 = 0;
537 // onslaught is special
540 head = findchain(classname, "onslaught_generator");
543 if (head.team == COLOR_TEAM1) c1 = 0;
544 if (head.team == COLOR_TEAM2) c2 = 0;
545 if (head.team == COLOR_TEAM3) c3 = 0;
546 if (head.team == COLOR_TEAM4) c4 = 0;
553 teament_name = "dom_team";
555 teament_name = "ctf_team";
557 teament_name = "tdm_team";
560 // cover anything else by treating it like tdm with no teams spawned
564 dm = cvar("g_tdm_teams");
566 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
570 c1 = c2 = c3 = c4 = 0;
583 // first find out what teams are allowed
584 head = find(world, classname, teament_name);
587 if(!(g_domination && head.netname == ""))
589 if(head.team == COLOR_TEAM1)
593 if(head.team == COLOR_TEAM2)
597 if(head.team == COLOR_TEAM3)
601 if(head.team == COLOR_TEAM4)
606 head = find(head, classname, teament_name);
609 if(cvar("bot_vs_human") > 0)
612 if(clienttype(for_whom) == CLIENTTYPE_BOT)
617 else if(cvar("bot_vs_human") < 0)
620 if(clienttype(for_whom) == CLIENTTYPE_BOT)
627 float PlayerValue(entity p)
629 if(IsTeamBalanceForced() == 1)
634 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
635 // teams that are allowed will now have their player counts stored in c1...c4
636 void GetTeamCounts(entity ignore)
640 // now count how many players are on each team already
642 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
643 // also remember the lowest-scoring player
645 FOR_EACH_PLAYER(head)
647 if(head != ignore)// && head.netname != "")
649 value = PlayerValue(head);
650 if(clienttype(head) == CLIENTTYPE_BOT)
654 if(head.team == COLOR_TEAM1)
662 if(head.team == COLOR_TEAM2)
670 if(head.team == COLOR_TEAM3)
678 if(head.team == COLOR_TEAM4)
690 // returns # of smallest team (1, 2, 3, 4)
691 // NOTE: Assumes CheckAllowedTeams has already been called!
692 float FindSmallestTeam(entity pl, float ignore_pl)
694 float totalteams, smallestteam, smallestteam_count, smallestteam_score, balance_type;
697 // find out what teams are available
698 //CheckAllowedTeams();
700 // make sure there are at least 2 teams to join
702 totalteams = totalteams + 1;
704 totalteams = totalteams + 1;
706 totalteams = totalteams + 1;
708 totalteams = totalteams + 1;
710 if(cvar("bot_vs_human"))
716 error("Too few teams available for domination\n");
718 error("Too few teams available for ctf\n");
720 error("Too few teams available for key hunt\n");
722 error("Too few teams available for team deathmatch\n");
726 // count how many players are in each team
730 GetTeamCounts(world);
732 // c1...c4 now have counts of each team
733 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
736 smallestteam_count = 999999999;
737 smallestteam_score = 999999999;
739 // 2 gives priority to what team you're already on, 1 goes in order
740 // 2 doesn't seem to work though...
744 //if(pl.classname != "player")
745 if(clienttype(pl) != CLIENTTYPE_BOT)
747 c1 -= cb1 * 255.0/256;
748 c2 -= cb2 * 255.0/256;
749 c3 -= cb3 * 255.0/256;
750 c4 -= cb4 * 255.0/256;
753 if(balance_type == 1)
755 if(c1 >= 0 && (c1 < smallestteam_count || (c1 <= smallestteam_count && team1_score < smallestteam_score)))
758 smallestteam_count = c1;
759 smallestteam_score = team1_score;
761 if(c2 >= 0 && (c2 < smallestteam_count || (c2 <= smallestteam_count && team2_score < smallestteam_score)))
764 smallestteam_count = c2;
765 smallestteam_score = team2_score;
767 if(c3 >= 0 && (c3 < smallestteam_count || (c3 <= smallestteam_count && team3_score < smallestteam_score)))
770 smallestteam_count = c3;
771 smallestteam_score = team3_score;
773 if(c4 >= 0 && (c4 < smallestteam_count || (c4 <= smallestteam_count && team4_score < smallestteam_score)))
776 smallestteam_count = c4;
777 smallestteam_score = team4_score;
782 if(c1 >= 0 && (c1 < smallestteam_count ||
783 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
786 smallestteam_count = c1;
788 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
789 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
792 smallestteam_count = c2;
794 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
795 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
798 smallestteam_count = c3;
800 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
801 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
804 smallestteam_count = c4;
811 float JoinBestTeam(entity pl, float only_return_best)
813 float smallest, selectedteam;
815 // don't join a team if we're not playing a team game
816 if(!cvar("teamplay") && !g_domination && !g_ctf && !g_keyhunt)
819 // find out what teams are available
820 CheckAllowedTeams(pl);
822 if(cvar("g_domination"))
824 if(cvar("g_domination_default_teams") < 3)
826 if(cvar("g_domination_default_teams") < 4)
830 // if we don't care what team he ends up on, put him on whatever team he entered as.
831 // if he's not on a valid team, then let other code put him on the smallest team
832 if(!cvar("g_campaign") && !cvar("g_balance_teams") && !cvar("g_balance_teams_force"))
834 if( c1 >= 0 && pl.team == COLOR_TEAM1)
835 selectedteam = pl.team;
836 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
837 selectedteam = pl.team;
838 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
839 selectedteam = pl.team;
840 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
841 selectedteam = pl.team;
846 if(!only_return_best)
848 SetPlayerColors(pl, selectedteam - 1);
853 // otherwise end up on the smallest team (handled below)
856 smallest = FindSmallestTeam(pl, TRUE);
859 if(!only_return_best)
861 TeamchangeFrags(self);
864 SetPlayerColors(pl, COLOR_TEAM1 - 1);
866 else if(smallest == 2)
868 SetPlayerColors(pl, COLOR_TEAM2 - 1);
870 else if(smallest == 3)
872 SetPlayerColors(pl, COLOR_TEAM3 - 1);
874 else if(smallest == 4)
876 SetPlayerColors(pl, COLOR_TEAM4 - 1);
880 error("smallest team: invalid team\n");
883 if(pl.deadflag == DEAD_NO)
884 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
891 void SV_ChangeTeam(float _color)
893 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
895 // in normal deathmatch we can just apply the color and we're done
896 if(!cvar("teamplay")) {
897 SetPlayerColors(self, _color);
901 scolor = self.clientcolors & 0x0F;
902 dcolor = _color & 0x0F;
904 if(scolor == COLOR_TEAM1 - 1)
906 else if(scolor == COLOR_TEAM2 - 1)
908 else if(scolor == COLOR_TEAM3 - 1)
910 else if(scolor == COLOR_TEAM4 - 1)
912 if(dcolor == COLOR_TEAM1 - 1)
914 else if(dcolor == COLOR_TEAM2 - 1)
916 else if(dcolor == COLOR_TEAM3 - 1)
918 else if(dcolor == COLOR_TEAM4 - 1)
921 // remap invalid teams in dom & ctf
923 if(cvar("g_ctf") && dteam == 3)
925 else if(cvar("g_ctf") && dteam == 4)
927 else if((cvar("g_domination") && cvar("g_domination_default_teams") < 3) || (cvar("g_tdm") && cvar("g_tdm_teams") < 3))
934 else if((cvar("g_domination") && cvar("g_domination_default_teams") < 4) || (cvar("g_tdm") && cvar("g_tdm_teams") < 4))
941 CheckAllowedTeams(self);
943 if(dteam == 1 && c1 < 0) dteam = 4;
944 if(dteam == 4 && c4 < 0) dteam = 3;
945 if(dteam == 3 && c3 < 0) dteam = 2;
946 if(dteam == 2 && c2 < 0) dteam = 1;
948 // not changing teams
951 //bprint("same team change\n");
952 SetPlayerTeam(self, dteam, steam, TRUE);
958 if(cvar("g_campaign") || cvar("g_changeteam_banned"))
960 sprint(self, "Team changes not allowed\n");
961 return; // changing teams is not allowed
964 if(!cvar("g_campaign") && cvar("g_balance_teams_prevent_imbalance"))
966 // only allow changing to a smaller or equal size team
968 // find out what teams are available
969 //CheckAllowedTeams();
970 // count how many players on each team
971 GetTeamCounts(world);
974 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
979 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
984 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
989 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
996 sprint(self, "Cannot change to an invalid team\n");
1001 // get starting team
1002 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
1004 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
1006 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
1008 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
1011 if(scount) // started at a valid, nonempty team
1013 // check if we're trying to change to a larger team that doens't have bots to swap with
1014 if(dcount >= scount && dbotcount <= 0)
1016 sprint(self, "Cannot change to a larger team\n");
1017 return; // can't change to a larger team
1023 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
1025 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1027 // reduce frags during a team change
1028 TeamchangeFrags(self);
1031 SetPlayerTeam(self, dteam, steam, FALSE);
1033 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1035 // kill player when changing teams
1036 if(self.deadflag == DEAD_NO)
1037 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
1041 void ShufflePlayerOutOfTeam (float source_team)
1043 float smallestteam, smallestteam_count, steam;
1044 float lowest_bot_score, lowest_player_score;
1045 entity head, lowest_bot, lowest_player, selected;
1048 smallestteam_count = 999999999;
1050 if(c1 >= 0 && c1 < smallestteam_count)
1053 smallestteam_count = c1;
1055 if(c2 >= 0 && c2 < smallestteam_count)
1058 smallestteam_count = c2;
1060 if(c3 >= 0 && c3 < smallestteam_count)
1063 smallestteam_count = c3;
1065 if(c4 >= 0 && c4 < smallestteam_count)
1068 smallestteam_count = c4;
1073 bprint("warning: no smallest team\n");
1077 if(source_team == 1)
1078 steam = COLOR_TEAM1;
1079 else if(source_team == 2)
1080 steam = COLOR_TEAM2;
1081 else if(source_team == 3)
1082 steam = COLOR_TEAM3;
1083 else if(source_team == 4)
1084 steam = COLOR_TEAM4;
1087 lowest_bot_score = 999999999;
1088 lowest_player = world;
1089 lowest_player_score = 999999999;
1091 // find the lowest-scoring player & bot of that team
1092 FOR_EACH_PLAYER(head)
1094 if(head.team == steam)
1098 if(head.frags < lowest_bot_score)
1101 lowest_bot_score = head.frags;
1106 if(head.frags < lowest_player_score)
1108 lowest_player = head;
1109 lowest_player_score = head.frags;
1115 // prefers to move a bot...
1116 if(lowest_bot != world)
1117 selected = lowest_bot;
1118 // but it will move a player if it has to
1120 selected = lowest_player;
1121 // don't do anything if it couldn't find anyone
1124 bprint("warning: couldn't find a player to move from team\n");
1128 // smallest team gains a member
1129 if(smallestteam == 1)
1133 else if(smallestteam == 2)
1137 else if(smallestteam == 3)
1141 else if(smallestteam == 4)
1147 bprint("warning: destination team invalid\n");
1150 // source team loses a member
1151 if(source_team == 1)
1155 else if(source_team == 2)
1159 else if(source_team == 3)
1163 else if(source_team == 4)
1169 bprint("warning: source team invalid\n");
1173 // move the player to the new team
1174 TeamchangeFrags(selected);
1175 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1177 if(selected.deadflag == DEAD_NO)
1178 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1179 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1182 float lastRebalanceInfo;
1183 void CauseRebalance(float source_team, float howmany_toomany)
1188 if(IsTeamBalanceForced() == 1)
1190 bprint("Rebalancing Teams\n");
1191 ShufflePlayerOutOfTeam(source_team);
1195 if(howmany_toomany < cvar("g_balance_teams_complain"))
1197 if(time < lastRebalanceInfo + 90)
1199 lastRebalanceInfo = time;
1200 if(source_team == 1)
1201 steam = COLOR_TEAM1;
1202 else if(source_team == 2)
1203 steam = COLOR_TEAM2;
1204 else if(source_team == 3)
1205 steam = COLOR_TEAM3;
1206 else if(source_team == 4)
1207 steam = COLOR_TEAM4;
1208 ServerConsoleEcho(strcat("Team ", ftos(source_team), " too large, complaining."), TRUE);
1209 FOR_EACH_REALPLAYER(head)
1211 if(head.team == steam)
1213 sprint(head, "\{1}\{13}^3SERVER NOTICE:^7 One of you please change teams!\n");
1214 centerprint_atprio(head, CENTERPRIO_REBALANCE, "^3SERVER NOTICE:\n\n^7Someone of you please change teams!");
1220 // part of g_balance_teams_force
1221 // occasionally perform an audit of the teams to make
1222 // sure they're more or less balanced in player count.
1225 float numplayers, numteams, smallest, toomany;
1227 balance = IsTeamBalanceForced();
1231 if(audit_teams_time > time)
1234 audit_teams_time = time + 4 + random();
1236 // bprint("Auditing teams\n");
1238 CheckAllowedTeams(world);
1239 GetTeamCounts(world);
1242 numteams = numplayers = smallest = 0;
1245 numteams = numteams + 1;
1246 numplayers = numplayers + c1;
1251 numteams = numteams + 1;
1252 numplayers = numplayers + c2;
1258 numteams = numteams + 1;
1259 numplayers = numplayers + c3;
1265 numteams = numteams + 1;
1266 numplayers = numplayers + c4;
1272 return; // no players to move around
1274 return; // don't bother shuffling if for some reason there aren't any teams
1276 toomany = smallest + 1;
1278 if(c1 && c1 > toomany)
1279 CauseRebalance(1, c1 - toomany);
1280 if(c2 && c2 > toomany)
1281 CauseRebalance(2, c2 - toomany);
1282 if(c3 && c3 > toomany)
1283 CauseRebalance(3, c3 - toomany);
1284 if(c4 && c4 > toomany)
1285 CauseRebalance(4, c4 - toomany);
1287 // if teams are still unbalanced, balance them further in the next audit,
1288 // which will happen sooner (keep doing rapid audits until things are in order)
1289 audit_teams_time = time + 0.7 + random()*0.3;
1294 /*void(entity e, float first) UpdateTeamScore =
1299 WriteByte (MSG_ALL, SVC_UPDATENAME);
1300 WriteByte (MSG_ALL, clientno);
1301 WriteString (MSG_ALL, e.netname);
1303 WriteByte (MSG_ALL, SVC_UPDATECOLORS);
1304 WriteByte (MSG_ALL, clientno);
1305 WriteByte (MSG_ALL, e.b_shirt * 16 + who.b_pants);
1308 WriteByte (MSG_ALL, SVC_UPDATEFRAGS);
1309 WriteByte (MSG_ALL, clientno);
1310 WriteShort (MSG_ALL, e.frags + 10000);
1316 // code from here on is just to support maps that don't have team entities
1317 void tdm_spawnteam (string teamname, float teamcolor)
1321 e.classname = "tdm_team";
1322 e.netname = teamname;
1327 // spawn some default teams if the map is not set up for tdm
1328 void() tdm_spawnteams =
1332 numteams = cvar("g_tdm_teams");
1334 tdm_spawnteam("Red", COLOR_TEAM1-1);
1335 tdm_spawnteam("Blue", COLOR_TEAM2-1);
1338 void() tdm_delayedinit =
1340 self.think = SUB_Remove;
1341 self.nextthink = time;
1342 // if no teams are found, spawn defaults
1343 if (find(world, classname, "tdm_team") == world)
1351 e.think = tdm_delayedinit;
1352 e.nextthink = time + 0.1;