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 fraglimit_override = autocvar_g_onslaught_point_limit;
150 have_team_spawns = -1; // request team spawns
151 MUTATOR_ADD(gamemode_onslaught);
156 if(autocvar_g_race_teams)
159 race_teams = bound(2, autocvar_g_race_teams, 4);
160 have_team_spawns = -1; // request team spawns
164 qualifying_override = autocvar_g_race_qualifying_timelimit_override;
165 fraglimit_override = autocvar_g_race_laps_limit;
166 leadlimit_override = 0; // currently not supported by race
168 // we need to find out the correct value for g_race_qualifying
169 float want_qualifying = ((qualifying_override >= 0) ? qualifying_override : autocvar_g_race_qualifying_timelimit) > 0;
171 if(autocvar_g_campaign)
173 g_race_qualifying = 1;
174 independent_players = 1;
176 else if(!autocvar_g_campaign && want_qualifying)
178 g_race_qualifying = 2;
179 independent_players = 1;
180 race_fraglimit = (race_fraglimit >= 0) ? fraglimit_override : autocvar_fraglimit;
181 race_leadlimit = (race_leadlimit >= 0) ? leadlimit_override : autocvar_leadlimit;
182 race_timelimit = (race_timelimit >= 0) ? timelimit_override : autocvar_timelimit;
183 fraglimit_override = 0;
184 leadlimit_override = 0;
185 timelimit_override = autocvar_g_race_qualifying_timelimit;
189 g_race_qualifying = 0;
192 MUTATOR_ADD(gamemode_race);
197 g_race_qualifying = 1;
198 fraglimit_override = 0;
199 leadlimit_override = 0;
200 independent_players = 1;
201 MUTATOR_ADD(gamemode_cts);
206 fraglimit_override = autocvar_g_nexball_goallimit;
207 leadlimit_override = autocvar_g_nexball_goalleadlimit;
209 have_team_spawns = -1; // request team spawns
210 MUTATOR_ADD(gamemode_nexball);
215 MUTATOR_ADD(gamemode_keepaway);
220 fraglimit_override = autocvar_g_invasion_point_limit;
221 if(autocvar_g_invasion_teams >= 2)
224 if(autocvar_g_invasion_team_spawns)
225 have_team_spawns = -1; // request team spawns
227 MUTATOR_ADD(gamemode_invasion);
233 cache_mutatormsg = strzone("");
234 cache_lastmutatormsg = strzone("");
236 // enforce the server's universal frag/time limits
237 if(!autocvar_g_campaign)
239 if(fraglimit_override >= 0)
240 cvar_set("fraglimit", ftos(fraglimit_override));
241 if(timelimit_override >= 0)
242 cvar_set("timelimit", ftos(timelimit_override));
243 if(leadlimit_override >= 0)
244 cvar_set("leadlimit", ftos(leadlimit_override));
245 if(qualifying_override >= 0)
246 cvar_set("g_race_qualifying_timelimit", ftos(qualifying_override));
249 InitializeEntity(world, default_delayedinit, INITPRIO_GAMETYPE_FALLBACK);
252 string GetClientVersionMessage() {
254 if (self.version_mismatch) {
255 if(self.version < autocvar_gameversion) {
256 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
258 versionmsg = "^3This server is using an outdated Xonotic version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
261 versionmsg = "^2client version and server version are compatible.^8";
266 string getwelcomemessage(void)
268 string s, modifications, motd;
271 MUTATOR_CALLHOOK(BuildMutatorsPrettyString);
272 modifications = ret_string;
276 if(g_weaponarena_random)
277 modifications = strcat(modifications, ", ", ftos(g_weaponarena_random), " of ", g_weaponarena_list, " Arena");
279 modifications = strcat(modifications, ", ", g_weaponarena_list, " Arena");
281 if(cvar("g_balance_blaster_weaponstart") == 0)
282 modifications = strcat(modifications, ", No start weapons");
283 if(cvar("sv_gravity") < stof(cvar_defstring("sv_gravity")))
284 modifications = strcat(modifications, ", Low gravity");
285 if(g_cloaked && !g_cts)
286 modifications = strcat(modifications, ", Cloaked");
288 modifications = strcat(modifications, ", Hook");
289 if(g_weapon_stay && !g_cts)
290 modifications = strcat(modifications, ", Weapons stay");
292 modifications = strcat(modifications, ", Jet pack");
293 if(autocvar_g_powerups == 0)
294 modifications = strcat(modifications, ", No powerups");
295 if(autocvar_g_powerups > 0)
296 modifications = strcat(modifications, ", Powerups");
297 modifications = substring(modifications, 2, strlen(modifications) - 2);
299 string versionmessage;
300 versionmessage = GetClientVersionMessage();
302 s = strcat("This is Xonotic ", autocvar_g_xonoticversion, "\n", versionmessage);
303 s = strcat(s, "^8\n\nmatch type is ^1", gamemode_name, "^8\n");
305 if(modifications != "")
306 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
308 if (g_grappling_hook)
309 s = strcat(s, "\n\n^3grappling hook^8 is enabled, press 'e' to use it\n");
312 s = strcat(s, "\n\n^3nades^8 are enabled, press 'g' to use them\n");
314 if(cache_lastmutatormsg != autocvar_g_mutatormsg)
316 if(cache_lastmutatormsg)
317 strunzone(cache_lastmutatormsg);
319 strunzone(cache_mutatormsg);
320 cache_lastmutatormsg = strzone(autocvar_g_mutatormsg);
321 cache_mutatormsg = strzone(cache_lastmutatormsg);
324 if (cache_mutatormsg != "") {
325 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
328 motd = autocvar_sv_motd;
330 s = strcat(s, "\n\n^8MOTD: ^7", strreplace("\\n", "\n", motd));
335 void SetPlayerColors(entity pl, float _color)
339 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
341 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
342 pl.clientcolors = 16*cl + cl;*/
345 pants = _color & 0x0F;
346 shirt = _color & 0xF0;
350 setcolor(pl, 16*pants + pants);
352 setcolor(pl, shirt + pants);
356 void SetPlayerTeam(entity pl, float t, float s, float noprint)
361 _color = NUM_TEAM_4 - 1;
363 _color = NUM_TEAM_3 - 1;
365 _color = NUM_TEAM_2 - 1;
367 _color = NUM_TEAM_1 - 1;
369 SetPlayerColors(pl,_color);
372 LogTeamchange(pl.playerid, pl.team, 3); // log manual team join
375 bprint(pl.netname, "^7 has changed from ", Team_NumberToColoredFullName(s), "^7 to ", Team_NumberToColoredFullName(t), "\n");
380 // set c1...c4 to show what teams are allowed
381 void CheckAllowedTeams (entity for_whom)
387 c1 = c2 = c3 = c4 = -1;
388 cb1 = cb2 = cb3 = cb4 = 0;
390 teament_name = string_null;
393 // onslaught is special
394 head = findchain(classname, "onslaught_generator");
397 if (head.team == NUM_TEAM_1) c1 = 0;
398 if (head.team == NUM_TEAM_2) c2 = 0;
399 if (head.team == NUM_TEAM_3) c3 = 0;
400 if (head.team == NUM_TEAM_4) c4 = 0;
404 else if(g_domination)
405 teament_name = "dom_team";
407 teament_name = "ctf_team";
409 teament_name = "tdm_team";
411 teament_name = "nexball_team";
413 c1 = c2 = 0; // Assault always has 2 teams
416 // cover anything else by treating it like tdm with no teams spawned
420 MUTATOR_CALLHOOK(GetTeamCount);
424 c1 = c2 = c3 = c4 = 0;
431 // find out what teams are allowed if necessary
434 head = find(world, classname, teament_name);
437 if(!(g_domination && head.netname == ""))
439 if(head.team == NUM_TEAM_1)
441 else if(head.team == NUM_TEAM_2)
443 else if(head.team == NUM_TEAM_3)
445 else if(head.team == NUM_TEAM_4)
448 head = find(head, classname, teament_name);
452 // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
454 if(autocvar_bot_vs_human && for_whom)
456 if(autocvar_bot_vs_human > 0)
459 if(IS_BOT_CLIENT(for_whom))
467 if(IS_BOT_CLIENT(for_whom))
474 // if player has a forced team, ONLY allow that one
475 if(self.team_forced == NUM_TEAM_1 && c1 >= 0)
477 else if(self.team_forced == NUM_TEAM_2 && c2 >= 0)
479 else if(self.team_forced == NUM_TEAM_3 && c3 >= 0)
481 else if(self.team_forced == NUM_TEAM_4 && c4 >= 0)
485 float PlayerValue(entity p)
488 // FIXME: it always returns 1...
491 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
492 // teams that are allowed will now have their player counts stored in c1...c4
493 void GetTeamCounts(entity ignore)
497 // now count how many players are on each team already
499 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
500 // also remember the lowest-scoring player
502 FOR_EACH_CLIENT(head)
505 if(IS_PLAYER(head) || head.caplayer)
507 else if(head.team_forced > 0)
508 t = head.team_forced; // reserve the spot
511 if(head != ignore)// && head.netname != "")
513 value = PlayerValue(head);
514 if(IS_BOT_CLIENT(head))
553 // if the player who has a forced team has not joined yet, reserve the spot
554 if(autocvar_g_campaign)
556 switch(autocvar_g_campaign_forceteam)
558 case 1: if(c1 == cb1) ++c1; break;
559 case 2: if(c2 == cb2) ++c2; break;
560 case 3: if(c3 == cb3) ++c3; break;
561 case 4: if(c4 == cb4) ++c4; break;
566 float TeamSmallerEqThanTeam(float ta, float tb, entity e)
568 // we assume that CheckAllowedTeams and GetTeamCounts have already been called
570 float ca = -1, cb = -1, cba = 0, cbb = 0, sa = 0, sb = 0;
574 case 1: ca = c1; cba = cb1; sa = team1_score; break;
575 case 2: ca = c2; cba = cb2; sa = team2_score; break;
576 case 3: ca = c3; cba = cb3; sa = team3_score; break;
577 case 4: ca = c4; cba = cb4; sa = team4_score; break;
581 case 1: cb = c1; cbb = cb1; sb = team1_score; break;
582 case 2: cb = c2; cbb = cb2; sb = team2_score; break;
583 case 3: cb = c3; cbb = cb3; sb = team3_score; break;
584 case 4: cb = c4; cbb = cb4; sb = team4_score; break;
595 if(IS_REAL_CLIENT(e))
604 // keep teams alive (teams of size 0 always count as smaller, ignoring score)
620 // the more we're at the end of the match, the more take scores into account
621 f = bound(0, game_completion_ratio * autocvar_g_balance_teams_scorefactor, 1);
628 // returns # of smallest team (1, 2, 3, 4)
629 // NOTE: Assumes CheckAllowedTeams has already been called!
630 float FindSmallestTeam(entity pl, float ignore_pl)
635 // find out what teams are available
636 //CheckAllowedTeams();
638 // make sure there are at least 2 teams to join
640 totalteams = totalteams + 1;
642 totalteams = totalteams + 1;
644 totalteams = totalteams + 1;
646 totalteams = totalteams + 1;
648 if((autocvar_bot_vs_human || pl.team_forced > 0) && totalteams == 1)
653 if(autocvar_g_campaign && pl && IS_REAL_CLIENT(pl))
654 return 1; // special case for campaign and player joining
656 error(sprintf("Too few teams available for %s\n", MapInfo_Type_ToString(MapInfo_CurrentGametype())));
659 // count how many players are in each team
663 GetTeamCounts(world);
665 RandomSelection_Init();
668 if(TeamSmallerEqThanTeam(2, t, pl))
670 if(TeamSmallerEqThanTeam(3, t, pl))
672 if(TeamSmallerEqThanTeam(4, t, pl))
675 // now t is the minimum, or A minimum!
676 if(t == 1 || TeamSmallerEqThanTeam(1, t, pl))
677 RandomSelection_Add(world, 1, string_null, 1, 1);
678 if(t == 2 || TeamSmallerEqThanTeam(2, t, pl))
679 RandomSelection_Add(world, 2, string_null, 1, 1);
680 if(t == 3 || TeamSmallerEqThanTeam(3, t, pl))
681 RandomSelection_Add(world, 3, string_null, 1, 1);
682 if(t == 4 || TeamSmallerEqThanTeam(4, t, pl))
683 RandomSelection_Add(world, 4, string_null, 1, 1);
685 return RandomSelection_chosen_float;
688 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
690 float smallest, selectedteam;
692 // don't join a team if we're not playing a team game
696 // find out what teams are available
697 CheckAllowedTeams(pl);
699 // if we don't care what team he ends up on, put him on whatever team he entered as.
700 // if he's not on a valid team, then let other code put him on the smallest team
703 if( c1 >= 0 && pl.team == NUM_TEAM_1)
704 selectedteam = pl.team;
705 else if(c2 >= 0 && pl.team == NUM_TEAM_2)
706 selectedteam = pl.team;
707 else if(c3 >= 0 && pl.team == NUM_TEAM_3)
708 selectedteam = pl.team;
709 else if(c4 >= 0 && pl.team == NUM_TEAM_4)
710 selectedteam = pl.team;
716 if(!only_return_best)
718 SetPlayerColors(pl, selectedteam - 1);
720 // when JoinBestTeam is called by client.qc/ClientKill_Now_TeamChange the players team is -1 and thus skipped
721 // when JoinBestTeam is called by cl_client.qc/ClientConnect the player_id is 0 the log attempt is rejected
722 LogTeamchange(pl.playerid, pl.team, 99);
726 // otherwise end up on the smallest team (handled below)
729 smallest = FindSmallestTeam(pl, true);
731 if(!only_return_best && !pl.bot_forced_team)
733 TeamchangeFrags(self);
736 SetPlayerColors(pl, NUM_TEAM_1 - 1);
738 else if(smallest == 2)
740 SetPlayerColors(pl, NUM_TEAM_2 - 1);
742 else if(smallest == 3)
744 SetPlayerColors(pl, NUM_TEAM_3 - 1);
746 else if(smallest == 4)
748 SetPlayerColors(pl, NUM_TEAM_4 - 1);
752 error("smallest team: invalid team\n");
755 LogTeamchange(pl.playerid, pl.team, 2); // log auto join
757 if(pl.deadflag == DEAD_NO)
758 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
764 //void() ctf_playerchanged;
765 void SV_ChangeTeam(float _color)
767 float scolor, dcolor, steam, dteam; //, dbotcount, scount, dcount;
769 // in normal deathmatch we can just apply the color and we're done
771 SetPlayerColors(self, _color);
775 scolor = self.clientcolors & 0x0F;
776 dcolor = _color & 0x0F;
778 if(scolor == NUM_TEAM_1 - 1)
780 else if(scolor == NUM_TEAM_2 - 1)
782 else if(scolor == NUM_TEAM_3 - 1)
784 else // if(scolor == NUM_TEAM_4 - 1)
786 if(dcolor == NUM_TEAM_1 - 1)
788 else if(dcolor == NUM_TEAM_2 - 1)
790 else if(dcolor == NUM_TEAM_3 - 1)
792 else // if(dcolor == NUM_TEAM_4 - 1)
795 CheckAllowedTeams(self);
797 if(dteam == 1 && c1 < 0) dteam = 4;
798 if(dteam == 4 && c4 < 0) dteam = 3;
799 if(dteam == 3 && c3 < 0) dteam = 2;
800 if(dteam == 2 && c2 < 0) dteam = 1;
802 // not changing teams
805 //bprint("same team change\n");
806 SetPlayerTeam(self, dteam, steam, true);
810 if((autocvar_g_campaign) || (autocvar_g_changeteam_banned && self.wasplayer)) {
811 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_TEAMCHANGE_NOTALLOWED);
812 return; // changing teams is not allowed
815 // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
816 if(autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
819 if(!TeamSmallerEqThanTeam(dteam, steam, self))
821 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_TEAMCHANGE_LARGERTEAM);
826 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
828 if(IS_PLAYER(self) && steam != dteam)
830 // reduce frags during a team change
831 TeamchangeFrags(self);
834 SetPlayerTeam(self, dteam, steam, false);
836 if(IS_PLAYER(self) && steam != dteam)
838 // kill player when changing teams
839 if(self.deadflag == DEAD_NO)
840 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
844 void ShufflePlayerOutOfTeam (float source_team)
846 float smallestteam, smallestteam_count, steam;
847 float lowest_bot_score, lowest_player_score;
848 entity head, lowest_bot, lowest_player, selected;
851 smallestteam_count = 999999999;
853 if(c1 >= 0 && c1 < smallestteam_count)
856 smallestteam_count = c1;
858 if(c2 >= 0 && c2 < smallestteam_count)
861 smallestteam_count = c2;
863 if(c3 >= 0 && c3 < smallestteam_count)
866 smallestteam_count = c3;
868 if(c4 >= 0 && c4 < smallestteam_count)
871 smallestteam_count = c4;
876 bprint("warning: no smallest team\n");
882 else if(source_team == 2)
884 else if(source_team == 3)
886 else // if(source_team == 4)
890 lowest_bot_score = 999999999;
891 lowest_player = world;
892 lowest_player_score = 999999999;
894 // find the lowest-scoring player & bot of that team
895 FOR_EACH_PLAYER(head)
897 if(head.team == steam)
901 if(head.totalfrags < lowest_bot_score)
904 lowest_bot_score = head.totalfrags;
909 if(head.totalfrags < lowest_player_score)
911 lowest_player = head;
912 lowest_player_score = head.totalfrags;
918 // prefers to move a bot...
919 if(lowest_bot != world)
920 selected = lowest_bot;
921 // but it will move a player if it has to
923 selected = lowest_player;
924 // don't do anything if it couldn't find anyone
927 bprint("warning: couldn't find a player to move from team\n");
931 // smallest team gains a member
932 if(smallestteam == 1)
936 else if(smallestteam == 2)
940 else if(smallestteam == 3)
944 else if(smallestteam == 4)
950 bprint("warning: destination team invalid\n");
953 // source team loses a member
958 else if(source_team == 2)
962 else if(source_team == 3)
966 else if(source_team == 4)
972 bprint("warning: source team invalid\n");
976 // move the player to the new team
977 TeamchangeFrags(selected);
978 SetPlayerTeam(selected, smallestteam, source_team, false);
980 if(selected.deadflag == DEAD_NO)
981 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
982 Send_Notification(NOTIF_ONE, selected, MSG_CENTER, CENTER_DEATH_SELF_AUTOTEAMCHANGE, selected.team);