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 return strcat(Team_ColorName(t), " Team");
44 string ColoredTeamName(float t)
46 return strcat(Team_ColorCode(t), Team_ColorName(t), " Team^7");
48 string TeamNoName(float t)
50 // fixme: Search for team entities and get their .netname's!
59 return "Neutral Team";
64 void runematch_init();
67 void(entity pl) LogTeamchange =
70 if(!cvar("sv_eventlog"))
74 str = strcat(":team:", ftos(pl.playerid), ":");
75 str = strcat(str, ftos(pl.team));
76 GameLogEcho(str, FALSE);
81 cvar_set("g_dm", "0");
82 cvar_set("g_tdm", "0");
83 cvar_set("g_domination", "0");
84 cvar_set("g_ctf", "0");
85 cvar_set("g_runematch", "0");
86 cvar_set("g_lms", "0");
87 cvar_set("g_arena", "0");
88 cvar_set("g_keyhunt", "0");
89 cvar_set("g_assault", "0");
90 cvar_set("g_onslaught", "0");
91 cvar_set("teamplay", "0");
94 void ActivateTeamplay()
96 float teamplay_default;
97 teamplay_default = cvar("teamplay_default");
100 cvar_set("teamplay", ftos(teamplay_default));
102 cvar_set("teamplay", "3");
105 void InitGameplayMode()
107 float fraglimit_override, timelimit_override;
111 game = cvar("gamecfg"); // load game options
113 // game cvars get reset before map changes
114 // then map's cfg sets them as desired
116 // FIXME: also set a message or game mode name to print to players when the join
118 // set both here, gamemode can override it later
119 timelimit_override = cvar("timelimit_override");
120 fraglimit_override = cvar("fraglimit_override");
122 if(game == GAME_DOMINATION || cvar("g_domination"))
125 game = GAME_DOMINATION;
126 cvar_set("g_domination", "1");
130 fraglimit_override = cvar("g_domination_point_limit");
132 gamemode_name = "Domination";
135 else if(game == GAME_CTF || cvar("g_ctf"))
139 cvar_set("g_ctf", "1");
143 fraglimit_override = cvar("g_ctf_capture_limit");
145 gamemode_name = "Capture the Flag";
148 else if(game == GAME_RUNEMATCH || cvar("g_runematch"))
150 game = GAME_RUNEMATCH;
151 cvar_set("g_runematch", "1");
152 cvar_set("g_minstagib", "0");
154 if(cvar("deathmatch_force_teamplay"))
157 fraglimit_override = cvar("g_runematch_point_limit");
159 gamemode_name = "Rune Match";
165 else if(game == GAME_DEATHMATCH || game == GAME_TEAM_DEATHMATCH || cvar("g_tdm"))
167 if(!cvar("deathmatch"))
168 cvar_set("deathmatch", "1");
171 if(game == GAME_TEAM_DEATHMATCH || cvar("g_tdm") || cvar("deathmatch_force_teamplay"))
174 game = GAME_TEAM_DEATHMATCH;
175 gamemode_name = "Team Deathmatch";
178 cvar_set("g_tdm", "1");
183 game = GAME_DEATHMATCH;
184 gamemode_name = "Deathmatch";
188 fraglimit_override = cvar("fraglimit_override");
190 else if(game == GAME_ASSAULT || cvar("g_assault"))
194 gamemode_name = "Assault";
197 cvar_set("g_assault", "1");
199 else if(game == GAME_LMS || cvar("g_lms"))
203 cvar_set("g_lms", "1");
204 fraglimit_override = cvar("g_lms_lives_override");
205 if(fraglimit_override == 0)
206 fraglimit_override = -1;
207 gamemode_name = "Last Man Standing";
209 lms_lowest_lives = 999;
211 else if(game == GAME_ARENA || cvar("g_arena"))
215 cvar_set("g_arena", "1");
216 fraglimit_override = cvar("g_arena_point_limit");
217 maxspawned = cvar("g_arena_maxspawned");
220 arena_roundbased = cvar("g_arena_roundbased");
221 gamemode_name = "Arena";
224 else if(game == GAME_KEYHUNT || cvar("g_keyhunt"))
228 cvar_set("g_keyhunt", "1");
229 fraglimit_override = cvar("g_keyhunt_point_limit");
231 gamemode_name = "Key Hunt";
234 else if(game == GAME_ONSLAUGHT || cvar("g_onslaught"))
237 game = GAME_ONSLAUGHT;
238 cvar_set("g_onslaught", "1");
242 gamemode_name = "Onslaught";
247 // we can only assume...
249 cvar_set("g_dm", "1");
250 gamemode_name = "Deathmatch";
253 /* else if(game == GAME_TEAM_DEATHMATCH)
255 if(!cvar("deathmatch"))
256 cvar_set("deathmatch", "1");
258 //if(!cvar("teamplay"))
259 // cvar_set("teamplay", "3");
262 fraglimit_override = cvar("fraglimit_override");
265 // those mutators rule each other out
266 if(cvar("g_minstagib"))
268 cvar_set("g_instagib", "0");
269 cvar_set("g_rocketarena", "0");
271 if(cvar("g_instagib"))
273 cvar_set("g_minstagib", "0");
274 cvar_set("g_rocketarena", "0");
276 if(cvar("g_rocketarena"))
278 cvar_set("g_instagib", "0");
279 cvar_set("g_minstagib", "0");
282 registercvar("_motd", "");
283 registercvar("_mutatormsg", "");
284 cvar_set("_motd", wordwrap(cvar_string("sv_motd"), 50));
285 cvar_set("_mutatormsg", wordwrap(cvar_string("g_mutatormsg"), 50));
287 g_domination = cvar("g_domination");
288 g_ctf = cvar("g_ctf");
289 g_lms = cvar("g_lms");
290 g_tdm = cvar("g_tdm");
291 g_runematch = cvar("g_runematch");
292 g_keyhunt = cvar("g_keyhunt");
293 g_onslaught = cvar("g_onslaught");
294 g_assault = cvar("g_assault");
295 g_arena = cvar("g_arena");
297 // enforce the server's universal frag/time limits
298 if(!cvar("g_campaign"))
300 if(fraglimit_override >= 0)
301 cvar_set("fraglimit", ftos(fraglimit_override));
302 if(timelimit_override >= 0)
303 cvar_set("timelimit", ftos(timelimit_override));
306 if (game == GAME_DOMINATION)//cvar("g_domination"))
308 else if (game == GAME_CTF)//cvar("g_ctf"))
310 else if (game == GAME_RUNEMATCH)//cvar("g_runematch"))
312 else if (game == GAME_TEAM_DEATHMATCH)//cvar("g_runematch"))
314 else if (game == GAME_KEYHUNT)//cvar("g_keyhunt"))
318 string GetClientVersionMessage(float v) {
319 local string versionmsg;
321 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";
322 // either that or someone wants to be funny
323 } else if (v != cvar("gameversion")) {
324 if(v < cvar("gameversion")) {
325 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
327 versionmsg = "^3This server is using an outdated Nexuiz version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
330 versionmsg = "^2client version and server version are compatible.^8";
332 return strzone(versionmsg);
337 void PrintWelcomeMessage(entity pl)
339 string s, mutator, modifications, padding;
341 /*if(self.welcomemessage_time > time)
343 self.welcomemessage_time = time + 0.8; */
345 if(self.cvar_scr_centertime == 0) return;
346 if(self.welcomemessage_time > time) return;
347 self.welcomemessage_time = time + self.cvar_scr_centertime * 0.6;
349 if(cvar("g_campaign"))
351 centerprint(pl, campaign_message);
357 if(self.classname == "observer")
359 if(g_lms && self.frags <= 0 && self.frags > -666)
360 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"));
361 else if(g_lms && self.frags == -666)
362 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"));
364 else if(self.classname == "spectator")
366 if ((g_lms && self.frags < 1) || g_arena)
367 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"));
369 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"));
374 mutator = "^2Minstagib ^1";
376 mutator = "^2Instagib ^1";
377 else if(g_rocketarena)
378 mutator = "^2Rocketarena ^1";
380 mutator = "^2No Items Nexuiz ^1";
383 // to protect against unheedingly made changes
385 modifications = strcat(modifications, ", ");
387 modifications = "midair";
391 modifications = strcat(modifications, ", ");
393 modifications = strcat(modifications, "vampire");
395 if(g_laserguided_missile) {
397 modifications = strcat(modifications, ", ");
399 modifications = strcat(modifications, "laser-guided-missiles");
402 local string versionmessage;
403 versionmessage = GetClientVersionMessage(self.version);
405 s = strcat(s, NEWLINES, "This is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
406 s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
408 if(modifications != "")
409 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
411 if(time < restart_countdown)
412 s = strcat(s, "\n^1Game starts in ", ftos(ceil(restart_countdown-time)), " seconds^7");
416 if (g_grappling_hook)
417 s = strcat(s, "\n\n^8grappling hook is enabled, press 'e' to use it\n");
419 if (cvar_string("_mutatormsg") != "") {
420 s = strcat(s, "\n\n^8special gameplay tips: ^7", cvar_string("_mutatormsg"));
423 if (cvar_string("_motd") != "") {
424 s = strcat(s, "\n\n^8MOTD: ^7", cvar_string("_motd"));
428 if(cvar("fraglimit"))
431 if(cvar("timelimit"))
434 s = strcat(s, "\n^8frag limit: ^7", cvar_string("fraglimit"), padding);
436 if(cvar("timelimit"))
437 s = strcat(s, "\n^8time limit: ^7", cvar_string("timelimit"), " minutes");
448 void SetPlayerColors(entity pl, float _color)
452 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
454 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
455 pl.clientcolors = 16*cl + cl;*/
458 pants = _color & 0x0F;
459 shirt = _color & 0xF0;
463 setcolor(pl, 16*pants + pants);
465 setcolor(pl, shirt + pants);
469 void SetPlayerTeam(entity pl, float t, float s, float noprint)
474 _color = COLOR_TEAM4 - 1;
476 _color = COLOR_TEAM3 - 1;
478 _color = COLOR_TEAM2 - 1;
480 _color = COLOR_TEAM1 - 1;
482 SetPlayerColors(pl,_color);
484 if(!noprint && t != s)
486 //bprint(pl.netname, " has changed to ", TeamNoName(t), "\n");
487 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
499 // set c1...c4 to show what teams are allowed
500 void CheckAllowedTeams (entity for_whom)
509 c1 = c2 = c3 = c4 = -1;
510 cb1 = cb2 = cb3 = cb4 = 0;
512 // onslaught is special
515 head = findchain(classname, "onslaught_generator");
518 if (head.team == COLOR_TEAM1) c1 = 0;
519 if (head.team == COLOR_TEAM2) c2 = 0;
520 if (head.team == COLOR_TEAM3) c3 = 0;
521 if (head.team == COLOR_TEAM4) c4 = 0;
528 teament_name = "dom_team";
530 teament_name = "ctf_team";
532 teament_name = "tdm_team";
535 c1 = c2 = 0; // Assault always has 2 teams
540 // cover anything else by treating it like tdm with no teams spawned
544 dm = cvar("g_tdm_teams");
546 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
550 c1 = c2 = c3 = c4 = 0;
563 // first find out what teams are allowed
564 head = find(world, classname, teament_name);
567 if(!(g_domination && head.netname == ""))
569 if(head.team == COLOR_TEAM1)
573 if(head.team == COLOR_TEAM2)
577 if(head.team == COLOR_TEAM3)
581 if(head.team == COLOR_TEAM4)
586 head = find(head, classname, teament_name);
589 if(cvar("bot_vs_human") > 0)
592 if(clienttype(for_whom) == CLIENTTYPE_BOT)
597 else if(cvar("bot_vs_human") < 0)
600 if(clienttype(for_whom) == CLIENTTYPE_BOT)
607 float PlayerValue(entity p)
609 if(IsTeamBalanceForced() == 1)
614 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
615 // teams that are allowed will now have their player counts stored in c1...c4
616 void GetTeamCounts(entity ignore)
620 // now count how many players are on each team already
622 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
623 // also remember the lowest-scoring player
625 FOR_EACH_PLAYER(head)
627 if(head != ignore)// && head.netname != "")
629 value = PlayerValue(head);
630 if(clienttype(head) == CLIENTTYPE_BOT)
634 if(head.team == COLOR_TEAM1)
642 if(head.team == COLOR_TEAM2)
650 if(head.team == COLOR_TEAM3)
658 if(head.team == COLOR_TEAM4)
670 // returns # of smallest team (1, 2, 3, 4)
671 // NOTE: Assumes CheckAllowedTeams has already been called!
672 float FindSmallestTeam(entity pl, float ignore_pl)
674 float totalteams, smallestteam, smallestteam_count, smallestteam_score, balance_type;
677 // find out what teams are available
678 //CheckAllowedTeams();
680 // make sure there are at least 2 teams to join
682 totalteams = totalteams + 1;
684 totalteams = totalteams + 1;
686 totalteams = totalteams + 1;
688 totalteams = totalteams + 1;
690 if(cvar("bot_vs_human"))
696 error("Too few teams available for domination\n");
698 error("Too few teams available for ctf\n");
700 error("Too few teams available for key hunt\n");
702 error("Too few teams available for team deathmatch\n");
706 // count how many players are in each team
710 GetTeamCounts(world);
712 // c1...c4 now have counts of each team
713 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
716 smallestteam_count = 999999999;
717 smallestteam_score = 999999999;
719 // 2 gives priority to what team you're already on, 1 goes in order
720 // 2 doesn't seem to work though...
724 //if(pl.classname != "player")
725 if(clienttype(pl) != CLIENTTYPE_BOT)
727 c1 -= cb1 * 255.0/256;
728 c2 -= cb2 * 255.0/256;
729 c3 -= cb3 * 255.0/256;
730 c4 -= cb4 * 255.0/256;
733 if(balance_type == 1)
735 if(c1 >= 0 && (c1 < smallestteam_count || (c1 <= smallestteam_count && team1_score < smallestteam_score)))
738 smallestteam_count = c1;
739 smallestteam_score = team1_score;
741 if(c2 >= 0 && (c2 < smallestteam_count || (c2 <= smallestteam_count && team2_score < smallestteam_score)))
744 smallestteam_count = c2;
745 smallestteam_score = team2_score;
747 if(c3 >= 0 && (c3 < smallestteam_count || (c3 <= smallestteam_count && team3_score < smallestteam_score)))
750 smallestteam_count = c3;
751 smallestteam_score = team3_score;
753 if(c4 >= 0 && (c4 < smallestteam_count || (c4 <= smallestteam_count && team4_score < smallestteam_score)))
756 smallestteam_count = c4;
757 smallestteam_score = team4_score;
762 if(c1 >= 0 && (c1 < smallestteam_count ||
763 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
766 smallestteam_count = c1;
768 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
769 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
772 smallestteam_count = c2;
774 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
775 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
778 smallestteam_count = c3;
780 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
781 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
784 smallestteam_count = c4;
791 float JoinBestTeam(entity pl, float only_return_best)
793 float smallest, selectedteam;
795 // don't join a team if we're not playing a team game
796 if(!cvar("teamplay") && !g_domination && !g_ctf && !g_keyhunt)
799 // find out what teams are available
800 CheckAllowedTeams(pl);
804 if(cvar("g_domination_default_teams") < 3)
806 if(cvar("g_domination_default_teams") < 4)
810 // if we don't care what team he ends up on, put him on whatever team he entered as.
811 // if he's not on a valid team, then let other code put him on the smallest team
812 if(!cvar("g_campaign") && !cvar("g_balance_teams") && !cvar("g_balance_teams_force"))
814 if( c1 >= 0 && pl.team == COLOR_TEAM1)
815 selectedteam = pl.team;
816 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
817 selectedteam = pl.team;
818 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
819 selectedteam = pl.team;
820 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
821 selectedteam = pl.team;
826 if(!only_return_best)
828 SetPlayerColors(pl, selectedteam - 1);
833 // otherwise end up on the smallest team (handled below)
836 smallest = FindSmallestTeam(pl, TRUE);
839 if(!only_return_best)
841 TeamchangeFrags(self);
844 SetPlayerColors(pl, COLOR_TEAM1 - 1);
846 else if(smallest == 2)
848 SetPlayerColors(pl, COLOR_TEAM2 - 1);
850 else if(smallest == 3)
852 SetPlayerColors(pl, COLOR_TEAM3 - 1);
854 else if(smallest == 4)
856 SetPlayerColors(pl, COLOR_TEAM4 - 1);
860 error("smallest team: invalid team\n");
863 if(pl.deadflag == DEAD_NO)
864 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
871 void SV_ChangeTeam(float _color)
873 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
875 // in normal deathmatch we can just apply the color and we're done
876 if(!cvar("teamplay")) {
877 SetPlayerColors(self, _color);
881 scolor = self.clientcolors & 0x0F;
882 dcolor = _color & 0x0F;
884 if(scolor == COLOR_TEAM1 - 1)
886 else if(scolor == COLOR_TEAM2 - 1)
888 else if(scolor == COLOR_TEAM3 - 1)
890 else if(scolor == COLOR_TEAM4 - 1)
892 if(dcolor == COLOR_TEAM1 - 1)
894 else if(dcolor == COLOR_TEAM2 - 1)
896 else if(dcolor == COLOR_TEAM3 - 1)
898 else if(dcolor == COLOR_TEAM4 - 1)
901 CheckAllowedTeams(self);
903 if(dteam == 1 && c1 < 0) dteam = 4;
904 if(dteam == 4 && c4 < 0) dteam = 3;
905 if(dteam == 3 && c3 < 0) dteam = 2;
906 if(dteam == 2 && c2 < 0) dteam = 1;
908 // not changing teams
911 //bprint("same team change\n");
912 SetPlayerTeam(self, dteam, steam, TRUE);
918 if(cvar("g_campaign") || cvar("g_changeteam_banned"))
920 sprint(self, "Team changes not allowed\n");
921 return; // changing teams is not allowed
924 if(!cvar("g_campaign") && cvar("g_balance_teams_prevent_imbalance"))
926 // only allow changing to a smaller or equal size team
928 // find out what teams are available
929 //CheckAllowedTeams();
930 // count how many players on each team
931 GetTeamCounts(world);
934 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
939 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
944 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
949 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
956 sprint(self, "Cannot change to an invalid team\n");
962 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
964 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
966 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
968 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
971 if(scount) // started at a valid, nonempty team
973 // check if we're trying to change to a larger team that doens't have bots to swap with
974 if(dcount >= scount && dbotcount <= 0)
976 sprint(self, "Cannot change to a larger team\n");
977 return; // can't change to a larger team
983 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
985 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
987 // reduce frags during a team change
988 TeamchangeFrags(self);
991 SetPlayerTeam(self, dteam, steam, FALSE);
993 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
995 // kill player when changing teams
996 if(self.deadflag == DEAD_NO)
997 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
1001 void ShufflePlayerOutOfTeam (float source_team)
1003 float smallestteam, smallestteam_count, steam;
1004 float lowest_bot_score, lowest_player_score;
1005 entity head, lowest_bot, lowest_player, selected;
1008 smallestteam_count = 999999999;
1010 if(c1 >= 0 && c1 < smallestteam_count)
1013 smallestteam_count = c1;
1015 if(c2 >= 0 && c2 < smallestteam_count)
1018 smallestteam_count = c2;
1020 if(c3 >= 0 && c3 < smallestteam_count)
1023 smallestteam_count = c3;
1025 if(c4 >= 0 && c4 < smallestteam_count)
1028 smallestteam_count = c4;
1033 bprint("warning: no smallest team\n");
1037 if(source_team == 1)
1038 steam = COLOR_TEAM1;
1039 else if(source_team == 2)
1040 steam = COLOR_TEAM2;
1041 else if(source_team == 3)
1042 steam = COLOR_TEAM3;
1043 else if(source_team == 4)
1044 steam = COLOR_TEAM4;
1047 lowest_bot_score = 999999999;
1048 lowest_player = world;
1049 lowest_player_score = 999999999;
1051 // find the lowest-scoring player & bot of that team
1052 FOR_EACH_PLAYER(head)
1054 if(head.team == steam)
1058 if(head.frags < lowest_bot_score)
1061 lowest_bot_score = head.frags;
1066 if(head.frags < lowest_player_score)
1068 lowest_player = head;
1069 lowest_player_score = head.frags;
1075 // prefers to move a bot...
1076 if(lowest_bot != world)
1077 selected = lowest_bot;
1078 // but it will move a player if it has to
1080 selected = lowest_player;
1081 // don't do anything if it couldn't find anyone
1084 bprint("warning: couldn't find a player to move from team\n");
1088 // smallest team gains a member
1089 if(smallestteam == 1)
1093 else if(smallestteam == 2)
1097 else if(smallestteam == 3)
1101 else if(smallestteam == 4)
1107 bprint("warning: destination team invalid\n");
1110 // source team loses a member
1111 if(source_team == 1)
1115 else if(source_team == 2)
1119 else if(source_team == 3)
1123 else if(source_team == 4)
1129 bprint("warning: source team invalid\n");
1133 // move the player to the new team
1134 TeamchangeFrags(selected);
1135 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1137 if(selected.deadflag == DEAD_NO)
1138 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1139 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1142 float lastRebalanceInfo;
1143 void CauseRebalance(float source_team, float howmany_toomany)
1148 if(IsTeamBalanceForced() == 1)
1150 bprint("Rebalancing Teams\n");
1151 ShufflePlayerOutOfTeam(source_team);
1155 if(howmany_toomany < cvar("g_balance_teams_complain"))
1157 if(time < lastRebalanceInfo + 90)
1159 lastRebalanceInfo = time;
1160 if(source_team == 1)
1161 steam = COLOR_TEAM1;
1162 else if(source_team == 2)
1163 steam = COLOR_TEAM2;
1164 else if(source_team == 3)
1165 steam = COLOR_TEAM3;
1166 else if(source_team == 4)
1167 steam = COLOR_TEAM4;
1168 ServerConsoleEcho(strcat("Team ", ftos(source_team), " too large, complaining."), TRUE);
1169 FOR_EACH_REALPLAYER(head)
1171 if(head.team == steam)
1173 sprint(head, "\{1}\{13}^3SERVER NOTICE:^7 One of you please change teams!\n");
1174 centerprint_atprio(head, CENTERPRIO_REBALANCE, "^3SERVER NOTICE:\n\n^7Someone of you please change teams!");
1180 // part of g_balance_teams_force
1181 // occasionally perform an audit of the teams to make
1182 // sure they're more or less balanced in player count.
1185 float numplayers, numteams, smallest, toomany;
1187 balance = IsTeamBalanceForced();
1191 if(audit_teams_time > time)
1194 audit_teams_time = time + 4 + random();
1196 // bprint("Auditing teams\n");
1198 CheckAllowedTeams(world);
1199 GetTeamCounts(world);
1202 numteams = numplayers = smallest = 0;
1205 numteams = numteams + 1;
1206 numplayers = numplayers + c1;
1211 numteams = numteams + 1;
1212 numplayers = numplayers + c2;
1218 numteams = numteams + 1;
1219 numplayers = numplayers + c3;
1225 numteams = numteams + 1;
1226 numplayers = numplayers + c4;
1232 return; // no players to move around
1234 return; // don't bother shuffling if for some reason there aren't any teams
1236 toomany = smallest + 1;
1238 if(c1 && c1 > toomany)
1239 CauseRebalance(1, c1 - toomany);
1240 if(c2 && c2 > toomany)
1241 CauseRebalance(2, c2 - toomany);
1242 if(c3 && c3 > toomany)
1243 CauseRebalance(3, c3 - toomany);
1244 if(c4 && c4 > toomany)
1245 CauseRebalance(4, c4 - toomany);
1247 // if teams are still unbalanced, balance them further in the next audit,
1248 // which will happen sooner (keep doing rapid audits until things are in order)
1249 audit_teams_time = time + 0.7 + random()*0.3;
1254 /*void(entity e, float first) UpdateTeamScore =
1259 WriteByte (MSG_ALL, SVC_UPDATENAME);
1260 WriteByte (MSG_ALL, clientno);
1261 WriteString (MSG_ALL, e.netname);
1263 WriteByte (MSG_ALL, SVC_UPDATECOLORS);
1264 WriteByte (MSG_ALL, clientno);
1265 WriteByte (MSG_ALL, e.b_shirt * 16 + who.b_pants);
1268 WriteByte (MSG_ALL, SVC_UPDATEFRAGS);
1269 WriteByte (MSG_ALL, clientno);
1270 WriteShort (MSG_ALL, e.frags + 10000);
1276 // code from here on is just to support maps that don't have team entities
1277 void tdm_spawnteam (string teamname, float teamcolor)
1281 e.classname = "tdm_team";
1282 e.netname = teamname;
1287 // spawn some default teams if the map is not set up for tdm
1288 void() tdm_spawnteams =
1292 numteams = cvar("g_tdm_teams");
1294 tdm_spawnteam("Red", COLOR_TEAM1-1);
1295 tdm_spawnteam("Blue", COLOR_TEAM2-1);
1297 tdm_spawnteam("Yellow", COLOR_TEAM3-1);
1299 tdm_spawnteam("Pink", COLOR_TEAM4-1);
1302 void() tdm_delayedinit =
1304 self.think = SUB_Remove;
1305 self.nextthink = time;
1306 // if no teams are found, spawn defaults
1307 if (find(world, classname, "tdm_team") == world)
1315 e.think = tdm_delayedinit;
1316 e.nextthink = time + 0.1;