3 void TeamchangeFrags(entity e)
8 void LogTeamchange(float player_id, float team_number, float type)
10 if(!autocvar_sv_eventlog)
16 GameLogEcho(strcat(":team:", ftos(player_id), ":", ftos(team_number), ":", ftos(type)));
19 void default_delayedinit()
21 if(!scores_initialized)
25 void ActivateTeamplay()
27 serverflags |= SERVERFLAG_TEAMPLAY;
31 void InitGameplayMode()
33 float fraglimit_override, timelimit_override, leadlimit_override, qualifying_override;
35 qualifying_override = -1;
39 // find out good world mins/maxs bounds, either the static bounds found by looking for solid, or the mapinfo specified bounds
44 MapInfo_LoadMapSettings(mapname);
46 serverflags &= ~SERVERFLAG_TEAMPLAY;
48 if (!cvar_value_issafe(world.fog))
50 print("The current map contains a potentially harmful fog setting, ignored\n");
51 world.fog = string_null;
53 if(MapInfo_Map_fog != "")
54 if(MapInfo_Map_fog == "none")
55 world.fog = string_null;
57 world.fog = strzone(MapInfo_Map_fog);
58 clientstuff = strzone(MapInfo_Map_clientstuff);
62 // set both here, gamemode can override it later
63 timelimit_override = autocvar_timelimit_override;
64 fraglimit_override = autocvar_fraglimit_override;
65 leadlimit_override = autocvar_leadlimit_override;
66 gamemode_name = MapInfo_Type_ToText(MapInfo_LoadedGametype);
75 fraglimit_override = autocvar_g_tdm_point_limit;
76 leadlimit_override = autocvar_g_tdm_point_leadlimit;
77 if(autocvar_g_tdm_team_spawns)
78 have_team_spawns = -1; // request team spawns
79 MUTATOR_ADD(gamemode_tdm);
85 fraglimit_override = autocvar_g_domination_point_limit;
86 leadlimit_override = autocvar_g_domination_point_leadlimit;
87 if(autocvar_g_domination_roundbased && autocvar_g_domination_roundbased_point_limit)
88 fraglimit_override = autocvar_g_domination_roundbased_point_limit;
89 have_team_spawns = -1; // request team spawns
90 MUTATOR_ADD(gamemode_domination);
96 fraglimit_override = autocvar_capturelimit_override;
97 leadlimit_override = autocvar_captureleadlimit_override;
98 have_team_spawns = -1; // request team spawns
99 MUTATOR_ADD(gamemode_ctf);
104 fraglimit_override = autocvar_g_lms_lives_override;
105 leadlimit_override = 0; // not supported by LMS
106 if(fraglimit_override == 0)
107 fraglimit_override = -1;
108 MUTATOR_ADD(gamemode_lms);
114 fraglimit_override = autocvar_g_ca_point_limit;
115 leadlimit_override = autocvar_g_ca_point_leadlimit;
116 if(autocvar_g_ca_team_spawns)
117 have_team_spawns = -1; // request team spawns
118 MUTATOR_ADD(gamemode_ca);
124 fraglimit_override = autocvar_g_keyhunt_point_limit;
125 leadlimit_override = autocvar_g_keyhunt_point_leadlimit;
126 MUTATOR_ADD(gamemode_keyhunt);
132 fraglimit_override = autocvar_g_freezetag_point_limit;
133 leadlimit_override = autocvar_g_freezetag_point_leadlimit;
134 if(autocvar_g_freezetag_team_spawns)
135 have_team_spawns = -1; // request team spawns
136 MUTATOR_ADD(gamemode_freezetag);
142 have_team_spawns = -1; // request team spawns
143 MUTATOR_ADD(gamemode_assault);
149 have_team_spawns = -1; // request team spawns
150 MUTATOR_ADD(gamemode_onslaught);
155 if(autocvar_g_race_teams)
158 race_teams = bound(2, autocvar_g_race_teams, 4);
159 have_team_spawns = -1; // request team spawns
163 qualifying_override = autocvar_g_race_qualifying_timelimit_override;
164 fraglimit_override = autocvar_g_race_laps_limit;
165 leadlimit_override = 0; // currently not supported by race
167 // we need to find out the correct value for g_race_qualifying
168 float want_qualifying = ((qualifying_override >= 0) ? qualifying_override : autocvar_g_race_qualifying_timelimit) > 0;
170 if(autocvar_g_campaign)
172 g_race_qualifying = 1;
173 independent_players = 1;
175 else if(!autocvar_g_campaign && want_qualifying)
177 g_race_qualifying = 2;
178 independent_players = 1;
179 race_fraglimit = (race_fraglimit >= 0) ? fraglimit_override : autocvar_fraglimit;
180 race_leadlimit = (race_leadlimit >= 0) ? leadlimit_override : autocvar_leadlimit;
181 race_timelimit = (race_timelimit >= 0) ? timelimit_override : autocvar_timelimit;
182 fraglimit_override = 0;
183 leadlimit_override = 0;
184 timelimit_override = autocvar_g_race_qualifying_timelimit;
188 g_race_qualifying = 0;
191 MUTATOR_ADD(gamemode_race);
196 g_race_qualifying = 1;
197 fraglimit_override = 0;
198 leadlimit_override = 0;
199 independent_players = 1;
200 MUTATOR_ADD(gamemode_cts);
205 fraglimit_override = autocvar_g_nexball_goallimit;
206 leadlimit_override = autocvar_g_nexball_goalleadlimit;
208 have_team_spawns = -1; // request team spawns
209 MUTATOR_ADD(gamemode_nexball);
214 MUTATOR_ADD(gamemode_keepaway);
219 fraglimit_override = autocvar_g_invasion_point_limit;
220 if(autocvar_g_invasion_teams >= 2)
223 if(autocvar_g_invasion_team_spawns)
224 have_team_spawns = -1; // request team spawns
226 MUTATOR_ADD(gamemode_invasion);
232 cache_mutatormsg = strzone("");
233 cache_lastmutatormsg = strzone("");
235 // enforce the server's universal frag/time limits
236 if(!autocvar_g_campaign)
238 if(fraglimit_override >= 0)
239 cvar_set("fraglimit", ftos(fraglimit_override));
240 if(timelimit_override >= 0)
241 cvar_set("timelimit", ftos(timelimit_override));
242 if(leadlimit_override >= 0)
243 cvar_set("leadlimit", ftos(leadlimit_override));
244 if(qualifying_override >= 0)
245 cvar_set("g_race_qualifying_timelimit", ftos(qualifying_override));
248 InitializeEntity(world, default_delayedinit, INITPRIO_GAMETYPE_FALLBACK);
251 string GetClientVersionMessage() {
253 if (self.version_mismatch) {
254 if(self.version < autocvar_gameversion) {
255 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
257 versionmsg = "^3This server is using an outdated Xonotic version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
260 versionmsg = "^2client version and server version are compatible.^8";
265 string getwelcomemessage(void)
267 string s, modifications, motd;
270 MUTATOR_CALLHOOK(BuildMutatorsPrettyString);
271 modifications = ret_string;
275 if(g_weaponarena_random)
276 modifications = strcat(modifications, ", ", ftos(g_weaponarena_random), " of ", g_weaponarena_list, " Arena");
278 modifications = strcat(modifications, ", ", g_weaponarena_list, " Arena");
280 if(cvar("g_balance_blaster_weaponstart") == 0)
281 modifications = strcat(modifications, ", No start weapons");
282 if(cvar("sv_gravity") < stof(cvar_defstring("sv_gravity")))
283 modifications = strcat(modifications, ", Low gravity");
284 if(g_cloaked && !g_cts)
285 modifications = strcat(modifications, ", Cloaked");
287 modifications = strcat(modifications, ", Hook");
288 if(g_weapon_stay && !g_cts)
289 modifications = strcat(modifications, ", Weapons stay");
291 modifications = strcat(modifications, ", Jet pack");
292 if(autocvar_g_powerups == 0)
293 modifications = strcat(modifications, ", No powerups");
294 if(autocvar_g_powerups > 0)
295 modifications = strcat(modifications, ", Powerups");
296 modifications = substring(modifications, 2, strlen(modifications) - 2);
298 string versionmessage;
299 versionmessage = GetClientVersionMessage();
301 s = strcat("This is Xonotic ", autocvar_g_xonoticversion, "\n", versionmessage);
302 s = strcat(s, "^8\n\nmatch type is ^1", gamemode_name, "^8\n");
304 if(modifications != "")
305 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
307 if (g_grappling_hook)
308 s = strcat(s, "\n\n^3grappling hook^8 is enabled, press 'e' to use it\n");
311 s = strcat(s, "\n\n^3nades^8 are enabled, press 'g' to use them\n");
313 if(cache_lastmutatormsg != autocvar_g_mutatormsg)
315 if(cache_lastmutatormsg)
316 strunzone(cache_lastmutatormsg);
318 strunzone(cache_mutatormsg);
319 cache_lastmutatormsg = strzone(autocvar_g_mutatormsg);
320 cache_mutatormsg = strzone(cache_lastmutatormsg);
323 if (cache_mutatormsg != "") {
324 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
327 motd = autocvar_sv_motd;
329 s = strcat(s, "\n\n^8MOTD: ^7", strreplace("\\n", "\n", motd));
334 void SetPlayerColors(entity pl, float _color)
338 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
340 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
341 pl.clientcolors = 16*cl + cl;*/
344 pants = _color & 0x0F;
345 shirt = _color & 0xF0;
349 setcolor(pl, 16*pants + pants);
351 setcolor(pl, shirt + pants);
355 void SetPlayerTeam(entity pl, float t, float s, float noprint)
360 _color = NUM_TEAM_4 - 1;
362 _color = NUM_TEAM_3 - 1;
364 _color = NUM_TEAM_2 - 1;
366 _color = NUM_TEAM_1 - 1;
368 SetPlayerColors(pl,_color);
371 LogTeamchange(pl.playerid, pl.team, 3); // log manual team join
374 bprint(pl.netname, "^7 has changed from ", Team_NumberToColoredFullName(s), "^7 to ", Team_NumberToColoredFullName(t), "\n");
379 // set c1...c4 to show what teams are allowed
380 void CheckAllowedTeams (entity for_whom)
386 c1 = c2 = c3 = c4 = -1;
387 cb1 = cb2 = cb3 = cb4 = 0;
389 teament_name = string_null;
392 // onslaught is special
393 head = findchain(classname, "onslaught_generator");
396 if (head.team == NUM_TEAM_1) c1 = 0;
397 if (head.team == NUM_TEAM_2) c2 = 0;
398 if (head.team == NUM_TEAM_3) c3 = 0;
399 if (head.team == NUM_TEAM_4) c4 = 0;
403 else if(g_domination)
404 teament_name = "dom_team";
406 teament_name = "ctf_team";
408 teament_name = "tdm_team";
410 teament_name = "nexball_team";
412 c1 = c2 = 0; // Assault always has 2 teams
415 // cover anything else by treating it like tdm with no teams spawned
419 MUTATOR_CALLHOOK(GetTeamCount);
423 c1 = c2 = c3 = c4 = 0;
430 // find out what teams are allowed if necessary
433 head = find(world, classname, teament_name);
436 if(!(g_domination && head.netname == ""))
438 if(head.team == NUM_TEAM_1)
440 else if(head.team == NUM_TEAM_2)
442 else if(head.team == NUM_TEAM_3)
444 else if(head.team == NUM_TEAM_4)
447 head = find(head, classname, teament_name);
451 // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
453 if(autocvar_bot_vs_human && for_whom)
455 if(autocvar_bot_vs_human > 0)
458 if(IS_BOT_CLIENT(for_whom))
466 if(IS_BOT_CLIENT(for_whom))
473 // if player has a forced team, ONLY allow that one
474 if(self.team_forced == NUM_TEAM_1 && c1 >= 0)
476 else if(self.team_forced == NUM_TEAM_2 && c2 >= 0)
478 else if(self.team_forced == NUM_TEAM_3 && c3 >= 0)
480 else if(self.team_forced == NUM_TEAM_4 && c4 >= 0)
484 float PlayerValue(entity p)
487 // FIXME: it always returns 1...
490 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
491 // teams that are allowed will now have their player counts stored in c1...c4
492 void GetTeamCounts(entity ignore)
496 // now count how many players are on each team already
498 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
499 // also remember the lowest-scoring player
501 FOR_EACH_CLIENT(head)
504 if(IS_PLAYER(head) || head.caplayer)
506 else if(head.team_forced > 0)
507 t = head.team_forced; // reserve the spot
510 if(head != ignore)// && head.netname != "")
512 value = PlayerValue(head);
513 if(IS_BOT_CLIENT(head))
552 // if the player who has a forced team has not joined yet, reserve the spot
553 if(autocvar_g_campaign)
555 switch(autocvar_g_campaign_forceteam)
557 case 1: if(c1 == cb1) ++c1; break;
558 case 2: if(c2 == cb2) ++c2; break;
559 case 3: if(c3 == cb3) ++c3; break;
560 case 4: if(c4 == cb4) ++c4; break;
565 float TeamSmallerEqThanTeam(float ta, float tb, entity e)
567 // we assume that CheckAllowedTeams and GetTeamCounts have already been called
569 float ca = -1, cb = -1, cba = 0, cbb = 0, sa = 0, sb = 0;
573 case 1: ca = c1; cba = cb1; sa = team1_score; break;
574 case 2: ca = c2; cba = cb2; sa = team2_score; break;
575 case 3: ca = c3; cba = cb3; sa = team3_score; break;
576 case 4: ca = c4; cba = cb4; sa = team4_score; break;
580 case 1: cb = c1; cbb = cb1; sb = team1_score; break;
581 case 2: cb = c2; cbb = cb2; sb = team2_score; break;
582 case 3: cb = c3; cbb = cb3; sb = team3_score; break;
583 case 4: cb = c4; cbb = cb4; sb = team4_score; break;
594 if(IS_REAL_CLIENT(e))
603 // keep teams alive (teams of size 0 always count as smaller, ignoring score)
619 // the more we're at the end of the match, the more take scores into account
620 f = bound(0, game_completion_ratio * autocvar_g_balance_teams_scorefactor, 1);
627 // returns # of smallest team (1, 2, 3, 4)
628 // NOTE: Assumes CheckAllowedTeams has already been called!
629 float FindSmallestTeam(entity pl, float ignore_pl)
634 // find out what teams are available
635 //CheckAllowedTeams();
637 // make sure there are at least 2 teams to join
639 totalteams = totalteams + 1;
641 totalteams = totalteams + 1;
643 totalteams = totalteams + 1;
645 totalteams = totalteams + 1;
647 if((autocvar_bot_vs_human || pl.team_forced > 0) && totalteams == 1)
652 if(autocvar_g_campaign && pl && IS_REAL_CLIENT(pl))
653 return 1; // special case for campaign and player joining
655 error(sprintf("Too few teams available for %s\n", MapInfo_Type_ToString(MapInfo_CurrentGametype())));
658 // count how many players are in each team
662 GetTeamCounts(world);
664 RandomSelection_Init();
667 if(TeamSmallerEqThanTeam(2, t, pl))
669 if(TeamSmallerEqThanTeam(3, t, pl))
671 if(TeamSmallerEqThanTeam(4, t, pl))
674 // now t is the minimum, or A minimum!
675 if(t == 1 || TeamSmallerEqThanTeam(1, t, pl))
676 RandomSelection_Add(world, 1, string_null, 1, 1);
677 if(t == 2 || TeamSmallerEqThanTeam(2, t, pl))
678 RandomSelection_Add(world, 2, string_null, 1, 1);
679 if(t == 3 || TeamSmallerEqThanTeam(3, t, pl))
680 RandomSelection_Add(world, 3, string_null, 1, 1);
681 if(t == 4 || TeamSmallerEqThanTeam(4, t, pl))
682 RandomSelection_Add(world, 4, string_null, 1, 1);
684 return RandomSelection_chosen_float;
687 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
689 float smallest, selectedteam;
691 // don't join a team if we're not playing a team game
695 // find out what teams are available
696 CheckAllowedTeams(pl);
698 // if we don't care what team he ends up on, put him on whatever team he entered as.
699 // if he's not on a valid team, then let other code put him on the smallest team
702 if( c1 >= 0 && pl.team == NUM_TEAM_1)
703 selectedteam = pl.team;
704 else if(c2 >= 0 && pl.team == NUM_TEAM_2)
705 selectedteam = pl.team;
706 else if(c3 >= 0 && pl.team == NUM_TEAM_3)
707 selectedteam = pl.team;
708 else if(c4 >= 0 && pl.team == NUM_TEAM_4)
709 selectedteam = pl.team;
715 if(!only_return_best)
717 SetPlayerColors(pl, selectedteam - 1);
719 // when JoinBestTeam is called by client.qc/ClientKill_Now_TeamChange the players team is -1 and thus skipped
720 // when JoinBestTeam is called by cl_client.qc/ClientConnect the player_id is 0 the log attempt is rejected
721 LogTeamchange(pl.playerid, pl.team, 99);
725 // otherwise end up on the smallest team (handled below)
728 smallest = FindSmallestTeam(pl, true);
730 if(!only_return_best && !pl.bot_forced_team)
732 TeamchangeFrags(self);
735 SetPlayerColors(pl, NUM_TEAM_1 - 1);
737 else if(smallest == 2)
739 SetPlayerColors(pl, NUM_TEAM_2 - 1);
741 else if(smallest == 3)
743 SetPlayerColors(pl, NUM_TEAM_3 - 1);
745 else if(smallest == 4)
747 SetPlayerColors(pl, NUM_TEAM_4 - 1);
751 error("smallest team: invalid team\n");
754 LogTeamchange(pl.playerid, pl.team, 2); // log auto join
756 if(pl.deadflag == DEAD_NO)
757 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
763 //void() ctf_playerchanged;
764 void SV_ChangeTeam(float _color)
766 float scolor, dcolor, steam, dteam; //, dbotcount, scount, dcount;
768 // in normal deathmatch we can just apply the color and we're done
770 SetPlayerColors(self, _color);
774 scolor = self.clientcolors & 0x0F;
775 dcolor = _color & 0x0F;
777 if(scolor == NUM_TEAM_1 - 1)
779 else if(scolor == NUM_TEAM_2 - 1)
781 else if(scolor == NUM_TEAM_3 - 1)
783 else // if(scolor == NUM_TEAM_4 - 1)
785 if(dcolor == NUM_TEAM_1 - 1)
787 else if(dcolor == NUM_TEAM_2 - 1)
789 else if(dcolor == NUM_TEAM_3 - 1)
791 else // if(dcolor == NUM_TEAM_4 - 1)
794 CheckAllowedTeams(self);
796 if(dteam == 1 && c1 < 0) dteam = 4;
797 if(dteam == 4 && c4 < 0) dteam = 3;
798 if(dteam == 3 && c3 < 0) dteam = 2;
799 if(dteam == 2 && c2 < 0) dteam = 1;
801 // not changing teams
804 //bprint("same team change\n");
805 SetPlayerTeam(self, dteam, steam, true);
809 if((autocvar_g_campaign) || (autocvar_g_changeteam_banned && self.wasplayer)) {
810 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_TEAMCHANGE_NOTALLOWED);
811 return; // changing teams is not allowed
814 // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
815 if(autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
818 if(!TeamSmallerEqThanTeam(dteam, steam, self))
820 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_TEAMCHANGE_LARGERTEAM);
825 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
827 if(IS_PLAYER(self) && steam != dteam)
829 // reduce frags during a team change
830 TeamchangeFrags(self);
833 SetPlayerTeam(self, dteam, steam, false);
835 if(IS_PLAYER(self) && steam != dteam)
837 // kill player when changing teams
838 if(self.deadflag == DEAD_NO)
839 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
843 void ShufflePlayerOutOfTeam (float source_team)
845 float smallestteam, smallestteam_count, steam;
846 float lowest_bot_score, lowest_player_score;
847 entity head, lowest_bot, lowest_player, selected;
850 smallestteam_count = 999999999;
852 if(c1 >= 0 && c1 < smallestteam_count)
855 smallestteam_count = c1;
857 if(c2 >= 0 && c2 < smallestteam_count)
860 smallestteam_count = c2;
862 if(c3 >= 0 && c3 < smallestteam_count)
865 smallestteam_count = c3;
867 if(c4 >= 0 && c4 < smallestteam_count)
870 smallestteam_count = c4;
875 bprint("warning: no smallest team\n");
881 else if(source_team == 2)
883 else if(source_team == 3)
885 else // if(source_team == 4)
889 lowest_bot_score = 999999999;
890 lowest_player = world;
891 lowest_player_score = 999999999;
893 // find the lowest-scoring player & bot of that team
894 FOR_EACH_PLAYER(head)
896 if(head.team == steam)
900 if(head.totalfrags < lowest_bot_score)
903 lowest_bot_score = head.totalfrags;
908 if(head.totalfrags < lowest_player_score)
910 lowest_player = head;
911 lowest_player_score = head.totalfrags;
917 // prefers to move a bot...
918 if(lowest_bot != world)
919 selected = lowest_bot;
920 // but it will move a player if it has to
922 selected = lowest_player;
923 // don't do anything if it couldn't find anyone
926 bprint("warning: couldn't find a player to move from team\n");
930 // smallest team gains a member
931 if(smallestteam == 1)
935 else if(smallestteam == 2)
939 else if(smallestteam == 3)
943 else if(smallestteam == 4)
949 bprint("warning: destination team invalid\n");
952 // source team loses a member
957 else if(source_team == 2)
961 else if(source_team == 3)
965 else if(source_team == 4)
971 bprint("warning: source team invalid\n");
975 // move the player to the new team
976 TeamchangeFrags(selected);
977 SetPlayerTeam(selected, smallestteam, source_team, false);
979 if(selected.deadflag == DEAD_NO)
980 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
981 Send_Notification(NOTIF_ONE, selected, MSG_CENTER, CENTER_DEATH_SELF_AUTOTEAMCHANGE, selected.team);