3 #include "cl_client.qh"
6 #include "scores_rules.qh"
10 #include "command/vote.qh"
12 #include "mutators/all.qh"
14 #include "../common/deathtypes/all.qh"
15 #include "../common/gamemodes/all.qh"
16 #include "../common/teams.qh"
18 void TeamchangeFrags(entity e)
23 void LogTeamchange(float player_id, float team_number, float type)
25 if(!autocvar_sv_eventlog)
31 GameLogEcho(strcat(":team:", ftos(player_id), ":", ftos(team_number), ":", ftos(type)));
34 void default_delayedinit()
36 if(!scores_initialized)
40 void ActivateTeamplay()
42 serverflags |= SERVERFLAG_TEAMPLAY;
44 cvar_set("teamplay", "2"); // DP needs this for sending proper getstatus replies.
47 void SetLimits(int fraglimit_override, int leadlimit_override, float timelimit_override, float qualifying_override)
49 // enforce the server's universal frag/time limits
50 // set to -1 to not change value
51 if(!autocvar_g_campaign)
53 if(fraglimit_override >= 0)
54 cvar_set("fraglimit", ftos(fraglimit_override));
55 if(timelimit_override >= 0)
56 cvar_set("timelimit", ftos(timelimit_override));
57 if(leadlimit_override >= 0)
58 cvar_set("leadlimit", ftos(leadlimit_override));
59 if(qualifying_override >= 0)
60 cvar_set("g_race_qualifying_timelimit", ftos(qualifying_override));
64 void InitGameplayMode()
68 // find out good world mins/maxs bounds, either the static bounds found by looking for solid, or the mapinfo specified bounds
73 MapInfo_LoadMapSettings(mapname);
74 serverflags &= ~SERVERFLAG_TEAMPLAY;
76 cvar_set("teamplay", "0"); // DP needs this for sending proper getstatus replies.
78 if (!cvar_value_issafe(world.fog))
80 LOG_INFO("The current map contains a potentially harmful fog setting, ignored\n");
81 world.fog = string_null;
83 if(MapInfo_Map_fog != "")
84 if(MapInfo_Map_fog == "none")
85 world.fog = string_null;
87 world.fog = strzone(MapInfo_Map_fog);
88 clientstuff = strzone(MapInfo_Map_clientstuff);
92 // set both here, gamemode can override it later
93 SetLimits(autocvar_fraglimit_override, autocvar_leadlimit_override, autocvar_timelimit_override, -1);
94 gamemode_name = MapInfo_Type_ToText(MapInfo_LoadedGametype);
96 cache_mutatormsg = strzone("");
97 cache_lastmutatormsg = strzone("");
99 InitializeEntity(world, default_delayedinit, INITPRIO_GAMETYPE_FALLBACK);
102 string GetClientVersionMessage()
105 if (self.version_mismatch) {
106 if(self.version < autocvar_gameversion) {
107 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
109 versionmsg = "^3This server is using an outdated Xonotic version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
112 versionmsg = "^2client version and server version are compatible.^8";
117 string getwelcomemessage(void)
119 string s, modifications, motd;
121 MUTATOR_CALLHOOK(BuildMutatorsPrettyString, "");
122 modifications = ret_string;
126 if(g_weaponarena_random)
127 modifications = strcat(modifications, ", ", ftos(g_weaponarena_random), " of ", g_weaponarena_list, " Arena");
129 modifications = strcat(modifications, ", ", g_weaponarena_list, " Arena");
131 else if(cvar("g_balance_blaster_weaponstart") == 0)
132 modifications = strcat(modifications, ", No start weapons");
133 if(cvar("sv_gravity") < stof(cvar_defstring("sv_gravity")))
134 modifications = strcat(modifications, ", Low gravity");
135 if(g_cloaked && !g_cts)
136 modifications = strcat(modifications, ", Cloaked");
137 if(g_weapon_stay && !g_cts)
138 modifications = strcat(modifications, ", Weapons stay");
140 modifications = strcat(modifications, ", Jet pack");
141 if(autocvar_g_powerups == 0)
142 modifications = strcat(modifications, ", No powerups");
143 if(autocvar_g_powerups > 0)
144 modifications = strcat(modifications, ", Powerups");
145 modifications = substring(modifications, 2, strlen(modifications) - 2);
147 string versionmessage;
148 versionmessage = GetClientVersionMessage();
150 s = strcat("This is Xonotic ", autocvar_g_xonoticversion, "\n", versionmessage);
151 s = strcat(s, "^8\n\nmatch type is ^1", gamemode_name, "^8\n");
153 if(modifications != "")
154 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
157 s = strcat(s, "\n\n^3nades^8 are enabled, press 'g' to use them\n");
159 if(cache_lastmutatormsg != autocvar_g_mutatormsg)
161 if(cache_lastmutatormsg)
162 strunzone(cache_lastmutatormsg);
164 strunzone(cache_mutatormsg);
165 cache_lastmutatormsg = strzone(autocvar_g_mutatormsg);
166 cache_mutatormsg = strzone(cache_lastmutatormsg);
169 if (cache_mutatormsg != "") {
170 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
173 string mutator_msg = "";
174 MUTATOR_CALLHOOK(BuildGameplayTipsString, mutator_msg);
175 mutator_msg = ret_string;
177 s = strcat(s, mutator_msg); // trust that the mutator will do proper formatting
179 motd = autocvar_sv_motd;
181 s = strcat(s, "\n\n^8MOTD: ^7", strreplace("\\n", "\n", motd));
186 void SetPlayerColors(entity pl, float _color)
190 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
192 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
193 pl.clientcolors = 16*cl + cl;*/
196 pants = _color & 0x0F;
197 shirt = _color & 0xF0;
201 setcolor(pl, 16*pants + pants);
203 setcolor(pl, shirt + pants);
207 void SetPlayerTeam(entity pl, float t, float s, float noprint)
212 _color = NUM_TEAM_4 - 1;
214 _color = NUM_TEAM_3 - 1;
216 _color = NUM_TEAM_2 - 1;
218 _color = NUM_TEAM_1 - 1;
220 SetPlayerColors(pl,_color);
223 LogTeamchange(pl.playerid, pl.team, 3); // log manual team join
226 bprint(pl.netname, "^7 has changed from ", Team_NumberToColoredFullName(s), "^7 to ", Team_NumberToColoredFullName(t), "\n");
231 // set c1...c4 to show what teams are allowed
232 void CheckAllowedTeams (entity for_whom)
236 c1 = c2 = c3 = c4 = -1;
237 cb1 = cb2 = cb3 = cb4 = 0;
239 string teament_name = string_null;
241 bool mutator_returnvalue = MUTATOR_CALLHOOK(GetTeamCount, dm, teament_name);
242 teament_name = ret_string;
245 if(!mutator_returnvalue)
248 c1 = c2 = c3 = c4 = 0;
255 // find out what teams are allowed if necessary
258 entity head = find(world, classname, teament_name);
263 case NUM_TEAM_1: c1 = 0; break;
264 case NUM_TEAM_2: c2 = 0; break;
265 case NUM_TEAM_3: c3 = 0; break;
266 case NUM_TEAM_4: c4 = 0; break;
269 head = find(head, classname, teament_name);
273 // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
275 if(autocvar_bot_vs_human && for_whom)
277 if(autocvar_bot_vs_human > 0)
280 if(IS_BOT_CLIENT(for_whom))
288 if(IS_BOT_CLIENT(for_whom))
295 // if player has a forced team, ONLY allow that one
296 if(self.team_forced == NUM_TEAM_1 && c1 >= 0)
298 else if(self.team_forced == NUM_TEAM_2 && c2 >= 0)
300 else if(self.team_forced == NUM_TEAM_3 && c3 >= 0)
302 else if(self.team_forced == NUM_TEAM_4 && c4 >= 0)
306 float PlayerValue(entity p)
309 // FIXME: it always returns 1...
312 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
313 // teams that are allowed will now have their player counts stored in c1...c4
314 void GetTeamCounts(entity ignore)
318 // now count how many players are on each team already
320 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
321 // also remember the lowest-scoring player
323 FOR_EACH_CLIENT(head)
326 if(IS_PLAYER(head) || head.caplayer)
328 else if(head.team_forced > 0)
329 t = head.team_forced; // reserve the spot
332 if(head != ignore)// && head.netname != "")
334 value = PlayerValue(head);
335 if(IS_BOT_CLIENT(head))
374 // if the player who has a forced team has not joined yet, reserve the spot
375 if(autocvar_g_campaign)
377 switch(autocvar_g_campaign_forceteam)
379 case 1: if(c1 == cb1) ++c1; break;
380 case 2: if(c2 == cb2) ++c2; break;
381 case 3: if(c3 == cb3) ++c3; break;
382 case 4: if(c4 == cb4) ++c4; break;
387 float TeamSmallerEqThanTeam(float ta, float tb, entity e)
389 // we assume that CheckAllowedTeams and GetTeamCounts have already been called
391 float ca = -1, cb = -1, cba = 0, cbb = 0, sa = 0, sb = 0;
395 case 1: ca = c1; cba = cb1; sa = team1_score; break;
396 case 2: ca = c2; cba = cb2; sa = team2_score; break;
397 case 3: ca = c3; cba = cb3; sa = team3_score; break;
398 case 4: ca = c4; cba = cb4; sa = team4_score; break;
402 case 1: cb = c1; cbb = cb1; sb = team1_score; break;
403 case 2: cb = c2; cbb = cb2; sb = team2_score; break;
404 case 3: cb = c3; cbb = cb3; sb = team3_score; break;
405 case 4: cb = c4; cbb = cb4; sb = team4_score; break;
416 if(IS_REAL_CLIENT(e))
425 // keep teams alive (teams of size 0 always count as smaller, ignoring score)
441 // the more we're at the end of the match, the more take scores into account
442 f = bound(0, game_completion_ratio * autocvar_g_balance_teams_scorefactor, 1);
449 // returns # of smallest team (1, 2, 3, 4)
450 // NOTE: Assumes CheckAllowedTeams has already been called!
451 float FindSmallestTeam(entity pl, float ignore_pl)
456 // find out what teams are available
457 //CheckAllowedTeams();
459 // make sure there are at least 2 teams to join
461 totalteams = totalteams + 1;
463 totalteams = totalteams + 1;
465 totalteams = totalteams + 1;
467 totalteams = totalteams + 1;
469 if((autocvar_bot_vs_human || pl.team_forced > 0) && totalteams == 1)
474 if(autocvar_g_campaign && pl && IS_REAL_CLIENT(pl))
475 return 1; // special case for campaign and player joining
477 error(sprintf("Too few teams available for %s\n", MapInfo_Type_ToString(MapInfo_CurrentGametype())));
480 // count how many players are in each team
484 GetTeamCounts(world);
486 RandomSelection_Init();
489 if(TeamSmallerEqThanTeam(2, t, pl))
491 if(TeamSmallerEqThanTeam(3, t, pl))
493 if(TeamSmallerEqThanTeam(4, t, pl))
496 // now t is the minimum, or A minimum!
497 if(t == 1 || TeamSmallerEqThanTeam(1, t, pl))
498 RandomSelection_Add(world, 1, string_null, 1, 1);
499 if(t == 2 || TeamSmallerEqThanTeam(2, t, pl))
500 RandomSelection_Add(world, 2, string_null, 1, 1);
501 if(t == 3 || TeamSmallerEqThanTeam(3, t, pl))
502 RandomSelection_Add(world, 3, string_null, 1, 1);
503 if(t == 4 || TeamSmallerEqThanTeam(4, t, pl))
504 RandomSelection_Add(world, 4, string_null, 1, 1);
506 return RandomSelection_chosen_float;
509 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
511 float smallest, selectedteam;
513 // don't join a team if we're not playing a team game
517 // find out what teams are available
518 CheckAllowedTeams(pl);
520 // if we don't care what team he ends up on, put him on whatever team he entered as.
521 // if he's not on a valid team, then let other code put him on the smallest team
524 if( c1 >= 0 && pl.team == NUM_TEAM_1)
525 selectedteam = pl.team;
526 else if(c2 >= 0 && pl.team == NUM_TEAM_2)
527 selectedteam = pl.team;
528 else if(c3 >= 0 && pl.team == NUM_TEAM_3)
529 selectedteam = pl.team;
530 else if(c4 >= 0 && pl.team == NUM_TEAM_4)
531 selectedteam = pl.team;
537 if(!only_return_best)
539 SetPlayerColors(pl, selectedteam - 1);
541 // when JoinBestTeam is called by client.qc/ClientKill_Now_TeamChange the players team is -1 and thus skipped
542 // when JoinBestTeam is called by cl_client.qc/ClientConnect the player_id is 0 the log attempt is rejected
543 LogTeamchange(pl.playerid, pl.team, 99);
547 // otherwise end up on the smallest team (handled below)
550 smallest = FindSmallestTeam(pl, true);
552 if(!only_return_best && !pl.bot_forced_team)
554 TeamchangeFrags(self);
557 SetPlayerColors(pl, NUM_TEAM_1 - 1);
559 else if(smallest == 2)
561 SetPlayerColors(pl, NUM_TEAM_2 - 1);
563 else if(smallest == 3)
565 SetPlayerColors(pl, NUM_TEAM_3 - 1);
567 else if(smallest == 4)
569 SetPlayerColors(pl, NUM_TEAM_4 - 1);
573 error("smallest team: invalid team\n");
576 LogTeamchange(pl.playerid, pl.team, 2); // log auto join
578 if(pl.deadflag == DEAD_NO)
579 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE.m_id, pl.origin, '0 0 0');
585 //void() ctf_playerchanged;
586 void SV_ChangeTeam(float _color)
588 float scolor, dcolor, steam, dteam; //, dbotcount, scount, dcount;
590 // in normal deathmatch we can just apply the color and we're done
592 SetPlayerColors(self, _color);
596 scolor = self.clientcolors & 0x0F;
597 dcolor = _color & 0x0F;
599 if(scolor == NUM_TEAM_1 - 1)
601 else if(scolor == NUM_TEAM_2 - 1)
603 else if(scolor == NUM_TEAM_3 - 1)
605 else // if(scolor == NUM_TEAM_4 - 1)
607 if(dcolor == NUM_TEAM_1 - 1)
609 else if(dcolor == NUM_TEAM_2 - 1)
611 else if(dcolor == NUM_TEAM_3 - 1)
613 else // if(dcolor == NUM_TEAM_4 - 1)
616 CheckAllowedTeams(self);
618 if(dteam == 1 && c1 < 0) dteam = 4;
619 if(dteam == 4 && c4 < 0) dteam = 3;
620 if(dteam == 3 && c3 < 0) dteam = 2;
621 if(dteam == 2 && c2 < 0) dteam = 1;
623 // not changing teams
626 //bprint("same team change\n");
627 SetPlayerTeam(self, dteam, steam, true);
631 if((autocvar_g_campaign) || (autocvar_g_changeteam_banned && self.wasplayer)) {
632 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_TEAMCHANGE_NOTALLOWED);
633 return; // changing teams is not allowed
636 // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
637 if(autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
640 if(!TeamSmallerEqThanTeam(dteam, steam, self))
642 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_TEAMCHANGE_LARGERTEAM);
647 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
649 if(IS_PLAYER(self) && steam != dteam)
651 // reduce frags during a team change
652 TeamchangeFrags(self);
655 // since this is an engine function, and gamecode doesn't have any calls earlier than this, do the connecting message here
657 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_CONNECTING, self.netname);
659 SetPlayerTeam(self, dteam, steam, !IS_CLIENT(self));
661 if(IS_PLAYER(self) && steam != dteam)
663 // kill player when changing teams
664 if(self.deadflag == DEAD_NO)
665 Damage(self, self, self, 100000, DEATH_TEAMCHANGE.m_id, self.origin, '0 0 0');
669 void ShufflePlayerOutOfTeam (float source_team)
671 float smallestteam, smallestteam_count, steam;
672 float lowest_bot_score, lowest_player_score;
673 entity head, lowest_bot, lowest_player, selected;
676 smallestteam_count = 999999999;
678 if(c1 >= 0 && c1 < smallestteam_count)
681 smallestteam_count = c1;
683 if(c2 >= 0 && c2 < smallestteam_count)
686 smallestteam_count = c2;
688 if(c3 >= 0 && c3 < smallestteam_count)
691 smallestteam_count = c3;
693 if(c4 >= 0 && c4 < smallestteam_count)
696 smallestteam_count = c4;
701 bprint("warning: no smallest team\n");
707 else if(source_team == 2)
709 else if(source_team == 3)
711 else // if(source_team == 4)
715 lowest_bot_score = 999999999;
716 lowest_player = world;
717 lowest_player_score = 999999999;
719 // find the lowest-scoring player & bot of that team
720 FOR_EACH_PLAYER(head)
722 if(head.team == steam)
726 if(head.totalfrags < lowest_bot_score)
729 lowest_bot_score = head.totalfrags;
734 if(head.totalfrags < lowest_player_score)
736 lowest_player = head;
737 lowest_player_score = head.totalfrags;
743 // prefers to move a bot...
744 if(lowest_bot != world)
745 selected = lowest_bot;
746 // but it will move a player if it has to
748 selected = lowest_player;
749 // don't do anything if it couldn't find anyone
752 bprint("warning: couldn't find a player to move from team\n");
756 // smallest team gains a member
757 if(smallestteam == 1)
761 else if(smallestteam == 2)
765 else if(smallestteam == 3)
769 else if(smallestteam == 4)
775 bprint("warning: destination team invalid\n");
778 // source team loses a member
783 else if(source_team == 2)
787 else if(source_team == 3)
791 else if(source_team == 4)
797 bprint("warning: source team invalid\n");
801 // move the player to the new team
802 TeamchangeFrags(selected);
803 SetPlayerTeam(selected, smallestteam, source_team, false);
805 if(selected.deadflag == DEAD_NO)
806 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE.m_id, selected.origin, '0 0 0');
807 Send_Notification(NOTIF_ONE, selected, MSG_CENTER, CENTER_DEATH_SELF_AUTOTEAMCHANGE, selected.team);