2 string cache_mutatormsg;
4 string cache_lastmutatormsg;
6 // client counts for each team
8 // # of bots on those teams
9 float cb1, cb2, cb3, cb4;
11 float audit_teams_time;
13 float IsTeamBalanceForced()
15 if(intermission_running)
16 return 0; // no rebalancing whatsoever please
19 if(cvar("g_campaign"))
21 if(!cvar("g_balance_teams_force"))
26 void TeamchangeFrags(entity e)
31 vector TeamColor(float teem)
36 return '1 0.0625 0.0625';
38 return '0.0625 0.0625 1';
48 string TeamName(float t)
50 return strcat(Team_ColorName(t), " Team");
52 string ColoredTeamName(float t)
54 return strcat(Team_ColorCode(t), Team_ColorName(t), " Team^7");
56 string TeamNoName(float t)
58 // fixme: Search for team entities and get their .netname's!
67 return "Neutral Team";
72 void runematch_init();
76 void LogTeamchange(entity pl)
79 if(!cvar("sv_eventlog"))
83 str = strcat(":team:", ftos(pl.playerid), ":");
84 str = strcat(str, ftos(pl.team));
85 GameLogEcho(str, FALSE);
90 cvar_set("g_dm", "0");
91 cvar_set("g_tdm", "0");
92 cvar_set("g_domination", "0");
93 cvar_set("g_ctf", "0");
94 cvar_set("g_runematch", "0");
95 cvar_set("g_lms", "0");
96 cvar_set("g_arena", "0");
97 cvar_set("g_keyhunt", "0");
98 cvar_set("g_assault", "0");
99 cvar_set("g_onslaught", "0");
100 cvar_set("teamplay", "0");
103 void ActivateTeamplay()
105 float teamplay_default;
106 teamplay_default = cvar("teamplay_default");
109 cvar_set("teamplay", ftos(teamplay_default));
111 cvar_set("teamplay", "3");
114 void InitGameplayMode()
116 float fraglimit_override, timelimit_override, capturelimit_override;
120 game = cvar("gamecfg"); // load game options
122 // game cvars get reset before map changes
123 // then map's cfg sets them as desired
125 // FIXME: also set a message or game mode name to print to players when the join
127 // set both here, gamemode can override it later
128 timelimit_override = cvar("timelimit_override");
129 fraglimit_override = cvar("fraglimit_override");
130 capturelimit_override = cvar("capturelimit_override");
132 if(game == GAME_DOMINATION || cvar("g_domination"))
135 game = GAME_DOMINATION;
136 cvar_set("g_domination", "1");
140 fraglimit_override = cvar("g_domination_point_limit");
142 gamemode_name = "Domination";
145 else if(game == GAME_CTF || cvar("g_ctf"))
149 cvar_set("g_ctf", "1");
153 fraglimit_override = cvar("g_ctf_capture_limit");
154 //no capture limit override here
156 gamemode_name = "Capture the Flag";
159 else if(game == GAME_RUNEMATCH || cvar("g_runematch"))
161 game = GAME_RUNEMATCH;
162 cvar_set("g_runematch", "1");
163 cvar_set("g_minstagib", "0");
165 if(cvar("deathmatch_force_teamplay"))
168 fraglimit_override = cvar("g_runematch_point_limit");
170 gamemode_name = "Rune Match";
176 else if(game == GAME_DEATHMATCH || game == GAME_TEAM_DEATHMATCH || cvar("g_tdm"))
178 if(!cvar("deathmatch"))
179 cvar_set("deathmatch", "1");
182 if(game == GAME_TEAM_DEATHMATCH || cvar("g_tdm") || cvar("deathmatch_force_teamplay"))
185 game = GAME_TEAM_DEATHMATCH;
186 gamemode_name = "Team Deathmatch";
189 cvar_set("g_tdm", "1");
194 game = GAME_DEATHMATCH;
195 gamemode_name = "Deathmatch";
199 fraglimit_override = cvar("fraglimit_override");
201 else if(game == GAME_ASSAULT || cvar("g_assault"))
205 gamemode_name = "Assault";
208 cvar_set("g_assault", "1");
210 else if(game == GAME_LMS || cvar("g_lms"))
214 cvar_set("g_lms", "1");
215 fraglimit_override = cvar("g_lms_lives_override");
216 if(fraglimit_override == 0)
217 fraglimit_override = -1;
218 gamemode_name = "Last Man Standing";
220 lms_lowest_lives = 999;
222 else if(game == GAME_ARENA || cvar("g_arena"))
226 cvar_set("g_arena", "1");
227 fraglimit_override = cvar("g_arena_point_limit");
228 maxspawned = cvar("g_arena_maxspawned");
231 arena_roundbased = cvar("g_arena_roundbased");
232 gamemode_name = "Arena";
235 else if(game == GAME_KEYHUNT || cvar("g_keyhunt"))
239 cvar_set("g_keyhunt", "1");
240 fraglimit_override = cvar("g_keyhunt_point_limit");
242 gamemode_name = "Key Hunt";
245 else if(game == GAME_ONSLAUGHT || cvar("g_onslaught"))
248 game = GAME_ONSLAUGHT;
249 cvar_set("g_onslaught", "1");
253 gamemode_name = "Onslaught";
258 // we can only assume...
260 game = GAME_DEATHMATCH;
261 cvar_set("g_dm", "1");
262 gamemode_name = "Deathmatch";
266 // those mutators rule each other out
267 if(cvar("g_minstagib"))
269 cvar_set("g_instagib", "0");
270 cvar_set("g_rocketarena", "0");
272 if(cvar("g_instagib"))
274 cvar_set("g_minstagib", "0");
275 cvar_set("g_rocketarena", "0");
277 if(cvar("g_rocketarena"))
279 cvar_set("g_instagib", "0");
280 cvar_set("g_minstagib", "0");
283 g_domination = cvar("g_domination");
284 g_ctf = cvar("g_ctf");
285 g_lms = cvar("g_lms");
286 g_tdm = cvar("g_tdm");
287 g_runematch = cvar("g_runematch");
288 g_keyhunt = cvar("g_keyhunt");
289 g_onslaught = cvar("g_onslaught");
290 g_assault = cvar("g_assault");
291 g_arena = cvar("g_arena");
293 cache_mutatormsg = strzone("");
294 cache_motd = strzone("");
295 cache_lastmutatormsg = strzone("");
296 cache_lastmotd = strzone("");
298 // enforce the server's universal frag/time limits
299 if(!cvar("g_campaign"))
301 if(fraglimit_override >= 0)
302 cvar_set("fraglimit", ftos(fraglimit_override));
303 if(timelimit_override >= 0)
304 cvar_set("timelimit", ftos(timelimit_override));
305 if(capturelimit_override >= 0)
306 cvar_set("capturelimit", ftos(capturelimit_override));
309 if (game == GAME_DOMINATION)//cvar("g_domination"))
311 else if (game == GAME_CTF)//cvar("g_ctf"))
313 else if (game == GAME_RUNEMATCH)//cvar("g_runematch"))
315 else if (game == GAME_TEAM_DEATHMATCH)//cvar("g_runematch"))
317 else if (game == GAME_KEYHUNT)//cvar("g_keyhunt"))
319 else if (game == GAME_ONSLAUGHT)
322 if(!scores_initialized)
326 CheckAllowedTeams(world);
327 ScoreInfo_Init((c1>=0) + (c2>=0) + (c3>=0) + (c4>=0)); // NOTE this assumes that if team 2 is allowed, 1 is too, and if 3 is, 2 is, and if 4 is, 3 is!
330 ScoreInfo_Init(0); // NOTE this assumes that if team 2 is allowed, 1 is too, and if 3 is, 2 is, and if 4 is, 3 is!
331 ScoreInfo_SetLabel_TeamScore (ST_SCORE, "score", SFL_SORT_PRIO_PRIMARY);
332 ScoreInfo_SetLabel_PlayerScore(SP_KILLS, "kills", 0);
333 ScoreInfo_SetLabel_PlayerScore(SP_DEATHS, "deaths", 0);
334 ScoreInfo_SetLabel_PlayerScore(SP_SUICIDES, "suicides", SFL_DECREASING);
335 ScoreInfo_SetLabel_PlayerScore(SP_SCORE, "score", SFL_SORT_PRIO_PRIMARY);
339 string GetClientVersionMessage(float v) {
340 local string versionmsg;
342 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";
343 // either that or someone wants to be funny
344 } else if (v != cvar("gameversion")) {
345 if(v < cvar("gameversion")) {
346 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
348 versionmsg = "^3This server is using an outdated Nexuiz version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
351 versionmsg = "^2client version and server version are compatible.^8";
357 void PrintWelcomeMessage(entity pl)
359 string s, mutator, modifications, padding;
361 /*if(self.welcomemessage_time > time)
363 self.welcomemessage_time = time + 0.8; */
365 if(self.cvar_scr_centertime == 0) return;
366 if( !(timeoutStatus >= 1 || (time < restart_countdown) ) ) { //really print the WelcomeMessage to the player every frame when timeout-seconds are shown or the game is restarted, to make sure that the shown number is accurate
367 if(self.welcomemessage_time > time) return;
368 self.welcomemessage_time = time + self.cvar_scr_centertime * 0.6;
371 if(cvar("g_campaign"))
373 centerprint(pl, campaign_message);
377 if(!self.BUTTON_INFO)
379 if(self.classname == "observer")
381 // FIXME this is broken for LMS, fix it
382 if(g_lms && self.frags <= 0 && self.frags > -666)
383 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"));
384 else if(g_lms && self.frags == -666)
385 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"));
387 else if(self.classname == "spectator")
389 // FIXME this is broken for LMS, fix it
390 if ((g_lms && self.frags < 1) || g_arena)
391 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"));
393 local string specString;
394 specString = 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");
396 if(time < restart_countdown) //also show the countdown when being a spectator
397 specString = strcat(specString, "\n\n^1Game starts in ", ftos(restartAnnouncer.cnt + 1), " seconds^7");
398 else if (timeoutStatus != 0)
399 specString = strcat(specString, "\n\n", getTimeoutText(1));
400 return centerprint_atprio(self, CENTERPRIO_SPAM, specString);
406 mutator = "^2Minstagib ^1";
408 mutator = "^2Instagib ^1";
409 else if(g_rocketarena)
410 mutator = "^2Rocketarena ^1";
412 mutator = "^2No Items Nexuiz ^1";
415 // to protect against unheedingly made changes
417 modifications = strcat(modifications, ", ");
419 modifications = "midair";
423 modifications = strcat(modifications, ", ");
425 modifications = strcat(modifications, "vampire");
427 if(g_laserguided_missile) {
429 modifications = strcat(modifications, ", ");
431 modifications = strcat(modifications, "laser-guided-missiles");
435 modifications = strcat(modifications, ", ");
437 modifications = strcat(modifications, "Tournament");
440 local string versionmessage;
441 versionmessage = GetClientVersionMessage(self.version);
443 s = strcat(s, NEWLINES, "This is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
444 s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
446 if(modifications != "")
447 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
450 if(!tourneyInMatchStage)
451 s = strcat(s, "\n^8The game is currently in ^3warmup-stage ^8!\n");
453 s = strcat(s, "\n^8The game is currently in ^3match-stage ^8!\n");
456 if(time < restart_countdown)
457 s = strcat(s, "\n^1Game starts in ", ftos(restartAnnouncer.cnt + 1), " seconds^7");
459 if(timeoutStatus != 0)
460 s = strcat(s, "\n\n", getTimeoutText(1));
462 if (g_grappling_hook)
463 s = strcat(s, "\n\n^8grappling hook is enabled, press 'e' to use it\n");
465 if(cache_lastmutatormsg != cvar_string("g_mutatormsg"))
467 if(cache_lastmutatormsg)
468 strunzone(cache_lastmutatormsg);
470 strunzone(cache_mutatormsg);
471 cache_lastmutatormsg = strzone(cvar_string("g_mutatormsg"));
472 cache_mutatormsg = strzone(wordwrap(cache_lastmutatormsg, 50));
475 if (cache_mutatormsg != "") {
476 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
479 if(cache_lastmotd != cvar_string("sv_motd"))
482 strunzone(cache_lastmotd);
484 strunzone(cache_motd);
485 cache_lastmotd = strzone(cvar_string("sv_motd"));
486 cache_motd = strzone(wordwrap(cache_lastmotd, 50));
489 if (cache_motd != "") {
490 s = strcat(s, "\n\n^8MOTD: ^7", cache_motd);
494 if(cvar("fraglimit"))
497 if(cvar("timelimit"))
500 s = strcat(s, "\n^8frag limit: ^7", cvar_string("fraglimit"), padding);
502 if(cvar("timelimit"))
503 s = strcat(s, "\n^8time limit: ^7", cvar_string("timelimit"), " minutes");
510 void SetPlayerColors(entity pl, float _color)
514 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
516 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
517 pl.clientcolors = 16*cl + cl;*/
520 pants = _color & 0x0F;
521 shirt = _color & 0xF0;
525 setcolor(pl, 16*pants + pants);
527 setcolor(pl, shirt + pants);
531 void SetPlayerTeam(entity pl, float t, float s, float noprint)
536 _color = COLOR_TEAM4 - 1;
538 _color = COLOR_TEAM3 - 1;
540 _color = COLOR_TEAM2 - 1;
542 _color = COLOR_TEAM1 - 1;
544 SetPlayerColors(pl,_color);
546 if(!noprint && t != s)
548 //bprint(pl.netname, " has changed to ", TeamNoName(t), "\n");
549 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
561 // set c1...c4 to show what teams are allowed
562 void CheckAllowedTeams (entity for_whom)
571 c1 = c2 = c3 = c4 = -1;
572 cb1 = cb2 = cb3 = cb4 = 0;
574 // onslaught is special
577 head = findchain(classname, "onslaught_generator");
580 if (head.team == COLOR_TEAM1) c1 = 0;
581 if (head.team == COLOR_TEAM2) c2 = 0;
582 if (head.team == COLOR_TEAM3) c3 = 0;
583 if (head.team == COLOR_TEAM4) c4 = 0;
590 teament_name = "dom_team";
592 teament_name = "ctf_team";
594 teament_name = "tdm_team";
597 c1 = c2 = 0; // Assault always has 2 teams
602 // cover anything else by treating it like tdm with no teams spawned
606 dm = cvar("g_tdm_teams");
608 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
612 c1 = c2 = c3 = c4 = 0;
625 // first find out what teams are allowed
626 head = find(world, classname, teament_name);
629 if(!(g_domination && head.netname == ""))
631 if(head.team == COLOR_TEAM1)
635 if(head.team == COLOR_TEAM2)
639 if(head.team == COLOR_TEAM3)
643 if(head.team == COLOR_TEAM4)
648 head = find(head, classname, teament_name);
653 if(cvar("bot_vs_human") > 0)
656 if(clienttype(for_whom) == CLIENTTYPE_BOT)
661 else if(cvar("bot_vs_human") < 0)
664 if(clienttype(for_whom) == CLIENTTYPE_BOT)
672 float PlayerValue(entity p)
674 if(IsTeamBalanceForced() == 1)
679 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
680 // teams that are allowed will now have their player counts stored in c1...c4
681 void GetTeamCounts(entity ignore)
685 // now count how many players are on each team already
687 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
688 // also remember the lowest-scoring player
690 FOR_EACH_PLAYER(head)
692 if(head != ignore)// && head.netname != "")
694 value = PlayerValue(head);
695 if(clienttype(head) == CLIENTTYPE_BOT)
699 if(head.team == COLOR_TEAM1)
707 if(head.team == COLOR_TEAM2)
715 if(head.team == COLOR_TEAM3)
723 if(head.team == COLOR_TEAM4)
735 // returns # of smallest team (1, 2, 3, 4)
736 // NOTE: Assumes CheckAllowedTeams has already been called!
737 float FindSmallestTeam(entity pl, float ignore_pl)
739 float totalteams, smallestteam, smallestteam_count, smallestteam_score, balance_type;
742 // find out what teams are available
743 //CheckAllowedTeams();
745 // make sure there are at least 2 teams to join
747 totalteams = totalteams + 1;
749 totalteams = totalteams + 1;
751 totalteams = totalteams + 1;
753 totalteams = totalteams + 1;
755 if(cvar("bot_vs_human"))
761 error("Too few teams available for domination\n");
763 error("Too few teams available for ctf\n");
765 error("Too few teams available for key hunt\n");
767 error("Too few teams available for team deathmatch\n");
770 // count how many players are in each team
774 GetTeamCounts(world);
776 // c1...c4 now have counts of each team
777 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
780 smallestteam_count = 999999999;
781 smallestteam_score = 999999999;
783 // 2 gives priority to what team you're already on, 1 goes in order
784 // 2 doesn't seem to work though...
788 //if(pl.classname != "player")
789 if(clienttype(pl) != CLIENTTYPE_BOT)
791 c1 -= cb1 * 255.0/256;
792 c2 -= cb2 * 255.0/256;
793 c3 -= cb3 * 255.0/256;
794 c4 -= cb4 * 255.0/256;
797 if(balance_type == 1)
799 if(c1 >= 0 && (c1 < smallestteam_count || (c1 <= smallestteam_count && team1_score < smallestteam_score)))
802 smallestteam_count = c1;
803 smallestteam_score = team1_score;
805 if(c2 >= 0 && (c2 < smallestteam_count || (c2 <= smallestteam_count && team2_score < smallestteam_score)))
808 smallestteam_count = c2;
809 smallestteam_score = team2_score;
811 if(c3 >= 0 && (c3 < smallestteam_count || (c3 <= smallestteam_count && team3_score < smallestteam_score)))
814 smallestteam_count = c3;
815 smallestteam_score = team3_score;
817 if(c4 >= 0 && (c4 < smallestteam_count || (c4 <= smallestteam_count && team4_score < smallestteam_score)))
820 smallestteam_count = c4;
821 smallestteam_score = team4_score;
826 if(c1 >= 0 && (c1 < smallestteam_count ||
827 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
830 smallestteam_count = c1;
832 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
833 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
836 smallestteam_count = c2;
838 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
839 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
842 smallestteam_count = c3;
844 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
845 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
848 smallestteam_count = c4;
855 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
857 float smallest, selectedteam;
859 // don't join a team if we're not playing a team game
860 if(!cvar("teamplay") && !g_domination && !g_ctf && !g_keyhunt)
863 // find out what teams are available
864 CheckAllowedTeams(pl);
869 if(cvar("g_domination_default_teams") < 3)
871 if(cvar("g_domination_default_teams") < 4)
875 // if we don't care what team he ends up on, put him on whatever team he entered as.
876 // if he's not on a valid team, then let other code put him on the smallest team
879 if( c1 >= 0 && pl.team == COLOR_TEAM1)
880 selectedteam = pl.team;
881 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
882 selectedteam = pl.team;
883 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
884 selectedteam = pl.team;
885 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
886 selectedteam = pl.team;
891 if(!only_return_best)
893 SetPlayerColors(pl, selectedteam - 1);
898 // otherwise end up on the smallest team (handled below)
901 smallest = FindSmallestTeam(pl, TRUE);
904 if(!only_return_best)
906 TeamchangeFrags(self);
909 SetPlayerColors(pl, COLOR_TEAM1 - 1);
911 else if(smallest == 2)
913 SetPlayerColors(pl, COLOR_TEAM2 - 1);
915 else if(smallest == 3)
917 SetPlayerColors(pl, COLOR_TEAM3 - 1);
919 else if(smallest == 4)
921 SetPlayerColors(pl, COLOR_TEAM4 - 1);
925 error("smallest team: invalid team\n");
928 if(pl.deadflag == DEAD_NO)
929 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
935 //void() ctf_playerchanged;
936 void SV_ChangeTeam(float _color)
938 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
940 // in normal deathmatch we can just apply the color and we're done
941 if(!cvar("teamplay")) {
942 SetPlayerColors(self, _color);
946 scolor = self.clientcolors & 0x0F;
947 dcolor = _color & 0x0F;
949 if(scolor == COLOR_TEAM1 - 1)
951 else if(scolor == COLOR_TEAM2 - 1)
953 else if(scolor == COLOR_TEAM3 - 1)
955 else if(scolor == COLOR_TEAM4 - 1)
957 if(dcolor == COLOR_TEAM1 - 1)
959 else if(dcolor == COLOR_TEAM2 - 1)
961 else if(dcolor == COLOR_TEAM3 - 1)
963 else if(dcolor == COLOR_TEAM4 - 1)
966 CheckAllowedTeams(self);
968 if(dteam == 1 && c1 < 0) dteam = 4;
969 if(dteam == 4 && c4 < 0) dteam = 3;
970 if(dteam == 3 && c3 < 0) dteam = 2;
971 if(dteam == 2 && c2 < 0) dteam = 1;
973 // not changing teams
976 //bprint("same team change\n");
977 SetPlayerTeam(self, dteam, steam, TRUE);
983 if(cvar("g_campaign") || cvar("g_changeteam_banned"))
985 sprint(self, "Team changes not allowed\n");
986 return; // changing teams is not allowed
989 if(!cvar("g_campaign") && cvar("g_balance_teams_prevent_imbalance"))
991 // only allow changing to a smaller or equal size team
993 // find out what teams are available
994 //CheckAllowedTeams();
995 // count how many players on each team
996 GetTeamCounts(world);
999 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
1004 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
1009 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
1014 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
1021 sprint(self, "Cannot change to an invalid team\n");
1026 // get starting team
1027 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
1029 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
1031 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
1033 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
1036 if(scount) // started at a valid, nonempty team
1038 // check if we're trying to change to a larger team that doens't have bots to swap with
1039 if(dcount >= scount && dbotcount <= 0)
1041 sprint(self, "Cannot change to a larger team\n");
1042 return; // can't change to a larger team
1048 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
1050 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1052 // reduce frags during a team change
1053 TeamchangeFrags(self);
1056 SetPlayerTeam(self, dteam, steam, FALSE);
1058 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1060 // kill player when changing teams
1061 if(self.deadflag == DEAD_NO)
1062 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
1064 //ctf_playerchanged();
1067 void ShufflePlayerOutOfTeam (float source_team)
1069 float smallestteam, smallestteam_count, steam;
1070 float lowest_bot_score, lowest_player_score;
1071 entity head, lowest_bot, lowest_player, selected;
1074 smallestteam_count = 999999999;
1076 if(c1 >= 0 && c1 < smallestteam_count)
1079 smallestteam_count = c1;
1081 if(c2 >= 0 && c2 < smallestteam_count)
1084 smallestteam_count = c2;
1086 if(c3 >= 0 && c3 < smallestteam_count)
1089 smallestteam_count = c3;
1091 if(c4 >= 0 && c4 < smallestteam_count)
1094 smallestteam_count = c4;
1099 bprint("warning: no smallest team\n");
1103 if(source_team == 1)
1104 steam = COLOR_TEAM1;
1105 else if(source_team == 2)
1106 steam = COLOR_TEAM2;
1107 else if(source_team == 3)
1108 steam = COLOR_TEAM3;
1109 else if(source_team == 4)
1110 steam = COLOR_TEAM4;
1113 lowest_bot_score = 999999999;
1114 lowest_player = world;
1115 lowest_player_score = 999999999;
1117 // find the lowest-scoring player & bot of that team
1118 // FIXME this is broken in new scoring system, add a helper to find the worst bot of a team
1119 FOR_EACH_PLAYER(head)
1121 if(head.team == steam)
1125 if(head.frags < lowest_bot_score)
1128 lowest_bot_score = head.frags;
1133 if(head.frags < lowest_player_score)
1135 lowest_player = head;
1136 lowest_player_score = head.frags;
1142 // prefers to move a bot...
1143 if(lowest_bot != world)
1144 selected = lowest_bot;
1145 // but it will move a player if it has to
1147 selected = lowest_player;
1148 // don't do anything if it couldn't find anyone
1151 bprint("warning: couldn't find a player to move from team\n");
1155 // smallest team gains a member
1156 if(smallestteam == 1)
1160 else if(smallestteam == 2)
1164 else if(smallestteam == 3)
1168 else if(smallestteam == 4)
1174 bprint("warning: destination team invalid\n");
1177 // source team loses a member
1178 if(source_team == 1)
1182 else if(source_team == 2)
1186 else if(source_team == 3)
1190 else if(source_team == 4)
1196 bprint("warning: source team invalid\n");
1200 // move the player to the new team
1201 TeamchangeFrags(selected);
1202 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1204 if(selected.deadflag == DEAD_NO)
1205 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1206 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1209 float lastRebalanceInfo;
1210 void CauseRebalance(float source_team, float howmany_toomany)
1215 if(IsTeamBalanceForced() == 1)
1217 bprint("Rebalancing Teams\n");
1218 ShufflePlayerOutOfTeam(source_team);
1222 if(howmany_toomany < cvar("g_balance_teams_complain"))
1224 if(time < lastRebalanceInfo + 90)
1226 lastRebalanceInfo = time;
1227 if(source_team == 1)
1228 steam = COLOR_TEAM1;
1229 else if(source_team == 2)
1230 steam = COLOR_TEAM2;
1231 else if(source_team == 3)
1232 steam = COLOR_TEAM3;
1233 else if(source_team == 4)
1234 steam = COLOR_TEAM4;
1235 ServerConsoleEcho(strcat("Team ", ftos(source_team), " too large, complaining."), TRUE);
1236 FOR_EACH_REALPLAYER(head)
1238 if(head.team == steam)
1240 sprint(head, "\{1}\{13}^3SERVER NOTICE:^7 One of you please change teams!\n");
1241 centerprint_atprio(head, CENTERPRIO_REBALANCE, "^3SERVER NOTICE:\n\n^7Someone of you please change teams!");
1247 // part of g_balance_teams_force
1248 // occasionally perform an audit of the teams to make
1249 // sure they're more or less balanced in player count.
1252 float numplayers, numteams, smallest, toomany;
1254 balance = IsTeamBalanceForced();
1258 if(audit_teams_time > time)
1261 audit_teams_time = time + 4 + random();
1263 // bprint("Auditing teams\n");
1265 CheckAllowedTeams(world);
1266 GetTeamCounts(world);
1269 numteams = numplayers = smallest = 0;
1272 numteams = numteams + 1;
1273 numplayers = numplayers + c1;
1278 numteams = numteams + 1;
1279 numplayers = numplayers + c2;
1285 numteams = numteams + 1;
1286 numplayers = numplayers + c3;
1292 numteams = numteams + 1;
1293 numplayers = numplayers + c4;
1299 return; // no players to move around
1301 return; // don't bother shuffling if for some reason there aren't any teams
1303 toomany = smallest + 1;
1305 if(c1 && c1 > toomany)
1306 CauseRebalance(1, c1 - toomany);
1307 if(c2 && c2 > toomany)
1308 CauseRebalance(2, c2 - toomany);
1309 if(c3 && c3 > toomany)
1310 CauseRebalance(3, c3 - toomany);
1311 if(c4 && c4 > toomany)
1312 CauseRebalance(4, c4 - toomany);
1314 // if teams are still unbalanced, balance them further in the next audit,
1315 // which will happen sooner (keep doing rapid audits until things are in order)
1316 audit_teams_time = time + 0.7 + random()*0.3;
1321 /*void UpdateTeamScore(entity e, float first)
1326 WriteByte (MSG_ALL, SVC_UPDATENAME);
1327 WriteByte (MSG_ALL, clientno);
1328 WriteString (MSG_ALL, e.netname);
1330 WriteByte (MSG_ALL, SVC_UPDATECOLORS);
1331 WriteByte (MSG_ALL, clientno);
1332 WriteByte (MSG_ALL, e.b_shirt * 16 + who.b_pants);
1335 WriteByte (MSG_ALL, SVC_UPDATEFRAGS);
1336 WriteByte (MSG_ALL, clientno);
1337 WriteShort (MSG_ALL, e.frags + 10000);
1343 // code from here on is just to support maps that don't have team entities
1344 void tdm_spawnteam (string teamname, float teamcolor)
1348 e.classname = "tdm_team";
1349 e.netname = teamname;
1354 // spawn some default teams if the map is not set up for tdm
1355 void tdm_spawnteams()
1359 numteams = cvar("g_tdm_teams");
1361 tdm_spawnteam("Red", COLOR_TEAM1-1);
1362 tdm_spawnteam("Blue", COLOR_TEAM2-1);
1364 tdm_spawnteam("Yellow", COLOR_TEAM3-1);
1366 tdm_spawnteam("Pink", COLOR_TEAM4-1);
1369 void tdm_delayedinit()
1371 self.think = SUB_Remove;
1372 self.nextthink = time;
1373 // if no teams are found, spawn defaults
1374 if (find(world, classname, "tdm_team") == world)
1382 e.think = tdm_delayedinit;
1383 e.nextthink = time + 0.1;