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 vector TeamColor(float teem)
45 return '1 0.0625 0.0625';
47 return '0.0625 0.0625 1';
57 string TeamName(float t)
59 return strcat(Team_ColorName(t), " Team");
61 string ColoredTeamName(float t)
63 return strcat(Team_ColorCode(t), Team_ColorName(t), " Team^7");
65 string TeamNoName(float t)
67 // fixme: Search for team entities and get their .netname's!
76 return "Neutral Team";
81 void runematch_init();
84 void(entity pl) LogTeamchange =
87 if(!cvar("sv_eventlog"))
91 str = strcat(":team:", ftos(pl.playerid), ":");
92 str = strcat(str, ftos(pl.team));
93 GameLogEcho(str, FALSE);
98 cvar_set("g_dm", "0");
99 cvar_set("g_tdm", "0");
100 cvar_set("g_domination", "0");
101 cvar_set("g_ctf", "0");
102 cvar_set("g_runematch", "0");
103 cvar_set("g_lms", "0");
104 cvar_set("g_arena", "0");
105 cvar_set("g_keyhunt", "0");
106 cvar_set("g_assault", "0");
107 cvar_set("g_onslaught", "0");
108 cvar_set("teamplay", "0");
111 void ActivateTeamplay()
113 float teamplay_default;
114 teamplay_default = cvar("teamplay_default");
117 cvar_set("teamplay", ftos(teamplay_default));
119 cvar_set("teamplay", "3");
122 void InitGameplayMode()
124 float fraglimit_override, timelimit_override;
128 game = cvar("gamecfg"); // load game options
130 // game cvars get reset before map changes
131 // then map's cfg sets them as desired
133 // FIXME: also set a message or game mode name to print to players when the join
135 // set both here, gamemode can override it later
136 timelimit_override = cvar("timelimit_override");
137 fraglimit_override = cvar("fraglimit_override");
139 if(game == GAME_DOMINATION || cvar("g_domination"))
142 game = GAME_DOMINATION;
143 cvar_set("g_domination", "1");
147 fraglimit_override = cvar("g_domination_point_limit");
149 gamemode_name = "Domination";
152 else if(game == GAME_CTF || cvar("g_ctf"))
156 cvar_set("g_ctf", "1");
160 fraglimit_override = cvar("g_ctf_capture_limit");
162 gamemode_name = "Capture the Flag";
165 else if(game == GAME_RUNEMATCH || cvar("g_runematch"))
167 game = GAME_RUNEMATCH;
168 cvar_set("g_runematch", "1");
169 cvar_set("g_minstagib", "0");
171 if(cvar("deathmatch_force_teamplay"))
174 fraglimit_override = cvar("g_runematch_point_limit");
176 gamemode_name = "Rune Match";
182 else if(game == GAME_DEATHMATCH || game == GAME_TEAM_DEATHMATCH || cvar("g_tdm"))
184 if(!cvar("deathmatch"))
185 cvar_set("deathmatch", "1");
188 if(game == GAME_TEAM_DEATHMATCH || cvar("g_tdm") || cvar("deathmatch_force_teamplay"))
191 game = GAME_TEAM_DEATHMATCH;
192 gamemode_name = "Team Deathmatch";
195 cvar_set("g_tdm", "1");
200 game = GAME_DEATHMATCH;
201 gamemode_name = "Deathmatch";
205 fraglimit_override = cvar("fraglimit_override");
207 else if(game == GAME_ASSAULT || cvar("g_assault"))
211 gamemode_name = "Assault";
214 cvar_set("g_assault", "1");
216 else if(game == GAME_LMS || cvar("g_lms"))
220 cvar_set("g_lms", "1");
221 fraglimit_override = cvar("g_lms_lives_override");
222 if(fraglimit_override == 0)
223 fraglimit_override = -1;
224 gamemode_name = "Last Man Standing";
226 lms_lowest_lives = 999;
228 else if(game == GAME_ARENA || cvar("g_arena"))
232 cvar_set("g_arena", "1");
233 fraglimit_override = cvar("g_arena_point_limit");
234 maxspawned = cvar("g_arena_maxspawned");
237 arena_roundbased = cvar("g_arena_roundbased");
238 gamemode_name = "Arena";
241 else if(game == GAME_KEYHUNT || cvar("g_keyhunt"))
245 cvar_set("g_keyhunt", "1");
246 fraglimit_override = cvar("g_keyhunt_point_limit");
248 gamemode_name = "Key Hunt";
251 else if(game == GAME_ONSLAUGHT || cvar("g_onslaught"))
254 game = GAME_ONSLAUGHT;
255 cvar_set("g_onslaught", "1");
259 gamemode_name = "Onslaught";
264 // we can only assume...
266 cvar_set("g_dm", "1");
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 // those mutators rule each other out
283 if(cvar("g_minstagib"))
285 cvar_set("g_instagib", "0");
286 cvar_set("g_rocketarena", "0");
288 if(cvar("g_instagib"))
290 cvar_set("g_minstagib", "0");
291 cvar_set("g_rocketarena", "0");
293 if(cvar("g_rocketarena"))
295 cvar_set("g_instagib", "0");
296 cvar_set("g_minstagib", "0");
299 registercvar("_motd", "");
300 registercvar("_mutatormsg", "");
301 cvar_set("_motd", wordwrap(cvar_string("sv_motd"), 50));
302 cvar_set("_mutatormsg", wordwrap(cvar_string("g_mutatormsg"), 50));
304 g_domination = cvar("g_domination");
305 g_ctf = cvar("g_ctf");
306 g_lms = cvar("g_lms");
307 g_tdm = cvar("g_tdm");
308 g_runematch = cvar("g_runematch");
309 g_keyhunt = cvar("g_keyhunt");
310 g_onslaught = cvar("g_onslaught");
311 g_assault = cvar("g_assault");
312 g_arena = cvar("g_arena");
314 // enforce the server's universal frag/time limits
315 if(!cvar("g_campaign"))
317 if(fraglimit_override >= 0)
318 cvar_set("fraglimit", ftos(fraglimit_override));
319 if(timelimit_override >= 0)
320 cvar_set("timelimit", ftos(timelimit_override));
323 if (game == GAME_DOMINATION)//cvar("g_domination"))
325 else if (game == GAME_CTF)//cvar("g_ctf"))
327 else if (game == GAME_RUNEMATCH)//cvar("g_runematch"))
329 else if (game == GAME_TEAM_DEATHMATCH)//cvar("g_runematch"))
331 else if (game == GAME_KEYHUNT)//cvar("g_keyhunt"))
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);
374 if(self.classname == "observer")
376 if(g_lms && self.frags <= 0 && self.frags > -666)
377 return centerprint_atprio(self, CENTERPRIO_SPAM, strcat(NEWLINES, "^1You have no more lives left\nwait for next round\n\n\n^7press attack to spectate other players"));
378 else if(g_lms && self.frags == -666)
379 return centerprint_atprio(self, CENTERPRIO_SPAM, strcat(NEWLINES, "^1Match has already begun\nwait for next round\n\n\n^7press attack to spectate other players"));
381 else if(self.classname == "spectator")
383 if ((g_lms && self.frags < 1) || g_arena)
384 return centerprint_atprio(self, CENTERPRIO_SPAM, strcat(NEWLINES, "spectating ", self.enemy.netname, "\n\n\n^7press attack for next player\npress attack2 for free fly mode"));
386 return centerprint_atprio(self, CENTERPRIO_SPAM, 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"));
391 mutator = "^2Minstagib ^1";
393 mutator = "^2Instagib ^1";
394 else if(g_rocketarena)
395 mutator = "^2Rocketarena ^1";
397 mutator = "^2No Items Nexuiz ^1";
400 // to protect against unheedingly made changes
402 modifications = strcat(modifications, ", ");
404 modifications = "midair";
408 modifications = strcat(modifications, ", ");
410 modifications = strcat(modifications, "vampire");
412 if(g_laserguided_missile) {
414 modifications = strcat(modifications, ", ");
416 modifications = strcat(modifications, "laser-guided-missiles");
419 local string versionmessage;
420 versionmessage = GetClientVersionMessage(self.version);
422 s = strcat(s, NEWLINES, "This is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
423 s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
425 if(modifications != "")
426 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
428 if(time < restart_countdown)
429 s = strcat(s, "\n^1Game starts in ", ftos(ceil(restart_countdown-time)), " seconds^7");
433 if (g_grappling_hook)
434 s = strcat(s, "\n\n^8grappling hook is enabled, press 'e' to use it\n");
436 if (cvar_string("_mutatormsg") != "") {
437 s = strcat(s, "\n\n^8special gameplay tips: ^7", cvar_string("_mutatormsg"));
440 if (cvar_string("_motd") != "") {
441 s = strcat(s, "\n\n^8MOTD: ^7", cvar_string("_motd"));
445 if(cvar("fraglimit"))
448 if(cvar("timelimit"))
451 s = strcat(s, "\n^8frag limit: ^7", cvar_string("fraglimit"), padding);
453 if(cvar("timelimit"))
454 s = strcat(s, "\n^8time limit: ^7", cvar_string("timelimit"), " minutes");
465 void SetPlayerColors(entity pl, float _color)
469 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
471 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
472 pl.clientcolors = 16*cl + cl;*/
475 pants = _color & 0x0F;
476 shirt = _color & 0xF0;
480 setcolor(pl, 16*pants + pants);
482 setcolor(pl, shirt + pants);
486 void SetPlayerTeam(entity pl, float t, float s, float noprint)
491 _color = COLOR_TEAM4 - 1;
493 _color = COLOR_TEAM3 - 1;
495 _color = COLOR_TEAM2 - 1;
497 _color = COLOR_TEAM1 - 1;
499 SetPlayerColors(pl,_color);
501 if(!noprint && t != s)
503 //bprint(pl.netname, " has changed to ", TeamNoName(t), "\n");
504 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
516 // set c1...c4 to show what teams are allowed
517 void CheckAllowedTeams (entity for_whom)
526 c1 = c2 = c3 = c4 = -1;
527 cb1 = cb2 = cb3 = cb4 = 0;
529 // onslaught is special
532 head = findchain(classname, "onslaught_generator");
535 if (head.team == COLOR_TEAM1) c1 = 0;
536 if (head.team == COLOR_TEAM2) c2 = 0;
537 if (head.team == COLOR_TEAM3) c3 = 0;
538 if (head.team == COLOR_TEAM4) c4 = 0;
545 teament_name = "dom_team";
547 teament_name = "ctf_team";
549 teament_name = "tdm_team";
552 c1 = c2 = 0; // Assault always has 2 teams
557 // cover anything else by treating it like tdm with no teams spawned
561 dm = cvar("g_tdm_teams");
563 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
567 c1 = c2 = c3 = c4 = 0;
580 // first find out what teams are allowed
581 head = find(world, classname, teament_name);
584 if(!(g_domination && head.netname == ""))
586 if(head.team == COLOR_TEAM1)
590 if(head.team == COLOR_TEAM2)
594 if(head.team == COLOR_TEAM3)
598 if(head.team == COLOR_TEAM4)
603 head = find(head, classname, teament_name);
606 if(cvar("bot_vs_human") > 0)
609 if(clienttype(for_whom) == CLIENTTYPE_BOT)
614 else if(cvar("bot_vs_human") < 0)
617 if(clienttype(for_whom) == CLIENTTYPE_BOT)
624 float PlayerValue(entity p)
626 if(IsTeamBalanceForced() == 1)
631 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
632 // teams that are allowed will now have their player counts stored in c1...c4
633 void GetTeamCounts(entity ignore)
637 // now count how many players are on each team already
639 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
640 // also remember the lowest-scoring player
642 FOR_EACH_PLAYER(head)
644 if(head != ignore)// && head.netname != "")
646 value = PlayerValue(head);
647 if(clienttype(head) == CLIENTTYPE_BOT)
651 if(head.team == COLOR_TEAM1)
659 if(head.team == COLOR_TEAM2)
667 if(head.team == COLOR_TEAM3)
675 if(head.team == COLOR_TEAM4)
687 // returns # of smallest team (1, 2, 3, 4)
688 // NOTE: Assumes CheckAllowedTeams has already been called!
689 float FindSmallestTeam(entity pl, float ignore_pl)
691 float totalteams, smallestteam, smallestteam_count, smallestteam_score, balance_type;
694 // find out what teams are available
695 //CheckAllowedTeams();
697 // make sure there are at least 2 teams to join
699 totalteams = totalteams + 1;
701 totalteams = totalteams + 1;
703 totalteams = totalteams + 1;
705 totalteams = totalteams + 1;
707 if(cvar("bot_vs_human"))
713 error("Too few teams available for domination\n");
715 error("Too few teams available for ctf\n");
717 error("Too few teams available for key hunt\n");
719 error("Too few teams available for team deathmatch\n");
723 // count how many players are in each team
727 GetTeamCounts(world);
729 // c1...c4 now have counts of each team
730 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
733 smallestteam_count = 999999999;
734 smallestteam_score = 999999999;
736 // 2 gives priority to what team you're already on, 1 goes in order
737 // 2 doesn't seem to work though...
741 //if(pl.classname != "player")
742 if(clienttype(pl) != CLIENTTYPE_BOT)
744 c1 -= cb1 * 255.0/256;
745 c2 -= cb2 * 255.0/256;
746 c3 -= cb3 * 255.0/256;
747 c4 -= cb4 * 255.0/256;
750 if(balance_type == 1)
752 if(c1 >= 0 && (c1 < smallestteam_count || (c1 <= smallestteam_count && team1_score < smallestteam_score)))
755 smallestteam_count = c1;
756 smallestteam_score = team1_score;
758 if(c2 >= 0 && (c2 < smallestteam_count || (c2 <= smallestteam_count && team2_score < smallestteam_score)))
761 smallestteam_count = c2;
762 smallestteam_score = team2_score;
764 if(c3 >= 0 && (c3 < smallestteam_count || (c3 <= smallestteam_count && team3_score < smallestteam_score)))
767 smallestteam_count = c3;
768 smallestteam_score = team3_score;
770 if(c4 >= 0 && (c4 < smallestteam_count || (c4 <= smallestteam_count && team4_score < smallestteam_score)))
773 smallestteam_count = c4;
774 smallestteam_score = team4_score;
779 if(c1 >= 0 && (c1 < smallestteam_count ||
780 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
783 smallestteam_count = c1;
785 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
786 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
789 smallestteam_count = c2;
791 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
792 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
795 smallestteam_count = c3;
797 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
798 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
801 smallestteam_count = c4;
808 float JoinBestTeam(entity pl, float only_return_best)
810 float smallest, selectedteam;
812 // don't join a team if we're not playing a team game
813 if(!cvar("teamplay") && !g_domination && !g_ctf && !g_keyhunt)
816 // find out what teams are available
817 CheckAllowedTeams(pl);
821 if(cvar("g_domination_default_teams") < 3)
823 if(cvar("g_domination_default_teams") < 4)
827 // if we don't care what team he ends up on, put him on whatever team he entered as.
828 // if he's not on a valid team, then let other code put him on the smallest team
829 if(!cvar("g_campaign") && !cvar("g_balance_teams") && !cvar("g_balance_teams_force"))
831 if( c1 >= 0 && pl.team == COLOR_TEAM1)
832 selectedteam = pl.team;
833 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
834 selectedteam = pl.team;
835 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
836 selectedteam = pl.team;
837 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
838 selectedteam = pl.team;
843 if(!only_return_best)
845 SetPlayerColors(pl, selectedteam - 1);
850 // otherwise end up on the smallest team (handled below)
853 smallest = FindSmallestTeam(pl, TRUE);
856 if(!only_return_best)
858 TeamchangeFrags(self);
861 SetPlayerColors(pl, COLOR_TEAM1 - 1);
863 else if(smallest == 2)
865 SetPlayerColors(pl, COLOR_TEAM2 - 1);
867 else if(smallest == 3)
869 SetPlayerColors(pl, COLOR_TEAM3 - 1);
871 else if(smallest == 4)
873 SetPlayerColors(pl, COLOR_TEAM4 - 1);
877 error("smallest team: invalid team\n");
880 if(pl.deadflag == DEAD_NO)
881 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
888 void SV_ChangeTeam(float _color)
890 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
892 // in normal deathmatch we can just apply the color and we're done
893 if(!cvar("teamplay")) {
894 SetPlayerColors(self, _color);
898 scolor = self.clientcolors & 0x0F;
899 dcolor = _color & 0x0F;
901 if(scolor == COLOR_TEAM1 - 1)
903 else if(scolor == COLOR_TEAM2 - 1)
905 else if(scolor == COLOR_TEAM3 - 1)
907 else if(scolor == COLOR_TEAM4 - 1)
909 if(dcolor == COLOR_TEAM1 - 1)
911 else if(dcolor == COLOR_TEAM2 - 1)
913 else if(dcolor == COLOR_TEAM3 - 1)
915 else if(dcolor == COLOR_TEAM4 - 1)
918 CheckAllowedTeams(self);
920 if(dteam == 1 && c1 < 0) dteam = 4;
921 if(dteam == 4 && c4 < 0) dteam = 3;
922 if(dteam == 3 && c3 < 0) dteam = 2;
923 if(dteam == 2 && c2 < 0) dteam = 1;
925 // not changing teams
928 //bprint("same team change\n");
929 SetPlayerTeam(self, dteam, steam, TRUE);
935 if(cvar("g_campaign") || cvar("g_changeteam_banned"))
937 sprint(self, "Team changes not allowed\n");
938 return; // changing teams is not allowed
941 if(!cvar("g_campaign") && cvar("g_balance_teams_prevent_imbalance"))
943 // only allow changing to a smaller or equal size team
945 // find out what teams are available
946 //CheckAllowedTeams();
947 // count how many players on each team
948 GetTeamCounts(world);
951 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
956 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
961 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
966 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
973 sprint(self, "Cannot change to an invalid team\n");
979 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
981 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
983 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
985 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
988 if(scount) // started at a valid, nonempty team
990 // check if we're trying to change to a larger team that doens't have bots to swap with
991 if(dcount >= scount && dbotcount <= 0)
993 sprint(self, "Cannot change to a larger team\n");
994 return; // can't change to a larger team
1000 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
1002 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1004 // reduce frags during a team change
1005 TeamchangeFrags(self);
1008 SetPlayerTeam(self, dteam, steam, FALSE);
1010 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1012 // kill player when changing teams
1013 if(self.deadflag == DEAD_NO)
1014 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
1018 void ShufflePlayerOutOfTeam (float source_team)
1020 float smallestteam, smallestteam_count, steam;
1021 float lowest_bot_score, lowest_player_score;
1022 entity head, lowest_bot, lowest_player, selected;
1025 smallestteam_count = 999999999;
1027 if(c1 >= 0 && c1 < smallestteam_count)
1030 smallestteam_count = c1;
1032 if(c2 >= 0 && c2 < smallestteam_count)
1035 smallestteam_count = c2;
1037 if(c3 >= 0 && c3 < smallestteam_count)
1040 smallestteam_count = c3;
1042 if(c4 >= 0 && c4 < smallestteam_count)
1045 smallestteam_count = c4;
1050 bprint("warning: no smallest team\n");
1054 if(source_team == 1)
1055 steam = COLOR_TEAM1;
1056 else if(source_team == 2)
1057 steam = COLOR_TEAM2;
1058 else if(source_team == 3)
1059 steam = COLOR_TEAM3;
1060 else if(source_team == 4)
1061 steam = COLOR_TEAM4;
1064 lowest_bot_score = 999999999;
1065 lowest_player = world;
1066 lowest_player_score = 999999999;
1068 // find the lowest-scoring player & bot of that team
1069 FOR_EACH_PLAYER(head)
1071 if(head.team == steam)
1075 if(head.frags < lowest_bot_score)
1078 lowest_bot_score = head.frags;
1083 if(head.frags < lowest_player_score)
1085 lowest_player = head;
1086 lowest_player_score = head.frags;
1092 // prefers to move a bot...
1093 if(lowest_bot != world)
1094 selected = lowest_bot;
1095 // but it will move a player if it has to
1097 selected = lowest_player;
1098 // don't do anything if it couldn't find anyone
1101 bprint("warning: couldn't find a player to move from team\n");
1105 // smallest team gains a member
1106 if(smallestteam == 1)
1110 else if(smallestteam == 2)
1114 else if(smallestteam == 3)
1118 else if(smallestteam == 4)
1124 bprint("warning: destination team invalid\n");
1127 // source team loses a member
1128 if(source_team == 1)
1132 else if(source_team == 2)
1136 else if(source_team == 3)
1140 else if(source_team == 4)
1146 bprint("warning: source team invalid\n");
1150 // move the player to the new team
1151 TeamchangeFrags(selected);
1152 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1154 if(selected.deadflag == DEAD_NO)
1155 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1156 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1159 float lastRebalanceInfo;
1160 void CauseRebalance(float source_team, float howmany_toomany)
1165 if(IsTeamBalanceForced() == 1)
1167 bprint("Rebalancing Teams\n");
1168 ShufflePlayerOutOfTeam(source_team);
1172 if(howmany_toomany < cvar("g_balance_teams_complain"))
1174 if(time < lastRebalanceInfo + 90)
1176 lastRebalanceInfo = time;
1177 if(source_team == 1)
1178 steam = COLOR_TEAM1;
1179 else if(source_team == 2)
1180 steam = COLOR_TEAM2;
1181 else if(source_team == 3)
1182 steam = COLOR_TEAM3;
1183 else if(source_team == 4)
1184 steam = COLOR_TEAM4;
1185 ServerConsoleEcho(strcat("Team ", ftos(source_team), " too large, complaining."), TRUE);
1186 FOR_EACH_REALPLAYER(head)
1188 if(head.team == steam)
1190 sprint(head, "\{1}\{13}^3SERVER NOTICE:^7 One of you please change teams!\n");
1191 centerprint_atprio(head, CENTERPRIO_REBALANCE, "^3SERVER NOTICE:\n\n^7Someone of you please change teams!");
1197 // part of g_balance_teams_force
1198 // occasionally perform an audit of the teams to make
1199 // sure they're more or less balanced in player count.
1202 float numplayers, numteams, smallest, toomany;
1204 balance = IsTeamBalanceForced();
1208 if(audit_teams_time > time)
1211 audit_teams_time = time + 4 + random();
1213 // bprint("Auditing teams\n");
1215 CheckAllowedTeams(world);
1216 GetTeamCounts(world);
1219 numteams = numplayers = smallest = 0;
1222 numteams = numteams + 1;
1223 numplayers = numplayers + c1;
1228 numteams = numteams + 1;
1229 numplayers = numplayers + c2;
1235 numteams = numteams + 1;
1236 numplayers = numplayers + c3;
1242 numteams = numteams + 1;
1243 numplayers = numplayers + c4;
1249 return; // no players to move around
1251 return; // don't bother shuffling if for some reason there aren't any teams
1253 toomany = smallest + 1;
1255 if(c1 && c1 > toomany)
1256 CauseRebalance(1, c1 - toomany);
1257 if(c2 && c2 > toomany)
1258 CauseRebalance(2, c2 - toomany);
1259 if(c3 && c3 > toomany)
1260 CauseRebalance(3, c3 - toomany);
1261 if(c4 && c4 > toomany)
1262 CauseRebalance(4, c4 - toomany);
1264 // if teams are still unbalanced, balance them further in the next audit,
1265 // which will happen sooner (keep doing rapid audits until things are in order)
1266 audit_teams_time = time + 0.7 + random()*0.3;
1271 /*void(entity e, float first) UpdateTeamScore =
1276 WriteByte (MSG_ALL, SVC_UPDATENAME);
1277 WriteByte (MSG_ALL, clientno);
1278 WriteString (MSG_ALL, e.netname);
1280 WriteByte (MSG_ALL, SVC_UPDATECOLORS);
1281 WriteByte (MSG_ALL, clientno);
1282 WriteByte (MSG_ALL, e.b_shirt * 16 + who.b_pants);
1285 WriteByte (MSG_ALL, SVC_UPDATEFRAGS);
1286 WriteByte (MSG_ALL, clientno);
1287 WriteShort (MSG_ALL, e.frags + 10000);
1293 // code from here on is just to support maps that don't have team entities
1294 void tdm_spawnteam (string teamname, float teamcolor)
1298 e.classname = "tdm_team";
1299 e.netname = teamname;
1304 // spawn some default teams if the map is not set up for tdm
1305 void() tdm_spawnteams =
1309 numteams = cvar("g_tdm_teams");
1311 tdm_spawnteam("Red", COLOR_TEAM1-1);
1312 tdm_spawnteam("Blue", COLOR_TEAM2-1);
1314 tdm_spawnteam("Yellow", COLOR_TEAM3-1);
1316 tdm_spawnteam("Pink", COLOR_TEAM4-1);
1319 void() tdm_delayedinit =
1321 self.think = SUB_Remove;
1322 self.nextthink = time;
1323 // if no teams are found, spawn defaults
1324 if (find(world, classname, "tdm_team") == world)
1332 e.think = tdm_delayedinit;
1333 e.nextthink = time + 0.1;