1 string cache_mutatormsg;
2 string cache_lastmutatormsg;
4 // client counts for each team
6 // # of bots on those teams
7 float cb1, cb2, cb3, cb4;
9 //float audit_teams_time;
11 void TeamchangeFrags(entity e)
19 void LogTeamchange(float player_id, float team_number, float type)
21 if(!autocvar_sv_eventlog)
27 GameLogEcho(strcat(":team:", ftos(player_id), ":", ftos(team_number), ":", ftos(type)));
30 void default_delayedinit()
32 if(!scores_initialized)
36 void ActivateTeamplay()
38 serverflags |= SERVERFLAG_TEAMPLAY;
42 void InitGameplayMode()
44 float fraglimit_override, timelimit_override, leadlimit_override, qualifying_override;
46 qualifying_override = -1;
50 // find out good world mins/maxs bounds, either the static bounds found by looking for solid, or the mapinfo specified bounds
55 MapInfo_LoadMapSettings(mapname);
57 serverflags &= ~SERVERFLAG_TEAMPLAY;
59 if (!cvar_value_issafe(world.fog))
61 print("The current map contains a potentially harmful fog setting, ignored\n");
62 world.fog = string_null;
64 if(MapInfo_Map_fog != "")
65 if(MapInfo_Map_fog == "none")
66 world.fog = string_null;
68 world.fog = strzone(MapInfo_Map_fog);
69 clientstuff = strzone(MapInfo_Map_clientstuff);
73 // set both here, gamemode can override it later
74 timelimit_override = autocvar_timelimit_override;
75 fraglimit_override = autocvar_fraglimit_override;
76 leadlimit_override = autocvar_leadlimit_override;
77 gamemode_name = MapInfo_Type_ToText(MapInfo_LoadedGametype);
87 if(autocvar_g_tdm_team_spawns)
88 have_team_spawns = -1; // request team spawns
94 fraglimit_override = autocvar_g_domination_point_limit;
95 leadlimit_override = autocvar_g_domination_point_leadlimit;
96 MUTATOR_ADD(gamemode_domination);
97 have_team_spawns = -1; // request team spawns
103 fraglimit_override = autocvar_capturelimit_override;
104 leadlimit_override = autocvar_captureleadlimit_override;
105 MUTATOR_ADD(gamemode_ctf);
106 have_team_spawns = -1; // request team spawns
111 fraglimit_override = autocvar_g_lms_lives_override;
112 leadlimit_override = 0; // not supported by LMS
113 if(fraglimit_override == 0)
114 fraglimit_override = -1;
115 MUTATOR_ADD(gamemode_lms);
121 fraglimit_override = autocvar_g_ca_point_limit;
122 leadlimit_override = autocvar_g_ca_point_leadlimit;
123 MUTATOR_ADD(gamemode_ca);
129 fraglimit_override = autocvar_g_keyhunt_point_limit;
130 leadlimit_override = autocvar_g_keyhunt_point_leadlimit;
131 MUTATOR_ADD(gamemode_keyhunt);
137 fraglimit_override = autocvar_g_freezetag_point_limit;
138 leadlimit_override = autocvar_g_freezetag_point_leadlimit;
139 MUTATOR_ADD(gamemode_freezetag);
145 MUTATOR_ADD(gamemode_assault);
146 have_team_spawns = -1; // request team spawns
152 have_team_spawns = -1; // request team spawns
153 MUTATOR_ADD(gamemode_onslaught);
159 if(autocvar_g_race_teams)
162 race_teams = bound(2, autocvar_g_race_teams, 4);
163 have_team_spawns = -1; // request team spawns
168 qualifying_override = autocvar_g_race_qualifying_timelimit_override;
169 fraglimit_override = autocvar_g_race_laps_limit;
170 leadlimit_override = 0; // currently not supported by race
175 g_race_qualifying = 1;
176 fraglimit_override = 0;
177 leadlimit_override = 0;
182 fraglimit_override = autocvar_g_nexball_goallimit;
183 leadlimit_override = autocvar_g_nexball_goalleadlimit;
185 have_team_spawns = -1; // request team spawns
186 MUTATOR_ADD(gamemode_nexball);
191 MUTATOR_ADD(gamemode_keepaway);
197 cache_mutatormsg = strzone("");
198 cache_lastmutatormsg = strzone("");
200 // enforce the server's universal frag/time limits
201 if(!autocvar_g_campaign)
203 if(fraglimit_override >= 0)
204 cvar_set("fraglimit", ftos(fraglimit_override));
205 if(timelimit_override >= 0)
206 cvar_set("timelimit", ftos(timelimit_override));
207 if(leadlimit_override >= 0)
208 cvar_set("leadlimit", ftos(leadlimit_override));
209 if(qualifying_override >= 0)
210 cvar_set("g_race_qualifying_timelimit", ftos(qualifying_override));
215 // we need to find out the correct value for g_race_qualifying
216 if(autocvar_g_campaign)
218 g_race_qualifying = 1;
220 else if(!autocvar_g_campaign && autocvar_g_race_qualifying_timelimit > 0)
222 g_race_qualifying = 2;
223 race_fraglimit = autocvar_fraglimit;
224 race_leadlimit = autocvar_leadlimit;
225 race_timelimit = autocvar_timelimit;
226 cvar_set("fraglimit", "0");
227 cvar_set("leadlimit", "0");
228 cvar_set("timelimit", ftos(autocvar_g_race_qualifying_timelimit));
231 g_race_qualifying = 0;
236 if(g_race_qualifying)
237 independent_players = 1;
242 InitializeEntity(world, default_delayedinit, INITPRIO_GAMETYPE_FALLBACK);
245 string GetClientVersionMessage() {
247 if (self.version_mismatch) {
248 if(self.version < autocvar_gameversion) {
249 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
251 versionmsg = "^3This server is using an outdated Xonotic version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
254 versionmsg = "^2client version and server version are compatible.^8";
259 string getwelcomemessage(void)
261 string s, modifications, motd;
264 MUTATOR_CALLHOOK(BuildMutatorsPrettyString);
265 modifications = ret_string;
269 if(g_weaponarena_random)
270 modifications = strcat(modifications, ", ", ftos(g_weaponarena_random), " of ", g_weaponarena_list, " Arena");
272 modifications = strcat(modifications, ", ", g_weaponarena_list, " Arena");
274 if(autocvar_g_start_weapon_laser == 0)
275 modifications = strcat(modifications, ", No start weapons");
276 if(autocvar_sv_gravity < 800)
277 modifications = strcat(modifications, ", Low gravity");
278 if(g_cloaked && !g_cts)
279 modifications = strcat(modifications, ", Cloaked");
281 modifications = strcat(modifications, ", Hook");
282 if(g_weapon_stay && !g_cts)
283 modifications = strcat(modifications, ", Weapons stay");
285 modifications = strcat(modifications, ", Jet pack");
286 if(autocvar_g_powerups == 0)
287 modifications = strcat(modifications, ", No powerups");
288 if(autocvar_g_powerups > 0)
289 modifications = strcat(modifications, ", Powerups");
290 modifications = substring(modifications, 2, strlen(modifications) - 2);
292 string versionmessage;
293 versionmessage = GetClientVersionMessage();
295 s = strcat("This is Xonotic ", autocvar_g_xonoticversion, "\n", versionmessage);
296 s = strcat(s, "^8\n\nmatch type is ^1", gamemode_name, "^8\n");
298 if(modifications != "")
299 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
301 if (g_grappling_hook)
302 s = strcat(s, "\n\n^3grappling hook^8 is enabled, press 'e' to use it\n");
304 if(cache_lastmutatormsg != autocvar_g_mutatormsg)
306 if(cache_lastmutatormsg)
307 strunzone(cache_lastmutatormsg);
309 strunzone(cache_mutatormsg);
310 cache_lastmutatormsg = strzone(autocvar_g_mutatormsg);
311 cache_mutatormsg = strzone(cache_lastmutatormsg);
314 if (cache_mutatormsg != "") {
315 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
318 motd = autocvar_sv_motd;
320 s = strcat(s, "\n\n^8MOTD: ^7", strreplace("\\n", "\n", motd));
325 void SetPlayerColors(entity pl, float _color)
329 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
331 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
332 pl.clientcolors = 16*cl + cl;*/
335 pants = _color & 0x0F;
336 shirt = _color & 0xF0;
340 setcolor(pl, 16*pants + pants);
342 setcolor(pl, shirt + pants);
346 void SetPlayerTeam(entity pl, float t, float s, float noprint)
351 _color = NUM_TEAM_4 - 1;
353 _color = NUM_TEAM_3 - 1;
355 _color = NUM_TEAM_2 - 1;
357 _color = NUM_TEAM_1 - 1;
359 SetPlayerColors(pl,_color);
362 LogTeamchange(pl.playerid, pl.team, 3); // log manual team join
365 bprint(pl.netname, "^7 has changed from ", Team_NumberToColoredFullName(s), "^7 to ", Team_NumberToColoredFullName(t), "\n");
370 // set c1...c4 to show what teams are allowed
371 void CheckAllowedTeams (entity for_whom)
377 c1 = c2 = c3 = c4 = -1;
378 cb1 = cb2 = cb3 = cb4 = 0;
380 teament_name = string_null;
383 // onslaught is special
384 head = findchain(classname, "onslaught_generator");
387 if (head.team == NUM_TEAM_1) c1 = 0;
388 if (head.team == NUM_TEAM_2) c2 = 0;
389 if (head.team == NUM_TEAM_3) c3 = 0;
390 if (head.team == NUM_TEAM_4) c4 = 0;
394 else if(g_domination)
395 teament_name = "dom_team";
397 teament_name = "ctf_team";
399 teament_name = "tdm_team";
401 teament_name = "nexball_team";
403 c1 = c2 = 0; // Assault always has 2 teams
406 // cover anything else by treating it like tdm with no teams spawned
413 MUTATOR_CALLHOOK(GetTeamCount);
417 c1 = c2 = c3 = c4 = 0;
424 // find out what teams are allowed if necessary
427 head = find(world, classname, teament_name);
430 if(!(g_domination && head.netname == ""))
432 if(head.team == NUM_TEAM_1)
434 else if(head.team == NUM_TEAM_2)
436 else if(head.team == NUM_TEAM_3)
438 else if(head.team == NUM_TEAM_4)
441 head = find(head, classname, teament_name);
445 // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
447 if(autocvar_bot_vs_human && for_whom)
449 if(autocvar_bot_vs_human > 0)
452 if(IS_BOT_CLIENT(for_whom))
460 if(IS_BOT_CLIENT(for_whom))
467 // if player has a forced team, ONLY allow that one
468 if(self.team_forced == NUM_TEAM_1 && c1 >= 0)
470 else if(self.team_forced == NUM_TEAM_2 && c2 >= 0)
472 else if(self.team_forced == NUM_TEAM_3 && c3 >= 0)
474 else if(self.team_forced == NUM_TEAM_4 && c4 >= 0)
478 float PlayerValue(entity p)
481 // FIXME: it always returns 1...
484 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
485 // teams that are allowed will now have their player counts stored in c1...c4
486 void GetTeamCounts(entity ignore)
490 // now count how many players are on each team already
492 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
493 // also remember the lowest-scoring player
495 FOR_EACH_CLIENT(head)
500 else if(head.team_forced > 0)
501 t = head.team_forced; // reserve the spot
504 if(head != ignore)// && head.netname != "")
506 value = PlayerValue(head);
507 if(IS_BOT_CLIENT(head))
546 // if the player who has a forced team has not joined yet, reserve the spot
547 if(autocvar_g_campaign)
549 switch(autocvar_g_campaign_forceteam)
551 case 1: if(c1 == cb1) ++c1; break;
552 case 2: if(c2 == cb2) ++c2; break;
553 case 3: if(c3 == cb3) ++c3; break;
554 case 4: if(c4 == cb4) ++c4; break;
559 float TeamSmallerEqThanTeam(float ta, float tb, entity e)
561 // we assume that CheckAllowedTeams and GetTeamCounts have already been called
563 float ca = -1, cb = -1, cba = 0, cbb = 0, sa = 0, sb = 0;
567 case 1: ca = c1; cba = cb1; sa = team1_score; break;
568 case 2: ca = c2; cba = cb2; sa = team2_score; break;
569 case 3: ca = c3; cba = cb3; sa = team3_score; break;
570 case 4: ca = c4; cba = cb4; sa = team4_score; break;
574 case 1: cb = c1; cbb = cb1; sb = team1_score; break;
575 case 2: cb = c2; cbb = cb2; sb = team2_score; break;
576 case 3: cb = c3; cbb = cb3; sb = team3_score; break;
577 case 4: cb = c4; cbb = cb4; sb = team4_score; break;
588 if(IS_REAL_CLIENT(e))
597 // keep teams alive (teams of size 0 always count as smaller, ignoring score)
613 // the more we're at the end of the match, the more take scores into account
614 f = bound(0, game_completion_ratio * autocvar_g_balance_teams_scorefactor, 1);
621 // returns # of smallest team (1, 2, 3, 4)
622 // NOTE: Assumes CheckAllowedTeams has already been called!
623 float FindSmallestTeam(entity pl, float ignore_pl)
628 // find out what teams are available
629 //CheckAllowedTeams();
631 // make sure there are at least 2 teams to join
633 totalteams = totalteams + 1;
635 totalteams = totalteams + 1;
637 totalteams = totalteams + 1;
639 totalteams = totalteams + 1;
641 if((autocvar_bot_vs_human || pl.team_forced > 0) && totalteams == 1)
646 if(autocvar_g_campaign && pl && IS_REAL_CLIENT(pl))
647 return 1; // special case for campaign and player joining
648 else if(g_domination)
649 error("Too few teams available for domination\n");
651 error("Too few teams available for ctf\n");
653 error("Too few teams available for key hunt\n");
655 error("Too few teams available for freeze tag\n");
657 error("Too few teams available for team deathmatch\n");
660 // count how many players are in each team
664 GetTeamCounts(world);
666 RandomSelection_Init();
669 if(TeamSmallerEqThanTeam(2, t, pl))
671 if(TeamSmallerEqThanTeam(3, t, pl))
673 if(TeamSmallerEqThanTeam(4, t, pl))
676 // now t is the minimum, or A minimum!
677 if(t == 1 || TeamSmallerEqThanTeam(1, t, pl))
678 RandomSelection_Add(world, 1, string_null, 1, 1);
679 if(t == 2 || TeamSmallerEqThanTeam(2, t, pl))
680 RandomSelection_Add(world, 2, string_null, 1, 1);
681 if(t == 3 || TeamSmallerEqThanTeam(3, t, pl))
682 RandomSelection_Add(world, 3, string_null, 1, 1);
683 if(t == 4 || TeamSmallerEqThanTeam(4, t, pl))
684 RandomSelection_Add(world, 4, string_null, 1, 1);
686 return RandomSelection_chosen_float;
689 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
691 float smallest, selectedteam;
693 // don't join a team if we're not playing a team game
697 // find out what teams are available
698 CheckAllowedTeams(pl);
700 // if we don't care what team he ends up on, put him on whatever team he entered as.
701 // if he's not on a valid team, then let other code put him on the smallest team
704 if( c1 >= 0 && pl.team == NUM_TEAM_1)
705 selectedteam = pl.team;
706 else if(c2 >= 0 && pl.team == NUM_TEAM_2)
707 selectedteam = pl.team;
708 else if(c3 >= 0 && pl.team == NUM_TEAM_3)
709 selectedteam = pl.team;
710 else if(c4 >= 0 && pl.team == NUM_TEAM_4)
711 selectedteam = pl.team;
717 if(!only_return_best)
719 SetPlayerColors(pl, selectedteam - 1);
721 // when JoinBestTeam is called by client.qc/ClientKill_Now_TeamChange the players team is -1 and thus skipped
722 // when JoinBestTeam is called by cl_client.qc/ClientConnect the player_id is 0 the log attempt is rejected
723 LogTeamchange(pl.playerid, pl.team, 99);
727 // otherwise end up on the smallest team (handled below)
730 smallest = FindSmallestTeam(pl, TRUE);
732 if(!only_return_best && !pl.bot_forced_team)
734 TeamchangeFrags(self);
737 SetPlayerColors(pl, NUM_TEAM_1 - 1);
739 else if(smallest == 2)
741 SetPlayerColors(pl, NUM_TEAM_2 - 1);
743 else if(smallest == 3)
745 SetPlayerColors(pl, NUM_TEAM_3 - 1);
747 else if(smallest == 4)
749 SetPlayerColors(pl, NUM_TEAM_4 - 1);
753 error("smallest team: invalid team\n");
756 LogTeamchange(pl.playerid, pl.team, 2); // log auto join
758 if(pl.deadflag == DEAD_NO)
759 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
765 //void() ctf_playerchanged;
766 void SV_ChangeTeam(float _color)
768 float scolor, dcolor, steam, dteam; //, dbotcount, scount, dcount;
770 // in normal deathmatch we can just apply the color and we're done
772 SetPlayerColors(self, _color);
776 scolor = self.clientcolors & 0x0F;
777 dcolor = _color & 0x0F;
779 if(scolor == NUM_TEAM_1 - 1)
781 else if(scolor == NUM_TEAM_2 - 1)
783 else if(scolor == NUM_TEAM_3 - 1)
785 else // if(scolor == NUM_TEAM_4 - 1)
787 if(dcolor == NUM_TEAM_1 - 1)
789 else if(dcolor == NUM_TEAM_2 - 1)
791 else if(dcolor == NUM_TEAM_3 - 1)
793 else // if(dcolor == NUM_TEAM_4 - 1)
796 CheckAllowedTeams(self);
798 if(dteam == 1 && c1 < 0) dteam = 4;
799 if(dteam == 4 && c4 < 0) dteam = 3;
800 if(dteam == 3 && c3 < 0) dteam = 2;
801 if(dteam == 2 && c2 < 0) dteam = 1;
803 // not changing teams
806 //bprint("same team change\n");
807 SetPlayerTeam(self, dteam, steam, TRUE);
811 if((autocvar_g_campaign) || (autocvar_g_changeteam_banned && self.wasplayer)) {
812 sprint(self, "Team changes not allowed\n");
813 return; // changing teams is not allowed
816 // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
817 if(autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
820 if(!TeamSmallerEqThanTeam(dteam, steam, self))
822 sprint(self, "Cannot change to a larger/better/shinier team\n");
827 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
829 if(IS_PLAYER(self) && steam != dteam)
831 // reduce frags during a team change
832 TeamchangeFrags(self);
835 SetPlayerTeam(self, dteam, steam, FALSE);
837 if(IS_PLAYER(self) && steam != dteam)
839 // kill player when changing teams
840 if(self.deadflag == DEAD_NO)
841 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
845 void ShufflePlayerOutOfTeam (float source_team)
847 float smallestteam, smallestteam_count, steam;
848 float lowest_bot_score, lowest_player_score;
849 entity head, lowest_bot, lowest_player, selected;
852 smallestteam_count = 999999999;
854 if(c1 >= 0 && c1 < smallestteam_count)
857 smallestteam_count = c1;
859 if(c2 >= 0 && c2 < smallestteam_count)
862 smallestteam_count = c2;
864 if(c3 >= 0 && c3 < smallestteam_count)
867 smallestteam_count = c3;
869 if(c4 >= 0 && c4 < smallestteam_count)
872 smallestteam_count = c4;
877 bprint("warning: no smallest team\n");
883 else if(source_team == 2)
885 else if(source_team == 3)
887 else // if(source_team == 4)
891 lowest_bot_score = 999999999;
892 lowest_player = world;
893 lowest_player_score = 999999999;
895 // find the lowest-scoring player & bot of that team
896 FOR_EACH_PLAYER(head)
898 if(head.team == steam)
902 if(head.totalfrags < lowest_bot_score)
905 lowest_bot_score = head.totalfrags;
910 if(head.totalfrags < lowest_player_score)
912 lowest_player = head;
913 lowest_player_score = head.totalfrags;
919 // prefers to move a bot...
920 if(lowest_bot != world)
921 selected = lowest_bot;
922 // but it will move a player if it has to
924 selected = lowest_player;
925 // don't do anything if it couldn't find anyone
928 bprint("warning: couldn't find a player to move from team\n");
932 // smallest team gains a member
933 if(smallestteam == 1)
937 else if(smallestteam == 2)
941 else if(smallestteam == 3)
945 else if(smallestteam == 4)
951 bprint("warning: destination team invalid\n");
954 // source team loses a member
959 else if(source_team == 2)
963 else if(source_team == 3)
967 else if(source_team == 4)
973 bprint("warning: source team invalid\n");
977 // move the player to the new team
978 TeamchangeFrags(selected);
979 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
981 if(selected.deadflag == DEAD_NO)
982 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
983 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", Team_ColoredFullName(selected.team)));
986 // code from here on is just to support maps that don't have team entities
987 void tdm_spawnteam (string teamname, float teamcolor)
991 e.classname = "tdm_team";
992 e.netname = teamname;
997 // spawn some default teams if the map is not set up for tdm
998 void tdm_spawnteams()
1002 numteams = autocvar_g_tdm_teams_override;
1004 numteams = autocvar_g_tdm_teams;
1005 numteams = bound(2, numteams, 4);
1007 tdm_spawnteam("Red", NUM_TEAM_1-1);
1008 tdm_spawnteam("Blue", NUM_TEAM_2-1);
1010 tdm_spawnteam("Yellow", NUM_TEAM_3-1);
1012 tdm_spawnteam("Pink", NUM_TEAM_4-1);
1015 void tdm_delayedinit()
1017 // if no teams are found, spawn defaults
1018 if (find(world, classname, "tdm_team") == world)
1024 InitializeEntity(world, tdm_delayedinit, INITPRIO_GAMETYPE);